Skip to content

Instantly share code, notes, and snippets.

Last active March 17, 2020 19:03
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Rotate screen on tablet PCs
#!/usr/bin/env python
from time import sleep
from os import path as op
import sys
from subprocess import check_call, check_output
from glob import glob
def bdopen(fname):
return open(op.join(basedir, fname))
def read(fname):
return bdopen(fname).read()
for basedir in glob('/sys/bus/iio/devices/iio:device*'):
if 'accel' in read('name'):
sys.stderr.write("Can't find an accellerator device!\n")
devices = check_output(['xinput', '--list', '--name-only']).splitlines()
touchscreen_names = ['touchscreen', 'wacom']
touchscreens = [i for i in devices if any(j in i.lower() for j in touchscreen_names)]
disable_touchpads = False
touchpad_names = ['touchpad', 'trackpoint']
touchpads = [i for i in devices if any(j in i.lower() for j in touchpad_names)]
scale = float(read('in_accel_scale'))
g = 7.0 # (m^2 / s) sensibility, gravity trigger
{'rot': 'normal', 'coord': '1 0 0 0 1 0 0 0 1', 'touchpad': 'enable',
'check': lambda x, y: y <= -g},
{'rot': 'inverted', 'coord': '-1 0 1 0 -1 1 0 0 1', 'touchpad': 'disable',
'check': lambda x, y: y >= g},
{'rot': 'left', 'coord': '0 -1 1 1 0 0 0 0 1', 'touchpad': 'disable',
'check': lambda x, y: x >= g},
{'rot': 'right', 'coord': '0 1 0 -1 0 1 0 0 1', 'touchpad': 'disable',
'check': lambda x, y: x <= -g},
def rotate(state):
s = STATES[state]
check_call(['xrandr', '-o', s['rot']])
for dev in touchscreens if disable_touchpads else (touchscreens + touchpads):
'xinput', 'set-prop', dev,
'Coordinate Transformation Matrix',
] + s['coord'].split())
if disable_touchpads:
for dev in touchpads:
check_call(['xinput', s['touchpad'], dev])
def read_accel(fp):
return float( * scale
if __name__ == '__main__':
accel_x = bdopen('in_accel_x_raw')
accel_y = bdopen('in_accel_y_raw')
current_state = None
while True:
x = read_accel(accel_x)
y = read_accel(accel_y)
for i in range(4):
if i == current_state:
if STATES[i]['check'](x, y):
current_state = i
Copy link

I0x0I commented Apr 29, 2015

It would be better if the first line is like
#!/usr/bin/env python2.7
python3 doesn't work :-)

Copy link

devices = check_output(['xinput', '--list', '--name-only']).splitlines()
devices = check_output(['xinput', '--list', '--name-only']).decode().splitlines()
to make it work in Python3.

Copy link

Also I'd replace all uses of check_call to just call otherwise devices with removable touchpads cause the script to raise an exception rather than ignore the error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment