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