-
Notifications
You must be signed in to change notification settings - Fork 0
/
ioctls.go
192 lines (174 loc) · 3.7 KB
/
ioctls.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
package uinput
import (
"os"
"syscall"
)
// Original function taken from: https://github.com/tianon/debian-golang-pty/blob/master/ioctl.go
func ioctl(deviceFD *os.File, cmd, ptr uintptr) error {
if _, _, errorCode := syscall.Syscall(syscall.SYS_IOCTL, deviceFD.Fd(), cmd, ptr); errorCode != 0 {
return errorCode
}
return nil
}
const (
uinputPath = "/dev/uinput"
vinputPath = "/sys/devices/virtual/input"
)
type iocDir uint
const (
iocNone iocDir = 0
iocWrite = 1
iocRead = 2
)
const (
iocNrBits = 8
iocTypeBits = 8
// TODO: On PowerPC, SPARC, MIPS and Alpha it is defined as a 13-bit constant.
// In the rest, including Intel and ARM it is defined as a 14-bit constant.
// See https://elixir.bootlin.com/linux/latest/ident/_IOC_SIZEBITS
iocSizeBits = 14
iocDirBits = 2
iocNrMask = (1 << iocNrBits) - 1
iocTypeMask = (1 << iocTypeBits) - 1
iocSizeMask = (1 << iocSizeBits) - 1
iocDirMask = (1 << iocDirBits) - 1
iocNrShift = 0
iocTypeShift = iocNrShift + iocNrBits
iocSizeShift = iocTypeShift + iocTypeBits
iocDirShift = iocSizeShift + iocSizeBits
)
const (
UINPUT_IOCTL_BASE = "U"
UINPUT_VERSION = 5
UINPUT_MAX_NAME_SIZE = 80
)
// Aliasing to Go style standards
const (
ioctlBase = UINPUT_IOCTL_BASE
uinputVersion = UINPUT_VERSION
maxDeviceNameLength = UINPUT_MAX_NAME_SIZE
)
// REF:
// https://github.com/torvalds/linux/blob/master/include/uapi/linux/uinput.h
type ioctlType int
const (
UI_DEV_CREATE ioctlType = iota
UI_DEV_DESTROY
UI_DEV_SETUP
UI_ABS_SETUP
UI_SET_EVBIT
UI_SET_KEYBIT
UI_SET_RELBIT
UI_SET_ABSBIT
UI_SET_MSCBIT
UI_SET_LEDBIT
UI_SET_SNDBIT
UI_SET_FFBIT
UI_SET_PHYS
UI_SET_SWBIT
UI_SET_PROPBIT
UI_BEGIN_FF_UPLOAD
UI_END_FF_UPLOAD
UI_BEGIN_FF_ERASE
UI_END_FF_ERASE
UI_GET_SYSNAME
UI_GET_VERSION
)
// Aliasing to Go style standards for a more intuitive API
const (
NewDevice = UI_DEV_CREATE
CreateDevice = UI_DEV_CREATE
RemoveDevice = UI_DEV_DESTROY
DestroyDevice = UI_DEV_DESTROY
RegisterKey = UI_SET_KEYBIT
KeyBit = UI_SET_KEYBIT
Event = UI_SET_EVBIT
EventBit = UI_SET_EVBIT
RelativeMovement = UI_SET_RELBIT
RelativeBit = UI_SET_RELBIT
AbsoluteMovement = UI_SET_ABSBIT
AbsoluteBit = UI_SET_ABSBIT
)
func (self ioctlType) ID() int {
switch self {
case UI_DEV_CREATE:
return 1
case UI_DEV_DESTROY:
return 2
case UI_DEV_SETUP:
return 3
case UI_ABS_SETUP:
return 4
case UI_SET_EVBIT:
return 100
case UI_SET_KEYBIT:
return 101
case UI_SET_RELBIT:
return 102
case UI_SET_ABSBIT:
return 103
case UI_SET_MSCBIT:
return 104
case UI_SET_LEDBIT:
return 105
case UI_SET_SNDBIT:
return 106
case UI_SET_FFBIT:
return 107
case UI_SET_PHYS:
return 108
case UI_SET_SWBIT:
return 109
case UI_SET_PROPBIT:
return 110
case UI_BEGIN_FF_UPLOAD:
return 200
case UI_END_FF_UPLOAD:
return 201
case UI_BEGIN_FF_ERASE:
return 202
case UI_END_FF_ERASE:
return 203
default:
return 0
}
}
func (self ioctlType) UIntPointer() uintptr {
switch self {
case UI_DEV_CREATE:
return 0x5501
case UI_DEV_DESTROY:
return 0x5502
case UI_DEV_SETUP:
return 0x405c5503
case UI_ABS_SETUP:
return 0x401c5504
case UI_SET_EVBIT:
return 0x40045564
case UI_SET_KEYBIT:
return 0x40045565
case UI_SET_RELBIT:
return 0x40045566
case UI_SET_ABSBIT:
return 0x40045567
case UI_SET_MSCBIT:
return 0x40045568
case UI_SET_LEDBIT:
return 0x40045569
case UI_SET_SNDBIT:
return 0x4004556a
case UI_SET_FFBIT:
return 0x4004556b
case UI_SET_PHYS:
return 0x4004556c
case UI_SET_SWBIT:
return 0x4004556d
case UI_SET_PROPBIT:
return 0x4004556e
default:
return 0
}
}
func (self ioctlType) Code() uintptr {
return self.UIntPointer()
}