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

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

Comment by Renato
2009-03-19 20:01:12

Thanks, it worked for me.

 
 
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?

 
Comment by Joe Subscribed to comments via email
2008-07-25 13:11:31

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

 
Comment by Mathias Subscribed to comments via email
2008-08-02 06:47:47

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

 
Comment by Dan CaJacob Subscribed to comments via email
2008-08-18 17:11:45

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!

Comment by Karol Krizka
2008-08-19 12:46:22

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

Comment by Raphael Kraus Subscribed to comments via email
2008-08-25 17:46:31

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

(Comments wont nest below this level)
Comment by Dan CaJacob Subscribed to comments via email
2008-09-19 11:33:21

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.

 
 
 
Comment by Sőrés Viktor Subscribed to comments via email
2008-09-07 22:30:16

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)

 
 
Comment by Pavan Kandepet
2008-09-06 10:56:19

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

 
2008-09-26 00:20:15

[...] information. I did not know how to do the accelerometer or screen rotation before reading these. http://www.krizka.net/2008/02/13/thinkpad-x61-tablet-automatic-screen-rotation-under-linux/ [...]

 
Comment by nicolas roy Subscribed to comments via email
2008-10-15 11:05:59

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

 
Comment by nicolas roy Subscribed to comments via email
2008-10-15 12:26:49

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 ?

Comment by Sőrés Viktor Subscribed to comments via email
2008-10-15 21:22:51

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

 
Comment by Karol Krizka
2008-10-16 21:44:21

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.

Comment by Karl Hegbloom
2008-11-03 22:40:58

/sys/devices/platform/thinkpad_acpi/hotkey_tablet_mode

(Comments wont nest below this level)
Comment by Karol Krizka
2008-11-18 08:37:21

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.

 
 
 
 
Comment by jagdfalke Subscribed to comments via email
2008-10-16 23:56:36

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

Comment by nicolas roy Subscribed to comments via email
2008-10-19 11:49:48

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

 
 
Comment by Matt
2008-11-11 15:14:43

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

Comment by Karol Krizka
2008-11-18 08:34:48

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.

Comment by Frames Subscribed to comments via email
2008-12-04 15:18:42

“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!

(Comments wont nest below this level)
Comment by Karol Krizka
2008-12-18 14:52:17

The same repository should work on intrepid too.

 
 
 
 
Comment by Denis
2009-02-06 16:39:04

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?

 
Comment by jones Subscribed to comments via email
2009-02-10 11:52:34

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?

 
Comment by Renato
2009-03-14 21:50:07

Thank you! It was very helpful!

 
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> <pre lang="" line="" escaped=""> in your comment.

Feed