evdev.h 28.5 KB
Newer Older
1
/*
2
 * Copyright © 2011, 2012 Intel Corporation
3
 * Copyright © 2013 Jonas Ådahl
Peter Hutterer's avatar
Peter Hutterer committed
4
 * Copyright © 2013-2015 Red Hat, Inc.
5
 *
6 7 8 9 10 11
 * 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:
12
 *
13 14 15 16 17 18 19 20 21 22 23
 * 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.
24 25
 */

26 27 28
#ifndef EVDEV_H
#define EVDEV_H

29 30
#include "config.h"

31
#include <stdbool.h>
32
#include <stdarg.h>
33
#include "linux/input.h"
Peter Hutterer's avatar
Peter Hutterer committed
34
#include <libevdev/libevdev.h>
35 36

#include "libinput-private.h"
37
#include "timer.h"
38
#include "filter.h"
39
#include "quirks.h"
40

41 42
/* The fake resolution value for abs devices without resolution */
#define EVDEV_FAKE_RESOLUTION 1
43

44
enum evdev_event_type {
45
	EVDEV_NONE,
Peter Hutterer's avatar
Peter Hutterer committed
46 47 48 49 50 51 52 53
	EVDEV_ABSOLUTE_TOUCH_DOWN	= bit(0),
	EVDEV_ABSOLUTE_MOTION		= bit(1),
	EVDEV_ABSOLUTE_TOUCH_UP		= bit(2),
	EVDEV_ABSOLUTE_MT		= bit(3),
	EVDEV_WHEEL			= bit(4),
	EVDEV_KEY			= bit(5),
	EVDEV_RELATIVE_MOTION		= bit(6),
	EVDEV_BUTTON			= bit(7),
54 55
};

56
enum evdev_device_seat_capability {
Peter Hutterer's avatar
Peter Hutterer committed
57 58 59 60 61 62 63
	EVDEV_DEVICE_POINTER		= bit(0),
	EVDEV_DEVICE_KEYBOARD		= bit(1),
	EVDEV_DEVICE_TOUCH		= bit(2),
	EVDEV_DEVICE_TABLET		= bit(3),
	EVDEV_DEVICE_TABLET_PAD		= bit(4),
	EVDEV_DEVICE_GESTURE		= bit(5),
	EVDEV_DEVICE_SWITCH		= bit(6),
64 65
};

66
enum evdev_device_tags {
Peter Hutterer's avatar
Peter Hutterer committed
67 68 69 70 71 72 73 74 75 76
	EVDEV_TAG_EXTERNAL_MOUSE	= bit(0),
	EVDEV_TAG_INTERNAL_TOUCHPAD	= bit(1),
	EVDEV_TAG_EXTERNAL_TOUCHPAD	= bit(2),
	EVDEV_TAG_TRACKPOINT		= bit(3),
	EVDEV_TAG_KEYBOARD		= bit(4),
	EVDEV_TAG_LID_SWITCH		= bit(5),
	EVDEV_TAG_INTERNAL_KEYBOARD	= bit(6),
	EVDEV_TAG_EXTERNAL_KEYBOARD	= bit(7),
	EVDEV_TAG_TABLET_MODE_SWITCH	= bit(8),
	EVDEV_TAG_TABLET_TOUCHPAD	= bit(9),
77 78
};

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
enum evdev_middlebutton_state {
	MIDDLEBUTTON_IDLE,
	MIDDLEBUTTON_LEFT_DOWN,
	MIDDLEBUTTON_RIGHT_DOWN,
	MIDDLEBUTTON_MIDDLE,
	MIDDLEBUTTON_LEFT_UP_PENDING,
	MIDDLEBUTTON_RIGHT_UP_PENDING,
	MIDDLEBUTTON_IGNORE_LR,
	MIDDLEBUTTON_IGNORE_L,
	MIDDLEBUTTON_IGNORE_R,
	MIDDLEBUTTON_PASSTHROUGH,
};

enum evdev_middlebutton_event {
	MIDDLEBUTTON_EVENT_L_DOWN,
	MIDDLEBUTTON_EVENT_R_DOWN,
	MIDDLEBUTTON_EVENT_OTHER,
	MIDDLEBUTTON_EVENT_L_UP,
	MIDDLEBUTTON_EVENT_R_UP,
	MIDDLEBUTTON_EVENT_TIMEOUT,
	MIDDLEBUTTON_EVENT_ALL_UP,
};

102 103 104 105 106
/**
 * model flags are used as shortcut for quirks that need to be checked
 * multiple times in timing-sensitive paths. For quirks that need to be
 * checked only once, use the quirk directly.
 */
