-
Notifications
You must be signed in to change notification settings - Fork 1
/
toggle_touchpad.py
70 lines (63 loc) · 1.75 KB
/
toggle_touchpad.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
import subprocess
import argparse
import time
def get_touchpad_id():
command = "xinput list"
proc = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
out, err = proc.communicate()
if not err:
out_split = out.split()
touchpad = b'TouchPad'
i = None
for ind in out_split:
if ind == touchpad:
i = out_split.index(touchpad)
val = out_split[i+1]
if val.startswith(b'id='):
return val[3:].decode()
else:
raise Exception('expected id value, index is not id')
else:
raise Exception(err)
def get_touchpad_status(ID):
command = "xinput list-props {}".format(ID)
proc = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
out, err = proc.communicate()
if not err:
out_split = out.split()
enabled = b'Enabled'
i = None
for ind in out_split:
if ind == enabled:
i = out_split.index(enabled)
val = out_split[i+2].decode()
if val in ['0', '1']:
return val
else:
raise Exception('expected 1 or 0')
else:
raise Exception(err)
def execute(ID, stat):
command = 'xinput --set-prop {} "Device Enabled" {}'.format(ID, stat)
print(command)
proc = subprocess.Popen(command.split())
touchpad_id = get_touchpad_id()
touchpad_status = get_touchpad_status(touchpad_id)
parser = argparse.ArgumentParser(description="Toggle Touch Pad Arguemnts")
parser.add_argument('-1', '--on', action='store_true',
help='turn touchpad on')
parser.add_argument('-0', '--off', action='store_true',
help='turn touchpad off')
args = vars(parser.parse_args())
if args['on']:
print('turn touchpad on')
execute(touchpad_id, 1)
elif args['off']:
print('turn touchpad off')
execute(touchpad_id, 0)
elif not args['on'] and not args['off']:
print('no argument')
if touchpad_status:
execute(touchpad_id, 0)
else:
execute(touchpad_id, 1)