1 /*
2 * Copyright © 2011, 2012 Intel Corporation
3 * Copyright © 2013 Jonas Ådahl
4 * Copyright © 2013-2015 Red Hat, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
25
26 #ifndef EVDEV_H
27 #define EVDEV_H
28
29 #include "config.h"
30
31 #include <stdbool.h>
32 #include <stdarg.h>
33 #include "linux/input.h"
34 #include <libevdev/libevdev.h>
35
36 #include "libinput-private.h"
37 #include "timer.h"
38 #include "filter.h"
39 #include "quirks.h"
40
41 /* The fake resolution value for abs devices without resolution */
42 #define EVDEV_FAKE_RESOLUTION 1
43
44 enum evdev_event_type {
45 EVDEV_NONE,
46 EVDEV_ABSOLUTE_TOUCH_DOWN = bit(0),
47 EVDEV_ABSOLUTE_MOTION = bit(1),
48 EVDEV_ABSOLUTE_TOUCH_UP = bit(2),
49 EVDEV_ABSOLUTE_MT = bit(3),
50 EVDEV_WHEEL = bit(4),
51 EVDEV_KEY = bit(5),
52 EVDEV_RELATIVE_MOTION = bit(6),
53 EVDEV_BUTTON = bit(7),
54 };
55
56 enum evdev_device_seat_capability {
57 EVDEV_DEVICE_POINTER = bit(0),
58 EVDEV_DEVICE_KEYBOARD = bit(1),
59 EVDEV_DEVICE_TOUCH = bit(2),
60 EVDEV_DEVICE_TABLET = bit(3),
61 EVDEV_DEVICE_TABLET_PAD = bit(4),
62 EVDEV_DEVICE_GESTURE = bit(5),
63 EVDEV_DEVICE_SWITCH = bit(6),
64 };
65
66 enum evdev_device_tags {
67 EVDEV_TAG_EXTERNAL_MOUSE = bit(0),
68 EVDEV_TAG_INTERNAL_TOUCHPAD = bit(1),
69 EVDEV_TAG_EXTERNAL_TOUCHPAD = bit(2),
70 EVDEV_TAG_TRACKPOINT = bit(3),
71 EVDEV_TAG_KEYBOARD = bit(4),
72 EVDEV_TAG_LID_SWITCH = bit(5),
73 EVDEV_TAG_INTERNAL_KEYBOARD = bit(6),
74 EVDEV_TAG_EXTERNAL_KEYBOARD = bit(7),
75 EVDEV_TAG_TABLET_MODE_SWITCH = bit(8),
76 EVDEV_TAG_TABLET_TOUCHPAD = bit(9),
77 };
78
79 enum evdev_middlebutton_state {
80 MIDDLEBUTTON_IDLE,
81 MIDDLEBUTTON_LEFT_DOWN,
82 MIDDLEBUTTON_RIGHT_DOWN,
83 MIDDLEBUTTON_MIDDLE,
84 MIDDLEBUTTON_LEFT_UP_PENDING,
85 MIDDLEBUTTON_RIGHT_UP_PENDING,
86 MIDDLEBUTTON_IGNORE_LR,
87 MIDDLEBUTTON_IGNORE_L,
88 MIDDLEBUTTON_IGNORE_R,
89 MIDDLEBUTTON_PASSTHROUGH,
90 };
91
92 enum evdev_middlebutton_event {
93 MIDDLEBUTTON_EVENT_L_DOWN,
94 MIDDLEBUTTON_EVENT_R_DOWN,
95 MIDDLEBUTTON_EVENT_OTHER,
96 MIDDLEBUTTON_EVENT_L_UP,
97 MIDDLEBUTTON_EVENT_R_UP,
98 MIDDLEBUTTON_EVENT_TIMEOUT,
99 MIDDLEBUTTON_EVENT_ALL_UP,
100 };
101
102 /**
103 * model flags are used as shortcut for quirks that need to be checked
104 * multiple times in timing-sensitive paths. For quirks that need to be
105 * checked only once, use the quirk directly.
106 */
107 enum evdev_device_model {
108 EVDEV_MODEL_DEFAULT = 0,
109 EVDEV_MODEL_WACOM_TOUCHPAD = bit(1),
110 EVDEV_MODEL_SYNAPTICS_SERIAL_TOUCHPAD = bit(2),
111 EVDEV_MODEL_ALPS_SERIAL_TOUCHPAD = bit(3),
112 EVDEV_MODEL_LENOVO_T450_TOUCHPAD = bit(4),
113 EVDEV_MODEL_APPLE_TOUCHPAD_ONEBUTTON = bit(5),
114 EVDEV_MODEL_LENOVO_SCROLLPOINT = bit(6),
115
116 /* udev tags, not true quirks */
117 EVDEV_MODEL_TEST_DEVICE = bit(20),
118 EVDEV_MODEL_TRACKBALL = bit(21),
119 EVDEV_MODEL_LENOVO_X220_TOUCHPAD_FW81 = bit(22),
120 };
121
122 enum evdev_button_scroll_state {
123 BUTTONSCROLL_IDLE,
124 BUTTONSCROLL_BUTTON_DOWN, /* button is down */
125 BUTTONSCROLL_READY, /* ready for scroll events */
126 BUTTONSCROLL_SCROLLING, /* have sent scroll events */
127 };
128
129 enum evdev_button_scroll_lock_state {
130 BUTTONSCROLL_LOCK_DISABLED,
131 BUTTONSCROLL_LOCK_IDLE,
132 BUTTONSCROLL_LOCK_FIRSTDOWN,
133 BUTTONSCROLL_LOCK_FIRSTUP,
134 BUTTONSCROLL_LOCK_SECONDDOWN,
135 };
136
137 enum evdev_debounce_state {
138 /**
139 * Initial state, no debounce but monitoring events
140 */
141 DEBOUNCE_INIT,
142 /**
143 * Bounce detected, future events need debouncing
144 */
145 DEBOUNCE_NEEDED,
146 /**
147 * Debounce is enabled, but no event is currently being filtered
148 */
149 DEBOUNCE_ON,
150 /**
151 * Debounce is enabled and we are currently filtering an event
152 */
153 DEBOUNCE_ACTIVE,
154 };
155
156 enum evdev_arbitration_state {
157 ARBITRATION_NOT_ACTIVE,
158 ARBITRATION_IGNORE_ALL,
159 ARBITRATION_IGNORE_RECT,
160 };
161
162 struct evdev_device {
163 struct libinput_device base;
164
165 struct libinput_source *source;
166
167 struct evdev_dispatch *dispatch;
168 struct libevdev *evdev;
169 struct udev_device *udev_device;
170 char *output_name;
171 const char *devname;
172 char *log_prefix_name;
173 char *sysname;
174 bool was_removed;
175 int fd;
176 enum evdev_device_seat_capability seat_caps;
177 enum evdev_device_tags tags;
178 bool is_mt;
179 bool is_suspended;
180 int dpi; /* HW resolution */
181 double trackpoint_multiplier; /* trackpoint constant multiplier */
182 bool use_velocity_averaging; /* whether averaging should be applied on velocity calculation */
183 struct ratelimit syn_drop_limit; /* ratelimit for SYN_DROPPED logging */
184 struct ratelimit delay_warning_limit; /* ratelimit for delayd processing logging */
185 struct ratelimit nonpointer_rel_limit; /* ratelimit for REL_* events from non-pointer devices */
186 uint32_t model_flags;
187 struct mtdev *mtdev;
188
189 struct {
190 const struct input_absinfo *absinfo_x, *absinfo_y;
191 bool is_fake_resolution;
192
193 int apply_calibration;
194 struct matrix calibration;
195 struct matrix default_calibration; /* from LIBINPUT_CALIBRATION_MATRIX */
196 struct matrix usermatrix; /* as supplied by the caller */
197
198 struct device_coords dimensions;
199
200 struct {
201 struct device_coords min, max;
202 struct ratelimit range_warn_limit;
203 } warning_range;
204 } abs;
205
206 struct {
207 struct libinput_timer timer;
208 struct libinput_device_config_scroll_method config;
209 /* Currently enabled method, button */
210 enum libinput_config_scroll_method method;
211 uint32_t button;
212 uint64_t button_down_time;
213
214 /* set during device init, used at runtime to delay changes
215 * until all buttons are up */
216 enum libinput_config_scroll_method want_method;
217 uint32_t want_button;
218 /* Checks if buttons are down and commits the setting */
219 void (*change_scroll_method)(struct evdev_device *device);
220 enum evdev_button_scroll_state button_scroll_state;
221 double threshold;
222 double direction_lock_threshold;
223 uint32_t direction;
224 struct normalized_coords buildup;
225
226 struct libinput_device_config_natural_scroll config_natural;
227 /* set during device init if we want natural scrolling,
228 * used at runtime to enable/disable the feature */
229 bool natural_scrolling_enabled;
230
231 /* set during device init to invert direction of
232 * horizontal scrolling */
233 bool invert_horizontal_scrolling;
234
235 /* angle per REL_WHEEL click in degrees */
236 struct wheel_angle wheel_click_angle;
237
238 enum evdev_button_scroll_lock_state lock_state;
239 bool want_lock_enabled;
240 bool lock_enabled;
241 } scroll;
242
243 struct {
244 struct libinput_device_config_accel config;
245 struct motion_filter *filter;
246 } pointer;
247
248 /* Key counter used for multiplexing button events internally in
249 * libinput. */
250 uint8_t key_count[KEY_CNT];
251
252 struct {
253 struct libinput_device_config_left_handed config;
254 /* left-handed currently enabled */
255 bool enabled;
256 /* set during device init if we want left_handed config,
257 * used at runtime to delay the effect until buttons are up */
258 bool want_enabled;
259 /* Checks if buttons are down and commits the setting */
260 void (*change_to_enabled)(struct evdev_device *device);
261 } left_handed;
262
263 struct {
264 struct libinput_device_config_middle_emulation config;
265 /* middle-button emulation enabled */
266 bool enabled;
267 bool enabled_default;
268 bool want_enabled;
269 enum evdev_middlebutton_state state;
270 struct libinput_timer timer;
271 uint32_t button_mask;
272 uint64_t first_event_time;
273 } middlebutton;
274 };
275
276 static inline struct evdev_device *
evdev_device(struct libinput_device *device)277 evdev_device(struct libinput_device *device)
278 {
279 return container_of(device, struct evdev_device, base);
280 }
281
282 #define EVDEV_UNHANDLED_DEVICE ((struct evdev_device *) 1)
283
284 struct evdev_dispatch;
285
286 struct evdev_dispatch_interface {
287 /* Process an evdev input event. */
288 void (*process)(struct evdev_dispatch *dispatch,
289 struct evdev_device *device,
290 struct input_event *event,
291 uint64_t time);
292
293 /* Device is being suspended */
294 void (*suspend)(struct evdev_dispatch *dispatch,
295 struct evdev_device *device);
296
297 /* Device is being removed (may be NULL) */
298 void (*remove)(struct evdev_dispatch *dispatch);
299
300 /* Destroy an event dispatch handler and free all its resources. */
301 void (*destroy)(struct evdev_dispatch *dispatch);
302
303 /* A new device was added */
304 void (*device_added)(struct evdev_device *device,
305 struct evdev_device *added_device);
306
307 /* A device was removed */
308 void (*device_removed)(struct evdev_device *device,
309 struct evdev_device *removed_device);
310
311 /* A device was suspended */
312 void (*device_suspended)(struct evdev_device *device,
313 struct evdev_device *suspended_device);
314
315 /* A device was resumed */
316 void (*device_resumed)(struct evdev_device *device,
317 struct evdev_device *resumed_device);
318
319 /* Called immediately after the LIBINPUT_EVENT_DEVICE_ADDED event
320 * was sent */
321 void (*post_added)(struct evdev_device *device,
322 struct evdev_dispatch *dispatch);
323
324 /* For touch arbitration, called on the device that should
325 * enable/disable touch capabilities.
326 */
327 void (*touch_arbitration_toggle)(struct evdev_dispatch *dispatch,
328 struct evdev_device *device,
329 enum evdev_arbitration_state which,
330 const struct phys_rect *rect, /* may be NULL */
331 uint64_t now);
332
333 /* Called when touch arbitration is on, updates the area where touch
334 * arbitration should apply.
335 */
336 void (*touch_arbitration_update_rect)(struct evdev_dispatch *dispatch,
337 struct evdev_device *device,
338 const struct phys_rect *rect,
339 uint64_t now);
340
341 /* Return the state of the given switch */
342 enum libinput_switch_state
343 (*get_switch_state)(struct evdev_dispatch *dispatch,
344 enum libinput_switch which);
345
346 void (*left_handed_toggle)(struct evdev_dispatch *dispatch,
347 struct evdev_device *device,
348 bool left_handed_enabled);
349 };
350
351 enum evdev_dispatch_type {
352 DISPATCH_FALLBACK,
353 DISPATCH_TOUCHPAD,
354 DISPATCH_TABLET,
355 DISPATCH_TABLET_PAD,
356 DISPATCH_TOTEM,
357 };
358
359 struct evdev_dispatch {
360 enum evdev_dispatch_type dispatch_type;
361 struct evdev_dispatch_interface *interface;
362
363 struct {
364 struct libinput_device_config_send_events config;
365 enum libinput_config_send_events_mode current_mode;
366 } sendevents;
367 };
368
369 static inline void
evdev_verify_dispatch_type(struct evdev_dispatch *dispatch, enum evdev_dispatch_type type)370 evdev_verify_dispatch_type(struct evdev_dispatch *dispatch,
371 enum evdev_dispatch_type type)
372 {
373 if (dispatch->dispatch_type != type)
374 abort();
375 }
376
377 struct evdev_device *
378 evdev_device_create(struct libinput_seat *seat,
379 struct udev_device *device);
380
381 static inline struct libinput *
evdev_libinput_context(const struct evdev_device *device)382 evdev_libinput_context(const struct evdev_device *device)
383 {
384 return device->base.seat->libinput;
385 }
386
387 static inline bool
evdev_device_has_model_quirk(struct evdev_device *device, enum quirk model_quirk)388 evdev_device_has_model_quirk(struct evdev_device *device,
389 enum quirk model_quirk)
390 {
391 struct quirks_context *quirks;
392 struct quirks *q;
393 bool result = false;
394
395 assert(quirk_get_name(model_quirk) != NULL);
396
397 quirks = evdev_libinput_context(device)->quirks;
398 q = quirks_fetch_for_device(quirks, device->udev_device);
399 quirks_get_bool(q, model_quirk, &result);
400 quirks_unref(q);
401
402 return result;
403 }
404
405 void
406 evdev_transform_absolute(struct evdev_device *device,
407 struct device_coords *point);
408
409 void
410 evdev_transform_relative(struct evdev_device *device,
411 struct device_coords *point);
412
413 void
414 evdev_init_calibration(struct evdev_device *device,
415 struct libinput_device_config_calibration *calibration);
416
417 void
418 evdev_read_calibration_prop(struct evdev_device *device);
419
420 int
421 evdev_read_fuzz_prop(struct evdev_device *device, unsigned int code);
422
423 enum switch_reliability
424 evdev_read_switch_reliability_prop(struct evdev_device *device);
425
426 void
427 evdev_init_sendevents(struct evdev_device *device,
428 struct evdev_dispatch *dispatch);
429
430 void
431 evdev_device_init_pointer_acceleration(struct evdev_device *device,
432 struct motion_filter *filter);
433
434 struct evdev_dispatch *
435 evdev_touchpad_create(struct evdev_device *device);
436
437 struct evdev_dispatch *
438 evdev_mt_touchpad_create(struct evdev_device *device);
439
440 struct evdev_dispatch *
441 evdev_tablet_create(struct evdev_device *device);
442
443 struct evdev_dispatch *
444 evdev_tablet_pad_create(struct evdev_device *device);
445
446 struct evdev_dispatch *
447 evdev_lid_switch_dispatch_create(struct evdev_device *device);
448
449 struct evdev_dispatch *
450 fallback_dispatch_create(struct libinput_device *libinput_device);
451
452 struct evdev_dispatch *
453 evdev_totem_create(struct evdev_device *device);
454
455 bool
456 evdev_is_fake_mt_device(struct evdev_device *device);
457
458 int
459 evdev_need_mtdev(struct evdev_device *device);
460
461 void
462 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds);
463
464 int
465 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size);
466
467 const char *
468 evdev_device_get_output(struct evdev_device *device);
469
470 const char *
471 evdev_device_get_sysname(struct evdev_device *device);
472
473 const char *
474 evdev_device_get_name(struct evdev_device *device);
475
476 unsigned int
477 evdev_device_get_id_product(struct evdev_device *device);
478
479 unsigned int
480 evdev_device_get_id_vendor(struct evdev_device *device);
481
482 struct udev_device *
483 evdev_device_get_udev_device(struct evdev_device *device);
484
485 void
486 evdev_device_set_default_calibration(struct evdev_device *device,
487 const float calibration[6]);
488 void
489 evdev_device_calibrate(struct evdev_device *device,
490 const float calibration[6]);
491
492 bool
493 evdev_device_has_capability(struct evdev_device *device,
494 enum libinput_device_capability capability);
495
496 int
497 evdev_device_get_size(const struct evdev_device *device,
498 double *w,
499 double *h);
500
501 int
502 evdev_device_has_button(struct evdev_device *device, uint32_t code);
503
504 int
505 evdev_device_has_key(struct evdev_device *device, uint32_t code);
506
507 int
508 evdev_device_get_touch_count(struct evdev_device *device);
509
510 int
511 evdev_device_has_switch(struct evdev_device *device,
512 enum libinput_switch sw);
513
514 int
515 evdev_device_tablet_pad_has_key(struct evdev_device *device,
516 uint32_t code);
517
518 int
519 evdev_device_tablet_pad_get_num_buttons(struct evdev_device *device);
520
521 int
522 evdev_device_tablet_pad_get_num_rings(struct evdev_device *device);
523
524 int
525 evdev_device_tablet_pad_get_num_strips(struct evdev_device *device);
526
527 int
528 evdev_device_tablet_pad_get_num_mode_groups(struct evdev_device *device);
529
530 struct libinput_tablet_pad_mode_group *
531 evdev_device_tablet_pad_get_mode_group(struct evdev_device *device,
532 unsigned int index);
533
534 enum libinput_switch_state
535 evdev_device_switch_get_state(struct evdev_device *device,
536 enum libinput_switch sw);
537
538 double
539 evdev_device_transform_x(struct evdev_device *device,
540 double x,
541 uint32_t width);
542
543 double
544 evdev_device_transform_y(struct evdev_device *device,
545 double y,
546 uint32_t height);
547 void
548 evdev_device_suspend(struct evdev_device *device);
549
550 int
551 evdev_device_resume(struct evdev_device *device);
552
553 void
554 evdev_notify_suspended_device(struct evdev_device *device);
555
556 void
557 evdev_notify_resumed_device(struct evdev_device *device);
558
559 void
560 evdev_pointer_notify_button(struct evdev_device *device,
561 uint64_t time,
562 unsigned int button,
563 enum libinput_button_state state);
564 void
565 evdev_pointer_notify_physical_button(struct evdev_device *device,
566 uint64_t time,
567 int button,
568 enum libinput_button_state state);
569
570 void
571 evdev_init_natural_scroll(struct evdev_device *device);
572
573 void
574 evdev_init_button_scroll(struct evdev_device *device,
575 void (*change_scroll_method)(struct evdev_device *));
576
577 void
578 evdev_set_button_scroll_lock_enabled(struct evdev_device *device,
579 bool enabled);
580
581 int
582 evdev_update_key_down_count(struct evdev_device *device,
583 int code,
584 int pressed);
585
586 void
587 evdev_notify_axis_legacy_wheel(struct evdev_device *device,
588 uint64_t time,
589 uint32_t axes,
590 const struct normalized_coords *delta_in,
591 const struct discrete_coords *discrete_in);
592 void
593 evdev_notify_axis_wheel(struct evdev_device *device,
594 uint64_t time,
595 uint32_t axes,
596 const struct normalized_coords *delta_in,
597 const struct wheel_v120 *v120_in);
598 void
599 evdev_notify_axis_finger(struct evdev_device *device,
600 uint64_t time,
601 uint32_t axes,
602 const struct normalized_coords *delta_in);
603 void
604 evdev_notify_axis_continous(struct evdev_device *device,
605 uint64_t time,
606 uint32_t axes,
607 const struct normalized_coords *delta_in);
608
609 void
610 evdev_post_scroll(struct evdev_device *device,
611 uint64_t time,
612 enum libinput_pointer_axis_source source,
613 const struct normalized_coords *delta);
614
615 void
616 evdev_stop_scroll(struct evdev_device *device,
617 uint64_t time,
618 enum libinput_pointer_axis_source source);
619
620 void
621 evdev_device_remove(struct evdev_device *device);
622
623 void
624 evdev_device_destroy(struct evdev_device *device);
625
626 bool
627 evdev_middlebutton_filter_button(struct evdev_device *device,
628 uint64_t time,
629 int button,
630 enum libinput_button_state state);
631
632 void
633 evdev_init_middlebutton(struct evdev_device *device,
634 bool enabled,
635 bool want_config);
636
637 enum libinput_config_middle_emulation_state
638 evdev_middlebutton_get(struct libinput_device *device);
639
640 int
641 evdev_middlebutton_is_available(struct libinput_device *device);
642
643 enum libinput_config_middle_emulation_state
644 evdev_middlebutton_get_default(struct libinput_device *device);
645
646 static inline double
evdev_convert_to_mm(const struct input_absinfo *absinfo, double v)647 evdev_convert_to_mm(const struct input_absinfo *absinfo, double v)
648 {
649 double value = v - absinfo->minimum;
650 return value/absinfo->resolution;
651 }
652
653 static inline struct phys_coords
evdev_convert_xy_to_mm(const struct evdev_device *device, int x, int y)654 evdev_convert_xy_to_mm(const struct evdev_device *device, int x, int y)
655 {
656 struct phys_coords mm;
657
658 mm.x = evdev_convert_to_mm(device->abs.absinfo_x, x);
659 mm.y = evdev_convert_to_mm(device->abs.absinfo_y, y);
660
661 return mm;
662 }
663
664 void
665 evdev_init_left_handed(struct evdev_device *device,
666 void (*change_to_left_handed)(struct evdev_device *));
667
668 bool
669 evdev_tablet_has_left_handed(struct evdev_device *device);
670
671 static inline uint32_t
evdev_to_left_handed(struct evdev_device *device, uint32_t button)672 evdev_to_left_handed(struct evdev_device *device,
673 uint32_t button)
674 {
675 if (device->left_handed.enabled) {
676 if (button == BTN_LEFT)
677 return BTN_RIGHT;
678 else if (button == BTN_RIGHT)
679 return BTN_LEFT;
680 }
681 return button;
682 }
683
684 /**
685 * Apply a hysteresis filtering to the coordinate in, based on the current
686 * hysteresis center and the margin. If 'in' is within 'margin' of center,
687 * return the center (and thus filter the motion). If 'in' is outside,
688 * return a point on the edge of the new margin (which is an ellipse, usually
689 * a circle). So for a point x in the space outside c + margin we return r:
690 * ,---. ,---.
691 * | c | x → | r x
692 * `---' `---'
693 *
694 * The effect of this is that initial small motions are filtered. Once we
695 * move into one direction we lag the real coordinates by 'margin' but any
696 * movement that continues into that direction will always be just outside
697 * margin - we get responsive movement. Once we move back into the other
698 * direction, the first movements are filtered again.
699 *
700 * Returning the edge rather than the point avoids cursor jumps, as the
701 * first reachable coordinate is the point next to the center (center + 1).
702 * Otherwise, the center has a dead zone of size margin around it and the
703 * first reachable point is the margin edge.
704 *
705 * @param in The input coordinate
706 * @param center Current center of the hysteresis
707 * @param margin Hysteresis width (on each side)
708 *
709 * @return The new center of the hysteresis
710 */
711 static inline struct device_coords
evdev_hysteresis(const struct device_coords *in, const struct device_coords *center, const struct device_coords *margin)712 evdev_hysteresis(const struct device_coords *in,
713 const struct device_coords *center,
714 const struct device_coords *margin)
715 {
716 int dx = in->x - center->x;
717 int dy = in->y - center->y;
718 int dx2 = dx * dx;
719 int dy2 = dy * dy;
720 int a = margin->x;
721 int b = margin->y;
722 double normalized_finger_distance, finger_distance, margin_distance;
723 double lag_x, lag_y;
724 struct device_coords result;
725
726 if (!a || !b)
727 return *in;
728
729 /*
730 * Basic equation for an ellipse of radii a,b:
731 * x²/a² + y²/b² = 1
732 * But we start by making a scaled ellipse passing through the
733 * relative finger location (dx,dy). So the scale of this ellipse is
734 * the ratio of finger_distance to margin_distance:
735 * dx²/a² + dy²/b² = normalized_finger_distance²
736 */
737 normalized_finger_distance = sqrt((double)dx2 / (a * a) +
738 (double)dy2 / (b * b));
739
740 /* Which means anything less than 1 is within the elliptical margin */
741 if (normalized_finger_distance < 1.0)
742 return *center;
743
744 finger_distance = sqrt(dx2 + dy2);
745 margin_distance = finger_distance / normalized_finger_distance;
746
747 /*
748 * Now calculate the x,y coordinates on the edge of the margin ellipse
749 * where it intersects the finger vector. Shortcut: We achieve this by
750 * finding the point with the same gradient as dy/dx.
751 */
752 if (dx) {
753 double gradient = (double)dy / dx;
754 lag_x = margin_distance / sqrt(gradient * gradient + 1);
755 lag_y = sqrt((margin_distance + lag_x) *
756 (margin_distance - lag_x));
757 } else { /* Infinite gradient */
758 lag_x = 0.0;
759 lag_y = margin_distance;
760 }
761
762 /*
763 * 'result' is the centre of an ellipse (radii a,b) which has been
764 * dragged by the finger moving inside it to 'in'. The finger is now
765 * touching the margin ellipse at some point: (±lag_x,±lag_y)
766 */
767 result.x = (dx >= 0) ? in->x - lag_x : in->x + lag_x;
768 result.y = (dy >= 0) ? in->y - lag_y : in->y + lag_y;
769 return result;
770 }
771
772 LIBINPUT_ATTRIBUTE_PRINTF(3, 4)
773 static inline void
evdev_log_msg(struct evdev_device *device, enum libinput_log_priority priority, const char *format, ...)774 evdev_log_msg(struct evdev_device *device,
775 enum libinput_log_priority priority,
776 const char *format,
777 ...)
778 {
779 va_list args;
780 char buf[1024];
781
782 if (!is_logged(evdev_libinput_context(device), priority))
783 return;
784
785 /* Anything info and above is user-visible, use the device name */
786 snprintf(buf,
787 sizeof(buf),
788 "%-7s - %s%s%s",
789 evdev_device_get_sysname(device),
790 (priority > LIBINPUT_LOG_PRIORITY_DEBUG) ? device->log_prefix_name : "",
791 (priority > LIBINPUT_LOG_PRIORITY_DEBUG) ? ": " : "",
792 format);
793
794 va_start(args, format);
795 #pragma GCC diagnostic push
796 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
797 log_msg_va(evdev_libinput_context(device), priority, buf, args);
798 #pragma GCC diagnostic pop
799 va_end(args);
800
801 }
802
803 LIBINPUT_ATTRIBUTE_PRINTF(4, 5)
804 static inline void
evdev_log_msg_ratelimit(struct evdev_device *device, struct ratelimit *ratelimit, enum libinput_log_priority priority, const char *format, ...)805 evdev_log_msg_ratelimit(struct evdev_device *device,
806 struct ratelimit *ratelimit,
807 enum libinput_log_priority priority,
808 const char *format,
809 ...)
810 {
811 va_list args;
812 char buf[1024];
813
814 enum ratelimit_state state;
815
816 if (!is_logged(evdev_libinput_context(device), priority))
817 return;
818
819 state = ratelimit_test(ratelimit);
820 if (state == RATELIMIT_EXCEEDED)
821 return;
822
823 /* Anything info and above is user-visible, use the device name */
824 snprintf(buf,
825 sizeof(buf),
826 "%-7s - %s%s%s",
827 evdev_device_get_sysname(device),
828 (priority > LIBINPUT_LOG_PRIORITY_DEBUG) ? device->log_prefix_name : "",
829 (priority > LIBINPUT_LOG_PRIORITY_DEBUG) ? ": " : "",
830 format);
831
832 va_start(args, format);
833 #pragma GCC diagnostic push
834 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
835 log_msg_va(evdev_libinput_context(device), priority, buf, args);
836 #pragma GCC diagnostic pop
837 va_end(args);
838
839 if (state == RATELIMIT_THRESHOLD) {
840 struct human_time ht = to_human_time(ratelimit->interval);
841 evdev_log_msg(device,
842 priority,
843 "WARNING: log rate limit exceeded (%d msgs per %d%s). "
844 "Discarding future messages.\n",
845 ratelimit->burst,
846 ht.value,
847 ht.unit);
848
849 }
850 }
851
852 #define evdev_log_debug(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_DEBUG, __VA_ARGS__)
853 #define evdev_log_info(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_INFO, __VA_ARGS__)
854 #define evdev_log_error(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_ERROR, __VA_ARGS__)
855 #define evdev_log_bug_kernel(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_ERROR, "kernel bug: " __VA_ARGS__)
856 #define evdev_log_bug_libinput(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_ERROR, "libinput bug: " __VA_ARGS__)
857 #define evdev_log_bug_client(d_, ...) evdev_log_msg((d_), LIBINPUT_LOG_PRIORITY_ERROR, "client bug: " __VA_ARGS__)
858
859 #define evdev_log_debug_ratelimit(d_, r_, ...) \
860 evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_DEBUG, __VA_ARGS__)
861 #define evdev_log_info_ratelimit(d_, r_, ...) \
862 evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_INFO, __VA_ARGS__)
863 #define evdev_log_error_ratelimit(d_, r_, ...) \
864 evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, __VA_ARGS__)
865 #define evdev_log_bug_kernel_ratelimit(d_, r_, ...) \
866 evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, "kernel bug: " __VA_ARGS__)
867 #define evdev_log_bug_libinput_ratelimit(d_, r_, ...) \
868 evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, "libinput bug: " __VA_ARGS__)
869 #define evdev_log_bug_client_ratelimit(d_, r_, ...) \
870 evdev_log_msg_ratelimit((d_), (r_), LIBINPUT_LOG_PRIORITY_ERROR, "client bug: " __VA_ARGS__)
871
872 /**
873 * Convert the pair of delta coordinates in device space to mm.
874 */
875 static inline struct phys_coords
evdev_device_unit_delta_to_mm(const struct evdev_device* device, const struct device_coords *units)876 evdev_device_unit_delta_to_mm(const struct evdev_device* device,
877 const struct device_coords *units)
878 {
879 struct phys_coords mm = { 0, 0 };
880 const struct input_absinfo *absx, *absy;
881
882 if (device->abs.absinfo_x == NULL ||
883 device->abs.absinfo_y == NULL) {
884 log_bug_libinput(evdev_libinput_context(device),
885 "%s: is not an abs device\n",
886 device->devname);
887 return mm;
888 }
889
890 absx = device->abs.absinfo_x;
891 absy = device->abs.absinfo_y;
892
893 mm.x = 1.0 * units->x/absx->resolution;
894 mm.y = 1.0 * units->y/absy->resolution;
895
896 return mm;
897 }
898
899 /**
900 * Convert the pair of coordinates in device space to mm. This takes the
901 * axis min into account, i.e. a unit of min is equivalent to 0 mm.
902 */
903 static inline struct phys_coords
evdev_device_units_to_mm(const struct evdev_device* device, const struct device_coords *units)904 evdev_device_units_to_mm(const struct evdev_device* device,
905 const struct device_coords *units)
906 {
907 struct phys_coords mm = { 0, 0 };
908 const struct input_absinfo *absx, *absy;
909
910 if (device->abs.absinfo_x == NULL ||
911 device->abs.absinfo_y == NULL) {
912 log_bug_libinput(evdev_libinput_context(device),
913 "%s: is not an abs device\n",
914 device->devname);
915 return mm;
916 }
917
918 absx = device->abs.absinfo_x;
919 absy = device->abs.absinfo_y;
920
921 mm.x = (units->x - absx->minimum)/absx->resolution;
922 mm.y = (units->y - absy->minimum)/absy->resolution;
923
924 return mm;
925 }
926
927 /**
928 * Convert the pair of coordinates in mm to device units. This takes the
929 * axis min into account, i.e. 0 mm is equivalent to the min.
930 */
931 static inline struct device_coords
evdev_device_mm_to_units(const struct evdev_device *device, const struct phys_coords *mm)932 evdev_device_mm_to_units(const struct evdev_device *device,
933 const struct phys_coords *mm)
934 {
935 struct device_coords units = { 0, 0 };
936 const struct input_absinfo *absx, *absy;
937
938 if (device->abs.absinfo_x == NULL ||
939 device->abs.absinfo_y == NULL) {
940 log_bug_libinput(evdev_libinput_context(device),
941 "%s: is not an abs device\n",
942 device->devname);
943 return units;
944 }
945
946 absx = device->abs.absinfo_x;
947 absy = device->abs.absinfo_y;
948
949 units.x = mm->x * absx->resolution + absx->minimum;
950 units.y = mm->y * absy->resolution + absy->minimum;
951
952 return units;
953 }
954
955 static inline struct device_coord_rect
evdev_phys_rect_to_units(const struct evdev_device *device, const struct phys_rect *mm)956 evdev_phys_rect_to_units(const struct evdev_device *device,
957 const struct phys_rect *mm)
958 {
959 struct device_coord_rect units = {0};
960 const struct input_absinfo *absx, *absy;
961
962 if (device->abs.absinfo_x == NULL ||
963 device->abs.absinfo_y == NULL) {
964 log_bug_libinput(evdev_libinput_context(device),
965 "%s: is not an abs device\n",
966 device->devname);
967 return units;
968 }
969
970 absx = device->abs.absinfo_x;
971 absy = device->abs.absinfo_y;
972
973 units.x = mm->x * absx->resolution + absx->minimum;
974 units.y = mm->y * absy->resolution + absy->minimum;
975 units.w = mm->w * absx->resolution;
976 units.h = mm->h * absy->resolution;
977
978 return units;
979 }
980
981 static inline void
evdev_device_init_abs_range_warnings(struct evdev_device *device)982 evdev_device_init_abs_range_warnings(struct evdev_device *device)
983 {
984 const struct input_absinfo *x, *y;
985 int width, height;
986
987 x = device->abs.absinfo_x;
988 y = device->abs.absinfo_y;
989 width = device->abs.dimensions.x;
990 height = device->abs.dimensions.y;
991
992 device->abs.warning_range.min.x = x->minimum - 0.05 * width;
993 device->abs.warning_range.min.y = y->minimum - 0.05 * height;
994 device->abs.warning_range.max.x = x->maximum + 0.05 * width;
995 device->abs.warning_range.max.y = y->maximum + 0.05 * height;
996
997 /* One warning every 5 min is enough */
998 ratelimit_init(&device->abs.warning_range.range_warn_limit,
999 s2us(3000),
1000 1);
1001 }
1002
1003 static inline void
evdev_device_check_abs_axis_range(struct evdev_device *device, unsigned int code, int value)1004 evdev_device_check_abs_axis_range(struct evdev_device *device,
1005 unsigned int code,
1006 int value)
1007 {
1008 int min, max;
1009
1010 switch(code) {
1011 case ABS_X:
1012 case ABS_MT_POSITION_X:
1013 min = device->abs.warning_range.min.x;
1014 max = device->abs.warning_range.max.x;
1015 break;
1016 case ABS_Y:
1017 case ABS_MT_POSITION_Y:
1018 min = device->abs.warning_range.min.y;
1019 max = device->abs.warning_range.max.y;
1020 break;
1021 default:
1022 return;
1023 }
1024
1025 if (value < min || value > max) {
1026 log_info_ratelimit(evdev_libinput_context(device),
1027 &device->abs.warning_range.range_warn_limit,
1028 "Axis %#x value %d is outside expected range [%d, %d]\n"
1029 "See %s/absolute_coordinate_ranges.html for details\n",
1030 code, value, min, max,
1031 HTTP_DOC_LINK);
1032 }
1033 }
1034
1035 struct evdev_paired_keyboard {
1036 struct list link;
1037 struct evdev_device *device;
1038 struct libinput_event_listener listener;
1039 };
1040
1041 static inline void
evdev_paired_keyboard_destroy(struct evdev_paired_keyboard *kbd)1042 evdev_paired_keyboard_destroy(struct evdev_paired_keyboard *kbd)
1043 {
1044 kbd->device = NULL;
1045 libinput_device_remove_event_listener(&kbd->listener);
1046 list_remove(&kbd->link);
1047 free(kbd);
1048 }
1049
1050 #endif /* EVDEV_H */
1051