107
enum evdev_device_model {
108
	EVDEV_MODEL_DEFAULT = 0,
Peter Hutterer's avatar
Peter Hutterer committed
109 110
	EVDEV_MODEL_WACOM_TOUCHPAD		= bit(1),
	EVDEV_MODEL_SYNAPTICS_SERIAL_TOUCHPAD	= bit(2),
111
	EVDEV_MODEL_ALPS_SERIAL_TOUCHPAD	= bit(3),
Peter Hutterer's avatar
Peter Hutterer committed
112 113 114
	EVDEV_MODEL_LENOVO_T450_TOUCHPAD	= bit(4),
	EVDEV_MODEL_APPLE_TOUCHPAD_ONEBUTTON	= bit(5),
	EVDEV_MODEL_LENOVO_SCROLLPOINT		= bit(6),
115 116

	/* udev tags, not true quirks */
Peter Hutterer's avatar
Peter Hutterer committed
117 118 119
	EVDEV_MODEL_TEST_DEVICE			= bit(20),
	EVDEV_MODEL_TRACKBALL			= bit(21),
	EVDEV_MODEL_LENOVO_X220_TOUCHPAD_FW81	= bit(22),
120 121
};

122 123 124
enum evdev_button_scroll_state {
	BUTTONSCROLL_IDLE,
	BUTTONSCROLL_BUTTON_DOWN,	/* button is down */
125 126
	BUTTONSCROLL_READY,		/* ready for scroll events */
	BUTTONSCROLL_SCROLLING,		/* have sent scroll events */
127 128
};

129 130 131 132 133 134 135 136
enum evdev_button_scroll_lock_state {
	BUTTONSCROLL_LOCK_DISABLED,
	BUTTONSCROLL_LOCK_IDLE,
	BUTTONSCROLL_LOCK_FIRSTDOWN,
	BUTTONSCROLL_LOCK_FIRSTUP,
	BUTTONSCROLL_LOCK_SECONDDOWN,
};

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
enum evdev_debounce_state {
	/**
	 * Initial state, no debounce but monitoring events
	 */
	DEBOUNCE_INIT,
	/**
	 * Bounce detected, future events need debouncing
	 */
	DEBOUNCE_NEEDED,
	/**
	 * Debounce is enabled, but no event is currently being filtered
	 */
	DEBOUNCE_ON,
	/**
	 * Debounce is enabled and we are currently filtering an event
	 */
	DEBOUNCE_ACTIVE,
};

156 157 158
enum evdev_arbitration_state {
	ARBITRATION_NOT_ACTIVE,
	ARBITRATION_IGNORE_ALL,
159
	ARBITRATION_IGNORE_RECT,
160 161
};

162
struct evdev_device {
163 164
	struct libinput_device base;

165 166
	struct libinput_source *source;

167
	struct evdev_dispatch *dispatch;
Peter Hutterer's avatar
Peter Hutterer committed
168
	struct libevdev *evdev;
169
	struct udev_device *udev_device;
170
	char *output_name;
Peter Hutterer's avatar
Peter Hutterer committed
171
	const char *devname;
172
	bool was_removed;
173
	int fd;
174 175
	enum evdev_device_seat_capability seat_caps;
	enum evdev_device_tags tags;
176 177
	bool is_mt;
	bool is_suspended;
178
	int dpi; /* HW resolution */
179
	double trackpoint_multiplier; /* trackpoint constant multiplier */
180
	bool use_velocity_averaging; /* whether averaging should be applied on velocity calculation */
181
	struct ratelimit syn_drop_limit; /* ratelimit for SYN_DROPPED logging */
182
	struct ratelimit delay_warning_limit; /* ratelimit for delayd processing logging */
183 184 185 186
	struct ratelimit nonpointer_rel_limit; /* ratelimit for REL_* events from non-pointer devices */
	uint32_t model_flags;
	struct mtdev *mtdev;

187
	struct {
188
		const struct input_absinfo *absinfo_x, *absinfo_y;
189
		bool is_fake_resolution;
190 191

		int apply_calibration;
192
		struct matrix calibration;
193
		struct matrix default_calibration; /* from LIBINPUT_CALIBRATION_MATRIX */
194
		struct matrix usermatrix; /* as supplied by the caller */
195 196

		struct device_coords dimensions;
197 198 199 200 201

