1 /*
2 * Copyright © 2014-2015 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #ifndef EVDEV_MT_TOUCHPAD_H
25 #define EVDEV_MT_TOUCHPAD_H
26
27 #include <stdbool.h>
28
29 #include "evdev.h"
30 #include "timer.h"
31
32 #define TOUCHPAD_HISTORY_LENGTH 4
33 #define TOUCHPAD_MIN_SAMPLES 4
34
35 /* Convert mm to a distance normalized to DEFAULT_MOUSE_DPI */
36 #define TP_MM_TO_DPI_NORMALIZED(mm) (DEFAULT_MOUSE_DPI/25.4 * mm)
37
38 enum touchpad_event {
39 TOUCHPAD_EVENT_NONE = 0,
40 TOUCHPAD_EVENT_MOTION = bit(0),
41 TOUCHPAD_EVENT_BUTTON_PRESS = bit(1),
42 TOUCHPAD_EVENT_BUTTON_RELEASE = bit(2),
43 TOUCHPAD_EVENT_OTHERAXIS = bit(3),
44 TOUCHPAD_EVENT_TIMESTAMP = bit(4),
45 };
46
47 enum touch_state {
48 TOUCH_NONE = 0,
49 TOUCH_HOVERING = 1,
50 TOUCH_BEGIN = 2,
51 TOUCH_UPDATE = 3,
52 TOUCH_MAYBE_END = 4,
53 TOUCH_END = 5,
54 };
55
56 static inline const char *
touch_state_to_str(enum touch_state state)57 touch_state_to_str(enum touch_state state)
58 {
59 switch(state) {
60 CASE_RETURN_STRING(TOUCH_NONE);
61 CASE_RETURN_STRING(TOUCH_HOVERING);
62 CASE_RETURN_STRING(TOUCH_BEGIN);
63 CASE_RETURN_STRING(TOUCH_UPDATE);
64 CASE_RETURN_STRING(TOUCH_MAYBE_END);
65 CASE_RETURN_STRING(TOUCH_END);
66 }
67 return NULL;
68 }
69
70 enum touch_palm_state {
71 PALM_NONE = 0,
72 PALM_EDGE,
73 PALM_TYPING,
74 PALM_TRACKPOINT,
75 PALM_TOOL_PALM,
76 PALM_PRESSURE,
77 PALM_TOUCH_SIZE,
78 PALM_ARBITRATION,
79 };
80
81 enum button_event {
82 BUTTON_EVENT_IN_BOTTOM_R = 30,
83 BUTTON_EVENT_IN_BOTTOM_M,
84 BUTTON_EVENT_IN_BOTTOM_L,
85 BUTTON_EVENT_IN_TOP_R,
86 BUTTON_EVENT_IN_TOP_M,
87 BUTTON_EVENT_IN_TOP_L,
88 BUTTON_EVENT_IN_AREA,
89 BUTTON_EVENT_UP,
90 BUTTON_EVENT_PRESS,
91 BUTTON_EVENT_RELEASE,
92 BUTTON_EVENT_TIMEOUT,
93 };
94
95 enum button_state {
96 BUTTON_STATE_NONE,
97 BUTTON_STATE_AREA,
98 BUTTON_STATE_BOTTOM,
99 BUTTON_STATE_TOP,
100 BUTTON_STATE_TOP_NEW,
101 BUTTON_STATE_TOP_TO_IGNORE,
102 BUTTON_STATE_IGNORE,
103 };
104
105 enum tp_tap_state {
106 TAP_STATE_IDLE = 4,
107 TAP_STATE_TOUCH,
108 TAP_STATE_HOLD,
109 TAP_STATE_TAPPED,
110 TAP_STATE_TOUCH_2,
111 TAP_STATE_TOUCH_2_HOLD,
112 TAP_STATE_TOUCH_2_RELEASE,
113 TAP_STATE_TOUCH_3,
114 TAP_STATE_TOUCH_3_HOLD,
115 TAP_STATE_DRAGGING_OR_DOUBLETAP,
116 TAP_STATE_DRAGGING_OR_TAP,
117 TAP_STATE_DRAGGING,
118 TAP_STATE_DRAGGING_WAIT,
119 TAP_STATE_DRAGGING_2,
120 TAP_STATE_DEAD, /**< finger count exceeded */
121 };
122
123 enum tp_tap_touch_state {
124 TAP_TOUCH_STATE_IDLE = 16, /**< not in touch */
125 TAP_TOUCH_STATE_TOUCH, /**< touching, may tap */
126 TAP_TOUCH_STATE_DEAD, /**< exceeded motion/timeout */
127 };
128
129 /* For edge scrolling, so we only care about right and bottom */
130 enum tp_edge {
131 EDGE_NONE = 0,
132 EDGE_RIGHT = bit(0),
133 EDGE_BOTTOM = bit(1),
134 };
135
136 enum tp_edge_scroll_touch_state {
137 EDGE_SCROLL_TOUCH_STATE_NONE,
138 EDGE_SCROLL_TOUCH_STATE_EDGE_NEW,
139 EDGE_SCROLL_TOUCH_STATE_EDGE,
140 EDGE_SCROLL_TOUCH_STATE_AREA,
141 };
142
143 enum tp_gesture_state {
144 GESTURE_STATE_NONE,
145 GESTURE_STATE_UNKNOWN,
146 GESTURE_STATE_SCROLL,
147 GESTURE_STATE_PINCH,
148 GESTURE_STATE_SWIPE,
149 };
150
151 enum tp_thumb_state {
152 THUMB_STATE_FINGER,
153 THUMB_STATE_JAILED,
154 THUMB_STATE_PINCH,
155 THUMB_STATE_SUPPRESSED,
156 THUMB_STATE_REVIVED,
157 THUMB_STATE_REVIVED_JAILED,
158 THUMB_STATE_DEAD,
159 };
160
161 enum tp_jump_state {
162 JUMP_STATE_IGNORE = 0,
163 JUMP_STATE_EXPECT_FIRST,
164 JUMP_STATE_EXPECT_DELAY,
165 };
166
167 struct tp_touch {
168 struct tp_dispatch *tp;
169 unsigned int index;
170 enum touch_state state;
171 bool has_ended; /* TRACKING_ID == -1 */
172 bool dirty;
173 struct device_coords point;
174 uint64_t time;
175 uint64_t initial_time;
176 int pressure;
177 bool is_tool_palm; /* MT_TOOL_PALM */
178 int major, minor;
179
180 bool was_down; /* if distance == 0, false for pure hovering
181 touches */
182
183 struct {
184 /* A quirk mostly used on Synaptics touchpads. In a
185 transition to/from fake touches > num_slots, the current
186 event data is likely garbage and the subsequent event
187 is likely too. This marker tells us to reset the motion
188 history again -> this effectively swallows any motion */
189 bool reset_motion_history;
190 } quirks;
191
192 struct {
193 struct tp_history_point {
194 uint64_t time;
195 struct device_coords point;
196 } samples[TOUCHPAD_HISTORY_LENGTH];
197 unsigned int index;
198 unsigned int count;
199 } history;
200
201 struct {
202 double last_delta_mm;
203 } jumps;
204
205 struct {
206 struct device_coords center;
207 uint8_t x_motion_history;
208 } hysteresis;
209
210 /* A pinned touchpoint is the one that pressed the physical button
211 * on a clickpad. After the release, it won't move until the center
212 * moves more than a threshold away from the original coordinates
213 */
214 struct {
215 bool is_pinned;
216 struct device_coords center;
217 } pinned;
218
219 /* Software-button state and timeout if applicable */
220 struct {
221 enum button_state state;
222 /* We use button_event here so we can use == on events */
223 enum button_event current;
224 struct libinput_timer timer;
225 struct device_coords initial;
226 bool has_moved; /* has moved more than threshold */
227 uint64_t initial_time;
228 } button;
229
230 struct {
231 enum tp_tap_touch_state state;
232 struct device_coords initial;
233 bool is_thumb;
234 bool is_palm;
235 } tap;
236
237 struct {
238 enum tp_edge_scroll_touch_state edge_state;
239 uint32_t edge;
240 int direction;
241 struct libinput_timer timer;
242 struct device_coords initial;
243 } scroll;
244
245 struct {
246 enum touch_palm_state state;
247 struct device_coords first; /* first coordinates if is_palm == true */
248 uint64_t time; /* first timestamp if is_palm == true */
249 } palm;
250
251 struct {
252 struct device_coords initial;
253 } gesture;
254
255 struct {
256 double last_speed; /* speed in mm/s at last sample */
257 unsigned int exceeded_count;
258 } speed;
259 };
260
261 enum suspend_trigger {
262 SUSPEND_NO_FLAG = 0x0,
263 SUSPEND_EXTERNAL_MOUSE = 0x1,
264 SUSPEND_SENDEVENTS = 0x2,
265 SUSPEND_LID = 0x4,
266 SUSPEND_TABLET_MODE = 0x8,
267 };
268
269 struct tp_dispatch {
270 struct evdev_dispatch base;
271 struct evdev_device *device;
272 unsigned int nfingers_down; /* number of fingers down */
273 unsigned int old_nfingers_down; /* previous no fingers down */
274 unsigned int slot; /* current slot */
275 bool has_mt;
276 bool semi_mt;
277
278 uint32_t suspend_reason;
279
280 /* pen/touch arbitration */
281 struct {
282 enum evdev_arbitration_state state;
283 struct libinput_timer arbitration_timer;
284 } arbitration;
285
286 unsigned int nactive_slots; /* number of active slots */
287 unsigned int num_slots; /* number of slots */
288 unsigned int ntouches; /* no slots inc. fakes */
289 struct tp_touch *touches; /* len == ntouches */
290 /* bit 0: BTN_TOUCH
291 * bit 1: BTN_TOOL_FINGER
292 * bit 2: BTN_TOOL_DOUBLETAP
293 * ...
294 */
295 unsigned int fake_touches;
296
297 struct {
298 bool detection_disabled;
299 struct ratelimit warning;
300 } jump;
301
302 /* if pressure goes above high -> touch down,
303 if pressure then goes below low -> touch up */
304 struct {
305 bool use_pressure;
306 int high;
307 int low;
308 } pressure;
309
310 /* If touch size (either axis) goes above high -> touch down,
311 if touch size (either axis) goes below low -> touch up */
312 struct {
313 bool use_touch_size;
314 int high;
315 int low;
316
317 /* convert device units to angle */
318 double orientation_to_angle;
319 } touch_size;
320
321 struct {
322 bool enabled;
323 struct device_coords margin;
324 unsigned int other_event_count;
325 uint64_t last_motion_time;
326 } hysteresis;
327
328 struct {
329 double x_scale_coeff;
330 double y_scale_coeff;
331 double xy_scale_coeff;
332 } accel;
333
334 struct {
335 bool enabled;
336 bool started;
337 unsigned int finger_count;
338 unsigned int finger_count_pending;
339 struct libinput_timer finger_count_switch_timer;
340 enum tp_gesture_state state;
341 struct tp_touch *touches[2];
342 uint64_t initial_time;
343 double initial_distance;
344 double prev_scale;
345 double angle;
346 struct device_float_coords center;
347 } gesture;
348
349 struct {
350 bool is_clickpad; /* true for clickpads */
351 bool has_topbuttons;
352 bool use_clickfinger; /* number of fingers decides button number */
353 bool click_pending;
354 uint32_t state;
355 uint32_t old_state;
356 struct {
357 double x_scale_coeff;
358 double y_scale_coeff;
359 } motion_dist; /* for pinned touches */
360 unsigned int active; /* currently active button, for release event */
361 bool active_is_topbutton; /* is active a top button? */
362
363 /* Only used for clickpads. The software button areas are
364 * always 2 horizontal stripes across the touchpad.
365 * The buttons are split according to the edge settings.
366 */
367 struct {
368 int32_t top_edge; /* in device coordinates */
369 int32_t rightbutton_left_edge; /* in device coordinates */
370 int32_t middlebutton_left_edge; /* in device coordinates */
371 } bottom_area;
372
373 struct {
374 int32_t bottom_edge; /* in device coordinates */
375 int32_t rightbutton_left_edge; /* in device coordinates */
376 int32_t leftbutton_right_edge; /* in device coordinates */
377 } top_area;
378
379 struct evdev_device *trackpoint;
380
381 enum libinput_config_click_method click_method;
382 struct libinput_device_config_click_method config_method;
383 } buttons;
384
385 struct {
386 struct libinput_device_config_scroll_method config_method;
387 enum libinput_config_scroll_method method;
388 int32_t right_edge; /* in device coordinates */
389 int32_t bottom_edge; /* in device coordinates */
390 struct {
391 bool h, v;
392 } active;
393 struct phys_coords vector;
394 uint64_t time_prev;
395 struct {
396 uint64_t h, v;
397 } duration;
398 } scroll;
399
400 enum touchpad_event queued;
401
402 struct {
403 struct libinput_device_config_tap config;
404 bool enabled;
405 bool suspended;
406 struct libinput_timer timer;
407 enum tp_tap_state state;
408 uint32_t buttons_pressed;
409 uint64_t saved_press_time,
410 saved_release_time;
411
412 enum libinput_config_tap_button_map map;
413 enum libinput_config_tap_button_map want_map;
414
415 bool drag_enabled;
416 bool drag_lock_enabled;
417
418 unsigned int nfingers_down; /* number of fingers down for tapping (excl. thumb/palm) */
419 } tap;
420
421 struct {
422 int32_t right_edge; /* in device coordinates */
423 int32_t left_edge; /* in device coordinates */
424 int32_t upper_edge; /* in device coordinates */
425
426 bool trackpoint_active;
427 struct libinput_event_listener trackpoint_listener;
428 struct libinput_timer trackpoint_timer;
429 uint64_t trackpoint_last_event_time;
430 uint32_t trackpoint_event_count;
431 bool monitor_trackpoint;
432
433 bool use_mt_tool;
434
435 bool use_pressure;
436 int pressure_threshold;
437
438 bool use_size;
439 int size_threshold;
440 } palm;
441
442 struct {
443 struct libinput_device_config_send_events config;
444 enum libinput_config_send_events_mode current_mode;
445 } sendevents;
446
447 struct {
448 struct libinput_device_config_dwt config;
449 bool dwt_enabled;
450
451 /* We have to allow for more than one device node to be the
452 * internal dwt keyboard (Razer Blade). But they're the same
453 * physical device, so we don't care about per-keyboard
454 * key/modifier masks.
455 */
456 struct list paired_keyboard_list;
457
458 unsigned long key_mask[NLONGS(KEY_CNT)];
459 unsigned long mod_mask[NLONGS(KEY_CNT)];
460 bool keyboard_active;
461 struct libinput_timer keyboard_timer;
462 uint64_t keyboard_last_press_time;
463 } dwt;
464
465 struct {
466 bool detect_thumbs;
467 int upper_thumb_line;
468 int lower_thumb_line;
469
470 bool use_pressure;
471 int pressure_threshold;
472
473 bool use_size;
474 int size_threshold;
475
476 enum tp_thumb_state state;
477 unsigned int index;
478 bool pinch_eligible;
479 } thumb;
480
481 struct {
482 /* A quirk used on the T450 series Synaptics hardware.
483 * Slowly moving the finger causes multiple events with only
484 * ABS_MT_PRESSURE but no x/y information. When the x/y
485 * event comes, it will be a jump of ~20 units. We use the
486 * below to count non-motion events to discard that first
487 * event with the jump.
488 */
489 unsigned int nonmotion_event_count;
490
491 struct msc_timestamp {
492 enum tp_jump_state state;
493 uint32_t interval;
494 uint32_t now;
495 } msc_timestamp;
496 } quirks;
497
498 struct {
499 struct libinput_event_listener listener;
500 struct evdev_device *lid_switch;
501 } lid_switch;
502
503 struct {
504 struct libinput_event_listener listener;
505 struct evdev_device *tablet_mode_switch;
506 } tablet_mode_switch;
507
508 struct {
509 bool rotate;
510 bool want_rotate;
511
512 bool must_rotate; /* true if we should rotate when applicable */
513 struct evdev_device *tablet_device;
514 bool tablet_left_handed_state;
515 } left_handed;
516 };
517
518 static inline struct tp_dispatch*
tp_dispatch(struct evdev_dispatch * dispatch)519 tp_dispatch(struct evdev_dispatch *dispatch)
520 {
521 evdev_verify_dispatch_type(dispatch, DISPATCH_TOUCHPAD);
522
523 return container_of(dispatch, struct tp_dispatch, base);
524 }
525
526 #define tp_for_each_touch(_tp, _t) \
527 for (unsigned int _i = 0; _i < (_tp)->ntouches && (_t = &(_tp)->touches[_i]); _i++)
528
529 static inline struct libinput*
tp_libinput_context(const struct tp_dispatch * tp)530 tp_libinput_context(const struct tp_dispatch *tp)
531 {
532 return evdev_libinput_context(tp->device);
533 }
534
535 static inline struct normalized_coords
tp_normalize_delta(const struct tp_dispatch * tp,struct device_float_coords delta)536 tp_normalize_delta(const struct tp_dispatch *tp,
537 struct device_float_coords delta)
538 {
539 struct normalized_coords normalized;
540
541 normalized.x = delta.x * tp->accel.x_scale_coeff;
542 normalized.y = delta.y * tp->accel.y_scale_coeff;
543
544 return normalized;
545 }
546
547 static inline struct phys_coords
tp_phys_delta(const struct tp_dispatch * tp,struct device_float_coords delta)548 tp_phys_delta(const struct tp_dispatch *tp,
549 struct device_float_coords delta)
550 {
551 struct phys_coords mm;
552
553 mm.x = delta.x / tp->device->abs.absinfo_x->resolution;
554 mm.y = delta.y / tp->device->abs.absinfo_y->resolution;
555
556 return mm;
557 }
558
559 /**
560 * Takes a set of device coordinates, returns that set of coordinates in the
561 * x-axis' resolution.
562 */
563 static inline struct device_float_coords
tp_scale_to_xaxis(const struct tp_dispatch * tp,struct device_float_coords delta)564 tp_scale_to_xaxis(const struct tp_dispatch *tp,
565 struct device_float_coords delta)
566 {
567 struct device_float_coords raw;
568
569 raw.x = delta.x;
570 raw.y = delta.y * tp->accel.xy_scale_coeff;
571
572 return raw;
573 }
574
575 struct device_coords
576 tp_get_delta(struct tp_touch *t);
577
578 struct normalized_coords
579 tp_filter_motion(struct tp_dispatch *tp,
580 const struct device_float_coords *unaccelerated,
581 uint64_t time);
582
583 struct normalized_coords
584 tp_filter_motion_unaccelerated(struct tp_dispatch *tp,
585 const struct device_float_coords *unaccelerated,
586 uint64_t time);
587
588 bool
589 tp_touch_active(const struct tp_dispatch *tp, const struct tp_touch *t);
590
591 bool
592 tp_touch_active_for_gesture(const struct tp_dispatch *tp,
593 const struct tp_touch *t);
594
595 int
596 tp_tap_handle_state(struct tp_dispatch *tp, uint64_t time);
597
598 void
599 tp_tap_post_process_state(struct tp_dispatch *tp);
600
601 void
602 tp_init_tap(struct tp_dispatch *tp);
603
604 void
605 tp_remove_tap(struct tp_dispatch *tp);
606
607 void
608 tp_init_buttons(struct tp_dispatch *tp, struct evdev_device *device);
609
610 void
611 tp_init_top_softbuttons(struct tp_dispatch *tp,
612 struct evdev_device *device,
613 double topbutton_size_mult);
614
615 void
616 tp_remove_buttons(struct tp_dispatch *tp);
617
618 void
619 tp_process_button(struct tp_dispatch *tp,
620 const struct input_event *e,
621 uint64_t time);
622
623 void
624 tp_release_all_buttons(struct tp_dispatch *tp,
625 uint64_t time);
626
627 int
628 tp_post_button_events(struct tp_dispatch *tp, uint64_t time);
629
630 void
631 tp_button_handle_state(struct tp_dispatch *tp, uint64_t time);
632
633 bool
634 tp_button_touch_active(const struct tp_dispatch *tp,
635 const struct tp_touch *t);
636
637 bool
638 tp_button_is_inside_softbutton_area(const struct tp_dispatch *tp,
639 const struct tp_touch *t);
640
641 void
642 tp_release_all_taps(struct tp_dispatch *tp,
643 uint64_t time);
644
645 void
646 tp_tap_suspend(struct tp_dispatch *tp, uint64_t time);
647
648 void
649 tp_tap_resume(struct tp_dispatch *tp, uint64_t time);
650
651 bool
652 tp_tap_dragging(const struct tp_dispatch *tp);
653
654 void
655 tp_edge_scroll_init(struct tp_dispatch *tp, struct evdev_device *device);
656
657 void
658 tp_remove_edge_scroll(struct tp_dispatch *tp);
659
660 void
661 tp_edge_scroll_handle_state(struct tp_dispatch *tp, uint64_t time);
662
663 int
664 tp_edge_scroll_post_events(struct tp_dispatch *tp, uint64_t time);
665
666 void
667 tp_edge_scroll_stop_events(struct tp_dispatch *tp, uint64_t time);
668
669 int
670 tp_edge_scroll_touch_active(const struct tp_dispatch *tp,
671 const struct tp_touch *t);
672
673 uint32_t
674 tp_touch_get_edge(const struct tp_dispatch *tp, const struct tp_touch *t);
675
676 void
677 tp_init_gesture(struct tp_dispatch *tp);
678
679 void
680 tp_remove_gesture(struct tp_dispatch *tp);
681
682 void
683 tp_gesture_stop(struct tp_dispatch *tp, uint64_t time);
684
685 void
686 tp_gesture_cancel(struct tp_dispatch *tp, uint64_t time);
687
688 void
689 tp_gesture_handle_state(struct tp_dispatch *tp, uint64_t time);
690
691 void
692 tp_gesture_post_events(struct tp_dispatch *tp, uint64_t time);
693
694 void
695 tp_gesture_stop_twofinger_scroll(struct tp_dispatch *tp, uint64_t time);
696
697 bool
698 tp_palm_tap_is_palm(const struct tp_dispatch *tp, const struct tp_touch *t);
699
700 void
701 tp_clickpad_middlebutton_apply_config(struct evdev_device *device);
702
703 bool
704 tp_thumb_ignored(const struct tp_dispatch *tp, const struct tp_touch *t);
705
706 void
707 tp_thumb_reset(struct tp_dispatch *tp);
708
709 bool
710 tp_thumb_ignored_for_gesture(const struct tp_dispatch *tp, const struct tp_touch *t);
711
712 bool
713 tp_thumb_ignored_for_tap(const struct tp_dispatch *tp,
714 const struct tp_touch *t);
715
716 void
717 tp_thumb_suppress(struct tp_dispatch *tp, struct tp_touch *t);
718
719 void
720 tp_thumb_update_touch(struct tp_dispatch *tp,
721 struct tp_touch *t,
722 uint64_t time);
723
724 void
725 tp_detect_thumb_while_moving(struct tp_dispatch *tp);
726
727 void
728 tp_thumb_update_multifinger(struct tp_dispatch *tp);
729
730 void
731 tp_init_thumb(struct tp_dispatch *tp);
732
733 #endif
734