-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
351 lines (275 loc) · 10.1 KB
/
__main__.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
import time
import uuid
import threading
from bluetooth import *
from pynput.keyboard import Key, Controller as KeyboardController
from pynput.mouse import Button, Controller as MouseController
global server_sock, client_sock, CONST, mouse, keyboard
def constant(f):
def f_set(self, value):
raise TypeError
def f_get(self):
return f(self)
return property(f_get, f_set)
class _Const(object):
@constant
def parameter_separator(self): return '\\t'
@constant
def line_separator(self): return '\\n'
@constant
def MOVE_LEFT(self): return 3
@constant
def MOVE_RIGHT(self): return 4
@constant
def MOVE_UP(self): return 5
@constant
def MOVE_DOWN(self): return 6
@constant
def CLICK(self): return 1
@constant
def RIGHT_CLICK(self): return 2
@constant
def DOUBLE_CLICK(self): return 3
@constant
def MOVE_CURSOR_RELATIVE(self): return 4
@constant
def MOVE_CURSOR_ABSOLUTE(self): return 5
@constant
def SELECT(self): return 6
@constant
def SCROLL(self): return 7
@constant
def UNDO(self): return 8
@constant
def COPY(self): return 9
@constant
def PASTE(self): return 10
@constant
def CUT(self): return 11
@constant
def RETURN_TO_DESKTOP(self): return 12
@constant
def ENABLE_TASK_MODE(self): return 13
@constant
def SWITCH_APPLICATION(self): return 14
@constant
def SWITCH_TAB(self): return 15
@constant
def INPUT_CHARACTER(self): return 16
@constant
def CANCEL_LAST_ACTION_FUNCTIONAL(self): return 100
@constant
def HEARTBEAT_FUNCTIONAL(self): return 101
@constant
def ACTION_NOT_FOUND_FUNCTIONAL(self): return 102
@constant
def EXITING_TOUCH_PAD_FUNCTIONAL(self): return 103
class SwitchException(Exception):
pass
class Mouse:
__scroll_thread = None
__scroll_tolerance = 10
__scroll_resistance = 1.5
__scroll_momentum_x = 0
__scroll_momentum_y = 0
__scroll_timeout = 0
def __init__(self):
self.__mouse = MouseController()
self.__is_dragging = False
def drag_or_release(self):
if self.__is_dragging:
self.__mouse.release(Button.left)
else:
self.__mouse.press(Button.left)
self.__is_dragging = not self.__is_dragging
def click(self, button, times):
self.__mouse.click(button, times)
def move(self, delta_x, delta_y, delay=0):
if delay == 0:
thread = threading.Thread(target=mouse.move, args=(delta_x, delta_y, 0.05))
thread.start()
time.sleep(delay)
self.__mouse.move(delta_x, delta_y)
def position(self, x, y):
self.__mouse.position(x, y)
def scroll(self, delta_x, delta_y):
if self.__scroll_thread is None:
print("new scroll thread created")
self.__scroll_thread = threading.Thread(target=self.__scroll_with_inertia)
self.__scroll_thread.start()
if delta_x * self.__scroll_momentum_x < 0:
self.__scroll_momentum_x = 0
else:
self.__scroll_momentum_x += delta_x
if delta_y * self.__scroll_momentum_y < 0:
self.__scroll_momentum_y = 0
else:
self.__scroll_momentum_y += delta_y
def __scroll_with_inertia(self):
while self.__scroll_timeout < 3:
time.sleep(0.1)
if self.__scroll_momentum_x == 0 and self.__scroll_momentum_y == 0:
self.__scroll_timeout += 0.1
continue
threading.Thread(target=self.__scroll_with_delay,
args=(self.__scroll_momentum_x, -self.__scroll_momentum_y)).start()
self.__scroll_momentum_x /= self.__scroll_resistance
self.__scroll_momentum_y /= self.__scroll_resistance
if abs(self.__scroll_momentum_x) < self.__scroll_tolerance:
self.__scroll_momentum_x = 0
if abs(self.__scroll_momentum_y) < self.__scroll_tolerance:
self.__scroll_momentum_y = 0
self.__scroll_thread = None
self.__scroll_timeout = 0
print("new scroll thread terminated")
def __scroll_with_delay(self, momentum_x, momentum_y):
times = 10
delay = 0.1 / times
print(delay, momentum_x / times, momentum_y / times)
for _ in range(times):
self.__mouse.scroll(momentum_x / times, momentum_y / times)
time.sleep(delay)
class Keyboard:
def __init__(self):
self.__keyboard = KeyboardController()
self.__stored_key_list = []
def release_all(self):
for button in self.__stored_key_list:
self.__keyboard.release(button)
self.__stored_key_list.clear()
def press_key_release(self, button):
self.__keyboard.press(button)
self.__keyboard.release(button)
def press_key_store(self, button):
if button not in self.__stored_key_list:
self.__stored_key_list.append(button)
self.__keyboard.press(button)
def stored_key_release(self, button):
if button in self.__stored_key_list:
self.__stored_key_list.remove(button)
self.__keyboard.release(button)
def switch_application(self, direction):
self.press_key_store(Key.alt)
if direction == CONST.MOVE_RIGHT:
self.stored_key_release(Key.shift)
else:
self.press_key_store(Key.shift)
self.press_key_release(Key.tab)
def switch_tab(self, direction):
self.press_key_store(Key.ctrl)
if direction == CONST.MOVE_RIGHT:
self.stored_key_release(Key.shift)
else:
self.press_key_store(Key.shift)
self.press_key_release(Key.tab)
def return_to_desktop(self):
self.press_key_store(Key.cmd)
self.press_key_release('d')
self.stored_key_release(Key.cmd)
def enable_task_mode(self):
self.press_key_store(Key.cmd)
self.press_key_release(Key.tab)
self.stored_key_release(Key.cmd)
def undo(self):
self.press_key_store(Key.ctrl)
self.press_key_release('z')
self.stored_key_release(Key.ctrl)
def copy(self):
self.press_key_store(Key.ctrl)
self.press_key_release('c')
self.stored_key_release(Key.ctrl)
def paste(self):
self.press_key_store(Key.ctrl)
self.press_key_release('v')
self.stored_key_release(Key.ctrl)
def cut(self):
self.press_key_store(Key.ctrl)
self.press_key_release('x')
self.stored_key_release(Key.ctrl)
def touch_pad_handle_message(message):
separated_message = message.split(CONST.line_separator)
for ele in separated_message:
param_list = ele.split(CONST.parameter_separator)
print(param_list)
instruction = int(param_list[0])
if instruction == CONST.CLICK:
mouse.click(Button.left, 1)
elif instruction == CONST.RIGHT_CLICK:
mouse.click(Button.right, 1)
elif instruction == CONST.DOUBLE_CLICK:
mouse.click(Button.left, 2)
elif instruction == CONST.MOVE_CURSOR_RELATIVE:
mouse.move(int(param_list[1]), int(param_list[2]))
elif instruction == CONST.MOVE_CURSOR_ABSOLUTE:
mouse.position(int(param_list[1]), int(param_list[2]))
elif instruction == CONST.SELECT:
mouse.drag_or_release()
elif instruction == CONST.SCROLL:
mouse.scroll(int(param_list[1]) * 20, int(param_list[2]) * 10)
elif instruction == CONST.UNDO:
keyboard.undo()
elif instruction == CONST.COPY:
keyboard.copy()
elif instruction == CONST.PASTE:
keyboard.paste()
elif instruction == CONST.CUT:
keyboard.cut()
elif instruction == CONST.RETURN_TO_DESKTOP:
keyboard.return_to_desktop()
elif instruction == CONST.ENABLE_TASK_MODE:
keyboard.enable_task_mode()
elif instruction == CONST.SWITCH_APPLICATION:
keyboard.switch_application(int(param_list[1]))
elif instruction == CONST.SWITCH_TAB:
keyboard.switch_tab(param_list[1])
# elif instruction == CONST.INPUT_CHARACTER:
elif instruction == CONST.CANCEL_LAST_ACTION_FUNCTIONAL:
keyboard.release_all()
elif instruction == CONST.EXITING_TOUCH_PAD_FUNCTIONAL:
raise SwitchException
else:
pass
def start_server():
global server_sock, client_sock
server_sock = BluetoothSocket(RFCOMM)
server_sock.bind(("", PORT_ANY))
server_sock.listen(1)
server_uuid = "00001101-0000-1000-8000-00805F9B34FB"
advertise_service(server_sock, "SampleServer",
service_id=server_uuid,
service_classes=[server_uuid, SERIAL_PORT_CLASS],
profiles=[SERIAL_PORT_PROFILE],
# protocols = [ OBEX_UUID ]
)
print("Bluetooth address of this machine:\n" + ':'.join(['{:02x}'.format((uuid.getnode() >> ele) & 0xff)
for ele in range(0, 8 * 6, 8)][::-1]))
print("Waiting for connection")
client_sock, client_info = server_sock.accept()
print("Accepted connection from ", client_info)
def receive_from_client():
return str(client_sock.recv(1024))[2:-3]
def general_listen():
try:
while True:
line_read = receive_from_client()
if line_read == "TOUCH_PAD":
print("TOUCH PAD SESSION START")
try:
while True:
touch_pad_handle_message(receive_from_client())
except SwitchException:
print("TOUCH PAD SESSION END")
if line_read == "EXIT":
break
except IOError:
print("IOError")
if __name__ == '__main__':
CONST = _Const()
mouse = Mouse()
keyboard = Keyboard()
start_server()
general_listen()
print("Disconnected")
client_sock.close()
server_sock.close()
print("Socket closed")