		struct {
			struct device_coords min, max;
			struct ratelimit range_warn_limit;
		} warning_range;
202 203
	} abs;

204
	struct {
205
		struct libinput_timer timer;
206 207 208
		struct libinput_device_config_scroll_method config;
		/* Currently enabled method, button */
		enum libinput_config_scroll_method method;
209
		uint32_t button;
210 211
		uint64_t button_down_time;

212 213
		/* set during device init, used at runtime to delay changes
		 * until all buttons are up */
214
		enum libinput_config_scroll_method want_method;
215 216
		uint32_t want_button;
		/* Checks if buttons are down and commits the setting */
217
		void (*change_scroll_method)(struct evdev_device *device);
218
		enum evdev_button_scroll_state button_scroll_state;
219
		double threshold;
220
		double direction_lock_threshold;
221
		uint32_t direction;
222
		struct normalized_coords buildup;
223 224

		struct libinput_device_config_natural_scroll config_natural;
225 226
		/* set during device init if we want natural scrolling,
		 * used at runtime to enable/disable the feature */
227
		bool natural_scrolling_enabled;
228

229 230 231 232
		/* set during device init to invert direction of
		 * horizontal scrolling */
		bool invert_horizontal_scrolling;

233
		/* angle per REL_WHEEL click in degrees */
234
		struct wheel_angle wheel_click_angle;
235

236 237 238
		enum evdev_button_scroll_lock_state lock_state;
		bool want_lock_enabled;
		bool lock_enabled;
239 240
	} scroll;

241
	struct {
242
		struct libinput_device_config_accel config;
243 244
		struct motion_filter *filter;
	} pointer;
245

246 247 248
	/* Key counter used for multiplexing button events internally in
	 * libinput. */
	uint8_t key_count[KEY_CNT];
249 250

	struct {
251
		struct libinput_device_config_left_handed config;
252
		/* left-handed currently enabled */
253
		bool enabled;
254 255
		/* set during device init if we want left_handed config,
		 * used at runtime to delay the effect until buttons are up */
256
		bool want_enabled;
257
		/* Checks if buttons are down and commits the setting */
258 259
		void (*change_to_enabled)(struct evdev_device *device);
	} left_handed;
260

261 262 263 264 265 266 267 268 269 270 271
	struct {
		struct libinput_device_config_middle_emulation config;
		/* middle-button emulation enabled */
		bool enabled;
		bool enabled_default;
		bool want_enabled;
		enum evdev_middlebutton_state state;
		struct libinput_timer timer;
		uint32_t button_mask;
		uint64_t first_event_time;
	} middlebutton;
272 273
};

274 275 276
static inline struct evdev_device *
evdev_device(struct libinput_device *device)
{
277
	return container_of(device, struct evdev_device, base);
278 279
}

280 281
#define EVDEV_UNHANDLED_DEVICE ((struct evdev_device *) 1)

282 283 284 285 286
struct evdev_dispatch;

struct evdev_dispatch_interface {
	/* Process an evdev input event. */
	void (*process)(struct evdev_dispatch *dispatch,
287
			struct evdev_device *device,
288
			struct input_event *event,
289
			uint64_t time);
290

291 292 293 294
	/* Device is being suspended */
	void (*suspend)(struct evdev_dispatch *dispatch,
			struct evdev_device *device);

295 296 297
	/* Device is being removed (may be NULL) */
	void (*remove)(struct evdev_dispatch *dispatch);

298 299
	/* Destroy an event dispatch handler and free all its resources. */
	void (*destroy)(struct evdev_dispatch *dispatch);
300 301 302 303 304 305 306 307

	/* A new device was added */
	void (*device_added)(struct evdev_device *device,
			     struct evdev_device *added_device);

	/* A device was removed */
	void (*device_removed)(struct evdev_device *device,
			       struct evdev_device *removed_device);
308

309 310 311 312 313 314 315 316
	/* A device was suspended */
	void (*device_suspended)(struct evdev_device *device,
				 struct evdev_device *suspended_device);

	/* A device was resumed */
	void (*device_resumed)(struct evdev_device *device,
			       struct evdev_device *resumed_device);

317 318 319 320
	/* Called immediately after the LIBINPUT_EVENT_DEVICE_ADDED event
	 * was sent */
	void (*post_added)(struct evdev_device *device,
			   struct evdev_dispatch *dispatch);
Peter Hutterer's avatar
Peter Hutterer committed
321

322
	/* For touch arbitration, called on the device that should
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
	 * enable/disable touch capabilities.
	 */
	void (*touch_arbitration_toggle)(struct evdev_dispatch *dispatch,
					 struct evdev_device *device,
					 enum evdev_arbitration_state which,
					 const struct phys_rect *rect, /* may be NULL */
					 uint64_t now);

