forked from pmfournier/usbkvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
651 lines (534 loc) · 13.5 KB
/
main.c
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
/* PMF 2015
*
* udev api intro: http://www.signal11.us/oss/udev/
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <unistd.h>
#include <stdbool.h>
#include <string.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/input.h>
#include <inttypes.h>
#include <poll.h>
#include <libudev.h>
#include "udev.h"
#include "libcstuff.h"
#include "emukb.h"
#include "emumouse.h"
#include "keymap.h"
/* A real hid device plugged into this machine's host controller */
struct hid_in {
/* The event device, typically /dev/input/eventX */
const char *input_dev;
const char *usbhid_sys_path;
};
//struct kbpt {
// list_head phys_kb;
//
// struct emu_kb emu_kb;
//};
//
//
int fd_keyboard = -1;
int fd_mouse = -1;
bool
register_physical_keyboard(struct hid_in *hin)
{
/* Set up listening for events */
int fd = open(hin->input_dev, O_RDONLY);
if (fd == -1) {
PERROR("open");
return false;
}
int result = ioctl(fd, EVIOCGRAB, 1);
if (result == -1) {
PERROR("ioctl(EVIOCGRAB)");
return false;
}
//list_add(kbpt.phys_kb, fd);
fd_keyboard = fd;
/* Ok, we have a physical keyboard all setup;
* now we can set up an emulated keyboard.
*/
//if (!register_emulated_keyboard()) {
// ERROR("failed to register emulated keyboard");
// return false;
//}
return true;
}
bool
register_physical_mouse(struct hid_in *hin)
{
/* Set up listening for events */
int fd = open(hin->input_dev, O_RDONLY);
if (fd == -1) {
PERROR("open");
return false;
}
int result = ioctl(fd, EVIOCGRAB, 1);
if (result == -1) {
PERROR("ioctl(EVIOCGRAB)");
return false;
}
fd_mouse = fd;
return true;
}
bool
probe_device(struct udev_device *dev)
{
DEBUG("probe: probing device at %s", udev_device_get_syspath(dev));
/* 1. Want an input device with an event character device */
const char *event_dev = NULL;
/* 2. Want a usb device of driver usbhid */
const char *usbhid_sys_path = NULL;
/* 3. capabilities/key */
const char *capabilities_key = NULL;
struct udev_device *orig_dev = dev;
for (; dev != NULL; dev = udev_device_get_parent(dev)) {
const char *subsys = udev_device_get_subsystem(dev);
const char *driver = udev_device_get_driver(dev);
if (!event_dev && subsys && !strcmp(subsys, "input")) {
event_dev = udev_device_get_devnode(dev);
/* Could be NULL, in that case we'll try again
* at the next opportunity.
*/
}
if (!usbhid_sys_path && subsys && driver && !strcmp(subsys, "usb") && !strcmp(driver, "usbhid")) {
usbhid_sys_path = udev_device_get_syspath(dev);
/* Could be NULL, in that case we'll try again
* at the next opportunity.
*/
}
if (!capabilities_key) {
capabilities_key = udev_device_get_sysattr_value(dev, "capabilities/key");
}
}
if (event_dev == NULL) {
DEBUG("rejected device because we couldn't find a device for it");
return false;
}
if (usbhid_sys_path == NULL) {
DEBUG("rejected device because we couldn't find a hid parent for it");
return false;
}
bool is_keyboard = false;
bool is_mouse = false;
/* USB keyboards have a key bitmap that ends in ffffffff fffffffe */
const char needle_keyboard_end[] = "ffffffff fffffffe";
const char needle_mouse_begin[] = "70000 0 0 0 0 0 0 0 0";
if (strlen(capabilities_key) >= sizeof(needle_keyboard_end) -1) {
if (strcmp(capabilities_key + strlen(capabilities_key) - (sizeof(needle_keyboard_end) - 1), needle_keyboard_end) == 0) {
is_keyboard = true;
VERBOSE("Got a good keyboard (%s) [path=%s] [%s]", event_dev, udev_device_get_syspath(orig_dev), capabilities_key);
} else {
DEBUG("rejected device as keyboard because it doesn't have the right key capabilities (%s)", capabilities_key);
}
} else {
DEBUG("rejected device as keyboard because it doesn't have the right key capabilities length");
}
if (strlen(capabilities_key) >= sizeof(needle_mouse_begin) -1) {
if (strcmp(capabilities_key, needle_mouse_begin) == 0) {
is_mouse = true;
VERBOSE("Got a good mouse (%s) [path=%s] [%s]", event_dev, udev_device_get_syspath(orig_dev), capabilities_key);
} else {
DEBUG("rejected device as mouse because it doesn't have the right key capabilities (%s)", capabilities_key);
}
} else {
DEBUG("rejected device as mouse because it doesn't have the right key capabilities length");
}
/* Ok we have a usb device which resolved as hid and input.
* Still check if it's a type that interests us.
*/
/* We have a device we're interested in */
struct hid_in *hin = malloc(sizeof(struct hid_in));
if (!hin) {
OOM();
return false;
}
hin->usbhid_sys_path = strdup(usbhid_sys_path);
if (!hin->usbhid_sys_path) {
OOM();
return false;
}
hin->input_dev = strdup(event_dev);
if (!hin->input_dev) {
OOM();
return false;
}
if (is_keyboard) {
if (!register_physical_keyboard(hin)) {
ERROR("failed to register keyboard");
return false;
}
}
/* FIXME */
if (is_mouse && fd_mouse == -1) {
if (!register_physical_mouse(hin)) {
ERROR("failed to register mouse");
return false;
}
}
return true;
}
uint16_t modifiers[] = {
KEY_LEFTCTRL,
KEY_LEFTSHIFT,
KEY_LEFTALT,
KEY_LEFTMETA,
KEY_RIGHTCTRL,
KEY_RIGHTSHIFT,
KEY_RIGHTALT,
KEY_RIGHTMETA,
};
int8_t key_to_usb_modifier(uint16_t key)
{
int i;
for (i = 0; i < 8; i++) {
if (key == modifiers[i]) {
return i;
}
}
return -1;
}
enum state {
STATE_PASSTHROUGH = 0,
STATE_COMMAND_INPUT = 1,
STATE_KEYMAP_GET_FROM = 2,
STATE_KEYMAP_GET_TO = 3,
} state = STATE_PASSTHROUGH;
bool process_command(const char *cmd)
{
ERROR("Got command %s", cmd);
if (!strcmp(cmd, "remap")) {
if (!emukb_erase_chars(strlen(cmd))) {
ERROR("failed to erase chars");
return false;
}
if (!emukb_inject("Ok enter key to remap: ")) {
ERROR("failed to inject to keyboard");
return false;
}
state = STATE_KEYMAP_GET_FROM;
} else if (!strcmp(cmd, "swap")) {
emukb_use_aux = !emukb_use_aux;
emumouse_use_aux = !emumouse_use_aux;
} else {
if (!emukb_erase_chars(strlen(cmd))) {
ERROR("failed to erase chars");
return false;
}
}
return true;
}
char current_command[100];
size_t current_command_len = 0;
uint8_t keymap_from;
bool send_pressed_report_from_scancode(uint8_t modifier_state, int64_t scancode, bool is_modifier)
{
char report[8];
memset(report, 0, sizeof(report));
report[0] = modifier_state;
DEBUG("Have modifiers %" PRIu8, modifier_state);
if (!is_modifier) {
report[2] = scancode;
}
if (modifier_state == 2 + 4 + 8 + 32 + 64 + 128) {
if (!emukb_inject(">")) {
ERROR("failed to inject to keyboard");
return false;
}
state = STATE_COMMAND_INPUT;
}
if (emukb_send_report(report, 8) == -1) {
ERROR("failed to send usb report");
return false;
}
return true;
}
bool key_pressed(uint8_t modifier_state, int64_t scancode, bool is_modifier)
{
if (state == STATE_PASSTHROUGH) {
if (scancode == 71) {
emukb_use_aux = !emukb_use_aux;
emumouse_use_aux = !emumouse_use_aux;
return true;
}
uint8_t mapped;
keymap_map(scancode, &mapped);
send_pressed_report_from_scancode(modifier_state, mapped, is_modifier);
} else if (state == STATE_COMMAND_INPUT) {
char ascii;
usb2ascii(scancode, &ascii);
if (ascii == '\n') {
/* Command done */
current_command[current_command_len] = 0;
state = STATE_PASSTHROUGH;
process_command(current_command);
current_command_len = 0;
return true;
}
current_command[current_command_len++] = ascii;
send_pressed_report_from_scancode(modifier_state, scancode, is_modifier);
if (current_command_len == sizeof(current_command) - 1) {
ERROR("command too long; resetting");
current_command_len = 0;
}
} else if (state == STATE_KEYMAP_GET_FROM) {
keymap_from = scancode;
state = STATE_KEYMAP_GET_TO;
if (!emukb_inject(" Ok; enter destination: ")) {
ERROR("failed to inject to keyboard");
return false;
}
} else if (state == STATE_KEYMAP_GET_TO) {
keymap_remap(keymap_from, scancode);
if (!emukb_inject(" Roger. ")) {
ERROR("failed to inject to keyboard");
return false;
}
state = STATE_PASSTHROUGH;
if (!emukb_erase_injected()) {
ERROR("failed to erase injected keys");
return false;
}
}
return true;
}
bool key_released(uint8_t modifier_state, int64_t scancode, bool is_modifier)
{
if (state == STATE_PASSTHROUGH || state == STATE_COMMAND_INPUT) {
char report[8];
memset(report, 0, sizeof(report));
report[0] = modifier_state;
if (emukb_send_report(report, 8) == -1) {
ERROR("failed to send usb report");
return false;
}
}
return true;
}
bool handle_keyboard_event(void)
{
int result;
struct input_event ev;
static uint8_t modifier_state = 0;
static int64_t current_key = 0;
static int64_t current_scancode = 0;
static int64_t current_act = -1;
result = read(fd_keyboard, &ev, sizeof(struct input_event));
if (result == -1) {
PERROR("read");
return false;
}
if (result == 0) {
ERROR("Got end of file on evdev");
return false;
}
if (ev.type == EV_SYN) {
/* Done, check if we have a valid event */
if (!current_key || !current_scancode || current_act == -1) {
current_key = 0;
current_scancode = 0;
current_act = -1;
return true;
}
DEBUG("%s linux key %" PRId64 " (usb %" PRId64 ")", current_act?"Pressed":"Released", current_key, current_scancode);
int8_t mod = key_to_usb_modifier(current_key);
if (current_act == 1) {
if (mod != -1) {
modifier_state |= (1 << mod);
}
key_pressed(modifier_state, current_scancode, (mod != -1));
} else {
if (mod != -1) {
modifier_state &= ~(1 << mod);
}
key_released(modifier_state, current_scancode, (mod != -1));
}
current_key = 0;
current_scancode = 0;
current_act = -1;
}
if (ev.type == EV_KEY && (ev.value == 0 || ev.value == 1)) {
current_key = ev.code;
current_act = ev.value;
}
if (ev.type == EV_MSC && ev.code == MSC_SCAN) {
current_scancode = ev.value & 0xffff;
}
return true;
}
bool handle_mouse_event(void)
{
static uint8_t button[3] = { 0, 0, 0};
int8_t wheel = 0;
int result;
struct input_event ev;
result = read(fd_mouse, &ev, sizeof(struct input_event));
if (result == -1) {
PERROR("read");
return false;
}
if (result == 0) {
ERROR("Got end of file on evdev");
return false;
}
DEBUG("Got a mouse event! %" PRIu16 " %" PRIu16 " %" PRId32, ev.type, ev.code, ev.value);
int16_t x_movement = 0;
int16_t y_movement = 0;
if (ev.type == 2 && ev.code == 0) {
x_movement = ev.value;
}
if (ev.type == 2 && ev.code == 1) {
y_movement = ev.value;
}
if (ev.type == 2 && ev.code == 8) {
wheel = ev.value;
}
if (ev.type == 1 && ev.code == BTN_LEFT) {
button[0] = ev.value;
}
if (ev.type == 1 && ev.code == BTN_RIGHT) {
button[1] = ev.value;
}
if (ev.type == 1 && ev.code == BTN_MIDDLE) {
button[2] = ev.value;
}
uint8_t but = (button[0] << 0) | (button[1] << 1) | (button[2] << 2);
if (emumouse_send_report(but, x_movement, y_movement, wheel) == -1) {
ERROR("failed to send usb report");
return false;
}
//if (emumouse_send_report(0, 0, 0) == -1) {
// ERROR("failed to send usb report");
// return false;
//}
return true;
}
bool input_loop(void)
{
size_t n_poll_fds = 0;
struct pollfd pfd[2];
if (fd_keyboard != -1) {
pfd[n_poll_fds].fd = fd_keyboard;
pfd[n_poll_fds].events = POLLIN;
n_poll_fds++;
} if (fd_mouse != -1) {
pfd[n_poll_fds].fd = fd_mouse;
pfd[n_poll_fds].events = POLLIN;
n_poll_fds++;
} else {
/* No keyboard nor mouse */
ERROR("no keyboard nor mouse; not continuing with event loop");
return false;
}
for (;;) {
if (poll(pfd, n_poll_fds, -1) == -1) {
PERROR("poll failed");
return false;
}
for (int i = 0; i < n_poll_fds; i++) {
if (!pfd[i].revents) {
continue;
}
if (pfd[i].fd == fd_keyboard) {
if (!handle_keyboard_event()) {
ERROR("failed to handle keyboard event");
return false;
}
} else {
if (!handle_mouse_event()) {
ERROR("failed to handle mouse event");
return false;
}
}
}
}
return true;
}
bool run(void)
{
keymap_init();
emukb_unregister();
emumouse_unregister();
system("rmdir /config/usb_gadget/kb/configs/c.1");
system("rmdir /config/usb_gadget/kb");
if (!emukb_register_pre_enable()) {
ERROR("emukb_register_pre_enable() failed");
return false;
}
if (!emukb_register_auxiliary("/dev/ttyO1", 115200)) {
ERROR("failed to register auxiliary gadget hardware");
return false;
}
if (!emumouse_register_pre_enable()) {
ERROR("emumouse_register_pre_enable() failed");
return false;
}
// ENABLE
system("echo musb-hdrc.0.auto >/config/usb_gadget/kb/UDC");
if (!emukb_register_post_enable()) {
ERROR("emukb_register_post_enable() failed");
return false;
}
if (!emumouse_register_post_enable()) {
ERROR("emumouse_register_post_enable() failed");
return false;
}
/* Look at installed hardware */
udev_initial_probe(probe_device);
if (fd_keyboard == -1) {
ERROR("failed to probe keyboard");
return false;
}
input_loop();
return true;
}
bool
parse_opt(int argc, char **argv)
{
struct option opts[] = {
{ "verbose", 0, NULL, 1 },
{ "debug", 0, NULL, 2 },
{ "help", 0, NULL, 'h' },
{ NULL, 0, NULL, 0 },
};
for (;;) {
int opt;
opt = getopt_long(argc, argv, "h", opts, NULL);
if (opt == -1) {
/* Finished */
break;
}
switch (opt) {
case 1: /* verbose */
__loglevel = LOGLEVEL_VERBOSE;
break;
case 2: /* debug */
__loglevel = LOGLEVEL_DEBUG;
break;
case 'h':
//usage(argv[0]);
exit(0);
default:
return false;
};
}
return true;
}
int main(int argc, char **argv)
{
if (!parse_opt(argc, argv)) {
ERROR("failed to parse command line options");
return 1;
}
if (!run()) {
return 1;
}
return 0;
}