Skip to content
Feb 13 / kkrizka

ThinkPad X61 Tablet Automatic Screen Rotation Under Linux

Check out my AutoRotate project for a more up-to-date and featureful version of this script.

One advantage of a tablet laptop is that you can position and rotate it any way you want. For example, while I ride on the bus I like to hold my ThinkPad x61 in the portrait orientation to maximize the display height of Google Reader that I am looking over. Since X.Org allows you to rotate your display orientation through the xrandr extension, this isn’t much of a problem. However things could be improved, because there is no way to rotate your screen automatically based on the physical orientation of your laptop. I have created a script that will do this by reading information from the X61 hard drive accelerometer and rotate the screen, fix the mouse pointer orientation and change the direction of the arrow buttons on the display. It is based on twalson’s x61 rotate script that alternates between tablet and laptop modes, so quite a lot of the credit goes to him. Note that I have made quite a lot of changes, so the script should not be considered as stable. If you run into any trouble, just leave a comment or contact me directly. In the rest of the post I will document how to install the script on Ubuntu Hardy Heron, but the script should work on any other Linux distribution too.

Features

  • Automatic screen rotation based on orientation when in tablet mode
  • Rotation is fixed and disabled when in laptop mode
  • Fixes orientation of the mouse buttons and wacom stylus
  • Minimum treshhold for change of rotation so small deviations won’t change the screen

Installation Instructions

  1. Make sure you have a working accelerometer that is reading the proper orientation as tested using the hdaps-gl utility.
  2. Add the python-xrandr PPA repositry (works on Hardy Heron too) by appending the following line to the end of the /etc/apt/sources.list file:
    deb http://ppa.launchpad.net/displayconfig-gtk/ubuntu gutsy main
  3. Update apt-get’s package list to include python-xrandr:
    sudo apt-get update
  4. Install the necessary packages for the script to work:
    sudo apt-get install wacom-tools python-xrandr
  5. Download the autorotate.py script and place it in the /usr/local/bin directory. Make sure it has execute permissions by running the following command:
    sudo chmod +x /usr/local/bin/autorotate.py
  6. Create a file to hold the information about the current screen mode (laptop vs tablet):
    sudo touch /etc/tabletmode
  7. Create a file /etc/acpi/events/swivel-up with the following content:
    # called when tablet screen swivels up (into laptop mode)
    event=ibm/hotkey HKEY 00000080 0000500a
    action=echo laptop > /etc/tabletmode
  8. Create a file /etc/acpi/events/swivel-down with the following content:
    # called when tablet screen swivels down (into tablet mode)
    event=ibm/hotkey HKEY 00000080 00005009
    action=echo tablet > /etc/tabletmode
  9. Restart ACPI to register the events:
    sudo /etc/init.d/acpid restart
  10. Have autorotate.py autostart on GDM. This way it will be available to all users, even during login! This is done by adding the following line to the end of the /etc/gdm/Init/Default file before the exit 0 command:
    /usr/local/bin/autorotate.py&
  11. Restart GDM to start the script:
    sudo /etc/init.d/gdm restart
  12. Swivel down your screen into the tablet mode, and tilt it at around 45 degrees to test if the script works!

Debugging

It is possible that you run into some problems, especially if you are missing some dependencies or have other problems. You can get error messages by running the script manually using the following command. Note that you do need to run it with sudo, because the wacom tool needs root privilages.

sudo /usr/local/bin/autorotate.py

41 Comments

