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