-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathuinput_iface_touchscreen.go
144 lines (114 loc) · 3.01 KB
/
uinput_iface_touchscreen.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
package uinput
import (
"fmt"
"io"
"os"
"time"
)
// TouchScreen interface
type TouchScreen interface {
Touch(x int32, y int32) error
io.Closer
}
type vTouchScreen struct {
devFile *os.File
}
func setupTouchScreen(devFile *os.File, minX int32, maxX int32, minY int32, maxY int32) error {
var uinp uinputUserDev
uinp.Name = uinputSetupNameToBytes([]byte("GoUinputDevice"))
uinp.ID.BusType = BusVirtual
uinp.ID.Vendor = 1
uinp.ID.Product = 2
uinp.ID.Version = 3
uinp.AbsMin[AbsMtPositionX] = minX
uinp.AbsMax[AbsMtPositionX] = maxX
uinp.AbsMin[AbsMtPositionY] = minY
uinp.AbsMax[AbsMtPositionY] = maxY
buf, err := uinputUserDevToBuffer(uinp)
if err != nil {
goto err
}
err = ioctl(devFile, uiSetEvBit, uintptr(EvKey))
if err != nil {
err = fmt.Errorf("Could not perform UI_SET_EVBIT ioctl: %v", err)
goto err
}
err = ioctl(devFile, uiSetEvBit, uintptr(EvAbs))
if err != nil {
err = fmt.Errorf("Could not perform UI_SET_EVBIT ioctl: %v", err)
goto err
}
err = ioctl(devFile, uiSetKeyBit, uintptr(BtnTouch))
if err != nil {
err = fmt.Errorf("Could not perform UI_SET_KEYBIT ioctl: %v", err)
goto err
}
err = ioctl(devFile, uiSetAbsBit, uintptr(AbsMtPositionX))
if err != nil {
err = fmt.Errorf("Could not perform UI_SET_ABSBIT ioctl for X axis: %v", err)
goto err
}
err = ioctl(devFile, uiSetAbsBit, uintptr(AbsMtPositionY))
if err != nil {
err = fmt.Errorf("Could not perform UI_SET_ABSBIT ioctl for Y axis: %v", err)
goto err
}
_, err = devFile.Write(buf)
if err != nil {
err = fmt.Errorf("Could not write uinputUserDev to device: %v", err)
goto err
}
err = ioctl(devFile, uiDevCreate, uintptr(0))
if err != nil {
err = fmt.Errorf("Could not perform UI_DEV_CREATE ioctl: %v", err)
goto err
}
time.Sleep(time.Millisecond * 200)
return nil
err:
destroyDevice(devFile)
return err
}
// CreateTouchScreen creates virtual input device that emulates touch screen
func CreateTouchScreen(minX int32, maxX int32, minY int32, maxY int32) (TouchScreen, error) {
dev, err := openUinputDev()
if err != nil {
return nil, err
}
err = setupTouchScreen(dev, minX, maxX, minY, maxY)
if err != nil {
return nil, err
}
return vTouchScreen{devFile: dev}, err
}
// Touch emits touch event
func (vts vTouchScreen) Touch(x int32, y int32) error {
err := emitEvent(vts.devFile, EvAbs, AbsMtPositionX, x)
if err != nil {
return fmt.Errorf("emitEvent: %v", err)
}
err = emitEvent(vts.devFile, EvAbs, AbsMtPositionY, y)
if err != nil {
return fmt.Errorf("emitEvent: %v", err)
}
err = emitEvent(vts.devFile, EvSyn, 2, 0)
if err != nil {
return fmt.Errorf("emitEvent: %v", err)
}
err = emitEvent(vts.devFile, EvSyn, 0, 0)
if err != nil {
return fmt.Errorf("emitEvent: %v", err)
}
err = emitEvent(vts.devFile, EvSyn, 2, 0)
if err != nil {
return fmt.Errorf("emitEvent: %v", err)
}
err = emitEvent(vts.devFile, EvSyn, 0, 0)
if err != nil {
return fmt.Errorf("emitEvent: %v", err)
}
return nil
}
func (vts vTouchScreen) Close() error {
return destroyDevice(vts.devFile)
}