-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
numberpad.py
executable file
·1851 lines (1411 loc) · 63.9 KB
/
numberpad.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/env python3
import configparser
import importlib
import logging
import math
import os
import re
import subprocess
import sys
import threading
from time import sleep, time
from typing import Optional
import numpy as np
from libevdev import EV_ABS, EV_KEY, EV_LED, EV_MSC, EV_SYN, Device, InputEvent, const, device
from pyinotify import WatchManager, IN_CLOSE_WRITE, IN_IGNORED, IN_MOVED_TO, AsyncNotifier
import Xlib.display
import Xlib.X
import Xlib.XK
EV_KEY_TOP_LEFT_ICON = "EV_KEY_TOP_LEFT_ICON"
numlock: bool = False
logging.basicConfig(
format='%(asctime)s %(levelname)-8s %(message)s',
level=os.environ.get('LOG', 'INFO')
)
log = logging.getLogger('asus-numberpad-driver')
# Constants
try_times = 5
try_sleep = 0.1
gsettings_failure_count = 0
gsettings_max_failure_count = 3
getting_device_via_xinput_status_failure_count = 0
getting_device_via_xinput_status_max_failure_count = 3
getting_device_via_synclient_status_failure_count = 0
getting_device_via_synclient_status_max_failure_count = 3
# Numpad layout model
model = None
if len(sys.argv) > 1:
model = sys.argv[1]
try:
model_layout = importlib.import_module('layouts.' + model)
except:
log.error("Numpad layout *.py from dir layouts is required as first argument. Re-run install script or add missing first argument (valid value is b7402, e210ma, g533, gx551, gx701, up5401ea, ..).")
sys.exit(1)
# Config file dir
config_file_dir = ""
if len(sys.argv) > 2:
config_file_dir = sys.argv[2]
# When is given config dir empty or is used default -> to ./ because inotify needs check folder (nor nothing = "")
if config_file_dir == "":
config_file_dir = "./"
# Layout
left_offset = getattr(model_layout, "left_offset", 0)
right_offset = getattr(model_layout, "right_offset", 0)
top_offset = getattr(model_layout, "top_offset", 0)
bottom_offset = getattr(model_layout, "bottom_offset", 0)
top_left_icon_width = getattr(model_layout, "top_left_icon_width", 0)
top_left_icon_height = getattr(model_layout, "top_left_icon_height", 0)
top_right_icon_width = getattr(model_layout, "top_right_icon_width", 0)
top_right_icon_height = getattr(model_layout, "top_right_icon_height", 0)
top_left_icon_slide_func_keys = getattr(model_layout, "top_left_icon_slide_func_keys", [
EV_KEY.KEY_CALC
])
keys = getattr(model_layout, "keys", [])
if not len(keys) > 0 or not len(keys[0]) > 0:
log.error('keys is required to set, dimension has to be atleast array of len 1 inside array')
sys.exit(1)
keys_ignore_offset = getattr(model_layout, "keys_ignore_offset", [])
backlight_levels = getattr(model_layout, "backlight_levels", [])
# Config
CONFIG_FILE_NAME = "numberpad_dev"
CONFIG_SECTION = "main"
CONFIG_ENABLED = "enabled"
CONFIG_ENABLED_DEFAULT = False
CONFIG_LAST_BRIGHTNESS = "brightness"
CONFIG_DEFAULT_BACKLIGHT_LEVEL = "default_backlight_level"
CONFIG_DEFAULT_BACKLIGHT_LEVEL_DEFAULT = "0x01"
CONFIG_LEFT_ICON_ACTIVATION_TIME = "top_left_icon_activation_time"
CONFIG_LEFT_ICON_ACTIVATION_TIME_DEFAULT = True
CONFIG_TOP_LEFT_ICON_BRIGHTNESS_FUNC_DISABLED = "top_left_icon_brightness_func_disabled"
CONFIG_TOP_LEFT_ICON_BRIGHTNESS_FUNC_DISABLED_DEFAULT = False
CONFIG_TOP_LEFT_ICON_SLIDE_FUNC_ACTIVATION_X_RATIO = "top_left_icon_slide_func_activation_x_ratio"
CONFIG_TOP_LEFT_ICON_SLIDE_FUNC_ACTIVATION_X_RATIO_DEFAULT = 0.3
CONFIG_TOP_LEFT_ICON_SLIDE_FUNC_ACTIVATION_Y_RATIO = "top_left_icon_slide_func_activation_y_ratio"
CONFIG_TOP_LEFT_ICON_SLIDE_FUNC_ACTIVATION_Y_RATIO_DEFAULT = 0.3
CONFIG_TOP_RIGHT_ICON_SLIDE_FUNC_ACTIVATION_X_RATIO = "top_right_icon_slide_func_activation_x_ratio"
CONFIG_TOP_RIGHT_ICON_SLIDE_FUNC_ACTIVATION_X_RATIO_DEFAULT = 0.3
CONFIG_TOP_RIGHT_ICON_SLIDE_FUNC_ACTIVATION_Y_RATIO = "top_right_icon_slide_func_activation_y_ratio"
CONFIG_TOP_RIGHT_ICON_SLIDE_FUNC_ACTIVATION_Y_RATIO_DEFAULT = 0.3
CONFIG_NUMPAD_DISABLES_SYS_NUMLOCK = "numpad_disables_sys_numlock"
CONFIG_NUMPAD_DISABLES_SYS_NUMLOCK_DEFAULT = True
CONFIG_DISABLE_DUE_INACTIVITY_TIME = "disable_due_inactivity_time"
CONFIG_DISABLE_DUE_INACTIVITY_TIME_DEFAULT = 60
CONFIG_TOUCHPAD_DISABLES_NUMPAD = "touchpad_disables_numpad"
CONFIG_TOUCHPAD_DISABLES_NUMPAD_DEFAULT = True
CONFIG_KEY_REPETITIONS = "key_repetitions"
CONFIG_KEY_REPETITIONS_DEFAULT = False
CONFIG_MULTITOUCH = "multitouch"
CONFIG_MULTITOUCH_DEFAULT = False
CONFIG_ONE_TOUCH_KEY_ROTATION = "one_touch_key_rotation"
CONFIG_ONE_TOUCH_KEY_ROTATION_DEFAULT = False
CONFIG_ACTIVATION_TIME = "activation_time"
CONFIG_ACTIVATION_TIME_DEFAULT = True
CONFIG_NUMLOCK_ENABLES_NUMPAD = "sys_numlock_enables_numpad"
CONFIG_NUMLOCK_ENABLES_NUMPAD_DEFAULT = True
CONFIG_ENABLED_TOUCHPAD_POINTER = "enabled_touchpad_pointer"
CONFIG_ENABLED_TOUCHPAD_POINTER_DEFAULT = 3
CONFIG_PRESS_KEY_WHEN_IS_DONE_UNTOUCH = "press_key_when_is_done_untouch"
CONFIG_PRESS_KEY_WHEN_IS_DONE_UNTOUCH_DEFAULT = True
CONFIG_DISTANCE_TO_MOVE_ONLY_POINTER = "distance_to_move_only_pointer"
CONFIG_DISTANCE_TO_MOVE_ONLY_POINTER_DEFAULT = False
config_file_path = config_file_dir + CONFIG_FILE_NAME
config = configparser.ConfigParser()
config_lock = threading.Lock()
# methods for read & write from config file
def config_get(key, key_default):
try:
value = config.get(CONFIG_SECTION, key)
parsed_value = parse_value_from_config(value)
return parsed_value
except:
config.set(CONFIG_SECTION, key, parse_value_to_config(key_default))
return key_default
def send_value_to_touchpad_via_i2c(value):
global device_id
cmd = ["i2ctransfer", "-f", "-y", device_id, "w13@0x15", "0x05", "0x00", "0x3d", "0x03", "0x06", "0x00", "0x07", "0x00", "0x0d", "0x14", "0x03", value, "0xad"]
try:
subprocess.call(cmd)
except subprocess.CalledProcessError as e:
log.error('Error during sending via i2c: \"%s\"', e.output)
def parse_value_from_config(value):
if value == '0':
return False
elif value == '1':
return True
else:
return value
def parse_value_to_config(value):
if value == True:
return '1'
elif value == False:
return '0'
else:
return str(value)
def config_save():
global config_file_dir, config_file_path
try:
with open(config_file_path, 'w') as configFile:
config.write(configFile)
log.debug('Writting to config file: \"%s\"', configFile)
except:
log.error('Error during writting to config file: \"%s\"', config_file_path)
pass
def config_set(key, value, no_save=False, already_has_lock=False):
global config, config_file_dir, config_lock
if not already_has_lock:
#log.debug("config_set: config_lock.acquire will be called")
config_lock.acquire()
#log.debug("config_set: config_lock.acquire called succesfully")
config.set(CONFIG_SECTION, key, parse_value_to_config(value))
log.info('Setting up for config file key: \"%s\" with value: \"%s\"', key, value)
if not no_save:
config_save()
if not already_has_lock:
# because inotify (deadlock)
sleep(0.1)
config_lock.release()
return value
def gsettingsSet(path, name, value):
global gsettings_failure_count, gsettings_max_failure_count
if gsettings_failure_count < gsettings_max_failure_count:
try:
sudo_user = os.environ.get('SUDO_USER')
if sudo_user is not None:
cmd = ['runuser', '-u', sudo_user, 'gsettings', 'set', path, name, str(value)]
else:
cmd = ['gsettings', 'set', path, name, str(value)]
log.debug(cmd)
subprocess.call(cmd)
except:
log.exception('gsettings set failed')
gsettings_failure_count+=1
else:
log.debug('Gsettings failed more then: \"%s\" so is not try anymore', gsettings_max_failure_count)
def gsettingsGet(path, name):
global gsettings_failure_count, gsettings_max_failure_count
if gsettings_failure_count < gsettings_max_failure_count:
try:
cmd = ['gsettings', 'get', path, name]
propData = subprocess.check_output(cmd)
return propData.decode()
except:
log.exception('gsettings get failed')
gsettings_failure_count+=1
else:
log.debug('Gsettings failed more then: \"%s\" so is not try anymore', gsettings_max_failure_count)
def gsettingsGetTouchpadSendEvents():
return gsettingsGet('org.gnome.desktop.peripherals.touchpad', 'send-events')
def gsettingsSetTouchpadTapToClick(value):
gsettingsSet('org.gnome.desktop.peripherals.touchpad', 'tap-to-click', str(bool(value)).lower())
def gsettingsGetUnicodeHotkey():
return gsettingsGet('org.freedesktop.ibus.panel.emoji', 'unicode-hotkey')
# Figure out devices from devices file
touchpad: Optional[str] = None
touchpad_name: Optional[str] = None
keyboard: Optional[str] = None
d_k = None
fd_k = None
numlock_lock = threading.Lock()
device_id: Optional[str] = None
# Look into the devices file #
while try_times > 0:
touchpad_detected = 0
keyboard_detected = 0
with open('/proc/bus/input/devices', 'r') as f:
lines = f.readlines()
for line in lines:
# Look for the touchpad #
# https://github.com/mohamed-badaoui/asus-touchpad-numpad-driver/issues/87
# https://github.com/asus-linux-drivers/asus-numberpad-driver/issues/95
# https://github.com/asus-linux-drivers/asus-numberpad-driver/issues/110
if (touchpad_detected == 0 and ("Name=\"ASUE" in line or "Name=\"ELAN" in line) and "Touchpad" in line) or \
(("Name=\"ASUE" in line or "Name=\"ELAN" in line) and ("1406" in line or "4F3:3101" in line) and "Touchpad" in line):
touchpad_detected = 1
log.info('Detecting touchpad from string: \"%s\"', line.strip())
touchpad_name = line.split("\"")[1]
if touchpad_detected == 1:
if "S: " in line:
# search device id
device_id = re.sub(r".*i2c-(\d+)/.*$",
r'\1', line).replace("\n", "")
log.info('Set touchpad device id %s from %s',
device_id, line.strip())
if "H: " in line:
touchpad = line.split("event")[1]
touchpad = touchpad.split(" ")[0]
touchpad_detected = 2
log.info('Set touchpad id %s from %s',
touchpad, line.strip())
# Look for the keyboard
if keyboard_detected == 0 and ("Name=\"AT Translated Set 2 keyboard" in line or (("Name=\"ASUE" in line or "Name=\"Asus" in line) and "Keyboard" in line)):
keyboard_detected = 1
log.info(
'Detecting keyboard from string: \"%s\"', line.strip())
# We look for keyboard with numlock, scrollock, capslock inputs
if keyboard_detected == 1 and "H: " in line:
keyboard = line.split("event")[1]
keyboard = keyboard.split(" ")[0]
with open('/dev/input/event' + str(keyboard), 'rb') as fd_k:
d_k = Device(fd_k)
if d_k.has(EV_LED.LED_NUML):
keyboard_detected = 2
log.info('Set keyboard %s from %s', keyboard, line.strip())
else:
keyboard_detected = 0
keyboard = None
d_k = None
# Do not stop looking if touchpad and keyboard have been found
# because more drivers can be installed
# https://github.com/mohamed-badaoui/asus-touchpad-numpad-driver/issues/87
# https://github.com/asus-linux-drivers/asus-numberpad-driver/issues/95
#if touchpad_detected == 2 and keyboard_detected == 2:
# break
if touchpad_detected != 2 or keyboard_detected != 2:
try_times -= 1
if try_times == 0:
with open('/proc/bus/input/devices', 'r') as f:
lines = f.readlines()
for line in lines:
log.error(line)
if keyboard_detected != 2:
log.error("Can't find keyboard (code: %s)", keyboard_detected)
# keyboard is optional, no sys.exit(1)!
if touchpad_detected != 2:
log.error("Can't find touchpad (code: %s)", touchpad_detected)
sys.exit(1)
if touchpad_detected == 2 and not device_id.isnumeric():
log.error("Can't find device id")
sys.exit(1)
else:
break
sleep(try_sleep)
# Start monitoring the touchpad
fd_t = open('/dev/input/event' + str(touchpad), 'rb')
d_t = Device(fd_t)
# Retrieve touchpad dimensions
ai = d_t.absinfo[EV_ABS.ABS_X]
(minx, maxx) = (ai.minimum, ai.maximum)
minx_numpad = minx + left_offset
maxx_numpad = maxx - right_offset
ai = d_t.absinfo[EV_ABS.ABS_Y]
(miny, maxy) = (ai.minimum, ai.maximum)
miny_numpad = miny + top_offset
maxy_numpad = maxy - bottom_offset
log.info('Touchpad min-max: x %d-%d, y %d-%d', minx, maxx, miny, maxy)
log.info('Numpad min-max: x %d-%d, y %d-%d', minx_numpad,
maxx_numpad, miny_numpad, maxy_numpad)
# Detect col, row count from map of keys
col_count = len(max(keys, key=len))
row_count = len(keys)
col_width = (maxx_numpad - minx_numpad) / col_count
row_height = (maxy_numpad - miny_numpad) / row_count
def get_keycode_of_ascii_char(char):
display_var = os.environ.get('DISPLAY')
display = Xlib.display.Display(display_var)
keysym = Xlib.XK.string_to_keysym(char)
keycode = display.keysym_to_keycode(keysym) - 8
return keycode
def get_key_which_reflects_current_layout(char, reset_udev=True):
global enabled_keys_for_unicode_shortcut, udev, dev
keycode = get_keycode_of_ascii_char(char)
key = EV_KEY.codes[int(keycode)]
if key not in enabled_keys_for_unicode_shortcut:
enabled_keys_for_unicode_shortcut.append(key)
dev.enable(key)
if reset_udev:
log.info("Old device at {} ({})".format(udev.devnode, udev.syspath))
udev = dev.create_uinput_device()
log.info("New device at {} ({})".format(udev.devnode, udev.syspath))
# Sleep for a little bit so udev, libinput, Xorg, Wayland, ... all have had
# a chance to see the device and initialize it. Otherwise the event
# will be sent by the kernel but nothing is ready to listen to the
# device yet
sleep(1)
return key
# Create a new keyboard device to send numpad events
dev = Device()
dev.name = touchpad_name.split(" ")[0] + touchpad_name.split(" ")[1] + " NumberPad"
dev.enable(EV_KEY.BTN_LEFT)
dev.enable(EV_KEY.BTN_RIGHT)
dev.enable(EV_KEY.BTN_MIDDLE)
dev.enable(EV_KEY.KEY_NUMLOCK)
# predefined for all possible unicode characters <leftshift>+<leftctrl>+<U>+<0-F>
enabled_keys_for_unicode_shortcut = [
EV_KEY.KEY_LEFTSHIFT,
EV_KEY.KEY_LEFTCTRL,
EV_KEY.KEY_SPACE,
EV_KEY.KEY_ENTER,
EV_KEY.KEY_U, # standart is U
EV_KEY.KEY_S, # for FR is S
EV_KEY.KEY_0,
EV_KEY.KEY_1,
EV_KEY.KEY_2,
EV_KEY.KEY_3,
EV_KEY.KEY_4,
EV_KEY.KEY_5,
EV_KEY.KEY_6,
EV_KEY.KEY_7,
EV_KEY.KEY_8,
EV_KEY.KEY_9,
EV_KEY.KEY_KP0,
EV_KEY.KEY_KP1,
EV_KEY.KEY_KP2,
EV_KEY.KEY_KP3,
EV_KEY.KEY_KP4,
EV_KEY.KEY_KP5,
EV_KEY.KEY_KP6,
EV_KEY.KEY_KP7,
EV_KEY.KEY_KP8,
EV_KEY.KEY_KP9,
EV_KEY.KEY_A,
EV_KEY.KEY_B,
EV_KEY.KEY_C,
EV_KEY.KEY_D,
EV_KEY.KEY_E,
EV_KEY.KEY_F
]
# enable equivalent key of "U" for currently used keyboard layout
try:
get_key_which_reflects_current_layout("U", False)
except:
pass
for key in enabled_keys_for_unicode_shortcut:
dev.enable(key)
for key_to_enable in top_left_icon_slide_func_keys:
dev.enable(key_to_enable)
def isEvent(event):
if getattr(event, "name", None) is not None and\
getattr(EV_KEY, event.name):
return True
else:
return False
def isEventList(events):
if type(events) is list:
for event in events:
if not isEvent(event):
return False
return True
else:
return False
def is_device_enabled(device_name):
global gsettings_failure_count, gsettings_max_failure_count, getting_device_via_xinput_status_failure_count, getting_device_via_xinput_status_max_failure_count
if gsettings_failure_count < gsettings_max_failure_count:
value = gsettingsGetTouchpadSendEvents()
if value:
if 'enabled' in value:
return True
elif 'disabled' in value:
return False
if getting_device_via_xinput_status_failure_count > getting_device_via_xinput_status_max_failure_count:
log.debug('Getting Device Enabled via xinput failed more then: \"%s\" so is not try anymore, returned Touchpad enabled', getting_device_via_xinput_status_max_failure_count)
return True
try:
cmd = ['xinput', '--list-props', device_name]
propData = subprocess.check_output(cmd)
propData = propData.decode()
for line in propData.splitlines():
if 'Device Enabled' in line:
line = line.strip()
if line[-1] == '1':
return True
else:
return False
log.error('Getting Device Enabled via xinput failed because was not found Device Enabled for Touchpad.')
getting_device_via_xinput_status_failure_count += 1
return True
except:
getting_device_via_xinput_status_failure_count += 1
log.exception('Getting Device Enabled via xinput failed')
return True
for col in keys:
for key in col:
if getattr(key, "name", None) is not None and\
getattr(EV_KEY, key.name):
dev.enable(key)
# Sleep for a bit so udev, libinput, Xorg, Wayland, ... all have had
# a chance to see the device and initialize it. Otherwise the event
# will be sent by the kernel but nothing is ready to listen to the
# device yet
udev = dev.create_uinput_device()
sleep(1)
def use_slide_func_for_top_right_icon():
global numlock, top_right_icon_touch_start_time, numlock_touch_start_time
log.info("Func for touchpad right_icon slide function")
top_right_icon_touch_start_time = 0
numlock_touch_start_time = 0
local_numlock_pressed()
def use_bindings_for_touchpad_left_icon_slide_function():
global udev, numlock, top_left_icon_slide_func_keys, top_left_icon_touch_start_time
top_left_icon_touch_start_time = 0
set_none_to_current_mt_slot()
key_events = []
for custom_key in top_left_icon_slide_func_keys:
key_events.append(InputEvent(custom_key, 1))
key_events.append(InputEvent(EV_SYN.SYN_REPORT, 0))
key_events.append(InputEvent(custom_key, 0))
key_events.append(InputEvent(EV_SYN.SYN_REPORT, 0))
try:
udev.send_events(key_events)
log.info("Used bindings for touchpad left_icon slide function")
except OSError as e:
log.error("Cannot send event, %s", e)
def is_pressed_touchpad_top_right_icon():
global top_right_icon_width, top_right_icon_height, abs_mt_slot_x_values, abs_mt_slot_y_values, abs_mt_slot_value
if abs_mt_slot_x_values[abs_mt_slot_value] >= maxx - top_right_icon_width and\
abs_mt_slot_y_values[abs_mt_slot_value] <= top_right_icon_height:
return True
return False
def is_pressed_touchpad_top_left_icon():
global top_left_icon_width, top_left_icon_height, abs_mt_slot_x_values, abs_mt_slot_y_values, abs_mt_slot_value
if not top_left_icon_width > 0 or \
not top_left_icon_height > 0:
return False
if abs_mt_slot_x_values[abs_mt_slot_value] <= top_left_icon_width and\
abs_mt_slot_y_values[abs_mt_slot_value] <= top_left_icon_height:
return True
else:
return False
def reset_mt_slot(index):
abs_mt_slot_numpad_key[index] = None
abs_mt_slot_x_init_values[index] = -1
abs_mt_slot_x_values[index] = -1
abs_mt_slot_y_init_values[index] = -1
abs_mt_slot_y_values[index] = -1
def set_none_to_current_mt_slot():
global abs_mt_slot_value
reset_mt_slot(abs_mt_slot_value)
def set_none_to_all_mt_slots():
global abs_mt_slot_numpad_key,\
abs_mt_slot_x_values, abs_mt_slot_y_values
abs_mt_slot_numpad_key[:] = None
abs_mt_slot_x_init_values[:] = -1
abs_mt_slot_x_values[:] = -1
abs_mt_slot_y_init_values[:] = -1
abs_mt_slot_y_values[:] = -1
def pressed_touchpad_top_left_icon(e):
global top_left_icon_touch_start_time, abs_mt_slot_numpad_key, abs_mt_slot_value
if e.value == 1:
top_left_icon_touch_start_time = time()
log.info("Touched top_left_icon in time: %s", time())
abs_mt_slot_numpad_key[abs_mt_slot_value] = EV_KEY_TOP_LEFT_ICON
else:
set_none_to_current_mt_slot()
def increase_brightness():
global brightness, backlight_levels, config
if (brightness + 1) >= len(backlight_levels):
brightness = 0
else:
brightness += 1
log.info("Increased brightness of backlight to")
log.info(brightness)
config_set(CONFIG_LAST_BRIGHTNESS, backlight_levels[brightness])
send_value_to_touchpad_via_i2c(backlight_levels[brightness])
def send_numlock_key(value):
global udev
events = [
InputEvent(EV_MSC.MSC_SCAN, 70053),
InputEvent(EV_KEY.KEY_NUMLOCK, value),
InputEvent(EV_SYN.SYN_REPORT, 0)
]
try:
udev.send_events(events)
except OSError as e:
log.error("Cannot send event, %s", e)
def grab_current_slot():
global d_t
try:
log.info("grab current slot")
d_t.grab()
abs_mt_slot_grab_status[abs_mt_slot_value] = 1
except device.DeviceGrabError as e:
log.error("Error of grabbing, %s", e)
def set_touchpad_prop_tap_to_click(value):
global touchpad_name, gsettings_failure_count, gsettings_max_failure_count, getting_device_via_xinput_status_failure_count, getting_device_via_xinput_status_max_failure_count, getting_device_via_synclient_status_failure_count, getting_device_via_synclient_status_max_failure_count
# 1. priority - gsettings
if gsettings_failure_count < gsettings_max_failure_count:
gsettingsSetTouchpadTapToClick(value)
return
# 2. priority - xinput
if getting_device_via_xinput_status_failure_count > getting_device_via_xinput_status_max_failure_count:
log.debug('Setting libinput Tapping EnabledDevice via xinput failed more then: \"%s\" times so is not try anymore', getting_device_via_xinput_status_max_failure_count)
else:
try:
cmd = ["xinput", "set-prop", touchpad_name, 'libinput Tapping Enabled', str(value)]
log.debug(cmd)
subprocess.call(cmd)
return
except:
getting_device_via_xinput_status_failure_count+=1
log.error('Setting libinput Tapping EnabledDevice via xinput failed')
# 3. priority - synclient
if getting_device_via_synclient_status_failure_count > getting_device_via_synclient_status_max_failure_count:
log.debug('Setting libinput Tapping EnabledDevice via xinput failed more then: \"%s\" times so is not try anymore', getting_device_via_xinput_status_max_failure_count)
try:
cmd = ["synclient", "TapButton1=" + str(value)]
log.debug(cmd)
subprocess.call(cmd)
return
except:
getting_device_via_synclient_status_failure_count+=1
def grab():
global d_t
try:
log.info("grab")
d_t.grab()
except device.DeviceGrabError as e:
log.error("Error of grabbing, %s", e)
def activate_numpad():
global brightness, default_backlight_level, enabled_touchpad_pointer, top_left_icon_brightness_func_disabled
if enabled_touchpad_pointer == 0 or enabled_touchpad_pointer == 2:
grab()
elif enabled_touchpad_pointer == 3:
set_touchpad_prop_tap_to_click(0)
# https://github.com/asus-linux-drivers/asus-numberpad-driver/issues/132
# both values are required to send for succesfull activation (brightness up)
send_value_to_touchpad_via_i2c("0x60")
send_value_to_touchpad_via_i2c("0x01")
if default_backlight_level != "0x01" and not top_left_icon_brightness_func_disabled:
send_value_to_touchpad_via_i2c(default_backlight_level)
try:
brightness = backlight_levels.index(default_backlight_level)
except ValueError:
# so after start and then click on icon for increasing brightness
# will be used first indexed value in given array with index 0 (0 = -1 + 1)
# (if exists)
# TODO: atm do not care what last value is now displayed and which one (nearest higher) should be next (default 0x01 means turn leds on with last used level of brightness)
brightness = -1
config_set(CONFIG_ENABLED, True)
def deactivate_numpad():
global brightness, enabled_touchpad_pointer
if enabled_touchpad_pointer == 0 or enabled_touchpad_pointer == 2:
ungrab()
elif enabled_touchpad_pointer == 1:
ungrab_current_slot()
elif enabled_touchpad_pointer == 3:
set_touchpad_prop_tap_to_click(1)
# inactivation can be doubled with another value 0x61 but purpose is
# not discovered yet so is used only 0x00 and 0x60 is send for sure during activating
# (in case 0x61 was called directly outside of driver)
#
# https://github.com/asus-linux-drivers/asus-numberpad-driver/issues/132
#
send_value_to_touchpad_via_i2c("0x00")
brightness = 0
config_set(CONFIG_ENABLED, False)
def get_system_numlock():
global keyboard
if not keyboard:
return None
with open('/dev/input/event' + str(keyboard), 'rb') as fd_k:
d_k = Device(fd_k)
state = d_k.value[EV_LED.LED_NUML]
d_k = None
return bool(state)
def local_numlock_pressed():
global brightness, numlock
#log.debug("local_numlock_pressed: numlock_lock.acquire will be called")
numlock_lock.acquire()
#log.debug("local_numlock_pressed: numlock_lock.acquire called succesfully")
is_touchpad_enabled = is_device_enabled(touchpad_name)
if not ((not touchpad_disables_numpad and not is_touchpad_enabled) or is_touchpad_enabled):
return
sys_numlock = get_system_numlock()
set_none_to_current_mt_slot()
# Activating
if not numlock:
numlock = True
if not sys_numlock:
send_numlock_key(1)
send_numlock_key(0)
log.info("System numlock activated")
log.info("Numpad activated")
activate_numpad()
# Inactivating
else:
numlock = False
if sys_numlock and numpad_disables_sys_numlock:
send_numlock_key(1)
send_numlock_key(0)
log.info("System numlock deactivated")
log.info("Numpad deactivated")
deactivate_numpad()
numlock_lock.release()
def read_config_file():
global config, config_file_path
try:
if not config.has_section(CONFIG_SECTION):
config.add_section(CONFIG_SECTION)
config.read(config_file_path)
except:
pass
def load_all_config_values():
global config
global keys
global top_right_icon_height
global top_right_icon_width
global numpad_disables_sys_numlock
global disable_due_inactivity_time
global touchpad_disables_numpad
global key_repetitions
global multitouch
global one_touch_key_rotation
global activation_time
global sys_numlock_enables_numpad
global top_left_icon_activation_time
global top_left_icon_slide_func_activation_x_ratio
global top_left_icon_slide_func_activation_y_ratio
global top_right_icon_slide_func_activation_x_ratio
global top_right_icon_slide_func_activation_y_ratio
global numlock
global default_backlight_level
global top_left_icon_brightness_func_disabled
global support_for_maximum_abs_mt_slots
global config_lock
global enabled_touchpad_pointer
global press_key_when_is_done_untouch
global distance_to_move_only_pointer
#log.debug("load_all_config_values: config_lock.acquire will be called")
config_lock.acquire()
#log.debug("load_all_config_values: config_lock.acquire called succesfully")
read_config_file()
numpad_disables_sys_numlock = config_get(CONFIG_NUMPAD_DISABLES_SYS_NUMLOCK, CONFIG_NUMPAD_DISABLES_SYS_NUMLOCK_DEFAULT)
disable_due_inactivity_time = float(config_get(CONFIG_DISABLE_DUE_INACTIVITY_TIME, CONFIG_DISABLE_DUE_INACTIVITY_TIME_DEFAULT))
touchpad_disables_numpad = config_get(CONFIG_TOUCHPAD_DISABLES_NUMPAD, CONFIG_TOUCHPAD_DISABLES_NUMPAD_DEFAULT)
key_repetitions = config_get(CONFIG_KEY_REPETITIONS, CONFIG_KEY_REPETITIONS_DEFAULT)
multitouch = config_get(CONFIG_MULTITOUCH, CONFIG_MULTITOUCH_DEFAULT)
one_touch_key_rotation = config_get(CONFIG_ONE_TOUCH_KEY_ROTATION, CONFIG_ONE_TOUCH_KEY_ROTATION_DEFAULT)
activation_time = float(config_get(CONFIG_ACTIVATION_TIME, CONFIG_ACTIVATION_TIME_DEFAULT))
sys_numlock_enables_numpad = config_get(CONFIG_NUMLOCK_ENABLES_NUMPAD, CONFIG_NUMLOCK_ENABLES_NUMPAD_DEFAULT)
key_numlock_is_used = any(EV_KEY.KEY_NUMLOCK in x for x in keys)
if (not top_right_icon_height > 0 or not top_right_icon_width > 0) and not key_numlock_is_used:
sys_numlock_enables_numpad_new = True
if sys_numlock_enables_numpad is not sys_numlock_enables_numpad_new:
config_set(CONFIG_NUMLOCK_ENABLES_NUMPAD, sys_numlock_enables_numpad_new, True, True)
top_left_icon_activation_time = float(config_get(CONFIG_LEFT_ICON_ACTIVATION_TIME, CONFIG_LEFT_ICON_ACTIVATION_TIME_DEFAULT))
top_left_icon_slide_func_activation_x_ratio = float(config_get(CONFIG_TOP_LEFT_ICON_SLIDE_FUNC_ACTIVATION_X_RATIO, CONFIG_TOP_LEFT_ICON_SLIDE_FUNC_ACTIVATION_X_RATIO_DEFAULT))
top_left_icon_slide_func_activation_y_ratio = float(config_get(CONFIG_TOP_LEFT_ICON_SLIDE_FUNC_ACTIVATION_Y_RATIO, CONFIG_TOP_LEFT_ICON_SLIDE_FUNC_ACTIVATION_Y_RATIO_DEFAULT))
top_right_icon_slide_func_activation_x_ratio = float(config_get(CONFIG_TOP_RIGHT_ICON_SLIDE_FUNC_ACTIVATION_X_RATIO, CONFIG_TOP_RIGHT_ICON_SLIDE_FUNC_ACTIVATION_X_RATIO_DEFAULT))
top_right_icon_slide_func_activation_y_ratio = float(config_get(CONFIG_TOP_RIGHT_ICON_SLIDE_FUNC_ACTIVATION_Y_RATIO, CONFIG_TOP_RIGHT_ICON_SLIDE_FUNC_ACTIVATION_Y_RATIO_DEFAULT))
enabled_touchpad_pointer = int(config_get(CONFIG_ENABLED_TOUCHPAD_POINTER, CONFIG_ENABLED_TOUCHPAD_POINTER_DEFAULT))
press_key_when_is_done_untouch = int(config_get(CONFIG_PRESS_KEY_WHEN_IS_DONE_UNTOUCH, CONFIG_PRESS_KEY_WHEN_IS_DONE_UNTOUCH_DEFAULT))
enabled = config_get(CONFIG_ENABLED, CONFIG_ENABLED_DEFAULT)
default_backlight_level = config_get(CONFIG_DEFAULT_BACKLIGHT_LEVEL, CONFIG_DEFAULT_BACKLIGHT_LEVEL_DEFAULT)
if default_backlight_level == "0x01":
try:
default_backlight_level = config.get(CONFIG_SECTION, CONFIG_LAST_BRIGHTNESS)
except:
pass
top_left_icon_brightness_func_disabled = config_get(CONFIG_TOP_LEFT_ICON_BRIGHTNESS_FUNC_DISABLED, CONFIG_TOP_LEFT_ICON_BRIGHTNESS_FUNC_DISABLED_DEFAULT)
if not backlight_levels or not top_left_icon_height or not top_left_icon_width:
top_left_icon_brightness_func_disabled_new = True
if top_left_icon_brightness_func_disabled is not top_left_icon_brightness_func_disabled_new:
config_set(CONFIG_TOP_LEFT_ICON_BRIGHTNESS_FUNC_DISABLED, top_left_icon_brightness_func_disabled_new, True, True)
if multitouch:
support_for_maximum_abs_mt_slots = 5
else:
support_for_maximum_abs_mt_slots = 1
distance_to_move_only_pointer = float(config_get(CONFIG_DISTANCE_TO_MOVE_ONLY_POINTER, CONFIG_DISTANCE_TO_MOVE_ONLY_POINTER_DEFAULT))
config_lock.release()
if enabled is not numlock:
local_numlock_pressed()
abs_mt_slot_value: int = 0
# -1 inactive, > 0 active
abs_mt_slot = np.array([-1, -1, -1, -1, -1], int)
abs_mt_slot_numpad_key = np.array([None, None, None, None, None], dtype=const.EventCode)
abs_mt_slot_x_init_values = np.array([-1, -1, -1, -1, -1], int)
abs_mt_slot_x_values = np.array([-1, -1, -1, -1, -1], int)
abs_mt_slot_y_init_values = np.array([-1, -1, -1, -1, -1], int)
abs_mt_slot_y_values = np.array([-1, -1, -1, -1, -1], int)
abs_mt_slot_grab_status = np.array([-1, -1, -1, -1, -1], int)
# equal to multi finger maximum
support_for_maximum_abs_mt_slots: int = 1
unsupported_abs_mt_slot: bool = False
numlock_touch_start_time = 0
top_left_icon_touch_start_time = 0
top_right_icon_touch_start_time = 0
last_event_time = 0
key_pointer_button_is_touched = None
config = configparser.ConfigParser()
load_all_config_values()
config_lock.acquire()
config_save()
config_lock.release()
# because inotify (deadlock)
sleep(0.1)
def set_tracking_id(value):
try:
if value > 0:
log.info("Started new slot")
# not know yet
# log.info(abs_mt_slot_numpad_key[abs_mt_slot_value])
else:
log.info("Ended existing slot")
# can be misunderstanding when is touched padding (is printed previous key)
# log.info(abs_mt_slot_numpad_key[abs_mt_slot_value])
abs_mt_slot[abs_mt_slot_value] = value
except IndexError as e:
log.error(e)
def get_compose_key_end_events_for_unicode_string():
space_pressed = InputEvent(EV_KEY.KEY_SPACE, 1)
space_unpressed = InputEvent(EV_KEY.KEY_SPACE, 0)
events = [
InputEvent(EV_MSC.MSC_SCAN, space_pressed.code.value),
space_pressed,
InputEvent(EV_SYN.SYN_REPORT, 0),
InputEvent(EV_MSC.MSC_SCAN, space_unpressed.code.value),
space_unpressed,
InputEvent(EV_SYN.SYN_REPORT, 0)
]
return events
def get_compose_key_start_events_for_unicode_string():
global gsettings_failure_count, gsettings_max_failure_count
string_with_unicode_hotkey = gsettingsGetUnicodeHotkey()
keys = []
if string_with_unicode_hotkey is not None:
string_with_unicode_hotkey = string_with_unicode_hotkey.split("'")[1]
gsettingsKeyModifiersToXlibSpecificKeyModifiers = {
'Control': 'Control_L',
'Shift': 'Shift_L'
}
for key, replacedWithKey in gsettingsKeyModifiersToXlibSpecificKeyModifiers.items():
string_with_unicode_hotkey = re.sub("<" + key + ">", "<" + replacedWithKey + ">", string_with_unicode_hotkey)
key_modifiers = re.findall("<(.*?)>", string_with_unicode_hotkey)
for key_modifier in key_modifiers:
try:
key_evdev = get_key_which_reflects_current_layout(key_modifier)
keys.append(key_evdev)
except:
log.error("Error during trying to find key for modifier of found compose shortcut {}".format(key_modifier))
pass
try:
first_number_index = string_with_unicode_hotkey.rfind('>') + 1
key_evdev = get_key_which_reflects_current_layout(string_with_unicode_hotkey[first_number_index])
keys.append(key_evdev)
except:
pass
else: