New work: Autorotate for the MS Surface Pro 3 running Linux & Krita

Posted by Elf Sternberg as programming, python

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.

3 Responses to New work: Autorotate for the MS Surface Pro 3 running Linux & Krita

Greg Bell

November 18th, 2017 at 2:00 pm

Hi Elf,

Holey moley that’s cleaner. I’m giving it a good go-over to learn from it.

Named tuples – good tip.

You’re using enough os.system calls that you might really like https://amoffat.github.io/sh/

My only confusions/criticism (if I may be so bold) is that I have no idea what lsn, lsx, lsi, ptx, ptl, and ptr stand for. ls = landscape, pt = portrait, sure, but n, x, i, and r? Maybe if I had more domain knowledge I’d know right way.

Also, the magic numbers – 65000, 64700, 64500, 64000, 1100, 1000, 800, 650…

Thanks for sharing that! Good stuff. I always learn something new when reading others’ Python code.

Elf Sternberg

February 12th, 2018 at 7:23 pm

I’m so sorry for not noticing this sooner! I = Inverted; N = Natural; LS = Landscape ; PT = Portrait, L = Left, R = Right! So LSX is “Landscape, read the X component to tell us if we’re in Landscape mode,” and “PTX” is “Portrait, which is just not Landscape,” etc. etc.

The numbers are basically thresholds read on a counter. I got them from the original; I suspect the numbers are ranges around a register of 65536 (2^16), which would make sense on a 16-bit machine. That’s why the Y checks in Portrait mode are also around that range. The X checks for Landscape mode are a little weirder; maybe Ayko did some empirical “If I hold it with the Windows button on the LEFT, the X register reads…” and went from there.

Also: For some reason the original uses Left to describe something X calls Right, and I left that as is.

Hoshi Inoue

March 7th, 2019 at 5:56 am

I don’t know how to do more than this.
The current script is broken because the stylus does not get mapped properly.

You need to have this line

xsetwacom set “NTRG0001:01 1B96:1B05 Pen stylus” Rotate ccw

and this one

xsetwacom set “NTRG0001:01 1B96:1B05 Pen stylus” Rotate cw

without these lines the stylus does not get mapped in portrait orientations.

I would add them myself if I understood python better but I can’t figure it out.

Please add these lines in the appropriate places.

Comment Form

Subscribe to Feed



November 2017
« Oct   Jan »