evdev-fallback.c 44.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Copyright © 2010 Intel Corporation
 * Copyright © 2013 Jonas Ådahl
 * Copyright © 2013-2017 Red Hat, Inc.
 * Copyright © 2017 James Ye <jye836@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "config.h"

#include <mtdev-plumbing.h>

31
#include "evdev-fallback.h"
32
#include "util-input-event.h"
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

static void
fallback_keyboard_notify_key(struct fallback_dispatch *dispatch,
			     struct evdev_device *device,
			     uint64_t time,
			     int key,
			     enum libinput_key_state state)
{
	int down_count;

	down_count = evdev_update_key_down_count(device, key, state);

	if ((state == LIBINPUT_KEY_STATE_PRESSED && down_count == 1) ||
	    (state == LIBINPUT_KEY_STATE_RELEASED && down_count == 0))
		keyboard_notify_key(&device->base, time, key, state);
}

static void
fallback_lid_notify_toggle(struct fallback_dispatch *dispatch,
			   struct evdev_device *device,
			   uint64_t time)
{
	if (dispatch->lid.is_closed ^ dispatch->lid.is_closed_client_state) {
		switch_notify_toggle(&device->base,
				     time,
				     LIBINPUT_SWITCH_LID,
				     dispatch->lid.is_closed);
		dispatch->lid.is_closed_client_state = dispatch->lid.is_closed;
	}
}

static enum libinput_switch_state
65 66
fallback_interface_get_switch_state(struct evdev_dispatch *evdev_dispatch,
				    enum libinput_switch sw)
67 68 69 70 71 72 73 74 75 76 77
{
	struct fallback_dispatch *dispatch = fallback_dispatch(evdev_dispatch);

	switch (sw) {
	case LIBINPUT_SWITCH_TABLET_MODE:
		break;
	default:
		/* Internal function only, so we can abort here */
		abort();
	}

78
	return dispatch->tablet_mode.sw.state ?
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
			LIBINPUT_SWITCH_STATE_ON :
			LIBINPUT_SWITCH_STATE_OFF;
}

static inline void
normalize_delta(struct evdev_device *device,
		const struct device_coords *delta,
		struct normalized_coords *normalized)
{
	normalized->x = delta->x * DEFAULT_MOUSE_DPI / (double)device->dpi;
	normalized->y = delta->y * DEFAULT_MOUSE_DPI / (double)device->dpi;
}

static inline bool
post_trackpoint_scroll(struct evdev_device *device,
		       struct normalized_coords unaccel,
		       uint64_t time)
{
	if (device->scroll.method != LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN)
		return false;

	switch(device->scroll.button_scroll_state) {
	case BUTTONSCROLL_IDLE:
		return false;
	case BUTTONSCROLL_BUTTON_DOWN:
		/* if the button is down but scroll is not active, we're within the
105 106
		   timeout where we swallow motion events but don't post
		   scroll buttons */
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
		evdev_log_debug(device, "btnscroll: discarding\n");
		return true;
	case BUTTONSCROLL_READY:
		device->scroll.button_scroll_state = BUTTONSCROLL_SCROLLING;
		/* fallthrough */
	case BUTTONSCROLL_SCROLLING:
		evdev_post_scroll(device, time,
				  LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS,
				  &unaccel);
		return true;
	}

	assert(!"invalid scroll button state");
}

static inline bool
fallback_filter_defuzz_touch(struct fallback_dispatch *dispatch,
			     struct evdev_device *device,
			     struct mt_slot *slot)
{
	struct device_coords point;

	if (!dispatch->mt.want_hysteresis)
		return false;

132 133 134
	point = evdev_hysteresis(&slot->point,
				 &slot->hysteresis_center,
				 &dispatch->mt.hysteresis_margin);
135
	slot->point = point;
136

137 138
	if (point.x == slot->hysteresis_center.x &&
	    point.y == slot->hysteresis_center.y)
139 140
		return true;

141
	slot->hysteresis_center = point;
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

	return false;
}

static inline void
fallback_rotate_relative(struct fallback_dispatch *dispatch,
			 struct evdev_device *device)
{
	struct device_coords rel = dispatch->rel;

	if (!device->base.config.rotation)
		return;

	/* loss of precision for non-90 degrees, but we only support 90 deg
	 * right now anyway */
	matrix_mult_vec(&dispatch->rotation.matrix, &rel.x, &rel.y);

	dispatch->rel = rel;
}

static void
fallback_flush_relative_motion(struct fallback_dispatch *dispatch,
			       struct evdev_device *device,
			       uint64_t time)
{
	struct libinput_device *base = &device->base;
	struct normalized_coords accel, unaccel;
	struct device_float_coords raw;

	if (!(device->seat_caps & EVDEV_DEVICE_POINTER))
		return;

	fallback_rotate_relative(dispatch, device);

	normalize_delta(device, &dispatch->rel, &unaccel);
	raw.x = dispatch->rel.x;
	raw.y = dispatch->rel.y;
	dispatch->rel.x = 0;
	dispatch->rel.y = 0;

	/* Use unaccelerated deltas for pointing stick scroll */
	if (post_trackpoint_scroll(device, unaccel, time))
		return;

	if (device->pointer.filter) {
		/* Apply pointer acceleration. */
		accel = filter_dispatch(device->pointer.filter,
					&raw,
					device,
					time);
	} else {
		evdev_log_bug_libinput(device,
				       "accel filter missing\n");
		accel = unaccel;
	}

	if (normalized_is_zero(accel) && normalized_is_zero(unaccel))
		return;

	pointer_notify_motion(base, time, &accel, &raw);
}

