-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.py
302 lines (230 loc) · 9.07 KB
/
controller.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
import socket
from time import sleep
from button import *
from typing_layout import DIRECTIONS
def resole_angle(angle):
return (angle + 360) % 360
class Controller:
def connect(self, configs):
connection_cfg = configs["connection"]
from kivy.utils import platform
if platform == 'android':
if bool(connection_cfg['via_wifi']):
connection_cfg = connection_cfg['wifi']
else:
connection_cfg = connection_cfg['phone']
else:
connection_cfg = connection_cfg['test']
network_num = connection_cfg["network_num"]
device_num = connection_cfg["device_num"]
server_ip = f'192.168.{network_num}.{device_num}'
mouse_port = 5005
scroll_port = 5007
button_port = 5009
self.mouse_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.scroll_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.button_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.mouse_sock.connect((server_ip, mouse_port))
self.scroll_sock.connect((server_ip, scroll_port))
self.button_sock.connect((server_ip, button_port))
def set_scroll_profile(self):
if self.is_high_precision:
profile = "normal"
else:
profile = "fast"
self.scroll_by = self.scroll_cfg[profile]['move_by']
def get_detailed_hints(self):
direction = self.typing_btn_1
if self.is_left:
hints = self.left_detailed_hints
else:
hints = self.right_detailed_hints
return hints[self.lang][direction]
def get_preview_hints(self, is_left):
if is_left:
hints = self.left_preview_hints
else:
hints = self.right_preview_hints
return hints[self.lang]
def detect_letter(self):
if self.is_left:
layout = self.typing_layout
else:
layout = self.right_first_layout
letter = layout[self.lang][self.typing_btn_1][self.typing_btn_2]
if letter == '':
print(f'no letter for this position: ({self.typing_btn_1}, {self.typing_btn_2}) or lang: {self.lang}')
return None
return letter
def update_typing_state(self, btn_direction, is_left):
if self.typing_btn_1 is None:
self.typing_btn_1 = btn_direction
self.is_left = is_left
return None
else:
if self.is_left == is_left:
if btn_direction == self.CentralDir:
self.reset_typing()
return self.Switch_code
else:
self.reset_typing()
return None
else:
self.typing_btn_2 = btn_direction
letter = self.detect_letter()
self.reset_typing()
return letter
def reset_typing(self):
self.typing_btn_1 = None
self.typing_btn_2 = None
self.is_left = None
def mouse_mode_btn_pressed(self, btn_direction):
command = self.mouse_mode_layout[btn_direction]
if command == '':
command = None
return command
def release_mouse(self):
for button in self.mouse_buttons:
self.force_release(button)
def release_all_pressed(self):
for button in self.btn_states.all_pressed():
self.force_release(button)
def release_mouse_and_pressed(self):
self.release_mouse()
self.force_release_modifiers()
self.release_all_pressed()
def release_all(self):
self.release_mouse()
self.force_release_modifiers()
for button in self.btn_states.all:
self.force_release(button)
self.reset_typing()
def send_type(self, buttons):
self.send_pressed(buttons)
self.send_released(buttons)
def send_pressed(self, buttons):
if not is_iterable(buttons):
self._send_pressed_single(buttons)
else:
for button in buttons:
self._send_pressed_single(button)
def send_released(self, buttons):
if not is_iterable(buttons):
self._send_released_single(buttons)
else:
for button in reverse(buttons):
self._send_released_single(button)
def _modifier_press_handled(self, button):
if button == self.Caps:
self.modifiers.current = self.Shift
else:
self.modifiers.current = button
if self.modifiers.current in self.modifiers.all:
if self.modifiers.cur_state == 0:
if button == self.Caps:
self.press_modifier(2)
else:
self.press_modifier(1)
elif self.modifiers.cur_state == 1:
if button == self.Caps:
self.release_modifier()
else:
self.release_modifier()
elif self.modifiers.cur_state == 2:
if button == self.Caps:
self.release_modifier()
else:
raise ValueError(f'incorrect state: {self.modifiers.cur_state}')
return True
else:
return False
def send_pressed_mouse(self, button):
if self.is_mouse_mode:
if self.btn_states.press(button):
self._send_pressed_raw(button)
def _send_pressed_single(self, button):
if button in self.mouse_buttons:
self.send_pressed_mouse(button)
elif not self._modifier_press_handled(button):
if button == self.Esc:
self.release_mouse_and_pressed()
if self.btn_states.press(button):
self._send_pressed_raw(button)
def _modifier_release_handled(self, button):
if button == self.Caps:
self.modifiers.current = self.Shift
else:
self.modifiers.current = button
return self.modifiers.current in self.modifiers.all
def send_released_mouse(self, button):
if self.btn_states.release(button):
self._send_released_raw(button)
def _send_released_single(self, button):
if button in self.mouse_buttons:
self.send_released_mouse(button)
elif not self._modifier_release_handled(button):
if self.btn_states.release(button):
self._send_released_raw(button)
self.release_all_modifiers()
def release_all_modifiers(self):
for modifier in self.modifiers.all_pressed_except_caps():
self._send_released_raw(modifier)
self.modifiers.release(modifier)
def force_release_modifiers(self):
for modifier in self.modifiers.all:
self._send_released_raw(modifier)
self.modifiers.release(modifier)
def press_modifier(self, state):
self._send_pressed_raw(self.modifiers.current)
self.modifiers.set_state(state)
def release_modifier(self):
self._send_released_raw(self.modifiers.current)
self.modifiers.release(self.modifiers.current)
def _send_pressed_raw(self, button):
self.msg[0] = button + 128
self.button_sock.send(self.msg)
def _send_released_raw(self, button):
self.msg[0] = button
self.button_sock.send(self.msg)
def force_release(self, button):
self._send_released_raw(button)
self.btn_states.release(button)
def double_click(self):
self.send_type(self.LeftMouse)
sleep(self.double_click_delay)
self.send_type(self.LeftMouse)
def __init__(self):
from typing_layout import load_layout, load_configs, generate_mouse_hints, generate_hints
self.CentralDir = 5 - 1
self.mouse_mode_layout, self.typing_layout = load_layout()
self.mouse_hints = generate_mouse_hints(self.mouse_mode_layout)
self.detailed_hints, self.preview_hints = generate_hints(self.typing_layout)
configs = load_configs()
self.msg = bytearray(1)
self.connect(configs)
self.lang = 'en'
self.LeftMouse = LeftMouse
self.RightMouse = RightMouse
self.MiddleMouse = MiddleMouse
self.mouse_buttons = (self.LeftMouse, self.RightMouse, self.MiddleMouse)
self.Ctrl = Ctrl
self.Alt = Alt
self.Shift = Shift
self.Caps = Caps
self.Switch_code = Switch_code
self.Esc = Esc
self.modifiers = ModifiersState()
self.btn_states = BtnState()
self.reset_typing()
self.is_high_precision = True
self.scroll_cfg = configs['scroll']
self.set_scroll_profile()
touchpad_cfg = configs['touchpad']
self.double_click_delay = touchpad_cfg['double_click_delay']
self.visuals_for_touchpad = bool(touchpad_cfg['visuals'])
font_size_cfg = configs['font']['size']
self.font_size = font_size_cfg['normal']
self.is_game_mode = bool(configs['is_game_mode'])
game_cfg = configs['game']
self.game_button_delay = game_cfg['button_delay']
self.is_mouse_mode = True