-
Notifications
You must be signed in to change notification settings - Fork 5
/
uinput_iface_touchpad.go
212 lines (164 loc) · 4.15 KB
/
uinput_iface_touchpad.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
package uinput
import (
"fmt"
"io"
"os"
"time"
)
type TouchPad interface {
LeftPress() error
LeftRelease() error
LeftClick() error
RightPress() error
RightRelease() error
RightClick() error
MoveTo(x int32, y int32) error
io.Closer
}
type vTouchPad struct {
devFile *os.File
}
func setupTouchPad(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[AbsX] = minX
uinp.AbsMax[AbsX] = maxX
uinp.AbsMin[AbsY] = minY
uinp.AbsMax[AbsY] = maxY
buf, err := uinputUserDevToBuffer(uinp)
if err != nil {
goto err
}
// register left and right buttons click events
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, uiSetKeyBit, uintptr(BtnLeft))
if err != nil {
err = fmt.Errorf("Could not perform UI_SET_KEYBIT ioctl: %v", err)
goto err
}
err = ioctl(devFile, uiSetKeyBit, uintptr(BtnRight))
if err != nil {
err = fmt.Errorf("Could not perform UI_SET_KEYBIT ioctl: %v", err)
goto err
}
// setup absolute axes
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, uiSetAbsBit, uintptr(AbsX))
if err != nil {
err = fmt.Errorf("Could not perform UI_SET_ABSBIT ioctl: %v", err)
goto err
}
err = ioctl(devFile, uiSetAbsBit, uintptr(AbsY))
if err != nil {
err = fmt.Errorf("Could not perform UI_SET_ABSBIT ioctl: %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 {
devFile.Close()
return fmt.Errorf("Could not perform UI_DEV_CREATE ioctl: %v", err)
}
time.Sleep(time.Millisecond * 200)
return nil
err:
destroyDevice(devFile)
return err
}
func CreateTouchPad(minX int32, maxX int32, minY int32, maxY int32) (TouchPad, error) {
dev, err := openUinputDev()
if err != nil {
return nil, err
}
err = setupTouchPad(dev, minX, maxX, minY, maxY)
if err != nil {
return nil, err
}
return vTouchPad{devFile: dev}, err
}
// LeftPress emits left button press event
func (tp vTouchPad) LeftPress() error {
err := emitEvent(tp.devFile, EvKey, BtnLeft, 1)
if err != nil {
return fmt.Errorf("emitEvent: %v", err)
}
err = emitEvent(tp.devFile, EvSyn, 0, SynReport)
if err != nil {
return fmt.Errorf("emitEvent: %v", err)
}
return nil
}
// LeftRelease emits left button release event
func (tp vTouchPad) LeftRelease() error {
err := emitEvent(tp.devFile, EvKey, BtnLeft, 0)
if err != nil {
return fmt.Errorf("emitEvent: %v", err)
}
err = emitEvent(tp.devFile, EvSyn, 0, SynReport)
if err != nil {
return fmt.Errorf("emitEvent: %v", err)
}
return nil
}
// LeftClick emits left button click event
func (tp vTouchPad) LeftClick() error {
err := tp.LeftPress()
err = tp.LeftRelease()
return err
}
// RightPress emits right button press event
func (tp vTouchPad) RightPress() error {
err := emitEvent(tp.devFile, EvKey, BtnRight, 1)
if err != nil {
return fmt.Errorf("emitEvent: %v", err)
}
err = emitEvent(tp.devFile, EvSyn, 0, SynReport)
if err != nil {
return fmt.Errorf("emitEvent: %v", err)
}
return nil
}
// RightPress emits right button release event
func (tp vTouchPad) RightRelease() error {
err := emitEvent(tp.devFile, EvKey, BtnRight, 0)
if err != nil {
return fmt.Errorf("emitEvent: %v", err)
}
err = emitEvent(tp.devFile, EvSyn, 0, SynReport)
if err != nil {
return fmt.Errorf("emitEvent: %v", err)
}
return nil
}
// RightClick emits right button click event
func (tp vTouchPad) RightClick() error {
err := tp.RightPress()
err = tp.RightRelease()
return err
}
func (tp vTouchPad) MoveTo(x int32, y int32) error {
err := emitAbsEvent(tp.devFile, x, y)
if err != nil {
return fmt.Errorf("emitEvent: %v", err)
}
return nil
}
func (tp vTouchPad) Close() error {
return destroyDevice(tp.devFile)
}