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