test/interactive: allow to set evdev offset
[platform/upstream/libxkbcommon.git] / test / interactive.c
1 /*
2  * Copyright © 2012 Ran Benita <ran234@gmail.com>
3  *
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:
10  *
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
13  * Software.
14  *
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.
22  */
23
24 #include <dirent.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <fnmatch.h>
28 #include <limits.h>
29 #include <locale.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <sysexits.h>
33 #include <unistd.h>
34
35 #include <sys/epoll.h>
36 #include <linux/input.h>
37
38 #include "test.h"
39
40 struct keyboard {
41     char *path;
42     int fd;
43     struct xkb_state *state;
44     struct keyboard *next;
45 };
46
47 static bool terminate;
48 static int evdev_offset = 8;
49
50 #define NLONGS(n) (((n) + LONG_BIT - 1) / LONG_BIT)
51
52 static bool
53 evdev_bit_is_set(const unsigned long *array, int bit)
54 {
55     return !!(array[bit / LONG_BIT] & (1LL << (bit % LONG_BIT)));
56 }
57
58 /* Some heuristics to see if the device is a keyboard. */
59 static bool
60 is_keyboard(int fd)
61 {
62     int i;
63     unsigned long evbits[NLONGS(EV_CNT)] = { 0 };
64     unsigned long keybits[NLONGS(KEY_CNT)] = { 0 };
65
66     errno = 0;
67     ioctl(fd, EVIOCGBIT(0, sizeof(evbits)), evbits);
68     if (errno)
69         return false;
70
71     if (!evdev_bit_is_set(evbits, EV_KEY))
72         return false;
73
74     errno = 0;
75     ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits);
76     if (errno)
77         return false;
78
79     for (i = KEY_RESERVED; i <= KEY_MIN_INTERESTING; i++)
80         if (evdev_bit_is_set(keybits, i))
81             return true;
82
83     return false;
84 }
85
86 static int
87 keyboard_new(struct dirent *ent, struct xkb_keymap *xkb, struct keyboard **out)
88 {
89     int ret;
90     char *path;
91     int fd;
92     struct xkb_state *state;
93     struct keyboard *kbd;
94
95     ret = asprintf(&path, "/dev/input/%s", ent->d_name);
96     if (ret < 0)
97         return -ENOMEM;
98
99     fd = open(path, O_NONBLOCK | O_CLOEXEC | O_RDONLY);
100     if (fd < 0) {
101         ret = -errno;
102         goto err_path;
103     }
104
105     if (!is_keyboard(fd)) {
106         /* Dummy "skip this device" value. */
107         ret = -ENOTSUP;
108         goto err_fd;
109     }
110
111     state = xkb_state_new(xkb);
112     if (!state) {
113         fprintf(stderr, "Couldn't create xkb state for %s\n", path);
114         ret = -EFAULT;
115         goto err_fd;
116     }
117
118     kbd = calloc(1, sizeof(*kbd));
119     if (!kbd) {
120         ret = -ENOMEM;
121         goto err_state;
122     }
123
124     kbd->path = path;
125     kbd->fd = fd;
126     kbd->state = state;
127     *out = kbd;
128     return 0;
129
130 err_state:
131     xkb_state_unref(state);
132 err_fd:
133     close(fd);
134 err_path:
135     free(path);
136     return ret;
137 }
138
139 static void
140 keyboard_free(struct keyboard *kbd)
141 {
142     if (!kbd)
143         return;
144     if (kbd->fd >= 0)
145         close(kbd->fd);
146     free(kbd->path);
147     xkb_state_unref(kbd->state);
148     free(kbd);
149 }
150
151 static int
152 filter_device_name(const struct dirent *ent)
153 {
154     return !fnmatch("event*", ent->d_name, 0);
155 }
156
157 static struct keyboard *
158 get_keyboards(struct xkb_keymap *keymap)
159 {
160     int ret, i, nents;
161     struct dirent **ents;
162     struct keyboard *kbds = NULL, *kbd = NULL;
163
164     nents = scandir("/dev/input", &ents, filter_device_name, alphasort);
165     if (nents < 0) {
166         fprintf(stderr, "Couldn't scan /dev/input: %s\n", strerror(errno));
167         return NULL;
168     }
169
170     for (i = 0; i < nents; i++) {
171         ret = keyboard_new(ents[i], keymap, &kbd);
172         if (ret) {
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));
177                 break;
178             }
179             if (ret != -ENOTSUP) {
180                 fprintf(stderr, "Couldn't open /dev/input/%s: %s. Skipping.\n",
181                         ents[i]->d_name, strerror(-ret));
182             }
183             continue;
184         }
185
186         kbd->next = kbds;
187         kbds = kbd;
188     }
189
190     if (!kbds) {
191         fprintf(stderr, "Couldn't find any keyboards I can use! Quitting.\n");
192         goto err;
193     }
194
195 err:
196     for (i = 0; i < nents; i++)
197         free(ents[i]);
198     free(ents);
199     return kbds;
200 }
201
202 static void
203 free_keyboards(struct keyboard *kbds)
204 {
205     struct keyboard *next;
206
207     while (kbds) {
208         next = kbds->next;
209         keyboard_free(kbds);
210         kbds = next;
211     }
212 }
213
214 static void
215 print_keycode(struct keyboard *kbd, xkb_keycode_t keycode)
216 {
217     unsigned int i;
218     struct xkb_keymap *keymap;
219     struct xkb_state *state;
220
221     const xkb_keysym_t *syms;
222     unsigned int nsyms;
223     char s[16];
224     uint32_t unicode;
225     xkb_layout_index_t group;
226     xkb_mod_index_t mod;
227     xkb_led_index_t led;
228
229     state = kbd->state;
230     keymap = xkb_state_get_keymap(state);
231
232     nsyms = xkb_state_key_get_syms(state, keycode, &syms);
233
234     if (nsyms <= 0)
235         return;
236
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);
241     }
242     printf("] ");
243
244     /*
245      * Only do this if wchar_t is UCS-4, so we can be lazy and print
246      * with %lc.
247      */
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' '));
253     }
254     printf("] ");
255 #endif
256
257     printf("groups [ ");
258     for (group = 0; group < xkb_keymap_num_layouts(keymap); group++) {
259         if (!xkb_state_layout_index_is_active(state, group, XKB_STATE_EFFECTIVE))
260             continue;
261         printf("%s (%d) ", xkb_keymap_layout_get_name(keymap, group), group);
262     }
263     printf("] ");
264
265     printf("mods [ ");
266     for (mod = 0; mod < xkb_keymap_num_mods(keymap); mod++) {
267         if (!xkb_state_mod_index_is_active(state, mod, XKB_STATE_EFFECTIVE))
268             continue;
269         if (xkb_state_mod_index_is_consumed(state, keycode, mod))
270             printf("-%s ", xkb_keymap_mod_get_name(keymap, mod));
271         else
272             printf("%s ", xkb_keymap_mod_get_name(keymap, mod));
273     }
274     printf("] ");
275
276     printf("leds [ ");
277     for (led = 0; led < xkb_keymap_num_leds(keymap); led++) {
278         if (!xkb_state_led_index_is_active(state, led))
279             continue;
280         printf("%s ", xkb_keymap_led_get_name(keymap, led));
281     }
282     printf("] ");
283
284     printf("\n");
285 }
286
287 /* The meaning of the input_event 'value' field. */
288 enum {
289     KEY_STATE_RELEASE = 0,
290     KEY_STATE_PRESS = 1,
291     KEY_STATE_REPEAT = 2,
292 };
293
294 static void
295 process_event(struct keyboard *kbd, uint16_t type, uint16_t code, int32_t value)
296 {
297     xkb_keycode_t keycode;
298     struct xkb_keymap *keymap;
299
300     if (type != EV_KEY)
301         return;
302
303     keycode = evdev_offset + code;
304     keymap = xkb_state_get_keymap(kbd->state);
305
306     if (value == KEY_STATE_REPEAT && !xkb_keymap_key_repeats(keymap, keycode))
307         return;
308
309     if (value != KEY_STATE_RELEASE)
310         print_keycode(kbd, keycode);
311
312     if (value == KEY_STATE_RELEASE)
313         xkb_state_update_key(kbd->state, keycode, XKB_KEY_UP);
314     else
315         xkb_state_update_key(kbd->state, keycode, XKB_KEY_DOWN);
316 }
317
318 static int
319 read_keyboard(struct keyboard *kbd)
320 {
321     size_t i;
322     ssize_t len;
323     struct input_event evs[16];
324     size_t nevs;
325
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);
331     }
332
333     if (len < 0 && errno != EWOULDBLOCK) {
334         fprintf(stderr, "Couldn't read %s: %s\n", kbd->path,
335                 strerror(errno));
336         return -errno;
337     }
338
339     return 0;
340 }
341
342 static int
343 loop(struct keyboard *kbds)
344 {
345     int i, ret;
346     int epfd;
347     struct keyboard *kbd;
348     struct epoll_event ev;
349     struct epoll_event evs[16];
350
351     epfd = epoll_create1(0);
352     if (epfd < 0) {
353         fprintf(stderr, "Couldn't create epoll instance: %s\n",
354                 strerror(errno));
355         return -errno;
356     }
357
358     for (kbd = kbds; kbd; kbd = kbd->next) {
359         memset(&ev, 0, sizeof(ev));
360         ev.events = EPOLLIN;
361         ev.data.ptr = kbd;
362         ret = epoll_ctl(epfd, EPOLL_CTL_ADD, kbd->fd, &ev);
363         if (ret) {
364             ret = -errno;
365             fprintf(stderr, "Couldn't add %s to epoll: %s\n",
366                     kbd->path, strerror(errno));
367             goto err_epoll;
368         }
369     }
370
371     while (!terminate) {
372         ret = epoll_wait(epfd, evs, 16, -1);
373         if (ret < 0) {
374             if (errno == EINTR)
375                 continue;
376             ret = -errno;
377             fprintf(stderr, "Couldn't poll for events: %s\n",
378                     strerror(errno));
379             goto err_epoll;
380         }
381
382         for (i = 0; i < ret; i++) {
383             kbd = evs[i].data.ptr;
384             ret = read_keyboard(kbd);
385             if (ret) {
386                 goto err_epoll;
387             }
388         }
389     }
390
391     close(epfd);
392     return 0;
393
394 err_epoll:
395     close(epfd);
396     return ret;
397 }
398
399 static void
400 sigintr_handler(int signum)
401 {
402     terminate = true;
403 }
404
405 int
406 main(int argc, char *argv[])
407 {
408     int ret;
409     int opt;
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;
420
421     setlocale(LC_ALL, "");
422
423     while ((opt = getopt(argc, argv, "r:m:l:v:o:k:n:")) != -1) {
424         switch (opt) {
425         case 'r':
426             rules = optarg;
427             break;
428         case 'm':
429             model = optarg;
430             break;
431         case 'l':
432             layout = optarg;
433             break;
434         case 'v':
435             variant = optarg;
436             break;
437         case 'o':
438             options = optarg;
439             break;
440         case 'k':
441             keymap_path = optarg;
442             break;
443         case 'n':
444             errno = 0;
445             evdev_offset = strtol(optarg, NULL, 10);
446             if (errno) {
447                 fprintf(stderr, "error: -n option expects a number\n");
448                 exit(EXIT_FAILURE);
449             }
450             break;
451         case '?':
452             fprintf(stderr, "Usage: %s [-r <rules>] [-m <model>] "
453                     "[-l <layout>] [-v <variant>] [-o <options>]\n",
454                     argv[0]);
455             fprintf(stderr, "   or: %s -k <path to keymap file>\n",
456                     argv[0]);
457             fprintf(stderr, "For both: -n <evdev keycode offset>\n");
458             exit(EX_USAGE);
459         }
460     }
461
462     ctx = test_get_context();
463     if (!ctx) {
464         ret = -1;
465         fprintf(stderr, "Couldn't create xkb context\n");
466         goto err_out;
467     }
468
469     if (keymap_path)
470         keymap = test_compile_file(ctx, keymap_path);
471     else
472         keymap = test_compile_rules(ctx, rules, model, layout, variant,
473                                     options);
474     if (!keymap) {
475         ret = -1;
476         fprintf(stderr, "Couldn't create xkb keymap\n");
477         goto err_ctx;
478     }
479
480     kbds = get_keyboards(keymap);
481     if (!kbds) {
482         ret = -1;
483         goto err_xkb;
484     }
485
486     act.sa_handler = sigintr_handler;
487     sigemptyset(&act.sa_mask);
488     act.sa_flags = 0;
489     sigaction(SIGINT, &act, NULL);
490     sigaction(SIGTERM, &act, NULL);
491
492     /* Instead of fiddling with termios.. */
493     system("stty -echo");
494
495     ret = loop(kbds);
496     if (ret)
497         goto err_stty;
498
499 err_stty:
500     system("stty echo");
501     free_keyboards(kbds);
502 err_xkb:
503     xkb_keymap_unref(keymap);
504 err_ctx:
505     xkb_context_unref(ctx);
506 err_out:
507     exit(ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
508 }