-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathminitouch.go
283 lines (258 loc) · 6.19 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
package stf
import (
"bufio"
"fmt"
"io"
"log"
"net"
"os"
"strconv"
"strings"
"syscall"
"time"
adb "github.com/openatx/go-adb"
"github.com/pkg/errors"
)
type TouchAction int
const (
TOUCH_DOWN = TouchAction(iota)
TOUCH_MOVE
TOUCH_UP
)
type STFTouch struct {
cmdC chan string
conn net.Conn
maxX, maxY int
rotation int
*adb.Device
errorMixin
safeMixin
}
func NewSTFTouch(device *adb.Device) *STFTouch {
return &STFTouch{
Device: device,
cmdC: make(chan string, 0),
}
}
func (s *STFTouch) Start() error {
return s.safeDo(_ACTION_START, func() error {
s.resetError()
if err := s.prepare(); err != nil {
return err
}
go s.runBinary()
go func() {
time.Sleep(time.Second)
s.drainCmd()
}()
return nil
})
}
func (s *STFTouch) Stop() error {
return s.safeDo(_ACTION_STOP, func() error {
s.killProc("minitouch", syscall.SIGKILL)
return s.Wait()
})
}
func (s *STFTouch) SetRotation(r int) {
s.rotation = r
}
func (s *STFTouch) width() float64 {
if s.rotation == 0 || s.rotation == 180 {
return float64(s.maxX)
} else {
return float64(s.maxY)
}
}
func (s *STFTouch) height() float64 {
if s.rotation == 0 || s.rotation == 180 {
return float64(s.maxY)
} else {
return float64(s.maxX)
}
}
/**
* Rotation affects the screen as follows:
*
* 0deg
* |------|
* | MENU |
* |------|
* --> | | --|
* | | | v
* | |
* | |
* |------|
* |----|-| |-|----|
* | |M| | | |
* | |E| | | |
* 90deg | |N| |U| | 270deg
* | |U| |N| |
* | | | |E| |
* | | | |M| |
* |----|-| |-|----|
* |------|
* ^ | | |
* |-- | | <--
* | |
* | |
* |------|
* | UNEM |
* |------|
* 180deg
*
* Which leads to the following mapping:
*
* |--------------|------|---------|---------|---------|
* | | 0deg | 90deg | 180deg | 270deg |
* |--------------|------|---------|---------|---------|
* | CSS rotate() | 0deg | -90deg | -180deg | 90deg |
* | bounding w | w | h | w | h |
* | bounding h | h | w | h | w |
* | pos x | x | h-y | w-x | y |
* | pos y | y | x | h-y | h-x |
* |--------------|------|---------|---------|---------|
*/
func (s *STFTouch) coords(xP, yP float64) (x, y int) {
switch s.rotation {
case 90:
xP, yP = 1-yP, xP
case 180:
xP, yP = 1-xP, 1-yP
case 270:
xP, yP = yP, 1-xP
}
w, h := float64(s.maxX), float64(s.maxY)
return int(w * xP), int(h * yP)
}
func (s *STFTouch) Down(index int, xP, yP float64) {
posX, posY := s.coords(xP, yP)
s.cmdC <- fmt.Sprintf("d %v %v %v 50", index, posX, posY)
}
func (s *STFTouch) Move(index int, xP, yP float64) {
posX, posY := s.coords(xP, yP)
s.cmdC <- fmt.Sprintf("m %v %v %v 50", index, posX, posY)
}
func (s *STFTouch) Up(index int) {
s.cmdC <- fmt.Sprintf("u %d", index)
}
func (s *STFTouch) prepare() error {
dst := "/data/local/tmp/minitouch"
if AdbFileExists(s.Device, dst) {
return nil
}
props, err := s.Properties()
if err != nil {
return err
}
abi, ok := props["ro.product.cpu.abi"]
if !ok {
return errors.New("No ro.product.cpu.abi propery")
}
urlStr := "https://github.com/openstf/stf/raw/master/vendor/minitouch/" + abi + "/minitouch"
return PushFileFromHTTP(s.Device, dst, 0755, urlStr)
}
func (s *STFTouch) runBinary() (err error) {
defer s.doneError(err)
c, err := s.OpenCommand("/data/local/tmp/minitouch")
if err != nil {
return
}
defer c.Close()
// _, err = io.Copy(ioutil.Discard, c)
_, err = io.Copy(os.Stdout, c)
return nil
}
func (s *STFTouch) drainCmd() {
if err := s.dialWithRetry(); err != nil {
s.doneError(errors.Wrap(err, "dial minitouch"))
return
}
for c := range s.cmdC {
c = strings.TrimSpace(c) + "\nc\n" // c: commit
_, err := io.WriteString(s.conn, c)
if err != nil {
s.doneError(errors.Wrap(err, "write command to minitouch tcp"))
s.conn.Close()
s.conn = nil
break
}
}
}
type lineFormatReader struct {
bufrd *bufio.Reader
err error
}
func (r *lineFormatReader) Scanf(format string, args ...interface{}) error {
if r.err != nil {
return r.err
}
var line []byte
line, _, r.err = r.bufrd.ReadLine()
if r.err != nil {
return r.err
}
_, r.err = fmt.Sscanf(string(line), format, args...)
return r.err
}
func (s *STFTouch) dialWithRetry() error {
var err error
for i := 0; i < 10; i++ {
err = s.dialTouch()
if err == nil {
return nil
}
log.Println("dial minitouch service fail, reconnect, err is", err)
time.Sleep(100 * time.Millisecond)
}
return err
}
func (s *STFTouch) dialTouch() error {
port, err := s.ForwardToFreePort(adb.ForwardSpec{adb.FProtocolAbstract, "minitouch"})
if err != nil {
return err
}
s.conn, err = net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port))
if err != nil {
return err
}
lineRd := lineFormatReader{bufrd: bufio.NewReader(s.conn)}
var flag string
var ver int
var maxContacts, maxPressure int
var pid int
lineRd.Scanf("%s %d", &flag, &ver)
lineRd.Scanf("%s %d %d %d %d", &flag, &maxContacts, &s.maxX, &s.maxY, &maxPressure)
if err := lineRd.Scanf("%s %d", &flag, &pid); err != nil {
s.conn.Close()
return err
}
return nil
}
// FIXME(ssx): maybe need to put into go-adb
func (s *STFTouch) killProc(psName string, sig syscall.Signal) (err error) {
out, err := s.RunCommand("ps", "-C", psName)
if err != nil {
return
}
lines := strings.Split(strings.TrimSpace(out), "\n")
if len(lines) <= 1 {
return errors.New("No process named " + psName + " founded.")
}
var pidIndex int
for idx, val := range strings.Fields(lines[0]) {
if val == "PID" {
pidIndex = idx
break
}
}
for _, line := range lines[1:] {
fields := strings.Fields(line)
if !strings.Contains(line, psName) {
continue
}
pid := fields[pidIndex]
s.RunCommand("kill", "-"+strconv.Itoa(int(sig)), pid)
}
return
}