Debian Codegraph @

test/test-pad.c

raw
1 /*
2 * Copyright © 2016 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <config.h>
25
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <libinput.h>
29 #include <stdbool.h>
30 #include <unistd.h>
31
32 #ifdef HAVE_LIBWACOM
33 #include <libwacom/libwacom.h>
34 #endif
35
36 #include "libinput-util.h"
37 #include "litest.h"
38
39 START_TEST(pad_cap)
40 {
41 struct litest_device *dev = litest_current_device();
42 struct libinput_device *device = dev->libinput_device;
43
44 litest_assert(
45 libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TABLET_PAD));
46 }
47 END_TEST
48
49 START_TEST(pad_no_cap)
50 {
51 struct litest_device *dev = litest_current_device();
52 struct libinput_device *device = dev->libinput_device;
53
54 litest_assert(!libinput_device_has_capability(device,
55 LIBINPUT_DEVICE_CAP_TABLET_PAD));
56 }
57 END_TEST
58
59 START_TEST(pad_time)
60 {
61 struct litest_device *dev = litest_current_device();
62 struct libinput *li = dev->libinput;
63 struct libinput_event *ev;
64 struct libinput_event_tablet_pad *pev;
65 unsigned int code;
66 uint64_t time, time_usec, oldtime;
67 bool has_buttons = false;
68
69 litest_drain_events(li);
70
71 for (code = BTN_0; code < BTN_DIGI; code++) {
72 if (!libevdev_has_event_code(dev->evdev, EV_KEY, code))
73 continue;
74
75 has_buttons = true;
76
77 litest_button_click(dev, code, 1);
78 litest_button_click(dev, code, 0);
79 litest_dispatch(li);
80
81 break;
82 }
83
84 if (!has_buttons)
85 return LITEST_NOT_APPLICABLE;
86
87 ev = libinput_get_event(li);
88 litest_assert_notnull(ev);
89 litest_assert_event_type(ev, LIBINPUT_EVENT_TABLET_PAD_BUTTON);
90 pev = libinput_event_get_tablet_pad_event(ev);
91 time = libinput_event_tablet_pad_get_time(pev);
92 time_usec = libinput_event_tablet_pad_get_time_usec(pev);
93
94 litest_assert(time != 0);
95 litest_assert(time == time_usec / 1000);
96
97 libinput_event_destroy(ev);
98
99 litest_drain_events(li);
100 msleep(10);
101
102 litest_button_click(dev, code, 1);
103 litest_button_click(dev, code, 0);
104 litest_dispatch(li);
105
106 ev = libinput_get_event(li);
107 litest_assert_event_type(ev, LIBINPUT_EVENT_TABLET_PAD_BUTTON);
108 pev = libinput_event_get_tablet_pad_event(ev);
109
110 oldtime = time;
111 time = libinput_event_tablet_pad_get_time(pev);
112 time_usec = libinput_event_tablet_pad_get_time_usec(pev);
113
114 litest_assert(time > oldtime);
115 litest_assert(time != 0);
116 litest_assert(time == time_usec / 1000);
117
118 libinput_event_destroy(ev);
119 }
120 END_TEST
121
122 START_TEST(pad_num_buttons_libwacom)
123 {
124 #ifdef HAVE_LIBWACOM
125 struct litest_device *dev = litest_current_device();
126 struct libinput_device *device = dev->libinput_device;
127 WacomDeviceDatabase *db = NULL;
128 WacomDevice *wacom = NULL;
129 unsigned int nb_lw, nb;
130
131 db = libwacom_database_new();
132 litest_assert_notnull(db);
133
134 wacom = libwacom_new_from_usbid(db,
135 libevdev_get_id_vendor(dev->evdev),
136 libevdev_get_id_product(dev->evdev),
137 NULL);
138 litest_assert_notnull(wacom);
139
140 nb_lw = libwacom_get_num_buttons(wacom);
141 nb = libinput_device_tablet_pad_get_num_buttons(device);
142
143 litest_assert_int_eq(nb, nb_lw);
144
145 libwacom_destroy(wacom);
146 libwacom_database_destroy(db);
147 #endif
148 }
149 END_TEST
150
151 START_TEST(pad_num_buttons)
152 {
153 struct litest_device *dev = litest_current_device();
154 struct libinput_device *device = dev->libinput_device;
155 unsigned int code;
156 int nbuttons = 0;
157
158 for (code = BTN_0; code < KEY_OK; code++) {
159 /* BTN_STYLUS is set for compatibility reasons but not
160 * actually hooked up */
161 if (code == BTN_STYLUS)
162 continue;
163
164 if (libevdev_has_event_code(dev->evdev, EV_KEY, code))
165 nbuttons++;
166 }
167
168 litest_assert_int_eq(libinput_device_tablet_pad_get_num_buttons(device),
169 nbuttons);
170 }
171 END_TEST
172
173 START_TEST(pad_button_intuos)
174 {
175 #ifndef HAVE_LIBWACOM
176 struct litest_device *dev = litest_current_device();
177 struct libinput *li = dev->libinput;
178 unsigned int code;
179 unsigned int expected_number = 0;
180 struct libinput_event *ev;
181 struct libinput_event_tablet_pad *pev;
182 unsigned int count = 0;
183
184 /* Intuos button mapping is sequential up from BTN_0 and continues
185 * with BTN_A */
186 if (!libevdev_has_event_code(dev->evdev, EV_KEY, BTN_0))
187 return LITEST_NOT_APPLICABLE;
188
189 litest_drain_events(li);
190
191 for (code = BTN_0; code < BTN_DIGI; code++) {
192 /* Skip over the BTN_MOUSE and BTN_JOYSTICK range */
193 if ((code >= BTN_MOUSE && code < BTN_JOYSTICK) || (code >= BTN_DIGI)) {
194 litest_assert(
195 !libevdev_has_event_code(dev->evdev, EV_KEY, code));
196 continue;
197 }
198
199 if (!libevdev_has_event_code(dev->evdev, EV_KEY, code))
200 continue;
201
202 litest_button_click(dev, code, 1);
203 litest_button_click(dev, code, 0);
204 litest_dispatch(li);
205
206 count++;
207
208 ev = libinput_get_event(li);
209 pev = litest_is_pad_button_event(ev,
210 expected_number,
211 LIBINPUT_BUTTON_STATE_PRESSED);
212 ev = libinput_event_tablet_pad_get_base_event(pev);
213 libinput_event_destroy(ev);
214
215 ev = libinput_get_event(li);
216 pev = litest_is_pad_button_event(ev,
217 expected_number,
218 LIBINPUT_BUTTON_STATE_RELEASED);
219 ev = libinput_event_tablet_pad_get_base_event(pev);
220 libinput_event_destroy(ev);
221
222 expected_number++;
223 }
224
225 litest_assert_empty_queue(li);
226
227 litest_assert_int_ge(count, 1U);
228 #endif
229 }
230 END_TEST
231
232 START_TEST(pad_button_bamboo)
233 {
234 #ifndef HAVE_LIBWACOM
235 struct litest_device *dev = litest_current_device();
236 struct libinput *li = dev->libinput;
237 unsigned int code;
238 unsigned int expected_number = 0;
239 struct libinput_event *ev;
240 struct libinput_event_tablet_pad *pev;
241 unsigned int count = 0;
242
243 if (!libevdev_has_event_code(dev->evdev, EV_KEY, BTN_LEFT))
244 return LITEST_NOT_APPLICABLE;
245
246 litest_drain_events(li);
247
248 for (code = BTN_LEFT; code < BTN_JOYSTICK; code++) {
249 if (!libevdev_has_event_code(dev->evdev, EV_KEY, code))
250 continue;
251
252 litest_button_click(dev, code, 1);
253 litest_button_click(dev, code, 0);
254 litest_dispatch(li);
255
256 count++;
257
258 ev = libinput_get_event(li);
259 pev = litest_is_pad_button_event(ev,
260 expected_number,
261 LIBINPUT_BUTTON_STATE_PRESSED);
262 ev = libinput_event_tablet_pad_get_base_event(pev);
263 libinput_event_destroy(ev);
264
265 ev = libinput_get_event(li);
266 pev = litest_is_pad_button_event(ev,
267 expected_number,
268 LIBINPUT_BUTTON_STATE_RELEASED);
269 ev = libinput_event_tablet_pad_get_base_event(pev);
270 libinput_event_destroy(ev);
271
272 expected_number++;
273 }
274
275 litest_assert_empty_queue(li);
276
277 litest_assert_int_gt(count, 3U);
278 #endif
279 }
280 END_TEST
281
282 START_TEST(pad_button_libwacom)
283 {
284 #ifdef HAVE_LIBWACOM
285 struct litest_device *dev = litest_current_device();
286 struct libinput *li = dev->libinput;
287 WacomDeviceDatabase *db = NULL;
288 WacomDevice *wacom = NULL;
289
290 db = libwacom_database_new();
291 assert(db);
292
293 wacom = libwacom_new_from_usbid(db,
294 libevdev_get_id_vendor(dev->evdev),
295 libevdev_get_id_product(dev->evdev),
296 NULL);
297 assert(wacom);
298
299 litest_drain_events(li);
300
301 for (int i = 0; i < libwacom_get_num_buttons(wacom); i++) {
302 unsigned int code;
303
304 code = libwacom_get_button_evdev_code(wacom, 'A' + i);
305
306 litest_button_click(dev, code, 1);
307 litest_button_click(dev, code, 0);
308 litest_dispatch(li);
309
310 litest_assert_pad_button_event(li, i, LIBINPUT_BUTTON_STATE_PRESSED);
311 litest_assert_pad_button_event(li, i, LIBINPUT_BUTTON_STATE_RELEASED);
312 }
313
314 libwacom_destroy(wacom);
315 libwacom_database_destroy(db);
316 #endif
317 }
318 END_TEST
319
320 START_TEST(pad_button_mode_groups)
321 {
322 struct litest_device *dev = litest_current_device();
323 struct libinput *li = dev->libinput;
324 struct libinput_event *ev;
325 struct libinput_event_tablet_pad *pev;
326 unsigned int expected_mode = 0;
327 int evdev_codes[KEY_OK - BTN_0] = { 0 };
328 #ifdef HAVE_LIBWACOM
329 WacomDeviceDatabase *db = NULL;
330 WacomDevice *wacom = NULL;
331
332 db = libwacom_database_new();
333 litest_assert_notnull(db);
334
335 wacom = libwacom_new_from_usbid(db,
336 libevdev_get_id_vendor(dev->evdev),
337 libevdev_get_id_product(dev->evdev),
338 NULL);
339 if (wacom) {
340 for (size_t i = 0; i < ARRAY_LENGTH(evdev_codes); i++) {
341 evdev_codes[i] = libwacom_get_button_evdev_code(wacom, 'A' + i);
342 }
343 }
344
345 libwacom_destroy(wacom);
346 libwacom_database_destroy(db);
347 #else
348 for (size_t i = 0; i < ARRAY_LENGTH(evdev_codes); i++) {
349 evdev_codes[i] = BTN_0 + 1;
350 }
351 #endif
352
353 litest_drain_events(li);
354
355 for (unsigned int b = 0; b < ARRAY_LENGTH(evdev_codes) && evdev_codes[b] != 0;
356 b++) {
357 unsigned int code = evdev_codes[b];
358 unsigned int mode, index;
359 struct libinput_tablet_pad_mode_group *group;
360
361 if (!libevdev_has_event_code(dev->evdev, EV_KEY, code))
362 continue;
363
364 litest_button_click(dev, code, 1);
365 litest_button_click(dev, code, 0);
366 litest_dispatch(li);
367
368 switch (code) {
369 case BTN_STYLUS:
370 litest_assert_empty_queue(li);
371 continue;
372 default:
373 break;
374 }
375
376 ev = libinput_get_event(li);
377 litest_assert_event_type(ev, LIBINPUT_EVENT_TABLET_PAD_BUTTON);
378 pev = libinput_event_get_tablet_pad_event(ev);
379
380 group = libinput_event_tablet_pad_get_mode_group(pev);
381 #ifdef HAVE_LIBWACOM_BUTTON_MODESWITCH_MODE
382 /* One of the few devices that has very specific mode switch buttons
383 * so we hardcode these here in the test */
384 if (dev->which == LITEST_WACOM_MOBILESTUDIO_PRO_16_PAD) {
385 switch (code) {
386 case BTN_9:
387 expected_mode = 0;
388 break;
389 case BTN_A:
390 expected_mode = 1;
391 break;
392 case BTN_B:
393 expected_mode = 2;
394 break;
395 case BTN_C:
396 expected_mode = 3;
397 break;
398 default:
399 break;
400 }
401 } else
402 #endif
403 if (libinput_tablet_pad_mode_group_button_is_toggle(group, b)) {
404 int num_modes =
405 libinput_tablet_pad_mode_group_get_num_modes(group);
406 expected_mode = (expected_mode + 1) % num_modes;
407 }
408 mode = libinput_event_tablet_pad_get_mode(pev);
409 litest_assert_int_eq(mode, expected_mode);
410
411 index = libinput_tablet_pad_mode_group_get_index(group);
412 litest_assert_int_eq(index, 0U);
413
414 libinput_event_destroy(ev);
415
416 ev = libinput_get_event(li);
417 litest_assert_event_type(ev, LIBINPUT_EVENT_TABLET_PAD_BUTTON);
418 pev = libinput_event_get_tablet_pad_event(ev);
419
420 mode = libinput_event_tablet_pad_get_mode(pev);
421 litest_assert_int_eq(mode, expected_mode);
422 group = libinput_event_tablet_pad_get_mode_group(pev);
423 index = libinput_tablet_pad_mode_group_get_index(group);
424 litest_assert_int_eq(index, 0U);
425 libinput_event_destroy(ev);
426 }
427
428 litest_assert_empty_queue(li);
429 }
430 END_TEST
431
432 START_TEST(pad_has_ring)
433 {
434 struct litest_device *dev = litest_current_device();
435 struct libinput_device *device = dev->libinput_device;
436 int nrings;
437
438 nrings = libinput_device_tablet_pad_get_num_rings(device);
439 litest_assert_int_ge(nrings, 1);
440 }
441 END_TEST
442
443 START_TEST(pad_ring)
444 {
445 struct litest_device *dev = litest_current_device();
446 struct libinput *li = dev->libinput;
447 struct libinput_event *ev;
448 struct libinput_event_tablet_pad *pev;
449 int val;
450 double degrees, expected;
451 int min, max;
452 int step_size;
453 int nevents = 0;
454
455 litest_pad_ring_start(dev, 10);
456
457 litest_drain_events(li);
458
459 /* Wacom's 0 value is at 275 degrees */
460 expected = 270;
461
462 min = libevdev_get_abs_minimum(dev->evdev, ABS_WHEEL);
463 max = libevdev_get_abs_maximum(dev->evdev, ABS_WHEEL);
464 step_size = 360 / (max - min + 1);
465
466 /* This is a bit strange because we rely on kernel filtering here.
467 The litest_*() functions take a percentage, but mapping this to
468 the pads 72 or 36 range pad ranges is lossy and a bit
469 unpredictable. So instead we increase by a small percentage,
470 expecting *most* events to be filtered by the kernel because they
471 resolve to the same integer value as the previous event. Whenever
472 an event gets through, we expect that to be the next integer
473 value in the range and thus the next step on the circle.
474 */
475 for (val = 0; val < 100.0; val += 1) {
476 litest_pad_ring_change(dev, val);
477 litest_dispatch(li);
478
479 ev = libinput_get_event(li);
480 if (!ev)
481 continue;
482
483 nevents++;
484 pev = litest_is_pad_ring_event(ev,
485 0,
486 LIBINPUT_TABLET_PAD_RING_SOURCE_FINGER);
487
488 degrees = libinput_event_tablet_pad_get_ring_position(pev);
489 litest_assert_double_ge(degrees, 0.0);
490 litest_assert_double_lt(degrees, 360.0);
491
492 litest_assert_double_eq(degrees, expected);
493
494 libinput_event_destroy(ev);
495 expected = fmod(degrees + step_size, 360);
496 }
497
498 litest_assert_int_eq(nevents, 360 / step_size - 1);
499
500 litest_pad_ring_end(dev);
501 }
502 END_TEST
503
504 START_TEST(pad_ring_finger_up)
505 {
506 struct litest_device *dev = litest_current_device();
507 struct libinput *li = dev->libinput;
508 struct libinput_event *ev;
509 struct libinput_event_tablet_pad *pev;
510 double degrees;
511
512 litest_pad_ring_start(dev, 10);
513
514 litest_drain_events(li);
515
516 litest_pad_ring_end(dev);
517 litest_dispatch(li);
518
519 ev = libinput_get_event(li);
520 pev = litest_is_pad_ring_event(ev, 0, LIBINPUT_TABLET_PAD_RING_SOURCE_FINGER);
521
522 degrees = libinput_event_tablet_pad_get_ring_position(pev);
523 litest_assert_double_eq(degrees, -1.0);
524 libinput_event_destroy(ev);
525
526 litest_assert_empty_queue(li);
527 }
528 END_TEST
529
530 START_TEST(pad_has_dial)
531 {
532 struct litest_device *dev = litest_current_device();
533 struct libinput_device *device = dev->libinput_device;
534 int ndials;
535 int expected_ndials = 1;
536
537 if (libevdev_has_event_code(dev->evdev, EV_REL, REL_HWHEEL))
538 expected_ndials = 2;
539
540 ndials = libinput_device_tablet_pad_get_num_dials(device);
541 litest_assert_int_eq(ndials, expected_ndials);
542 }
543 END_TEST
544
545 START_TEST(pad_dial_low_res)
546 {
547 struct litest_device *dev = litest_current_device();
548 struct libinput *li = dev->libinput;
549 unsigned int code = 0;
550
551 if (libevdev_has_event_code(dev->evdev, EV_REL, REL_WHEEL))
552 code = REL_WHEEL;
553 if (libevdev_has_event_code(dev->evdev, EV_REL, REL_DIAL))
554 code = REL_DIAL;
555
556 litest_drain_events(li);
557
558 for (int i = 0; i < 10; i++) {
559 int direction = -1 + 2 * i % 2;
560 litest_event(dev, EV_REL, code, direction);
561 if (code == REL_WHEEL)
562 litest_event(dev, EV_REL, REL_WHEEL_HI_RES, direction * 120);
563 litest_event(dev, EV_SYN, SYN_REPORT, 0);
564 litest_dispatch(li);
565
566 struct libinput_event *ev = libinput_get_event(li);
567 struct libinput_event_tablet_pad *pev = litest_is_pad_dial_event(ev, 0);
568
569 double v120 = libinput_event_tablet_pad_get_dial_delta_v120(pev);
570 switch (code) {
571 case REL_WHEEL: /* inverted */
572 litest_assert_double_eq(v120, -120.0 * direction);
573 break;
574 case REL_DIAL:
575 litest_assert_double_eq(v120, 120.0 * direction);
576 break;
577 default:
578 litest_abort_msg("Invalid dial code");
579 }
580 libinput_event_destroy(ev);
581 }
582 }
583 END_TEST
584
585 START_TEST(pad_dial_hi_res)
586 {
587 struct litest_device *dev = litest_current_device();
588 struct libinput *li = dev->libinput;
589 const int increment = 30;
590 int accumulated = 0;
591
592 if (!libevdev_has_event_code(dev->evdev, EV_REL, REL_WHEEL_HI_RES))
593 return LITEST_NOT_APPLICABLE;
594
595 litest_drain_events(li);
596
597 for (int i = 0; i < 10; i++) {
598 litest_event(dev, EV_REL, REL_WHEEL_HI_RES, increment);
599 accumulated += increment;
600 if (accumulated % 120 == 0)
601 litest_event(dev, EV_REL, REL_WHEEL, 1);
602 litest_event(dev, EV_SYN, SYN_REPORT, 0);
603 litest_dispatch(li);
604
605 struct libinput_event *ev = libinput_get_event(li);
606 struct libinput_event_tablet_pad *pev = litest_is_pad_dial_event(ev, 0);
607
608 double v120 = libinput_event_tablet_pad_get_dial_delta_v120(pev);
609 litest_assert_double_eq(v120, -increment); /* REL_WHEEL is inverted */
610 libinput_event_destroy(ev);
611 }
612 }
613 END_TEST
614
615 START_TEST(pad_has_strip)
616 {
617 struct litest_device *dev = litest_current_device();
618 struct libinput_device *device = dev->libinput_device;
619 int nstrips;
620
621 nstrips = libinput_device_tablet_pad_get_num_strips(device);
622 litest_assert_int_ge(nstrips, 1);
623 }
624 END_TEST
625
626 START_TEST(pad_strip)
627 {
628 struct litest_device *dev = litest_current_device();
629 struct libinput *li = dev->libinput;
630 struct libinput_event *ev;
631 struct libinput_event_tablet_pad *pev;
632 int val;
633 double pos, expected;
634
635 litest_pad_strip_start(dev, 10);
636
637 litest_drain_events(li);
638
639 expected = 0;
640
641 /* 9.5 works with the generic axis scaling without jumping over a
642 * value. */
643 for (val = 0; val < 100; val += 9.5) {
644 litest_pad_strip_change(dev, val);
645 litest_dispatch(li);
646
647 ev = libinput_get_event(li);
648 pev = litest_is_pad_strip_event(
649 ev,
650 0,
651 LIBINPUT_TABLET_PAD_STRIP_SOURCE_FINGER);
652
653 pos = libinput_event_tablet_pad_get_strip_position(pev);
654 litest_assert_double_ge(pos, 0.0);
655 litest_assert_double_lt(pos, 1.0);
656
657 /* rounding errors, mostly caused by small physical range */
658 litest_assert_double_ge(pos, expected - 0.02);
659 litest_assert_double_le(pos, expected + 0.02);
660
661 libinput_event_destroy(ev);
662
663 expected = pos + 0.08;
664 }
665
666 litest_pad_strip_change(dev, 100);
667 litest_dispatch(li);
668
669 ev = libinput_get_event(li);
670 pev = litest_is_pad_strip_event(ev, 0, LIBINPUT_TABLET_PAD_STRIP_SOURCE_FINGER);
671 pos = libinput_event_tablet_pad_get_strip_position(pev);
672 litest_assert_double_eq(pos, 1.0);
673 libinput_event_destroy(ev);
674
675 litest_pad_strip_end(dev);
676 }
677 END_TEST
678
679 START_TEST(pad_strip_finger_up)
680 {
681 struct litest_device *dev = litest_current_device();
682 struct libinput *li = dev->libinput;
683 struct libinput_event *ev;
684 struct libinput_event_tablet_pad *pev;
685 double pos;
686
687 litest_pad_strip_start(dev, 10);
688 litest_drain_events(li);
689
690 litest_pad_strip_end(dev);
691 litest_dispatch(li);
692
693 ev = libinput_get_event(li);
694 pev = litest_is_pad_strip_event(ev, 0, LIBINPUT_TABLET_PAD_STRIP_SOURCE_FINGER);
695
696 pos = libinput_event_tablet_pad_get_strip_position(pev);
697 litest_assert_double_eq(pos, -1.0);
698 libinput_event_destroy(ev);
699
700 litest_assert_empty_queue(li);
701 }
702 END_TEST
703
704 START_TEST(pad_left_handed_default)
705 {
706 #ifdef HAVE_LIBWACOM
707 struct litest_device *dev = litest_current_device();
708 struct libinput_device *device = dev->libinput_device;
709 enum libinput_config_status status;
710
711 litest_assert(libinput_device_config_left_handed_is_available(device));
712
713 litest_assert_int_eq(libinput_device_config_left_handed_get_default(device), 0);
714 litest_assert_int_eq(libinput_device_config_left_handed_get(device), 0);
715
716 status = libinput_device_config_left_handed_set(dev->libinput_device, 1);
717 litest_assert_enum_eq(status, LIBINPUT_CONFIG_STATUS_SUCCESS);
718
719 litest_assert_int_eq(libinput_device_config_left_handed_get(device), 1);
720 litest_assert_int_eq(libinput_device_config_left_handed_get_default(device), 0);
721
722 status = libinput_device_config_left_handed_set(dev->libinput_device, 0);
723 litest_assert_enum_eq(status, LIBINPUT_CONFIG_STATUS_SUCCESS);
724
725 litest_assert_int_eq(libinput_device_config_left_handed_get(device), 0);
726 litest_assert_int_eq(libinput_device_config_left_handed_get_default(device), 0);
727
728 #endif
729 }
730 END_TEST
731
732 START_TEST(pad_no_left_handed)
733 {
734 struct litest_device *dev = litest_current_device();
735 struct libinput_device *device = dev->libinput_device;
736
737 /* Without libwacom we default to left-handed being available */
738 #ifdef HAVE_LIBWACOM
739 litest_assert(!libinput_device_config_left_handed_is_available(device));
740 #else
741 litest_assert(libinput_device_config_left_handed_is_available(device));
742 #endif
743
744 litest_assert_int_eq(libinput_device_config_left_handed_get_default(device), 0);
745 litest_assert_int_eq(libinput_device_config_left_handed_get(device), 0);
746
747 #ifdef HAVE_LIBWACOM
748 enum libinput_config_status status;
749 status = libinput_device_config_left_handed_set(dev->libinput_device, 1);
750 litest_assert_enum_eq(status, LIBINPUT_CONFIG_STATUS_UNSUPPORTED);
751
752 litest_assert_int_eq(libinput_device_config_left_handed_get(device), 0);
753 litest_assert_int_eq(libinput_device_config_left_handed_get_default(device), 0);
754
755 status = libinput_device_config_left_handed_set(dev->libinput_device, 0);
756 litest_assert_enum_eq(status, LIBINPUT_CONFIG_STATUS_UNSUPPORTED);
757
758 litest_assert_int_eq(libinput_device_config_left_handed_get(device), 0);
759 litest_assert_int_eq(libinput_device_config_left_handed_get_default(device), 0);
760 #endif
761 }
762 END_TEST
763
764 START_TEST(pad_left_handed_ring)
765 {
766 #ifdef HAVE_LIBWACOM
767 struct litest_device *dev = litest_current_device();
768 struct libinput *li = dev->libinput;
769 struct libinput_event *ev;
770 struct libinput_event_tablet_pad *pev;
771 int val;
772 double degrees, expected;
773
774 libinput_device_config_left_handed_set(dev->libinput_device, 1);
775
776 litest_pad_ring_start(dev, 10);
777
778 litest_drain_events(li);
779
780 /* Wacom's 0 value is at 275 degrees -> 90 in left-handed mode*/
781 expected = 90;
782
783 for (val = 0; val < 100; val += 10) {
784 litest_pad_ring_change(dev, val);
785 litest_dispatch(li);
786
787 ev = libinput_get_event(li);
788 pev = litest_is_pad_ring_event(ev,
789 0,
790 LIBINPUT_TABLET_PAD_RING_SOURCE_FINGER);
791
792 degrees = libinput_event_tablet_pad_get_ring_position(pev);
793 litest_assert_double_ge(degrees, 0.0);
794 litest_assert_double_lt(degrees, 360.0);
795
796 /* rounding errors, mostly caused by small physical range */
797 litest_assert_double_ge(degrees, expected - 2);
798 litest_assert_double_le(degrees, expected + 2);
799
800 libinput_event_destroy(ev);
801
802 expected = fmod(degrees + 36, 360);
803 }
804
805 litest_pad_ring_end(dev);
806 #endif
807 }
808 END_TEST
809
810 static bool
811 pad_has_groups(struct litest_device *dev)
812 {
813 bool rc = false;
814 #ifdef HAVE_LIBWACOM
815 WacomDeviceDatabase *db = NULL;
816 WacomDevice *wacom = NULL;
817
818 db = libwacom_database_new();
819 litest_assert_notnull(db);
820
821 wacom = libwacom_new_from_usbid(db,
822 libevdev_get_id_vendor(dev->evdev),
823 libevdev_get_id_product(dev->evdev),
824 NULL);
825 if (wacom && (libwacom_get_ring_num_modes(wacom) != 0 ||
826 libwacom_get_ring2_num_modes(wacom) != 0 ||
827 libwacom_get_strips_num_modes(wacom) != 0))
828 rc = true;
829
830 libwacom_destroy(wacom);
831 libwacom_database_destroy(db);
832 #endif
833 return rc;
834 }
835
836 START_TEST(pad_mode_groups)
837 {
838 struct litest_device *dev = litest_current_device();
839 struct libinput_device *device = dev->libinput_device;
840 struct libinput_tablet_pad_mode_group *group;
841 int ngroups;
842 unsigned int i;
843
844 ngroups = libinput_device_tablet_pad_get_num_mode_groups(device);
845 litest_assert_int_ge(ngroups, 1);
846
847 for (i = 0; i < (unsigned int)ngroups; i++) {
848 group = libinput_device_tablet_pad_get_mode_group(device, i);
849 litest_assert_notnull(group);
850 litest_assert_int_eq(libinput_tablet_pad_mode_group_get_index(group),
851 i);
852 }
853
854 group = libinput_device_tablet_pad_get_mode_group(device, ngroups);
855 litest_assert(group == NULL);
856 group = libinput_device_tablet_pad_get_mode_group(device, ngroups + 1);
857 litest_assert(group == NULL);
858 }
859 END_TEST
860
861 START_TEST(pad_mode_groups_userdata)
862 {
863 struct litest_device *dev = litest_current_device();
864 struct libinput_device *device = dev->libinput_device;
865 struct libinput_tablet_pad_mode_group *group;
866 int rc;
867 void *userdata = &rc;
868
869 group = libinput_device_tablet_pad_get_mode_group(device, 0);
870 litest_assert(libinput_tablet_pad_mode_group_get_user_data(group) == NULL);
871 libinput_tablet_pad_mode_group_set_user_data(group, userdata);
872 litest_assert(libinput_tablet_pad_mode_group_get_user_data(group) == &rc);
873
874 libinput_tablet_pad_mode_group_set_user_data(group, NULL);
875 litest_assert(libinput_tablet_pad_mode_group_get_user_data(group) == NULL);
876 }
877 END_TEST
878
879 START_TEST(pad_mode_groups_ref)
880 {
881 struct litest_device *dev = litest_current_device();
882 struct libinput_device *device = dev->libinput_device;
883 struct libinput_tablet_pad_mode_group *group, *g;
884
885 group = libinput_device_tablet_pad_get_mode_group(device, 0);
886 g = libinput_tablet_pad_mode_group_ref(group);
887 litest_assert_ptr_eq(g, group);
888
889 /* We don't expect this to be freed. Any leaks should be caught by
890 * valgrind. */
891 g = libinput_tablet_pad_mode_group_unref(group);
892 litest_assert_ptr_eq(g, group);
893 }
894 END_TEST
895
896 START_TEST(pad_mode_group_mode)
897 {
898 struct litest_device *dev = litest_current_device();
899 struct libinput_device *device = dev->libinput_device;
900 struct libinput_tablet_pad_mode_group *group;
901 int ngroups;
902 unsigned int nmodes, mode;
903
904 if (pad_has_groups(dev))
905 return LITEST_NOT_APPLICABLE;
906
907 ngroups = libinput_device_tablet_pad_get_num_mode_groups(device);
908 litest_assert_int_ge(ngroups, 1);
909
910 group = libinput_device_tablet_pad_get_mode_group(device, 0);
911
912 nmodes = libinput_tablet_pad_mode_group_get_num_modes(group);
913 litest_assert_int_eq(nmodes, 1U);
914
915 mode = libinput_tablet_pad_mode_group_get_mode(group);
916 litest_assert_int_lt(mode, nmodes);
917 }
918 END_TEST
919
920 START_TEST(pad_mode_group_has)
921 {
922 struct litest_device *dev = litest_current_device();
923 struct libinput_device *device = dev->libinput_device;
924 struct libinput_tablet_pad_mode_group *group;
925 int ngroups, nbuttons, nrings, nstrips;
926 int i, b, r, s;
927
928 ngroups = libinput_device_tablet_pad_get_num_mode_groups(device);
929 litest_assert_int_ge(ngroups, 1);
930
931 nbuttons = libinput_device_tablet_pad_get_num_buttons(device);
932 nrings = libinput_device_tablet_pad_get_num_rings(device);
933 nstrips = libinput_device_tablet_pad_get_num_strips(device);
934
935 for (b = 0; b < nbuttons; b++) {
936 bool found = false;
937 for (i = 0; i < ngroups; i++) {
938 group = libinput_device_tablet_pad_get_mode_group(device, i);
939 if (libinput_tablet_pad_mode_group_has_button(group, b)) {
940 litest_assert(!found);
941 found = true;
942 }
943 }
944 litest_assert(found);
945 }
946
947 for (s = 0; s < nstrips; s++) {
948 bool found = false;
949 for (i = 0; i < ngroups; i++) {
950 group = libinput_device_tablet_pad_get_mode_group(device, i);
951 if (libinput_tablet_pad_mode_group_has_strip(group, s)) {
952 litest_assert(!found);
953 found = true;
954 }
955 }
956 litest_assert(found);
957 }
958
959 for (r = 0; r < nrings; r++) {
960 bool found = false;
961 for (i = 0; i < ngroups; i++) {
962 group = libinput_device_tablet_pad_get_mode_group(device, i);
963 if (libinput_tablet_pad_mode_group_has_ring(group, r)) {
964 litest_assert(!found);
965 found = true;
966 }
967 }
968 litest_assert(found);
969 }
970 }
971 END_TEST
972
973 START_TEST(pad_mode_group_has_invalid)
974 {
975 struct litest_device *dev = litest_current_device();
976 struct libinput_device *device = dev->libinput_device;
977 struct libinput_tablet_pad_mode_group *group;
978 int ngroups, nbuttons, nrings, nstrips;
979 int i;
980 int rc;
981
982 ngroups = libinput_device_tablet_pad_get_num_mode_groups(device);
983 litest_assert_int_ge(ngroups, 1);
984
985 nbuttons = libinput_device_tablet_pad_get_num_buttons(device);
986 nrings = libinput_device_tablet_pad_get_num_rings(device);
987 nstrips = libinput_device_tablet_pad_get_num_strips(device);
988
989 for (i = 0; i < ngroups; i++) {
990 group = libinput_device_tablet_pad_get_mode_group(device, i);
991 rc = libinput_tablet_pad_mode_group_has_button(group, nbuttons);
992 litest_assert_int_eq(rc, 0);
993 rc = libinput_tablet_pad_mode_group_has_button(group, nbuttons + 1);
994 litest_assert_int_eq(rc, 0);
995 rc = libinput_tablet_pad_mode_group_has_button(group, 0x1000000);
996 litest_assert_int_eq(rc, 0);
997 }
998
999 for (i = 0; i < ngroups; i++) {
1000 group = libinput_device_tablet_pad_get_mode_group(device, i);
1001 rc = libinput_tablet_pad_mode_group_has_strip(group, nstrips);
1002 litest_assert_int_eq(rc, 0);
1003 rc = libinput_tablet_pad_mode_group_has_strip(group, nstrips + 1);
1004 litest_assert_int_eq(rc, 0);
1005 rc = libinput_tablet_pad_mode_group_has_strip(group, 0x1000000);
1006 litest_assert_int_eq(rc, 0);
1007 }
1008
1009 for (i = 0; i < ngroups; i++) {
1010 group = libinput_device_tablet_pad_get_mode_group(device, i);
1011 rc = libinput_tablet_pad_mode_group_has_ring(group, nrings);
1012 litest_assert_int_eq(rc, 0);
1013 rc = libinput_tablet_pad_mode_group_has_ring(group, nrings + 1);
1014 litest_assert_int_eq(rc, 0);
1015 rc = libinput_tablet_pad_mode_group_has_ring(group, 0x1000000);
1016 litest_assert_int_eq(rc, 0);
1017 }
1018 }
1019 END_TEST
1020
1021 START_TEST(pad_mode_group_has_no_toggle)
1022 {
1023 #ifdef HAVE_LIBWACOM
1024 struct litest_device *dev = litest_current_device();
1025 struct libinput_device *device = dev->libinput_device;
1026 struct libinput_tablet_pad_mode_group *group;
1027 int ngroups, nbuttons;
1028 int i, b;
1029
1030 if (pad_has_groups(dev))
1031 return LITEST_NOT_APPLICABLE;
1032
1033 ngroups = libinput_device_tablet_pad_get_num_mode_groups(device);
1034 litest_assert_int_eq(ngroups, 1);
1035
1036 /* Button must not be toggle buttons */
1037 nbuttons = libinput_device_tablet_pad_get_num_buttons(device);
1038 for (i = 0; i < ngroups; i++) {
1039 group = libinput_device_tablet_pad_get_mode_group(device, i);
1040 for (b = 0; b < nbuttons; b++) {
1041 litest_assert(
1042 !libinput_tablet_pad_mode_group_button_is_toggle(group,
1043 b));
1044 }
1045 }
1046 #endif
1047 }
1048 END_TEST
1049
1050 static bool
1051 pad_has_keys(struct litest_device *dev)
1052 {
1053 struct libevdev *evdev = dev->evdev;
1054
1055 return (libevdev_has_event_code(evdev, EV_KEY, KEY_BUTTONCONFIG) ||
1056 libevdev_has_event_code(evdev, EV_KEY, KEY_ONSCREEN_KEYBOARD) ||
1057 libevdev_has_event_code(evdev, EV_KEY, KEY_CONTROLPANEL));
1058 }
1059
1060 static void
1061 pad_key_down(struct litest_device *dev, unsigned int which)
1062 {
1063 litest_event(dev, EV_ABS, ABS_MISC, 15);
1064 litest_event(dev, EV_KEY, which, 1);
1065 litest_event(dev, EV_SYN, SYN_REPORT, 0);
1066 }
1067
1068 static void
1069 pad_key_up(struct litest_device *dev, unsigned int which)
1070 {
1071 litest_event(dev, EV_ABS, ABS_MISC, 0);
1072 litest_event(dev, EV_KEY, which, 0);
1073 litest_event(dev, EV_SYN, SYN_REPORT, 0);
1074 }
1075
1076 START_TEST(pad_keys)
1077 {
1078 struct litest_device *dev = litest_current_device();
1079 struct libinput *li = dev->libinput;
1080 unsigned int key;
1081
1082 if (!pad_has_keys(dev))
1083 return LITEST_NOT_APPLICABLE;
1084
1085 litest_drain_events(li);
1086
1087 key = KEY_BUTTONCONFIG;
1088 pad_key_down(dev, key);
1089 litest_dispatch(li);
1090 litest_assert_pad_key_event(li, key, LIBINPUT_KEY_STATE_PRESSED);
1091
1092 pad_key_up(dev, key);
1093 litest_dispatch(li);
1094 litest_assert_pad_key_event(li, key, LIBINPUT_KEY_STATE_RELEASED);
1095
1096 key = KEY_ONSCREEN_KEYBOARD;
1097 pad_key_down(dev, key);
1098 litest_dispatch(li);
1099 litest_assert_pad_key_event(li, key, LIBINPUT_KEY_STATE_PRESSED);
1100
1101 pad_key_up(dev, key);
1102 litest_dispatch(li);
1103 litest_assert_pad_key_event(li, key, LIBINPUT_KEY_STATE_RELEASED);
1104
1105 key = KEY_CONTROLPANEL;
1106 pad_key_down(dev, key);
1107 litest_dispatch(li);
1108 litest_assert_pad_key_event(li, key, LIBINPUT_KEY_STATE_PRESSED);
1109
1110 pad_key_up(dev, key);
1111 litest_dispatch(li);
1112 litest_assert_pad_key_event(li, key, LIBINPUT_KEY_STATE_RELEASED);
1113 }
1114 END_TEST
1115
1116 START_TEST(pad_strip_wacom_degenerate_max)
1117 {
1118 struct libinput_event *ev;
1119 struct libinput_event_tablet_pad *pev;
1120 double pos;
1121
1122 /* Override ABS_RX to have maximum=1, which would cause
1123 * log2(1) = 0 and division by zero in normalize_wacom_strip()
1124 * without the guard. */
1125 /* clang-format off */
1126 struct input_absinfo abs_override[] = {
1127 { ABS_RX, 0, 1, 0, 0, 0 },
1128 { -1, -1, -1, -1, -1, -1 },
1129 };
1130 /* clang-format on */
1131
1132 _litest_context_destroy_ struct libinput *li = litest_create_context();
1133 struct litest_device *dev =
1134 litest_add_device_with_overrides(li,
1135 LITEST_WACOM_INTUOS3_PAD,
1136 NULL,
1137 NULL,
1138 abs_override,
1139 NULL);
1140
1141 litest_drain_events(li);
1142
1143 /* Send strip events - value 1 with max 1 triggers
1144 * log2(1)/log2(1) = 0/0 = NaN without the guard */
1145 litest_pad_strip_start(dev, 100);
1146 litest_dispatch(li);
1147
1148 ev = libinput_get_event(li);
1149 pev = litest_is_pad_strip_event(ev, 0, LIBINPUT_TABLET_PAD_STRIP_SOURCE_FINGER);
1150 pos = libinput_event_tablet_pad_get_strip_position(pev);
1151 /* Without the fix, pos would be NaN from 0/0 division.
1152 * With the fix, pos must be a valid finite number. */
1153 litest_assert_double_ge(pos, 0.0);
1154 litest_assert_double_le(pos, 1.0);
1155 libinput_event_destroy(ev);
1156
1157 litest_pad_strip_end(dev);
1158 litest_drain_events(li);
1159
1160 litest_device_destroy(dev);
1161 }
1162 END_TEST
1163
1164 START_TEST(pad_send_events_disabled)
1165 {
1166 struct litest_device *dev = litest_current_device();
1167 struct libinput *li = dev->libinput;
1168 enum libinput_config_status status;
1169
1170 litest_drain_events(li);
1171
1172 status = libinput_device_config_send_events_set_mode(
1173 dev->libinput_device,
1174 LIBINPUT_CONFIG_SEND_EVENTS_DISABLED);
1175 litest_assert_enum_eq(status, LIBINPUT_CONFIG_STATUS_SUCCESS);
1176
1177 litest_pad_strip_start(dev, 10);
1178 litest_assert_empty_queue(li);
1179 litest_pad_strip_change(dev, 100);
1180 litest_assert_empty_queue(li);
1181 litest_pad_strip_end(dev);
1182 litest_assert_empty_queue(li);
1183
1184 litest_pad_ring_start(dev, 10);
1185 litest_assert_empty_queue(li);
1186 litest_pad_ring_change(dev, 100);
1187 litest_assert_empty_queue(li);
1188 litest_pad_ring_end(dev);
1189 litest_assert_empty_queue(li);
1190
1191 pad_key_down(dev, KEY_BUTTONCONFIG);
1192 litest_assert_empty_queue(li);
1193 pad_key_up(dev, KEY_BUTTONCONFIG);
1194 litest_assert_empty_queue(li);
1195 }
1196 END_TEST
1197
1198 TEST_COLLECTION(pad)
1199 {
1200 /* clang-format off */
1201 litest_add(pad_cap, LITEST_TABLET_PAD, LITEST_ANY);
1202 litest_add(pad_no_cap, LITEST_ANY, LITEST_TABLET_PAD);
1203
1204 litest_add(pad_time, LITEST_TABLET_PAD, LITEST_ANY);
1205
1206 litest_add(pad_num_buttons, LITEST_TABLET_PAD, LITEST_ANY);
1207 /* None of our dial devices have libwacom entries */
1208 litest_add(pad_num_buttons_libwacom, LITEST_TABLET_PAD, LITEST_DIAL);
1209 litest_add(pad_button_intuos, LITEST_TABLET_PAD, LITEST_ANY);
1210 litest_add(pad_button_bamboo, LITEST_TABLET_PAD, LITEST_ANY);
1211 /* None of our dial devices have libwacom entries */
1212 litest_add(pad_button_libwacom, LITEST_TABLET_PAD, LITEST_DIAL);
1213 litest_add(pad_button_mode_groups, LITEST_TABLET_PAD, LITEST_ANY);
1214
1215 litest_add(pad_has_ring, LITEST_RING, LITEST_ANY);
1216 litest_add(pad_ring, LITEST_RING, LITEST_ANY);
1217 litest_add(pad_ring_finger_up, LITEST_RING, LITEST_ANY);
1218
1219 litest_add(pad_has_strip, LITEST_STRIP, LITEST_ANY);
1220 litest_add(pad_strip, LITEST_STRIP, LITEST_ANY);
1221 litest_add(pad_strip_finger_up, LITEST_STRIP, LITEST_ANY);
1222 litest_add_no_device(pad_strip_wacom_degenerate_max);
1223
1224 litest_add(pad_has_dial, LITEST_DIAL, LITEST_ANY);
1225 litest_add(pad_dial_low_res, LITEST_DIAL, LITEST_ANY);
1226 litest_add(pad_dial_hi_res, LITEST_DIAL, LITEST_ANY);
1227
1228 litest_add_for_device(pad_left_handed_default, LITEST_WACOM_INTUOS5_PAD);
1229 litest_add_for_device(pad_no_left_handed, LITEST_WACOM_INTUOS3_PAD);
1230 litest_add_for_device(pad_left_handed_ring, LITEST_WACOM_INTUOS5_PAD);
1231 /* None of the current strip tablets are left-handed */
1232
1233 litest_add(pad_mode_groups, LITEST_TABLET_PAD, LITEST_ANY);
1234 litest_add(pad_mode_groups_userdata, LITEST_TABLET_PAD, LITEST_ANY);
1235 litest_add(pad_mode_groups_ref, LITEST_TABLET_PAD, LITEST_ANY);
1236 litest_add(pad_mode_group_mode, LITEST_TABLET_PAD, LITEST_ANY);
1237 litest_add(pad_mode_group_has, LITEST_TABLET_PAD, LITEST_ANY);
1238 litest_add(pad_mode_group_has_invalid, LITEST_TABLET_PAD, LITEST_ANY);
1239 litest_add(pad_mode_group_has_no_toggle, LITEST_TABLET_PAD, LITEST_ANY);
1240
1241 litest_add(pad_keys, LITEST_TABLET_PAD, LITEST_ANY);
1242
1243 litest_add(pad_send_events_disabled, LITEST_TABLET_PAD, LITEST_ANY);
1244 /* clang-format on */
1245 }