1 /*
2 * Copyright © 2010 Intel Corporation
3 * Copyright © 2013 Jonas Ådahl
4 * Copyright © 2013-2017 Red Hat, Inc.
5 * Copyright © 2017 James Ye <jye836@gmail.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 */
26
27 #include "config.h"
28
29 #include <mtdev-plumbing.h>
30
31 #include "evdev-fallback.h"
32 #include "util-input-event.h"
33
34 static void
fallback_keyboard_notify_key(struct fallback_dispatch * dispatch,struct evdev_device * device,uint64_t time,int key,enum libinput_key_state state)35 fallback_keyboard_notify_key(struct fallback_dispatch *dispatch,
36 struct evdev_device *device,
37 uint64_t time,
38 int key,
39 enum libinput_key_state state)
40 {
41 int down_count;
42
43 down_count = evdev_update_key_down_count(device, key, state);
44
45 if ((state == LIBINPUT_KEY_STATE_PRESSED && down_count == 1) ||
46 (state == LIBINPUT_KEY_STATE_RELEASED && down_count == 0))
47 keyboard_notify_key(&device->base, time, key, state);
48 }
49
50 static void
fallback_lid_notify_toggle(struct fallback_dispatch * dispatch,struct evdev_device * device,uint64_t time)51 fallback_lid_notify_toggle(struct fallback_dispatch *dispatch,
52 struct evdev_device *device,
53 uint64_t time)
54 {
55 if (dispatch->lid.is_closed ^ dispatch->lid.is_closed_client_state) {
56 switch_notify_toggle(&device->base,
57 time,
58 LIBINPUT_SWITCH_LID,
59 dispatch->lid.is_closed);
60 dispatch->lid.is_closed_client_state = dispatch->lid.is_closed;
61 }
62 }
63
64 static enum libinput_switch_state
fallback_interface_get_switch_state(struct evdev_dispatch * evdev_dispatch,enum libinput_switch sw)65 fallback_interface_get_switch_state(struct evdev_dispatch *evdev_dispatch,
66 enum libinput_switch sw)
67 {
68 struct fallback_dispatch *dispatch = fallback_dispatch(evdev_dispatch);
69
70 switch (sw) {
71 case LIBINPUT_SWITCH_TABLET_MODE:
72 break;
73 default:
74 /* Internal function only, so we can abort here */
75 abort();
76 }
77
78 return dispatch->tablet_mode.sw.state ?
79 LIBINPUT_SWITCH_STATE_ON :
80 LIBINPUT_SWITCH_STATE_OFF;
81 }
82
83 static inline void
normalize_delta(struct evdev_device * device,const struct device_coords * delta,struct normalized_coords * normalized)84 normalize_delta(struct evdev_device *device,
85 const struct device_coords *delta,
86 struct normalized_coords *normalized)
87 {
88 normalized->x = delta->x * DEFAULT_MOUSE_DPI / (double)device->dpi;
89 normalized->y = delta->y * DEFAULT_MOUSE_DPI / (double)device->dpi;
90 }
91
92 static inline bool
post_trackpoint_scroll(struct evdev_device * device,struct normalized_coords unaccel,uint64_t time)93 post_trackpoint_scroll(struct evdev_device *device,
94 struct normalized_coords unaccel,
95 uint64_t time)
96 {
97 if (device->scroll.method != LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN)
98 return false;
99
100 switch(device->scroll.button_scroll_state) {
101 case BUTTONSCROLL_IDLE:
102 return false;
103 case BUTTONSCROLL_BUTTON_DOWN:
104 /* if the button is down but scroll is not active, we're within the
105 timeout where we swallow motion events but don't post
106 scroll buttons */
107 evdev_log_debug(device, "btnscroll: discarding\n");
108 return true;
109 case BUTTONSCROLL_READY:
110 device->scroll.button_scroll_state = BUTTONSCROLL_SCROLLING;
111 /* fallthrough */
112 case BUTTONSCROLL_SCROLLING:
113 evdev_post_scroll(device, time,
114 LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS,
115 &unaccel);
116 return true;
117 }
118
119 assert(!"invalid scroll button state");
120 }
121
122 static inline bool
fallback_filter_defuzz_touch(struct fallback_dispatch * dispatch,struct evdev_device * device,struct mt_slot * slot)123 fallback_filter_defuzz_touch(struct fallback_dispatch *dispatch,
124 struct evdev_device *device,
125 struct mt_slot *slot)
126 {
127 struct device_coords point;
128
129 if (!dispatch->mt.want_hysteresis)
130 return false;
131
132 point = evdev_hysteresis(&slot->point,
133 &slot->hysteresis_center,
134 &dispatch->mt.hysteresis_margin);
135 slot->point = point;
136
137 if (point.x == slot->hysteresis_center.x &&
138 point.y == slot->hysteresis_center.y)
139 return true;
140
141 slot->hysteresis_center = point;
142
143 return false;
144 }
145
146 static inline void
fallback_rotate_relative(struct fallback_dispatch * dispatch,struct evdev_device * device)147 fallback_rotate_relative(struct fallback_dispatch *dispatch,
148 struct evdev_device *device)
149 {
150 struct device_coords rel = dispatch->rel;
151
152 if (!device->base.config.rotation)
153 return;
154
155 /* loss of precision for non-90 degrees, but we only support 90 deg
156 * right now anyway */
157 matrix_mult_vec(&dispatch->rotation.matrix, &rel.x, &rel.y);
158
159 dispatch->rel = rel;
160 }
161
162 static void
fallback_flush_relative_motion(struct fallback_dispatch * dispatch,struct evdev_device * device,uint64_t time)163 fallback_flush_relative_motion(struct fallback_dispatch *dispatch,
164 struct evdev_device *device,
165 uint64_t time)
166 {
167 struct libinput_device *base = &device->base;
168 struct normalized_coords accel, unaccel;
169 struct device_float_coords raw;
170
171 if (!(device->seat_caps & EVDEV_DEVICE_POINTER))
172 return;
173
174 fallback_rotate_relative(dispatch, device);
175
176 normalize_delta(device, &dispatch->rel, &unaccel);
177 raw.x = dispatch->rel.x;
178 raw.y = dispatch->rel.y;
179 dispatch->rel.x = 0;
180 dispatch->rel.y = 0;
181
182 /* Use unaccelerated deltas for pointing stick scroll */
183 if (post_trackpoint_scroll(device, unaccel, time))
184 return;
185
186 if (device->pointer.filter) {
187 /* Apply pointer acceleration. */
188 accel = filter_dispatch(device->pointer.filter,
189 &raw,
190 device,
191 time);
192 } else {
193 evdev_log_bug_libinput(device,
194 "accel filter missing\n");
195 accel = unaccel;
196 }
197
198 if (normalized_is_zero(accel) && normalized_is_zero(unaccel))
199 return;
200
201 pointer_notify_motion(base, time, &accel, &raw);
202 }
203
204 static void
fallback_flush_wheels(struct fallback_dispatch * dispatch,struct evdev_device * device,uint64_t time)205 fallback_flush_wheels(struct fallback_dispatch *dispatch,
206 struct evdev_device *device,
207 uint64_t time)
208 {
209 struct normalized_coords wheel_degrees = { 0.0, 0.0 };
210 struct discrete_coords discrete = { 0.0, 0.0 };
211
212 if (!(device->seat_caps & EVDEV_DEVICE_POINTER))
213 return;
214
215 if (device->model_flags & EVDEV_MODEL_LENOVO_SCROLLPOINT) {
216 struct normalized_coords unaccel = { 0.0, 0.0 };
217
218 dispatch->wheel.y *= -1;
219 normalize_delta(device, &dispatch->wheel, &unaccel);
220 evdev_post_scroll(device,
221 time,
222 LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS,
223 &unaccel);
224 dispatch->wheel.x = 0;
225 dispatch->wheel.y = 0;
226
227 return;
228 }
229
230 if (dispatch->wheel.y != 0) {
231 wheel_degrees.y = -1 * dispatch->wheel.y *
232 device->scroll.wheel_click_angle.y;
233 discrete.y = -1 * dispatch->wheel.y;
234
235 evdev_notify_axis(
236 device,
237 time,
238 bit(LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL),
239 LIBINPUT_POINTER_AXIS_SOURCE_WHEEL,
240 &wheel_degrees,
241 &discrete);
242 dispatch->wheel.y = 0;
243 }
244
245 if (dispatch->wheel.x != 0) {
246 wheel_degrees.x = dispatch->wheel.x *
247 device->scroll.wheel_click_angle.x;
248 discrete.x = dispatch->wheel.x;
249
250 evdev_notify_axis(
251 device,
252 time,
253 bit(LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL),
254 LIBINPUT_POINTER_AXIS_SOURCE_WHEEL,
255 &wheel_degrees,
256 &discrete);
257 dispatch->wheel.x = 0;
258 }
259 }
260
261 static void
fallback_flush_absolute_motion(struct fallback_dispatch * dispatch,struct evdev_device * device,uint64_t time)262 fallback_flush_absolute_motion(struct fallback_dispatch *dispatch,
263 struct evdev_device *device,
264 uint64_t time)
265 {
266 struct libinput_device *base = &device->base;
267 struct device_coords point;
268
269 if (!(device->seat_caps & EVDEV_DEVICE_POINTER))
270 return;
271
272 point = dispatch->abs.point;
273 evdev_transform_absolute(device, &point);
274
275 pointer_notify_motion_absolute(base, time, &point);
276 }
277
278 static bool
fallback_flush_mt_down(struct fallback_dispatch * dispatch,struct evdev_device * device,int slot_idx,uint64_t time)279 fallback_flush_mt_down(struct fallback_dispatch *dispatch,
280 struct evdev_device *device,
281 int slot_idx,
282 uint64_t time)
283 {
284 struct libinput_device *base = &device->base;
285 struct libinput_seat *seat = base->seat;
286 struct device_coords point;
287 struct mt_slot *slot;
288 int seat_slot;
289
290 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
291 return false;
292
293 slot = &dispatch->mt.slots[slot_idx];
294 if (slot->seat_slot != -1) {
295 evdev_log_bug_kernel(device,
296 "driver sent multiple touch down for the same slot");
297 return false;
298 }
299
300 seat_slot = ffs(~seat->slot_map) - 1;
301 slot->seat_slot = seat_slot;
302
303 if (seat_slot == -1)
304 return false;
305
306 seat->slot_map |= bit(seat_slot);
307 point = slot->point;
308 slot->hysteresis_center = point;
309 evdev_transform_absolute(device, &point);
310
311 touch_notify_touch_down(base, time, slot_idx, seat_slot,
312 &point);
313
314 return true;
315 }
316
317 static bool
fallback_flush_mt_motion(struct fallback_dispatch * dispatch,struct evdev_device * device,int slot_idx,uint64_t time)318 fallback_flush_mt_motion(struct fallback_dispatch *dispatch,
319 struct evdev_device *device,
320 int slot_idx,
321 uint64_t time)
322 {
323 struct libinput_device *base = &device->base;
324 struct device_coords point;
325 struct mt_slot *slot;
326 int seat_slot;
327
328 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
329 return false;
330
331 slot = &dispatch->mt.slots[slot_idx];
332 seat_slot = slot->seat_slot;
333 point = slot->point;
334
335 if (seat_slot == -1)
336 return false;
337
338 if (fallback_filter_defuzz_touch(dispatch, device, slot))
339 return false;
340
341 evdev_transform_absolute(device, &point);
342 touch_notify_touch_motion(base, time, slot_idx, seat_slot,
343 &point);
344
345 return true;
346 }
347
348 static bool
fallback_flush_mt_up(struct fallback_dispatch * dispatch,struct evdev_device * device,int slot_idx,uint64_t time)349 fallback_flush_mt_up(struct fallback_dispatch *dispatch,
350 struct evdev_device *device,
351 int slot_idx,
352 uint64_t time)
353 {
354 struct libinput_device *base = &device->base;
355 struct libinput_seat *seat = base->seat;
356 struct mt_slot *slot;
357 int seat_slot;
358
359 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
360 return false;
361
362 slot = &dispatch->mt.slots[slot_idx];
363 seat_slot = slot->seat_slot;
364 slot->seat_slot = -1;
365
366 if (seat_slot == -1)
367 return false;
368
369 seat->slot_map &= ~bit(seat_slot);
370
371 touch_notify_touch_up(base, time, slot_idx, seat_slot);
372
373 return true;
374 }
375
376 static bool
fallback_flush_mt_cancel(struct fallback_dispatch * dispatch,struct evdev_device * device,int slot_idx,uint64_t time)377 fallback_flush_mt_cancel(struct fallback_dispatch *dispatch,
378 struct evdev_device *device,
379 int slot_idx,
380 uint64_t time)
381 {
382 struct libinput_device *base = &device->base;
383 struct libinput_seat *seat = base->seat;
384 struct mt_slot *slot;
385 int seat_slot;
386
387 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
388 return false;
389
390 slot = &dispatch->mt.slots[slot_idx];
391 seat_slot = slot->seat_slot;
392 slot->seat_slot = -1;
393
394 if (seat_slot == -1)
395 return false;
396
397 seat->slot_map &= ~bit(seat_slot);
398
399 touch_notify_touch_cancel(base, time, slot_idx, seat_slot);
400
401 return true;
402 }
403
404 static bool
fallback_flush_st_down(struct fallback_dispatch * dispatch,struct evdev_device * device,uint64_t time)405 fallback_flush_st_down(struct fallback_dispatch *dispatch,
406 struct evdev_device *device,
407 uint64_t time)
408 {
409 struct libinput_device *base = &device->base;
410 struct libinput_seat *seat = base->seat;
411 struct device_coords point;
412 int seat_slot;
413
414 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
415 return false;
416
417 if (dispatch->abs.seat_slot != -1) {
418 evdev_log_bug_kernel(device,
419 "driver sent multiple touch down for the same slot");
420 return false;
421 }
422
423 seat_slot = ffs(~seat->slot_map) - 1;
424 dispatch->abs.seat_slot = seat_slot;
425
426 if (seat_slot == -1)
427 return false;
428
429 seat->slot_map |= bit(seat_slot);
430
431 point = dispatch->abs.point;
432 evdev_transform_absolute(device, &point);
433
434 touch_notify_touch_down(base, time, -1, seat_slot, &point);
435
436 return true;
437 }
438
439 static bool
fallback_flush_st_motion(struct fallback_dispatch * dispatch,struct evdev_device * device,uint64_t time)440 fallback_flush_st_motion(struct fallback_dispatch *dispatch,
441 struct evdev_device *device,
442 uint64_t time)
443 {
444 struct libinput_device *base = &device->base;
445 struct device_coords point;
446 int seat_slot;
447
448 point = dispatch->abs.point;
449 evdev_transform_absolute(device, &point);
450
451 seat_slot = dispatch->abs.seat_slot;
452
453 if (seat_slot == -1)
454 return false;
455
456 touch_notify_touch_motion(base, time, -1, seat_slot, &point);
457
458 return true;
459 }
460
461 static bool
fallback_flush_st_up(struct fallback_dispatch * dispatch,struct evdev_device * device,uint64_t time)462 fallback_flush_st_up(struct fallback_dispatch *dispatch,
463 struct evdev_device *device,
464 uint64_t time)
465 {
466 struct libinput_device *base = &device->base;
467 struct libinput_seat *seat = base->seat;
468 int seat_slot;
469
470 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
471 return false;
472
473 seat_slot = dispatch->abs.seat_slot;
474 dispatch->abs.seat_slot = -1;
475
476 if (seat_slot == -1)
477 return false;
478
479 seat->slot_map &= ~bit(seat_slot);
480
481 touch_notify_touch_up(base, time, -1, seat_slot);
482
483 return true;
484 }
485
486 static bool
fallback_flush_st_cancel(struct fallback_dispatch * dispatch,struct evdev_device * device,uint64_t time)487 fallback_flush_st_cancel(struct fallback_dispatch *dispatch,
488 struct evdev_device *device,
489 uint64_t time)
490 {
491 struct libinput_device *base = &device->base;
492 struct libinput_seat *seat = base->seat;
493 int seat_slot;
494
495 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
496 return false;
497
498 seat_slot = dispatch->abs.seat_slot;
499 dispatch->abs.seat_slot = -1;
500
501 if (seat_slot == -1)
502 return false;
503
504 seat->slot_map &= ~bit(seat_slot);
505
506 touch_notify_touch_cancel(base, time, -1, seat_slot);
507
508 return true;
509 }
510
511 static void
fallback_process_touch_button(struct fallback_dispatch * dispatch,struct evdev_device * device,uint64_t time,int value)512 fallback_process_touch_button(struct fallback_dispatch *dispatch,
513 struct evdev_device *device,
514 uint64_t time, int value)
515 {
516 dispatch->pending_event |= (value) ?
517 EVDEV_ABSOLUTE_TOUCH_DOWN :
518 EVDEV_ABSOLUTE_TOUCH_UP;
519 }
520
521 static inline void
fallback_process_key(struct fallback_dispatch * dispatch,struct evdev_device * device,struct input_event * e,uint64_t time)522 fallback_process_key(struct fallback_dispatch *dispatch,
523 struct evdev_device *device,
524 struct input_event *e, uint64_t time)
525 {
526 enum key_type type;
527
528 /* ignore kernel key repeat */
529 if (e->value == 2)
530 return;
531
532 if (e->code == BTN_TOUCH) {
533 if (!device->is_mt)
534 fallback_process_touch_button(dispatch,
535 device,
536 time,
537 e->value);
538 return;
539 }
540
541 type = get_key_type(e->code);
542
543 /* Ignore key release events from the kernel for keys that libinput
544 * never got a pressed event for or key presses for keys that we
545 * think are still down */
546 switch (type) {
547 case KEY_TYPE_NONE:
548 break;
549 case KEY_TYPE_KEY:
550 case KEY_TYPE_BUTTON:
551 if ((e->value && hw_is_key_down(dispatch, e->code)) ||
552 (e->value == 0 && !hw_is_key_down(dispatch, e->code)))
553 return;
554
555 dispatch->pending_event |= EVDEV_KEY;
556 break;
557 }
558
559 hw_set_key_down(dispatch, e->code, e->value);
560
561 switch (type) {
562 case KEY_TYPE_NONE:
563 break;
564 case KEY_TYPE_KEY:
565 fallback_keyboard_notify_key(
566 dispatch,
567 device,
568 time,
569 e->code,
570 e->value ? LIBINPUT_KEY_STATE_PRESSED :
571 LIBINPUT_KEY_STATE_RELEASED);
572 break;
573 case KEY_TYPE_BUTTON:
574 break;
575 }
576 }
577
578 static void
fallback_process_touch(struct fallback_dispatch * dispatch,struct evdev_device * device,struct input_event * e,uint64_t time)579 fallback_process_touch(struct fallback_dispatch *dispatch,
580 struct evdev_device *device,
581 struct input_event *e,
582 uint64_t time)
583 {
584 struct mt_slot *slot = &dispatch->mt.slots[dispatch->mt.slot];
585
586 if (e->code == ABS_MT_SLOT) {
587 if ((size_t)e->value >= dispatch->mt.slots_len) {
588 evdev_log_bug_libinput(device,
589 "exceeded slot count (%d of max %zd)\n",
590 e->value,
591 dispatch->mt.slots_len);
592 e->value = dispatch->mt.slots_len - 1;
593 }
594 dispatch->mt.slot = e->value;
595 return;
596 }
597
598 switch (e->code) {
599 case ABS_MT_TRACKING_ID:
600 if (e->value >= 0) {
601 dispatch->pending_event |= EVDEV_ABSOLUTE_MT;
602 slot->state = SLOT_STATE_BEGIN;
603 if (dispatch->mt.has_palm) {
604 int v;
605 v = libevdev_get_slot_value(device->evdev,
606 dispatch->mt.slot,
607 ABS_MT_TOOL_TYPE);
608 switch (v) {
609 case MT_TOOL_PALM:
610 /* new touch, no cancel needed */
611 slot->palm_state = PALM_WAS_PALM;
612 break;
613 default:
614 slot->palm_state = PALM_NONE;
615 break;
616 }
617 } else {
618 slot->palm_state = PALM_NONE;
619 }
620 } else {
621 dispatch->pending_event |= EVDEV_ABSOLUTE_MT;
622 slot->state = SLOT_STATE_END;
623 }
624 slot->dirty = true;
625 break;
626 case ABS_MT_POSITION_X:
627 evdev_device_check_abs_axis_range(device, e->code, e->value);
628 dispatch->mt.slots[dispatch->mt.slot].point.x = e->value;
629 dispatch->pending_event |= EVDEV_ABSOLUTE_MT;
630 slot->dirty = true;
631 break;
632 case ABS_MT_POSITION_Y:
633 evdev_device_check_abs_axis_range(device, e->code, e->value);
634 dispatch->mt.slots[dispatch->mt.slot].point.y = e->value;
635 dispatch->pending_event |= EVDEV_ABSOLUTE_MT;
636 slot->dirty = true;
637 break;
638 case ABS_MT_TOOL_TYPE:
639 /* The transitions matter - we (may) need to send a touch
640 * cancel event if we just switched to a palm touch. And the
641 * kernel may switch back to finger but we keep the touch as
642 * palm - but then we need to reset correctly on a new touch
643 * sequence.
644 */
645 switch (e->value) {
646 case MT_TOOL_PALM:
647 if (slot->palm_state == PALM_NONE)
648 slot->palm_state = PALM_NEW;
649 break;
650 default:
651 if (slot->palm_state == PALM_IS_PALM)
652 slot->palm_state = PALM_WAS_PALM;
653 break;
654 }
655 dispatch->pending_event |= EVDEV_ABSOLUTE_MT;
656 slot->dirty = true;
657 break;
658 }
659 }
660
661 static inline void
fallback_process_absolute_motion(struct fallback_dispatch * dispatch,struct evdev_device * device,struct input_event * e)662 fallback_process_absolute_motion(struct fallback_dispatch *dispatch,
663 struct evdev_device *device,
664 struct input_event *e)
665 {
666 switch (e->code) {
667 case ABS_X:
668 evdev_device_check_abs_axis_range(device, e->code, e->value);
669 dispatch->abs.point.x = e->value;
670 dispatch->pending_event |= EVDEV_ABSOLUTE_MOTION;
671 break;
672 case ABS_Y:
673 evdev_device_check_abs_axis_range(device, e->code, e->value);
674 dispatch->abs.point.y = e->value;
675 dispatch->pending_event |= EVDEV_ABSOLUTE_MOTION;
676 break;
677 }
678 }
679
680 static void
fallback_lid_keyboard_event(uint64_t time,struct libinput_event * event,void * data)681 fallback_lid_keyboard_event(uint64_t time,
682 struct libinput_event *event,
683 void *data)
684 {
685 struct fallback_dispatch *dispatch = fallback_dispatch(data);
686
687 if (!dispatch->lid.is_closed)
688 return;
689
690 if (event->type != LIBINPUT_EVENT_KEYBOARD_KEY)
691 return;
692
693 if (dispatch->lid.reliability == RELIABILITY_WRITE_OPEN) {
694 int fd = libevdev_get_fd(dispatch->device->evdev);
695 int rc;
696 struct input_event ev[2];
697
698 ev[0] = input_event_init(0, EV_SW, SW_LID, 0);
699 ev[1] = input_event_init(0, EV_SYN, SYN_REPORT, 0);
700
701 rc = write(fd, ev, sizeof(ev));
702
703 if (rc < 0)
704 evdev_log_error(dispatch->device,
705 "failed to write SW_LID state (%s)",
706 strerror(errno));
707
708 /* In case write() fails, we sync the lid state manually
709 * regardless. */
710 }
711
712 /* Posting the event here means we preempt the keyboard events that
713 * caused us to wake up, so the lid event is always passed on before
714 * the key event.
715 */
716 dispatch->lid.is_closed = false;
717 fallback_lid_notify_toggle(dispatch, dispatch->device, time);
718 }
719
720 static void
fallback_lid_toggle_keyboard_listener(struct fallback_dispatch * dispatch,struct evdev_paired_keyboard * kbd,bool is_closed)721 fallback_lid_toggle_keyboard_listener(struct fallback_dispatch *dispatch,
722 struct evdev_paired_keyboard *kbd,
723 bool is_closed)
724 {
725 assert(kbd->device);
726
727 libinput_device_remove_event_listener(&kbd->listener);
728
729 if (is_closed) {
730 libinput_device_add_event_listener(
731 &kbd->device->base,
732 &kbd->listener,
733 fallback_lid_keyboard_event,
734 dispatch);
735 } else {
736 libinput_device_init_event_listener(&kbd->listener);
737 }
738 }
739
740 static void
fallback_lid_toggle_keyboard_listeners(struct fallback_dispatch * dispatch,bool is_closed)741 fallback_lid_toggle_keyboard_listeners(struct fallback_dispatch *dispatch,
742 bool is_closed)
743 {
744 struct evdev_paired_keyboard *kbd;
745
746 list_for_each(kbd, &dispatch->lid.paired_keyboard_list, link) {
747 if (!kbd->device)
748 continue;
749
750 fallback_lid_toggle_keyboard_listener(dispatch,
751 kbd,
752 is_closed);
753 }
754 }
755
756 static inline void
fallback_process_switch(struct fallback_dispatch * dispatch,struct evdev_device * device,struct input_event * e,uint64_t time)757 fallback_process_switch(struct fallback_dispatch *dispatch,
758 struct evdev_device *device,
759 struct input_event *e,
760 uint64_t time)
761 {
762 enum libinput_switch_state state;
763 bool is_closed;
764
765 /* TODO: this should to move to handle_state */
766
767 switch (e->code) {
768 case SW_LID:
769 is_closed = !!e->value;
770
771 fallback_lid_toggle_keyboard_listeners(dispatch, is_closed);
772
773 if (dispatch->lid.is_closed == is_closed)
774 return;
775
776 dispatch->lid.is_closed = is_closed;
777 fallback_lid_notify_toggle(dispatch, device, time);
778 break;
779 case SW_TABLET_MODE:
780 if (dispatch->tablet_mode.sw.state == e->value)
781 return;
782
783 dispatch->tablet_mode.sw.state = e->value;
784 if (e->value)
785 state = LIBINPUT_SWITCH_STATE_ON;
786 else
787 state = LIBINPUT_SWITCH_STATE_OFF;
788 switch_notify_toggle(&device->base,
789 time,
790 LIBINPUT_SWITCH_TABLET_MODE,
791 state);
792 break;
793 }
794 }
795
796 static inline bool
fallback_reject_relative(struct evdev_device * device,const struct input_event * e,uint64_t time)797 fallback_reject_relative(struct evdev_device *device,
798 const struct input_event *e,
799 uint64_t time)
800 {
801 if ((e->code == REL_X || e->code == REL_Y) &&
802 (device->seat_caps & EVDEV_DEVICE_POINTER) == 0) {
803 evdev_log_bug_libinput_ratelimit(device,
804 &device->nonpointer_rel_limit,
805 "REL_X/Y from a non-pointer device\n");
806 return true;
807 }
808
809 return false;
810 }
811
812 static inline void
fallback_process_relative(struct fallback_dispatch * dispatch,struct evdev_device * device,struct input_event * e,uint64_t time)813 fallback_process_relative(struct fallback_dispatch *dispatch,
814 struct evdev_device *device,
815 struct input_event *e, uint64_t time)
816 {
817 if (fallback_reject_relative(device, e, time))
818 return;
819
820 switch (e->code) {
821 case REL_X:
822 dispatch->rel.x += e->value;
823 dispatch->pending_event |= EVDEV_RELATIVE_MOTION;
824 break;
825 case REL_Y:
826 dispatch->rel.y += e->value;
827 dispatch->pending_event |= EVDEV_RELATIVE_MOTION;
828 break;
829 case REL_WHEEL:
830 dispatch->wheel.y += e->value;
831 dispatch->pending_event |= EVDEV_WHEEL;
832 break;
833 case REL_HWHEEL:
834 dispatch->wheel.x += e->value;
835 dispatch->pending_event |= EVDEV_WHEEL;
836 break;
837 }
838 }
839
840 static inline void
fallback_process_absolute(struct fallback_dispatch * dispatch,struct evdev_device * device,struct input_event * e,uint64_t time)841 fallback_process_absolute(struct fallback_dispatch *dispatch,
842 struct evdev_device *device,
843 struct input_event *e,
844 uint64_t time)
845 {
846 if (device->is_mt) {
847 fallback_process_touch(dispatch, device, e, time);
848 } else {
849 fallback_process_absolute_motion(dispatch, device, e);
850 }
851 }
852
853 static inline bool
fallback_any_button_down(struct fallback_dispatch * dispatch,struct evdev_device * device)854 fallback_any_button_down(struct fallback_dispatch *dispatch,
855 struct evdev_device *device)
856 {
857 unsigned int button;
858
859 for (button = BTN_LEFT; button < BTN_JOYSTICK; button++) {
860 if (libevdev_has_event_code(device->evdev, EV_KEY, button) &&
861 hw_is_key_down(dispatch, button))
862 return true;
863 }
864 return false;
865 }
866
867 static inline bool
fallback_arbitrate_touch(struct fallback_dispatch * dispatch,struct mt_slot * slot)868 fallback_arbitrate_touch(struct fallback_dispatch *dispatch,
869 struct mt_slot *slot)
870 {
871 bool discard = false;
872
873 if (dispatch->arbitration.state == ARBITRATION_IGNORE_RECT &&
874 point_in_rect(&slot->point, &dispatch->arbitration.rect)) {
875 slot->palm_state = PALM_IS_PALM;
876 discard = true;
877 }
878
879 return discard;
880 }
881
882 static inline bool
fallback_flush_mt_events(struct fallback_dispatch * dispatch,struct evdev_device * device,uint64_t time)883 fallback_flush_mt_events(struct fallback_dispatch *dispatch,
884 struct evdev_device *device,
885 uint64_t time)
886 {
887 bool sent = false;
888
889 for (size_t i = 0; i < dispatch->mt.slots_len; i++) {
890 struct mt_slot *slot = &dispatch->mt.slots[i];
891
892 if (!slot->dirty)
893 continue;
894
895 slot->dirty = false;
896
897 /* Any palm state other than PALM_NEW means we've either
898 * already cancelled the touch or the touch was never
899 * a finger anyway and we didn't send the begin.
900 */
901 if (slot->palm_state == PALM_NEW) {
902 if (slot->state != SLOT_STATE_BEGIN)
903 sent = fallback_flush_mt_cancel(dispatch,
904 device,
905 i,
906 time);
907 slot->palm_state = PALM_IS_PALM;
908 } else if (slot->palm_state == PALM_NONE) {
909 switch (slot->state) {
910 case SLOT_STATE_BEGIN:
911 if (!fallback_arbitrate_touch(dispatch,
912 slot)) {
913 sent = fallback_flush_mt_down(dispatch,
914 device,
915 i,
916 time);
917 }
918 break;
919 case SLOT_STATE_UPDATE:
920 sent = fallback_flush_mt_motion(dispatch,
921 device,
922 i,
923 time);
924 break;
925 case SLOT_STATE_END:
926 sent = fallback_flush_mt_up(dispatch,
927 device,
928 i,
929 time);
930 break;
931 case SLOT_STATE_NONE:
932 break;
933 }
934 }
935
936 /* State machine continues independent of the palm state */
937 switch (slot->state) {
938 case SLOT_STATE_BEGIN:
939 slot->state = SLOT_STATE_UPDATE;
940 break;
941 case SLOT_STATE_UPDATE:
942 break;
943 case SLOT_STATE_END:
944 slot->state = SLOT_STATE_NONE;
945 break;
946 case SLOT_STATE_NONE:
947 /* touch arbitration may swallow the begin,
948 * so we may get updates for a touch still
949 * in NONE state */
950 break;
951 }
952 }
953
954 return sent;
955 }
956
957 static void
fallback_handle_state(struct fallback_dispatch * dispatch,struct evdev_device * device,uint64_t time)958 fallback_handle_state(struct fallback_dispatch *dispatch,
959 struct evdev_device *device,
960 uint64_t time)
961 {
962 bool need_touch_frame = false;
963
964 /* Relative motion */
965 if (dispatch->pending_event & EVDEV_RELATIVE_MOTION)
966 fallback_flush_relative_motion(dispatch, device, time);
967
968 /* Single touch or absolute pointer devices */
969 if (dispatch->pending_event & EVDEV_ABSOLUTE_TOUCH_DOWN) {
970 if (fallback_flush_st_down(dispatch, device, time))
971 need_touch_frame = true;
972 } else if (dispatch->pending_event & EVDEV_ABSOLUTE_MOTION) {
973 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
974 if (fallback_flush_st_motion(dispatch,
975 device,
976 time))
977 need_touch_frame = true;
978 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
979 fallback_flush_absolute_motion(dispatch,
980 device,
981 time);
982 }
983 }
984
985 if (dispatch->pending_event & EVDEV_ABSOLUTE_TOUCH_UP) {
986 if (fallback_flush_st_up(dispatch, device, time))
987 need_touch_frame = true;
988 }
989
990 /* Multitouch devices */
991 if (dispatch->pending_event & EVDEV_ABSOLUTE_MT)
992 need_touch_frame = fallback_flush_mt_events(dispatch,
993 device,
994 time);
995
996 if (need_touch_frame)
997 touch_notify_frame(&device->base, time);
998
999 fallback_flush_wheels(dispatch, device, time);
1000
1001 /* Buttons and keys */
1002 if (dispatch->pending_event & EVDEV_KEY) {
1003 bool want_debounce = false;
1004 for (unsigned int code = 0; code <= KEY_MAX; code++) {
1005 if (!hw_key_has_changed(dispatch, code))
1006 continue;
1007
1008 if (get_key_type(code) == KEY_TYPE_BUTTON) {
1009 want_debounce = true;
1010 break;
1011 }
1012 }
1013
1014 if (want_debounce)
1015 fallback_debounce_handle_state(dispatch, time);
1016
1017 hw_key_update_last_state(dispatch);
1018 }
1019
1020 dispatch->pending_event = EVDEV_NONE;
1021 }
1022
1023 static void
fallback_interface_process(struct evdev_dispatch * evdev_dispatch,struct evdev_device * device,struct input_event * event,uint64_t time)1024 fallback_interface_process(struct evdev_dispatch *evdev_dispatch,
1025 struct evdev_device *device,
1026 struct input_event *event,
1027 uint64_t time)
1028 {
1029 struct fallback_dispatch *dispatch = fallback_dispatch(evdev_dispatch);
1030
1031 if (dispatch->arbitration.in_arbitration)
1032 return;
1033
1034 switch (event->type) {
1035 case EV_REL:
1036 fallback_process_relative(dispatch, device, event, time);
1037 break;
1038 case EV_ABS:
1039 fallback_process_absolute(dispatch, device, event, time);
1040 break;
1041 case EV_KEY:
1042 fallback_process_key(dispatch, device, event, time);
1043 break;
1044 case EV_SW:
1045 fallback_process_switch(dispatch, device, event, time);
1046 break;
1047 case EV_SYN:
1048 fallback_handle_state(dispatch, device, time);
1049 break;
1050 }
1051 }
1052
1053 static void
cancel_touches(struct fallback_dispatch * dispatch,struct evdev_device * device,const struct device_coord_rect * rect,uint64_t time)1054 cancel_touches(struct fallback_dispatch *dispatch,
1055 struct evdev_device *device,
1056 const struct device_coord_rect *rect,
1057 uint64_t time)
1058 {
1059 unsigned int idx;
1060 bool need_frame = false;
1061
1062 if (!rect || point_in_rect(&dispatch->abs.point, rect))
1063 need_frame = fallback_flush_st_cancel(dispatch,
1064 device,
1065 time);
1066
1067 for (idx = 0; idx < dispatch->mt.slots_len; idx++) {
1068 struct mt_slot *slot = &dispatch->mt.slots[idx];
1069
1070 if (slot->seat_slot == -1)
1071 continue;
1072
1073 if ((!rect || point_in_rect(&slot->point, rect)) &&
1074 fallback_flush_mt_cancel(dispatch, device, idx, time))
1075 need_frame = true;
1076 }
1077
1078 if (need_frame)
1079 touch_notify_frame(&device->base, time);
1080 }
1081
1082 static void
release_pressed_keys(struct fallback_dispatch * dispatch,struct evdev_device * device,uint64_t time)1083 release_pressed_keys(struct fallback_dispatch *dispatch,
1084 struct evdev_device *device,
1085 uint64_t time)
1086 {
1087 int code;
1088
1089 for (code = 0; code < KEY_CNT; code++) {
1090 int count = get_key_down_count(device, code);
1091
1092 if (count == 0)
1093 continue;
1094
1095 if (count > 1) {
1096 evdev_log_bug_libinput(device,
1097 "key %d is down %d times.\n",
1098 code,
1099 count);
1100 }
1101
1102 switch (get_key_type(code)) {
1103 case KEY_TYPE_NONE:
1104 break;
1105 case KEY_TYPE_KEY:
1106 fallback_keyboard_notify_key(
1107 dispatch,
1108 device,
1109 time,
1110 code,
1111 LIBINPUT_KEY_STATE_RELEASED);
1112 break;
1113 case KEY_TYPE_BUTTON:
1114 evdev_pointer_notify_button(
1115 device,
1116 time,
1117 evdev_to_left_handed(device, code),
1118 LIBINPUT_BUTTON_STATE_RELEASED);
1119 break;
1120 }
1121
1122 count = get_key_down_count(device, code);
1123 if (count != 0) {
1124 evdev_log_bug_libinput(device,
1125 "releasing key %d failed.\n",
1126 code);
1127 break;
1128 }
1129 }
1130 }
1131
1132 static void
fallback_return_to_neutral_state(struct fallback_dispatch * dispatch,struct evdev_device * device)1133 fallback_return_to_neutral_state(struct fallback_dispatch *dispatch,
1134 struct evdev_device *device)
1135 {
1136 struct libinput *libinput = evdev_libinput_context(device);
1137 uint64_t time;
1138
1139 if ((time = libinput_now(libinput)) == 0)
1140 return;
1141
1142 cancel_touches(dispatch, device, NULL, time);
1143 release_pressed_keys(dispatch, device, time);
1144 memset(dispatch->hw_key_mask, 0, sizeof(dispatch->hw_key_mask));
1145 memset(dispatch->hw_key_mask, 0, sizeof(dispatch->last_hw_key_mask));
1146 }
1147
1148 static void
fallback_interface_suspend(struct evdev_dispatch * evdev_dispatch,struct evdev_device * device)1149 fallback_interface_suspend(struct evdev_dispatch *evdev_dispatch,
1150 struct evdev_device *device)
1151 {
1152 struct fallback_dispatch *dispatch = fallback_dispatch(evdev_dispatch);
1153
1154 fallback_return_to_neutral_state(dispatch, device);
1155 }
1156
1157 static void
fallback_interface_remove(struct evdev_dispatch * evdev_dispatch)1158 fallback_interface_remove(struct evdev_dispatch *evdev_dispatch)
1159 {
1160 struct fallback_dispatch *dispatch = fallback_dispatch(evdev_dispatch);
1161 struct evdev_paired_keyboard *kbd, *tmp;
1162
1163 libinput_timer_cancel(&dispatch->debounce.timer);
1164 libinput_timer_cancel(&dispatch->debounce.timer_short);
1165 libinput_timer_cancel(&dispatch->arbitration.arbitration_timer);
1166
1167 libinput_device_remove_event_listener(&dispatch->tablet_mode.other.listener);
1168
1169 list_for_each_safe(kbd,
1170 tmp,
1171 &dispatch->lid.paired_keyboard_list,
1172 link) {
1173 evdev_paired_keyboard_destroy(kbd);
1174 }
1175 }
1176
1177 static void
fallback_interface_sync_initial_state(struct evdev_device * device,struct evdev_dispatch * evdev_dispatch)1178 fallback_interface_sync_initial_state(struct evdev_device *device,
1179 struct evdev_dispatch *evdev_dispatch)
1180 {
1181 struct fallback_dispatch *dispatch = fallback_dispatch(evdev_dispatch);
1182 uint64_t time = libinput_now(evdev_libinput_context(device));
1183
1184 if (device->tags & EVDEV_TAG_LID_SWITCH) {
1185 struct libevdev *evdev = device->evdev;
1186
1187 dispatch->lid.is_closed = libevdev_get_event_value(evdev,
1188 EV_SW,
1189 SW_LID);
1190 dispatch->lid.is_closed_client_state = false;
1191
1192 /* For the initial state sync, we depend on whether the lid switch
1193 * is reliable. If we know it's reliable, we sync as expected.
1194 * If we're not sure, we ignore the initial state and only sync on
1195 * the first future lid close event. Laptops with a broken switch
1196 * that always have the switch in 'on' state thus don't mess up our
1197 * touchpad.
1198 */
1199 if (dispatch->lid.is_closed &&
1200 dispatch->lid.reliability == RELIABILITY_RELIABLE) {
1201 fallback_lid_notify_toggle(dispatch, device, time);
1202 }
1203 }
1204
1205 if (dispatch->tablet_mode.sw.state) {
1206 switch_notify_toggle(&device->base,
1207 time,
1208 LIBINPUT_SWITCH_TABLET_MODE,
1209 LIBINPUT_SWITCH_STATE_ON);
1210 }
1211 }
1212
1213 static void
fallback_interface_update_rect(struct evdev_dispatch * evdev_dispatch,struct evdev_device * device,const struct phys_rect * phys_rect,uint64_t time)1214 fallback_interface_update_rect(struct evdev_dispatch *evdev_dispatch,
1215 struct evdev_device *device,
1216 const struct phys_rect *phys_rect,
1217 uint64_t time)
1218 {
1219 struct fallback_dispatch *dispatch = fallback_dispatch(evdev_dispatch);
1220 struct device_coord_rect rect;
1221
1222 assert(phys_rect);
1223
1224 /* Existing touches do not change, we just update the rect and only
1225 * new touches in these areas will be ignored. If you want to paint
1226 * over your finger, be my guest. */
1227 rect = evdev_phys_rect_to_units(device, phys_rect);
1228 dispatch->arbitration.rect = rect;
1229 }
1230
1231 static void
fallback_interface_toggle_touch(struct evdev_dispatch * evdev_dispatch,struct evdev_device * device,enum evdev_arbitration_state which,const struct phys_rect * phys_rect,uint64_t time)1232 fallback_interface_toggle_touch(struct evdev_dispatch *evdev_dispatch,
1233 struct evdev_device *device,
1234 enum evdev_arbitration_state which,
1235 const struct phys_rect *phys_rect,
1236 uint64_t time)
1237 {
1238 struct fallback_dispatch *dispatch = fallback_dispatch(evdev_dispatch);
1239 struct device_coord_rect rect = {0};
1240
1241 if (which == dispatch->arbitration.state)
1242 return;
1243
1244 switch (which) {
1245 case ARBITRATION_NOT_ACTIVE:
1246 /* if in-kernel arbitration is in use and there is a touch
1247 * and a pen in proximity, lifting the pen out of proximity
1248 * causes a touch begin for the touch. On a hand-lift the
1249 * proximity out precedes the touch up by a few ms, so we
1250 * get what looks like a tap. Fix this by delaying
1251 * arbitration by just a little bit so that any touch in
1252 * event is caught as palm touch. */
1253 libinput_timer_set(&dispatch->arbitration.arbitration_timer,
1254 time + ms2us(90));
1255 break;
1256 case ARBITRATION_IGNORE_RECT:
1257 assert(phys_rect);
1258 rect = evdev_phys_rect_to_units(device, phys_rect);
1259 cancel_touches(dispatch, device, &rect, time);
1260 dispatch->arbitration.rect = rect;
1261 break;
1262 case ARBITRATION_IGNORE_ALL:
1263 libinput_timer_cancel(&dispatch->arbitration.arbitration_timer);
1264 fallback_return_to_neutral_state(dispatch, device);
1265 dispatch->arbitration.in_arbitration = true;
1266 break;
1267 }
1268
1269 dispatch->arbitration.state = which;
1270 }
1271
1272 static void
fallback_interface_destroy(struct evdev_dispatch * evdev_dispatch)1273 fallback_interface_destroy(struct evdev_dispatch *evdev_dispatch)
1274 {
1275 struct fallback_dispatch *dispatch = fallback_dispatch(evdev_dispatch);
1276
1277 libinput_timer_destroy(&dispatch->arbitration.arbitration_timer);
1278 libinput_timer_destroy(&dispatch->debounce.timer);
1279 libinput_timer_destroy(&dispatch->debounce.timer_short);
1280
1281 free(dispatch->mt.slots);
1282 free(dispatch);
1283 }
1284
1285 static void
fallback_lid_pair_keyboard(struct evdev_device * lid_switch,struct evdev_device * keyboard)1286 fallback_lid_pair_keyboard(struct evdev_device *lid_switch,
1287 struct evdev_device *keyboard)
1288 {
1289 struct fallback_dispatch *dispatch =
1290 fallback_dispatch(lid_switch->dispatch);
1291 struct evdev_paired_keyboard *kbd;
1292 size_t count = 0;
1293
1294 if ((keyboard->tags & EVDEV_TAG_KEYBOARD) == 0 ||
1295 (lid_switch->tags & EVDEV_TAG_LID_SWITCH) == 0)
1296 return;
1297
1298 if ((keyboard->tags & EVDEV_TAG_INTERNAL_KEYBOARD) == 0)
1299 return;
1300
1301 list_for_each(kbd, &dispatch->lid.paired_keyboard_list, link) {
1302 count++;
1303 if (count > 3) {
1304 evdev_log_info(lid_switch,
1305 "lid: too many internal keyboards\n");
1306 break;
1307 }
1308 }
1309
1310 kbd = zalloc(sizeof(*kbd));
1311 kbd->device = keyboard;
1312 libinput_device_init_event_listener(&kbd->listener);
1313 list_insert(&dispatch->lid.paired_keyboard_list, &kbd->link);
1314 evdev_log_debug(lid_switch,
1315 "lid: keyboard paired with %s<->%s\n",
1316 lid_switch->devname,
1317 keyboard->devname);
1318
1319 /* We need to init the event listener now only if the
1320 * reported state is closed. */
1321 if (dispatch->lid.is_closed)
1322 fallback_lid_toggle_keyboard_listener(dispatch,
1323 kbd,
1324 dispatch->lid.is_closed);
1325 }
1326
1327 static void
fallback_resume(struct fallback_dispatch * dispatch,struct evdev_device * device)1328 fallback_resume(struct fallback_dispatch *dispatch,
1329 struct evdev_device *device)
1330 {
1331 if (dispatch->base.sendevents.current_mode ==
1332 LIBINPUT_CONFIG_SEND_EVENTS_DISABLED)
1333 return;
1334
1335 evdev_device_resume(device);
1336 }
1337
1338 static void
fallback_suspend(struct fallback_dispatch * dispatch,struct evdev_device * device)1339 fallback_suspend(struct fallback_dispatch *dispatch,
1340 struct evdev_device *device)
1341 {
1342 evdev_device_suspend(device);
1343 }
1344
1345 static void
fallback_tablet_mode_switch_event(uint64_t time,struct libinput_event * event,void * data)1346 fallback_tablet_mode_switch_event(uint64_t time,
1347 struct libinput_event *event,
1348 void *data)
1349 {
1350 struct fallback_dispatch *dispatch = data;
1351 struct evdev_device *device = dispatch->device;
1352 struct libinput_event_switch *swev;
1353
1354 if (libinput_event_get_type(event) != LIBINPUT_EVENT_SWITCH_TOGGLE)
1355 return;
1356
1357 swev = libinput_event_get_switch_event(event);
1358 if (libinput_event_switch_get_switch(swev) !=
1359 LIBINPUT_SWITCH_TABLET_MODE)
1360 return;
1361
1362 switch (libinput_event_switch_get_switch_state(swev)) {
1363 case LIBINPUT_SWITCH_STATE_OFF:
1364 fallback_resume(dispatch, device);
1365 evdev_log_debug(device, "tablet-mode: resuming device\n");
1366 break;
1367 case LIBINPUT_SWITCH_STATE_ON:
1368 fallback_suspend(dispatch, device);
1369 evdev_log_debug(device, "tablet-mode: suspending device\n");
1370 break;
1371 }
1372 }
1373
1374 static void
fallback_pair_tablet_mode(struct evdev_device * keyboard,struct evdev_device * tablet_mode_switch)1375 fallback_pair_tablet_mode(struct evdev_device *keyboard,
1376 struct evdev_device *tablet_mode_switch)
1377 {
1378 struct fallback_dispatch *dispatch =
1379 fallback_dispatch(keyboard->dispatch);
1380
1381 if ((keyboard->tags & EVDEV_TAG_EXTERNAL_KEYBOARD))
1382 return;
1383
1384 if ((keyboard->tags & EVDEV_TAG_TRACKPOINT)) {
1385 if (keyboard->tags & EVDEV_TAG_EXTERNAL_MOUSE)
1386 return;
1387 /* This filters out all internal keyboard-like devices (Video
1388 * Switch) */
1389 } else if ((keyboard->tags & EVDEV_TAG_INTERNAL_KEYBOARD) == 0) {
1390 return;
1391 }
1392
1393 if (evdev_device_has_model_quirk(keyboard,
1394 QUIRK_MODEL_TABLET_MODE_NO_SUSPEND))
1395 return;
1396
1397 if ((tablet_mode_switch->tags & EVDEV_TAG_TABLET_MODE_SWITCH) == 0)
1398 return;
1399
1400 if (dispatch->tablet_mode.other.sw_device)
1401 return;
1402
1403 evdev_log_debug(keyboard,
1404 "tablet-mode: paired %s<->%s\n",
1405 keyboard->devname,
1406 tablet_mode_switch->devname);
1407
1408 libinput_device_add_event_listener(&tablet_mode_switch->base,
1409 &dispatch->tablet_mode.other.listener,
1410 fallback_tablet_mode_switch_event,
1411 dispatch);
1412 dispatch->tablet_mode.other.sw_device = tablet_mode_switch;
1413
1414 if (evdev_device_switch_get_state(tablet_mode_switch,
1415 LIBINPUT_SWITCH_TABLET_MODE)
1416 == LIBINPUT_SWITCH_STATE_ON) {
1417 evdev_log_debug(keyboard, "tablet-mode: suspending device\n");
1418 fallback_suspend(dispatch, keyboard);
1419 }
1420 }
1421
1422 static void
fallback_interface_device_added(struct evdev_device * device,struct evdev_device * added_device)1423 fallback_interface_device_added(struct evdev_device *device,
1424 struct evdev_device *added_device)
1425 {
1426 fallback_lid_pair_keyboard(device, added_device);
1427 fallback_pair_tablet_mode(device, added_device);
1428 }
1429
1430 static void
fallback_interface_device_removed(struct evdev_device * device,struct evdev_device * removed_device)1431 fallback_interface_device_removed(struct evdev_device *device,
1432 struct evdev_device *removed_device)
1433 {
1434 struct fallback_dispatch *dispatch =
1435 fallback_dispatch(device->dispatch);
1436 struct evdev_paired_keyboard *kbd, *tmp;
1437
1438 list_for_each_safe(kbd,
1439 tmp,
1440 &dispatch->lid.paired_keyboard_list,
1441 link) {
1442 if (!kbd->device)
1443 continue;
1444
1445 if (kbd->device != removed_device)
1446 continue;
1447
1448 evdev_paired_keyboard_destroy(kbd);
1449 }
1450
1451 if (removed_device == dispatch->tablet_mode.other.sw_device) {
1452 libinput_device_remove_event_listener(
1453 &dispatch->tablet_mode.other.listener);
1454 libinput_device_init_event_listener(
1455 &dispatch->tablet_mode.other.listener);
1456 dispatch->tablet_mode.other.sw_device = NULL;
1457 }
1458 }
1459
1460 struct evdev_dispatch_interface fallback_interface = {
1461 .process = fallback_interface_process,
1462 .suspend = fallback_interface_suspend,
1463 .remove = fallback_interface_remove,
1464 .destroy = fallback_interface_destroy,
1465 .device_added = fallback_interface_device_added,
1466 .device_removed = fallback_interface_device_removed,
1467 .device_suspended = fallback_interface_device_removed, /* treat as remove */
1468 .device_resumed = fallback_interface_device_added, /* treat as add */
1469 .post_added = fallback_interface_sync_initial_state,
1470 .touch_arbitration_toggle = fallback_interface_toggle_touch,
1471 .touch_arbitration_update_rect = fallback_interface_update_rect,
1472 .get_switch_state = fallback_interface_get_switch_state,
1473 };
1474
1475 static void
fallback_change_to_left_handed(struct evdev_device * device)1476 fallback_change_to_left_handed(struct evdev_device *device)
1477 {
1478 struct fallback_dispatch *dispatch = fallback_dispatch(device->dispatch);
1479
1480 if (device->left_handed.want_enabled == device->left_handed.enabled)
1481 return;
1482
1483 if (fallback_any_button_down(dispatch, device))
1484 return;
1485
1486 device->left_handed.enabled = device->left_handed.want_enabled;
1487 }
1488
1489 static void
fallback_change_scroll_method(struct evdev_device * device)1490 fallback_change_scroll_method(struct evdev_device *device)
1491 {
1492 struct fallback_dispatch *dispatch = fallback_dispatch(device->dispatch);
1493
1494 if (device->scroll.want_method == device->scroll.method &&
1495 device->scroll.want_button == device->scroll.button &&
1496 device->scroll.want_lock_enabled == device->scroll.lock_enabled)
1497 return;
1498
1499 if (fallback_any_button_down(dispatch, device))
1500 return;
1501
1502 device->scroll.method = device->scroll.want_method;
1503 device->scroll.button = device->scroll.want_button;
1504 device->scroll.lock_enabled = device->scroll.want_lock_enabled;
1505 evdev_set_button_scroll_lock_enabled(device, device->scroll.lock_enabled);
1506 }
1507
1508 static int
fallback_rotation_config_is_available(struct libinput_device * device)1509 fallback_rotation_config_is_available(struct libinput_device *device)
1510 {
1511 /* This function only gets called when we support rotation */
1512 return 1;
1513 }
1514
1515 static enum libinput_config_status
fallback_rotation_config_set_angle(struct libinput_device * libinput_device,unsigned int degrees_cw)1516 fallback_rotation_config_set_angle(struct libinput_device *libinput_device,
1517 unsigned int degrees_cw)
1518 {
1519 struct evdev_device *device = evdev_device(libinput_device);
1520 struct fallback_dispatch *dispatch = fallback_dispatch(device->dispatch);
1521
1522 dispatch->rotation.angle = degrees_cw;
1523 matrix_init_rotate(&dispatch->rotation.matrix, degrees_cw);
1524
1525 return LIBINPUT_CONFIG_STATUS_SUCCESS;
1526 }
1527
1528 static unsigned int
fallback_rotation_config_get_angle(struct libinput_device * libinput_device)1529 fallback_rotation_config_get_angle(struct libinput_device *libinput_device)
1530 {
1531 struct evdev_device *device = evdev_device(libinput_device);
1532 struct fallback_dispatch *dispatch = fallback_dispatch(device->dispatch);
1533
1534 return dispatch->rotation.angle;
1535 }
1536
1537 static unsigned int
fallback_rotation_config_get_default_angle(struct libinput_device * device)1538 fallback_rotation_config_get_default_angle(struct libinput_device *device)
1539 {
1540 return 0;
1541 }
1542
1543 static void
fallback_init_rotation(struct fallback_dispatch * dispatch,struct evdev_device * device)1544 fallback_init_rotation(struct fallback_dispatch *dispatch,
1545 struct evdev_device *device)
1546 {
1547 if ((device->model_flags & EVDEV_MODEL_TRACKBALL) == 0)
1548 return;
1549
1550 dispatch->rotation.config.is_available = fallback_rotation_config_is_available;
1551 dispatch->rotation.config.set_angle = fallback_rotation_config_set_angle;
1552 dispatch->rotation.config.get_angle = fallback_rotation_config_get_angle;
1553 dispatch->rotation.config.get_default_angle = fallback_rotation_config_get_default_angle;
1554 dispatch->rotation.is_enabled = false;
1555 matrix_init_identity(&dispatch->rotation.matrix);
1556 device->base.config.rotation = &dispatch->rotation.config;
1557 }
1558
1559 static inline int
fallback_dispatch_init_slots(struct fallback_dispatch * dispatch,struct evdev_device * device)1560 fallback_dispatch_init_slots(struct fallback_dispatch *dispatch,
1561 struct evdev_device *device)
1562 {
1563 struct libevdev *evdev = device->evdev;
1564 struct mt_slot *slots;
1565 int num_slots;
1566 int active_slot;
1567 int slot;
1568
1569 if (evdev_is_fake_mt_device(device) ||
1570 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) ||
1571 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y))
1572 return 0;
1573
1574 /* We only handle the slotted Protocol B in libinput.
1575 Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
1576 require mtdev for conversion. */
1577 if (evdev_need_mtdev(device)) {
1578 device->mtdev = mtdev_new_open(device->fd);
1579 if (!device->mtdev)
1580 return -1;
1581
1582 /* pick 10 slots as default for type A
1583 devices. */
1584 num_slots = 10;
1585 active_slot = device->mtdev->caps.slot.value;
1586 } else {
1587 num_slots = libevdev_get_num_slots(device->evdev);
1588 active_slot = libevdev_get_current_slot(evdev);
1589 }
1590
1591 slots = zalloc(num_slots * sizeof(struct mt_slot));
1592
1593 for (slot = 0; slot < num_slots; ++slot) {
1594 slots[slot].seat_slot = -1;
1595
1596 if (evdev_need_mtdev(device))
1597 continue;
1598
1599 slots[slot].point.x = libevdev_get_slot_value(evdev,
1600 slot,
1601 ABS_MT_POSITION_X);
1602 slots[slot].point.y = libevdev_get_slot_value(evdev,
1603 slot,
1604 ABS_MT_POSITION_Y);
1605 }
1606 dispatch->mt.slots = slots;
1607 dispatch->mt.slots_len = num_slots;
1608 dispatch->mt.slot = active_slot;
1609 dispatch->mt.has_palm = libevdev_has_event_code(evdev,
1610 EV_ABS,
1611 ABS_MT_TOOL_TYPE);
1612
1613 if (device->abs.absinfo_x->fuzz || device->abs.absinfo_y->fuzz) {
1614 dispatch->mt.want_hysteresis = true;
1615 dispatch->mt.hysteresis_margin.x = device->abs.absinfo_x->fuzz/2;
1616 dispatch->mt.hysteresis_margin.y = device->abs.absinfo_y->fuzz/2;
1617 }
1618
1619 return 0;
1620 }
1621
1622 static inline void
fallback_dispatch_init_rel(struct fallback_dispatch * dispatch,struct evdev_device * device)1623 fallback_dispatch_init_rel(struct fallback_dispatch *dispatch,
1624 struct evdev_device *device)
1625 {
1626 dispatch->rel.x = 0;
1627 dispatch->rel.y = 0;
1628 }
1629
1630 static inline void
fallback_dispatch_init_abs(struct fallback_dispatch * dispatch,struct evdev_device * device)1631 fallback_dispatch_init_abs(struct fallback_dispatch *dispatch,
1632 struct evdev_device *device)
1633 {
1634 if (!libevdev_has_event_code(device->evdev, EV_ABS, ABS_X))
1635 return;
1636
1637 dispatch->abs.point.x = device->abs.absinfo_x->value;
1638 dispatch->abs.point.y = device->abs.absinfo_y->value;
1639 dispatch->abs.seat_slot = -1;
1640
1641 evdev_device_init_abs_range_warnings(device);
1642 }
1643
1644 static inline void
fallback_dispatch_init_switch(struct fallback_dispatch * dispatch,struct evdev_device * device)1645 fallback_dispatch_init_switch(struct fallback_dispatch *dispatch,
1646 struct evdev_device *device)
1647 {
1648 int val;
1649
1650 list_init(&dispatch->lid.paired_keyboard_list);
1651
1652 if (device->tags & EVDEV_TAG_LID_SWITCH) {
1653 dispatch->lid.reliability = evdev_read_switch_reliability_prop(device);
1654 dispatch->lid.is_closed = false;
1655 }
1656
1657 if (device->tags & EVDEV_TAG_TABLET_MODE_SWITCH) {
1658 val = libevdev_get_event_value(device->evdev,
1659 EV_SW,
1660 SW_TABLET_MODE);
1661 dispatch->tablet_mode.sw.state = val;
1662 }
1663
1664 libinput_device_init_event_listener(&dispatch->tablet_mode.other.listener);
1665 }
1666
1667 static void
fallback_arbitration_timeout(uint64_t now,void * data)1668 fallback_arbitration_timeout(uint64_t now, void *data)
1669 {
1670 struct fallback_dispatch *dispatch = data;
1671
1672 if (dispatch->arbitration.in_arbitration)
1673 dispatch->arbitration.in_arbitration = false;
1674 }
1675
1676 static void
fallback_init_arbitration(struct fallback_dispatch * dispatch,struct evdev_device * device)1677 fallback_init_arbitration(struct fallback_dispatch *dispatch,
1678 struct evdev_device *device)
1679 {
1680 char timer_name[64];
1681
1682 snprintf(timer_name,
1683 sizeof(timer_name),
1684 "%s arbitration",
1685 evdev_device_get_sysname(device));
1686 libinput_timer_init(&dispatch->arbitration.arbitration_timer,
1687 evdev_libinput_context(device),
1688 timer_name,
1689 fallback_arbitration_timeout,
1690 dispatch);
1691 dispatch->arbitration.in_arbitration = false;
1692 }
1693
1694 struct evdev_dispatch *
fallback_dispatch_create(struct libinput_device * libinput_device)1695 fallback_dispatch_create(struct libinput_device *libinput_device)
1696 {
1697 struct evdev_device *device = evdev_device(libinput_device);
1698 struct fallback_dispatch *dispatch;
1699
1700 dispatch = zalloc(sizeof *dispatch);
1701 dispatch->device = evdev_device(libinput_device);
1702 dispatch->base.dispatch_type = DISPATCH_FALLBACK;
1703 dispatch->base.interface = &fallback_interface;
1704 dispatch->pending_event = EVDEV_NONE;
1705 list_init(&dispatch->lid.paired_keyboard_list);
1706
1707 fallback_dispatch_init_rel(dispatch, device);
1708 fallback_dispatch_init_abs(dispatch, device);
1709 if (fallback_dispatch_init_slots(dispatch, device) == -1) {
1710 free(dispatch);
1711 return NULL;
1712 }
1713
1714 fallback_dispatch_init_switch(dispatch, device);
1715
1716 if (device->left_handed.want_enabled)
1717 evdev_init_left_handed(device,
1718 fallback_change_to_left_handed);
1719
1720 if (device->scroll.want_button)
1721 evdev_init_button_scroll(device,
1722 fallback_change_scroll_method);
1723
1724 if (device->scroll.natural_scrolling_enabled)
1725 evdev_init_natural_scroll(device);
1726
1727 evdev_init_calibration(device, &dispatch->calibration);
1728 evdev_init_sendevents(device, &dispatch->base);
1729 fallback_init_rotation(dispatch, device);
1730
1731 /* BTN_MIDDLE is set on mice even when it's not present. So
1732 * we can only use the absence of BTN_MIDDLE to mean something, i.e.
1733 * we enable it by default on anything that only has L&R.
1734 * If we have L&R and no middle, we don't expose it as config
1735 * option */
1736 if (libevdev_has_event_code(device->evdev, EV_KEY, BTN_LEFT) &&
1737 libevdev_has_event_code(device->evdev, EV_KEY, BTN_RIGHT)) {
1738 bool has_middle = libevdev_has_event_code(device->evdev,
1739 EV_KEY,
1740 BTN_MIDDLE);
1741 bool want_config = has_middle;
1742 bool enable_by_default = !has_middle;
1743
1744 evdev_init_middlebutton(device,
1745 enable_by_default,
1746 want_config);
1747 }
1748
1749 fallback_init_debounce(dispatch);
1750 fallback_init_arbitration(dispatch, device);
1751
1752 return &dispatch->base;
1753 }
1754