	/* Called when touch arbitration is on, updates the area where touch
	 * arbitration should apply.
	 */
	void (*touch_arbitration_update_rect)(struct evdev_dispatch *dispatch,
					      struct evdev_device *device,
					      const struct phys_rect *rect,
					      uint64_t now);

339 340 341 342 343

	/* Return the state of the given switch */
	enum libinput_switch_state
		(*get_switch_state)(struct evdev_dispatch *dispatch,
				    enum libinput_switch which);
344 345 346 347

	void (*left_handed_toggle)(struct evdev_dispatch *dispatch,
				   struct evdev_device *device,
				   bool left_handed_enabled);
348 349
};

350 351 352 353 354
enum evdev_dispatch_type {
	DISPATCH_FALLBACK,
	DISPATCH_TOUCHPAD,
	DISPATCH_TABLET,
	DISPATCH_TABLET_PAD,
355
	DISPATCH_TOTEM,
356 357
};

358
struct evdev_dispatch {
359
	enum evdev_dispatch_type dispatch_type;
360
	struct evdev_dispatch_interface *interface;
361 362 363 364 365 366 367

	struct {
		struct libinput_device_config_send_events config;
		enum libinput_config_send_events_mode current_mode;
	} sendevents;
};

368 369 370 371 372 373 374 375
static inline void
evdev_verify_dispatch_type(struct evdev_dispatch *dispatch,
			   enum evdev_dispatch_type type)
{
	if (dispatch->dispatch_type != type)
		abort();
}

376 377
struct evdev_device *
evdev_device_create(struct libinput_seat *seat,
378
		    struct udev_device *device);
379

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
static inline struct libinput *
evdev_libinput_context(const struct evdev_device *device)
{
	return device->base.seat->libinput;
}

static inline bool
evdev_device_has_model_quirk(struct evdev_device *device,
			     enum quirk model_quirk)
{
	struct quirks_context *quirks;
	struct quirks *q;
	bool result = false;

	assert(quirk_get_name(model_quirk) != NULL);

	quirks = evdev_libinput_context(device)->quirks;
	q = quirks_fetch_for_device(quirks, device->udev_device);
	quirks_get_bool(q, model_quirk, &result);
	quirks_unref(q);

	return result;
}

404 405
void
evdev_transform_absolute(struct evdev_device *device,
406 407 408 409
			 struct device_coords *point);

void
evdev_transform_relative(struct evdev_device *device,
410 411
			 struct device_coords *point);

412 413
void
evdev_init_calibration(struct evdev_device *device,
414
		        struct libinput_device_config_calibration *calibration);
415

416 417 418
void
evdev_read_calibration_prop(struct evdev_device *device);

419 420 421
int
evdev_read_fuzz_prop(struct evdev_device *device, unsigned int code);

422 423 424
enum switch_reliability
evdev_read_switch_reliability_prop(struct evdev_device *device);

425 426 427 428
void
evdev_init_sendevents(struct evdev_device *device,
		      struct evdev_dispatch *dispatch);

429
void
430
evdev_device_init_pointer_acceleration(struct evdev_device *device,
431
				       struct motion_filter *filter);
432

433
struct evdev_dispatch *
434
evdev_touchpad_create(struct evdev_device *device);
435

436 437 438
struct evdev_dispatch *
evdev_mt_touchpad_create(struct evdev_device *device);

439 440 441
struct evdev_dispatch *
evdev_tablet_create(struct evdev_device *device);

442 443 444
struct evdev_dispatch *
evdev_tablet_pad_create(struct evdev_device *device);

445 446 447
struct evdev_dispatch *
evdev_lid_switch_dispatch_create(struct evdev_device *device);

448 449 450
struct evdev_dispatch *
fallback_dispatch_create(struct libinput_device *libinput_device);

451 452 453
struct evdev_dispatch *
evdev_totem_create(struct evdev_device *device);

454 455 456 457 458 459
bool
evdev_is_fake_mt_device(struct evdev_device *device);

int
evdev_need_mtdev(struct evdev_device *device);

460
void
461
evdev_device_led_update(struct evdev_device *device, enum libinput_led leds);
462

463 464
int
evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size);
465

466 467 468
const char *
evdev_device_get_output(struct evdev_device *device);

469 470 471
const char *
evdev_device_get_sysname(struct evdev_device *device);

472 473 474 475 476 477 478 479 480
const char *
evdev_device_get_name(struct evdev_device *device);

unsigned int
evdev_device_get_id_product(struct evdev_device *device);

unsigned int
evdev_device_get_id_vendor(struct evdev_device *device);

481 482 483
struct udev_device *
evdev_device_get_udev_device(struct evdev_device *device);

484
void
485 486
evdev_device_set_default_calibration(struct evdev_device *device,
				     const float calibration[6]);
