forked from openatx-archive/go-minitouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minitouch.go
377 lines (347 loc) · 7.01 KB
/
minitouch.go
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* File: minicap.go
* Author : bigwavelet
* Description: android minicap service
* Created: 2016-09-13
*/
package minitouch
import (
"bufio"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"os/exec"
"strings"
"sync"
"syscall"
"time"
// "github.com/pixiv/go-libjpeg/jpeg" // not work on windows
)
var (
ErrAlreadyClosed = errors.New("already closed")
HOST = "127.0.0.1"
)
type Options struct {
Serial string
Port int
Adb string
}
type Service struct {
d AdbDevice
proc *exec.Cmd
port int
host string
closed bool
brd *bufio.Reader
mu sync.Mutex
cmdC chan string
restartC chan bool
r Rotation
orientation int
dispInfo DisplayInfo
}
/*
Download file to device based on goadb OpenWrite interface.
*/
func (s *Service) download(path, url string) (err error) {
fout, err := s.d.Device.OpenWrite(path, 0755, time.Now())
if err != nil {
return
}
defer fout.Close()
response, err := http.Get(url)
if err != nil {
return
}
defer response.Body.Close()
_, err = io.Copy(fout, response.Body)
if err != nil {
return
}
return
}
/*
Create Minitouch Service
Description:
Serial : device serialno
Port(default: random): minitouch service port
Adb(default: adb): adb path
Eg.
opt := Option{}
opt.Serial = "aaa"
service := minitouch.NewService(opt)
*/
func NewService(option Options) (s Service, err error) {
s = Service{}
s.d, err = newAdbDevice(option.Serial, option.Adb)
if option.Port == 0 {
port, err := randPort()
if err != nil {
return s, errors.New("port required")
}
s.port = port
} else {
s.port = option.Port
}
s.host = HOST
s.closed = true
if err != nil {
return
}
s.restartC = make(chan bool, 1)
s.r, err = newRotationService(option)
return
}
/*
Install minitouch on device
Eg.
service := minitouch.NewService(opt)
err := service.Install()
P.s.
Install function will download files, so keep network connected.
*/
func (s *Service) Install() (err error) {
err = s.r.install()
if err != nil {
return
}
abi, err := s.d.getProp("ro.product.cpu.abi")
if err != nil {
return
}
if isExists := s.d.isFileExists("/data/local/tmp/minitouch"); isExists {
return
}
url := "https://github.com/openstf/stf/raw/master/vendor/minitouch/" + abi + "/minitouch"
err = s.download("/data/local/tmp/minitouch", url)
return
}
/*
Check whether minitouch is supported on the device
Eg.
service := minitouch.NewService(opt)
supported := service.IsSupported()
For more information, see: https://github.com/openstf/minitouch
*/
func (s *Service) IsSupported() bool {
fileExists := s.d.isFileExists("/data/local/tmp/minitouch")
if !fileExists {
err := s.Install()
if err != nil {
return false
}
}
out, err := s.d.shell("/data/local/tmp/minitouch", "-h")
if err != nil {
return false
}
supported := strings.Contains(out, "-d") && strings.Contains(out, "-n") && strings.Contains(out, "-h")
return supported
}
/*
Uninstall minicap service
Remove minicap on the device
Eg.
service := minicap.NewService(opt)
err := service.Uninstall()
*/
func (s *Service) Uninstall() (err error) {
if isExists := s.d.isFileExists("/data/local/tmp/minitouch"); isExists {
if _, err := s.d.shell("rm", "/data/local/tmp/minitouch"); err != nil {
return err
}
return
}
return
}
/*
Start minitouch service
*/
func (s *Service) startMinitouch() (err error) {
if !s.IsSupported() {
return errors.New("sorry, minitouch not supported")
}
s.closeMinitouch()
s.proc = s.d.buildCommand("/data/local/tmp/minitouch")
s.proc.Stderr = os.Stderr
stdoutReader, err := s.proc.StdoutPipe()
if err != nil {
return
}
s.brd = bufio.NewReader(stdoutReader)
err = s.proc.Start()
if err != nil {
return
}
time.Sleep(3 * time.Second)
if _, err = s.d.run("forward", fmt.Sprintf("tcp:%d", s.port), "localabstract:minitouch"); err != nil {
return
}
s.closed = false
s.cmdC = make(chan string, 1)
return
}
/*
Close Minitouch stream
Eg.
err := service.Close()
*/
func (s *Service) Close() (err error) {
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return ErrAlreadyClosed
}
s.closed = true
s.closeMinitouch()
s.d.run("forward", "--remove", fmt.Sprintf("tcp:%d", s.port))
return
}
func (s *Service) closeMinitouch() (err error) {
if s.proc != nil && s.proc.Process != nil {
s.proc.Process.Signal(syscall.SIGTERM)
}
//kill minitouch proc on device
err = s.d.killProc("minitouch")
return
}
/*
check whether the minitouch stream is closed.
Eg.
err := service.IsClosed()
*/
func (s *Service) IsClosed() bool {
return s.closed
}
func (s *Service) sendMinitouch() (err error) {
if err = s.startMinitouch(); err != nil {
return
}
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", s.host, s.port))
if err != nil {
return
}
go func() {
for cmd := range s.cmdC {
if cmd == "" {
continue
} else if cmd[len(cmd)-1] != '\n' {
cmd += "\n"
}
_, err := conn.Write([]byte(cmd))
if err != nil {
log.Fatal(err)
s.restartC <- true
}
}
conn.Close()
}()
return
}
func (s *Service) Start() (err error) {
//rotation watcher
err = s.r.start()
if err != nil {
return
}
orienC, err := s.r.watch()
if err != nil {
return
}
go func() {
for {
s.orientation = <-orienC
}
}()
s.sendMinitouch()
return
if err = s.sendMinitouch(); err != nil {
return
}
go func() {
for {
<-s.restartC
if err := s.startMinitouch(); err != nil {
break
}
}
}()
return
}
//handle posx, posy
func (s *Service) handlePos(oldX, oldY int) (newX, newY int) {
if s.dispInfo.Height == 0 {
var err error
s.dispInfo, err = s.d.getDisplayInfo()
if err != nil {
return oldX, oldY
}
}
if s.dispInfo.Width > s.dispInfo.Height {
s.dispInfo.Width, s.dispInfo.Height = s.dispInfo.Height, s.dispInfo.Width
}
switch s.orientation {
case 0:
newX, newY = oldX, oldY
case 90:
newX, newY = s.dispInfo.Width-oldY, oldX
case 270:
newX, newY = oldY, s.dispInfo.Height-oldX
}
return
}
/*
Click postiion(x,y)
*/
func (s *Service) Click(x, y int) {
x, y = s.handlePos(x, y)
cmd := fmt.Sprintf("d 0 %d %d 50\nc\nu 0\nc\n", x, y)
s.cmdC <- cmd
return
}
/*
Swipe from (sx, sy) to (ex, ey)
*/
func (s *Service) Swipe(sx, sy, ex, ey int) {
sx, sy = s.handlePos(sx, sy)
ex, ey = s.handlePos(ex, ey)
step := 10
dx := (ex - sx) / step
dy := (ey - sy) / step
s.cmdC <- fmt.Sprintf("d 0 %d %d 50\nc\n", sx, sy)
for i := 0; i < step; i++ {
x, y := sx+i*dx, sy+i*dy
s.cmdC <- fmt.Sprintf("m 0 %d %d 50\nc\n", x, y)
}
s.cmdC <- fmt.Sprintf("u 0 %d %d 50\nc\nu 0\nc\n", ex, ey)
return
}
/*
General interface: Operation
@Parameters:
action:
d: down
m: move
u: up
index:
input index
PosX:
postion x axis
PosY:
postion y axis
*/
func (s *Service) Operation(action string, index, posX, posY int) {
posX, posY = s.handlePos(posX, posY)
switch action {
case "d":
s.cmdC <- fmt.Sprintf("d %v %v %v 50\nc\n", index, posX, posY)
case "m":
s.cmdC <- fmt.Sprintf("m %v %v %v 50\nc\n", index, posX, posY)
case "u":
s.cmdC <- fmt.Sprintf("u %v %v %v 50\nc\nu %v\nc\n", index, posX, posY, index)
}
}