-
Notifications
You must be signed in to change notification settings - Fork 3
/
asus_fliplock.py
executable file
·148 lines (109 loc) · 4.56 KB
/
asus_fliplock.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
#!/usr/bin/env python3
import importlib
import logging
import os
from time import sleep
import re
import sys
from typing import Optional
from libevdev import Device, EV_SW
# Setup logging
# LOG=DEBUG sudo -E ./asus_fliplock.py "default" # all messages
# LOG=ERROR sudo -E ./asus_fliplock.py "default" # only error messages
logging.basicConfig()
log = logging.getLogger('Asus fliplock')
log.setLevel(os.environ.get('LOG', 'INFO'))
# Layout
layout = 'default'
if len(sys.argv) > 1:
layout = sys.argv[1]
fliplock_layouts = importlib.import_module('conf.'+ layout)
switches: Optional[str] = None
wmi_hotkeys: Optional[str] = None
switches_detected = 0
wmi_hotkeys_detected = 0
def search_devices():
global switches, wmi_hotkeys
global touchpad_detected, switches_detected, wmi_hotkeys_detected
tries = 5
# Look into the devices file
while tries > 0:
with open('/proc/bus/input/devices', 'r') as f:
lines = f.readlines()
for line in lines:
# Look for the device switches
if re.search("switches", line):
switches_detected = 1
log.debug('Detect switches from %s', line.strip())
# Look for the device wmi hotkey or https://github.com/asus-linux-drivers/asus-accel-tablet-mode-driver
if wmi_hotkeys_detected == 0 and (("Name=\"Asus WMI hotkeys" in line and tries == 4) or ("Name=\"Asus WMI accel tablet mode" in line and tries == 5)):
wmi_hotkeys_detected = 1
log.debug('Detect wmi hotkeys from %s', line.strip())
if wmi_hotkeys_detected == 1:
if "H: " in line:
wmi_hotkeys = line.split("event")[1]
wmi_hotkeys = wmi_hotkeys.split(" ")[0]
wmi_hotkeys_detected = 2
log.debug('Set wmi hotkeys id %s from %s', wmi_hotkeys, line.strip())
break
if switches_detected == 1:
if "H: " in line:
switches = line.split("event")[1]
switches = switches.split(" ")[0]
switches_detected = 2
log.debug('Set switches id %s from %s', switches, line.strip())
break
if switches_detected == 2 and wmi_hotkeys_detected == 2:
break
if switches_detected != 2 or wmi_hotkeys_detected != 2:
tries -= 1
if tries == 0:
if switches_detected != 2 and wmi_hotkeys_detected != 2:
log.error("Can't find neither switches or wmi hotkeys device")
sys.exit(1)
sleep(0.1)
else:
break
search_devices()
# Start monitoring the switch device
fd_t_switches = None
d_t_switches = None
fd_t_wmi_hotkeys = open('/dev/input/event' + str(wmi_hotkeys), 'rb')
d_t_wmi_hotkeys = Device(fd_t_wmi_hotkeys)
# When we listen only key we predict start state is laptop mode
intel_hid_switches_tablet_mode = 1
def execute_cmd(cmd):
try:
os.system(cmd)
except OSError as e:
log.error(e)
def execute_cmds_in_array(cmds):
for cmd in cmds:
execute_cmd(cmd)
def flip(intel_hid_switches_tablet_mode):
if intel_hid_switches_tablet_mode:
execute_cmds_in_array(fliplock_layouts.tablet_mode_actions)
else:
execute_cmds_in_array(fliplock_layouts.laptop_mode_actions)
# If mode has been changed, do something
if switches is None and wmi_hotkeys is not None:
for e in d_t_wmi_hotkeys.events():
log.debug(e)
if switches is not None:
break
# Pressed and released event is called during one! flipping simultanously
if e.matches(fliplock_layouts.flip_key) and e.value == 1:
flip(intel_hid_switches_tablet_mode)
intel_hid_switches_tablet_mode = not intel_hid_switches_tablet_mode
# TODO: first flip is ommited because device for swithes is added immediately when is flip_key (usually EV_KEY.KEY_PROG2) triggered
# dmegs:
# [ 4685.611692] intel-hid INTC1051:00: switch event received, enable switches supports
# [ 4685.611762] input: Intel HID switches as /devices/platform/INTC1051:00/input/input37
search_devices()
if switches is not None:
fd_t_switches = open('/dev/input/event' + str(switches), 'rb')
d_t_switches = Device(fd_t_switches)
for e in d_t_switches.events():
log.debug(e)
if e.matches(EV_SW.SW_TABLET_MODE):
flip(e.value)