-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethods.go
499 lines (449 loc) · 13.1 KB
/
methods.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
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
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Stany MARCEL <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package uinput
import (
"C"
"fmt"
"log"
"os"
"syscall"
"unsafe"
)
const (
// UI_SET_EVBIT ioctl code from uintput.h _IOW(UINPUT_IOCTL_BASE, 100, int)
UI_SET_EVBIT = (((_IOC_WRITE) << _IOC_DIRSHIFT) |
((UINPUT_IOCTL_BASE) << _IOC_TYPESHIFT) |
((100) << _IOC_NRSHIFT) |
((C.sizeof_int) << _IOC_SIZESHIFT))
// UI_SET_KEYBIT ioctl code from uintput.h _IOW(UINPUT_IOCTL_BASE, 101, int)
UI_SET_KEYBIT = (((_IOC_WRITE) << _IOC_DIRSHIFT) |
((UINPUT_IOCTL_BASE) << _IOC_TYPESHIFT) |
((101) << _IOC_NRSHIFT) |
((C.sizeof_int) << _IOC_SIZESHIFT))
// UI_SET_RELBIT ioctl code from uintput.h _IOW(UINPUT_IOCTL_BASE, 102, int)
UI_SET_RELBIT = (((_IOC_WRITE) << _IOC_DIRSHIFT) |
((UINPUT_IOCTL_BASE) << _IOC_TYPESHIFT) |
((102) << _IOC_NRSHIFT) |
((C.sizeof_int) << _IOC_SIZESHIFT))
// UI_SET_ABSBIT ioctl code from uintput.h _IOW(UINPUT_IOCTL_BASE, 103, int)
UI_SET_ABSBIT = (((_IOC_WRITE) << _IOC_DIRSHIFT) |
((UINPUT_IOCTL_BASE) << _IOC_TYPESHIFT) |
((103) << _IOC_NRSHIFT) |
((C.sizeof_int) << _IOC_SIZESHIFT))
)
// WriteDeviceInterface define the minimal function to be implemented by a output device
type WriteDeviceInterface interface {
Open() error
Close()
Write([]byte) (int, error)
IOCtl(uintptr, interface{}) error
}
// WriteDevice is the actual WriteDeviceInterface
type WriteDevice struct {
fd *os.File
}
// UInput struct used to store the device interface and
// a map used as a sef of pressed keys
type UInput struct {
wd WriteDeviceInterface
keyPressedMap map[EventCode]bool
}
// EventCode is used for all UInput Events
type EventCode uint16
// EventValue is used for all UInput Events Value
type EventValue int32
// AxisSetup is the struct that must be used to initialize an ABS Event
type AxisSetup struct {
Code EventCode
Min, Max, Fuzz, Flat EventValue
}
// Open implements WriteDeviceInterface Open for WriteDevice
// It opens the uinput device for writing
func (wd *WriteDevice) Open() error {
// open uinput device
var err error
if wd.fd == nil {
if wd.fd, err = os.OpenFile("/dev/uinput", os.O_WRONLY, 0); err != nil {
log.Fatal(err)
}
}
return err
}
// Close implements WriteDeviceInterface Close for WriteDevice
// It Closes the device if opened and sends an UI_DEV_DESTROY ioctl
func (wd *WriteDevice) Close() {
if wd.fd != nil {
wd.IOCtl(UI_DEV_DESTROY, 0)
wd.fd.Close()
}
}
// Write implements WriteDeviceInterface Write for WriteDevice
// It writes len(b) bytes to the Device.
// It returns the number of bytes written and an error, if any.
// Write returns a non-nil error when n != len(b).
func (wd *WriteDevice) Write(b []byte) (int, error) {
return wd.fd.Write(b)
}
// IOCtl implements WriteDeviceInterface IOCtl for WriteDevice
func (wd *WriteDevice) IOCtl(request uintptr, data interface{}) error {
var arg uintptr
switch dt := data.(type) {
case uintptr:
arg = dt
case EventCode:
arg = uintptr(dt)
case int:
arg = uintptr(dt)
default:
return fmt.Errorf("ioctl: Unable to convert: %T", data)
}
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, wd.fd.Fd(), request, arg); err != 0 {
return fmt.Errorf("ioctl: %s", err.Error())
}
return nil
}
// Init initializes the uinput device.
// It opens it if not already opened
// Init returns a non-nil error if not correctly initialized
func (ui *UInput) Init(
wd WriteDeviceInterface,
name string,
vendor, product, version uint16,
keys, rels []EventCode,
axes []AxisSetup,
keyboard bool) error {
ui.wd = wd
var uidev UInputUserDev
// copy Name
for i, v := range name {
if i >= len(uidev.Name)-1 {
break
}
uidev.Name[i] = byte(v)
}
uidev.Id.Bustype = BUS_USB
uidev.Id.Vendor = vendor
uidev.Id.Product = product
uidev.Id.Version = version
if err := ui.setupKeys(keys); err != nil {
return err
}
if err := ui.setupAxes(axes, &uidev); err != nil {
return err
}
if err := ui.setupRels(rels); err != nil {
return err
}
if keyboard {
if err := ui.setupKeyboard(); err != nil {
return err
}
}
ref, _ := uidev.PassRef()
defer uidev.Free()
// Write
ui.wd.Write((*[sizeOfStructUinputUserDevValue]byte)(unsafe.Pointer(ref))[:])
if err := ui.wd.IOCtl(UI_DEV_CREATE, 0); err != nil {
return err
}
return nil
}
func (ui *UInput) setupKeys(keys []EventCode) error {
if len(keys) > 0 {
if err := ui.wd.IOCtl(UI_SET_EVBIT, EV_KEY); err != nil {
return err
}
}
for _, key := range keys {
if err := ui.wd.IOCtl(UI_SET_KEYBIT, key); err != nil {
return err
}
}
return nil
}
func (ui *UInput) setupRels(rels []EventCode) error {
if len(rels) > 0 {
if err := ui.wd.IOCtl(UI_SET_EVBIT, EV_REL); err != nil {
return err
}
}
for _, rel := range rels {
if err := ui.wd.IOCtl(UI_SET_RELBIT, rel); err != nil {
return err
}
}
return nil
}
func (ui *UInput) setupAxes(axes []AxisSetup, uidev *UInputUserDev) error {
if len(axes) > 0 {
if err := ui.wd.IOCtl(UI_SET_EVBIT, EV_ABS); err != nil {
return err
}
}
for _, axis := range axes {
if err := ui.wd.IOCtl(UI_SET_ABSBIT, axis.Code); err != nil {
return err
}
uidev.Absmin[axis.Code] = int32(axis.Min)
uidev.Absmax[axis.Code] = int32(axis.Max)
uidev.Absfuzz[axis.Code] = int32(axis.Fuzz)
uidev.Absflat[axis.Code] = int32(axis.Flat)
}
return nil
}
func (ui *UInput) setupKeyboard() error {
if err := ui.wd.IOCtl(UI_SET_EVBIT, EV_MSC); err != nil {
return err
}
if err := ui.wd.IOCtl(UI_SET_EVBIT, MSC_SCAN); err != nil {
return err
}
if err := ui.wd.IOCtl(UI_SET_EVBIT, EV_REP); err != nil {
return err
}
return nil
}
func (ui *UInput) genEvent(eventType uint16, code EventCode, value EventValue) error {
ev := InputEvent{
Type: eventType,
Code: uint16(code),
Value: int32(value),
}
ref, _ := ev.PassRef()
defer ev.Free()
_, err := ui.wd.Write((*[sizeOfStructInputEventValue]byte)(unsafe.Pointer(ref))[:])
return err
}
// KeyEvent generates a code key event with value
// KEyEvent returns a non-nil error if not correctly generated
func (ui *UInput) KeyEvent(code EventCode, value EventValue) error {
return ui.genEvent(EV_KEY, code, value)
}
// AbsEvent generates a code axis event with value
// AbsEvent returns a non-nil error if not correctly generated
func (ui *UInput) AbsEvent(code EventCode, value EventValue) error {
return ui.genEvent(EV_ABS, code, value)
}
// RelEvent generates a code rel event with value
// RelEvent returns a non-nil error if not correctly generated
func (ui *UInput) RelEvent(code EventCode, value EventValue) error {
return ui.genEvent(EV_REL, code, value)
}
// ScanEvent generates a MSC_SCAN EV_MSC event with value
// ScanEvent returns a non-nil error if not correctly generated
func (ui *UInput) ScanEvent(value EventValue) error {
return ui.genEvent(EV_MSC, MSC_SCAN, value)
}
// SynEvent generates a SYN_REPORT EV_SYN event with value
// SynEvent returns a non-nil error if not correctly generated
func (ui *UInput) SynEvent() error {
return ui.genEvent(EV_SYN, SYN_REPORT, 0)
}
// SetDelayPeriod sets the EV_REP REP_DELAY and REP_PERIODS
// SetDelayPeriod returns a non-nil error if not correctly set
func (ui *UInput) SetDelayPeriod(delay, period EventValue) error {
if err := ui.genEvent(EV_REP, REP_DELAY, delay); err != nil {
return err
}
if err := ui.genEvent(EV_REP, REP_PERIOD, period); err != nil {
return err
}
return nil
}
// scans map keys EventCodes to ScanCodes as recorded from a logitech keyboard
var scans = map[EventCode]EventValue{
KEY_ESC: 0x70029,
KEY_F1: 0x7003a,
KEY_F2: 0x7003b,
KEY_F3: 0x7003c,
KEY_F4: 0x7003d,
KEY_F5: 0x7003e,
KEY_F6: 0x7003f,
KEY_F7: 0x70040,
KEY_F8: 0x70041,
KEY_F9: 0x70042,
KEY_F10: 0x70043,
KEY_F11: 0x70044,
KEY_F12: 0x70045,
KEY_SYSRQ: 0x70046,
KEY_SCROLLLOCK: 0x70047,
KEY_PAUSE: 0x70048,
KEY_GRAVE: 0x70035,
KEY_1: 0x7001e,
KEY_2: 0x7001f,
KEY_3: 0x70020,
KEY_4: 0x70021,
KEY_5: 0x70022,
KEY_6: 0x70023,
KEY_7: 0x70024,
KEY_8: 0x70025,
KEY_9: 0x70026,
KEY_0: 0x70027,
KEY_MINUS: 0x7002d,
KEY_EQUAL: 0x7002e,
KEY_BACKSPACE: 0x7002a,
KEY_TAB: 0x7002b,
KEY_Q: 0x70014,
KEY_W: 0x7001a,
KEY_E: 0x70008,
KEY_R: 0x70015,
KEY_T: 0x70017,
KEY_Y: 0x7001c,
KEY_U: 0x70018,
KEY_I: 0x7000c,
KEY_O: 0x70012,
KEY_P: 0x70013,
KEY_LEFTBRACE: 0x7002f,
KEY_RIGHTBRACE: 0x70030,
KEY_ENTER: 0x70028,
KEY_CAPSLOCK: 0x70039,
KEY_A: 0x70004,
KEY_S: 0x70016,
KEY_D: 0x70007,
KEY_F: 0x70009,
KEY_G: 0x7000a,
KEY_H: 0x7000b,
KEY_J: 0x7000d,
KEY_K: 0x7000e,
KEY_L: 0x7000f,
KEY_SEMICOLON: 0x70033,
KEY_APOSTROPHE: 0x70034,
KEY_BACKSLASH: 0x70032,
KEY_LEFTSHIFT: 0x700e1,
KEY_102ND: 0x70064,
KEY_Z: 0x7001d,
KEY_X: 0x7001b,
KEY_C: 0x70006,
KEY_V: 0x70019,
KEY_B: 0x70005,
KEY_N: 0x70011,
KEY_M: 0x70010,
KEY_COMMA: 0x70036,
KEY_DOT: 0x70037,
KEY_SLASH: 0x70038,
KEY_RIGHTSHIFT: 0x700e5,
KEY_LEFTCTRL: 0x700e0,
KEY_LEFTMETA: 0x700e3,
KEY_LEFTALT: 0x700e2,
KEY_SPACE: 0x7002c,
KEY_RIGHTALT: 0x700e6,
KEY_RIGHTMETA: 0x700e7,
KEY_COMPOSE: 0x70065,
KEY_RIGHTCTRL: 0x700e4,
KEY_INSERT: 0x70049,
KEY_HOME: 0x7004a,
KEY_PAGEUP: 0x7004b,
KEY_DELETE: 0x7004c,
KEY_END: 0x7004d,
KEY_PAGEDOWN: 0x7004e,
KEY_UP: 0x70052,
KEY_LEFT: 0x70050,
KEY_DOWN: 0x70051,
KEY_RIGHT: 0x7004f,
KEY_NUMLOCK: 0x70053,
KEY_KPSLASH: 0x70054,
KEY_KPASTERISK: 0x70055,
KEY_KPMINUS: 0x70056,
KEY_KP7: 0x7005f,
KEY_KP8: 0x70060,
KEY_KP9: 0x70061,
KEY_KPPLUS: 0x70057,
KEY_KP4: 0x7005c,
KEY_KP5: 0x7005d,
KEY_KP6: 0x7005e,
KEY_KP1: 0x70059,
KEY_KP2: 0x7005a,
KEY_KP3: 0x7005b,
KEY_KPENTER: 0x70058,
KEY_KP0: 0x70062,
KEY_KPDOT: 0x70063,
KEY_CONFIG: 0xc0183,
KEY_PLAYPAUSE: 0xc00cd,
KEY_MUTE: 0xc00e2,
KEY_VOLUMEDOWN: 0xc00ea,
KEY_VOLUMEUP: 0xc00e9,
KEY_HOMEPAGE: 0xc0223,
KEY_PREVIOUSSONG: 0xc00f0,
KEY_NEXTSONG: 0xc00f1,
KEY_BACK: 0xc00f2,
KEY_FORWARD: 0xc00f3,
}
// KeyPressed generates a KeyEvent and a ScanEvent for all new pressed code keys.
// It also generates a SynEvent if at least one new key is pressed.
// KeyPressed returns a non-nil error if events not correctly generated.
func (ui *UInput) KeyPressed(codes []EventCode) error {
gensyn := false
for _, k := range codes {
if _, isin := ui.keyPressedMap[k]; !isin {
gensyn = true
ui.keyPressedMap[k] = true
if err := ui.ScanEvent(scans[k]); err != nil {
return err
}
if err := ui.KeyEvent(k, 1); err != nil {
return err
}
}
}
if gensyn {
return ui.SynEvent()
}
return nil
}
// KeyReleased generates a KeyEvent and a ScanEvent for all new released code keys.
// It releases all pressed keys if no codes are provided.
// It also generates a SynEvent if at least one new key is released.
// KeyReleased returns a non-nil error if events not correctly generated.
func (ui *UInput) KeyReleased(codes []EventCode) error {
gensyn := false
// with no argument release all pressed keys
if len(codes) <= 0 {
for k := range ui.keyPressedMap {
gensyn = true
delete(ui.keyPressedMap, k)
if err := ui.ScanEvent(scans[k]); err != nil {
return err
}
if err := ui.KeyEvent(k, 0); err != nil {
return err
}
}
}
for _, k := range codes {
if _, isin := ui.keyPressedMap[k]; isin {
gensyn = true
delete(ui.keyPressedMap, k)
if err := ui.ScanEvent(scans[k]); err != nil {
return err
}
if err := ui.KeyEvent(k, 0); err != nil {
return err
}
}
}
if gensyn {
return ui.SynEvent()
}
return nil
}