• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2013-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 #include "config.h"
25 
26 #include <assert.h>
27 #include <stdbool.h>
28 #include <stdio.h>
29 
30 #include "evdev-mt-touchpad.h"
31 
32 #define DEFAULT_TAP_TIMEOUT_PERIOD ms2us(180)
33 #define DEFAULT_DRAG_TIMEOUT_PERIOD ms2us(300)
34 #define DEFAULT_TAP_MOVE_THRESHOLD 1.3 /* mm */
35 
36 enum tap_event {
37 	TAP_EVENT_TOUCH = 12,
38 	TAP_EVENT_MOTION,
39 	TAP_EVENT_RELEASE,
40 	TAP_EVENT_BUTTON,
41 	TAP_EVENT_TIMEOUT,
42 	TAP_EVENT_THUMB,
43 	TAP_EVENT_PALM,
44 	TAP_EVENT_PALM_UP,
45 };
46 
47 /*****************************************
48  * DO NOT EDIT THIS FILE!
49  *
50  * Look at the state diagram in doc/touchpad-tap-state-machine.svg
51  * (generated with https://draw.io)
52  *
53  * Any changes in this file must be represented in the diagram.
54  */
55 
56 static inline const char*
tap_state_to_str(enum tp_tap_state state)57 tap_state_to_str(enum tp_tap_state state)
58 {
59 	switch(state) {
60 	CASE_RETURN_STRING(TAP_STATE_IDLE);
61 	CASE_RETURN_STRING(TAP_STATE_HOLD);
62 	CASE_RETURN_STRING(TAP_STATE_TOUCH);
63 	CASE_RETURN_STRING(TAP_STATE_TAPPED);
64 	CASE_RETURN_STRING(TAP_STATE_TOUCH_2);
65 	CASE_RETURN_STRING(TAP_STATE_TOUCH_2_HOLD);
66 	CASE_RETURN_STRING(TAP_STATE_TOUCH_2_RELEASE);
67 	CASE_RETURN_STRING(TAP_STATE_TOUCH_3);
68 	CASE_RETURN_STRING(TAP_STATE_TOUCH_3_HOLD);
69 	CASE_RETURN_STRING(TAP_STATE_DRAGGING);
70 	CASE_RETURN_STRING(TAP_STATE_DRAGGING_WAIT);
71 	CASE_RETURN_STRING(TAP_STATE_DRAGGING_OR_DOUBLETAP);
72 	CASE_RETURN_STRING(TAP_STATE_DRAGGING_OR_TAP);
73 	CASE_RETURN_STRING(TAP_STATE_DRAGGING_2);
74 	CASE_RETURN_STRING(TAP_STATE_DEAD);
75 	}
76 	return NULL;
77 }
78 
79 static inline const char*
tap_event_to_str(enum tap_event event)80 tap_event_to_str(enum tap_event event)
81 {
82 	switch(event) {
83 	CASE_RETURN_STRING(TAP_EVENT_TOUCH);
84 	CASE_RETURN_STRING(TAP_EVENT_MOTION);
85 	CASE_RETURN_STRING(TAP_EVENT_RELEASE);
86 	CASE_RETURN_STRING(TAP_EVENT_TIMEOUT);
87 	CASE_RETURN_STRING(TAP_EVENT_BUTTON);
88 	CASE_RETURN_STRING(TAP_EVENT_THUMB);
89 	CASE_RETURN_STRING(TAP_EVENT_PALM);
90 	CASE_RETURN_STRING(TAP_EVENT_PALM_UP);
91 	}
92 	return NULL;
93 }
94 
95 static inline void
log_tap_bug(struct tp_dispatch * tp,struct tp_touch * t,enum tap_event event)96 log_tap_bug(struct tp_dispatch *tp, struct tp_touch *t, enum tap_event event)
97 {
98 	evdev_log_bug_libinput(tp->device,
99 			       "%d: invalid tap event %s in state %s\n",
100 			       t->index,
101 			       tap_event_to_str(event),
102 			       tap_state_to_str(tp->tap.state));
103 
104 }
105 
106 static void
tp_tap_notify(struct tp_dispatch * tp,uint64_t time,int nfingers,enum libinput_button_state state)107 tp_tap_notify(struct tp_dispatch *tp,
108 	      uint64_t time,
109 	      int nfingers,
110 	      enum libinput_button_state state)
111 {
112 	int32_t button;
113 	int32_t button_map[2][3] = {
114 		{ BTN_LEFT, BTN_RIGHT, BTN_MIDDLE },
115 		{ BTN_LEFT, BTN_MIDDLE, BTN_RIGHT },
116 	};
117 
118 	assert(tp->tap.map < ARRAY_LENGTH(button_map));
119 
120 	if (nfingers > 3)
121 		return;
122 
123 	button = button_map[tp->tap.map][nfingers - 1];
124 
125 	if (state == LIBINPUT_BUTTON_STATE_PRESSED)
126 		tp->tap.buttons_pressed |= (1 << nfingers);
127 	else
128 		tp->tap.buttons_pressed &= ~(1 << nfingers);
129 
130 	evdev_pointer_notify_button(tp->device,
131 				    time,
132 				    button,
133 				    state);
134 }
135 
136 static void
tp_tap_set_timer(struct tp_dispatch * tp,uint64_t time)137 tp_tap_set_timer(struct tp_dispatch *tp, uint64_t time)
138 {
139 	libinput_timer_set(&tp->tap.timer, time + DEFAULT_TAP_TIMEOUT_PERIOD);
140 }
141 
142 static void
tp_tap_set_drag_timer(struct tp_dispatch * tp,uint64_t time)143 tp_tap_set_drag_timer(struct tp_dispatch *tp, uint64_t time)
144 {
145 	libinput_timer_set(&tp->tap.timer, time + DEFAULT_DRAG_TIMEOUT_PERIOD);
146 }
147 
148 static void
tp_tap_clear_timer(struct tp_dispatch * tp)149 tp_tap_clear_timer(struct tp_dispatch *tp)
150 {
151 	libinput_timer_cancel(&tp->tap.timer);
152 }
153 
154 static void
tp_tap_move_to_dead(struct tp_dispatch * tp,struct tp_touch * t)155 tp_tap_move_to_dead(struct tp_dispatch *tp, struct tp_touch *t)
156 {
157 	tp->tap.state = TAP_STATE_DEAD;
158 	t->tap.state = TAP_TOUCH_STATE_DEAD;
159 	tp_tap_clear_timer(tp);
160 }
161 
162 static void
tp_tap_idle_handle_event(struct tp_dispatch * tp,struct tp_touch * t,enum tap_event event,uint64_t time)163 tp_tap_idle_handle_event(struct tp_dispatch *tp,
164 			 struct tp_touch *t,
165 			 enum tap_event event, uint64_t time)
166 {
167 	switch (event) {
168 	case TAP_EVENT_TOUCH:
169 		tp->tap.state = TAP_STATE_TOUCH;
170 		tp->tap.saved_press_time = time;
171 		tp_tap_set_timer(tp, time);
172 		break;
173 	case TAP_EVENT_RELEASE:
174 		break;
175 	case TAP_EVENT_MOTION:
176 		log_tap_bug(tp, t, event);
177 		break;
178 	case TAP_EVENT_TIMEOUT:
179 		break;
180 	case TAP_EVENT_BUTTON:
181 		tp->tap.state = TAP_STATE_DEAD;
182 		break;
183 	case TAP_EVENT_THUMB:
184 		log_tap_bug(tp, t, event);
185 		break;
186 	case TAP_EVENT_PALM:
187 		tp->tap.state = TAP_STATE_IDLE;
188 		break;
189 	case TAP_EVENT_PALM_UP:
190 		break;
191 	}
192 }
193 
194 static void
tp_tap_touch_handle_event(struct tp_dispatch * tp,struct tp_touch * t,enum tap_event event,uint64_t time)195 tp_tap_touch_handle_event(struct tp_dispatch *tp,
196 			  struct tp_touch *t,
197 			  enum tap_event event, uint64_t time)
198 {
199 
200 	switch (event) {
201 	case TAP_EVENT_TOUCH:
202 		tp->tap.state = TAP_STATE_TOUCH_2;
203 		tp->tap.saved_press_time = time;
204 		tp_tap_set_timer(tp, time);
205 		break;
206 	case TAP_EVENT_RELEASE:
207 		tp_tap_notify(tp,
208 			      tp->tap.saved_press_time,
209 			      1,
210 			      LIBINPUT_BUTTON_STATE_PRESSED);
211 		if (tp->tap.drag_enabled) {
212 			tp->tap.state = TAP_STATE_TAPPED;
213 			tp->tap.saved_release_time = time;
214 			tp_tap_set_timer(tp, time);
215 		} else {
216 			tp_tap_notify(tp,
217 				      time,
218 				      1,
219 				      LIBINPUT_BUTTON_STATE_RELEASED);
220 			tp->tap.state = TAP_STATE_IDLE;
221 		}
222 		break;
223 	case TAP_EVENT_MOTION:
224 		tp_tap_move_to_dead(tp, t);
225 		break;
226 	case TAP_EVENT_TIMEOUT:
227 		tp->tap.state = TAP_STATE_HOLD;
228 		tp_tap_clear_timer(tp);
229 		break;
230 	case TAP_EVENT_BUTTON:
231 		tp->tap.state = TAP_STATE_DEAD;
232 		break;
233 	case TAP_EVENT_THUMB:
234 		tp->tap.state = TAP_STATE_IDLE;
235 		t->tap.is_thumb = true;
236 		tp->tap.nfingers_down--;
237 		t->tap.state = TAP_TOUCH_STATE_DEAD;
238 		tp_tap_clear_timer(tp);
239 		break;
240 	case TAP_EVENT_PALM:
241 		tp->tap.state = TAP_STATE_IDLE;
242 		tp_tap_clear_timer(tp);
243 		break;
244 	case TAP_EVENT_PALM_UP:
245 		break;
246 	}
247 }
248 
249 static void
tp_tap_hold_handle_event(struct tp_dispatch * tp,struct tp_touch * t,enum tap_event event,uint64_t time)250 tp_tap_hold_handle_event(struct tp_dispatch *tp,
251 			 struct tp_touch *t,
252 			 enum tap_event event, uint64_t time)
253 {
254 
255 	switch (event) {
256 	case TAP_EVENT_TOUCH:
257 		tp->tap.state = TAP_STATE_TOUCH_2;
258 		tp->tap.saved_press_time = time;
259 		tp_tap_set_timer(tp, time);
260 		break;
261 	case TAP_EVENT_RELEASE:
262 		tp->tap.state = TAP_STATE_IDLE;
263 		break;
264 	case TAP_EVENT_MOTION:
265 		tp_tap_move_to_dead(tp, t);
266 		break;
267 	case TAP_EVENT_TIMEOUT:
268 		break;
269 	case TAP_EVENT_BUTTON:
270 		tp->tap.state = TAP_STATE_DEAD;
271 		break;
272 	case TAP_EVENT_THUMB:
273 		tp->tap.state = TAP_STATE_IDLE;
274 		t->tap.is_thumb = true;
275 		tp->tap.nfingers_down--;
276 		t->tap.state = TAP_TOUCH_STATE_DEAD;
277 		break;
278 	case TAP_EVENT_PALM:
279 		tp->tap.state = TAP_STATE_IDLE;
280 		break;
281 	case TAP_EVENT_PALM_UP:
282 		break;
283 	}
284 }
285 
286 static void
tp_tap_tapped_handle_event(struct tp_dispatch * tp,struct tp_touch * t,enum tap_event event,uint64_t time)287 tp_tap_tapped_handle_event(struct tp_dispatch *tp,
288 			   struct tp_touch *t,
289 			   enum tap_event event, uint64_t time)
290 {
291 	switch (event) {
292 	case TAP_EVENT_MOTION:
293 	case TAP_EVENT_RELEASE:
294 		log_tap_bug(tp, t, event);
295 		break;
296 	case TAP_EVENT_TOUCH:
297 		tp->tap.state = TAP_STATE_DRAGGING_OR_DOUBLETAP;
298 		tp->tap.saved_press_time = time;
299 		tp_tap_set_timer(tp, time);
300 		break;
301 	case TAP_EVENT_TIMEOUT:
302 		tp->tap.state = TAP_STATE_IDLE;
303 		tp_tap_notify(tp,
304 			      tp->tap.saved_release_time,
305 			      1,
306 			      LIBINPUT_BUTTON_STATE_RELEASED);
307 		break;
308 	case TAP_EVENT_BUTTON:
309 		tp->tap.state = TAP_STATE_DEAD;
310 		tp_tap_notify(tp,
311 			      tp->tap.saved_release_time,
312 			      1,
313 			      LIBINPUT_BUTTON_STATE_RELEASED);
314 		break;
315 	case TAP_EVENT_THUMB:
316 		log_tap_bug(tp, t, event);
317 		break;
318 	case TAP_EVENT_PALM:
319 	case TAP_EVENT_PALM_UP:
320 		break;
321 	}
322 }
323 
324 static void
tp_tap_touch2_handle_event(struct tp_dispatch * tp,struct tp_touch * t,enum tap_event event,uint64_t time)325 tp_tap_touch2_handle_event(struct tp_dispatch *tp,
326 			   struct tp_touch *t,
327 			   enum tap_event event, uint64_t time)
328 {
329 
330 	switch (event) {
331 	case TAP_EVENT_TOUCH:
332 		tp->tap.state = TAP_STATE_TOUCH_3;
333 		tp->tap.saved_press_time = time;
334 		tp_tap_set_timer(tp, time);
335 		break;
336 	case TAP_EVENT_RELEASE:
337 		tp->tap.state = TAP_STATE_TOUCH_2_RELEASE;
338 		tp->tap.saved_release_time = time;
339 		tp_tap_set_timer(tp, time);
340 		break;
341 	case TAP_EVENT_MOTION:
342 		tp_tap_move_to_dead(tp, t);
343 		break;
344 	case TAP_EVENT_TIMEOUT:
345 		tp->tap.state = TAP_STATE_TOUCH_2_HOLD;
346 		break;
347 	case TAP_EVENT_BUTTON:
348 		tp->tap.state = TAP_STATE_DEAD;
349 		break;
350 	case TAP_EVENT_THUMB:
351 		break;
352 	case TAP_EVENT_PALM:
353 		tp->tap.state = TAP_STATE_TOUCH;
354 		tp_tap_set_timer(tp, time); /* overwrite timer */
355 		break;
356 	case TAP_EVENT_PALM_UP:
357 		break;
358 	}
359 }
360 
361 static void
tp_tap_touch2_hold_handle_event(struct tp_dispatch * tp,struct tp_touch * t,enum tap_event event,uint64_t time)362 tp_tap_touch2_hold_handle_event(struct tp_dispatch *tp,
363 				struct tp_touch *t,
364 				enum tap_event event, uint64_t time)
365 {
366 
367 	switch (event) {
368 	case TAP_EVENT_TOUCH:
369 		tp->tap.state = TAP_STATE_TOUCH_3;
370 		tp->tap.saved_press_time = time;
371 		tp_tap_set_timer(tp, time);
372 		break;
373 	case TAP_EVENT_RELEASE:
374 		tp->tap.state = TAP_STATE_HOLD;
375 		break;
376 	case TAP_EVENT_MOTION:
377 		tp_tap_move_to_dead(tp, t);
378 		break;
379 	case TAP_EVENT_TIMEOUT:
380 		tp->tap.state = TAP_STATE_TOUCH_2_HOLD;
381 		break;
382 	case TAP_EVENT_BUTTON:
383 		tp->tap.state = TAP_STATE_DEAD;
384 		break;
385 	case TAP_EVENT_THUMB:
386 		break;
387 	case TAP_EVENT_PALM:
388 		tp->tap.state = TAP_STATE_HOLD;
389 		break;
390 	case TAP_EVENT_PALM_UP:
391 		break;
392 	}
393 }
394 
395 static void
tp_tap_touch2_release_handle_event(struct tp_dispatch * tp,struct tp_touch * t,enum tap_event event,uint64_t time)396 tp_tap_touch2_release_handle_event(struct tp_dispatch *tp,
397 				   struct tp_touch *t,
398 				   enum tap_event event, uint64_t time)
399 {
400 
401 	switch (event) {
402 	case TAP_EVENT_TOUCH:
403 		tp->tap.state = TAP_STATE_TOUCH_2_HOLD;
404 		t->tap.state = TAP_TOUCH_STATE_DEAD;
405 		tp_tap_clear_timer(tp);
406 		break;
407 	case TAP_EVENT_RELEASE:
408 		tp_tap_notify(tp,
409 			      tp->tap.saved_press_time,
410 			      2,
411 			      LIBINPUT_BUTTON_STATE_PRESSED);
412 		tp_tap_notify(tp,
413 			      tp->tap.saved_release_time,
414 			      2,
415 			      LIBINPUT_BUTTON_STATE_RELEASED);
416 		tp->tap.state = TAP_STATE_IDLE;
417 		break;
418 	case TAP_EVENT_MOTION:
419 		tp_tap_move_to_dead(tp, t);
420 		break;
421 	case TAP_EVENT_TIMEOUT:
422 		tp->tap.state = TAP_STATE_HOLD;
423 		break;
424 	case TAP_EVENT_BUTTON:
425 		tp->tap.state = TAP_STATE_DEAD;
426 		break;
427 	case TAP_EVENT_THUMB:
428 		break;
429 	case TAP_EVENT_PALM:
430 		/* There's only one saved press time and it's overwritten by
431 		 * the last touch down. So in the case of finger down, palm
432 		 * down, finger up, palm detected, we use the
433 		 * palm touch's press time here instead of the finger's press
434 		 * time. Let's wait and see if that's an issue.
435 		 */
436 		tp_tap_notify(tp,
437 			      tp->tap.saved_press_time,
438 			      1,
439 			      LIBINPUT_BUTTON_STATE_PRESSED);
440 		if (tp->tap.drag_enabled) {
441 			tp->tap.state = TAP_STATE_TAPPED;
442 			tp->tap.saved_release_time = time;
443 			tp_tap_set_timer(tp, time);
444 		} else {
445 			tp_tap_notify(tp,
446 				      time,
447 				      1,
448 				      LIBINPUT_BUTTON_STATE_RELEASED);
449 			tp->tap.state = TAP_STATE_IDLE;
450 		}
451 		break;
452 	case TAP_EVENT_PALM_UP:
453 		break;
454 	}
455 }
456 
457 static void
tp_tap_touch3_handle_event(struct tp_dispatch * tp,struct tp_touch * t,enum tap_event event,uint64_t time)458 tp_tap_touch3_handle_event(struct tp_dispatch *tp,
459 			   struct tp_touch *t,
460 			   enum tap_event event, uint64_t time)
461 {
462 
463 	switch (event) {
464 	case TAP_EVENT_TOUCH:
465 		tp->tap.state = TAP_STATE_DEAD;
466 		tp_tap_clear_timer(tp);
467 		break;
468 	case TAP_EVENT_MOTION:
469 		tp_tap_move_to_dead(tp, t);
470 		break;
471 	case TAP_EVENT_TIMEOUT:
472 		tp->tap.state = TAP_STATE_TOUCH_3_HOLD;
473 		tp_tap_clear_timer(tp);
474 		break;
475 	case TAP_EVENT_RELEASE:
476 		tp->tap.state = TAP_STATE_TOUCH_2_HOLD;
477 		if (t->tap.state == TAP_TOUCH_STATE_TOUCH) {
478 			tp_tap_notify(tp,
479 				      tp->tap.saved_press_time,
480 				      3,
481 				      LIBINPUT_BUTTON_STATE_PRESSED);
482 			tp_tap_notify(tp, time, 3, LIBINPUT_BUTTON_STATE_RELEASED);
483 		}
484 		break;
485 	case TAP_EVENT_BUTTON:
486 		tp->tap.state = TAP_STATE_DEAD;
487 		break;
488 	case TAP_EVENT_THUMB:
489 		break;
490 	case TAP_EVENT_PALM:
491 		tp->tap.state = TAP_STATE_TOUCH_2;
492 		break;
493 	case TAP_EVENT_PALM_UP:
494 		break;
495 	}
496 }
497 
498 static void
tp_tap_touch3_hold_handle_event(struct tp_dispatch * tp,struct tp_touch * t,enum tap_event event,uint64_t time)499 tp_tap_touch3_hold_handle_event(struct tp_dispatch *tp,
500 				struct tp_touch *t,
501 				enum tap_event event, uint64_t time)
502 {
503 
504 	switch (event) {
505 	case TAP_EVENT_TOUCH:
506 		tp->tap.state = TAP_STATE_DEAD;
507 		tp_tap_set_timer(tp, time);
508 		break;
509 	case TAP_EVENT_RELEASE:
510 		tp->tap.state = TAP_STATE_TOUCH_2_HOLD;
511 		break;
512 	case TAP_EVENT_MOTION:
513 		tp_tap_move_to_dead(tp, t);
514 		break;
515 	case TAP_EVENT_TIMEOUT:
516 		break;
517 	case TAP_EVENT_BUTTON:
518 		tp->tap.state = TAP_STATE_DEAD;
519 		break;
520 	case TAP_EVENT_THUMB:
521 		break;
522 	case TAP_EVENT_PALM:
523 		tp->tap.state = TAP_STATE_TOUCH_2_HOLD;
524 		break;
525 	case TAP_EVENT_PALM_UP:
526 		break;
527 	}
528 }
529 
530 static void
tp_tap_dragging_or_doubletap_handle_event(struct tp_dispatch * tp,struct tp_touch * t,enum tap_event event,uint64_t time)531 tp_tap_dragging_or_doubletap_handle_event(struct tp_dispatch *tp,
532 					  struct tp_touch *t,
533 					  enum tap_event event, uint64_t time)
534 {
535 	switch (event) {
536 	case TAP_EVENT_TOUCH:
537 		tp->tap.state = TAP_STATE_DRAGGING_2;
538 		break;
539 	case TAP_EVENT_RELEASE:
540 		tp->tap.state = TAP_STATE_TAPPED;
541 		tp_tap_notify(tp,
542 			      tp->tap.saved_release_time,
543 			      1,
544 			      LIBINPUT_BUTTON_STATE_RELEASED);
545 		tp_tap_notify(tp,
546 			      tp->tap.saved_press_time,
547 			      1,
548 			      LIBINPUT_BUTTON_STATE_PRESSED);
549 		tp->tap.saved_release_time = time;
550 		tp_tap_set_timer(tp, time);
551 		break;
552 	case TAP_EVENT_MOTION:
553 	case TAP_EVENT_TIMEOUT:
554 		tp->tap.state = TAP_STATE_DRAGGING;
555 		break;
556 	case TAP_EVENT_BUTTON:
557 		tp->tap.state = TAP_STATE_DEAD;
558 		tp_tap_notify(tp,
559 			      tp->tap.saved_release_time,
560 			      1,
561 			      LIBINPUT_BUTTON_STATE_RELEASED);
562 		break;
563 	case TAP_EVENT_THUMB:
564 		break;
565 	case TAP_EVENT_PALM:
566 		tp->tap.state = TAP_STATE_TAPPED;
567 		break;
568 	case TAP_EVENT_PALM_UP:
569 		break;
570 	}
571 }
572 
573 static void
tp_tap_dragging_handle_event(struct tp_dispatch * tp,struct tp_touch * t,enum tap_event event,uint64_t time)574 tp_tap_dragging_handle_event(struct tp_dispatch *tp,
575 			     struct tp_touch *t,
576 			     enum tap_event event, uint64_t time)
577 {
578 
579 	switch (event) {
580 	case TAP_EVENT_TOUCH:
581 		tp->tap.state = TAP_STATE_DRAGGING_2;
582 		break;
583 	case TAP_EVENT_RELEASE:
584 		if (tp->tap.drag_lock_enabled) {
585 			tp->tap.state = TAP_STATE_DRAGGING_WAIT;
586 			tp_tap_set_drag_timer(tp, time);
587 		} else {
588 			tp_tap_notify(tp,
589 				      time,
590 				      1,
591 				      LIBINPUT_BUTTON_STATE_RELEASED);
592 			tp->tap.state = TAP_STATE_IDLE;
593 		}
594 		break;
595 	case TAP_EVENT_MOTION:
596 	case TAP_EVENT_TIMEOUT:
597 		/* noop */
598 		break;
599 	case TAP_EVENT_BUTTON:
600 		tp->tap.state = TAP_STATE_DEAD;
601 		tp_tap_notify(tp, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
602 		break;
603 	case TAP_EVENT_THUMB:
604 		break;
605 	case TAP_EVENT_PALM:
606 		tp_tap_notify(tp,
607 			      tp->tap.saved_release_time,
608 			      1,
609 			      LIBINPUT_BUTTON_STATE_RELEASED);
610 		tp->tap.state = TAP_STATE_IDLE;
611 		break;
612 	case TAP_EVENT_PALM_UP:
613 		break;
614 	}
615 }
616 
617 static void
tp_tap_dragging_wait_handle_event(struct tp_dispatch * tp,struct tp_touch * t,enum tap_event event,uint64_t time)618 tp_tap_dragging_wait_handle_event(struct tp_dispatch *tp,
619 				  struct tp_touch *t,
620 				  enum tap_event event, uint64_t time)
621 {
622 
623 	switch (event) {
624 	case TAP_EVENT_TOUCH:
625 		tp->tap.state = TAP_STATE_DRAGGING_OR_TAP;
626 		tp_tap_set_timer(tp, time);
627 		break;
628 	case TAP_EVENT_RELEASE:
629 	case TAP_EVENT_MOTION:
630 		break;
631 	case TAP_EVENT_TIMEOUT:
632 		tp->tap.state = TAP_STATE_IDLE;
633 		tp_tap_notify(tp, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
634 		break;
635 	case TAP_EVENT_BUTTON:
636 		tp->tap.state = TAP_STATE_DEAD;
637 		tp_tap_notify(tp, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
638 		break;
639 	case TAP_EVENT_THUMB:
640 	case TAP_EVENT_PALM:
641 		break;
642 	case TAP_EVENT_PALM_UP:
643 		break;
644 	}
645 }
646 
647 static void
tp_tap_dragging_tap_handle_event(struct tp_dispatch * tp,struct tp_touch * t,enum tap_event event,uint64_t time)648 tp_tap_dragging_tap_handle_event(struct tp_dispatch *tp,
649 				  struct tp_touch *t,
650 				  enum tap_event event, uint64_t time)
651 {
652 
653 	switch (event) {
654 	case TAP_EVENT_TOUCH:
655 		tp->tap.state = TAP_STATE_DRAGGING_2;
656 		tp_tap_clear_timer(tp);
657 		break;
658 	case TAP_EVENT_RELEASE:
659 		tp->tap.state = TAP_STATE_IDLE;
660 		tp_tap_notify(tp, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
661 		break;
662 	case TAP_EVENT_MOTION:
663 	case TAP_EVENT_TIMEOUT:
664 		tp->tap.state = TAP_STATE_DRAGGING;
665 		break;
666 	case TAP_EVENT_BUTTON:
667 		tp->tap.state = TAP_STATE_DEAD;
668 		tp_tap_notify(tp, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
669 		break;
670 	case TAP_EVENT_THUMB:
671 		break;
672 	case TAP_EVENT_PALM:
673 		tp_tap_notify(tp,
674 			      tp->tap.saved_release_time,
675 			      1,
676 			      LIBINPUT_BUTTON_STATE_RELEASED);
677 		tp->tap.state = TAP_STATE_IDLE;
678 		break;
679 	case TAP_EVENT_PALM_UP:
680 		break;
681 	}
682 }
683 
684 static void
tp_tap_dragging2_handle_event(struct tp_dispatch * tp,struct tp_touch * t,enum tap_event event,uint64_t time)685 tp_tap_dragging2_handle_event(struct tp_dispatch *tp,
686 			      struct tp_touch *t,
687 			      enum tap_event event, uint64_t time)
688 {
689 
690 	switch (event) {
691 	case TAP_EVENT_RELEASE:
692 		tp->tap.state = TAP_STATE_DRAGGING;
693 		break;
694 	case TAP_EVENT_TOUCH:
695 		tp->tap.state = TAP_STATE_DEAD;
696 		tp_tap_notify(tp, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
697 		break;
698 	case TAP_EVENT_MOTION:
699 	case TAP_EVENT_TIMEOUT:
700 		/* noop */
701 		break;
702 	case TAP_EVENT_BUTTON:
703 		tp->tap.state = TAP_STATE_DEAD;
704 		tp_tap_notify(tp, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
705 		break;
706 	case TAP_EVENT_THUMB:
707 		break;
708 	case TAP_EVENT_PALM:
709 		tp->tap.state = TAP_STATE_DRAGGING_OR_DOUBLETAP;
710 		break;
711 	case TAP_EVENT_PALM_UP:
712 		break;
713 	}
714 }
715 
716 static void
tp_tap_dead_handle_event(struct tp_dispatch * tp,struct tp_touch * t,enum tap_event event,uint64_t time)717 tp_tap_dead_handle_event(struct tp_dispatch *tp,
718 			 struct tp_touch *t,
719 			 enum tap_event event,
720 			 uint64_t time)
721 {
722 
723 	switch (event) {
724 	case TAP_EVENT_RELEASE:
725 		if (tp->tap.nfingers_down == 0)
726 			tp->tap.state = TAP_STATE_IDLE;
727 		break;
728 	case TAP_EVENT_TOUCH:
729 	case TAP_EVENT_MOTION:
730 	case TAP_EVENT_TIMEOUT:
731 	case TAP_EVENT_BUTTON:
732 		break;
733 	case TAP_EVENT_THUMB:
734 		break;
735 	case TAP_EVENT_PALM:
736 	case TAP_EVENT_PALM_UP:
737 		if (tp->tap.nfingers_down == 0)
738 			tp->tap.state = TAP_STATE_IDLE;
739 		break;
740 	}
741 }
742 
743 static void
tp_tap_handle_event(struct tp_dispatch * tp,struct tp_touch * t,enum tap_event event,uint64_t time)744 tp_tap_handle_event(struct tp_dispatch *tp,
745 		    struct tp_touch *t,
746 		    enum tap_event event,
747 		    uint64_t time)
748 {
749 	enum tp_tap_state current;
750 
751 	current = tp->tap.state;
752 
753 	switch(tp->tap.state) {
754 	case TAP_STATE_IDLE:
755 		tp_tap_idle_handle_event(tp, t, event, time);
756 		break;
757 	case TAP_STATE_TOUCH:
758 		tp_tap_touch_handle_event(tp, t, event, time);
759 		break;
760 	case TAP_STATE_HOLD:
761 		tp_tap_hold_handle_event(tp, t, event, time);
762 		break;
763 	case TAP_STATE_TAPPED:
764 		tp_tap_tapped_handle_event(tp, t, event, time);
765 		break;
766 	case TAP_STATE_TOUCH_2:
767 		tp_tap_touch2_handle_event(tp, t, event, time);
768 		break;
769 	case TAP_STATE_TOUCH_2_HOLD:
770 		tp_tap_touch2_hold_handle_event(tp, t, event, time);
771 		break;
772 	case TAP_STATE_TOUCH_2_RELEASE:
773 		tp_tap_touch2_release_handle_event(tp, t, event, time);
774 		break;
775 	case TAP_STATE_TOUCH_3:
776 		tp_tap_touch3_handle_event(tp, t, event, time);
777 		break;
778 	case TAP_STATE_TOUCH_3_HOLD:
779 		tp_tap_touch3_hold_handle_event(tp, t, event, time);
780 		break;
781 	case TAP_STATE_DRAGGING_OR_DOUBLETAP:
782 		tp_tap_dragging_or_doubletap_handle_event(tp, t, event, time);
783 		break;
784 	case TAP_STATE_DRAGGING:
785 		tp_tap_dragging_handle_event(tp, t, event, time);
786 		break;
787 	case TAP_STATE_DRAGGING_WAIT:
788 		tp_tap_dragging_wait_handle_event(tp, t, event, time);
789 		break;
790 	case TAP_STATE_DRAGGING_OR_TAP:
791 		tp_tap_dragging_tap_handle_event(tp, t, event, time);
792 		break;
793 	case TAP_STATE_DRAGGING_2:
794 		tp_tap_dragging2_handle_event(tp, t, event, time);
795 		break;
796 	case TAP_STATE_DEAD:
797 		tp_tap_dead_handle_event(tp, t, event, time);
798 		break;
799 	}
800 
801 	if (tp->tap.state == TAP_STATE_IDLE || tp->tap.state == TAP_STATE_DEAD)
802 		tp_tap_clear_timer(tp);
803 
804 	if (current != tp->tap.state)
805 		evdev_log_debug(tp->device,
806 			  "tap: touch %d (%s), tap state %s → %s → %s\n",
807 			  t ? (int)t->index : -1,
808 			  t ? touch_state_to_str(t->state) : "",
809 			  tap_state_to_str(current),
810 			  tap_event_to_str(event),
811 			  tap_state_to_str(tp->tap.state));
812 }
813 
814 static bool
tp_tap_exceeds_motion_threshold(struct tp_dispatch * tp,struct tp_touch * t)815 tp_tap_exceeds_motion_threshold(struct tp_dispatch *tp,
816 				struct tp_touch *t)
817 {
818 	struct phys_coords mm =
819 		tp_phys_delta(tp, device_delta(t->point, t->tap.initial));
820 
821 	/* if we have more fingers down than slots, we know that synaptics
822 	 * touchpads are likely to give us pointer jumps.
823 	 * This triggers the movement threshold, making three-finger taps
824 	 * less reliable (#101435)
825 	 *
826 	 * This uses the real nfingers_down, not the one for taps.
827 	 */
828 	if (tp->device->model_flags & EVDEV_MODEL_SYNAPTICS_SERIAL_TOUCHPAD &&
829 	    (tp->nfingers_down > 2 || tp->old_nfingers_down > 2) &&
830 	    (tp->nfingers_down > tp->num_slots ||
831 	     tp->old_nfingers_down > tp->num_slots)) {
832 		return false;
833 	}
834 
835 	/* Semi-mt devices will give us large movements on finger release,
836 	 * depending which touch is released. Make sure we ignore any
837 	 * movement in the same frame as a finger change.
838 	 */
839 	if (tp->semi_mt && tp->nfingers_down != tp->old_nfingers_down)
840 		return false;
841 
842 	return length_in_mm(mm) > DEFAULT_TAP_MOVE_THRESHOLD;
843 }
844 
845 static bool
tp_tap_enabled(struct tp_dispatch * tp)846 tp_tap_enabled(struct tp_dispatch *tp)
847 {
848 	return tp->tap.enabled && !tp->tap.suspended;
849 }
850 
851 int
tp_tap_handle_state(struct tp_dispatch * tp,uint64_t time)852 tp_tap_handle_state(struct tp_dispatch *tp, uint64_t time)
853 {
854 	struct tp_touch *t;
855 	int filter_motion = 0;
856 
857 	if (!tp_tap_enabled(tp))
858 		return 0;
859 
860 	/* Handle queued button pressed events from clickpads. For touchpads
861 	 * with separate physical buttons, ignore button pressed events so they
862 	 * don't interfere with tapping. */
863 	if (tp->buttons.is_clickpad && tp->queued & TOUCHPAD_EVENT_BUTTON_PRESS)
864 		tp_tap_handle_event(tp, NULL, TAP_EVENT_BUTTON, time);
865 
866 	tp_for_each_touch(tp, t) {
867 		if (!t->dirty || t->state == TOUCH_NONE)
868 			continue;
869 
870 		if (tp->buttons.is_clickpad &&
871 		    tp->queued & TOUCHPAD_EVENT_BUTTON_PRESS)
872 			t->tap.state = TAP_TOUCH_STATE_DEAD;
873 
874 		/* If a touch was considered thumb for tapping once, we
875 		 * ignore it for the rest of lifetime */
876 		if (t->tap.is_thumb)
877 			continue;
878 
879 		/* A palm tap needs to be properly relased because we might
880 		 * be who-knows-where in the state machine. Otherwise, we
881 		 * ignore any event from it.
882 		 */
883 		if (t->tap.is_palm) {
884 			if (t->state == TOUCH_END)
885 				tp_tap_handle_event(tp,
886 						    t,
887 						    TAP_EVENT_PALM_UP,
888 						    time);
889 			continue;
890 		}
891 
892 		if (t->state == TOUCH_HOVERING)
893 			continue;
894 
895 		if (t->palm.state != PALM_NONE) {
896 			assert(!t->tap.is_palm);
897 			t->tap.is_palm = true;
898 			t->tap.state = TAP_TOUCH_STATE_DEAD;
899 			if (t->state != TOUCH_BEGIN) {
900 				tp_tap_handle_event(tp, t, TAP_EVENT_PALM, time);
901 				assert(tp->tap.nfingers_down > 0);
902 				tp->tap.nfingers_down--;
903 			}
904 		} else if (t->state == TOUCH_BEGIN) {
905 			/* The simple version: if a touch is a thumb on
906 			 * begin we ignore it. All other thumb touches
907 			 * follow the normal tap state for now */
908 			if (tp_thumb_ignored_for_tap(tp, t)) {
909 				t->tap.is_thumb = true;
910 				continue;
911 			}
912 
913 			t->tap.state = TAP_TOUCH_STATE_TOUCH;
914 			t->tap.initial = t->point;
915 			tp->tap.nfingers_down++;
916 			tp_tap_handle_event(tp, t, TAP_EVENT_TOUCH, time);
917 
918 			/* If we think this is a palm, pretend there's a
919 			 * motion event which will prevent tap clicks
920 			 * without requiring extra states in the FSM.
921 			 */
922 			if (tp_palm_tap_is_palm(tp, t))
923 				tp_tap_handle_event(tp, t, TAP_EVENT_MOTION, time);
924 
925 		} else if (t->state == TOUCH_END) {
926 			if (t->was_down) {
927 				assert(tp->tap.nfingers_down >= 1);
928 				tp->tap.nfingers_down--;
929 				tp_tap_handle_event(tp, t, TAP_EVENT_RELEASE, time);
930 			}
931 			t->tap.state = TAP_TOUCH_STATE_IDLE;
932 		} else if (tp->tap.state != TAP_STATE_IDLE &&
933 			   tp_thumb_ignored(tp, t)) {
934 			tp_tap_handle_event(tp, t, TAP_EVENT_THUMB, time);
935 		} else if (tp->tap.state != TAP_STATE_IDLE &&
936 			   tp_tap_exceeds_motion_threshold(tp, t)) {
937 			struct tp_touch *tmp;
938 
939 			/* Any touch exceeding the threshold turns all
940 			 * touches into DEAD */
941 			tp_for_each_touch(tp, tmp) {
942 				if (tmp->tap.state == TAP_TOUCH_STATE_TOUCH)
943 					tmp->tap.state = TAP_TOUCH_STATE_DEAD;
944 			}
945 
946 			tp_tap_handle_event(tp, t, TAP_EVENT_MOTION, time);
947 		}
948 	}
949 
950 	/**
951 	 * In any state where motion exceeding the move threshold would
952 	 * move to the next state, filter that motion until we actually
953 	 * exceed it. This prevents small motion events while we're waiting
954 	 * on a decision if a tap is a tap.
955 	 */
956 	switch (tp->tap.state) {
957 	case TAP_STATE_TOUCH:
958 	case TAP_STATE_TAPPED:
959 	case TAP_STATE_DRAGGING_OR_DOUBLETAP:
960 	case TAP_STATE_DRAGGING_OR_TAP:
961 	case TAP_STATE_TOUCH_2:
962 	case TAP_STATE_TOUCH_3:
963 		filter_motion = 1;
964 		break;
965 
966 	default:
967 		break;
968 
969 	}
970 
971 	assert(tp->tap.nfingers_down <= tp->nfingers_down);
972 	if (tp->nfingers_down == 0)
973 		assert(tp->tap.nfingers_down == 0);
974 
975 	return filter_motion;
976 }
977 
978 static inline void
tp_tap_update_map(struct tp_dispatch * tp)979 tp_tap_update_map(struct tp_dispatch *tp)
980 {
981 	if (tp->tap.state != TAP_STATE_IDLE)
982 		return;
983 
984 	if (tp->tap.map != tp->tap.want_map)
985 		tp->tap.map = tp->tap.want_map;
986 }
987 
988 void
tp_tap_post_process_state(struct tp_dispatch * tp)989 tp_tap_post_process_state(struct tp_dispatch *tp)
990 {
991 	tp_tap_update_map(tp);
992 }
993 
994 static void
tp_tap_handle_timeout(uint64_t time,void * data)995 tp_tap_handle_timeout(uint64_t time, void *data)
996 {
997 	struct tp_dispatch *tp = data;
998 	struct tp_touch *t;
999 
1000 	tp_tap_handle_event(tp, NULL, TAP_EVENT_TIMEOUT, time);
1001 
1002 	tp_for_each_touch(tp, t) {
1003 		if (t->state == TOUCH_NONE ||
1004 		    t->tap.state == TAP_TOUCH_STATE_IDLE)
1005 			continue;
1006 
1007 		t->tap.state = TAP_TOUCH_STATE_DEAD;
1008 	}
1009 }
1010 
1011 static void
tp_tap_enabled_update(struct tp_dispatch * tp,bool suspended,bool enabled,uint64_t time)1012 tp_tap_enabled_update(struct tp_dispatch *tp, bool suspended, bool enabled, uint64_t time)
1013 {
1014 	bool was_enabled = tp_tap_enabled(tp);
1015 
1016 	tp->tap.suspended = suspended;
1017 	tp->tap.enabled = enabled;
1018 
1019 	if (tp_tap_enabled(tp) == was_enabled)
1020 		return;
1021 
1022 	if (tp_tap_enabled(tp)) {
1023 		struct tp_touch *t;
1024 
1025 		/* On resume, all touches are considered palms */
1026 		tp_for_each_touch(tp, t) {
1027 			if (t->state == TOUCH_NONE)
1028 				continue;
1029 
1030 			t->tap.is_palm = true;
1031 			t->tap.state = TAP_TOUCH_STATE_DEAD;
1032 		}
1033 
1034 		tp->tap.state = TAP_STATE_IDLE;
1035 		tp->tap.nfingers_down = 0;
1036 	} else {
1037 		tp_release_all_taps(tp, time);
1038 	}
1039 }
1040 
1041 static int
tp_tap_config_count(struct libinput_device * device)1042 tp_tap_config_count(struct libinput_device *device)
1043 {
1044 	struct evdev_dispatch *dispatch = evdev_device(device)->dispatch;
1045 	struct tp_dispatch *tp = tp_dispatch(dispatch);
1046 
1047 	return min(tp->ntouches, 3U); /* we only do up to 3 finger tap */
1048 }
1049 
1050 static enum libinput_config_status
tp_tap_config_set_enabled(struct libinput_device * device,enum libinput_config_tap_state enabled)1051 tp_tap_config_set_enabled(struct libinput_device *device,
1052 			  enum libinput_config_tap_state enabled)
1053 {
1054 	struct evdev_dispatch *dispatch = evdev_device(device)->dispatch;
1055 	struct tp_dispatch *tp = tp_dispatch(dispatch);
1056 
1057 	tp_tap_enabled_update(tp, tp->tap.suspended,
1058 			      (enabled == LIBINPUT_CONFIG_TAP_ENABLED),
1059 			      libinput_now(device->seat->libinput));
1060 
1061 	return LIBINPUT_CONFIG_STATUS_SUCCESS;
1062 }
1063 
1064 static enum libinput_config_tap_state
tp_tap_config_is_enabled(struct libinput_device * device)1065 tp_tap_config_is_enabled(struct libinput_device *device)
1066 {
1067 	struct evdev_dispatch *dispatch = evdev_device(device)->dispatch;
1068 	struct tp_dispatch *tp = tp_dispatch(dispatch);
1069 
1070 	return tp->tap.enabled ? LIBINPUT_CONFIG_TAP_ENABLED :
1071 				 LIBINPUT_CONFIG_TAP_DISABLED;
1072 }
1073 
1074 static enum libinput_config_tap_state
tp_tap_default(struct evdev_device * evdev)1075 tp_tap_default(struct evdev_device *evdev)
1076 {
1077 	/**
1078 	 * If we don't have a left button we must have tapping enabled by
1079 	 * default.
1080 	 */
1081 	if (!libevdev_has_event_code(evdev->evdev, EV_KEY, BTN_LEFT))
1082 		return LIBINPUT_CONFIG_TAP_ENABLED;
1083 
1084 	/**
1085 	 * Tapping is disabled by default for two reasons:
1086 	 * * if you don't know that tapping is a thing (or enabled by
1087 	 *   default), you get spurious mouse events that make the desktop
1088 	 *   feel buggy.
1089 	 * * if you do know what tapping is and you want it, you
1090 	 *   usually know where to enable it, or at least you can search for
1091 	 *   it.
1092 	 */
1093 	return LIBINPUT_CONFIG_TAP_DISABLED;
1094 }
1095 
1096 static enum libinput_config_tap_state
tp_tap_config_get_default(struct libinput_device * device)1097 tp_tap_config_get_default(struct libinput_device *device)
1098 {
1099 	struct evdev_device *evdev = evdev_device(device);
1100 
1101 	return tp_tap_default(evdev);
1102 }
1103 
1104 static enum libinput_config_status
tp_tap_config_set_map(struct libinput_device * device,enum libinput_config_tap_button_map map)1105 tp_tap_config_set_map(struct libinput_device *device,
1106 		      enum libinput_config_tap_button_map map)
1107 {
1108 	struct evdev_dispatch *dispatch = evdev_device(device)->dispatch;
1109 	struct tp_dispatch *tp = tp_dispatch(dispatch);
1110 
1111 	tp->tap.want_map = map;
1112 
1113 	tp_tap_update_map(tp);
1114 
1115 	return LIBINPUT_CONFIG_STATUS_SUCCESS;
1116 }
1117 
1118 static enum libinput_config_tap_button_map
tp_tap_config_get_map(struct libinput_device * device)1119 tp_tap_config_get_map(struct libinput_device *device)
1120 {
1121 	struct evdev_dispatch *dispatch = evdev_device(device)->dispatch;
1122 	struct tp_dispatch *tp = tp_dispatch(dispatch);
1123 
1124 	return tp->tap.want_map;
1125 }
1126 
1127 static enum libinput_config_tap_button_map
tp_tap_config_get_default_map(struct libinput_device * device)1128 tp_tap_config_get_default_map(struct libinput_device *device)
1129 {
1130 	return LIBINPUT_CONFIG_TAP_MAP_LRM;
1131 }
1132 
1133 static enum libinput_config_status
tp_tap_config_set_drag_enabled(struct libinput_device * device,enum libinput_config_drag_state enabled)1134 tp_tap_config_set_drag_enabled(struct libinput_device *device,
1135 			       enum libinput_config_drag_state enabled)
1136 {
1137 	struct evdev_dispatch *dispatch = evdev_device(device)->dispatch;
1138 	struct tp_dispatch *tp = tp_dispatch(dispatch);
1139 
1140 	tp->tap.drag_enabled = enabled;
1141 
1142 	return LIBINPUT_CONFIG_STATUS_SUCCESS;
1143 }
1144 
1145 static enum libinput_config_drag_state
tp_tap_config_get_drag_enabled(struct libinput_device * device)1146 tp_tap_config_get_drag_enabled(struct libinput_device *device)
1147 {
1148 	struct evdev_dispatch *dispatch = evdev_device(device)->dispatch;
1149 	struct tp_dispatch *tp = tp_dispatch(dispatch);
1150 
1151 	return tp->tap.drag_enabled;
1152 }
1153 
1154 static inline enum libinput_config_drag_state
tp_drag_default(struct evdev_device * device)1155 tp_drag_default(struct evdev_device *device)
1156 {
1157 	return LIBINPUT_CONFIG_DRAG_ENABLED;
1158 }
1159 
1160 static enum libinput_config_drag_state
tp_tap_config_get_default_drag_enabled(struct libinput_device * device)1161 tp_tap_config_get_default_drag_enabled(struct libinput_device *device)
1162 {
1163 	struct evdev_device *evdev = evdev_device(device);
1164 
1165 	return tp_drag_default(evdev);
1166 }
1167 
1168 static enum libinput_config_status
tp_tap_config_set_draglock_enabled(struct libinput_device * device,enum libinput_config_drag_lock_state enabled)1169 tp_tap_config_set_draglock_enabled(struct libinput_device *device,
1170 				   enum libinput_config_drag_lock_state enabled)
1171 {
1172 	struct evdev_dispatch *dispatch = evdev_device(device)->dispatch;
1173 	struct tp_dispatch *tp = tp_dispatch(dispatch);
1174 
1175 	tp->tap.drag_lock_enabled = enabled;
1176 
1177 	return LIBINPUT_CONFIG_STATUS_SUCCESS;
1178 }
1179 
1180 static enum libinput_config_drag_lock_state
tp_tap_config_get_draglock_enabled(struct libinput_device * device)1181 tp_tap_config_get_draglock_enabled(struct libinput_device *device)
1182 {
1183 	struct evdev_dispatch *dispatch = evdev_device(device)->dispatch;
1184 	struct tp_dispatch *tp = tp_dispatch(dispatch);
1185 
1186 	return tp->tap.drag_lock_enabled;
1187 }
1188 
1189 static inline enum libinput_config_drag_lock_state
tp_drag_lock_default(struct evdev_device * device)1190 tp_drag_lock_default(struct evdev_device *device)
1191 {
1192 	return LIBINPUT_CONFIG_DRAG_LOCK_DISABLED;
1193 }
1194 
1195 static enum libinput_config_drag_lock_state
tp_tap_config_get_default_draglock_enabled(struct libinput_device * device)1196 tp_tap_config_get_default_draglock_enabled(struct libinput_device *device)
1197 {
1198 	struct evdev_device *evdev = evdev_device(device);
1199 
1200 	return tp_drag_lock_default(evdev);
1201 }
1202 
1203 void
tp_init_tap(struct tp_dispatch * tp)1204 tp_init_tap(struct tp_dispatch *tp)
1205 {
1206 	char timer_name[64];
1207 
1208 	tp->tap.config.count = tp_tap_config_count;
1209 	tp->tap.config.set_enabled = tp_tap_config_set_enabled;
1210 	tp->tap.config.get_enabled = tp_tap_config_is_enabled;
1211 	tp->tap.config.get_default = tp_tap_config_get_default;
1212 	tp->tap.config.set_map = tp_tap_config_set_map;
1213 	tp->tap.config.get_map = tp_tap_config_get_map;
1214 	tp->tap.config.get_default_map = tp_tap_config_get_default_map;
1215 	tp->tap.config.set_drag_enabled = tp_tap_config_set_drag_enabled;
1216 	tp->tap.config.get_drag_enabled = tp_tap_config_get_drag_enabled;
1217 	tp->tap.config.get_default_drag_enabled = tp_tap_config_get_default_drag_enabled;
1218 	tp->tap.config.set_draglock_enabled = tp_tap_config_set_draglock_enabled;
1219 	tp->tap.config.get_draglock_enabled = tp_tap_config_get_draglock_enabled;
1220 	tp->tap.config.get_default_draglock_enabled = tp_tap_config_get_default_draglock_enabled;
1221 	tp->device->base.config.tap = &tp->tap.config;
1222 
1223 	tp->tap.state = TAP_STATE_IDLE;
1224 	tp->tap.enabled = tp_tap_default(tp->device);
1225 	tp->tap.map = LIBINPUT_CONFIG_TAP_MAP_LRM;
1226 	tp->tap.want_map = tp->tap.map;
1227 	tp->tap.drag_enabled = tp_drag_default(tp->device);
1228 	tp->tap.drag_lock_enabled = tp_drag_lock_default(tp->device);
1229 
1230 	snprintf(timer_name,
1231 		 sizeof(timer_name),
1232 		 "%s tap",
1233 		 evdev_device_get_sysname(tp->device));
1234 	libinput_timer_init(&tp->tap.timer,
1235 			    tp_libinput_context(tp),
1236 			    timer_name,
1237 			    tp_tap_handle_timeout, tp);
1238 }
1239 
1240 void
tp_remove_tap(struct tp_dispatch * tp)1241 tp_remove_tap(struct tp_dispatch *tp)
1242 {
1243 	libinput_timer_cancel(&tp->tap.timer);
1244 }
1245 
1246 void
tp_release_all_taps(struct tp_dispatch * tp,uint64_t now)1247 tp_release_all_taps(struct tp_dispatch *tp, uint64_t now)
1248 {
1249 	struct tp_touch *t;
1250 	int i;
1251 
1252 	for (i = 1; i <= 3; i++) {
1253 		if (tp->tap.buttons_pressed & (1 << i))
1254 			tp_tap_notify(tp, now, i, LIBINPUT_BUTTON_STATE_RELEASED);
1255 	}
1256 
1257 	/* To neutralize all current touches, we make them all palms */
1258 	tp_for_each_touch(tp, t) {
1259 		if (t->state == TOUCH_NONE)
1260 			continue;
1261 
1262 		if (t->tap.is_palm)
1263 			continue;
1264 
1265 		t->tap.is_palm = true;
1266 		t->tap.state = TAP_TOUCH_STATE_DEAD;
1267 	}
1268 
1269 	tp->tap.state = TAP_STATE_IDLE;
1270 	tp->tap.nfingers_down = 0;
1271 }
1272 
1273 void
tp_tap_suspend(struct tp_dispatch * tp,uint64_t time)1274 tp_tap_suspend(struct tp_dispatch *tp, uint64_t time)
1275 {
1276 	tp_tap_enabled_update(tp, true, tp->tap.enabled, time);
1277 }
1278 
1279 void
tp_tap_resume(struct tp_dispatch * tp,uint64_t time)1280 tp_tap_resume(struct tp_dispatch *tp, uint64_t time)
1281 {
1282 	tp_tap_enabled_update(tp, false, tp->tap.enabled, time);
1283 }
1284 
1285 bool
tp_tap_dragging(const struct tp_dispatch * tp)1286 tp_tap_dragging(const struct tp_dispatch *tp)
1287 {
1288 	switch (tp->tap.state) {
1289 	case TAP_STATE_DRAGGING:
1290 	case TAP_STATE_DRAGGING_2:
1291 	case TAP_STATE_DRAGGING_WAIT:
1292 	case TAP_STATE_DRAGGING_OR_TAP:
1293 		return true;
1294 	default:
1295 		return false;
1296 	}
1297 }
1298