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