204 205 206 207 208 209 210 211 212 213 214
static void
fallback_flush_wheels(struct fallback_dispatch *dispatch,
		      struct evdev_device *device,
		      uint64_t time)
{
	struct normalized_coords wheel_degrees = { 0.0, 0.0 };
	struct discrete_coords discrete = { 0.0, 0.0 };

	if (!(device->seat_caps & EVDEV_DEVICE_POINTER))
		return;

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
	if (device->model_flags & EVDEV_MODEL_LENOVO_SCROLLPOINT) {
		struct normalized_coords unaccel = { 0.0, 0.0 };

		dispatch->wheel.y *= -1;
		normalize_delta(device, &dispatch->wheel, &unaccel);
		evdev_post_scroll(device,
				  time,
				  LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS,
				  &unaccel);
		dispatch->wheel.x = 0;
		dispatch->wheel.y = 0;

		return;
	}

230 231 232 233 234 235 236 237
	if (dispatch->wheel.y != 0) {
		wheel_degrees.y = -1 * dispatch->wheel.y *
					device->scroll.wheel_click_angle.y;
		discrete.y = -1 * dispatch->wheel.y;

		evdev_notify_axis(
			device,
			time,
238
			bit(LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL),
239
			LIBINPUT_POINTER_AXIS_SOURCE_WHEEL,
240 241 242 243 244 245 246 247 248 249 250 251 252
			&wheel_degrees,
			&discrete);
		dispatch->wheel.y = 0;
	}

	if (dispatch->wheel.x != 0) {
		wheel_degrees.x = dispatch->wheel.x *
					device->scroll.wheel_click_angle.x;
		discrete.x = dispatch->wheel.x;

		evdev_notify_axis(
			device,
			time,
253
			bit(LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL),
254
			LIBINPUT_POINTER_AXIS_SOURCE_WHEEL,
255 256 257 258 259 260
			&wheel_degrees,
			&discrete);
		dispatch->wheel.x = 0;
	}
}

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
static void
fallback_flush_absolute_motion(struct fallback_dispatch *dispatch,
			       struct evdev_device *device,
			       uint64_t time)
{
	struct libinput_device *base = &device->base;
	struct device_coords point;

	if (!(device->seat_caps & EVDEV_DEVICE_POINTER))
		return;

	point = dispatch->abs.point;
	evdev_transform_absolute(device, &point);

	pointer_notify_motion_absolute(base, time, &point);
}

static bool
fallback_flush_mt_down(struct fallback_dispatch *dispatch,
		       struct evdev_device *device,
		       int slot_idx,
		       uint64_t time)
{
	struct libinput_device *base = &device->base;
	struct libinput_seat *seat = base->seat;
	struct device_coords point;
	struct mt_slot *slot;
	int seat_slot;

	if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
		return false;

	slot = &dispatch->mt.slots[slot_idx];
	if (slot->seat_slot != -1) {
		evdev_log_bug_kernel(device,
				     "driver sent multiple touch down for the same slot");
		return false;
	}

	seat_slot = ffs(~seat->slot_map) - 1;
	slot->seat_slot = seat_slot;

	if (seat_slot == -1)
		return false;

306
	seat->slot_map |= bit(seat_slot);
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
	point = slot->point;
	slot->hysteresis_center = point;
	evdev_transform_absolute(device, &point);

	touch_notify_touch_down(base, time, slot_idx, seat_slot,
				&point);

	return true;
}

static bool
fallback_flush_mt_motion(struct fallback_dispatch *dispatch,
			 struct evdev_device *device,
			 int slot_idx,
			 uint64_t time)
{
	struct libinput_device *base = &device->base;
	struct device_coords point;
	struct mt_slot *slot;
	int seat_slot;

	if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
		return false;

	slot = &dispatch->mt.slots[slot_idx];
	seat_slot = slot->seat_slot;
	point = slot->point;

	if (seat_slot == -1)
		return false;

	if (fallback_filter_defuzz_touch(dispatch, device, slot))
		return false;

	evdev_transform_absolute(device, &point);
	touch_notify_touch_motion(base, time, slot_idx, seat_slot,
				  &point);

	return true;
}

static bool
fallback_flush_mt_up(struct fallback_dispatch *dispatch,
		     struct evdev_device *device,
		     int slot_idx,
		     uint64_t time)
{
	struct libinput_device *base = &device->base;
	struct libinput_seat *seat = base->seat;
	struct mt_slot *slot;
	int seat_slot;

	if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
		return false;

	slot = &dispatch->mt.slots[slot_idx];
	seat_slot = slot->seat_slot;
	slot->seat_slot = -1;

	if (seat_slot == -1)
		return false;

369
	seat->slot_map &= ~bit(seat_slot);
370 371 372 373 374 375

	touch_notify_touch_up(base, time, slot_idx, seat_slot);

	return true;
}

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
static bool
fallback_flush_mt_cancel(struct fallback_dispatch *dispatch,
			 struct evdev_device *device,
			 int slot_idx,
			 uint64_t time)
{
	struct libinput_device *base = &device->base;
	struct libinput_seat *seat = base->seat;
	struct mt_slot *slot;
	int seat_slot;

	if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
		return false;

	slot = &dispatch->mt.slots[slot_idx];
	seat_slot = slot->seat_slot;
	slot->seat_slot = -1;

	if (seat_slot == -1)
		return false;

397
	seat->slot_map &= ~bit(seat_slot);
398 399 400 401 402 403

	touch_notify_touch_cancel(base, time, slot_idx, seat_slot);

	return true;
}

404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
static bool
fallback_flush_st_down(struct fallback_dispatch *dispatch,
		       struct evdev_device *device,
		       uint64_t time)
{
	struct libinput_device *base = &device->base;
	struct libinput_seat *seat = base->seat;
	struct device_coords point;
	int seat_slot;

	if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
		return false;

	if (dispatch->abs.seat_slot != -1) {
		evdev_log_bug_kernel(device,
				     "driver sent multiple touch down for the same slot");
		return false;
	}

	seat_slot = ffs(~seat->slot_map) - 1;
	dispatch->abs.seat_slot = seat_slot;

	if (seat_slot == -1)
		return false;

429
	seat->slot_map |= bit(seat_slot);
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478

	point = dispatch->abs.point;
	evdev_transform_absolute(device, &point);

	touch_notify_touch_down(base, time, -1, seat_slot, &point);

	return true;
}

static bool
fallback_flush_st_motion(struct fallback_dispatch *dispatch,
			 struct evdev_device *device,
			 uint64_t time)
{
	struct libinput_device *base = &device->base;
	struct device_coords point;
	int seat_slot;

	point = dispatch->abs.point;
	evdev_transform_absolute(device, &point);

	seat_slot = dispatch->abs.seat_slot;

	if (seat_slot == -1)
		return false;

	touch_notify_touch_motion(base, time, -1, seat_slot, &point);

	return true;
}

