New work: autorotate. As I mentioned earlier, I managed to score a brand new Microsoft Surface Pro 3 for $230 (not including the additional cost of a new keyboard, stylus, and power supply... sigh) from a woman who much preferred her MacBook and was moving and had to get rid of everything she didn't want to carry. I even checked with Microsoft to see if it was registered as stolen, and bought it on the spot.

If you see my earlier post about getting it working, you can see that I liked Ayko Poel's autorotate script. And it's true, I did... until I bought a stylus and tried to draw on the thing in portrait mode. Poel's scripts broke. There's a bit of deep, almost occult esoterica about mapping screen touches to drawing coordinates, especially when rotating the screen, and Poel's scripts failed at the intersection of advanced touch devices (the pen and the eraser) and coordinate transformation.

So I fixed it.

Along the way, I removed the huge horkin' if statement and replaced it with a transformation matrix which encapsulates all the rules quite nicely. It actually doesn't change the line count (sadly): Detecting if you're in landscape or portrait is two expressions, and which landscape or portrait is two more expressions each, followed by a four-line table that maps these rules to transformations and labels. But I think it's a hell of a lot more maintainable. I also fixed the palm rejection code (which is now a heck of a lot shorter) to understand that the pen and eraser both require palm rejection.

I changed device recognition to be a heck of a lot more robust, and I used Python's namedtuple to make this all much more readable. I used a lot of regular expressions— but then, I've been good at regular expressions since 1993— and that lets me recognize the devices, the drivers, and everything else without any fiddling or meddling.