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