<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.thinkwiki.org/w/index.php?action=history&amp;feed=atom&amp;title=Using_hdaps_for_screen_rotation</id>
	<title>Using hdaps for screen rotation - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://www.thinkwiki.org/w/index.php?action=history&amp;feed=atom&amp;title=Using_hdaps_for_screen_rotation"/>
	<link rel="alternate" type="text/html" href="https://www.thinkwiki.org/w/index.php?title=Using_hdaps_for_screen_rotation&amp;action=history"/>
	<updated>2026-05-23T09:15:30Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.31.12</generator>
	<entry>
		<id>https://www.thinkwiki.org/w/index.php?title=Using_hdaps_for_screen_rotation&amp;diff=42636&amp;oldid=prev</id>
		<title>JoeWreschnig: HDAPS-based screen rotation for the X61</title>
		<link rel="alternate" type="text/html" href="https://www.thinkwiki.org/w/index.php?title=Using_hdaps_for_screen_rotation&amp;diff=42636&amp;oldid=prev"/>
		<updated>2009-04-12T08:57:55Z</updated>

		<summary type="html">&lt;p&gt;HDAPS-based screen rotation for the X61&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;In the middle of fighting to get the X61's screen rotate button to talk to xrandr, I realized that the laptop already has an accelerometer in it and given that, the screen rotate button was a crappy UI anyway.&lt;br /&gt;
&lt;br /&gt;
So, here's a way to make the laptop automatically rotate the screen, based on the accelerometer, when in tablet mode. It was written on a system running Ubuntu 8.10, but any system with ACPI and HDAPS configured should work.&lt;br /&gt;
&lt;br /&gt;
First, we need two files to tell acpid to stick a flag in the filesystem when switching between tablet and laptop modes.&lt;br /&gt;
&lt;br /&gt;
*/etc/acpi/lenovo-rotate&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event=ibm/hotkey HKEY 00000080 00005009&lt;br /&gt;
action=/usr/bin/touch /var/lib/acpi-support/tablet&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*/etc/acpi/lenovo-rotate-back&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
event=ibm/hotkey HKEY 00000080 0000500a&lt;br /&gt;
action=/bin/rm -f /var/lib/acpi-support/tablet&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Second, we need something to rotate the screen when the HDAPS device passes a certain movement threshold.&lt;br /&gt;
&lt;br /&gt;
* autorotate.py&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/env python&lt;br /&gt;
&lt;br /&gt;
import glob&lt;br /&gt;
import select&lt;br /&gt;
import struct&lt;br /&gt;
import sys&lt;br /&gt;
import os&lt;br /&gt;
from fcntl import ioctl&lt;br /&gt;
&lt;br /&gt;
JS_EVENT_FMT = &amp;quot;IhBB&amp;quot;&lt;br /&gt;
JS_EVENT_SIZE = struct.calcsize(JS_EVENT_FMT)&lt;br /&gt;
&lt;br /&gt;
# Event types --&lt;br /&gt;
JS_EVENT_BUTTON = 0x01 # button pressed/released&lt;br /&gt;
JS_EVENT_AXIS   = 0x02 # joystick moved&lt;br /&gt;
JS_EVENT_INIT   = 0x80 # &amp;quot;virtual&amp;quot; event flag&lt;br /&gt;
&lt;br /&gt;
# ioctl magic codes for device info --&lt;br /&gt;
JSIOCGAXES    = 0x80016a11    # axis count&lt;br /&gt;
JSIOCGBUTTONS = 0x80016a12    # button count&lt;br /&gt;
JSIOCGNAME    = 0x81006a13    # device name&lt;br /&gt;
&lt;br /&gt;
# ~0.56 is straight vertical&lt;br /&gt;
TILT_THRESHOLD = 0.38&lt;br /&gt;
&lt;br /&gt;
# Names of xinput devices to rotate with the screen&lt;br /&gt;
TOUCH_DEVICES = &amp;quot;stylus eraser pad touch&amp;quot;.split()&lt;br /&gt;
&lt;br /&gt;
def find_hdaps():&lt;br /&gt;
    for filename in glob.glob(&amp;quot;/dev/input/js*&amp;quot;) + glob.glob(&amp;quot;/dev/js*&amp;quot;):&lt;br /&gt;
        try: dev = open(filename, &amp;quot;rb&amp;quot;)&lt;br /&gt;
        except (IOError, OSError): continue&lt;br /&gt;
        else:&lt;br /&gt;
            axes = struct.unpack(&lt;br /&gt;
                &amp;quot;b&amp;quot;, ioctl(dev, JSIOCGAXES, &amp;quot; &amp;quot;))[0]&lt;br /&gt;
            buttons = struct.unpack(&lt;br /&gt;
                &amp;quot;b&amp;quot;, ioctl(dev, JSIOCGBUTTONS, &amp;quot; &amp;quot;))[0]&lt;br /&gt;
            name = struct.unpack(&lt;br /&gt;
                &amp;quot;1024s&amp;quot;, ioctl(dev, JSIOCGNAME, &amp;quot; &amp;quot; * 1024))[0]&lt;br /&gt;
            name = name.rstrip().strip(&amp;quot;\x00&amp;quot;)&lt;br /&gt;
            if name == &amp;quot;hdaps&amp;quot;:&lt;br /&gt;
                return dev&lt;br /&gt;
            else:&lt;br /&gt;
                dev.close()&lt;br /&gt;