487
void
488 489
evdev_device_calibrate(struct evdev_device *device,
		       const float calibration[6]);
490

491
bool
492 493 494
evdev_device_has_capability(struct evdev_device *device,
			    enum libinput_device_capability capability);

495
int
496
evdev_device_get_size(const struct evdev_device *device,
497 498 499
		      double *w,
		      double *h);

500 501 502
int
evdev_device_has_button(struct evdev_device *device, uint32_t code);

503 504 505
int
evdev_device_has_key(struct evdev_device *device, uint32_t code);

506 507 508
int
evdev_device_get_touch_count(struct evdev_device *device);

509 510 511 512
int
evdev_device_has_switch(struct evdev_device *device,
			enum libinput_switch sw);

513 514 515 516
int
evdev_device_tablet_pad_has_key(struct evdev_device *device,
				uint32_t code);

517 518 519 520 521 522 523 524 525
int
evdev_device_tablet_pad_get_num_buttons(struct evdev_device *device);

int
evdev_device_tablet_pad_get_num_rings(struct evdev_device *device);

int
evdev_device_tablet_pad_get_num_strips(struct evdev_device *device);

526 527 528 529 530 531 532
int
evdev_device_tablet_pad_get_num_mode_groups(struct evdev_device *device);

struct libinput_tablet_pad_mode_group *
evdev_device_tablet_pad_get_mode_group(struct evdev_device *device,
				       unsigned int index);

533 534 535 536
enum libinput_switch_state
evdev_device_switch_get_state(struct evdev_device *device,
			      enum libinput_switch sw);

537
double
538
evdev_device_transform_x(struct evdev_device *device,
539
			 double x,
540 541
			 uint32_t width);

542
double
543
evdev_device_transform_y(struct evdev_device *device,
544
			 double y,
545
			 uint32_t height);
546
void
547
evdev_device_suspend(struct evdev_device *device);
548

549 550 551
int
evdev_device_resume(struct evdev_device *device);

552 553 554 555 556 557
void
evdev_notify_suspended_device(struct evdev_device *device);

void
evdev_notify_resumed_device(struct evdev_device *device);

558 559
void
evdev_pointer_notify_button(struct evdev_device *device,
560
			    uint64_t time,
561
			    unsigned int button,
562
			    enum libinput_button_state state);
563 564
void
evdev_pointer_notify_physical_button(struct evdev_device *device,
565
				     uint64_t time,
566 567
				     int button,
				     enum libinput_button_state state);
568

569 570 571
void
evdev_init_natural_scroll(struct evdev_device *device);

572 573 574 575
void
evdev_init_button_scroll(struct evdev_device *device,
			 void (*change_scroll_method)(struct evdev_device *));

576 577 578 579
void
evdev_set_button_scroll_lock_enabled(struct evdev_device *device,
				     bool enabled);

580 581 582 583 584
int
evdev_update_key_down_count(struct evdev_device *device,
			    int code,
			    int pressed);

585 586 587 588 589 590 591
void
evdev_notify_axis(struct evdev_device *device,
		  uint64_t time,
		  uint32_t axes,
		  enum libinput_pointer_axis_source source,
		  const struct normalized_coords *delta_in,
		  const struct discrete_coords *discrete_in);
592 593 594
void
evdev_post_scroll(struct evdev_device *device,
		  uint64_t time,
595
		  enum libinput_pointer_axis_source source,
596
		  const struct normalized_coords *delta);
597 598

void
599 600 601
evdev_stop_scroll(struct evdev_device *device,
		  uint64_t time,
		  enum libinput_pointer_axis_source source);
602

603
void
604
evdev_device_remove(struct evdev_device *device);
605

606
void
607
evdev_device_destroy(struct evdev_device *device);
608

609 610 611 612 613 614 615 616 617 618 619
bool
evdev_middlebutton_filter_button(struct evdev_device *device,
				 uint64_t time,
				 int button,
				 enum libinput_button_state state);

void
evdev_init_middlebutton(struct evdev_device *device,
			bool enabled,
			bool want_config);

620 621 622 623 624 625 626 627 628
enum libinput_config_middle_emulation_state
evdev_middlebutton_get(struct libinput_device *device);

int
evdev_middlebutton_is_available(struct libinput_device *device);

enum libinput_config_middle_emulation_state
evdev_middlebutton_get_default(struct libinput_device *device);

629 630 631 632 633 634 635
static inline double
evdev_convert_to_mm(const struct input_absinfo *absinfo, double v)
{
	double value = v - absinfo->minimum;
	return value/absinfo->resolution;
}

