Skip to content

Instantly share code, notes, and snippets.

@ftalbrecht
Created October 5, 2020 08:41
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 ftalbrecht/6ab24b75029b708502511083c23a848b to your computer and use it in GitHub Desktop.
Save ftalbrecht/6ab24b75029b708502511083c23a848b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
# ================ settings ============
disable_touchpads = False
blacklist = [
# this is a keyboard, not a pointer
'Wacom Pen and multitouch sensor Pen',
]
# ======================================
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'):
break
else:
sys.stderr.write("Can't find an accellerator device!\n")
sys.exit(1)
devices = check_output(['xinput', '--list', '--name-only']).splitlines()
devices = [i.decode('ascii') for i in devices]
touchscreen_names = ['touchscreen', 'wacom']
touchscreens = [i for i in devices if any(j in i.lower() for j in touchscreen_names)]
touchscreens = [i for i in touchscreens if not i in blacklist]
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
STATES = [
{'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):
check_call([
'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):
fp.seek(0)
return float(fp.read()) * 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:
continue
if STATES[i]['check'](x, y):
current_state = i
rotate(i)
break
sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment