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.
41 #include <sys/epoll.h>
42 #include <linux/input.h>
44 #include "xkbcommon/xkbcommon.h"
46 #include "tools-common.h"
51 struct xkb_state *state;
52 struct xkb_compose_state *compose_state;
53 struct keyboard *next;
56 static bool terminate;
57 static int evdev_offset = 8;
58 static bool report_state_changes;
59 static bool with_compose;
60 static enum xkb_consumed_mode consumed_mode = XKB_CONSUMED_MODE_XKB;
62 #define NLONGS(n) (((n) + LONG_BIT - 1) / LONG_BIT)
65 evdev_bit_is_set(const unsigned long *array, int bit)
67 return array[bit / LONG_BIT] & (1LL << (bit % LONG_BIT));
70 /* Some heuristics to see if the device is a keyboard. */
75 unsigned long evbits[NLONGS(EV_CNT)] = { 0 };
76 unsigned long keybits[NLONGS(KEY_CNT)] = { 0 };
79 ioctl(fd, EVIOCGBIT(0, sizeof(evbits)), evbits);
83 if (!evdev_bit_is_set(evbits, EV_KEY))
87 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
91 for (i = KEY_RESERVED; i <= KEY_MIN_INTERESTING; i++)
92 if (evdev_bit_is_set(keybits, i))
99 keyboard_new(struct dirent *ent, struct xkb_keymap *keymap,
100 struct xkb_compose_table *compose_table, struct keyboard **out)
105 struct xkb_state *state;
106 struct xkb_compose_state *compose_state = NULL;
107 struct keyboard *kbd;
109 ret = asprintf(&path, "/dev/input/%s", ent->d_name);
113 fd = open(path, O_NONBLOCK | O_CLOEXEC | O_RDONLY);
119 if (!is_keyboard(fd)) {
120 /* Dummy "skip this device" value. */
125 state = xkb_state_new(keymap);
127 fprintf(stderr, "Couldn't create xkb state for %s\n", path);
133 compose_state = xkb_compose_state_new(compose_table,
134 XKB_COMPOSE_STATE_NO_FLAGS);
135 if (!compose_state) {
136 fprintf(stderr, "Couldn't create compose state for %s\n", path);
142 kbd = calloc(1, sizeof(*kbd));
145 goto err_compose_state;
151 kbd->compose_state = compose_state;
156 xkb_compose_state_unref(compose_state);
158 xkb_state_unref(state);
167 keyboard_free(struct keyboard *kbd)
174 xkb_state_unref(kbd->state);
175 xkb_compose_state_unref(kbd->compose_state);
180 filter_device_name(const struct dirent *ent)
182 return !fnmatch("event*", ent->d_name, 0);
185 static struct keyboard *
186 get_keyboards(struct xkb_keymap *keymap,
187 struct xkb_compose_table *compose_table)
190 struct dirent **ents;
191 struct keyboard *kbds = NULL, *kbd = NULL;
193 nents = scandir("/dev/input", &ents, filter_device_name, alphasort);
195 fprintf(stderr, "Couldn't scan /dev/input: %s\n", strerror(errno));
199 for (i = 0; i < nents; i++) {
200 ret = keyboard_new(ents[i], keymap, compose_table, &kbd);
202 if (ret == -EACCES) {
203 fprintf(stderr, "Couldn't open /dev/input/%s: %s. "
204 "You probably need root to run this.\n",
205 ents[i]->d_name, strerror(-ret));
208 if (ret != -ENOTSUP) {
209 fprintf(stderr, "Couldn't open /dev/input/%s: %s. Skipping.\n",
210 ents[i]->d_name, strerror(-ret));
221 fprintf(stderr, "Couldn't find any keyboards I can use! Quitting.\n");
226 for (i = 0; i < nents; i++)
233 free_keyboards(struct keyboard *kbds)
235 struct keyboard *next;
244 /* The meaning of the input_event 'value' field. */
246 KEY_STATE_RELEASE = 0,
248 KEY_STATE_REPEAT = 2,
252 process_event(struct keyboard *kbd, uint16_t type, uint16_t code, int32_t value)
254 xkb_keycode_t keycode;
255 struct xkb_keymap *keymap;
256 enum xkb_state_component changed;
257 enum xkb_compose_status status;
262 keycode = evdev_offset + code;
263 keymap = xkb_state_get_keymap(kbd->state);
265 if (value == KEY_STATE_REPEAT && !xkb_keymap_key_repeats(keymap, keycode))
268 if (with_compose && value != KEY_STATE_RELEASE) {
269 xkb_keysym_t keysym = xkb_state_key_get_one_sym(kbd->state, keycode);
270 xkb_compose_state_feed(kbd->compose_state, keysym);
273 if (value != KEY_STATE_RELEASE)
274 tools_print_keycode_state(kbd->state, kbd->compose_state, keycode,
278 status = xkb_compose_state_get_status(kbd->compose_state);
279 if (status == XKB_COMPOSE_CANCELLED || status == XKB_COMPOSE_COMPOSED)
280 xkb_compose_state_reset(kbd->compose_state);
283 if (value == KEY_STATE_RELEASE)
284 changed = xkb_state_update_key(kbd->state, keycode, XKB_KEY_UP);
286 changed = xkb_state_update_key(kbd->state, keycode, XKB_KEY_DOWN);
288 if (report_state_changes)
289 tools_print_state_changes(changed);
293 read_keyboard(struct keyboard *kbd)
296 struct input_event evs[16];
298 /* No fancy error checking here. */
299 while ((len = read(kbd->fd, &evs, sizeof(evs))) > 0) {
300 const size_t nevs = len / sizeof(struct input_event);
301 for (size_t i = 0; i < nevs; i++)
302 process_event(kbd, evs[i].type, evs[i].code, evs[i].value);
305 if (len < 0 && errno != EWOULDBLOCK) {
306 fprintf(stderr, "Couldn't read %s: %s\n", kbd->path, strerror(errno));
314 loop(struct keyboard *kbds)
318 struct keyboard *kbd;
319 struct epoll_event ev;
320 struct epoll_event evs[16];
322 epfd = epoll_create1(0);
324 fprintf(stderr, "Couldn't create epoll instance: %s\n",
329 for (kbd = kbds; kbd; kbd = kbd->next) {
330 memset(&ev, 0, sizeof(ev));
333 ret = epoll_ctl(epfd, EPOLL_CTL_ADD, kbd->fd, &ev);
335 fprintf(stderr, "Couldn't add %s to epoll: %s\n",
336 kbd->path, strerror(errno));
342 ret = epoll_wait(epfd, evs, 16, -1);
346 fprintf(stderr, "Couldn't poll for events: %s\n",
351 for (i = 0; i < ret; i++) {
352 kbd = evs[i].data.ptr;
353 ret = read_keyboard(kbd);
367 sigintr_handler(int signum)
373 usage(FILE *fp, char *progname)
375 fprintf(fp, "Usage: %s [--rules <rules>] [--model <model>] "
376 "[--layout <layout>] [--variant <variant>] [--options <options>]\n",
378 fprintf(fp, " or: %s --keymap <path to keymap file>\n",
380 fprintf(fp, "For both:\n"
381 " --report-state-changes (report changes to the state)\n"
382 " --enable-compose (enable compose)\n"
383 " --consumed-mode={gtk|xkb} (select the consumed mode)\n");
387 main(int argc, char *argv[])
389 int ret = EXIT_FAILURE;
390 struct keyboard *kbds;
391 struct xkb_context *ctx = NULL;
392 struct xkb_keymap *keymap = NULL;
393 struct xkb_compose_table *compose_table = NULL;
394 const char *rules = NULL;
395 const char *model = NULL;
396 const char *layout = NULL;
397 const char *variant = NULL;
398 const char *options = NULL;
399 const char *keymap_path = NULL;
401 struct sigaction act;
413 static struct option opts[] = {
414 {"help", no_argument, 0, 'h'},
415 {"rules", required_argument, 0, OPT_RULES},
416 {"model", required_argument, 0, OPT_MODEL},
417 {"layout", required_argument, 0, OPT_LAYOUT},
418 {"variant", required_argument, 0, OPT_VARIANT},
419 {"options", required_argument, 0, OPT_OPTION},
420 {"keymap", required_argument, 0, OPT_KEYMAP},
421 {"consumed-mode", required_argument, 0, OPT_CONSUMED_MODE},
422 {"enable-compose", no_argument, 0, OPT_COMPOSE},
423 {"report-state-changes", no_argument, 0, OPT_REPORT_STATE},
427 setlocale(LC_ALL, "");
431 int option_index = 0;
433 opt = getopt_long(argc, argv, "h", opts, &option_index);
454 keymap_path = optarg;
456 case OPT_REPORT_STATE:
457 report_state_changes = true;
462 case OPT_CONSUMED_MODE:
463 if (strcmp(optarg, "gtk") == 0) {
464 consumed_mode = XKB_CONSUMED_MODE_GTK;
465 } else if (strcmp(optarg, "xkb") == 0) {
466 consumed_mode = XKB_CONSUMED_MODE_XKB;
468 usage(stderr, argv[0]);
469 return EXIT_INVALID_USAGE;
473 usage(stdout, argv[0]);
476 usage(stderr, argv[0]);
477 return EXIT_INVALID_USAGE;
481 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
483 fprintf(stderr, "Couldn't create xkb context\n");
488 FILE *file = fopen(keymap_path, "rb");
490 fprintf(stderr, "Couldn't open '%s': %s\n",
491 keymap_path, strerror(errno));
494 keymap = xkb_keymap_new_from_file(ctx, file,
495 XKB_KEYMAP_FORMAT_TEXT_V1,
496 XKB_KEYMAP_COMPILE_NO_FLAGS);
500 struct xkb_rule_names rmlvo = {
501 .rules = (rules == NULL || rules[0] == '\0') ? NULL : rules,
502 .model = (model == NULL || model[0] == '\0') ? NULL : model,
503 .layout = (layout == NULL || layout[0] == '\0') ? NULL : layout,
504 .variant = (variant == NULL || variant[0] == '\0') ? NULL : variant,
505 .options = (options == NULL || options[0] == '\0') ? NULL : options
508 if (!rules && !model && !layout && !variant && !options)
509 keymap = xkb_keymap_new_from_names(ctx, NULL, 0);
511 keymap = xkb_keymap_new_from_names(ctx, &rmlvo, 0);
515 "Failed to compile RMLVO: '%s', '%s', '%s', '%s', '%s'\n",
516 rules, model, layout, variant, options);
522 fprintf(stderr, "Couldn't create xkb keymap\n");
527 locale = setlocale(LC_CTYPE, NULL);
529 xkb_compose_table_new_from_locale(ctx, locale,
530 XKB_COMPOSE_COMPILE_NO_FLAGS);
531 if (!compose_table) {
532 fprintf(stderr, "Couldn't create compose from locale\n");
537 kbds = get_keyboards(keymap, compose_table);
542 act.sa_handler = sigintr_handler;
543 sigemptyset(&act.sa_mask);
545 sigaction(SIGINT, &act, NULL);
546 sigaction(SIGTERM, &act, NULL);
548 tools_disable_stdin_echo();
550 tools_enable_stdin_echo();
552 free_keyboards(kbds);
554 xkb_compose_table_unref(compose_table);
555 xkb_keymap_unref(keymap);
556 xkb_context_unref(ctx);