-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
79 lines (71 loc) · 2.82 KB
/
handler.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
import socket
import json
from enum import IntEnum
class Command(IntEnum):
MOVE = 1
CLICK = 2
DOUBLE_CLICK = 3
RIGHT_CLICK = 4
SCROLL = 5
SCREENSHOT = 6
class Handler:
def __init__(self) -> None:
self.port = 12345
# self.mouse_down = False
self.clicked = False
self.right_clicked = False
self.double_clicked = False
self.screenshot = False
self.last_scroll = -1
self.initialize_server()
def initialize_server(self):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
server_socket.bind(('192.168.148.23', self.port))
server_socket.listen(1)
print(f'Server is listening at port {self.port}')
connection, address = server_socket.accept()
print(f"Connected by {address}")
self.client = connection
def handle_gesture(self, gesture, co1=(0, 0)):
# move mouse
if gesture == [0, 1, 0, 0, 0]:
data = {'type': Command.MOVE, 'x': float(co1[0]), 'y': float(co1[1])}
self.client.send((json.dumps(data) + '\0').encode('utf-8'))
print("Move mouse case")
self.clicked = False
self.right_clicked = False
self.double_clicked = False
self.screenshot = False
# click
elif gesture == [0, 1, 1, 0, 0]:
if not self.clicked:
data = {'type': Command.CLICK}
self.client.send((json.dumps(data) + '\0').encode('utf-8'))
print("Click case")
self.clicked = True
# double click
elif gesture == [1, 1, 0, 0, 0]:
if not self.double_clicked:
data = {'type': Command.DOUBLE_CLICK}
self.client.send((json.dumps(data) + '\0').encode('utf-8'))
print("Double click case")
self.double_clicked = True
# right click
elif gesture == [1, 1, 1, 0, 0]:
if not self.right_clicked:
data = {'type': Command.RIGHT_CLICK}
self.client.send((json.dumps(data) + '\0').encode('utf-8'))
print("Right click case")
self.right_clicked = True
# scroll
elif gesture == [1, 1, 1, 1, 1]:
if not self.screenshot:
data = {'type': Command.SCREENSHOT}
self.client.send((json.dumps(data) + '\0').encode('utf-8'))
self.screenshot = True
# elif abs(self.last_scroll - co1[1]) > 10:
# offset = int((self.last_scroll - co1[1]))
# self.last_scroll = co1[1]
# data = {'type': Command.SCROLL, 'offset': offset}
# self.client.send((json.dumps(data) + '\0').encode('utf-8'))
# print("Scroll case")