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;
50 #define NLONGS(n) (((n) + LONG_BIT - 1) / LONG_BIT)
53 evdev_bit_is_set(const unsigned long *array, int bit)
55 return !!(array[bit / LONG_BIT] & (1LL << (bit % LONG_BIT)));
58 /* Some heuristics to see if the device is a keyboard. */
63 unsigned long evbits[NLONGS(EV_CNT)] = { 0 };
64 unsigned long keybits[NLONGS(KEY_CNT)] = { 0 };
67 ioctl(fd, EVIOCGBIT(0, sizeof(evbits)), evbits);
71 if (!evdev_bit_is_set(evbits, EV_KEY))
75 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
79 for (i = KEY_RESERVED; i <= KEY_MIN_INTERESTING; i++)
80 if (evdev_bit_is_set(keybits, i))
87 keyboard_new(struct dirent *ent, struct xkb_keymap *xkb, struct keyboard **out)
92 struct xkb_state *state;
95 ret = asprintf(&path, "/dev/input/%s", ent->d_name);
99 fd = open(path, O_NONBLOCK | O_CLOEXEC | O_RDONLY);
105 if (!is_keyboard(fd)) {
106 /* Dummy "skip this device" value. */
111 state = xkb_state_new(xkb);
113 fprintf(stderr, "Couldn't create xkb state for %s\n", path);
118 kbd = calloc(1, sizeof(*kbd));
131 xkb_state_unref(state);
140 keyboard_free(struct keyboard *kbd)
147 xkb_state_unref(kbd->state);
152 filter_device_name(const struct dirent *ent)
154 return !fnmatch("event*", ent->d_name, 0);
157 static struct keyboard *
158 get_keyboards(struct xkb_keymap *keymap)
161 struct dirent **ents;
162 struct keyboard *kbds = NULL, *kbd = NULL;
164 nents = scandir("/dev/input", &ents, filter_device_name, alphasort);
166 fprintf(stderr, "Couldn't scan /dev/input: %s\n", strerror(errno));
170 for (i = 0; i < nents; i++) {
171 ret = keyboard_new(ents[i], keymap, &kbd);
173 if (ret == -EACCES) {
174 fprintf(stderr, "Couldn't open /dev/input/%s: %s. "
175 "You probably need root to run this.\n",
176 ents[i]->d_name, strerror(-ret));
179 if (ret != -ENOTSUP) {
180 fprintf(stderr, "Couldn't open /dev/input/%s: %s. Skipping.\n",
181 ents[i]->d_name, strerror(-ret));
191 fprintf(stderr, "Couldn't find any keyboards I can use! Quitting.\n");
196 for (i = 0; i < nents; i++)
203 free_keyboards(struct keyboard *kbds)
205 struct keyboard *next;
215 print_keycode(struct keyboard *kbd, xkb_keycode_t keycode)
218 struct xkb_keymap *keymap;
219 struct xkb_state *state;
221 const xkb_keysym_t *syms;
225 xkb_layout_index_t group;
230 keymap = xkb_state_get_keymap(state);
232 nsyms = xkb_state_key_get_syms(state, keycode, &syms);
237 printf("keysyms [ ");
238 for (i = 0; i < nsyms; i++) {
239 xkb_keysym_get_name(syms[i], s, sizeof(s));
240 printf("%-*s ", (int)sizeof(s), s);
245 * Only do this if wchar_t is UCS-4, so we can be lazy and print
248 #ifdef __STDC_ISO_10646__
249 printf("unicode [ ");
250 for (i = 0; i < nsyms; i++) {
251 unicode = xkb_keysym_to_utf32(syms[i]);
252 printf("%lc ", (int)(unicode ? unicode : L' '));
258 for (group = 0; group < xkb_keymap_num_layouts(keymap); group++) {
259 if (!xkb_state_layout_index_is_active(state, group, XKB_STATE_EFFECTIVE))
261 printf("%s (%d) ", xkb_keymap_layout_get_name(keymap, group), group);
266 for (mod = 0; mod < xkb_keymap_num_mods(keymap); mod++) {
267 if (!xkb_state_mod_index_is_active(state, mod, XKB_STATE_EFFECTIVE))
269 if (xkb_state_mod_index_is_consumed(state, keycode, mod))
270 printf("-%s ", xkb_keymap_mod_get_name(keymap, mod));
272 printf("%s ", xkb_keymap_mod_get_name(keymap, mod));
277 for (led = 0; led < xkb_keymap_num_leds(keymap); led++) {
278 if (!xkb_state_led_index_is_active(state, led))
280 printf("%s ", xkb_keymap_led_get_name(keymap, led));
287 /* The meaning of the input_event 'value' field. */
289 KEY_STATE_RELEASE = 0,
291 KEY_STATE_REPEAT = 2,
295 process_event(struct keyboard *kbd, uint16_t type, uint16_t code, int32_t value)
297 xkb_keycode_t keycode;
298 struct xkb_keymap *keymap;
303 keycode = evdev_offset + code;
304 keymap = xkb_state_get_keymap(kbd->state);
306 if (value == KEY_STATE_REPEAT && !xkb_keymap_key_repeats(keymap, keycode))
309 if (value != KEY_STATE_RELEASE)
310 print_keycode(kbd, keycode);
312 if (value == KEY_STATE_RELEASE)
313 xkb_state_update_key(kbd->state, keycode, XKB_KEY_UP);
315 xkb_state_update_key(kbd->state, keycode, XKB_KEY_DOWN);
319 read_keyboard(struct keyboard *kbd)
323 struct input_event evs[16];
326 /* No fancy error checking here. */
327 while ((len = read(kbd->fd, &evs, sizeof(evs))) > 0) {
328 nevs = len / sizeof(struct input_event);
329 for (i = 0; i < nevs; i++)
330 process_event(kbd, evs[i].type, evs[i].code, evs[i].value);
333 if (len < 0 && errno != EWOULDBLOCK) {
334 fprintf(stderr, "Couldn't read %s: %s\n", kbd->path,
343 loop(struct keyboard *kbds)
347 struct keyboard *kbd;
348 struct epoll_event ev;
349 struct epoll_event evs[16];
351 epfd = epoll_create1(0);
353 fprintf(stderr, "Couldn't create epoll instance: %s\n",
358 for (kbd = kbds; kbd; kbd = kbd->next) {
359 memset(&ev, 0, sizeof(ev));
362 ret = epoll_ctl(epfd, EPOLL_CTL_ADD, kbd->fd, &ev);
365 fprintf(stderr, "Couldn't add %s to epoll: %s\n",
366 kbd->path, strerror(errno));
372 ret = epoll_wait(epfd, evs, 16, -1);
377 fprintf(stderr, "Couldn't poll for events: %s\n",
382 for (i = 0; i < ret; i++) {
383 kbd = evs[i].data.ptr;
384 ret = read_keyboard(kbd);
400 sigintr_handler(int signum)
406 main(int argc, char *argv[])
410 struct keyboard *kbds;
411 struct xkb_context *ctx;
412 struct xkb_keymap *keymap;
413 const char *rules = NULL;
414 const char *model = NULL;
415 const char *layout = NULL;
416 const char *variant = NULL;
417 const char *options = NULL;
418 const char *keymap_path = NULL;
419 struct sigaction act;
421 setlocale(LC_ALL, "");
423 while ((opt = getopt(argc, argv, "r:m:l:v:o:k:n:")) != -1) {
441 keymap_path = optarg;
445 evdev_offset = strtol(optarg, NULL, 10);
447 fprintf(stderr, "error: -n option expects a number\n");
452 fprintf(stderr, "Usage: %s [-r <rules>] [-m <model>] "
453 "[-l <layout>] [-v <variant>] [-o <options>]\n",
455 fprintf(stderr, " or: %s -k <path to keymap file>\n",
457 fprintf(stderr, "For both: -n <evdev keycode offset>\n");
462 ctx = test_get_context();
465 fprintf(stderr, "Couldn't create xkb context\n");
470 keymap = test_compile_file(ctx, keymap_path);
472 keymap = test_compile_rules(ctx, rules, model, layout, variant,
476 fprintf(stderr, "Couldn't create xkb keymap\n");
480 kbds = get_keyboards(keymap);
486 act.sa_handler = sigintr_handler;
487 sigemptyset(&act.sa_mask);
489 sigaction(SIGINT, &act, NULL);
490 sigaction(SIGTERM, &act, NULL);
492 /* Instead of fiddling with termios.. */
493 system("stty -echo");
501 free_keyboards(kbds);
503 xkb_keymap_unref(keymap);
505 xkb_context_unref(ctx);
507 exit(ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);