-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplitdev.c
118 lines (101 loc) · 3.11 KB
/
splitdev.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
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <libevdev/libevdev-uinput.h>
#include <unistd.h>
void fatal(const char *msg)
{
perror(msg);
exit(errno);
}
struct libevdev * open_input_device(const char *iname)
{
int fd = open(iname, O_RDONLY);
if (fd == -1)
fatal("Open input");
struct libevdev *dev = NULL;
int rc = libevdev_new_from_fd(fd, &dev);
if (rc < 0)
fatal("Input evdev");
if (libevdev_grab(dev, LIBEVDEV_GRAB))
fatal("Grab");
return dev;
}
struct libevdev_uinput * create_keyboard()
{
struct libevdev *dev = libevdev_new();
libevdev_set_name(dev, "Split Keyboard");
libevdev_set_id_vendor(dev, 0x1235);
libevdev_set_id_product(dev, 0x9876);
libevdev_enable_event_type(dev, EV_KEY);
for (int i=0; i < 256; ++i)
libevdev_enable_event_code(dev, EV_KEY, i, NULL);
struct libevdev_uinput *uidev;
int err = libevdev_uinput_create_from_device(dev,
LIBEVDEV_UINPUT_OPEN_MANAGED,
&uidev);
if (err != 0)
fatal("Create keyboard");
return uidev;
}
struct libevdev_uinput * create_mouse()
{
struct libevdev *dev = libevdev_new();
libevdev_set_name(dev, "Split Mouse");
libevdev_set_id_vendor(dev, 0x1235);
libevdev_set_id_product(dev, 0x6789);
libevdev_enable_event_type(dev, EV_REL);
libevdev_enable_event_code(dev, EV_REL, REL_X, NULL);
libevdev_enable_event_code(dev, EV_REL, REL_Y, NULL);
libevdev_enable_event_code(dev, EV_REL, REL_WHEEL, NULL);
libevdev_enable_event_code(dev, EV_REL, REL_WHEEL_HI_RES, NULL);
libevdev_enable_event_type(dev, EV_KEY);
libevdev_enable_event_code(dev, EV_KEY, BTN_LEFT, NULL);
libevdev_enable_event_code(dev, EV_KEY, BTN_MIDDLE, NULL);
libevdev_enable_event_code(dev, EV_KEY, BTN_RIGHT, NULL);
struct libevdev_uinput *uidev;
int err = libevdev_uinput_create_from_device(dev,
LIBEVDEV_UINPUT_OPEN_MANAGED,
&uidev);
if (err != 0)
fatal("Create mouse");
return uidev;
}
int main(int argc, char *argv[])
{
const char *iname = "/dev/input/by-id/usb-Logitech_USB_Receiver-if02-event-mouse";
if (argc > 2) {
fprintf(stderr, "Usage: splitdev [device path]\n");
exit(-1);
} else if (argc == 2) {
iname = argv[1];
}
struct libevdev *dev = open_input_device(iname);
struct libevdev_uinput *mouse = create_mouse();
struct libevdev_uinput *keyboard = create_keyboard();
unsigned int flags = LIBEVDEV_READ_FLAG_NORMAL|LIBEVDEV_READ_FLAG_BLOCKING;
for (;;) {
struct input_event ev;
int rc = libevdev_next_event(dev, flags, &ev);
if (rc != LIBEVDEV_READ_STATUS_SUCCESS) {
continue;
}
if (ev.type == EV_REL) {
libevdev_uinput_write_event(mouse, ev.type, ev.code, ev.value);
libevdev_uinput_write_event(mouse, 0,0,0);
} else if (ev.type == EV_KEY) {
if (ev.code == BTN_LEFT || ev.code == BTN_RIGHT || ev.code == BTN_MIDDLE) {
libevdev_uinput_write_event(mouse, ev.type, ev.code, ev.value);
libevdev_uinput_write_event(mouse, 0,0,0);
if (ev.value == 0) usleep(100000);
} else {
libevdev_uinput_write_event(keyboard, ev.type, ev.code, ev.value);
libevdev_uinput_write_event(keyboard, 0,0,0);
}
}
}
libevdev_uinput_destroy(mouse);
libevdev_uinput_destroy(keyboard);
return 0;
}