static bool
fallback_flush_st_up(struct fallback_dispatch *dispatch,
		     struct evdev_device *device,
		     uint64_t time)
{
	struct libinput_device *base = &device->base;
	struct libinput_seat *seat = base->seat;
	int seat_slot;

	if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
		return false;

	seat_slot = dispatch->abs.seat_slot;
	dispatch->abs.seat_slot = -1;

	if (seat_slot == -1)
		return false;

479
	seat->slot_map &= ~bit(seat_slot);
480 481 482 483 484 485

	touch_notify_touch_up(base, time, -1, seat_slot);

	return true;
}

486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
static bool
fallback_flush_st_cancel(struct fallback_dispatch *dispatch,
			 struct evdev_device *device,
			 uint64_t time)
{
	struct libinput_device *base = &device->base;
	struct libinput_seat *seat = base->seat;
	int seat_slot;

	if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
		return false;

	seat_slot = dispatch->abs.seat_slot;
	dispatch->abs.seat_slot = -1;

	if (seat_slot == -1)
		return false;

504
	seat->slot_map &= ~bit(seat_slot);
505 506 507 508 509 510

	touch_notify_touch_cancel(base, time, -1, seat_slot);

	return true;
}

511 512 513 514 515
static void
fallback_process_touch_button(struct fallback_dispatch *dispatch,
			      struct evdev_device *device,
			      uint64_t time, int value)
{
516
	dispatch->pending_event |= (value) ?
517
				 EVDEV_ABSOLUTE_TOUCH_DOWN :
518
				 EVDEV_ABSOLUTE_TOUCH_UP;
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
}

static inline void
fallback_process_key(struct fallback_dispatch *dispatch,
		     struct evdev_device *device,
		     struct input_event *e, uint64_t time)
{
	enum key_type type;

	/* ignore kernel key repeat */
	if (e->value == 2)
		return;

	if (e->code == BTN_TOUCH) {
		if (!device->is_mt)
			fallback_process_touch_button(dispatch,
						      device,
						      time,
						      e->value);
		return;
	}

	type = get_key_type(e->code);

	/* Ignore key release events from the kernel for keys that libinput
	 * never got a pressed event for or key presses for keys that we
	 * think are still down */
	switch (type) {
	case KEY_TYPE_NONE:
		break;
	case KEY_TYPE_KEY:
	case KEY_TYPE_BUTTON:
		if ((e->value && hw_is_key_down(dispatch, e->code)) ||
		    (e->value == 0 && !hw_is_key_down(dispatch, e->code)))
			return;
554

555
		dispatch->pending_event |= EVDEV_KEY;
556 557 558 559
		break;
	}

	hw_set_key_down(dispatch, e->code, e->value);
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575

	switch (type) {
	case KEY_TYPE_NONE:
		break;
	case KEY_TYPE_KEY:
		fallback_keyboard_notify_key(
			     dispatch,
			     device,
			     time,
			     e->code,
			     e->value ? LIBINPUT_KEY_STATE_PRESSED :
					LIBINPUT_KEY_STATE_RELEASED);
		break;
	case KEY_TYPE_BUTTON:
		break;
	}
576 577 578 579 580 581 582 583
}

static void
fallback_process_touch(struct fallback_dispatch *dispatch,
		       struct evdev_device *device,
		       struct input_event *e,
		       uint64_t time)
{
584 585 586
	struct mt_slot *slot = &dispatch->mt.slots[dispatch->mt.slot];

	if (e->code == ABS_MT_SLOT) {
587 588 589 590 591 592 593 594
		if ((size_t)e->value >= dispatch->mt.slots_len) {
			evdev_log_bug_libinput(device,
					 "exceeded slot count (%d of max %zd)\n",
					 e->value,
					 dispatch->mt.slots_len);
			e->value = dispatch->mt.slots_len - 1;
		}
		dispatch->mt.slot = e->value;
595 596 597 598
		return;
	}

	switch (e->code) {
599
	case ABS_MT_TRACKING_ID:
600 601 602
		if (e->value >= 0) {
			dispatch->pending_event |= EVDEV_ABSOLUTE_MT;
			slot->state = SLOT_STATE_BEGIN;
603 604 605 606 607 608 609 610 611 612 613 614 615 616
			if (dispatch->mt.has_palm) {
				int v;
				v = libevdev_get_slot_value(device->evdev,
							    dispatch->mt.slot,
							    ABS_MT_TOOL_TYPE);
				switch (v) {
				case MT_TOOL_PALM:
					/* new touch, no cancel needed */
					slot->palm_state = PALM_WAS_PALM;
					break;
				default:
					slot->palm_state = PALM_NONE;
					break;
				}
617 618
			} else {
				slot->palm_state = PALM_NONE;
619
			}
620 621 622 623 624
		} else {
			dispatch->pending_event |= EVDEV_ABSOLUTE_MT;
			slot->state = SLOT_STATE_END;
		}
		slot->dirty = true;
625 626 627 628
		break;
	case ABS_MT_POSITION_X:
		evdev_device_check_abs_axis_range(device, e->code, e->value);
		dispatch->mt.slots[dispatch->mt.slot].point.x = e->value;
629 630
		dispatch->pending_event |= EVDEV_ABSOLUTE_MT;
		slot->dirty = true;
631 632 633 634
		break;
	case ABS_MT_POSITION_Y:
		evdev_device_check_abs_axis_range(device, e->code, e->value);
		dispatch->mt.slots[dispatch->mt.slot].point.y = e->value;
635 636
		dispatch->pending_event |= EVDEV_ABSOLUTE_MT;
		slot->dirty = true;
637
		break;
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
	case ABS_MT_TOOL_TYPE:
		/* The transitions matter - we (may) need to send a touch
		 * cancel event if we just switched to a palm touch. And the
		 * kernel may switch back to finger but we keep the touch as
		 * palm - but then we need to reset correctly on a new touch
		 * sequence.
		 */
		switch (e->value) {
		case MT_TOOL_PALM:
			if (slot->palm_state == PALM_NONE)
				slot->palm_state = PALM_NEW;
			break;
		default:
			if (slot->palm_state == PALM_IS_PALM)
				slot->palm_state = PALM_WAS_PALM;
			break;
		}
		dispatch->pending_event |= EVDEV_ABSOLUTE_MT;
		slot->dirty = true;
		break;
658 659
	}
}
660

