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