-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathtest_keycode_mapper.py
1369 lines (1105 loc) · 52.6 KB
/
test_keycode_mapper.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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# input-remapper - GUI for device specific keyboard mappings
# Copyright (C) 2022 sezanzeb <[email protected]>
#
# This file is part of input-remapper.
#
# input-remapper 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.
#
# input-remapper is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.
from tests.test import (
new_event,
UInput,
uinput_write_history,
quick_cleanup,
InputDevice,
MAX_ABS,
MIN_ABS,
)
import unittest
import asyncio
import time
from evdev.ecodes import (
EV_KEY,
EV_ABS,
KEY_A,
KEY_B,
KEY_C,
BTN_TL,
ABS_HAT0X,
ABS_HAT0Y,
ABS_HAT1X,
ABS_HAT1Y,
ABS_Y,
)
from inputremapper.event_combination import EventCombination
from inputremapper.injection.consumers.keycode_mapper import (
active_macros,
KeycodeMapper,
unreleased,
subsets,
)
from inputremapper.configs.system_mapping import system_mapping
from inputremapper.injection.macros.parse import parse
from inputremapper.injection.context import Context
from inputremapper.utils import RELEASE, PRESS
from inputremapper.configs.global_config import global_config, BUTTONS
from inputremapper.configs.preset import Preset
from inputremapper.configs.system_mapping import DISABLE_CODE
from inputremapper.injection.global_uinputs import global_uinputs
def wait(func, timeout=1.0):
"""Wait for func to return True."""
iterations = 0
sleepytime = 0.1
while not func():
time.sleep(sleepytime)
iterations += 1
if iterations * sleepytime > timeout:
raise Exception("Timeout")
def calculate_event_number(holdtime, before, after):
"""Calculate how many events a k(a).h(k(b)).k(c) macro would inject
Parameters
----------
holdtime : int
in ms, how long was the key held down
before : int
how many extra k() calls are executed before h()
after : int
how many extra k() calls are executed after h()
"""
keystroke_sleep = global_config.get("macros.keystroke_sleep_ms", 10)
# down and up: two sleeps per k
# one initial k(a):
events = before * 2
holdtime -= keystroke_sleep * 2
# because it sleeps for a millisecond to prevent freezes if keystroke_sleep_ms is 0
holdtime -= 1
# hold events
events += (holdtime / (keystroke_sleep * 2)) * 2
# one trailing k(c)
events += after * 2
return events
class TestKeycodeMapper(unittest.IsolatedAsyncioTestCase):
def setUp(self):
self.mapping = Preset()
self.context = Context(self.mapping)
self.source = InputDevice("/dev/input/event11")
self.history = []
def tearDown(self):
# make sure all macros are stopped by tests
self.history = []
for macro in active_macros.values():
if macro.is_holding():
macro.release_trigger()
asyncio.get_event_loop().run_until_complete(asyncio.sleep(0.01))
self.assertFalse(macro.is_holding())
self.assertFalse(macro.running)
quick_cleanup()
def setup_keycode_mapper(self, keycodes, macro_mapping):
"""Setup a default keycode mapper than can be used for most tests."""
system_mapping.clear()
for key, code in keycodes.items():
system_mapping._set(key, code)
# parse requires an intact system_mapping!
self.context.macros = {
key: (parse(code, self.context), "keyboard")
for key, code in macro_mapping.items()
}
uinput = UInput()
self.context.uinput = uinput
keycode_mapper = KeycodeMapper(self.context, self.source, UInput())
keycode_mapper.macro_write = self.create_handler
return keycode_mapper
def create_handler(self, _):
# to reduce the likelihood of race conditions of macros that for some reason
# are still running after the test, make sure they don't access the history
# of the next test.
history = self.history
return lambda *args: history.append(args)
async def test_subsets(self):
a = subsets(((1,), (2,), (3,)))
self.assertIn(((1,), (2,)), a)
self.assertIn(((2,), (3,)), a)
self.assertIn(((1,), (3,)), a)
self.assertIn(((1,), (2,), (3,)), a)
self.assertEqual(len(a), 4)
b = subsets(((1,), (2,)))
self.assertIn(((1,), (2,)), b)
self.assertEqual(len(b), 1)
c = subsets(((1,),))
self.assertEqual(len(c), 0)
async def test_d_pad(self):
ev_1 = (EV_ABS, ABS_HAT0X, 1)
ev_2 = (EV_ABS, ABS_HAT0X, -1)
ev_3 = (EV_ABS, ABS_HAT0X, 0)
ev_4 = (EV_ABS, ABS_HAT0Y, 1)
ev_5 = (EV_ABS, ABS_HAT0Y, -1)
ev_6 = (EV_ABS, ABS_HAT0Y, 0)
uinput = UInput()
self.context.uinput = uinput
self.context.key_to_code = {
EventCombination(ev_1): (51, "keyboard"),
EventCombination(ev_2): (52, "keyboard"),
EventCombination(ev_4): (54, "keyboard"),
EventCombination(ev_5): (55, "keyboard"),
}
keycode_mapper = KeycodeMapper(self.context, self.source, uinput)
# a bunch of d-pad key down events at once
await keycode_mapper.notify(new_event(*ev_1))
await keycode_mapper.notify(new_event(*ev_4))
self.assertEqual(len(unreleased), 2)
self.assertEqual(
unreleased.get(ev_1[:2]).target,
(EV_KEY, *self.context.key_to_code[(ev_1,)]),
)
self.assertEqual(unreleased.get(ev_1[:2]).input_event_tuple, ev_1)
self.assertEqual(
unreleased.get(ev_1[:2]).triggered_key, (ev_1,)
) # as seen in key_to_code
self.assertEqual(
unreleased.get(ev_4[:2]).target,
(EV_KEY, *self.context.key_to_code[(ev_4,)]),
ev_4,
)
self.assertEqual(unreleased.get(ev_4[:2]).input_event_tuple, ev_4)
self.assertEqual(unreleased.get(ev_4[:2]).triggered_key, (ev_4,))
# release all of them
await keycode_mapper.notify(new_event(*ev_3))
await keycode_mapper.notify(new_event(*ev_6))
self.assertEqual(len(unreleased), 0)
# repeat with other values
await keycode_mapper.notify(new_event(*ev_2))
await keycode_mapper.notify(new_event(*ev_5))
self.assertEqual(len(unreleased), 2)
self.assertEqual(
unreleased.get(ev_2[:2]).target,
(EV_KEY, *self.context.key_to_code[(ev_2,)]),
)
self.assertEqual(unreleased.get(ev_2[:2]).input_event_tuple, ev_2)
self.assertEqual(
unreleased.get(ev_5[:2]).target,
(EV_KEY, *self.context.key_to_code[(ev_5,)]),
)
self.assertEqual(unreleased.get(ev_5[:2]).input_event_tuple, ev_5)
# release all of them again
await keycode_mapper.notify(new_event(*ev_3))
await keycode_mapper.notify(new_event(*ev_6))
self.assertEqual(len(unreleased), 0)
self.assertEqual(len(uinput_write_history), 8)
self.assertEqual(uinput_write_history[0].t, (EV_KEY, 51, 1))
self.assertEqual(uinput_write_history[1].t, (EV_KEY, 54, 1))
self.assertEqual(uinput_write_history[2].t, (EV_KEY, 51, 0))
self.assertEqual(uinput_write_history[3].t, (EV_KEY, 54, 0))
self.assertEqual(uinput_write_history[4].t, (EV_KEY, 52, 1))
self.assertEqual(uinput_write_history[5].t, (EV_KEY, 55, 1))
self.assertEqual(uinput_write_history[6].t, (EV_KEY, 52, 0))
self.assertEqual(uinput_write_history[7].t, (EV_KEY, 55, 0))
async def test_not_forward(self):
down = (EV_KEY, 91, 1)
up = (EV_KEY, 91, 0)
uinput = global_uinputs.devices["keyboard"]
keycode_mapper = KeycodeMapper(self.context, self.source, uinput)
keycode_mapper.handle_keycode(new_event(*down), PRESS, forward=False)
self.assertEqual(unreleased[(EV_KEY, 91)].input_event_tuple, down)
self.assertEqual(unreleased[(EV_KEY, 91)].target, (*down[:2], None))
self.assertEqual(len(unreleased), 1)
self.assertEqual(uinput.write_count, 0)
keycode_mapper.handle_keycode(new_event(*up), RELEASE, forward=False)
self.assertEqual(len(unreleased), 0)
self.assertEqual(uinput.write_count, 0)
async def test_release_joystick_button(self):
# with the left joystick mapped as button, it will release the mapped
# key when it goes back to close to its resting position
ev_1 = (3, 0, MAX_ABS // 10) # release
ev_3 = (3, 0, MIN_ABS) # press
uinput = UInput()
_key_to_code = {EventCombination((3, 0, -1)): (73, "keyboard")}
self.mapping.set("gamepad.joystick.left_purpose", BUTTONS)
# something with gamepad capabilities
source = InputDevice("/dev/input/event30")
self.context.uinput = uinput
self.context.key_to_code = _key_to_code
keycode_mapper = KeycodeMapper(self.context, source, uinput)
await keycode_mapper.notify(new_event(*ev_3))
await keycode_mapper.notify(new_event(*ev_1))
# array of 3-tuples
self.history = [a.t for a in uinput_write_history]
self.assertIn((EV_KEY, 73, 1), self.history)
self.assertEqual(self.history.count((EV_KEY, 73, 1)), 1)
self.assertIn((EV_KEY, 73, 0), self.history)
self.assertEqual(self.history.count((EV_KEY, 73, 0)), 1)
async def test_dont_filter_unmapped(self):
# if an event is not used at all, it should be written but not
# furthermore modified. For example wheel events
# keep reporting events of the same value without a release inbetween,
# they should be forwarded.
down = (EV_KEY, 91, 1)
up = (EV_KEY, 91, 0)
uinput = global_uinputs.devices["keyboard"]
forward_to = UInput()
keycode_mapper = KeycodeMapper(self.context, self.source, forward_to)
for _ in range(10):
# don't filter duplicate events if not mapped
await keycode_mapper.notify(new_event(*down))
self.assertEqual(unreleased[(EV_KEY, 91)].input_event_tuple, down)
self.assertEqual(unreleased[(EV_KEY, 91)].target, (*down[:2], None))
self.assertEqual(len(unreleased), 1)
self.assertEqual(forward_to.write_count, 10)
self.assertEqual(uinput.write_count, 0)
await keycode_mapper.notify(new_event(*up))
self.assertEqual(len(unreleased), 0)
self.assertEqual(forward_to.write_count, 11)
self.assertEqual(uinput.write_count, 0)
async def test_filter_combi_mapped_duplicate_down(self):
# the opposite of the other test, but don't map the key directly
# but rather as the trigger for a combination
down_1 = (EV_KEY, 91, 1)
down_2 = (EV_KEY, 92, 1)
up_1 = (EV_KEY, 91, 0)
up_2 = (EV_KEY, 92, 0)
# forwarded and mapped event will end up at the same place
forward = global_uinputs.devices["keyboard"]
output = 71
key_to_code = {EventCombination(down_1, down_2): (71, "keyboard")}
self.context.key_to_code = key_to_code
keycode_mapper = KeycodeMapper(self.context, self.source, forward)
await keycode_mapper.notify(new_event(*down_1))
for _ in range(10):
await keycode_mapper.notify(new_event(*down_2))
# all duplicate down events should have been ignored
self.assertEqual(len(unreleased), 2)
self.assertEqual(forward.write_count, 2)
self.assertEqual(uinput_write_history[0].t, down_1)
self.assertEqual(uinput_write_history[1].t, (EV_KEY, output, 1))
await keycode_mapper.notify(new_event(*up_1))
await keycode_mapper.notify(new_event(*up_2))
self.assertEqual(len(unreleased), 0)
self.assertEqual(forward.write_count, 4)
self.assertEqual(uinput_write_history[2].t, up_1)
self.assertEqual(uinput_write_history[3].t, (EV_KEY, output, 0))
async def test_d_pad_combination(self):
ev_1 = (EV_ABS, ABS_HAT0X, 1)
ev_2 = (EV_ABS, ABS_HAT0Y, -1)
ev_3 = (EV_ABS, ABS_HAT0X, 0)
ev_4 = (EV_ABS, ABS_HAT0Y, 0)
_key_to_code = {
EventCombination(ev_1, ev_2): (51, "keyboard"),
EventCombination(ev_2): (52, "keyboard"),
}
uinput = UInput()
self.context.uinput = uinput
self.context.key_to_code = _key_to_code
keycode_mapper = KeycodeMapper(self.context, self.source, uinput)
# a bunch of d-pad key down events at once
await keycode_mapper.notify(new_event(*ev_1))
await keycode_mapper.notify(new_event(*ev_2))
# (what_will_be_released, what_caused_the_key_down)
self.assertEqual(unreleased.get(ev_1[:2]).target, (EV_ABS, ABS_HAT0X, None))
self.assertEqual(unreleased.get(ev_1[:2]).input_event_tuple, ev_1)
self.assertEqual(unreleased.get(ev_2[:2]).target, (EV_KEY, 51, "keyboard"))
self.assertEqual(unreleased.get(ev_2[:2]).input_event_tuple, ev_2)
self.assertEqual(len(unreleased), 2)
# ev_1 is unmapped and the other is the triggered combination
self.assertEqual(len(uinput_write_history), 2)
self.assertEqual(uinput_write_history[0].t, ev_1)
self.assertEqual(uinput_write_history[1].t, (EV_KEY, 51, 1))
# release all of them
await keycode_mapper.notify(new_event(*ev_3))
await keycode_mapper.notify(new_event(*ev_4))
self.assertEqual(len(unreleased), 0)
self.assertEqual(len(uinput_write_history), 4)
self.assertEqual(uinput_write_history[2].t, ev_3)
self.assertEqual(uinput_write_history[3].t, (EV_KEY, 51, 0))
async def test_notify(self):
code_2 = 2
# this also makes sure that the keycode_mapper doesn't get confused
# when input and output codes are the same (because it at some point
# screwed it up because of that)
_key_to_code = {
EventCombination((EV_KEY, 1, 1)): (101, "keyboard"),
EventCombination((EV_KEY, code_2, 1)): (code_2, "keyboard"),
}
uinput_mapped = global_uinputs.devices["keyboard"]
uinput_forwarded = UInput()
self.context.key_to_code = _key_to_code
keycode_mapper = KeycodeMapper(self.context, self.source, uinput_forwarded)
await keycode_mapper.notify(new_event(EV_KEY, 1, 1))
await keycode_mapper.notify(new_event(EV_KEY, 3, 1))
await keycode_mapper.notify(new_event(EV_KEY, code_2, 1))
await keycode_mapper.notify(new_event(EV_KEY, code_2, 0))
self.assertEqual(len(uinput_write_history), 4)
self.assertEqual(uinput_mapped.write_history[0].t, (EV_KEY, 101, 1))
self.assertEqual(uinput_mapped.write_history[1].t, (EV_KEY, code_2, 1))
self.assertEqual(uinput_mapped.write_history[2].t, (EV_KEY, code_2, 0))
self.assertEqual(uinput_forwarded.write_history[0].t, (EV_KEY, 3, 1))
async def test_combination_keycode(self):
combination = EventCombination((EV_KEY, 1, 1), (EV_KEY, 2, 1))
_key_to_code = {combination: (101, "keyboard")}
uinput = UInput()
self.context.uinput = uinput
self.context.key_to_code = _key_to_code
keycode_mapper = KeycodeMapper(self.context, self.source, uinput)
await keycode_mapper.notify(combination[0])
await keycode_mapper.notify(combination[1])
self.assertEqual(len(uinput_write_history), 2)
# the first event is written and then the triggered combination
self.assertEqual(uinput_write_history[0].t, (EV_KEY, 1, 1))
self.assertEqual(uinput_write_history[1].t, (EV_KEY, 101, 1))
# release them
await keycode_mapper.notify(combination[0].modify(value=0))
await keycode_mapper.notify(combination[1].modify(value=0))
# the first key writes its release event. The second key is hidden
# behind the executed combination. The result of the combination is
# also released, because it acts like a key.
self.assertEqual(len(uinput_write_history), 4)
self.assertEqual(uinput_write_history[2].t, (EV_KEY, 1, 0))
self.assertEqual(uinput_write_history[3].t, (EV_KEY, 101, 0))
# press them in the wrong order (the wrong key at the end, the order
# of all other keys won't matter). no combination should be triggered
await keycode_mapper.notify(combination[1])
await keycode_mapper.notify(combination[0])
self.assertEqual(len(uinput_write_history), 6)
self.assertEqual(uinput_write_history[4].t, (EV_KEY, 2, 1))
self.assertEqual(uinput_write_history[5].t, (EV_KEY, 1, 1))
async def test_combination_keycode_2(self):
combination_1 = EventCombination(
(EV_KEY, 1, 1),
(EV_ABS, ABS_Y, MIN_ABS),
(EV_KEY, 3, 1),
(EV_KEY, 4, 1),
)
combination_2 = EventCombination(
# should not be triggered, combination_1 should be prioritized
# when all of its keys are down
(EV_KEY, 2, 1),
(EV_KEY, 3, 1),
(EV_KEY, 4, 1),
)
down_5 = (EV_KEY, 5, 1)
up_5 = (EV_KEY, 5, 0)
up_4 = (EV_KEY, 4, 0)
def sign_value(event):
return event.modify(value=event.value / abs(event.value))
_key_to_code = {
# key_to_code is supposed to only contain values classified into PRESS,
# PRESS_NEGATIVE and RELEASE
EventCombination(*[sign_value(a) for a in combination_1]): (
101,
"keyboard",
),
combination_2: (102, "keyboard"),
EventCombination(down_5): (103, "keyboard"),
}
uinput = UInput()
source = InputDevice("/dev/input/event30")
# ABS_Y is part of the combination, which only works if the joystick
# is configured as D-Pad
self.mapping.set("gamepad.joystick.left_purpose", BUTTONS)
self.context.uinput = uinput
self.context.key_to_code = _key_to_code
keycode_mapper = KeycodeMapper(self.context, source, uinput)
self.assertIsNotNone(keycode_mapper._abs_range)
# 10 and 11: insert some more arbitrary key-down events,
# they should not break the combinations
await keycode_mapper.notify(new_event(EV_KEY, 10, 1))
await keycode_mapper.notify(combination_1[0])
await keycode_mapper.notify(combination_1[1])
await keycode_mapper.notify(combination_1[2])
await keycode_mapper.notify(new_event(EV_KEY, 11, 1))
await keycode_mapper.notify(combination_1[3])
# combination_1 should have been triggered now
self.assertEqual(len(uinput_write_history), 6)
# the first events are written and then the triggered combination,
# while the triggering event is the only one that is omitted
self.assertEqual(uinput_write_history[1].t, combination_1[0])
self.assertEqual(uinput_write_history[2].t, combination_1[1])
self.assertEqual(uinput_write_history[3].t, combination_1[2])
self.assertEqual(uinput_write_history[5].t, (EV_KEY, 101, 1))
# while the combination is down, another unrelated key can be used
await keycode_mapper.notify(new_event(*down_5))
# the keycode_mapper searches for subsets of the current held-down
# keys to activate combinations, down_5 should not trigger them
# again.
self.assertEqual(len(uinput_write_history), 7)
self.assertEqual(uinput_write_history[6].t, (EV_KEY, 103, 1))
# release the combination by releasing the last key, and release
# the unrelated key
await keycode_mapper.notify(new_event(*up_4))
await keycode_mapper.notify(new_event(*up_5))
self.assertEqual(len(uinput_write_history), 9)
self.assertEqual(uinput_write_history[7].t, (EV_KEY, 101, 0))
self.assertEqual(uinput_write_history[8].t, (EV_KEY, 103, 0))
async def test_macro_writes_to_global_uinput(self):
macro_mapping = {
((EV_KEY, 1, 1),): (parse("k(a)", self.context), "keyboard"),
}
self.context.macros = macro_mapping
forward_to = UInput()
keycode_mapper = KeycodeMapper(self.context, self.source, forward_to)
await keycode_mapper.notify(new_event(EV_KEY, 1, 1))
sleeptime = global_config.get("macros.keystroke_sleep_ms", 10) * 12
await asyncio.sleep(sleeptime / 1000 + 0.1)
self.assertEqual(
global_uinputs.devices["keyboard"].write_count, 2
) # down and up
self.assertEqual(forward_to.write_count, 0)
await keycode_mapper.notify(new_event(EV_KEY, 2, 1))
self.assertEqual(forward_to.write_count, 1)
async def test_notify_macro(self):
code_a, code_b = 100, 101
keycode_mapper = self.setup_keycode_mapper(
{"a": code_a, "b": code_b},
{
((EV_KEY, 1, 1),): "k(a)",
((EV_KEY, 2, 1),): "r(5, k(b))",
},
)
await keycode_mapper.notify(new_event(EV_KEY, 1, 1))
await keycode_mapper.notify(new_event(EV_KEY, 2, 1))
sleeptime = global_config.get("macros.keystroke_sleep_ms", 10) * 12
# let the mainloop run for some time so that the macro does its stuff
await asyncio.sleep(sleeptime / 1000 + 0.1)
# 6 keycodes written, with down and up events
self.assertEqual(len(self.history), 12)
self.assertIn((EV_KEY, code_a, 1), self.history)
self.assertIn((EV_KEY, code_a, 0), self.history)
self.assertIn((EV_KEY, code_b, 1), self.history)
self.assertIn((EV_KEY, code_b, 0), self.history)
# releasing stuff
self.assertIn((EV_KEY, 1), unreleased)
self.assertIn((EV_KEY, 2), unreleased)
await keycode_mapper.notify(new_event(EV_KEY, 1, 0))
await keycode_mapper.notify(new_event(EV_KEY, 2, 0))
self.assertNotIn((EV_KEY, 1), unreleased)
self.assertNotIn((EV_KEY, 2), unreleased)
await asyncio.sleep(0.1)
self.assertEqual(len(self.history), 12)
async def test_if_single(self):
code_a, code_b = 100, 101
keycode_mapper = self.setup_keycode_mapper(
{"a": code_a, "b": code_b}, {((EV_KEY, 1, 1),): "if_single(k(a), k(b))"}
)
"""triggers then"""
await keycode_mapper.notify(new_event(EV_KEY, 1, 1)) # start the macro
await asyncio.sleep(0.05)
self.assertTrue(active_macros[(EV_KEY, 1)].running)
await keycode_mapper.notify(new_event(EV_KEY, 1, 0))
await asyncio.sleep(0.05)
self.assertFalse(active_macros[(EV_KEY, 1)].running)
self.assertIn((EV_KEY, code_a, 1), self.history)
self.assertIn((EV_KEY, code_a, 0), self.history)
self.assertNotIn((EV_KEY, code_b, 1), self.history)
self.assertNotIn((EV_KEY, code_b, 0), self.history)
"""triggers else"""
self.history.clear()
await keycode_mapper.notify(new_event(EV_KEY, 1, 1)) # start the macro
await asyncio.sleep(0.05)
self.assertTrue(active_macros[(EV_KEY, 1)].running)
await keycode_mapper.notify(new_event(EV_KEY, 2, 1))
await asyncio.sleep(0.05)
self.assertFalse(active_macros[(EV_KEY, 1)].running)
self.assertNotIn((EV_KEY, code_a, 1), self.history)
self.assertNotIn((EV_KEY, code_a, 0), self.history)
self.assertIn((EV_KEY, code_b, 1), self.history)
self.assertIn((EV_KEY, code_b, 0), self.history)
async def test_hold(self):
code_a, code_b, code_c = 100, 101, 102
keycode_mapper = self.setup_keycode_mapper(
{"a": code_a, "b": code_b, "c": code_c},
{((EV_KEY, 1, 1),): "k(a).h(k(b)).k(c)"},
)
"""start macro"""
await keycode_mapper.notify(new_event(EV_KEY, 1, 1))
# let the mainloop run for some time so that the macro does its stuff
sleeptime = 500
keystroke_sleep = global_config.get("macros.keystroke_sleep_ms", 10)
await asyncio.sleep(sleeptime / 1000)
self.assertTrue(active_macros[(EV_KEY, 1)].is_holding())
self.assertTrue(active_macros[(EV_KEY, 1)].running)
"""stop macro"""
await keycode_mapper.notify(new_event(EV_KEY, 1, 0))
await asyncio.sleep(keystroke_sleep * 10 / 1000)
events = calculate_event_number(sleeptime, 1, 1)
self.assertGreater(len(self.history), events * 0.9)
self.assertLess(len(self.history), events * 1.1)
self.assertIn((EV_KEY, code_a, 1), self.history)
self.assertIn((EV_KEY, code_a, 0), self.history)
self.assertIn((EV_KEY, code_b, 1), self.history)
self.assertIn((EV_KEY, code_b, 0), self.history)
self.assertIn((EV_KEY, code_c, 1), self.history)
self.assertIn((EV_KEY, code_c, 0), self.history)
self.assertGreater(self.history.count((EV_KEY, code_b, 1)), 1)
self.assertGreater(self.history.count((EV_KEY, code_b, 0)), 1)
# it's stopped and won't write stuff anymore
count_before = len(self.history)
await asyncio.sleep(0.2)
count_after = len(self.history)
self.assertEqual(count_before, count_after)
self.assertFalse(active_macros[(EV_KEY, 1)].is_holding())
self.assertFalse(active_macros[(EV_KEY, 1)].running)
async def test_hold_2(self):
# test irregular input patterns
code_a, code_b, code_c, code_d = 100, 101, 102, 103
keycode_mapper = self.setup_keycode_mapper(
{"a": code_a, "b": code_b, "c": code_c, "d": code_d},
{
((EV_KEY, 1, 1),): "h(k(b))",
((EV_KEY, 2, 1),): "k(c).r(1, r(1, r(1, h(k(a))))).k(d)",
((EV_KEY, 3, 1),): "h(k(b))",
},
)
"""start macro 2"""
await keycode_mapper.notify(new_event(EV_KEY, 2, 1))
await asyncio.sleep(0.1)
# starting code_c written
self.assertEqual(self.history.count((EV_KEY, code_c, 1)), 1)
self.assertEqual(self.history.count((EV_KEY, code_c, 0)), 1)
# spam garbage events
for _ in range(5):
await keycode_mapper.notify(new_event(EV_KEY, 1, 1))
await keycode_mapper.notify(new_event(EV_KEY, 3, 1))
await asyncio.sleep(0.05)
self.assertTrue(active_macros[(EV_KEY, 1)].is_holding())
self.assertTrue(active_macros[(EV_KEY, 1)].running)
self.assertTrue(active_macros[(EV_KEY, 2)].is_holding())
self.assertTrue(active_macros[(EV_KEY, 2)].running)
self.assertTrue(active_macros[(EV_KEY, 3)].is_holding())
self.assertTrue(active_macros[(EV_KEY, 3)].running)
# there should only be one code_c in the events, because no key
# up event was ever done so the hold just continued
self.assertEqual(self.history.count((EV_KEY, code_c, 1)), 1)
self.assertEqual(self.history.count((EV_KEY, code_c, 0)), 1)
# without an key up event on 2, it won't write code_d
self.assertNotIn((code_d, 1), self.history)
self.assertNotIn((code_d, 0), self.history)
# stop macro 2
await keycode_mapper.notify(new_event(EV_KEY, 2, 0))
await asyncio.sleep(0.1)
# it stopped and didn't restart, so the count stays at 1
self.assertEqual(self.history.count((EV_KEY, code_c, 1)), 1)
self.assertEqual(self.history.count((EV_KEY, code_c, 0)), 1)
# and the trailing d was written
self.assertEqual(self.history.count((EV_KEY, code_d, 1)), 1)
self.assertEqual(self.history.count((EV_KEY, code_d, 0)), 1)
# it's stopped and won't write stuff anymore
count_before = self.history.count((EV_KEY, code_a, 1))
self.assertGreater(count_before, 1)
await asyncio.sleep(0.1)
count_after = self.history.count((EV_KEY, code_a, 1))
self.assertEqual(count_before, count_after)
"""restart macro 2"""
self.history.clear()
await keycode_mapper.notify(new_event(EV_KEY, 2, 1))
await asyncio.sleep(0.1)
self.assertEqual(self.history.count((EV_KEY, code_c, 1)), 1)
self.assertEqual(self.history.count((EV_KEY, code_c, 0)), 1)
# spam garbage events again, this time key-up events on all other
# macros
for _ in range(5):
await keycode_mapper.notify(new_event(EV_KEY, 1, 0))
await keycode_mapper.notify(new_event(EV_KEY, 3, 0))
await asyncio.sleep(0.05)
self.assertFalse(active_macros[(EV_KEY, 1)].is_holding())
self.assertFalse(active_macros[(EV_KEY, 1)].running)
self.assertTrue(active_macros[(EV_KEY, 2)].is_holding())
self.assertTrue(active_macros[(EV_KEY, 2)].running)
self.assertFalse(active_macros[(EV_KEY, 3)].is_holding())
self.assertFalse(active_macros[(EV_KEY, 3)].running)
# stop macro 2
await keycode_mapper.notify(new_event(EV_KEY, 2, 0))
await asyncio.sleep(0.1)
# was started only once
self.assertEqual(self.history.count((EV_KEY, code_c, 1)), 1)
self.assertEqual(self.history.count((EV_KEY, code_c, 0)), 1)
# and the trailing d was also written only once
self.assertEqual(self.history.count((EV_KEY, code_d, 1)), 1)
self.assertEqual(self.history.count((EV_KEY, code_d, 0)), 1)
# stop all macros
await keycode_mapper.notify(new_event(EV_KEY, 1, 0))
await keycode_mapper.notify(new_event(EV_KEY, 3, 0))
await asyncio.sleep(0.1)
# it's stopped and won't write stuff anymore
count_before = len(self.history)
await asyncio.sleep(0.1)
count_after = len(self.history)
self.assertEqual(count_before, count_after)
self.assertFalse(active_macros[(EV_KEY, 1)].is_holding())
self.assertFalse(active_macros[(EV_KEY, 1)].running)
self.assertFalse(active_macros[(EV_KEY, 2)].is_holding())
self.assertFalse(active_macros[(EV_KEY, 2)].running)
self.assertFalse(active_macros[(EV_KEY, 3)].is_holding())
self.assertFalse(active_macros[(EV_KEY, 3)].running)
async def test_hold_3(self):
# test irregular input patterns
code_a, code_b, code_c = 100, 101, 102
keycode_mapper = self.setup_keycode_mapper(
{"a": code_a, "b": code_b, "c": code_c},
{((EV_KEY, 1, 1),): "k(a).h(k(b)).k(c)"},
)
await keycode_mapper.notify(new_event(EV_KEY, 1, 1))
await asyncio.sleep(0.1)
for _ in range(5):
self.assertTrue(active_macros[(EV_KEY, 1)].is_holding())
self.assertTrue(active_macros[(EV_KEY, 1)].running)
await keycode_mapper.notify(new_event(EV_KEY, 1, 1))
await asyncio.sleep(0.05)
# duplicate key down events don't do anything
self.assertEqual(self.history.count((EV_KEY, code_a, 1)), 1)
self.assertEqual(self.history.count((EV_KEY, code_a, 0)), 1)
self.assertEqual(self.history.count((EV_KEY, code_c, 1)), 0)
self.assertEqual(self.history.count((EV_KEY, code_c, 0)), 0)
# stop
await keycode_mapper.notify(new_event(EV_KEY, 1, 0))
await asyncio.sleep(0.1)
self.assertEqual(self.history.count((EV_KEY, code_a, 1)), 1)
self.assertEqual(self.history.count((EV_KEY, code_a, 0)), 1)
self.assertEqual(self.history.count((EV_KEY, code_c, 1)), 1)
self.assertEqual(self.history.count((EV_KEY, code_c, 0)), 1)
self.assertFalse(active_macros[(EV_KEY, 1)].is_holding())
self.assertFalse(active_macros[(EV_KEY, 1)].running)
# it's stopped and won't write stuff anymore
count_before = len(self.history)
await asyncio.sleep(0.1)
count_after = len(self.history)
self.assertEqual(count_before, count_after)
async def test_hold_two(self):
# holding two macros at the same time,
# the first one is triggered by a combination
key_0 = (EV_KEY, 10)
key_1 = (EV_KEY, 11)
key_2 = (EV_ABS, ABS_HAT0X)
down_0 = (*key_0, 1)
down_1 = (*key_1, 1)
down_2 = (*key_2, -1)
up_0 = (*key_0, 0)
up_1 = (*key_1, 0)
up_2 = (*key_2, 0)
code_1, code_2, code_3, code_a, code_b, code_c = 100, 101, 102, 103, 104, 105
keycode_mapper = self.setup_keycode_mapper(
{1: code_1, 2: code_2, 3: code_3, "a": code_a, "b": code_b, "c": code_c},
{
(down_0, down_1): "k(1).h(k(2)).k(3)",
(down_2,): "k(a).h(k(b)).k(c)",
},
)
# key up won't do anything
await keycode_mapper.notify(new_event(*up_0))
await keycode_mapper.notify(new_event(*up_1))
await keycode_mapper.notify(new_event(*up_2))
await asyncio.sleep(0.1)
self.assertEqual(len(active_macros), 0)
"""start macros"""
uinput_2 = UInput()
self.context.uinput = uinput_2
keycode_mapper = KeycodeMapper(self.context, self.source, uinput_2)
keycode_mapper.macro_write = self.create_handler
await keycode_mapper.notify(new_event(*down_0))
self.assertEqual(uinput_2.write_count, 1)
await keycode_mapper.notify(new_event(*down_1))
await keycode_mapper.notify(new_event(*down_2))
self.assertEqual(uinput_2.write_count, 1)
# let the mainloop run for some time so that the macro does its stuff
sleeptime = 500
keystroke_sleep = global_config.get("macros.keystroke_sleep_ms", 10)
await asyncio.sleep(sleeptime / 1000)
# test that two macros are really running at the same time
self.assertEqual(len(active_macros), 2)
self.assertTrue(active_macros[key_1].is_holding())
self.assertTrue(active_macros[key_1].running)
self.assertTrue(active_macros[key_2].is_holding())
self.assertTrue(active_macros[key_2].running)
self.assertIn(down_0[:2], unreleased)
self.assertIn(down_1[:2], unreleased)
self.assertIn(down_2[:2], unreleased)
"""stop macros"""
keycode_mapper = KeycodeMapper(self.context, self.source, None)
# releasing the last key of a combination releases the whole macro
await keycode_mapper.notify(new_event(*up_1))
await keycode_mapper.notify(new_event(*up_2))
self.assertIn(down_0[:2], unreleased)
self.assertNotIn(down_1[:2], unreleased)
self.assertNotIn(down_2[:2], unreleased)
await asyncio.sleep(keystroke_sleep * 10 / 1000)
self.assertFalse(active_macros[key_1].is_holding())
self.assertFalse(active_macros[key_1].running)
self.assertFalse(active_macros[key_2].is_holding())
self.assertFalse(active_macros[key_2].running)
events = calculate_event_number(sleeptime, 1, 1) * 2
self.assertGreater(len(self.history), events * 0.9)
self.assertLess(len(self.history), events * 1.1)
self.assertIn((EV_KEY, code_a, 1), self.history)
self.assertIn((EV_KEY, code_a, 0), self.history)
self.assertIn((EV_KEY, code_b, 1), self.history)
self.assertIn((EV_KEY, code_b, 0), self.history)
self.assertIn((EV_KEY, code_c, 1), self.history)
self.assertIn((EV_KEY, code_c, 0), self.history)
self.assertIn((EV_KEY, code_1, 1), self.history)
self.assertIn((EV_KEY, code_1, 0), self.history)
self.assertIn((EV_KEY, code_2, 1), self.history)
self.assertIn((EV_KEY, code_2, 0), self.history)
self.assertIn((EV_KEY, code_3, 1), self.history)
self.assertIn((EV_KEY, code_3, 0), self.history)
self.assertGreater(self.history.count((EV_KEY, code_b, 1)), 1)
self.assertGreater(self.history.count((EV_KEY, code_b, 0)), 1)
self.assertGreater(self.history.count((EV_KEY, code_2, 1)), 1)
self.assertGreater(self.history.count((EV_KEY, code_2, 0)), 1)
# it's stopped and won't write stuff anymore
count_before = len(self.history)
await asyncio.sleep(0.2)
count_after = len(self.history)
self.assertEqual(count_before, count_after)
async def test_hold_failing_child(self):
# if a child macro fails, hold will not try to run it again.
# The exception is properly propagated through both `hold`s and the macro
# stops. If the code is broken, this test might enter an infinite loop.
code_a = 100
keycode_mapper = self.setup_keycode_mapper(
{"a": code_a},
{((EV_KEY, 1, 1),): "hold(hold(key(a)))"},
)
def macro_write(*args, **kwargs):
def f():
raise Exception("foo")
return f
keycode_mapper.macro_write = macro_write
await keycode_mapper.notify(new_event(EV_KEY, 1, 1))
await asyncio.sleep(0.1)
self.assertFalse(active_macros[(EV_KEY, 1)].running)
async def test_filter_trigger_spam(self):
# test_filter_duplicates
trigger = (EV_KEY, BTN_TL)
_key_to_code = {
EventCombination((*trigger, 1)): (51, "keyboard"),
EventCombination((*trigger, -1)): (52, "keyboard"),
}
uinput = UInput()
self.context.uinput = uinput
self.context.key_to_code = _key_to_code
keycode_mapper = KeycodeMapper(self.context, self.source, uinput)
"""positive"""
for _ in range(1, 20):
await keycode_mapper.notify(new_event(*trigger, 1))
self.assertIn(trigger, unreleased)
await keycode_mapper.notify(new_event(*trigger, 0))
self.assertNotIn(trigger, unreleased)
self.assertEqual(len(uinput_write_history), 2)
"""negative"""
for _ in range(1, 20):
await keycode_mapper.notify(new_event(*trigger, -1))
self.assertIn(trigger, unreleased)
await keycode_mapper.notify(new_event(*trigger, 0))