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