• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2011, 2012 Intel Corporation
3  * Copyright © 2013 Jonas Ådahl
4  * Copyright © 2013-2015 Red Hat, Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 
26 #ifndef EVDEV_H
27 #define EVDEV_H
28 
29 #include "config.h"
30 
31 #include <stdbool.h>
32 #include <stdarg.h>
33 #include "linux/input.h"
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 /* The fake resolution value for abs devices without resolution */
42 #define EVDEV_FAKE_RESOLUTION 1
43 
44 enum evdev_event_type {
45 	EVDEV_NONE,
46 	EVDEV_ABSOLUTE_TOUCH_DOWN	= bit(0),
47 	EVDEV_ABSOLUTE_MOTION		= bit(1),
48 	EVDEV_ABSOLUTE_TOUCH_UP		= bit(2),
49 	EVDEV_ABSOLUTE_MT		= bit(3),
50 	EVDEV_WHEEL			= bit(4),
51 	EVDEV_KEY			= bit(5),
52 	EVDEV_RELATIVE_MOTION		= bit(6),
53 	EVDEV_BUTTON			= bit(7),
54 };
55 
56 enum evdev_device_seat_capability {
57 	EVDEV_DEVICE_POINTER		= bit(0),
58 	EVDEV_DEVICE_KEYBOARD		= bit(1),
59 	EVDEV_DEVICE_TOUCH		= bit(2),
60 	EVDEV_DEVICE_TABLET		= bit(3),
61 	EVDEV_DEVICE_TABLET_PAD		= bit(4),
62 	EVDEV_DEVICE_GESTURE		= bit(5),
63 	EVDEV_DEVICE_SWITCH		= bit(6),
64 };
65 
66 enum evdev_device_tags {
67 	EVDEV_TAG_EXTERNAL_MOUSE	= bit(0),
68 	EVDEV_TAG_INTERNAL_TOUCHPAD	= bit(1),
69 	EVDEV_TAG_EXTERNAL_TOUCHPAD	= bit(2),
70 	EVDEV_TAG_TRACKPOINT		= bit(3),
71 	EVDEV_TAG_KEYBOARD		= bit(4),
72 	EVDEV_TAG_LID_SWITCH		= bit(5),
73 	EVDEV_TAG_INTERNAL_KEYBOARD	= bit(6),
74 	EVDEV_TAG_EXTERNAL_KEYBOARD	= bit(7),
75 	EVDEV_TAG_TABLET_MODE_SWITCH	= bit(8),
76 	EVDEV_TAG_TABLET_TOUCHPAD	= bit(9),
77 };
78 
79 enum evdev_middlebutton_state {
80 	MIDDLEBUTTON_IDLE,
81 	MIDDLEBUTTON_LEFT_DOWN,
82 	MIDDLEBUTTON_RIGHT_DOWN,
83 	MIDDLEBUTTON_MIDDLE,
84 	MIDDLEBUTTON_LEFT_UP_PENDING,
85 	MIDDLEBUTTON_RIGHT_UP_PENDING,
86 	MIDDLEBUTTON_IGNORE_LR,
87 	MIDDLEBUTTON_IGNORE_L,
88 	MIDDLEBUTTON_IGNORE_R,
89 	MIDDLEBUTTON_PASSTHROUGH,
90 };
91 
92 enum evdev_middlebutton_event {
93 	MIDDLEBUTTON_EVENT_L_DOWN,
94 	MIDDLEBUTTON_EVENT_R_DOWN,
95 	MIDDLEBUTTON_EVENT_OTHER,
96 	MIDDLEBUTTON_EVENT_L_UP,
97 	MIDDLEBUTTON_EVENT_R_UP,
98 	MIDDLEBUTTON_EVENT_TIMEOUT,
99 	MIDDLEBUTTON_EVENT_ALL_UP,
100 };
101 
102 /**
103  * model flags are used as shortcut for quirks that need to be checked
104  * multiple times in timing-sensitive paths. For quirks that need to be
105  * checked only once, use the quirk directly.
106  */
107 enum evdev_device_model {
108 	EVDEV_MODEL_DEFAULT = 0,
109 	EVDEV_MODEL_WACOM_TOUCHPAD		= bit(1),
110 	EVDEV_MODEL_SYNAPTICS_SERIAL_TOUCHPAD	= bit(2),
111 	EVDEV_MODEL_ALPS_SERIAL_TOUCHPAD	= bit(3),
112 	EVDEV_MODEL_LENOVO_T450_TOUCHPAD	= bit(4),
113 	EVDEV_MODEL_APPLE_TOUCHPAD_ONEBUTTON	= bit(5),
114 	EVDEV_MODEL_LENOVO_SCROLLPOINT		= bit(6),
115 
116 	/* udev tags, not true quirks */
117 	EVDEV_MODEL_TEST_DEVICE			= bit(20),
118 	EVDEV_MODEL_TRACKBALL			= bit(21),
119 	EVDEV_MODEL_LENOVO_X220_TOUCHPAD_FW81	= bit(22),
120 };
121 
122 enum evdev_button_scroll_state {
123 	BUTTONSCROLL_IDLE,
124 	BUTTONSCROLL_BUTTON_DOWN,	/* button is down */
125 	BUTTONSCROLL_READY,		/* ready for scroll events */
126 	BUTTONSCROLL_SCROLLING,		/* have sent scroll events */
127 };
128 
129 enum evdev_button_scroll_lock_state {
130 	BUTTONSCROLL_LOCK_DISABLED,
131 	BUTTONSCROLL_LOCK_IDLE,
132 	BUTTONSCROLL_LOCK_FIRSTDOWN,
133 	BUTTONSCROLL_LOCK_FIRSTUP,
134 	BUTTONSCROLL_LOCK_SECONDDOWN,
135 };
136 
137 enum evdev_debounce_state {
138 	/**
139 	 * Initial state, no debounce but monitoring events
140 	 */
141 	DEBOUNCE_INIT,
142 	/**
143 	 * Bounce detected, future events need debouncing
144 	 */
145 	DEBOUNCE_NEEDED,
146 	/**
147 	 * Debounce is enabled, but no event is currently being filtered
148 	 */
149 	DEBOUNCE_ON,
150 	/**
151 	 * Debounce is enabled and we are currently filtering an event
152 	 */
153 	DEBOUNCE_ACTIVE,
154 };
155 
156 enum evdev_arbitration_state {
157 	ARBITRATION_NOT_ACTIVE,
158 	ARBITRATION_IGNORE_ALL,
159 	ARBITRATION_IGNORE_RECT,
160 };
161 
162 struct evdev_device {
163 	struct libinput_device base;
164 
165 	struct libinput_source *source;
166 
167 	struct evdev_dispatch *dispatch;
168 	struct libevdev *evdev;
169 	struct udev_device *udev_device;
170 	char *output_name;
171 	const char *devname;
172 	char *log_prefix_name;
173 	char *sysname;
174 	bool was_removed;
175 	int fd;
176 	enum evdev_device_seat_capability seat_caps;
177 	enum evdev_device_tags tags;
178 	bool is_mt;
179 	bool is_suspended;
180 	int dpi; /* HW resolution */
181 	double trackpoint_multiplier; /* trackpoint constant multiplier */
182 	bool use_velocity_averaging; /* whether averaging should be applied on velocity calculation */
183 	struct ratelimit syn_drop_limit; /* ratelimit for SYN_DROPPED logging */
184 	struct ratelimit delay_warning_limit; /* ratelimit for delayd processing logging */
185 	struct ratelimit nonpointer_rel_limit; /* ratelimit for REL_* events from non-pointer devices */
186 	uint32_t model_flags;
187 	struct mtdev *mtdev;
188 
189 	struct {
190 		const struct input_absinfo *absinfo_x, *absinfo_y;
191 		bool is_fake_resolution;
192 
193 		int apply_calibration;
194 		struct matrix calibration;
195 		struct matrix default_calibration; /* from LIBINPUT_CALIBRATION_MATRIX */
196 		struct matrix usermatrix; /* as supplied by the caller */
197 
198 		struct device_coords dimensions;
199 
200 		struct {
201 			struct device_coords min, max;
202 			struct ratelimit range_warn_limit;
203 		} warning_range;
204 	} abs;
205 
206 	struct {
207 		struct libinput_timer timer;
208 		struct libinput_device_config_scroll_method config;
209 		/* Currently enabled method, button */
210 		enum libinput_config_scroll_method method;
211 		uint32_t button;
212 		uint64_t button_down_time;
213 
214 		/* set during device init, used at runtime to delay changes
215 		 * until all buttons are up */
216 		enum libinput_config_scroll_method want_method;
217 		uint32_t want_button;
218 		/* Checks if buttons are down and commits the setting */
219 		void (*change_scroll_method)(struct evdev_device *device);
220 		enum evdev_button_scroll_state button_scroll_state;
221 		double threshold;
222 		double direction_lock_threshold;
223 		uint32_t direction;
224 		struct normalized_coords buildup;
225 
226 		struct libinput_device_config_natural_scroll config_natural;
227 		/* set during device init if we want natural scrolling,
228 		 * used at runtime to enable/disable the feature */
229 		bool natural_scrolling_enabled;
230 
231 		/* set during device init to invert direction of
232 		 * horizontal scrolling */
233 		bool invert_horizontal_scrolling;
234 
235 		/* angle per REL_WHEEL click in degrees */
236 		struct wheel_angle wheel_click_angle;
237 
238 		enum evdev_button_scroll_lock_state lock_state;
239 		bool want_lock_enabled;
240 		bool lock_enabled;
241 	} scroll;
242 
243 	struct {
244 		struct libinput_device_config_accel config;
245 		struct motion_filter *filter;
246 	} pointer;
247 
248 	/* Key counter used for multiplexing button events internally in
249 	 * libinput. */
250 	uint8_t key_count[KEY_CNT];
251 
252 	struct {
253 		struct libinput_device_config_left_handed config;
254 		/* left-handed currently enabled */
255 		bool enabled;
256 		/* set during device init if we want left_handed config,
257 		 * used at runtime to delay the effect until buttons are up */
258 		bool want_enabled;
259 		/* Checks if buttons are down and commits the setting */
260 		void (*change_to_enabled)(struct evdev_device *device);
261 	} left_handed;
262 
263 	struct {
264 		struct libinput_device_config_middle_emulation config;
265 		/* middle-button emulation enabled */
266 		bool enabled;
267 		bool enabled_default;
268 		bool want_enabled;
269 		enum evdev_middlebutton_state state;
270 		struct libinput_timer timer;
271 		uint32_t button_mask;
272 		uint64_t first_event_time;
273 	} middlebutton;
274 };
275 
276 static inline struct evdev_device *
evdev_device(struct libinput_device * device)277 evdev_device(struct libinput_device *device)
278 {
279 	return container_of(device, struct evdev_device, base);
280 }
281 
282 #define EVDEV_UNHANDLED_DEVICE ((struct evdev_device *) 1)
283 
284 struct evdev_dispatch;
285 
286 struct evdev_dispatch_interface {
287 	/* Process an evdev input event. */
288 	void (*process)(struct evdev_dispatch *dispatch,
289 			struct evdev_device *device,
290 			struct input_event *event,
291 			uint64_t time);
292 
293 	/* Device is being suspended */
294 	void (*suspend)(struct evdev_dispatch *dispatch,
295 			struct evdev_device *device);
296 
297 	/* Device is being removed (may be NULL) */
298 	void (*remove)(struct evdev_dispatch *dispatch);
299 
300 	/* Destroy an event dispatch handler and free all its resources. */
301 	void (*destroy)(struct evdev_dispatch *dispatch);
302 
303 	/* A new device was added */
304 	void (*device_added)(struct evdev_device *device,
305 			     struct evdev_device *added_device);
306 
307 	/* A device was removed */
308 	void (*device_removed)(struct evdev_device *device,
309 			       struct evdev_device *removed_device);
310 
311 	/* A device was suspended */
312 	void (*device_suspended)(struct evdev_device *device,
313 				 struct evdev_device *suspended_device);
314 
315 	/* A device was resumed */
316 	void (*device_resumed)(struct evdev_device *device,
317 			       struct evdev_device *resumed_device);
318 
319 	/* Called immediately after the LIBINPUT_EVENT_DEVICE_ADDED event
320 	 * was sent */
321 	void (*post_added)(struct evdev_device *device,
322 			   struct evdev_dispatch *dispatch);
323 
324 	/* For touch arbitration, called on the device that should
325 	 * enable/disable touch capabilities.
326 	 */
327 	void (*touch_arbitration_toggle)(struct evdev_dispatch *dispatch,
328 					 struct evdev_device *device,
329 					 enum evdev_arbitration_state which,
330 					 const struct phys_rect *rect, /* may be NULL */
331 					 uint64_t now);
332 
333 	/* Called when touch arbitration is on, updates the area where touch
334 	 * arbitration should apply.
335 	 */
336 	void (*touch_arbitration_update_rect)(struct evdev_dispatch *dispatch,
337 					      struct evdev_device *device,
338 					      const struct phys_rect *rect,
339 					      uint64_t now);
340 
341 
342 	/* Return the state of the given switch */
343 	enum libinput_switch_state
344 		(*get_switch_state)(struct evdev_dispatch *dispatch,
345 				    enum libinput_switch which);
346 
347 	void (*left_handed_toggle)(struct evdev_dispatch *dispatch,
348 				   struct evdev_device *device,
349 				   bool left_handed_enabled);
350 };
351 
352 enum evdev_dispatch_type {
353 	DISPATCH_FALLBACK,
354 	DISPATCH_TOUCHPAD,
355 	DISPATCH_TABLET,
356 	DISPATCH_TABLET_PAD,
357 	DISPATCH_TOTEM,
358 };
359 
360 struct evdev_dispatch {
361 	enum evdev_dispatch_type dispatch_type;
362 	struct evdev_dispatch_interface *interface;
363 
364 	struct {
365 		struct libinput_device_config_send_events config;
366 		enum libinput_config_send_events_mode current_mode;
367 	} sendevents;
368 };
369 
370 static inline void
evdev_verify_dispatch_type(struct evdev_dispatch * dispatch,enum evdev_dispatch_type type)371 evdev_verify_dispatch_type(struct evdev_dispatch *dispatch,
372 			   enum evdev_dispatch_type type)
373 {
374 	if (dispatch->dispatch_type != type)
375 		abort();
376 }
377 
378 struct evdev_device *
379 evdev_device_create(struct libinput_seat *seat,
380 		    struct udev_device *device);
381 
382 static inline struct libinput *
evdev_libinput_context(const struct evdev_device * device)383 evdev_libinput_context(const struct evdev_device *device)
384 {
385 	return device->base.seat->libinput;
386 }
387 
388 static inline bool
evdev_device_has_model_quirk(struct evdev_device * device,enum quirk model_quirk)389 evdev_device_has_model_quirk(struct evdev_device *device,
390 			     enum quirk model_quirk)
391 {
392 	struct quirks_context *quirks;
393 	struct quirks *q;
394 	bool result = false;
395 
396 	assert(quirk_get_name(model_quirk) != NULL);
397 
398 	quirks = evdev_libinput_context(device)->quirks;
399 	q = quirks_fetch_for_device(quirks, device->udev_device);
400 	quirks_get_bool(q, model_quirk, &result);
401 	quirks_unref(q);
402 
403 	return result;
404 }
405 
406 void
407 evdev_transform_absolute(struct evdev_device *device,
408 			 struct device_coords *point);
409 
410 void
411 evdev_transform_relative(struct evdev_device *device,
412 			 struct device_coords *point);
413 
414 void
415 evdev_init_calibration(struct evdev_device *device,
416 		        struct libinput_device_config_calibration *calibration);
417 
418 void
419 evdev_read_calibration_prop(struct evdev_device *device);
420 
421 int
422 evdev_read_fuzz_prop(struct evdev_device *device, unsigned int code);
423 
424 enum switch_reliability
425 evdev_read_switch_reliability_prop(struct evdev_device *device);
426 
427 void
428 evdev_init_sendevents(struct evdev_device *device,
429 		      struct evdev_dispatch *dispatch);
430 
431 void
432 evdev_device_init_pointer_acceleration(struct evdev_device *device,
433 				       struct motion_filter *filter);
434 
435 struct evdev_dispatch *
436 evdev_touchpad_create(struct evdev_device *device);
437 
438 struct evdev_dispatch *
439 evdev_mt_touchpad_create(struct evdev_device *device);
440 
441 struct evdev_dispatch *
442 evdev_tablet_create(struct evdev_device *device);
443 
444 struct evdev_dispatch *
445 evdev_tablet_pad_create(struct evdev_device *device);
446 
447 struct evdev_dispatch *
448 evdev_lid_switch_dispatch_create(struct evdev_device *device);
449 
450 struct evdev_dispatch *
451 fallback_dispatch_create(struct libinput_device *libinput_device);
452 
453 struct evdev_dispatch *
454 evdev_totem_create(struct evdev_device *device);
455 
456 bool
457 evdev_is_fake_mt_device(struct evdev_device *device);
458 
459 int
460 evdev_need_mtdev(struct evdev_device *device);
461 
462 void
463 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds);
464 
465 int
466 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size);
467 
468 const char *
469 evdev_device_get_output(struct evdev_device *device);
470 
471 const char *
472 evdev_device_get_sysname(struct evdev_device *device);
473 
474 const char *
475 evdev_device_get_name(struct evdev_device *device);
476 
477 unsigned int
478 evdev_device_get_id_product(struct evdev_device *device);
479 
480 unsigned int
481 evdev_device_get_id_vendor(struct evdev_device *device);
482 
483 struct udev_device *
484 evdev_device_get_udev_device(struct evdev_device *device);
485 
486 void
487 evdev_device_set_default_calibration(struct evdev_device *device,
488 				     const float calibration[6]);
489 void
490 evdev_device_calibrate(struct evdev_device *device,
491 		       const float calibration[6]);
492 
493 bool
494 evdev_device_has_capability(struct evdev_device *device,
495 			    enum libinput_device_capability capability);
496 
497 int
498 evdev_device_get_size(const struct evdev_device *device,
499 		      double *w,
500 		      double *h);
501 
502 int
503 evdev_device_has_button(struct evdev_device *device, uint32_t code);
504 
505 int
506 evdev_device_has_key(struct evdev_device *device, uint32_t code);
507 
508 int
509 evdev_device_get_touch_count(struct evdev_device *device);
510 
511 int
512 evdev_device_has_switch(struct evdev_device *device,
513 			enum libinput_switch sw);
514 
515 int
516 evdev_device_tablet_pad_has_key(struct evdev_device *device,
517 				uint32_t code);
518 
519 int
520 evdev_device_tablet_pad_get_num_buttons(struct evdev_device *device);
521 
522 int
523 evdev_device_tablet_pad_get_num_rings(struct evdev_device *device);
524 
525 int
526 evdev_device_tablet_pad_get_num_strips(struct evdev_device *device);
527 
528 int
529 evdev_device_tablet_pad_get_num_mode_groups(struct evdev_device *device);
530 
531 struct libinput_tablet_pad_mode_group *
532 evdev_device_tablet_pad_get_mode_group(struct evdev_device *device,
533 				       unsigned int index);
534 
535 enum libinput_switch_state
536 evdev_device_switch_get_state(struct evdev_device *device,
537 			      enum libinput_switch sw);
538 
539 double
540 evdev_device_transform_x(struct evdev_device *device,
541 			 double x,
542 			 uint32_t width);
543 
544 double
545 evdev_device_transform_y(struct evdev_device *device,
546 			 double y,
547 			 uint32_t height);
548 void
549 evdev_device_suspend(struct evdev_device *device);
550 
551 int
552 evdev_device_resume(struct evdev_device *device);
553 
554 void
555 evdev_notify_suspended_device(struct evdev_device *device);
556 
557 void
558 evdev_notify_resumed_device(struct evdev_device *device);
559 
560 void
561 evdev_pointer_notify_button(struct evdev_device *device,
562 			    uint64_t time,
563 			    unsigned int button,
564 			    enum libinput_button_state state);
565 void
566 evdev_pointer_notify_physical_button(struct evdev_device *device,
567 				     uint64_t time,
568 				     int button,
569 				     enum libinput_button_state state);
570 
571 void
572 evdev_init_natural_scroll(struct evdev_device *device);
573 
574 void
575 evdev_init_button_scroll(struct evdev_device *device,
576 			 void (*change_scroll_method)(struct evdev_device *));
577 
578 void
579 evdev_set_button_scroll_lock_enabled(struct evdev_device *device,
580 				     bool enabled);
581 
582 int
583 evdev_update_key_down_count(struct evdev_device *device,
584 			    int code,
585 			    int pressed);
586 
587 void
588 evdev_notify_axis_legacy_wheel(struct evdev_device *device,
589 			       uint64_t time,
590 			       uint32_t axes,
591 			       const struct normalized_coords *delta_in,
592 			       const struct discrete_coords *discrete_in);
593 void
594 evdev_notify_axis_wheel(struct evdev_device *device,
595 			uint64_t time,
596 			uint32_t axes,
597 			const struct normalized_coords *delta_in,
598 			const struct wheel_v120 *v120_in);
599 void
600 evdev_notify_axis_finger(struct evdev_device *device,
601 			uint64_t time,
602 			uint32_t axes,
603 			const struct normalized_coords *delta_in);
604 void
605 evdev_notify_axis_continous(struct evdev_device *device,
606 			    uint64_t time,
607 			    uint32_t axes,
608 			    const struct normalized_coords *delta_in);
609 
610 void
611 evdev_post_scroll(struct evdev_device *device,
612 		  uint64_t time,
613 		  enum libinput_pointer_axis_source source,
614 		  const struct normalized_coords *delta);
615 
616 void
617 evdev_stop_scroll(struct evdev_device *device,
618 		  uint64_t time,
619 		  enum libinput_pointer_axis_source source);
620 
621 void
622 evdev_device_remove(struct evdev_device *device);
623 
624 void
625 evdev_device_destroy(struct evdev_device *device);
626 
627 bool
628 evdev_middlebutton_filter_button(struct evdev_device *device,
629 				 uint64_t time,
630 				 int button,
631 				 enum libinput_button_state state);
632 
633 void
634 evdev_init_middlebutton(struct evdev_device *device,
635 			bool enabled,
636 			bool want_config);
637 
638 enum libinput_config_middle_emulation_state
639 evdev_middlebutton_get(struct libinput_device *device);
640 
641 int
642 evdev_middlebutton_is_available(struct libinput_device *device);
643 
644 enum libinput_config_middle_emulation_state
645 evdev_middlebutton_get_default(struct libinput_device *device);
646 
647 static inline double
evdev_convert_to_mm(const struct input_absinfo * absinfo,double v)648 evdev_convert_to_mm(const struct input_absinfo *absinfo, double v)
649 {
650 	double value = v - absinfo->minimum;
651 	return value/absinfo->resolution;
652 }
653 
654 static inline struct phys_coords
evdev_convert_xy_to_mm(const struct evdev_device * device,int x,int y)655 evdev_convert_xy_to_mm(const struct evdev_device *device, int x, int y)
656 {
657 	struct phys_coords mm;
658 
659 	mm.x = evdev_convert_to_mm(device->abs.absinfo_x, x);
660 	mm.y = evdev_convert_to_mm(device->abs.absinfo_y, y);
661 
662 	return mm;
663 }
664 
665 void
666 evdev_init_left_handed(struct evdev_device *device,
667 		       void (*change_to_left_handed)(struct evdev_device *));
668 
669 bool
670 evdev_tablet_has_left_handed(struct evdev_device *device);
671 
672 static inline uint32_t
evdev_to_left_handed(struct evdev_device * device,uint32_t button)673 evdev_to_left_handed(struct evdev_device *device,
674 		     uint32_t button)
675 {
676 	if (device->left_handed.enabled) {
677 		if (button == BTN_LEFT)
678 			return BTN_RIGHT;
679 		else if (button == BTN_RIGHT)
680 			return BTN_LEFT;
681 	}
682 	return button;
683 }
684 
685 /**
686  * Apply a hysteresis filtering to the coordinate in, based on the current
687  * hysteresis center and the margin. If 'in' is within 'margin' of center,
688  * return the center (and thus filter the motion). If 'in' is outside,
689  * return a point on the edge of the new margin (which is an ellipse, usually
690  * a circle). So for a point x in the space outside c + margin we return r:
691  * ,---.       ,---.
692  * | c |  x →  | r x
693  * `---'       `---'
694  *
695  * The effect of this is that initial small motions are filtered. Once we
696  * move into one direction we lag the real coordinates by 'margin' but any
697  * movement that continues into that direction will always be just outside
698  * margin - we get responsive movement. Once we move back into the other
699  * direction, the first movements are filtered again.
700  *
701  * Returning the edge rather than the point avoids cursor jumps, as the
702  * first reachable coordinate is the point next to the center (center + 1).
703  * Otherwise, the center has a dead zone of size margin around it and the
704  * first reachable point is the margin edge.
705  *
706  * @param in The input coordinate
707  * @param center Current center of the hysteresis
708  * @param margin Hysteresis width (on each side)
709  *
710  * @return The new center of the hysteresis
711  */
712 static inline struct device_coords
evdev_hysteresis(const struct device_coords * in,const struct device_coords * center,const struct device_coords * margin)713 evdev_hysteresis(const struct device_coords *in,
714 		 const struct device_coords *center,
715 		 const struct device_coords *margin)
716 {
717 	int dx = in->x - center->x;
718 	int dy = in->y - center->y;
719 	int dx2 = dx * dx;
720 	int dy2 = dy * dy;
721 	int a = margin->x;
722 	int b = margin->y;
723 	double normalized_finger_distance, finger_distance, margin_distance;
724 	double lag_x, lag_y;
725 	struct device_coords result;
726 
727 	if (!a || !b)
728 		return *in;
729 
730 	/*
731 	 * Basic equation for an ellipse of radii a,b:
732 	 *   x²/a² + y²/b² = 1
733 	 * But we start by making a scaled ellipse passing through the
734 	 * relative finger location (dx,dy). So the scale of this ellipse is
735 	 * the ratio of finger_distance to margin_distance:
736 	 *   dx²/a² + dy²/b² = normalized_finger_distance²
737 	 */
738 	normalized_finger_distance = sqrt((double)dx2 / (a * a) +
739 					  (double)dy2 / (b * b));
740 
741 	/* Which means anything less than 1 is within the elliptical margin */
742 	if (normalized_finger_distance < 1.0)
743 		return *center;
744 
745 	finger_distance = sqrt(dx2 + dy2);
746 	margin_distance = finger_distance / normalized_finger_distance;
747 
748 	/*
749 	 * Now calculate the x,y coordinates on the edge of the margin ellipse
750 	 * where it intersects the finger vector. Shortcut: We achieve this by
751 	 * finding the point with the same gradient as dy/dx.
752 	 */
753 	if (dx) {
754 		double gradient = (double)dy / dx;
755 		lag_x = margin_distance / sqrt(gradient * gradient + 1);
756 		lag_y = sqrt((margin_distance + lag_x) *
757 			     (margin_distance - lag_x));
758 	} else {  /* Infinite gradient */
759 		lag_x = 0.0;
760 		lag_y = margin_distance;
761 	}
762 
763 	/*
764 	 * 'result' is the centre of an ellipse (radii a,b) which has been
765 	 * dragged by the finger moving inside it to 'in'. The finger is now
766 	 * touching the margin ellipse at some point: (±lag_x,±lag_y)
767 	 */
768 	result.x = (dx >= 0) ? in->x - lag_x : in->x + lag_x;
769 	result.y = (dy >= 0) ? in->y - lag_y : in->y + lag_y;
770 	return result;
771 }
772 
773 LIBINPUT_ATTRIBUTE_PRINTF(3, 4)
774 static inline void
evdev_log_msg(struct evdev_device * device,enum libinput_log_priority priority,const char * format,...)775 evdev_log_msg(struct evdev_device *device,
776 	      enum libinput_log_priority priority,
777 	      const char *format,
778 	      ...)
779 {
780 	va_list args;
781 	char buf[1024];
782 
783 	if (!is_logged(evdev_libinput_context(device), priority))
784 		return;
785 
786 	/* Anything info and above is user-visible, use the device name */
787 	snprintf(buf,
788 		 sizeof(buf),
789 		 "%-7s - %s%s%s",
790 		 evdev_device_get_sysname(device),
791 		 (priority > LIBINPUT_LOG_PRIORITY_DEBUG) ?  device->log_prefix_name : "",
792 		 (priority > LIBINPUT_LOG_PRIORITY_DEBUG) ?  ": " : "",
793 		 format);
794 
795 	va_start(args, format);
796 #pragma GCC diagnostic push
797 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
798 	log_msg_va(evdev_libinput_context(device), priority, buf, args);
799 #pragma GCC diagnostic pop
800 	va_end(args);
801 
802 }
803 
804 LIBINPUT_ATTRIBUTE_PRINTF(4, 5)
805 static inline void
evdev_log_msg_ratelimit(struct evdev_device * device,struct ratelimit * ratelimit,enum libinput_log_priority priority,const char * format,...)806 evdev_log_msg_ratelimit(struct evdev_device *device,
807 			struct ratelimit *ratelimit,
808 			enum libinput_log_priority priority,
809 			const char *format,
810 			...)
811 {
812 	va_list args;
813 	char buf[1024];
814 
815 	enum ratelimit_state state;
816 
817 	if (!is_logged(evdev_libinput_context(device), priority))
818 		return;
819 
820 	state = ratelimit_test(ratelimit);
821 	if (state == RATELIMIT_EXCEEDED)
822 		return;
823 
824 	/* Anything info and above is user-visible, use the device name */
825 	snprintf(buf,
826 		 sizeof(buf),
827 		 "%-7s - %s%s%s",
828 		 evdev_device_get_sysname(device),
829 		 (priority > LIBINPUT_LOG_PRIORITY_DEBUG) ?  device->log_prefix_name : "",
830 		 (priority > LIBINPUT_LOG_PRIORITY_DEBUG) ?  ": " : "",
831 		 format);
832 
833 	va_start(args, format);
834 #pragma GCC diagnostic push
835 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
836 	log_msg_va(evdev_libinput_context(device), priority, buf, args);
837 #pragma GCC diagnostic pop
838 	va_end(args);
839 
840 	if (state == RATELIMIT_THRESHOLD) {
841 		struct human_time ht = to_human_time(ratelimit->interval);
842 		evdev_log_msg(device,
843 			      priority,
844 			      "WARNING: log rate limit exceeded (%d msgs per %d%s). "
845 			      "Discarding future messages.\n",
846 			      ratelimit->burst,
847 			      ht.value,
848 			      ht.unit);
849 
850 	}
851 }
852 
853 #define evdev_log_debug(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_DEBUG, __VA_ARGS__)
854 #define evdev_log_info(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_INFO, __VA_ARGS__)
855 #define evdev_log_error(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_ERROR, __VA_ARGS__)
856 #define evdev_log_bug_kernel(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_ERROR, "kernel bug: " __VA_ARGS__)
857 #define evdev_log_bug_libinput(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_ERROR, "libinput bug: " __VA_ARGS__)
858 #define evdev_log_bug_client(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_ERROR, "client bug: " __VA_ARGS__)
859 
860 #define evdev_log_debug_ratelimit(d_, r_, ...) \
861 	evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_DEBUG, __VA_ARGS__)
862 #define evdev_log_info_ratelimit(d_, r_, ...) \
863 	evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_INFO, __VA_ARGS__)
864 #define evdev_log_error_ratelimit(d_, r_, ...) \
865 	evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, __VA_ARGS__)
866 #define evdev_log_bug_kernel_ratelimit(d_, r_, ...) \
867 	evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, "kernel bug: " __VA_ARGS__)
868 #define evdev_log_bug_libinput_ratelimit(d_, r_, ...) \
869 	evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, "libinput bug: " __VA_ARGS__)
870 #define evdev_log_bug_client_ratelimit(d_, r_, ...) \
871 	evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, "client bug: " __VA_ARGS__)
872 
873 /**
874  * Convert the pair of delta coordinates in device space to mm.
875  */
876 static inline struct phys_coords
evdev_device_unit_delta_to_mm(const struct evdev_device * device,const struct device_coords * units)877 evdev_device_unit_delta_to_mm(const struct evdev_device* device,
878 			      const struct device_coords *units)
879 {
880 	struct phys_coords mm = { 0,  0 };
881 	const struct input_absinfo *absx, *absy;
882 
883 	if (device->abs.absinfo_x == NULL ||
884 	    device->abs.absinfo_y == NULL) {
885 		log_bug_libinput(evdev_libinput_context(device),
886 				 "%s: is not an abs device\n",
887 				 device->devname);
888 		return mm;
889 	}
890 
891 	absx = device->abs.absinfo_x;
892 	absy = device->abs.absinfo_y;
893 
894 	mm.x = 1.0 * units->x/absx->resolution;
895 	mm.y = 1.0 * units->y/absy->resolution;
896 
897 	return mm;
898 }
899 
900 /**
901  * Convert the pair of coordinates in device space to mm. This takes the
902  * axis min into account, i.e. a unit of min is equivalent to 0 mm.
903  */
904 static inline struct phys_coords
evdev_device_units_to_mm(const struct evdev_device * device,const struct device_coords * units)905 evdev_device_units_to_mm(const struct evdev_device* device,
906 			 const struct device_coords *units)
907 {
908 	struct phys_coords mm = { 0,  0 };
909 	const struct input_absinfo *absx, *absy;
910 
911 	if (device->abs.absinfo_x == NULL ||
912 	    device->abs.absinfo_y == NULL) {
913 		log_bug_libinput(evdev_libinput_context(device),
914 				 "%s: is not an abs device\n",
915 				 device->devname);
916 		return mm;
917 	}
918 
919 	absx = device->abs.absinfo_x;
920 	absy = device->abs.absinfo_y;
921 
922 	mm.x = (units->x - absx->minimum)/absx->resolution;
923 	mm.y = (units->y - absy->minimum)/absy->resolution;
924 
925 	return mm;
926 }
927 
928 /**
929  * Convert the pair of coordinates in mm to device units. This takes the
930  * axis min into account, i.e. 0 mm  is equivalent to the min.
931  */
932 static inline struct device_coords
evdev_device_mm_to_units(const struct evdev_device * device,const struct phys_coords * mm)933 evdev_device_mm_to_units(const struct evdev_device *device,
934 			 const struct phys_coords *mm)
935 {
936 	struct device_coords units = { 0,  0 };
937 	const struct input_absinfo *absx, *absy;
938 
939 	if (device->abs.absinfo_x == NULL ||
940 	    device->abs.absinfo_y == NULL) {
941 		log_bug_libinput(evdev_libinput_context(device),
942 				 "%s: is not an abs device\n",
943 				 device->devname);
944 		return units;
945 	}
946 
947 	absx = device->abs.absinfo_x;
948 	absy = device->abs.absinfo_y;
949 
950 	units.x = mm->x * absx->resolution + absx->minimum;
951 	units.y = mm->y * absy->resolution + absy->minimum;
952 
953 	return units;
954 }
955 
956 static inline struct device_coord_rect
evdev_phys_rect_to_units(const struct evdev_device * device,const struct phys_rect * mm)957 evdev_phys_rect_to_units(const struct evdev_device *device,
958 			 const struct phys_rect *mm)
959 {
960 	struct device_coord_rect units = {0};
961 	const struct input_absinfo *absx, *absy;
962 
963 	if (device->abs.absinfo_x == NULL ||
964 	    device->abs.absinfo_y == NULL) {
965 		log_bug_libinput(evdev_libinput_context(device),
966 				 "%s: is not an abs device\n",
967 				 device->devname);
968 		return units;
969 	}
970 
971 	absx = device->abs.absinfo_x;
972 	absy = device->abs.absinfo_y;
973 
974 	units.x = mm->x * absx->resolution + absx->minimum;
975 	units.y = mm->y * absy->resolution + absy->minimum;
976 	units.w = mm->w * absx->resolution;
977 	units.h = mm->h * absy->resolution;
978 
979 	return units;
980 }
981 
982 static inline void
evdev_device_init_abs_range_warnings(struct evdev_device * device)983 evdev_device_init_abs_range_warnings(struct evdev_device *device)
984 {
985 	const struct input_absinfo *x, *y;
986 	int width, height;
987 
988 	x = device->abs.absinfo_x;
989 	y = device->abs.absinfo_y;
990 	width = device->abs.dimensions.x;
991 	height = device->abs.dimensions.y;
992 
993 	device->abs.warning_range.min.x = x->minimum - 0.05 * width;
994 	device->abs.warning_range.min.y = y->minimum - 0.05 * height;
995 	device->abs.warning_range.max.x = x->maximum + 0.05 * width;
996 	device->abs.warning_range.max.y = y->maximum + 0.05 * height;
997 
998 	/* One warning every 5 min is enough */
999 	ratelimit_init(&device->abs.warning_range.range_warn_limit,
1000 		       s2us(3000),
1001 		       1);
1002 }
1003 
1004 static inline void
evdev_device_check_abs_axis_range(struct evdev_device * device,unsigned int code,int value)1005 evdev_device_check_abs_axis_range(struct evdev_device *device,
1006 				  unsigned int code,
1007 				  int value)
1008 {
1009 	int min, max;
1010 
1011 	switch(code) {
1012 	case ABS_X:
1013 	case ABS_MT_POSITION_X:
1014 		min = device->abs.warning_range.min.x;
1015 		max = device->abs.warning_range.max.x;
1016 		break;
1017 	case ABS_Y:
1018 	case ABS_MT_POSITION_Y:
1019 		min = device->abs.warning_range.min.y;
1020 		max = device->abs.warning_range.max.y;
1021 		break;
1022 	default:
1023 		return;
1024 	}
1025 
1026 	if (value < min || value > max) {
1027 		log_info_ratelimit(evdev_libinput_context(device),
1028 				   &device->abs.warning_range.range_warn_limit,
1029 				   "Axis %#x value %d is outside expected range [%d, %d]\n"
1030 				   "See %s/absolute_coordinate_ranges.html for details\n",
1031 				   code, value, min, max,
1032 				   HTTP_DOC_LINK);
1033 	}
1034 }
1035 
1036 struct evdev_paired_keyboard {
1037 	struct list link;
1038 	struct evdev_device *device;
1039 	struct libinput_event_listener listener;
1040 };
1041 
1042 static inline void
evdev_paired_keyboard_destroy(struct evdev_paired_keyboard * kbd)1043 evdev_paired_keyboard_destroy(struct evdev_paired_keyboard *kbd)
1044 {
1045 	kbd->device = NULL;
1046 	libinput_device_remove_event_listener(&kbd->listener);
1047 	list_remove(&kbd->link);
1048 	free(kbd);
1049 }
1050 
1051 #endif /* EVDEV_H */
1052