-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdriver_for_mac.py
230 lines (208 loc) · 5.54 KB
/
driver_for_mac.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
import socket
import pyautogui
import time
import subprocess
import threading
from qrcode import QRCode
import os
import platform
from pynput.mouse import Button, Controller as MouseController
pyautogui.PAUSE = 0.01
keyboard = None
mouse = MouseController()
xSupport = True if platform.system() == 'Windows' else False
xcontroller = None
if(xSupport):
import pyxinput
xcontroller = pyxinput.vController()
import dinput
from pynput.keyboard import Key, Controller as KeyboardController
keyboard = KeyboardController()
else:
from pymouse import PyMouse
from pykeyboard import PyKeyboard
keyboard = PyKeyboard()
button = '$'
duty_ratio = 0
sub = '$'
serverIP = ''
def get_ip_address():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
return s.getsockname()[0]
class myThread (threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
global sub
while(True):
if button == '$'or duty_ratio==0 :
time.sleep(0.1)
continue
sub = button
keyboard.press(sub)
time.sleep(0.1*duty_ratio)
keyboard.release(sub)
time.sleep((0.1)*(1-duty_ratio))
thread1 = myThread(1, "Thread-1", 1)
def main():
s = socket.socket()
thread1.start()
print ("Socket successfully created.")
while True:
try:
print("Enter port (leave blank for 4444): ",end="")
inp = input()
if inp == '':
port = 4444
else:
port = int(inp)
s.bind(('', port))
print ("Socket bound to %s" %(port))
break
except OSError:
print("Port " + str(port) + " is already in use. Please use the following command:")
print("lsof -n -i4TCP:" + str(port) +" | grep LISTEN")
os._exit(os.EX_OK)
except:
print ("Unexpected error while connecting to port")
s.listen()
try:
# serverIP = socket.gethostbyname(socket.gethostname())+":"+str(port)
serverIP = get_ip_address()+":"+str(port)
except socket.gaierror:
output = subprocess.check_output("ipconfig getifaddr en0", shell=True).decode()[:-1]
serverIP = output+":"+str(port)
qr = QRCode()
qr.add_data(serverIP)
while True:
print("Connect at IP = "+serverIP)
print('Or scan this:')
qr.print_ascii(invert=True)
qr.print_ascii()
print ("Socket is listening...")
c, addr = s.accept()
print ('Got connection from', addr)
while True:
try:
message = c.recv(256).decode("utf-8"); #message comes in byte array so change it to string first
message = message.split("%") #use & to split tokens, and % to split messages.
for msg in message:
if msg != '':
msg = msg.split("&")
print("DEBUG: ", msg)
if(msg[0] == 'button'):
handleButton(msg[1], msg[2])
elif(msg[0] == 'tilt'):
if(len(msg) == 2):
if(msg[1] == '0'):
global button
button = '$'
if(len(msg) < 3):
continue
elif(msg[2] == ''):
continue
elif msg[1] == '+':
tilt(True, float(msg[2]))
elif msg[1] == '-':
tilt(False, float(msg[2]))
elif(msg[0] == 'cont'):
handleController(msg[1:])
elif(msg[0] == 'track'):
handleTrackpad(msg[1:])
elif(msg[0] == 'status'):
c.send(bytes('pass'+msg[1],"utf-8"))
elif(msg[0] == 'disconnect'):
raise ConnectionResetError
except ConnectionResetError:
print("Disconnected from client")
c.close()
print("Socket closed")
ans = input("Do you want to connect again? (y/n): ")
if(ans == 'n'):
os._exit(0)
break
#c.send(bytes('Thank you for connecting', "utf-8"))
def handleButton(type, msg):
if(type == 'down'):
# pyautogui.keyDown(msg)
# if(msg=='space'):
# keyboard.press(Key.space)
# elif(msg=='shift'):
# keyboard.press(Key.shift)
# else:
# keyboard.press(msg)
if xSupport:
dinput.handleInputs(1,msg)
else:
keyboard.press_key(msg)
else:
# pyautogui.keyUp(msg)
# if(msg=='space'):
# keyboard.release(Key.space)
# elif(msg=='shift'):
# keyboard.release(Key.shift)
# else:
# keyboard.release(msg)
if xSupport:
dinput.handleInputs(0,msg)
else:
keyboard.release_key(msg)
def tilt(message,value):
global button
global duty_ratio
if message:
button = 'a'
duty_ratio = value
else:
button = 'd'
duty_ratio = value
def handleController(msg):
try:
if(not xSupport):
return
if('down' in msg[0] or 'up' in msg[0]):
if('Btn' in msg[1]):
typeInt = 1 if msg[0] == 'down' else 0
xcontroller.set_value(msg[1], typeInt)
elif('Trigger' in msg[1]):
typeInt = 127 if msg[0] == 'down' else 0
xcontroller.set_value(msg[1], typeInt)
elif('Axis' in msg[0]):
xcontroller.set_value(msg[0], float(msg[1]))
elif('Dpad' in msg[0]):
xcontroller.set_value(msg[0], int(msg[1]))
else:
print(msg)
print('^Not yet handled in driver^')
except:
print("Error: Incomplete message received")
def handleTrackpad(msg):
try:
if(len(msg) == 3):
if(msg[0] == 'move'):
mouse.move(float(msg[1])*5, float(msg[2])*5)
elif(msg[0] == 'button'):
if(msg[1] == 'down'):
if(msg[2] == 'left'):
mouse.press(Button.left)
elif(msg[2] == 'right'):
mouse.press(Button.right)
else:
if(msg[2] == 'left'):
mouse.release(Button.left)
elif(msg[2] == 'right'):
mouse.release(Button.right)
elif(msg[0] == 'click'):
clicks = int(msg[1])
if(msg[2] == 'left'):
mouse.click(Button.left, clicks)
if(msg[2] == 'right'):
mouse.click(Button.right, clicks)
except:
print('Invalid message')
if __name__=="__main__":
main()