-
Notifications
You must be signed in to change notification settings - Fork 14
/
input.py
728 lines (590 loc) · 23.2 KB
/
input.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
from ctypes import *
from typing import Generic, Optional, TypeVar
import collections
import signal
import threading
from errno import ENODEV
from gl import *
from libevdev import EV_ABS, EV_KEY, EV_REL, EventsDroppedException
from pathlib import Path
from PIL import Image
from lib import glsl
from threading import Thread
_pending_inputs = collections.deque()
_active_inputs = []
_texture_units = iter([])
def _init_slots():
global _texture_units
max_texture_image_units = pointer(c_uint())
glsl.glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, max_texture_image_units)
value = max_texture_image_units.contents.value
_texture_units = iter([i for i in range(value if value > 0 else 16)])
def _input_devices():
for input in _active_inputs:
if isinstance(input, MultiInput):
for i in input.inputs:
if isinstance(i, InputDevice):
yield i
elif isinstance(input, InputDevice):
yield input
def _evdev_event(input):
try:
while True:
try:
for ev in input.dev.events():
input.handler.event(ev, target=input)
except EventsDroppedException:
for ev in input.dev.sync():
input.handler.event(ev, target=input)
except IOError as e:
if e.errno == ENODEV:
print(f'input device {input.dev.name} unplugged')
else:
print(f'error reading events from input device {input.dev.name}', e)
except Exception as e:
print(f'error reading events from input device {input.dev.name}', e)
finally:
ClosingDevice(input)
def _validate_input(input, program, width, height):
# Remove the input if its device has closed
if isinstance(input, ClosingDevice):
if (isinstance(input.target, Mouse)
and (multi := next(filter(lambda i: isinstance(i, MultiMouse), _active_inputs), None))):
multi.remove(input.target)
if len(multi.mice) == 1:
mouse = multi.mice[0]
multi.remove(mouse)
_active_inputs.append(mouse)
_active_inputs.remove(multi)
else:
_active_inputs.remove(input.target)
return
# Check if it's an input device that's already open
if (isinstance(input, InputDevice)
and next(filter(lambda i: i.dev.fd.name == input.dev.fd.name, _input_devices()), None)):
return
# Hacky way to append the synthetic input at the end of the queue, so it's popped right next
def _push_right(f): i = f(); _pending_inputs.rotate(-1); return i
# Check the input refers to an existing uniform
try:
input.init(program=program, width=width, height=height)
except NoActiveUniformVariable as e:
if isinstance(input, Mouse) and input.name != 'iMouse':
print(f"invalid {type(input).__name__} input '{input.name}': {e}")
elif isinstance(input, Touchscreen):
# TODO: keep the touchscreen device that's the same as the display device
# Fall back to using the touchscreen as a mouse device
_push_right(lambda: TouchMouse('iMouse', input.dev))
elif isinstance(input, Trackpad):
# Fall back to using the trackpad as a mouse device
_push_right(lambda: TrackMouse('iMouse', input.dev))
return
except Exception as e:
print(f"invalid {type(input).__name__} input '{input.name}': {e}")
return
# Handle the input multiplexing
if isinstance(input, Mouse):
# Multiplex mouse devices into a single input
# TODO: multiplex by uniform name
if multi := next(filter(lambda i: isinstance(i, MultiMouse), _active_inputs), None):
multi.add(input)
elif mouse := next(filter(lambda i: isinstance(i, Mouse), _active_inputs), None):
_active_inputs.remove(mouse)
_push_right(lambda: MultiMouse('iMouse')).add(mouse, input)
else:
_active_inputs.append(input)
else:
_active_inputs.append(input)
# Start processing events from the input device
if isinstance(input, InputDevice):
input.dev.grab()
Thread(target=_evdev_event, args=[input], daemon=True).start()
def _drain(q: collections.deque):
while True:
try:
yield q.pop()
except IndexError:
break
@CFUNCTYPE(None, c_uint, c_uint, c_uint)
def _setup(program, width, height):
_init_slots()
# Drain all the inputs defined during initialisation
for input in _drain(_pending_inputs):
_validate_input(input, program, width, height)
@CFUNCTYPE(None, c_uint64, c_float)
def _update(frame, time):
# Drain pending inputs (added at runtime)
if len(_pending_inputs):
program = c_uint()
glsl.glGetIntegerv(GL_CURRENT_PROGRAM, pointer(program))
viewport = (c_uint*4)()
glsl.glGetIntegerv(GL_VIEWPORT, viewport)
(width, height) = viewport[2:4]
for input in _drain(_pending_inputs):
_validate_input(input, program, width, height)
# Render active inputs
for input in _active_inputs:
input.render(frame=frame, time=time)
glsl.onInit(_setup)
glsl.onRender(_update)
class NoActiveUniformVariable(Exception):
name = None
def __init__(self, name):
super().__init__(f"no active uniform variable '{name}'")
self.name = name
class Input:
name = ''
loc = None
def __init__(self, name):
self.name = name
_pending_inputs.appendleft(self)
def init(self, program, width, height):
self.loc = glsl.glGetUniformLocation(program, bytes(self.name, 'utf-8'))
if self.loc < 0:
raise NoActiveUniformVariable(self.name)
def render(self, frame, time):
return
class Texture(Input):
tex = None
unit = None
def init(self, **kwargs):
super().init(**kwargs)
self.tex = c_uint()
self.unit = next(_texture_units)
glsl.glUniform1i(self.loc, self.unit)
glsl.glActiveTexture(GL_TEXTURE0 + self.unit)
glsl.glGenTextures(1, pointer(self.tex))
class ImageTexture(Texture):
path = ''
transpose = None
def __init__(self, name, path, transpose=None):
super().__init__(name)
self.path = path
self.transpose = transpose
def init(self, **kwargs):
super().init(**kwargs)
glsl.glBindTexture(GL_TEXTURE_2D, self.tex)
glsl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glsl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glsl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
glsl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
image = Image.open(self.path)
if self.transpose:
image = image.transpose(getattr(Image, self.transpose))
data = image.convert('RGBA').tobytes()
glsl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.width, image.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data)
glsl.glGenerateMipmap(GL_TEXTURE_2D)
image.close()
class VolumeTexture(Texture):
path = ''
def __init__(self, name, path):
super().__init__(name)
self.path = path
def init(self, **kwargs):
super().init(**kwargs)
data = Path(self.path).read_bytes()
width = int.from_bytes(data[4:8], byteorder='little')
height = int.from_bytes(data[8:12], byteorder='little')
depth = int.from_bytes(data[12:16], byteorder='little')
channels = int.from_bytes(data[16:17], byteorder='little')
bin_format = int.from_bytes(data[18:20], byteorder='little')
is_float = bin_format == 10
internal_format = GL_RGBA
source_format = GL_RGBA
if channels == 4:
internal_format = GL_RGBA16F if is_float else GL_RGBA8
source_format = GL_RGBA
elif channels == 3:
internal_format = GL_RGB16F if is_float else GL_RGB8
source_format = GL_RGB
elif channels == 2:
internal_format = GL_RG16F if is_float else GL_RG8
source_format = GL_RG
elif channels == 1:
internal_format = GL_R16F if is_float else GL_R8
source_format = GL_RED
glsl.glBindTexture(GL_TEXTURE_3D, self.tex)
glsl.glTexImage3D(GL_TEXTURE_3D, 0, internal_format, width, height, depth, 0, source_format,
GL_FLOAT if is_float else GL_UNSIGNED_BYTE, data[20:])
glsl.glGenerateMipmap(GL_TEXTURE_3D)
class CubemapTexture(Texture):
path = ''
def __init__(self, name, path):
super().__init__(name)
self.path = path
def init(self, **kwargs):
super().init(**kwargs)
glsl.glBindTexture(GL_TEXTURE_CUBE_MAP, self.tex)
glsl.glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glsl.glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
glsl.glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE)
glsl.glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
glsl.glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
image = Image.open(self.path)
data = image.convert('RGB').tobytes()
channels = len(image.getbands())
for i in range(0, 6):
glsl.glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB8, image.width, image.width, 0, GL_RGB,
GL_UNSIGNED_BYTE, data[i*image.width**2*channels:])
glsl.glGenerateMipmap(GL_TEXTURE_CUBE_MAP)
image.close()
class EventHandler:
def event(self, ev, target, **__):
raise NotImplementedError
class InputDevice(Input, EventHandler):
dev = None
handler = None
def __init__(self, name, dev):
super().__init__(name)
self.dev = dev
self.handler = self
@property
def device(self):
return self.dev
class ClosingDevice(InputDevice):
target: InputDevice = None
def __init__(self, target):
super().__init__(target.name, target.dev)
self.target = target
@property
def device(self):
return self.target.dev
keycodes = {
EV_KEY.KEY_BACKSPACE: 8,
EV_KEY.KEY_TAB: 9,
EV_KEY.KEY_ENTER: 13,
EV_KEY.KEY_LEFTSHIFT: 16,
EV_KEY.KEY_RIGHTSHIFT: 16,
EV_KEY.KEY_LEFTCTRL: 17,
EV_KEY.KEY_RIGHTCTRL: 17,
EV_KEY.KEY_LEFTALT: 18,
EV_KEY.KEY_RIGHTALT: 18,
EV_KEY.KEY_ESC: 27,
EV_KEY.KEY_SPACE: 32,
EV_KEY.KEY_LEFT: 37,
EV_KEY.KEY_UP: 38,
EV_KEY.KEY_RIGHT: 39,
EV_KEY.KEY_DOWN: 40,
EV_KEY.KEY_0: 48,
EV_KEY.KEY_1: 49,
EV_KEY.KEY_2: 50,
EV_KEY.KEY_3: 51,
EV_KEY.KEY_4: 52,
EV_KEY.KEY_5: 53,
EV_KEY.KEY_6: 54,
EV_KEY.KEY_7: 55,
EV_KEY.KEY_8: 56,
EV_KEY.KEY_9: 57,
EV_KEY.KEY_A: 65,
EV_KEY.KEY_B: 66,
EV_KEY.KEY_C: 67,
EV_KEY.KEY_D: 68,
EV_KEY.KEY_E: 69,
EV_KEY.KEY_F: 70,
EV_KEY.KEY_G: 71,
EV_KEY.KEY_H: 72,
EV_KEY.KEY_I: 73,
EV_KEY.KEY_J: 74,
EV_KEY.KEY_K: 75,
EV_KEY.KEY_L: 76,
EV_KEY.KEY_M: 77,
EV_KEY.KEY_N: 78,
EV_KEY.KEY_O: 79,
EV_KEY.KEY_P: 80,
EV_KEY.KEY_Q: 81,
EV_KEY.KEY_R: 82,
EV_KEY.KEY_S: 83,
EV_KEY.KEY_T: 84,
EV_KEY.KEY_U: 85,
EV_KEY.KEY_V: 86,
EV_KEY.KEY_W: 87,
EV_KEY.KEY_X: 88,
EV_KEY.KEY_Y: 89,
EV_KEY.KEY_Z: 90,
EV_KEY.KEY_LEFTMETA: 91,
EV_KEY.KEY_RIGHTMETA: 92,
EV_KEY.KEY_SLASH: 191,
}
class Keyboard(InputDevice, Texture):
buffer = [0] * 256 * 3
def event(self, ev, **_):
if not ev.matches(EV_KEY):
return
code = -1
if ev.code in keycodes:
code = keycodes[ev.code]
if code < 0:
return
if ev.value == 0:
self.buffer[code] = 0
self.buffer[256 + code] = 0
elif ev.value == 1:
self.buffer[code] = 255
self.buffer[256 + code] = 255
self.buffer[256 * 2 + code] = 255 - self.buffer[256 * 2 + code]
# The keyboard device has been grabbed, so events are not sent to virtual
# devices. The main thread has to be interrupted on CTRL+C explicitly.
if ev.matches(EV_KEY.KEY_C) and self.buffer[17]:
signal.pthread_kill(threading.main_thread().ident, signal.SIGINT)
def render(self, frame, **_):
glsl.glActiveTexture(GL_TEXTURE0 + self.unit)
glsl.glBindTexture(GL_TEXTURE_2D, self.tex)
glsl.glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 256, 3, 0, GL_RED, GL_UNSIGNED_BYTE,
(c_ubyte * (256 * 3))(*self.buffer))
glsl.glGenerateMipmap(GL_TEXTURE_2D)
for i in range(256, 2 * 256):
self.buffer[i] = 0
class Mouse(InputDevice):
drag = False
resolution: (int, int) = None
def init(self, width, height, **kwargs):
super().init(width=width, height=height, **kwargs)
self.resolution = (width, height)
class ButtonMouse(Mouse):
click = False
pointer_xy = drag_start = drag_xy = (1, 1)
def event(self, ev, **_):
if ev.matches(EV_KEY):
if not ev.code == EV_KEY.BTN_LEFT:
return
if ev.value == 1:
self.click = True
self.drag_start = self.drag_xy = self.pointer_xy
self.drag = True
elif ev.value == 0:
self.drag = False
elif ev.matches(EV_REL):
if ev.code == EV_REL.REL_X:
self.pointer_xy = (max(1, min(self.pointer_xy[0] + ev.value, self.resolution[0])), self.pointer_xy[1])
elif ev.code == EV_REL.REL_Y:
self.pointer_xy = (self.pointer_xy[0], max(1, min(self.pointer_xy[1] - ev.value, self.resolution[1])))
if self.drag:
self.drag_xy = self.pointer_xy
def render(self, **_):
(z, w) = self.drag_start
if self.drag:
if not self.click:
(z, w) = (z, -w)
else:
(z, w) = (-z, -w)
glsl.glUniform4f(self.loc, c_float(self.drag_xy[0]), c_float(self.drag_xy[1]), c_float(z), c_float(w))
if self.click:
self.click = False
# TODO: rely on implicit Generic class once Python 3.12+ becomes a requirement
T = TypeVar('T')
class MultiInput(Generic[T], Input):
inputs: [T] = []
class MultiMouse(MultiInput[Mouse], EventHandler):
active: Optional[Mouse] = None
@property
def mice(self):
return self.inputs
def init(self, **kwargs):
super().init(**kwargs)
for mouse in self.mice:
mouse.init(**kwargs)
def add(self, *mice: [Mouse]):
for mouse in mice:
mouse.handler = self
self.mice.append(mouse)
def remove(self, *mice: [Mouse]):
for mouse in mice:
self.mice.remove(mouse)
mouse.handler = mouse
def event(self, ev, target, **kwargs):
target.event(ev, **kwargs)
if self.active is None and target.drag:
self.active = target
def render(self, **kwargs):
if self.active is not None:
self.active.render(**kwargs)
if not self.active.drag:
self.active = None
class _MTSlot:
touch = False
drag = (False, False)
drag_xy = drag_start = (1, 1)
class Touchscreen(InputDevice):
dev_abs_max = None
resolution: (int, int) = None
slots = []
u4fv = None
dirty = False
def init(self, width, height, **kwargs):
super().init(width=width, height=height, **kwargs)
self.dev_abs_max = (self.dev.absinfo[EV_ABS.ABS_X].maximum, self.dev.absinfo[EV_ABS.ABS_Y].maximum)
self.resolution = (width, height)
self.slots = [_MTSlot() for _ in range(self.dev.num_slots)]
self.u4fv = [0.0] * self.dev.num_slots * 4
def event(self, ev, **_):
self.dirty = True
slot = self.slots[self.dev.current_slot]
if ev.code == EV_ABS.ABS_MT_TRACKING_ID:
if ev.value >= 0:
slot.touch = True
else:
slot.drag = (False, False)
elif ev.code == EV_ABS.ABS_MT_POSITION_X:
slot.drag_xy = (ev.value / self.dev_abs_max[0] * self.resolution[0], slot.drag_xy[1])
if not slot.drag[0]:
slot.drag_start = (slot.drag_xy[0], slot.drag_start[1])
slot.drag = (True, slot.drag[1])
elif ev.code == EV_ABS.ABS_MT_POSITION_Y:
slot.drag_xy = (slot.drag_xy[0], self.resolution[1] - ev.value / self.dev_abs_max[1] * self.resolution[1])
if not slot.drag[1]:
slot.drag_start = (slot.drag_start[0], slot.drag_xy[1])
slot.drag = (slot.drag[0], True)
def render(self, **_):
if not self.dirty:
return
dirty = self.dirty
self.dirty = False
for i, slot in enumerate(self.slots):
self.u4fv[4 * i] = slot.drag_xy[0]
self.u4fv[4 * i + 1] = slot.drag_xy[1]
(z, w) = slot.drag_start
if slot.drag[0] and slot.drag[1]:
if not slot.touch:
(z, w) = (z, -w)
else:
(z, w) = (-z, -w)
self.u4fv[4 * i + 2] = z
self.u4fv[4 * i + 3] = w
if slot.touch:
slot.touch = False
self.dirty = True
if dirty:
glsl.glUniform4fv(self.loc, len(self.u4fv), (c_float * len(self.u4fv))(*self.u4fv))
class TouchMouse(Mouse):
dev_abs_max = None
_drag = (False, False)
drag_xy = drag_start = (1, 1)
touch = False
def init(self, **kwargs):
super().init(**kwargs)
self.dev_abs_max = (self.dev.absinfo[EV_ABS.ABS_X].maximum, self.dev.absinfo[EV_ABS.ABS_Y].maximum)
def event(self, ev, **_):
if ev.code == EV_KEY.BTN_TOUCH:
if ev.value == 1:
self.drag = True
self.touch = True
else:
self.drag = False
self._drag = (False, False)
elif ev.code == EV_ABS.ABS_X:
self.drag_xy = (ev.value / self.dev_abs_max[0] * self.resolution[0], self.drag_xy[1])
if not self._drag[0]:
self.drag_start = (self.drag_xy[0], self.drag_start[1])
self._drag = (True, self._drag[1])
elif ev.code == EV_ABS.ABS_Y:
self.drag_xy = (self.drag_xy[0], self.resolution[1] - ev.value / self.dev_abs_max[1] * self.resolution[1])
if not self._drag[1]:
self.drag_start = (self.drag_start[0], self.drag_xy[1])
self._drag = (self._drag[0], True)
def render(self, **_):
(z, w) = self.drag_start
if self.drag:
if not self.touch:
(z, w) = (z, -w)
else:
(z, w) = (-z, -w)
glsl.glUniform4f(self.loc, c_float(self.drag_xy[0]), c_float(self.drag_xy[1]), c_float(z), c_float(w))
if self.touch:
self.touch = False
class Trackpad(InputDevice):
dev_abs_x: (int, int) = None
dev_abs_y: (int, int) = None
resolution: (int, int) = None
slots = []
u4fv = None
dirty = False
def init(self, width, height, **kwargs):
super().init(width=width, height=height, **kwargs)
self.dev_abs_x = (self.dev.absinfo[EV_ABS.ABS_X].minimum, self.dev.absinfo[EV_ABS.ABS_X].maximum)
self.dev_abs_y = (self.dev.absinfo[EV_ABS.ABS_Y].minimum, self.dev.absinfo[EV_ABS.ABS_Y].maximum)
self.resolution = (width, height)
self.slots = [_MTSlot() for _ in range(self.dev.num_slots)]
self.u4fv = [0.0] * self.dev.num_slots * 4
def event(self, ev, **_):
self.dirty = True
slot = self.slots[self.dev.current_slot]
if ev.code == EV_ABS.ABS_MT_TRACKING_ID:
if ev.value >= 0:
slot.touch = True
else:
slot.drag = (False, False)
elif ev.code == EV_ABS.ABS_MT_POSITION_X:
slot.drag_xy = ((ev.value - self.dev_abs_x[0]) /
(self.dev_abs_x[1] - self.dev_abs_x[0]) * self.resolution[0], slot.drag_xy[1])
if not slot.drag[0]:
slot.drag_start = (slot.drag_xy[0], slot.drag_start[1])
slot.drag = (True, slot.drag[1])
elif ev.code == EV_ABS.ABS_MT_POSITION_Y:
slot.drag_xy = (slot.drag_xy[0], self.resolution[1] - (ev.value - self.dev_abs_y[0]) /
(self.dev_abs_y[1] - self.dev_abs_y[0]) * self.resolution[1])
if not slot.drag[1]:
slot.drag_start = (slot.drag_start[0], slot.drag_xy[1])
slot.drag = (slot.drag[0], True)
def render(self, **_):
if not self.dirty:
return
dirty = self.dirty
self.dirty = False
for i, slot in enumerate(self.slots):
self.u4fv[4 * i] = slot.drag_xy[0]
self.u4fv[4 * i + 1] = slot.drag_xy[1]
(z, w) = slot.drag_start
if slot.drag[0] and slot.drag[1]:
if not slot.touch:
(z, w) = (z, -w)
else:
(z, w) = (-z, -w)
self.u4fv[4 * i + 2] = z
self.u4fv[4 * i + 3] = w
if slot.touch:
slot.touch = False
self.dirty = True
if dirty:
glsl.glUniform4fv(self.loc, len(self.u4fv), (c_float * len(self.u4fv))(*self.u4fv))
class TrackMouse(Mouse):
dev_abs_x: (int, int) = None
dev_abs_y: (int, int) = None
resolution: (int, int) = None
_drag = (False, False)
drag_xy = drag_start = (1, 1)
touch = False
def init(self, width, height, **kwargs):
super().init(width=width, height=height, **kwargs)
self.dev_abs_x = (self.dev.absinfo[EV_ABS.ABS_X].minimum, self.dev.absinfo[EV_ABS.ABS_X].maximum)
self.dev_abs_y = (self.dev.absinfo[EV_ABS.ABS_Y].minimum, self.dev.absinfo[EV_ABS.ABS_Y].maximum)
self.resolution = (width, height)
def event(self, ev, **_):
if ev.code == EV_KEY.BTN_TOUCH:
if ev.value == 1:
self.drag = True
self.touch = True
else:
self.drag = False
self._drag = (False, False)
elif ev.code == EV_ABS.ABS_X:
self.drag_xy = ((ev.value - self.dev_abs_x[0]) /
(self.dev_abs_x[1] - self.dev_abs_x[0]) * self.resolution[0], self.drag_xy[1])
if not self._drag[0]:
self.drag_start = (self.drag_xy[0], self.drag_start[1])
self._drag = (True, self._drag[1])
elif ev.code == EV_ABS.ABS_Y:
self.drag_xy = (self.drag_xy[0], self.resolution[1] - (ev.value - self.dev_abs_y[0]) /
(self.dev_abs_y[1] - self.dev_abs_y[0]) * self.resolution[1])
if not self._drag[1]:
self.drag_start = (self.drag_start[0], self.drag_xy[1])
self._drag = (self._drag[0], True)
def render(self, **_):
(z, w) = self.drag_start
if self.drag:
if not self.touch:
(z, w) = (z, -w)
else:
(z, w) = (-z, -w)
glsl.glUniform4f(self.loc, c_float(self.drag_xy[0]), c_float(self.drag_xy[1]), c_float(z), c_float(w))
if self.touch:
self.touch = False