Skip to content

Instantly share code, notes, and snippets.

@pastleo
Created July 3, 2018 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pastleo/32457fc486c478318cc955a74bbb3d6c to your computer and use it in GitHub Desktop.
Save pastleo/32457fc486c478318cc955a74bbb3d6c to your computer and use it in GitHub Desktop.
[Desktop Entry]
Version=1.0
Exec=python ~/.bin/iio-rotate.py
Name=IIO rotate
GenericName=IIO rotate
Comment=IIO rotate
Encoding=UTF-8
Terminal=true
Type=Application
Categories=Application;System;
Icon=tablet
#!/usr/bin/env python2
"""
iio-rotate.py
Rotates any detected screen, wacom digitizers, touchscreens,
touchpad/trackpoint based on orientation gathered from accelerometer.
Tested with Lenovo Dell latitude 11 5179
Acknowledgements:
Modified from source:
https://github.com/admiralakber/thinkpad-yoga-scripts/blob/master/rotate/thinkpad-rotate.py
https://gist.githubusercontent.com/ei-grad/4d9d23b1463a99d24a8d/raw/rotate.py
"""
### BEGIN Configurables
STATES = [
{'rot': 'normal', 'pen': 'none', 'coord': '1 0 0 0 1 0 0 0 1', 'touchpad': 'enable',
'check': lambda x, y: y <= -g},
{'rot': 'inverted', 'pen': 'half', 'coord': '-1 0 1 0 -1 1 0 0 1', 'touchpad': 'enable',
'check': lambda x, y: y >= g},
{'rot': 'left', 'pen': 'ccw', 'coord': '0 -1 1 1 0 0 0 0 1', 'touchpad': 'enable',
'check': lambda x, y: x >= g},
{'rot': 'right', 'pen': 'cw', 'coord': '0 1 0 -1 0 1 0 0 1', 'touchpad': 'enable',
'check': lambda x, y: x <= -g},
]
rotate_pens = False # Set false if your DE rotates pen for you
disable_touchpads = False # Don't use in conjunction with tablet-mode
# device keywords in lower case:
touchscreen_name_keywords = ['touchscreen', 'touch digitizer', 'finger']
pens_keywords = ['pen']
touchpad_name_keywords = ['touchpad', 'trackpoint']
### END Configurables
from os import path as op
import sys
from subprocess import check_call, check_output
from glob import glob
from os import environ
def bdopen(fname):
return open(op.join(iio_basedir, fname))
def read(fname):
return bdopen(fname).read()
for iio_basedir in glob('/sys/bus/iio/devices/iio:device*'):
if 'accel' in read('name'):
break
else:
sys.stderr.write("Can't find an accellerator device!\n")
sys.exit(1)
print('iio_basedir:')
print(iio_basedir)
env = environ.copy()
devices = check_output(['xinput', '--list', '--name-only'],env=env).splitlines()
touchscreens = [i for i in devices if any(j in i.lower() for j in touchscreen_name_keywords)]
print('touchscreens:')
print(touchscreens)
pens = [i for i in devices if any(j in i.lower() for j in pens_keywords)]
print('pens:')
print(pens)
touchpads = [i for i in devices if any(j in i.lower() for j in touchpad_name_keywords)]
print('touchpads:')
print(touchpads)
scale = float(read('in_accel_scale'))
g = 7.0 # (m^2 / s) sensibility, gravity trigger
def rotate(state):
s = STATES[state]
print("=============")
print("rot: " + s['rot'])
check_call(['xrandr', '-o', s['rot']],env=env)
for dev in touchscreens if disable_touchpads else (touchscreens + touchpads):
check_call([
'xinput', 'set-prop', dev,
'Coordinate Transformation Matrix',
] + s['coord'].split(),env=env)
if rotate_pens:
for dev in pens:
check_call([
'xsetwacom','set', dev,
'rotate',s['pen']],env=env)
if disable_touchpads:
for dev in touchpads:
check_call(['xinput', s['touchpad'], dev],env=env)
def read_accel(fp):
fp.seek(0)
return float(fp.read()) * scale
if __name__ == '__main__':
print("reading accel...")
accel_x = bdopen('in_accel_x_raw')
accel_y = bdopen('in_accel_y_raw')
x = read_accel(accel_x)
y = read_accel(accel_y)
print("x: " + str(x) + ", y: " + str(y))
for i in range(4):
if STATES[i]['check'](x, y):
rotate(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment