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.
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;
61 #define DEFAULT_PRINT_FIELDS PRINT_ALL_FIELDS
62 print_state_fields_mask_t print_fields = DEFAULT_PRINT_FIELDS;
64 #define DEFAULT_INCLUDE_PATH_PLACEHOLDER "__defaults__"
65 #define NLONGS(n) (((n) + LONG_BIT - 1) / LONG_BIT)
68 evdev_bit_is_set(const unsigned long *array, int bit)
70 return array[bit / LONG_BIT] & (1LL << (bit % LONG_BIT));
73 /* Some heuristics to see if the device is a keyboard. */
78 unsigned long evbits[NLONGS(EV_CNT)] = { 0 };
79 unsigned long keybits[NLONGS(KEY_CNT)] = { 0 };
82 ioctl(fd, EVIOCGBIT(0, sizeof(evbits)), evbits);
86 if (!evdev_bit_is_set(evbits, EV_KEY))
90 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
94 for (i = KEY_RESERVED; i <= KEY_MIN_INTERESTING; i++)
95 if (evdev_bit_is_set(keybits, i))
102 keyboard_new(struct dirent *ent, struct xkb_keymap *keymap,
103 struct xkb_compose_table *compose_table, struct keyboard **out)
108 struct xkb_state *state;
109 struct xkb_compose_state *compose_state = NULL;
110 struct keyboard *kbd;
112 ret = asprintf(&path, "/dev/input/%s", ent->d_name);
116 fd = open(path, O_NONBLOCK | O_CLOEXEC | O_RDONLY);
122 if (!is_keyboard(fd)) {
123 /* Dummy "skip this device" value. */
128 state = xkb_state_new(keymap);
130 fprintf(stderr, "Couldn't create xkb state for %s\n", path);
136 compose_state = xkb_compose_state_new(compose_table,
137 XKB_COMPOSE_STATE_NO_FLAGS);
138 if (!compose_state) {
139 fprintf(stderr, "Couldn't create compose state for %s\n", path);
145 kbd = calloc(1, sizeof(*kbd));
148 goto err_compose_state;
154 kbd->compose_state = compose_state;
159 xkb_compose_state_unref(compose_state);
161 xkb_state_unref(state);
170 keyboard_free(struct keyboard *kbd)
177 xkb_state_unref(kbd->state);
178 xkb_compose_state_unref(kbd->compose_state);
183 filter_device_name(const struct dirent *ent)
185 return !fnmatch("event*", ent->d_name, 0);
188 static struct keyboard *
189 get_keyboards(struct xkb_keymap *keymap,
190 struct xkb_compose_table *compose_table)
193 struct dirent **ents;
194 struct keyboard *kbds = NULL, *kbd = NULL;
196 nents = scandir("/dev/input", &ents, filter_device_name, alphasort);
198 fprintf(stderr, "Couldn't scan /dev/input: %s\n", strerror(errno));
202 for (i = 0; i < nents; i++) {
203 ret = keyboard_new(ents[i], keymap, compose_table, &kbd);
205 if (ret == -EACCES) {
206 fprintf(stderr, "Couldn't open /dev/input/%s: %s. "
207 "You probably need root to run this.\n",
208 ents[i]->d_name, strerror(-ret));
211 if (ret != -ENOTSUP) {
212 fprintf(stderr, "Couldn't open /dev/input/%s: %s. Skipping.\n",
213 ents[i]->d_name, strerror(-ret));
224 fprintf(stderr, "Couldn't find any keyboards I can use! Quitting.\n");
229 for (i = 0; i < nents; i++)
236 free_keyboards(struct keyboard *kbds)
238 struct keyboard *next;
247 /* The meaning of the input_event 'value' field. */
249 KEY_STATE_RELEASE = 0,
251 KEY_STATE_REPEAT = 2,
255 process_event(struct keyboard *kbd, uint16_t type, uint16_t code, int32_t value)
257 xkb_keycode_t keycode;
258 struct xkb_keymap *keymap;
259 enum xkb_state_component changed;
260 enum xkb_compose_status status;
265 keycode = evdev_offset + code;
266 keymap = xkb_state_get_keymap(kbd->state);
268 if (value == KEY_STATE_REPEAT && !xkb_keymap_key_repeats(keymap, keycode))
271 if (with_compose && value != KEY_STATE_RELEASE) {
272 xkb_keysym_t keysym = xkb_state_key_get_one_sym(kbd->state, keycode);
273 xkb_compose_state_feed(kbd->compose_state, keysym);
276 if (value != KEY_STATE_RELEASE) {
277 tools_print_keycode_state(
278 kbd->state, kbd->compose_state, keycode,
279 consumed_mode, print_fields
284 status = xkb_compose_state_get_status(kbd->compose_state);
285 if (status == XKB_COMPOSE_CANCELLED || status == XKB_COMPOSE_COMPOSED)
286 xkb_compose_state_reset(kbd->compose_state);
289 if (value == KEY_STATE_RELEASE)
290 changed = xkb_state_update_key(kbd->state, keycode, XKB_KEY_UP);
292 changed = xkb_state_update_key(kbd->state, keycode, XKB_KEY_DOWN);
294 if (report_state_changes)
295 tools_print_state_changes(changed);
299 read_keyboard(struct keyboard *kbd)
302 struct input_event evs[16];
304 /* No fancy error checking here. */
305 while ((len = read(kbd->fd, &evs, sizeof(evs))) > 0) {
306 const size_t nevs = len / sizeof(struct input_event);
307 for (size_t i = 0; i < nevs; i++)
308 process_event(kbd, evs[i].type, evs[i].code, evs[i].value);
311 if (len < 0 && errno != EWOULDBLOCK) {
312 fprintf(stderr, "Couldn't read %s: %s\n", kbd->path, strerror(errno));
320 loop(struct keyboard *kbds)
323 struct keyboard *kbd;
325 struct pollfd *fds = NULL;
327 for (kbd = kbds, nfds = 0; kbd; kbd = kbd->next, nfds++) {}
328 fds = calloc(nfds, sizeof(*fds));
330 fprintf(stderr, "Out of memory");
334 for (i = 0, kbd = kbds; kbd; kbd = kbd->next, i++) {
336 fds[i].events = POLLIN;
340 ret = poll(fds, nfds, -1);
344 fprintf(stderr, "Couldn't poll for events: %s\n",
349 for (i = 0, kbd = kbds; kbd; kbd = kbd->next, i++) {
350 if (fds[i].revents != 0) {
351 ret = read_keyboard(kbd);
366 sigintr_handler(int signum)
372 usage(FILE *fp, char *progname)
374 fprintf(fp, "Usage: %s [--include=<path>] [--include-defaults] "
375 "[--rules=<rules>] [--model=<model>] [--layout=<layout>] "
376 "[--variant=<variant>] [--options=<options>]\n",
378 fprintf(fp, " or: %s --keymap <path to keymap file>\n",
380 fprintf(fp, "For both:\n"
381 " --short (do not print layout nor Unicode keysym translation)\n"
382 " --report-state-changes (report changes to the state)\n"
383 " --enable-compose (enable Compose)\n"
384 " --consumed-mode={xkb|gtk} (select the consumed modifiers mode, default: xkb)\n"
385 " --without-x11-offset (don't add X11 keycode offset)\n"
390 main(int argc, char *argv[])
392 int ret = EXIT_FAILURE;
393 struct keyboard *kbds;
394 struct xkb_context *ctx = NULL;
395 struct xkb_keymap *keymap = NULL;
396 struct xkb_compose_table *compose_table = NULL;
397 const char *includes[64];
398 size_t num_includes = 0;
399 const char *rules = NULL;
400 const char *model = NULL;
401 const char *layout = NULL;
402 const char *variant = NULL;
403 const char *options = NULL;
404 const char *keymap_path = NULL;
406 struct sigaction act;
409 OPT_INCLUDE_DEFAULTS,
416 OPT_WITHOUT_X11_OFFSET,
422 static struct option opts[] = {
423 {"help", no_argument, 0, 'h'},
424 {"include", required_argument, 0, OPT_INCLUDE},
425 {"include-defaults", no_argument, 0, OPT_INCLUDE_DEFAULTS},
426 {"rules", required_argument, 0, OPT_RULES},
427 {"model", required_argument, 0, OPT_MODEL},
428 {"layout", required_argument, 0, OPT_LAYOUT},
429 {"variant", required_argument, 0, OPT_VARIANT},
430 {"options", required_argument, 0, OPT_OPTION},
431 {"keymap", required_argument, 0, OPT_KEYMAP},
432 {"consumed-mode", required_argument, 0, OPT_CONSUMED_MODE},
433 {"enable-compose", no_argument, 0, OPT_COMPOSE},
434 {"short", no_argument, 0, OPT_SHORT},
435 {"report-state-changes", no_argument, 0, OPT_REPORT_STATE},
436 {"without-x11-offset", no_argument, 0, OPT_WITHOUT_X11_OFFSET},
440 setlocale(LC_ALL, "");
444 int option_index = 0;
446 opt = getopt_long(argc, argv, "h", opts, &option_index);
452 if (num_includes >= ARRAY_SIZE(includes)) {
453 fprintf(stderr, "error: too many includes\n");
454 exit(EXIT_INVALID_USAGE);
456 includes[num_includes++] = optarg;
458 case OPT_INCLUDE_DEFAULTS:
459 if (num_includes >= ARRAY_SIZE(includes)) {
460 fprintf(stderr, "error: too many includes\n");
461 exit(EXIT_INVALID_USAGE);
463 includes[num_includes++] = DEFAULT_INCLUDE_PATH_PLACEHOLDER;
481 keymap_path = optarg;
483 case OPT_WITHOUT_X11_OFFSET:
486 case OPT_REPORT_STATE:
487 report_state_changes = true;
493 print_fields &= ~PRINT_VERBOSE_FIELDS;
495 case OPT_CONSUMED_MODE:
496 if (strcmp(optarg, "gtk") == 0) {
497 consumed_mode = XKB_CONSUMED_MODE_GTK;
498 } else if (strcmp(optarg, "xkb") == 0) {
499 consumed_mode = XKB_CONSUMED_MODE_XKB;
501 fprintf(stderr, "error: invalid --consumed-mode \"%s\"\n", optarg);
502 usage(stderr, argv[0]);
503 return EXIT_INVALID_USAGE;
507 usage(stdout, argv[0]);
510 usage(stderr, argv[0]);
511 return EXIT_INVALID_USAGE;
515 ctx = xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES);
517 fprintf(stderr, "Couldn't create xkb context\n");
521 if (num_includes == 0)
522 includes[num_includes++] = DEFAULT_INCLUDE_PATH_PLACEHOLDER;
524 for (size_t i = 0; i < num_includes; i++) {
525 const char *include = includes[i];
526 if (strcmp(include, DEFAULT_INCLUDE_PATH_PLACEHOLDER) == 0)
527 xkb_context_include_path_append_default(ctx);
529 xkb_context_include_path_append(ctx, include);
533 FILE *file = fopen(keymap_path, "rb");
535 fprintf(stderr, "Couldn't open '%s': %s\n",
536 keymap_path, strerror(errno));
539 keymap = xkb_keymap_new_from_file(ctx, file,
540 XKB_KEYMAP_FORMAT_TEXT_V1,
541 XKB_KEYMAP_COMPILE_NO_FLAGS);
545 struct xkb_rule_names rmlvo = {
546 .rules = (rules == NULL || rules[0] == '\0') ? NULL : rules,
547 .model = (model == NULL || model[0] == '\0') ? NULL : model,
548 .layout = (layout == NULL || layout[0] == '\0') ? NULL : layout,
549 .variant = (variant == NULL || variant[0] == '\0') ? NULL : variant,
550 .options = (options == NULL || options[0] == '\0') ? NULL : options
553 if (!rules && !model && !layout && !variant && !options)
554 keymap = xkb_keymap_new_from_names(ctx, NULL, 0);
556 keymap = xkb_keymap_new_from_names(ctx, &rmlvo, 0);
560 "Failed to compile RMLVO: '%s', '%s', '%s', '%s', '%s'\n",
561 rules, model, layout, variant, options);
567 fprintf(stderr, "Couldn't create xkb keymap\n");
572 locale = setlocale(LC_CTYPE, NULL);
574 xkb_compose_table_new_from_locale(ctx, locale,
575 XKB_COMPOSE_COMPILE_NO_FLAGS);
576 if (!compose_table) {
577 fprintf(stderr, "Couldn't create compose from locale\n");
582 kbds = get_keyboards(keymap, compose_table);
587 act.sa_handler = sigintr_handler;
588 sigemptyset(&act.sa_mask);
590 sigaction(SIGINT, &act, NULL);
591 sigaction(SIGTERM, &act, NULL);
593 tools_disable_stdin_echo();
595 tools_enable_stdin_echo();
597 free_keyboards(kbds);
599 xkb_compose_table_unref(compose_table);
600 xkb_keymap_unref(keymap);
601 xkb_context_unref(ctx);