-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoremap.py
executable file
·83 lines (56 loc) · 2.09 KB
/
autoremap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
import os
from argparse import ArgumentParser
import dbus
import evdev
from evdev import UInput
from evdev.ecodes import EV_KEY, KEY_VOLUMEUP, KEY_VOLUMEDOWN
INPUT_DEVICE_PATH = '/dev/input/by-path/'
INPUT_DEVICE_PREFIX = 'platform-gpio-keys'
GBUS_SENSORS_IFACE = 'net.hadess.SensorProxy'
GBUS_SENSORS_OBJ = '/net/hadess/SensorProxy'
GBUS_SENSOR_PROP_ACCEL = 'AccelerometerOrientation'
def is_gpio_volume_input(dev):
keys = dev.capabilities().get(EV_KEY)
if keys is None:
return False
return KEY_VOLUMEUP in keys and KEY_VOLUMEDOWN in keys
def find_gpio_volume_input():
for dev_name in os.listdir(INPUT_DEVICE_PATH):
if dev_name.startswith(INPUT_DEVICE_PREFIX):
dev = evdev.InputDevice(INPUT_DEVICE_PATH + dev_name)
if is_gpio_volume_input(dev):
return dev
return None
def translate(ev, iio_props):
if invert_buttons(iio_props):
if ev.type == EV_KEY:
if ev.code == KEY_VOLUMEUP:
ev.code = KEY_VOLUMEDOWN
elif ev.code == KEY_VOLUMEDOWN:
ev.code = KEY_VOLUMEUP
return ev
def invert_buttons(iio_props):
orientation = iio_props.Get(GBUS_SENSORS_IFACE, GBUS_SENSOR_PROP_ACCEL)
return orientation in ['normal', 'right-up']
def handle_events(dev, iio_props):
ui = UInput()
dev.grab()
for ev in dev.read_loop():
ui.write_event(translate(ev, iio_props))
def main():
cli = ArgumentParser(description='Remap volume button events based on device orientation.')
cli.add_argument('-d', '--device', metavar='D', help='input device to use')
args = cli.parse_args()
if args.device is None:
dev = find_gpio_volume_input()
if dev is None:
print('error: could not find default volume gpio-keys device')
else:
dev = evdev.InputDevice(args.device)
bus = dbus.SystemBus()
iio = bus.get_object(GBUS_SENSORS_IFACE, GBUS_SENSORS_OBJ)
iio_props = dbus.Interface(iio, 'org.freedesktop.DBus.Properties')
handle_events(dev, iio_props)
if __name__ == '__main__':
main()