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 = (1 << 0),
47 	EVDEV_ABSOLUTE_MOTION = (1 << 1),
48 	EVDEV_ABSOLUTE_TOUCH_UP = (1 << 2),
49 	EVDEV_ABSOLUTE_MT = (1 << 3),
50 	EVDEV_WHEEL = (1 << 4),
51 	EVDEV_KEY = (1 << 5),
52 	EVDEV_RELATIVE_MOTION = (1 << 6),
53 	EVDEV_BUTTON = (1 << 7),
54 };
55 
56 enum evdev_device_seat_capability {
57 	EVDEV_DEVICE_POINTER = (1 << 0),
58 	EVDEV_DEVICE_KEYBOARD = (1 << 1),
59 	EVDEV_DEVICE_TOUCH = (1 << 2),
60 	EVDEV_DEVICE_TABLET = (1 << 3),
61 	EVDEV_DEVICE_TABLET_PAD = (1 << 4),
62 	EVDEV_DEVICE_GESTURE = (1 << 5),
63 	EVDEV_DEVICE_SWITCH = (1 << 6),
64 };
65 
66 enum evdev_device_tags {
67 	EVDEV_TAG_EXTERNAL_MOUSE = (1 << 0),
68 	EVDEV_TAG_INTERNAL_TOUCHPAD = (1 << 1),
69 	EVDEV_TAG_EXTERNAL_TOUCHPAD = (1 << 2),
70 	EVDEV_TAG_TRACKPOINT = (1 << 3),
71 	EVDEV_TAG_KEYBOARD = (1 << 4),
72 	EVDEV_TAG_LID_SWITCH = (1 << 5),
73 	EVDEV_TAG_INTERNAL_KEYBOARD = (1 << 6),
74 	EVDEV_TAG_EXTERNAL_KEYBOARD = (1 << 7),
75 	EVDEV_TAG_TABLET_MODE_SWITCH = (1 << 8),
76 	EVDEV_TAG_TABLET_TOUCHPAD = (1 << 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		= (1 << 1),
110 	EVDEV_MODEL_SYNAPTICS_SERIAL_TOUCHPAD	= (1 << 2),
111 	EVDEV_MODEL_LENOVO_T450_TOUCHPAD	= (1 << 4),
112 	EVDEV_MODEL_APPLE_TOUCHPAD_ONEBUTTON	= (1 << 5),
113 	EVDEV_MODEL_LENOVO_SCROLLPOINT		= (1 << 6),
114 
115 	/* udev tags, not true quirks */
116 	EVDEV_MODEL_TEST_DEVICE			= (1 << 20),
117 	EVDEV_MODEL_TRACKBALL			= (1 << 21),
118 	EVDEV_MODEL_LENOVO_X220_TOUCHPAD_FW81	= (1 << 22),
119 };
120 
121 enum evdev_button_scroll_state {
122 	BUTTONSCROLL_IDLE,
123 	BUTTONSCROLL_BUTTON_DOWN,	/* button is down */
124 	BUTTONSCROLL_READY,		/* ready for scroll events */
125 	BUTTONSCROLL_SCROLLING,		/* have sent scroll events */
126 };
127 
128 enum evdev_debounce_state {
129 	/**
130 	 * Initial state, no debounce but monitoring events
131 	 */
132 	DEBOUNCE_INIT,
133 	/**
134 	 * Bounce detected, future events need debouncing
135 	 */
136 	DEBOUNCE_NEEDED,
137 	/**
138 	 * Debounce is enabled, but no event is currently being filtered
139 	 */
140 	DEBOUNCE_ON,
141 	/**
142 	 * Debounce is enabled and we are currently filtering an event
143 	 */
144 	DEBOUNCE_ACTIVE,
145 };
146 
147 struct evdev_device {
148 	struct libinput_device base;
149 
150 	struct libinput_source *source;
151 
152 	struct evdev_dispatch *dispatch;
153 	struct libevdev *evdev;
154 	struct udev_device *udev_device;
155 	char *output_name;
156 	const char *devname;
157 	bool was_removed;
158 	int fd;
159 	enum evdev_device_seat_capability seat_caps;
160 	enum evdev_device_tags tags;
161 	bool is_mt;
162 	bool is_suspended;
163 	int dpi; /* HW resolution */
164 	double trackpoint_multiplier; /* trackpoint constant multiplier */
165 	bool use_velocity_averaging; /* whether averaging should be applied on velocity calculation */
166 	struct ratelimit syn_drop_limit; /* ratelimit for SYN_DROPPED logging */
167 	struct ratelimit nonpointer_rel_limit; /* ratelimit for REL_* events from non-pointer devices */
168 	uint32_t model_flags;
169 	struct mtdev *mtdev;
170 
171 	struct {
172 		const struct input_absinfo *absinfo_x, *absinfo_y;
173 		bool is_fake_resolution;
174 
175 		int apply_calibration;
176 		struct matrix calibration;
177 		struct matrix default_calibration; /* from LIBINPUT_CALIBRATION_MATRIX */
178 		struct matrix usermatrix; /* as supplied by the caller */
179 
180 		struct device_coords dimensions;
181 
182 		struct {
183 			struct device_coords min, max;
184 			struct ratelimit range_warn_limit;
185 		} warning_range;
186 	} abs;
187 
188 	struct {
189 		struct libinput_timer timer;
190 		struct libinput_device_config_scroll_method config;
191 		/* Currently enabled method, button */
192 		enum libinput_config_scroll_method method;
193 		uint32_t button;
194 		uint64_t button_down_time;
195 
196 		/* set during device init, used at runtime to delay changes
197 		 * until all buttons are up */
198 		enum libinput_config_scroll_method want_method;
199 		uint32_t want_button;
200 		/* Checks if buttons are down and commits the setting */
201 		void (*change_scroll_method)(struct evdev_device *device);
202 		enum evdev_button_scroll_state button_scroll_state;
203 		double threshold;
204 		double direction_lock_threshold;
205 		uint32_t direction;
206 		struct normalized_coords buildup;
207 
208 		struct libinput_device_config_natural_scroll config_natural;
209 		/* set during device init if we want natural scrolling,
210 		 * used at runtime to enable/disable the feature */
211 		bool natural_scrolling_enabled;
212 
213 		/* angle per REL_WHEEL click in degrees */
214 		struct wheel_angle wheel_click_angle;
215 
216 		struct wheel_tilt_flags is_tilt;
217 	} scroll;
218 
219 	struct {
220 		struct libinput_device_config_accel config;
221 		struct motion_filter *filter;
222 	} pointer;
223 
224 	/* Key counter used for multiplexing button events internally in
225 	 * libinput. */
226 	uint8_t key_count[KEY_CNT];
227 
228 	struct {
229 		struct libinput_device_config_left_handed config;
230 		/* left-handed currently enabled */
231 		bool enabled;
232 		/* set during device init if we want left_handed config,
233 		 * used at runtime to delay the effect until buttons are up */
234 		bool want_enabled;
235 		/* Checks if buttons are down and commits the setting */
236 		void (*change_to_enabled)(struct evdev_device *device);
237 	} left_handed;
238 
239 	struct {
240 		struct libinput_device_config_middle_emulation config;
241 		/* middle-button emulation enabled */
242 		bool enabled;
243 		bool enabled_default;
244 		bool want_enabled;
245 		enum evdev_middlebutton_state state;
246 		struct libinput_timer timer;
247 		uint32_t button_mask;
248 		uint64_t first_event_time;
249 	} middlebutton;
250 };
251 
252 static inline struct evdev_device *
evdev_device(struct libinput_device * device)253 evdev_device(struct libinput_device *device)
254 {
255 	return container_of(device, struct evdev_device, base);
256 }
257 
258 #define EVDEV_UNHANDLED_DEVICE ((struct evdev_device *) 1)
259 
260 struct evdev_dispatch;
261 
262 struct evdev_dispatch_interface {
263 	/* Process an evdev input event. */
264 	void (*process)(struct evdev_dispatch *dispatch,
265 			struct evdev_device *device,
266 			struct input_event *event,
267 			uint64_t time);
268 
269 	/* Device is being suspended */
270 	void (*suspend)(struct evdev_dispatch *dispatch,
271 			struct evdev_device *device);
272 
273 	/* Device is being removed (may be NULL) */
274 	void (*remove)(struct evdev_dispatch *dispatch);
275 
276 	/* Destroy an event dispatch handler and free all its resources. */
277 	void (*destroy)(struct evdev_dispatch *dispatch);
278 
279 	/* A new device was added */
280 	void (*device_added)(struct evdev_device *device,
281 			     struct evdev_device *added_device);
282 
283 	/* A device was removed */
284 	void (*device_removed)(struct evdev_device *device,
285 			       struct evdev_device *removed_device);
286 
287 	/* A device was suspended */
288 	void (*device_suspended)(struct evdev_device *device,
289 				 struct evdev_device *suspended_device);
290 
291 	/* A device was resumed */
292 	void (*device_resumed)(struct evdev_device *device,
293 			       struct evdev_device *resumed_device);
294 
295 	/* Called immediately after the LIBINPUT_EVENT_DEVICE_ADDED event
296 	 * was sent */
297 	void (*post_added)(struct evdev_device *device,
298 			   struct evdev_dispatch *dispatch);
299 
300 	/* For touch arbitration, called on the device that should
301 	 * enable/disable touch capabilities */
302 	void (*toggle_touch)(struct evdev_dispatch *dispatch,
303 			     struct evdev_device *device,
304 			     bool enable,
305 			     uint64_t now);
306 
307 	/* Return the state of the given switch */
308 	enum libinput_switch_state
309 		(*get_switch_state)(struct evdev_dispatch *dispatch,
310 				    enum libinput_switch which);
311 };
312 
313 enum evdev_dispatch_type {
314 	DISPATCH_FALLBACK,
315 	DISPATCH_TOUCHPAD,
316 	DISPATCH_TABLET,
317 	DISPATCH_TABLET_PAD,
318 };
319 
320 struct evdev_dispatch {
321 	enum evdev_dispatch_type dispatch_type;
322 	struct evdev_dispatch_interface *interface;
323 
324 	struct {
325 		struct libinput_device_config_send_events config;
326 		enum libinput_config_send_events_mode current_mode;
327 	} sendevents;
328 };
329 
330 static inline void
evdev_verify_dispatch_type(struct evdev_dispatch * dispatch,enum evdev_dispatch_type type)331 evdev_verify_dispatch_type(struct evdev_dispatch *dispatch,
332 			   enum evdev_dispatch_type type)
333 {
334 	if (dispatch->dispatch_type != type)
335 		abort();
336 }
337 
338 struct evdev_device *
339 evdev_device_create(struct libinput_seat *seat,
340 		    struct udev_device *device);
341 
342 static inline struct libinput *
evdev_libinput_context(const struct evdev_device * device)343 evdev_libinput_context(const struct evdev_device *device)
344 {
345 	return device->base.seat->libinput;
346 }
347 
348 static inline bool
evdev_device_has_model_quirk(struct evdev_device * device,enum quirk model_quirk)349 evdev_device_has_model_quirk(struct evdev_device *device,
350 			     enum quirk model_quirk)
351 {
352 	struct quirks_context *quirks;
353 	struct quirks *q;
354 	bool result = false;
355 
356 	assert(quirk_get_name(model_quirk) != NULL);
357 
358 	quirks = evdev_libinput_context(device)->quirks;
359 	q = quirks_fetch_for_device(quirks, device->udev_device);
360 	quirks_get_bool(q, model_quirk, &result);
361 	quirks_unref(q);
362 
363 	return result;
364 }
365 
366 void
367 evdev_transform_absolute(struct evdev_device *device,
368 			 struct device_coords *point);
369 
370 void
371 evdev_transform_relative(struct evdev_device *device,
372 			 struct device_coords *point);
373 
374 void
375 evdev_init_calibration(struct evdev_device *device,
376 		        struct libinput_device_config_calibration *calibration);
377 
378 void
379 evdev_read_calibration_prop(struct evdev_device *device);
380 
381 int
382 evdev_read_fuzz_prop(struct evdev_device *device, unsigned int code);
383 
384 enum switch_reliability
385 evdev_read_switch_reliability_prop(struct evdev_device *device);
386 
387 void
388 evdev_init_sendevents(struct evdev_device *device,
389 		      struct evdev_dispatch *dispatch);
390 
391 void
392 evdev_device_init_pointer_acceleration(struct evdev_device *device,
393 				       struct motion_filter *filter);
394 
395 struct evdev_dispatch *
396 evdev_touchpad_create(struct evdev_device *device);
397 
398 struct evdev_dispatch *
399 evdev_mt_touchpad_create(struct evdev_device *device);
400 
401 struct evdev_dispatch *
402 evdev_tablet_create(struct evdev_device *device);
403 
404 struct evdev_dispatch *
405 evdev_tablet_pad_create(struct evdev_device *device);
406 
407 struct evdev_dispatch *
408 evdev_lid_switch_dispatch_create(struct evdev_device *device);
409 
410 struct evdev_dispatch *
411 fallback_dispatch_create(struct libinput_device *libinput_device);
412 
413 bool
414 evdev_is_fake_mt_device(struct evdev_device *device);
415 
416 int
417 evdev_need_mtdev(struct evdev_device *device);
418 
419 void
420 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds);
421 
422 int
423 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size);
424 
425 const char *
426 evdev_device_get_output(struct evdev_device *device);
427 
428 const char *
429 evdev_device_get_sysname(struct evdev_device *device);
430 
431 const char *
432 evdev_device_get_name(struct evdev_device *device);
433 
434 unsigned int
435 evdev_device_get_id_product(struct evdev_device *device);
436 
437 unsigned int
438 evdev_device_get_id_vendor(struct evdev_device *device);
439 
440 struct udev_device *
441 evdev_device_get_udev_device(struct evdev_device *device);
442 
443 void
444 evdev_device_set_default_calibration(struct evdev_device *device,
445 				     const float calibration[6]);
446 void
447 evdev_device_calibrate(struct evdev_device *device,
448 		       const float calibration[6]);
449 
450 bool
451 evdev_device_has_capability(struct evdev_device *device,
452 			    enum libinput_device_capability capability);
453 
454 int
455 evdev_device_get_size(const struct evdev_device *device,
456 		      double *w,
457 		      double *h);
458 
459 int
460 evdev_device_has_button(struct evdev_device *device, uint32_t code);
461 
462 int
463 evdev_device_has_key(struct evdev_device *device, uint32_t code);
464 
465 int
466 evdev_device_get_touch_count(struct evdev_device *device);
467 
468 int
469 evdev_device_has_switch(struct evdev_device *device,
470 			enum libinput_switch sw);
471 
472 int
473 evdev_device_tablet_pad_get_num_buttons(struct evdev_device *device);
474 
475 int
476 evdev_device_tablet_pad_get_num_rings(struct evdev_device *device);
477 
478 int
479 evdev_device_tablet_pad_get_num_strips(struct evdev_device *device);
480 
481 int
482 evdev_device_tablet_pad_get_num_mode_groups(struct evdev_device *device);
483 
484 struct libinput_tablet_pad_mode_group *
485 evdev_device_tablet_pad_get_mode_group(struct evdev_device *device,
486 				       unsigned int index);
487 
488 enum libinput_switch_state
489 evdev_device_switch_get_state(struct evdev_device *device,
490 			      enum libinput_switch sw);
491 
492 double
493 evdev_device_transform_x(struct evdev_device *device,
494 			 double x,
495 			 uint32_t width);
496 
497 double
498 evdev_device_transform_y(struct evdev_device *device,
499 			 double y,
500 			 uint32_t height);
501 void
502 evdev_device_suspend(struct evdev_device *device);
503 
504 int
505 evdev_device_resume(struct evdev_device *device);
506 
507 void
508 evdev_notify_suspended_device(struct evdev_device *device);
509 
510 void
511 evdev_notify_resumed_device(struct evdev_device *device);
512 
513 void
514 evdev_pointer_notify_button(struct evdev_device *device,
515 			    uint64_t time,
516 			    unsigned int button,
517 			    enum libinput_button_state state);
518 void
519 evdev_pointer_notify_physical_button(struct evdev_device *device,
520 				     uint64_t time,
521 				     int button,
522 				     enum libinput_button_state state);
523 
524 void
525 evdev_init_natural_scroll(struct evdev_device *device);
526 
527 void
528 evdev_init_button_scroll(struct evdev_device *device,
529 			 void (*change_scroll_method)(struct evdev_device *));
530 
531 int
532 evdev_update_key_down_count(struct evdev_device *device,
533 			    int code,
534 			    int pressed);
535 
536 void
537 evdev_notify_axis(struct evdev_device *device,
538 		  uint64_t time,
539 		  uint32_t axes,
540 		  enum libinput_pointer_axis_source source,
541 		  const struct normalized_coords *delta_in,
542 		  const struct discrete_coords *discrete_in);
543 void
544 evdev_post_scroll(struct evdev_device *device,
545 		  uint64_t time,
546 		  enum libinput_pointer_axis_source source,
547 		  const struct normalized_coords *delta);
548 
549 void
550 evdev_stop_scroll(struct evdev_device *device,
551 		  uint64_t time,
552 		  enum libinput_pointer_axis_source source);
553 
554 void
555 evdev_device_remove(struct evdev_device *device);
556 
557 void
558 evdev_device_destroy(struct evdev_device *device);
559 
560 bool
561 evdev_middlebutton_filter_button(struct evdev_device *device,
562 				 uint64_t time,
563 				 int button,
564 				 enum libinput_button_state state);
565 
566 void
567 evdev_init_middlebutton(struct evdev_device *device,
568 			bool enabled,
569 			bool want_config);
570 
571 enum libinput_config_middle_emulation_state
572 evdev_middlebutton_get(struct libinput_device *device);
573 
574 int
575 evdev_middlebutton_is_available(struct libinput_device *device);
576 
577 enum libinput_config_middle_emulation_state
578 evdev_middlebutton_get_default(struct libinput_device *device);
579 
580 static inline double
evdev_convert_to_mm(const struct input_absinfo * absinfo,double v)581 evdev_convert_to_mm(const struct input_absinfo *absinfo, double v)
582 {
583 	double value = v - absinfo->minimum;
584 	return value/absinfo->resolution;
585 }
586 
587 static inline struct phys_coords
evdev_convert_xy_to_mm(const struct evdev_device * device,int x,int y)588 evdev_convert_xy_to_mm(const struct evdev_device *device, int x, int y)
589 {
590 	struct phys_coords mm;
591 
592 	mm.x = evdev_convert_to_mm(device->abs.absinfo_x, x);
593 	mm.y = evdev_convert_to_mm(device->abs.absinfo_y, y);
594 
595 	return mm;
596 }
597 
598 void
599 evdev_init_left_handed(struct evdev_device *device,
600 		       void (*change_to_left_handed)(struct evdev_device *));
601 
602 bool
603 evdev_tablet_has_left_handed(struct evdev_device *device);
604 
605 static inline uint32_t
evdev_to_left_handed(struct evdev_device * device,uint32_t button)606 evdev_to_left_handed(struct evdev_device *device,
607 		     uint32_t button)
608 {
609 	if (device->left_handed.enabled) {
610 		if (button == BTN_LEFT)
611 			return BTN_RIGHT;
612 		else if (button == BTN_RIGHT)
613 			return BTN_LEFT;
614 	}
615 	return button;
616 }
617 
618 /**
619  * Apply a hysteresis filtering to the coordinate in, based on the current
620  * hysteresis center and the margin. If 'in' is within 'margin' of center,
621  * return the center (and thus filter the motion). If 'in' is outside,
622  * return a point on the edge of the new margin (which is an ellipse, usually
623  * a circle). So for a point x in the space outside c + margin we return r:
624  * ,---.       ,---.
625  * | c |  x →  | r x
626  * `---'       `---'
627  *
628  * The effect of this is that initial small motions are filtered. Once we
629  * move into one direction we lag the real coordinates by 'margin' but any
630  * movement that continues into that direction will always be just outside
631  * margin - we get responsive movement. Once we move back into the other
632  * direction, the first movements are filtered again.
633  *
634  * Returning the edge rather than the point avoids cursor jumps, as the
635  * first reachable coordinate is the point next to the center (center + 1).
636  * Otherwise, the center has a dead zone of size margin around it and the
637  * first reachable point is the margin edge.
638  *
639  * @param in The input coordinate
640  * @param center Current center of the hysteresis
641  * @param margin Hysteresis width (on each side)
642  *
643  * @return The new center of the hysteresis
644  */
645 static inline struct device_coords
evdev_hysteresis(const struct device_coords * in,const struct device_coords * center,const struct device_coords * margin)646 evdev_hysteresis(const struct device_coords *in,
647 		 const struct device_coords *center,
648 		 const struct device_coords *margin)
649 {
650 	int dx = in->x - center->x;
651 	int dy = in->y - center->y;
652 	int dx2 = dx * dx;
653 	int dy2 = dy * dy;
654 	int a = margin->x;
655 	int b = margin->y;
656 	double normalized_finger_distance, finger_distance, margin_distance;
657 	double lag_x, lag_y;
658 	struct device_coords result;
659 
660 	if (!a || !b)
661 		return *in;
662 
663 	/*
664 	 * Basic equation for an ellipse of radii a,b:
665 	 *   x²/a² + y²/b² = 1
666 	 * But we start by making a scaled ellipse passing through the
667 	 * relative finger location (dx,dy). So the scale of this ellipse is
668 	 * the ratio of finger_distance to margin_distance:
669 	 *   dx²/a² + dy²/b² = normalized_finger_distance²
670 	 */
671 	normalized_finger_distance = sqrt((double)dx2 / (a * a) +
672 					  (double)dy2 / (b * b));
673 
674 	/* Which means anything less than 1 is within the elliptical margin */
675 	if (normalized_finger_distance < 1.0)
676 		return *center;
677 
678 	finger_distance = sqrt(dx2 + dy2);
679 	margin_distance = finger_distance / normalized_finger_distance;
680 
681 	/*
682 	 * Now calculate the x,y coordinates on the edge of the margin ellipse
683 	 * where it intersects the finger vector. Shortcut: We achieve this by
684 	 * finding the point with the same gradient as dy/dx.
685 	 */
686 	if (dx) {
687 		double gradient = (double)dy / dx;
688 		lag_x = margin_distance / sqrt(gradient * gradient + 1);
689 		lag_y = sqrt((margin_distance + lag_x) *
690 			     (margin_distance - lag_x));
691 	} else {  /* Infinite gradient */
692 		lag_x = 0.0;
693 		lag_y = margin_distance;
694 	}
695 
696 	/*
697 	 * 'result' is the centre of an ellipse (radii a,b) which has been
698 	 * dragged by the finger moving inside it to 'in'. The finger is now
699 	 * touching the margin ellipse at some point: (±lag_x,±lag_y)
700 	 */
701 	result.x = (dx >= 0) ? in->x - lag_x : in->x + lag_x;
702 	result.y = (dy >= 0) ? in->y - lag_y : in->y + lag_y;
703 	return result;
704 }
705 
706 LIBINPUT_ATTRIBUTE_PRINTF(3, 0)
707 static inline void
evdev_log_msg_va(struct evdev_device * device,enum libinput_log_priority priority,const char * format,va_list args)708 evdev_log_msg_va(struct evdev_device *device,
709 		 enum libinput_log_priority priority,
710 		 const char *format,
711 		 va_list args)
712 {
713 	char buf[1024];
714 
715 	/* Anything info and above is user-visible, use the device name */
716 	snprintf(buf,
717 		 sizeof(buf),
718 		 "%-7s - %s%s%s",
719 		 evdev_device_get_sysname(device),
720 		 (priority > LIBINPUT_LOG_PRIORITY_DEBUG) ?  device->devname : "",
721 		 (priority > LIBINPUT_LOG_PRIORITY_DEBUG) ?  ": " : "",
722 		 format);
723 
724 	log_msg_va(evdev_libinput_context(device), priority, buf, args);
725 }
726 
727 LIBINPUT_ATTRIBUTE_PRINTF(3, 4)
728 static inline void
evdev_log_msg(struct evdev_device * device,enum libinput_log_priority priority,const char * format,...)729 evdev_log_msg(struct evdev_device *device,
730 	      enum libinput_log_priority priority,
731 	      const char *format,
732 	      ...)
733 {
734 	va_list args;
735 
736 	va_start(args, format);
737 	evdev_log_msg_va(device, priority, format, args);
738 	va_end(args);
739 
740 }
741 
742 LIBINPUT_ATTRIBUTE_PRINTF(4, 5)
743 static inline void
evdev_log_msg_ratelimit(struct evdev_device * device,struct ratelimit * ratelimit,enum libinput_log_priority priority,const char * format,...)744 evdev_log_msg_ratelimit(struct evdev_device *device,
745 			struct ratelimit *ratelimit,
746 			enum libinput_log_priority priority,
747 			const char *format,
748 			...)
749 {
750 	va_list args;
751 	enum ratelimit_state state;
752 
753 	state = ratelimit_test(ratelimit);
754 	if (state == RATELIMIT_EXCEEDED)
755 		return;
756 
757 	va_start(args, format);
758 	evdev_log_msg_va(device, priority, format, args);
759 	va_end(args);
760 
761 	if (state == RATELIMIT_THRESHOLD)
762 		evdev_log_msg(device,
763 			      priority,
764 			      "WARNING: log rate limit exceeded (%d msgs per %dms). Discarding future messages.\n",
765 			      ratelimit->burst,
766 			      us2ms(ratelimit->interval));
767 }
768 
769 #define evdev_log_debug(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_DEBUG, __VA_ARGS__)
770 #define evdev_log_info(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_INFO, __VA_ARGS__)
771 #define evdev_log_error(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_ERROR, __VA_ARGS__)
772 #define evdev_log_bug_kernel(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_ERROR, "kernel bug: " __VA_ARGS__)
773 #define evdev_log_bug_libinput(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_ERROR, "libinput bug: " __VA_ARGS__)
774 #define evdev_log_bug_client(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_ERROR, "client bug: " __VA_ARGS__)
775 
776 #define evdev_log_debug_ratelimit(d_, r_, ...) \
777 	evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_DEBUG, __VA_ARGS__)
778 #define evdev_log_info_ratelimit(d_, r_, ...) \
779 	evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_INFO, __VA_ARGS__)
780 #define evdev_log_error_ratelimit(d_, r_, ...) \
781 	evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, __VA_ARGS__)
782 #define evdev_log_bug_kernel_ratelimit(d_, r_, ...) \
783 	evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, "kernel bug: " __VA_ARGS__)
784 #define evdev_log_bug_libinput_ratelimit(d_, r_, ...) \
785 	evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, "libinput bug: " __VA_ARGS__)
786 #define evdev_log_bug_client_ratelimit(d_, r_, ...) \
787 	evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, "client bug: " __VA_ARGS__)
788 
789 /**
790  * Convert the pair of delta coordinates in device space to mm.
791  */
792 static inline struct phys_coords
evdev_device_unit_delta_to_mm(const struct evdev_device * device,const struct device_coords * units)793 evdev_device_unit_delta_to_mm(const struct evdev_device* device,
794 			      const struct device_coords *units)
795 {
796 	struct phys_coords mm = { 0,  0 };
797 	const struct input_absinfo *absx, *absy;
798 
799 	if (device->abs.absinfo_x == NULL ||
800 	    device->abs.absinfo_y == NULL) {
801 		log_bug_libinput(evdev_libinput_context(device),
802 				 "%s: is not an abs device\n",
803 				 device->devname);
804 		return mm;
805 	}
806 
807 	absx = device->abs.absinfo_x;
808 	absy = device->abs.absinfo_y;
809 
810 	mm.x = 1.0 * units->x/absx->resolution;
811 	mm.y = 1.0 * units->y/absy->resolution;
812 
813 	return mm;
814 }
815 
816 /**
817  * Convert the pair of coordinates in device space to mm. This takes the
818  * axis min into account, i.e. a unit of min is equivalent to 0 mm.
819  */
820 static inline struct phys_coords
evdev_device_units_to_mm(const struct evdev_device * device,const struct device_coords * units)821 evdev_device_units_to_mm(const struct evdev_device* device,
822 			 const struct device_coords *units)
823 {
824 	struct phys_coords mm = { 0,  0 };
825 	const struct input_absinfo *absx, *absy;
826 
827 	if (device->abs.absinfo_x == NULL ||
828 	    device->abs.absinfo_y == NULL) {
829 		log_bug_libinput(evdev_libinput_context(device),
830 				 "%s: is not an abs device\n",
831 				 device->devname);
832 		return mm;
833 	}
834 
835 	absx = device->abs.absinfo_x;
836 	absy = device->abs.absinfo_y;
837 
838 	mm.x = (units->x - absx->minimum)/absx->resolution;
839 	mm.y = (units->y - absy->minimum)/absy->resolution;
840 
841 	return mm;
842 }
843 
844 /**
845  * Convert the pair of coordinates in mm to device units. This takes the
846  * axis min into account, i.e. 0 mm  is equivalent to the min.
847  */
848 static inline struct device_coords
evdev_device_mm_to_units(const struct evdev_device * device,const struct phys_coords * mm)849 evdev_device_mm_to_units(const struct evdev_device *device,
850 			 const struct phys_coords *mm)
851 {
852 	struct device_coords units = { 0,  0 };
853 	const struct input_absinfo *absx, *absy;
854 
855 	if (device->abs.absinfo_x == NULL ||
856 	    device->abs.absinfo_y == NULL) {
857 		log_bug_libinput(evdev_libinput_context(device),
858 				 "%s: is not an abs device\n",
859 				 device->devname);
860 		return units;
861 	}
862 
863 	absx = device->abs.absinfo_x;
864 	absy = device->abs.absinfo_y;
865 
866 	units.x = mm->x * absx->resolution + absx->minimum;
867 	units.y = mm->y * absy->resolution + absy->minimum;
868 
869 	return units;
870 }
871 
872 static inline void
evdev_device_init_abs_range_warnings(struct evdev_device * device)873 evdev_device_init_abs_range_warnings(struct evdev_device *device)
874 {
875 	const struct input_absinfo *x, *y;
876 	int width, height;
877 
878 	x = device->abs.absinfo_x;
879 	y = device->abs.absinfo_y;
880 	width = device->abs.dimensions.x;
881 	height = device->abs.dimensions.y;
882 
883 	device->abs.warning_range.min.x = x->minimum - 0.05 * width;
884 	device->abs.warning_range.min.y = y->minimum - 0.05 * height;
885 	device->abs.warning_range.max.x = x->maximum + 0.05 * width;
886 	device->abs.warning_range.max.y = y->maximum + 0.05 * height;
887 
888 	/* One warning every 5 min is enough */
889 	ratelimit_init(&device->abs.warning_range.range_warn_limit,
890 		       s2us(3000),
891 		       1);
892 }
893 
894 static inline void
evdev_device_check_abs_axis_range(struct evdev_device * device,unsigned int code,int value)895 evdev_device_check_abs_axis_range(struct evdev_device *device,
896 				  unsigned int code,
897 				  int value)
898 {
899 	int min, max;
900 
901 	switch(code) {
902 	case ABS_X:
903 	case ABS_MT_POSITION_X:
904 		min = device->abs.warning_range.min.x;
905 		max = device->abs.warning_range.max.x;
906 		break;
907 	case ABS_Y:
908 	case ABS_MT_POSITION_Y:
909 		min = device->abs.warning_range.min.y;
910 		max = device->abs.warning_range.max.y;
911 		break;
912 	default:
913 		return;
914 	}
915 
916 	if (value < min || value > max) {
917 		log_info_ratelimit(evdev_libinput_context(device),
918 				   &device->abs.warning_range.range_warn_limit,
919 				   "Axis %#x value %d is outside expected range [%d, %d]\n"
920 				   "See %sabsolute_coordinate_ranges.html for details\n",
921 				   code, value, min, max,
922 				   HTTP_DOC_LINK);
923 	}
924 }
925 
926 struct evdev_paired_keyboard {
927 	struct list link;
928 	struct evdev_device *device;
929 	struct libinput_event_listener listener;
930 };
931 
932 static inline void
evdev_paired_keyboard_destroy(struct evdev_paired_keyboard * kbd)933 evdev_paired_keyboard_destroy(struct evdev_paired_keyboard *kbd)
934 {
935 	kbd->device = NULL;
936 	libinput_device_remove_event_listener(&kbd->listener);
937 	list_remove(&kbd->link);
938 	free(kbd);
939 }
940 
941 #endif /* EVDEV_H */
942