-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.c
More file actions
351 lines (279 loc) · 10.4 KB
/
main.c
File metadata and controls
351 lines (279 loc) · 10.4 KB
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
#include <linux/input-event-codes.h>
#include <stdio.h>
#include <linux/version.h>
#include <linux/input.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdbool.h>
#include <libudev.h>
#include <poll.h>
#define CHECK_READ(res, buf) \
if (res == -1) { \
perror("read failed."); \
goto exit; \
} \
if (res != sizeof(buf)) { \
fprintf(stderr, "expected to read %d bytes, but readed only %d\n", (int) sizeof(struct input_event), res); \
goto exit; \
} \
#define CHECK_WRITE(res, buf) \
if (res == -1) { \
perror("write failed."); \
goto exit; \
} \
if (res != sizeof(buf)) { \
fprintf(stderr, "expected to write %d bytes, but written only %d\n", (int) sizeof(struct input_event), res); \
goto exit; \
} \
static volatile sig_atomic_t stop = 0;
static void interrupt_handler(int sig)
{
stop = 1;
}
static int constructKeyboard (const char *name, struct input_id *id)
{
struct uinput_user_dev description;
int res = 0;
int fd = 0;
fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
if (fd == -1) {
perror("open /dev/uinput");
goto err;
}
res = ioctl(fd, UI_SET_EVBIT, EV_KEY);
if (res == -1) {
perror("ioctl UI_SET_EVBIT EV_KEY");
goto err;
}
for(int c = 0; c < KEY_MAX; c++) {
res = ioctl(fd, UI_SET_KEYBIT, c);
if (res == -1) {
fprintf(stderr, "ioctl UI_SET_KEYBIT %d\n", c);
goto err;
}
}
memset(&description, 0, sizeof(description));
strncpy(description.name, name, UINPUT_MAX_NAME_SIZE);
memcpy(&description.id, id, sizeof(description.id));
res = ioctl(fd, UI_DEV_SETUP, &description);
if (res == -1) {
perror("ioctl UI_DEV_SETUP");
goto err;
}
res = ioctl(fd, UI_DEV_CREATE);
if (res == -1) {
perror("ioctl UI_DEV_CREATE");
goto err;
}
return fd;
err:
if (fd != -1) {
close(fd);
}
return -1;
}
static void wait_for_usb_connection()
{
struct udev *udev;
struct udev_monitor *mon;
int fd;
bool exit = false;
udev = udev_new();
if (!udev) {
fprintf(stderr, "Can't create udev\n");
sleep(1);
return;
}
mon = udev_monitor_new_from_netlink(udev, "udev");
if (!mon) {
fprintf(stderr, "Can't create udev monitor\n");
udev_unref(udev);
sleep(1);
return;
}
udev_monitor_filter_add_match_subsystem_devtype(mon, "usb", NULL);
udev_monitor_enable_receiving(mon);
fd = udev_monitor_get_fd(mon);
printf("Monitoring USB events. Plug or unplug a USB device...\n");
while (!exit) {
struct pollfd fds[1];
fds[0].fd = fd;
fds[0].events = POLLIN;
int ret = poll(fds, 1, -1);
if (ret > 0 && (fds[0].revents & POLLIN)) {
struct udev_device *dev = udev_monitor_receive_device(mon);
if (dev) {
const char* action = udev_device_get_action(dev);
const char* devpath = udev_device_get_devpath(dev);
const char* vendor_id = udev_device_get_sysattr_value(dev, "idVendor");
const char* model_id = udev_device_get_sysattr_value(dev, "idProduct");
if (action && devpath) {
printf("Action: %s\n", action);
printf("Devpath: %s\n", devpath);
if (vendor_id && model_id) {
printf("VendorID: %s, ProductID: %s\n", vendor_id, model_id);
exit = true;
}
}
udev_device_unref(dev);
}
}
}
udev_monitor_unref(mon);
udev_unref(udev);
}
int main(int argc, char *argv[])
{
int fd = 0;
int uinput = 0;
int rc = 0;
do {
if ((fd = open(argv[1], O_RDONLY)) < 0) {
perror("open failed.");
if (errno == EACCES && getuid() != 0)
fprintf(stderr, "You do not have access to %s. Try running as root instead.\n", argv[1]);
goto exit;
}
if (signal(SIGINT, interrupt_handler) == SIG_ERR) {
perror("signal call for SIGINT failed.");
goto exit;
}
if (signal(SIGTERM, interrupt_handler) == SIG_ERR) {
perror("signal call for SIGTERM failed.");
goto exit;
}
fd_set rdfs;
FD_ZERO(&rdfs);
FD_SET(fd, &rdfs);
struct input_id id;
if (ioctl(fd, EVIOCGID, &id) == -1) {
perror("ioctl for EVIOCGID failed.");
goto exit;
}
if ((id.bustype != 3)||(id.vendor != 1133)||(id.product != 50504)) {
fprintf(stderr, "Wrong keyboard detected!\n");
goto exit;
}
if (id.version != 273) {
printf("WARNING!!! keyboard version 273 expected, but %d detected!\n", id.version);
}
uinput = constructKeyboard("Example device", &id);
if (uinput == -1) {
fprintf(stderr, "constructKeyboard call failed!\n");
goto exit;
}
if (getppid() != 1) {
printf("WARNING!!! Not started as daemon. Will delay 1sec!\n");
sleep(1);
printf("Continuing...\n");
}
if (ioctl(fd, EVIOCGRAB, (void*)1) == -1) {
perror("ioctl for EVIOCGRAB 1 failed.");
goto exit;
}
bool keep_converting = false;
while(!stop)
{
struct input_event ev;
int wr = 0;
int rv = 0;
int rd = 0;
rd = select(fd + 1, &rdfs, NULL, NULL, NULL);
if (stop) {
break;
}
if (rd == -1) {
perror("select failed.");
goto exit;
}
rd = read(fd, &ev, sizeof(ev));
CHECK_READ(rd, ev);
if ((ev.code == KEY_LEFTMETA)&&(ev.type == 1)&&(ev.value == 1)) {
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 50000;
rv = select(fd + 1, &rdfs, NULL, NULL, &timeout);
if (rv == -1) {
perror("select failed.");
goto exit;
}
if (rv > 0) {
rd = read(fd, &ev, sizeof(ev));
CHECK_READ(rd, ev);
rd = read(fd, &ev, sizeof(ev));
CHECK_READ(rd, ev);
rd = read(fd, &ev, sizeof(ev));
CHECK_READ(rd, ev);
if ((ev.code == KEY_SPACE)&&(ev.type == 1)&&(ev.value == 1)) {
ev.code = KEY_INSERT; ev.type = 1; ev.value = 1;
wr = write(uinput, &ev, sizeof(ev));
CHECK_WRITE(wr, ev);
ev.code = 0; ev.type = 0; ev.value = 0;
wr = write(uinput, &ev, sizeof(ev));
CHECK_WRITE(wr, ev);
ev.code = KEY_INSERT; ev.type = 1; ev.value = 0;
wr = write(uinput, &ev, sizeof(ev));
CHECK_WRITE(wr, ev);
ev.code = 0; ev.type = 0; ev.value = 0;
wr = write(uinput, &ev, sizeof(ev));
CHECK_WRITE(wr, ev);
rd = read(fd, &ev, sizeof(ev));
CHECK_READ(rd, ev);
rd = read(fd, &ev, sizeof(ev));
CHECK_READ(rd, ev);
rd = read(fd, &ev, sizeof(ev));
CHECK_READ(rd, ev);
rd = read(fd, &ev, sizeof(ev));
CHECK_READ(rd, ev);
keep_converting = true;
}
}
}
if ((keep_converting)&&(ev.code == KEY_LEFTMETA)&&(ev.type == 1)&&(ev.value == 0)) {
keep_converting = false;
}
if ((keep_converting)&&(ev.code == KEY_SPACE)) {
ev.code = KEY_INSERT;
}
wr = write(uinput, &ev, sizeof(ev));
CHECK_WRITE(wr, ev);
}
rc = ioctl(fd, EVIOCGRAB, (void*)0);
if (rc == -1) {
perror("ioctl for EVIOCGRAB 0 failed.");
goto exit;
}
exit:
if (uinput > 0) {
close(uinput);
uinput = 0;
}
if (fd > 0) {
close(fd);
fd = 0;
}
if (!stop) {
wait_for_usb_connection();
}
} while(!stop);
return 0;
}