-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdgeSwipeDetect.py
More file actions
executable file
·200 lines (155 loc) · 6.85 KB
/
EdgeSwipeDetect.py
File metadata and controls
executable file
·200 lines (155 loc) · 6.85 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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import configparser
import evdev
from evdev import ecodes
from evdev.device import InputDevice
from select import select
from datetime import datetime
from subprocess import Popen, PIPE
class EdgeSwipeDetect:
def __init__(self, argv, config=dict()):
self.dev = None
self.edges = ["left", "top", "right", "bottom"]
self.gestures = {"left":0,
"top": 1,
"right": 2,
"bottom": 3}
self.orientations = {"normal": 0,
"left": 1,
"inverted": 2,
"right": 3}
self.checkOrientation = bool(config.get('checkOrientation', True))
self.ecodesX = ecodes.ABS_MT_POSITION_X
self.ecodesY = ecodes.ABS_MT_POSITION_Y
self.running = False
self.counter = 0
self.last_tap = -1
self.margin = int(config.get('margin', 2))
self.touching = 0
self.handling = ""
self.value = -1
self.last_value = -1
self.min_x = self.min_y = self.max_x = self.max_y = self.min_xy = -1
self.deviceName = config.get('deviceName', None)
searchTerm = ((self.deviceName is not None and self.deviceName) or
(len(argv) > 1 and argv[1] or "finger")
)
for d in evdev.list_devices():
de = InputDevice(d)
if de.name.lower().rfind(searchTerm.lower()) > 0:
self.dev = de
break
print("device: ", self.dev, file=sys.stderr)
if self.dev:
for cap in self.dev.capabilities()[ecodes.EV_ABS]:
if cap[0] == self.ecodesX:
self.min_x = int(config.get('min_x', cap[1].min))
self.max_x = int(config.get('max_x', cap[1].max))
elif cap[0] == self.ecodesY:
self.min_y = int(config.get('min_y', cap[1].min))
self.max_y = int(config.get('max_y', cap[1].max))
self.min_xy = min(self.max_y - self.min_y, self.max_x - self.min_x)
print("min x: ", self.min_x, file=sys.stderr)
print("max x: ", self.max_x, file=sys.stderr)
print("min y: ", self.min_y, file=sys.stderr)
print("max y: ", self.max_y, file=sys.stderr)
def run(self):
if not self.dev:
return
self.running = True
while self.running:
r, w, x = select([self.dev.fd], [], [])
if r:
if not self.running:
break
for event in self.dev.read():
if event.code == self.ecodesX:
self.handleXChange(event.value)
elif event.code == self.ecodesY:
self.handleYChange(event.value)
elif event.code == ecodes.BTN_TOUCH:
self.touching = event.value
if self.handling and not self.touching: # oh my
if (self.value > self.last_value
and self.value > self.min_xy * 0.05):
orientation = (self.checkOrientation and
self.getScreenOrientation() or 0)
edgeSwiped = self.gestures[self.handling]
rotatedEdge = self.edges[
(edgeSwiped + orientation) % 4
]
print(rotatedEdge)
# else:
# print(self.handling, "canceled")
# print(self.handling, "end")
self.handling = ""
self.value = -1
self.last_value = -1
if not self.handling and not self.touching:
now = datetime.now().timestamp()
if now - self.last_tap > 0.25:
self.counter = 1
else:
self.counter += 1
self.last_tap = now
if self.counter == 3:
print("tap_3")
self.counter = 0
sys.stdout.flush()
def handleXChange(self, x):
if x <= self.min_x + self.margin and not self.handling:
# print("left started")
self.handling = "left"
elif x >= self.max_x - self.margin and not self.handling:
# print("right started")
self.handling = "right"
if self.handling == "left":
self.handleLeftEdge(x)
elif self.handling == "right":
self.handleRightEdge(x)
if abs(self.last_value - self.value) > self.min_xy * 0.1:
self.last_value = self.value
def handleYChange(self, y):
#print(y, self.min_y + self.margin, self.max_y - self.margin)
if y <= self.min_y + self.margin and not self.handling:
#print("top started")
self.handling = "top"
elif y >= self.max_y - self.margin and not self.handling:
# print("bottom started")
self.handling = "bottom"
if self.handling == "top":
self.handleTopEdge(y)
if self.handling == "bottom":
self.handleBottomEdge(y)
if abs(self.last_value - self.value) > self.min_xy * 0.1:
self.last_value = self.value
def handleLeftEdge(self, x):
self.value = x
# print("l: %d, %d, %d" % (self.min_x, x, self.max_x))
def handleRightEdge(self, x):
self.value = (self.max_x - x)
# print("r: %d, %d, %d" % (self.min_x, x, self.max_x))
def handleTopEdge(self, y):
self.value = y
# print("t: %d, %d, %d" % (self.min_y, y, self.max_y))
def handleBottomEdge(self, y):
self.value = (self.max_y - y)
# print("b: %d, %d, %d" % (self.min_y, y, self.max_y))
def getScreenOrientation(self):
# xrandr --current --verbose | grep "LVDS1 connected" | awk '{print $5}'
output = Popen("xrandr --current --verbose | grep 'LVDS1 connected' | awk '{print $6}'",
shell=True, stdout=PIPE).communicate()[0].decode('UTF-8')[:-1]
orientation = orientation = self.orientations[output]
return orientation
def main():
config = configparser.ConfigParser()
config.read('config.ini')
if len(sys.argv) > 1:
# check if first argument is in config
if sys.argv[1] in config:
return EdgeSwipeDetect(sys.argv, config=config[sys.argv[1]]).run()
return EdgeSwipeDetect(sys.argv).run()
if __name__ == '__main__':
main()