-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibevdev_input.go
More file actions
302 lines (265 loc) · 7.14 KB
/
Copy pathlibevdev_input.go
File metadata and controls
302 lines (265 loc) · 7.14 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package golibevdev
// #cgo pkg-config: libevdev
// #include <string.h>
// #include <stdlib.h>
// #include <libevdev/libevdev.h>
// #include <libevdev/libevdev-uinput.h>
import "C"
import (
"fmt"
"unsafe"
)
type UInputDev struct {
cStruct *C.struct_libevdev_uinput
dev *InputDev
}
func NewVirtualKeyboard(name string) (uid *UInputDev, err error) {
dev := C.libevdev_new()
n := C.CString(name)
defer C.free(unsafe.Pointer(n))
C.libevdev_set_name(dev, n)
input := &InputDev{cStruct: dev}
defer func() {
if err != nil {
input.Close()
}
}()
if err = input.EnableEventType(EvKey); err != nil {
return
}
for k := KeyReserved; k <= KeyMax; k++ {
if err = input.EnableEventCode(EvKey, k); err != nil {
return
}
}
var uidev *C.struct_libevdev_uinput
rc := C.libevdev_uinput_create_from_device(dev, C.LIBEVDEV_UINPUT_OPEN_MANAGED, &uidev)
if rc != 0 {
return nil, fmt.Errorf("failed to create uinput device: %v", strError(rc))
}
return &UInputDev{
cStruct: uidev,
dev: input,
}, nil
}
func NewVirtualMouse(name string) (uid *UInputDev, err error) {
dev := C.libevdev_new()
n := C.CString(name)
defer C.free(unsafe.Pointer(n))
C.libevdev_set_name(dev, n)
input := &InputDev{cStruct: dev}
defer func() {
if err != nil {
input.Close()
}
}()
// Enable necessary event types for mouse
if err = input.EnableEventType(EvKey); err != nil {
return
}
if err = input.EnableEventType(EvRel); err != nil {
return
}
// Enable mouse buttons
mouseButtons := []EventCode{
BtnLeft,
BtnRight,
BtnMiddle,
}
for _, btn := range mouseButtons {
if err = input.EnableEventCode(EvKey, btn); err != nil {
return
}
}
// Enable relative axes
relAxes := []EventCode{
RelX,
RelY,
RelWheel,
}
for _, axis := range relAxes {
if err = input.EnableEventCode(EvRel, axis); err != nil {
return
}
}
var uidev *C.struct_libevdev_uinput
rc := C.libevdev_uinput_create_from_device(dev, C.LIBEVDEV_UINPUT_OPEN_MANAGED, &uidev)
if rc != 0 {
C.libevdev_free(dev)
return nil, fmt.Errorf("failed to create uinput device: %v", strError(rc))
}
return &UInputDev{
cStruct: uidev,
dev: input,
}, nil
}
func NewVirtualTouchpad(name string) (uid *UInputDev, err error) {
dev := C.libevdev_new()
n := C.CString(name)
defer C.free(unsafe.Pointer(n))
C.libevdev_set_name(dev, n)
input := &InputDev{cStruct: dev}
defer func() {
if err != nil {
input.Close()
}
}()
// Enable necessary event types for touchpad
if err = input.EnableEventType(EvKey); err != nil {
return
}
if err = input.EnableEventType(EvAbs); err != nil {
return
}
// Enable touchpad buttons
touchpadButtons := []EventCode{
BtnTouch,
BtnToolFinger,
BtnLeft,
BtnRight,
}
for _, btn := range touchpadButtons {
if err = input.EnableEventCode(EvKey, btn); err != nil {
return
}
}
// Enable absolute axes with proper ranges
absAxes := []struct {
code EventCode
min, max int32
resolution int32
}{
{AbsX, 0, 3000, 30},
{AbsY, 0, 2000, 30},
{AbsPressure, 0, 255, 0},
}
for _, axis := range absAxes {
if err = input.EnableEventCode(EvAbs, axis.code); err != nil {
return
}
C.libevdev_set_abs_minimum(dev, C.uint(axis.code.Value()), C.int(axis.min))
C.libevdev_set_abs_maximum(dev, C.uint(axis.code.Value()), C.int(axis.max))
if axis.resolution > 0 {
C.libevdev_set_abs_resolution(dev, C.uint(axis.code.Value()), C.int(axis.resolution))
}
}
var uidev *C.struct_libevdev_uinput
rc := C.libevdev_uinput_create_from_device(dev, C.LIBEVDEV_UINPUT_OPEN_MANAGED, &uidev)
if rc != 0 {
C.libevdev_free(dev)
return nil, fmt.Errorf("failed to create uinput device: %v", strError(rc))
}
return &UInputDev{
cStruct: uidev,
dev: input,
}, nil
}
func NewVirtualGamepad(name string) (uid *UInputDev, err error) {
dev := C.libevdev_new()
n := C.CString(name)
defer C.free(unsafe.Pointer(n))
C.libevdev_set_name(dev, n)
input := &InputDev{cStruct: dev}
defer func() {
if err != nil {
input.Close()
}
}()
// Enable necessary event types for gamepad
if err = input.EnableEventType(EvKey); err != nil {
return
}
if err = input.EnableEventType(EvAbs); err != nil {
return
}
// Enable gamepad buttons
gamepadButtons := []EventCode{
BtnA,
BtnB,
BtnX,
BtnY,
BtnTL,
BtnTR,
BtnSelect,
BtnStart,
}
for _, btn := range gamepadButtons {
if err = input.EnableEventCode(EvKey, btn); err != nil {
return
}
}
// Enable absolute axes with proper ranges for analog sticks
absAxes := []struct {
code EventCode
min, max int32
flat, fuzz int32
}{
{AbsX, -32768, 32767, 128, 16}, // Left stick X
{AbsY, -32768, 32767, 128, 16}, // Left stick Y
{AbsRx, -32768, 32767, 128, 16}, // Right stick X
{AbsRy, -32768, 32767, 128, 16}, // Right stick Y
{AbsZ, 0, 255, 0, 0}, // Left trigger
{AbsRz, 0, 255, 0, 0}, // Right trigger
}
for _, axis := range absAxes {
if err = input.EnableEventCode(EvAbs, axis.code); err != nil {
return
}
C.libevdev_set_abs_minimum(dev, C.uint(axis.code.Value()), C.int(axis.min))
C.libevdev_set_abs_maximum(dev, C.uint(axis.code.Value()), C.int(axis.max))
C.libevdev_set_abs_flat(dev, C.uint(axis.code.Value()), C.int(axis.flat))
C.libevdev_set_abs_fuzz(dev, C.uint(axis.code.Value()), C.int(axis.fuzz))
}
var uidev *C.struct_libevdev_uinput
rc := C.libevdev_uinput_create_from_device(dev, C.LIBEVDEV_UINPUT_OPEN_MANAGED, &uidev)
if rc != 0 {
C.libevdev_free(dev)
return nil, fmt.Errorf("failed to create uinput device: %v", strError(rc))
}
return &UInputDev{
cStruct: uidev,
dev: input,
}, nil
}
func (dev *UInputDev) Close() {
C.libevdev_uinput_destroy(dev.cStruct)
}
func (dev *UInputDev) WriteEvent(typ EventType, code EventCode, value int32) error {
rc := C.libevdev_uinput_write_event(dev.cStruct, C.uint(typ), C.uint(code.Value()), C.int(value))
if rc != 0 {
return fmt.Errorf("failed to write event: %v", strError(rc))
}
return nil
}
// Sync sends a sync event
func (dev *UInputDev) Sync() error {
return dev.WriteEvent(EvSyn, SynReport, 0)
}
// GetName returns the name of the device
func (dev *UInputDev) GetName() string {
return C.GoString(C.libevdev_get_name(dev.dev.cStruct))
}
// DeviceInfo contains basic information about an input device
type DeviceInfo struct {
Name string
ID struct{ Vendor, Product, Version, BusType uint16 }
DriverVersion int
}
// GetDeviceInfo returns information about the device
func (dev *UInputDev) GetDeviceInfo() DeviceInfo {
info := DeviceInfo{}
info.Name = C.GoString(C.libevdev_get_name(dev.dev.cStruct))
info.ID.Vendor = uint16(C.libevdev_get_id_vendor(dev.dev.cStruct))
info.ID.Product = uint16(C.libevdev_get_id_product(dev.dev.cStruct))
info.ID.Version = uint16(C.libevdev_get_id_version(dev.dev.cStruct))
info.ID.BusType = uint16(C.libevdev_get_id_bustype(dev.dev.cStruct))
info.DriverVersion = int(C.libevdev_get_driver_version(dev.dev.cStruct))
return info
}
// ReadEvent reads the next event from the device
func (dev *UInputDev) ReadEvent(flag ReadFlag) (Event, error) {
return dev.dev.NextEvent(flag)
}
func (dev *UInputDev) ReadEventWithStatus(flag ReadFlag) (Event, ReadStatus, error) {
return dev.dev.NextEventWithStatus(flag)
}