1 /*
2  * Copyright © 2010 Intel Corporation
3  * Copyright © 2013 Jonas Ådahl
4  * Copyright © 2013-2017 Red Hat, Inc.
5  * Copyright © 2017 James Ye <jye836@gmail.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the next
15  * paragraph) shall be included in all copies or substantial portions of the
16  * Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */
26 
27 #include "config.h"
28 
29 #include <errno.h>
30 #include <stdbool.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/stat.h>
34 #include "linux/input.h"
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <mtdev-plumbing.h>
38 #include <assert.h>
39 #include <math.h>
40 
41 #include "libinput.h"
42 #include "evdev.h"
43 #include "filter.h"
44 #include "libinput-private.h"
45 #include "quirks.h"
46 #include "util-input-event.h"
47 
48 #if HAVE_LIBWACOM
49 #include <libwacom/libwacom.h>
50 #endif
51 
52 #define DEFAULT_WHEEL_CLICK_ANGLE 15
53 #define DEFAULT_BUTTON_SCROLL_TIMEOUT ms2us(200)
54 
55 enum evdev_device_udev_tags {
56 	EVDEV_UDEV_TAG_INPUT		= bit(0),
57 	EVDEV_UDEV_TAG_KEYBOARD		= bit(1),
58 	EVDEV_UDEV_TAG_MOUSE		= bit(2),
59 	EVDEV_UDEV_TAG_TOUCHPAD		= bit(3),
60 	EVDEV_UDEV_TAG_TOUCHSCREEN	= bit(4),
61 	EVDEV_UDEV_TAG_TABLET		= bit(5),
62 	EVDEV_UDEV_TAG_JOYSTICK		= bit(6),
63 	EVDEV_UDEV_TAG_ACCELEROMETER	= bit(7),
64 	EVDEV_UDEV_TAG_TABLET_PAD	= bit(8),
65 	EVDEV_UDEV_TAG_POINTINGSTICK	= bit(9),
66 	EVDEV_UDEV_TAG_TRACKBALL	= bit(10),
67 	EVDEV_UDEV_TAG_SWITCH		= bit(11),
68 };
69 
70 struct evdev_udev_tag_match {
71 	const char *name;
72 	enum evdev_device_udev_tags tag;
73 };
74 
75 static const struct evdev_udev_tag_match evdev_udev_tag_matches[] = {
76 	{"ID_INPUT",			EVDEV_UDEV_TAG_INPUT},
77 	{"ID_INPUT_KEYBOARD",		EVDEV_UDEV_TAG_KEYBOARD},
78 	{"ID_INPUT_KEY",		EVDEV_UDEV_TAG_KEYBOARD},
79 	{"ID_INPUT_MOUSE",		EVDEV_UDEV_TAG_MOUSE},
80 	{"ID_INPUT_TOUCHPAD",		EVDEV_UDEV_TAG_TOUCHPAD},
81 	{"ID_INPUT_TOUCHSCREEN",	EVDEV_UDEV_TAG_TOUCHSCREEN},
82 	{"ID_INPUT_TABLET",		EVDEV_UDEV_TAG_TABLET},
83 	{"ID_INPUT_TABLET_PAD",		EVDEV_UDEV_TAG_TABLET_PAD},
84 	{"ID_INPUT_JOYSTICK",		EVDEV_UDEV_TAG_JOYSTICK},
85 	{"ID_INPUT_ACCELEROMETER",	EVDEV_UDEV_TAG_ACCELEROMETER},
86 	{"ID_INPUT_POINTINGSTICK",	EVDEV_UDEV_TAG_POINTINGSTICK},
87 	{"ID_INPUT_TRACKBALL",		EVDEV_UDEV_TAG_TRACKBALL},
88 	{"ID_INPUT_SWITCH",		EVDEV_UDEV_TAG_SWITCH},
89 };
90 
91 static const unsigned int well_known_keyboard_keys[] = {
92 	KEY_LEFTCTRL,
93 	KEY_CAPSLOCK,
94 	KEY_NUMLOCK,
95 	KEY_INSERT,
96 	KEY_MUTE,
97 	KEY_CALC,
98 	KEY_FILE,
99 	KEY_MAIL,
100 	KEY_PLAYPAUSE,
101 	KEY_BRIGHTNESSDOWN,
102 };
103 
104 static inline bool
parse_udev_flag(struct evdev_device *device, struct udev_device *udev_device, const char *property)105 parse_udev_flag(struct evdev_device *device,
106 		struct udev_device *udev_device,
107 		const char *property)
108 {
109 	const char *val;
110 	bool b;
111 
112 	val = udev_device_get_property_value(udev_device, property);
113 	if (!val)
114 		return false;
115 
116 	if (!parse_boolean_property(val, &b)) {
117 		evdev_log_error(device,
118 				"property %s has invalid value '%s'\n",
119 				property,
120 				val);
121 		return false;
122 	}
123 
124 	return b;
125 }
126 
127 int
evdev_update_key_down_count(struct evdev_device *device, int code, int pressed)128 evdev_update_key_down_count(struct evdev_device *device,
129 			    int code,
130 			    int pressed)
131 {
132 	int key_count = 0;
133 	assert(code >= 0 && code < KEY_CNT);
134 
135 	if (pressed) {
136 		key_count = ++device->key_count[code];
137 	} else {
138 		if (device->key_count[code] > 0) {
139 			key_count = --device->key_count[code];
140 		} else {
141 			evdev_log_bug_libinput(device,
142 					       "releasing key %s with count %d\n",
143 					       libevdev_event_code_get_name(EV_KEY, code),
144 					       device->key_count[code]);
145 		}
146 	}
147 
148 	if (key_count > 32) {
149 		evdev_log_bug_libinput(device,
150 				       "key count for %s reached abnormal values\n",
151 				       libevdev_event_code_get_name(EV_KEY, code));
152 	}
153 
154 	return key_count;
155 }
156 
157 enum libinput_switch_state
evdev_device_switch_get_state(struct evdev_device *device, enum libinput_switch sw)158 evdev_device_switch_get_state(struct evdev_device *device,
159 			      enum libinput_switch sw)
160 {
161 	struct evdev_dispatch *dispatch = device->dispatch;
162 
163 	assert(dispatch->interface->get_switch_state);
164 
165 	return dispatch->interface->get_switch_state(dispatch, sw);
166 }
167 
168 void
evdev_pointer_notify_physical_button(struct evdev_device *device, uint64_t time, int button, enum libinput_button_state state)169 evdev_pointer_notify_physical_button(struct evdev_device *device,
170 				     uint64_t time,
171 				     int button,
172 				     enum libinput_button_state state)
173 {
174 	if (evdev_middlebutton_filter_button(device,
175 					     time,
176 					     button,
177 					     state))
178 			return;
179 
180 	evdev_pointer_notify_button(device,
181 				    time,
182 				    (unsigned int)button,
183 				    state);
184 }
185 
186 static void
evdev_pointer_post_button(struct evdev_device *device, uint64_t time, unsigned int button, enum libinput_button_state state)187 evdev_pointer_post_button(struct evdev_device *device,
188 			  uint64_t time,
189 			  unsigned int button,
190 			  enum libinput_button_state state)
191 {
192 	int down_count;
193 
194 	down_count = evdev_update_key_down_count(device, button, state);
195 
196 	if ((state == LIBINPUT_BUTTON_STATE_PRESSED && down_count == 1) ||
197 	    (state == LIBINPUT_BUTTON_STATE_RELEASED && down_count == 0)) {
198 		pointer_notify_button(&device->base, time, button, state);
199 
200 		if (state == LIBINPUT_BUTTON_STATE_RELEASED) {
201 			if (device->left_handed.change_to_enabled)
202 				device->left_handed.change_to_enabled(device);
203 
204 			if (device->scroll.change_scroll_method)
205 				device->scroll.change_scroll_method(device);
206 		}
207 	}
208 
209 }
210 
211 static void
evdev_button_scroll_timeout(uint64_t time, void *data)212 evdev_button_scroll_timeout(uint64_t time, void *data)
213 {
214 	struct evdev_device *device = data;
215 
216 	device->scroll.button_scroll_state = BUTTONSCROLL_READY;
217 }
218 
219 static void
evdev_button_scroll_button(struct evdev_device *device, uint64_t time, int is_press)220 evdev_button_scroll_button(struct evdev_device *device,
221 			   uint64_t time, int is_press)
222 {
223 	/* Where the button lock is enabled, we wrap the buttons into
224 	   their own little state machine and filter out the events.
225 	 */
226 	switch (device->scroll.lock_state) {
227 	case BUTTONSCROLL_LOCK_DISABLED:
228 		break;
229 	case BUTTONSCROLL_LOCK_IDLE:
230 		assert(is_press);
231 		device->scroll.lock_state = BUTTONSCROLL_LOCK_FIRSTDOWN;
232 		evdev_log_debug(device, "scroll lock: first down\n");
233 		break; /* handle event */
234 	case BUTTONSCROLL_LOCK_FIRSTDOWN:
235 		assert(!is_press);
236 		device->scroll.lock_state = BUTTONSCROLL_LOCK_FIRSTUP;
237 		evdev_log_debug(device, "scroll lock: first up\n");
238 		return; /* filter release event */
239 	case BUTTONSCROLL_LOCK_FIRSTUP:
240 		assert(is_press);
241 		device->scroll.lock_state = BUTTONSCROLL_LOCK_SECONDDOWN;
242 		evdev_log_debug(device, "scroll lock: second down\n");
243 		return; /* filter press event */
244 	case BUTTONSCROLL_LOCK_SECONDDOWN:
245 		assert(!is_press);
246 		device->scroll.lock_state = BUTTONSCROLL_LOCK_IDLE;
247 		evdev_log_debug(device, "scroll lock: idle\n");
248 		break; /* handle event */
249 	}
250 
251 	if (is_press) {
252 		if (device->scroll.button < BTN_MOUSE + 5) {
253 			/* For mouse buttons 1-5 (0x110 to 0x114) we apply a timeout before scrolling
254 			 * since the button could also be used for regular clicking. */
255 			enum timer_flags flags = TIMER_FLAG_NONE;
256 
257 			device->scroll.button_scroll_state = BUTTONSCROLL_BUTTON_DOWN;
258 
259 			/* Special case: if middle button emulation is enabled and
260 			 * our scroll button is the left or right button, we only
261 			 * get here *after* the middle button timeout has expired
262 			 * for that button press. The time passed is the button-down
263 			 * time though (which is in the past), so we have to allow
264 			 * for a negative timer to be set.
265 			 */
266 			if (device->middlebutton.enabled &&
267 				(device->scroll.button == BTN_LEFT ||
268 				device->scroll.button == BTN_RIGHT)) {
269 				flags = TIMER_FLAG_ALLOW_NEGATIVE;
270 			}
271 
272 			libinput_timer_set_flags(&device->scroll.timer,
273 						time + DEFAULT_BUTTON_SCROLL_TIMEOUT,
274 						flags);
275 		} else {
276 			/* For extra mouse buttons numbered 6 or more (0x115+) we assume it is
277 			 * dedicated exclusively to scrolling, so we don't apply the timeout
278 			 * in order to provide immediate scrolling responsiveness. */
279 			device->scroll.button_scroll_state = BUTTONSCROLL_READY;
280 		}
281 		device->scroll.button_down_time = time;
282 		evdev_log_debug(device, "btnscroll: down\n");
283 	} else {
284 		libinput_timer_cancel(&device->scroll.timer);
285 		switch(device->scroll.button_scroll_state) {
286 		case BUTTONSCROLL_IDLE:
287 			evdev_log_bug_libinput(device,
288 				       "invalid state IDLE for button up\n");
289 			break;
290 		case BUTTONSCROLL_BUTTON_DOWN:
291 		case BUTTONSCROLL_READY:
292 			evdev_log_debug(device, "btnscroll: cancel\n");
293 
294 			/* If the button is released quickly enough or
295 			 * without scroll events, emit the
296 			 * button press/release events. */
297 			evdev_pointer_post_button(device,
298 					device->scroll.button_down_time,
299 					device->scroll.button,
300 					LIBINPUT_BUTTON_STATE_PRESSED);
301 			evdev_pointer_post_button(device, time,
302 					device->scroll.button,
303 					LIBINPUT_BUTTON_STATE_RELEASED);
304 			break;
305 		case BUTTONSCROLL_SCROLLING:
306 			evdev_log_debug(device, "btnscroll: up\n");
307 			evdev_stop_scroll(device, time,
308 					  LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS);
309 			break;
310 		}
311 
312 		device->scroll.button_scroll_state = BUTTONSCROLL_IDLE;
313 	}
314 }
315 
316 void
evdev_pointer_notify_button(struct evdev_device *device, uint64_t time, unsigned int button, enum libinput_button_state state)317 evdev_pointer_notify_button(struct evdev_device *device,
318 			    uint64_t time,
319 			    unsigned int button,
320 			    enum libinput_button_state state)
321 {
322 	if (device->scroll.method == LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN &&
323 	    button == device->scroll.button) {
324 		evdev_button_scroll_button(device, time, state);
325 		return;
326 	}
327 
328 	evdev_pointer_post_button(device, time, button, state);
329 }
330 
331 void
evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)332 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)
333 {
334 	static const struct {
335 		enum libinput_led libinput;
336 		int evdev;
337 	} map[] = {
338 		{ LIBINPUT_LED_NUM_LOCK, LED_NUML },
339 		{ LIBINPUT_LED_CAPS_LOCK, LED_CAPSL },
340 		{ LIBINPUT_LED_SCROLL_LOCK, LED_SCROLLL },
341 	};
342 	struct input_event ev[ARRAY_LENGTH(map) + 1];
343 	unsigned int i;
344 
345 	if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
346 		return;
347 
348 	memset(ev, 0, sizeof(ev));
349 	for (i = 0; i < ARRAY_LENGTH(map); i++) {
350 		ev[i].type = EV_LED;
351 		ev[i].code = map[i].evdev;
352 		ev[i].value = !!(leds & map[i].libinput);
353 	}
354 	ev[i].type = EV_SYN;
355 	ev[i].code = SYN_REPORT;
356 
357 	i = write(device->fd, ev, sizeof ev);
358 	(void)i; /* no, we really don't care about the return value */
359 }
360 
361 void
evdev_transform_absolute(struct evdev_device *device, struct device_coords *point)362 evdev_transform_absolute(struct evdev_device *device,
363 			 struct device_coords *point)
364 {
365 	if (!device->abs.apply_calibration)
366 		return;
367 
368 	matrix_mult_vec(&device->abs.calibration, &point->x, &point->y);
369 }
370 
371 void
evdev_transform_relative(struct evdev_device *device, struct device_coords *point)372 evdev_transform_relative(struct evdev_device *device,
373 			 struct device_coords *point)
374 {
375 	struct matrix rel_matrix;
376 
377 	if (!device->abs.apply_calibration)
378 		return;
379 
380 	matrix_to_relative(&rel_matrix, &device->abs.calibration);
381 	matrix_mult_vec(&rel_matrix, &point->x, &point->y);
382 }
383 
384 static inline double
scale_axis(const struct input_absinfo *absinfo, double val, double to_range)385 scale_axis(const struct input_absinfo *absinfo, double val, double to_range)
386 {
387 	return (val - absinfo->minimum) * to_range / absinfo_range(absinfo);
388 }
389 
390 double
evdev_device_transform_x(struct evdev_device *device, double x, uint32_t width)391 evdev_device_transform_x(struct evdev_device *device,
392 			 double x,
393 			 uint32_t width)
394 {
395 	return scale_axis(device->abs.absinfo_x, x, width);
396 }
397 
398 double
evdev_device_transform_y(struct evdev_device *device, double y, uint32_t height)399 evdev_device_transform_y(struct evdev_device *device,
400 			 double y,
401 			 uint32_t height)
402 {
403 	return scale_axis(device->abs.absinfo_y, y, height);
404 }
405 
406 void
evdev_notify_axis_legacy_wheel(struct evdev_device *device, uint64_t time, uint32_t axes, const struct normalized_coords *delta_in, const struct discrete_coords *discrete_in)407 evdev_notify_axis_legacy_wheel(struct evdev_device *device,
408 			       uint64_t time,
409 			       uint32_t axes,
410 			       const struct normalized_coords *delta_in,
411 			       const struct discrete_coords *discrete_in)
412 {
413 	struct normalized_coords delta = *delta_in;
414 	struct discrete_coords discrete = *discrete_in;
415 
416 	if (device->scroll.invert_horizontal_scrolling) {
417 		delta.x *= -1;
418 		discrete.x *= -1;
419 	}
420 
421 	if (device->scroll.natural_scrolling_enabled) {
422 		delta.x *= -1;
423 		delta.y *= -1;
424 		discrete.x *= -1;
425 		discrete.y *= -1;
426 	}
427 
428 	pointer_notify_axis_legacy_wheel(&device->base,
429 					 time,
430 					 axes,
431 					 &delta,
432 					 &discrete);
433 }
434 
435 void
evdev_notify_axis_wheel(struct evdev_device *device, uint64_t time, uint32_t axes, const struct normalized_coords *delta_in, const struct wheel_v120 *v120_in)436 evdev_notify_axis_wheel(struct evdev_device *device,
437 			uint64_t time,
438 			uint32_t axes,
439 			const struct normalized_coords *delta_in,
440 			const struct wheel_v120 *v120_in)
441 {
442 	struct normalized_coords delta = *delta_in;
443 	struct wheel_v120 v120 = *v120_in;
444 
445 	if (device->scroll.invert_horizontal_scrolling) {
446 		delta.x *= -1;
447 		v120.x *= -1;
448 	}
449 
450 	if (device->scroll.natural_scrolling_enabled) {
451 		delta.x *= -1;
452 		delta.y *= -1;
453 		v120.x *= -1;
454 		v120.y *= -1;
455 	}
456 
457 	pointer_notify_axis_wheel(&device->base,
458 				  time,
459 				  axes,
460 				  &delta,
461 				  &v120);
462 }
463 
464 void
evdev_notify_axis_finger(struct evdev_device *device, uint64_t time, uint32_t axes, const struct normalized_coords *delta_in)465 evdev_notify_axis_finger(struct evdev_device *device,
466 			uint64_t time,
467 			uint32_t axes,
468 			const struct normalized_coords *delta_in)
469 {
470 	struct normalized_coords delta = *delta_in;
471 
472 	if (device->scroll.natural_scrolling_enabled) {
473 		delta.x *= -1;
474 		delta.y *= -1;
475 	}
476 
477 	pointer_notify_axis_finger(&device->base,
478 				  time,
479 				  axes,
480 				  &delta);
481 }
482 
483 void
evdev_notify_axis_continous(struct evdev_device *device, uint64_t time, uint32_t axes, const struct normalized_coords *delta_in)484 evdev_notify_axis_continous(struct evdev_device *device,
485 			    uint64_t time,
486 			    uint32_t axes,
487 			    const struct normalized_coords *delta_in)
488 {
489 	struct normalized_coords delta = *delta_in;
490 
491 	if (device->scroll.natural_scrolling_enabled) {
492 		delta.x *= -1;
493 		delta.y *= -1;
494 	}
495 
496 	pointer_notify_axis_continuous(&device->base,
497 				       time,
498 				       axes,
499 				       &delta);
500 }
501 
502 static void
evdev_tag_external_mouse(struct evdev_device *device, struct udev_device *udev_device)503 evdev_tag_external_mouse(struct evdev_device *device,
504 			 struct udev_device *udev_device)
505 {
506 	int bustype;
507 
508 	bustype = libevdev_get_id_bustype(device->evdev);
509 	if (bustype == BUS_USB || bustype == BUS_BLUETOOTH)
510 		device->tags |= EVDEV_TAG_EXTERNAL_MOUSE;
511 }
512 
513 static void
evdev_tag_trackpoint(struct evdev_device *device, struct udev_device *udev_device)514 evdev_tag_trackpoint(struct evdev_device *device,
515 		     struct udev_device *udev_device)
516 {
517 	struct quirks_context *quirks;
518 	struct quirks *q;
519 	char *prop;
520 
521 	if (!libevdev_has_property(device->evdev,
522 				  INPUT_PROP_POINTING_STICK) &&
523 	    !parse_udev_flag(device, udev_device, "ID_INPUT_POINTINGSTICK"))
524 		return;
525 
526 	device->tags |= EVDEV_TAG_TRACKPOINT;
527 
528 	quirks = evdev_libinput_context(device)->quirks;
529 	q = quirks_fetch_for_device(quirks, device->udev_device);
530 	if (q && quirks_get_string(q, QUIRK_ATTR_TRACKPOINT_INTEGRATION, &prop)) {
531 		if (streq(prop, "internal")) {
532 			/* noop, this is the default anyway */
533 		} else if (streq(prop, "external")) {
534 			device->tags |= EVDEV_TAG_EXTERNAL_MOUSE;
535 			evdev_log_info(device,
536 				       "is an external pointing stick\n");
537 		} else {
538 			evdev_log_info(device,
539 				       "tagged with unknown value %s\n",
540 				       prop);
541 		}
542 	}
543 
544 	quirks_unref(q);
545 }
546 
547 static inline void
evdev_tag_keyboard_internal(struct evdev_device *device)548 evdev_tag_keyboard_internal(struct evdev_device *device)
549 {
550 	device->tags |= EVDEV_TAG_INTERNAL_KEYBOARD;
551 	device->tags &= ~EVDEV_TAG_EXTERNAL_KEYBOARD;
552 }
553 
554 static inline void
evdev_tag_keyboard_external(struct evdev_device *device)555 evdev_tag_keyboard_external(struct evdev_device *device)
556 {
557 	device->tags |= EVDEV_TAG_EXTERNAL_KEYBOARD;
558 	device->tags &= ~EVDEV_TAG_INTERNAL_KEYBOARD;
559 }
560 
561 static void
evdev_tag_keyboard(struct evdev_device *device, struct udev_device *udev_device)562 evdev_tag_keyboard(struct evdev_device *device,
563 		   struct udev_device *udev_device)
564 {
565 	struct quirks_context *quirks;
566 	struct quirks *q;
567 	char *prop;
568 	int code;
569 
570 	if (!libevdev_has_event_type(device->evdev, EV_KEY))
571 		return;
572 
573 	for (code = KEY_Q; code <= KEY_P; code++) {
574 		if (!libevdev_has_event_code(device->evdev,
575 					     EV_KEY,
576 					     code))
577 			return;
578 	}
579 
580 	quirks = evdev_libinput_context(device)->quirks;
581 	q = quirks_fetch_for_device(quirks, device->udev_device);
582 	if (q && quirks_get_string(q, QUIRK_ATTR_KEYBOARD_INTEGRATION, &prop)) {
583 		if (streq(prop, "internal")) {
584 			evdev_tag_keyboard_internal(device);
585 		} else if (streq(prop, "external")) {
586 			evdev_tag_keyboard_external(device);
587 		} else {
588 			evdev_log_info(device,
589 				       "tagged with unknown value %s\n",
590 				       prop);
591 		}
592 	}
593 
594 	quirks_unref(q);
595 
596 	device->tags |= EVDEV_TAG_KEYBOARD;
597 }
598 
599 static void
evdev_tag_tablet_touchpad(struct evdev_device *device)600 evdev_tag_tablet_touchpad(struct evdev_device *device)
601 {
602 	device->tags |= EVDEV_TAG_TABLET_TOUCHPAD;
603 }
604 
605 static int
evdev_calibration_has_matrix(struct libinput_device *libinput_device)606 evdev_calibration_has_matrix(struct libinput_device *libinput_device)
607 {
608 	struct evdev_device *device = evdev_device(libinput_device);
609 
610 	return device->abs.absinfo_x && device->abs.absinfo_y;
611 }
612 
613 static enum libinput_config_status
evdev_calibration_set_matrix(struct libinput_device *libinput_device, const float matrix[6])614 evdev_calibration_set_matrix(struct libinput_device *libinput_device,
615 			     const float matrix[6])
616 {
617 	struct evdev_device *device = evdev_device(libinput_device);
618 
619 	evdev_device_calibrate(device, matrix);
620 
621 	return LIBINPUT_CONFIG_STATUS_SUCCESS;
622 }
623 
624 static int
evdev_calibration_get_matrix(struct libinput_device *libinput_device, float matrix[6])625 evdev_calibration_get_matrix(struct libinput_device *libinput_device,
626 			     float matrix[6])
627 {
628 	struct evdev_device *device = evdev_device(libinput_device);
629 
630 	matrix_to_farray6(&device->abs.usermatrix, matrix);
631 
632 	return !matrix_is_identity(&device->abs.usermatrix);
633 }
634 
635 static int
evdev_calibration_get_default_matrix(struct libinput_device *libinput_device, float matrix[6])636 evdev_calibration_get_default_matrix(struct libinput_device *libinput_device,
637 				     float matrix[6])
638 {
639 	struct evdev_device *device = evdev_device(libinput_device);
640 
641 	matrix_to_farray6(&device->abs.default_calibration, matrix);
642 
643 	return !matrix_is_identity(&device->abs.default_calibration);
644 }
645 
646 static uint32_t
evdev_sendevents_get_modes(struct libinput_device *device)647 evdev_sendevents_get_modes(struct libinput_device *device)
648 {
649 	return LIBINPUT_CONFIG_SEND_EVENTS_DISABLED;
650 }
651 
652 static enum libinput_config_status
evdev_sendevents_set_mode(struct libinput_device *device, enum libinput_config_send_events_mode mode)653 evdev_sendevents_set_mode(struct libinput_device *device,
654 			  enum libinput_config_send_events_mode mode)
655 {
656 	struct evdev_device *evdev = evdev_device(device);
657 	struct evdev_dispatch *dispatch = evdev->dispatch;
658 
659 	if (mode == dispatch->sendevents.current_mode)
660 		return LIBINPUT_CONFIG_STATUS_SUCCESS;
661 
662 	switch(mode) {
663 	case LIBINPUT_CONFIG_SEND_EVENTS_ENABLED:
664 		evdev_device_resume(evdev);
665 		break;
666 	case LIBINPUT_CONFIG_SEND_EVENTS_DISABLED:
667 		evdev_device_suspend(evdev);
668 		break;
669 	default: /* no support for combined modes yet */
670 		return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
671 	}
672 
673 	dispatch->sendevents.current_mode = mode;
674 
675 	return LIBINPUT_CONFIG_STATUS_SUCCESS;
676 }
677 
678 static enum libinput_config_send_events_mode
evdev_sendevents_get_mode(struct libinput_device *device)679 evdev_sendevents_get_mode(struct libinput_device *device)
680 {
681 	struct evdev_device *evdev = evdev_device(device);
682 	struct evdev_dispatch *dispatch = evdev->dispatch;
683 
684 	return dispatch->sendevents.current_mode;
685 }
686 
687 static enum libinput_config_send_events_mode
evdev_sendevents_get_default_mode(struct libinput_device *device)688 evdev_sendevents_get_default_mode(struct libinput_device *device)
689 {
690 	return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
691 }
692 
693 static int
evdev_left_handed_has(struct libinput_device *device)694 evdev_left_handed_has(struct libinput_device *device)
695 {
696 	/* This is only hooked up when we have left-handed configuration, so we
697 	 * can hardcode 1 here */
698 	return 1;
699 }
700 
701 static enum libinput_config_status
evdev_left_handed_set(struct libinput_device *device, int left_handed)702 evdev_left_handed_set(struct libinput_device *device, int left_handed)
703 {
704 	struct evdev_device *evdev = evdev_device(device);
705 
706 	evdev->left_handed.want_enabled = left_handed ? true : false;
707 
708 	evdev->left_handed.change_to_enabled(evdev);
709 
710 	return LIBINPUT_CONFIG_STATUS_SUCCESS;
711 }
712 
713 static int
evdev_left_handed_get(struct libinput_device *device)714 evdev_left_handed_get(struct libinput_device *device)
715 {
716 	struct evdev_device *evdev = evdev_device(device);
717 
718 	/* return the wanted configuration, even if it hasn't taken
719 	 * effect yet! */
720 	return evdev->left_handed.want_enabled;
721 }
722 
723 static int
evdev_left_handed_get_default(struct libinput_device *device)724 evdev_left_handed_get_default(struct libinput_device *device)
725 {
726 	return 0;
727 }
728 
729 void
evdev_init_left_handed(struct evdev_device *device, void (*change_to_left_handed)(struct evdev_device *))730 evdev_init_left_handed(struct evdev_device *device,
731 		       void (*change_to_left_handed)(struct evdev_device *))
732 {
733 	device->left_handed.config.has = evdev_left_handed_has;
734 	device->left_handed.config.set = evdev_left_handed_set;
735 	device->left_handed.config.get = evdev_left_handed_get;
736 	device->left_handed.config.get_default = evdev_left_handed_get_default;
737 	device->base.config.left_handed = &device->left_handed.config;
738 	device->left_handed.enabled = false;
739 	device->left_handed.want_enabled = false;
740 	device->left_handed.change_to_enabled = change_to_left_handed;
741 }
742 
743 static uint32_t
evdev_scroll_get_methods(struct libinput_device *device)744 evdev_scroll_get_methods(struct libinput_device *device)
745 {
746 	return LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
747 }
748 
749 static enum libinput_config_status
evdev_scroll_set_method(struct libinput_device *device, enum libinput_config_scroll_method method)750 evdev_scroll_set_method(struct libinput_device *device,
751 			enum libinput_config_scroll_method method)
752 {
753 	struct evdev_device *evdev = evdev_device(device);
754 
755 	evdev->scroll.want_method = method;
756 	evdev->scroll.change_scroll_method(evdev);
757 
758 	return LIBINPUT_CONFIG_STATUS_SUCCESS;
759 }
760 
761 static enum libinput_config_scroll_method
evdev_scroll_get_method(struct libinput_device *device)762 evdev_scroll_get_method(struct libinput_device *device)
763 {
764 	struct evdev_device *evdev = evdev_device(device);
765 
766 	/* return the wanted configuration, even if it hasn't taken
767 	 * effect yet! */
768 	return evdev->scroll.want_method;
769 }
770 
771 static enum libinput_config_scroll_method
evdev_scroll_get_default_method(struct libinput_device *device)772 evdev_scroll_get_default_method(struct libinput_device *device)
773 {
774 	struct evdev_device *evdev = evdev_device(device);
775 
776 	if (evdev->tags & EVDEV_TAG_TRACKPOINT)
777 		return LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
778 
779 	/* Mice without a scroll wheel but with middle button have on-button
780 	 * scrolling by default */
781 	if (!libevdev_has_event_code(evdev->evdev, EV_REL, REL_WHEEL) &&
782 	    !libevdev_has_event_code(evdev->evdev, EV_REL, REL_HWHEEL) &&
783 	    libevdev_has_event_code(evdev->evdev, EV_KEY, BTN_MIDDLE))
784 		return LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
785 
786 	return LIBINPUT_CONFIG_SCROLL_NO_SCROLL;
787 }
788 
789 static enum libinput_config_status
evdev_scroll_set_button(struct libinput_device *device, uint32_t button)790 evdev_scroll_set_button(struct libinput_device *device,
791 			uint32_t button)
792 {
793 	struct evdev_device *evdev = evdev_device(device);
794 
795 	evdev->scroll.want_button = button;
796 	evdev->scroll.change_scroll_method(evdev);
797 
798 	return LIBINPUT_CONFIG_STATUS_SUCCESS;
799 }
800 
801 static uint32_t
evdev_scroll_get_button(struct libinput_device *device)802 evdev_scroll_get_button(struct libinput_device *device)
803 {
804 	struct evdev_device *evdev = evdev_device(device);
805 
806 	/* return the wanted configuration, even if it hasn't taken
807 	 * effect yet! */
808 	return evdev->scroll.want_button;
809 }
810 
811 static uint32_t
evdev_scroll_get_default_button(struct libinput_device *device)812 evdev_scroll_get_default_button(struct libinput_device *device)
813 {
814 	struct evdev_device *evdev = evdev_device(device);
815 	unsigned int code;
816 
817 	if (libevdev_has_event_code(evdev->evdev, EV_KEY, BTN_MIDDLE))
818 		return BTN_MIDDLE;
819 
820 	for (code = BTN_SIDE; code <= BTN_TASK; code++) {
821 		if (libevdev_has_event_code(evdev->evdev, EV_KEY, code))
822 			return code;
823 	}
824 
825 	if (libevdev_has_event_code(evdev->evdev, EV_KEY, BTN_RIGHT))
826 		return BTN_RIGHT;
827 
828 	return 0;
829 }
830 
831 static enum libinput_config_status
evdev_scroll_set_button_lock(struct libinput_device *device, enum libinput_config_scroll_button_lock_state state)832 evdev_scroll_set_button_lock(struct libinput_device *device,
833 			     enum libinput_config_scroll_button_lock_state state)
834 {
835 	struct evdev_device *evdev = evdev_device(device);
836 
837 	switch (state) {
838 	case LIBINPUT_CONFIG_SCROLL_BUTTON_LOCK_DISABLED:
839 		evdev->scroll.want_lock_enabled = false;
840 		break;
841 	case LIBINPUT_CONFIG_SCROLL_BUTTON_LOCK_ENABLED:
842 		evdev->scroll.want_lock_enabled = true;
843 		break;
844 	default:
845 		return LIBINPUT_CONFIG_STATUS_INVALID;
846 	}
847 
848 	evdev->scroll.change_scroll_method(evdev);
849 
850 	return LIBINPUT_CONFIG_STATUS_SUCCESS;
851 }
852 
853 static enum libinput_config_scroll_button_lock_state
evdev_scroll_get_button_lock(struct libinput_device *device)854 evdev_scroll_get_button_lock(struct libinput_device *device)
855 {
856 	struct evdev_device *evdev = evdev_device(device);
857 
858 	if (evdev->scroll.lock_state == BUTTONSCROLL_LOCK_DISABLED)
859 		return LIBINPUT_CONFIG_SCROLL_BUTTON_LOCK_DISABLED;
860 
861 	return LIBINPUT_CONFIG_SCROLL_BUTTON_LOCK_ENABLED;
862 }
863 
864 static enum libinput_config_scroll_button_lock_state
evdev_scroll_get_default_button_lock(struct libinput_device *device)865 evdev_scroll_get_default_button_lock(struct libinput_device *device)
866 {
867 	return LIBINPUT_CONFIG_SCROLL_BUTTON_LOCK_DISABLED;
868 }
869 
870 void
evdev_set_button_scroll_lock_enabled(struct evdev_device *device, bool enabled)871 evdev_set_button_scroll_lock_enabled(struct evdev_device *device,
872 				     bool enabled)
873 {
874 	if (enabled)
875 		device->scroll.lock_state = BUTTONSCROLL_LOCK_IDLE;
876 	else
877 		device->scroll.lock_state = BUTTONSCROLL_LOCK_DISABLED;
878 }
879 
880 void
evdev_init_button_scroll(struct evdev_device *device, void (*change_scroll_method)(struct evdev_device *))881 evdev_init_button_scroll(struct evdev_device *device,
882 			 void (*change_scroll_method)(struct evdev_device *))
883 {
884 	char timer_name[64];
885 
886 	snprintf(timer_name,
887 		 sizeof(timer_name),
888 		 "%s btnscroll",
889 		 evdev_device_get_sysname(device));
890 	libinput_timer_init(&device->scroll.timer,
891 			    evdev_libinput_context(device),
892 			    timer_name,
893 			    evdev_button_scroll_timeout, device);
894 	device->scroll.config.get_methods = evdev_scroll_get_methods;
895 	device->scroll.config.set_method = evdev_scroll_set_method;
896 	device->scroll.config.get_method = evdev_scroll_get_method;
897 	device->scroll.config.get_default_method = evdev_scroll_get_default_method;
898 	device->scroll.config.set_button = evdev_scroll_set_button;
899 	device->scroll.config.get_button = evdev_scroll_get_button;
900 	device->scroll.config.get_default_button = evdev_scroll_get_default_button;
901 	device->scroll.config.set_button_lock = evdev_scroll_set_button_lock;
902 	device->scroll.config.get_button_lock = evdev_scroll_get_button_lock;
903 	device->scroll.config.get_default_button_lock = evdev_scroll_get_default_button_lock;
904 	device->base.config.scroll_method = &device->scroll.config;
905 	device->scroll.method = evdev_scroll_get_default_method((struct libinput_device *)device);
906 	device->scroll.want_method = device->scroll.method;
907 	device->scroll.button = evdev_scroll_get_default_button((struct libinput_device *)device);
908 	device->scroll.want_button = device->scroll.button;
909 	device->scroll.change_scroll_method = change_scroll_method;
910 }
911 
912 void
evdev_init_calibration(struct evdev_device *device, struct libinput_device_config_calibration *calibration)913 evdev_init_calibration(struct evdev_device *device,
914 		       struct libinput_device_config_calibration *calibration)
915 {
916 	device->base.config.calibration = calibration;
917 
918 	calibration->has_matrix = evdev_calibration_has_matrix;
919 	calibration->set_matrix = evdev_calibration_set_matrix;
920 	calibration->get_matrix = evdev_calibration_get_matrix;
921 	calibration->get_default_matrix = evdev_calibration_get_default_matrix;
922 }
923 
924 void
evdev_init_sendevents(struct evdev_device *device, struct evdev_dispatch *dispatch)925 evdev_init_sendevents(struct evdev_device *device,
926 		      struct evdev_dispatch *dispatch)
927 {
928 	device->base.config.sendevents = &dispatch->sendevents.config;
929 
930 	dispatch->sendevents.current_mode = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
931 	dispatch->sendevents.config.get_modes = evdev_sendevents_get_modes;
932 	dispatch->sendevents.config.set_mode = evdev_sendevents_set_mode;
933 	dispatch->sendevents.config.get_mode = evdev_sendevents_get_mode;
934 	dispatch->sendevents.config.get_default_mode = evdev_sendevents_get_default_mode;
935 }
936 
937 static int
evdev_scroll_config_natural_has(struct libinput_device *device)938 evdev_scroll_config_natural_has(struct libinput_device *device)
939 {
940 	return 1;
941 }
942 
943 static enum libinput_config_status
evdev_scroll_config_natural_set(struct libinput_device *device, int enabled)944 evdev_scroll_config_natural_set(struct libinput_device *device,
945 				int enabled)
946 {
947 	struct evdev_device *dev = evdev_device(device);
948 
949 	dev->scroll.natural_scrolling_enabled = enabled ? true : false;
950 
951 	return LIBINPUT_CONFIG_STATUS_SUCCESS;
952 }
953 
954 static int
evdev_scroll_config_natural_get(struct libinput_device *device)955 evdev_scroll_config_natural_get(struct libinput_device *device)
956 {
957 	struct evdev_device *dev = evdev_device(device);
958 
959 	return dev->scroll.natural_scrolling_enabled ? 1 : 0;
960 }
961 
962 static int
evdev_scroll_config_natural_get_default(struct libinput_device *device)963 evdev_scroll_config_natural_get_default(struct libinput_device *device)
964 {
965 	/* Overridden in evdev-mt-touchpad.c for Apple touchpads. */
966 	return 0;
967 }
968 
969 void
evdev_init_natural_scroll(struct evdev_device *device)970 evdev_init_natural_scroll(struct evdev_device *device)
971 {
972 	device->scroll.config_natural.has = evdev_scroll_config_natural_has;
973 	device->scroll.config_natural.set_enabled = evdev_scroll_config_natural_set;
974 	device->scroll.config_natural.get_enabled = evdev_scroll_config_natural_get;
975 	device->scroll.config_natural.get_default_enabled = evdev_scroll_config_natural_get_default;
976 	device->scroll.natural_scrolling_enabled = false;
977 	device->base.config.natural_scroll = &device->scroll.config_natural;
978 }
979 
980 int
evdev_need_mtdev(struct evdev_device *device)981 evdev_need_mtdev(struct evdev_device *device)
982 {
983 	struct libevdev *evdev = device->evdev;
984 
985 	return (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
986 		libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y) &&
987 		!libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT));
988 }
989 
990 /* Fake MT devices have the ABS_MT_SLOT bit set because of
991    the limited ABS_* range - they aren't MT devices, they
992    just have too many ABS_ axes */
993 bool
evdev_is_fake_mt_device(struct evdev_device *device)994 evdev_is_fake_mt_device(struct evdev_device *device)
995 {
996 	struct libevdev *evdev = device->evdev;
997 
998 	return libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT) &&
999 		libevdev_get_num_slots(evdev) == -1;
1000 }
1001 
1002 enum switch_reliability
evdev_read_switch_reliability_prop(struct evdev_device *device)1003 evdev_read_switch_reliability_prop(struct evdev_device *device)
1004 {
1005 	enum switch_reliability r;
1006 	struct quirks_context *quirks;
1007 	struct quirks *q;
1008 	char *prop;
1009 
1010 	quirks = evdev_libinput_context(device)->quirks;
1011 	q = quirks_fetch_for_device(quirks, device->udev_device);
1012 	if (!q || !quirks_get_string(q, QUIRK_ATTR_LID_SWITCH_RELIABILITY, &prop)) {
1013 		r = RELIABILITY_RELIABLE;
1014 	} else if (!parse_switch_reliability_property(prop, &r)) {
1015 		evdev_log_error(device,
1016 				"%s: switch reliability set to unknown value '%s'\n",
1017 				device->devname,
1018 				prop);
1019 		r = RELIABILITY_RELIABLE;
1020 	} else if (r == RELIABILITY_WRITE_OPEN) {
1021 		evdev_log_info(device, "will write switch open events\n");
1022 	}
1023 
1024 	quirks_unref(q);
1025 
1026 	return r;
1027 }
1028 
1029 LIBINPUT_UNUSED
1030 static inline void
evdev_print_event(struct evdev_device *device, const struct input_event *e)1031 evdev_print_event(struct evdev_device *device,
1032 		  const struct input_event *e)
1033 {
1034 	static uint32_t offset = 0;
1035 	static uint32_t last_time = 0;
1036 	uint32_t time = us2ms(input_event_time(e));
1037 
1038 	if (offset == 0) {
1039 		offset = time;
1040 		last_time = time - offset;
1041 	}
1042 
1043 	time -= offset;
1044 
1045 	if (libevdev_event_is_code(e, EV_SYN, SYN_REPORT)) {
1046 		evdev_log_debug(device,
1047 			  "%u.%03u -------------- EV_SYN ------------ +%ums\n",
1048 			  time / 1000,
1049 			  time % 1000,
1050 			  time - last_time);
1051 
1052 		last_time = time;
1053 	} else {
1054 		evdev_log_debug(device,
1055 			  "%u.%03u %-16s %-20s %4d\n",
1056 			  time / 1000,
1057 			  time % 1000,
1058 			  libevdev_event_type_get_name(e->type),
1059 			  libevdev_event_code_get_name(e->type, e->code),
1060 			  e->value);
1061 	}
1062 }
1063 
1064 static inline void
evdev_process_event(struct evdev_device *device, struct input_event *e)1065 evdev_process_event(struct evdev_device *device, struct input_event *e)
1066 {
1067 	struct evdev_dispatch *dispatch = device->dispatch;
1068 	uint64_t time = input_event_time(e);
1069 
1070 #if 0
1071 	evdev_print_event(device, e);
1072 #endif
1073 
1074 	libinput_timer_flush(evdev_libinput_context(device), time);
1075 
1076 	dispatch->interface->process(dispatch, device, e, time);
1077 }
1078 
1079 static inline void
evdev_device_dispatch_one(struct evdev_device *device, struct input_event *ev)1080 evdev_device_dispatch_one(struct evdev_device *device,
1081 			  struct input_event *ev)
1082 {
1083 	if (!device->mtdev) {
1084 		evdev_process_event(device, ev);
1085 	} else {
1086 		mtdev_put_event(device->mtdev, ev);
1087 		if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
1088 			while (!mtdev_empty(device->mtdev)) {
1089 				struct input_event e;
1090 				mtdev_get_event(device->mtdev, &e);
1091 				evdev_process_event(device, &e);
1092 			}
1093 		}
1094 	}
1095 }
1096 
1097 static int
evdev_sync_device(struct evdev_device *device)1098 evdev_sync_device(struct evdev_device *device)
1099 {
1100 	struct input_event ev;
1101 	int rc;
1102 
1103 	do {
1104 		rc = libevdev_next_event(device->evdev,
1105 					 LIBEVDEV_READ_FLAG_SYNC, &ev);
1106 		if (rc < 0)
1107 			break;
1108 		evdev_device_dispatch_one(device, &ev);
1109 	} while (rc == LIBEVDEV_READ_STATUS_SYNC);
1110 
1111 	return rc == -EAGAIN ? 0 : rc;
1112 }
1113 
1114 static inline void
evdev_note_time_delay(struct evdev_device *device, const struct input_event *ev)1115 evdev_note_time_delay(struct evdev_device *device,
1116 		      const struct input_event *ev)
1117 {
1118 	struct libinput *libinput = evdev_libinput_context(device);
1119 	uint32_t tdelta;
1120 	uint64_t eventtime = input_event_time(ev);
1121 
1122 	/* if we have a current libinput_dispatch() snapshot, compare our
1123 	 * event time with the one from the snapshot. If we have more than
1124 	 * 10ms delay, complain about it. This catches delays in processing
1125 	 * where there is no steady event flow and thus SYN_DROPPED may not
1126 	 * get hit by the kernel despite us being too slow.
1127 	 */
1128 	if (libinput->dispatch_time == 0 ||
1129 	    eventtime > libinput->dispatch_time)
1130 		return;
1131 
1132 	tdelta = us2ms(libinput->dispatch_time - eventtime);
1133 	if (tdelta > 20) {
1134 		evdev_log_bug_client_ratelimit(device,
1135 					       &device->delay_warning_limit,
1136 					       "event processing lagging behind by %dms, your system is too slow\n",
1137 					       tdelta);
1138 	}
1139 }
1140 
1141 static void
evdev_device_dispatch(void *data)1142 evdev_device_dispatch(void *data)
1143 {
1144 	struct evdev_device *device = data;
1145 	struct libinput *libinput = evdev_libinput_context(device);
1146 	struct input_event ev;
1147 	int rc;
1148 	bool once = false;
1149 
1150 	/* If the compositor is repainting, this function is called only once
1151 	 * per frame and we have to process all the events available on the
1152 	 * fd, otherwise there will be input lag. */
1153 	do {
1154 		rc = libevdev_next_event(device->evdev,
1155 					 LIBEVDEV_READ_FLAG_NORMAL, &ev);
1156 		if (rc == LIBEVDEV_READ_STATUS_SYNC) {
1157 			evdev_log_info_ratelimit(device,
1158 						 &device->syn_drop_limit,
1159 						 "SYN_DROPPED event - some input events have been lost.\n");
1160 
1161 			/* send one more sync event so we handle all
1162 			   currently pending events before we sync up
1163 			   to the current state */
1164 			ev.code = SYN_REPORT;
1165 			evdev_device_dispatch_one(device, &ev);
1166 
1167 			rc = evdev_sync_device(device);
1168 			if (rc == 0)
1169 				rc = LIBEVDEV_READ_STATUS_SUCCESS;
1170 		} else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
1171 			if (!once) {
1172 				evdev_note_time_delay(device, &ev);
1173 				once = true;
1174 			}
1175 			evdev_device_dispatch_one(device, &ev);
1176 		} else if (rc == -ENODEV) {
1177 			evdev_device_remove(device);
1178 			return;
1179 		}
1180 	} while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
1181 
1182 	if (rc != -EAGAIN && rc != -EINTR) {
1183 		libinput_remove_source(libinput, device->source);
1184 		device->source = NULL;
1185 	}
1186 }
1187 
1188 static inline bool
evdev_init_accel(struct evdev_device *device, enum libinput_config_accel_profile which)1189 evdev_init_accel(struct evdev_device *device,
1190 		 enum libinput_config_accel_profile which)
1191 {
1192 	struct motion_filter *filter = NULL;
1193 
1194 	if (which == LIBINPUT_CONFIG_ACCEL_PROFILE_CUSTOM)
1195 		filter = create_custom_accelerator_filter();
1196 	else if (device->tags & EVDEV_TAG_TRACKPOINT) {
1197 		if (which == LIBINPUT_CONFIG_ACCEL_PROFILE_FLAT)
1198 			filter = create_pointer_accelerator_filter_trackpoint_flat(device->trackpoint_multiplier);
1199 		else
1200 			filter = create_pointer_accelerator_filter_trackpoint(device->trackpoint_multiplier,
1201 									      device->use_velocity_averaging);
1202 	} else {
1203 		if (which == LIBINPUT_CONFIG_ACCEL_PROFILE_FLAT)
1204 			filter = create_pointer_accelerator_filter_flat(device->dpi);
1205 		else if (device->dpi < DEFAULT_MOUSE_DPI)
1206 			filter = create_pointer_accelerator_filter_linear_low_dpi(device->dpi,
1207 										  device->use_velocity_averaging);
1208 	}
1209 
1210 	if (!filter)
1211 		filter = create_pointer_accelerator_filter_linear(device->dpi,
1212 								  device->use_velocity_averaging);
1213 
1214 	if (!filter)
1215 		return false;
1216 
1217 	evdev_device_init_pointer_acceleration(device, filter);
1218 
1219 	return true;
1220 }
1221 
1222 static int
evdev_accel_config_available(struct libinput_device *device)1223 evdev_accel_config_available(struct libinput_device *device)
1224 {
1225 	/* this function is only called if we set up ptraccel, so we can
1226 	   reply with a resounding "Yes" */
1227 	return 1;
1228 }
1229 
1230 static enum libinput_config_status
evdev_accel_config_set_speed(struct libinput_device *device, double speed)1231 evdev_accel_config_set_speed(struct libinput_device *device, double speed)
1232 {
1233 	struct evdev_device *dev = evdev_device(device);
1234 
1235 	if (!filter_set_speed(dev->pointer.filter, speed))
1236 		return LIBINPUT_CONFIG_STATUS_INVALID;
1237 
1238 	return LIBINPUT_CONFIG_STATUS_SUCCESS;
1239 }
1240 
1241 static double
evdev_accel_config_get_speed(struct libinput_device *device)1242 evdev_accel_config_get_speed(struct libinput_device *device)
1243 {
1244 	struct evdev_device *dev = evdev_device(device);
1245 
1246 	return filter_get_speed(dev->pointer.filter);
1247 }
1248 
1249 static double
evdev_accel_config_get_default_speed(struct libinput_device *device)1250 evdev_accel_config_get_default_speed(struct libinput_device *device)
1251 {
1252 	return 0.0;
1253 }
1254 
1255 static uint32_t
evdev_accel_config_get_profiles(struct libinput_device *libinput_device)1256 evdev_accel_config_get_profiles(struct libinput_device *libinput_device)
1257 {
1258 	struct evdev_device *device = evdev_device(libinput_device);
1259 
1260 	if (!device->pointer.filter)
1261 		return LIBINPUT_CONFIG_ACCEL_PROFILE_NONE;
1262 
1263 	return LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE |
1264 	       LIBINPUT_CONFIG_ACCEL_PROFILE_FLAT |
1265 	       LIBINPUT_CONFIG_ACCEL_PROFILE_CUSTOM;
1266 }
1267 
1268 static enum libinput_config_status
evdev_accel_config_set_profile(struct libinput_device *libinput_device, enum libinput_config_accel_profile profile)1269 evdev_accel_config_set_profile(struct libinput_device *libinput_device,
1270 			       enum libinput_config_accel_profile profile)
1271 {
1272 	struct evdev_device *device = evdev_device(libinput_device);
1273 	struct motion_filter *filter;
1274 	double speed;
1275 
1276 	filter = device->pointer.filter;
1277 	if (filter_get_type(filter) == profile)
1278 		return LIBINPUT_CONFIG_STATUS_SUCCESS;
1279 
1280 	speed = filter_get_speed(filter);
1281 	device->pointer.filter = NULL;
1282 
1283 	if (evdev_init_accel(device, profile)) {
1284 		evdev_accel_config_set_speed(libinput_device, speed);
1285 		filter_destroy(filter);
1286 	} else {
1287 		device->pointer.filter = filter;
1288 		return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
1289 	}
1290 
1291 	return LIBINPUT_CONFIG_STATUS_SUCCESS;
1292 }
1293 
1294 static enum libinput_config_accel_profile
evdev_accel_config_get_profile(struct libinput_device *libinput_device)1295 evdev_accel_config_get_profile(struct libinput_device *libinput_device)
1296 {
1297 	struct evdev_device *device = evdev_device(libinput_device);
1298 
1299 	return filter_get_type(device->pointer.filter);
1300 }
1301 
1302 static enum libinput_config_accel_profile
evdev_accel_config_get_default_profile(struct libinput_device *libinput_device)1303 evdev_accel_config_get_default_profile(struct libinput_device *libinput_device)
1304 {
1305 	struct evdev_device *device = evdev_device(libinput_device);
1306 
1307 	if (!device->pointer.filter)
1308 		return LIBINPUT_CONFIG_ACCEL_PROFILE_NONE;
1309 
1310 	/* No device has a flat profile as default */
1311 	return LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE;
1312 }
1313 
1314 static enum libinput_config_status
evdev_set_accel_config(struct libinput_device *libinput_device, struct libinput_config_accel *accel_config)1315 evdev_set_accel_config(struct libinput_device *libinput_device,
1316 		       struct libinput_config_accel *accel_config)
1317 {
1318 	assert(evdev_accel_config_get_profile(libinput_device) == accel_config->profile);
1319 
1320 	struct evdev_device *dev = evdev_device(libinput_device);
1321 
1322 	if (!filter_set_accel_config(dev->pointer.filter, accel_config))
1323 		return LIBINPUT_CONFIG_STATUS_INVALID;
1324 
1325 	return LIBINPUT_CONFIG_STATUS_SUCCESS;
1326 }
1327 
1328 void
evdev_device_init_pointer_acceleration(struct evdev_device *device, struct motion_filter *filter)1329 evdev_device_init_pointer_acceleration(struct evdev_device *device,
1330 				       struct motion_filter *filter)
1331 {
1332 	device->pointer.filter = filter;
1333 
1334 	if (device->base.config.accel == NULL) {
1335 		double default_speed;
1336 
1337 		device->pointer.config.available = evdev_accel_config_available;
1338 		device->pointer.config.set_speed = evdev_accel_config_set_speed;
1339 		device->pointer.config.get_speed = evdev_accel_config_get_speed;
1340 		device->pointer.config.get_default_speed = evdev_accel_config_get_default_speed;
1341 		device->pointer.config.get_profiles = evdev_accel_config_get_profiles;
1342 		device->pointer.config.set_profile = evdev_accel_config_set_profile;
1343 		device->pointer.config.get_profile = evdev_accel_config_get_profile;
1344 		device->pointer.config.get_default_profile = evdev_accel_config_get_default_profile;
1345 		device->pointer.config.set_accel_config = evdev_set_accel_config;
1346 		device->base.config.accel = &device->pointer.config;
1347 
1348 		default_speed = evdev_accel_config_get_default_speed(&device->base);
1349 		evdev_accel_config_set_speed(&device->base, default_speed);
1350 	}
1351 }
1352 
1353 static inline bool
evdev_read_wheel_click_prop(struct evdev_device *device, const char *prop, double *angle)1354 evdev_read_wheel_click_prop(struct evdev_device *device,
1355 			    const char *prop,
1356 			    double *angle)
1357 {
1358 	int val;
1359 
1360 	*angle = DEFAULT_WHEEL_CLICK_ANGLE;
1361 	prop = udev_device_get_property_value(device->udev_device, prop);
1362 	if (!prop)
1363 		return false;
1364 
1365 	val = parse_mouse_wheel_click_angle_property(prop);
1366 	if (val) {
1367 		*angle = val;
1368 		return true;
1369 	}
1370 
1371 	evdev_log_error(device,
1372 		  "mouse wheel click angle is present but invalid, "
1373 		  "using %d degrees instead\n",
1374 		  DEFAULT_WHEEL_CLICK_ANGLE);
1375 
1376 	return false;
1377 }
1378 
1379 static inline bool
evdev_read_wheel_click_count_prop(struct evdev_device *device, const char *prop, double *angle)1380 evdev_read_wheel_click_count_prop(struct evdev_device *device,
1381 				  const char *prop,
1382 				  double *angle)
1383 {
1384 	int val;
1385 
1386 	prop = udev_device_get_property_value(device->udev_device, prop);
1387 	if (!prop)
1388 		return false;
1389 
1390 	val = parse_mouse_wheel_click_angle_property(prop);
1391 	if (val) {
1392 		*angle = 360.0/val;
1393 		return true;
1394 	}
1395 
1396 	evdev_log_error(device,
1397 		  "mouse wheel click count is present but invalid, "
1398 		  "using %d degrees for angle instead instead\n",
1399 		  DEFAULT_WHEEL_CLICK_ANGLE);
1400 	*angle = DEFAULT_WHEEL_CLICK_ANGLE;
1401 
1402 	return false;
1403 }
1404 
1405 static inline struct wheel_angle
evdev_read_wheel_click_props(struct evdev_device *device)1406 evdev_read_wheel_click_props(struct evdev_device *device)
1407 {
1408 	struct wheel_angle angles;
1409 	const char *wheel_count = "MOUSE_WHEEL_CLICK_COUNT";
1410 	const char *wheel_angle = "MOUSE_WHEEL_CLICK_ANGLE";
1411 	const char *hwheel_count = "MOUSE_WHEEL_CLICK_COUNT_HORIZONTAL";
1412 	const char *hwheel_angle = "MOUSE_WHEEL_CLICK_ANGLE_HORIZONTAL";
1413 
1414 	/* CLICK_COUNT overrides CLICK_ANGLE */
1415 	if (evdev_read_wheel_click_count_prop(device, wheel_count, &angles.y) ||
1416 	    evdev_read_wheel_click_prop(device, wheel_angle, &angles.y)) {
1417 		evdev_log_debug(device,
1418 				"wheel: vert click angle: %.2f\n", angles.y);
1419 	}
1420 	if (evdev_read_wheel_click_count_prop(device, hwheel_count, &angles.x) ||
1421 	    evdev_read_wheel_click_prop(device, hwheel_angle, &angles.x)) {
1422 		evdev_log_debug(device,
1423 				"wheel: horizontal click angle: %.2f\n", angles.y);
1424 	} else {
1425 		angles.x = angles.y;
1426 	}
1427 
1428 	return angles;
1429 }
1430 
1431 static inline double
evdev_get_trackpoint_multiplier(struct evdev_device *device)1432 evdev_get_trackpoint_multiplier(struct evdev_device *device)
1433 {
1434 	struct quirks_context *quirks;
1435 	struct quirks *q;
1436 	double multiplier = 1.0;
1437 
1438 	if (!(device->tags & EVDEV_TAG_TRACKPOINT))
1439 		return 1.0;
1440 
1441 	quirks = evdev_libinput_context(device)->quirks;
1442 	q = quirks_fetch_for_device(quirks, device->udev_device);
1443 	if (q) {
1444 		quirks_get_double(q, QUIRK_ATTR_TRACKPOINT_MULTIPLIER, &multiplier);
1445 		quirks_unref(q);
1446 	}
1447 
1448 	if (multiplier <= 0.0) {
1449 		evdev_log_bug_libinput(device,
1450 				       "trackpoint multiplier %.2f is invalid\n",
1451 				       multiplier);
1452 		multiplier = 1.0;
1453 	}
1454 
1455 	if (multiplier != 1.0)
1456 		evdev_log_info(device,
1457 			       "trackpoint multiplier is %.2f\n",
1458 			       multiplier);
1459 
1460 	return multiplier;
1461 }
1462 
1463 static inline bool
evdev_need_velocity_averaging(struct evdev_device *device)1464 evdev_need_velocity_averaging(struct evdev_device *device)
1465 {
1466 	struct quirks_context *quirks;
1467 	struct quirks *q;
1468 	bool use_velocity_averaging = false; /* default off unless we have quirk */
1469 
1470 	quirks = evdev_libinput_context(device)->quirks;
1471 	q = quirks_fetch_for_device(quirks, device->udev_device);
1472 	if (q) {
1473 		quirks_get_bool(q,
1474 				QUIRK_ATTR_USE_VELOCITY_AVERAGING,
1475 				&use_velocity_averaging);
1476 		quirks_unref(q);
1477 	}
1478 
1479 	if (use_velocity_averaging)
1480 		evdev_log_info(device,
1481 			       "velocity averaging is turned on\n");
1482 
1483 	return use_velocity_averaging;
1484 }
1485 
1486 static inline int
evdev_read_dpi_prop(struct evdev_device *device)1487 evdev_read_dpi_prop(struct evdev_device *device)
1488 {
1489 	const char *mouse_dpi;
1490 	int dpi = DEFAULT_MOUSE_DPI;
1491 
1492 	if (device->tags & EVDEV_TAG_TRACKPOINT)
1493 		return DEFAULT_MOUSE_DPI;
1494 
1495 	mouse_dpi = udev_device_get_property_value(device->udev_device,
1496 						   "MOUSE_DPI");
1497 	if (mouse_dpi) {
1498 		dpi = parse_mouse_dpi_property(mouse_dpi);
1499 		if (!dpi) {
1500 			evdev_log_error(device,
1501 					"mouse DPI property is present but invalid, "
1502 					"using %d DPI instead\n",
1503 					DEFAULT_MOUSE_DPI);
1504 			dpi = DEFAULT_MOUSE_DPI;
1505 		}
1506 		evdev_log_info(device,
1507 			       "device set to %d DPI\n",
1508 			       dpi);
1509 	}
1510 
1511 	return dpi;
1512 }
1513 
1514 static inline uint32_t
evdev_read_model_flags(struct evdev_device *device)1515 evdev_read_model_flags(struct evdev_device *device)
1516 {
1517 	const struct model_map {
1518 		enum quirk quirk;
1519 		enum evdev_device_model model;
1520 	} model_map[] = {
1521 #define MODEL(name) { QUIRK_MODEL_##name, EVDEV_MODEL_##name }
1522 		MODEL(WACOM_TOUCHPAD),
1523 		MODEL(SYNAPTICS_SERIAL_TOUCHPAD),
1524 		MODEL(ALPS_SERIAL_TOUCHPAD),
1525 		MODEL(LENOVO_T450_TOUCHPAD),
1526 		MODEL(TRACKBALL),
1527 		MODEL(APPLE_TOUCHPAD_ONEBUTTON),
1528 		MODEL(LENOVO_SCROLLPOINT),
1529 #undef MODEL
1530 		{ 0, 0 },
1531 	};
1532 	const struct model_map *m = model_map;
1533 	uint32_t model_flags = 0;
1534 	uint32_t all_model_flags = 0;
1535 	struct quirks_context *quirks;
1536 	struct quirks *q;
1537 
1538 	quirks = evdev_libinput_context(device)->quirks;
1539 	q = quirks_fetch_for_device(quirks, device->udev_device);
1540 
1541 	while (q && m->quirk) {
1542 		bool is_set;
1543 
1544 		/* Check for flag re-use */
1545 		assert((all_model_flags & m->model) == 0);
1546 		all_model_flags |= m->model;
1547 
1548 		if (quirks_get_bool(q, m->quirk, &is_set)) {
1549 			if (is_set) {
1550 				evdev_log_debug(device,
1551 						"tagged as %s\n",
1552 						quirk_get_name(m->quirk));
1553 				model_flags |= m->model;
1554 			} else {
1555 				evdev_log_debug(device,
1556 						"untagged as %s\n",
1557 						quirk_get_name(m->quirk));
1558 				model_flags &= ~m->model;
1559 			}
1560 		}
1561 
1562 		m++;
1563 	}
1564 
1565 	quirks_unref(q);
1566 
1567 	if (parse_udev_flag(device,
1568 			    device->udev_device,
1569 			    "ID_INPUT_TRACKBALL")) {
1570 		evdev_log_debug(device, "tagged as trackball\n");
1571 		model_flags |= EVDEV_MODEL_TRACKBALL;
1572 	}
1573 
1574 	/**
1575 	 * Device is 6 years old at the time of writing this and this was
1576 	 * one of the few udev properties that wasn't reserved for private
1577 	 * usage, so we need to keep this for backwards compat.
1578 	 */
1579 	if (parse_udev_flag(device,
1580 			    device->udev_device,
1581 			    "LIBINPUT_MODEL_LENOVO_X220_TOUCHPAD_FW81")) {
1582 		evdev_log_debug(device, "tagged as trackball\n");
1583 		model_flags |= EVDEV_MODEL_LENOVO_X220_TOUCHPAD_FW81;
1584 	}
1585 
1586 	if (parse_udev_flag(device, device->udev_device,
1587 			    "LIBINPUT_TEST_DEVICE")) {
1588 		evdev_log_debug(device, "is a test device\n");
1589 		model_flags |= EVDEV_MODEL_TEST_DEVICE;
1590 	}
1591 
1592 	return model_flags;
1593 }
1594 
1595 static inline bool
evdev_read_attr_res_prop(struct evdev_device *device, size_t *xres, size_t *yres)1596 evdev_read_attr_res_prop(struct evdev_device *device,
1597 			 size_t *xres,
1598 			 size_t *yres)
1599 {
1600 	struct quirks_context *quirks;
1601 	struct quirks *q;
1602 	struct quirk_dimensions dim;
1603 	bool rc = false;
1604 
1605 	quirks = evdev_libinput_context(device)->quirks;
1606 	q = quirks_fetch_for_device(quirks, device->udev_device);
1607 	if (!q)
1608 		return false;
1609 
1610 	rc = quirks_get_dimensions(q, QUIRK_ATTR_RESOLUTION_HINT, &dim);
1611 	if (rc) {
1612 		*xres = dim.x;
1613 		*yres = dim.y;
1614 	}
1615 
1616 	quirks_unref(q);
1617 
1618 	return rc;
1619 }
1620 
1621 static inline bool
evdev_read_attr_size_prop(struct evdev_device *device, size_t *size_x, size_t *size_y)1622 evdev_read_attr_size_prop(struct evdev_device *device,
1623 			  size_t *size_x,
1624 			  size_t *size_y)
1625 {
1626 	struct quirks_context *quirks;
1627 	struct quirks *q;
1628 	struct quirk_dimensions dim;
1629 	bool rc = false;
1630 
1631 	quirks = evdev_libinput_context(device)->quirks;
1632 	q = quirks_fetch_for_device(quirks, device->udev_device);
1633 	if (!q)
1634 		return false;
1635 
1636 	rc = quirks_get_dimensions(q, QUIRK_ATTR_SIZE_HINT, &dim);
1637 	if (rc) {
1638 		*size_x = dim.x;
1639 		*size_y = dim.y;
1640 	}
1641 
1642 	quirks_unref(q);
1643 
1644 	return rc;
1645 }
1646 
1647 /* Return 1 if the device is set to the fake resolution or 0 otherwise */
1648 static inline int
evdev_fix_abs_resolution(struct evdev_device *device, unsigned int xcode, unsigned int ycode)1649 evdev_fix_abs_resolution(struct evdev_device *device,
1650 			 unsigned int xcode,
1651 			 unsigned int ycode)
1652 {
1653 	struct libevdev *evdev = device->evdev;
1654 	const struct input_absinfo *absx, *absy;
1655 	size_t widthmm = 0, heightmm = 0;
1656 	size_t xres = EVDEV_FAKE_RESOLUTION,
1657 	       yres = EVDEV_FAKE_RESOLUTION;
1658 
1659 	if (!(xcode == ABS_X && ycode == ABS_Y)  &&
1660 	    !(xcode == ABS_MT_POSITION_X && ycode == ABS_MT_POSITION_Y)) {
1661 		evdev_log_bug_libinput(device,
1662 				       "invalid x/y code combination %d/%d\n",
1663 				       xcode,
1664 				       ycode);
1665 		return 0;
1666 	}
1667 
1668 	absx = libevdev_get_abs_info(evdev, xcode);
1669 	absy = libevdev_get_abs_info(evdev, ycode);
1670 
1671 	if (absx->resolution != 0 || absy->resolution != 0)
1672 		return 0;
1673 
1674 	/* Note: we *do not* override resolutions if provided by the kernel.
1675 	 * If a device needs this, add it to 60-evdev.hwdb. The libinput
1676 	 * property is only for general size hints where we can make
1677 	 * educated guesses but don't know better.
1678 	 */
1679 	if (!evdev_read_attr_res_prop(device, &xres, &yres) &&
1680 	    evdev_read_attr_size_prop(device, &widthmm, &heightmm)) {
1681 		xres = absinfo_range(absx)/widthmm;
1682 		yres = absinfo_range(absy)/heightmm;
1683 	}
1684 
1685 	/* libevdev_set_abs_resolution() changes the absinfo we already
1686 	   have a pointer to, no need to fetch it again */
1687 	libevdev_set_abs_resolution(evdev, xcode, xres);
1688 	libevdev_set_abs_resolution(evdev, ycode, yres);
1689 
1690 	return xres == EVDEV_FAKE_RESOLUTION;
1691 }
1692 
1693 static enum evdev_device_udev_tags
evdev_device_get_udev_tags(struct evdev_device *device, struct udev_device *udev_device)1694 evdev_device_get_udev_tags(struct evdev_device *device,
1695 			   struct udev_device *udev_device)
1696 {
1697 	enum evdev_device_udev_tags tags = 0;
1698 	int i;
1699 
1700 	for (i = 0; i < 2 && udev_device; i++) {
1701 		unsigned j;
1702 		for (j = 0; j < ARRAY_LENGTH(evdev_udev_tag_matches); j++) {
1703 			const struct evdev_udev_tag_match match = evdev_udev_tag_matches[j];
1704 			if (parse_udev_flag(device,
1705 					    udev_device,
1706 					    match.name))
1707 				tags |= match.tag;
1708 		}
1709 		udev_device = udev_device_get_parent(udev_device);
1710 	}
1711 
1712 	return tags;
1713 }
1714 
1715 static inline void
evdev_fix_android_mt(struct evdev_device *device)1716 evdev_fix_android_mt(struct evdev_device *device)
1717 {
1718 	struct libevdev *evdev = device->evdev;
1719 
1720 	if (libevdev_has_event_code(evdev, EV_ABS, ABS_X) ||
1721 	    libevdev_has_event_code(evdev, EV_ABS, ABS_Y))
1722 		return;
1723 
1724 	if (!libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) ||
1725 	    !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y) ||
1726 	    evdev_is_fake_mt_device(device))
1727 		return;
1728 
1729 	libevdev_enable_event_code(evdev, EV_ABS, ABS_X,
1730 		      libevdev_get_abs_info(evdev, ABS_MT_POSITION_X));
1731 	libevdev_enable_event_code(evdev, EV_ABS, ABS_Y,
1732 		      libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y));
1733 }
1734 
1735 static inline bool
evdev_check_min_max(struct evdev_device *device, unsigned int code)1736 evdev_check_min_max(struct evdev_device *device, unsigned int code)
1737 {
1738 	struct libevdev *evdev = device->evdev;
1739 	const struct input_absinfo *absinfo;
1740 
1741 	if (!libevdev_has_event_code(evdev, EV_ABS, code))
1742 		return true;
1743 
1744 	absinfo = libevdev_get_abs_info(evdev, code);
1745 	if (absinfo->minimum == absinfo->maximum) {
1746 		/* Some devices have a sort-of legitimate min/max of 0 for
1747 		 * ABS_MISC and above (e.g. Roccat Kone XTD). Don't ignore
1748 		 * them, simply disable the axes so we won't get events,
1749 		 * we don't know what to do with them anyway.
1750 		 */
1751 		if (absinfo->minimum == 0 &&
1752 		    code >= ABS_MISC && code < ABS_MT_SLOT) {
1753 			evdev_log_info(device,
1754 				       "disabling EV_ABS %#x on device (min == max == 0)\n",
1755 				       code);
1756 			libevdev_disable_event_code(device->evdev,
1757 						    EV_ABS,
1758 						    code);
1759 		} else {
1760 			evdev_log_bug_kernel(device,
1761 					     "device has min == max on %s\n",
1762 					     libevdev_event_code_get_name(EV_ABS, code));
1763 			return false;
1764 		}
1765 	}
1766 
1767 	return true;
1768 }
1769 
1770 static bool
evdev_reject_device(struct evdev_device *device)1771 evdev_reject_device(struct evdev_device *device)
1772 {
1773 	struct libevdev *evdev = device->evdev;
1774 	unsigned int code;
1775 	const struct input_absinfo *absx, *absy;
1776 
1777 	if (libevdev_has_event_code(evdev, EV_ABS, ABS_X) ^
1778 	    libevdev_has_event_code(evdev, EV_ABS, ABS_Y))
1779 		return true;
1780 
1781 	if (libevdev_has_event_code(evdev, EV_REL, REL_X) ^
1782 	    libevdev_has_event_code(evdev, EV_REL, REL_Y))
1783 		return true;
1784 
1785 	if (!evdev_is_fake_mt_device(device) &&
1786 	    libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) ^
1787 	    libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y))
1788 		return true;
1789 
1790 	if (libevdev_has_event_code(evdev, EV_ABS, ABS_X)) {
1791 		absx = libevdev_get_abs_info(evdev, ABS_X);
1792 		absy = libevdev_get_abs_info(evdev, ABS_Y);
1793 		if ((absx->resolution == 0 && absy->resolution != 0) ||
1794 		    (absx->resolution != 0 && absy->resolution == 0)) {
1795 			evdev_log_bug_kernel(device,
1796 				       "kernel has only x or y resolution, not both.\n");
1797 			return true;
1798 		}
1799 	}
1800 
1801 	if (!evdev_is_fake_mt_device(device) &&
1802 	    libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X)) {
1803 		absx = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
1804 		absy = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
1805 		if ((absx->resolution == 0 && absy->resolution != 0) ||
1806 		    (absx->resolution != 0 && absy->resolution == 0)) {
1807 			evdev_log_bug_kernel(device,
1808 				       "kernel has only x or y MT resolution, not both.\n");
1809 			return true;
1810 		}
1811 	}
1812 
1813 	for (code = 0; code < ABS_CNT; code++) {
1814 		switch (code) {
1815 		case ABS_MISC:
1816 		case ABS_MT_SLOT:
1817 		case ABS_MT_TOOL_TYPE:
1818 			break;
1819 		default:
1820 			if (!evdev_check_min_max(device, code))
1821 				return true;
1822 		}
1823 	}
1824 
1825 	return false;
1826 }
1827 
1828 static void
evdev_extract_abs_axes(struct evdev_device *device, enum evdev_device_udev_tags udev_tags)1829 evdev_extract_abs_axes(struct evdev_device *device,
1830 		       enum evdev_device_udev_tags udev_tags)
1831 {
1832 	struct libevdev *evdev = device->evdev;
1833 	int fuzz;
1834 
1835 	if (!libevdev_has_event_code(evdev, EV_ABS, ABS_X) ||
1836 	    !libevdev_has_event_code(evdev, EV_ABS, ABS_Y))
1837 		 return;
1838 
1839 	if (evdev_fix_abs_resolution(device, ABS_X, ABS_Y))
1840 		device->abs.is_fake_resolution = true;
1841 
1842 	if (udev_tags & (EVDEV_UDEV_TAG_TOUCHPAD|EVDEV_UDEV_TAG_TOUCHSCREEN)) {
1843 		fuzz = evdev_read_fuzz_prop(device, ABS_X);
1844 		libevdev_set_abs_fuzz(evdev, ABS_X, fuzz);
1845 		fuzz = evdev_read_fuzz_prop(device, ABS_Y);
1846 		libevdev_set_abs_fuzz(evdev, ABS_Y, fuzz);
1847 	}
1848 
1849 	device->abs.absinfo_x = libevdev_get_abs_info(evdev, ABS_X);
1850 	device->abs.absinfo_y = libevdev_get_abs_info(evdev, ABS_Y);
1851 	device->abs.dimensions.x = abs((int)absinfo_range(device->abs.absinfo_x));
1852 	device->abs.dimensions.y = abs((int)absinfo_range(device->abs.absinfo_y));
1853 
1854 	if (evdev_is_fake_mt_device(device) ||
1855 	    !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) ||
1856 	    !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y))
1857 		 return;
1858 
1859 	if (evdev_fix_abs_resolution(device,
1860 				     ABS_MT_POSITION_X,
1861 				     ABS_MT_POSITION_Y))
1862 		device->abs.is_fake_resolution = true;
1863 
1864 	if ((fuzz = evdev_read_fuzz_prop(device, ABS_MT_POSITION_X)))
1865 	    libevdev_set_abs_fuzz(evdev, ABS_MT_POSITION_X, fuzz);
1866 	if ((fuzz = evdev_read_fuzz_prop(device, ABS_MT_POSITION_Y)))
1867 	    libevdev_set_abs_fuzz(evdev, ABS_MT_POSITION_Y, fuzz);
1868 
1869 	device->abs.absinfo_x = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
1870 	device->abs.absinfo_y = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
1871 	device->abs.dimensions.x = abs((int)absinfo_range(device->abs.absinfo_x));
1872 	device->abs.dimensions.y = abs((int)absinfo_range(device->abs.absinfo_y));
1873 	device->is_mt = 1;
1874 }
1875 
1876 static void
evdev_disable_accelerometer_axes(struct evdev_device *device)1877 evdev_disable_accelerometer_axes(struct evdev_device *device)
1878 {
1879 	struct libevdev *evdev = device->evdev;
1880 
1881 	libevdev_disable_event_code(evdev, EV_ABS, ABS_X);
1882 	libevdev_disable_event_code(evdev, EV_ABS, ABS_Y);
1883 	libevdev_disable_event_code(evdev, EV_ABS, ABS_Z);
1884 
1885 	libevdev_disable_event_code(evdev, EV_ABS, REL_X);
1886 	libevdev_disable_event_code(evdev, EV_ABS, REL_Y);
1887 	libevdev_disable_event_code(evdev, EV_ABS, REL_Z);
1888 }
1889 
1890 static bool
evdev_device_is_joystick_or_gamepad(struct evdev_device *device)1891 evdev_device_is_joystick_or_gamepad(struct evdev_device *device)
1892 {
1893 	enum evdev_device_udev_tags udev_tags;
1894 	bool has_joystick_tags;
1895 	struct libevdev *evdev = device->evdev;
1896 	unsigned int code;
1897 
1898 	/* The EVDEV_UDEV_TAG_JOYSTICK is set when a joystick or gamepad button
1899 	 * is found. However, it can not be used to identify joysticks or
1900 	 * gamepads because there are keyboards that also have it. Even worse,
1901 	 * many joysticks also map KEY_* and thus are tagged as keyboards.
1902 	 *
1903 	 * In order to be able to detect joysticks and gamepads and
1904 	 * differentiate them from keyboards, apply the following rules:
1905 	 *
1906 	 *  1. The device is tagged as joystick but not as tablet
1907 	 *  2. The device doesn't have 4 well-known keyboard keys
1908 	 *  3. It has at least 2 joystick buttons
1909 	 *  4. It doesn't have 10 keyboard keys */
1910 
1911 	udev_tags = evdev_device_get_udev_tags(device, device->udev_device);
1912 	has_joystick_tags = (udev_tags & EVDEV_UDEV_TAG_JOYSTICK) &&
1913 			    !(udev_tags & EVDEV_UDEV_TAG_TABLET) &&
1914 			    !(udev_tags & EVDEV_UDEV_TAG_TABLET_PAD);
1915 
1916 	if (!has_joystick_tags)
1917 		return false;
1918 
1919 	unsigned int num_well_known_keys = 0;
1920 
1921 	for (size_t i = 0; i < ARRAY_LENGTH(well_known_keyboard_keys); i++) {
1922 		code = well_known_keyboard_keys[i];
1923 		if (libevdev_has_event_code(evdev, EV_KEY, code))
1924 			num_well_known_keys++;
1925 	}
1926 
1927 	if (num_well_known_keys >= 4) /* should not have 4 well-known keys */
1928 		return false;
1929 
1930 	unsigned int num_joystick_btns = 0;
1931 
1932 	for (code = BTN_JOYSTICK; code < BTN_DIGI; code++) {
1933 		if (libevdev_has_event_code(evdev, EV_KEY, code))
1934 			num_joystick_btns++;
1935 	}
1936 
1937 	for (code = BTN_TRIGGER_HAPPY; code <= BTN_TRIGGER_HAPPY40; code++) {
1938 		if (libevdev_has_event_code(evdev, EV_KEY, code))
1939 			num_joystick_btns++;
1940 	}
1941 
1942 	if (num_joystick_btns < 2) /* require at least 2 joystick buttons */
1943 		return false;
1944 
1945 	unsigned int num_keys = 0;
1946 
1947 	for (code = KEY_ESC; code <= KEY_MICMUTE; code++) {
1948 		if (libevdev_has_event_code(evdev, EV_KEY, code) )
1949 			num_keys++;
1950 	}
1951 
1952 	for (code = KEY_OK; code <= KEY_LIGHTS_TOGGLE; code++) {
1953 		if (libevdev_has_event_code(evdev, EV_KEY, code) )
1954 			num_keys++;
1955 	}
1956 
1957 	for (code = KEY_ALS_TOGGLE; code < BTN_TRIGGER_HAPPY; code++) {
1958 		if (libevdev_has_event_code(evdev, EV_KEY, code) )
1959 			num_keys++;
1960 	}
1961 
1962 	if (num_keys >= 10) /* should not have 10 keyboard keys */
1963 		return false;
1964 
1965 	return true;
1966 }
1967 
1968 static struct evdev_dispatch *
evdev_configure_device(struct evdev_device *device)1969 evdev_configure_device(struct evdev_device *device)
1970 {
1971 	struct libevdev *evdev = device->evdev;
1972 	enum evdev_device_udev_tags udev_tags;
1973 	unsigned int tablet_tags;
1974 	struct evdev_dispatch *dispatch;
1975 
1976 	udev_tags = evdev_device_get_udev_tags(device, device->udev_device);
1977 
1978 	if ((udev_tags & EVDEV_UDEV_TAG_INPUT) == 0 ||
1979 	    (udev_tags & ~EVDEV_UDEV_TAG_INPUT) == 0) {
1980 		evdev_log_info(device,
1981 			       "not tagged as supported input device\n");
1982 		return NULL;
1983 	}
1984 
1985 	evdev_log_info(device,
1986 		 "is tagged by udev as:%s%s%s%s%s%s%s%s%s%s%s\n",
1987 		 udev_tags & EVDEV_UDEV_TAG_KEYBOARD ? " Keyboard" : "",
1988 		 udev_tags & EVDEV_UDEV_TAG_MOUSE ? " Mouse" : "",
1989 		 udev_tags & EVDEV_UDEV_TAG_TOUCHPAD ? " Touchpad" : "",
1990 		 udev_tags & EVDEV_UDEV_TAG_TOUCHSCREEN ? " Touchscreen" : "",
1991 		 udev_tags & EVDEV_UDEV_TAG_TABLET ? " Tablet" : "",
1992 		 udev_tags & EVDEV_UDEV_TAG_POINTINGSTICK ? " Pointingstick" : "",
1993 		 udev_tags & EVDEV_UDEV_TAG_JOYSTICK ? " Joystick" : "",
1994 		 udev_tags & EVDEV_UDEV_TAG_ACCELEROMETER ? " Accelerometer" : "",
1995 		 udev_tags & EVDEV_UDEV_TAG_TABLET_PAD ? " TabletPad" : "",
1996 		 udev_tags & EVDEV_UDEV_TAG_TRACKBALL ? " Trackball" : "",
1997 		 udev_tags & EVDEV_UDEV_TAG_SWITCH ? " Switch" : "");
1998 
1999 	/* Ignore pure accelerometers, but accept devices that are
2000 	 * accelerometers with other axes */
2001 	if (udev_tags == (EVDEV_UDEV_TAG_INPUT|EVDEV_UDEV_TAG_ACCELEROMETER)) {
2002 		evdev_log_info(device,
2003 			 "device is an accelerometer, ignoring\n");
2004 		return NULL;
2005 	}
2006 
2007 	if (udev_tags & EVDEV_UDEV_TAG_ACCELEROMETER) {
2008 		evdev_disable_accelerometer_axes(device);
2009 	}
2010 
2011 	if (evdev_device_is_joystick_or_gamepad(device)) {
2012 		evdev_log_info(device,
2013 			       "device is a joystick or a gamepad, ignoring\n");
2014 		return NULL;
2015 	}
2016 
2017 	if (evdev_reject_device(device)) {
2018 		evdev_log_info(device, "was rejected\n");
2019 		return NULL;
2020 	}
2021 
2022 	if (!evdev_is_fake_mt_device(device))
2023 		evdev_fix_android_mt(device);
2024 
2025 	if (libevdev_has_event_code(evdev, EV_ABS, ABS_X)) {
2026 		evdev_extract_abs_axes(device, udev_tags);
2027 
2028 		if (evdev_is_fake_mt_device(device))
2029 			udev_tags &= ~EVDEV_UDEV_TAG_TOUCHSCREEN;
2030 	}
2031 
2032 	if (evdev_device_has_model_quirk(device,
2033 					 QUIRK_MODEL_DELL_CANVAS_TOTEM)) {
2034 		dispatch = evdev_totem_create(device);
2035 		device->seat_caps |= EVDEV_DEVICE_TABLET;
2036 		evdev_log_info(device, "device is a totem\n");
2037 		return dispatch;
2038 	}
2039 
2040 	/* libwacom assigns touchpad (or touchscreen) _and_ tablet to the
2041 	   tablet touch bits, so make sure we don't initialize the tablet
2042 	   interface for the touch device */
2043 	tablet_tags = EVDEV_UDEV_TAG_TABLET |
2044 		      EVDEV_UDEV_TAG_TOUCHPAD |
2045 		      EVDEV_UDEV_TAG_TOUCHSCREEN;
2046 
2047 	/* libwacom assigns tablet _and_ tablet_pad to the pad devices */
2048 	if (udev_tags & EVDEV_UDEV_TAG_TABLET_PAD) {
2049 		dispatch = evdev_tablet_pad_create(device);
2050 		device->seat_caps |= EVDEV_DEVICE_TABLET_PAD;
2051 		evdev_log_info(device, "device is a tablet pad\n");
2052 		return dispatch;
2053 
2054 	}
2055 
2056 	if ((udev_tags & tablet_tags) == EVDEV_UDEV_TAG_TABLET) {
2057 		dispatch = evdev_tablet_create(device);
2058 		device->seat_caps |= EVDEV_DEVICE_TABLET;
2059 		evdev_log_info(device, "device is a tablet\n");
2060 		return dispatch;
2061 	}
2062 
2063 	if (udev_tags & EVDEV_UDEV_TAG_TOUCHPAD) {
2064 		if (udev_tags & EVDEV_UDEV_TAG_TABLET)
2065 			evdev_tag_tablet_touchpad(device);
2066 		/* whether velocity should be averaged, false by default */
2067 		device->use_velocity_averaging = evdev_need_velocity_averaging(device);
2068 		dispatch = evdev_mt_touchpad_create(device);
2069 		evdev_log_info(device, "device is a touchpad\n");
2070 		return dispatch;
2071 	}
2072 
2073 	if (udev_tags & EVDEV_UDEV_TAG_MOUSE ||
2074 	    udev_tags & EVDEV_UDEV_TAG_POINTINGSTICK) {
2075 		evdev_tag_external_mouse(device, device->udev_device);
2076 		evdev_tag_trackpoint(device, device->udev_device);
2077 		if (device->tags & EVDEV_TAG_TRACKPOINT)
2078 			device->trackpoint_multiplier = evdev_get_trackpoint_multiplier(device);
2079 		else
2080 			device->dpi = evdev_read_dpi_prop(device);
2081 		/* whether velocity should be averaged, false by default */
2082 		device->use_velocity_averaging = evdev_need_velocity_averaging(device);
2083 
2084 		device->seat_caps |= EVDEV_DEVICE_POINTER;
2085 
2086 		evdev_log_info(device, "device is a pointer\n");
2087 
2088 		/* want left-handed config option */
2089 		device->left_handed.want_enabled = true;
2090 		/* want natural-scroll config option */
2091 		device->scroll.natural_scrolling_enabled = true;
2092 		/* want button scrolling config option */
2093 		if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
2094 		    libevdev_has_event_code(evdev, EV_REL, REL_Y))
2095 			device->scroll.want_button = 1;
2096 	}
2097 
2098 	if (udev_tags & EVDEV_UDEV_TAG_KEYBOARD) {
2099 		device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
2100 		evdev_log_info(device, "device is a keyboard\n");
2101 
2102 		/* want natural-scroll config option */
2103 		if (libevdev_has_event_code(evdev, EV_REL, REL_WHEEL) ||
2104 		    libevdev_has_event_code(evdev, EV_REL, REL_HWHEEL)) {
2105 			device->scroll.natural_scrolling_enabled = true;
2106 			device->seat_caps |= EVDEV_DEVICE_POINTER;
2107 		}
2108 
2109 		evdev_tag_keyboard(device, device->udev_device);
2110 	}
2111 
2112 	if (udev_tags & EVDEV_UDEV_TAG_TOUCHSCREEN) {
2113 		device->seat_caps |= EVDEV_DEVICE_TOUCH;
2114 		evdev_log_info(device, "device is a touch device\n");
2115 	}
2116 
2117 	if (udev_tags & EVDEV_UDEV_TAG_SWITCH) {
2118 		if (libevdev_has_event_code(evdev, EV_SW, SW_LID)) {
2119 			device->seat_caps |= EVDEV_DEVICE_SWITCH;
2120 			device->tags |= EVDEV_TAG_LID_SWITCH;
2121 		}
2122 
2123 		if (libevdev_has_event_code(evdev, EV_SW, SW_TABLET_MODE)) {
2124 		    if (evdev_device_has_model_quirk(device,
2125 				 QUIRK_MODEL_TABLET_MODE_SWITCH_UNRELIABLE)) {
2126 			    evdev_log_info(device,
2127 				"device is an unreliable tablet mode switch, filtering events.\n");
2128 			    libevdev_disable_event_code(device->evdev,
2129 							EV_SW,
2130 							SW_TABLET_MODE);
2131 		    } else {
2132 			    device->tags |= EVDEV_TAG_TABLET_MODE_SWITCH;
2133 			    device->seat_caps |= EVDEV_DEVICE_SWITCH;
2134 		    }
2135 		}
2136 
2137 		if (device->seat_caps & EVDEV_DEVICE_SWITCH)
2138 		    evdev_log_info(device, "device is a switch device\n");
2139 	}
2140 
2141 	if (device->seat_caps & EVDEV_DEVICE_POINTER &&
2142 	    libevdev_has_event_code(evdev, EV_REL, REL_X) &&
2143 	    libevdev_has_event_code(evdev, EV_REL, REL_Y) &&
2144 	    !evdev_init_accel(device, LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE)) {
2145 		evdev_log_error(device,
2146 				"failed to initialize pointer acceleration\n");
2147 		return NULL;
2148 	}
2149 
2150 	if (evdev_device_has_model_quirk(device, QUIRK_MODEL_INVERT_HORIZONTAL_SCROLLING)) {
2151 		device->scroll.invert_horizontal_scrolling = true;
2152 	}
2153 
2154 	return fallback_dispatch_create(&device->base);
2155 }
2156 
2157 static void
evdev_notify_added_device(struct evdev_device *device)2158 evdev_notify_added_device(struct evdev_device *device)
2159 {
2160 	struct libinput_device *dev;
2161 
2162 	list_for_each(dev, &device->base.seat->devices_list, link) {
2163 		struct evdev_device *d = evdev_device(dev);
2164 		if (dev == &device->base)
2165 			continue;
2166 
2167 		/* Notify existing device d about addition of device */
2168 		if (d->dispatch->interface->device_added)
2169 			d->dispatch->interface->device_added(d, device);
2170 
2171 		/* Notify new device about existing device d */
2172 		if (device->dispatch->interface->device_added)
2173 			device->dispatch->interface->device_added(device, d);
2174 
2175 		/* Notify new device if existing device d is suspended */
2176 		if (d->is_suspended &&
2177 		    device->dispatch->interface->device_suspended)
2178 			device->dispatch->interface->device_suspended(device, d);
2179 	}
2180 
2181 	notify_added_device(&device->base);
2182 
2183 	if (device->dispatch->interface->post_added)
2184 		device->dispatch->interface->post_added(device,
2185 							device->dispatch);
2186 }
2187 
2188 static bool
evdev_device_have_same_syspath(struct udev_device *udev_device, int fd)2189 evdev_device_have_same_syspath(struct udev_device *udev_device, int fd)
2190 {
2191 	struct udev *udev = udev_device_get_udev(udev_device);
2192 	struct udev_device *udev_device_new = NULL;
2193 	struct stat st;
2194 	bool rc = false;
2195 
2196 	if (fstat(fd, &st) < 0)
2197 		goto out;
2198 
2199 	udev_device_new = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
2200 	if (!udev_device_new)
2201 		goto out;
2202 
2203 	rc = streq(udev_device_get_syspath(udev_device_new),
2204 		   udev_device_get_syspath(udev_device));
2205 out:
2206 	if (udev_device_new)
2207 		udev_device_unref(udev_device_new);
2208 	return rc;
2209 }
2210 
2211 static bool
evdev_set_device_group(struct evdev_device *device, struct udev_device *udev_device)2212 evdev_set_device_group(struct evdev_device *device,
2213 		       struct udev_device *udev_device)
2214 {
2215 	struct libinput *libinput = evdev_libinput_context(device);
2216 	struct libinput_device_group *group = NULL;
2217 	const char *udev_group;
2218 
2219 	udev_group = udev_device_get_property_value(udev_device,
2220 						    "LIBINPUT_DEVICE_GROUP");
2221 	if (udev_group)
2222 		group = libinput_device_group_find_group(libinput, udev_group);
2223 
2224 	if (!group) {
2225 		group = libinput_device_group_create(libinput, udev_group);
2226 		if (!group)
2227 			return false;
2228 		libinput_device_set_device_group(&device->base, group);
2229 		libinput_device_group_unref(group);
2230 	} else {
2231 		libinput_device_set_device_group(&device->base, group);
2232 	}
2233 
2234 	return true;
2235 }
2236 
2237 static inline void
evdev_drain_fd(int fd)2238 evdev_drain_fd(int fd)
2239 {
2240 	struct input_event ev[24];
2241 	size_t sz = sizeof ev;
2242 
2243 	while (read(fd, &ev, sz) == (int)sz) {
2244 		/* discard all pending events */
2245 	}
2246 }
2247 
2248 static inline void
evdev_pre_configure_model_quirks(struct evdev_device *device)2249 evdev_pre_configure_model_quirks(struct evdev_device *device)
2250 {
2251 	struct quirks_context *quirks;
2252 	struct quirks *q;
2253 	const struct quirk_tuples *t;
2254 	char *prop;
2255 
2256 	/* Touchpad claims to have 4 slots but only ever sends 2
2257 	 * https://bugs.freedesktop.org/show_bug.cgi?id=98100 */
2258 	if (evdev_device_has_model_quirk(device, QUIRK_MODEL_HP_ZBOOK_STUDIO_G3))
2259 		libevdev_set_abs_maximum(device->evdev, ABS_MT_SLOT, 1);
2260 
2261 	/* Generally we don't care about MSC_TIMESTAMP and it can cause
2262 	 * unnecessary wakeups but on some devices we need to watch it for
2263 	 * pointer jumps */
2264 	quirks = evdev_libinput_context(device)->quirks;
2265 	q = quirks_fetch_for_device(quirks, device->udev_device);
2266 	if (!q ||
2267 	    !quirks_get_string(q, QUIRK_ATTR_MSC_TIMESTAMP, &prop) ||
2268 	    !streq(prop, "watch")) {
2269 		libevdev_disable_event_code(device->evdev, EV_MSC, MSC_TIMESTAMP);
2270 	}
2271 
2272 	if (quirks_get_tuples(q, QUIRK_ATTR_EVENT_CODE, &t)) {
2273 		for (size_t i = 0; i < t->ntuples; i++) {
2274 			const struct input_absinfo absinfo = {
2275 				.minimum = 0,
2276 				.maximum = 1,
2277 			};
2278 
2279 			int type = t->tuples[i].first;
2280 			int code = t->tuples[i].second;
2281 			bool enable = t->tuples[i].third;
2282 
2283 			if (code == EVENT_CODE_UNDEFINED) {
2284 				if (enable)
2285 					libevdev_enable_event_type(device->evdev, type);
2286 				else
2287 					libevdev_disable_event_type(device->evdev, type);
2288 			} else {
2289 				if (enable)
2290 					libevdev_enable_event_code(device->evdev,
2291 								   type,
2292 								   code,
2293 								   type == EV_ABS ?  &absinfo : NULL);
2294 				else
2295 					libevdev_disable_event_code(device->evdev,
2296 								    type,
2297 								    code);
2298 			}
2299 			evdev_log_debug(device,
2300 					"quirks: %s %s %s (%#x %#x)\n",
2301 					enable ? "enabling" : "disabling",
2302 					libevdev_event_type_get_name(type),
2303 					libevdev_event_code_get_name(type, code),
2304 					type,
2305 					code);
2306 		}
2307 	}
2308 
2309 	if (quirks_get_tuples(q, QUIRK_ATTR_INPUT_PROP, &t)) {
2310 		for (size_t idx = 0; idx < t->ntuples; idx++) {
2311 			unsigned int p = t->tuples[idx].first;
2312 			bool enable = t->tuples[idx].second;
2313 
2314 			if (enable) {
2315 				libevdev_enable_property(device->evdev, p);
2316 			}
2317 			else {
2318 #if HAVE_LIBEVDEV_DISABLE_PROPERTY
2319 				libevdev_disable_property(device->evdev, p);
2320 #else
2321 				evdev_log_error(device,
2322 						"quirks: a quirk for this device requires newer libevdev than installed\n");
2323 #endif
2324 			}
2325 			evdev_log_debug(device,
2326 					"quirks: %s %s (%#x)\n",
2327 					enable ? "enabling" : "disabling",
2328 					libevdev_property_get_name(p),
2329 					p);
2330 		}
2331 	}
2332 
2333 	quirks_unref(q);
2334 }
2335 
2336 static void
libevdev_log_func(const struct libevdev *evdev, enum libevdev_log_priority priority, void *data, const char *file, int line, const char *func, const char *format, va_list args)2337 libevdev_log_func(const struct libevdev *evdev,
2338 		  enum libevdev_log_priority priority,
2339 		  void *data,
2340 		  const char *file,
2341 		  int line,
2342 		  const char *func,
2343 		  const char *format,
2344 		  va_list args)
2345 {
2346 	struct libinput *libinput = data;
2347 	enum libinput_log_priority pri = LIBINPUT_LOG_PRIORITY_ERROR;
2348 	const char prefix[] = "libevdev: ";
2349 	char fmt[strlen(format) + strlen(prefix) + 1];
2350 
2351 	switch (priority) {
2352 	case LIBEVDEV_LOG_ERROR:
2353 		pri = LIBINPUT_LOG_PRIORITY_ERROR;
2354 		break;
2355 	case LIBEVDEV_LOG_INFO:
2356 		pri = LIBINPUT_LOG_PRIORITY_INFO;
2357 		break;
2358 	case LIBEVDEV_LOG_DEBUG:
2359 		pri = LIBINPUT_LOG_PRIORITY_DEBUG;
2360 		break;
2361 	}
2362 
2363 	snprintf(fmt, sizeof(fmt), "%s%s", prefix, format);
2364 
2365 #pragma GCC diagnostic push
2366 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
2367 	log_msg_va(libinput, pri, fmt, args);
2368 #pragma GCC diagnostic pop
2369 }
2370 
2371 static bool
udev_device_should_be_ignored(struct udev_device *udev_device)2372 udev_device_should_be_ignored(struct udev_device *udev_device)
2373 {
2374 	const char *value;
2375 
2376 	value = udev_device_get_property_value(udev_device,
2377 					       "LIBINPUT_IGNORE_DEVICE");
2378 
2379 	return value && !streq(value, "0");
2380 }
2381 
2382 struct evdev_device *
evdev_device_create(struct libinput_seat *seat, struct udev_device *udev_device)2383 evdev_device_create(struct libinput_seat *seat,
2384 		    struct udev_device *udev_device)
2385 {
2386 	struct libinput *libinput = seat->libinput;
2387 	struct evdev_device *device = NULL;
2388 	int rc;
2389 	int fd = -1;
2390 	int unhandled_device = 0;
2391 	const char *devnode = udev_device_get_devnode(udev_device);
2392 	char *sysname = str_sanitize(udev_device_get_sysname(udev_device));
2393 
2394 	if (!devnode) {
2395 		log_info(libinput, "%s: no device node associated\n", sysname);
2396 		goto err;
2397 	}
2398 
2399 	if (udev_device_should_be_ignored(udev_device)) {
2400 		log_debug(libinput, "%s: device is ignored\n", sysname);
2401 		goto err;
2402 	}
2403 
2404 	/* Use non-blocking mode so that we can loop on read on
2405 	 * evdev_device_data() until all events on the fd are
2406 	 * read.  mtdev_get() also expects this. */
2407 	fd = open_restricted(libinput, devnode,
2408 			     O_RDWR | O_NONBLOCK | O_CLOEXEC);
2409 	if (fd < 0) {
2410 		log_info(libinput,
2411 			 "%s: opening input device '%s' failed (%s).\n",
2412 			 sysname,
2413 			 devnode,
2414 			 strerror(-fd));
2415 		goto err;
2416 	}
2417 
2418 	if (!evdev_device_have_same_syspath(udev_device, fd))
2419 		goto err;
2420 
2421 	device = zalloc(sizeof *device);
2422 	device->sysname = sysname;
2423 	sysname = NULL;
2424 
2425 	libinput_device_init(&device->base, seat);
2426 	libinput_seat_ref(seat);
2427 
2428 	evdev_drain_fd(fd);
2429 
2430 	rc = libevdev_new_from_fd(fd, &device->evdev);
2431 	if (rc != 0)
2432 		goto err;
2433 
2434 	libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
2435 	libevdev_set_device_log_function(device->evdev,
2436 					 libevdev_log_func,
2437 					 LIBEVDEV_LOG_ERROR,
2438 					 libinput);
2439 	device->seat_caps = 0;
2440 	device->is_mt = 0;
2441 	device->mtdev = NULL;
2442 	device->udev_device = udev_device_ref(udev_device);
2443 	device->dispatch = NULL;
2444 	device->fd = fd;
2445 	device->devname = libevdev_get_name(device->evdev);
2446 	/* the log_prefix_name is used as part of a printf format string and
2447 	 * must not contain % directives, see evdev_log_msg */
2448 	device->log_prefix_name = str_sanitize(device->devname);
2449 	device->scroll.threshold = 5.0; /* Default may be overridden */
2450 	device->scroll.direction_lock_threshold = 5.0; /* Default may be overridden */
2451 	device->scroll.direction = 0;
2452 	device->scroll.wheel_click_angle =
2453 		evdev_read_wheel_click_props(device);
2454 	device->model_flags = evdev_read_model_flags(device);
2455 	device->dpi = DEFAULT_MOUSE_DPI;
2456 
2457 	/* at most 5 SYN_DROPPED log-messages per 30s */
2458 	ratelimit_init(&device->syn_drop_limit, s2us(30), 5);
2459 	/* at most 5 "delayed processing" log messages per hour */
2460 	ratelimit_init(&device->delay_warning_limit, s2us(60 * 60), 5);
2461 	/* at most 5 log-messages per 5s */
2462 	ratelimit_init(&device->nonpointer_rel_limit, s2us(5), 5);
2463 
2464 	matrix_init_identity(&device->abs.calibration);
2465 	matrix_init_identity(&device->abs.usermatrix);
2466 	matrix_init_identity(&device->abs.default_calibration);
2467 
2468 	evdev_pre_configure_model_quirks(device);
2469 
2470 	device->dispatch = evdev_configure_device(device);
2471 	if (device->dispatch == NULL || device->seat_caps == 0)
2472 		goto err;
2473 
2474 	device->source =
2475 		libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
2476 	if (!device->source)
2477 		goto err;
2478 
2479 	if (!evdev_set_device_group(device, udev_device))
2480 		goto err;
2481 
2482 	list_insert(seat->devices_list.prev, &device->base.link);
2483 
2484 	evdev_notify_added_device(device);
2485 
2486 	return device;
2487 
2488 err:
2489 	if (fd >= 0) {
2490 		close_restricted(libinput, fd);
2491 		if (device) {
2492 			unhandled_device = device->seat_caps == 0;
2493 			evdev_device_destroy(device);
2494 		}
2495 	}
2496 
2497 	free(sysname);
2498 
2499 	return unhandled_device ? EVDEV_UNHANDLED_DEVICE :  NULL;
2500 }
2501 
2502 const char *
evdev_device_get_output(struct evdev_device *device)2503 evdev_device_get_output(struct evdev_device *device)
2504 {
2505 	return device->output_name;
2506 }
2507 
2508 const char *
evdev_device_get_sysname(struct evdev_device *device)2509 evdev_device_get_sysname(struct evdev_device *device)
2510 {
2511 	return device->sysname;
2512 }
2513 
2514 const char *
evdev_device_get_name(struct evdev_device *device)2515 evdev_device_get_name(struct evdev_device *device)
2516 {
2517 	return device->devname;
2518 }
2519 
2520 unsigned int
evdev_device_get_id_product(struct evdev_device *device)2521 evdev_device_get_id_product(struct evdev_device *device)
2522 {
2523 	return libevdev_get_id_product(device->evdev);
2524 }
2525 
2526 unsigned int
evdev_device_get_id_vendor(struct evdev_device *device)2527 evdev_device_get_id_vendor(struct evdev_device *device)
2528 {
2529 	return libevdev_get_id_vendor(device->evdev);
2530 }
2531 
2532 struct udev_device *
evdev_device_get_udev_device(struct evdev_device *device)2533 evdev_device_get_udev_device(struct evdev_device *device)
2534 {
2535 	return udev_device_ref(device->udev_device);
2536 }
2537 
2538 void
evdev_device_set_default_calibration(struct evdev_device *device, const float calibration[6])2539 evdev_device_set_default_calibration(struct evdev_device *device,
2540 				     const float calibration[6])
2541 {
2542 	matrix_from_farray6(&device->abs.default_calibration, calibration);
2543 	evdev_device_calibrate(device, calibration);
2544 }
2545 
2546 void
evdev_device_calibrate(struct evdev_device *device, const float calibration[6])2547 evdev_device_calibrate(struct evdev_device *device,
2548 		       const float calibration[6])
2549 {
2550 	struct matrix scale,
2551 		      translate,
2552 		      transform;
2553 	double sx, sy;
2554 
2555 	matrix_from_farray6(&transform, calibration);
2556 	device->abs.apply_calibration = !matrix_is_identity(&transform);
2557 
2558 	/* back up the user matrix so we can return it on request */
2559 	matrix_from_farray6(&device->abs.usermatrix, calibration);
2560 
2561 	if (!device->abs.apply_calibration) {
2562 		matrix_init_identity(&device->abs.calibration);
2563 		return;
2564 	}
2565 
2566 	sx = absinfo_range(device->abs.absinfo_x);
2567 	sy = absinfo_range(device->abs.absinfo_y);
2568 
2569 	/* The transformation matrix is in the form:
2570 	 *  [ a b c ]
2571 	 *  [ d e f ]
2572 	 *  [ 0 0 1 ]
2573 	 * Where a, e are the scale components, a, b, d, e are the rotation
2574 	 * component (combined with scale) and c and f are the translation
2575 	 * component. The translation component in the input matrix must be
2576 	 * normalized to multiples of the device width and height,
2577 	 * respectively. e.g. c == 1 shifts one device-width to the right.
2578 	 *
2579 	 * We pre-calculate a single matrix to apply to event coordinates:
2580 	 *     M = Un-Normalize * Calibration * Normalize
2581 	 *
2582 	 * Normalize: scales the device coordinates to [0,1]
2583 	 * Calibration: user-supplied matrix
2584 	 * Un-Normalize: scales back up to device coordinates
2585 	 * Matrix maths requires the normalize/un-normalize in reverse
2586 	 * order.
2587 	 */
2588 
2589 	/* Un-Normalize */
2590 	matrix_init_translate(&translate,
2591 			      device->abs.absinfo_x->minimum,
2592 			      device->abs.absinfo_y->minimum);
2593 	matrix_init_scale(&scale, sx, sy);
2594 	matrix_mult(&scale, &translate, &scale);
2595 
2596 	/* Calibration */
2597 	matrix_mult(&transform, &scale, &transform);
2598 
2599 	/* Normalize */
2600 	matrix_init_translate(&translate,
2601 			      -device->abs.absinfo_x->minimum/sx,
2602 			      -device->abs.absinfo_y->minimum/sy);
2603 	matrix_init_scale(&scale, 1.0/sx, 1.0/sy);
2604 	matrix_mult(&scale, &translate, &scale);
2605 
2606 	/* store final matrix in device */
2607 	matrix_mult(&device->abs.calibration, &transform, &scale);
2608 }
2609 
2610 void
evdev_read_calibration_prop(struct evdev_device *device)2611 evdev_read_calibration_prop(struct evdev_device *device)
2612 {
2613 	const char *prop;
2614 	float calibration[6];
2615 
2616 	prop = udev_device_get_property_value(device->udev_device,
2617 					      "LIBINPUT_CALIBRATION_MATRIX");
2618 
2619 	if (prop == NULL)
2620 		return;
2621 
2622 	if (!device->abs.absinfo_x || !device->abs.absinfo_y)
2623 		return;
2624 
2625 	if (!parse_calibration_property(prop, calibration))
2626 		return;
2627 
2628 	evdev_device_set_default_calibration(device, calibration);
2629 	evdev_log_info(device,
2630 		       "applying calibration: %f %f %f %f %f %f\n",
2631 		       calibration[0],
2632 		       calibration[1],
2633 		       calibration[2],
2634 		       calibration[3],
2635 		       calibration[4],
2636 		       calibration[5]);
2637 }
2638 
2639 int
evdev_read_fuzz_prop(struct evdev_device *device, unsigned int code)2640 evdev_read_fuzz_prop(struct evdev_device *device, unsigned int code)
2641 {
2642 	const char *prop;
2643 	char name[32];
2644 	int rc;
2645 	int fuzz = 0;
2646 	const struct input_absinfo *abs;
2647 
2648 	rc = snprintf(name, sizeof(name), "LIBINPUT_FUZZ_%02x", code);
2649 	if (rc == -1)
2650 		return 0;
2651 
2652 	prop = udev_device_get_property_value(device->udev_device, name);
2653 	if (prop && (safe_atoi(prop, &fuzz) == false || fuzz < 0)) {
2654 		evdev_log_bug_libinput(device,
2655 				       "invalid LIBINPUT_FUZZ property value: %s\n",
2656 				       prop);
2657 		return 0;
2658 	}
2659 
2660 	/* The udev callout should have set the kernel fuzz to zero.
2661 	 * If the kernel fuzz is nonzero, something has gone wrong there, so
2662 	 * let's complain but still use a fuzz of zero for our view of the
2663 	 * device. Otherwise, the kernel will use the nonzero fuzz, we then
2664 	 * use the same fuzz on top of the pre-fuzzed data and that leads to
2665 	 * unresponsive behaviur.
2666 	 */
2667 	abs = libevdev_get_abs_info(device->evdev, code);
2668 	if (!abs || abs->fuzz == 0)
2669 		return fuzz;
2670 
2671 	if (prop) {
2672 		evdev_log_bug_libinput(device,
2673 				       "kernel fuzz of %d even with LIBINPUT_FUZZ_%02x present\n",
2674 				       abs->fuzz,
2675 				       code);
2676 	} else {
2677 		evdev_log_bug_libinput(device,
2678 				       "kernel fuzz of %d but LIBINPUT_FUZZ_%02x is missing\n",
2679 				       abs->fuzz,
2680 				       code);
2681 	}
2682 
2683 	return 0;
2684 }
2685 
2686 bool
evdev_device_has_capability(struct evdev_device *device, enum libinput_device_capability capability)2687 evdev_device_has_capability(struct evdev_device *device,
2688 			    enum libinput_device_capability capability)
2689 {
2690 	switch (capability) {
2691 	case LIBINPUT_DEVICE_CAP_POINTER:
2692 		return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
2693 	case LIBINPUT_DEVICE_CAP_KEYBOARD:
2694 		return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
2695 	case LIBINPUT_DEVICE_CAP_TOUCH:
2696 		return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
2697 	case LIBINPUT_DEVICE_CAP_GESTURE:
2698 		return !!(device->seat_caps & EVDEV_DEVICE_GESTURE);
2699 	case LIBINPUT_DEVICE_CAP_TABLET_TOOL:
2700 		return !!(device->seat_caps & EVDEV_DEVICE_TABLET);
2701 	case LIBINPUT_DEVICE_CAP_TABLET_PAD:
2702 		return !!(device->seat_caps & EVDEV_DEVICE_TABLET_PAD);
2703 	case LIBINPUT_DEVICE_CAP_SWITCH:
2704 		return !!(device->seat_caps & EVDEV_DEVICE_SWITCH);
2705 	default:
2706 		return false;
2707 	}
2708 }
2709 
2710 int
evdev_device_get_size(const struct evdev_device *device, double *width, double *height)2711 evdev_device_get_size(const struct evdev_device *device,
2712 		      double *width,
2713 		      double *height)
2714 {
2715 	const struct input_absinfo *x, *y;
2716 
2717 	x = libevdev_get_abs_info(device->evdev, ABS_X);
2718 	y = libevdev_get_abs_info(device->evdev, ABS_Y);
2719 
2720 	if (!x || !y || device->abs.is_fake_resolution ||
2721 	    !x->resolution || !y->resolution)
2722 		return -1;
2723 
2724 	*width = evdev_convert_to_mm(x, x->maximum);
2725 	*height = evdev_convert_to_mm(y, y->maximum);
2726 
2727 	return 0;
2728 }
2729 
2730 int
evdev_device_has_button(struct evdev_device *device, uint32_t code)2731 evdev_device_has_button(struct evdev_device *device, uint32_t code)
2732 {
2733 	if (!(device->seat_caps & EVDEV_DEVICE_POINTER))
2734 		return -1;
2735 
2736 	return libevdev_has_event_code(device->evdev, EV_KEY, code);
2737 }
2738 
2739 int
evdev_device_has_key(struct evdev_device *device, uint32_t code)2740 evdev_device_has_key(struct evdev_device *device, uint32_t code)
2741 {
2742 	if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
2743 		return -1;
2744 
2745 	return libevdev_has_event_code(device->evdev, EV_KEY, code);
2746 }
2747 
2748 int
evdev_device_get_touch_count(struct evdev_device *device)2749 evdev_device_get_touch_count(struct evdev_device *device)
2750 {
2751 	int ntouches;
2752 
2753 	if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
2754 		return -1;
2755 
2756 	ntouches = libevdev_get_num_slots(device->evdev);
2757 	if (ntouches == -1) {
2758 		/* mtdev devices have multitouch but we don't know
2759 		 * how many. Otherwise, any touch device with num_slots of
2760 		 * -1 is a single-touch device */
2761 		if (device->mtdev)
2762 			ntouches = 0;
2763 		else
2764 			ntouches = 1;
2765 	}
2766 
2767 	return ntouches;
2768 }
2769 
2770 int
evdev_device_has_switch(struct evdev_device *device, enum libinput_switch sw)2771 evdev_device_has_switch(struct evdev_device *device,
2772 			enum libinput_switch sw)
2773 {
2774 	unsigned int code;
2775 
2776 	if (!(device->seat_caps & EVDEV_DEVICE_SWITCH))
2777 		return -1;
2778 
2779 	switch (sw) {
2780 	case LIBINPUT_SWITCH_LID:
2781 		code = SW_LID;
2782 		break;
2783 	case LIBINPUT_SWITCH_TABLET_MODE:
2784 		code = SW_TABLET_MODE;
2785 		break;
2786 	default:
2787 		return -1;
2788 	}
2789 
2790 	return libevdev_has_event_code(device->evdev, EV_SW, code);
2791 }
2792 
2793 static inline bool
evdev_is_scrolling(const struct evdev_device *device, enum libinput_pointer_axis axis)2794 evdev_is_scrolling(const struct evdev_device *device,
2795 		   enum libinput_pointer_axis axis)
2796 {
2797 	assert(axis == LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL ||
2798 	       axis == LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2799 
2800 	return (device->scroll.direction & bit(axis)) != 0;
2801 }
2802 
2803 static inline void
evdev_start_scrolling(struct evdev_device *device, enum libinput_pointer_axis axis)2804 evdev_start_scrolling(struct evdev_device *device,
2805 		      enum libinput_pointer_axis axis)
2806 {
2807 	assert(axis == LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL ||
2808 	       axis == LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2809 
2810 	device->scroll.direction |= bit(axis);
2811 }
2812 
2813 void
evdev_post_scroll(struct evdev_device *device, uint64_t time, enum libinput_pointer_axis_source source, const struct normalized_coords *delta)2814 evdev_post_scroll(struct evdev_device *device,
2815 		  uint64_t time,
2816 		  enum libinput_pointer_axis_source source,
2817 		  const struct normalized_coords *delta)
2818 {
2819 	const struct normalized_coords *trigger;
2820 	struct normalized_coords event;
2821 
2822 	if (!evdev_is_scrolling(device,
2823 				LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
2824 		device->scroll.buildup.y += delta->y;
2825 	if (!evdev_is_scrolling(device,
2826 				LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
2827 		device->scroll.buildup.x += delta->x;
2828 
2829 	trigger = &device->scroll.buildup;
2830 
2831 	/* If we're not scrolling yet, use a distance trigger: moving
2832 	   past a certain distance starts scrolling */
2833 	if (!evdev_is_scrolling(device,
2834 				LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL) &&
2835 	    !evdev_is_scrolling(device,
2836 				LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
2837 		if (fabs(trigger->y) >= device->scroll.threshold)
2838 			evdev_start_scrolling(device,
2839 					      LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2840 		if (fabs(trigger->x) >= device->scroll.threshold)
2841 			evdev_start_scrolling(device,
2842 					      LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
2843 	/* We're already scrolling in one direction. Require some
2844 	   trigger speed to start scrolling in the other direction */
2845 	} else if (!evdev_is_scrolling(device,
2846 			       LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
2847 		if (fabs(delta->y) >= device->scroll.direction_lock_threshold)
2848 			evdev_start_scrolling(device,
2849 				      LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2850 	} else if (!evdev_is_scrolling(device,
2851 				LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL)) {
2852 		if (fabs(delta->x) >= device->scroll.direction_lock_threshold)
2853 			evdev_start_scrolling(device,
2854 				      LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
2855 	}
2856 
2857 	event = *delta;
2858 
2859 	/* We use the trigger to enable, but the delta from this event for
2860 	 * the actual scroll movement. Otherwise we get a jump once
2861 	 * scrolling engages */
2862 	if (!evdev_is_scrolling(device,
2863 			       LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
2864 		event.y = 0.0;
2865 
2866 	if (!evdev_is_scrolling(device,
2867 			       LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
2868 		event.x = 0.0;
2869 
2870 	if (!normalized_is_zero(event)) {
2871 		uint32_t axes = device->scroll.direction;
2872 
2873 		if (event.y == 0.0)
2874 			axes &= ~bit(LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2875 		if (event.x == 0.0)
2876 			axes &= ~bit(LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
2877 
2878 		switch (source) {
2879 		case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
2880 			evdev_notify_axis_finger(device, time, axes, &event);
2881 			break;
2882 		case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
2883 			evdev_notify_axis_continous(device, time, axes, &event);
2884 			break;
2885 		default:
2886 			evdev_log_bug_libinput(device,
2887 					       "Posting invalid scroll source %d\n",
2888 					       source);
2889 			break;
2890 		}
2891 	}
2892 }
2893 
2894 void
evdev_stop_scroll(struct evdev_device *device, uint64_t time, enum libinput_pointer_axis_source source)2895 evdev_stop_scroll(struct evdev_device *device,
2896 		  uint64_t time,
2897 		  enum libinput_pointer_axis_source source)
2898 {
2899 	const struct normalized_coords zero = { 0.0, 0.0 };
2900 
2901 	/* terminate scrolling with a zero scroll event */
2902 	if (device->scroll.direction != 0) {
2903 		switch (source) {
2904 		case LIBINPUT_POINTER_AXIS_SOURCE_FINGER:
2905 			pointer_notify_axis_finger(&device->base,
2906 						   time,
2907 						   device->scroll.direction,
2908 						   &zero);
2909 			break;
2910 		case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS:
2911 			pointer_notify_axis_continuous(&device->base,
2912 						       time,
2913 						       device->scroll.direction,
2914 						       &zero);
2915 			break;
2916 		default:
2917 			evdev_log_bug_libinput(device,
2918 					       "Stopping invalid scroll source %d\n",
2919 					       source);
2920 			break;
2921 		}
2922 	}
2923 
2924 	device->scroll.buildup.x = 0;
2925 	device->scroll.buildup.y = 0;
2926 	device->scroll.direction = 0;
2927 }
2928 
2929 void
evdev_notify_suspended_device(struct evdev_device *device)2930 evdev_notify_suspended_device(struct evdev_device *device)
2931 {
2932 	struct libinput_device *it;
2933 
2934 	if (device->is_suspended)
2935 		return;
2936 
2937 	list_for_each(it, &device->base.seat->devices_list, link) {
2938 		struct evdev_device *d = evdev_device(it);
2939 		if (it == &device->base)
2940 			continue;
2941 
2942 		if (d->dispatch->interface->device_suspended)
2943 			d->dispatch->interface->device_suspended(d, device);
2944 	}
2945 
2946 	device->is_suspended = true;
2947 }
2948 
2949 void
evdev_notify_resumed_device(struct evdev_device *device)2950 evdev_notify_resumed_device(struct evdev_device *device)
2951 {
2952 	struct libinput_device *it;
2953 
2954 	if (!device->is_suspended)
2955 		return;
2956 
2957 	list_for_each(it, &device->base.seat->devices_list, link) {
2958 		struct evdev_device *d = evdev_device(it);
2959 		if (it == &device->base)
2960 			continue;
2961 
2962 		if (d->dispatch->interface->device_resumed)
2963 			d->dispatch->interface->device_resumed(d, device);
2964 	}
2965 
2966 	device->is_suspended = false;
2967 }
2968 
2969 void
evdev_device_suspend(struct evdev_device *device)2970 evdev_device_suspend(struct evdev_device *device)
2971 {
2972 	struct libinput *libinput = evdev_libinput_context(device);
2973 
2974 	evdev_notify_suspended_device(device);
2975 
2976 	if (device->dispatch->interface->suspend)
2977 		device->dispatch->interface->suspend(device->dispatch,
2978 						     device);
2979 
2980 	if (device->source) {
2981 		libinput_remove_source(libinput, device->source);
2982 		device->source = NULL;
2983 	}
2984 
2985 	if (device->mtdev) {
2986 		mtdev_close_delete(device->mtdev);
2987 		device->mtdev = NULL;
2988 	}
2989 
2990 	if (device->fd != -1) {
2991 		close_restricted(libinput, device->fd);
2992 		device->fd = -1;
2993 	}
2994 }
2995 
2996 int
evdev_device_resume(struct evdev_device *device)2997 evdev_device_resume(struct evdev_device *device)
2998 {
2999 	struct libinput *libinput = evdev_libinput_context(device);
3000 	int fd;
3001 	const char *devnode;
3002 	struct input_event ev;
3003 	enum libevdev_read_status status;
3004 
3005 	if (device->fd != -1)
3006 		return 0;
3007 
3008 	if (device->was_removed)
3009 		return -ENODEV;
3010 
3011 	devnode = udev_device_get_devnode(device->udev_device);
3012 	if (!devnode)
3013 		return -ENODEV;
3014 
3015 	fd = open_restricted(libinput, devnode,
3016 			     O_RDWR | O_NONBLOCK | O_CLOEXEC);
3017 
3018 	if (fd < 0)
3019 		return -errno;
3020 
3021 	if (!evdev_device_have_same_syspath(device->udev_device, fd)) {
3022 		close_restricted(libinput, fd);
3023 		return -ENODEV;
3024 	}
3025 
3026 	evdev_drain_fd(fd);
3027 
3028 	device->fd = fd;
3029 
3030 	if (evdev_need_mtdev(device)) {
3031 		device->mtdev = mtdev_new_open(device->fd);
3032 		if (!device->mtdev)
3033 			return -ENODEV;
3034 	}
3035 
3036 	libevdev_change_fd(device->evdev, fd);
3037 	libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
3038 
3039 	/* re-sync libevdev's view of the device, but discard the actual
3040 	   events. Our device is in a neutral state already */
3041 	libevdev_next_event(device->evdev,
3042 			    LIBEVDEV_READ_FLAG_FORCE_SYNC,
3043 			    &ev);
3044 	do {
3045 		status = libevdev_next_event(device->evdev,
3046 					     LIBEVDEV_READ_FLAG_SYNC,
3047 					     &ev);
3048 	} while (status == LIBEVDEV_READ_STATUS_SYNC);
3049 
3050 	device->source =
3051 		libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
3052 	if (!device->source) {
3053 		mtdev_close_delete(device->mtdev);
3054 		return -ENOMEM;
3055 	}
3056 
3057 	evdev_notify_resumed_device(device);
3058 
3059 	return 0;
3060 }
3061 
3062 void
evdev_device_remove(struct evdev_device *device)3063 evdev_device_remove(struct evdev_device *device)
3064 {
3065 	struct libinput_device *dev;
3066 
3067 	evdev_log_info(device, "device removed\n");
3068 
3069 	libinput_timer_cancel(&device->scroll.timer);
3070 	libinput_timer_cancel(&device->middlebutton.timer);
3071 
3072 	list_for_each(dev, &device->base.seat->devices_list, link) {
3073 		struct evdev_device *d = evdev_device(dev);
3074 		if (dev == &device->base)
3075 			continue;
3076 
3077 		if (d->dispatch->interface->device_removed)
3078 			d->dispatch->interface->device_removed(d, device);
3079 	}
3080 
3081 	evdev_device_suspend(device);
3082 
3083 	if (device->dispatch->interface->remove)
3084 		device->dispatch->interface->remove(device->dispatch);
3085 
3086 	/* A device may be removed while suspended, mark it to
3087 	 * skip re-opening a different device with the same node */
3088 	device->was_removed = true;
3089 
3090 	list_remove(&device->base.link);
3091 
3092 	notify_removed_device(&device->base);
3093 	libinput_device_unref(&device->base);
3094 }
3095 
3096 void
evdev_device_destroy(struct evdev_device *device)3097 evdev_device_destroy(struct evdev_device *device)
3098 {
3099 	struct evdev_dispatch *dispatch;
3100 
3101 	dispatch = device->dispatch;
3102 	if (dispatch)
3103 		dispatch->interface->destroy(dispatch);
3104 
3105 	if (device->base.group)
3106 		libinput_device_group_unref(device->base.group);
3107 
3108 	free(device->log_prefix_name);
3109 	free(device->sysname);
3110 	free(device->output_name);
3111 	filter_destroy(device->pointer.filter);
3112 	libinput_timer_destroy(&device->scroll.timer);
3113 	libinput_timer_destroy(&device->middlebutton.timer);
3114 	libinput_seat_unref(device->base.seat);
3115 	libevdev_free(device->evdev);
3116 	udev_device_unref(device->udev_device);
3117 	free(device);
3118 }
3119 
3120 bool
evdev_tablet_has_left_handed(struct evdev_device *device)3121 evdev_tablet_has_left_handed(struct evdev_device *device)
3122 {
3123 	bool has_left_handed = true;
3124 #if HAVE_LIBWACOM
3125 	struct libinput *li = evdev_libinput_context(device);
3126 	WacomDeviceDatabase *db = NULL;
3127 	WacomDevice *d = NULL;
3128 	WacomError *error;
3129 	const char *devnode;
3130 
3131 	db = libinput_libwacom_ref(li);
3132 	if (!db)
3133 		goto out;
3134 
3135 	error = libwacom_error_new();
3136 	devnode = udev_device_get_devnode(device->udev_device);
3137 
3138 	d = libwacom_new_from_path(db,
3139 				   devnode,
3140 				   WFALLBACK_NONE,
3141 				   error);
3142 
3143 	if (d) {
3144 		has_left_handed = !!libwacom_is_reversible(d);
3145 	} else if (libwacom_error_get_code(error) == WERROR_UNKNOWN_MODEL) {
3146 		evdev_log_info(device,
3147 			       "tablet '%s' unknown to libwacom\n",
3148 			       device->devname);
3149 	} else {
3150 		evdev_log_error(device,
3151 				"libwacom error: %s\n",
3152 				libwacom_error_get_message(error));
3153 	}
3154 
3155 	if (error)
3156 		libwacom_error_free(&error);
3157 	if (d)
3158 		libwacom_destroy(d);
3159 	if (db)
3160 		libinput_libwacom_unref(li);
3161 
3162 out:
3163 #endif
3164 	return has_left_handed;
3165 }
3166