ThinkPad X61 Tablet Automatic Screen Rotation Under Linux

Karol Krizka @ February 13, 2008

Linux

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

Did you enjoy this post? Then why not subscribe to my RSS feed or subscribe by e-mail? Also check out the many other FREE ways to appreciate a blogger.


Related Posts

RSS feed | Trackback URI

13 Comments »

2008-02-14 11:23:04

[...] http://www.krizka.net/2008/02/13/thinkpad-x61-tablet-automatic-screen-rotation-under-linux/  Socialize it: Share this post with others. Posted by md on February 14th, 2008 | Filed in Desktop, Linux, Open Source Software, Technology, Ubuntu | [...]

 
Comment by Y-TeaM
2008-02-20 11:00:57

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

Comment by Karol Krizka
2008-02-21 14:13:06

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?

 
 
Comment by Y-TeaM
2008-02-21 14:59:36

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.

 
Comment by Niklas Subscribed to comments via email
2008-03-08 13:42:25

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.

 
2008-04-28 19:41:26

[...] installed Karol Krizka’s automatic rotation script.  I am not yet sure if I am a fan of the automatic rotation rather than [...]

 
Comment by thinkmassive Subscribed to comments via email
2008-05-10 12:12:03

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!

 
Comment by TerraMedicX Subscribed to comments via email
2008-05-10 15:28:02

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

 
Comment by Mathias Subscribed to comments via email
2008-05-22 02:32:06

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

 
Comment by Mathias Subscribed to comments via email
2008-05-28 09:53:14

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.

Comment by Karol Krizka
2008-05-29 06:49:56

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. ;)

 
 
Comment by thinkmassive Subscribed to comments via email
2008-05-29 05:40:51

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?

 
Comment by Mathias Subscribed to comments via email
2008-05-29 06:29:26

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

 
Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.

Feed