ThinkPad X61 Tablet Automatic Screen Rotation Under Linux
Karol Krizka @ February 13, 2008
LinuxOne 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
- Make sure you have a working accelerometer that is reading the proper orientation as tested using the hdaps-gl utility.
- 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 - Update apt-get’s package list to include python-xrandr:
sudo apt-get update - Install the necessary packages for the script to work:
sudo apt-get install wacom-tools python-xrandr - 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 - Create a file to hold the information about the current screen mode (laptop vs tablet):
sudo touch /etc/tabletmode - 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 - 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 - Restart ACPI to register the events:
sudo /etc/init.d/acpid restart - 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& - Restart GDM to start the script:
sudo /etc/init.d/gdm restart - 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.
My name is Karol Krizka, and I am a undergraduate student at SFU where I study physics and computer science. In my free time, I write simple applications and play with my PSP.
[...] 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 | [...]
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
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?
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.
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.
[...] installed Karol Krizka’s automatic rotation script. I am not yet sure if I am a fan of the automatic rotation rather than [...]
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!
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
would somebody please email me the autorotate.py script to nitehoaxxer@gmx.net? Server seems down an I really need it - thx
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.
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.
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?
right, I tried it and experienced the same effect you described. My model is X61 Tablet - more information needed?
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/tabletmodebecause 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”.
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
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!
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.