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.

AutoRotateScreen
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.

11 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.

  3. Keith R Miller Reply

    Hi, I am wondering which version of the 360 you are using? I am looking at buying the HP Pavilion x360 15.6 with i-5 8250U and having some confusion verifying that I can in fact install linux on it.

    • Alvaro Post authorReply

      Hi, I have the HP Spectre x360 13t with an i7-7500U, but I don’t think that would matter very much, in my experience I’ve always been able to install linux in all the computers I’ve owned 🙂

  4. Ian Reply

    Hi Thanks rotate works great, but need a bit of help please.
    The screen rotate is great, but I can’t get the screen touch correct.
    I tried removing the comment from the touch line. Not sure if I did the correct move.

    # Enable touchpad rotation by uncommenting the following line.
    touchpad = “SynPS/2 Synaptics TouchPad”he screen touch.

    I was hoping thew above referred to the touch screen not the touch pad.
    Below is the message I get in terminal.

    oldkarew@oldkarew-HP-Spectre-x360-Convertible-13-ap0xxx:~/Downloads$ python ARS.py
    unable to find device SynPS/2 Synaptics TouchPad
    Traceback (most recent call last):
    File “ARS.py”, line 46, in
    ‘Coordinate Transformation Matrix’] + transval.split())
    File “/usr/lib/python2.7/subprocess.py”, line 190, in check_call
    raise CalledProcessError(retcode, cmd)
    subprocess.CalledProcessError: Command ‘[‘xinput’, ‘set-prop’, ‘SynPS/2 Synaptics TouchPad’, ‘Coordinate Transformation Matrix’, ‘1’, ‘0’, ‘0’, ‘0’, ‘1’, ‘0’, ‘0’, ‘0’, ‘1’]’ returned non-zero exit status 1
    oldkarew@oldkarew-HP-Spectre-x360-Convertible-13-ap0xxx:~/Downloads$

    I’m running Dual Boot Ubuntu 18.04.2 with Win10 (Big problems with Win10 NOT shutting down correctly, Error 41)
    If screen rotate with touch rotate can work for me, I’ll dump dual boot and just run Ubunto 18.04.2

    Any help much appreciated.

    • Alvaro Post authorReply

      Hi, I’m not sure I understand what you’re trying to do.

      That line refers to the touchpad, so that the cursor moves with the same rotation as the screen (otherwise when the laptop is upside down the mouse goes up when you move your finger down, instead of up).
      The problem is that the touchpad isn’t being found, please type “xinput list” to find the correct name for yours and replace it in the script. I also use Python3, I haven’t tested it with 2.7, just in case.

      I’m just not sure what you mean with “can’t get the screen touch correct”

  5. Alessandro Vernassa Reply

    I make this script with work in HP-Spectre-x360-15-df0009nl on kubuntu 19.4
    allows autorotation and OLED screen brightness change.

  6. Guy Reply

    sounds amazing ! where can i download the script to try ?

    thank you

    • Alvaro Post authorReply

      Thanks! The code is in the GitHub repository, the link is just under the video

Leave a Reply

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