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);
256 case TAP_EVENT_TIMEOUT:
257 tp->tap.state = TAP_STATE_TOUCH_2_HOLD;
259 case TAP_EVENT_BUTTON:
260 tp->tap.state = TAP_STATE_DEAD;
266 tp_tap_touch2_hold_handle_event(struct tp_dispatch *tp,
268 enum tap_event event, uint64_t time)
272 case TAP_EVENT_TOUCH:
273 tp->tap.state = TAP_STATE_TOUCH_3;
274 tp_tap_set_timer(tp, time);
276 case TAP_EVENT_RELEASE:
277 tp->tap.state = TAP_STATE_HOLD;
279 case TAP_EVENT_MOTION:
280 case TAP_EVENT_TIMEOUT:
281 tp->tap.state = TAP_STATE_TOUCH_2_HOLD;
283 case TAP_EVENT_BUTTON:
284 tp->tap.state = TAP_STATE_DEAD;
290 tp_tap_touch3_handle_event(struct tp_dispatch *tp,
292 enum tap_event event, uint64_t time)
296 case TAP_EVENT_TOUCH:
297 tp->tap.state = TAP_STATE_DEAD;
298 tp_tap_clear_timer(tp);
300 case TAP_EVENT_MOTION:
301 case TAP_EVENT_TIMEOUT:
302 tp->tap.state = TAP_STATE_TOUCH_3_HOLD;
303 tp_tap_clear_timer(tp);
305 case TAP_EVENT_RELEASE:
306 tp->tap.state = TAP_STATE_TOUCH_2_HOLD;
307 tp_tap_notify(tp, t, time, 3, LIBINPUT_BUTTON_STATE_PRESSED);
308 tp_tap_notify(tp, t, time, 3, LIBINPUT_BUTTON_STATE_RELEASED);
310 case TAP_EVENT_BUTTON:
311 tp->tap.state = TAP_STATE_DEAD;
317 tp_tap_touch3_hold_handle_event(struct tp_dispatch *tp,
319 enum tap_event event, uint64_t time)
323 case TAP_EVENT_TOUCH:
324 tp->tap.state = TAP_STATE_DEAD;
325 tp_tap_set_timer(tp, time);
327 case TAP_EVENT_RELEASE:
328 tp->tap.state = TAP_STATE_TOUCH_2_HOLD;
330 case TAP_EVENT_MOTION:
331 case TAP_EVENT_TIMEOUT:
333 case TAP_EVENT_BUTTON:
334 tp->tap.state = TAP_STATE_DEAD;
340 tp_tap_dragging_or_doubletap_handle_event(struct tp_dispatch *tp,
342 enum tap_event event, uint64_t time)
345 case TAP_EVENT_TOUCH:
346 tp->tap.state = TAP_STATE_DRAGGING_2;
348 case TAP_EVENT_RELEASE:
349 tp->tap.state = TAP_STATE_IDLE;
350 tp_tap_notify(tp, t, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
351 tp_tap_notify(tp, t, time, 1, LIBINPUT_BUTTON_STATE_PRESSED);
352 tp_tap_notify(tp, t, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
353 tp_tap_clear_timer(tp);
355 case TAP_EVENT_MOTION:
356 case TAP_EVENT_TIMEOUT:
357 tp->tap.state = TAP_STATE_DRAGGING;
359 case TAP_EVENT_BUTTON:
360 tp->tap.state = TAP_STATE_DEAD;
361 tp_tap_notify(tp, t, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
367 tp_tap_dragging_handle_event(struct tp_dispatch *tp,
369 enum tap_event event, uint64_t time)
373 case TAP_EVENT_TOUCH:
374 tp->tap.state = TAP_STATE_DRAGGING_2;
376 case TAP_EVENT_RELEASE:
377 tp->tap.state = TAP_STATE_DRAGGING_WAIT;
378 tp_tap_set_timer(tp, time);
380 case TAP_EVENT_MOTION:
381 case TAP_EVENT_TIMEOUT:
384 case TAP_EVENT_BUTTON:
385 tp->tap.state = TAP_STATE_DEAD;
386 tp_tap_notify(tp, t, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
392 tp_tap_dragging_wait_handle_event(struct tp_dispatch *tp,
394 enum tap_event event, uint64_t time)
398 case TAP_EVENT_TOUCH:
399 tp->tap.state = TAP_STATE_DRAGGING;
400 tp_tap_clear_timer(tp);
402 case TAP_EVENT_RELEASE:
403 case TAP_EVENT_MOTION:
405 case TAP_EVENT_TIMEOUT:
406 tp->tap.state = TAP_STATE_IDLE;
407 tp_tap_notify(tp, t, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
409 case TAP_EVENT_BUTTON:
410 tp->tap.state = TAP_STATE_DEAD;
411 tp_tap_notify(tp, t, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
417 tp_tap_dragging2_handle_event(struct tp_dispatch *tp,
419 enum tap_event event, uint64_t time)
423 case TAP_EVENT_RELEASE:
424 tp->tap.state = TAP_STATE_DRAGGING;
426 case TAP_EVENT_TOUCH:
427 tp->tap.state = TAP_STATE_DEAD;
428 tp_tap_notify(tp, t, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
430 case TAP_EVENT_MOTION:
431 case TAP_EVENT_TIMEOUT:
434 case TAP_EVENT_BUTTON:
435 tp->tap.state = TAP_STATE_DEAD;
436 tp_tap_notify(tp, t, time, 1, LIBINPUT_BUTTON_STATE_RELEASED);
442 tp_tap_dead_handle_event(struct tp_dispatch *tp,
444 enum tap_event event,
449 case TAP_EVENT_RELEASE:
450 if (tp->nfingers_down == 0)
451 tp->tap.state = TAP_STATE_IDLE;
453 case TAP_EVENT_TOUCH:
454 case TAP_EVENT_MOTION:
455 case TAP_EVENT_TIMEOUT:
456 case TAP_EVENT_BUTTON:
462 tp_tap_handle_event(struct tp_dispatch *tp,
464 enum tap_event event,
467 struct libinput *libinput = tp->device->base.seat->libinput;
468 enum tp_tap_state current;
470 current = tp->tap.state;
472 switch(tp->tap.state) {
474 if (!tp->tap.enabled)
477 tp_tap_idle_handle_event(tp, t, event, time);
479 case TAP_STATE_TOUCH:
480 tp_tap_touch_handle_event(tp, t, event, time);
483 tp_tap_hold_handle_event(tp, t, event, time);
485 case TAP_STATE_TAPPED:
486 tp_tap_tapped_handle_event(tp, t, event, time);
488 case TAP_STATE_TOUCH_2:
489 tp_tap_touch2_handle_event(tp, t, event, time);
491 case TAP_STATE_TOUCH_2_HOLD:
492 tp_tap_touch2_hold_handle_event(tp, t, event, time);
494 case TAP_STATE_TOUCH_3:
495 tp_tap_touch3_handle_event(tp, t, event, time);
497 case TAP_STATE_TOUCH_3_HOLD:
498 tp_tap_touch3_hold_handle_event(tp, t, event, time);
500 case TAP_STATE_DRAGGING_OR_DOUBLETAP:
501 tp_tap_dragging_or_doubletap_handle_event(tp, t, event, time);
503 case TAP_STATE_DRAGGING:
504 tp_tap_dragging_handle_event(tp, t, event, time);
506 case TAP_STATE_DRAGGING_WAIT:
507 tp_tap_dragging_wait_handle_event(tp, t, event, time);
509 case TAP_STATE_DRAGGING_2:
510 tp_tap_dragging2_handle_event(tp, t, event, time);
513 tp_tap_dead_handle_event(tp, t, event, time);
517 if (tp->tap.state == TAP_STATE_IDLE || tp->tap.state == TAP_STATE_DEAD)
518 tp_tap_clear_timer(tp);
521 "tap state: %s → %s → %s\n",
522 tap_state_to_str(current),
523 tap_event_to_str(event),
524 tap_state_to_str(tp->tap.state));
528 tp_tap_exceeds_motion_threshold(struct tp_dispatch *tp, struct tp_touch *t)
530 int threshold = DEFAULT_TAP_MOVE_THRESHOLD;
533 tp_get_delta(t, &dx, &dy);
535 return dx * dx + dy * dy > threshold * threshold;
539 tp_tap_handle_state(struct tp_dispatch *tp, uint64_t time)
542 int filter_motion = 0;
544 if (tp->queued & TOUCHPAD_EVENT_BUTTON_PRESS)
545 tp_tap_handle_event(tp, NULL, TAP_EVENT_BUTTON, time);
547 tp_for_each_touch(tp, t) {
548 if (!t->dirty || t->state == TOUCH_NONE)
551 if (tp->queued & TOUCHPAD_EVENT_BUTTON_PRESS)
552 t->tap.state = TAP_TOUCH_STATE_DEAD;
554 if (t->state == TOUCH_BEGIN) {
555 t->tap.state = TAP_TOUCH_STATE_TOUCH;
556 tp_tap_handle_event(tp, t, TAP_EVENT_TOUCH, time);
557 } else if (t->state == TOUCH_END) {
558 tp_tap_handle_event(tp, t, TAP_EVENT_RELEASE, time);
559 t->tap.state = TAP_TOUCH_STATE_DEAD;
560 } else if (tp->tap.state != TAP_STATE_IDLE &&
561 tp_tap_exceeds_motion_threshold(tp, t)) {
562 struct tp_touch *tmp;
564 /* Any touch exceeding the threshold turns all
565 * touches into DEAD */
566 tp_for_each_touch(tp, tmp) {
567 if (tmp->tap.state == TAP_TOUCH_STATE_TOUCH)
568 tmp->tap.state = TAP_TOUCH_STATE_DEAD;
571 tp_tap_handle_event(tp, t, TAP_EVENT_MOTION, time);
576 * In any state where motion exceeding the move threshold would
577 * move to the next state, filter that motion until we actually
578 * exceed it. This prevents small motion events while we're waiting
579 * on a decision if a tap is a tap.
581 switch (tp->tap.state) {
582 case TAP_STATE_TOUCH:
583 case TAP_STATE_TAPPED:
584 case TAP_STATE_DRAGGING_OR_DOUBLETAP:
585 case TAP_STATE_TOUCH_2:
586 case TAP_STATE_TOUCH_3:
595 return filter_motion;
599 tp_tap_handle_timeout(uint64_t time, void *data)
601 struct tp_dispatch *tp = data;
604 tp_tap_handle_event(tp, NULL, TAP_EVENT_TIMEOUT, time);
606 tp_for_each_touch(tp, t) {
607 if (t->state == TOUCH_NONE ||
608 t->tap.state == TAP_TOUCH_STATE_IDLE)
611 t->tap.state = TAP_TOUCH_STATE_DEAD;
616 tp_tap_config_count(struct libinput_device *device)
618 struct evdev_dispatch *dispatch;
619 struct tp_dispatch *tp;
621 dispatch = ((struct evdev_device *) device)->dispatch;
622 tp = container_of(dispatch, tp, base);
624 return min(tp->ntouches, 3); /* we only do up to 3 finger tap */
627 static enum libinput_config_status
628 tp_tap_config_set_enabled(struct libinput_device *device,
629 enum libinput_config_tap_state enabled)
631 struct evdev_dispatch *dispatch;
632 struct tp_dispatch *tp;
634 dispatch = ((struct evdev_device *) device)->dispatch;
635 tp = container_of(dispatch, tp, base);
637 tp->tap.enabled = (enabled == LIBINPUT_CONFIG_TAP_ENABLED);
639 return LIBINPUT_CONFIG_STATUS_SUCCESS;
642 static enum libinput_config_tap_state
643 tp_tap_config_is_enabled(struct libinput_device *device)
645 struct evdev_dispatch *dispatch;
646 struct tp_dispatch *tp;
648 dispatch = ((struct evdev_device *) device)->dispatch;
649 tp = container_of(dispatch, tp, base);
651 return tp->tap.enabled ? LIBINPUT_CONFIG_TAP_ENABLED :
652 LIBINPUT_CONFIG_TAP_DISABLED;
655 static enum libinput_config_tap_state
656 tp_tap_config_get_default(struct libinput_device *device)
659 * Tapping is disabled by default for two reasons:
660 * * if you don't know that tapping is a thing (or enabled by
661 * default), you get spurious mouse events that make the desktop
663 * * if you do know what tapping is and you want it, you
664 * usually know where to enable it, or at least you can search for
667 return LIBINPUT_CONFIG_TAP_DISABLED;
671 tp_init_tap(struct tp_dispatch *tp)
673 tp->tap.config.count = tp_tap_config_count;
674 tp->tap.config.set_enabled = tp_tap_config_set_enabled;
675 tp->tap.config.get_enabled = tp_tap_config_is_enabled;
676 tp->tap.config.get_default = tp_tap_config_get_default;
677 tp->device->base.config.tap = &tp->tap.config;
679 tp->tap.state = TAP_STATE_IDLE;
681 libinput_timer_init(&tp->tap.timer,
682 tp->device->base.seat->libinput,
683 tp_tap_handle_timeout, tp);
689 tp_destroy_tap(struct tp_dispatch *tp)
691 libinput_timer_cancel(&tp->tap.timer);