661 662 663 664 665 666 667 668 669
static inline void
fallback_process_absolute_motion(struct fallback_dispatch *dispatch,
				 struct evdev_device *device,
				 struct input_event *e)
{
	switch (e->code) {
	case ABS_X:
		evdev_device_check_abs_axis_range(device, e->code, e->value);
		dispatch->abs.point.x = e->value;
670
		dispatch->pending_event |= EVDEV_ABSOLUTE_MOTION;
671 672 673 674
		break;
	case ABS_Y:
		evdev_device_check_abs_axis_range(device, e->code, e->value);
		dispatch->abs.point.y = e->value;
675
		dispatch->pending_event |= EVDEV_ABSOLUTE_MOTION;
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
		break;
	}
}

static void
fallback_lid_keyboard_event(uint64_t time,
			    struct libinput_event *event,
			    void *data)
{
	struct fallback_dispatch *dispatch = fallback_dispatch(data);

	if (!dispatch->lid.is_closed)
		return;

	if (event->type != LIBINPUT_EVENT_KEYBOARD_KEY)
		return;

	if (dispatch->lid.reliability == RELIABILITY_WRITE_OPEN) {
		int fd = libevdev_get_fd(dispatch->device->evdev);
695
		int rc;
696 697 698 699
		struct input_event ev[2];

		ev[0] = input_event_init(0, EV_SW, SW_LID, 0);
		ev[1] = input_event_init(0, EV_SYN, SYN_REPORT, 0);
700

701 702 703 704 705 706 707
		rc = write(fd, ev, sizeof(ev));

		if (rc < 0)
			evdev_log_error(dispatch->device,
					"failed to write SW_LID state (%s)",
					strerror(errno));

708 709 710 711 712 713 714 715 716 717 718 719 720 721
		/* In case write() fails, we sync the lid state manually
		 * regardless. */
	}

	/* Posting the event here means we preempt the keyboard events that
	 * caused us to wake up, so the lid event is always passed on before
	 * the key event.
	 */
	dispatch->lid.is_closed = false;
	fallback_lid_notify_toggle(dispatch, dispatch->device, time);
}

static void
fallback_lid_toggle_keyboard_listener(struct fallback_dispatch *dispatch,
722
				      struct evdev_paired_keyboard *kbd,
723 724 725 726
				      bool is_closed)
{
	assert(kbd->device);

727 728
	libinput_device_remove_event_listener(&kbd->listener);

729 730 731 732 733 734 735
	if (is_closed) {
		libinput_device_add_event_listener(
					&kbd->device->base,
					&kbd->listener,
					fallback_lid_keyboard_event,
					dispatch);
	} else {
736
		libinput_device_init_event_listener(&kbd->listener);
737 738 739 740 741 742 743
	}
}

static void
fallback_lid_toggle_keyboard_listeners(struct fallback_dispatch *dispatch,
				       bool is_closed)
{
744
	struct evdev_paired_keyboard *kbd;
745

746
	list_for_each(kbd, &dispatch->lid.paired_keyboard_list, link) {
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
		if (!kbd->device)
			continue;

		fallback_lid_toggle_keyboard_listener(dispatch,
						      kbd,
						      is_closed);
	}
}

static inline void
fallback_process_switch(struct fallback_dispatch *dispatch,
			struct evdev_device *device,
			struct input_event *e,
			uint64_t time)
{
	enum libinput_switch_state state;
	bool is_closed;

765 766
	/* TODO: this should to move to handle_state */

767 768 769 770
	switch (e->code) {
	case SW_LID:
		is_closed = !!e->value;

771 772
		fallback_lid_toggle_keyboard_listeners(dispatch, is_closed);

773 774 775 776 777 778 779
		if (dispatch->lid.is_closed == is_closed)
			return;

		dispatch->lid.is_closed = is_closed;
		fallback_lid_notify_toggle(dispatch, device, time);
		break;
	case SW_TABLET_MODE:
780
		if (dispatch->tablet_mode.sw.state == e->value)
781 782
			return;

783
		dispatch->tablet_mode.sw.state = e->value;
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
		if (e->value)
			state = LIBINPUT_SWITCH_STATE_ON;
		else
			state = LIBINPUT_SWITCH_STATE_OFF;
		switch_notify_toggle(&device->base,
				     time,
				     LIBINPUT_SWITCH_TABLET_MODE,
				     state);
		break;
	}
}

static inline bool
fallback_reject_relative(struct evdev_device *device,
			 const struct input_event *e,
			 uint64_t time)
{
	if ((e->code == REL_X || e->code == REL_Y) &&
	    (device->seat_caps & EVDEV_DEVICE_POINTER) == 0) {
		evdev_log_bug_libinput_ratelimit(device,
						 &device->nonpointer_rel_limit,
						 "REL_X/Y from a non-pointer device\n");
		return true;
	}

	return false;
}

static inline void
fallback_process_relative(struct fallback_dispatch *dispatch,
			  struct evdev_device *device,
			  struct input_event *e, uint64_t time)
{
	if (fallback_reject_relative(device, e, time))
		return;

	switch (e->code) {
	case REL_X:
		dispatch->rel.x += e->value;
823
		dispatch->pending_event |= EVDEV_RELATIVE_MOTION;
824 825 826
		break;
	case REL_Y:
		dispatch->rel.y += e->value;
827
		dispatch->pending_event |= EVDEV_RELATIVE_MOTION;
828 829
		break;
	case REL_WHEEL:
830 831
		dispatch->wheel.y += e->value;
		dispatch->pending_event |= EVDEV_WHEEL;
832 833
		break;
	case REL_HWHEEL:
834 835
		dispatch->wheel.x += e->value;
		dispatch->pending_event |= EVDEV_WHEEL;
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
		break;
	}
}

static inline void
fallback_process_absolute(struct fallback_dispatch *dispatch,
			  struct evdev_device *device,
			  struct input_event *e,
			  uint64_t time)
{
	if (device->is_mt) {
		fallback_process_touch(dispatch, device, e, time);
	} else {
		fallback_process_absolute_motion(dispatch, device, e);
	}
}

static inline bool
fallback_any_button_down(struct fallback_dispatch *dispatch,
		      struct evdev_device *device)
{
	unsigned int button;

	for (button = BTN_LEFT; button < BTN_JOYSTICK; button++) {
		if (libevdev_has_event_code(device->evdev, EV_KEY, button) &&
		    hw_is_key_down(dispatch, button))
			return true;
	}
	return false;
}

867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
static inline bool
fallback_arbitrate_touch(struct fallback_dispatch *dispatch,
			 struct mt_slot *slot)
{
	bool discard = false;

