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