636 637 638 639 640 641 642 643 644 645 646
static inline struct phys_coords
evdev_convert_xy_to_mm(const struct evdev_device *device, int x, int y)
{
	struct phys_coords mm;

	mm.x = evdev_convert_to_mm(device->abs.absinfo_x, x);
	mm.y = evdev_convert_to_mm(device->abs.absinfo_y, y);

	return mm;
}

647
void
648 649 650
evdev_init_left_handed(struct evdev_device *device,
		       void (*change_to_left_handed)(struct evdev_device *));

651 652 653
bool
evdev_tablet_has_left_handed(struct evdev_device *device);

654 655 656 657
static inline uint32_t
evdev_to_left_handed(struct evdev_device *device,
		     uint32_t button)
{
658
	if (device->left_handed.enabled) {
659 660 661 662 663 664 665 666
		if (button == BTN_LEFT)
			return BTN_RIGHT;
		else if (button == BTN_RIGHT)
			return BTN_LEFT;
	}
	return button;
}

667 668
/**
 * Apply a hysteresis filtering to the coordinate in, based on the current
Peter Hutterer's avatar
Peter Hutterer committed
669
 * hysteresis center and the margin. If 'in' is within 'margin' of center,
670
 * return the center (and thus filter the motion). If 'in' is outside,
671 672 673
 * return a point on the edge of the new margin (which is an ellipse, usually
 * a circle). So for a point x in the space outside c + margin we return r:
 * ,---.       ,---.
674
 * | c |  x →  | r x
675
 * `---'       `---'
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
 *
 * The effect of this is that initial small motions are filtered. Once we
 * move into one direction we lag the real coordinates by 'margin' but any
 * movement that continues into that direction will always be just outside
 * margin - we get responsive movement. Once we move back into the other
 * direction, the first movements are filtered again.
 *
 * Returning the edge rather than the point avoids cursor jumps, as the
 * first reachable coordinate is the point next to the center (center + 1).
 * Otherwise, the center has a dead zone of size margin around it and the
 * first reachable point is the margin edge.
 *
 * @param in The input coordinate
 * @param center Current center of the hysteresis
 * @param margin Hysteresis width (on each side)
 *
 * @return The new center of the hysteresis
 */
694 695 696 697
static inline struct device_coords
evdev_hysteresis(const struct device_coords *in,
		 const struct device_coords *center,
		 const struct device_coords *margin)
698
{
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
	int dx = in->x - center->x;
	int dy = in->y - center->y;
	int dx2 = dx * dx;
	int dy2 = dy * dy;
	int a = margin->x;
	int b = margin->y;
	double normalized_finger_distance, finger_distance, margin_distance;
	double lag_x, lag_y;
	struct device_coords result;

	if (!a || !b)
		return *in;

	/*
	 * Basic equation for an ellipse of radii a,b:
	 *   x²/a² + y²/b² = 1
	 * But we start by making a scaled ellipse passing through the
	 * relative finger location (dx,dy). So the scale of this ellipse is
	 * the ratio of finger_distance to margin_distance:
	 *   dx²/a² + dy²/b² = normalized_finger_distance²
	 */
	normalized_finger_distance = sqrt((double)dx2 / (a * a) +
					  (double)dy2 / (b * b));

	/* Which means anything less than 1 is within the elliptical margin */
	if (normalized_finger_distance < 1.0)
		return *center;

	finger_distance = sqrt(dx2 + dy2);
	margin_distance = finger_distance / normalized_finger_distance;

	/*
	 * Now calculate the x,y coordinates on the edge of the margin ellipse
	 * where it intersects the finger vector. Shortcut: We achieve this by
	 * finding the point with the same gradient as dy/dx.
	 */
	if (dx) {
		double gradient = (double)dy / dx;
		lag_x = margin_distance / sqrt(gradient * gradient + 1);
		lag_y = sqrt((margin_distance + lag_x) *
			     (margin_distance - lag_x));
	} else {  /* Infinite gradient */
		lag_x = 0.0;
		lag_y = margin_distance;
	}

	/*
	 * 'result' is the centre of an ellipse (radii a,b) which has been
	 * dragged by the finger moving inside it to 'in'. The finger is now
	 * touching the margin ellipse at some point: (±lag_x,±lag_y)
	 */
	result.x = (dx >= 0) ? in->x - lag_x : in->x + lag_x;
	result.y = (dy >= 0) ? in->y - lag_y : in->y + lag_y;
	return result;
753 754
}

755
LIBINPUT_ATTRIBUTE_PRINTF(3, 4)
756
static inline void
757 758 759 760
evdev_log_msg(struct evdev_device *device,
	      enum libinput_log_priority priority,
	      const char *format,
	      ...)