	if (dispatch->arbitration.state == ARBITRATION_IGNORE_RECT &&
	    point_in_rect(&slot->point, &dispatch->arbitration.rect)) {
		slot->palm_state = PALM_IS_PALM;
		discard = true;
	}

	return discard;
}

882 883 884 885 886 887 888 889 890 891 892 893 894
static inline bool
fallback_flush_mt_events(struct fallback_dispatch *dispatch,
			 struct evdev_device *device,
			 uint64_t time)
{
	bool sent = false;

	for (size_t i = 0; i < dispatch->mt.slots_len; i++) {
		struct mt_slot *slot = &dispatch->mt.slots[i];

		if (!slot->dirty)
			continue;

895 896 897 898
		slot->dirty = false;

		/* Any palm state other than PALM_NEW means we've either
		 * already cancelled the touch or the touch was never
Peter Hutterer's avatar
Peter Hutterer committed
899
		 * a finger anyway and we didn't send the begin.
900 901 902 903 904 905 906 907 908 909 910
		 */
		if (slot->palm_state == PALM_NEW) {
			if (slot->state != SLOT_STATE_BEGIN)
				sent = fallback_flush_mt_cancel(dispatch,
								device,
								i,
								time);
			slot->palm_state = PALM_IS_PALM;
		} else if (slot->palm_state == PALM_NONE) {
			switch (slot->state) {
			case SLOT_STATE_BEGIN:
911 912 913 914 915 916 917
				if (!fallback_arbitrate_touch(dispatch,
							     slot)) {
					sent = fallback_flush_mt_down(dispatch,
								      device,
								      i,
								      time);
				}
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
				break;
			case SLOT_STATE_UPDATE:
				sent = fallback_flush_mt_motion(dispatch,
								device,
								i,
								time);
				break;
			case SLOT_STATE_END:
				sent = fallback_flush_mt_up(dispatch,
							    device,
							    i,
							    time);
				break;
			case SLOT_STATE_NONE:
				break;
			}
		}

		/* State machine continues independent of the palm state */
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
		switch (slot->state) {
		case SLOT_STATE_BEGIN:
			slot->state = SLOT_STATE_UPDATE;
			break;
		case SLOT_STATE_UPDATE:
			break;
		case SLOT_STATE_END:
			slot->state = SLOT_STATE_NONE;
			break;
		case SLOT_STATE_NONE:
			/* touch arbitration may swallow the begin,
			 * so we may get updates for a touch still
			 * in NONE state */
			break;
		}
	}

	return sent;
}

957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
static void
fallback_handle_state(struct fallback_dispatch *dispatch,
		      struct evdev_device *device,
		      uint64_t time)
{
	bool need_touch_frame = false;

	/* Relative motion */
	if (dispatch->pending_event & EVDEV_RELATIVE_MOTION)
		fallback_flush_relative_motion(dispatch, device, time);

	/* Single touch or absolute pointer devices */
	if (dispatch->pending_event & EVDEV_ABSOLUTE_TOUCH_DOWN) {
		if (fallback_flush_st_down(dispatch, device, time))
			need_touch_frame = true;
972
	} else if (dispatch->pending_event & EVDEV_ABSOLUTE_MOTION) {
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
		if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
			if (fallback_flush_st_motion(dispatch,
						     device,
						     time))
				need_touch_frame = true;
		} else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
			fallback_flush_absolute_motion(dispatch,
						       device,
						       time);
		}
	}

	if (dispatch->pending_event & EVDEV_ABSOLUTE_TOUCH_UP) {
		if (fallback_flush_st_up(dispatch, device, time))
			need_touch_frame = true;
	}

	/* Multitouch devices */
991 992
	if (dispatch->pending_event & EVDEV_ABSOLUTE_MT)
		need_touch_frame = fallback_flush_mt_events(dispatch,
993 994 995 996 997 998 999 1000 1001 1002
							    device,
							    time);

	if (need_touch_frame)
		touch_notify_frame(&device->base, time);

	fallback_flush_wheels(dispatch, device, time);

	/* Buttons and keys */
	if (dispatch->pending_event & EVDEV_KEY) {
1003
		bool want_debounce = false;
1004 1005 1006 1007
		for (unsigned int code = 0; code <= KEY_MAX; code++) {
			if (!hw_key_has_changed(dispatch, code))
				continue;

1008
			if (get_key_type(code) == KEY_TYPE_BUTTON) {
1009
				want_debounce = true;
1010 1011 1012
				break;
			}
		}
1013 1014 1015 1016

		if (want_debounce)
			fallback_debounce_handle_state(dispatch, time);

1017 1018 1019 1020 1021 1022
		hw_key_update_last_state(dispatch);
	}

	dispatch->pending_event = EVDEV_NONE;
}

1023
static void
1024 1025 1026 1027
fallback_interface_process(struct evdev_dispatch *evdev_dispatch,
			   struct evdev_device *device,
			   struct input_event *event,
			   uint64_t time)
1028 1029 1030
{
	struct fallback_dispatch *dispatch = fallback_dispatch(evdev_dispatch);

1031
	if (dispatch->arbitration.in_arbitration)
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
		return;

	switch (event->type) {
	case EV_REL:
		fallback_process_relative(dispatch, device, event, time);
		break;
	case EV_ABS:
		fallback_process_absolute(dispatch, device, event, time);
		break;
	case EV_KEY:
		fallback_process_key(dispatch, device, event, time);
		break;
	case EV_SW:
		fallback_process_switch(dispatch, device, event, time);
		break;
	case EV_SYN:
1048
		fallback_handle_state(dispatch, device, time);
1049 1050 1051 1052 1053
		break;
	}
}

static void
1054 1055
cancel_touches(struct fallback_dispatch *dispatch,
	       struct evdev_device *device,
1056
	       const struct device_coord_rect *rect,
1057
	       uint64_t time)
1058 1059 1060 1061
{
	unsigned int idx;
	bool need_frame = false;

1062 1063 1064 1065
	if (!rect || point_in_rect(&dispatch->abs.point, rect))
		need_frame = fallback_flush_st_cancel(dispatch,
						      device,
						      time);
1066 1067 1068 1069 1070 1071 1072

	for (idx = 0; idx < dispatch->mt.slots_len; idx++) {
		struct mt_slot *slot = &dispatch->mt.slots[idx];

		if (slot->seat_slot == -1)
			continue;

1073 1074
		if ((!rect || point_in_rect(&slot->point, rect)) &&
		    fallback_flush_mt_cancel(dispatch, device, idx, time))
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
			need_frame = true;
	}

	if (need_frame)
		touch_notify_frame(&device->base, time);
}

