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