-
Notifications
You must be signed in to change notification settings - Fork 0
/
xinput.py
51 lines (31 loc) · 1.1 KB
/
xinput.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
#-*- coding: utf-8 -*-
"""
Enable or disable devices from X using xinput
"""
import signal
import os
disabled_ids = []
def get_ids():
return [id.replace('\n', '') for id in os.popen('xinput list --id-only').readlines()]
def get_device_from_id(id):
return os.popen('xinput --list-props {0} | grep "Device Node"'.format(id)).read().split(':')[-1].strip().replace('"', '')
def get_devices_to_id_dict():
return dict([(get_device_from_id(id), id) for id in get_ids()])
def get_id_from_device(device):
device = os.path.realpath(device)
device_to_id = get_devices_to_id_dict()
return device_to_id.get(device, '')
def disable_id(id):
os.popen('xinput disable {0}'.format(id))
disabled_ids.append(id)
def enable_id(id):
os.popen('xinput enable {0}'.format(id))
def disable_device(device):
disable_id(get_id_from_device(device))
def enable_device(device):
enable_id(get_id_from_device(device))
def trap_handler(signum, frame):
for id in disabled_ids:
enable_id(id)
signal.signal(signal.SIGTERM, trap_handler)
signal.signal(signal.SIGINT, trap_handler)