static void
release_pressed_keys(struct fallback_dispatch *dispatch,
		     struct evdev_device *device,
		     uint64_t time)
{
	int code;

	for (code = 0; code < KEY_CNT; code++) {
		int count = get_key_down_count(device, code);

		if (count == 0)
			continue;

		if (count > 1) {
			evdev_log_bug_libinput(device,
					       "key %d is down %d times.\n",
					       code,
					       count);
		}

		switch (get_key_type(code)) {
		case KEY_TYPE_NONE:
			break;
		case KEY_TYPE_KEY:
			fallback_keyboard_notify_key(
				dispatch,
				device,
				time,
				code,
				LIBINPUT_KEY_STATE_RELEASED);
			break;
		case KEY_TYPE_BUTTON:
1114
			evdev_pointer_notify_button(
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
				device,
				time,
				evdev_to_left_handed(device, code),
				LIBINPUT_BUTTON_STATE_RELEASED);
			break;
		}

		count = get_key_down_count(device, code);
		if (count != 0) {
			evdev_log_bug_libinput(device,
					       "releasing key %d failed.\n",
					       code);
			break;
		}
	}
}

static void
fallback_return_to_neutral_state(struct fallback_dispatch *dispatch,
				 struct evdev_device *device)
{
	struct libinput *libinput = evdev_libinput_context(device);
	uint64_t time;

	if ((time = libinput_now(libinput)) == 0)
		return;

1142
	cancel_touches(dispatch, device, NULL, time);
1143 1144
	release_pressed_keys(dispatch, device, time);
	memset(dispatch->hw_key_mask, 0, sizeof(dispatch->hw_key_mask));
1145
	memset(dispatch->hw_key_mask, 0, sizeof(dispatch->last_hw_key_mask));
1146 1147 1148
}

static void
1149 1150
fallback_interface_suspend(struct evdev_dispatch *evdev_dispatch,
			   struct evdev_device *device)
1151 1152 1153 1154 1155 1156 1157
{
	struct fallback_dispatch *dispatch = fallback_dispatch(evdev_dispatch);

	fallback_return_to_neutral_state(dispatch, device);
}

static void
1158
fallback_interface_remove(struct evdev_dispatch *evdev_dispatch)
1159 1160
{
	struct fallback_dispatch *dispatch = fallback_dispatch(evdev_dispatch);
1161
	struct evdev_paired_keyboard *kbd, *tmp;
1162

1163 1164
	libinput_timer_cancel(&dispatch->debounce.timer);
	libinput_timer_cancel(&dispatch->debounce.timer_short);
1165
	libinput_timer_cancel(&dispatch->arbitration.arbitration_timer);
1166

1167 1168
	libinput_device_remove_event_listener(&dispatch->tablet_mode.other.listener);

1169 1170 1171 1172
	list_for_each_safe(kbd,
			   tmp,
			   &dispatch->lid.paired_keyboard_list,
			   link) {
1173
		evdev_paired_keyboard_destroy(kbd);
1174 1175 1176 1177
	}
}

static void
1178 1179
fallback_interface_sync_initial_state(struct evdev_device *device,
				      struct evdev_dispatch *evdev_dispatch)
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
{
	struct fallback_dispatch *dispatch = fallback_dispatch(evdev_dispatch);
	uint64_t time = libinput_now(evdev_libinput_context(device));

	if (device->tags & EVDEV_TAG_LID_SWITCH) {
		struct libevdev *evdev = device->evdev;

		dispatch->lid.is_closed = libevdev_get_event_value(evdev,
								   EV_SW,
								   SW_LID);
		dispatch->lid.is_closed_client_state = false;

		/* For the initial state sync, we depend on whether the lid switch
		 * is reliable. If we know it's reliable, we sync as expected.
		 * If we're not sure, we ignore the initial state and only sync on
		 * the first future lid close event. Laptops with a broken switch
		 * that always have the switch in 'on' state thus don't mess up our
		 * touchpad.
		 */
		if (dispatch->lid.is_closed &&
		    dispatch->lid.reliability == RELIABILITY_RELIABLE) {
			fallback_lid_notify_toggle(dispatch, device, time);
		}
	}

1205
	if (dispatch->tablet_mode.sw.state) {
1206 1207 1208 1209 1210 1211 1212
		switch_notify_toggle(&device->base,
				     time,
				     LIBINPUT_SWITCH_TABLET_MODE,
				     LIBINPUT_SWITCH_STATE_ON);
	}
}

1213 1214 1215 1216 1217 1218 1219
static void
fallback_interface_update_rect(struct evdev_dispatch *evdev_dispatch,
			       struct evdev_device *device,
				const struct phys_rect *phys_rect,
				uint64_t time)
{
	struct fallback_dispatch *dispatch = fallback_dispatch(evdev_dispatch);
1220
	struct device_coord_rect rect;
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230

	assert(phys_rect);

	/* Existing touches do not change, we just update the rect and only
	 * new touches in these areas will be ignored. If you want to paint
	 * over your finger, be my guest. */
	rect = evdev_phys_rect_to_units(device, phys_rect);
	dispatch->arbitration.rect = rect;
}

1231
static void
1232 1233
fallback_interface_toggle_touch(struct evdev_dispatch *evdev_dispatch,
				struct evdev_device *device,
1234
				enum evdev_arbitration_state which,
1235
				const struct phys_rect *phys_rect,
1236
				uint64_t time)
1237 1238
{
	struct fallback_dispatch *dispatch = fallback_dispatch(evdev_dispatch);
1239
	struct device_coord_rect rect = {0};
1240

1241
	if (which == dispatch->arbitration.state)
1242 1243
		return;

1244 1245
	switch (which) {
	case ARBITRATION_NOT_ACTIVE:
1246 1247 1248 1249 1250 1251 1252 1253 1254
		/* if in-kernel arbitration is in use and there is a touch
		 * and a pen in proximity, lifting the pen out of proximity
		 * causes a touch begin for the touch. On a hand-lift the
		 * proximity out precedes the touch up by a few ms, so we
		 * get what looks like a tap. Fix this by delaying
		 * arbitration by just a little bit so that any touch in
		 * event is caught as palm touch. */
		libinput_timer_set(&dispatch->arbitration.arbitration_timer,
				   time + ms2us(90));
1255
		break;
1256 1257 1258 1259 1260 1261
	case ARBITRATION_IGNORE_RECT:
		assert(phys_rect);
		rect = evdev_phys_rect_to_units(device, phys_rect);
		cancel_touches(dispatch, device, &rect, time);
		dispatch->arbitration.rect = rect;
		break;
1262 1263 1264 1265 1266
	case ARBITRATION_IGNORE_ALL:
		libinput_timer_cancel(&dispatch->arbitration.arbitration_timer);
		fallback_return_to_neutral_state(dispatch, device);
		dispatch->arbitration.in_arbitration = true;
		break;
1267
	}
1268

1269
	dispatch->arbitration.state = which;
1270 1271 1272
}

