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