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"
46 struct xkb_state *state;
47 struct keyboard *next;
52 #define NLONGS(n) (((n) + LONG_BIT - 1) / LONG_BIT)
55 evdev_bit_is_set(const unsigned long *array, int bit)
57 return !!(array[bit / LONG_BIT] & (1LL << (bit % LONG_BIT)));
60 /* Some heuristics to see if the device is a keyboard. */
65 unsigned long evbits[NLONGS(EV_CNT)] = { 0 };
66 unsigned long keybits[NLONGS(KEY_CNT)] = { 0 };
69 ioctl(fd, EVIOCGBIT(0, sizeof(evbits)), evbits);
73 if (!evdev_bit_is_set(evbits, EV_KEY))
77 ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
81 for (i = KEY_RESERVED; i <= KEY_MIN_INTERESTING; i++)
82 if (evdev_bit_is_set(keybits, i))
89 keyboard_new(struct dirent *ent, struct xkb_keymap *xkb, 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(xkb);
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_group_index_t group;
232 keymap = xkb_state_get_map(state);
234 nsyms = xkb_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' '));
260 for (group = 0; group < xkb_map_num_groups(keymap); group++) {
261 if (!xkb_state_group_index_is_active(state, group, XKB_STATE_EFFECTIVE))
263 printf("%s (%d) ", xkb_map_group_get_name(keymap, group), group);
268 for (mod = 0; mod < xkb_map_num_mods(keymap); mod++) {
269 if (!xkb_state_mod_index_is_active(state, mod, XKB_STATE_EFFECTIVE))
271 printf("%s ", xkb_map_mod_get_name(keymap, mod));
276 for (led = 0; led < xkb_map_num_leds(keymap); led++) {
277 if (!xkb_state_led_index_is_active(state, led))
279 printf("%s ", xkb_map_led_get_name(keymap, led));
286 /* The meaning of the input_event 'value' field. */
288 KEY_STATE_RELEASE = 0,
290 KEY_STATE_REPEAT = 2,
293 #define EVDEV_OFFSET 8
296 process_event(struct keyboard *kbd, uint16_t type, uint16_t code, int32_t value)
298 xkb_keycode_t keycode;
299 struct xkb_keymap *keymap;
304 keycode = EVDEV_OFFSET + code;
305 keymap = xkb_state_get_map(kbd->state);
307 if (value == KEY_STATE_REPEAT && !xkb_key_repeats(keymap, keycode))
310 if (value != KEY_STATE_RELEASE)
311 print_keycode(kbd, keycode);
313 if (value == KEY_STATE_RELEASE)
314 xkb_state_update_key(kbd->state, keycode, XKB_KEY_UP);
316 xkb_state_update_key(kbd->state, keycode, XKB_KEY_DOWN);
320 read_keyboard(struct keyboard *kbd)
324 struct input_event evs[16];
327 /* No fancy error checking here. */
328 while ((len = read(kbd->fd, &evs, sizeof(evs))) > 0) {
329 nevs = len / sizeof(struct input_event);
330 for (i = 0; i < nevs; i++)
331 process_event(kbd, evs[i].type, evs[i].code, evs[i].value);
334 if (len < 0 && errno != EWOULDBLOCK) {
335 fprintf(stderr, "Couldn't read %s: %s\n", kbd->path,
344 loop(struct keyboard *kbds)
348 struct keyboard *kbd;
349 struct epoll_event ev;
350 struct epoll_event evs[16];
352 epfd = epoll_create1(0);
354 fprintf(stderr, "Couldn't create epoll instance: %s\n",
359 for (kbd = kbds; kbd; kbd = kbd->next) {
360 memset(&ev, 0, sizeof(ev));
363 ret = epoll_ctl(epfd, EPOLL_CTL_ADD, kbd->fd, &ev);
366 fprintf(stderr, "Couldn't add %s to epoll: %s\n",
367 kbd->path, strerror(errno));
373 ret = epoll_wait(epfd, evs, 16, -1);
378 fprintf(stderr, "Couldn't poll for events: %s\n",
383 for (i = 0; i < ret; i++) {
384 kbd = evs[i].data.ptr;
385 ret = read_keyboard(kbd);
401 sigintr_handler(int signum)
407 main(int argc, char *argv[])
411 struct keyboard *kbds;
412 struct xkb_context *ctx;
413 struct xkb_keymap *keymap;
414 struct xkb_rule_names names = {
421 const char *keymap_path = NULL;
423 struct sigaction act;
425 setlocale(LC_ALL, "");
427 while ((opt = getopt(argc, argv, "r:m:l:v:o:k:")) != -1) {
430 names.rules = optarg;
433 names.model = optarg;
436 names.layout = optarg;
439 names.variant = optarg;
442 names.options = optarg;
445 keymap_path = optarg;
448 fprintf(stderr, "Usage: %s [-r <rules>] [-m <model>] "
449 "[-l <layout>] [-v <variant>] [-o <options>]\n",
451 fprintf(stderr, " or: %s -k <path to keymap file>\n",
457 ctx = xkb_context_new(0);
460 fprintf(stderr, "Couldn't create xkb context\n");
465 file = fopen(keymap_path, "r");
467 fprintf(stderr, "Couldn't open file %s: %s\n",
468 keymap_path, strerror(errno));
471 keymap = xkb_map_new_from_file(ctx, file,
472 XKB_KEYMAP_FORMAT_TEXT_V1, 0);
474 keymap = xkb_map_new_from_names(ctx, &names, 0);
478 fprintf(stderr, "Couldn't create xkb keymap\n");
482 kbds = get_keyboards(keymap);
488 act.sa_handler = sigintr_handler;
489 sigemptyset(&act.sa_mask);
491 sigaction(SIGINT, &act, NULL);
492 sigaction(SIGTERM, &act, NULL);
494 /* Instead of fiddling with termios.. */
495 system("stty -echo");
504 free_keyboards(kbds);
506 xkb_map_unref(keymap);
508 xkb_context_unref(ctx);
510 exit(ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);