static void
1273
fallback_interface_destroy(struct evdev_dispatch *evdev_dispatch)
1274 1275 1276
{
	struct fallback_dispatch *dispatch = fallback_dispatch(evdev_dispatch);

1277
	libinput_timer_destroy(&dispatch->arbitration.arbitration_timer);
1278
	libinput_timer_destroy(&dispatch->debounce.timer);
1279
	libinput_timer_destroy(&dispatch->debounce.timer_short);
1280

1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
	free(dispatch->mt.slots);
	free(dispatch);
}

static void
fallback_lid_pair_keyboard(struct evdev_device *lid_switch,
			   struct evdev_device *keyboard)
{
	struct fallback_dispatch *dispatch =
		fallback_dispatch(lid_switch->dispatch);
1291
	struct evdev_paired_keyboard *kbd;
1292
	size_t count = 0;
1293 1294 1295 1296 1297 1298 1299 1300

	if ((keyboard->tags & EVDEV_TAG_KEYBOARD) == 0 ||
	    (lid_switch->tags & EVDEV_TAG_LID_SWITCH) == 0)
		return;

	if ((keyboard->tags & EVDEV_TAG_INTERNAL_KEYBOARD) == 0)
		return;

1301 1302 1303 1304 1305 1306 1307
	list_for_each(kbd, &dispatch->lid.paired_keyboard_list, link) {
		count++;
		if (count > 3) {
			evdev_log_info(lid_switch,
				       "lid: too many internal keyboards\n");
			break;
		}
1308 1309
	}

1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
	kbd = zalloc(sizeof(*kbd));
	kbd->device = keyboard;
	libinput_device_init_event_listener(&kbd->listener);
	list_insert(&dispatch->lid.paired_keyboard_list, &kbd->link);
	evdev_log_debug(lid_switch,
			"lid: keyboard paired with %s<->%s\n",
			lid_switch->devname,
			keyboard->devname);

	/* We need to init the event listener now only if the
	 * reported state is closed. */
	if (dispatch->lid.is_closed)
		fallback_lid_toggle_keyboard_listener(dispatch,
						      kbd,
						      dispatch->lid.is_closed);
1325 1326
}

1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
static void
fallback_resume(struct fallback_dispatch *dispatch,
		struct evdev_device *device)
{
	if (dispatch->base.sendevents.current_mode ==
	    LIBINPUT_CONFIG_SEND_EVENTS_DISABLED)
		return;

	evdev_device_resume(device);
}

static void
fallback_suspend(struct fallback_dispatch *dispatch,
		 struct evdev_device *device)
{
	evdev_device_suspend(device);
}

static void
fallback_tablet_mode_switch_event(uint64_t time,
				  struct libinput_event *event,
				  void *data)
{
	struct fallback_dispatch *dispatch = data;
	struct evdev_device *device = dispatch->device;
	struct libinput_event_switch *swev;

	if (libinput_event_get_type(event) != LIBINPUT_EVENT_SWITCH_TOGGLE)
		return;

	swev = libinput_event_get_switch_event(event);
	if (libinput_event_switch_get_switch(swev) !=
	    LIBINPUT_SWITCH_TABLET_MODE)
		return;

	switch (libinput_event_switch_get_switch_state(swev)) {
	case LIBINPUT_SWITCH_STATE_OFF:
		fallback_resume(dispatch, device);
		evdev_log_debug(device, "tablet-mode: resuming device\n");
		break;
	case LIBINPUT_SWITCH_STATE_ON:
		fallback_suspend(dispatch, device);
		evdev_log_debug(device, "tablet-mode: suspending device\n");
		break;
	}
}

static void
1375 1376
fallback_pair_tablet_mode(struct evdev_device *keyboard,
			  struct evdev_device *tablet_mode_switch)
1377 1378 1379 1380
{
	struct fallback_dispatch *dispatch =
		fallback_dispatch(keyboard->dispatch);

1381 1382 1383
	if ((keyboard->tags & EVDEV_TAG_EXTERNAL_KEYBOARD))
		return;

1384 1385 1386 1387 1388 1389
	if ((keyboard->tags & EVDEV_TAG_TRACKPOINT)) {
		if (keyboard->tags & EVDEV_TAG_EXTERNAL_MOUSE)
			return;
	/* This filters out all internal keyboard-like devices (Video
	 * Switch) */
	} else if ((keyboard->tags & EVDEV_TAG_INTERNAL_KEYBOARD) == 0)
1390 1391
		return;

1392 1393
	if (evdev_device_has_model_quirk(keyboard,
					 QUIRK_MODEL_TABLET_MODE_NO_SUSPEND))
1394 1395
		return;

1396 1397 1398 1399 1400 1401 1402
	if ((tablet_mode_switch->tags & EVDEV_TAG_TABLET_MODE_SWITCH) == 0)
		return;

	if (dispatch->tablet_mode.other.sw_device)
		return;

	evdev_log_debug(keyboard,
1403
			"tablet-mode: paired %s<->%s\n",
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
			keyboard->devname,
			tablet_mode_switch->devname);

	libinput_device_add_event_listener(&tablet_mode_switch->base,
				&dispatch->tablet_mode.other.listener,
				fallback_tablet_mode_switch_event,
				dispatch);
	dispatch->tablet_mode.other.sw_device = tablet_mode_switch;

	if (evdev_device_switch_get_state(tablet_mode_switch,
					  LIBINPUT_SWITCH_TABLET_MODE)
1415 1416
		    == LIBINPUT_SWITCH_STATE_ON) {
		evdev_log_debug(keyboard, "tablet-mode: suspending device\n");
1417
		fallback_suspend(dispatch, keyboard);
1418
	}
1419 1420
}

