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