Adding auto-rotation to laptop screen

I've recently bought an HP Spectre x360 convertible laptop, and while everything worked perfectly out of the box in Linux (wifi, bluetooth, touchscreen...) there was just one thing that kept bugging me: the screen auto-rotation feature didn't work.

This meant that "tent mode" wasn't usable (not that I find it very useful, but still), but most importantly that when being held in "tablet mode" the screen remained in landscape view, even though portrait is usually a better fit for browsing the Internet or viewing documents in general (maybe even vertical videos?).

After searching for scripts to fix this on Internet and failing to get the few I found to work, I decided that writing my own would be the fastest solution. I used iio-sensor-proxy to get the inclination angles and xrandr to set the screen orientation.

The python script is actually very simple, it gets the screen's pitch and roll angles and compares them to see which orientation suits the current position better.

The set of diagrams shows the reasoning behind the script. Taking the equation lines y=x and y=-x as the limits, each possible position follows one of the inequalities. In order to increase the hysteresis of the system a buffer zone of ±α is added around the limits, and the inequalities are updated. Some simplification yields the last diagram. After reading the sensors I found out that the pitch angle was positive when the laptop was in its natural position and negative when upside down, and the roll angle was positive when turned clockwise and negative otherwise. This gave the following orientations:
  • If |y| < |x|-α :
    • If x > 0 :  Normal
    • If x < 0 :  Inverted
  • If |y| > |x|+α :
    • If y > 0 :  Left
    • If y < 0 :  Right
In the following video we can see the script changing the screen orientation with the angle, and how texts are a better fit in portrait mode.

4 thoughts on “Adding auto-rotation to laptop screen

  1. Néstor Reply

    There is a mode to make it run at startup, just like in Windows? In Windows, the login can rotate, also.

    • Alvaro Post authorReply

      Hi, you can make it run at startup by adding it to the “Startup Applications” program or adding it to /etc/init.d.
      I think doing the latter should allow you to rotate the login as well, although I have since reinstalled Ubuntu and autorotation now works out of the box, login included, so you could give that a try as well.

  2. Chris Reply

    First, I want to say that I find this script simply Awesome! Thanks so much for writing it.
    I have just received my x360 convertible so this is very cool.

    Perhaps you can point me (and others) in the right direction for a few other items …

    You mentioned that you reinstalled Ubuntu. What version and desktop did you install?
    (I currently have Linux Mint 18.3 Cinnamon on mine and your script works nicely.)

    Can you get a keyboard to pop up on the screen in order to type in a URL or do email in tablet mode?

    My x360 came with a stylus from HP … have you used one (or heard about using one) in a linux environment?

    One of the things I often find myself doing on my iPad is taking screen snapshots by pressing physical external buttons on the the tablet. Any ideas on how to do screen snapshots when the x360 is in tablet mode?

    Thanks again!

    • Alvaro Post authorReply

      Hi, thanks a lot! I really appreciate the feedback 🙂
      As for the other questions:

      – I installed Ubuntu 17.04 with the 4.12 kernel and Gnome 3.24. I chose Gnome because it offered the best touchscreen experience at the time, which I’m guessing it’s still the case.

      -The keyboard pops up automatically when you click on a writable field on tablet mode, but you can make it appear at any time by swiping up from the bottom of the screen.

      – I didn’t get the HP stylus but I have tried the Surface Pen (v3 I think) and it worked automatically without pairing. There is a test zone in Gnome’s settings specifically designed for styli where you can try yours. Moving the cursor on hover and pressing to change the thickness are also supported.

      -In theory you should be able to detect a combination of button presses to trigger a screenshot, but it’s easier to just add the native screenshot app to the dock and click on it when needed. You can then capture the whole screen, just a window or a specific zone.

Leave a Reply

Your email address will not be published. Required fields are marked *