-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvirtual_device.go
More file actions
539 lines (456 loc) · 13.1 KB
/
virtual_device.go
File metadata and controls
539 lines (456 loc) · 13.1 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
package virtual_device
import (
"errors"
"fmt"
"github.com/jbdemonte/virtual-device/linux"
"github.com/jbdemonte/virtual-device/sdl"
"github.com/jbdemonte/virtual-device/utils"
"os"
"strings"
"syscall"
"time"
"unsafe"
)
// VirtualDevice represents a Linux uinput virtual input device.
type VirtualDevice interface {
WithPath(path string) VirtualDevice
WithMode(mode os.FileMode) VirtualDevice
WithQueueLen(queueLen int) VirtualDevice
WithBusType(busType linux.BusType) VirtualDevice
WithVendor(vendor sdl.Vendor) VirtualDevice
WithProduct(product sdl.Product) VirtualDevice
WithVersion(version uint16) VirtualDevice
WithName(name string) VirtualDevice
WithKeys(keys []linux.Key) VirtualDevice
WithButtons(buttons []linux.Button) VirtualDevice
WithAbsAxes(absoluteAxes []AbsAxis) VirtualDevice
WithRelAxes(relativeAxes []linux.RelativeAxis) VirtualDevice
WithRepeat(delay, period int32) VirtualDevice
WithLEDs(leds []linux.Led) VirtualDevice
WithProperties(properties []linux.InputProp) VirtualDevice
WithMiscEvents(events []linux.MiscEvent) VirtualDevice
Register() error
Unregister() error
Send(evType, code uint16, value int32)
Sync(evType linux.SyncEvent)
SyncReport()
PressKey(key linux.Key)
ReleaseKey(key linux.Key)
PressButton(key linux.Button)
ReleaseButton(key linux.Button)
SendAbsoluteEvent(axis linux.AbsoluteAxis, value int32)
SendRelativeEvent(axis linux.RelativeAxis, value int32)
SendMiscEvent(event linux.MiscEvent, value int32)
SetLed(led linux.Led, state bool)
EventPath() string
}
// NewVirtualDevice creates a new VirtualDevice with default settings.
func NewVirtualDevice() VirtualDevice {
return &virtualDevice{
path: "/dev/uinput",
mode: 0660,
queueLen: 1024,
isRegistered: &utils.AtomicBool{},
}
}
func (vd *virtualDevice) WithPath(path string) VirtualDevice {
vd.path = path
return vd
}
func (vd *virtualDevice) WithMode(mode os.FileMode) VirtualDevice {
vd.mode = mode
return vd
}
func (vd *virtualDevice) WithQueueLen(queueLen int) VirtualDevice {
vd.queueLen = queueLen
return vd
}
func (vd *virtualDevice) WithBusType(busType linux.BusType) VirtualDevice {
vd.id.BusType = busType
return vd
}
func (vd *virtualDevice) WithVendor(vendor uint16) VirtualDevice {
vd.id.Vendor = vendor
return vd
}
func (vd *virtualDevice) WithProduct(product uint16) VirtualDevice {
vd.id.Product = product
return vd
}
func (vd *virtualDevice) WithVersion(version uint16) VirtualDevice {
vd.id.Version = version
return vd
}
func (vd *virtualDevice) WithName(name string) VirtualDevice {
vd.name = name
return vd
}
func (vd *virtualDevice) WithKeys(keys []linux.Key) VirtualDevice {
vd.config.keys = keys
return vd
}
func (vd *virtualDevice) WithButtons(buttons []linux.Button) VirtualDevice {
vd.config.buttons = buttons
return vd
}
func (vd *virtualDevice) WithAbsAxes(absoluteAxes []AbsAxis) VirtualDevice {
vd.config.absoluteAxes = absoluteAxes
return vd
}
func (vd *virtualDevice) WithRelAxes(relativeAxes []linux.RelativeAxis) VirtualDevice {
vd.config.relativeAxes = relativeAxes
return vd
}
func (vd *virtualDevice) WithRepeat(delay, period int32) VirtualDevice {
vd.config.repeat = &Repeat{delay, period}
return vd
}
func (vd *virtualDevice) WithLEDs(leds []linux.Led) VirtualDevice {
vd.config.leds = leds
return vd
}
func (vd *virtualDevice) WithProperties(properties []linux.InputProp) VirtualDevice {
vd.config.properties = properties
return vd
}
func (vd *virtualDevice) WithMiscEvents(miscEvents []linux.MiscEvent) VirtualDevice {
vd.config.miscEvents = miscEvents
return vd
}
func (vd *virtualDevice) Register() error {
if vd.isRegistered.Get() {
return nil
}
fd, err := os.OpenFile(vd.path, syscall.O_WRONLY|syscall.O_NONBLOCK, vd.mode)
if err != nil {
return errors.New("could not open device file")
}
vd.fd = fd
steps := []func() error{
vd.registerKeys,
vd.registerAxes,
vd.registerProperties,
vd.registerMiscEvents,
vd.registerLeds,
vd.createDevice,
}
for _, step := range steps {
if err := step(); err != nil {
return vd.unregisterOnError(err)
}
}
vd.pull()
vd.isRegistered.Set(true)
return nil
}
func (vd *virtualDevice) fetchEventPath() (string, error) {
sysInputDir := "/sys/devices/virtual/input/"
path := make([]byte, 65) // 64 bytes + null byte
err := ioctl(vd.fd, linux.UI_GET_SYSNAME(64), uintptr(unsafe.Pointer(&path[0])))
if err != nil {
return "", fmt.Errorf("ioctl uiGetSysname failed: %v", err)
}
sysPath := sysInputDir + strings.TrimRight(string(path), "\x00")
files, err := os.ReadDir(sysPath)
if err != nil {
return "", fmt.Errorf("unable to read directory %s: %v", sysPath, err)
}
for _, file := range files {
if strings.HasPrefix(file.Name(), "event") {
return fmt.Sprintf("/dev/input/%s", file.Name()), nil
}
}
return "", fmt.Errorf("no event file found in %s", sysPath)
}
func (vd *virtualDevice) createDevice() (err error) {
var fixedSizeName [linux.UINPUT_MAX_NAME_SIZE]byte
copy(fixedSizeName[:], vd.name)
if len(vd.name) < len(fixedSizeName) {
fixedSizeName[len(vd.name)] = 0
}
var uinputDev linux.UInputUserDev
copy(uinputDev.Name[:], fixedSizeName[:])
uinputDev.ID = vd.id
setAbsResolution := false
for _, event := range vd.config.absoluteAxes {
uinputDev.AbsMin[event.Axis] = event.Min
uinputDev.AbsMax[event.Axis] = event.Max
uinputDev.AbsFlat[event.Axis] = event.Flat
uinputDev.AbsFuzz[event.Axis] = event.Fuzz
if event.Resolution > 0 {
setAbsResolution = true
}
}
_, err = vd.fd.Write((*[unsafe.Sizeof(uinputDev)]byte)(unsafe.Pointer(&uinputDev))[:])
if err != nil {
return fmt.Errorf("failed to write uidev struct to device file: %v", err)
}
err = ioctl(vd.fd, linux.UI_DEV_CREATE, uintptr(0))
if err != nil {
return fmt.Errorf("failed to create device: %v", err)
}
vd.eventPath, err = vd.fetchEventPath()
if err != nil {
return fmt.Errorf("fetchEventPath: %v", err)
}
if setAbsResolution {
err = utils.WaitForEventFile(vd.eventPath, 2*time.Second)
if err != nil {
return fmt.Errorf("WaitForEventFile (needed for setAbsResolution): %v", err)
}
err = vd.setAbsResolution()
if err != nil {
return fmt.Errorf("setAbsResolution: %v", err)
}
}
return nil
}
func (vd *virtualDevice) setAbsResolution() error {
eventFile, err := os.Open(vd.eventPath)
if err != nil {
return fmt.Errorf("failed to open event file %s: %v", vd.eventPath, err)
}
defer eventFile.Close()
for _, event := range vd.config.absoluteAxes {
if event.Resolution > 0 {
absInfo := linux.InputAbsInfo{
Value: event.Value,
Minimum: event.Min,
Maximum: event.Max,
Fuzz: event.Fuzz,
Flat: event.Flat,
Resolution: event.Resolution,
}
err = ioctl(eventFile, linux.EVIOCSABS(event.Axis), uintptr(unsafe.Pointer(&absInfo)))
if err != nil {
return fmt.Errorf("failed to set EVIOCSABS(0x%x), InputAbsInfo: %v", event.Axis, err)
}
}
}
return nil
}
func (vd *virtualDevice) registerKeys() error {
if (vd.config.keys == nil || len(vd.config.keys) == 0) && (vd.config.buttons == nil || len(vd.config.buttons) == 0) {
return nil
}
err := ioctl(vd.fd, linux.UI_SET_EVBIT, uintptr(linux.EV_KEY))
if err != nil {
return fmt.Errorf("failed to set UI_SET_EVBIT, EV_KEY: %v", err)
}
if vd.config.keys != nil {
for _, key := range vd.config.keys {
err = ioctl(vd.fd, linux.UI_SET_KEYBIT, uintptr(key))
if err != nil {
return fmt.Errorf("failed to register key 0x%x: %v", key, err)
}
}
}
if vd.config.buttons != nil {
for _, button := range vd.config.buttons {
err = ioctl(vd.fd, linux.UI_SET_KEYBIT, uintptr(button))
if err != nil {
return fmt.Errorf("failed to register button 0x%x: %v", button, err)
}
}
}
if vd.config.repeat != nil {
err := ioctl(vd.fd, linux.UI_SET_EVBIT, uintptr(linux.EV_REP))
if err != nil {
return fmt.Errorf("failed to set UI_SET_EVBIT, EV_REP: %v", err)
}
}
return nil
}
func (vd *virtualDevice) registerAxes() error {
if vd.config.absoluteAxes != nil {
err := ioctl(vd.fd, linux.UI_SET_EVBIT, uintptr(linux.EV_ABS))
if err != nil {
return fmt.Errorf("failed to set UI_SET_EVBIT, EV_ABS: %v", err)
}
for _, event := range vd.config.absoluteAxes {
err = ioctl(vd.fd, linux.UI_SET_ABSBIT, uintptr(event.Axis))
if err != nil {
return fmt.Errorf("failed to register absolute axis 0x%x: %v", event.Axis, err)
}
}
}
if vd.config.relativeAxes != nil {
err := ioctl(vd.fd, linux.UI_SET_EVBIT, uintptr(linux.EV_REL))
if err != nil {
return fmt.Errorf("failed to set UI_SET_EVBIT, EV_REL: %v", err)
}
for _, axis := range vd.config.relativeAxes {
err = ioctl(vd.fd, linux.UI_SET_RELBIT, uintptr(axis))
if err != nil {
return fmt.Errorf("failed to register relative axis 0x%x: %v", axis, err)
}
}
}
return nil
}
func (vd *virtualDevice) registerProperties() error {
if len(vd.config.properties) == 0 {
return nil
}
for _, prop := range vd.config.properties {
err := ioctl(vd.fd, linux.UI_SET_PROPBIT, uintptr(prop))
if err != nil {
return fmt.Errorf("failed to set UI_SET_PROPBIT, 0x%x: %v", prop, err)
}
}
return nil
}
func (vd *virtualDevice) registerMiscEvents() error {
if len(vd.config.miscEvents) == 0 {
return nil
}
err := ioctl(vd.fd, linux.UI_SET_EVBIT, uintptr(linux.EV_MSC))
if err != nil {
return fmt.Errorf("failed to set UI_SET_EVBIT, EV_MSC: %v", err)
}
for _, event := range vd.config.miscEvents {
err := ioctl(vd.fd, linux.UI_SET_MSCBIT, uintptr(event))
if err != nil {
return fmt.Errorf("failed to set UI_SET_MSCBIT, 0x%x: %v", event, err)
}
}
return nil
}
func (vd *virtualDevice) registerLeds() error {
if len(vd.config.leds) == 0 {
return nil
}
err := ioctl(vd.fd, linux.UI_SET_EVBIT, uintptr(linux.EV_LED))
if err != nil {
return fmt.Errorf("failed to set UI_SET_EVBIT, EV_LED: %v", err)
}
for _, led := range vd.config.leds {
err := ioctl(vd.fd, linux.UI_SET_LEDBIT, uintptr(led))
if err != nil {
return fmt.Errorf("failed to set UI_SET_LEDBIT, 0x%x: %v", led, err)
}
}
return nil
}
func (vd *virtualDevice) pull() {
vd.queue = make(chan *linux.InputEvent, vd.queueLen)
vd.pullDone = make(chan struct{})
go func() {
defer close(vd.pullDone)
for event := range vd.queue {
err := vd.writeEvent(event)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to write event: %v\n", err)
}
}
}()
if vd.config.repeat != nil {
vd.Send(uint16(linux.EV_REP), uint16(linux.REP_DELAY), vd.config.repeat.delay)
vd.Send(uint16(linux.EV_REP), uint16(linux.REP_PERIOD), vd.config.repeat.period)
vd.SyncReport()
}
}
func (vd *virtualDevice) writeEvent(event *linux.InputEvent) error {
buf := (*[unsafe.Sizeof(*event)]byte)(unsafe.Pointer(event))[:]
n, err := vd.fd.Write(buf)
if err != nil {
return err
}
if n < linux.SizeofEvent {
fmt.Fprintf(os.Stderr, "poll outbox: short write\n")
}
return nil
}
func (vd *virtualDevice) unregisterOnError(err error) error {
uErr := vd.Unregister()
return concatErrors(err, uErr)
}
func (vd *virtualDevice) closeQueue() {
if vd.queue == nil {
return
}
// wait for the queue to be flushed
for len(vd.queue) > 0 {
time.Sleep(time.Millisecond)
}
close(vd.queue)
<-vd.pullDone
vd.queue = nil
}
func (vd *virtualDevice) releaseDevice() error {
if vd.fd == nil {
return nil
}
err := ioctl(vd.fd, linux.UI_DEV_DESTROY, 0)
if err != nil {
err = fmt.Errorf("failed to unregister the device: %v", err)
}
return err
}
func (vd *virtualDevice) closeDevice() error {
if vd.fd == nil {
return nil
}
err := vd.fd.Close()
if err != nil {
err = fmt.Errorf("failed to close the device: %v", err)
}
vd.fd = nil
return err
}
func (vd *virtualDevice) Unregister() (err error) {
vd.isRegistered.Set(false)
vd.closeQueue()
return concatErrors(
vd.releaseDevice(),
vd.closeDevice(),
)
}
// Send an event to the device.
func (vd *virtualDevice) Send(evType, code uint16, value int32) {
if !vd.isRegistered.Get() {
return
}
vd.queue <- &linux.InputEvent{
Type: evType,
Code: code,
Value: value,
}
}
func (vd *virtualDevice) Sync(evType linux.SyncEvent) {
vd.Send(uint16(linux.EV_SYN), uint16(evType), 0)
}
func (vd *virtualDevice) SyncReport() {
vd.Sync(linux.SYN_REPORT)
}
func (vd *virtualDevice) PressKey(key linux.Key) {
vd.Send(uint16(linux.EV_KEY), uint16(key), 1)
}
func (vd *virtualDevice) ReleaseKey(key linux.Key) {
vd.Send(uint16(linux.EV_KEY), uint16(key), 0)
}
func (vd *virtualDevice) PressButton(button linux.Button) {
vd.Send(uint16(linux.EV_KEY), uint16(button), 1)
}
func (vd *virtualDevice) ReleaseButton(button linux.Button) {
vd.Send(uint16(linux.EV_KEY), uint16(button), 0)
}
func (vd *virtualDevice) SendAbsoluteEvent(axis linux.AbsoluteAxis, value int32) {
vd.Send(uint16(linux.EV_ABS), uint16(axis), value)
}
func (vd *virtualDevice) SendRelativeEvent(axis linux.RelativeAxis, value int32) {
vd.Send(uint16(linux.EV_REL), uint16(axis), value)
}
func (vd *virtualDevice) SendMiscEvent(event linux.MiscEvent, value int32) {
vd.Send(uint16(linux.EV_MSC), uint16(event), value)
}
func (vd *virtualDevice) EventPath() string {
return vd.eventPath
}
func (vd *virtualDevice) SetLed(led linux.Led, state bool) {
value := int32(0)
if state {
value = 1
}
vd.Send(uint16(linux.EV_LED), uint16(led), value)
}