2 * Copyright © 2013 Red Hat, Inc.
4 * Permission to use, copy, modify, distribute, and sell this software
5 * and its documentation for any purpose is hereby granted without
6 * fee, provided that the above copyright notice appear in all copies
7 * and that both that copyright notice and this permission notice
8 * appear in supporting documentation, and that the name of Red Hat
9 * not be used in advertising or publicity pertaining to distribution
10 * of the software without specific, written prior permission. Red
11 * Hat makes no representations about the suitability of this software
12 * for any purpose. It is provided "as is" without express or implied
15 * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
17 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
19 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
20 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
35 #include "evdev-mt-touchpad.h"
37 #define CASE_RETURN_STRING(a) case a: return #a;
39 #define DEFAULT_TAP_TIMEOUT_PERIOD 180
40 #define DEFAULT_TAP_MOVE_THRESHOLD 30
50 /*****************************************
51 * DO NOT EDIT THIS FILE!
53 * Look at the state diagram in doc/touchpad-tap-state-machine.svg, or
55 * https://drive.google.com/file/d/0B1NwWmji69noYTdMcU1kTUZuUVE/edit?usp=sharing
56 * (it's a http://draw.io diagram)
58 * Any changes in this file must be represented in the diagram.
61 static inline const char*
62 tap_state_to_str(enum tp_tap_state state) {
65 CASE_RETURN_STRING(TAP_STATE_IDLE);
66 CASE_RETURN_STRING(TAP_STATE_HOLD);
67 CASE_RETURN_STRING(TAP_STATE_TOUCH);
68 CASE_RETURN_STRING(TAP_STATE_TAPPED);
69 CASE_RETURN_STRING(TAP_STATE_TOUCH_2);
70 CASE_RETURN_STRING(TAP_STATE_TOUCH_2_HOLD);
71 CASE_RETURN_STRING(TAP_STATE_TOUCH_3);
72 CASE_RETURN_STRING(TAP_STATE_TOUCH_3_HOLD);
73 CASE_RETURN_STRING(TAP_STATE_DRAGGING);
74 CASE_RETURN_STRING(TAP_STATE_DRAGGING_WAIT);
75 CASE_RETURN_STRING(TAP_STATE_DRAGGING_OR_DOUBLETAP);
76 CASE_RETURN_STRING(TAP_STATE_DRAGGING_2);
77 CASE_RETURN_STRING(TAP_STATE_DEAD);
82 static inline const char*
83 tap_event_to_str(enum tap_event event) {
86 CASE_RETURN_STRING(TAP_EVENT_TOUCH);
87 CASE_RETURN_STRING(TAP_EVENT_MOTION);
88 CASE_RETURN_STRING(TAP_EVENT_RELEASE);
89 CASE_RETURN_STRING(TAP_EVENT_TIMEOUT);
90 CASE_RETURN_STRING(TAP_EVENT_BUTTON);
94 #undef CASE_RETURN_STRING
97 tp_tap_notify(struct tp_dispatch *tp,
101 enum libinput_button_state state)
105 if (t && t->tap.state == TAP_TOUCH_STATE_DEAD)
109 case 1: button = BTN_LEFT; break;
110 case 2: button = BTN_RIGHT; break;
111 case 3: button = BTN_MIDDLE; break;
116 evdev_pointer_notify_button(tp->device,
123 tp_tap_set_timer(struct tp_dispatch *tp, uint64_t time)
125 libinput_timer_set(&tp->tap.timer, time + DEFAULT_TAP_TIMEOUT_PERIOD);
129 tp_tap_clear_timer(struct tp_dispatch *tp)
131 libinput_timer_cancel(&tp->tap.timer);
135 tp_tap_idle_handle_event(struct tp_dispatch *tp,
137 enum tap_event event, uint64_t time)
139 struct libinput *libinput = tp->device->base.seat->libinput;
142 case TAP_EVENT_TOUCH:
143 tp->tap.state = TAP_STATE_TOUCH;
144 tp_tap_set_timer(tp, time);
146 case TAP_EVENT_RELEASE:
147 case TAP_EVENT_MOTION:
148 log_bug_libinput(libinput,
149 "invalid event, no fingers are down\n");
151 case TAP_EVENT_TIMEOUT:
153 case TAP_EVENT_BUTTON:
154 tp->tap.state = TAP_STATE_DEAD;
160 tp_tap_touch_handle_event(struct tp_dispatch *tp,
162 enum tap_event event, uint64_t time)
166 case TAP_EVENT_TOUCH:
167 tp->tap.state = TAP_STATE_TOUCH_2;
168 tp_tap_set_timer(tp, time);
170 case TAP_EVENT_RELEASE:
171 tp->tap.state = TAP_STATE_TAPPED;
172 tp_tap_notify(tp, t, time, 1, LIBINPUT_BUTTON_STATE_PRESSED);
173 tp_tap_set_timer(tp, time);
175 case TAP_EVENT_TIMEOUT:
176 case TAP_EVENT_MOTION:
177 tp->tap.state = TAP_STATE_HOLD;
178 tp_tap_clear_timer(tp);
180 case TAP_EVENT_BUTTON:
181 tp->tap.state = TAP_STATE_DEAD;
187 tp_tap_hold_handle_event(struct tp_dispatch *tp,
189 enum tap_event event, uint64_t time)
193 case TAP_EVENT_TOUCH:
194 tp->tap.state = TAP_STATE_TOUCH_2;
195 tp_tap_set_timer(tp, time);
197 case TAP_EVENT_RELEASE:
198 tp->tap.state = TAP_STATE_IDLE;
200 case TAP_EVENT_MOTION:
201 case TAP_EVENT_TIMEOUT:
203 case TAP_EVENT_BUTTON:
204 tp->tap.state = TAP_STATE_DEAD;
210 tp_tap_tapped_handle_event(struct tp_dispatch *tp,
212 enum tap_event event, uint64_t time)
214 struct libinput *libinput = tp->device->base.seat->libinput;
217 case TAP_EVENT_MOTION:
218 case TAP_EVENT_RELEASE:
219 log_bug_libinput(libinput,
220 "invalid event when fingers are up\n");
222 case TAP_EVENT_TOUCH:
223 tp->tap.state = TAP_STATE_DRAGGING_OR_DOUBLETAP;
224 tp_tap_clear_timer(tp);
226 case TAP_EVENT_TIMEOUT:
227 tp->tap.state = TAP_STATE_IDLE;
228 tp_tap_notify(tp, t, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
230 case TAP_EVENT_BUTTON:
231 tp->tap.state = TAP_STATE_DEAD;
232 tp_tap_notify(tp, t, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
238 tp_tap_touch2_handle_event(struct tp_dispatch *tp,
240 enum tap_event event, uint64_t time)
244 case TAP_EVENT_TOUCH:
245 tp->tap.state = TAP_STATE_TOUCH_3;
246 tp_tap_set_timer(tp, time);
248 case TAP_EVENT_RELEASE:
249 tp->tap.state = TAP_STATE_HOLD;
250 tp_tap_notify(tp, t, time, 2, LIBINPUT_BUTTON_STATE_PRESSED);
251 tp_tap_notify(tp, t, time, 2, LIBINPUT_BUTTON_STATE_RELEASED);
252 tp_tap_clear_timer(tp);
254 case TAP_EVENT_MOTION:
255 tp_tap_clear_timer(tp);
257 case TAP_EVENT_TIMEOUT:
258 tp->tap.state = TAP_STATE_TOUCH_2_HOLD;
260 case TAP_EVENT_BUTTON:
261 tp->tap.state = TAP_STATE_DEAD;
267 tp_tap_touch2_hold_handle_event(struct tp_dispatch *tp,
269 enum tap_event event, uint64_t time)
273 case TAP_EVENT_TOUCH:
274 tp->tap.state = TAP_STATE_TOUCH_3;
275 tp_tap_set_timer(tp, time);
277 case TAP_EVENT_RELEASE:
278 tp->tap.state = TAP_STATE_HOLD;
280 case TAP_EVENT_MOTION:
281 case TAP_EVENT_TIMEOUT:
282 tp->tap.state = TAP_STATE_TOUCH_2_HOLD;
284 case TAP_EVENT_BUTTON:
285 tp->tap.state = TAP_STATE_DEAD;
291 tp_tap_touch3_handle_event(struct tp_dispatch *tp,
293 enum tap_event event, uint64_t time)
297 case TAP_EVENT_TOUCH:
298 tp->tap.state = TAP_STATE_DEAD;
299 tp_tap_clear_timer(tp);
301 case TAP_EVENT_MOTION:
302 case TAP_EVENT_TIMEOUT:
303 tp->tap.state = TAP_STATE_TOUCH_3_HOLD;
304 tp_tap_clear_timer(tp);
306 case TAP_EVENT_RELEASE:
307 tp->tap.state = TAP_STATE_TOUCH_2_HOLD;
308 tp_tap_notify(tp, t, time, 3, LIBINPUT_BUTTON_STATE_PRESSED);
309 tp_tap_notify(tp, t, time, 3, LIBINPUT_BUTTON_STATE_RELEASED);
311 case TAP_EVENT_BUTTON:
312 tp->tap.state = TAP_STATE_DEAD;
318 tp_tap_touch3_hold_handle_event(struct tp_dispatch *tp,
320 enum tap_event event, uint64_t time)
324 case TAP_EVENT_TOUCH:
325 tp->tap.state = TAP_STATE_DEAD;
326 tp_tap_set_timer(tp, time);
328 case TAP_EVENT_RELEASE:
329 tp->tap.state = TAP_STATE_TOUCH_2_HOLD;
331 case TAP_EVENT_MOTION:
332 case TAP_EVENT_TIMEOUT:
334 case TAP_EVENT_BUTTON:
335 tp->tap.state = TAP_STATE_DEAD;
341 tp_tap_dragging_or_doubletap_handle_event(struct tp_dispatch *tp,
343 enum tap_event event, uint64_t time)
346 case TAP_EVENT_TOUCH:
347 tp->tap.state = TAP_STATE_DRAGGING_2;
349 case TAP_EVENT_RELEASE:
350 tp->tap.state = TAP_STATE_IDLE;
351 tp_tap_notify(tp, t, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
352 tp_tap_notify(tp, t, time, 1, LIBINPUT_BUTTON_STATE_PRESSED);
353 tp_tap_notify(tp, t, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
354 tp_tap_clear_timer(tp);
356 case TAP_EVENT_MOTION:
357 case TAP_EVENT_TIMEOUT:
358 tp->tap.state = TAP_STATE_DRAGGING;
360 case TAP_EVENT_BUTTON:
361 tp->tap.state = TAP_STATE_DEAD;
362 tp_tap_notify(tp, t, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
368 tp_tap_dragging_handle_event(struct tp_dispatch *tp,
370 enum tap_event event, uint64_t time)
374 case TAP_EVENT_TOUCH:
375 tp->tap.state = TAP_STATE_DRAGGING_2;
377 case TAP_EVENT_RELEASE:
378 tp->tap.state = TAP_STATE_DRAGGING_WAIT;
379 tp_tap_set_timer(tp, time);
381 case TAP_EVENT_MOTION:
382 case TAP_EVENT_TIMEOUT:
385 case TAP_EVENT_BUTTON:
386 tp->tap.state = TAP_STATE_DEAD;
387 tp_tap_notify(tp, t, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
393 tp_tap_dragging_wait_handle_event(struct tp_dispatch *tp,
395 enum tap_event event, uint64_t time)
399 case TAP_EVENT_TOUCH:
400 tp->tap.state = TAP_STATE_DRAGGING;
401 tp_tap_clear_timer(tp);
403 case TAP_EVENT_RELEASE:
404 case TAP_EVENT_MOTION:
406 case TAP_EVENT_TIMEOUT:
407 tp->tap.state = TAP_STATE_IDLE;
408 tp_tap_notify(tp, t, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
410 case TAP_EVENT_BUTTON:
411 tp->tap.state = TAP_STATE_DEAD;
412 tp_tap_notify(tp, t, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
418 tp_tap_dragging2_handle_event(struct tp_dispatch *tp,
420 enum tap_event event, uint64_t time)
424 case TAP_EVENT_RELEASE:
425 tp->tap.state = TAP_STATE_DRAGGING;
427 case TAP_EVENT_TOUCH:
428 tp->tap.state = TAP_STATE_DEAD;
429 tp_tap_notify(tp, t, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
431 case TAP_EVENT_MOTION:
432 case TAP_EVENT_TIMEOUT:
435 case TAP_EVENT_BUTTON:
436 tp->tap.state = TAP_STATE_DEAD;
437 tp_tap_notify(tp, t, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
443 tp_tap_dead_handle_event(struct tp_dispatch *tp,
445 enum tap_event event,
450 case TAP_EVENT_RELEASE:
451 if (tp->nfingers_down == 0)
452 tp->tap.state = TAP_STATE_IDLE;
454 case TAP_EVENT_TOUCH:
455 case TAP_EVENT_MOTION:
456 case TAP_EVENT_TIMEOUT:
457 case TAP_EVENT_BUTTON:
463 tp_tap_handle_event(struct tp_dispatch *tp,
465 enum tap_event event,
468 struct libinput *libinput = tp->device->base.seat->libinput;
469 enum tp_tap_state current;
471 current = tp->tap.state;
473 switch(tp->tap.state) {
475 if (!tp->tap.enabled)
478 tp_tap_idle_handle_event(tp, t, event, time);
480 case TAP_STATE_TOUCH:
481 tp_tap_touch_handle_event(tp, t, event, time);
484 tp_tap_hold_handle_event(tp, t, event, time);
486 case TAP_STATE_TAPPED:
487 tp_tap_tapped_handle_event(tp, t, event, time);
489 case TAP_STATE_TOUCH_2:
490 tp_tap_touch2_handle_event(tp, t, event, time);
492 case TAP_STATE_TOUCH_2_HOLD:
493 tp_tap_touch2_hold_handle_event(tp, t, event, time);
495 case TAP_STATE_TOUCH_3:
496 tp_tap_touch3_handle_event(tp, t, event, time);
498 case TAP_STATE_TOUCH_3_HOLD:
499 tp_tap_touch3_hold_handle_event(tp, t, event, time);
501 case TAP_STATE_DRAGGING_OR_DOUBLETAP:
502 tp_tap_dragging_or_doubletap_handle_event(tp, t, event, time);
504 case TAP_STATE_DRAGGING:
505 tp_tap_dragging_handle_event(tp, t, event, time);
507 case TAP_STATE_DRAGGING_WAIT:
508 tp_tap_dragging_wait_handle_event(tp, t, event, time);
510 case TAP_STATE_DRAGGING_2:
511 tp_tap_dragging2_handle_event(tp, t, event, time);
514 tp_tap_dead_handle_event(tp, t, event, time);
518 if (tp->tap.state == TAP_STATE_IDLE || tp->tap.state == TAP_STATE_DEAD)
519 tp_tap_clear_timer(tp);
522 "tap state: %s → %s → %s\n",
523 tap_state_to_str(current),
524 tap_event_to_str(event),
525 tap_state_to_str(tp->tap.state));
529 tp_tap_exceeds_motion_threshold(struct tp_dispatch *tp, struct tp_touch *t)
531 int threshold = DEFAULT_TAP_MOVE_THRESHOLD;
534 tp_get_delta(t, &dx, &dy);
536 return dx * dx + dy * dy > threshold * threshold;
540 tp_tap_handle_state(struct tp_dispatch *tp, uint64_t time)
543 int filter_motion = 0;
545 if (tp->queued & TOUCHPAD_EVENT_BUTTON_PRESS)
546 tp_tap_handle_event(tp, NULL, TAP_EVENT_BUTTON, time);
548 tp_for_each_touch(tp, t) {
549 if (!t->dirty || t->state == TOUCH_NONE)
552 if (tp->queued & TOUCHPAD_EVENT_BUTTON_PRESS)
553 t->tap.state = TAP_TOUCH_STATE_DEAD;
555 if (t->state == TOUCH_BEGIN) {
556 t->tap.state = TAP_TOUCH_STATE_TOUCH;
557 tp_tap_handle_event(tp, t, TAP_EVENT_TOUCH, time);
558 } else if (t->state == TOUCH_END) {
559 tp_tap_handle_event(tp, t, TAP_EVENT_RELEASE, time);
560 t->tap.state = TAP_TOUCH_STATE_DEAD;
561 } else if (tp->tap.state != TAP_STATE_IDLE &&
562 tp_tap_exceeds_motion_threshold(tp, t)) {
563 struct tp_touch *tmp;
565 /* Any touch exceeding the threshold turns all
566 * touches into DEAD */
567 tp_for_each_touch(tp, tmp) {
568 if (tmp->tap.state == TAP_TOUCH_STATE_TOUCH)
569 tmp->tap.state = TAP_TOUCH_STATE_DEAD;
572 tp_tap_handle_event(tp, t, TAP_EVENT_MOTION, time);
577 * In any state where motion exceeding the move threshold would
578 * move to the next state, filter that motion until we actually
579 * exceed it. This prevents small motion events while we're waiting
580 * on a decision if a tap is a tap.
582 switch (tp->tap.state) {
583 case TAP_STATE_TOUCH:
584 case TAP_STATE_TAPPED:
585 case TAP_STATE_DRAGGING_OR_DOUBLETAP:
586 case TAP_STATE_TOUCH_2:
587 case TAP_STATE_TOUCH_3:
596 return filter_motion;
600 tp_tap_handle_timeout(uint64_t time, void *data)
602 struct tp_dispatch *tp = data;
605 tp_tap_handle_event(tp, NULL, TAP_EVENT_TIMEOUT, time);
607 tp_for_each_touch(tp, t) {
608 if (t->state == TOUCH_NONE ||
609 t->tap.state == TAP_TOUCH_STATE_IDLE)
612 t->tap.state = TAP_TOUCH_STATE_DEAD;
617 tp_tap_config_count(struct libinput_device *device)
619 struct evdev_dispatch *dispatch;
620 struct tp_dispatch *tp;
622 dispatch = ((struct evdev_device *) device)->dispatch;
623 tp = container_of(dispatch, tp, base);
625 return min(tp->ntouches, 3); /* we only do up to 3 finger tap */
628 static enum libinput_config_status
629 tp_tap_config_set_enabled(struct libinput_device *device,
630 enum libinput_config_tap_state enabled)
632 struct evdev_dispatch *dispatch;
633 struct tp_dispatch *tp;
635 dispatch = ((struct evdev_device *) device)->dispatch;
636 tp = container_of(dispatch, tp, base);
638 tp->tap.enabled = (enabled == LIBINPUT_CONFIG_TAP_ENABLED);
640 return LIBINPUT_CONFIG_STATUS_SUCCESS;
643 static enum libinput_config_tap_state
644 tp_tap_config_is_enabled(struct libinput_device *device)
646 struct evdev_dispatch *dispatch;
647 struct tp_dispatch *tp;
649 dispatch = ((struct evdev_device *) device)->dispatch;
650 tp = container_of(dispatch, tp, base);
652 return tp->tap.enabled ? LIBINPUT_CONFIG_TAP_ENABLED :
653 LIBINPUT_CONFIG_TAP_DISABLED;
656 static enum libinput_config_tap_state
657 tp_tap_config_get_default(struct libinput_device *device)
660 * Tapping is disabled by default for two reasons:
661 * * if you don't know that tapping is a thing (or enabled by
662 * default), you get spurious mouse events that make the desktop
664 * * if you do know what tapping is and you want it, you
665 * usually know where to enable it, or at least you can search for
668 return LIBINPUT_CONFIG_TAP_DISABLED;
672 tp_init_tap(struct tp_dispatch *tp)
674 tp->tap.config.count = tp_tap_config_count;
675 tp->tap.config.set_enabled = tp_tap_config_set_enabled;
676 tp->tap.config.get_enabled = tp_tap_config_is_enabled;
677 tp->tap.config.get_default = tp_tap_config_get_default;
678 tp->device->base.config.tap = &tp->tap.config;
680 tp->tap.state = TAP_STATE_IDLE;
682 libinput_timer_init(&tp->tap.timer,
683 tp->device->base.seat->libinput,
684 tp_tap_handle_timeout, tp);
690 tp_destroy_tap(struct tp_dispatch *tp)
692 libinput_timer_cancel(&tp->tap.timer);