2 * Copyright © 2012 Ran Benita <ran234@gmail.com>
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:
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
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.
35 #include <sys/epoll.h>
36 #include <linux/input.h>
43 struct xkb_state *state;
44 struct keyboard *next;
47 static bool terminate;
48 static int evdev_offset = 8;
49 static bool report_state_changes;
51 #define NLONGS(n) (((n) + LONG_BIT - 1) / LONG_BIT)
54 evdev_bit_is_set(const unsigned long *array, int bit)
56 return !!(array[bit / LONG_BIT] & (1LL << (bit % LONG_BIT)));
59 /* Some heuristics to see if the device is a keyboard. */
64 unsigned long evbits[NLONGS(EV_CNT)] = { 0 };
65 unsigned long keybits[NLONGS(KEY_CNT)] = { 0 };
68 ioctl(fd, EVIOCGBIT(0, sizeof(evbits)), evbits);
72 if (!evdev_bit_is_set(evbits, EV_KEY))
76 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
80 for (i = KEY_RESERVED; i <= KEY_MIN_INTERESTING; i++)
81 if (evdev_bit_is_set(keybits, i))
88 keyboard_new(struct dirent *ent, struct xkb_keymap *keymap,
89 struct keyboard **out)
94 struct xkb_state *state;
97 ret = asprintf(&path, "/dev/input/%s", ent->d_name);
101 fd = open(path, O_NONBLOCK | O_CLOEXEC | O_RDONLY);
107 if (!is_keyboard(fd)) {
108 /* Dummy "skip this device" value. */
113 state = xkb_state_new(keymap);
115 fprintf(stderr, "Couldn't create xkb state for %s\n", path);
120 kbd = calloc(1, sizeof(*kbd));
133 xkb_state_unref(state);
142 keyboard_free(struct keyboard *kbd)
149 xkb_state_unref(kbd->state);
154 filter_device_name(const struct dirent *ent)
156 return !fnmatch("event*", ent->d_name, 0);
159 static struct keyboard *
160 get_keyboards(struct xkb_keymap *keymap)
163 struct dirent **ents;
164 struct keyboard *kbds = NULL, *kbd = NULL;
166 nents = scandir("/dev/input", &ents, filter_device_name, alphasort);
168 fprintf(stderr, "Couldn't scan /dev/input: %s\n", strerror(errno));
172 for (i = 0; i < nents; i++) {
173 ret = keyboard_new(ents[i], keymap, &kbd);
175 if (ret == -EACCES) {
176 fprintf(stderr, "Couldn't open /dev/input/%s: %s. "
177 "You probably need root to run this.\n",
178 ents[i]->d_name, strerror(-ret));
181 if (ret != -ENOTSUP) {
182 fprintf(stderr, "Couldn't open /dev/input/%s: %s. Skipping.\n",
183 ents[i]->d_name, strerror(-ret));
193 fprintf(stderr, "Couldn't find any keyboards I can use! Quitting.\n");
198 for (i = 0; i < nents; i++)
205 free_keyboards(struct keyboard *kbds)
207 struct keyboard *next;
217 print_keycode(struct keyboard *kbd, xkb_keycode_t keycode)
220 struct xkb_keymap *keymap;
221 struct xkb_state *state;
223 const xkb_keysym_t *syms;
227 xkb_layout_index_t layout;
232 keymap = xkb_state_get_keymap(state);
234 nsyms = xkb_state_key_get_syms(state, keycode, &syms);
239 printf("keysyms [ ");
240 for (i = 0; i < nsyms; i++) {
241 xkb_keysym_get_name(syms[i], s, sizeof(s));
242 printf("%-*s ", (int)sizeof(s), s);
247 * Only do this if wchar_t is UCS-4, so we can be lazy and print
250 #ifdef __STDC_ISO_10646__
251 printf("unicode [ ");
252 for (i = 0; i < nsyms; i++) {
253 unicode = xkb_keysym_to_utf32(syms[i]);
254 printf("%lc ", (int)(unicode ? unicode : L' '));
259 layout = xkb_state_key_get_layout(state, keycode);
260 printf("layout [ %s (%d) ] ",
261 xkb_keymap_layout_get_name(keymap, layout), layout);
263 printf("level [ %d ] ",
264 xkb_state_key_get_level(state, keycode, layout));
267 for (mod = 0; mod < xkb_keymap_num_mods(keymap); mod++) {
268 if (xkb_state_mod_index_is_active(state, mod,
269 XKB_STATE_MODS_EFFECTIVE) <= 0)
271 if (xkb_state_mod_index_is_consumed(state, keycode, mod))
272 printf("-%s ", xkb_keymap_mod_get_name(keymap, mod));
274 printf("%s ", xkb_keymap_mod_get_name(keymap, mod));
279 for (led = 0; led < xkb_keymap_num_leds(keymap); led++) {
280 if (xkb_state_led_index_is_active(state, led) <= 0)
282 printf("%s ", xkb_keymap_led_get_name(keymap, led));
290 print_state_changes(enum xkb_state_component changed)
295 printf("changed [ ");
296 if (changed & XKB_STATE_LAYOUT_EFFECTIVE)
297 printf("effective-layout ");
298 if (changed & XKB_STATE_LAYOUT_DEPRESSED)
299 printf("depressed-layout ");
300 if (changed & XKB_STATE_LAYOUT_LATCHED)
301 printf("latched-layout ");
302 if (changed & XKB_STATE_LAYOUT_LOCKED)
303 printf("locked-layout ");
304 if (changed & XKB_STATE_MODS_EFFECTIVE)
305 printf("effective-mods ");
306 if (changed & XKB_STATE_MODS_DEPRESSED)
307 printf("depressed-mods ");
308 if (changed & XKB_STATE_MODS_LATCHED)
309 printf("latched-mods ");
310 if (changed & XKB_STATE_MODS_LOCKED)
311 printf("locked-mods ");
312 if (changed & XKB_STATE_LEDS)
317 /* The meaning of the input_event 'value' field. */
319 KEY_STATE_RELEASE = 0,
321 KEY_STATE_REPEAT = 2,
325 process_event(struct keyboard *kbd, uint16_t type, uint16_t code, int32_t value)
327 xkb_keycode_t keycode;
328 struct xkb_keymap *keymap;
329 enum xkb_state_component changed;
334 keycode = evdev_offset + code;
335 keymap = xkb_state_get_keymap(kbd->state);
337 if (value == KEY_STATE_REPEAT && !xkb_keymap_key_repeats(keymap, keycode))
340 if (value != KEY_STATE_RELEASE)
341 print_keycode(kbd, keycode);
343 if (value == KEY_STATE_RELEASE)
344 changed = xkb_state_update_key(kbd->state, keycode, XKB_KEY_UP);
346 changed = xkb_state_update_key(kbd->state, keycode, XKB_KEY_DOWN);
348 if (report_state_changes)
349 print_state_changes(changed);
353 read_keyboard(struct keyboard *kbd)
357 struct input_event evs[16];
360 /* No fancy error checking here. */
361 while ((len = read(kbd->fd, &evs, sizeof(evs))) > 0) {
362 nevs = len / sizeof(struct input_event);
363 for (i = 0; i < nevs; i++)
364 process_event(kbd, evs[i].type, evs[i].code, evs[i].value);
367 if (len < 0 && errno != EWOULDBLOCK) {
368 fprintf(stderr, "Couldn't read %s: %s\n", kbd->path,
377 loop(struct keyboard *kbds)
381 struct keyboard *kbd;
382 struct epoll_event ev;
383 struct epoll_event evs[16];
385 epfd = epoll_create1(0);
387 fprintf(stderr, "Couldn't create epoll instance: %s\n",
392 for (kbd = kbds; kbd; kbd = kbd->next) {
393 memset(&ev, 0, sizeof(ev));
396 ret = epoll_ctl(epfd, EPOLL_CTL_ADD, kbd->fd, &ev);
399 fprintf(stderr, "Couldn't add %s to epoll: %s\n",
400 kbd->path, strerror(errno));
406 ret = epoll_wait(epfd, evs, 16, -1);
411 fprintf(stderr, "Couldn't poll for events: %s\n",
416 for (i = 0; i < ret; i++) {
417 kbd = evs[i].data.ptr;
418 ret = read_keyboard(kbd);
434 sigintr_handler(int signum)
440 main(int argc, char *argv[])
444 struct keyboard *kbds;
445 struct xkb_context *ctx;
446 struct xkb_keymap *keymap;
447 const char *rules = NULL;
448 const char *model = NULL;
449 const char *layout = NULL;
450 const char *variant = NULL;
451 const char *options = NULL;
452 const char *keymap_path = NULL;
453 struct sigaction act;
455 setlocale(LC_ALL, "");
457 while ((opt = getopt(argc, argv, "r:m:l:v:o:k:n:c")) != -1) {
475 keymap_path = optarg;
479 evdev_offset = strtol(optarg, NULL, 10);
481 fprintf(stderr, "error: -n option expects a number\n");
486 report_state_changes = true;
489 fprintf(stderr, " Usage: %s [-r <rules>] [-m <model>] "
490 "[-l <layout>] [-v <variant>] [-o <options>]\n",
492 fprintf(stderr, " or: %s -k <path to keymap file>\n",
494 fprintf(stderr, "For both: -n <evdev keycode offset>\n"
495 " -c (to report changes to the state)\n");
500 ctx = test_get_context();
503 fprintf(stderr, "Couldn't create xkb context\n");
508 FILE *file = fopen(keymap_path, "r");
511 fprintf(stderr, "Couldn't open '%s': %s\n",
512 keymap_path, strerror(errno));
515 keymap = xkb_keymap_new_from_file(ctx, file,
516 XKB_KEYMAP_FORMAT_TEXT_V1, 0);
520 keymap = test_compile_rules(ctx, rules, model, layout, variant,
526 fprintf(stderr, "Couldn't create xkb keymap\n");
530 kbds = get_keyboards(keymap);
536 act.sa_handler = sigintr_handler;
537 sigemptyset(&act.sa_mask);
539 sigaction(SIGINT, &act, NULL);
540 sigaction(SIGTERM, &act, NULL);
542 /* Instead of fiddling with termios.. */
543 system("stty -echo");
551 free_keyboards(kbds);
553 xkb_keymap_unref(keymap);
555 xkb_context_unref(ctx);
557 exit(ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);