&lt;br /&gt;
def set_state(state):&lt;br /&gt;
    os.system(&amp;quot;xrandr -o %s&amp;quot; % state)&lt;br /&gt;
    pen_rot = [&amp;quot;normal&amp;quot;, &amp;quot;right&amp;quot;, &amp;quot;left&amp;quot;, &amp;quot;inverted&amp;quot;].index(state)&lt;br /&gt;
    for dev in TOUCH_DEVICES:&lt;br /&gt;
        os.system(&amp;quot;xsetwacom set %s rotate %d&amp;quot; % (dev, pen_rot))&lt;br /&gt;
&lt;br /&gt;
def main(argv):&lt;br /&gt;
    state = &amp;quot;normal&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    dev = find_hdaps()&lt;br /&gt;
    if not dev:&lt;br /&gt;
        raise SystemExit(&amp;quot;Unable to find HDAPS joystick device.&amp;quot;)&lt;br /&gt;
    while select.select([dev], [], []):&lt;br /&gt;
        # Unpack the event and send it!&lt;br /&gt;
        evt = dev.read(JS_EVENT_SIZE)&lt;br /&gt;
        time, value, etype, number = struct.unpack(JS_EVENT_FMT, evt)&lt;br /&gt;
        if etype &amp;amp; JS_EVENT_AXIS:&lt;br /&gt;
            value = value / 32768.0&lt;br /&gt;
            if number == 0 and value &amp;gt; TILT_THRESHOLD:&lt;br /&gt;
                new_state = &amp;quot;normal&amp;quot;&lt;br /&gt;
            elif number == 0 and value &amp;lt; -TILT_THRESHOLD:&lt;br /&gt;
                new_state = &amp;quot;inverted&amp;quot;&lt;br /&gt;
            elif number == 1 and value &amp;lt; -TILT_THRESHOLD:&lt;br /&gt;
                new_state = &amp;quot;left&amp;quot;&lt;br /&gt;
            elif number == 1 and value &amp;gt; TILT_THRESHOLD:&lt;br /&gt;
                new_state = &amp;quot;right&amp;quot;&lt;br /&gt;
            else:&lt;br /&gt;
                new_state = state&lt;br /&gt;
            if state != new_state:&lt;br /&gt;
                if (new_state != &amp;quot;normal&amp;quot;&lt;br /&gt;
                    and not os.path.exists(&amp;quot;/var/lib/acpi-support/tablet&amp;quot;)):&lt;br /&gt;
                    new_state = &amp;quot;normal&amp;quot;&lt;br /&gt;
                if new_state != state:&lt;br /&gt;
                    state = new_state&lt;br /&gt;
                    set_state(state)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    main(sys.argv[1:])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, &amp;lt;code&amp;gt;/etc/init.d/acpid reload&amp;lt;/code&amp;gt;, and run &amp;lt;code&amp;gt;autorotate.py&amp;lt;/code&amp;gt;. Switch your laptop to tablet mode, and spin it around. The screen should rotate appropriately. If you find it rotates too easily or not easily enough, tweak the TILT_THRESHOLD variable. If you named your XInput devices for the touchscreen differently, change the TOUCH_DEVICES string.&lt;br /&gt;
&lt;br /&gt;
[[Category:X61]]&lt;/div&gt;</summary>
		<author><name>JoeWreschnig</name></author>
		
	</entry>
</feed>