Leave a comment
  1. Y-TeaM / Feb 20 2008

    Dear Karol,

    I got to a point where

    sudo /usr/local/bin/autorotate.py

    would return

    Traceback (most recent call last):
    File “/usr/local/bin/autorotate.py”, line 92, in
    main()
    File “/usr/local/bin/autorotate.py”, line 45, in main
    yCal=int(m.group(2))
    ValueError: invalid literal for int() with base 10: ”

    Seems like the part

    f=open(‘/sys/devices/platform/hdaps/calibrate’,’r’)
    line=f.readline().replace(“n”,””).replace(“r”,””);
    m=re.search(‘.([0-9]*),([0-9]*).’,line);
    xCal=int(m.group(1))
    yCal=int(m.group(2))

    returns a blank as a value for yCal.

    Contents of calibrate file read

    (-502,-503)

    I experimented with modifying the script, but I don’t know Python, so I can’t get it right…

    Y

    • Karol Krizka / Feb 21 2008

      Thanks for telling me this. What version of Python are you running? I am running python 2.5, so there might be some kind of a API change?

  2. Y-TeaM / Feb 21 2008

    I’m running Ubuntu Gutsy, so it’s 2.5 too.
    As I said, don’t know the workings of Python, just took a look at it to see where it hangs.

    But now I just ran it again, and it gets past line 45 and returns the same kind or error from line 62.

    Traceback (most recent call last):
    File “/usr/local/bin/autorotate.py”, line 92, in
    main()
    File “/usr/local/bin/autorotate.py”, line 62, in main
    x=int(m.group(2))-xCal
    ValueError: invalid literal for int() with base 10: ”

    Tried again and it’s back to error on line 45..!

    I’ll retrace my steps and see if I get it to work that way.

  3. Niklas / Mar 8 2008

    I had the same problem when configuring auto-rotate on my brothers x41 Tablet. The problem appeared to be that the regexp didn’t take negative values into account.
    Replace “m=re.search(‘.([0-9]*),([0-9]*).’,line)” with “m=re.search(‘.(-?[0-9]*),(-?[0-9]*).’,line)” on lines 43 and 61 in autorotate.py.
    The “-?” will match a ‘-‘ before the number if it exists.

    Although using regexp for this feels a bit wasteful, there has to be an easier way of getting these values directly from the hdaps driver. I might just examine the source of hdaps-pivot and I’ll write here again if I find a better way.

    • Renato / Mar 19 2009

      Thanks, it worked for me.

  4. thinkmassive / May 10 2008

    Thank you very much for the autorotate script. I’ve noticed a few bugs that I would like to fix, but my Python experience is limited and I’ve only created scripts for cronjobs and automated builds.

    My configuration is Xubuntu 8.04 on a ThinkPad X61 Tablet (Intel GMA X3100, 1024×768 multitouch, Wacom serial stylus). Sometimes my resolution seems to be reduced when gdm starts, and I think it correlates with the orientation during startup. In xorg.conf I have Screen/Display set to a single mode “1024×768” and a Virtual 1024 1024. Has anyone else seen this behavior?

    It also doesn’t seem to sense when the screen is rotated and locked in tablet mode, as opposed to laptop mode. Is there a way to test for this?

    Keep up the great work!

  5. TerraMedicX / May 10 2008

    Trying to get autorotate working when I run it manually I get:
    /usr/local/bin/autorotate.py: 1: Syntax error: newline unexpected

    I’m running Hardy Heron on an X61t. lsmod shows that the tp_smapi and hdaps are both loaded.

    Thanx

  6. Mathias / May 22 2008

    would somebody please email me the autorotate.py script to nitehoaxxer@gmx.net? Server seems down an I really need it – thx

  7. Mathias / May 28 2008

    Hi,
    I got the script working. Is this correct, that in tablet mode (echo tablet > /etc/tabletmode) there are only 2 states of rotation? The one brings the top of gnome to the tablet keys (Power, Rotate, Lock, Esc and the arrow keys) and the other one brings gnome’s left edge to the tablet keys.

    Is this intended to be like this? I thought it would rotate according to however the screen is positioned.

    • Karol Krizka / May 29 2008

      The values in tabletmode of my script hold only two values:
      tablet: the screen is tilted down, so enable automatic screen rotation based on position
      laptop: screen is up, do not rotate the screen even if tilted.

      I will have time after Saturday to debug the script a little bit, as I’ve noticed some people have problems with it.

      In the meantime, come to SFU Open House on Saturday if you live in the Vancouver area. 😉

  8. thinkmassive / May 29 2008

    Mathias: I observed similar behavior but only intermittently. Running the script manually prints the rotation position to the console, and sometimes I get all 4 values while other times only 2 values. What model is your tablet?

  9. Mathias / May 29 2008

    right, I tried it and experienced the same effect you described. My model is X61 Tablet – more information needed?

  10. Joe / Jul 25 2008

    Thanks so much for writing this! It’s really slick.

    I had to make 2 small changes to get things to work on my X61t with Kubuntu Hardy.

    First, in the /etc/acpi/events/swivel-* files, I had to remove the quotes from the action= lines, ie instead of this

    action="echo laptop > /etc/tabletmode"

    I needed this:

    action=echo laptop > /etc/tabletmode

    because I was getting “unknown command” errors from the shell.

    Second, since I’m using KDE, I added the line

    /usr/local/bin/autorotate.py &

    to the file /etc/kde3/kdm/Xstartup, to get the script to run automatically when I login. I added it right before the line that says “exec”.

  11. Mathias / Aug 2 2008

    Hi everybody,

    I ported the functionality to a bash script – including the rotation of the arrow keys.

    Visit my homepage: http://javafalke.de to download the script.

    BUT: the script needs sudo privileges to be able to set the arrow keys correctly.

    cu
    Mathias

  12. Dan CaJacob / Aug 18 2008

    Hi,

    I am having trouble with the script. It works rotating the screen very well, but I cannot activate anything with my pen. The cursor moves, but I can’t open the “Applications” menu in tablet mode, for example. I am running Ubuntu 8.04 I applied the fix for the negative calibration values referred to in the comments and in doing so, noticed that both my /sys/devices/platform/hdaps/position and /sys/devices/platform/hdaps/calibrate files are empty! What would cause this? I assume that this is what is causing my problem. BTW, I get no error when I run autorotate.py – but when I kill it with Ctrl-C, I get:

    Traceback (most recent call last):
    File “/usr/local/bin/autorotate.py”, line 92, in
    main()
    File “/usr/local/bin/autorotate.py”, line 48, in main
    time.sleep(0.1);
    KeyboardInterrupt

    Thanks!

    • Karol Krizka / Aug 19 2008

      A few questions:
      1) Can you use the pen without my rotation script running?
      2) Have you loaded the hdaps_ec module? If not, then that would explain why the calibrate and position files are empty.

      Finally, the error you experience when you kill the script is just a standard message from python for keyboard interrupts. It just means that you didn’t quit the problem cleanly, but killed it. However, that should not make any difference with my rotate script, so don’t worry about it. 🙂

      • Raphael Kraus / Aug 25 2008

        Hi Karol,

        I have the same problem as Dan. To answer your questions:

        1) Yes, the pen can be used without the scripts running.
        2) Yes, the modules are loaded.

        No, autorotate.py doesn’t produce any errors either. 🙁

        I find that I can change the screen orientation just by tilting the laptop irregardless of whether the screen is up (regular laptop) or down (tablet).

        All the best,

        Raphael

        • Dan CaJacob / Sep 19 2008

          I found the solution:

          just turn off compiz. Use metacity instead. You could build this into your script even: i.e. when the user presses a button to rotate the screen or the tablet senses the screen flip, it reverts to metacity.

    • Sőrés Viktor / Sep 7 2008

      Had the same problem, so I wrote a simpler Python script myself for debugging. This one only rotates on swivel events, no hdaps or arrow key correction is used. I observed this: if I switch to Metacity (shut down Compiz) everything works fine. I guess Compiz has troubles with vertical rotation. (Mouse is on the X Window level, that’s why it works)

  13. Pavan Kandepet / Sep 6 2008

    Thanks for the great script. I used to have a few
    buttons on my desktop panel to execute some xrandr commands to get this done. You just made my life easier and some friends jealous.
    -Pavan

  14. nicolas roy / Oct 15 2008

    Hello, i encounter the following issue, already mentioned by others : i have (apparently randomly) sometimes 4 available rotations, and sometimes only 2. Did someone managed to fix this ?

    Thanks

    Nicolas

  15. nicolas roy / Oct 15 2008

    Hello !

    Another remark to the script. If i am in tablet position, then the file /etc/tabletmode contains “tablet”. If now if switch off the computer, then turn the screen back in laptop position and switch on, this file is not “updated”, and therefore the orientation of the screen follows the rules of the tablet mode, instead of the the tablet mode.
    I understand that the acpi events “swiwel-up/down” detects when when there is a change of position of the screen. But isn’t there any to read the actual position (closed or open) of the screen, that one could use at boot to initialize correctly the file /etc/tabletmode ?

    • Sőrés Viktor / Oct 15 2008

      I’m sure there is, because Windows Vista can do it. I was however unable to find any info about it.

    • Karol Krizka / Oct 16 2008

      Hi,

      I am familiar with this problem. As far as I know, it is not possible to detect the position of the screen under Linux. I did some research, but it turned up nothing. 🙁 However as Sőrés says, it is possible under Windows, so there is hope that this will be possible under Linux in the future.

      • Karl Hegbloom / Nov 3 2008

        /sys/devices/platform/thinkpad_acpi/hotkey_tablet_mode

        • Karol Krizka / Nov 18 2008

          I don’t have that file on Hardy Heron… But if you have it, that means it is probably in the latest version of acpi. That is great!

          I will try to compile the latest tp_smapi package later today and see if it’s there.

  16. jagdfalke / Oct 16 2008

    @Karol:
    Any suggestions regarding the availability of sometimes 4 sometimes 2 rotation modes? I couldn’t really figure out what is happening in those cases.

    • nicolas roy / Oct 19 2008

      Hej !

      Maybe some hints to start debugging this problem :

      * I’m sure i remember, there was a time with sometimes 4 sometimes 2 rotation modes. But for some time now, i get only 2.
      * if i boot, login in gdm, then i have only 2 rotation mode
      * They seem to be “very sensitive” (i mean, i don’t need to orientate more than 45°, in order to change the mode)
      * Then i do the following :
      $ ps ax | grep autorotate
      5837 ? S 0:00 /usr/bin/python /usr/local/bin/autorotate.py
      $ sudo kill -9 5837
      $ sudo /usr/bin/python /usr/local/bin/autorotate.py
      * and now, it works correctly.

      Nicolas

  17. Matt / Nov 11 2008

    So I had the same problem with the screen not refreshing. I could move the mouse, but not select anything. Rotating the display from the terminal I had the same problem, but I could type terminal commands to reset the display to normal orientation and restore full functionality.

    Turns out if I change my appearance settings to none either through the menu or activating metacity with “metacity –replace” everything works great, I just don’t have all the pretty visual effects. I’m ok with this if it is limited to tablet mode, but I’d like to incorporate this behavior into the script. I have two problems.

    First is that I am totally unfamiliar with python. I imagine I would add a command just after the #tabletmode comment, but I don’t know what command to add.

    Second, I don’t know how to revert to normal visual settings mode from terminal (this is my first week on linux). If I type compiz into the terminal after using “metacity –replace” I get all kinds of problems and have to restart. I’m guessing that this is a separate issue I’ll need to work out, but I was wondering if maybe there is some other command I should be using to reset the visual effects.

    Thanks for the help and extremely useful script

    • Karol Krizka / Nov 18 2008

      Sorry, but I am not sure why compiz is acting up on you. What version (distro/release?) of Linux are you using? If it is not the latest release, I would suggest upgrading to it. I have no trouble with rotation on Ubuntu Hardy Heron and Intrepid while having my windows wobbling.

      • Frames / Dec 4 2008

        “I have no trouble with rotation on Ubuntu Hardy Heron and Intrepid”

        I got stuck with the need to install python-xrandr. The repo you suggest is for hardy, there’s nothing newer and I cannot find another repo with that package for intrepid.

        Can you please let me know how you did this in intrepid?

        Thanks!

  18. Denis / Feb 6 2009

    Great work, but I have 2 questions:

    1 – Does this modification work on Xandros, the OS used on EEE PC?

    2 – Could I use any accelerometer?

  19. jones / Feb 10 2009

    hi karol
    first,sorry for my bad english!^^
    thx to create this script,i try to use it,but get some trouble!
    1.i just install mint6(base on intrepid) 64bit in my x61 tablet,python version:2.5.2
    ,acpi work well(it can auto write /etc/tabletmode),but i found the script(autorotate.py) cann’t run in this surrounding,just you say,maybe something need to fix,then i use Mathias’s script(http://javafalke.de/index.php?menu_id=7),change some word,it can work well!
    but i want to know how to fix in your script.thx^^
    2.do you know how to mapping the rotation button on tablet to work?

  20. Renato / Mar 14 2009

    Thank you! It was very helpful!

  21. Saint DanBert / Aug 24 2010

    I have been using Karl Hegbloom’s package
    [code]
    dpkg –list | grep “rotation-”
    ii tablet-screen-rotation-support 0.1.10 Support for laptop/tablet screen rotation
    [/code]
    to handle this issue. It works well and seems to remove dependence on “hacks”.

    I’m running Ubuntu Lucid (v10.04 LTS) so I cannot comment on what happens with older
    editions.

    ~~~ 0;-Dan

Trackbacks and Pingbacks

  1. Very cool: automatic screen rotation with ThinkPad X61 tablet
  2. Hardy reinstall results « akth
  3. aliencam dot net blog. » Blog Archive » Ubuntu Setup Guide -Tilt Control and Automatic Rotation
  4. AutoRotate 0.3 Released | Karol Krizka
  5. Is there a tablet-centric desktop available for Ubuntu? Drija
Leave a comment
Cancel reply