forked from bendahl/uinput
-
Notifications
You must be signed in to change notification settings - Fork 0
/
touchpad.go
216 lines (179 loc) · 6.17 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
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
io.Closer
}
type vTouchPad struct {
name []byte
deviceFile *os.File
}
// CreateTouchPad will create a new touch pad 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) {
validateDevicePath(path)
validateUinputName(name)
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, evBtnLeft, btnStatePressed)
if err != nil {
return fmt.Errorf("Failed to issue the LeftClick event: %v", err)
}
err = sendBtnEvent(vTouch.deviceFile, evBtnLeft, btnStateReleased)
if err != nil {
return fmt.Errorf("Failed to issue the KeyUp event: %v", err)
}
return syncEvents(vTouch.deviceFile)
}
func (vTouch vTouchPad) RightClick() error {
err := sendBtnEvent(vTouch.deviceFile, evBtnRight, btnStatePressed)
if err != nil {
return fmt.Errorf("Failed to issue the RightClick event: %v", err)
}
err = sendBtnEvent(vTouch.deviceFile, evBtnRight, btnStateReleased)
if err != nil {
return fmt.Errorf("Failed to issue the KeyUp event: %v", err)
}
return syncEvents(vTouch.deviceFile)
}
// 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 {
err := sendBtnEvent(vTouch.deviceFile, evBtnLeft, btnStatePressed)
if err != nil {
return fmt.Errorf("Failed press the left mouse button: %v", err)
}
return syncEvents(vTouch.deviceFile)
}
// LeftRelease will simulate the release of the left mouse button.
func (vTouch vTouchPad) LeftRelease() error {
err := sendBtnEvent(vTouch.deviceFile, evBtnLeft, btnStateReleased)
if err != nil {
return fmt.Errorf("Failed to release the left mouse button: %v", err)
}
return syncEvents(vTouch.deviceFile)
}
// 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 {
err := sendBtnEvent(vTouch.deviceFile, evBtnRight, btnStatePressed)
if err != nil {
return fmt.Errorf("Failed to press the right mouse button: %v", err)
}
return syncEvents(vTouch.deviceFile)
}
// RightRelease will simulate the release of the right mouse button.
func (vTouch vTouchPad) RightRelease() error {
err := sendBtnEvent(vTouch.deviceFile, evBtnRight, btnStateReleased)
if err != nil {
return fmt.Errorf("Failed to release the right mouse button: %v", err)
}
return syncEvents(vTouch.deviceFile)
}
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)
err = ioctl(deviceFile, uiSetKeyBit, uintptr(evBtnLeft))
if err != nil {
deviceFile.Close()
return nil, fmt.Errorf("failed to register left click event: %v", err)
}
err = ioctl(deviceFile, uiSetKeyBit, uintptr(evBtnRight))
if err != nil {
deviceFile.Close()
return nil, fmt.Errorf("failed to register right click event: %v", 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
err = ioctl(deviceFile, uiSetAbsBit, uintptr(absX))
if err != nil {
deviceFile.Close()
return nil, fmt.Errorf("failed to register absolute x axis events: %v", err)
}
err = ioctl(deviceFile, uiSetAbsBit, uintptr(absY))
if err != nil {
deviceFile.Close()
return nil, fmt.Errorf("failed to register absolute y axis events: %v", 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 {
var ev [2]inputEvent
ev[0].Type = evAbs
ev[0].Code = absX
ev[0].Value = xPos
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)
}