• 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 	bool was_removed;
173 	int fd;
174 	enum evdev_device_seat_capability seat_caps;
175 	enum evdev_device_tags tags;
176 	bool is_mt;
177 	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 	struct ratelimit nonpointer_rel_limit; /* ratelimit for REL_* events from non-pointer devices */
184 	uint32_t model_flags;
185 	struct mtdev *mtdev;
186 
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 		struct {
199 			struct device_coords min, max;
200 			struct ratelimit range_warn_limit;
201 		} warning_range;
202 	} abs;
203 
204 	struct {
205 		struct libinput_timer timer;
206 		struct libinput_device_config_scroll_method config;
207 		/* Currently enabled method, button */
208 		enum libinput_config_scroll_method method;
209 		uint32_t button;
210 		uint64_t button_down_time;
211 
212 		/* set during device init, used at runtime to delay changes
213 		 * until all buttons are up */
214 		enum libinput_config_scroll_method want_method;
215 		uint32_t want_button;
216 		/* 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 		/* set during device init if we want natural scrolling,
226 		 * used at runtime to enable/disable the feature */
227 		bool natural_scrolling_enabled;
228 
229 		/* set during device init to invert direction of
230 		 * horizontal scrolling */
231 		bool invert_horizontal_scrolling;
232 
233 		/* angle per REL_WHEEL click in degrees */
234 		struct wheel_angle wheel_click_angle;
235 
236 		enum evdev_button_scroll_lock_state lock_state;
237 		bool want_lock_enabled;
238 		bool lock_enabled;
239 	} scroll;
240 
241 	struct {
242 		struct libinput_device_config_accel config;
243 		struct motion_filter *filter;
244 	} pointer;
245 
246 	/* Key counter used for multiplexing button events internally in
247 	 * libinput. */
248 	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 		/* set during device init if we want left_handed config,
255 		 * 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 		void (*change_to_enabled)(struct evdev_device *device);
259 	} left_handed;
260 
261 	struct {
262 		struct libinput_device_config_middle_emulation config;
263 		/* middle-button emulation enabled */
264 		bool enabled;
265 		bool enabled_default;
266 		bool want_enabled;
267 		enum evdev_middlebutton_state state;
268 		struct libinput_timer timer;
269 		uint32_t button_mask;
270 		uint64_t first_event_time;
271 	} middlebutton;
272 };
273 
274 static inline struct evdev_device *
evdev_device(struct libinput_device * device)275 evdev_device(struct libinput_device *device)
276 {
277 	return container_of(device, struct evdev_device, base);
278 }
279 
280 #define EVDEV_UNHANDLED_DEVICE ((struct evdev_device *) 1)
281 
282 struct evdev_dispatch;
283 
284 struct evdev_dispatch_interface {
285 	/* Process an evdev input event. */
286 	void (*process)(struct evdev_dispatch *dispatch,
287 			struct evdev_device *device,
288 			struct input_event *event,
289 			uint64_t time);
290 
291 	/* Device is being suspended */
292 	void (*suspend)(struct evdev_dispatch *dispatch,
293 			struct evdev_device *device);
294 
295 	/* Device is being removed (may be NULL) */
296 	void (*remove)(struct evdev_dispatch *dispatch);
297 
298 	/* Destroy an event dispatch handler and free all its resources. */
299 	void (*destroy)(struct evdev_dispatch *dispatch);
300 
301 	/* A new device was added */
302 	void (*device_added)(struct evdev_device *device,
303 			     struct evdev_device *added_device);
304 
305 	/* A device was removed */
306 	void (*device_removed)(struct evdev_device *device,
307 			       struct evdev_device *removed_device);
308 
309 	/* A device was suspended */
310 	void (*device_suspended)(struct evdev_device *device,
311 				 struct evdev_device *suspended_device);
312 
313 	/* A device was resumed */
314 	void (*device_resumed)(struct evdev_device *device,
315 			       struct evdev_device *resumed_device);
316 
317 	/* Called immediately after the LIBINPUT_EVENT_DEVICE_ADDED event
318 	 * was sent */
319 	void (*post_added)(struct evdev_device *device,
320 			   struct evdev_dispatch *dispatch);
321 
322 	/* For touch arbitration, called on the device that should
323 	 * enable/disable touch capabilities.
324 	 */
325 	void (*touch_arbitration_toggle)(struct evdev_dispatch *dispatch,
326 					 struct evdev_device *device,
327 					 enum evdev_arbitration_state which,
328 					 const struct phys_rect *rect, /* may be NULL */
329 					 uint64_t now);
330 
331 	/* Called when touch arbitration is on, updates the area where touch
332 	 * arbitration should apply.
333 	 */
334 	void (*touch_arbitration_update_rect)(struct evdev_dispatch *dispatch,
335 					      struct evdev_device *device,
336 					      const struct phys_rect *rect,
337 					      uint64_t now);
338 
339 
340 	/* Return the state of the given switch */
341 	enum libinput_switch_state
342 		(*get_switch_state)(struct evdev_dispatch *dispatch,
343 				    enum libinput_switch which);
344 
345 	void (*left_handed_toggle)(struct evdev_dispatch *dispatch,
346 				   struct evdev_device *device,
347 				   bool left_handed_enabled);
348 };
349 
350 enum evdev_dispatch_type {
351 	DISPATCH_FALLBACK,
352 	DISPATCH_TOUCHPAD,
353 	DISPATCH_TABLET,
354 	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 	struct {
363 		struct libinput_device_config_send_events config;
364 		enum libinput_config_send_events_mode current_mode;
365 	} sendevents;
366 };
367 
368 static inline void
evdev_verify_dispatch_type(struct evdev_dispatch * dispatch,enum evdev_dispatch_type type)369 evdev_verify_dispatch_type(struct evdev_dispatch *dispatch,
370 			   enum evdev_dispatch_type type)
371 {
372 	if (dispatch->dispatch_type != type)
373 		abort();
374 }
375 
376 struct evdev_device *
377 evdev_device_create(struct libinput_seat *seat,
378 		    struct udev_device *device);
379 
380 static inline struct libinput *
evdev_libinput_context(const struct evdev_device * device)381 evdev_libinput_context(const struct evdev_device *device)
382 {
383 	return device->base.seat->libinput;
384 }
385 
386 static inline bool
evdev_device_has_model_quirk(struct evdev_device * device,enum quirk model_quirk)387 evdev_device_has_model_quirk(struct evdev_device *device,
388 			     enum quirk model_quirk)
389 {
390 	struct quirks_context *quirks;
391 	struct quirks *q;
392 	bool result = false;
393 
394 	assert(quirk_get_name(model_quirk) != NULL);
395 
396 	quirks = evdev_libinput_context(device)->quirks;
397 	q = quirks_fetch_for_device(quirks, device->udev_device);
398 	quirks_get_bool(q, model_quirk, &result);
399 	quirks_unref(q);
400 
401 	return result;
402 }
403 
404 void
405 evdev_transform_absolute(struct evdev_device *device,
406 			 struct device_coords *point);
407 
408 void
409 evdev_transform_relative(struct evdev_device *device,
410 			 struct device_coords *point);
411 
412 void
413 evdev_init_calibration(struct evdev_device *device,
414 		        struct libinput_device_config_calibration *calibration);
415 
416 void
417 evdev_read_calibration_prop(struct evdev_device *device);
418 
419 int
420 evdev_read_fuzz_prop(struct evdev_device *device, unsigned int code);
421 
422 enum switch_reliability
423 evdev_read_switch_reliability_prop(struct evdev_device *device);
424 
425 void
426 evdev_init_sendevents(struct evdev_device *device,
427 		      struct evdev_dispatch *dispatch);
428 
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 struct evdev_dispatch *
437 evdev_mt_touchpad_create(struct evdev_device *device);
438 
439 struct evdev_dispatch *
440 evdev_tablet_create(struct evdev_device *device);
441 
442 struct evdev_dispatch *
443 evdev_tablet_pad_create(struct evdev_device *device);
444 
445 struct evdev_dispatch *
446 evdev_lid_switch_dispatch_create(struct evdev_device *device);
447 
448 struct evdev_dispatch *
449 fallback_dispatch_create(struct libinput_device *libinput_device);
450 
451 struct evdev_dispatch *
452 evdev_totem_create(struct evdev_device *device);
453 
454 bool
455 evdev_is_fake_mt_device(struct evdev_device *device);
456 
457 int
458 evdev_need_mtdev(struct evdev_device *device);
459 
460 void
461 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds);
462 
463 int
464 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size);
465 
466 const char *
467 evdev_device_get_output(struct evdev_device *device);
468 
469 const char *
470 evdev_device_get_sysname(struct evdev_device *device);
471 
472 const char *
473 evdev_device_get_name(struct evdev_device *device);
474 
475 unsigned int
476 evdev_device_get_id_product(struct evdev_device *device);
477 
478 unsigned int
479 evdev_device_get_id_vendor(struct evdev_device *device);
480 
481 struct udev_device *
482 evdev_device_get_udev_device(struct evdev_device *device);
483 
484 void
485 evdev_device_set_default_calibration(struct evdev_device *device,
486 				     const float calibration[6]);
487 void
488 evdev_device_calibrate(struct evdev_device *device,
489 		       const float calibration[6]);
490 
491 bool
492 evdev_device_has_capability(struct evdev_device *device,
493 			    enum libinput_device_capability capability);
494 
495 int
496 evdev_device_get_size(const struct evdev_device *device,
497 		      double *w,
498 		      double *h);
499 
500 int
501 evdev_device_has_button(struct evdev_device *device, uint32_t code);
502 
503 int
504 evdev_device_has_key(struct evdev_device *device, uint32_t code);
505 
506 int
507 evdev_device_get_touch_count(struct evdev_device *device);
508 
509 int
510 evdev_device_has_switch(struct evdev_device *device,
511 			enum libinput_switch sw);
512 
513 int
514 evdev_device_tablet_pad_has_key(struct evdev_device *device,
515 				uint32_t code);
516 
517 int
518 evdev_device_tablet_pad_get_num_buttons(struct evdev_device *device);
519 
520 int
521 evdev_device_tablet_pad_get_num_rings(struct evdev_device *device);
522 
523 int
524 evdev_device_tablet_pad_get_num_strips(struct evdev_device *device);
525 
526 int
527 evdev_device_tablet_pad_get_num_mode_groups(struct evdev_device *device);
528 
529 struct libinput_tablet_pad_mode_group *
530 evdev_device_tablet_pad_get_mode_group(struct evdev_device *device,
531 				       unsigned int index);
532 
533 enum libinput_switch_state
534 evdev_device_switch_get_state(struct evdev_device *device,
535 			      enum libinput_switch sw);
536 
537 double
538 evdev_device_transform_x(struct evdev_device *device,
539 			 double x,
540 			 uint32_t width);
541 
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 int
550 evdev_device_resume(struct evdev_device *device);
551 
552 void
553 evdev_notify_suspended_device(struct evdev_device *device);
554 
555 void
556 evdev_notify_resumed_device(struct evdev_device *device);
557 
558 void
559 evdev_pointer_notify_button(struct evdev_device *device,
560 			    uint64_t time,
561 			    unsigned int button,
562 			    enum libinput_button_state state);
563 void
564 evdev_pointer_notify_physical_button(struct evdev_device *device,
565 				     uint64_t time,
566 				     int button,
567 				     enum libinput_button_state state);
568 
569 void
570 evdev_init_natural_scroll(struct evdev_device *device);
571 
572 void
573 evdev_init_button_scroll(struct evdev_device *device,
574 			 void (*change_scroll_method)(struct evdev_device *));
575 
576 void
577 evdev_set_button_scroll_lock_enabled(struct evdev_device *device,
578 				     bool enabled);
579 
580 int
581 evdev_update_key_down_count(struct evdev_device *device,
582 			    int code,
583 			    int pressed);
584 
585 void
586 evdev_notify_axis(struct evdev_device *device,
587 		  uint64_t time,
588 		  uint32_t axes,
589 		  enum libinput_pointer_axis_source source,
590 		  const struct normalized_coords *delta_in,
591 		  const struct discrete_coords *discrete_in);
592 void
593 evdev_post_scroll(struct evdev_device *device,
594 		  uint64_t time,
595 		  enum libinput_pointer_axis_source source,
596 		  const struct normalized_coords *delta);
597 
598 void
599 evdev_stop_scroll(struct evdev_device *device,
600 		  uint64_t time,
601 		  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 bool
610 evdev_middlebutton_filter_button(struct evdev_device *device,
611 				 uint64_t time,
612 				 int button,
613 				 enum libinput_button_state state);
614 
615 void
616 evdev_init_middlebutton(struct evdev_device *device,
617 			bool enabled,
618 			bool want_config);
619 
620 enum libinput_config_middle_emulation_state
621 evdev_middlebutton_get(struct libinput_device *device);
622 
623 int
624 evdev_middlebutton_is_available(struct libinput_device *device);
625 
626 enum libinput_config_middle_emulation_state
627 evdev_middlebutton_get_default(struct libinput_device *device);
628 
629 static inline double
evdev_convert_to_mm(const struct input_absinfo * absinfo,double v)630 evdev_convert_to_mm(const struct input_absinfo *absinfo, double v)
631 {
632 	double value = v - absinfo->minimum;
633 	return value/absinfo->resolution;
634 }
635 
636 static inline struct phys_coords
evdev_convert_xy_to_mm(const struct evdev_device * device,int x,int y)637 evdev_convert_xy_to_mm(const struct evdev_device *device, int x, int y)
638 {
639 	struct phys_coords mm;
640 
641 	mm.x = evdev_convert_to_mm(device->abs.absinfo_x, x);
642 	mm.y = evdev_convert_to_mm(device->abs.absinfo_y, y);
643 
644 	return mm;
645 }
646 
647 void
648 evdev_init_left_handed(struct evdev_device *device,
649 		       void (*change_to_left_handed)(struct evdev_device *));
650 
651 bool
652 evdev_tablet_has_left_handed(struct evdev_device *device);
653 
654 static inline uint32_t
evdev_to_left_handed(struct evdev_device * device,uint32_t button)655 evdev_to_left_handed(struct evdev_device *device,
656 		     uint32_t button)
657 {
658 	if (device->left_handed.enabled) {
659 		if (button == BTN_LEFT)
660 			return BTN_RIGHT;
661 		else if (button == BTN_RIGHT)
662 			return BTN_LEFT;
663 	}
664 	return button;
665 }
666 
667 /**
668  * Apply a hysteresis filtering to the coordinate in, based on the current
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  * return a point on the edge of the new margin (which is an ellipse, usually
672  * a circle). So for a point x in the space outside c + margin we return r:
673  * ,---.       ,---.
674  * | c |  x →  | r x
675  * `---'       `---'
676  *
677  * The effect of this is that initial small motions are filtered. Once we
678  * move into one direction we lag the real coordinates by 'margin' but any
679  * movement that continues into that direction will always be just outside
680  * margin - we get responsive movement. Once we move back into the other
681  * direction, the first movements are filtered again.
682  *
683  * Returning the edge rather than the point avoids cursor jumps, as the
684  * first reachable coordinate is the point next to the center (center + 1).
685  * Otherwise, the center has a dead zone of size margin around it and the
686  * first reachable point is the margin edge.
687  *
688  * @param in The input coordinate
689  * @param center Current center of the hysteresis
690  * @param margin Hysteresis width (on each side)
691  *
692  * @return The new center of the hysteresis
693  */
694 static inline struct device_coords
evdev_hysteresis(const struct device_coords * in,const struct device_coords * center,const struct device_coords * margin)695 evdev_hysteresis(const struct device_coords *in,
696 		 const struct device_coords *center,
697 		 const struct device_coords *margin)
698 {
699 	int dx = in->x - center->x;
700 	int dy = in->y - center->y;
701 	int dx2 = dx * dx;
702 	int dy2 = dy * dy;
703 	int a = margin->x;
704 	int b = margin->y;
705 	double normalized_finger_distance, finger_distance, margin_distance;
706 	double lag_x, lag_y;
707 	struct device_coords result;
708 
709 	if (!a || !b)
710 		return *in;
711 
712 	/*
713 	 * Basic equation for an ellipse of radii a,b:
714 	 *   x²/a² + y²/b² = 1
715 	 * But we start by making a scaled ellipse passing through the
716 	 * relative finger location (dx,dy). So the scale of this ellipse is
717 	 * the ratio of finger_distance to margin_distance:
718 	 *   dx²/a² + dy²/b² = normalized_finger_distance²
719 	 */
720 	normalized_finger_distance = sqrt((double)dx2 / (a * a) +
721 					  (double)dy2 / (b * b));
722 
723 	/* Which means anything less than 1 is within the elliptical margin */
724 	if (normalized_finger_distance < 1.0)
725 		return *center;
726 
727 	finger_distance = sqrt(dx2 + dy2);
728 	margin_distance = finger_distance / normalized_finger_distance;
729 
730 	/*
731 	 * Now calculate the x,y coordinates on the edge of the margin ellipse
732 	 * where it intersects the finger vector. Shortcut: We achieve this by
733 	 * finding the point with the same gradient as dy/dx.
734 	 */
735 	if (dx) {
736 		double gradient = (double)dy / dx;
737 		lag_x = margin_distance / sqrt(gradient * gradient + 1);
738 		lag_y = sqrt((margin_distance + lag_x) *
739 			     (margin_distance - lag_x));
740 	} else {  /* Infinite gradient */
741 		lag_x = 0.0;
742 		lag_y = margin_distance;
743 	}
744 
745 	/*
746 	 * 'result' is the centre of an ellipse (radii a,b) which has been
747 	 * dragged by the finger moving inside it to 'in'. The finger is now
748 	 * touching the margin ellipse at some point: (±lag_x,±lag_y)
749 	 */
750 	result.x = (dx >= 0) ? in->x - lag_x : in->x + lag_x;
751 	result.y = (dy >= 0) ? in->y - lag_y : in->y + lag_y;
752 	return result;
753 }
754 
755 LIBINPUT_ATTRIBUTE_PRINTF(3, 4)
756 static inline void
evdev_log_msg(struct evdev_device * device,enum libinput_log_priority priority,const char * format,...)757 evdev_log_msg(struct evdev_device *device,
758 	      enum libinput_log_priority priority,
759 	      const char *format,
760 	      ...)
761 {
762 	va_list args;
763 	char buf[1024];
764 
765 	if (!is_logged(evdev_libinput_context(device), priority))
766 		return;
767 
768 	/* Anything info and above is user-visible, use the device name */
769 	snprintf(buf,
770 		 sizeof(buf),
771 		 "%-7s - %s%s%s",
772 		 evdev_device_get_sysname(device),
773 		 (priority > LIBINPUT_LOG_PRIORITY_DEBUG) ?  device->devname : "",
774 		 (priority > LIBINPUT_LOG_PRIORITY_DEBUG) ?  ": " : "",
775 		 format);
776 
777 	va_start(args, format);
778 	log_msg_va(evdev_libinput_context(device), priority, buf, args);
779 	va_end(args);
780 
781 }
782 
783 LIBINPUT_ATTRIBUTE_PRINTF(4, 5)
784 static inline void
evdev_log_msg_ratelimit(struct evdev_device * device,struct ratelimit * ratelimit,enum libinput_log_priority priority,const char * format,...)785 evdev_log_msg_ratelimit(struct evdev_device *device,
786 			struct ratelimit *ratelimit,
787 			enum libinput_log_priority priority,
788 			const char *format,
789 			...)
790 {
791 	va_list args;
792 	char buf[1024];
793 
794 	enum ratelimit_state state;
795 
796 	if (!is_logged(evdev_libinput_context(device), priority))
797 		return;
798 
799 	state = ratelimit_test(ratelimit);
800 	if (state == RATELIMIT_EXCEEDED)
801 		return;
802 
803 	/* Anything info and above is user-visible, use the device name */
804 	snprintf(buf,
805 		 sizeof(buf),
806 		 "%-7s - %s%s%s",
807 		 evdev_device_get_sysname(device),
808 		 (priority > LIBINPUT_LOG_PRIORITY_DEBUG) ?  device->devname : "",
809 		 (priority > LIBINPUT_LOG_PRIORITY_DEBUG) ?  ": " : "",
810 		 format);
811 
812 	va_start(args, format);
813 	log_msg_va(evdev_libinput_context(device), priority, buf, args);
814 	va_end(args);
815 
816 	if (state == RATELIMIT_THRESHOLD) {
817 		struct human_time ht = to_human_time(ratelimit->interval);
818 		evdev_log_msg(device,
819 			      priority,
820 			      "WARNING: log rate limit exceeded (%d msgs per %d%s). "
821 			      "Discarding future messages.\n",
822 			      ratelimit->burst,
823 			      ht.value,
824 			      ht.unit);
825 
826 	}
827 }
828 
829 #define evdev_log_debug(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_DEBUG, __VA_ARGS__)
830 #define evdev_log_info(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_INFO, __VA_ARGS__)
831 #define evdev_log_error(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_ERROR, __VA_ARGS__)
832 #define evdev_log_bug_kernel(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_ERROR, "kernel bug: " __VA_ARGS__)
833 #define evdev_log_bug_libinput(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_ERROR, "libinput bug: " __VA_ARGS__)
834 #define evdev_log_bug_client(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_ERROR, "client bug: " __VA_ARGS__)
835 
836 #define evdev_log_debug_ratelimit(d_, r_, ...) \
837 	evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_DEBUG, __VA_ARGS__)
838 #define evdev_log_info_ratelimit(d_, r_, ...) \
839 	evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_INFO, __VA_ARGS__)
840 #define evdev_log_error_ratelimit(d_, r_, ...) \
841 	evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, __VA_ARGS__)
842 #define evdev_log_bug_kernel_ratelimit(d_, r_, ...) \
843 	evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, "kernel bug: " __VA_ARGS__)
844 #define evdev_log_bug_libinput_ratelimit(d_, r_, ...) \
845 	evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, "libinput bug: " __VA_ARGS__)
846 #define evdev_log_bug_client_ratelimit(d_, r_, ...) \
847 	evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, "client bug: " __VA_ARGS__)
848 
849 /**
850  * Convert the pair of delta coordinates in device space to mm.
851  */
852 static inline struct phys_coords
evdev_device_unit_delta_to_mm(const struct evdev_device * device,const struct device_coords * units)853 evdev_device_unit_delta_to_mm(const struct evdev_device* device,
854 			      const struct device_coords *units)
855 {
856 	struct phys_coords mm = { 0,  0 };
857 	const struct input_absinfo *absx, *absy;
858 
859 	if (device->abs.absinfo_x == NULL ||
860 	    device->abs.absinfo_y == NULL) {
861 		log_bug_libinput(evdev_libinput_context(device),
862 				 "%s: is not an abs device\n",
863 				 device->devname);
864 		return mm;
865 	}
866 
867 	absx = device->abs.absinfo_x;
868 	absy = device->abs.absinfo_y;
869 
870 	mm.x = 1.0 * units->x/absx->resolution;
871 	mm.y = 1.0 * units->y/absy->resolution;
872 
873 	return mm;
874 }
875 
876 /**
877  * Convert the pair of coordinates in device space to mm. This takes the
878  * axis min into account, i.e. a unit of min is equivalent to 0 mm.
879  */
880 static inline struct phys_coords
evdev_device_units_to_mm(const struct evdev_device * device,const struct device_coords * units)881 evdev_device_units_to_mm(const struct evdev_device* device,
882 			 const struct device_coords *units)
883 {
884 	struct phys_coords mm = { 0,  0 };
885 	const struct input_absinfo *absx, *absy;
886 
887 	if (device->abs.absinfo_x == NULL ||
888 	    device->abs.absinfo_y == NULL) {
889 		log_bug_libinput(evdev_libinput_context(device),
890 				 "%s: is not an abs device\n",
891 				 device->devname);
892 		return mm;
893 	}
894 
895 	absx = device->abs.absinfo_x;
896 	absy = device->abs.absinfo_y;
897 
898 	mm.x = (units->x - absx->minimum)/absx->resolution;
899 	mm.y = (units->y - absy->minimum)/absy->resolution;
900 
901 	return mm;
902 }
903 
904 /**
905  * Convert the pair of coordinates in mm to device units. This takes the
906  * axis min into account, i.e. 0 mm  is equivalent to the min.
907  */
908 static inline struct device_coords
evdev_device_mm_to_units(const struct evdev_device * device,const struct phys_coords * mm)909 evdev_device_mm_to_units(const struct evdev_device *device,
910 			 const struct phys_coords *mm)
911 {
912 	struct device_coords units = { 0,  0 };
913 	const struct input_absinfo *absx, *absy;
914 
915 	if (device->abs.absinfo_x == NULL ||
916 	    device->abs.absinfo_y == NULL) {
917 		log_bug_libinput(evdev_libinput_context(device),
918 				 "%s: is not an abs device\n",
919 				 device->devname);
920 		return units;
921 	}
922 
923 	absx = device->abs.absinfo_x;
924 	absy = device->abs.absinfo_y;
925 
926 	units.x = mm->x * absx->resolution + absx->minimum;
927 	units.y = mm->y * absy->resolution + absy->minimum;
928 
929 	return units;
930 }
931 
932 static inline struct device_coord_rect
evdev_phys_rect_to_units(const struct evdev_device * device,const struct phys_rect * mm)933 evdev_phys_rect_to_units(const struct evdev_device *device,
934 			 const struct phys_rect *mm)
935 {
936 	struct device_coord_rect units = {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 	units.w = mm->w * absx->resolution;
953 	units.h = mm->h * absy->resolution;
954 
955 	return units;
956 }
957 
958 static inline void
evdev_device_init_abs_range_warnings(struct evdev_device * device)959 evdev_device_init_abs_range_warnings(struct evdev_device *device)
960 {
961 	const struct input_absinfo *x, *y;
962 	int width, height;
963 
964 	x = device->abs.absinfo_x;
965 	y = device->abs.absinfo_y;
966 	width = device->abs.dimensions.x;
967 	height = device->abs.dimensions.y;
968 
969 	device->abs.warning_range.min.x = x->minimum - 0.05 * width;
970 	device->abs.warning_range.min.y = y->minimum - 0.05 * height;
971 	device->abs.warning_range.max.x = x->maximum + 0.05 * width;
972 	device->abs.warning_range.max.y = y->maximum + 0.05 * height;
973 
974 	/* One warning every 5 min is enough */
975 	ratelimit_init(&device->abs.warning_range.range_warn_limit,
976 		       s2us(3000),
977 		       1);
978 }
979 
980 static inline void
evdev_device_check_abs_axis_range(struct evdev_device * device,unsigned int code,int value)981 evdev_device_check_abs_axis_range(struct evdev_device *device,
982 				  unsigned int code,
983 				  int value)
984 {
985 	int min, max;
986 
987 	switch(code) {
988 	case ABS_X:
989 	case ABS_MT_POSITION_X:
990 		min = device->abs.warning_range.min.x;
991 		max = device->abs.warning_range.max.x;
992 		break;
993 	case ABS_Y:
994 	case ABS_MT_POSITION_Y:
995 		min = device->abs.warning_range.min.y;
996 		max = device->abs.warning_range.max.y;
997 		break;
998 	default:
999 		return;
1000 	}
1001 
1002 	if (value < min || value > max) {
1003 		log_info_ratelimit(evdev_libinput_context(device),
1004 				   &device->abs.warning_range.range_warn_limit,
1005 				   "Axis %#x value %d is outside expected range [%d, %d]\n"
1006 				   "See %sabsolute_coordinate_ranges.html for details\n",
1007 				   code, value, min, max,
1008 				   HTTP_DOC_LINK);
1009 	}
1010 }
1011 
1012 struct evdev_paired_keyboard {
1013 	struct list link;
1014 	struct evdev_device *device;
1015 	struct libinput_event_listener listener;
1016 };
1017 
1018 static inline void
evdev_paired_keyboard_destroy(struct evdev_paired_keyboard * kbd)1019 evdev_paired_keyboard_destroy(struct evdev_paired_keyboard *kbd)
1020 {
1021 	kbd->device = NULL;
1022 	libinput_device_remove_event_listener(&kbd->listener);
1023 	list_remove(&kbd->link);
1024 	free(kbd);
1025 }
1026 
1027 #endif /* EVDEV_H */
1028