1421 1422 1423 1424 1425
static void
fallback_interface_device_added(struct evdev_device *device,
				struct evdev_device *added_device)
{
	fallback_lid_pair_keyboard(device, added_device);
1426
	fallback_pair_tablet_mode(device, added_device);
1427 1428 1429 1430 1431 1432 1433 1434
}

static void
fallback_interface_device_removed(struct evdev_device *device,
				  struct evdev_device *removed_device)
{
	struct fallback_dispatch *dispatch =
			fallback_dispatch(device->dispatch);
1435
	struct evdev_paired_keyboard *kbd, *tmp;
1436

1437 1438 1439 1440
	list_for_each_safe(kbd,
			   tmp,
			   &dispatch->lid.paired_keyboard_list,
			   link) {
1441 1442 1443 1444 1445 1446
		if (!kbd->device)
			continue;

		if (kbd->device != removed_device)
			continue;

1447
		evdev_paired_keyboard_destroy(kbd);
1448
	}
1449 1450 1451 1452 1453 1454 1455 1456

	if (removed_device == dispatch->tablet_mode.other.sw_device) {
		libinput_device_remove_event_listener(
				&dispatch->tablet_mode.other.listener);
		libinput_device_init_event_listener(
				&dispatch->tablet_mode.other.listener);
		dispatch->tablet_mode.other.sw_device = NULL;
	}
1457 1458 1459
}

struct evdev_dispatch_interface fallback_interface = {
1460 1461 1462 1463
	.process = fallback_interface_process,
	.suspend = fallback_interface_suspend,
	.remove = fallback_interface_remove,
	.destroy = fallback_interface_destroy,
1464 1465 1466 1467
	.device_added = fallback_interface_device_added,
	.device_removed = fallback_interface_device_removed,
	.device_suspended = fallback_interface_device_removed, /* treat as remove */
	.device_resumed = fallback_interface_device_added,   /* treat as add */
1468
	.post_added = fallback_interface_sync_initial_state,
1469
	.touch_arbitration_toggle = fallback_interface_toggle_touch,
1470
	.touch_arbitration_update_rect = fallback_interface_update_rect,
1471
	.get_switch_state = fallback_interface_get_switch_state,
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
};

static void
fallback_change_to_left_handed(struct evdev_device *device)
{
	struct fallback_dispatch *dispatch = fallback_dispatch(device->dispatch);

	if (device->left_handed.want_enabled == device->left_handed.enabled)
		return;

	if (fallback_any_button_down(dispatch, device))
		return;

	device->left_handed.enabled = device->left_handed.want_enabled;
}

static void
fallback_change_scroll_method(struct evdev_device *device)
{
	struct fallback_dispatch *dispatch = fallback_dispatch(device->dispatch);

	if (device->scroll.want_method == device->scroll.method &&
1494 1495
	    device->scroll.want_button == device->scroll.button &&
	    device->scroll.want_lock_enabled == device->scroll.lock_enabled)
1496 1497 1498 1499 1500 1501 1502
		return;

	if (fallback_any_button_down(dispatch, device))
		return;

	device->scroll.method = device->scroll.want_method;
	device->scroll.button = device->scroll.want_button;
1503 1504
	device->scroll.lock_enabled = device->scroll.want_lock_enabled;
	evdev_set_button_scroll_lock_enabled(device, device->scroll.lock_enabled);
1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
}

static int
fallback_rotation_config_is_available(struct libinput_device *device)
{
	/* This function only gets called when we support rotation */
	return 1;
}

static enum libinput_config_status
fallback_rotation_config_set_angle(struct libinput_device *libinput_device,
				unsigned int degrees_cw)
{
	struct evdev_device *device = evdev_device(libinput_device);
	struct fallback_dispatch *dispatch = fallback_dispatch(device->dispatch);

	dispatch->rotation.angle = degrees_cw;
	matrix_init_rotate(&dispatch->rotation.matrix, degrees_cw);

	return LIBINPUT_CONFIG_STATUS_SUCCESS;
}

static unsigned int
fallback_rotation_config_get_angle(struct libinput_device *libinput_device)
{
	struct evdev_device *device = evdev_device(libinput_device);
	struct fallback_dispatch *dispatch = fallback_dispatch(device->dispatch);

	return dispatch->rotation.angle;
}

static unsigned int
fallback_rotation_config_get_default_angle(struct libinput_device *device)
{
	return 0;
}

static void
fallback_init_rotation(struct fallback_dispatch *dispatch,
		       struct evdev_device *device)
{
	if ((device->model_flags & EVDEV_MODEL_TRACKBALL) == 0)
		return;

	dispatch->rotation.config.is_available = fallback_rotation_config_is_available;
	dispatch->rotation.config.set_angle = fallback_rotation_config_set_angle;
	dispatch->rotation.config.get_angle = fallback_rotation_config_get_angle;
	dispatch->rotation.config.get_default_angle = fallback_rotation_config_get_default_angle;
	dispatch->rotation.is_enabled = false;
	matrix_init_identity(&dispatch->rotation.matrix);
	device->base.config.rotation = &dispatch->rotation.config;
}

static inline int
fallback_dispatch_init_slots(struct fallback_dispatch *dispatch,
			     struct evdev_device *device)
{
	struct libevdev *evdev = device->evdev;
	struct mt_slot *slots;
	int num_slots;
	int active_slot;
	int slot;

	if (evdev_is_fake_mt_device(device) ||
	    !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) ||
	    !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y))
		 return 0;

	/* We only handle the slotted Protocol B in libinput.
	   Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
	   require mtdev for conversion. */
	if (evdev_need_mtdev(device)) {
		device->mtdev = mtdev_new_open(device->fd);
		if (!device->mtdev)
			return -1;

		/* pick 10 slots as default for type A
		   devices. */
		num_slots = 10;
		active_slot = device->mtdev->caps.slot.value;
	} else {
		num_slots = libevdev_get_num_slots(device->evdev);
		active_slot = libevdev_get_current_slot(evdev);
	}

	slots = zalloc(num_slots * sizeof(struct mt_slot));

	for (slot = 0; slot < num_slots; ++slot) {
		slots[slot].seat_slot = -1;

		if (evdev_need_mtdev(device))
			continue;

		slots[slot].point.x = libevdev_get_slot_value(evdev,
							      slot,
							      ABS_MT_POSITION_X);
		slots[slot].point.y