-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhidproxy.go
734 lines (685 loc) · 19.3 KB
/
hidproxy.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
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
package hidproxy
// Go implementation of Bluetooth to USB HID proxy
// Author: Taneli Leppä <[email protected]>
// Licensed under Apache License 2.0
import (
"bytes"
"context"
evdev "github.com/gvalkov/golang-evdev"
udev "github.com/jochenvg/go-udev"
"github.com/loov/hrtime"
"github.com/muka/go-bluetooth/api"
"github.com/muka/go-bluetooth/bluez/profile/adapter"
log "github.com/sirupsen/logrus"
orderedmap "github.com/wk8/go-ordered-map"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"syscall"
"time"
)
type Config struct {
SetupHid bool
SetupMouse bool
SetupKeyboard bool
MonitorUdev bool
AdapterId string
KbdRepeat int
KbdDelay int
LogLevel log.Level
}
type InputDevice struct {
Device string
Name string
}
type InputMessage struct {
Message []byte
Timestamp time.Duration
}
var Scancodes = map[uint16]uint16{
2: 30, // 1
3: 31, // 2
4: 32, // 3
5: 33, // 4
6: 34, // 5
7: 35, // 6
8: 36, // 7
9: 37, // 8
10: 38, // 9
11: 39, // 0
57: 44, // space
14: 42, // bkspc
28: 40, // enter
1: 41, // ESC
106: 79, // RIGHT
105: 80, // LEFT
108: 81, // DOWN
103: 82, // UP
59: 58, // F1
60: 59, // F2
61: 60, // F3
62: 61, // F4
63: 62, // F5
64: 63, // F6
65: 64, // F7
66: 65, // F8
67: 66, // F9
68: 67, // F10
69: 68, // F11
70: 69, // F12
12: 45, // -
13: 46, // =
15: 43, // TAB
26: 47, // {
27: 48, // ]
39: 51, // :
40: 52, // "
51: 54, // <
52: 55, // >
53: 56, // ?
41: 50, // //
43: 49, // \
30: 4, // a
48: 5, // b
46: 6, // c
32: 7, // d
18: 8, // e
33: 9, // f
34: 10, // g
35: 11, // h
23: 12, // i
36: 13, // j
37: 14, // k
38: 15, // l
50: 16, // m
49: 17, // n
24: 18, // o
25: 19, // p
16: 20, // q
19: 21, // r
31: 22, // s
20: 23, // t
22: 24, // u
47: 25, // v
17: 26, // w
45: 27, // x
21: 28, // y
44: 29, // z
86: 49, // | & \
104: 75, // PgUp
109: 78, // PgDn
102: 74, // Home
107: 77, // End
110: 73, // Insert
119: 72, // Pause
//70: 71, // ScrLk
99: 70, // PrtSc
87: 68, // F11
88: 69, // F12
113: 127, // Mute
114: 129, // VolDn
115: 128, // VolUp
58: 57, // CapsLock (non-locking)
158: 122, // "Undo" (Thinkpad special key)
159: 121, // "Again" (Thinkpad special key)
29: 224, // Left-Ctrl
125: 227, // Left-Cmd
42: 225, // Left-Shift
56: 226, // Left-Alt
100: 230, // AltGr (Right-Alt)
127: 231, // Right-Cmd
97: 228, // Right-Ctrl
54: 229, // Right-Shift
111: 76, // Delete
164: 232, // Play-Pause
165: 234, // Previous-Track
163: 233, // Next-Track
}
const (
RIGHT_META = 1 << 7
RIGHT_ALT = 1 << 6
RIGHT_SHIFT = 1 << 5
RIGHT_CONTROL = 1 << 4
LEFT_META = 1 << 3
LEFT_ALT = 1 << 2
LEFT_SHIFT = 1 << 1
LEFT_CONTROL = 1 << 0
BUTTON_LEFT = 1 << 0
BUTTON_RIGHT = 1 << 1
BUTTON_MIDDLE = 1 << 2
)
func SetupUSBGadget() {
var paths = []string{
"/sys/kernel/config/usb_gadget/piproxy",
"/sys/kernel/config/usb_gadget/piproxy/strings/0x409",
"/sys/kernel/config/usb_gadget/piproxy/configs/c.1/strings/0x409",
"/sys/kernel/config/usb_gadget/piproxy/functions/hid.usb0",
"/sys/kernel/config/usb_gadget/piproxy/functions/hid.usb1",
}
filesStr := orderedmap.New()
filesStr.Set("/sys/kernel/config/usb_gadget/piproxy/idVendor", "0x1d6b")
filesStr.Set("/sys/kernel/config/usb_gadget/piproxy/idProduct", "0x0104")
filesStr.Set("/sys/kernel/config/usb_gadget/piproxy/bcdDevice", "0x0100")
filesStr.Set("/sys/kernel/config/usb_gadget/piproxy/bcdUSB", "0x0200")
filesStr.Set("/sys/kernel/config/usb_gadget/piproxy/strings/0x409/serialnumber", "fedcba9876543210")
filesStr.Set("/sys/kernel/config/usb_gadget/piproxy/strings/0x409/manufacturer", "Raspberry Pi")
filesStr.Set("/sys/kernel/config/usb_gadget/piproxy/strings/0x409/product", "pizero keyboard Device")
filesStr.Set("/sys/kernel/config/usb_gadget/piproxy/configs/c.1/strings/0x409/configuration", "Config 1: ECM network")
filesStr.Set("/sys/kernel/config/usb_gadget/piproxy/configs/c.1/MaxPower", "250")
filesStr.Set("/sys/kernel/config/usb_gadget/piproxy/functions/hid.usb0/protocol", "1")
filesStr.Set("/sys/kernel/config/usb_gadget/piproxy/functions/hid.usb0/subclass", "1")
filesStr.Set("/sys/kernel/config/usb_gadget/piproxy/functions/hid.usb0/report_length", "8")
filesStr.Set("/sys/kernel/config/usb_gadget/piproxy/functions/hid.usb1/protocol", "2")
filesStr.Set("/sys/kernel/config/usb_gadget/piproxy/functions/hid.usb1/subclass", "1")
filesStr.Set("/sys/kernel/config/usb_gadget/piproxy/functions/hid.usb1/report_length", "4")
var filesBytes = map[string][]byte{
"/sys/kernel/config/usb_gadget/piproxy/functions/hid.usb0/report_desc": []byte{0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x05, 0x07, 0x19, 0xe0, 0x29, 0xe7, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x08, 0x81, 0x02, 0x95, 0x01, 0x75, 0x08, 0x81, 0x03, 0x95, 0x05, 0x75, 0x01, 0x05, 0x08, 0x19, 0x01, 0x29, 0x05, 0x91, 0x02, 0x95, 0x01, 0x75, 0x03, 0x91, 0x03, 0x95, 0x06, 0x75, 0x08, 0x15, 0x00, 0x25, 0x65, 0x05, 0x07, 0x19, 0x00, 0x29, 0x65, 0x81, 0x00, 0xc0},
"/sys/kernel/config/usb_gadget/piproxy/functions/hid.usb1/report_desc": []byte{0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x09, 0x01, 0xa1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x05, 0x15, 0x00, 0x25, 0x01, 0x95, 0x05, 0x75, 0x01, 0x81, 0x02, 0x95, 0x01, 0x75, 0x03, 0x81, 0x01, 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x09, 0x38, 0x15, 0x81, 0x25, 0x7f, 0x75, 0x08, 0x95, 0x03, 0x81, 0x06, 0xc0, 0xc0},
}
var symlinks = map[string]string{
"/sys/kernel/config/usb_gadget/piproxy/functions/hid.usb0": "/sys/kernel/config/usb_gadget/piproxy/configs/c.1/hid.usb0",
"/sys/kernel/config/usb_gadget/piproxy/functions/hid.usb1": "/sys/kernel/config/usb_gadget/piproxy/configs/c.1/hid.usb1",
}
for _, path := range paths {
if _, err := os.Stat(path); os.IsNotExist(err) {
log.Debugf("Creating directory: %s", path)
err := os.MkdirAll(path, os.ModeDir)
if err != nil {
log.Fatalf("Failed to create directory path: %s", path)
}
}
}
for pair := filesStr.Oldest(); pair != nil; pair = pair.Next() {
content, err := ioutil.ReadFile(pair.Key.(string))
if err == nil {
if bytes.Compare(content[0:len(content)-1], []byte(pair.Value.(string))) == 0 {
continue
}
}
log.Debugf("Writing file: %s", pair.Key.(string))
err = ioutil.WriteFile(pair.Key.(string), []byte(pair.Value.(string)), os.FileMode(0644))
if err != nil {
log.Warnf("Failed to write file: %s (maybe already set up)", pair.Key.(string))
}
}
for file, contents := range filesBytes {
content, err := ioutil.ReadFile(file)
if err == nil {
if bytes.Compare(content, contents) == 0 {
continue
}
}
log.Debugf("Writing file: %s", file)
err = ioutil.WriteFile(file, contents, os.FileMode(0644))
if err != nil {
log.Warnf("Failed to create file: %s (maybe already set up)", file)
}
}
for source, target := range symlinks {
if _, err := os.Stat(target); os.IsNotExist(err) {
log.Debugf("Creating symlink from %s to: %s", source, target)
err := os.Symlink(source, target)
if err != nil {
log.Fatalf("Failed to create symlink %s -> %s", source, target)
}
}
}
time.Sleep(1000 * time.Millisecond)
matches, err := filepath.Glob("/sys/class/udc/*")
if err != nil {
log.Fatalf("Failed to list files in /sys/class/udc: %s", err.Error())
}
var udcFile string = "/sys/kernel/config/usb_gadget/piproxy/UDC"
var udc string = ""
for _, match := range matches {
udc = udc + filepath.Base(match) + " "
}
content, err := ioutil.ReadFile(udcFile)
if err == nil {
if bytes.Compare(content[0:len(content)-1], []byte(strings.TrimSpace(udc))) != 0 {
err = ioutil.WriteFile(udcFile, []byte(strings.TrimSpace(udc)), os.FileMode(0644))
if err != nil {
log.Warnf("Failed to create file %s: %s: (%s)", udcFile, udc, err.Error())
}
}
}
// Give it a second to settle
time.Sleep(1000 * time.Millisecond)
}
func HandleKeyboard(output chan<- error, input chan<- InputMessage, close <-chan bool, rate uint, delay uint, dev evdev.InputDevice) error {
keysDown := make([]uint16, 0)
err := dev.Grab()
if err != nil {
log.Fatal(err)
output <- err
return err
}
defer dev.Release()
log.Infof("Grabbed keyboard-like device: %s (%s)", dev.Name, dev.Fn)
syscall.SetNonblock(int(dev.File.Fd()), true)
log.Infof("Setting repeat rate to %d, delay %d for %s (%s)", rate, delay, dev.Name, dev.Fn)
dev.SetRepeatRate(rate, delay)
loop := 0
for {
err = dev.File.SetReadDeadline(time.Now().Add(250 * time.Millisecond))
if err != nil {
log.Fatal(err)
output <- err
return err
}
event, err := dev.ReadOne()
if err != nil && strings.Contains(err.Error(), "i/o timeout") {
continue
}
if err != nil {
log.Fatal(err)
output <- err
return err
}
log.Debugf("Keyboard input event: type=%d, code=%d, value=%d", event.Type, event.Code, event.Value)
if event.Type == evdev.EV_KEY {
keyEvent := evdev.NewKeyEvent(event)
log.Debugf("Key event: scancode=%d, keycode=%d, state=%d", keyEvent.Scancode, keyEvent.Keycode, keyEvent.State)
if keyCode, ok := Scancodes[keyEvent.Scancode]; ok {
if keyEvent.State == 1 { // Key down
keyIsDown := false
for _, k := range keysDown {
if k == keyCode {
keyIsDown = true
}
}
if !keyIsDown {
keysDown = append(keysDown, keyCode)
}
}
if keyEvent.State == 0 { // Key up
newKeysDown := make([]uint16, 0)
for _, k := range keysDown {
if k != keyCode {
newKeysDown = append(newKeysDown, k)
}
}
keysDown = newKeysDown
}
var modifiers uint8 = 0
keysToSend := make([]uint8, 0)
for _, k := range keysDown {
switch {
case k == 224: // Left-Ctrl
modifiers |= LEFT_CONTROL
case k == 227: // Left-Cmd
modifiers |= LEFT_META
case k == 225: // Left-Shift
modifiers |= LEFT_SHIFT
case k == 226: // Left-Alt
modifiers |= LEFT_ALT
case k == 228: // Right-Ctrl
modifiers |= RIGHT_CONTROL
case k == 231: // Right-Cmd
modifiers |= RIGHT_META
case k == 229: // Right-Shift
modifiers |= RIGHT_SHIFT
case k == 230: // Right-Alt
modifiers |= RIGHT_ALT
default:
keysToSend = append(keysToSend, uint8(k))
}
}
keysToSend = append([]uint8{modifiers, 0}, keysToSend...)
if len(keysToSend) < 8 {
for i := len(keysToSend); i < 8; i++ {
keysToSend = append(keysToSend, uint8(0))
}
}
input <- InputMessage{
Timestamp: hrtime.Now(),
Message: keysToSend,
}
log.Debugf("Key status (scancode %d, keycode %d): %v\n", keyEvent.Scancode, keyCode, keysToSend)
} else {
log.Warnf("Unknown scancode: %d\n", keyEvent.Scancode)
}
}
loop += 1
if loop > 3 {
select {
case _ = <-close:
log.Infof("Stopping processing keyboard input from: %s (%s)", dev.Name, dev.Fn)
output <- nil
return nil
default:
}
loop = 0
}
}
output <- nil
return nil
}
func HandleMouse(output chan<- error, input chan<- InputMessage, close <-chan bool, dev evdev.InputDevice) error {
err := dev.Grab()
if err != nil {
log.Fatal(err)
output <- err
return err
}
defer dev.Release()
log.Infof("Grabbed mouse-like device: %s (%s)", dev.Name, dev.Fn)
syscall.SetNonblock(int(dev.File.Fd()), true)
loop := 0
var buttons uint8 = 0x0
for {
err = dev.File.SetReadDeadline(time.Now().Add(250 * time.Millisecond))
if err != nil {
log.Fatal(err)
output <- err
return err
}
event, err := dev.ReadOne()
if err != nil && strings.Contains(err.Error(), "i/o timeout") {
continue
}
if err != nil {
log.Fatal(err)
output <- err
return err
}
log.Debugf("Mouse input event: type=%d, code=%d, value=%d", event.Type, event.Code, event.Value)
var buttonOp bool = false
if event.Type == evdev.EV_KEY {
if event.Code == 272 {
if event.Value > 0 {
buttons |= BUTTON_LEFT
} else {
buttons &= ^uint8(BUTTON_LEFT)
}
buttonOp = true
}
if event.Code == 273 {
if event.Value > 0 {
buttons |= BUTTON_RIGHT
} else {
buttons &= ^uint8(BUTTON_RIGHT)
}
buttonOp = true
}
if event.Code == 274 {
if event.Value > 0 {
buttons |= BUTTON_MIDDLE
} else {
buttons &= ^uint8(BUTTON_MIDDLE)
}
buttonOp = true
}
}
if event.Type == evdev.EV_REL || buttonOp {
mouseToSend := make([]uint8, 0)
mouseToSend = append(mouseToSend, buttons)
if event.Type == evdev.EV_REL {
if event.Code == 0 {
mouseToSend = append(mouseToSend, uint8(event.Value))
mouseToSend = append(mouseToSend, 0x00)
mouseToSend = append(mouseToSend, 0x00)
}
if event.Code == 1 {
mouseToSend = append(mouseToSend, 0x00)
mouseToSend = append(mouseToSend, uint8(event.Value))
mouseToSend = append(mouseToSend, 0x00)
}
if event.Code == 11 {
mouseToSend = append(mouseToSend, 0x00)
mouseToSend = append(mouseToSend, 0x00)
mouseToSend = append(mouseToSend, uint8(event.Value))
}
} else {
mouseToSend = append(mouseToSend, 0x00)
mouseToSend = append(mouseToSend, 0x00)
mouseToSend = append(mouseToSend, 0x00)
}
input <- InputMessage{
Timestamp: hrtime.Now(),
Message: mouseToSend,
}
}
loop += 1
if loop > 3 {
select {
case _ = <-close:
log.Infof("Stopping processing mouse input from: %s (%s)", dev.Name, dev.Fn)
output <- nil
return nil
default:
}
loop = 0
}
}
output <- nil
return nil
}
func SendKeyboardReports(input <-chan InputMessage) error {
log.Info("Opening keyboard /dev/hidg0 for writing...")
file, err := os.OpenFile("/dev/hidg0", os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
log.Warn("Error opening /dev/hidg0, are you running as root?")
log.Fatal(err)
return err
}
defer file.Close()
var avg, min, max, loop int64 = 0, 0, 0, 0
for {
msg := <-input
bytesWritten, err := file.Write(msg.Message)
if err != nil {
log.Fatal(err)
return err
}
latency := hrtime.Since(msg.Timestamp).Nanoseconds()
if latency < min {
min = latency
}
if latency > max {
max = latency
}
avg = (avg + latency) / 2
loop += 1
if loop > 50 {
log.Debugf("Latency: now=%d, avg=%d, min=%d, max=%d μs", latency/1000, avg/1000, min/1000, max/1000)
loop = 0
}
log.Debugf("Wrote %d bytes to /dev/hidg0 (%v)", bytesWritten, msg)
}
return nil
}
func SendMouseReports(input <-chan InputMessage) error {
log.Info("Opening keyboard /dev/hidg1 for writing...")
file, err := os.OpenFile("/dev/hidg1", os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
log.Warn("Error opening /dev/hidg1, are you running as root?")
log.Fatal(err)
return err
}
defer file.Close()
var avg, min, max, loop int64 = 0, 0, 0, 0
for {
msg := <-input
bytesWritten, err := file.Write(msg.Message)
if err != nil {
log.Fatal(err)
return err
}
log.Debugf("Wrote %d bytes to /dev/hidg1 (%v)", bytesWritten, msg)
latency := hrtime.Since(msg.Timestamp).Nanoseconds()
if latency < min {
min = latency
}
if latency > max {
max = latency
}
avg = (avg + latency) / 2
loop += 1
if loop > 100 {
log.Debugf("Latency: now=%d, avg=%d, min=%d, max=%d μs", latency/1000, avg/1000, min/1000, max/1000)
loop = 0
}
}
return nil
}
func GetDisconnectedDevices(adapterId string) ([]string, error) {
log.Debugf("Getting adapter: %s", adapterId)
a, err := adapter.GetAdapter(adapterId)
if err != nil {
return nil, err
}
log.Debugf("Getting devices from adapter: %s", adapterId)
devices, err := a.GetDevices()
if err != nil {
return nil, err
}
disconnected := make([]string, 0)
connected := make([]string, 0)
for _, dev := range devices {
address, err := dev.GetAddress()
if err != nil {
continue
}
name, err := dev.GetName()
if err != nil {
name = "?"
}
log.Infof("Checking if device %s (%s) is connected...", name, address)
deviceConnected, err := dev.GetConnected()
if err == nil {
if !deviceConnected {
log.Infof("Device %s is disconnected.", name)
disconnected = append(disconnected, name)
} else {
log.Infof("Device %s is still connected.", name)
connected = append(connected, name)
}
}
}
results := make([]string, 0)
for _, dname := range disconnected {
ok := true
for _, cname := range connected {
if cname == dname {
ok = false
break
}
}
if ok {
inResults := false
for _, rname := range results {
if rname == dname {
inResults = true
}
}
if !inResults {
results = append(results, dname)
}
}
}
return results, nil
}
func Start(config Config) {
var wg sync.WaitGroup
log.SetLevel(config.LogLevel)
if config.SetupHid {
log.Info("Setting up HID files...")
SetupUSBGadget()
}
keyboardInput := make(chan InputMessage, 10)
mouseInput := make(chan InputMessage, 100)
output := make(map[InputDevice]chan error, 0)
close := make(map[InputDevice]chan bool, 0)
var udevCh <-chan *udev.Device
var cancel context.CancelFunc
var ctx context.Context
defer api.Exit()
u := udev.Udev{}
if config.MonitorUdev {
log.Info("Starting udev monitoring for Bluetooth devices")
m := u.NewMonitorFromNetlink("udev")
m.FilterAddMatchSubsystem("bluetooth")
ctx, cancel = context.WithCancel(context.Background())
udevCh, _ = m.DeviceChan(ctx)
}
go SendKeyboardReports(keyboardInput)
go SendMouseReports(mouseInput)
wg.Add(1)
for {
select {
case d := <-udevCh:
if d.Action() == "add" || d.Action() == "remove" {
disconnected, err := GetDisconnectedDevices(config.AdapterId)
if err != nil {
log.Errorf("Error checking disconnected devices: %s", err.Error())
} else {
for _, device := range disconnected {
for devId, _ := range output {
if strings.HasPrefix(devId.Name, device) {
log.Infof("Disconnected device, stopping listening to: %s (%s)", devId.Name, devId.Device)
select {
case close[devId] <- true:
log.Infof("Sent stop signal to: %s (%s)", devId.Name, devId.Device)
default:
}
}
}
}
}
}
default:
}
log.Info("Polling for new devices in /dev/input\n")
devices, _ := evdev.ListInputDevices()
for _, dev := range devices {
isMouse := false
isKeyboard := false
for k := range dev.Capabilities {
if k.Name == "EV_REL" {
isMouse = true
}
if k.Name == "EV_KEY" {
isKeyboard = true
}
}
log.Debugf("Device %s (%s), capabilities: %v (mouse=%t, kbd=%t)", dev.Name, dev.Fn, dev.Capabilities, isMouse, isKeyboard)
if isKeyboard || isMouse {
devId := InputDevice{
Device: dev.Fn,
Name: dev.Name,
}
if _, ok := output[devId]; !ok {
output[devId] = make(chan error, 10)
close[devId] = make(chan bool, 10)
if isKeyboard && !isMouse && config.SetupKeyboard {
go HandleKeyboard(output[devId], keyboardInput, close[devId], uint(config.KbdRepeat), uint(config.KbdDelay), *dev)
wg.Add(1)
}
log.Debugf("isKeyboard: %t, isMouse: %t, setupMouse: %t", !isKeyboard, isMouse, config.SetupMouse)
if isMouse && config.SetupMouse {
go HandleMouse(output[devId], mouseInput, close[devId], *dev)
wg.Add(1)
}
}
}
}
time.Sleep(1000 * time.Millisecond)
for id, eventOutput := range output {
select {
case msg := <-eventOutput:
if msg == nil {
log.Warnf("Event handler quit: %s", id.Device)
} else {
log.Errorf("Received error from %s: %s", id.Device, msg.Error())
}
delete(output, id)
wg.Done()
default:
}
}
}
cancel()
}