-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard.go
391 lines (358 loc) · 10.3 KB
/
keyboard.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
// Copyright (c) 2023 Joshua Rich <[email protected]>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
package gokbd
// #cgo pkg-config: libevdev
// #include <libevdev/libevdev.h>
// #include <libevdev/libevdev-uinput.h>
import "C"
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
"unicode"
"github.com/rs/zerolog/log"
"kernel.org/pub/linux/libs/security/libcap/cap"
)
const devicePath = "/dev/input"
func grabDevice(dev *C.struct_libevdev) (func() error, error) {
rv := C.libevdev_grab(dev, C.LIBEVDEV_GRAB)
if rv < 0 {
return nil, errors.New("failed to grab device")
}
ungrab := func() error {
rv := C.libevdev_grab(dev, C.LIBEVDEV_UNGRAB)
if rv < 0 {
return errors.New("failed to ungrab device")
}
return nil
}
return ungrab, nil
}
// KeyboardDevice represents a physical keyboard, it contains the dev struct,
// file descriptor and state of any "modifier" keys
type KeyboardDevice struct {
dev *C.struct_libevdev
fd *os.File
modifiers *KeyModifiers
}
func (k *KeyboardDevice) Grab() (func() error, error) {
return grabDevice(k.dev)
}
// Close will gracefully handle closing a keyboard device, freeing memory and
// file descriptors
func (k *KeyboardDevice) Close() {
C.libevdev_free(k.dev)
k.fd.Close()
}
func (k *KeyboardDevice) isKeyboard() bool {
if C.libevdev_has_event_code(k.dev, C.EV_KEY, C.KEY_CAPSLOCK) == 1 {
return true
} else {
return false
}
}
// OpenKeyboardDevice will open a specific keyboard device (from the device path
// passed as a string)
func OpenKeyboardDevice(devPath string) (*KeyboardDevice, error) {
dev := C.libevdev_new()
fd, err := os.Open(devPath)
if err != nil {
return nil, err
}
c_err := C.libevdev_set_fd(dev, C.int(fd.Fd()))
if c_err > 0 {
return nil, errors.New("failed to init libevdev")
}
return &KeyboardDevice{
dev: dev,
fd: fd,
modifiers: NewKeyModifers(),
}, nil
}
// OpenAllKeyboardDevices will open all currently connected keyboards passing
// them out through a channel for further processing
func OpenAllKeyboardDevices() <-chan *KeyboardDevice {
kbdChan := make(chan *KeyboardDevice)
go func() {
for _, kbdPath := range findAllInputDevices() {
kbd, err := OpenKeyboardDevice(kbdPath)
if err != nil {
log.Error().Err(err).
Msgf("Unable to open device %s.", kbdPath)
}
if kbd.isKeyboard() {
log.Debug().Caller().
Msgf("Opening keyboard device %s.", kbdPath)
kbdChan <- kbd
} else {
kbd.Close()
}
}
close(kbdChan)
}()
return kbdChan
}
func findAllInputDevices() []string {
var paths []string
fileRegexp, _ := regexp.Compile(`event\d+$`)
log.Debug().Caller().
Msg("Looking for keyboards...")
err := filepath.WalkDir(devicePath, func(path string, d fs.DirEntry, err error) error {
if err != nil {
log.Error().Caller().Err(err).
Msgf("could not read %q.", path)
return err
}
if !d.IsDir() {
if fileRegexp.MatchString(path) {
paths = append(paths, path)
}
}
return nil
})
if err != nil {
log.Error().Caller().Err(err).
Msgf("Couldn't traverse device path: %s.", devicePath)
}
log.Debug().Caller().
Msg("Keyboard search finished.")
return paths
}
// SnoopAllKeyboards will snoop or listen for all key events on all currently connected keyboards. Keyboards are passed in through a channel, see OpenKeyboardDevices for an example of opening all connected keyboards
func SnoopAllKeyboards(ctx context.Context, kbds <-chan *KeyboardDevice) <-chan KeyEvent {
keys := make(chan KeyEvent, 1)
done := make(chan struct{})
for kbd := range kbds {
log.Debug().Caller().
Msgf("Tracking keys on device %s.", kbd.fd.Name())
go func(k *KeyboardDevice, keyCh chan KeyEvent, doneCh chan struct{}) {
kbdSnoop(k, keyCh, doneCh)
}(kbd, keys, done)
}
go func() {
defer close(keys)
defer close(done)
<-ctx.Done()
}()
return keys
}
// SnoopKeyboard will snoop or listen for all key events on the given keyboard
// device.
func SnoopKeyboard(ctx context.Context, kbd *KeyboardDevice) <-chan KeyEvent {
keys := make(chan KeyEvent)
done := make(chan struct{})
go func() {
kbdSnoop(kbd, keys, done)
}()
go func() {
defer close(keys)
defer close(done)
<-ctx.Done()
}()
return keys
}
func kbdSnoop(kbd *KeyboardDevice, keys chan KeyEvent, done chan struct{}) {
norm := C.enum_libevdev_read_flag(C.LIBEVDEV_READ_FLAG_NORMAL)
for {
var ev C.struct_input_event
if err := C.libevdev_next_event(kbd.dev, C.uint(norm), &ev); err != 0 {
log.Error().Msg("libevdev_next_event returned an error.")
}
e := NewKeyEvent(ev)
if e.Value != 2 {
switch e.EventName {
case "KEY_CAPSLOCK":
kbd.modifiers.ToggleCapsLock()
case "KEY_LEFTSHIFT", "KEY_RIGHTSHIFT":
kbd.modifiers.ToggleShift()
case "KEY_LEFTCTRL", "KEY_RIGHTCTRL":
kbd.modifiers.ToggleCtrl()
case "KEY_LEFTALT", "KEY_RIGHTALT":
kbd.modifiers.ToggleAlt()
case "KEY_LEFTMETA", "KEY_RIGHTMETA":
kbd.modifiers.ToggleMeta()
}
}
e.updateRune(kbd.modifiers)
select {
case <-done:
return
case keys <- *e:
}
}
}
// VirtualKeyboardDevice represents a "virtual" (uinput) keyboard device
type VirtualKeyboardDevice struct {
uidev *C.struct_libevdev_uinput
dev *C.struct_libevdev
Name string
DevNode string
SysPath string
}
// NewVirtualKeyboard will create a new virtual keyboard device (with the name
// passed in)
func NewVirtualKeyboard(name string) (*VirtualKeyboardDevice, error) {
if name == "" {
return nil, errors.New("no name provided")
}
var uidev *C.struct_libevdev_uinput
uid, gid := getUserIds()
setIDsWithCaps(0, 0, nil)
dev := C.libevdev_new()
C.libevdev_set_name(dev, C.CString(name))
// expose the relevant event types
C.libevdev_enable_event_type(dev, C.EV_REL)
C.libevdev_enable_event_type(dev, C.EV_KEY)
C.libevdev_enable_event_type(dev, C.EV_REP)
C.libevdev_enable_event_type(dev, C.EV_SYN)
// expose all physical ascii keys on a standard qwerty keyboard
for k := range runeMap {
C.libevdev_enable_event_code(dev, C.EV_KEY, C.uint(k), nil)
}
// expose some modifier keys (in this case just the left ones, we only need those)
C.libevdev_enable_event_code(dev, C.EV_KEY, C.KEY_LEFTSHIFT, nil)
C.libevdev_enable_event_code(dev, C.EV_KEY, C.KEY_LEFTCTRL, nil)
C.libevdev_enable_event_code(dev, C.EV_KEY, C.KEY_LEFTALT, nil)
C.libevdev_enable_event_code(dev, C.EV_KEY, C.KEY_LEFTMETA, nil)
rv := C.libevdev_uinput_create_from_device(dev, C.LIBEVDEV_UINPUT_OPEN_MANAGED, &uidev)
if rv > 0 || uidev == nil {
return nil, errors.New("failed to create new uinput device")
}
log.Debug().Caller().
Msgf("Virtual keyboard created at %s.",
C.GoString(C.libevdev_uinput_get_devnode(uidev)))
time.Sleep(time.Millisecond * 500)
setIDsWithCaps(uid, gid, []int{getInputGroupGid()})
if err := cap.NewSet().SetProc(); err != nil {
return nil, fmt.Errorf("unable to drop privilege: %v", err)
}
return &VirtualKeyboardDevice{
uidev: uidev,
dev: dev,
Name: name,
DevNode: C.GoString(C.libevdev_uinput_get_devnode(uidev)),
SysPath: C.GoString(C.libevdev_uinput_get_syspath(uidev)),
}, nil
}
func (u *VirtualKeyboardDevice) sendKeys(done <-chan struct{}, ev ...<-chan *key) <-chan error {
var wg sync.WaitGroup
out := make(chan error)
output := func(in <-chan *key) {
for k := range in {
select {
case <-done:
return
default:
rv := C.libevdev_uinput_write_event(u.uidev, C.uint(k.keyType), C.uint(k.keyCode), C.int(k.value))
if rv < 0 {
out <- fmt.Errorf("failed send key event type: %v code: %v value %v", k.keyType, k.keyCode, k.value)
}
time.Sleep(time.Microsecond)
}
}
wg.Done()
}
wg.Add(len(ev))
for _, c := range ev {
go output(c)
}
// Start a goroutine to close out once all the output goroutines are
// done. This must start after the wg.Add call.
go func() {
wg.Wait()
close(out)
}()
return out
}
func (u *VirtualKeyboardDevice) TypeKey(c int, holdShift bool) error {
done := make(chan struct{})
defer close(done)
if holdShift {
errc := u.sendKeys(done, keySequence(keyPress(C.KEY_LEFTSHIFT), keySync(), keyPress(c), keySync(), keyRelease(c), keySync(), keyRelease(C.KEY_LEFTSHIFT), keySync()))
if err := <-errc; err != nil {
return err
}
} else {
errc := u.sendKeys(done, keySequence(keyPress(c), keySync(), keyRelease(c), keySync()))
if err := <-errc; err != nil {
return err
}
}
return nil
}
func (u *VirtualKeyboardDevice) TypeRune(r rune) error {
if !unicode.In(r, unicode.PrintRanges...) {
return fmt.Errorf("rune %c (%U) is not a printable character", r, r)
}
keyCode, isUpperCase := CodeAndCase(r)
if keyCode == 0 {
return fmt.Errorf("rune %c (%U) is not in rune map", r, r)
} else {
return u.TypeKey(keyCode, isUpperCase)
}
}
// TypeSpace is a high level way to "type" a space character (effectively,
// press/release the spacebar)
func (u *VirtualKeyboardDevice) TypeSpace() error {
return u.TypeKey(C.KEY_SPACE, false)
}
// TypeBackspace allows you to "type" a backspace key and remove a single
// character
func (u *VirtualKeyboardDevice) TypeBackspace() error {
return u.TypeKey(C.KEY_BACKSPACE, false)
}
// TypeString is a high level function that makes it easy to "type" out a string
// to the virtual keyboard
func (u *VirtualKeyboardDevice) TypeString(str string) error {
s := strings.NewReader(str)
for {
r, _, err := s.ReadRune() // returns rune, nbytes, error
if err == io.EOF {
break
}
if err != nil {
return err
}
switch r {
case ' ':
err := u.TypeSpace()
if err != nil {
return err
}
default:
err := u.TypeRune(r)
if err != nil {
return err
}
}
}
return nil
}
// Close will gracefully remove a virtual keyboard, freeing memory and file
// descriptors
func (u *VirtualKeyboardDevice) Close() {
log.Debug().Caller().
Msg("Closing virtual keyboard device.")
C.libevdev_uinput_destroy(u.uidev)
C.libevdev_free(u.dev)
}
// Grab will grab the virtual keyboard which prevents any other clients and the
// kernel from recieving events from it. The returned func can be used to ungrab
// the keyboard, allowing other clients and the kernel to see its events again.
func (u *VirtualKeyboardDevice) Grab() (func() error, error) {
kbd, err := OpenKeyboardDevice(u.DevNode)
if err != nil {
return nil, fmt.Errorf("could not open %s", u.Name)
}
return grabDevice(kbd.dev)
}