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.
38 #include <sys/epoll.h>
39 #include <linux/input.h>
41 #include "xkbcommon/xkbcommon.h"
47 struct xkb_state *state;
48 struct keyboard *next;
53 #define NLONGS(n) (((n) + LONG_BIT - 1) / LONG_BIT)
56 evdev_bit_is_set(const unsigned long *array, int bit)
58 return !!(array[bit / LONG_BIT] & (1LL << (bit % LONG_BIT)));
61 /* Some heuristics to see if the device is a keyboard. */
66 unsigned long evbits[NLONGS(EV_CNT)] = { 0 };
67 unsigned long keybits[NLONGS(KEY_CNT)] = { 0 };
70 ioctl(fd, EVIOCGBIT(0, sizeof(evbits)), evbits);
74 if (!evdev_bit_is_set(evbits, EV_KEY))
78 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
82 for (i = KEY_RESERVED; i <= KEY_MIN_INTERESTING; i++)
83 if (evdev_bit_is_set(keybits, i))
90 keyboard_new(struct dirent *ent, struct xkb_keymap *xkb, struct keyboard **out)
95 struct xkb_state *state;
98 ret = asprintf(&path, "/dev/input/%s", ent->d_name);
102 fd = open(path, O_NONBLOCK | O_CLOEXEC | O_RDONLY);
108 if (!is_keyboard(fd)) {
109 /* Dummy "skip this device" value. */
114 state = xkb_state_new(xkb);
116 fprintf(stderr, "Couldn't create xkb state for %s\n", path);
121 kbd = calloc(1, sizeof(*kbd));
134 xkb_state_unref(state);
143 keyboard_free(struct keyboard *kbd)
150 xkb_state_unref(kbd->state);
155 filter_device_name(const struct dirent *ent)
157 return !fnmatch("event*", ent->d_name, 0);
160 static struct keyboard *
161 get_keyboards(struct xkb_keymap *keymap)
164 struct dirent **ents;
165 struct keyboard *kbds = NULL, *kbd = NULL;
167 nents = scandir("/dev/input", &ents, filter_device_name, alphasort);
169 fprintf(stderr, "Couldn't scan /dev/input: %s\n", strerror(errno));
173 for (i = 0; i < nents; i++) {
174 ret = keyboard_new(ents[i], keymap, &kbd);
176 if (ret == -EACCES) {
177 fprintf(stderr, "Couldn't open /dev/input/%s: %s. "
178 "You probably need root to run this.\n",
179 ents[i]->d_name, strerror(-ret));
182 if (ret != -ENOTSUP) {
183 fprintf(stderr, "Couldn't open /dev/input/%s: %s. Skipping.\n",
184 ents[i]->d_name, strerror(-ret));
194 fprintf(stderr, "Couldn't find any keyboards I can use! Quitting.\n");
199 for (i = 0; i < nents; i++)
206 free_keyboards(struct keyboard *kbds)
208 struct keyboard *next;
218 print_keycode(struct keyboard *kbd, xkb_keycode_t keycode)
221 struct xkb_keymap *keymap;
222 struct xkb_state *state;
224 const xkb_keysym_t *syms;
228 xkb_group_index_t group;
233 keymap = xkb_state_get_map(state);
235 nsyms = xkb_key_get_syms(state, keycode, &syms);
240 printf("keysyms [ ");
241 for (i = 0; i < nsyms; i++) {
242 xkb_keysym_get_name(syms[i], s, sizeof(s));
243 printf("%-*s ", (int)sizeof(s), s);
248 * Only do this if wchar_t is UCS-4, so we can be lazy and print
251 #ifdef __STDC_ISO_10646__
252 printf("unicode [ ");
253 for (i = 0; i < nsyms; i++) {
254 unicode = xkb_keysym_to_utf32(syms[i]);
255 printf("%lc ", (int)(unicode ? unicode : L' '));
261 for (group = 0; group < xkb_map_num_groups(keymap); group++) {
262 if (!xkb_state_group_index_is_active(state, group, XKB_STATE_EFFECTIVE))
264 printf("%s (%d) ", xkb_map_group_get_name(keymap, group), group);
269 for (mod = 0; mod < xkb_map_num_mods(keymap); mod++) {
270 if (!xkb_state_mod_index_is_active(state, mod, XKB_STATE_EFFECTIVE))
272 if (xkb_key_mod_index_is_consumed(state, keycode, mod))
273 printf("-%s ", xkb_map_mod_get_name(keymap, mod));
275 printf("%s ", xkb_map_mod_get_name(keymap, mod));
280 for (led = 0; led < xkb_map_num_leds(keymap); led++) {
281 if (!xkb_state_led_index_is_active(state, led))
283 printf("%s ", xkb_map_led_get_name(keymap, led));
290 /* The meaning of the input_event 'value' field. */
292 KEY_STATE_RELEASE = 0,
294 KEY_STATE_REPEAT = 2,
297 #define EVDEV_OFFSET 8
300 process_event(struct keyboard *kbd, uint16_t type, uint16_t code, int32_t value)
302 xkb_keycode_t keycode;
303 struct xkb_keymap *keymap;
308 keycode = EVDEV_OFFSET + code;
309 keymap = xkb_state_get_map(kbd->state);
311 if (value == KEY_STATE_REPEAT && !xkb_key_repeats(keymap, keycode))
314 if (value != KEY_STATE_RELEASE)
315 print_keycode(kbd, keycode);
317 if (value == KEY_STATE_RELEASE)
318 xkb_state_update_key(kbd->state, keycode, XKB_KEY_UP);
320 xkb_state_update_key(kbd->state, keycode, XKB_KEY_DOWN);
324 read_keyboard(struct keyboard *kbd)
328 struct input_event evs[16];
331 /* No fancy error checking here. */
332 while ((len = read(kbd->fd, &evs, sizeof(evs))) > 0) {
333 nevs = len / sizeof(struct input_event);
334 for (i = 0; i < nevs; i++)
335 process_event(kbd, evs[i].type, evs[i].code, evs[i].value);
338 if (len < 0 && errno != EWOULDBLOCK) {
339 fprintf(stderr, "Couldn't read %s: %s\n", kbd->path,
348 loop(struct keyboard *kbds)
352 struct keyboard *kbd;
353 struct epoll_event ev;
354 struct epoll_event evs[16];
356 epfd = epoll_create1(0);
358 fprintf(stderr, "Couldn't create epoll instance: %s\n",
363 for (kbd = kbds; kbd; kbd = kbd->next) {
364 memset(&ev, 0, sizeof(ev));
367 ret = epoll_ctl(epfd, EPOLL_CTL_ADD, kbd->fd, &ev);
370 fprintf(stderr, "Couldn't add %s to epoll: %s\n",
371 kbd->path, strerror(errno));
377 ret = epoll_wait(epfd, evs, 16, -1);
382 fprintf(stderr, "Couldn't poll for events: %s\n",
387 for (i = 0; i < ret; i++) {
388 kbd = evs[i].data.ptr;
389 ret = read_keyboard(kbd);
405 sigintr_handler(int signum)
411 main(int argc, char *argv[])
415 struct keyboard *kbds;
416 struct xkb_context *ctx;
417 struct xkb_keymap *keymap;
418 const char *rules = NULL;
419 const char *model = NULL;
420 const char *layout = NULL;
421 const char *variant = NULL;
422 const char *options = NULL;
423 const char *keymap_path = NULL;
424 struct sigaction act;
426 setlocale(LC_ALL, "");
428 while ((opt = getopt(argc, argv, "r:m:l:v:o:k:")) != -1) {
446 keymap_path = optarg;
449 fprintf(stderr, "Usage: %s [-r <rules>] [-m <model>] "
450 "[-l <layout>] [-v <variant>] [-o <options>]\n",
452 fprintf(stderr, " or: %s -k <path to keymap file>\n",
458 ctx = test_get_context();
461 fprintf(stderr, "Couldn't create xkb context\n");
466 keymap = test_compile_file(ctx, keymap_path);
468 keymap = test_compile_rules(ctx, rules, model, layout, variant,
472 fprintf(stderr, "Couldn't create xkb keymap\n");
476 kbds = get_keyboards(keymap);
482 act.sa_handler = sigintr_handler;
483 sigemptyset(&act.sa_mask);
485 sigaction(SIGINT, &act, NULL);
486 sigaction(SIGTERM, &act, NULL);
488 /* Instead of fiddling with termios.. */
489 system("stty -echo");
497 free_keyboards(kbds);
499 xkb_map_unref(keymap);
501 xkb_context_unref(ctx);
503 exit(ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);