-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathuinput.go
160 lines (133 loc) · 4.21 KB
/
uinput.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
package evdev
import (
"bytes"
"encoding/binary"
"fmt"
"os"
"syscall"
)
const (
uinputMaxNameSize = 80
absSize = 64
)
// CreateDevice creates a device from scratch with the provided capabilities and name
// If set up fails the device will be removed from the system,
// once set up it can be removed by calling dev.Close
func CreateDevice(name string, id InputID, capabilities map[EvType][]EvCode) (*InputDevice, error) {
deviceFile, err := os.OpenFile("/dev/uinput", syscall.O_WRONLY|syscall.O_NONBLOCK, 0660)
if err != nil {
return nil, err
}
newDev := &InputDevice{
file: deviceFile,
}
for ev, codes := range capabilities {
if err := ioctlUISETEVBIT(newDev.file.Fd(), uintptr(ev)); err != nil {
DestroyDevice(newDev)
return nil, fmt.Errorf("failed to set ev bit: %d - %w", ev, err)
}
if err := setEventCodes(newDev, ev, codes); err != nil {
DestroyDevice(newDev)
return nil, fmt.Errorf("failed to set ev code: %w", err)
}
}
if _, err = createInputDevice(newDev.file, UinputUserDevice{
Name: toUinputName([]byte(name)),
ID: id,
}); err != nil {
DestroyDevice(newDev)
return nil, fmt.Errorf("failed to create device: %w", err)
}
return newDev, nil
}
// CloneDevice creates a new device from an existing one
// all capabilites will be coppied over to the new virtual device
// If set up fails the device will be removed from the system,
// once set up it can be removed by calling dev.Close
func CloneDevice(name string, dev *InputDevice) (*InputDevice, error) {
deviceFile, err := os.OpenFile("/dev/uinput", syscall.O_WRONLY|syscall.O_NONBLOCK, 0660)
if err != nil {
return nil, err
}
newDev := &InputDevice{
file: deviceFile,
driverVersion: dev.driverVersion,
}
for _, ev := range dev.CapableTypes() {
if err := ioctlUISETEVBIT(newDev.file.Fd(), uintptr(ev)); err != nil {
DestroyDevice(newDev)
return nil, fmt.Errorf("failed to set ev bit: %d - %w", ev, err)
}
eventCodes := dev.CapableEvents(ev)
if err := setEventCodes(newDev, ev, eventCodes); err != nil {
DestroyDevice(newDev)
return nil, fmt.Errorf("failed to set ev code: %w", err)
}
}
id, err := dev.InputID()
if err != nil {
DestroyDevice(newDev)
return nil, fmt.Errorf("failed to get original device id: %w", err)
}
if _, err = createInputDevice(newDev.file, UinputUserDevice{
Name: toUinputName([]byte(name)),
ID: id,
}); err != nil {
return nil, fmt.Errorf("failed to create device: %w", err)
}
return newDev, nil
}
// Destroy destroys an input device, removing it from the system
// This is designed to be called on self created virtual devices and may fail if called
// on real devices attached to the system
func DestroyDevice(dev *InputDevice) error {
return ioctlUIDEVDESTROY(dev.file.Fd())
}
func setEventCodes(dev *InputDevice, ev EvType, codes []EvCode) error {
for _, code := range codes {
var err error
switch ev {
case EV_ABS:
err = ioctlUISETABSBIT(dev.file.Fd(), uintptr(code))
case EV_FF:
err = ioctlUISETFFBIT(dev.file.Fd(), uintptr(code))
case EV_KEY:
err = ioctlUISETKEYBIT(dev.file.Fd(), uintptr(code))
case EV_LED:
err = ioctlUISETLEDBIT(dev.file.Fd(), uintptr(code))
case EV_MSC:
err = ioctlUISETMSCBIT(dev.file.Fd(), uintptr(code))
case EV_REL:
err = ioctlUISETRELBIT(dev.file.Fd(), uintptr(code))
case EV_SND:
err = ioctlUISETSNDBIT(dev.file.Fd(), uintptr(code))
case EV_SW:
err = ioctlUISETSWBIT(dev.file.Fd(), uintptr(code))
}
if err != nil {
return err
}
}
return nil
}
func toUinputName(name []byte) (uinputName [uinputMaxNameSize]byte) {
var fixedSizeName [uinputMaxNameSize]byte
copy(fixedSizeName[:], name)
return fixedSizeName
}
func createInputDevice(file *os.File, dev UinputUserDevice) (fd *os.File, err error) {
buf := new(bytes.Buffer)
if err = binary.Write(buf, binary.LittleEndian, dev); err != nil {
file.Close()
return nil, fmt.Errorf("failed to write user device buffer: %w", err)
}
if _, err = file.Write(buf.Bytes()); err != nil {
file.Close()
return nil, fmt.Errorf("failed to write uidev struct to device file: %w", err)
}
if err = ioctlUIDEVCREATE(file.Fd()); err != nil {
file.Close()
return nil, fmt.Errorf("failed to create device: %w", err)
}
return file, nil
}