Debian Codegraph @

test/litest.c

raw
1 /*
2 * Copyright © 2013 Red Hat, Inc.
3 * Copyright © 2013 Marcin Slusarz <[email protected]>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "config.h"
26
27 #include <dirent.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <fnmatch.h>
31 #include <getopt.h>
32 #include <libudev.h>
33 #include <poll.h>
34 #include <signal.h>
35 #include <stdarg.h>
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <sys/ptrace.h>
40 #include <sys/resource.h>
41 #include <sys/stat.h>
42 #include <sys/sysinfo.h>
43 #include <sys/timerfd.h>
44 #include <sys/types.h>
45 #include <sys/wait.h>
46 #include <time.h>
47 #include <unistd.h>
48
49 #include "linux/input.h"
50 #ifdef HAVE_LIBSYSTEMD
51 #include <systemd/sd-bus.h>
52 #endif
53 #ifdef __FreeBSD__
54 #include <termios.h>
55 #endif
56
57 #include <libevdev/libevdev.h>
58 #include <linux/kd.h>
59 #include <valgrind/valgrind.h>
60
61 #include "util-backtrace.h"
62 #include "util-files.h"
63 #include "util-libinput.h"
64 #include "util-mem.h"
65
66 #include "builddir.h"
67 #include "libinput-util.h"
68 #include "litest-int.h"
69 #include "litest-runner.h"
70 #include "litest.h"
71 #include "quirks.h"
72
73 #define evbit(t, c) ((t) << 16U | (c & 0xffff))
74
75 #define UDEV_RULES_D "/run/udev/rules.d"
76 #define UDEV_FUZZ_OVERRIDE_RULE_FILE \
77 UDEV_RULES_D \
78 "/91-litest-fuzz-override-REMOVEME-XXXXXX.rules"
79 #define UDEV_TEST_DEVICE_RULE_FILE \
80 UDEV_RULES_D \
81 "/91-litest-test-device-REMOVEME-XXXXXXX.rules"
82 #define UDEV_DEVICE_GROUPS_FILE \
83 UDEV_RULES_D \
84 "/80-libinput-device-groups-litest-XXXXXX.rules"
85
86 bool verbose = false;
87 bool in_debugger = false;
88 bool run_deviceless = false;
89 static bool use_system_rules_quirks = false;
90 static bool exit_first = false;
91 static FILE *outfile = NULL;
92 static const char *filter_test = NULL;
93 static const char *filter_device = NULL;
94 static const char *filter_group = NULL;
95 static int filter_rangeval = INT_MIN;
96 bool use_colors = false;
97
98 struct param_filter {
99 char name[64];
100 char glob[64];
101 };
102 struct param_filter filter_params[8]; /* name=NULL terminated */
103
104 static struct quirks_context *quirks_context;
105
106 struct created_file {
107 struct list link;
108 char *path;
109 };
110
111 static void
112 created_file_destroy(struct created_file *f)
113 {
114 list_remove(&f->link);
115 free(f->path);
116 free(f);
117 }
118
119 static void
120 created_file_unlink(struct created_file *f)
121 {
122 unlink(f->path);
123 rmdir(f->path);
124 }
125
126 struct suite *current_suite = NULL;
127
128 static void
129 litest_init_udev_rules(struct list *created_files_list);
130 static void
131 litest_remove_udev_rules(struct list *created_files_list);
132 static void
133 litest_print_event(struct libinput_event *event, const char *message);
134
135 enum quirks_setup_mode {
136 QUIRKS_SETUP_USE_SRCDIR,
137 QUIRKS_SETUP_ONLY_DEVICE,
138 QUIRKS_SETUP_FULL,
139 };
140 static void
141 litest_setup_quirks(struct list *created_files_list, enum quirks_setup_mode mode);
142
143 /* defined for the litest selftest */
144 #ifndef LITEST_DISABLE_BACKTRACE_LOGGING
145 #define litest_log(...) fprintf(stderr, __VA_ARGS__)
146 #define litest_vlog(format_, args_) vfprintf(stderr, format_, args_)
147 #else
148 #define litest_log(...) \
149 { /* __VA_ARGS__ */ \
150 }
151 #define litest_vlog(...) \
152 { /* __VA_ARGS__ */ \
153 }
154 #endif
155
156 LIBINPUT_ATTRIBUTE_PRINTF(4, 5)
157 void
158 _litest_checkpoint(const char *func,
159 int line,
160 const char *color,
161 const char *format,
162 ...)
163 {
164 char buf[1024];
165 va_list args;
166
167 va_start(args, format);
168 if (verbose) {
169 vsnprintf(buf, sizeof(buf), format, args);
170 fprintf(stderr,
171 "%s%s():%d - %s%s%s\n",
172 use_colors ? ANSI_BRIGHT_BLUE : "",
173 func,
174 line,
175 use_colors ? color : "",
176 buf,
177 use_colors ? ANSI_NORMAL : "");
178 }
179 va_end(args);
180 }
181
182 void
183 litest_backtrace(const char *func)
184 {
185 #ifndef LITEST_DISABLE_BACKTRACE_LOGGING
186 if (RUNNING_ON_VALGRIND) {
187 fprintf(stderr, "Using valgrind, omitting backtrace\n");
188 return;
189 }
190 char buf[256];
191
192 snprintf(buf, sizeof(buf), "in %s", func);
193
194 backtrace_print(stderr,
195 use_colors,
196 "in litest_backtrace",
197 "in litest_runner_test_run",
198 func ? buf : NULL);
199 #endif
200 }
201
202 LIBINPUT_ATTRIBUTE_PRINTF(5, 6)
203 __attribute__((noreturn)) void
204 litest_fail_condition(const char *file,
205 int line,
206 const char *func,
207 const char *condition,
208 const char *message,
209 ...)
210 {
211 litest_log("FAILED: %s\n", condition);
212
213 if (message) {
214 char buf[1024];
215 va_list args;
216 va_start(args, message);
217 vsnprintf(buf, sizeof(buf), message, args);
218 va_end(args);
219 litest_log("%s\n", buf);
220 }
221
222 litest_log("in %s() (%s:%d)\n", func, file ? file : "???", line);
223 litest_backtrace(func);
224 litest_runner_abort();
225 }
226
227 __attribute__((noreturn)) void
228 litest_fail_comparison_int(const char *file,
229 int line,
230 const char *func,
231 const char *operator,
232 int a,
233 int b,
234 const char *astr,
235 const char *bstr)
236 {
237 litest_log("FAILED COMPARISON: %s %s %s\n", astr, operator, bstr);
238 litest_log("Resolved to: %d %s %d\n", a, operator, b);
239 litest_log("in %s() (%s:%d)\n", func, file, line);
240 litest_backtrace(func);
241 litest_runner_abort();
242 }
243
244 __attribute__((noreturn)) void
245 litest_fail_comparison_double(const char *file,
246 int line,
247 const char *func,
248 const char *operator,
249 double a,
250 double b,
251 const char *astr,
252 const char *bstr)
253 {
254 litest_log("FAILED COMPARISON: %s %s %s\n", astr, operator, bstr);
255 litest_log("Resolved to: %.3f %s %.3f\n", a, operator, b);
256 litest_log("in %s() (%s:%d)\n", func, file, line);
257 litest_backtrace(func);
258 litest_runner_abort();
259 }
260
261 __attribute__((noreturn)) void
262 litest_fail_comparison_ptr(const char *file,
263 int line,
264 const char *func,
265 const char *comparison)
266 {
267 litest_log("FAILED COMPARISON: %s\n", comparison);
268 litest_log("in %s() (%s:%d)\n", func, file, line);
269 litest_backtrace(func);
270 litest_runner_abort();
271 }
272
273 __attribute__((noreturn)) void
274 litest_fail_comparison_str(const char *file,
275 int line,
276 const char *func,
277 const char *comparison,
278 const char *operator,
279 const char *astr,
280 const char *bstr)
281 {
282 litest_log("FAILED COMPARISON: %s %s %s\n", astr, operator, bstr);
283 litest_log("Resolved to: %s %s %s\n", astr, operator, bstr);
284 litest_log("in %s() (%s:%d)\n", func, file, line);
285 litest_backtrace(func);
286 litest_runner_abort();
287 }
288
289 struct litest_parameter_value {
290 size_t refcnt;
291 struct list link; /* litest_parameter->values */
292
293 struct multivalue value;
294 };
295
296 struct litest_parameter {
297 size_t refcnt;
298 struct list link; /* litest_parameters.params */
299 char name[128];
300 char type; /* One of u, i, d, c, s, b */
301
302 struct list values; /* litest_parameter_value */
303 };
304
305 struct litest_parameters {
306 size_t refcnt;
307 struct list params; /* struct litest_parameter */
308 };
309
310 static struct litest_parameter_value *
311 litest_parameter_value_new(void)
312 {
313 struct litest_parameter_value *pv = zalloc(sizeof *pv);
314
315 list_init(&pv->link);
316 pv->refcnt = 1;
317
318 return pv;
319 }
320
321 static inline void
322 litest_parameter_add_string(struct litest_parameter *p, const char *s)
323 {
324 assert(p->type == 's');
325
326 struct litest_parameter_value *pv = litest_parameter_value_new();
327 pv->value = multivalue_new_string(s);
328 list_append(&p->values, &pv->link);
329 }
330
331 static inline void
332 litest_parameter_add_char(struct litest_parameter *p, char c)
333 {
334 assert(p->type == 'c');
335
336 struct litest_parameter_value *pv = litest_parameter_value_new();
337 pv->value = multivalue_new_char(c);
338 list_append(&p->values, &pv->link);
339 }
340
341 static inline void
342 litest_parameter_add_bool(struct litest_parameter *p, bool b)
343 {
344 assert(p->type == 'b');
345
346 struct litest_parameter_value *pv = litest_parameter_value_new();
347 pv->value = multivalue_new_bool(b);
348 list_append(&p->values, &pv->link);
349 }
350
351 static inline void
352 litest_parameter_add_u32(struct litest_parameter *p, uint32_t u)
353 {
354 assert(p->type == 'u');
355
356 struct litest_parameter_value *pv = litest_parameter_value_new();
357 pv->value = multivalue_new_u32(u);
358 list_append(&p->values, &pv->link);
359 }
360
361 static inline void
362 litest_parameter_add_i32(struct litest_parameter *p, int32_t i)
363 {
364 assert(p->type == 'i');
365
366 struct litest_parameter_value *pv = litest_parameter_value_new();
367 pv->value = multivalue_new_i32(i);
368 list_append(&p->values, &pv->link);
369 }
370
371 static void
372 litest_parameter_add_double(struct litest_parameter *p, double d)
373 {
374 assert(p->type == 'd');
375
376 struct litest_parameter_value *pv = litest_parameter_value_new();
377 pv->value = multivalue_new_double(d);
378 list_append(&p->values, &pv->link);
379 }
380
381 static inline void
382 litest_parameter_add_named_i32(struct litest_parameter *p,
383 const struct litest_named_i32 i)
384 {
385 assert(p->type == 'I');
386
387 struct litest_parameter_value *pv = litest_parameter_value_new();
388 pv->value = multivalue_new_named_i32(i.value, i.name);
389 list_append(&p->values, &pv->link);
390 }
391
392 #if 0
393 static struct litest_parameter_value *
394 litest_parameter_value_ref(struct litest_parameter_value *pv) {
395 assert(pv);
396 assert(pv->refcnt > 0);
397 pv->refcnt++;
398 return pv;
399 }
400 #endif
401
402 static struct litest_parameter_value *
403 litest_parameter_value_unref(struct litest_parameter_value *pv)
404 {
405 if (pv) {
406 assert(pv->refcnt > 0);
407 if (--pv->refcnt == 0) {
408 list_remove(&pv->link);
409 free(pv);
410 }
411 }
412 return NULL;
413 }
414
415 static struct litest_parameter *
416 litest_parameter_new(const char *name, char type)
417 {
418 struct litest_parameter *p = zalloc(sizeof *p);
419
420 switch (type) {
421 case 'b':
422 case 'c':
423 case 'd':
424 case 'i':
425 case 'I':
426 case 's':
427 case 'u':
428 break;
429 default:
430 assert(!"Type not yet implemented");
431 }
432
433 list_init(&p->link);
434 list_init(&p->values);
435 snprintf(p->name, sizeof(p->name), "%s", name);
436 p->type = type;
437 p->refcnt = 1;
438
439 return p;
440 }
441
442 static struct litest_parameter *
443 litest_parameter_ref(struct litest_parameter *p)
444 {
445 assert(p);
446 assert(p->refcnt > 0);
447 p->refcnt++;
448 return p;
449 }
450
451 static struct litest_parameter *
452 litest_parameter_unref(struct litest_parameter *p)
453 {
454 if (p) {
455 assert(p->refcnt > 0);
456 if (--p->refcnt == 0) {
457 struct litest_parameter_value *pv;
458 list_for_each_safe(pv, &p->values, link) {
459 litest_parameter_value_unref(pv);
460 }
461 list_remove(&p->link);
462 free(p);
463 }
464 }
465 return NULL;
466 }
467
468 DEFINE_UNREF_CLEANUP_FUNC(litest_parameter);
469
470 static void
471 litest_parameters_add(struct litest_parameters *ps, struct litest_parameter *param)
472 {
473 struct litest_parameter *p;
474 list_for_each(p, &ps->params, link) {
475 assert(!streq(p->name, param->name));
476 }
477
478 litest_parameter_ref(param);
479 list_append(&ps->params, &param->link);
480 }
481
482 struct litest_parameters *
483 _litest_parameters_new(const char *name, ...)
484 {
485 struct litest_parameters *ps = zalloc(sizeof *ps);
486
487 list_init(&ps->params);
488 ps->refcnt = 1;
489
490 va_list args;
491 va_start(args, name);
492
493 while (name) {
494 char type = va_arg(args, int);
495
496 _unref_(litest_parameter) *param = litest_parameter_new(name, type);
497 if (type == 'b') {
498 litest_parameter_add_bool(param, true);
499 litest_parameter_add_bool(param, false);
500 } else {
501 unsigned int nargs = va_arg(args, unsigned int);
502 for (unsigned int _ = 0; _ < nargs; _++) {
503 switch (type) {
504 case 'c': {
505 char b = va_arg(args, int);
506 litest_parameter_add_char(param, b);
507 break;
508 }
509 case 'u': {
510 uint32_t b = va_arg(args, uint32_t);
511 litest_parameter_add_u32(param, b);
512 break;
513 }
514 case 'i': {
515 int32_t b = va_arg(args, int32_t);
516 litest_parameter_add_i32(param, b);
517 break;
518 }
519 case 'd': {
520 double b = va_arg(args, double);
521 litest_parameter_add_double(param, b);
522 break;
523 }
524 case 's': {
525 const char *s = va_arg(args, const char *);
526 litest_parameter_add_string(param, s);
527 break;
528 }
529 case 'I': {
530 struct litest_named_i32 p =
531 va_arg(args, struct litest_named_i32);
532 litest_parameter_add_named_i32(param, p);
533 break;
534 }
535 default:
536 litest_abort_msg(
537 "Unhandled parameter type '%c'",
538 type);
539 break;
540 }
541 }
542 }
543
544 litest_parameters_add(ps, param);
545 name = va_arg(args, const char *);
546 }
547
548 va_end(args);
549
550 return ps;
551 }
552
553 struct litest_parameters *
554 litest_parameters_ref(struct litest_parameters *p)
555 {
556 assert(p);
557 assert(p->refcnt > 0);
558 p->refcnt++;
559 return p;
560 }
561
562 struct litest_parameters *
563 litest_parameters_unref(struct litest_parameters *params)
564 {
565 if (params) {
566 assert(params->refcnt > 0);
567 if (--params->refcnt == 0) {
568 struct litest_parameter *p;
569 list_for_each_safe(p, &params->params, link) {
570 litest_parameter_unref(p);
571 }
572 free(params);
573 }
574 }
575 return NULL;
576 }
577
578 static inline int
579 _permutate(struct litest_parameters_permutation *permutation,
580 struct list *next_param,
581 void *list_head,
582 litest_parameters_permutation_func_t func,
583 void *userdata)
584 {
585 if (next_param->next == list_head) {
586 func(permutation, userdata);
587 return 0;
588 }
589 struct litest_parameter_value *pv;
590 struct litest_parameter *param = list_first_entry(next_param, param, link);
591 list_for_each(pv, &param->values, link) {
592 struct litest_parameters_permutation_value v = {
593 .value = pv->value,
594 };
595
596 memcpy(v.name, param->name, min(sizeof(v.name), sizeof(param->name)));
597
598 list_append(&permutation->values, &v.link);
599 int rc = _permutate(permutation,
600 &param->link,
601 list_head,
602 func,
603 userdata);
604 if (rc)
605 return rc;
606 list_remove(&v.link);
607 }
608
609 return 0;
610 }
611
612 /**
613 * Calls the given function func with each permutation of
614 * the given test parameters.
615 */
616 int
617 litest_parameters_permutations(struct litest_parameters *params,
618 litest_parameters_permutation_func_t func,
619 void *userdata)
620 {
621
622 struct litest_parameters_permutation permutation;
623 list_init(&permutation.values);
624
625 return _permutate(&permutation,
626 &params->params,
627 &params->params,
628 func,
629 userdata);
630 }
631
632 static struct litest_device *current_device;
633
634 struct litest_device *
635 litest_current_device(void)
636 {
637 return current_device;
638 }
639
640 int
641 _litest_dispatch(struct libinput *li, const char *func, int line)
642 {
643 static int dispatch_counter = 0;
644
645 ++dispatch_counter;
646
647 _litest_checkpoint(func,
648 line,
649 ANSI_MAGENTA,
650 "┌──────────────────── dispatch %3d ────────────────────┐",
651 dispatch_counter);
652 int rc = libinput_dispatch(li);
653 enum libinput_event_type type = libinput_next_event_type(li);
654
655 const char *evtype =
656 type == LIBINPUT_EVENT_NONE ? "NONE" : litest_event_type_str(type);
657 _litest_checkpoint(
658 func,
659 line,
660 ANSI_MAGENTA,
661 "└──────────────────── /dispatch %3d ────────────────────┘ pending %s",
662 dispatch_counter,
663 evtype);
664 return rc;
665 }
666
667 static void
668 grab_device(struct litest_device *device, bool mode)
669 {
670 struct libinput *li = libinput_device_get_context(device->libinput_device);
671 struct litest_context *ctx = libinput_get_user_data(li);
672 _unref_(udev_device) * udev_device;
673 const char *devnode;
674 struct path *p;
675
676 udev_device = libinput_device_get_udev_device(device->libinput_device);
677 litest_assert_ptr_notnull(udev_device);
678
679 devnode = udev_device_get_devnode(udev_device);
680
681 /* Note: in some tests we create multiple devices for the same path.
682 * This will only grab the first device in the list but we're using
683 * list_insert() so the first device is the latest that was
684 * initialized, so we should be good.
685 */
686 list_for_each(p, &ctx->paths, link) {
687 if (streq(p->path, devnode)) {
688 int rc = ioctl(p->fd, EVIOCGRAB, (void *)mode ? 1 : 0);
689 litest_assert_errno_success(rc);
690 return;
691 }
692 }
693 litest_abort_msg("Failed to find device %s to %sgrab",
694 devnode,
695 mode ? "" : "un");
696 }
697
698 void
699 litest_grab_device(struct litest_device *device)
700 {
701 grab_device(device, true);
702 }
703
704 void
705 litest_ungrab_device(struct litest_device *device)
706 {
707 grab_device(device, false);
708 }
709
710 void
711 litest_set_current_device(struct litest_device *device)
712 {
713 current_device = device;
714 }
715
716 void
717 litest_generic_device_teardown(void)
718 {
719 litest_device_destroy(current_device);
720 current_device = NULL;
721 }
722
723 static struct list devices = LIST_INIT(devices); /* struct litest_test_device */
724
725 void
726 litest_add_test_device(struct list *device)
727 {
728 list_append(&devices, device);
729 }
730
731 static inline void
732 litest_system(const char *command)
733 {
734 int ret;
735
736 ret = system(command);
737
738 if (ret == -1) {
739 litest_abort_msg("Failed to execute: %s", command);
740 } else if (WIFEXITED(ret)) {
741 if (WEXITSTATUS(ret))
742 litest_abort_msg("'%s' failed with %d",
743 command,
744 WEXITSTATUS(ret));
745 } else if (WIFSIGNALED(ret)) {
746 litest_abort_msg("'%s' terminated with signal %d",
747 command,
748 WTERMSIG(ret));
749 }
750 }
751
752 static void
753 litest_reload_udev_rules(void)
754 {
755 litest_system("udevadm control --reload-rules");
756 }
757
758 static bool
759 filter_for_rangeval(const struct range *range, int rangeval)
760 {
761 return !range || filter_rangeval == INT_MIN || filter_rangeval == rangeval;
762 }
763
764 static void
765 litest_add_tcase_for_device(struct suite *suite,
766 const char *funcname,
767 const void *func,
768 const struct litest_test_device *dev,
769 const struct range *range)
770 {
771 const struct range no_range = range_init_empty();
772
773 if (run_deviceless)
774 return;
775
776 if (!range)
777 range = &no_range;
778
779 int rangeval = range->lower;
780 do {
781 if (filter_for_rangeval(range, rangeval)) {
782 struct test *t;
783
784 t = zalloc(sizeof(*t));
785 t->name = safe_strdup(funcname);
786 t->devname = safe_strdup(dev->shortname);
787 t->func = func;
788 t->setup = dev->setup;
789 t->teardown = dev->teardown ? dev->teardown
790 : litest_generic_device_teardown;
791 if (range)
792 t->range = *range;
793 t->rangeval = rangeval;
794
795 list_append(&suite->tests, &t->node);
796 }
797 } while (++rangeval < range->upper);
798 }
799
800 struct permutation_userdata {
801 struct suite *suite;
802 const char *funcname;
803 const void *func;
804 const struct litest_test_device *dev;
805 char devname[64]; /* set if dev == NULL */
806
807 const struct param_filter *param_filters; /* name=NULL terminated */
808 };
809
810 static int
811 permutation_func(struct litest_parameters_permutation *permutation, void *userdata)
812 {
813 struct permutation_userdata *data = userdata;
814
815 struct litest_test_parameters *params = litest_test_parameters_new();
816 struct litest_parameters_permutation_value *pmv;
817 bool filtered = false;
818 list_for_each(pmv, &permutation->values, link) {
819 const struct param_filter *f = data->param_filters;
820 while (!filtered && strlen(f->name)) {
821 if (streq(pmv->name, f->name)) {
822 _autofree_ char *s = multivalue_as_str(&pmv->value);
823 if (fnmatch(f->glob, s, 0) != 0)
824 filtered = true;
825 }
826 f++;
827 }
828
829 if (filtered)
830 break;
831
832 struct litest_test_param *tp = zalloc(sizeof *tp);
833 snprintf(tp->name, sizeof(tp->name), "%s", pmv->name);
834 tp->value = multivalue_copy(&pmv->value);
835 list_append(&params->test_params, &tp->link);
836 }
837
838 if (filtered) {
839 litest_test_parameters_unref(params);
840 return 0;
841 }
842
843 struct test *t;
844
845 t = zalloc(sizeof(*t));
846 t->name = safe_strdup(data->funcname);
847 t->func = data->func;
848 if (data->dev) {
849 t->devname = safe_strdup(data->dev->shortname);
850 t->setup = data->dev->setup;
851 t->teardown = data->dev->teardown ? data->dev->teardown
852 : litest_generic_device_teardown;
853 } else {
854 t->devname = safe_strdup(data->devname);
855 t->setup = NULL;
856 t->teardown = NULL;
857 }
858 t->rangeval = 0;
859 t->params = params;
860
861 list_append(&data->suite->tests, &t->node);
862
863 return 0;
864 }
865
866 static void
867 litest_add_tcase_for_device_with_params(struct suite *suite,
868 const char *funcname,
869 const void *func,
870 const struct litest_test_device *dev,
871 struct litest_parameters *params)
872 {
873 if (run_deviceless)
874 return;
875
876 struct permutation_userdata data = {
877 .suite = suite,
878 .funcname = funcname,
879 .func = func,
880 .dev = dev,
881 .param_filters = filter_params,
882 };
883
884 litest_parameters_permutations(params, permutation_func, &data);
885 }
886
887 static void
888 litest_add_tcase_no_device(struct suite *suite,
889 const void *func,
890 const char *funcname,
891 const struct range *range)
892 {
893 const char *test_name = funcname;
894 const struct range no_range = range_init_empty();
895
896 if (filter_device && fnmatch(filter_device, test_name, 0) != 0)
897 return;
898
899 if (run_deviceless)
900 return;
901
902 if (!range)
903 range = &no_range;
904
905 int rangeval = range->lower;
906 do {
907 if (filter_for_rangeval(range, rangeval)) {
908 struct test *t;
909
910 t = zalloc(sizeof(*t));
911 t->name = safe_strdup(test_name);
912 t->devname = safe_strdup("no device");
913 t->func = func;
914 if (range)
915 t->range = *range;
916 t->rangeval = rangeval;
917 t->setup = NULL;
918 t->teardown = NULL;
919
920 list_append(&suite->tests, &t->node);
921 }
922 } while (++rangeval < range->upper);
923 }
924
925 static void
926 litest_add_tcase_no_device_with_params(struct suite *suite,
927 const void *func,
928 const char *funcname,
929 struct litest_parameters *params)
930 {
931 const char *test_name = funcname;
932
933 if (filter_device && fnmatch(filter_device, test_name, 0) != 0)
934 return;
935
936 if (run_deviceless)
937 return;
938
939 struct permutation_userdata data = {
940 .suite = suite,
941 .funcname = funcname,
942 .func = func,
943 .param_filters = filter_params,
944 };
945 snprintf(data.devname, sizeof(data.devname), "no device");
946
947 litest_parameters_permutations(params, permutation_func, &data);
948 }
949
950 static void
951 litest_add_tcase_deviceless(struct suite *suite,
952 const void *func,
953 const char *funcname,
954 const struct range *range)
955 {
956 const char *test_name = funcname;
957 const struct range no_range = range_init_empty();
958
959 if (filter_device && fnmatch(filter_device, test_name, 0) != 0)
960 return;
961
962 if (!range)
963 range = &no_range;
964
965 int rangeval = range->lower;
966 do {
967 if (filter_for_rangeval(range, rangeval)) {
968 struct test *t;
969
970 t = zalloc(sizeof(*t));
971 t->deviceless = true;
972 t->name = safe_strdup(test_name);
973 t->devname = safe_strdup("deviceless");
974 t->func = func;
975 if (range)
976 t->range = *range;
977 t->rangeval = rangeval;
978 t->setup = NULL;
979 t->teardown = NULL;
980
981 list_append(&suite->tests, &t->node);
982 }
983 } while (++rangeval < range->upper);
984 }
985
986 static void
987 litest_add_tcase_deviceless_with_params(struct suite *suite,
988 const void *func,
989 const char *funcname,
990 struct litest_parameters *params)
991 {
992 const char *test_name = funcname;
993
994 if (filter_device && fnmatch(filter_device, test_name, 0) != 0)
995 return;
996
997 struct permutation_userdata data = {
998 .suite = suite,
999 .funcname = funcname,
1000 .func = func,
1001 .param_filters = filter_params,
1002 };
1003 snprintf(data.devname, sizeof(data.devname), "deviceless");
1004
1005 litest_parameters_permutations(params, permutation_func, &data);
1006 }
1007
1008 static void
1009 litest_add_tcase(const char *filename,
1010 const char *funcname,
1011 const void *func,
1012 int64_t required,
1013 int64_t excluded,
1014 const struct range *range,
1015 struct litest_parameters *params)
1016 {
1017 bool added = false;
1018
1019 litest_assert(required >= LITEST_DEVICELESS);
1020 litest_assert(excluded >= LITEST_DEVICELESS);
1021
1022 if (filter_test && fnmatch(filter_test, funcname, 0) != 0)
1023 return;
1024
1025 struct suite *suite = current_suite;
1026
1027 if (filter_group && fnmatch(filter_group, suite->name, 0) != 0)
1028 return;
1029
1030 if (required == LITEST_DEVICELESS && excluded == LITEST_DEVICELESS) {
1031 if (params)
1032 litest_add_tcase_deviceless_with_params(suite,
1033 func,
1034 funcname,
1035 params);
1036 else
1037 litest_add_tcase_deviceless(suite, func, funcname, range);
1038 added = true;
1039 } else if (required == LITEST_DISABLE_DEVICE &&
1040 excluded == LITEST_DISABLE_DEVICE) {
1041 if (params)
1042 litest_add_tcase_no_device_with_params(suite,
1043 func,
1044 funcname,
1045 params);
1046 else
1047 litest_add_tcase_no_device(suite, func, funcname, range);
1048 added = true;
1049 } else if (required != LITEST_ANY || excluded != LITEST_ANY) {
1050 struct litest_test_device *dev;
1051
1052 list_for_each(dev, &devices, node) {
1053 if (dev->features & LITEST_IGNORED)
1054 continue;
1055
1056 if (filter_device &&
1057 fnmatch(filter_device, dev->shortname, 0) != 0)
1058 continue;
1059 if ((dev->features & required) != required ||
1060 (dev->features & excluded) != 0)
1061 continue;
1062
1063 if (params) {
1064 litest_add_tcase_for_device_with_params(suite,
1065 funcname,
1066 func,
1067 dev,
1068 params);
1069 } else {
1070 litest_add_tcase_for_device(suite,
1071 funcname,
1072 func,
1073 dev,
1074 range);
1075 }
1076 added = true;
1077 }
1078 } else {
1079 struct litest_test_device *dev;
1080
1081 list_for_each(dev, &devices, node) {
1082 if (dev->features & LITEST_IGNORED)
1083 continue;
1084
1085 if (filter_device &&
1086 fnmatch(filter_device, dev->shortname, 0) != 0)
1087 continue;
1088
1089 if (params) {
1090 litest_add_tcase_for_device_with_params(suite,
1091 funcname,
1092 func,
1093 dev,
1094 params);
1095 } else {
1096 litest_add_tcase_for_device(suite,
1097 funcname,
1098 func,
1099 dev,
1100 range);
1101 }
1102 added = true;
1103 }
1104 }
1105
1106 if (!added && filter_test == NULL && filter_device == NULL &&
1107 filter_group == NULL) {
1108 fprintf(stderr,
1109 "Test '%s' does not match any devices. Aborting.\n",
1110 funcname);
1111 abort();
1112 }
1113 }
1114
1115 void
1116 _litest_add_no_device(const char *name, const char *funcname, const void *func)
1117 {
1118 _litest_add(name, funcname, func, LITEST_DISABLE_DEVICE, LITEST_DISABLE_DEVICE);
1119 }
1120
1121 void
1122 _litest_add_parametrized_no_device(const char *name,
1123 const char *funcname,
1124 const void *func,
1125 struct litest_parameters *params)
1126 {
1127 _litest_add_parametrized(name,
1128 funcname,
1129 func,
1130 LITEST_DISABLE_DEVICE,
1131 LITEST_DISABLE_DEVICE,
1132 params);
1133 }
1134
1135 void
1136 _litest_add_ranged_no_device(const char *name,
1137 const char *funcname,
1138 const void *func,
1139 const struct range *range)
1140 {
1141 _litest_add_ranged(name,
1142 funcname,
1143 func,
1144 LITEST_DISABLE_DEVICE,
1145 LITEST_DISABLE_DEVICE,
1146 range);
1147 }
1148
1149 void
1150 _litest_add_deviceless(const char *name, const char *funcname, const void *func)
1151 {
1152 _litest_add_ranged(name,
1153 funcname,
1154 func,
1155 LITEST_DEVICELESS,
1156 LITEST_DEVICELESS,
1157 NULL);
1158 }
1159
1160 void
1161 _litest_add_parametrized_deviceless(const char *name,
1162 const char *funcname,
1163 const void *func,
1164 struct litest_parameters *params)
1165 {
1166 _litest_add_parametrized(name,
1167 funcname,
1168 func,
1169 LITEST_DISABLE_DEVICE,
1170 LITEST_DISABLE_DEVICE,
1171 params);
1172 }
1173
1174 void
1175 _litest_add(const char *name,
1176 const char *funcname,
1177 const void *func,
1178 int64_t required,
1179 int64_t excluded)
1180 {
1181 _litest_add_ranged(name, funcname, func, required, excluded, NULL);
1182 }
1183
1184 void
1185 _litest_add_ranged(const char *name,
1186 const char *funcname,
1187 const void *func,
1188 int64_t required,
1189 int64_t excluded,
1190 const struct range *range)
1191 {
1192 litest_add_tcase(name, funcname, func, required, excluded, range, NULL);
1193 }
1194
1195 void
1196 _litest_add_parametrized(const char *name,
1197 const char *funcname,
1198 const void *func,
1199 int64_t required,
1200 int64_t excluded,
1201 struct litest_parameters *params)
1202 {
1203 litest_add_tcase(name, funcname, func, required, excluded, NULL, params);
1204 }
1205
1206 void
1207 _litest_add_for_device(const char *name,
1208 const char *funcname,
1209 const void *func,
1210 enum litest_device_type type)
1211 {
1212 _litest_add_ranged_for_device(name, funcname, func, type, NULL);
1213 }
1214
1215 void
1216 _litest_add_ranged_for_device(const char *filename,
1217 const char *funcname,
1218 const void *func,
1219 enum litest_device_type type,
1220 const struct range *range)
1221 {
1222 struct litest_test_device *dev;
1223 bool device_filtered = false;
1224
1225 litest_assert(type < LITEST_NO_DEVICE);
1226
1227 if (filter_test && fnmatch(filter_test, funcname, 0) != 0)
1228 return;
1229
1230 struct suite *s = current_suite;
1231
1232 if (filter_group && fnmatch(filter_group, s->name, 0) != 0)
1233 return;
1234
1235 list_for_each(dev, &devices, node) {
1236 if (filter_device && fnmatch(filter_device, dev->shortname, 0) != 0) {
1237 device_filtered = true;
1238 continue;
1239 }
1240
1241 if (dev->type == type) {
1242 litest_add_tcase_for_device(s, funcname, func, dev, range);
1243 return;
1244 }
1245 }
1246
1247 /* only abort if no filter was set, that's a bug */
1248 if (!device_filtered)
1249 litest_abort_msg("Invalid test device type");
1250 }
1251
1252 void
1253 _litest_add_parametrized_for_device(const char *filename,
1254 const char *funcname,
1255 const void *func,
1256 enum litest_device_type type,
1257 struct litest_parameters *params)
1258 {
1259 struct litest_test_device *dev;
1260 bool device_filtered = false;
1261
1262 litest_assert(type < LITEST_NO_DEVICE);
1263
1264 if (filter_test && fnmatch(filter_test, funcname, 0) != 0)
1265 return;
1266
1267 struct suite *s = current_suite;
1268
1269 if (filter_group && fnmatch(filter_group, s->name, 0) != 0)
1270 return;
1271
1272 list_for_each(dev, &devices, node) {
1273 if (filter_device && fnmatch(filter_device, dev->shortname, 0) != 0) {
1274 device_filtered = true;
1275 continue;
1276 }
1277
1278 if (dev->type == type) {
1279 litest_add_tcase_for_device_with_params(s,
1280 funcname,
1281 func,
1282 dev,
1283 params);
1284 return;
1285 }
1286 }
1287
1288 /* only abort if no filter was set, that's a bug */
1289 if (!device_filtered)
1290 litest_abort_msg("Invalid test device type");
1291 }
1292
1293 static int
1294 is_actual_error(const char *error, size_t index, void *data)
1295 {
1296 /* We don't want to abort on these errors */
1297 if (((RUNNING_ON_VALGRIND || in_debugger) &&
1298 strstr(error, "scheduled expiry is in the past")) ||
1299 strstr(error, "event processing lagging behind")) {
1300 return 0;
1301 }
1302
1303 return 1;
1304 }
1305
1306 LIBINPUT_ATTRIBUTE_PRINTF(3, 0)
1307 static void
1308 litest_log_handler(struct libinput *libinput,
1309 enum libinput_log_priority pri,
1310 const char *format,
1311 va_list args)
1312 {
1313 const char *priority = NULL;
1314 const char *color = ""; // NOLINT: deadcode.DeadStores
1315 const char *color_reset = ANSI_NORMAL;
1316
1317 switch (pri) {
1318 case LIBINPUT_LOG_PRIORITY_INFO:
1319 priority = "info ";
1320 color = ANSI_BOLD;
1321 break;
1322 case LIBINPUT_LOG_PRIORITY_ERROR:
1323 priority = "error";
1324 color = ANSI_BRIGHT_RED;
1325 break;
1326 case LIBINPUT_LOG_PRIORITY_DEBUG:
1327 priority = "debug";
1328 color = ANSI_NORMAL;
1329 break;
1330 default:
1331 abort();
1332 }
1333
1334 _autofree_ char *msg = strdup_vprintf(format, args);
1335
1336 if (!use_colors)
1337 color_reset = "";
1338 else if (strstr(format, "tap:"))
1339 color = ANSI_BLUE;
1340 else if (strstr(format, "thumb state:"))
1341 color = ANSI_YELLOW;
1342 else if (strstr(format, "button state:"))
1343 color = ANSI_MAGENTA;
1344 else if (strstr(format, "touch-size:") || strstr(format, "pressure:"))
1345 color = ANSI_GREEN;
1346 else if (strstr(format, "palm:") || strstr(format, "thumb:"))
1347 color = ANSI_CYAN;
1348 else if (strstr(format, "edge-scroll:"))
1349 color = ANSI_BRIGHT_GREEN;
1350 else if (strstr(format, "gesture:"))
1351 color = ANSI_YELLOW;
1352 else if (strstr(msg, "Plugin:"))
1353 color = ANSI_BRIGHT_CYAN;
1354
1355 fprintf(stderr, "%slitest %s %s%s", color, priority, msg, color_reset);
1356
1357 if (strstr(msg, "client bug: ") || strstr(msg, "libinput bug: ") ||
1358 strstr(msg, "plugin bug: ")) {
1359 /* valgrind is too slow and some of our offsets are too
1360 * short, don't abort if during a valgrind run we get a
1361 * negative offset */
1362 if ((RUNNING_ON_VALGRIND || in_debugger) &&
1363 strstr(format, "scheduled expiry is in the past")) {
1364 /* noop */
1365 } else if (strstr(format, "event processing lagging behind")) {
1366 /* noop */
1367 } else {
1368 litest_abort_msg("libinput bug triggered, aborting.");
1369 }
1370 }
1371
1372 if (strstr(msg, "Touch jump detected and discarded")) {
1373 litest_abort_msg("libinput touch jump triggered, aborting.");
1374 }
1375 }
1376
1377 static void
1378 litest_init_device_udev_rules(struct litest_test_device *dev, FILE *f)
1379 {
1380 const struct key_value_str *kv;
1381 static int count;
1382 bool need_keyboard_builtin = false;
1383
1384 if (dev->udev_properties[0].key == NULL)
1385 return;
1386
1387 count++;
1388
1389 fprintf(f, "# %s\n", dev->shortname);
1390 fprintf(f, "ACTION==\"remove\", GOTO=\"rule%d_end\"\n", count);
1391 fprintf(f, "KERNEL!=\"event*\", GOTO=\"rule%d_end\"\n", count);
1392
1393 fprintf(f, "ATTRS{name}==\"litest %s*\"", dev->name);
1394
1395 kv = dev->udev_properties;
1396 while (kv->key) {
1397 fprintf(f, ", \\\n\tENV{%s}=\"%s\"", kv->key, kv->value);
1398 if (strstartswith(kv->key, "EVDEV_ABS_"))
1399 need_keyboard_builtin = true;
1400 kv++;
1401 }
1402 fprintf(f, "\n");
1403
1404 /* Special case: the udev keyboard builtin is only run for hwdb
1405 * matches but we don't set any up in litest. So instead scan the
1406 * device's udev properties for any EVDEV_ABS properties and where
1407 * they exist, force a (re-)run of the keyboard builtin to set up
1408 * the evdev device correctly.
1409 * This needs to be done as separate rule apparently, otherwise the
1410 * ENV variables aren't set yet by the time the builtin runs.
1411 */
1412 if (need_keyboard_builtin) {
1413 fprintf(f,
1414 ""
1415 "ATTRS{name}==\"litest %s*\","
1416 " IMPORT{builtin}=\"keyboard\"\n",
1417 dev->name);
1418 }
1419
1420 fprintf(f, "LABEL=\"rule%d_end\"\n\n", count);
1421 ;
1422 }
1423
1424 static void
1425 litest_init_all_device_udev_rules(struct list *created_files)
1426 {
1427 struct created_file *file = zalloc(sizeof(*file));
1428 struct litest_test_device *dev;
1429 FILE *f;
1430 int fd;
1431
1432 char *path = strdup_printf("%s/99-litest-XXXXXX.rules", UDEV_RULES_D);
1433 litest_assert_ptr_notnull(path);
1434
1435 fd = mkstemps(path, 6);
1436 litest_assert_errno_success(fd);
1437 f = fdopen(fd, "w");
1438 litest_assert_notnull(f);
1439
1440 list_for_each(dev, &devices, node)
1441 litest_init_device_udev_rules(dev, f);
1442
1443 fclose(f);
1444
1445 file->path = path;
1446 list_insert(created_files, &file->link);
1447 }
1448
1449 static int
1450 open_restricted(const char *path, int flags, void *userdata)
1451 {
1452 const char prefix[] = "/dev/input/event";
1453 struct litest_context *ctx = userdata;
1454 struct path *p;
1455 int fd;
1456
1457 litest_assert_ptr_notnull(ctx);
1458
1459 fd = open(path, flags);
1460 if (fd < 0)
1461 return -errno;
1462
1463 if (strstartswith(path, prefix)) {
1464 p = zalloc(sizeof *p);
1465 p->path = safe_strdup(path);
1466 p->fd = fd;
1467 /* We specifically insert here so that the most-recently
1468 * opened path is the first one in the list. This helps when
1469 * we have multiple test devices with the same device path,
1470 * the fd of the most recent device is the first one to get
1471 * grabbed
1472 */
1473 list_insert(&ctx->paths, &p->link);
1474 }
1475
1476 return fd;
1477 }
1478
1479 static void
1480 close_restricted(int fd, void *userdata)
1481 {
1482 struct litest_context *ctx = userdata;
1483 struct path *p;
1484
1485 list_for_each_safe(p, &ctx->paths, link) {
1486 if (p->fd != fd)
1487 continue;
1488 list_remove(&p->link);
1489 free(p->path);
1490 free(p);
1491 }
1492
1493 close(fd);
1494 }
1495
1496 static struct libinput_interface interface = {
1497 .open_restricted = open_restricted,
1498 .close_restricted = close_restricted,
1499 };
1500
1501 LIBINPUT_ATTRIBUTE_PRINTF(3, 0)
1502 static inline void
1503 quirk_log_handler(struct libinput *unused,
1504 enum libinput_log_priority priority,
1505 const char *format,
1506 va_list args)
1507 {
1508 if (priority < LIBINPUT_LOG_PRIORITY_ERROR)
1509 return;
1510
1511 vfprintf(stderr, format, args);
1512 }
1513
1514 static enum litest_runner_result
1515 init_quirks(void *userdata)
1516 {
1517 const char *data_path = getenv("LIBINPUT_QUIRKS_DIR");
1518 if (!data_path)
1519 data_path = LIBINPUT_QUIRKS_DIR;
1520
1521 quirks_context = quirks_init_subsystem(data_path,
1522 NULL,
1523 quirk_log_handler,
1524 NULL,
1525 QLOG_LIBINPUT_LOGGING);
1526
1527 return LITEST_PASS;
1528 }
1529
1530 static void
1531 teardown_quirks(void *userdata)
1532 {
1533 quirks_context_unref(quirks_context);
1534 }
1535
1536 static int
1537 litest_run_suite(struct list *suites, int njobs)
1538 {
1539 size_t ntests = 0;
1540 enum litest_runner_result result = LITEST_SKIP;
1541 struct suite *s;
1542 _destroy_(litest_runner) *runner = litest_runner_new();
1543
1544 litest_runner_set_num_parallel(runner, njobs > 0 ? njobs : 0);
1545 if (outfile)
1546 litest_runner_set_output_file(runner, outfile);
1547 litest_runner_set_verbose(runner, verbose);
1548 litest_runner_set_use_colors(runner, use_colors);
1549 litest_runner_set_timeout(runner, 30);
1550 litest_runner_set_exit_on_fail(runner, exit_first);
1551 litest_runner_set_setup_funcs(runner, init_quirks, teardown_quirks, NULL);
1552
1553 list_for_each(s, suites, node) {
1554 struct test *t;
1555 list_for_each(t, &s->tests, node) {
1556 struct litest_runner_test_description tdesc = { 0 };
1557
1558 if (range_is_valid(&t->range)) {
1559 snprintf(tdesc.name,
1560 sizeof(tdesc.name),
1561 "%s:%s:%s:%d",
1562 s->name,
1563 t->name,
1564 t->devname,
1565 t->rangeval);
1566 } else if (t->params) {
1567 char buf[256] = { 0 };
1568
1569 struct litest_test_param *tp;
1570 bool is_first = true;
1571 list_for_each(tp, &t->params->test_params, link) {
1572 _autofree_ char *val =
1573 multivalue_as_str(&tp->value);
1574 snprintf(buf + strlen(buf),
1575 sizeof(buf) - strlen(buf),
1576 "%s%s:%s",
1577 is_first ? "" : ",",
1578 tp->name,
1579 val);
1580 is_first = false;
1581 }
1582 snprintf(tdesc.name,
1583 sizeof(tdesc.name),
1584 "%s:%s:%s:%s",
1585 s->name,
1586 t->name,
1587 t->devname,
1588 buf);
1589 } else {
1590 snprintf(tdesc.name,
1591 sizeof(tdesc.name),
1592 "%s:%s:%s",
1593 s->name,
1594 t->name,
1595 t->devname);
1596 }
1597 tdesc.func = t->func;
1598 tdesc.setup = t->setup;
1599 tdesc.teardown = t->teardown;
1600 tdesc.args.range = t->range;
1601 tdesc.rangeval = t->rangeval;
1602 tdesc.params = t->params;
1603 litest_runner_add_test(runner, &tdesc);
1604 ntests++;
1605 }
1606 }
1607
1608 if (ntests > 0)
1609 result = litest_runner_run_tests(runner);
1610
1611 return result;
1612 }
1613
1614 static inline int
1615 inhibit(void)
1616 {
1617 int lock_fd = -1;
1618 #ifdef HAVE_LIBSYSTEMD
1619 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1620 _unref_(sd_bus_message) *m = NULL;
1621 _unref_(sd_bus) *bus = NULL;
1622 int rc;
1623
1624 if (run_deviceless)
1625 return -1;
1626
1627 rc = sd_bus_open_system(&bus);
1628 if (rc != 0) {
1629 fprintf(stderr, "Warning: inhibit failed: %s\n", strerror(-rc));
1630 goto out;
1631 }
1632
1633 rc = sd_bus_call_method(
1634 bus,
1635 "org.freedesktop.login1",
1636 "/org/freedesktop/login1",
1637 "org.freedesktop.login1.Manager",
1638 "Inhibit",
1639 &error,
1640 &m,
1641 "ssss",
1642 "sleep:shutdown:handle-lid-switch:handle-power-key:handle-suspend-key:"
1643 "handle-hibernate-key",
1644 "libinput test-suite runner",
1645 "testing in progress",
1646 "block");
1647 if (rc < 0) {
1648 fprintf(stderr, "Warning: inhibit failed: %s\n", error.message);
1649 goto out;
1650 }
1651
1652 rc = sd_bus_message_read(m, "h", &lock_fd);
1653 if (rc < 0) {
1654 fprintf(stderr, "Warning: inhibit failed: %s\n", strerror(-rc));
1655 goto out;
1656 }
1657
1658 lock_fd = dup(lock_fd);
1659 out:
1660 sd_bus_close(bus);
1661 #endif
1662 return lock_fd;
1663 }
1664
1665 static int
1666 disable_tty(void)
1667 {
1668 int tty_mode = -1;
1669
1670 if (isatty(STDIN_FILENO) && ioctl(STDIN_FILENO, KDGKBMODE, &tty_mode) == 0) {
1671 #ifdef __linux__
1672 ioctl(STDIN_FILENO, KDSKBMODE, K_OFF);
1673 #elif __FreeBSD__
1674 ioctl(STDIN_FILENO, KDSKBMODE, K_RAW);
1675
1676 /* Put the tty into raw mode */
1677 struct termios tios;
1678 if (tcgetattr(STDIN_FILENO, &tios))
1679 fprintf(stderr,
1680 "Failed to get terminal attribute: %d - %s\n",
1681 errno,
1682 strerror(errno));
1683 cfmakeraw(&tios);
1684 if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tios))
1685 fprintf(stderr,
1686 "Failed to set terminal attribute: %d - %s\n",
1687 errno,
1688 strerror(errno));
1689 #endif
1690 }
1691
1692 return tty_mode;
1693 }
1694
1695 static void
1696 restore_tty(int tty_mode)
1697 {
1698 if (tty_mode != -1) {
1699 ioctl(STDIN_FILENO, KDSKBMODE, tty_mode);
1700 #ifdef __FreeBSD__
1701 /* Put the tty into "sane" mode */
1702 struct termios tios;
1703 if (tcgetattr(STDIN_FILENO, &tios))
1704 fprintf(stderr,
1705 "Failed to get terminal attribute: %d - %s\n",
1706 errno,
1707 strerror(errno));
1708 cfmakesane(&tios);
1709 if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tios))
1710 fprintf(stderr,
1711 "Failed to set terminal attribute: %d - %s\n",
1712 errno,
1713 strerror(errno));
1714 #endif
1715 }
1716 }
1717
1718 enum litest_runner_result
1719 litest_run(struct list *suites, int njobs)
1720 {
1721 const struct rlimit corelimit = { 0, 0 };
1722 int inhibit_lock_fd;
1723 int tty_mode = -1;
1724
1725 setenv("LIBINPUT_RUNNING_TEST_SUITE", "1", 1);
1726
1727 if (setrlimit(RLIMIT_CORE, &corelimit) != 0)
1728 perror("WARNING: Core dumps not disabled");
1729
1730 struct list created_files_list = LIST_INIT(created_files_list);
1731
1732 if (run_deviceless) {
1733 litest_setup_quirks(&created_files_list, QUIRKS_SETUP_USE_SRCDIR);
1734 } else {
1735 enum quirks_setup_mode mode;
1736 litest_init_udev_rules(&created_files_list);
1737
1738 mode = use_system_rules_quirks ? QUIRKS_SETUP_ONLY_DEVICE
1739 : QUIRKS_SETUP_FULL;
1740 litest_setup_quirks(&created_files_list, mode);
1741 }
1742
1743 /* If we're running 'normally' on the VT, disable the keyboard to
1744 * avoid messing up our host. But if we're inside gdb or running
1745 * without forking, leave it as-is.
1746 */
1747 if (!run_deviceless && njobs > 1 && !in_debugger)
1748 tty_mode = disable_tty();
1749
1750 inhibit_lock_fd = inhibit();
1751
1752 enum litest_runner_result result = litest_run_suite(suites, njobs);
1753
1754 close(inhibit_lock_fd);
1755
1756 restore_tty(tty_mode);
1757
1758 litest_remove_udev_rules(&created_files_list);
1759
1760 return result;
1761 }
1762
1763 static struct input_absinfo *
1764 merge_absinfo(const struct input_absinfo *orig, const struct input_absinfo *override)
1765 {
1766 struct input_absinfo *abs;
1767 unsigned int nelem, i;
1768 size_t sz = ABS_MAX + 1;
1769
1770 if (!orig)
1771 return NULL;
1772
1773 abs = zalloc(sz * sizeof(*abs));
1774 litest_assert_ptr_notnull(abs);
1775
1776 nelem = 0;
1777 while (orig[nelem].value != -1) {
1778 abs[nelem] = orig[nelem];
1779 nelem++;
1780 litest_assert_int_lt(nelem, sz);
1781 }
1782
1783 /* just append, if the same axis is present twice, libevdev will
1784 only use the last value anyway */
1785 i = 0;
1786 while (override && override[i].value != -1) {
1787 abs[nelem++] = override[i++];
1788 litest_assert_int_lt(nelem, sz);
1789 }
1790
1791 litest_assert_int_lt(nelem, sz);
1792 abs[nelem].value = -1;
1793
1794 return abs;
1795 }
1796
1797 static int *
1798 merge_events(const int *orig, const int *override)
1799 {
1800 int *events;
1801 unsigned int nelem, i;
1802 size_t sz = KEY_MAX * 3;
1803
1804 if (!orig)
1805 return NULL;
1806
1807 events = zalloc(sz * sizeof(int));
1808 litest_assert_ptr_notnull(events);
1809
1810 nelem = 0;
1811 while (orig[nelem] != -1) {
1812 events[nelem] = orig[nelem];
1813 nelem++;
1814 litest_assert_int_lt(nelem, sz);
1815 }
1816
1817 /* just append, if the same axis is present twice, libevdev will
1818 * ignore the double definition anyway */
1819 i = 0;
1820 while (override && override[i] != -1) {
1821 events[nelem++] = override[i++];
1822 litest_assert_int_le(nelem, sz);
1823 }
1824
1825 litest_assert_int_lt(nelem, sz);
1826 events[nelem] = -1;
1827
1828 return events;
1829 }
1830
1831 static inline struct created_file *
1832 litest_copy_file(const char *dest, const char *src, const char *header, bool is_file)
1833 {
1834 int in, out, length;
1835 struct created_file *file;
1836
1837 file = zalloc(sizeof(*file));
1838 file->path = safe_strdup(dest);
1839
1840 if (strstr(dest, "XXXXXX")) {
1841 int suffixlen;
1842
1843 suffixlen = file->path + strlen(file->path) - rindex(file->path, '.');
1844 out = mkstemps(file->path, suffixlen);
1845 } else {
1846 out = open(file->path, O_CREAT | O_WRONLY, 0644);
1847 }
1848 if (out == -1)
1849 litest_abort_msg("Failed to write to file %s (%s)",
1850 file->path,
1851 strerror(errno));
1852 litest_assert_errno_success(chmod(file->path, 0644));
1853
1854 if (header) {
1855 length = strlen(header);
1856 litest_assert_int_eq(write(out, header, length), length);
1857 }
1858
1859 if (is_file) {
1860 in = open(src, O_RDONLY);
1861 if (in == -1)
1862 litest_abort_msg("Failed to open file %s (%s)",
1863 src,
1864 strerror(errno));
1865 /* lazy, just check for error and empty file copy */
1866 litest_assert_int_gt(litest_send_file(out, in), 0);
1867 close(in);
1868 } else {
1869 size_t written = write(out, src, strlen(src));
1870 litest_assert_int_eq(written, strlen(src));
1871 }
1872 close(out);
1873
1874 return file;
1875 }
1876
1877 static inline void
1878 litest_install_model_quirks(struct list *created_files_list)
1879 {
1880 const char *warning =
1881 "#################################################################\n"
1882 "# WARNING: REMOVE THIS FILE\n"
1883 "# This is a run-time file for the libinput test suite and\n"
1884 "# should be removed on exit. If the test-suite is not currently \n"
1885 "# running, remove this file\n"
1886 "#################################################################\n\n";
1887 struct created_file *file;
1888 const char *test_device_udev_rule =
1889 "KERNELS==\"*input*\", "
1890 "ATTRS{name}==\"litest *\", "
1891 "ENV{LIBINPUT_TEST_DEVICE}=\"1\"";
1892
1893 file = litest_copy_file(UDEV_TEST_DEVICE_RULE_FILE,
1894 test_device_udev_rule,
1895 warning,
1896 false);
1897 list_insert(created_files_list, &file->link);
1898
1899 /* Only install the litest device rule when we're running as system
1900 * test suite, we expect the others to be in place already */
1901 if (use_system_rules_quirks)
1902 return;
1903
1904 file = litest_copy_file(UDEV_DEVICE_GROUPS_FILE,
1905 LIBINPUT_DEVICE_GROUPS_RULES_FILE,
1906 warning,
1907 true);
1908 list_insert(created_files_list, &file->link);
1909
1910 file = litest_copy_file(UDEV_FUZZ_OVERRIDE_RULE_FILE,
1911 LIBINPUT_FUZZ_OVERRIDE_UDEV_RULES_FILE,
1912 warning,
1913 true);
1914 list_insert(created_files_list, &file->link);
1915 }
1916
1917 static char *
1918 litest_init_device_quirk_file(const char *data_dir, struct litest_test_device *dev)
1919 {
1920 int fd;
1921 FILE *f;
1922 char path[PATH_MAX];
1923 static int count;
1924
1925 if (!dev->quirk_file)
1926 return NULL;
1927
1928 snprintf(path,
1929 sizeof(path),
1930 "%s/99-%03d-%s.quirks",
1931 data_dir,
1932 ++count,
1933 dev->shortname);
1934 fd = open(path, O_CREAT | O_WRONLY, 0644);
1935 litest_assert_errno_success(fd);
1936 f = fdopen(fd, "w");
1937 litest_assert_notnull(f);
1938 litest_assert_int_ge(fputs(dev->quirk_file, f), 0);
1939 fclose(f);
1940
1941 return safe_strdup(path);
1942 }
1943
1944 static int
1945 is_quirks_file(const struct dirent *dir)
1946 {
1947 return strendswith(dir->d_name, ".quirks");
1948 }
1949
1950 /**
1951 * Install the quirks from the quirks/ source directory.
1952 */
1953 static void
1954 litest_install_source_quirks(struct list *created_files_list, const char *dirname)
1955 {
1956 _autofree_ struct dirent **namelist;
1957 int ndev;
1958
1959 ndev = scandir(LIBINPUT_QUIRKS_SRCDIR, &namelist, is_quirks_file, versionsort);
1960 litest_assert_int_ge(ndev, 0);
1961
1962 for (int idx = 0; idx < ndev; idx++) {
1963 struct created_file *file;
1964 char *filename;
1965 char dest[PATH_MAX];
1966 char src[PATH_MAX];
1967
1968 _autofree_ struct dirent *entry = namelist[idx];
1969 filename = entry->d_name;
1970 snprintf(src, sizeof(src), "%s/%s", LIBINPUT_QUIRKS_SRCDIR, filename);
1971 snprintf(dest, sizeof(dest), "%s/%s", dirname, filename);
1972 file = litest_copy_file(dest, src, NULL, true);
1973 list_append(created_files_list, &file->link);
1974 }
1975 }
1976
1977 /**
1978 * Install the quirks from the various litest test devices
1979 */
1980 static void
1981 litest_install_device_quirks(struct list *created_files_list, const char *dirname)
1982 {
1983 struct litest_test_device *dev;
1984
1985 list_for_each(dev, &devices, node) {
1986 char *path;
1987
1988 path = litest_init_device_quirk_file(dirname, dev);
1989 if (path) {
1990 struct created_file *file = zalloc(sizeof(*file));
1991 file->path = path;
1992 list_insert(created_files_list, &file->link);
1993 }
1994 }
1995 }
1996
1997 static void
1998 litest_setup_quirks(struct list *created_files_list, enum quirks_setup_mode mode)
1999 {
2000 struct created_file *file = NULL;
2001 const char *dirname;
2002 char tmpdir[] = "/run/litest-XXXXXX";
2003
2004 switch (mode) {
2005 case QUIRKS_SETUP_USE_SRCDIR:
2006 if (builddir_lookup(NULL))
2007 dirname = LIBINPUT_QUIRKS_SRCDIR;
2008 else
2009 dirname = LIBINPUT_QUIRKS_DIR;
2010 break;
2011 case QUIRKS_SETUP_ONLY_DEVICE:
2012 dirname = LIBINPUT_QUIRKS_DIR;
2013 litest_install_device_quirks(created_files_list, dirname);
2014 break;
2015 case QUIRKS_SETUP_FULL:
2016 litest_assert_notnull(mkdtemp(tmpdir));
2017 litest_assert_errno_success(chmod(tmpdir, 0755));
2018 file = zalloc(sizeof *file);
2019 file->path = safe_strdup(tmpdir);
2020 dirname = tmpdir;
2021
2022 litest_install_source_quirks(created_files_list, dirname);
2023 litest_install_device_quirks(created_files_list, dirname);
2024 list_append(created_files_list, &file->link);
2025 break;
2026 }
2027
2028 setenv("LIBINPUT_QUIRKS_DIR", dirname, 1);
2029 }
2030
2031 static inline void
2032 litest_init_udev_rules(struct list *created_files)
2033 {
2034 litest_assert_neg_errno_success(mkdir_p(UDEV_RULES_D));
2035
2036 litest_install_model_quirks(created_files);
2037 litest_init_all_device_udev_rules(created_files);
2038 litest_reload_udev_rules();
2039 }
2040
2041 static inline void
2042 litest_remove_udev_rules(struct list *created_files_list)
2043 {
2044 struct created_file *f;
2045 bool reload_udev;
2046
2047 reload_udev = !list_empty(created_files_list);
2048
2049 list_for_each_safe(f, created_files_list, link) {
2050 created_file_unlink(f);
2051 created_file_destroy(f);
2052 }
2053
2054 if (reload_udev)
2055 litest_reload_udev_rules();
2056 }
2057
2058 /**
2059 * Creates a uinput device but does not add it to a libinput context
2060 */
2061 struct litest_device *
2062 litest_create(enum litest_device_type which,
2063 const char *name_override,
2064 struct input_id *id_override,
2065 const struct input_absinfo *abs_override,
2066 const int *events_override)
2067 {
2068 struct litest_device *d = NULL;
2069 struct litest_test_device *dev;
2070 const char *name;
2071 const struct input_id *id;
2072 _autofree_ struct input_absinfo *abs;
2073 _autofree_ int *events;
2074 int *e;
2075 const char *path;
2076 int fd, rc;
2077 bool found = false;
2078 bool create_device = true;
2079
2080 list_for_each(dev, &devices, node) {
2081 if (dev->type == which) {
2082 found = true;
2083 break;
2084 }
2085 }
2086
2087 if (!found)
2088 litest_abort_msg("Invalid device type %d", which);
2089
2090 d = zalloc(sizeof(*d));
2091 d->which = which;
2092
2093 /* device has custom create method */
2094 if (dev->create) {
2095 create_device = dev->create(d);
2096 if (abs_override || events_override) {
2097 litest_abort_msg("Custom create cannot be overridden");
2098 }
2099 }
2100
2101 abs = merge_absinfo(dev->absinfo, abs_override);
2102 events = merge_events(dev->events, events_override);
2103 name = name_override ? name_override : dev->name;
2104 id = id_override ? id_override : dev->id;
2105
2106 if (create_device) {
2107 d->uinput = litest_create_uinput_device_from_description(name,
2108 id,
2109 abs,
2110 events);
2111 d->interface = dev->interface;
2112
2113 for (e = events; *e != -1; e += 2) {
2114 unsigned int type = *e, code = *(e + 1);
2115
2116 if (type == INPUT_PROP_MAX && code == INPUT_PROP_SEMI_MT) {
2117 d->semi_mt.is_semi_mt = true;
2118 break;
2119 }
2120 }
2121 }
2122
2123 path = libevdev_uinput_get_devnode(d->uinput);
2124 litest_assert_ptr_notnull(path);
2125 fd = open(path, O_RDWR | O_NONBLOCK);
2126 litest_assert_errno_success(fd);
2127
2128 rc = libevdev_new_from_fd(fd, &d->evdev);
2129 litest_assert_neg_errno_success(rc);
2130
2131 return d;
2132 }
2133
2134 struct libinput *
2135 litest_create_context_with_plugindir(const char *plugindir)
2136 {
2137 struct libinput *libinput;
2138 struct litest_context *ctx;
2139
2140 ctx = zalloc(sizeof *ctx);
2141 list_init(&ctx->paths);
2142
2143 libinput = libinput_path_create_context(&interface, ctx);
2144 litest_assert_notnull(libinput);
2145
2146 libinput_log_set_handler(libinput, litest_log_handler);
2147 if (verbose)
2148 libinput_log_set_priority(libinput, LIBINPUT_LOG_PRIORITY_DEBUG);
2149
2150 if (plugindir)
2151 libinput_plugin_system_append_path(libinput, plugindir);
2152
2153 return libinput;
2154 }
2155
2156 struct libinput *
2157 litest_create_context(void)
2158 {
2159 struct libinput *li = litest_create_context_with_plugindir(NULL);
2160
2161 libinput_plugin_system_load_plugins(li, LIBINPUT_PLUGIN_SYSTEM_FLAG_NONE);
2162
2163 return li;
2164 }
2165
2166 void
2167 litest_destroy_context(struct libinput *li)
2168 {
2169 if (li) {
2170 _autofree_ struct litest_context *ctx = libinput_get_user_data(li);
2171 litest_assert_ptr_notnull(ctx);
2172 libinput_unref(li);
2173
2174 struct path *p;
2175 list_for_each_safe(p, &ctx->paths, link) {
2176 litest_abort_msg("Device paths should be removed by now");
2177 }
2178 }
2179 }
2180
2181 void
2182 litest_context_set_user_data(struct libinput *li, void *data)
2183 {
2184 struct litest_user_data *litest_data = libinput_get_user_data(li);
2185 litest_assert_ptr_notnull(litest_data);
2186 litest_data->private = data;
2187 }
2188
2189 void *
2190 litest_context_get_user_data(struct libinput *li)
2191 {
2192 struct litest_user_data *litest_data = libinput_get_user_data(li);
2193 litest_assert_ptr_notnull(litest_data);
2194 return litest_data->private;
2195 }
2196
2197 void
2198 litest_disable_log_handler(struct libinput *libinput)
2199 {
2200 libinput_log_set_handler(libinput, NULL);
2201 }
2202
2203 void
2204 litest_restore_log_handler(struct libinput *libinput)
2205 {
2206 libinput_log_set_handler(libinput, litest_log_handler);
2207 if (verbose)
2208 libinput_log_set_priority(libinput, LIBINPUT_LOG_PRIORITY_DEBUG);
2209 }
2210
2211 LIBINPUT_ATTRIBUTE_PRINTF(3, 0)
2212 static void
2213 litest_bug_log_handler(struct libinput *libinput,
2214 enum libinput_log_priority pri,
2215 const char *format,
2216 va_list args)
2217 {
2218 if (pri != LIBINPUT_LOG_PRIORITY_ERROR || strstr(format, "client bug: ") ||
2219 strstr(format, "libinput bug: ") || strstr(format, "kernel bug: "))
2220 return;
2221
2222 /* messages from plugins don't have the string in the format, it's one of the
2223 * args... */
2224 _autofree_ char *msg = strdup_vprintf(format, args);
2225 if (strstr(msg, "plugin bug:"))
2226 return;
2227
2228 litest_abort_msg("Expected bug statement in log msg ('%s'), aborting.", msg);
2229 }
2230
2231 void
2232 litest_set_log_handler_bug(struct libinput *libinput)
2233 {
2234 libinput_log_set_handler(libinput, litest_bug_log_handler);
2235 }
2236
2237 struct litest_device *
2238 litest_add_device_with_overrides(struct libinput *libinput,
2239 enum litest_device_type which,
2240 const char *name_override,
2241 struct input_id *id_override,
2242 const struct input_absinfo *abs_override,
2243 const int *events_override)
2244 {
2245 struct litest_device *d;
2246 const char *path;
2247
2248 d = litest_create(which,
2249 name_override,
2250 id_override,
2251 abs_override,
2252 events_override);
2253
2254 path = libevdev_uinput_get_devnode(d->uinput);
2255 litest_assert_ptr_notnull(path);
2256
2257 d->libinput = libinput;
2258 d->libinput_device = libinput_path_add_device(d->libinput, path);
2259 litest_assert_ptr_notnull(d->libinput_device);
2260 _unref_(udev_device) *ud = libinput_device_get_udev_device(d->libinput_device);
2261 d->quirks = quirks_fetch_for_device(quirks_context, ud);
2262
2263 libinput_device_ref(d->libinput_device);
2264
2265 if (d->interface) {
2266 unsigned int code;
2267
2268 code = ABS_X;
2269 if (!libevdev_has_event_code(d->evdev, EV_ABS, code))
2270 code = ABS_MT_POSITION_X;
2271 if (libevdev_has_event_code(d->evdev, EV_ABS, code)) {
2272 d->interface->min[ABS_X] =
2273 libevdev_get_abs_minimum(d->evdev, code);
2274 d->interface->max[ABS_X] =
2275 libevdev_get_abs_maximum(d->evdev, code);
2276 }
2277
2278 code = ABS_Y;
2279 if (!libevdev_has_event_code(d->evdev, EV_ABS, code))
2280 code = ABS_MT_POSITION_Y;
2281 if (libevdev_has_event_code(d->evdev, EV_ABS, code)) {
2282 d->interface->min[ABS_Y] =
2283 libevdev_get_abs_minimum(d->evdev, code);
2284 d->interface->max[ABS_Y] =
2285 libevdev_get_abs_maximum(d->evdev, code);
2286 }
2287 d->interface->tool_type = BTN_TOOL_PEN;
2288 }
2289 return d;
2290 }
2291
2292 struct litest_device *
2293 litest_add_device(struct libinput *libinput, enum litest_device_type which)
2294 {
2295 return litest_add_device_with_overrides(libinput,
2296 which,
2297 NULL,
2298 NULL,
2299 NULL,
2300 NULL);
2301 }
2302
2303 struct litest_device *
2304 litest_create_device_with_overrides(enum litest_device_type which,
2305 const char *name_override,
2306 struct input_id *id_override,
2307 const struct input_absinfo *abs_override,
2308 const int *events_override)
2309 {
2310 struct litest_device *dev =
2311 litest_add_device_with_overrides(litest_create_context(),
2312 which,
2313 name_override,
2314 id_override,
2315 abs_override,
2316 events_override);
2317 dev->owns_context = true;
2318 return dev;
2319 }
2320
2321 struct litest_device *
2322 litest_create_device(enum litest_device_type which)
2323 {
2324 return litest_create_device_with_overrides(which, NULL, NULL, NULL, NULL);
2325 }
2326
2327 static struct udev_monitor *
2328 udev_setup_monitor(void)
2329 {
2330 _unref_(udev) *udev = udev_new();
2331 _unref_(udev_monitor) *udev_monitor = NULL;
2332 int rc;
2333
2334 litest_assert_notnull(udev);
2335 udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
2336 litest_assert_notnull(udev_monitor);
2337 udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "input", NULL);
2338
2339 /* remove O_NONBLOCK */
2340 rc = fcntl(udev_monitor_get_fd(udev_monitor), F_SETFL, 0);
2341 litest_assert_errno_success(rc);
2342 litest_assert_int_eq(udev_monitor_enable_receiving(udev_monitor), 0);
2343
2344 return steal(&udev_monitor);
2345 }
2346
2347 static struct udev_device *
2348 udev_wait_for_device_event(struct udev_monitor *udev_monitor,
2349 const char *udev_event,
2350 const char *syspath)
2351 {
2352 /* blocking, we don't want to continue until udev is ready */
2353 while (1) {
2354 _unref_(udev_device) *udev_device = NULL;
2355 const char *udev_syspath = NULL;
2356 const char *udev_action;
2357
2358 udev_device = udev_monitor_receive_device(udev_monitor);
2359 if (!udev_device) {
2360 if (errno == EAGAIN)
2361 continue;
2362
2363 litest_abort_msg(
2364 "Failed to receive udev device from monitor: %s (%d)",
2365 strerror(errno),
2366 errno);
2367 }
2368 udev_action = udev_device_get_action(udev_device);
2369 if (!udev_action || !streq(udev_action, udev_event)) {
2370 continue;
2371 }
2372
2373 udev_syspath = udev_device_get_syspath(udev_device);
2374 if (strstartswith(udev_syspath, syspath))
2375 return steal(&udev_device);
2376 }
2377 }
2378
2379 void
2380 litest_device_destroy(struct litest_device *d)
2381 {
2382
2383 _unref_(udev_monitor) *udev_monitor = NULL;
2384 _unref_(udev_device) *udev_device = NULL;
2385 char path[PATH_MAX];
2386
2387 if (!d)
2388 return;
2389
2390 udev_monitor = udev_setup_monitor();
2391 snprintf(path,
2392 sizeof(path),
2393 "%s/event",
2394 libevdev_uinput_get_syspath(d->uinput));
2395
2396 litest_assert_int_eq(d->skip_ev_syn, 0);
2397
2398 quirks_unref(d->quirks);
2399
2400 if (d->libinput_device) {
2401 libinput_path_remove_device(d->libinput_device);
2402 libinput_device_unref(d->libinput_device);
2403 }
2404 if (d->owns_context) {
2405 libinput_dispatch(d->libinput);
2406 litest_destroy_context(d->libinput);
2407 }
2408 close(libevdev_get_fd(d->evdev));
2409 libevdev_free(d->evdev);
2410 libevdev_uinput_destroy(d->uinput);
2411 free(d->private);
2412 memset(d, 0, sizeof(*d));
2413 free(d);
2414
2415 udev_device = // NOLINT: deadcode.DeadStores
2416 udev_wait_for_device_event(udev_monitor, "remove", path);
2417 }
2418
2419 void
2420 litest_event_unchecked(struct litest_device *d,
2421 unsigned int type,
2422 unsigned int code,
2423 int value)
2424 {
2425 int ret = libevdev_uinput_write_event(d->uinput, type, code, value);
2426 litest_assert_neg_errno_success(ret);
2427 }
2428
2429 void
2430 litest_event(struct litest_device *d, unsigned int type, unsigned int code, int value)
2431 {
2432 if (!libevdev_has_event_code(d->evdev, type, code))
2433 return;
2434
2435 if (type == EV_SYN && code == SYN_REPORT) {
2436 if (d->skip_ev_syn)
2437 return;
2438
2439 for (size_t i = 0; i < d->frame.nevents; i++) {
2440 struct input_event *e = &d->frame.events[i];
2441 int ret = libevdev_uinput_write_event(d->uinput,
2442 e->type,
2443 e->code,
2444 e->value);
2445 litest_assert_neg_errno_success(ret);
2446 }
2447
2448 int ret = libevdev_uinput_write_event(d->uinput,
2449 EV_SYN,
2450 SYN_REPORT,
2451 value);
2452 litest_assert_neg_errno_success(ret);
2453
2454 d->frame.nevents = 0;
2455 } else {
2456 size_t i;
2457
2458 if (type == EV_SYN || (type == EV_ABS && code >= ABS_MT_SLOT)) {
2459 i = d->frame.nevents;
2460 } else {
2461 for (i = 0; i < d->frame.nevents; i++) {
2462 if (d->frame.events[i].type == type &&
2463 d->frame.events[i].code == code)
2464 break;
2465 }
2466 }
2467 litest_assert_int_lt(i, ARRAY_LENGTH(d->frame.events));
2468 d->frame.events[i] = (struct input_event){
2469 .type = type,
2470 .code = code,
2471 .value = value,
2472 };
2473 if (i >= d->frame.nevents)
2474 d->frame.nevents++;
2475 }
2476 }
2477
2478 static bool
2479 axis_replacement_value(struct litest_device *d,
2480 struct axis_replacement *axes,
2481 int32_t evcode,
2482 int32_t *value)
2483 {
2484 struct axis_replacement *axis = axes;
2485
2486 if (!axes)
2487 return false;
2488
2489 while (axis->evcode != -1) {
2490 if (axis->evcode == evcode) {
2491 switch (evcode) {
2492 case ABS_MISC:
2493 case ABS_MT_SLOT:
2494 case ABS_MT_TRACKING_ID:
2495 case ABS_MT_TOOL_TYPE:
2496 *value = axis->value;
2497 break;
2498 default:
2499 *value = litest_scale(d, evcode, axis->value);
2500 break;
2501 }
2502 return true;
2503 }
2504 axis++;
2505 }
2506
2507 return false;
2508 }
2509
2510 int
2511 litest_auto_assign_value(struct litest_device *d,
2512 const struct input_event *ev,
2513 int slot,
2514 double x,
2515 double y,
2516 struct axis_replacement *axes,
2517 bool touching)
2518 {
2519 static int tracking_id;
2520 int value = ev->value;
2521
2522 if (value != LITEST_AUTO_ASSIGN || ev->type != EV_ABS)
2523 return value;
2524
2525 switch (ev->code) {
2526 case ABS_X:
2527 case ABS_MT_POSITION_X:
2528 value = litest_scale(d, ABS_X, x);
2529 break;
2530 case ABS_Y:
2531 case ABS_MT_POSITION_Y:
2532 value = litest_scale(d, ABS_Y, y);
2533 break;
2534 case ABS_MT_TRACKING_ID:
2535 value = ++tracking_id;
2536 break;
2537 case ABS_MT_SLOT:
2538 value = slot;
2539 break;
2540 case ABS_MT_DISTANCE:
2541 value = touching ? 0 : 1;
2542 break;
2543 case ABS_MT_TOOL_TYPE:
2544 if (!axis_replacement_value(d, axes, ev->code, &value))
2545 value = MT_TOOL_FINGER;
2546 break;
2547 default:
2548 if (!axis_replacement_value(d, axes, ev->code, &value) &&
2549 d->interface->get_axis_default) {
2550 int error = d->interface->get_axis_default(d, ev->code, &value);
2551 if (error) {
2552 litest_abort_msg(
2553 "Failed to get default axis value for %s (%d)",
2554 libevdev_event_code_get_name(EV_ABS, ev->code),
2555 ev->code);
2556 }
2557 }
2558 break;
2559 }
2560
2561 return value;
2562 }
2563
2564 static void
2565 send_btntool(struct litest_device *d, bool hover)
2566 {
2567 litest_event(d, EV_KEY, BTN_TOUCH, d->ntouches_down != 0 && !hover);
2568 litest_event(d, EV_KEY, BTN_TOOL_FINGER, d->ntouches_down == 1);
2569 litest_event(d, EV_KEY, BTN_TOOL_DOUBLETAP, d->ntouches_down == 2);
2570 litest_event(d, EV_KEY, BTN_TOOL_TRIPLETAP, d->ntouches_down == 3);
2571 litest_event(d, EV_KEY, BTN_TOOL_QUADTAP, d->ntouches_down == 4);
2572 litest_event(d, EV_KEY, BTN_TOOL_QUINTTAP, d->ntouches_down == 5);
2573 }
2574
2575 static void
2576 slot_start(struct litest_device *d,
2577 unsigned int slot,
2578 double x,
2579 double y,
2580 struct axis_replacement *axes,
2581 bool touching,
2582 bool filter_abs_xy)
2583 {
2584 struct input_event *ev;
2585
2586 litest_assert_int_ge(d->ntouches_down, 0);
2587 d->ntouches_down++;
2588
2589 send_btntool(d, !touching);
2590
2591 /* If the test device overrides touch_down and says it didn't
2592 * handle the event, let's continue normally */
2593 if (d->interface->touch_down && d->interface->touch_down(d, slot, x, y))
2594 return;
2595
2596 for (ev = d->interface->touch_down_events;
2597 ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1;
2598 ev++) {
2599 int value = litest_auto_assign_value(d, ev, slot, x, y, axes, touching);
2600 if (value == LITEST_AUTO_ASSIGN)
2601 continue;
2602
2603 if (filter_abs_xy && ev->type == EV_ABS &&
2604 (ev->code == ABS_X || ev->code == ABS_Y))
2605 continue;
2606
2607 litest_event(d, ev->type, ev->code, value);
2608 }
2609 }
2610
2611 static void
2612 slot_move(struct litest_device *d,
2613 unsigned int slot,
2614 double x,
2615 double y,
2616 struct axis_replacement *axes,
2617 bool touching,
2618 bool filter_abs_xy)
2619 {
2620 struct input_event *ev;
2621
2622 if (d->interface->touch_move && d->interface->touch_move(d, slot, x, y))
2623 return;
2624
2625 for (ev = d->interface->touch_move_events;
2626 ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1;
2627 ev++) {
2628 int value = litest_auto_assign_value(d, ev, slot, x, y, axes, touching);
2629 if (value == LITEST_AUTO_ASSIGN)
2630 continue;
2631
2632 if (filter_abs_xy && ev->type == EV_ABS &&
2633 (ev->code == ABS_X || ev->code == ABS_Y))
2634 continue;
2635
2636 litest_event(d, ev->type, ev->code, value);
2637 }
2638 }
2639
2640 static void
2641 touch_up(struct litest_device *d, unsigned int slot)
2642 {
2643 struct input_event *ev;
2644 struct input_event up[] = {
2645 { .type = EV_ABS, .code = ABS_MT_SLOT, .value = LITEST_AUTO_ASSIGN },
2646 { .type = EV_ABS, .code = ABS_MT_TRACKING_ID, .value = -1 },
2647 { .type = EV_ABS, .code = ABS_MT_PRESSURE, .value = 0 },
2648 { .type = EV_ABS, .code = ABS_MT_TOUCH_MAJOR, .value = 0 },
2649 { .type = EV_ABS, .code = ABS_MT_TOUCH_MINOR, .value = 0 },
2650 { .type = EV_SYN, .code = SYN_REPORT, .value = 0 },
2651 { .type = -1, .code = -1 }
2652 };
2653
2654 litest_assert_int_gt(d->ntouches_down, 0);
2655 d->ntouches_down--;
2656
2657 send_btntool(d, false);
2658
2659 if (d->interface->touch_up && d->interface->touch_up(d, slot)) {
2660 return;
2661 } else if (d->interface->touch_up_events) {
2662 ev = d->interface->touch_up_events;
2663 } else
2664 ev = up;
2665
2666 for (/* */; ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1; ev++) {
2667 int value = litest_auto_assign_value(d, ev, slot, 0, 0, NULL, false);
2668 litest_event(d, ev->type, ev->code, value);
2669 }
2670 }
2671
2672 static void
2673 litest_slot_start(struct litest_device *d,
2674 unsigned int slot,
2675 double x,
2676 double y,
2677 struct axis_replacement *axes,
2678 bool touching)
2679 {
2680 double t, l, r = 0, b = 0; /* top, left, right, bottom */
2681 bool filter_abs_xy = false;
2682
2683 if (!d->semi_mt.is_semi_mt) {
2684 slot_start(d, slot, x, y, axes, touching, filter_abs_xy);
2685 return;
2686 }
2687
2688 if (d->ntouches_down >= 2 || slot > 1)
2689 return;
2690
2691 slot = d->ntouches_down;
2692
2693 if (d->ntouches_down == 0) {
2694 l = x;
2695 t = y;
2696 } else {
2697 int other = (slot + 1) % 2;
2698 l = min(x, d->semi_mt.touches[other].x);
2699 t = min(y, d->semi_mt.touches[other].y);
2700 r = max(x, d->semi_mt.touches[other].x);
2701 b = max(y, d->semi_mt.touches[other].y);
2702 }
2703
2704 litest_with_event_frame(d) {
2705 if (d->ntouches_down == 0)
2706 slot_start(d, 0, l, t, axes, touching, filter_abs_xy);
2707 else
2708 slot_move(d, 0, l, t, axes, touching, filter_abs_xy);
2709
2710 if (slot == 1) {
2711 filter_abs_xy = true;
2712 slot_start(d, 1, r, b, axes, touching, filter_abs_xy);
2713 }
2714 }
2715
2716 d->semi_mt.touches[slot].x = x;
2717 d->semi_mt.touches[slot].y = y;
2718 }
2719
2720 void
2721 litest_touch_sequence(struct litest_device *d,
2722 unsigned int slot,
2723 double x_from,
2724 double y_from,
2725 double x_to,
2726 double y_to,
2727 int steps)
2728 {
2729 litest_touch_down(d, slot, x_from, y_from);
2730 litest_touch_move_to(d, slot, x_from, y_from, x_to, y_to, steps);
2731 litest_touch_up(d, slot);
2732 }
2733
2734 void
2735 litest_touch_down(struct litest_device *d, unsigned int slot, double x, double y)
2736 {
2737 litest_slot_start(d, slot, x, y, NULL, true);
2738 }
2739
2740 void
2741 litest_touch_down_extended(struct litest_device *d,
2742 unsigned int slot,
2743 double x,
2744 double y,
2745 struct axis_replacement *axes)
2746 {
2747 litest_slot_start(d, slot, x, y, axes, true);
2748 }
2749
2750 static void
2751 litest_slot_move(struct litest_device *d,
2752 unsigned int slot,
2753 double x,
2754 double y,
2755 struct axis_replacement *axes,
2756 bool touching)
2757 {
2758 double t, l, r = 0, b = 0; /* top, left, right, bottom */
2759 bool filter_abs_xy = false;
2760
2761 if (!d->semi_mt.is_semi_mt) {
2762 slot_move(d, slot, x, y, axes, touching, filter_abs_xy);
2763 return;
2764 }
2765
2766 if (d->ntouches_down > 2 || slot > 1)
2767 return;
2768
2769 if (d->ntouches_down == 1) {
2770 l = x;
2771 t = y;
2772 } else {
2773 int other = (slot + 1) % 2;
2774 l = min(x, d->semi_mt.touches[other].x);
2775 t = min(y, d->semi_mt.touches[other].y);
2776 r = max(x, d->semi_mt.touches[other].x);
2777 b = max(y, d->semi_mt.touches[other].y);
2778 }
2779
2780 litest_with_event_frame(d) {
2781 slot_move(d, 0, l, t, axes, touching, filter_abs_xy);
2782
2783 if (d->ntouches_down == 2) {
2784 filter_abs_xy = true;
2785 slot_move(d, 1, r, b, axes, touching, filter_abs_xy);
2786 }
2787 }
2788
2789 d->semi_mt.touches[slot].x = x;
2790 d->semi_mt.touches[slot].y = y;
2791 }
2792
2793 void
2794 litest_touch_up(struct litest_device *d, unsigned int slot)
2795 {
2796 if (!d->semi_mt.is_semi_mt) {
2797 touch_up(d, slot);
2798 return;
2799 }
2800
2801 if (d->ntouches_down > 2 || slot > 1)
2802 return;
2803
2804 litest_with_event_frame(d) {
2805 touch_up(d, d->ntouches_down - 1);
2806
2807 /* if we have one finger left, send x/y coords for that finger left.
2808 this is likely to happen with a real touchpad */
2809 if (d->ntouches_down == 1) {
2810 bool touching = true;
2811 bool filter_abs_xy = false;
2812
2813 int other = (slot + 1) % 2;
2814 slot_move(d,
2815 0,
2816 d->semi_mt.touches[other].x,
2817 d->semi_mt.touches[other].y,
2818 NULL,
2819 touching,
2820 filter_abs_xy);
2821 }
2822 }
2823 }
2824
2825 void
2826 litest_touch_move(struct litest_device *d, unsigned int slot, double x, double y)
2827 {
2828 litest_slot_move(d, slot, x, y, NULL, true);
2829 }
2830
2831 void
2832 litest_touch_move_extended(struct litest_device *d,
2833 unsigned int slot,
2834 double x,
2835 double y,
2836 struct axis_replacement *axes)
2837 {
2838 litest_slot_move(d, slot, x, y, axes, true);
2839 }
2840
2841 void
2842 litest_touch_move_to(struct litest_device *d,
2843 unsigned int slot,
2844 double x_from,
2845 double y_from,
2846 double x_to,
2847 double y_to,
2848 int steps)
2849 {
2850 litest_touch_move_to_extended(d, slot, x_from, y_from, x_to, y_to, NULL, steps);
2851 }
2852
2853 void
2854 litest_touch_move_to_extended(struct litest_device *d,
2855 unsigned int slot,
2856 double x_from,
2857 double y_from,
2858 double x_to,
2859 double y_to,
2860 struct axis_replacement *axes,
2861 int steps)
2862 {
2863 int sleep_ms = 10;
2864
2865 for (int i = 1; i < steps; i++) {
2866 litest_touch_move_extended(d,
2867 slot,
2868 x_from + (x_to - x_from) / steps * i,
2869 y_from + (y_to - y_from) / steps * i,
2870 axes);
2871 libinput_dispatch(d->libinput);
2872 msleep(sleep_ms);
2873 libinput_dispatch(d->libinput);
2874 }
2875 litest_touch_move_extended(d, slot, x_to, y_to, axes);
2876 }
2877
2878 static int
2879 auto_assign_tablet_value(struct litest_device *d,
2880 const struct input_event *ev,
2881 int x,
2882 int y,
2883 struct axis_replacement *axes)
2884 {
2885 static int tracking_id;
2886 int value = ev->value;
2887
2888 if (ev->type != EV_ABS)
2889 return value;
2890
2891 switch (ev->code) {
2892 case ABS_MT_TRACKING_ID:
2893 value = ++tracking_id;
2894 break;
2895 case ABS_X:
2896 case ABS_MT_POSITION_X:
2897 value = litest_scale(d, ABS_X, x);
2898 break;
2899 case ABS_Y:
2900 case ABS_MT_POSITION_Y:
2901 value = litest_scale(d, ABS_Y, y);
2902 break;
2903 default:
2904 if (!axis_replacement_value(d, axes, ev->code, &value) &&
2905 value == LITEST_AUTO_ASSIGN && d->interface->get_axis_default) {
2906 int error = d->interface->get_axis_default(d, ev->code, &value);
2907 if (error) {
2908 litest_abort_msg(
2909 "Failed to get default axis value for %s (%d)",
2910 libevdev_event_code_get_name(EV_ABS, ev->code),
2911 ev->code);
2912 }
2913 }
2914 break;
2915 }
2916
2917 return value;
2918 }
2919
2920 static int
2921 tablet_ignore_event(const struct input_event *ev, int value)
2922 {
2923 return value == -1 && (ev->code == ABS_PRESSURE || ev->code == ABS_DISTANCE);
2924 }
2925
2926 void
2927 litest_tablet_set_tool_type(struct litest_device *d, unsigned int code)
2928 {
2929 switch (code) {
2930 case BTN_TOOL_PEN:
2931 case BTN_TOOL_RUBBER:
2932 case BTN_TOOL_BRUSH:
2933 case BTN_TOOL_PENCIL:
2934 case BTN_TOOL_AIRBRUSH:
2935 case BTN_TOOL_MOUSE:
2936 case BTN_TOOL_LENS:
2937 break;
2938 default:
2939 abort();
2940 }
2941
2942 d->interface->tool_type = code;
2943 }
2944
2945 static void
2946 litest_tool_event(struct litest_device *d, int value)
2947 {
2948 unsigned int tool = d->interface->tool_type;
2949
2950 litest_event(d, EV_KEY, tool, value);
2951 }
2952
2953 void
2954 litest_tablet_proximity_in(struct litest_device *d,
2955 double x,
2956 double y,
2957 struct axis_replacement *axes)
2958 {
2959 struct input_event *ev;
2960
2961 /* If the test device overrides proximity_in and says it didn't
2962 * handle the event, let's continue normally */
2963 if (d->interface->tablet_proximity_in &&
2964 d->interface->tablet_proximity_in(d, d->interface->tool_type, &x, &y, axes))
2965 return;
2966
2967 ev = d->interface->tablet_proximity_in_events;
2968 while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
2969 int value;
2970
2971 switch (evbit(ev->type, ev->code)) {
2972 case evbit(EV_KEY, LITEST_BTN_TOOL_AUTO):
2973 litest_tool_event(d, ev->value);
2974 break;
2975 default:
2976 value = auto_assign_tablet_value(d, ev, x, y, axes);
2977 if (!tablet_ignore_event(ev, value))
2978 litest_event(d, ev->type, ev->code, value);
2979 }
2980 ev++;
2981 }
2982 }
2983
2984 void
2985 litest_tablet_proximity_out(struct litest_device *d)
2986 {
2987 struct input_event *ev;
2988
2989 /* If the test device overrides proximity_out and says it didn't
2990 * handle the event, let's continue normally */
2991 if (d->interface->tablet_proximity_out &&
2992 d->interface->tablet_proximity_out(d, d->interface->tool_type))
2993 return;
2994
2995 ev = d->interface->tablet_proximity_out_events;
2996 while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
2997 int value;
2998
2999 switch (evbit(ev->type, ev->code)) {
3000 case evbit(EV_KEY, LITEST_BTN_TOOL_AUTO):
3001 litest_tool_event(d, ev->value);
3002 break;
3003 case evbit(EV_ABS, ABS_MT_TRACKING_ID):
3004 if (ev->value == -1) {
3005 litest_event(d, ev->type, ev->code, ev->value);
3006 break;
3007 }
3008 _fallthrough_;
3009 default:
3010 value = auto_assign_tablet_value(d, ev, 0, 0, NULL);
3011 if (!tablet_ignore_event(ev, value))
3012 litest_event(d, ev->type, ev->code, value);
3013 break;
3014 }
3015 ev++;
3016 }
3017 }
3018
3019 void
3020 litest_tablet_motion(struct litest_device *d,
3021 double x,
3022 double y,
3023 struct axis_replacement *axes)
3024 {
3025 struct input_event *ev;
3026
3027 /* If the test device overrides proximity_out and says it didn't
3028 * handle the event, let's continue normally */
3029 if (d->interface->tablet_motion && d->interface->tablet_motion(d, &x, &y, axes))
3030 return;
3031
3032 ev = d->interface->tablet_motion_events;
3033 while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
3034 int value = auto_assign_tablet_value(d, ev, x, y, axes);
3035 if (!tablet_ignore_event(ev, value))
3036 litest_event(d, ev->type, ev->code, value);
3037 ev++;
3038 }
3039 }
3040
3041 void
3042 litest_tablet_tip_down(struct litest_device *d,
3043 double x,
3044 double y,
3045 struct axis_replacement *axes)
3046 {
3047 /* If the test device overrides tip_down and says it didn't
3048 * handle the event, let's continue normally */
3049 if (d->interface->tablet_tip_down &&
3050 d->interface->tablet_tip_down(d, &x, &y, axes))
3051 return;
3052
3053 litest_event(d, EV_KEY, BTN_TOUCH, 1);
3054 litest_tablet_motion(d, x, y, axes);
3055 }
3056
3057 void
3058 litest_tablet_tip_up(struct litest_device *d,
3059 double x,
3060 double y,
3061 struct axis_replacement *axes)
3062 {
3063 /* If the test device overrides tip_down and says it didn't
3064 * handle the event, let's continue normally */
3065 if (d->interface->tablet_tip_up && d->interface->tablet_tip_up(d, &x, &y, axes))
3066 return;
3067
3068 litest_event(d, EV_KEY, BTN_TOUCH, 0);
3069 litest_tablet_motion(d, x, y, axes);
3070 }
3071
3072 void
3073 litest_touch_move_two_touches(struct litest_device *d,
3074 double x0,
3075 double y0,
3076 double x1,
3077 double y1,
3078 double dx,
3079 double dy,
3080 int steps)
3081 {
3082 int sleep_ms = 10;
3083
3084 for (int i = 1; i < steps; i++) {
3085 litest_with_event_frame(d) {
3086 litest_touch_move(d,
3087 0,
3088 x0 + dx / steps * i,
3089 y0 + dy / steps * i);
3090 litest_touch_move(d,
3091 1,
3092 x1 + dx / steps * i,
3093 y1 + dy / steps * i);
3094 }
3095 libinput_dispatch(d->libinput);
3096 msleep(sleep_ms);
3097 libinput_dispatch(d->libinput);
3098 }
3099 litest_with_event_frame(d) {
3100 litest_touch_move(d, 0, x0 + dx, y0 + dy);
3101 litest_touch_move(d, 1, x1 + dx, y1 + dy);
3102 }
3103 }
3104
3105 void
3106 litest_touch_move_three_touches(struct litest_device *d,
3107 double x0,
3108 double y0,
3109 double x1,
3110 double y1,
3111 double x2,
3112 double y2,
3113 double dx,
3114 double dy,
3115 int steps)
3116 {
3117 int sleep_ms = 10;
3118
3119 for (int i = 1; i <= steps; i++) {
3120 double step_x = dx / steps * i;
3121 double step_y = dy / steps * i;
3122
3123 litest_with_event_frame(d) {
3124 litest_touch_move(d, 0, x0 + step_x, y0 + step_y);
3125 litest_touch_move(d, 1, x1 + step_x, y1 + step_y);
3126 litest_touch_move(d, 2, x2 + step_x, y2 + step_y);
3127 }
3128
3129 libinput_dispatch(d->libinput);
3130 msleep(sleep_ms);
3131 }
3132 libinput_dispatch(d->libinput);
3133 }
3134
3135 void
3136 litest_hover_start(struct litest_device *d, unsigned int slot, double x, double y)
3137 {
3138 struct axis_replacement axes[] = {
3139 { ABS_MT_PRESSURE, 0 },
3140 { ABS_PRESSURE, 0 },
3141 { -1, -1 },
3142 };
3143
3144 litest_slot_start(d, slot, x, y, axes, 0);
3145 }
3146
3147 void
3148 litest_hover_end(struct litest_device *d, unsigned int slot)
3149 {
3150 struct input_event *ev;
3151 struct input_event up[] = {
3152 { .type = EV_ABS, .code = ABS_MT_SLOT, .value = LITEST_AUTO_ASSIGN },
3153 { .type = EV_ABS, .code = ABS_MT_DISTANCE, .value = 1 },
3154 { .type = EV_ABS, .code = ABS_MT_TRACKING_ID, .value = -1 },
3155 { .type = EV_SYN, .code = SYN_REPORT, .value = 0 },
3156 { .type = -1, .code = -1 }
3157 };
3158
3159 litest_assert_int_gt(d->ntouches_down, 0);
3160 d->ntouches_down--;
3161
3162 send_btntool(d, true);
3163
3164 if (d->interface->touch_up) {
3165 d->interface->touch_up(d, slot);
3166 return;
3167 } else if (d->interface->touch_up_events) {
3168 ev = d->interface->touch_up_events;
3169 } else
3170 ev = up;
3171
3172 while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
3173 int value = litest_auto_assign_value(d, ev, slot, 0, 0, NULL, false);
3174 litest_event(d, ev->type, ev->code, value);
3175 ev++;
3176 }
3177 }
3178
3179 void
3180 litest_hover_move(struct litest_device *d, unsigned int slot, double x, double y)
3181 {
3182 struct axis_replacement axes[] = {
3183 { ABS_MT_PRESSURE, 0 },
3184 { ABS_PRESSURE, 0 },
3185 { -1, -1 },
3186 };
3187
3188 litest_slot_move(d, slot, x, y, axes, false);
3189 }
3190
3191 void
3192 litest_hover_move_to(struct litest_device *d,
3193 unsigned int slot,
3194 double x_from,
3195 double y_from,
3196 double x_to,
3197 double y_to,
3198 int steps)
3199 {
3200 int sleep_ms = 10;
3201
3202 for (int i = 0; i < steps - 1; i++) {
3203 litest_hover_move(d,
3204 slot,
3205 x_from + (x_to - x_from) / steps * i,
3206 y_from + (y_to - y_from) / steps * i);
3207 libinput_dispatch(d->libinput);
3208 msleep(sleep_ms);
3209 libinput_dispatch(d->libinput);
3210 }
3211 litest_hover_move(d, slot, x_to, y_to);
3212 }
3213
3214 void
3215 litest_hover_move_two_touches(struct litest_device *d,
3216 double x0,
3217 double y0,
3218 double x1,
3219 double y1,
3220 double dx,
3221 double dy,
3222 int steps)
3223 {
3224 int sleep_ms = 10;
3225
3226 for (int i = 0; i < steps - 1; i++) {
3227 litest_with_event_frame(d) {
3228 litest_hover_move(d,
3229 0,
3230 x0 + dx / steps * i,
3231 y0 + dy / steps * i);
3232 litest_hover_move(d,
3233 1,
3234 x1 + dx / steps * i,
3235 y1 + dy / steps * i);
3236 }
3237 libinput_dispatch(d->libinput);
3238 msleep(sleep_ms);
3239 libinput_dispatch(d->libinput);
3240 }
3241 litest_with_event_frame(d) {
3242 litest_hover_move(d, 0, x0 + dx, y0 + dy);
3243 litest_hover_move(d, 1, x1 + dx, y1 + dy);
3244 }
3245 }
3246
3247 void
3248 litest_button_click(struct litest_device *d, unsigned int button, bool is_press)
3249 {
3250 struct input_event click[] = {
3251 { .type = EV_KEY, .code = button, .value = is_press ? 1 : 0 },
3252 { .type = EV_SYN, .code = SYN_REPORT, .value = 0 },
3253 };
3254
3255 ARRAY_FOR_EACH(click, ev)
3256 litest_event(d, ev->type, ev->code, ev->value);
3257 }
3258
3259 void
3260 litest_button_click_debounced(struct litest_device *d,
3261 struct libinput *li,
3262 unsigned int button,
3263 bool is_press)
3264 {
3265 litest_button_click(d, button, is_press);
3266 litest_timeout_debounce(li);
3267 }
3268
3269 void
3270 litest_button_scroll(struct litest_device *dev,
3271 unsigned int button,
3272 double dx,
3273 double dy)
3274 {
3275 struct libinput *li = dev->libinput;
3276
3277 litest_button_click_debounced(dev, li, button, 1);
3278
3279 litest_timeout_buttonscroll(li);
3280
3281 litest_event(dev, EV_REL, REL_X, dx);
3282 litest_event(dev, EV_REL, REL_Y, dy);
3283 litest_event(dev, EV_SYN, SYN_REPORT, 0);
3284
3285 litest_button_click_debounced(dev, li, button, 0);
3286
3287 libinput_dispatch(li);
3288 }
3289
3290 void
3291 litest_button_scroll_locked(struct litest_device *dev,
3292 unsigned int button,
3293 double dx,
3294 double dy)
3295 {
3296 struct libinput *li = dev->libinput;
3297
3298 litest_button_click_debounced(dev, li, button, 1);
3299 litest_button_click_debounced(dev, li, button, 0);
3300
3301 litest_timeout_buttonscroll(li);
3302
3303 litest_event(dev, EV_REL, REL_X, dx);
3304 litest_event(dev, EV_REL, REL_Y, dy);
3305 litest_event(dev, EV_SYN, SYN_REPORT, 0);
3306
3307 libinput_dispatch(li);
3308 }
3309
3310 void
3311 litest_keyboard_key(struct litest_device *d, unsigned int key, bool is_press)
3312 {
3313 struct input_event click[] = {
3314 { .type = EV_KEY, .code = key, .value = is_press ? 1 : 0 },
3315 { .type = EV_SYN, .code = SYN_REPORT, .value = 0 },
3316 };
3317
3318 ARRAY_FOR_EACH(click, ev)
3319 litest_event(d, ev->type, ev->code, ev->value);
3320 }
3321
3322 void
3323 litest_switch_action(struct litest_device *dev,
3324 enum libinput_switch sw,
3325 enum libinput_switch_state state)
3326 {
3327 unsigned int code;
3328
3329 switch (sw) {
3330 case LIBINPUT_SWITCH_LID:
3331 code = SW_LID;
3332 break;
3333 case LIBINPUT_SWITCH_TABLET_MODE:
3334 code = SW_TABLET_MODE;
3335 break;
3336 case LIBINPUT_SWITCH_KEYPAD_SLIDE:
3337 code = SW_KEYPAD_SLIDE;
3338 break;
3339 default:
3340 litest_abort_msg("Invalid switch %d", sw);
3341 break;
3342 }
3343 litest_event(dev, EV_SW, code, state);
3344 litest_event(dev, EV_SYN, SYN_REPORT, 0);
3345 }
3346
3347 static int
3348 litest_scale_axis(const struct litest_device *d, unsigned int axis, double val)
3349 {
3350 const struct input_absinfo *abs;
3351
3352 litest_assert_double_ge(val, 0.0);
3353 /* major/minor must be able to beyond 100% for large fingers */
3354 if (axis != ABS_MT_TOUCH_MAJOR && axis != ABS_MT_TOUCH_MINOR) {
3355 litest_assert_double_le(val, 100.0);
3356 }
3357
3358 abs = libevdev_get_abs_info(d->evdev, axis);
3359 litest_assert_notnull(abs);
3360
3361 return (abs->maximum - abs->minimum) * val / 100.0 + abs->minimum;
3362 }
3363
3364 static inline int
3365 litest_scale_range(int min, int max, double val)
3366 {
3367 litest_assert_int_ge((int)val, 0);
3368 litest_assert_int_le((int)val, 100);
3369
3370 return (max - min) * val / 100.0 + min;
3371 }
3372
3373 int
3374 litest_scale(const struct litest_device *d, unsigned int axis, double val)
3375 {
3376 int min, max;
3377
3378 litest_assert_double_ge(val, 0.0);
3379 /* major/minor must be able to beyond 100% for large fingers */
3380 if (axis != ABS_MT_TOUCH_MAJOR && axis != ABS_MT_TOUCH_MINOR)
3381 litest_assert_double_le(val, 100.0);
3382
3383 if (axis <= ABS_Y) {
3384 min = d->interface->min[axis];
3385 max = d->interface->max[axis];
3386
3387 return litest_scale_range(min, max, val);
3388 } else {
3389 return litest_scale_axis(d, axis, val);
3390 }
3391 }
3392
3393 static inline int
3394 auto_assign_pad_value(struct litest_device *dev, struct input_event *ev, double value)
3395 {
3396 const struct input_absinfo *abs;
3397
3398 if (ev->value != LITEST_AUTO_ASSIGN)
3399 return value;
3400
3401 if (ev->type == EV_REL) {
3402 switch (ev->code) {
3403 case REL_WHEEL:
3404 case REL_HWHEEL:
3405 case REL_DIAL:
3406 assert(fmod(value, 120.0) ==
3407 0.0); /* Fractions not supported yet */
3408 return value / 120.0;
3409 default:
3410 return value;
3411 }
3412 } else if (ev->type != EV_ABS) {
3413 return value;
3414 }
3415
3416 abs = libevdev_get_abs_info(dev->evdev, ev->code);
3417 litest_assert_notnull(abs);
3418
3419 if (ev->code == ABS_RX || ev->code == ABS_RY) {
3420 double min = abs->minimum != 0 ? log2(abs->minimum) : 0,
3421 max = abs->maximum != 0 ? log2(abs->maximum) : 0;
3422
3423 /* Value 0 is reserved for finger up, so a value of 0% is
3424 * actually 1 */
3425 if (value == 0.0) {
3426 return 1;
3427 } else {
3428 value = litest_scale_range(min, max, value);
3429 return pow(2, value);
3430 }
3431 } else {
3432 return litest_scale_range(abs->minimum, abs->maximum, value);
3433 }
3434 }
3435
3436 static void
3437 litest_pad_events(struct litest_device *d, struct input_event *evs, double value)
3438 {
3439 while (evs && (int16_t)evs->type != -1 && (int16_t)evs->code != -1) {
3440 if (evs->type == EV_SYN && evs->code == SYN_REPORT)
3441 value = 0;
3442 value = auto_assign_pad_value(d, evs, value);
3443 litest_event(d, evs->type, evs->code, value);
3444 evs++;
3445 }
3446 }
3447
3448 void
3449 litest_pad_ring_start(struct litest_device *d, double value)
3450 {
3451 litest_pad_events(d, d->interface->pad_ring_start_events, value);
3452 }
3453
3454 void
3455 litest_pad_ring_change(struct litest_device *d, double value)
3456 {
3457 litest_pad_events(d, d->interface->pad_ring_change_events, value);
3458 }
3459
3460 void
3461 litest_pad_ring_end(struct litest_device *d)
3462 {
3463 struct input_event *ev;
3464
3465 ev = d->interface->pad_ring_end_events;
3466 while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
3467 litest_event(d, ev->type, ev->code, ev->value);
3468 ev++;
3469 }
3470 }
3471
3472 void
3473 litest_pad_strip_start(struct litest_device *d, double value)
3474 {
3475 litest_pad_events(d, d->interface->pad_strip_start_events, value);
3476 }
3477
3478 void
3479 litest_pad_strip_change(struct litest_device *d, double value)
3480 {
3481 litest_pad_events(d, d->interface->pad_strip_change_events, value);
3482 }
3483
3484 void
3485 litest_pad_strip_end(struct litest_device *d)
3486 {
3487 struct input_event *ev;
3488
3489 ev = d->interface->pad_strip_end_events;
3490 while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
3491 litest_event(d, ev->type, ev->code, ev->value);
3492 ev++;
3493 }
3494 }
3495
3496 void
3497 litest_wait_for_event(struct libinput *li)
3498 {
3499 return litest_wait_for_event_of_type(li, -1);
3500 }
3501
3502 void
3503 _litest_wait_for_event_of_type(struct libinput *li, const char *func, int lineno, ...)
3504 {
3505 va_list args;
3506 enum libinput_event_type types[32] = { LIBINPUT_EVENT_NONE };
3507 size_t ntypes = 0;
3508 enum libinput_event_type type;
3509 struct pollfd fds;
3510
3511 va_start(args, lineno);
3512 type = va_arg(args, int);
3513 while ((int)type != -1) {
3514 litest_assert_int_gt(type, 0U);
3515 litest_assert_int_lt(ntypes, ARRAY_LENGTH(types));
3516 types[ntypes++] = type;
3517 type = va_arg(args, int);
3518 }
3519 va_end(args);
3520
3521 fds.fd = libinput_get_fd(li);
3522 fds.events = POLLIN;
3523 fds.revents = 0;
3524
3525 const int timeout = 2000;
3526 usec_t expiry = usec_from_uint64_t(0);
3527 int rc = now_in_us(&expiry);
3528 expiry = usec_add_millis(expiry, timeout);
3529 litest_assert_errno_success(rc);
3530
3531 while (1) {
3532 size_t i;
3533 enum libinput_event_type type;
3534
3535 while ((type = libinput_next_event_type(li)) == LIBINPUT_EVENT_NONE) {
3536 int rc = poll(&fds, 1, timeout);
3537 litest_assert_errno_success(rc);
3538 litest_assert_int_gt(rc, 0);
3539 litest_dispatch(li);
3540 }
3541
3542 if (type == LIBINPUT_EVENT_NONE) {
3543 usec_t now;
3544 now_in_us(&now);
3545 if (usec_cmp(now, expiry) > 0) {
3546 _litest_abort_msg(
3547 NULL,
3548 lineno,
3549 func,
3550 "Waited >%dms for events, but no events are pending",
3551 timeout);
3552 }
3553 }
3554
3555 /* no event mask means wait for any event */
3556 if (ntypes == 0)
3557 return;
3558
3559 for (i = 0; i < ntypes; i++) {
3560 if (type == types[i])
3561 return;
3562 }
3563
3564 _destroy_(libinput_event) *event = libinput_get_event(li);
3565 if (verbose) {
3566 litest_print_event(event, "Discarding event while waiting: ");
3567 }
3568 }
3569 }
3570
3571 void
3572 litest_drain_events(struct libinput *li)
3573 {
3574 do {
3575 libinput_dispatch(li);
3576
3577 _destroy_(libinput_event) *event = libinput_get_event(li);
3578 if (!event)
3579 break;
3580
3581 if (verbose) {
3582 litest_print_event(event, "litest: draining event: ");
3583 }
3584 } while (true);
3585 }
3586
3587 void
3588 _litest_drain_events_of_type(struct libinput *li, ...)
3589 {
3590 enum libinput_event_type type;
3591 enum libinput_event_type types[32] = { LIBINPUT_EVENT_NONE };
3592 size_t ntypes = 0;
3593 va_list args;
3594
3595 va_start(args, li);
3596 type = va_arg(args, int);
3597 while ((int)type != -1) {
3598 litest_assert_int_gt(type, 0U);
3599 litest_assert_int_lt(ntypes, ARRAY_LENGTH(types));
3600 types[ntypes++] = type;
3601 type = va_arg(args, int);
3602 }
3603 va_end(args);
3604
3605 libinput_dispatch(li);
3606 type = libinput_next_event_type(li);
3607 while (type != LIBINPUT_EVENT_NONE) {
3608 bool found = false;
3609
3610 type = libinput_next_event_type(li);
3611
3612 for (size_t i = 0; i < ntypes; i++) {
3613 if (type == types[i]) {
3614 found = true;
3615 break;
3616 }
3617 }
3618 if (!found)
3619 return;
3620
3621 _destroy_(libinput_event) *event = libinput_get_event(li);
3622 if (verbose)
3623 litest_print_event(event, "litest: draining typed event: ");
3624 libinput_dispatch(li);
3625 }
3626 }
3627
3628 const char *
3629 litest_event_type_str(enum libinput_event_type type)
3630 {
3631 const char *str = NULL;
3632
3633 switch (type) {
3634 case LIBINPUT_EVENT_NONE:
3635 abort();
3636 case LIBINPUT_EVENT_DEVICE_ADDED:
3637 str = "ADDED";
3638 break;
3639 case LIBINPUT_EVENT_DEVICE_REMOVED:
3640 str = "REMOVED";
3641 break;
3642 case LIBINPUT_EVENT_KEYBOARD_KEY:
3643 str = "KEY";
3644 break;
3645 case LIBINPUT_EVENT_POINTER_MOTION:
3646 str = "MOTION";
3647 break;
3648 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
3649 str = "ABSOLUTE";
3650 break;
3651 case LIBINPUT_EVENT_POINTER_BUTTON:
3652 str = "BUTTON";
3653 break;
3654 case LIBINPUT_EVENT_POINTER_AXIS:
3655 str = "AXIS";
3656 break;
3657 case LIBINPUT_EVENT_POINTER_SCROLL_WHEEL:
3658 str = "SCROLL_WHEEL";
3659 break;
3660 case LIBINPUT_EVENT_POINTER_SCROLL_FINGER:
3661 str = "SCROLL_FINGER";
3662 break;
3663 case LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS:
3664 str = "SCROLL_CONTINUOUS";
3665 break;
3666 case LIBINPUT_EVENT_TOUCH_DOWN:
3667 str = "TOUCH DOWN";
3668 break;
3669 case LIBINPUT_EVENT_TOUCH_UP:
3670 str = "TOUCH UP";
3671 break;
3672 case LIBINPUT_EVENT_TOUCH_MOTION:
3673 str = "TOUCH MOTION";
3674 break;
3675 case LIBINPUT_EVENT_TOUCH_CANCEL:
3676 str = "TOUCH CANCEL";
3677 break;
3678 case LIBINPUT_EVENT_TOUCH_FRAME:
3679 str = "TOUCH FRAME";
3680 break;
3681 case LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN:
3682 str = "GESTURE SWIPE BEGIN";
3683 break;
3684 case LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE:
3685 str = "GESTURE SWIPE UPDATE";
3686 break;
3687 case LIBINPUT_EVENT_GESTURE_SWIPE_END:
3688 str = "GESTURE SWIPE END";
3689 break;
3690 case LIBINPUT_EVENT_GESTURE_PINCH_BEGIN:
3691 str = "GESTURE PINCH BEGIN";
3692 break;
3693 case LIBINPUT_EVENT_GESTURE_PINCH_UPDATE:
3694 str = "GESTURE PINCH UPDATE";
3695 break;
3696 case LIBINPUT_EVENT_GESTURE_PINCH_END:
3697 str = "GESTURE PINCH END";
3698 break;
3699 case LIBINPUT_EVENT_GESTURE_HOLD_BEGIN:
3700 str = "GESTURE HOLD BEGIN";
3701 break;
3702 case LIBINPUT_EVENT_GESTURE_HOLD_END:
3703 str = "GESTURE HOLD END";
3704 break;
3705 case LIBINPUT_EVENT_TABLET_TOOL_AXIS:
3706 str = "TABLET TOOL AXIS";
3707 break;
3708 case LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY:
3709 str = "TABLET TOOL PROX";
3710 break;
3711 case LIBINPUT_EVENT_TABLET_TOOL_TIP:
3712 str = "TABLET TOOL TIP";
3713 break;
3714 case LIBINPUT_EVENT_TABLET_TOOL_BUTTON:
3715 str = "TABLET TOOL BUTTON";
3716 break;
3717 case LIBINPUT_EVENT_TABLET_PAD_BUTTON:
3718 str = "TABLET PAD BUTTON";
3719 break;
3720 case LIBINPUT_EVENT_TABLET_PAD_RING:
3721 str = "TABLET PAD RING";
3722 break;
3723 case LIBINPUT_EVENT_TABLET_PAD_STRIP:
3724 str = "TABLET PAD STRIP";
3725 break;
3726 case LIBINPUT_EVENT_TABLET_PAD_KEY:
3727 str = "TABLET PAD KEY";
3728 break;
3729 case LIBINPUT_EVENT_TABLET_PAD_DIAL:
3730 str = "TABLET PAD DIAL";
3731 break;
3732 case LIBINPUT_EVENT_SWITCH_TOGGLE:
3733 str = "SWITCH TOGGLE";
3734 break;
3735 }
3736 return str;
3737 }
3738
3739 static const char *
3740 litest_event_get_type_str(struct libinput_event *event)
3741 {
3742 return litest_event_type_str(libinput_event_get_type(event));
3743 }
3744
3745 static void
3746 litest_print_event(struct libinput_event *event, const char *message)
3747 {
3748 _autofree_ char *event_str = libinput_event_to_str(event, 0, NULL);
3749 fprintf(stderr, "litest: %s %s\n", message, event_str);
3750 }
3751
3752 void
3753 _litest_assert_event_type_is_one_of(struct libinput_event *event,
3754 const char *func,
3755 int lineno,
3756 ...)
3757 {
3758 va_list args;
3759 enum libinput_event_type expected_type;
3760 enum libinput_event_type actual_type = libinput_event_get_type(event);
3761 bool match = false;
3762
3763 va_start(args, lineno);
3764 expected_type = va_arg(args, int);
3765 while ((int)expected_type != -1 && !match) {
3766 match = (actual_type == expected_type);
3767 expected_type = va_arg(args, int);
3768 }
3769 va_end(args);
3770
3771 if (match)
3772 return;
3773
3774 fprintf(stderr,
3775 "FAILED EVENT TYPE: %s: have %s (%d) but want ",
3776 libinput_device_get_name(libinput_event_get_device(event)),
3777 litest_event_get_type_str(event),
3778 libinput_event_get_type(event));
3779
3780 va_start(args, lineno);
3781 expected_type = va_arg(args, int);
3782 while ((int)expected_type != -1) {
3783 fprintf(stderr,
3784 "%s (%d)",
3785 litest_event_type_str(expected_type),
3786 expected_type);
3787 expected_type = va_arg(args, int);
3788
3789 if ((int)expected_type != -1)
3790 fprintf(stderr, " || ");
3791 }
3792 va_end(args);
3793 fprintf(stderr, "\n");
3794
3795 litest_print_event(event, "Wrong event is:");
3796 litest_backtrace(func);
3797 litest_runner_abort();
3798 }
3799
3800 void
3801 _litest_assert_event_type(struct libinput_event *event,
3802 enum libinput_event_type want,
3803 const char *func,
3804 int lineno)
3805 {
3806 _litest_assert_event_type_is_one_of(event, func, lineno, want, -1);
3807 }
3808
3809 void
3810 _litest_assert_event_type_not_one_of(struct libinput_event *event,
3811 const char *func,
3812 int lineno,
3813 ...)
3814 {
3815 va_list args;
3816 enum libinput_event_type not_expected_type;
3817 enum libinput_event_type actual_type = libinput_event_get_type(event);
3818 bool match = false;
3819
3820 va_start(args, lineno);
3821 not_expected_type = va_arg(args, int);
3822 while ((int)not_expected_type != -1 && !match) {
3823 match = (actual_type == not_expected_type);
3824 not_expected_type = va_arg(args, int);
3825 }
3826 va_end(args);
3827
3828 if (!match)
3829 return;
3830
3831 fprintf(stderr,
3832 "FAILED EVENT TYPE: %s: have %s (%d) but didn't want that\n",
3833 libinput_device_get_name(libinput_event_get_device(event)),
3834 litest_event_get_type_str(event),
3835 libinput_event_get_type(event));
3836
3837 litest_print_event(event, "\nWrong event is: ");
3838 litest_backtrace(func);
3839 litest_runner_abort();
3840 }
3841
3842 void
3843 _litest_assert_empty_queue(struct libinput *li, const char *func, int line)
3844 {
3845 bool empty_queue = true;
3846 struct libinput_event *event;
3847
3848 _litest_checkpoint(func, line, ANSI_BRIGHT_CYAN, "asserting empty queue");
3849
3850 libinput_dispatch(li);
3851 while ((event = libinput_get_event(li))) {
3852 empty_queue = false;
3853 litest_print_event(event, "Unexpected event: ");
3854 libinput_event_destroy(event);
3855 libinput_dispatch(li);
3856 }
3857
3858 litest_assert(empty_queue);
3859 }
3860
3861 static struct libevdev_uinput *
3862 litest_create_uinput(const char *name,
3863 const struct input_id *id,
3864 const struct input_absinfo *abs_info,
3865 const int *events)
3866 {
3867 struct libevdev_uinput *uinput;
3868 _free_(libevdev) *dev = libevdev_new();
3869 int type, code;
3870 int rc;
3871 const struct input_absinfo *abs;
3872 const struct input_absinfo default_abs = { .value = 0,
3873 .minimum = 0,
3874 .maximum = 100,
3875 .fuzz = 0,
3876 .flat = 0,
3877 .resolution = 100 };
3878 /* See kernel commit 206f533a0a7c ("Input: uinput - reject requests with
3879 * unreasonable number of slots") */
3880 const struct input_absinfo default_abs_mt_slot = { .value = 0,
3881 .minimum = 0,
3882 .maximum = 64,
3883 .fuzz = 0,
3884 .flat = 0,
3885 .resolution = 100 };
3886 char buf[512];
3887
3888 litest_assert_ptr_notnull(dev);
3889
3890 snprintf(buf, sizeof(buf), "litest %s", name);
3891 libevdev_set_name(dev, buf);
3892 if (id) {
3893 libevdev_set_id_bustype(dev, id->bustype);
3894 libevdev_set_id_vendor(dev, id->vendor);
3895 libevdev_set_id_product(dev, id->product);
3896 libevdev_set_id_version(dev, id->version);
3897 }
3898
3899 abs = abs_info;
3900 while (abs && abs->value != -1) {
3901 struct input_absinfo a = *abs;
3902
3903 /* abs_info->value is used for the code and may be outside
3904 of [min, max] */
3905 a.value = abs->minimum;
3906 rc = libevdev_enable_event_code(dev, EV_ABS, abs->value, &a);
3907 litest_assert_int_eq(rc, 0);
3908 abs++;
3909 }
3910
3911 while (events && (type = *events++) != -1 && (code = *events++) != -1) {
3912 if (type == INPUT_PROP_MAX) {
3913 rc = libevdev_enable_property(dev, code);
3914 } else {
3915 const struct input_absinfo *abs = (code == ABS_MT_SLOT)
3916 ? &default_abs_mt_slot
3917 : &default_abs;
3918 rc = libevdev_enable_event_code(dev,
3919 type,
3920 code,
3921 type == EV_ABS ? abs : NULL);
3922 }
3923 litest_assert_int_eq(rc, 0);
3924 }
3925
3926 rc = libevdev_uinput_create_from_device(dev,
3927 LIBEVDEV_UINPUT_OPEN_MANAGED,
3928 &uinput);
3929 litest_assert_msg(rc == 0,
3930 "Failed to create uinput device: %s\n",
3931 strerror(-rc));
3932
3933 return uinput;
3934 }
3935
3936 struct libevdev_uinput *
3937 litest_create_uinput_device_from_description(const char *name,
3938 const struct input_id *id,
3939 const struct input_absinfo *abs_info,
3940 const int *events)
3941 {
3942 struct libevdev_uinput *uinput;
3943 const char *syspath;
3944 char path[PATH_MAX];
3945
3946 _unref_(udev_monitor) *udev_monitor = udev_setup_monitor();
3947 _unref_(udev_device) *udev_device = NULL;
3948
3949 uinput = litest_create_uinput(name, id, abs_info, events);
3950
3951 syspath = libevdev_uinput_get_syspath(uinput);
3952 snprintf(path, sizeof(path), "%s/event", syspath);
3953
3954 udev_device = udev_wait_for_device_event(udev_monitor, "add", path);
3955
3956 litest_assert(udev_device_get_property_value(udev_device, "ID_INPUT"));
3957
3958 return uinput;
3959 }
3960
3961 static struct libevdev_uinput *
3962 litest_create_uinput_abs_device_v(const char *name,
3963 struct input_id *id,
3964 const struct input_absinfo *abs,
3965 va_list args)
3966 {
3967 int events[KEY_MAX * 2 + 2]; /* increase this if not sufficient */
3968 int *event = events;
3969 int type, code;
3970
3971 while ((type = va_arg(args, int)) != -1 && (code = va_arg(args, int)) != -1) {
3972 *event++ = type;
3973 *event++ = code;
3974 litest_assert(event < &events[ARRAY_LENGTH(events) - 2]);
3975 }
3976
3977 *event++ = -1;
3978 *event++ = -1;
3979
3980 return litest_create_uinput_device_from_description(name, id, abs, events);
3981 }
3982
3983 struct libevdev_uinput *
3984 litest_create_uinput_abs_device(const char *name,
3985 struct input_id *id,
3986 const struct input_absinfo *abs,
3987 ...)
3988 {
3989 struct libevdev_uinput *uinput;
3990 va_list args;
3991
3992 va_start(args, abs);
3993 uinput = litest_create_uinput_abs_device_v(name, id, abs, args);
3994 va_end(args);
3995
3996 return uinput;
3997 }
3998
3999 struct libevdev_uinput *
4000 litest_create_uinput_device(const char *name, struct input_id *id, ...)
4001 {
4002 struct libevdev_uinput *uinput;
4003 va_list args;
4004
4005 va_start(args, id);
4006 uinput = litest_create_uinput_abs_device_v(name, id, NULL, args);
4007 va_end(args);
4008
4009 return uinput;
4010 }
4011
4012 struct libinput_event_pointer *
4013 litest_is_button_event(struct libinput_event *event,
4014 unsigned int button,
4015 enum libinput_button_state state)
4016 {
4017 struct libinput_event_pointer *ptrev;
4018 enum libinput_event_type type = LIBINPUT_EVENT_POINTER_BUTTON;
4019
4020 litest_assert_ptr_notnull(event);
4021 litest_assert_event_type(event, type);
4022 ptrev = libinput_event_get_pointer_event(event);
4023 litest_assert_int_eq(libinput_event_pointer_get_button(ptrev), button);
4024 litest_assert_int_eq(libinput_event_pointer_get_button_state(ptrev), state);
4025
4026 return ptrev;
4027 }
4028
4029 struct libinput_event_pointer *
4030 litest_is_axis_event(struct libinput_event *event,
4031 enum libinput_event_type axis_type,
4032 enum libinput_pointer_axis axis,
4033 enum libinput_pointer_axis_source source)
4034 {
4035 struct libinput_event_pointer *ptrev;
4036
4037 litest_assert(axis_type == LIBINPUT_EVENT_POINTER_SCROLL_WHEEL ||
4038 axis_type == LIBINPUT_EVENT_POINTER_SCROLL_FINGER ||
4039 axis_type == LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS);
4040
4041 litest_assert_ptr_notnull(event);
4042 litest_assert_event_type_is_one_of(event,
4043 LIBINPUT_EVENT_POINTER_AXIS,
4044 axis_type);
4045 ptrev = libinput_event_get_pointer_event(event);
4046 litest_assert(libinput_event_pointer_has_axis(ptrev, axis));
4047
4048 if (source != 0)
4049 litest_assert_int_eq(litest_event_pointer_get_axis_source(ptrev),
4050 source);
4051
4052 return ptrev;
4053 }
4054
4055 bool
4056 litest_is_high_res_axis_event(struct libinput_event *event)
4057 {
4058 litest_assert_event_type_is_one_of(event,
4059 LIBINPUT_EVENT_POINTER_AXIS,
4060 LIBINPUT_EVENT_POINTER_SCROLL_WHEEL,
4061 LIBINPUT_EVENT_POINTER_SCROLL_FINGER,
4062 LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS);
4063
4064 return (libinput_event_get_type(event) != LIBINPUT_EVENT_POINTER_AXIS);
4065 }
4066
4067 struct libinput_event_pointer *
4068 litest_is_motion_event(struct libinput_event *event)
4069 {
4070 struct libinput_event_pointer *ptrev;
4071 enum libinput_event_type type = LIBINPUT_EVENT_POINTER_MOTION;
4072 double x, y, ux, uy;
4073
4074 litest_assert_ptr_notnull(event);
4075 litest_assert_event_type(event, type);
4076 ptrev = libinput_event_get_pointer_event(event);
4077
4078 x = libinput_event_pointer_get_dx(ptrev);
4079 y = libinput_event_pointer_get_dy(ptrev);
4080 ux = libinput_event_pointer_get_dx_unaccelerated(ptrev);
4081 uy = libinput_event_pointer_get_dy_unaccelerated(ptrev);
4082
4083 /* No 0 delta motion events */
4084 litest_assert(x != 0.0 || y != 0.0 || ux != 0.0 || uy != 0.0);
4085
4086 return ptrev;
4087 }
4088
4089 void
4090 _litest_assert_key_event(struct libinput *li,
4091 unsigned int key,
4092 enum libinput_key_state state,
4093 const char *func,
4094 int lineno)
4095 {
4096 litest_wait_for_event(li);
4097 _destroy_(libinput_event) *event = libinput_get_event(li);
4098 litest_is_keyboard_event(event, key, state);
4099 }
4100
4101 void
4102 _litest_assert_button_event(struct libinput *li,
4103 unsigned int button,
4104 enum libinput_button_state state,
4105 const char *func,
4106 int line)
4107 {
4108 _litest_checkpoint(func,
4109 line,
4110 ANSI_CYAN,
4111 "asserting button event %s (%d) state %d",
4112 libevdev_event_code_get_name(EV_KEY, button),
4113 button,
4114 state);
4115
4116 litest_wait_for_event(li);
4117 _destroy_(libinput_event) *event = libinput_get_event(li);
4118 litest_is_button_event(event, button, state);
4119 }
4120
4121 struct libinput_event_touch *
4122 litest_is_touch_event(struct libinput_event *event, enum libinput_event_type type)
4123 {
4124 struct libinput_event_touch *touch;
4125
4126 litest_assert_ptr_notnull(event);
4127
4128 if (type == 0)
4129 type = libinput_event_get_type(event);
4130
4131 switch (type) {
4132 case LIBINPUT_EVENT_TOUCH_DOWN:
4133 case LIBINPUT_EVENT_TOUCH_UP:
4134 case LIBINPUT_EVENT_TOUCH_MOTION:
4135 case LIBINPUT_EVENT_TOUCH_FRAME:
4136 case LIBINPUT_EVENT_TOUCH_CANCEL:
4137 litest_assert_event_type(event, type);
4138 break;
4139 default:
4140 litest_abort_msg("%s: invalid touch type %d", __func__, type);
4141 }
4142
4143 touch = libinput_event_get_touch_event(event);
4144
4145 return touch;
4146 }
4147
4148 struct libinput_event_keyboard *
4149 litest_is_keyboard_event(struct libinput_event *event,
4150 unsigned int key,
4151 enum libinput_key_state state)
4152 {
4153 struct libinput_event_keyboard *kevent;
4154 enum libinput_event_type type = LIBINPUT_EVENT_KEYBOARD_KEY;
4155
4156 litest_assert_ptr_notnull(event);
4157 litest_assert_event_type(event, type);
4158
4159 kevent = libinput_event_get_keyboard_event(event);
4160 litest_assert_ptr_notnull(kevent);
4161
4162 litest_assert_int_eq(libinput_event_keyboard_get_key(kevent), key);
4163 litest_assert_int_eq(libinput_event_keyboard_get_key_state(kevent), state);
4164 return kevent;
4165 }
4166
4167 struct libinput_event_gesture *
4168 litest_is_gesture_event(struct libinput_event *event,
4169 enum libinput_event_type type,
4170 int nfingers)
4171 {
4172 struct libinput_event_gesture *gevent;
4173
4174 litest_assert_ptr_notnull(event);
4175 litest_assert_event_type(event, type);
4176
4177 gevent = libinput_event_get_gesture_event(event);
4178 litest_assert_ptr_notnull(gevent);
4179
4180 if (nfingers != -1)
4181 litest_assert_int_eq(libinput_event_gesture_get_finger_count(gevent),
4182 nfingers);
4183 return gevent;
4184 }
4185
4186 void
4187 _litest_assert_gesture_event(struct libinput *li,
4188 enum libinput_event_type type,
4189 int nfingers,
4190 const char *func,
4191 int line)
4192 {
4193
4194 _litest_checkpoint(func,
4195 line,
4196 ANSI_CYAN,
4197 "asserting gesture event %s %dfg",
4198 litest_event_type_str(type),
4199 nfingers);
4200
4201 litest_wait_for_event(li);
4202
4203 _destroy_(libinput_event) *event = libinput_get_event(li);
4204 litest_is_gesture_event(event, type, nfingers);
4205 }
4206
4207 struct libinput_event_tablet_tool *
4208 litest_is_tablet_event(struct libinput_event *event, enum libinput_event_type type)
4209 {
4210 struct libinput_event_tablet_tool *tevent;
4211
4212 litest_assert_ptr_notnull(event);
4213 litest_assert_event_type(event, type);
4214
4215 tevent = libinput_event_get_tablet_tool_event(event);
4216 litest_assert_ptr_notnull(tevent);
4217
4218 return tevent;
4219 }
4220
4221 void
4222 _litest_assert_tablet_button_event(struct libinput *li,
4223 unsigned int button,
4224 enum libinput_button_state state,
4225 const char *func,
4226 int lineno)
4227 {
4228 struct libinput_event_tablet_tool *tev;
4229 enum libinput_event_type type = LIBINPUT_EVENT_TABLET_TOOL_BUTTON;
4230
4231 _litest_checkpoint(func,
4232 lineno,
4233 ANSI_CYAN,
4234 "asserting tablet button event button %d down: %s",
4235 button,
4236 yesno(state));
4237
4238 litest_wait_for_event(li);
4239
4240 _destroy_(libinput_event) *event = libinput_get_event(li);
4241 litest_assert_notnull(event);
4242 litest_assert_event_type(event, type);
4243 tev = libinput_event_get_tablet_tool_event(event);
4244 litest_assert_int_eq(libinput_event_tablet_tool_get_button(tev), button);
4245 litest_assert_int_eq(libinput_event_tablet_tool_get_button_state(tev), state);
4246 }
4247
4248 struct libinput_event_tablet_tool *
4249 litest_is_proximity_event(struct libinput_event *event,
4250 enum libinput_tablet_tool_proximity_state state)
4251 {
4252 struct libinput_event_tablet_tool *tev;
4253 enum libinput_event_type type = LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY;
4254
4255 litest_assert_notnull(event);
4256 litest_assert_event_type(event, type);
4257 tev = libinput_event_get_tablet_tool_event(event);
4258 litest_assert_int_eq(libinput_event_tablet_tool_get_proximity_state(tev),
4259 state);
4260 return tev;
4261 }
4262
4263 double
4264 litest_event_pointer_get_value(struct libinput_event_pointer *ptrev,
4265 enum libinput_pointer_axis axis)
4266 {
4267 struct libinput_event *event;
4268 enum libinput_event_type type;
4269
4270 event = libinput_event_pointer_get_base_event(ptrev);
4271 type = libinput_event_get_type(event);
4272
4273 switch (type) {
4274 case LIBINPUT_EVENT_POINTER_AXIS:
4275 return libinput_event_pointer_get_axis_value(ptrev, axis);
4276 case LIBINPUT_EVENT_POINTER_SCROLL_WHEEL:
4277 return libinput_event_pointer_get_scroll_value_v120(ptrev, axis);
4278 case LIBINPUT_EVENT_POINTER_SCROLL_FINGER:
4279 case LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS:
4280 return libinput_event_pointer_get_scroll_value(ptrev, axis);
4281 default:
4282 abort();
4283 }
4284 }
4285
4286 enum libinput_pointer_axis_source
4287 litest_event_pointer_get_axis_source(struct libinput_event_pointer *ptrev)
4288 {
4289 struct libinput_event *event;
4290 enum libinput_event_type type;
4291
4292 event = libinput_event_pointer_get_base_event(ptrev);
4293 type = libinput_event_get_type(event);
4294
4295 if (type == LIBINPUT_EVENT_POINTER_AXIS)
4296 return libinput_event_pointer_get_axis_source(ptrev);
4297
4298 switch (type) {
4299 case LIBINPUT_EVENT_POINTER_SCROLL_WHEEL:
4300 return LIBINPUT_POINTER_AXIS_SOURCE_WHEEL;
4301 case LIBINPUT_EVENT_POINTER_SCROLL_FINGER:
4302 return LIBINPUT_POINTER_AXIS_SOURCE_FINGER;
4303 case LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS:
4304 return LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS;
4305 default:
4306 abort();
4307 }
4308 }
4309
4310 void
4311 _litest_assert_tablet_axis_event(struct libinput *li, const char *func, int lineno)
4312 {
4313 _litest_checkpoint(func, lineno, ANSI_CYAN, "asserting axis event");
4314
4315 litest_wait_for_event(li);
4316 _destroy_(libinput_event) *event = libinput_get_event(li);
4317 litest_is_tablet_event(event, LIBINPUT_EVENT_TABLET_TOOL_AXIS);
4318 }
4319
4320 void
4321 _litest_assert_tablet_proximity_event(struct libinput *li,
4322 enum libinput_tablet_tool_proximity_state state,
4323 const char *func,
4324 int lineno)
4325 {
4326 _litest_checkpoint(func,
4327 lineno,
4328 ANSI_CYAN,
4329 "asserting proximity %s event",
4330 state ? "in" : "out");
4331
4332 litest_wait_for_event(li);
4333 _destroy_(libinput_event) *event = libinput_get_event(li);
4334 litest_is_proximity_event(event, state);
4335 }
4336
4337 void
4338 _litest_assert_tablet_tip_event(struct libinput *li,
4339 enum libinput_tablet_tool_tip_state state,
4340 const char *func,
4341 int lineno)
4342 {
4343 struct libinput_event_tablet_tool *tev;
4344 enum libinput_event_type type = LIBINPUT_EVENT_TABLET_TOOL_TIP;
4345
4346 _litest_checkpoint(func,
4347 lineno,
4348 ANSI_CYAN,
4349 "asserting tip %s event",
4350 state ? "down" : "up");
4351
4352 litest_wait_for_event(li);
4353
4354 _destroy_(libinput_event) *event = libinput_get_event(li);
4355 litest_assert_notnull(event);
4356 litest_assert_event_type(event, type);
4357 tev = libinput_event_get_tablet_tool_event(event);
4358 litest_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tev), state);
4359 }
4360
4361 struct libinput_event_tablet_pad *
4362 litest_is_pad_button_event(struct libinput_event *event,
4363 unsigned int button,
4364 enum libinput_button_state state)
4365 {
4366 struct libinput_event_tablet_pad *p;
4367 enum libinput_event_type type = LIBINPUT_EVENT_TABLET_PAD_BUTTON;
4368
4369 litest_assert_ptr_notnull(event);
4370 litest_assert_event_type(event, type);
4371
4372 p = libinput_event_get_tablet_pad_event(event);
4373 litest_assert_ptr_notnull(p);
4374
4375 litest_assert_int_eq(libinput_event_tablet_pad_get_button_number(p), button);
4376 litest_assert_int_eq(libinput_event_tablet_pad_get_button_state(p), state);
4377
4378 return p;
4379 }
4380
4381 struct libinput_event_tablet_pad *
4382 litest_is_pad_dial_event(struct libinput_event *event, unsigned int number)
4383 {
4384 struct libinput_event_tablet_pad *p;
4385 enum libinput_event_type type = LIBINPUT_EVENT_TABLET_PAD_DIAL;
4386
4387 litest_assert_ptr_notnull(event);
4388 litest_assert_event_type(event, type);
4389 p = libinput_event_get_tablet_pad_event(event);
4390
4391 litest_assert_int_eq(libinput_event_tablet_pad_get_dial_number(p), number);
4392
4393 return p;
4394 }
4395
4396 struct libinput_event_tablet_pad *
4397 litest_is_pad_ring_event(struct libinput_event *event,
4398 unsigned int number,
4399 enum libinput_tablet_pad_ring_axis_source source)
4400 {
4401 struct libinput_event_tablet_pad *p;
4402 enum libinput_event_type type = LIBINPUT_EVENT_TABLET_PAD_RING;
4403
4404 litest_assert_ptr_notnull(event);
4405 litest_assert_event_type(event, type);
4406 p = libinput_event_get_tablet_pad_event(event);
4407
4408 litest_assert_int_eq(libinput_event_tablet_pad_get_ring_number(p), number);
4409 litest_assert_int_eq(libinput_event_tablet_pad_get_ring_source(p), source);
4410
4411 return p;
4412 }
4413
4414 struct libinput_event_tablet_pad *
4415 litest_is_pad_strip_event(struct libinput_event *event,
4416 unsigned int number,
4417 enum libinput_tablet_pad_strip_axis_source source)
4418 {
4419 struct libinput_event_tablet_pad *p;
4420 enum libinput_event_type type = LIBINPUT_EVENT_TABLET_PAD_STRIP;
4421
4422 litest_assert_ptr_notnull(event);
4423 litest_assert_event_type(event, type);
4424 p = libinput_event_get_tablet_pad_event(event);
4425
4426 litest_assert_int_eq(libinput_event_tablet_pad_get_strip_number(p), number);
4427 litest_assert_int_eq(libinput_event_tablet_pad_get_strip_source(p), source);
4428
4429 return p;
4430 }
4431
4432 struct libinput_event_tablet_pad *
4433 litest_is_pad_key_event(struct libinput_event *event,
4434 unsigned int key,
4435 enum libinput_key_state state)
4436 {
4437 struct libinput_event_tablet_pad *p;
4438 enum libinput_event_type type = LIBINPUT_EVENT_TABLET_PAD_KEY;
4439
4440 litest_assert(event != NULL);
4441 litest_assert_event_type(event, type);
4442
4443 p = libinput_event_get_tablet_pad_event(event);
4444 litest_assert(p != NULL);
4445
4446 litest_assert_int_eq(libinput_event_tablet_pad_get_key(p), key);
4447 litest_assert_int_eq(libinput_event_tablet_pad_get_key_state(p), state);
4448
4449 return p;
4450 }
4451
4452 struct libinput_event_switch *
4453 litest_is_switch_event(struct libinput_event *event,
4454 enum libinput_switch sw,
4455 enum libinput_switch_state state)
4456 {
4457 struct libinput_event_switch *swev;
4458 enum libinput_event_type type = LIBINPUT_EVENT_SWITCH_TOGGLE;
4459
4460 litest_assert_notnull(event);
4461 litest_assert_event_type(event, type);
4462 swev = libinput_event_get_switch_event(event);
4463
4464 litest_assert_int_eq(libinput_event_switch_get_switch(swev), sw);
4465 litest_assert_int_eq(libinput_event_switch_get_switch_state(swev), state);
4466
4467 return swev;
4468 }
4469
4470 void
4471 _litest_assert_switch_event(struct libinput *li,
4472 enum libinput_switch sw,
4473 enum libinput_switch_state state,
4474 const char *func,
4475 int lineno)
4476 {
4477 litest_wait_for_event(li);
4478
4479 _destroy_(libinput_event) *event = libinput_get_event(li);
4480 litest_is_switch_event(event, sw, state);
4481 }
4482
4483 void
4484 _litest_assert_pad_button_event(struct libinput *li,
4485 unsigned int button,
4486 enum libinput_button_state state,
4487 const char *func,
4488 int lineno)
4489 {
4490 litest_wait_for_event(li);
4491
4492 _destroy_(libinput_event) *event = libinput_get_event(li);
4493 litest_is_pad_button_event(event, button, state);
4494 }
4495
4496 void
4497 _litest_assert_pad_key_event(struct libinput *li,
4498 unsigned int key,
4499 enum libinput_key_state state,
4500 const char *func,
4501 int lineno)
4502 {
4503 litest_wait_for_event(li);
4504
4505 _destroy_(libinput_event) *event = libinput_get_event(li);
4506 litest_is_pad_key_event(event, key, state);
4507 }
4508
4509 void
4510 litest_assert_scroll(struct libinput *li,
4511 enum libinput_event_type axis_type,
4512 enum libinput_pointer_axis axis,
4513 int minimum_movement)
4514 {
4515 struct libinput_event *event;
4516 struct libinput_event_pointer *ptrev;
4517 bool last_hi_res_event_found, last_low_res_event_found;
4518 int value;
4519 int nevents = 0;
4520
4521 litest_assert(axis_type == LIBINPUT_EVENT_POINTER_SCROLL_WHEEL ||
4522 axis_type == LIBINPUT_EVENT_POINTER_SCROLL_FINGER ||
4523 axis_type == LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS);
4524
4525 last_hi_res_event_found = false;
4526 last_low_res_event_found = false;
4527 event = libinput_get_event(li);
4528 litest_assert_ptr_notnull(event);
4529
4530 while (event) {
4531 int min = minimum_movement;
4532
4533 ptrev = litest_is_axis_event(event, axis_type, axis, 0);
4534 nevents++;
4535
4536 /* Due to how the hysteresis works on touchpad
4537 * events, the first event is reduced by the
4538 * hysteresis margin that can cause the first event
4539 * go under the minimum we expect for all other
4540 * events */
4541 if (nevents == 1)
4542 min = minimum_movement / 2;
4543
4544 value = litest_event_pointer_get_value(ptrev, axis);
4545 if (litest_is_high_res_axis_event(event)) {
4546 litest_assert(!last_hi_res_event_found);
4547
4548 if (axis_type == LIBINPUT_EVENT_POINTER_SCROLL_WHEEL)
4549 min *= 120;
4550
4551 if (value == 0)
4552 last_hi_res_event_found = true;
4553 } else {
4554 litest_assert(!last_low_res_event_found);
4555
4556 if (value == 0)
4557 last_low_res_event_found = true;
4558 }
4559
4560 if (value != 0) {
4561 if (minimum_movement > 0)
4562 litest_assert_int_ge(value, min);
4563 else
4564 litest_assert_int_le(value, min);
4565 }
4566
4567 libinput_event_destroy(event);
4568 event = libinput_get_event(li);
4569 }
4570
4571 litest_assert(last_low_res_event_found);
4572 litest_assert(last_hi_res_event_found);
4573 }
4574
4575 void
4576 litest_assert_axis_end_sequence(struct libinput *li,
4577 enum libinput_event_type axis_type,
4578 enum libinput_pointer_axis axis,
4579 enum libinput_pointer_axis_source source)
4580 {
4581 struct libinput_event_pointer *ptrev;
4582 bool last_hi_res_event_found, last_low_res_event_found;
4583 double val;
4584 int i;
4585
4586 litest_assert(axis_type == LIBINPUT_EVENT_POINTER_SCROLL_WHEEL ||
4587 axis_type == LIBINPUT_EVENT_POINTER_SCROLL_FINGER ||
4588 axis_type == LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS);
4589
4590 last_hi_res_event_found = false;
4591 last_low_res_event_found = false;
4592
4593 /* both high and low scroll end events must be sent */
4594 for (i = 0; i < 2; i++) {
4595 _destroy_(libinput_event) *event = libinput_get_event(li);
4596 ptrev = litest_is_axis_event(event, axis_type, axis, source);
4597 val = litest_event_pointer_get_value(ptrev, axis);
4598 litest_assert(val == 0.0);
4599
4600 if (litest_is_high_res_axis_event(event)) {
4601 litest_assert(!last_hi_res_event_found);
4602 last_hi_res_event_found = true;
4603 } else {
4604 litest_assert(!last_low_res_event_found);
4605 last_low_res_event_found = true;
4606 }
4607 }
4608
4609 litest_assert(last_low_res_event_found);
4610 litest_assert(last_hi_res_event_found);
4611 }
4612
4613 void
4614 _litest_assert_only_typed_events(struct libinput *li,
4615 enum libinput_event_type type,
4616 const char *func,
4617 int line)
4618 {
4619 struct libinput_event *event;
4620
4621 litest_assert(type != LIBINPUT_EVENT_NONE);
4622
4623 _litest_checkpoint(func,
4624 line,
4625 ANSI_CYAN,
4626 "asserting only typed events %s",
4627 litest_event_type_str(type));
4628
4629 libinput_dispatch(li);
4630 event = libinput_get_event(li);
4631 litest_assert_notnull(event);
4632
4633 while (event) {
4634 litest_assert_event_type(event, type);
4635 libinput_event_destroy(event);
4636 libinput_dispatch(li);
4637 event = libinput_get_event(li);
4638 }
4639 }
4640
4641 void
4642 litest_assert_only_axis_events(struct libinput *li, enum libinput_event_type axis_type)
4643 {
4644 struct libinput_event *event;
4645
4646 litest_assert(axis_type == LIBINPUT_EVENT_POINTER_SCROLL_WHEEL ||
4647 axis_type == LIBINPUT_EVENT_POINTER_SCROLL_FINGER ||
4648 axis_type == LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS);
4649
4650 libinput_dispatch(li);
4651 event = libinput_get_event(li);
4652 litest_assert_notnull(event);
4653
4654 while (event) {
4655 litest_assert_event_type_is_one_of(event,
4656 LIBINPUT_EVENT_POINTER_AXIS,
4657 axis_type);
4658 libinput_event_destroy(event);
4659 libinput_dispatch(li);
4660 event = libinput_get_event(li);
4661 }
4662 }
4663
4664 void
4665 litest_assert_no_typed_events(struct libinput *li, enum libinput_event_type type)
4666 {
4667 struct libinput_event *event;
4668
4669 litest_assert(type != LIBINPUT_EVENT_NONE);
4670
4671 libinput_dispatch(li);
4672 event = libinput_get_event(li);
4673
4674 while (event) {
4675 litest_assert_int_ne(libinput_event_get_type(event), type);
4676 libinput_event_destroy(event);
4677 libinput_dispatch(li);
4678 event = libinput_get_event(li);
4679 }
4680 }
4681
4682 void
4683 litest_assert_touch_sequence(struct libinput *li)
4684 {
4685 struct libinput_event *event;
4686 struct libinput_event_touch *tev;
4687 int slot;
4688
4689 event = libinput_get_event(li);
4690 tev = litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_DOWN);
4691 slot = libinput_event_touch_get_slot(tev);
4692 libinput_event_destroy(event);
4693
4694 event = libinput_get_event(li);
4695 litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_FRAME);
4696 libinput_event_destroy(event);
4697
4698 event = libinput_get_event(li);
4699 do {
4700 tev = litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_MOTION);
4701 litest_assert_int_eq(slot, libinput_event_touch_get_slot(tev));
4702 libinput_event_destroy(event);
4703
4704 event = libinput_get_event(li);
4705 litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_FRAME);
4706 libinput_event_destroy(event);
4707
4708 event = libinput_get_event(li);
4709 litest_assert_notnull(event);
4710 } while (libinput_event_get_type(event) != LIBINPUT_EVENT_TOUCH_UP);
4711
4712 tev = litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_UP);
4713 litest_assert_int_eq(slot, libinput_event_touch_get_slot(tev));
4714 libinput_event_destroy(event);
4715 event = libinput_get_event(li);
4716 litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_FRAME);
4717 libinput_event_destroy(event);
4718 }
4719
4720 void
4721 litest_assert_touch_motion_frame(struct libinput *li)
4722 {
4723 struct libinput_event *event;
4724
4725 /* expect at least one, but maybe more */
4726 event = libinput_get_event(li);
4727 litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_MOTION);
4728 libinput_event_destroy(event);
4729
4730 event = libinput_get_event(li);
4731 litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_FRAME);
4732 libinput_event_destroy(event);
4733
4734 event = libinput_get_event(li);
4735 while (event) {
4736 litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_MOTION);
4737 libinput_event_destroy(event);
4738
4739 event = libinput_get_event(li);
4740 litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_FRAME);
4741 libinput_event_destroy(event);
4742
4743 event = libinput_get_event(li);
4744 }
4745 }
4746
4747 void
4748 litest_assert_touch_down_frame(struct libinput *li)
4749 {
4750 _destroy_(libinput_event) *down = libinput_get_event(li);
4751 litest_is_touch_event(down, LIBINPUT_EVENT_TOUCH_DOWN);
4752
4753 _destroy_(libinput_event) *frame = libinput_get_event(li);
4754 litest_is_touch_event(frame, LIBINPUT_EVENT_TOUCH_FRAME);
4755 }
4756
4757 void
4758 litest_assert_touch_up_frame(struct libinput *li)
4759 {
4760 _destroy_(libinput_event) *up = libinput_get_event(li);
4761 litest_is_touch_event(up, LIBINPUT_EVENT_TOUCH_UP);
4762
4763 _destroy_(libinput_event) *frame = libinput_get_event(li);
4764 litest_is_touch_event(frame, LIBINPUT_EVENT_TOUCH_FRAME);
4765 }
4766
4767 void
4768 litest_assert_touch_cancel(struct libinput *li)
4769 {
4770 _destroy_(libinput_event) *cancel = libinput_get_event(li);
4771 litest_is_touch_event(cancel, LIBINPUT_EVENT_TOUCH_CANCEL);
4772
4773 _destroy_(libinput_event) *frame = libinput_get_event(li);
4774 litest_is_touch_event(frame, LIBINPUT_EVENT_TOUCH_FRAME);
4775 }
4776
4777 void
4778 _litest_timeout(struct libinput *li, const char *func, int lineno, int millis)
4779 {
4780 if (li)
4781 _litest_dispatch(li, func, lineno);
4782 msleep(millis);
4783 if (li)
4784 _litest_dispatch(li, func, lineno);
4785 }
4786
4787 void
4788 _litest_assert_logcapture_no_errors(struct litest_logcapture *capture,
4789 const char *file,
4790 const char *func,
4791 int line)
4792 {
4793 litest_assert_ptr_notnull(capture);
4794
4795 if (capture->errors &&
4796 strv_for_each((const char **)capture->errors, is_actual_error, NULL)) {
4797 _autofree_ char *errors = strv_join(capture->errors, "\n");
4798 _litest_abort_msg(file,
4799 line,
4800 func,
4801 "Unexpected errors in log capture:\n%s\n",
4802 errors);
4803 }
4804 }
4805
4806 void
4807 litest_logcapture_destroy(struct litest_logcapture *c)
4808 {
4809 strv_free(c->errors);
4810 strv_free(c->infos);
4811 strv_free(c->debugs);
4812 strv_free(c->bugs);
4813 free(c);
4814 }
4815
4816 LIBINPUT_ATTRIBUTE_PRINTF(3, 0)
4817 static void
4818 litest_log_handler_msgcapture(struct libinput *libinput,
4819 enum libinput_log_priority pri,
4820 const char *format,
4821 va_list args)
4822 {
4823 struct litest_user_data *user_data = libinput_get_user_data(libinput);
4824 struct litest_logcapture *capture = user_data->private;
4825 const char *priority = NULL;
4826 const char *color =
4827 use_colors ? ANSI_RGB(255, 255, 70) ANSI_RGB_BG(40, 40, 40) : "";
4828
4829 switch (pri) {
4830 case LIBINPUT_LOG_PRIORITY_ERROR:
4831 priority = "error ";
4832 break;
4833 case LIBINPUT_LOG_PRIORITY_INFO:
4834 priority = "info ";
4835 break;
4836 case LIBINPUT_LOG_PRIORITY_DEBUG:
4837 priority = "debug ";
4838 break;
4839 }
4840
4841 _autofree_ char *message = strdup_vprintf(format, args);
4842
4843 fprintf(stderr,
4844 "%slitest captured: %-6s%s %s",
4845 color,
4846 priority,
4847 use_colors ? ANSI_NORMAL : "",
4848 message);
4849
4850 if (strstr(message, "kernel bug: ") || strstr(message, "client bug: ") ||
4851 strstr(message, "libinput bug: ") || strstr(message, "plugin bug: ")) {
4852 capture->bugs = strv_append_strdup(capture->bugs, message);
4853 }
4854
4855 switch (pri) {
4856 case LIBINPUT_LOG_PRIORITY_ERROR:
4857 capture->errors = strv_append_take(capture->errors, &message);
4858 break;
4859 case LIBINPUT_LOG_PRIORITY_INFO:
4860 capture->infos = strv_append_take(capture->infos, &message);
4861 break;
4862 case LIBINPUT_LOG_PRIORITY_DEBUG:
4863 capture->debugs = strv_append_take(capture->debugs, &message);
4864 break;
4865 }
4866 }
4867
4868 struct litest_logcapture *
4869 litest_logcapture_setup(struct libinput *li)
4870 {
4871 struct litest_logcapture *c = zalloc(sizeof(*c));
4872 litest_context_set_user_data(li, c);
4873 libinput_log_set_handler(li, litest_log_handler_msgcapture);
4874 return c;
4875 }
4876
4877 struct litest_logcapture *
4878 litest_logcapture_remove(struct libinput *li, struct litest_logcapture *capture)
4879 {
4880 litest_restore_log_handler(li);
4881 litest_logcapture_destroy(capture);
4882 return NULL;
4883 }
4884
4885 #define litest_with_logcapture(li_, capture_) \
4886 for (struct litest_logcapture *capture_ = litest_logcapture_setup(li_); \
4887 capture_ != NULL; capture_ = litest_logcapture_remove(li_, capture_))
4888
4889 void
4890 _litest_assert_strv_substring(char **strv,
4891 char *substring,
4892 const char *file,
4893 const char *func,
4894 int line)
4895 {
4896 if (!strv) {
4897 _litest_abort_msg(file,
4898 line,
4899 func,
4900 "Expected substring '%s' but strv is NULL",
4901 substring);
4902 }
4903
4904 bool found = strv_find_substring(strv, substring, NULL);
4905 if (!found) {
4906 _autofree_ char *strv_str = strv_join(strv, "', '");
4907 _litest_abort_msg(file,
4908 line,
4909 func,
4910 "Expected substring '%s' not found in strv: ['%s']",
4911 substring,
4912 strv_str);
4913 }
4914 }
4915
4916 void
4917 _litest_assert_strv_no_substring(char **strv,
4918 char *substring,
4919 const char *file,
4920 const char *func,
4921 int line)
4922 {
4923 if (!strv)
4924 return;
4925
4926 size_t index;
4927 bool found = strv_find_substring(strv, substring, &index);
4928 if (found) {
4929 _autofree_ char *strv_str = strv_join(strv, "', '");
4930 _litest_abort_msg(
4931 file,
4932 line,
4933 func,
4934 "Unexpected substring '%s' present at index %zd in strv: ['%s']",
4935 substring,
4936 index,
4937 strv_str);
4938 }
4939 }
4940
4941 void
4942 litest_push_event_frame(struct litest_device *dev)
4943 {
4944 litest_assert_int_ge(dev->skip_ev_syn, 0);
4945 dev->skip_ev_syn++;
4946 }
4947
4948 void
4949 litest_pop_event_frame(struct litest_device *dev)
4950 {
4951 litest_assert_int_gt(dev->skip_ev_syn, 0);
4952 dev->skip_ev_syn--;
4953 if (dev->skip_ev_syn == 0)
4954 litest_event(dev, EV_SYN, SYN_REPORT, 0);
4955 }
4956
4957 void
4958 litest_filter_event(struct litest_device *dev, unsigned int type, unsigned int code)
4959 {
4960 libevdev_disable_event_code(dev->evdev, type, code);
4961 }
4962
4963 void
4964 litest_unfilter_event(struct litest_device *dev, unsigned int type, unsigned int code)
4965 {
4966 /* would need an non-NULL argument for re-enabling, so simply abort
4967 * until we need to be more sophisticated */
4968 litest_assert_int_ne(type, (unsigned int)EV_ABS);
4969
4970 libevdev_enable_event_code(dev->evdev, type, code, NULL);
4971 }
4972
4973 static void
4974 send_abs_xy(struct litest_device *d, double x, double y)
4975 {
4976 struct input_event e;
4977 int val;
4978
4979 e.type = EV_ABS;
4980 e.code = ABS_X;
4981 e.value = LITEST_AUTO_ASSIGN;
4982 val = litest_auto_assign_value(d, &e, 0, x, y, NULL, true);
4983 litest_event(d, EV_ABS, ABS_X, val);
4984
4985 e.code = ABS_Y;
4986 val = litest_auto_assign_value(d, &e, 0, x, y, NULL, true);
4987 litest_event(d, EV_ABS, ABS_Y, val);
4988 }
4989
4990 static void
4991 send_abs_mt_xy(struct litest_device *d, double x, double y)
4992 {
4993 struct input_event e;
4994 int val;
4995
4996 e.type = EV_ABS;
4997 e.code = ABS_MT_POSITION_X;
4998 e.value = LITEST_AUTO_ASSIGN;
4999 val = litest_auto_assign_value(d, &e, 0, x, y, NULL, true);
5000 litest_event(d, EV_ABS, ABS_MT_POSITION_X, val);
5001
5002 e.code = ABS_MT_POSITION_Y;
5003 e.value = LITEST_AUTO_ASSIGN;
5004 val = litest_auto_assign_value(d, &e, 0, x, y, NULL, true);
5005 litest_event(d, EV_ABS, ABS_MT_POSITION_Y, val);
5006 }
5007
5008 void
5009 litest_semi_mt_touch_down(struct litest_device *d,
5010 struct litest_semi_mt *semi_mt,
5011 unsigned int slot,
5012 double x,
5013 double y)
5014 {
5015 double t, l, r = 0, b = 0; /* top, left, right, bottom */
5016
5017 if (d->ntouches_down > 2 || slot > 1)
5018 return;
5019
5020 if (d->ntouches_down == 1) {
5021 l = x;
5022 t = y;
5023 } else {
5024 int other = (slot + 1) % 2;
5025 l = min(x, semi_mt->touches[other].x);
5026 t = min(y, semi_mt->touches[other].y);
5027 r = max(x, semi_mt->touches[other].x);
5028 b = max(y, semi_mt->touches[other].y);
5029 }
5030
5031 send_abs_xy(d, l, t);
5032
5033 litest_event(d, EV_ABS, ABS_MT_SLOT, 0);
5034
5035 if (d->ntouches_down == 1)
5036 litest_event(d, EV_ABS, ABS_MT_TRACKING_ID, ++semi_mt->tracking_id);
5037
5038 send_abs_mt_xy(d, l, t);
5039
5040 if (d->ntouches_down == 2) {
5041 litest_event(d, EV_ABS, ABS_MT_SLOT, 1);
5042 litest_event(d, EV_ABS, ABS_MT_TRACKING_ID, ++semi_mt->tracking_id);
5043
5044 send_abs_mt_xy(d, r, b);
5045 }
5046
5047 litest_event(d, EV_SYN, SYN_REPORT, 0);
5048
5049 semi_mt->touches[slot].x = x;
5050 semi_mt->touches[slot].y = y;
5051 }
5052
5053 void
5054 litest_semi_mt_touch_move(struct litest_device *d,
5055 struct litest_semi_mt *semi_mt,
5056 unsigned int slot,
5057 double x,
5058 double y)
5059 {
5060 double t, l, r = 0, b = 0; /* top, left, right, bottom */
5061
5062 if (d->ntouches_down > 2 || slot > 1)
5063 return;
5064
5065 if (d->ntouches_down == 1) {
5066 l = x;
5067 t = y;
5068 } else {
5069 int other = (slot + 1) % 2;
5070 l = min(x, semi_mt->touches[other].x);
5071 t = min(y, semi_mt->touches[other].y);
5072 r = max(x, semi_mt->touches[other].x);
5073 b = max(y, semi_mt->touches[other].y);
5074 }
5075
5076 send_abs_xy(d, l, t);
5077
5078 litest_event(d, EV_ABS, ABS_MT_SLOT, 0);
5079 send_abs_mt_xy(d, l, t);
5080
5081 if (d->ntouches_down == 2) {
5082 litest_event(d, EV_ABS, ABS_MT_SLOT, 1);
5083 send_abs_mt_xy(d, r, b);
5084 }
5085
5086 litest_event(d, EV_SYN, SYN_REPORT, 0);
5087
5088 semi_mt->touches[slot].x = x;
5089 semi_mt->touches[slot].y = y;
5090 }
5091
5092 void
5093 litest_semi_mt_touch_up(struct litest_device *d,
5094 struct litest_semi_mt *semi_mt,
5095 unsigned int slot)
5096 {
5097 /* note: ntouches_down is decreased before we get here */
5098 if (d->ntouches_down >= 2 || slot > 1)
5099 return;
5100
5101 litest_event(d, EV_ABS, ABS_MT_SLOT, d->ntouches_down);
5102 litest_event(d, EV_ABS, ABS_MT_TRACKING_ID, -1);
5103
5104 /* if we have one finger left, send x/y coords for that finger left.
5105 this is likely to happen with a real touchpad */
5106 if (d->ntouches_down == 1) {
5107 int other = (slot + 1) % 2;
5108 send_abs_xy(d, semi_mt->touches[other].x, semi_mt->touches[other].y);
5109 litest_event(d, EV_ABS, ABS_MT_SLOT, 0);
5110 send_abs_mt_xy(d, semi_mt->touches[other].x, semi_mt->touches[other].y);
5111 }
5112
5113 litest_event(d, EV_SYN, SYN_REPORT, 0);
5114 }
5115
5116 enum litest_mode
5117 litest_parse_argv(int argc, char **argv, int *njobs_out)
5118 {
5119 enum {
5120 OPT_EXIT_FIRST,
5121 OPT_FILTER_TEST,
5122 OPT_FILTER_DEVICE,
5123 OPT_FILTER_GROUP,
5124 OPT_FILTER_RANGEVAL,
5125 OPT_FILTER_DEVICELESS,
5126 OPT_FILTER_PARAMETER,
5127 OPT_OUTPUT_FILE,
5128 OPT_JOBS,
5129 OPT_LIST,
5130 OPT_VERBOSE,
5131 };
5132 static const struct option opts[] = {
5133 { "filter-test", 1, 0, OPT_FILTER_TEST },
5134 { "filter-device", 1, 0, OPT_FILTER_DEVICE },
5135 { "filter-group", 1, 0, OPT_FILTER_GROUP },
5136 { "filter-rangeval", 1, 0, OPT_FILTER_RANGEVAL },
5137 { "filter-deviceless", 0, 0, OPT_FILTER_DEVICELESS },
5138 { "filter-parameter", 1, 0, OPT_FILTER_PARAMETER },
5139 { "output-file", 1, 0, OPT_OUTPUT_FILE },
5140 { "exitfirst", 0, 0, OPT_EXIT_FIRST },
5141 { "jobs", 1, 0, OPT_JOBS },
5142 { "list", 0, 0, OPT_LIST },
5143 { "verbose", 0, 0, OPT_VERBOSE },
5144 { "help", 0, 0, 'h' },
5145 { 0, 0, 0, 0 }
5146 };
5147 enum {
5148 JOBS_DEFAULT,
5149 JOBS_NONE,
5150 JOBS_SINGLE,
5151 JOBS_CUSTOM
5152 } want_jobs = JOBS_DEFAULT;
5153 char *jobs_env;
5154 int jobs = 0;
5155
5156 /* If we are not running from the builddir, we assume we're running
5157 * against the system as installed */
5158 if (!builddir_lookup(NULL))
5159 use_system_rules_quirks = true;
5160
5161 if (in_debugger)
5162 want_jobs = JOBS_NONE;
5163
5164 if ((jobs_env = getenv("LITEST_JOBS"))) {
5165 if (!safe_atoi(jobs_env, &jobs)) {
5166 fprintf(stderr,
5167 "LITEST_JOBS environment variable must be positive integer\n");
5168 exit(EXIT_FAILURE);
5169 }
5170 }
5171
5172 while (1) {
5173 int c;
5174 int option_index = 0;
5175
5176 c = getopt_long(argc, argv, "j:x", opts, &option_index);
5177 if (c == -1)
5178 break;
5179 switch (c) {
5180 default:
5181 case 'h':
5182 printf("Usage: %s [--verbose] [--jobs] [--filter-...]\n"
5183 "\n"
5184 "Options:\n"
5185 " -x | --exitfirst\n"
5186 " Exit instantly on first failed test\n"
5187 " --filter-test=.... \n"
5188 " Glob to filter on test names\n"
5189 " --filter-device=.... \n"
5190 " Glob to filter on device names\n"
5191 " --filter-group=.... \n"
5192 " Glob to filter on test groups\n"
5193 " --filter-rangeval=N \n"
5194 " Only run tests with the given range value\n"
5195 " --filter-deviceless \n"
5196 " Only run tests that do not create test devices\n"
5197 " --filter-parameter=param1:glob,param2:glob,... \n"
5198 " Glob(s) to filter on the given parameters in their string "
5199 "representation.\n"
5200 " Boolean parameters are filtered via 'true' and 'false'.\n"
5201 " --verbose\n"
5202 " Enable verbose output\n"
5203 " --jobs 8\n"
5204 " Number of parallel test suites to run (default: 8).\n"
5205 " This overrides the LITEST_JOBS environment variable.\n"
5206 " --list\n"
5207 " List all tests\n"
5208 "\n"
5209 "See the libinput-test-suite(1) man page for details.\n",
5210 program_invocation_short_name);
5211 exit(c != 'h');
5212 break;
5213 case OPT_FILTER_TEST:
5214 filter_test = optarg;
5215 if (want_jobs == JOBS_DEFAULT)
5216 want_jobs = JOBS_SINGLE;
5217 break;
5218 case OPT_FILTER_DEVICE:
5219 filter_device = optarg;
5220 if (want_jobs == JOBS_DEFAULT)
5221 want_jobs = JOBS_SINGLE;
5222 break;
5223 case OPT_FILTER_DEVICELESS:
5224 run_deviceless = true;
5225 break;
5226 case OPT_FILTER_GROUP:
5227 filter_group = optarg;
5228 break;
5229 case OPT_FILTER_RANGEVAL:
5230 filter_rangeval = atoi(optarg);
5231 break;
5232 case OPT_FILTER_PARAMETER: {
5233 size_t nelems;
5234 _autostrvfree_ char **params =
5235 strv_from_string(optarg, ",", &nelems);
5236 const size_t max_filters = ARRAY_LENGTH(filter_params) - 1;
5237 if (nelems >= max_filters) {
5238 fprintf(stderr,
5239 "Only %zd parameter filters are supported\n",
5240 max_filters);
5241 exit(1);
5242 }
5243 for (size_t i = 0; i < nelems; i++) {
5244 size_t n;
5245 _autostrvfree_ char **strv =
5246 strv_from_string(params[i], ":", &n);
5247 assert(n == 2);
5248
5249 const char *name = strv[0];
5250 const char *glob = strv[1];
5251
5252 struct param_filter *f = &filter_params[i];
5253 snprintf(f->name, sizeof(f->name), "%s", name);
5254 snprintf(f->glob, sizeof(f->glob), "%s", glob);
5255 }
5256 break;
5257 }
5258 case 'j':
5259 case OPT_JOBS:
5260 jobs = atoi(optarg);
5261 want_jobs = JOBS_CUSTOM;
5262 break;
5263 case OPT_LIST:
5264 return LITEST_MODE_LIST;
5265 case OPT_VERBOSE:
5266 verbose = true;
5267 break;
5268 case OPT_OUTPUT_FILE:
5269 outfile = fopen(optarg, "w+");
5270 if (!outfile) {
5271 fprintf(stderr, "Failed to open %s: %m\n", optarg);
5272 exit(1);
5273 }
5274 break;
5275 case 'x':
5276 case OPT_EXIT_FIRST:
5277 exit_first = true;
5278 break;
5279 }
5280 }
5281
5282 switch (want_jobs) {
5283 case JOBS_SINGLE:
5284 jobs = 1;
5285 break;
5286 case JOBS_NONE:
5287 jobs = 0;
5288 break;
5289 default:
5290 break;
5291 }
5292
5293 *njobs_out = jobs;
5294
5295 return LITEST_MODE_TEST;
5296 }