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