-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtouchbtns.py
More file actions
executable file
·69 lines (56 loc) · 2.04 KB
/
touchbtns.py
File metadata and controls
executable file
·69 lines (56 loc) · 2.04 KB
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
#!/usr/bin/env python3
import re
from subprocess import Popen, PIPE, run
import sys
from typing import List
DEVICE_ID_RX = re.compile(r".*id=(\d+)")
def get_devices():
devs = []
result = run(['xinput', 'list', '--long'], capture_output=True)
if result.returncode != 0:
raise RuntimeError(
"Could not obtain list of devices via xinput; running under X11?")
return result.stdout.decode('UTF-8').split('\n')
def get_touchpad_id(name, devs):
for row in devs:
if name in row:
match = DEVICE_ID_RX.match(row)
if match and (dev_id := match.groups(1)):
return dev_id[0]
def get_button_states(dev_id):
result = run(['xinput', 'get-button-map', dev_id], capture_output=True)
return result.stdout.decode('UTF-8').split('\n')[0].split(r" ")
def toggle_touchpad_buttons(touchpad_name,
toggleable_button_positions: List):
devices = get_devices()
touchpad_id = get_touchpad_id(touchpad_name, devices)
button_states = get_button_states(touchpad_id)
new_toggleable_states = []
on_or_off = 0
for pos in toggleable_button_positions:
if int(button_states[pos]) == 0:
new_toggleable_states.append(1 + pos)
on_or_off += 1
else:
new_toggleable_states.append(0)
on_or_off += -1
new_button_states = (str(val) for val in new_toggleable_states +
button_states[len(toggleable_button_positions):])
run(
['xinput', 'set-button-map', touchpad_id, *new_button_states],
capture_output=True,
)
print(f"Bottom button row turned {'off' if on_or_off < 0 else 'on'}")
if __name__ == "__main__":
try:
touchpad_name = sys.argv[1]
except:
touchpad_name = "Elan Touchpad"
if len(sys.argv) > 2:
toggle_buttons = [int(val) for val in sys.argv[2:]]
else:
toggle_buttons = [0, 1, 2]
toggle_touchpad_buttons(
touchpad_name=touchpad_name,
toggleable_button_positions=toggle_buttons,
)