-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpalmrejection.py
214 lines (170 loc) · 6.51 KB
/
palmrejection.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/local/bin/python
from __future__ import print_function
import subprocess
import re
import time
import sys
import atexit
import sys
if sys.version_info < (3, 0):
try:
FileNotFoundError
except NameError:
FileNotFoundError = IOError
# Configuration
touch_screen = ""
pen = ""
sleep_time_in_sec = 0.250
def find_device_name(udev_result, udev_device_flag):
device_information = []
found_i2c_device = False
found_device_flag = False
found_device_name = False
device_name = ""
for line in udev_result:
# Found new device, process current device before adding a new one
if line.startswith('P: ') and found_i2c_device:
if found_device_flag:
for line in device_information:
if "E: NAME=" in line:
found_device_name = True
device_name = line
break
device_information.clear()
found_i2c_device = False
found_device_flag = False
# Each device information starts with P in front in first line, touchscreen or wacom most likely will be connected via i2c
if line.startswith('P: ') and "i2c" in line and not found_i2c_device:
found_i2c_device = True
device_information.append(line)
continue
if found_i2c_device:
device_information.append(line)
if udev_device_flag in line:
found_device_flag = True
if not found_device_name:
print("Couldn't find device, please configure manually.")
return None
device_name = device_name.split(sep='=')[1]
device_name = device_name.replace('"', '')
return device_name
def auto_configure():
udevadm_process = subprocess.Popen(['udevadm', 'info', '--export-db'], stdout=subprocess.PIPE)
result = udevadm_process.communicate()[0].decode('UTF-8').splitlines()
udev_touchscreen_flag = "ID_INPUT_TOUCHSCREEN=1"
udev_wacom_flag = "ID_INPUT_TABLET=1"
global touch_screen
touch_screen = find_device_name(result, udev_touchscreen_flag)
global pen
pen = find_device_name(result, udev_wacom_flag)
print("Autoconfiguration result: ")
print("Touchscreen: ", touch_screen)
print("Wacom pen: ", pen)
if touch_screen != None and pen != None:
return True
return False
def find_id(device):
"""Extracts device id(s) from xinput. Parametr device is name of device.
Returns a list of device id(s)"""
result = ""
try:
p = subprocess.Popen(['xinput', '-list'], stdout=subprocess.PIPE)
p2 = subprocess.Popen(['grep', device], stdin=p.stdout,
stdout=subprocess.PIPE)
p.stdout.close()
result = p2.communicate()[0].decode('UTF-8')
if not result:
raise ValueError("[E1]: Couldn't find such device: {}. Recheck configuration.".format(touch_screen))
except subprocess.CalledProcessError as e:
print(e)
sys.exit(1)
except FileNotFoundError as e:
print("[E2]: Check whether 'xinput' and/or 'grep' is installed")
print(e)
sys.exit(1)
except ValueError as e:
print(e)
sys.exit(1)
except:
print("Unexpected error. Closing...")
sys.exit(1)
pattern = "\s+(id=\d+)"
matches = re.findall(pattern, result)
ids = [id.split("=")[1] for id in matches]
print("Found {} device id(s) {}".format(device, ids))
return ids
def pen_status(pen_ids):
"""Checks via xinput query-state whether pen is close to screen
Returns list of statuses. There might be more than one status when pen does have eraser"""
status = []
result = ""
pattern = "Absolute Proximity=(Out|In)"
for id in pen_ids:
cmd = ['xinput', 'query-state', id]
try:
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
result = p.communicate()[0].decode('UTF-8')
if not result:
raise ValueError("[E4]: Can't find query state about pen with id: {}. Recheck configuration.".format(id))
matches = re.findall(pattern, result)
if not matches:
raise ValueError("[E5]: Can't match proximity pattern")
if "In" in matches:
status.append(True)
else:
status.append(False)
except subprocess.CalledProcessError as e:
print(e)
sys.exit(1)
except FileNotFoundError as e:
print("[E2]: Check whether 'xinput' is installed")
print(e)
sys.exit(1)
except ValueError as e:
print(e)
sys.exit(1)
except:
print("Unexpected error. Closing...")
sys.exit(1)
return status
def disable_touchscreen(touch_screen_id):
cmd = ['xinput', 'disable', touch_screen_id[0]]
proc = subprocess.Popen(cmd)
# print("Touchscreen disabled")
def enable_touchscreen(touch_screen_id):
cmd = ['xinput', 'enable', touch_screen_id[0]]
proc = subprocess.Popen(cmd)
# print("Touchscreen enabled")
if __name__ == '__main__':
if len(sys.argv) == 1:
print("For auto configuration type: python ", sys.argv[0], "--auto")
if not touch_screen and not pen:
print("Empty configuration, autoconfiguration attempt.")
auto_configure()
elif len(sys.argv) == 2 and sys.argv[1] == "--auto":
auto_configure()
devices_id = {touch_screen: find_id(touch_screen), pen: find_id(pen)}
last_status = [False, False]
while True:
current_status = pen_status(devices_id[pen])
try:
if current_status == last_status:
last_status = current_status
time.sleep(sleep_time_in_sec)
continue
# If Pen or eraser is near disable touchscreen
if any(current_status):
disable_touchscreen(devices_id[touch_screen])
elif not all(current_status): # Both pen and eraser is not near
enable_touchscreen(devices_id[touch_screen])
last_status = current_status
time.sleep(sleep_time_in_sec)
# Enables touchscreen at exception just in case, so we won't be left with disabled touchscreen
except KeyboardInterrupt as e:
print(e)
enable_touchscreen(devices_id[touch_screen])
sys.exit(1)
except:
print("Unexpected error. Closing...")
enable_touchscreen(devices_id[touch_screen])
sys.exit(1)