forked from fhorinek/adbmirror
-
Notifications
You must be signed in to change notification settings - Fork 2
/
touchclient.py
73 lines (65 loc) · 2.46 KB
/
touchclient.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
import socket
import errno
from subprocess import Popen
from threadqueue import ThreadedInOutQueue
from time import sleep
from adb import ADBBIN
class TouchClient(ThreadedInOutQueue):
def __init__(self, parent):
ThreadedInOutQueue.__init__(self)
cmd = [ADBBIN, "shell", " %s/minitouch" % (parent.path)]
self.server = Popen(cmd)
# Sensible defaults for my device, can be overridden later on
self.pressure = 0
self.max_x = 1079
self.max_y = 1919
def cut_data(self, size):
tmp = self.data[:size]
self.data = self.data[size:]
return tmp
def send(self, data):
data = bytes(data, 'UTF-8') + b"\nc\n";
self.socket.sendall(data)
def run(self):
sleep(1)
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect(("localhost", 1111))
self.socket.setblocking(0)
self.running = True
self.data = b""
while self.running:
for msg in self.internal_read():
cmd = msg[0]
if cmd == "end":
self.running = False
if cmd == "down":
x = int(msg[1] * self.max_x)
y = int(msg[2] * self.max_y)
self.send("d 0 %u %u %u" % (x, y, self.pressure))
if cmd == "up":
self.send("u 0")
if cmd == "move":
x = int(msg[1] * self.max_x)
y = int(msg[2] * self.max_y)
self.send("m 0 %u %u %u" % (x, y, self.pressure))
try:
data = self.socket.recv(1024)
self.data += data
except socket.error as e:
err = e.args[0]
if err == errno.EAGAIN or err == errno.EWOULDBLOCK:
pass
while b'\n' in self.data:
data = self.cut_data(self.data.find(b'\n') + 1)
data = data.split()
print('Data received:', data)
if data[0] is b'v':
self.version = int(data[1])
if data[0] is b'$':
self.pid = int(data[1])
if data[0] is b'^':
self.max_x = int(data[2])
self.max_y = int(data[3])
self.pressure = int(data[4])
self.socket.close()
self.server.kill()