761
{
762
	va_list args;
763 764
	char buf[1024];

765 766 767
	if (!is_logged(evdev_libinput_context(device), priority))
		return;

768
	/* Anything info and above is user-visible, use the device name */
769 770 771 772 773 774 775 776
	snprintf(buf,
		 sizeof(buf),
		 "%-7s - %s%s%s",
		 evdev_device_get_sysname(device),
		 (priority > LIBINPUT_LOG_PRIORITY_DEBUG) ?  device->devname : "",
		 (priority > LIBINPUT_LOG_PRIORITY_DEBUG) ?  ": " : "",
		 format);

777
	va_start(args, format);
778
	log_msg_va(evdev_libinput_context(device), priority, buf, args);
779 780 781 782
	va_end(args);

}

783
LIBINPUT_ATTRIBUTE_PRINTF(4, 5)
784 785 786 787 788 789 790 791
static inline void
evdev_log_msg_ratelimit(struct evdev_device *device,
			struct ratelimit *ratelimit,
			enum libinput_log_priority priority,
			const char *format,
			...)
{
	va_list args;
792 793
	char buf[1024];

794 795
	enum ratelimit_state state;

796 797 798
	if (!is_logged(evdev_libinput_context(device), priority))
		return;

799 800 801 802
	state = ratelimit_test(ratelimit);
	if (state == RATELIMIT_EXCEEDED)
		return;

803 804 805 806 807 808 809 810 811
	/* Anything info and above is user-visible, use the device name */
	snprintf(buf,
		 sizeof(buf),
		 "%-7s - %s%s%s",
		 evdev_device_get_sysname(device),
		 (priority > LIBINPUT_LOG_PRIORITY_DEBUG) ?  device->devname : "",
		 (priority > LIBINPUT_LOG_PRIORITY_DEBUG) ?  ": " : "",
		 format);

812
	va_start(args, format);
813
	log_msg_va(evdev_libinput_context(device), priority, buf, args);
814 815
	va_end(args);

816 817
	if (state == RATELIMIT_THRESHOLD) {
		struct human_time ht = to_human_time(ratelimit->interval);
818 819
		evdev_log_msg(device,
			      priority,
820 821
			      "WARNING: log rate limit exceeded (%d msgs per %d%s). "
			      "Discarding future messages.\n",
822
			      ratelimit->burst,
823 824 825 826
			      ht.value,
			      ht.unit);

	}
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
}

#define evdev_log_debug(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_DEBUG, __VA_ARGS__)
#define evdev_log_info(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_INFO, __VA_ARGS__)
#define evdev_log_error(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_ERROR, __VA_ARGS__)
#define evdev_log_bug_kernel(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_ERROR, "kernel bug: " __VA_ARGS__)
#define evdev_log_bug_libinput(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_ERROR, "libinput bug: " __VA_ARGS__)
#define evdev_log_bug_client(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_ERROR, "client bug: " __VA_ARGS__)

#define evdev_log_debug_ratelimit(d_, r_, ...) \
	evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_DEBUG, __VA_ARGS__)
#define evdev_log_info_ratelimit(d_, r_, ...) \
	evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_INFO, __VA_ARGS__)
#define evdev_log_error_ratelimit(d_, r_, ...) \
	evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, __VA_ARGS__)
#define evdev_log_bug_kernel_ratelimit(d_, r_, ...) \
	evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, "kernel bug: " __VA_ARGS__)
#define evdev_log_bug_libinput_ratelimit(d_, r_, ...) \
	evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, "libinput bug: " __VA_ARGS__)
#define evdev_log_bug_client_ratelimit(d_, r_, ...) \
	evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, "client bug: " __VA_ARGS__)

849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
/**
 * Convert the pair of delta coordinates in device space to mm.
 */
static inline struct phys_coords
evdev_device_unit_delta_to_mm(const struct evdev_device* device,
			      const struct device_coords *units)
{
	struct phys_coords mm = { 0,  0 };
	const struct input_absinfo *absx, *absy;

	if (device->abs.absinfo_x == NULL ||
	    device->abs.absinfo_y == NULL) {
		log_bug_libinput(evdev_libinput_context(device),
				 "%s: is not an abs device\n",
				 device->devname);
		return mm;
	}

	absx = device->abs.absinfo_x;
	absy = device->abs.absinfo_y;

	mm.x = 1.0 * units->x/absx->resolution;
	mm.y = 1.0 * units->y/absy->resolution;

	return mm;
}

