1 /*
2 * Copyright © 2012 Ran Benita <ran234@gmail.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <dirent.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <fnmatch.h>
28 #include <limits.h>
29 #include <locale.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <unistd.h>
33
34 #include <sys/epoll.h>
35 #include <linux/input.h>
36
37 #include "test.h"
38
39 struct keyboard {
40 char *path;
41 int fd;
42 struct xkb_state *state;
43 struct xkb_compose_state *compose_state;
44 struct keyboard *next;
45 };
46
47 static bool terminate;
48 static int evdev_offset = 8;
49 static bool report_state_changes;
50 static bool with_compose;
51
52 #define NLONGS(n) (((n) + LONG_BIT - 1) / LONG_BIT)
53
54 static bool
evdev_bit_is_set(const unsigned long * array,int bit)55 evdev_bit_is_set(const unsigned long *array, int bit)
56 {
57 return array[bit / LONG_BIT] & (1LL << (bit % LONG_BIT));
58 }
59
60 /* Some heuristics to see if the device is a keyboard. */
61 static bool
is_keyboard(int fd)62 is_keyboard(int fd)
63 {
64 int i;
65 unsigned long evbits[NLONGS(EV_CNT)] = { 0 };
66 unsigned long keybits[NLONGS(KEY_CNT)] = { 0 };
67
68 errno = 0;
69 ioctl(fd, EVIOCGBIT(0, sizeof(evbits)), evbits);
70 if (errno)
71 return false;
72
73 if (!evdev_bit_is_set(evbits, EV_KEY))
74 return false;
75
76 errno = 0;
77 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
78 if (errno)
79 return false;
80
81 for (i = KEY_RESERVED; i <= KEY_MIN_INTERESTING; i++)
82 if (evdev_bit_is_set(keybits, i))
83 return true;
84
85 return false;
86 }
87
88 static int
keyboard_new(struct dirent * ent,struct xkb_keymap * keymap,struct xkb_compose_table * compose_table,struct keyboard ** out)89 keyboard_new(struct dirent *ent, struct xkb_keymap *keymap,
90 struct xkb_compose_table *compose_table, struct keyboard **out)
91 {
92 int ret;
93 char *path;
94 int fd;
95 struct xkb_state *state;
96 struct xkb_compose_state *compose_state = NULL;
97 struct keyboard *kbd;
98
99 ret = asprintf(&path, "/dev/input/%s", ent->d_name);
100 if (ret < 0)
101 return -ENOMEM;
102
103 fd = open(path, O_NONBLOCK | O_CLOEXEC | O_RDONLY);
104 if (fd < 0) {
105 ret = -errno;
106 goto err_path;
107 }
108
109 if (!is_keyboard(fd)) {
110 /* Dummy "skip this device" value. */
111 ret = -ENOTSUP;
112 goto err_fd;
113 }
114
115 state = xkb_state_new(keymap);
116 if (!state) {
117 fprintf(stderr, "Couldn't create xkb state for %s\n", path);
118 ret = -EFAULT;
119 goto err_fd;
120 }
121
122 if (with_compose) {
123 compose_state = xkb_compose_state_new(compose_table,
124 XKB_COMPOSE_STATE_NO_FLAGS);
125 if (!compose_state) {
126 fprintf(stderr, "Couldn't create compose state for %s\n", path);
127 ret = -EFAULT;
128 goto err_state;
129 }
130 }
131
132 kbd = calloc(1, sizeof(*kbd));
133 if (!kbd) {
134 ret = -ENOMEM;
135 goto err_compose_state;
136 }
137
138 kbd->path = path;
139 kbd->fd = fd;
140 kbd->state = state;
141 kbd->compose_state = compose_state;
142 *out = kbd;
143 return 0;
144
145 err_compose_state:
146 xkb_compose_state_unref(compose_state);
147 err_state:
148 xkb_state_unref(state);
149 err_fd:
150 close(fd);
151 err_path:
152 free(path);
153 return ret;
154 }
155
156 static void
keyboard_free(struct keyboard * kbd)157 keyboard_free(struct keyboard *kbd)
158 {
159 if (!kbd)
160 return;
161 if (kbd->fd >= 0)
162 close(kbd->fd);
163 free(kbd->path);
164 xkb_state_unref(kbd->state);
165 xkb_compose_state_unref(kbd->compose_state);
166 free(kbd);
167 }
168
169 static int
filter_device_name(const struct dirent * ent)170 filter_device_name(const struct dirent *ent)
171 {
172 return !fnmatch("event*", ent->d_name, 0);
173 }
174
175 static struct keyboard *
get_keyboards(struct xkb_keymap * keymap,struct xkb_compose_table * compose_table)176 get_keyboards(struct xkb_keymap *keymap,
177 struct xkb_compose_table *compose_table)
178 {
179 int ret, i, nents;
180 struct dirent **ents;
181 struct keyboard *kbds = NULL, *kbd = NULL;
182
183 nents = scandir("/dev/input", &ents, filter_device_name, alphasort);
184 if (nents < 0) {
185 fprintf(stderr, "Couldn't scan /dev/input: %s\n", strerror(errno));
186 return NULL;
187 }
188
189 for (i = 0; i < nents; i++) {
190 ret = keyboard_new(ents[i], keymap, compose_table, &kbd);
191 if (ret) {
192 if (ret == -EACCES) {
193 fprintf(stderr, "Couldn't open /dev/input/%s: %s. "
194 "You probably need root to run this.\n",
195 ents[i]->d_name, strerror(-ret));
196 break;
197 }
198 if (ret != -ENOTSUP) {
199 fprintf(stderr, "Couldn't open /dev/input/%s: %s. Skipping.\n",
200 ents[i]->d_name, strerror(-ret));
201 }
202 continue;
203 }
204
205 kbd->next = kbds;
206 kbds = kbd;
207 }
208
209 if (!kbds) {
210 fprintf(stderr, "Couldn't find any keyboards I can use! Quitting.\n");
211 goto err;
212 }
213
214 err:
215 for (i = 0; i < nents; i++)
216 free(ents[i]);
217 free(ents);
218 return kbds;
219 }
220
221 static void
free_keyboards(struct keyboard * kbds)222 free_keyboards(struct keyboard *kbds)
223 {
224 struct keyboard *next;
225
226 while (kbds) {
227 next = kbds->next;
228 keyboard_free(kbds);
229 kbds = next;
230 }
231 }
232
233 /* The meaning of the input_event 'value' field. */
234 enum {
235 KEY_STATE_RELEASE = 0,
236 KEY_STATE_PRESS = 1,
237 KEY_STATE_REPEAT = 2,
238 };
239
240 static void
process_event(struct keyboard * kbd,uint16_t type,uint16_t code,int32_t value)241 process_event(struct keyboard *kbd, uint16_t type, uint16_t code, int32_t value)
242 {
243 xkb_keycode_t keycode;
244 struct xkb_keymap *keymap;
245 enum xkb_state_component changed;
246 enum xkb_compose_status status;
247
248 if (type != EV_KEY)
249 return;
250
251 keycode = evdev_offset + code;
252 keymap = xkb_state_get_keymap(kbd->state);
253
254 if (value == KEY_STATE_REPEAT && !xkb_keymap_key_repeats(keymap, keycode))
255 return;
256
257 if (with_compose && value != KEY_STATE_RELEASE) {
258 xkb_keysym_t keysym = xkb_state_key_get_one_sym(kbd->state, keycode);
259 xkb_compose_state_feed(kbd->compose_state, keysym);
260 }
261
262 if (value != KEY_STATE_RELEASE)
263 test_print_keycode_state(kbd->state, kbd->compose_state, keycode);
264
265 if (with_compose) {
266 status = xkb_compose_state_get_status(kbd->compose_state);
267 if (status == XKB_COMPOSE_CANCELLED || status == XKB_COMPOSE_COMPOSED)
268 xkb_compose_state_reset(kbd->compose_state);
269 }
270
271 if (value == KEY_STATE_RELEASE)
272 changed = xkb_state_update_key(kbd->state, keycode, XKB_KEY_UP);
273 else
274 changed = xkb_state_update_key(kbd->state, keycode, XKB_KEY_DOWN);
275
276 if (report_state_changes)
277 test_print_state_changes(changed);
278 }
279
280 static int
read_keyboard(struct keyboard * kbd)281 read_keyboard(struct keyboard *kbd)
282 {
283 ssize_t len;
284 struct input_event evs[16];
285
286 /* No fancy error checking here. */
287 while ((len = read(kbd->fd, &evs, sizeof(evs))) > 0) {
288 const size_t nevs = len / sizeof(struct input_event);
289 for (size_t i = 0; i < nevs; i++)
290 process_event(kbd, evs[i].type, evs[i].code, evs[i].value);
291 }
292
293 if (len < 0 && errno != EWOULDBLOCK) {
294 fprintf(stderr, "Couldn't read %s: %s\n", kbd->path, strerror(errno));
295 return -errno;
296 }
297
298 return 0;
299 }
300
301 static int
loop(struct keyboard * kbds)302 loop(struct keyboard *kbds)
303 {
304 int i, ret;
305 int epfd;
306 struct keyboard *kbd;
307 struct epoll_event ev;
308 struct epoll_event evs[16];
309
310 epfd = epoll_create1(0);
311 if (epfd < 0) {
312 fprintf(stderr, "Couldn't create epoll instance: %s\n",
313 strerror(errno));
314 return -errno;
315 }
316
317 for (kbd = kbds; kbd; kbd = kbd->next) {
318 memset(&ev, 0, sizeof(ev));
319 ev.events = EPOLLIN;
320 ev.data.ptr = kbd;
321 ret = epoll_ctl(epfd, EPOLL_CTL_ADD, kbd->fd, &ev);
322 if (ret) {
323 ret = -errno;
324 fprintf(stderr, "Couldn't add %s to epoll: %s\n",
325 kbd->path, strerror(errno));
326 goto err_epoll;
327 }
328 }
329
330 while (!terminate) {
331 ret = epoll_wait(epfd, evs, 16, -1);
332 if (ret < 0) {
333 if (errno == EINTR)
334 continue;
335 ret = -errno;
336 fprintf(stderr, "Couldn't poll for events: %s\n",
337 strerror(errno));
338 goto err_epoll;
339 }
340
341 for (i = 0; i < ret; i++) {
342 kbd = evs[i].data.ptr;
343 ret = read_keyboard(kbd);
344 if (ret) {
345 goto err_epoll;
346 }
347 }
348 }
349
350 close(epfd);
351 return 0;
352
353 err_epoll:
354 close(epfd);
355 return ret;
356 }
357
358 static void
sigintr_handler(int signum)359 sigintr_handler(int signum)
360 {
361 terminate = true;
362 }
363
364 int
main(int argc,char * argv[])365 main(int argc, char *argv[])
366 {
367 int ret;
368 int opt;
369 struct keyboard *kbds;
370 struct xkb_context *ctx;
371 struct xkb_keymap *keymap;
372 struct xkb_compose_table *compose_table = NULL;
373 const char *rules = NULL;
374 const char *model = NULL;
375 const char *layout = NULL;
376 const char *variant = NULL;
377 const char *options = NULL;
378 const char *keymap_path = NULL;
379 const char *locale;
380 struct sigaction act;
381
382 setlocale(LC_ALL, "");
383
384 while ((opt = getopt(argc, argv, "r:m:l:v:o:k:n:cd")) != -1) {
385 switch (opt) {
386 case 'r':
387 rules = optarg;
388 break;
389 case 'm':
390 model = optarg;
391 break;
392 case 'l':
393 layout = optarg;
394 break;
395 case 'v':
396 variant = optarg;
397 break;
398 case 'o':
399 options = optarg;
400 break;
401 case 'k':
402 keymap_path = optarg;
403 break;
404 case 'n':
405 errno = 0;
406 evdev_offset = strtol(optarg, NULL, 10);
407 if (errno) {
408 fprintf(stderr, "error: -n option expects a number\n");
409 exit(EXIT_FAILURE);
410 }
411 break;
412 case 'c':
413 report_state_changes = true;
414 break;
415 case 'd':
416 with_compose = true;
417 break;
418 case '?':
419 fprintf(stderr, " Usage: %s [-r <rules>] [-m <model>] "
420 "[-l <layout>] [-v <variant>] [-o <options>]\n",
421 argv[0]);
422 fprintf(stderr, " or: %s -k <path to keymap file>\n",
423 argv[0]);
424 fprintf(stderr, "For both: -n <evdev keycode offset>\n"
425 " -c (to report changes to the state)\n"
426 " -d (to enable compose)\n");
427 exit(2);
428 }
429 }
430
431 ctx = test_get_context(0);
432 if (!ctx) {
433 ret = -1;
434 fprintf(stderr, "Couldn't create xkb context\n");
435 goto err_out;
436 }
437
438 if (keymap_path) {
439 FILE *file = fopen(keymap_path, "r");
440 if (!file) {
441 ret = EXIT_FAILURE;
442 fprintf(stderr, "Couldn't open '%s': %s\n",
443 keymap_path, strerror(errno));
444 goto err_ctx;
445 }
446 keymap = xkb_keymap_new_from_file(ctx, file,
447 XKB_KEYMAP_FORMAT_TEXT_V1, 0);
448 fclose(file);
449 }
450 else {
451 keymap = test_compile_rules(ctx, rules, model, layout, variant,
452 options);
453 }
454
455 if (!keymap) {
456 ret = -1;
457 fprintf(stderr, "Couldn't create xkb keymap\n");
458 goto err_ctx;
459 }
460
461 if (with_compose) {
462 locale = setlocale(LC_CTYPE, NULL);
463 compose_table =
464 xkb_compose_table_new_from_locale(ctx, locale,
465 XKB_COMPOSE_COMPILE_NO_FLAGS);
466 if (!compose_table) {
467 ret = -1;
468 fprintf(stderr, "Couldn't create compose from locale\n");
469 goto err_xkb;
470 }
471 }
472
473 kbds = get_keyboards(keymap, compose_table);
474 if (!kbds) {
475 ret = -1;
476 goto err_compose;
477 }
478
479 act.sa_handler = sigintr_handler;
480 sigemptyset(&act.sa_mask);
481 act.sa_flags = 0;
482 sigaction(SIGINT, &act, NULL);
483 sigaction(SIGTERM, &act, NULL);
484
485 /* Instead of fiddling with termios.. */
486 system("stty -echo");
487
488 ret = loop(kbds);
489 if (ret)
490 goto err_stty;
491
492 err_stty:
493 system("stty echo");
494 free_keyboards(kbds);
495 err_compose:
496 xkb_compose_table_unref(compose_table);
497 err_xkb:
498 xkb_keymap_unref(keymap);
499 err_ctx:
500 xkb_context_unref(ctx);
501 err_out:
502 exit(ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
503 }
504