2 * Copyright © 2014 Red Hat, Inc.
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 #include "evdev-mt-touchpad.h"
31 #define DEFAULT_CONSTANT_ACCEL_NUMERATOR 50
32 #define DEFAULT_MIN_ACCEL_FACTOR 0.16
33 #define DEFAULT_MAX_ACCEL_FACTOR 1.0
34 #define DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR 700.0
37 tp_hysteresis(int in, int center, int margin)
39 int diff = in - center;
40 if (abs(diff) <= margin)
44 return center + diff - margin;
45 else if (diff < -margin)
46 return center + diff + margin;
51 tp_accel_profile(struct motion_filter *filter,
56 struct tp_dispatch *tp =
57 (struct tp_dispatch *) data;
61 accel_factor = velocity * tp->accel.constant_factor;
63 if (accel_factor > tp->accel.max_factor)
64 accel_factor = tp->accel.max_factor;
65 else if (accel_factor < tp->accel.min_factor)
66 accel_factor = tp->accel.min_factor;
71 static inline struct tp_motion *
72 tp_motion_history_offset(struct tp_touch *t, int offset)
75 (t->history.index - offset + TOUCHPAD_HISTORY_LENGTH) %
76 TOUCHPAD_HISTORY_LENGTH;
78 return &t->history.samples[offset_index];
82 tp_filter_motion(struct tp_dispatch *tp,
83 double *dx, double *dy, uint32_t time)
85 struct motion_params motion;
90 filter_dispatch(tp->filter, &motion, tp, time);
97 tp_motion_history_push(struct tp_touch *t)
99 int motion_index = (t->history.index + 1) % TOUCHPAD_HISTORY_LENGTH;
101 if (t->history.count < TOUCHPAD_HISTORY_LENGTH)
104 t->history.samples[motion_index].x = t->x;
105 t->history.samples[motion_index].y = t->y;
106 t->history.index = motion_index;
110 tp_motion_hysteresis(struct tp_dispatch *tp,
116 if (t->history.count == 0) {
117 t->hysteresis.center_x = t->x;
118 t->hysteresis.center_y = t->y;
121 t->hysteresis.center_x,
122 tp->hysteresis.margin_x);
124 t->hysteresis.center_y,
125 tp->hysteresis.margin_y);
126 t->hysteresis.center_x = x;
127 t->hysteresis.center_y = y;
134 tp_motion_history_reset(struct tp_touch *t)
136 t->history.count = 0;
139 static inline struct tp_touch *
140 tp_current_touch(struct tp_dispatch *tp)
142 return &tp->touches[min(tp->slot, tp->ntouches)];
145 static inline struct tp_touch *
146 tp_get_touch(struct tp_dispatch *tp, unsigned int slot)
148 assert(slot < tp->ntouches);
149 return &tp->touches[slot];
153 tp_begin_touch(struct tp_dispatch *tp, struct tp_touch *t)
155 struct tp_touch *tmp;
157 if (t->state != TOUCH_UPDATE) {
158 tp_motion_history_reset(t);
160 t->state = TOUCH_BEGIN;
162 assert(tp->nfingers_down >= 1);
163 tp->queued |= TOUCHPAD_EVENT_MOTION;
165 tp_for_each_touch(tp, tmp) {
170 if (!tmp->is_pointer) {
171 t->is_pointer = true;
177 tp_end_touch(struct tp_dispatch *tp, struct tp_touch *t)
179 if (t->state == TOUCH_NONE)
183 t->is_pointer = false;
184 t->state = TOUCH_END;
185 assert(tp->nfingers_down >= 1);
187 tp->queued |= TOUCHPAD_EVENT_MOTION;
191 tp_estimate_delta(int x0, int x1, int x2, int x3)
193 return (x0 + x1 - x2 - x3) / 4;
197 tp_get_delta(struct tp_touch *t, double *dx, double *dy)
199 if (t->history.count < 4) {
205 *dx = tp_estimate_delta(tp_motion_history_offset(t, 0)->x,
206 tp_motion_history_offset(t, 1)->x,
207 tp_motion_history_offset(t, 2)->x,
208 tp_motion_history_offset(t, 3)->x);
209 *dy = tp_estimate_delta(tp_motion_history_offset(t, 0)->y,
210 tp_motion_history_offset(t, 1)->y,
211 tp_motion_history_offset(t, 2)->y,
212 tp_motion_history_offset(t, 3)->y);
216 tp_process_absolute(struct tp_dispatch *tp,
217 const struct input_event *e,
220 struct tp_touch *t = tp_current_touch(tp);
223 case ABS_MT_POSITION_X:
227 tp->queued |= TOUCHPAD_EVENT_MOTION;
229 case ABS_MT_POSITION_Y:
233 tp->queued |= TOUCHPAD_EVENT_MOTION;
238 case ABS_MT_TRACKING_ID:
241 tp_begin_touch(tp, t);
248 tp_process_absolute_st(struct tp_dispatch *tp,
249 const struct input_event *e,
252 struct tp_touch *t = tp_current_touch(tp);
264 tp->queued |= TOUCHPAD_EVENT_MOTION;
270 tp_process_fake_touch(struct tp_dispatch *tp,
271 const struct input_event *e,
275 unsigned int fake_touches;
276 unsigned int nfake_touches;
280 if (e->code != BTN_TOUCH &&
281 (e->code < BTN_TOOL_DOUBLETAP || e->code > BTN_TOOL_QUADTAP))
284 shift = e->code == BTN_TOUCH ? 0 : (e->code - BTN_TOOL_DOUBLETAP + 1);
287 tp->fake_touches |= 1 << shift;
289 tp->fake_touches &= ~(0x1 << shift);
291 fake_touches = tp->fake_touches;
293 while (fake_touches) {
298 for (i = 0; i < tp->ntouches; i++) {
299 t = tp_get_touch(tp, i);
300 if (i >= nfake_touches) {
301 if (t->state != TOUCH_NONE) {
305 } else if (t->state != TOUCH_UPDATE &&
306 t->state != TOUCH_BEGIN) {
307 t->state = TOUCH_NONE;
308 tp_begin_touch(tp, t);
314 assert(tp->nfingers_down == nfake_touches);
318 tp_process_key(struct tp_dispatch *tp,
319 const struct input_event *e,
328 mask = 1 << (e->code - BTN_LEFT);
330 tp->buttons.state |= mask;
331 tp->queued |= TOUCHPAD_EVENT_BUTTON_PRESS;
333 tp->buttons.state &= ~mask;
334 tp->queued |= TOUCHPAD_EVENT_BUTTON_RELEASE;
338 case BTN_TOOL_DOUBLETAP:
339 case BTN_TOOL_TRIPLETAP:
340 case BTN_TOOL_QUADTAP:
342 tp_process_fake_touch(tp, e, time);
348 tp_unpin_finger(struct tp_dispatch *tp)
351 tp_for_each_touch(tp, t) {
353 t->is_pinned = false;
355 if (t->state != TOUCH_END &&
356 tp->nfingers_down == 1)
357 t->is_pointer = true;
364 tp_pin_finger(struct tp_dispatch *tp)
369 tp_for_each_touch(tp, t) {
378 pinned = tp_current_touch(tp);
380 if (tp->nfingers_down != 1) {
381 tp_for_each_touch(tp, t) {
385 if (t->y > pinned->y)
390 pinned->is_pinned = true;
391 pinned->is_pointer = false;
395 tp_process_state(struct tp_dispatch *tp, uint32_t time)
398 struct tp_touch *first = tp_get_touch(tp, 0);
400 tp_for_each_touch(tp, t) {
401 if (!tp->has_mt && t != first && first->fake) {
405 t->dirty = first->dirty;
406 } else if (!t->dirty)
409 tp_motion_hysteresis(tp, t);
410 tp_motion_history_push(t);
413 /* We have a physical button down event on a clickpad. For drag and
414 drop, this means we try to identify which finger pressed the
415 physical button and "pin" it, i.e. remove pointer-moving
416 capabilities from it.
418 if ((tp->queued & TOUCHPAD_EVENT_BUTTON_PRESS) &&
419 !tp->buttons.has_buttons)
424 tp_post_process_state(struct tp_dispatch *tp, uint32_t time)
428 tp_for_each_touch(tp, t) {
432 if (t->state == TOUCH_END) {
433 t->state = TOUCH_NONE;
435 } else if (t->state == TOUCH_BEGIN)
436 t->state = TOUCH_UPDATE;
441 tp->buttons.old_state = tp->buttons.state;
443 if (tp->queued & TOUCHPAD_EVENT_BUTTON_RELEASE)
446 tp->queued = TOUCHPAD_EVENT_NONE;
450 tp_post_twofinger_scroll(struct tp_dispatch *tp, uint32_t time)
454 double dx = 0, dy =0;
457 tp_for_each_touch(tp, t) {
460 tp_get_delta(t, &tmpx, &tmpy);
473 tp_filter_motion(tp, &dx, &dy, time);
475 if (tp->scroll.state == SCROLL_STATE_NONE) {
476 /* Require at least one px scrolling to start */
477 if (dx <= -1.0 || dx >= 1.0) {
478 tp->scroll.state = SCROLL_STATE_SCROLLING;
479 tp->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL);
482 if (dy <= -1.0 || dy >= 1.0) {
483 tp->scroll.state = SCROLL_STATE_SCROLLING;
484 tp->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL);
487 if (tp->scroll.state == SCROLL_STATE_NONE)
492 (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL))) {
493 pointer_notify_axis(&tp->device->base,
495 LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
496 li_fixed_from_double(dy));
500 (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL))) {
501 pointer_notify_axis(&tp->device->base,
503 LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
504 li_fixed_from_double(dx));
509 tp_post_scroll_events(struct tp_dispatch *tp, uint32_t time)
511 /* don't scroll if a clickpad is held down */
512 if (!tp->buttons.has_buttons &&
513 (tp->buttons.state || tp->buttons.old_state))
516 if (tp->nfingers_down != 2) {
517 /* terminate scrolling with a zero scroll event to notify
518 * caller that it really ended now */
519 if (tp->scroll.state != SCROLL_STATE_NONE) {
520 tp->scroll.state = SCROLL_STATE_NONE;
521 tp->scroll.direction = 0;
522 if (tp->scroll.direction & LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL)
523 pointer_notify_axis(&tp->device->base,
525 LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
527 if (tp->scroll.direction & LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL)
528 pointer_notify_axis(&tp->device->base,
530 LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
534 tp_post_twofinger_scroll(tp, time);
541 tp_post_clickfinger_buttons(struct tp_dispatch *tp, uint32_t time)
543 uint32_t current, old, button;
544 enum libinput_pointer_button_state state;
546 current = tp->buttons.state;
547 old = tp->buttons.old_state;
552 switch (tp->nfingers_down) {
553 case 1: button = BTN_LEFT; break;
554 case 2: button = BTN_RIGHT; break;
555 case 3: button = BTN_MIDDLE; break;
561 state = LIBINPUT_POINTER_BUTTON_STATE_PRESSED;
563 state = LIBINPUT_POINTER_BUTTON_STATE_RELEASED;
565 pointer_notify_button(&tp->device->base,
573 tp_post_physical_buttons(struct tp_dispatch *tp, uint32_t time)
575 uint32_t current, old, button;
577 current = tp->buttons.state;
578 old = tp->buttons.old_state;
581 while (current || old) {
582 enum libinput_pointer_button_state state;
584 if ((current & 0x1) ^ (old & 0x1)) {
585 if (!!(current & 0x1))
586 state = LIBINPUT_POINTER_BUTTON_STATE_PRESSED;
588 state = LIBINPUT_POINTER_BUTTON_STATE_RELEASED;
590 pointer_notify_button(&tp->device->base,
605 tp_post_button_events(struct tp_dispatch *tp, uint32_t time)
610 (TOUCHPAD_EVENT_BUTTON_PRESS|TOUCHPAD_EVENT_BUTTON_RELEASE)) == 0)
613 if (tp->buttons.has_buttons)
614 rc = tp_post_physical_buttons(tp, time);
616 rc = tp_post_clickfinger_buttons(tp, time);
622 tp_post_events(struct tp_dispatch *tp, uint32_t time)
624 struct tp_touch *t = tp_current_touch(tp);
627 if (tp_post_button_events(tp, time) != 0)
630 if (tp_tap_handle_state(tp, time) != 0)
633 if (tp_post_scroll_events(tp, time) != 0)
636 if (t->history.count >= TOUCHPAD_MIN_SAMPLES) {
637 if (!t->is_pointer) {
638 tp_for_each_touch(tp, t) {
647 tp_get_delta(t, &dx, &dy);
648 tp_filter_motion(tp, &dx, &dy, time);
650 if (dx != 0 || dy != 0)
651 pointer_notify_motion(
654 li_fixed_from_double(dx),
655 li_fixed_from_double(dy));
660 tp_process(struct evdev_dispatch *dispatch,
661 struct evdev_device *device,
662 struct input_event *e,
665 struct tp_dispatch *tp =
666 (struct tp_dispatch *)dispatch;
671 tp_process_absolute(tp, e, time);
673 tp_process_absolute_st(tp, e, time);
676 tp_process_key(tp, e, time);
679 tp_process_state(tp, time);
680 tp_post_events(tp, time);
681 tp_post_process_state(tp, time);
687 tp_destroy(struct evdev_dispatch *dispatch)
689 struct tp_dispatch *tp =
690 (struct tp_dispatch*)dispatch;
693 tp->filter->interface->destroy(tp->filter);
698 static struct evdev_dispatch_interface tp_interface = {
704 tp_init_slots(struct tp_dispatch *tp,
705 struct evdev_device *device)
707 const struct input_absinfo *absinfo;
709 absinfo = libevdev_get_abs_info(device->evdev, ABS_MT_SLOT);
711 tp->ntouches = absinfo->maximum + 1;
712 tp->slot = absinfo->value;
715 tp->ntouches = 5; /* FIXME: based on DOUBLETAP, etc. */
719 tp->touches = calloc(tp->ntouches,
720 sizeof(struct tp_touch));
726 tp_init_accel(struct tp_dispatch *touchpad, double diagonal)
728 struct motion_filter *accel;
730 touchpad->accel.constant_factor =
731 DEFAULT_CONSTANT_ACCEL_NUMERATOR / diagonal;
732 touchpad->accel.min_factor = DEFAULT_MIN_ACCEL_FACTOR;
733 touchpad->accel.max_factor = DEFAULT_MAX_ACCEL_FACTOR;
735 accel = create_pointer_accelator_filter(tp_accel_profile);
739 touchpad->filter = accel;
745 tp_init_scroll(struct tp_dispatch *tp)
747 tp->scroll.direction = 0;
748 tp->scroll.state = SCROLL_STATE_NONE;
754 tp_init(struct tp_dispatch *tp,
755 struct evdev_device *device)
760 tp->base.interface = &tp_interface;
763 if (tp_init_slots(tp, device) != 0)
766 width = abs(device->abs.max_x - device->abs.min_x);
767 height = abs(device->abs.max_y - device->abs.min_y);
768 diagonal = sqrt(width*width + height*height);
770 tp->hysteresis.margin_x =
771 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
772 tp->hysteresis.margin_y =
773 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
775 if (libevdev_has_event_code(device->evdev, EV_KEY, BTN_RIGHT) ||
776 libevdev_has_event_code(device->evdev, EV_KEY, BTN_RIGHT))
777 tp->buttons.has_buttons = true;
779 if (tp_init_scroll(tp) != 0)
782 if (tp_init_accel(tp, diagonal) != 0)
785 if (tp_init_tap(tp) != 0)
791 struct evdev_dispatch *
792 evdev_mt_touchpad_create(struct evdev_device *device)
794 struct tp_dispatch *tp;
796 tp = zalloc(sizeof *tp);
800 if (tp_init(tp, device) != 0) {
801 tp_destroy(&tp->base);