00db464b293377bc13668323bbac895e03726af6
[platform/upstream/libinput.git] / test / litest.c
1 /*
2  * Copyright © 2013 Red Hat, Inc.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #if HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <assert.h>
28 #include <check.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <getopt.h>
32 #include <poll.h>
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <time.h>
37 #include <unistd.h>
38 #include "linux/input.h"
39 #include <sys/ptrace.h>
40 #include <sys/timerfd.h>
41 #include <sys/wait.h>
42
43 #include "litest.h"
44 #include "litest-int.h"
45 #include "libinput-util.h"
46
47 static int in_debugger = -1;
48 static int verbose = 0;
49
50 struct test {
51         struct list node;
52         char *name;
53         TCase *tc;
54         enum litest_device_type devices;
55 };
56
57 struct suite {
58         struct list node;
59         struct list tests;
60         char *name;
61         Suite *suite;
62 };
63
64 static struct litest_device *current_device;
65
66 struct litest_device *litest_current_device(void) {
67         return current_device;
68 }
69
70 void litest_set_current_device(struct litest_device *device) {
71         current_device = device;
72 }
73
74 void litest_generic_device_teardown(void)
75 {
76         litest_delete_device(current_device);
77         current_device = NULL;
78 }
79
80 extern struct litest_test_device litest_keyboard_device;
81 extern struct litest_test_device litest_synaptics_clickpad_device;
82 extern struct litest_test_device litest_synaptics_touchpad_device;
83 extern struct litest_test_device litest_synaptics_t440_device;
84 extern struct litest_test_device litest_trackpoint_device;
85 extern struct litest_test_device litest_bcm5974_device;
86 extern struct litest_test_device litest_mouse_device;
87 extern struct litest_test_device litest_wacom_touch_device;
88 extern struct litest_test_device litest_alps_device;
89
90 struct litest_test_device* devices[] = {
91         &litest_synaptics_clickpad_device,
92         &litest_synaptics_touchpad_device,
93         &litest_synaptics_t440_device,
94         &litest_keyboard_device,
95         &litest_trackpoint_device,
96         &litest_bcm5974_device,
97         &litest_mouse_device,
98         &litest_wacom_touch_device,
99         &litest_alps_device,
100         NULL,
101 };
102
103 static struct list all_tests;
104
105 static void
106 litest_add_tcase_for_device(struct suite *suite,
107                             void *func,
108                             const struct litest_test_device *dev)
109 {
110         struct test *t;
111         const char *test_name = dev->shortname;
112
113         list_for_each(t, &suite->tests, node) {
114                 if (strcmp(t->name, test_name) != 0)
115                         continue;
116
117                 tcase_add_test(t->tc, func);
118                 return;
119         }
120
121         t = zalloc(sizeof(*t));
122         t->name = strdup(test_name);
123         t->tc = tcase_create(test_name);
124         list_insert(&suite->tests, &t->node);
125         tcase_add_checked_fixture(t->tc, dev->setup,
126                                   dev->teardown ? dev->teardown : litest_generic_device_teardown);
127         tcase_add_test(t->tc, func);
128         suite_add_tcase(suite->suite, t->tc);
129 }
130
131 static void
132 litest_add_tcase_no_device(struct suite *suite, void *func)
133 {
134         struct test *t;
135         const char *test_name = "no device";
136
137         list_for_each(t, &suite->tests, node) {
138                 if (strcmp(t->name, test_name) != 0)
139                         continue;
140
141                 tcase_add_test(t->tc, func);
142                 return;
143         }
144
145         t = zalloc(sizeof(*t));
146         t->name = strdup(test_name);
147         t->tc = tcase_create(test_name);
148         list_insert(&suite->tests, &t->node);
149         tcase_add_test(t->tc, func);
150         suite_add_tcase(suite->suite, t->tc);
151 }
152
153 static void
154 litest_add_tcase(struct suite *suite, void *func,
155                  enum litest_device_feature required,
156                  enum litest_device_feature excluded)
157 {
158         struct litest_test_device **dev = devices;
159
160         if (required == LITEST_DISABLE_DEVICE &&
161             excluded == LITEST_DISABLE_DEVICE) {
162                 litest_add_tcase_no_device(suite, func);
163         } else if (required != LITEST_ANY || excluded != LITEST_ANY) {
164                 while (*dev) {
165                         if (((*dev)->features & required) == required &&
166                             ((*dev)->features & excluded) == 0)
167                                 litest_add_tcase_for_device(suite, func, *dev);
168                         dev++;
169                 }
170         } else {
171                 while (*dev) {
172                         litest_add_tcase_for_device(suite, func, *dev);
173                         dev++;
174                 }
175         }
176 }
177
178 void
179 litest_add_no_device(const char *name, void *func)
180 {
181         litest_add(name, func, LITEST_DISABLE_DEVICE, LITEST_DISABLE_DEVICE);
182 }
183
184 static struct suite *
185 get_suite(const char *name)
186 {
187         struct suite *s;
188
189         if (all_tests.next == NULL && all_tests.prev == NULL)
190                 list_init(&all_tests);
191
192         list_for_each(s, &all_tests, node) {
193                 if (strcmp(s->name, name) == 0)
194                         return s;
195         }
196
197         s = zalloc(sizeof(*s));
198         s->name = strdup(name);
199         s->suite = suite_create(s->name);
200
201         list_init(&s->tests);
202         list_insert(&all_tests, &s->node);
203
204         return s;
205 }
206
207 void
208 litest_add(const char *name,
209            void *func,
210            enum litest_device_feature required,
211            enum litest_device_feature excluded)
212 {
213         litest_add_tcase(get_suite(name), func, required, excluded);
214 }
215
216 void
217 litest_add_for_device(const char *name,
218                       void *func,
219                       enum litest_device_type type)
220 {
221         struct suite *s;
222         struct litest_test_device **dev = devices;
223
224         s = get_suite(name);
225         while (*dev) {
226                 if ((*dev)->type == type) {
227                         litest_add_tcase_for_device(s, func, *dev);
228                         return;
229                 }
230                 dev++;
231         }
232
233         ck_abort_msg("Invalid test device type");
234 }
235
236 static int
237 is_debugger_attached(void)
238 {
239         int status;
240         int rc;
241         int pid = fork();
242
243         if (pid == -1)
244                 return 0;
245
246         if (pid == 0) {
247                 int ppid = getppid();
248                 if (ptrace(PTRACE_ATTACH, ppid, NULL, NULL) == 0) {
249                         waitpid(ppid, NULL, 0);
250                         ptrace(PTRACE_CONT, NULL, NULL);
251                         ptrace(PTRACE_DETACH, ppid, NULL, NULL);
252                         rc = 0;
253                 } else {
254                         rc = 1;
255                 }
256                 _exit(rc);
257         } else {
258                 waitpid(pid, &status, 0);
259                 rc = WEXITSTATUS(status);
260         }
261
262         return rc;
263 }
264
265 static void
266 litest_list_tests(struct list *tests)
267 {
268         struct suite *s;
269
270         list_for_each(s, tests, node) {
271                 struct test *t;
272                 printf("%s:\n", s->name);
273                 list_for_each(t, &s->tests, node) {
274                         printf("        %s\n", t->name);
275                 }
276         }
277 }
278
279 static void
280 litest_log_handler(struct libinput *libinput,
281                    enum libinput_log_priority pri,
282                    const char *format,
283                    va_list args)
284 {
285         const char *priority = NULL;
286
287         switch(pri) {
288         case LIBINPUT_LOG_PRIORITY_INFO: priority = "info"; break;
289         case LIBINPUT_LOG_PRIORITY_ERROR: priority = "error"; break;
290         case LIBINPUT_LOG_PRIORITY_DEBUG: priority = "debug"; break;
291         }
292
293         fprintf(stderr, "litest %s: ", priority);
294         vfprintf(stderr, format, args);
295 }
296
297 static int
298 open_restricted(const char *path, int flags, void *userdata)
299 {
300         return open(path, flags);
301 }
302
303 static void
304 close_restricted(int fd, void *userdata)
305 {
306         close(fd);
307 }
308
309 struct libinput_interface interface = {
310         .open_restricted = open_restricted,
311         .close_restricted = close_restricted,
312 };
313
314 static const struct option opts[] = {
315         { "list", 0, 0, 'l' },
316         { "verbose", 0, 0, 'v' },
317         { 0, 0, 0, 0}
318 };
319
320 int
321 litest_run(int argc, char **argv) {
322         struct suite *s, *snext;
323         int failed;
324         SRunner *sr = NULL;
325
326         if (in_debugger == -1) {
327                 in_debugger = is_debugger_attached();
328                 if (in_debugger)
329                         setenv("CK_FORK", "no", 0);
330         }
331
332         list_for_each(s, &all_tests, node) {
333                 if (!sr)
334                         sr = srunner_create(s->suite);
335                 else
336                         srunner_add_suite(sr, s->suite);
337         }
338
339         while(1) {
340                 int c;
341                 int option_index = 0;
342
343                 c = getopt_long(argc, argv, "", opts, &option_index);
344                 if (c == -1)
345                         break;
346                 switch(c) {
347                         case 'l':
348                                 litest_list_tests(&all_tests);
349                                 return 0;
350                         case 'v':
351                                 verbose = 1;
352                                 break;
353                         default:
354                                 fprintf(stderr, "usage: %s [--list]\n", argv[0]);
355                                 return 1;
356
357                 }
358         }
359
360         srunner_run_all(sr, CK_NORMAL);
361         failed = srunner_ntests_failed(sr);
362         srunner_free(sr);
363
364         list_for_each_safe(s, snext, &all_tests, node) {
365                 struct test *t, *tnext;
366
367                 list_for_each_safe(t, tnext, &s->tests, node) {
368                         free(t->name);
369                         list_remove(&t->node);
370                         free(t);
371                 }
372
373                 list_remove(&s->node);
374                 free(s->name);
375                 free(s);
376         }
377
378         return failed;
379 }
380
381 static struct input_absinfo *
382 merge_absinfo(const struct input_absinfo *orig,
383               const struct input_absinfo *override)
384 {
385         struct input_absinfo *abs;
386         unsigned int nelem, i;
387         size_t sz = ABS_MAX + 1;
388
389         if (!orig)
390                 return NULL;
391
392         abs = calloc(sz, sizeof(*abs));
393         ck_assert(abs != NULL);
394
395         nelem = 0;
396         while (orig[nelem].value != -1) {
397                 abs[nelem] = orig[nelem];
398                 nelem++;
399                 ck_assert_int_lt(nelem, sz);
400         }
401
402         /* just append, if the same axis is present twice, libevdev will
403            only use the last value anyway */
404         i = 0;
405         while (override && override[i].value != -1) {
406                 abs[nelem++] = override[i++];
407                 ck_assert_int_lt(nelem, sz);
408         }
409
410         ck_assert_int_lt(nelem, sz);
411         abs[nelem].value = -1;
412
413         return abs;
414 }
415
416 static int*
417 merge_events(const int *orig, const int *override)
418 {
419         int *events;
420         unsigned int nelem, i;
421         size_t sz = KEY_MAX * 3;
422
423         if (!orig)
424                 return NULL;
425
426         events = calloc(sz, sizeof(int));
427         ck_assert(events != NULL);
428
429         nelem = 0;
430         while (orig[nelem] != -1) {
431                 events[nelem] = orig[nelem];
432                 nelem++;
433                 ck_assert_int_lt(nelem, sz);
434         }
435
436         /* just append, if the same axis is present twice, libevdev will
437          * ignore the double definition anyway */
438         i = 0;
439         while (override && override[i] != -1) {
440                 events[nelem++] = override[i++];
441                 ck_assert_int_le(nelem, sz);
442         }
443
444         ck_assert_int_lt(nelem, sz);
445         events[nelem] = -1;
446
447         return events;
448 }
449
450 static struct litest_device *
451 litest_create(enum litest_device_type which,
452               const char *name_override,
453               struct input_id *id_override,
454               const struct input_absinfo *abs_override,
455               const int *events_override)
456 {
457         struct litest_device *d = NULL;
458         struct litest_test_device **dev;
459         const char *name;
460         const struct input_id *id;
461         struct input_absinfo *abs;
462         int *events;
463
464         dev = devices;
465         while (*dev) {
466                 if ((*dev)->type == which)
467                         break;
468                 dev++;
469         }
470
471         if (!*dev)
472                 ck_abort_msg("Invalid device type %d\n", which);
473
474         d = zalloc(sizeof(*d));
475         ck_assert(d != NULL);
476
477         /* device has custom create method */
478         if ((*dev)->create) {
479                 (*dev)->create(d);
480                 if (abs_override || events_override)
481                         ck_abort_msg("Custom create cannot"
482                                      "be overridden");
483
484                 return d;
485         }
486
487         abs = merge_absinfo((*dev)->absinfo, abs_override);
488         events = merge_events((*dev)->events, events_override);
489         name = name_override ? name_override : (*dev)->name;
490         id = id_override ? id_override : (*dev)->id;
491
492         d->uinput = litest_create_uinput_device_from_description(name,
493                                                                  id,
494                                                                  abs,
495                                                                  events);
496         d->interface = (*dev)->interface;
497         free(abs);
498         free(events);
499
500         return d;
501
502 }
503
504 struct libinput *
505 litest_create_context(void)
506 {
507         struct libinput *libinput =
508                 libinput_path_create_context(&interface, NULL);
509         ck_assert_notnull(libinput);
510
511         libinput_log_set_handler(libinput, litest_log_handler);
512         if (verbose)
513                 libinput_log_set_priority(libinput, LIBINPUT_LOG_PRIORITY_DEBUG);
514
515         return libinput;
516 }
517
518 struct litest_device *
519 litest_add_device_with_overrides(struct libinput *libinput,
520                                  enum litest_device_type which,
521                                  const char *name_override,
522                                  struct input_id *id_override,
523                                  const struct input_absinfo *abs_override,
524                                  const int *events_override)
525 {
526         struct litest_device *d;
527         int fd;
528         int rc;
529         const char *path;
530
531         d = litest_create(which,
532                           name_override,
533                           id_override,
534                           abs_override,
535                           events_override);
536
537         path = libevdev_uinput_get_devnode(d->uinput);
538         ck_assert(path != NULL);
539         fd = open(path, O_RDWR|O_NONBLOCK);
540         ck_assert_int_ne(fd, -1);
541
542         rc = libevdev_new_from_fd(fd, &d->evdev);
543         ck_assert_int_eq(rc, 0);
544
545         d->libinput = libinput;
546         d->libinput_device = libinput_path_add_device(d->libinput, path);
547         ck_assert(d->libinput_device != NULL);
548         libinput_device_ref(d->libinput_device);
549
550         if (d->interface) {
551                 d->interface->min[ABS_X] = libevdev_get_abs_minimum(d->evdev, ABS_X);
552                 d->interface->max[ABS_X] = libevdev_get_abs_maximum(d->evdev, ABS_X);
553                 d->interface->min[ABS_Y] = libevdev_get_abs_minimum(d->evdev, ABS_Y);
554                 d->interface->max[ABS_Y] = libevdev_get_abs_maximum(d->evdev, ABS_Y);
555         }
556         return d;
557 }
558
559 struct litest_device *
560 litest_create_device_with_overrides(enum litest_device_type which,
561                                     const char *name_override,
562                                     struct input_id *id_override,
563                                     const struct input_absinfo *abs_override,
564                                     const int *events_override)
565 {
566         struct litest_device *dev =
567                 litest_add_device_with_overrides(litest_create_context(),
568                                                  which,
569                                                  name_override,
570                                                  id_override,
571                                                  abs_override,
572                                                  events_override);
573         dev->owns_context = true;
574         return dev;
575 }
576
577 struct litest_device *
578 litest_create_device(enum litest_device_type which)
579 {
580         return litest_create_device_with_overrides(which, NULL, NULL, NULL, NULL);
581 }
582
583 int
584 litest_handle_events(struct litest_device *d)
585 {
586         struct pollfd fd;
587
588         fd.fd = libinput_get_fd(d->libinput);
589         fd.events = POLLIN;
590
591         while (poll(&fd, 1, 1))
592                 libinput_dispatch(d->libinput);
593
594         return 0;
595 }
596
597 void
598 litest_delete_device(struct litest_device *d)
599 {
600         if (!d)
601                 return;
602
603         libinput_device_unref(d->libinput_device);
604         libinput_path_remove_device(d->libinput_device);
605         if (d->owns_context)
606                 libinput_unref(d->libinput);
607         libevdev_free(d->evdev);
608         libevdev_uinput_destroy(d->uinput);
609         free(d->private);
610         memset(d,0, sizeof(*d));
611         free(d);
612 }
613
614 void
615 litest_event(struct litest_device *d, unsigned int type,
616              unsigned int code, int value)
617 {
618         int ret = libevdev_uinput_write_event(d->uinput, type, code, value);
619         ck_assert_int_eq(ret, 0);
620 }
621
622 int
623 litest_auto_assign_value(struct litest_device *d,
624                          const struct input_event *ev,
625                          int slot, double x, double y)
626 {
627         static int tracking_id;
628         int value = ev->value;
629
630         if (value != LITEST_AUTO_ASSIGN || ev->type != EV_ABS)
631                 return value;
632
633         switch (ev->code) {
634         case ABS_X:
635         case ABS_MT_POSITION_X:
636                 value = litest_scale(d, ABS_X, x);
637                 break;
638         case ABS_Y:
639         case ABS_MT_POSITION_Y:
640                 value = litest_scale(d, ABS_Y, y);
641                 break;
642         case ABS_MT_TRACKING_ID:
643                 value = ++tracking_id;
644                 break;
645         case ABS_MT_SLOT:
646                 value = slot;
647                 break;
648         }
649
650         return value;
651 }
652
653 static void
654 send_btntool(struct litest_device *d)
655 {
656         litest_event(d, EV_KEY, BTN_TOUCH, d->ntouches_down != 0);
657         litest_event(d, EV_KEY, BTN_TOOL_FINGER, d->ntouches_down == 1);
658         litest_event(d, EV_KEY, BTN_TOOL_DOUBLETAP, d->ntouches_down == 2);
659         litest_event(d, EV_KEY, BTN_TOOL_TRIPLETAP, d->ntouches_down == 3);
660         litest_event(d, EV_KEY, BTN_TOOL_QUADTAP, d->ntouches_down == 4);
661         litest_event(d, EV_KEY, BTN_TOOL_QUINTTAP, d->ntouches_down == 5);
662 }
663
664 void
665 litest_touch_down(struct litest_device *d, unsigned int slot,
666                   double x, double y)
667 {
668         struct input_event *ev;
669
670         assert(++d->ntouches_down > 0);
671
672         send_btntool(d);
673
674         if (d->interface->touch_down) {
675                 d->interface->touch_down(d, slot, x, y);
676                 return;
677         }
678
679         ev = d->interface->touch_down_events;
680         while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
681                 int value = litest_auto_assign_value(d, ev, slot, x, y);
682                 litest_event(d, ev->type, ev->code, value);
683                 ev++;
684         }
685 }
686
687 void
688 litest_touch_up(struct litest_device *d, unsigned int slot)
689 {
690         struct input_event *ev;
691         struct input_event up[] = {
692                 { .type = EV_ABS, .code = ABS_MT_SLOT, .value = LITEST_AUTO_ASSIGN },
693                 { .type = EV_ABS, .code = ABS_MT_TRACKING_ID, .value = -1 },
694                 { .type = EV_SYN, .code = SYN_REPORT, .value = 0 },
695                 { .type = -1, .code = -1 }
696         };
697
698         assert(--d->ntouches_down >= 0);
699
700         send_btntool(d);
701
702         if (d->interface->touch_up) {
703                 d->interface->touch_up(d, slot);
704                 return;
705         } else if (d->interface->touch_up_events) {
706                 ev = d->interface->touch_up_events;
707         } else
708                 ev = up;
709
710         while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
711                 int value = litest_auto_assign_value(d, ev, slot, 0, 0);
712                 litest_event(d, ev->type, ev->code, value);
713                 ev++;
714         }
715 }
716
717 void
718 litest_touch_move(struct litest_device *d, unsigned int slot,
719                   double x, double y)
720 {
721         struct input_event *ev;
722
723         if (d->interface->touch_move) {
724                 d->interface->touch_move(d, slot, x, y);
725                 return;
726         }
727
728         ev = d->interface->touch_move_events;
729         while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
730                 int value = litest_auto_assign_value(d, ev, slot, x, y);
731                 litest_event(d, ev->type, ev->code, value);
732                 ev++;
733         }
734 }
735
736 void
737 litest_touch_move_to(struct litest_device *d,
738                      unsigned int slot,
739                      double x_from, double y_from,
740                      double x_to, double y_to,
741                      int steps)
742 {
743         for (int i = 0; i < steps - 1; i++)
744                 litest_touch_move(d, slot,
745                                   x_from + (x_to - x_from)/steps * i,
746                                   y_from + (y_to - y_from)/steps * i);
747         litest_touch_move(d, slot, x_to, y_to);
748 }
749
750 void
751 litest_button_click(struct litest_device *d, unsigned int button, bool is_press)
752 {
753
754         struct input_event *ev;
755         struct input_event click[] = {
756                 { .type = EV_KEY, .code = button, .value = is_press ? 1 : 0 },
757                 { .type = EV_SYN, .code = SYN_REPORT, .value = 0 },
758         };
759
760         ARRAY_FOR_EACH(click, ev)
761                 litest_event(d, ev->type, ev->code, ev->value);
762 }
763
764 void
765 litest_keyboard_key(struct litest_device *d, unsigned int key, bool is_press)
766 {
767         litest_button_click(d, key, is_press);
768 }
769
770 int
771 litest_scale(const struct litest_device *d, unsigned int axis, double val)
772 {
773         int min, max;
774         ck_assert_int_ge(val, 0);
775         ck_assert_int_le(val, 100);
776         ck_assert_int_le(axis, ABS_Y);
777
778         min = d->interface->min[axis];
779         max = d->interface->max[axis];
780         return (max - min) * val/100.0 + min;
781 }
782
783 void
784 litest_drain_events(struct libinput *li)
785 {
786         struct libinput_event *event;
787
788         libinput_dispatch(li);
789         while ((event = libinput_get_event(li))) {
790                 libinput_event_destroy(event);
791                 libinput_dispatch(li);
792         }
793 }
794
795 static void
796 litest_print_event(struct libinput_event *event)
797 {
798         struct libinput_event_pointer *p;
799         struct libinput_device *dev;
800         enum libinput_event_type type;
801         double x, y;
802
803         dev = libinput_event_get_device(event);
804         type = libinput_event_get_type(event);
805
806         fprintf(stderr,
807                 "device %s type %d ",
808                 libinput_device_get_sysname(dev),
809                 type);
810         switch (type) {
811         case LIBINPUT_EVENT_POINTER_MOTION:
812                 p = libinput_event_get_pointer_event(event);
813                 x = libinput_event_pointer_get_dx(p);
814                 y = libinput_event_pointer_get_dy(p);
815                 fprintf(stderr, "motion: %.2f/%.2f", x, y);
816                 break;
817         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
818                 p = libinput_event_get_pointer_event(event);
819                 x = libinput_event_pointer_get_absolute_x(p);
820                 y = libinput_event_pointer_get_absolute_y(p);
821                 fprintf(stderr, "motion: %.2f/%.2f", x, y);
822                 break;
823         case LIBINPUT_EVENT_POINTER_BUTTON:
824                 p = libinput_event_get_pointer_event(event);
825                 fprintf(stderr,
826                         "button: %d state %d",
827                         libinput_event_pointer_get_button(p),
828                         libinput_event_pointer_get_button_state(p));
829                 break;
830         default:
831                 break;
832         }
833
834         fprintf(stderr, "\n");
835 }
836
837 void
838 litest_assert_empty_queue(struct libinput *li)
839 {
840         bool empty_queue = true;
841         struct libinput_event *event;
842
843         libinput_dispatch(li);
844         while ((event = libinput_get_event(li))) {
845                 empty_queue = false;
846                 fprintf(stderr,
847                         "Unexpected event: ");
848                 litest_print_event(event);
849                 libinput_event_destroy(event);
850                 libinput_dispatch(li);
851         }
852
853         ck_assert(empty_queue);
854 }
855
856 struct libevdev_uinput *
857 litest_create_uinput_device_from_description(const char *name,
858                                              const struct input_id *id,
859                                              const struct input_absinfo *abs_info,
860                                              const int *events)
861 {
862         struct libevdev_uinput *uinput;
863         struct libevdev *dev;
864         int type, code;
865         int rc, fd;
866         const struct input_absinfo *abs;
867         const struct input_absinfo default_abs = {
868                 .value = 0,
869                 .minimum = 0,
870                 .maximum = 0xffff,
871                 .fuzz = 0,
872                 .flat = 0,
873                 .resolution = 100
874         };
875         char buf[512];
876         const char *devnode;
877
878         dev = libevdev_new();
879         ck_assert(dev != NULL);
880
881         snprintf(buf, sizeof(buf), "litest %s", name);
882         libevdev_set_name(dev, buf);
883         if (id) {
884                 libevdev_set_id_bustype(dev, id->bustype);
885                 libevdev_set_id_vendor(dev, id->vendor);
886                 libevdev_set_id_product(dev, id->product);
887         }
888
889         abs = abs_info;
890         while (abs && abs->value != -1) {
891                 rc = libevdev_enable_event_code(dev, EV_ABS,
892                                                 abs->value, abs);
893                 ck_assert_int_eq(rc, 0);
894                 abs++;
895         }
896
897         while (events &&
898                (type = *events++) != -1 &&
899                (code = *events++) != -1) {
900                 if (type == INPUT_PROP_MAX) {
901                         rc = libevdev_enable_property(dev, code);
902                 } else {
903                         rc = libevdev_enable_event_code(dev, type, code,
904                                                         type == EV_ABS ? &default_abs : NULL);
905                 }
906                 ck_assert_int_eq(rc, 0);
907         }
908
909         rc = libevdev_uinput_create_from_device(dev,
910                                                 LIBEVDEV_UINPUT_OPEN_MANAGED,
911                                                 &uinput);
912         ck_assert_int_eq(rc, 0);
913
914         libevdev_free(dev);
915
916         /* uinput does not yet support setting the resolution, so we set it
917          * afterwards. This is of course racy as hell but the way we
918          * _generally_ use this function by the time libinput uses the
919          * device, we're finished here */
920
921         devnode = libevdev_uinput_get_devnode(uinput);
922         ck_assert_notnull(devnode);
923         fd = open(devnode, O_RDONLY);
924         ck_assert_int_gt(fd, -1);
925         rc = libevdev_new_from_fd(fd, &dev);
926         ck_assert_int_eq(rc, 0);
927
928         abs = abs_info;
929         while (abs && abs->value != -1) {
930                 if (abs->resolution != 0) {
931                         rc = libevdev_kernel_set_abs_info(dev,
932                                                           abs->value,
933                                                           abs);
934                         ck_assert_int_eq(rc, 0);
935                 }
936                 abs++;
937         }
938         close(fd);
939         libevdev_free(dev);
940
941         return uinput;
942 }
943
944 static struct libevdev_uinput *
945 litest_create_uinput_abs_device_v(const char *name,
946                                   struct input_id *id,
947                                   const struct input_absinfo *abs,
948                                   va_list args)
949 {
950         int events[KEY_MAX * 2 + 2]; /* increase this if not sufficient */
951         int *event = events;
952         int type, code;
953
954         while ((type = va_arg(args, int)) != -1 &&
955                (code = va_arg(args, int)) != -1) {
956                 *event++ = type;
957                 *event++ = code;
958                 ck_assert(event < &events[ARRAY_LENGTH(events) - 2]);
959         }
960
961         *event++ = -1;
962         *event++ = -1;
963
964         return litest_create_uinput_device_from_description(name, id,
965                                                             abs, events);
966 }
967
968 struct libevdev_uinput *
969 litest_create_uinput_abs_device(const char *name,
970                                 struct input_id *id,
971                                 const struct input_absinfo *abs,
972                                 ...)
973 {
974         struct libevdev_uinput *uinput;
975         va_list args;
976
977         va_start(args, abs);
978         uinput = litest_create_uinput_abs_device_v(name, id, abs, args);
979         va_end(args);
980
981         return uinput;
982 }
983
984 struct libevdev_uinput *
985 litest_create_uinput_device(const char *name, struct input_id *id, ...)
986 {
987         struct libevdev_uinput *uinput;
988         va_list args;
989
990         va_start(args, id);
991         uinput = litest_create_uinput_abs_device_v(name, id, NULL, args);
992         va_end(args);
993
994         return uinput;
995 }