876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
/**
 * Convert the pair of coordinates in device space to mm. This takes the
 * axis min into account, i.e. a unit of min is equivalent to 0 mm.
 */
static inline struct phys_coords
evdev_device_units_to_mm(const struct evdev_device* device,
			 const struct device_coords *units)
{
	struct phys_coords mm = { 0,  0 };
	const struct input_absinfo *absx, *absy;

	if (device->abs.absinfo_x == NULL ||
	    device->abs.absinfo_y == NULL) {
		log_bug_libinput(evdev_libinput_context(device),
				 "%s: is not an abs device\n",
				 device->devname);
		return mm;
	}

	absx = device->abs.absinfo_x;
	absy = device->abs.absinfo_y;

	mm.x = (units->x - absx->minimum)/absx->resolution;
	mm.y = (units->y - absy->minimum)/absy->resolution;

	return mm;
}

/**
 * Convert the pair of coordinates in mm to device units. This takes the
 * axis min into account, i.e. 0 mm  is equivalent to the min.
 */
static inline struct device_coords
evdev_device_mm_to_units(const struct evdev_device *device,
			 const struct phys_coords *mm)
{
	struct device_coords units = { 0,  0 };
	const struct input_absinfo *absx, *absy;

	if (device->abs.absinfo_x == NULL ||
	    device->abs.absinfo_y == NULL) {
		log_bug_libinput(evdev_libinput_context(device),
				 "%s: is not an abs device\n",
				 device->devname);
		return units;
	}

	absx = device->abs.absinfo_x;
	absy = device->abs.absinfo_y;

	units.x = mm->x * absx->resolution + absx->minimum;
	units.y = mm->y * absy->resolution + absy->minimum;

	return units;
}
931

932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
static inline struct device_coord_rect
evdev_phys_rect_to_units(const struct evdev_device *device,
			 const struct phys_rect *mm)
{
	struct device_coord_rect units = {0};
	const struct input_absinfo *absx, *absy;

	if (device->abs.absinfo_x == NULL ||
	    device->abs.absinfo_y == NULL) {
		log_bug_libinput(evdev_libinput_context(device),
				 "%s: is not an abs device\n",
				 device->devname);
		return units;
	}

	absx = device->abs.absinfo_x;
	absy = device->abs.absinfo_y;

	units.x = mm->x * absx->resolution + absx->minimum;
	units.y = mm->y * absy->resolution + absy->minimum;
	units.w = mm->w * absx->resolution;
	units.h = mm->h * absy->resolution;

	return units;
}

958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
static inline void
evdev_device_init_abs_range_warnings(struct evdev_device *device)
{
	const struct input_absinfo *x, *y;
	int width, height;

	x = device->abs.absinfo_x;
	y = device->abs.absinfo_y;
	width = device->abs.dimensions.x;
	height = device->abs.dimensions.y;

	device->abs.warning_range.min.x = x->minimum - 0.05 * width;
	device->abs.warning_range.min.y = y->minimum - 0.05 * height;
	device->abs.warning_range.max.x = x->maximum + 0.05 * width;
	device->abs.warning_range.max.y = y->maximum + 0.05 * height;

	/* One warning every 5 min is enough */
	ratelimit_init(&device->abs.warning_range.range_warn_limit,
		       s2us(3000),
		       1);
}

static inline void
evdev_device_check_abs_axis_range(struct evdev_device *device,
				  unsigned int code,
				  int value)
{
	int min, max;

	switch(code) {
	case ABS_X:
	case ABS_MT_POSITION_X:
		min = device->abs.warning_range.min.x;
		max = device->abs.warning_range.max.x;
		break;
	case ABS_Y:
	case ABS_MT_POSITION_Y:
		min = device->abs.warning_range.min.y;
		max = device->abs.warning_range.max.y;
		break;
	default:
		return;
	}

	if (value < min || value > max) {
		log_info_ratelimit(evdev_libinput_context(device),
				   &device->abs.warning_range.range_warn_limit,
				   "Axis %#x value %d is outside expected range [%d, %d]\n"
1006
				   "See %sabsolute_coordinate_ranges.html for details\n",
1007 1008 1009 1010 1011
				   code, value, min, max,
				   HTTP_DOC_LINK);
	}
}

1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
struct evdev_paired_keyboard {
	struct list link;
	struct evdev_device *device;
	struct libinput_event_listener listener;
};

static inline void
evdev_paired_keyboard_destroy(struct evdev_paired_keyboard *kbd)
{
	kbd->device = NULL;
	libinput_device_remove_event_listener(&kbd->listener);
	list_remove(&kbd->link);
	free(kbd);
}

1027
#endif /* EVDEV_H */