This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
touchpad.go
219 lines (177 loc) · 6.69 KB
/
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
213
214
215
216
217
218
219
package uinput
import (
"fmt"
"io"
"os"
)
// A TouchPad is an input device that uses absolute axis events, meaning that you can specify
// the exact position the cursor should move to. Therefore, it is necessary to define the size
// of the rectangle in which the cursor may move upon creation of the device.
type TouchPad interface {
// MoveTo will move the cursor to the specified position on the screen
MoveTo(x int32, y int32) error
// LeftClick will issue a single left click.
LeftClick() error
// RightClick will issue a right click.
RightClick() error
// LeftPress will simulate a press of the left mouse button. Note that the button will not be released until
// LeftRelease is invoked.
LeftPress() error
// LeftRelease will simulate the release of the left mouse button.
LeftRelease() error
// RightPress will simulate the press of the right mouse button. Note that the button will not be released until
// RightRelease is invoked.
RightPress() error
// RightRelease will simulate the release of the right mouse button.
RightRelease() error
// TouchDown will simulate a single touch to a virtual touch device. Use TouchUp to end the touch gesture.
TouchDown() error
// TouchUp will end or ,more precisely, unset the touch event issued by TouchDown
TouchUp() error
// FetchSyspath will return the syspath to the device file.
FetchSyspath() (string, error)
io.Closer
}
type vTouchPad struct {
name []byte
deviceFile *os.File
}
// CreateTouchPad will create a new touchpad device. note that you will need to define the x and y-axis boundaries
// (min and max) within which the cursor maybe moved around.
func CreateTouchPad(path string, name []byte, minX int32, maxX int32, minY int32, maxY int32) (TouchPad, error) {
err := validateDevicePath(path)
if err != nil {
return nil, err
}
err = validateUinputName(name)
if err != nil {
return nil, err
}
fd, err := createTouchPad(path, name, minX, maxX, minY, maxY)
if err != nil {
return nil, err
}
return vTouchPad{name: name, deviceFile: fd}, nil
}
func (vTouch vTouchPad) MoveTo(x int32, y int32) error {
return sendAbsEvent(vTouch.deviceFile, x, y)
}
func (vTouch vTouchPad) LeftClick() error {
err := sendBtnEvent(vTouch.deviceFile, []int{evMouseBtnLeft}, btnStatePressed)
if err != nil {
return fmt.Errorf("failed to issue the LeftClick event: %v", err)
}
return sendBtnEvent(vTouch.deviceFile, []int{evMouseBtnLeft}, btnStateReleased)
}
func (vTouch vTouchPad) RightClick() error {
err := sendBtnEvent(vTouch.deviceFile, []int{evMouseBtnRight}, btnStatePressed)
if err != nil {
return fmt.Errorf("failed to issue the RightClick event: %v", err)
}
return sendBtnEvent(vTouch.deviceFile, []int{evMouseBtnRight}, btnStateReleased)
}
// LeftPress will simulate a press of the left mouse button. Note that the button will not be released until
// LeftRelease is invoked.
func (vTouch vTouchPad) LeftPress() error {
return sendBtnEvent(vTouch.deviceFile, []int{evMouseBtnLeft}, btnStatePressed)
}
// LeftRelease will simulate the release of the left mouse button.
func (vTouch vTouchPad) LeftRelease() error {
return sendBtnEvent(vTouch.deviceFile, []int{evMouseBtnLeft}, btnStateReleased)
}
// RightPress will simulate the press of the right mouse button. Note that the button will not be released until
// RightRelease is invoked.
func (vTouch vTouchPad) RightPress() error {
return sendBtnEvent(vTouch.deviceFile, []int{evMouseBtnRight}, btnStatePressed)
}
// RightRelease will simulate the release of the right mouse button.
func (vTouch vTouchPad) RightRelease() error {
return sendBtnEvent(vTouch.deviceFile, []int{evMouseBtnRight}, btnStateReleased)
}
func (vTouch vTouchPad) TouchDown() error {
return sendBtnEvent(vTouch.deviceFile, []int{evBtnTouch}, btnStatePressed)
}
func (vTouch vTouchPad) TouchUp() error {
return sendBtnEvent(vTouch.deviceFile, []int{evBtnTouch}, btnStateReleased)
}
func (vTouch vTouchPad) Close() error {
return closeDevice(vTouch.deviceFile)
}
func createTouchPad(path string, name []byte, minX int32, maxX int32, minY int32, maxY int32) (fd *os.File, err error) {
deviceFile, err := createDeviceFile(path)
if err != nil {
return nil, fmt.Errorf("could not create absolute axis input device: %v", err)
}
err = registerDevice(deviceFile, uintptr(evKey))
if err != nil {
_ = deviceFile.Close()
return nil, fmt.Errorf("failed to register key device: %v", err)
}
// register button events (in order to enable left and right click)
for _, event := range []int{evMouseBtnLeft, evMouseBtnRight, evBtnTouch} {
err = ioctl(deviceFile, uiSetKeyBit, uintptr(event))
if err != nil {
_ = deviceFile.Close()
return nil, fmt.Errorf("failed to register button event %v: %v", event, err)
}
}
err = registerDevice(deviceFile, uintptr(evAbs))
if err != nil {
_ = deviceFile.Close()
return nil, fmt.Errorf("failed to register absolute axis input device: %v", err)
}
// register x and y-axis events
for _, event := range []int{absX, absY} {
err = ioctl(deviceFile, uiSetAbsBit, uintptr(event))
if err != nil {
_ = deviceFile.Close()
return nil, fmt.Errorf("failed to register absolute axis event %v: %v", event, err)
}
}
var absMin [absSize]int32
absMin[absX] = minX
absMin[absY] = minY
var absMax [absSize]int32
absMax[absX] = maxX
absMax[absY] = maxY
return createUsbDevice(deviceFile,
uinputUserDev{
Name: toUinputName(name),
ID: inputID{
Bustype: busUsb,
Vendor: 0x4711,
Product: 0x0817,
Version: 1},
Absmin: absMin,
Absmax: absMax})
}
func sendAbsEvent(deviceFile *os.File, xPos int32, yPos int32) error { // TODO: Perhaps move this to a more generic function? This conflicts with the gamepad ABS events which only have one value.
var ev [2]inputEvent
ev[0].Type = evAbs
ev[0].Code = absX
ev[0].Value = xPos
// Various tests (using evtest) have shown that positioning on x=0;y=0 doesn't trigger any event and will not move
// the cursor as expected. Setting at least one of the coordinates to -1 will however have the desired effect of
// moving the cursor to the upper left corner. Interestingly, the same is true for equivalent code in C, which rules
// out issues related to Go's data type representation or the like. This will need to be investigated further...
if xPos == 0 && yPos == 0 {
yPos--
}
ev[1].Type = evAbs
ev[1].Code = absY
ev[1].Value = yPos
for _, iev := range ev {
buf, err := inputEventToBuffer(iev)
if err != nil {
return fmt.Errorf("writing abs event failed: %v", err)
}
_, err = deviceFile.Write(buf)
if err != nil {
return fmt.Errorf("failed to write abs event to device file: %v", err)
}
}
return syncEvents(deviceFile)
}
func (vTouch vTouchPad) FetchSyspath() (string, error) {
return fetchSyspath(vTouch.deviceFile)
}