Debian Codegraph @

test/test-switch.c

raw
1 /*
2 * Copyright © 2017 James Ye <[email protected]>
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 <fcntl.h>
27 #include <libevdev/libevdev.h>
28 #include <libinput.h>
29
30 #include "libinput-util.h"
31 #include "litest.h"
32
33 static inline bool
34 switch_has_lid(struct litest_device *dev)
35 {
36 return libinput_device_switch_has_switch(dev->libinput_device,
37 LIBINPUT_SWITCH_LID) > 0;
38 }
39
40 static inline bool
41 switch_has_tablet_mode(struct litest_device *dev)
42 {
43 return libinput_device_switch_has_switch(dev->libinput_device,
44 LIBINPUT_SWITCH_TABLET_MODE) > 0;
45 }
46
47 START_TEST(switch_has_cap)
48 {
49 struct litest_device *dev = litest_current_device();
50
51 /* Need to check for this specific device here because the
52 * unreliable tablet mode switch removes the capability too */
53 if (dev->which == LITEST_TABLET_MODE_UNRELIABLE) {
54 litest_assert(
55 !libinput_device_has_capability(dev->libinput_device,
56 LIBINPUT_DEVICE_CAP_SWITCH));
57 return LITEST_NOT_APPLICABLE;
58 }
59
60 litest_assert(libinput_device_has_capability(dev->libinput_device,
61 LIBINPUT_DEVICE_CAP_SWITCH));
62 }
63 END_TEST
64
65 START_TEST(switch_has_lid_switch)
66 {
67 struct litest_device *dev = litest_current_device();
68
69 if (!libevdev_has_event_code(dev->evdev, EV_SW, SW_LID))
70 return LITEST_NOT_APPLICABLE;
71
72 litest_assert_int_eq(libinput_device_switch_has_switch(dev->libinput_device,
73 LIBINPUT_SWITCH_LID),
74 1);
75 }
76 END_TEST
77
78 static bool
79 tablet_mode_switch_is_reliable(struct litest_device *dev)
80 {
81 bool is_unreliable = false;
82
83 quirks_get_bool(dev->quirks,
84 QUIRK_MODEL_TABLET_MODE_SWITCH_UNRELIABLE,
85 &is_unreliable);
86
87 return !is_unreliable;
88 }
89
90 START_TEST(switch_has_tablet_mode_switch)
91 {
92 struct litest_device *dev = litest_current_device();
93 int has_switch;
94
95 if (!libevdev_has_event_code(dev->evdev, EV_SW, SW_TABLET_MODE))
96 return LITEST_NOT_APPLICABLE;
97
98 has_switch = libinput_device_switch_has_switch(dev->libinput_device,
99 LIBINPUT_SWITCH_TABLET_MODE);
100
101 if (!tablet_mode_switch_is_reliable(dev))
102 litest_assert_int_ne(has_switch, 1);
103 else
104 litest_assert_int_eq(has_switch, 1);
105 }
106 END_TEST
107
108 START_TEST(switch_has_keypad_slide_switch)
109 {
110 struct litest_device *dev = litest_current_device();
111
112 if (!libevdev_has_event_code(dev->evdev, EV_SW, SW_KEYPAD_SLIDE))
113 return LITEST_NOT_APPLICABLE;
114
115 litest_assert_int_eq(
116 libinput_device_switch_has_switch(dev->libinput_device,
117 LIBINPUT_SWITCH_KEYPAD_SLIDE),
118 1);
119 }
120 END_TEST
121
122 START_TEST(switch_toggle)
123 {
124 struct litest_device *dev = litest_current_device();
125 struct libinput *li = dev->libinput;
126 struct libinput_event *event;
127 enum libinput_switch sw = litest_test_param_get_i32(test_env->params, "switch");
128
129 litest_drain_events(li);
130
131 litest_grab_device(dev);
132 litest_switch_action(dev, sw, LIBINPUT_SWITCH_STATE_ON);
133 litest_dispatch(li);
134
135 if (libinput_device_switch_has_switch(dev->libinput_device, sw) > 0) {
136 event = libinput_get_event(li);
137 litest_is_switch_event(event, sw, LIBINPUT_SWITCH_STATE_ON);
138 libinput_event_destroy(event);
139 } else {
140 litest_assert_empty_queue(li);
141 }
142
143 litest_switch_action(dev, sw, LIBINPUT_SWITCH_STATE_OFF);
144 litest_dispatch(li);
145
146 if (libinput_device_switch_has_switch(dev->libinput_device, sw) > 0) {
147 event = libinput_get_event(li);
148 litest_is_switch_event(event, sw, LIBINPUT_SWITCH_STATE_OFF);
149 libinput_event_destroy(event);
150 }
151
152 litest_assert_empty_queue(li);
153 litest_ungrab_device(dev);
154 }
155 END_TEST
156
157 START_TEST(switch_toggle_double)
158 {
159 struct litest_device *dev = litest_current_device();
160 struct libinput *li = dev->libinput;
161 struct libinput_event *event;
162 enum libinput_switch sw = litest_test_param_get_i32(test_env->params, "switch");
163
164 if (libinput_device_switch_has_switch(dev->libinput_device, sw) <= 0)
165 return LITEST_NOT_APPLICABLE;
166
167 litest_drain_events(li);
168
169 litest_grab_device(dev);
170 litest_switch_action(dev, sw, LIBINPUT_SWITCH_STATE_ON);
171 litest_dispatch(li);
172
173 event = libinput_get_event(li);
174 litest_is_switch_event(event, sw, LIBINPUT_SWITCH_STATE_ON);
175 libinput_event_destroy(event);
176
177 /* This will be filtered by the kernel, so this test is a bit
178 * useless */
179 litest_switch_action(dev, sw, LIBINPUT_SWITCH_STATE_ON);
180 litest_dispatch(li);
181
182 litest_assert_empty_queue(li);
183 litest_ungrab_device(dev);
184 }
185 END_TEST
186
187 static bool
188 lid_switch_is_reliable(struct litest_device *dev)
189 {
190 char *prop;
191 bool is_reliable = false;
192
193 if (quirks_get_string(dev->quirks, QUIRK_ATTR_LID_SWITCH_RELIABILITY, &prop)) {
194 is_reliable = streq(prop, "reliable");
195 }
196
197 return is_reliable;
198 }
199
200 START_TEST(switch_down_on_init)
201 {
202 struct litest_device *dev = litest_current_device();
203 struct libinput_event *event;
204 enum libinput_switch sw = litest_test_param_get_i32(test_env->params, "switch");
205
206 if (libinput_device_switch_has_switch(dev->libinput_device, sw) <= 0)
207 return LITEST_NOT_APPLICABLE;
208
209 if (sw == LIBINPUT_SWITCH_LID && !lid_switch_is_reliable(dev))
210 return LITEST_NOT_APPLICABLE;
211
212 litest_grab_device(dev);
213 litest_switch_action(dev, sw, LIBINPUT_SWITCH_STATE_ON);
214 litest_ungrab_device(dev);
215
216 /* need separate context to test */
217 _litest_context_destroy_ struct libinput *li = litest_create_context();
218 libinput_path_add_device(li, libevdev_uinput_get_devnode(dev->uinput));
219 litest_dispatch(li);
220
221 litest_wait_for_event_of_type(li, LIBINPUT_EVENT_SWITCH_TOGGLE);
222 event = libinput_get_event(li);
223 litest_is_switch_event(event, sw, LIBINPUT_SWITCH_STATE_ON);
224 libinput_event_destroy(event);
225
226 while ((event = libinput_get_event(li))) {
227 litest_assert_enum_ne(libinput_event_get_type(event),
228 LIBINPUT_EVENT_SWITCH_TOGGLE);
229 libinput_event_destroy(event);
230 }
231
232 litest_switch_action(dev, sw, LIBINPUT_SWITCH_STATE_OFF);
233 litest_dispatch(li);
234 event = libinput_get_event(li);
235 litest_is_switch_event(event, sw, LIBINPUT_SWITCH_STATE_OFF);
236 libinput_event_destroy(event);
237 litest_assert_empty_queue(li);
238 }
239 END_TEST
240
241 START_TEST(switch_not_down_on_init)
242 {
243 struct litest_device *dev = litest_current_device();
244 struct libinput_event *event;
245 enum libinput_switch sw = LIBINPUT_SWITCH_LID;
246
247 if (libinput_device_switch_has_switch(dev->libinput_device, sw) <= 0)
248 return LITEST_NOT_APPLICABLE;
249
250 if (sw == LIBINPUT_SWITCH_LID && lid_switch_is_reliable(dev))
251 return LITEST_NOT_APPLICABLE;
252
253 litest_grab_device(dev);
254 litest_switch_action(dev, sw, LIBINPUT_SWITCH_STATE_ON);
255 litest_ungrab_device(dev);
256
257 /* need separate context to test */
258 _litest_context_destroy_ struct libinput *li = litest_create_context();
259 libinput_path_add_device(li, libevdev_uinput_get_devnode(dev->uinput));
260 litest_dispatch(li);
261
262 while ((event = libinput_get_event(li)) != NULL) {
263 litest_assert_enum_ne(libinput_event_get_type(event),
264 LIBINPUT_EVENT_SWITCH_TOGGLE);
265 libinput_event_destroy(event);
266 }
267
268 litest_switch_action(dev, sw, LIBINPUT_SWITCH_STATE_OFF);
269 litest_assert_empty_queue(li);
270 }
271 END_TEST
272
273 static inline struct litest_device *
274 switch_init_paired_touchpad(struct libinput *li)
275 {
276 enum litest_device_type which = LITEST_SYNAPTICS_I2C;
277
278 return litest_add_device(li, which);
279 }
280
281 START_TEST(switch_disable_touchpad)
282 {
283 struct litest_device *sw = litest_current_device();
284 struct litest_device *touchpad;
285 struct libinput *li = sw->libinput;
286 enum libinput_switch which =
287 litest_test_param_get_i32(test_env->params, "switch");
288 if (libinput_device_switch_has_switch(sw->libinput_device, which) <= 0)
289 return LITEST_NOT_APPLICABLE;
290
291 touchpad = switch_init_paired_touchpad(li);
292 litest_disable_tap(touchpad->libinput_device);
293 litest_disable_hold_gestures(touchpad->libinput_device);
294 litest_drain_events(li);
295
296 litest_grab_device(sw);
297
298 /* switch is on - no events */
299 litest_switch_action(sw, which, LIBINPUT_SWITCH_STATE_ON);
300 litest_assert_only_typed_events(li, LIBINPUT_EVENT_SWITCH_TOGGLE);
301
302 litest_touch_down(touchpad, 0, 50, 50);
303 litest_touch_move_to(touchpad, 0, 50, 50, 70, 50, 10);
304 litest_touch_up(touchpad, 0);
305 litest_assert_empty_queue(li);
306
307 /* switch is off - motion events */
308 litest_switch_action(sw, which, LIBINPUT_SWITCH_STATE_OFF);
309 litest_assert_only_typed_events(li, LIBINPUT_EVENT_SWITCH_TOGGLE);
310
311 litest_touch_down(touchpad, 0, 50, 50);
312 litest_touch_move_to(touchpad, 0, 50, 50, 70, 50, 10);
313 litest_touch_up(touchpad, 0);
314 litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION);
315
316 litest_device_destroy(touchpad);
317 litest_ungrab_device(sw);
318 }
319 END_TEST
320
321 START_TEST(switch_disable_touchpad_during_touch)
322 {
323 struct litest_device *sw = litest_current_device();
324 struct litest_device *touchpad;
325 struct libinput *li = sw->libinput;
326 enum libinput_switch which =
327 litest_test_param_get_i32(test_env->params, "switch");
328 if (libinput_device_switch_has_switch(sw->libinput_device, which) <= 0)
329 return LITEST_NOT_APPLICABLE;
330
331 touchpad = switch_init_paired_touchpad(li);
332 litest_disable_tap(touchpad->libinput_device);
333 litest_disable_hold_gestures(touchpad->libinput_device);
334 litest_drain_events(li);
335
336 litest_touch_down(touchpad, 0, 50, 50);
337 litest_touch_move_to(touchpad, 0, 50, 50, 70, 50, 5);
338 litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION);
339
340 litest_grab_device(sw);
341 litest_switch_action(sw, which, LIBINPUT_SWITCH_STATE_ON);
342 litest_assert_only_typed_events(li, LIBINPUT_EVENT_SWITCH_TOGGLE);
343 litest_ungrab_device(sw);
344
345 litest_touch_move_to(touchpad, 0, 70, 50, 50, 50, 5);
346 litest_touch_up(touchpad, 0);
347 litest_assert_empty_queue(li);
348
349 litest_device_destroy(touchpad);
350 }
351 END_TEST
352
353 START_TEST(switch_disable_touchpad_edge_scroll)
354 {
355 struct litest_device *sw = litest_current_device();
356 struct litest_device *touchpad;
357 struct libinput *li = sw->libinput;
358 enum libinput_switch which =
359 litest_test_param_get_i32(test_env->params, "switch");
360 if (libinput_device_switch_has_switch(sw->libinput_device, which) <= 0)
361 return LITEST_NOT_APPLICABLE;
362
363 touchpad = switch_init_paired_touchpad(li);
364 litest_enable_edge_scroll(touchpad);
365
366 litest_drain_events(li);
367
368 litest_grab_device(sw);
369 litest_switch_action(sw, which, LIBINPUT_SWITCH_STATE_ON);
370 litest_assert_only_typed_events(li, LIBINPUT_EVENT_SWITCH_TOGGLE);
371 litest_ungrab_device(sw);
372
373 litest_touch_down(touchpad, 0, 99, 20);
374 litest_dispatch(li);
375 litest_timeout_edgescroll(li);
376 litest_dispatch(li);
377 litest_assert_empty_queue(li);
378
379 litest_touch_move_to(touchpad, 0, 99, 20, 99, 80, 60);
380 litest_dispatch(li);
381 litest_assert_empty_queue(li);
382
383 litest_touch_move_to(touchpad, 0, 99, 80, 99, 20, 60);
384 litest_touch_up(touchpad, 0);
385 litest_dispatch(li);
386 litest_assert_empty_queue(li);
387
388 litest_device_destroy(touchpad);
389 }
390 END_TEST
391
392 START_TEST(switch_disable_touchpad_edge_scroll_interrupt)
393 {
394 struct litest_device *sw = litest_current_device();
395 struct litest_device *touchpad;
396 struct libinput *li = sw->libinput;
397 struct libinput_event *event;
398 enum libinput_switch which =
399 litest_test_param_get_i32(test_env->params, "switch");
400 if (libinput_device_switch_has_switch(sw->libinput_device, which) <= 0)
401 return LITEST_NOT_APPLICABLE;
402
403 touchpad = switch_init_paired_touchpad(li);
404 litest_enable_edge_scroll(touchpad);
405
406 litest_drain_events(li);
407
408 litest_touch_down(touchpad, 0, 99, 20);
409 litest_dispatch(li);
410 litest_timeout_edgescroll(li);
411 litest_touch_move_to(touchpad, 0, 99, 20, 99, 30, 10);
412 litest_dispatch(li);
413 litest_assert_only_axis_events(li, LIBINPUT_EVENT_POINTER_SCROLL_FINGER);
414
415 litest_grab_device(sw);
416 litest_switch_action(sw, which, LIBINPUT_SWITCH_STATE_ON);
417 litest_ungrab_device(sw);
418 litest_dispatch(li);
419
420 litest_assert_axis_end_sequence(li,
421 LIBINPUT_EVENT_POINTER_SCROLL_FINGER,
422 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
423 LIBINPUT_POINTER_AXIS_SOURCE_FINGER);
424
425 event = libinput_get_event(li);
426 litest_is_switch_event(event, which, LIBINPUT_SWITCH_STATE_ON);
427 libinput_event_destroy(event);
428
429 litest_device_destroy(touchpad);
430 }
431 END_TEST
432
433 START_TEST(switch_disable_touchpad_already_open)
434 {
435 struct litest_device *sw = litest_current_device();
436 struct litest_device *touchpad;
437 struct libinput *li = sw->libinput;
438 enum libinput_switch which =
439 litest_test_param_get_i32(test_env->params, "switch");
440 if (libinput_device_switch_has_switch(sw->libinput_device, which) <= 0)
441 return LITEST_NOT_APPLICABLE;
442
443 touchpad = switch_init_paired_touchpad(li);
444
445 litest_disable_tap(touchpad->libinput_device);
446 litest_disable_hold_gestures(touchpad->libinput_device);
447 litest_drain_events(li);
448
449 /* default: switch is off - motion events */
450 litest_touch_down(touchpad, 0, 50, 50);
451 litest_touch_move_to(touchpad, 0, 50, 50, 70, 50, 10);
452 litest_touch_up(touchpad, 0);
453 litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION);
454
455 /* disable switch - motion events */
456 litest_switch_action(sw, which, LIBINPUT_SWITCH_STATE_OFF);
457 litest_assert_empty_queue(li);
458
459 litest_touch_down(touchpad, 0, 50, 50);
460 litest_touch_move_to(touchpad, 0, 50, 50, 70, 50, 10);
461 litest_touch_up(touchpad, 0);
462 litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION);
463
464 litest_device_destroy(touchpad);
465 }
466 END_TEST
467
468 START_TEST(switch_dont_resume_disabled_touchpad)
469 {
470 struct litest_device *sw = litest_current_device();
471 struct litest_device *touchpad;
472 struct libinput *li = sw->libinput;
473 enum libinput_switch which =
474 litest_test_param_get_i32(test_env->params, "switch");
475 if (libinput_device_switch_has_switch(sw->libinput_device, which) <= 0)
476 return LITEST_NOT_APPLICABLE;
477
478 touchpad = switch_init_paired_touchpad(li);
479 litest_disable_tap(touchpad->libinput_device);
480 litest_disable_hold_gestures(touchpad->libinput_device);
481 libinput_device_config_send_events_set_mode(
482 touchpad->libinput_device,
483 LIBINPUT_CONFIG_SEND_EVENTS_DISABLED);
484 litest_drain_events(li);
485
486 /* switch is on - no events */
487 litest_grab_device(sw);
488 litest_switch_action(sw, which, LIBINPUT_SWITCH_STATE_ON);
489 litest_ungrab_device(sw);
490 litest_assert_only_typed_events(li, LIBINPUT_EVENT_SWITCH_TOGGLE);
491
492 litest_touch_down(touchpad, 0, 50, 50);
493 litest_touch_move_to(touchpad, 0, 50, 50, 70, 50, 10);
494 litest_touch_up(touchpad, 0);
495 litest_assert_empty_queue(li);
496
497 /* switch is off but but tp is still disabled */
498 litest_switch_action(sw, which, LIBINPUT_SWITCH_STATE_OFF);
499 litest_assert_only_typed_events(li, LIBINPUT_EVENT_SWITCH_TOGGLE);
500
501 litest_touch_down(touchpad, 0, 50, 50);
502 litest_touch_move_to(touchpad, 0, 50, 50, 70, 50, 10);
503 litest_touch_up(touchpad, 0);
504 litest_assert_empty_queue(li);
505
506 litest_device_destroy(touchpad);
507 }
508 END_TEST
509
510 START_TEST(switch_dont_resume_disabled_touchpad_external_mouse)
511 {
512 struct litest_device *sw = litest_current_device();
513 struct litest_device *touchpad, *mouse;
514 struct libinput *li = sw->libinput;
515 enum libinput_switch which =
516 litest_test_param_get_i32(test_env->params, "switch");
517 if (libinput_device_switch_has_switch(sw->libinput_device, which) <= 0)
518 return LITEST_NOT_APPLICABLE;
519
520 touchpad = switch_init_paired_touchpad(li);
521 mouse = litest_add_device(li, LITEST_MOUSE);
522 litest_disable_tap(touchpad->libinput_device);
523 litest_disable_hold_gestures(touchpad->libinput_device);
524 libinput_device_config_send_events_set_mode(
525 touchpad->libinput_device,
526 LIBINPUT_CONFIG_SEND_EVENTS_DISABLED_ON_EXTERNAL_MOUSE);
527 litest_drain_events(li);
528
529 litest_touch_down(touchpad, 0, 50, 50);
530 litest_touch_move_to(touchpad, 0, 50, 50, 70, 50, 10);
531 litest_touch_up(touchpad, 0);
532 litest_assert_empty_queue(li);
533
534 /* switch is on - no events */
535 litest_grab_device(sw);
536 litest_switch_action(sw, which, LIBINPUT_SWITCH_STATE_ON);
537 litest_ungrab_device(sw);
538 litest_assert_only_typed_events(li, LIBINPUT_EVENT_SWITCH_TOGGLE);
539
540 litest_touch_down(touchpad, 0, 50, 50);
541 litest_touch_move_to(touchpad, 0, 50, 50, 70, 50, 10);
542 litest_touch_up(touchpad, 0);
543 litest_assert_empty_queue(li);
544
545 /* switch is off but but tp is still disabled */
546 litest_switch_action(sw, which, LIBINPUT_SWITCH_STATE_OFF);
547 litest_assert_only_typed_events(li, LIBINPUT_EVENT_SWITCH_TOGGLE);
548
549 litest_touch_down(touchpad, 0, 50, 50);
550 litest_touch_move_to(touchpad, 0, 50, 50, 70, 50, 10);
551 litest_touch_up(touchpad, 0);
552 litest_assert_empty_queue(li);
553
554 litest_device_destroy(touchpad);
555 litest_device_destroy(mouse);
556 }
557 END_TEST
558
559 START_TEST(lid_open_on_key)
560 {
561 struct litest_device *sw = litest_current_device();
562 struct litest_device *keyboard;
563 struct libinput *li = sw->libinput;
564 struct libinput_event *event;
565
566 if (!switch_has_lid(sw))
567 return LITEST_NOT_APPLICABLE;
568
569 keyboard = litest_add_device(li, LITEST_KEYBOARD);
570
571 litest_grab_device(sw);
572 for (int i = 0; i < 3; i++) {
573 litest_switch_action(sw, LIBINPUT_SWITCH_LID, LIBINPUT_SWITCH_STATE_ON);
574 litest_drain_events(li);
575
576 litest_event(keyboard, EV_KEY, KEY_A, 1);
577 litest_event(keyboard, EV_SYN, SYN_REPORT, 0);
578 litest_event(keyboard, EV_KEY, KEY_A, 0);
579 litest_event(keyboard, EV_SYN, SYN_REPORT, 0);
580 litest_dispatch(li);
581
582 event = libinput_get_event(li);
583 litest_is_switch_event(event,
584 LIBINPUT_SWITCH_LID,
585 LIBINPUT_SWITCH_STATE_OFF);
586 libinput_event_destroy(event);
587
588 litest_assert_only_typed_events(li, LIBINPUT_EVENT_KEYBOARD_KEY);
589
590 litest_switch_action(sw,
591 LIBINPUT_SWITCH_LID,
592 LIBINPUT_SWITCH_STATE_OFF);
593 litest_assert_empty_queue(li);
594 }
595 litest_ungrab_device(sw);
596
597 litest_device_destroy(keyboard);
598 }
599 END_TEST
600
601 START_TEST(lid_open_on_key_touchpad_enabled)
602 {
603 struct litest_device *sw = litest_current_device();
604 struct litest_device *keyboard, *touchpad;
605 struct libinput *li = sw->libinput;
606
607 if (!switch_has_lid(sw))
608 return LITEST_NOT_APPLICABLE;
609
610 keyboard = litest_add_device(li, LITEST_KEYBOARD);
611 touchpad = litest_add_device(li, LITEST_SYNAPTICS_I2C);
612
613 litest_disable_hold_gestures(touchpad->libinput_device);
614
615 litest_grab_device(sw);
616 litest_switch_action(sw, LIBINPUT_SWITCH_LID, LIBINPUT_SWITCH_STATE_ON);
617 litest_ungrab_device(sw);
618 litest_drain_events(li);
619
620 litest_event(keyboard, EV_KEY, KEY_A, 1);
621 litest_event(keyboard, EV_SYN, SYN_REPORT, 0);
622 litest_event(keyboard, EV_KEY, KEY_A, 0);
623 litest_event(keyboard, EV_SYN, SYN_REPORT, 0);
624 litest_drain_events(li);
625 litest_timeout_dwt_long(li);
626
627 litest_touch_down(touchpad, 0, 50, 50);
628 litest_touch_move_to(touchpad, 0, 50, 50, 70, 70, 10);
629 litest_touch_up(touchpad, 0);
630 litest_dispatch(li);
631
632 litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION);
633
634 litest_device_destroy(keyboard);
635 litest_device_destroy(touchpad);
636 }
637 END_TEST
638
639 START_TEST(switch_suspend_with_keyboard)
640 {
641 struct litest_device *keyboard;
642 struct litest_device *sw;
643 enum libinput_switch which =
644 litest_test_param_get_i32(test_env->params, "switch");
645
646 _litest_context_destroy_ struct libinput *li = litest_create_context();
647
648 switch (which) {
649 case LIBINPUT_SWITCH_LID:
650 sw = litest_add_device(li, LITEST_LID_SWITCH);
651 break;
652 case LIBINPUT_SWITCH_TABLET_MODE:
653 sw = litest_add_device(li, LITEST_THINKPAD_EXTRABUTTONS);
654 break;
655 case LIBINPUT_SWITCH_KEYPAD_SLIDE:
656 sw = litest_add_device(li, LITEST_KEYPAD_SLIDE_SWITCH);
657 break;
658 default:
659 abort();
660 }
661
662 litest_dispatch(li);
663
664 keyboard = litest_add_device(li, LITEST_KEYBOARD);
665 litest_dispatch(li);
666
667 litest_grab_device(sw);
668 litest_switch_action(sw, which, LIBINPUT_SWITCH_STATE_ON);
669 litest_drain_events(li);
670 litest_switch_action(sw, which, LIBINPUT_SWITCH_STATE_OFF);
671 litest_drain_events(li);
672 litest_ungrab_device(sw);
673
674 litest_device_destroy(keyboard);
675 litest_drain_events(li);
676
677 litest_device_destroy(sw);
678 litest_dispatch(li);
679 }
680 END_TEST
681
682 START_TEST(switch_suspend_with_touchpad)
683 {
684 struct litest_device *touchpad, *sw;
685 enum libinput_switch which =
686 litest_test_param_get_i32(test_env->params, "switch");
687
688 _litest_context_destroy_ struct libinput *li = litest_create_context();
689
690 switch (which) {
691 case LIBINPUT_SWITCH_LID:
692 sw = litest_add_device(li, LITEST_LID_SWITCH);
693 break;
694 case LIBINPUT_SWITCH_TABLET_MODE:
695 sw = litest_add_device(li, LITEST_THINKPAD_EXTRABUTTONS);
696 break;
697 default:
698 abort();
699 }
700
701 litest_drain_events(li);
702
703 touchpad = litest_add_device(li, LITEST_SYNAPTICS_I2C);
704 litest_drain_events(li);
705
706 litest_grab_device(sw);
707 litest_switch_action(sw, which, LIBINPUT_SWITCH_STATE_ON);
708 litest_drain_events(li);
709 litest_switch_action(sw, which, LIBINPUT_SWITCH_STATE_OFF);
710 litest_drain_events(li);
711 litest_ungrab_device(sw);
712
713 litest_device_destroy(sw);
714 litest_drain_events(li);
715 litest_device_destroy(touchpad);
716 litest_drain_events(li);
717 }
718 END_TEST
719
720 START_TEST(lid_update_hw_on_key)
721 {
722 struct litest_device *sw = litest_current_device();
723 struct libinput *li = sw->libinput;
724 struct litest_device *keyboard;
725 _free_(libevdev) *evdev = NULL;
726 struct input_event event;
727 int rc;
728
729 if (!switch_has_lid(sw))
730 return LITEST_NOT_APPLICABLE;
731
732 keyboard = litest_add_device(li, LITEST_KEYBOARD);
733
734 litest_grab_device(sw);
735 litest_switch_action(sw, LIBINPUT_SWITCH_LID, LIBINPUT_SWITCH_STATE_ON);
736 litest_drain_events(li);
737 litest_ungrab_device(sw);
738
739 /* Separate direct libevdev context to check if the HW event goes
740 * through */
741 _autoclose_ int fd =
742 open(libevdev_uinput_get_devnode(sw->uinput), O_RDONLY | O_NONBLOCK);
743 litest_assert_int_ge(fd, 0);
744 litest_assert_int_eq(libevdev_new_from_fd(fd, &evdev), 0);
745 litest_assert_int_eq(libevdev_get_event_value(evdev, EV_SW, SW_LID), 1);
746
747 /* Typing on the keyboard should trigger a lid open event */
748 litest_event(keyboard, EV_KEY, KEY_A, 1);
749 litest_event(keyboard, EV_SYN, SYN_REPORT, 0);
750 litest_event(keyboard, EV_KEY, KEY_A, 0);
751 litest_event(keyboard, EV_SYN, SYN_REPORT, 0);
752 litest_drain_events(li);
753
754 rc = libevdev_next_event(evdev, LIBEVDEV_READ_FLAG_NORMAL, &event);
755 litest_assert_int_eq(rc, LIBEVDEV_READ_STATUS_SUCCESS);
756 litest_assert_int_eq(event.type, EV_SW);
757 litest_assert_int_eq(event.code, SW_LID);
758 litest_assert_int_eq(event.value, 0);
759 rc = libevdev_next_event(evdev, LIBEVDEV_READ_FLAG_NORMAL, &event);
760 litest_assert_int_eq(rc, LIBEVDEV_READ_STATUS_SUCCESS);
761 litest_assert_int_eq(event.type, EV_SYN);
762 litest_assert_int_eq(event.code, SYN_REPORT);
763 litest_assert_int_eq(event.value, 0);
764 rc = libevdev_next_event(evdev, LIBEVDEV_READ_FLAG_NORMAL, &event);
765 litest_assert_int_eq(rc, -EAGAIN);
766
767 litest_device_destroy(keyboard);
768 }
769 END_TEST
770
771 START_TEST(lid_update_hw_on_key_closed_on_init)
772 {
773 struct litest_device *sw = litest_current_device();
774 struct litest_device *keyboard;
775 _free_(libevdev) * evdev;
776 struct input_event event;
777 int rc;
778
779 litest_grab_device(sw);
780 litest_switch_action(sw, LIBINPUT_SWITCH_LID, LIBINPUT_SWITCH_STATE_ON);
781 litest_ungrab_device(sw);
782
783 /* Separate direct libevdev context to check if the HW event goes
784 * through */
785 _autoclose_ int fd =
786 open(libevdev_uinput_get_devnode(sw->uinput), O_RDONLY | O_NONBLOCK);
787 litest_assert_int_ge(fd, 0);
788 litest_assert_int_eq(libevdev_new_from_fd(fd, &evdev), 0);
789 litest_assert_int_eq(libevdev_get_event_value(evdev, EV_SW, SW_LID), 1);
790
791 keyboard = litest_add_device(sw->libinput, LITEST_KEYBOARD);
792
793 /* separate context for the right state on init */
794 _litest_context_destroy_ struct libinput *li = litest_create_context();
795 libinput_path_add_device(li, libevdev_uinput_get_devnode(sw->uinput));
796 libinput_path_add_device(li, libevdev_uinput_get_devnode(keyboard->uinput));
797
798 /* don't expect a switch waiting for us, this is run for an
799 * unreliable device */
800 while (libinput_next_event_type(li) != LIBINPUT_EVENT_NONE) {
801 litest_assert_enum_ne(libinput_next_event_type(li),
802 LIBINPUT_EVENT_SWITCH_TOGGLE);
803 libinput_event_destroy(libinput_get_event(li));
804 }
805
806 litest_event(keyboard, EV_KEY, KEY_A, 1);
807 litest_event(keyboard, EV_SYN, SYN_REPORT, 0);
808 litest_event(keyboard, EV_KEY, KEY_A, 0);
809 litest_event(keyboard, EV_SYN, SYN_REPORT, 0);
810 /* No switch event, we're still in vanilla (open) state */
811 litest_assert_only_typed_events(li, LIBINPUT_EVENT_KEYBOARD_KEY);
812
813 /* Make sure kernel state has updated */
814 rc = libevdev_next_event(evdev, LIBEVDEV_READ_FLAG_NORMAL, &event);
815 litest_assert_int_eq(rc, LIBEVDEV_READ_STATUS_SUCCESS);
816 litest_assert_int_eq(event.type, EV_SW);
817 litest_assert_int_eq(event.code, SW_LID);
818 litest_assert_int_eq(event.value, 0);
819 rc = libevdev_next_event(evdev, LIBEVDEV_READ_FLAG_NORMAL, &event);
820 litest_assert_int_eq(rc, LIBEVDEV_READ_STATUS_SUCCESS);
821 litest_assert_int_eq(event.type, EV_SYN);
822 litest_assert_int_eq(event.code, SYN_REPORT);
823 litest_assert_int_eq(event.value, 0);
824 rc = libevdev_next_event(evdev, LIBEVDEV_READ_FLAG_NORMAL, &event);
825 litest_assert_int_eq(rc, -EAGAIN);
826
827 litest_device_destroy(keyboard);
828 }
829 END_TEST
830
831 START_TEST(lid_update_hw_on_key_multiple_keyboards)
832 {
833 struct litest_device *sw = litest_current_device();
834 struct libinput *li = sw->libinput;
835 struct litest_device *keyboard1, *keyboard2;
836 _free_(libevdev) * evdev;
837 struct input_event event;
838 int rc;
839
840 if (!switch_has_lid(sw))
841 return LITEST_NOT_APPLICABLE;
842
843 keyboard1 = litest_add_device(li, LITEST_KEYBOARD_BLADE_STEALTH_VIDEOSWITCH);
844 litest_dispatch(li);
845
846 keyboard2 = litest_add_device(li, LITEST_KEYBOARD_BLADE_STEALTH);
847 litest_dispatch(li);
848
849 litest_grab_device(sw);
850 litest_switch_action(sw, LIBINPUT_SWITCH_LID, LIBINPUT_SWITCH_STATE_ON);
851 litest_drain_events(li);
852 litest_ungrab_device(sw);
853
854 /* Separate direct libevdev context to check if the HW event goes
855 * through */
856 _autoclose_ int fd =
857 open(libevdev_uinput_get_devnode(sw->uinput), O_RDONLY | O_NONBLOCK);
858 litest_assert_int_ge(fd, 0);
859 litest_assert_int_eq(libevdev_new_from_fd(fd, &evdev), 0);
860 litest_assert_int_eq(libevdev_get_event_value(evdev, EV_SW, SW_LID), 1);
861
862 /* Typing on the second keyboard should trigger a lid open event */
863 litest_event(keyboard2, EV_KEY, KEY_A, 1);
864 litest_event(keyboard2, EV_SYN, SYN_REPORT, 0);
865 litest_event(keyboard2, EV_KEY, KEY_A, 0);
866 litest_event(keyboard2, EV_SYN, SYN_REPORT, 0);
867 litest_drain_events(li);
868
869 rc = libevdev_next_event(evdev, LIBEVDEV_READ_FLAG_NORMAL, &event);
870 litest_assert_int_eq(rc, LIBEVDEV_READ_STATUS_SUCCESS);
871 litest_assert_int_eq(event.type, EV_SW);
872 litest_assert_int_eq(event.code, SW_LID);
873 litest_assert_int_eq(event.value, 0);
874 rc = libevdev_next_event(evdev, LIBEVDEV_READ_FLAG_NORMAL, &event);
875 litest_assert_int_eq(rc, LIBEVDEV_READ_STATUS_SUCCESS);
876 litest_assert_int_eq(event.type, EV_SYN);
877 litest_assert_int_eq(event.code, SYN_REPORT);
878 litest_assert_int_eq(event.value, 0);
879 rc = libevdev_next_event(evdev, LIBEVDEV_READ_FLAG_NORMAL, &event);
880 litest_assert_int_eq(rc, -EAGAIN);
881
882 litest_device_destroy(keyboard1);
883 litest_device_destroy(keyboard2);
884 }
885 END_TEST
886
887 START_TEST(lid_key_press)
888 {
889 struct litest_device *sw = litest_current_device();
890 struct libinput *li = sw->libinput;
891
892 litest_drain_events(li);
893
894 litest_keyboard_key(sw, KEY_VOLUMEUP, true);
895 litest_keyboard_key(sw, KEY_VOLUMEUP, false);
896 litest_dispatch(li);
897
898 /* Check that we're routing key events from a lid device too */
899 litest_assert_only_typed_events(li, LIBINPUT_EVENT_KEYBOARD_KEY);
900 }
901 END_TEST
902
903 START_TEST(tablet_mode_disable_touchpad_on_init)
904 {
905 struct litest_device *sw = litest_current_device();
906 struct litest_device *touchpad;
907 struct libinput *li = sw->libinput;
908
909 if (!switch_has_tablet_mode(sw))
910 return LITEST_NOT_APPLICABLE;
911
912 litest_grab_device(sw);
913 litest_switch_action(sw, LIBINPUT_SWITCH_TABLET_MODE, LIBINPUT_SWITCH_STATE_ON);
914 litest_drain_events(li);
915
916 /* touchpad comes with switch already on - no events */
917 touchpad = switch_init_paired_touchpad(li);
918 litest_disable_tap(touchpad->libinput_device);
919 litest_disable_hold_gestures(touchpad->libinput_device);
920 litest_drain_events(li);
921
922 litest_touch_down(touchpad, 0, 50, 50);
923 litest_touch_move_to(touchpad, 0, 50, 50, 70, 50, 10);
924 litest_touch_up(touchpad, 0);
925 litest_assert_empty_queue(li);
926
927 litest_switch_action(sw,
928 LIBINPUT_SWITCH_TABLET_MODE,
929 LIBINPUT_SWITCH_STATE_OFF);
930 litest_assert_only_typed_events(li, LIBINPUT_EVENT_SWITCH_TOGGLE);
931
932 litest_touch_down(touchpad, 0, 50, 50);
933 litest_touch_move_to(touchpad, 0, 50, 50, 70, 50, 10);
934 litest_touch_up(touchpad, 0);
935 litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION);
936 litest_ungrab_device(sw);
937
938 litest_device_destroy(touchpad);
939 }
940 END_TEST
941
942 START_TEST(tablet_mode_disable_touchpad_on_resume)
943 {
944 struct litest_device *sw = litest_current_device();
945 struct litest_device *touchpad;
946 struct libinput *li = sw->libinput;
947 struct libinput_event *event;
948 bool have_switch_toggle = false;
949
950 if (!switch_has_tablet_mode(sw))
951 return LITEST_NOT_APPLICABLE;
952
953 touchpad = switch_init_paired_touchpad(li);
954 litest_disable_tap(touchpad->libinput_device);
955 litest_disable_hold_gestures(touchpad->libinput_device);
956 litest_drain_events(li);
957
958 libinput_suspend(li);
959 litest_switch_action(sw, LIBINPUT_SWITCH_TABLET_MODE, LIBINPUT_SWITCH_STATE_ON);
960 litest_drain_events(li);
961 libinput_resume(li);
962 litest_dispatch(li);
963
964 while ((event = libinput_get_event(li))) {
965 enum libinput_event_type type;
966
967 type = libinput_event_get_type(event);
968 switch (type) {
969 case LIBINPUT_EVENT_DEVICE_ADDED:
970 break;
971 case LIBINPUT_EVENT_SWITCH_TOGGLE:
972 litest_is_switch_event(event,
973 LIBINPUT_SWITCH_TABLET_MODE,
974 LIBINPUT_SWITCH_STATE_ON);
975 have_switch_toggle = true;
976 break;
977 default:
978 litest_abort_msg("Unexpected event type");
979 }
980 libinput_event_destroy(event);
981 }
982
983 litest_assert(have_switch_toggle);
984
985 litest_touch_down(touchpad, 0, 50, 50);
986 litest_touch_move_to(touchpad, 0, 50, 50, 70, 50, 10);
987 litest_touch_up(touchpad, 0);
988 litest_assert_empty_queue(li);
989
990 litest_switch_action(sw,
991 LIBINPUT_SWITCH_TABLET_MODE,
992 LIBINPUT_SWITCH_STATE_OFF);
993 litest_dispatch(li);
994 event = libinput_get_event(li);
995 litest_is_switch_event(event,
996 LIBINPUT_SWITCH_TABLET_MODE,
997 LIBINPUT_SWITCH_STATE_OFF);
998 libinput_event_destroy(event);
999
1000 litest_touch_down(touchpad, 0, 50, 50);
1001 litest_touch_move_to(touchpad, 0, 50, 50, 70, 50, 10);
1002 litest_touch_up(touchpad, 0);
1003 litest_dispatch(li);
1004 litest_drain_events_of_type(li,
1005 LIBINPUT_EVENT_GESTURE_HOLD_BEGIN,
1006 LIBINPUT_EVENT_GESTURE_HOLD_END);
1007 litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION);
1008
1009 litest_device_destroy(touchpad);
1010 }
1011 END_TEST
1012
1013 START_TEST(tablet_mode_enable_touchpad_on_resume)
1014 {
1015 struct litest_device *sw = litest_current_device();
1016 struct litest_device *touchpad;
1017 struct libinput *li = sw->libinput;
1018 struct libinput_event *event;
1019
1020 if (!switch_has_tablet_mode(sw))
1021 return LITEST_NOT_APPLICABLE;
1022
1023 touchpad = switch_init_paired_touchpad(li);
1024 litest_disable_tap(touchpad->libinput_device);
1025 litest_drain_events(li);
1026
1027 litest_switch_action(sw, LIBINPUT_SWITCH_TABLET_MODE, LIBINPUT_SWITCH_STATE_ON);
1028 libinput_suspend(li);
1029 litest_drain_events(li);
1030
1031 litest_switch_action(sw,
1032 LIBINPUT_SWITCH_TABLET_MODE,
1033 LIBINPUT_SWITCH_STATE_OFF);
1034
1035 libinput_resume(li);
1036 litest_dispatch(li);
1037
1038 litest_assert_only_typed_events(li, LIBINPUT_EVENT_DEVICE_ADDED);
1039
1040 litest_touch_down(touchpad, 0, 50, 50);
1041 litest_touch_move_to(touchpad, 0, 50, 50, 70, 50, 10);
1042 litest_touch_up(touchpad, 0);
1043 litest_drain_events_of_type(li,
1044 LIBINPUT_EVENT_GESTURE_HOLD_BEGIN,
1045 LIBINPUT_EVENT_GESTURE_HOLD_END);
1046 litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION);
1047
1048 litest_switch_action(sw, LIBINPUT_SWITCH_TABLET_MODE, LIBINPUT_SWITCH_STATE_ON);
1049 litest_dispatch(li);
1050 event = libinput_get_event(li);
1051 litest_is_switch_event(event,
1052 LIBINPUT_SWITCH_TABLET_MODE,
1053 LIBINPUT_SWITCH_STATE_ON);
1054 libinput_event_destroy(event);
1055
1056 litest_touch_down(touchpad, 0, 50, 50);
1057 litest_touch_move_to(touchpad, 0, 50, 50, 70, 50, 10);
1058 litest_touch_up(touchpad, 0);
1059 litest_assert_empty_queue(li);
1060
1061 litest_device_destroy(touchpad);
1062 }
1063 END_TEST
1064
1065 START_TEST(tablet_mode_disable_keyboard)
1066 {
1067 struct litest_device *sw = litest_current_device();
1068 struct litest_device *keyboard;
1069 struct libinput *li = sw->libinput;
1070
1071 if (!switch_has_tablet_mode(sw))
1072 return LITEST_NOT_APPLICABLE;
1073
1074 keyboard = litest_add_device(li, LITEST_KEYBOARD);
1075 litest_drain_events(li);
1076
1077 litest_keyboard_key(keyboard, KEY_A, true);
1078 litest_keyboard_key(keyboard, KEY_A, false);
1079 litest_keyboard_key(keyboard, KEY_B, true); /* KEY_B down but not up */
1080 litest_dispatch(li);
1081
1082 litest_assert_key_event(li, KEY_A, LIBINPUT_KEY_STATE_PRESSED);
1083 litest_assert_key_event(li, KEY_A, LIBINPUT_KEY_STATE_RELEASED);
1084 litest_assert_key_event(li,
1085 KEY_B,
1086 LIBINPUT_KEY_STATE_PRESSED); /* KEY_B down but not up */
1087
1088 litest_switch_action(sw, LIBINPUT_SWITCH_TABLET_MODE, LIBINPUT_SWITCH_STATE_ON);
1089
1090 /* The key currently down must be released */
1091 litest_assert_key_event(li, KEY_B, LIBINPUT_KEY_STATE_RELEASED);
1092 litest_assert_switch_event(li,
1093 LIBINPUT_SWITCH_TABLET_MODE,
1094 LIBINPUT_SWITCH_STATE_ON);
1095 litest_assert_empty_queue(li);
1096
1097 litest_keyboard_key(keyboard, KEY_B, false); /* release the kernel device */
1098 litest_keyboard_key(keyboard, KEY_A, true);
1099 litest_keyboard_key(keyboard, KEY_A, false);
1100 litest_assert_empty_queue(li);
1101
1102 litest_switch_action(sw,
1103 LIBINPUT_SWITCH_TABLET_MODE,
1104 LIBINPUT_SWITCH_STATE_OFF);
1105 litest_assert_switch_event(li,
1106 LIBINPUT_SWITCH_TABLET_MODE,
1107 LIBINPUT_SWITCH_STATE_OFF);
1108
1109 litest_keyboard_key(keyboard, KEY_A, true);
1110 litest_keyboard_key(keyboard, KEY_A, false);
1111 litest_keyboard_key(keyboard, KEY_B, true);
1112 litest_keyboard_key(keyboard, KEY_B, false);
1113 litest_assert_key_event(li, KEY_A, LIBINPUT_KEY_STATE_PRESSED);
1114 litest_assert_key_event(li, KEY_A, LIBINPUT_KEY_STATE_RELEASED);
1115 litest_assert_key_event(li, KEY_B, LIBINPUT_KEY_STATE_PRESSED);
1116 litest_assert_key_event(li, KEY_B, LIBINPUT_KEY_STATE_RELEASED);
1117
1118 litest_device_destroy(keyboard);
1119 }
1120 END_TEST
1121
1122 START_TEST(tablet_mode_disable_keyboard_on_init)
1123 {
1124 struct litest_device *sw = litest_current_device();
1125 struct litest_device *keyboard;
1126 struct libinput *li = sw->libinput;
1127
1128 if (!switch_has_tablet_mode(sw))
1129 return LITEST_NOT_APPLICABLE;
1130
1131 litest_switch_action(sw, LIBINPUT_SWITCH_TABLET_MODE, LIBINPUT_SWITCH_STATE_ON);
1132 litest_drain_events(li);
1133
1134 /* keyboard comes with switch already on - no events */
1135 keyboard = litest_add_device(li, LITEST_KEYBOARD);
1136 litest_drain_events(li);
1137
1138 litest_keyboard_key(keyboard, KEY_A, true);
1139 litest_keyboard_key(keyboard, KEY_A, false);
1140 litest_assert_empty_queue(li);
1141
1142 litest_switch_action(sw,
1143 LIBINPUT_SWITCH_TABLET_MODE,
1144 LIBINPUT_SWITCH_STATE_OFF);
1145 litest_assert_only_typed_events(li, LIBINPUT_EVENT_SWITCH_TOGGLE);
1146
1147 litest_keyboard_key(keyboard, KEY_A, true);
1148 litest_keyboard_key(keyboard, KEY_A, false);
1149 litest_assert_only_typed_events(li, LIBINPUT_EVENT_KEYBOARD_KEY);
1150
1151 litest_device_destroy(keyboard);
1152 }
1153 END_TEST
1154
1155 START_TEST(tablet_mode_disable_keyboard_on_resume)
1156 {
1157 struct litest_device *sw = litest_current_device();
1158 struct litest_device *keyboard;
1159 struct libinput *li = sw->libinput;
1160 struct libinput_event *event;
1161 bool have_switch_toggle = false;
1162
1163 if (!switch_has_tablet_mode(sw))
1164 return LITEST_NOT_APPLICABLE;
1165
1166 keyboard = litest_add_device(li, LITEST_KEYBOARD);
1167 litest_drain_events(li);
1168 libinput_suspend(li);
1169
1170 /* We cannot grab this device because libinput doesn't have an open
1171 * fd to this device, we need an independent grab.
1172 */
1173 libevdev_grab(sw->evdev, LIBEVDEV_GRAB);
1174 litest_switch_action(sw, LIBINPUT_SWITCH_TABLET_MODE, LIBINPUT_SWITCH_STATE_ON);
1175 libevdev_grab(sw->evdev, LIBEVDEV_UNGRAB);
1176 litest_drain_events(li);
1177
1178 libinput_resume(li);
1179 litest_dispatch(li);
1180
1181 while ((event = libinput_get_event(li))) {
1182 enum libinput_event_type type;
1183
1184 type = libinput_event_get_type(event);
1185 switch (type) {
1186 case LIBINPUT_EVENT_DEVICE_ADDED:
1187 break;
1188 case LIBINPUT_EVENT_SWITCH_TOGGLE:
1189 litest_is_switch_event(event,
1190 LIBINPUT_SWITCH_TABLET_MODE,
1191 LIBINPUT_SWITCH_STATE_ON);
1192 have_switch_toggle = true;
1193 break;
1194 default:
1195 litest_abort_msg("Unexpected event type");
1196 }
1197 libinput_event_destroy(event);
1198 }
1199
1200 litest_assert(have_switch_toggle);
1201
1202 litest_keyboard_key(keyboard, KEY_A, true);
1203 litest_keyboard_key(keyboard, KEY_A, false);
1204 litest_assert_empty_queue(li);
1205
1206 litest_grab_device(sw);
1207 litest_switch_action(sw,
1208 LIBINPUT_SWITCH_TABLET_MODE,
1209 LIBINPUT_SWITCH_STATE_OFF);
1210 litest_ungrab_device(sw);
1211 litest_assert_only_typed_events(li, LIBINPUT_EVENT_SWITCH_TOGGLE);
1212
1213 litest_keyboard_key(keyboard, KEY_A, true);
1214 litest_keyboard_key(keyboard, KEY_A, false);
1215 litest_assert_only_typed_events(li, LIBINPUT_EVENT_KEYBOARD_KEY);
1216
1217 litest_device_destroy(keyboard);
1218 }
1219 END_TEST
1220
1221 START_TEST(tablet_mode_enable_keyboard_on_resume)
1222 {
1223 struct litest_device *sw = litest_current_device();
1224 struct litest_device *keyboard;
1225 struct libinput *li = sw->libinput;
1226
1227 if (!switch_has_tablet_mode(sw))
1228 return LITEST_NOT_APPLICABLE;
1229
1230 keyboard = litest_add_device(li, LITEST_KEYBOARD);
1231 litest_grab_device(sw);
1232 litest_switch_action(sw, LIBINPUT_SWITCH_TABLET_MODE, LIBINPUT_SWITCH_STATE_ON);
1233 litest_drain_events(li);
1234 litest_ungrab_device(sw);
1235 libinput_suspend(li);
1236 litest_drain_events(li);
1237
1238 litest_switch_action(sw,
1239 LIBINPUT_SWITCH_TABLET_MODE,
1240 LIBINPUT_SWITCH_STATE_OFF);
1241
1242 libinput_resume(li);
1243 litest_dispatch(li);
1244 litest_assert_only_typed_events(li, LIBINPUT_EVENT_DEVICE_ADDED);
1245
1246 litest_keyboard_key(keyboard, KEY_A, true);
1247 litest_keyboard_key(keyboard, KEY_A, false);
1248 litest_assert_only_typed_events(li, LIBINPUT_EVENT_KEYBOARD_KEY);
1249
1250 litest_switch_action(sw, LIBINPUT_SWITCH_TABLET_MODE, LIBINPUT_SWITCH_STATE_ON);
1251 litest_assert_only_typed_events(li, LIBINPUT_EVENT_SWITCH_TOGGLE);
1252
1253 litest_keyboard_key(keyboard, KEY_A, true);
1254 litest_keyboard_key(keyboard, KEY_A, false);
1255 litest_assert_empty_queue(li);
1256
1257 litest_device_destroy(keyboard);
1258 }
1259 END_TEST
1260
1261 START_TEST(tablet_mode_disable_trackpoint)
1262 {
1263 struct litest_device *sw = litest_current_device();
1264 struct litest_device *trackpoint;
1265 struct libinput *li = sw->libinput;
1266
1267 if (!switch_has_tablet_mode(sw))
1268 return LITEST_NOT_APPLICABLE;
1269
1270 trackpoint = litest_add_device(li, LITEST_TRACKPOINT);
1271 litest_drain_events(li);
1272
1273 litest_event(trackpoint, EV_REL, REL_Y, -1);
1274 litest_event(trackpoint, EV_SYN, SYN_REPORT, 0);
1275 litest_event(trackpoint, EV_REL, REL_Y, -1);
1276 litest_event(trackpoint, EV_SYN, SYN_REPORT, 0);
1277 litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION);
1278
1279 litest_grab_device(sw);
1280 litest_switch_action(sw, LIBINPUT_SWITCH_TABLET_MODE, LIBINPUT_SWITCH_STATE_ON);
1281 litest_drain_events(li);
1282
1283 litest_event(trackpoint, EV_REL, REL_Y, -1);
1284 litest_event(trackpoint, EV_SYN, SYN_REPORT, 0);
1285 litest_event(trackpoint, EV_REL, REL_Y, -1);
1286 litest_event(trackpoint, EV_SYN, SYN_REPORT, 0);
1287 litest_assert_empty_queue(li);
1288
1289 litest_switch_action(sw,
1290 LIBINPUT_SWITCH_TABLET_MODE,
1291 LIBINPUT_SWITCH_STATE_OFF);
1292 litest_assert_only_typed_events(li, LIBINPUT_EVENT_SWITCH_TOGGLE);
1293
1294 litest_event(trackpoint, EV_REL, REL_Y, -1);
1295 litest_event(trackpoint, EV_SYN, SYN_REPORT, 0);
1296 litest_event(trackpoint, EV_REL, REL_Y, -1);
1297 litest_event(trackpoint, EV_SYN, SYN_REPORT, 0);
1298 litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION);
1299 litest_ungrab_device(sw);
1300
1301 litest_device_destroy(trackpoint);
1302 }
1303 END_TEST
1304
1305 START_TEST(tablet_mode_disable_trackpoint_on_init)
1306 {
1307 struct litest_device *sw = litest_current_device();
1308 struct litest_device *trackpoint;
1309 struct libinput *li = sw->libinput;
1310
1311 if (!switch_has_tablet_mode(sw))
1312 return LITEST_NOT_APPLICABLE;
1313
1314 litest_grab_device(sw);
1315 litest_switch_action(sw, LIBINPUT_SWITCH_TABLET_MODE, LIBINPUT_SWITCH_STATE_ON);
1316 litest_drain_events(li);
1317
1318 /* trackpoint comes with switch already on - no events */
1319 trackpoint = litest_add_device(li, LITEST_TRACKPOINT);
1320 litest_drain_events(li);
1321
1322 litest_event(trackpoint, EV_REL, REL_Y, -1);
1323 litest_event(trackpoint, EV_SYN, SYN_REPORT, 0);
1324 litest_event(trackpoint, EV_REL, REL_Y, -1);
1325 litest_event(trackpoint, EV_SYN, SYN_REPORT, 0);
1326 litest_assert_empty_queue(li);
1327
1328 litest_switch_action(sw,
1329 LIBINPUT_SWITCH_TABLET_MODE,
1330 LIBINPUT_SWITCH_STATE_OFF);
1331 litest_assert_only_typed_events(li, LIBINPUT_EVENT_SWITCH_TOGGLE);
1332
1333 litest_event(trackpoint, EV_REL, REL_Y, -1);
1334 litest_event(trackpoint, EV_SYN, SYN_REPORT, 0);
1335 litest_event(trackpoint, EV_REL, REL_Y, -1);
1336 litest_event(trackpoint, EV_SYN, SYN_REPORT, 0);
1337 litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION);
1338 litest_ungrab_device(sw);
1339
1340 litest_device_destroy(trackpoint);
1341 }
1342 END_TEST
1343
1344 START_TEST(dock_toggle)
1345 {
1346 struct litest_device *sw = litest_current_device();
1347 struct libinput *li = sw->libinput;
1348
1349 if (!libevdev_has_event_code(sw->evdev, EV_SW, SW_DOCK))
1350 return LITEST_NOT_APPLICABLE;
1351
1352 litest_drain_events(li);
1353
1354 litest_grab_device(sw);
1355 litest_event(sw, EV_SW, SW_DOCK, 1);
1356 litest_dispatch(li);
1357
1358 litest_event(sw, EV_SW, SW_DOCK, 0);
1359 litest_dispatch(li);
1360 litest_ungrab_device(sw);
1361
1362 litest_assert_empty_queue(li);
1363 }
1364 END_TEST
1365
1366 TEST_COLLECTION(switch)
1367 {
1368 /* clang-format off */
1369 litest_add(switch_has_cap, LITEST_SWITCH, LITEST_ANY);
1370 litest_add(switch_has_lid_switch, LITEST_SWITCH, LITEST_ANY);
1371 litest_add(switch_has_tablet_mode_switch, LITEST_SWITCH, LITEST_ANY);
1372 litest_add(switch_has_keypad_slide_switch, LITEST_SWITCH, LITEST_ANY);
1373 litest_add(switch_not_down_on_init, LITEST_SWITCH, LITEST_ANY);
1374
1375 litest_with_parameters(params, "switch", 'I', 3, litest_named_i32(LIBINPUT_SWITCH_LID, "lid"),
1376 litest_named_i32(LIBINPUT_SWITCH_TABLET_MODE, "tablet_mode"),
1377 litest_named_i32(LIBINPUT_SWITCH_KEYPAD_SLIDE, "keypad_slide")) {
1378 litest_add_parametrized(switch_toggle, LITEST_SWITCH, LITEST_ANY, params);
1379 litest_add_parametrized(switch_toggle_double, LITEST_SWITCH, LITEST_ANY, params);
1380 litest_add_parametrized(switch_down_on_init, LITEST_SWITCH, LITEST_ANY, params);
1381 litest_add_parametrized(switch_not_down_on_init, LITEST_SWITCH, LITEST_ANY, params);
1382 }
1383
1384 litest_with_parameters(params, "switch", 'I', 2, litest_named_i32(LIBINPUT_SWITCH_LID, "lid"),
1385 litest_named_i32(LIBINPUT_SWITCH_TABLET_MODE, "tablet_mode")) {
1386 litest_add_parametrized(switch_disable_touchpad, LITEST_SWITCH, LITEST_ANY, params);
1387 litest_add_parametrized(switch_disable_touchpad_during_touch, LITEST_SWITCH, LITEST_ANY, params);
1388 litest_add_parametrized(switch_disable_touchpad_edge_scroll, LITEST_SWITCH, LITEST_ANY, params);
1389 litest_add_parametrized(switch_disable_touchpad_edge_scroll_interrupt, LITEST_SWITCH, LITEST_ANY, params);
1390 litest_add_parametrized(switch_disable_touchpad_already_open, LITEST_SWITCH, LITEST_ANY, params);
1391 litest_add_parametrized(switch_dont_resume_disabled_touchpad, LITEST_SWITCH, LITEST_ANY, params);
1392 litest_add_parametrized(switch_dont_resume_disabled_touchpad_external_mouse, LITEST_SWITCH, LITEST_ANY, params);
1393
1394 litest_add_parametrized_no_device(switch_suspend_with_keyboard, params);
1395 litest_add_parametrized_no_device(switch_suspend_with_touchpad, params);
1396 }
1397
1398 litest_add(lid_open_on_key, LITEST_SWITCH, LITEST_ANY);
1399 litest_add(lid_open_on_key_touchpad_enabled, LITEST_SWITCH, LITEST_ANY);
1400 litest_add_for_device(lid_update_hw_on_key, LITEST_LID_SWITCH_SURFACE3);
1401 litest_add_for_device(lid_update_hw_on_key_closed_on_init, LITEST_LID_SWITCH_SURFACE3);
1402 litest_add_for_device(lid_update_hw_on_key_multiple_keyboards, LITEST_LID_SWITCH_SURFACE3);
1403 litest_add_for_device(lid_key_press, LITEST_GPIO_KEYS);
1404
1405 litest_add(tablet_mode_disable_touchpad_on_init, LITEST_SWITCH, LITEST_ANY);
1406 litest_add(tablet_mode_disable_touchpad_on_resume, LITEST_SWITCH, LITEST_ANY);
1407 litest_add(tablet_mode_enable_touchpad_on_resume, LITEST_SWITCH, LITEST_ANY);
1408 litest_add(tablet_mode_disable_keyboard, LITEST_SWITCH, LITEST_ANY);
1409 litest_add(tablet_mode_disable_keyboard_on_init, LITEST_SWITCH, LITEST_ANY);
1410 litest_add(tablet_mode_disable_keyboard_on_resume, LITEST_SWITCH, LITEST_ANY);
1411 litest_add(tablet_mode_enable_keyboard_on_resume, LITEST_SWITCH, LITEST_ANY);
1412 litest_add(tablet_mode_disable_trackpoint, LITEST_SWITCH, LITEST_ANY);
1413 litest_add(tablet_mode_disable_trackpoint_on_init, LITEST_SWITCH, LITEST_ANY);
1414
1415 litest_add(dock_toggle, LITEST_SWITCH, LITEST_ANY);
1416 /* clang-format on */
1417 }