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