-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgestures.py
322 lines (291 loc) · 12.3 KB
/
gestures.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# Copyright 2024.
# This file is part of Amity.
# Amity is free software: you can redistribute it and/or modify it under the terms of the
# GNU General Public License as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
import math, time
from remote import HwRevisions
from aconfig import config
config.default('remote.touchpad.pressure_threshold', 20)
config.default('remote.touchpad.swipe.distance_thresholds_mm', (7.0, 7.0))
config.default('remote.touchpad.swipe.deceleration', -250)
class EventType(object):
Inactive = 0
Running = 1
Begin = 2
Detected = 4
End = 8
Cancelled = 16
class TouchState(object):
def __init__(self, start_touch):
self.start_touch = start_touch
self.last_active_touch = start_touch
def is_begin_touch(self, touch):
return self.start_touch.timestamp == touch.timestamp
class GestureRecognizer(object):
def __init__(self, max_touches, callback):
self.touch_states = {}
self.max_touches = max_touches
self.callback = callback
self.pressure_threshold = config['remote.touchpad.pressure_threshold']
def reset(self):
self.touch_states = {}
def state_for_touch(self, touch):
et = EventType.Inactive
touch_state = self.touch_states.get(touch.id)
if touch_state is None:
if touch.p < self.pressure_threshold:
# This touch is too light to count as anything
return None, EventType.Begin | EventType.End
elif len(self.touch_states) == self.max_touches:
# This is a multi-touch
return None, EventType.Cancelled
touch_state = TouchState(touch)
et |= EventType.Begin
self.touch_states[touch.id] = touch_state
if touch.p < self.pressure_threshold: # Touch is over
del self.touch_states[touch.id]
et |= EventType.End
return touch_state, et
def last_active_touches(self):
return [ts.last_active_touch for ts in self.touch_states.values()]
def touches(self, remote, event):
return None
class SwipeEvent(object):
def __init__(self, type, xy):
self.type = type
self.x, self.y = xy
def __str__(self):
return f'0x{self.type:02X} ({self.x}, {self.y})'
class OneAxisSwipeRecognizer(GestureRecognizer):
def __init__(self, axis, distance_threshold_mm, callback):
super().__init__(1, callback) # No multitouch
self.axis = axis
self.distance_threshold_mm = distance_threshold_mm
self.deceleration = config['remote.touchpad.swipe.deceleration']
def touches_internal(self, remote, touches):
event_type = EventType.Running
counter = 0
for touch in touches:
touch_state, et = self.state_for_touch(touch)
if touch_state is None:
if et & EventType.Cancelled:
# Ignore multitouch
self.reset()
return SwipeEvent(EventType.Cancelled, (0, 0))
else:
# This touch is too light to matter
continue
event_type |= et
if event_type & EventType.End:
# If this is a release event, the touch is over.
# Check for a swipe gesture, and remove the state.
v = touch.axis_velocities_from_touch(touch_state.start_touch)[self.axis]
a = self.deceleration # mm/s^2
if v < 0:
a = -a
t = -v / a
d = v*t + .5*a*t*t
counter = int(d / self.distance_threshold_mm)
if counter != 0:
# Clamp the counter so it doesn't do surprising things...
counter = min(max(counter, -6), 6)
event_type |= EventType.Detected
else:
axis_distances = touch.axis_distances_from_touch(touch_state.last_active_touch)
distance = axis_distances[self.axis]
# Do we have an event?
if abs(distance) > self.distance_threshold_mm:
counter = int(distance / self.distance_threshold_mm)
touch_state.last_active_touch = touch
event_type |= EventType.Detected
xy = [0, 0]
xy[self.axis] = counter
return SwipeEvent(event_type, tuple(xy))
def touches(self, remote, touches):
event = self.touches_internal(remote, touches)
if event is not None:
self.callback(self, event)
class SwipeRecognizer(object):
def __init__(self, callback):
self.callback = callback
self.rs = []
self.primary_axis = None
thresholds_mm = config['remote.touchpad.swipe.distance_thresholds_mm']
for i in range(2):
self.rs.append(OneAxisSwipeRecognizer(i, thresholds_mm[i], None))
def reset(self):
for r in self.rs:
r.reset()
def touches(self, remote, touches):
# Initially, either axis can generate an event.
# Once one of the axes generates an event, all further event generation is locked
# to that axis.
if self.primary_axis is None:
for r in self.rs:
event = r.touches_internal(remote, touches)
if event.type & EventType.Detected:
self.primary_axis = r.axis
self.callback(self, event)
return
else:
event = self.rs[self.primary_axis].touches_internal(remote, touches)
if event is None: return event
if event.type & EventType.End:
self.primary_axis = None
self.reset()
self.callback(self, event)
if self.primary_axis is None:
event_type = EventType.Inactive
else:
event_type = EventType.Running
self.callback(self, SwipeEvent(event_type, (0, 0)))
class TapEvent(object):
def __init__(self, type, remote, x, y):
self.type = type
self.remote = remote
self.x = x
self.y = y
class TapRecognizer(GestureRecognizer):
def __init__(self, num_fingers, callback):
super().__init__(num_fingers, callback)
self.num_fingers = num_fingers
self.reset()
def reset(self):
super().reset()
self.num_active_touches = 0
self.have_all_touches = False
self.end_touches = []
self.begin_touch_timestamp = None
self.begin_xy = None
def midpoint_of_touches(self, touches):
x = y = 0
if not touches:
return x, y
for touch in touches:
x += touch.x
y += touch.y
x = int(x / len(touches))
y = int(y / len(touches))
return x, y
def touches(self, remote, touches):
event_type = EventType.Inactive
for touch in touches:
touch_state, et = self.state_for_touch(touch)
if touch_state is None:
# Too many touches! Reset our state.
self.reset()
self.callback(self, TapEvent(EventType.Cancelled, remote, 0, 0))
return
touch_state.last_active_touch = touch
event_type |= EventType.Running
if et == EventType.Begin:
self.num_active_touches += 1
if self.num_active_touches == 1:
self.begin_touch_timestamp = time.time()
if self.num_active_touches == self.num_fingers:
self.begin_xy = self.midpoint_of_touches(self.last_active_touches())
self.end_touches = []
if self.num_active_touches == self.num_fingers:
self.have_all_touches = True
event_type |= EventType.Begin
if et == EventType.End:
self.num_active_touches -= 1
self.end_touches.append(touch)
if self.num_active_touches == 0:
if self.have_all_touches:
# Was this more of a long press than a tap?
td = time.time() - self.begin_touch_timestamp
if td > .4 or td < .05:
self.reset()
self.callback(self, TapEvent(EventType.Cancelled, remote, 0, 0))
return
# Is this more a of swipe than a tap?
end_xy = self.midpoint_of_touches(self.end_touches)
rtp = remote.profile.touchpad
dx_mm = (end_xy[0] - self.begin_xy[0]) / rtp.RESOLUTION[0] * rtp.SIZE_MM
dy_mm = (end_xy[1] - self.begin_xy[1]) / rtp.RESOLUTION[1] * rtp.SIZE_MM
distance_mm_2 = dx_mm*dx_mm + dy_mm*dy_mm
if distance_mm_2 > 25:
self.reset()
self.callback(self, TapEvent(EventType.Cancelled, remote, 0, 0))
return
event_type &= ~EventType.Running
event_type |= EventType.Detected | EventType.End
if event_type & EventType.Detected:
xy_touches = self.end_touches
else:
xy_touches = self.last_active_touches()
x, y = self.midpoint_of_touches(xy_touches)
if event_type & EventType.End:
self.reset()
self.callback(self, TapEvent(event_type, remote, x, y))
class MultiTapRecognizer(object):
def __init__(self, num_fingers, num_taps, callback):
self.num_fingers = num_fingers
self.num_taps = num_taps
self.callback = callback
self.tap_recognizer = TapRecognizer(self.num_fingers, self.tap_event)
self.reset()
def reset(self):
self.last_tap_time = time.time()
self.tap_recognizer.reset()
self.tap_count = 0
def tap_event(self, recognizer, event):
if event.type & EventType.Detected:
now = time.time()
if now - self.last_tap_time > .4:
if self.tap_count > 0:
self.callback(self, TapEvent(EventType.Cancelled, event.remote, 0, 0))
self.tap_count = 0
self.tap_count += 1
self.last_tap_time = now
if self.tap_count == 1:
if self.num_taps == 1:
self.callback(self, event)
self.tap_recognizer.reset()
return
else:
self.callback(self, TapEvent(EventType.Begin, event.remote, event.x, event.y))
if self.tap_count == self.num_taps:
self.callback(self, event)
self.tap_count = 0
def touches(self, remote, touches):
self.tap_recognizer.touches(remote, touches)
# This tweaks button values, in lieu of an event callback
class DPadEmulator(object):
def __init__(self):
self.last_touch = None
self.last_buttons = 0
def touches(self, remote, touches):
self.last_touch = touches[0]
def buttons(self, remote, buttons):
if remote.profile.hw_revision not in (HwRevisions.GEN_1, HwRevisions.GEN_1_5):
return buttons
if self.last_touch is None:
return buttons
pressed = buttons & ~self.last_buttons
released = self.last_buttons & ~buttons
self.last_buttons = buttons
btns = remote.profile.buttons
if pressed & btns.SELECT:
zt = remote.zero_touch()
d_mm = self.last_touch.distance_from_touch(zt)
if d_mm < 12:
return buttons
dx_mm, dy_mm = self.last_touch.axis_distances_from_touch(zt)
direction = math.degrees(math.atan2(dx_mm, dy_mm)) + 180
a = 30
btn = btns.INVALID
if direction < a or direction > 360 - a:
btn = btns.DOWN
elif direction > 90-a and direction < 90+a:
btn = btns.LEFT
elif direction > 180-a and direction < 180+a:
btn = btns.UP
elif direction > 270-a and direction < 270+a:
btn = btns.RIGHT
return (buttons & ~btns.SELECT) | btn
elif released & btns.SELECT:
return buttons & ~(btns.DOWN|btns.LEFT|btns.UP|btns.RIGHT)
return buttons