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);
259 tp->queued |= TOUCHPAD_EVENT_MOTION;
265 tp->queued |= TOUCHPAD_EVENT_MOTION;
271 tp_process_fake_touch(struct tp_dispatch *tp,
272 const struct input_event *e,
276 unsigned int fake_touches;
277 unsigned int nfake_touches;
281 if (e->code != BTN_TOUCH &&
282 (e->code < BTN_TOOL_DOUBLETAP || e->code > BTN_TOOL_QUADTAP))
285 shift = e->code == BTN_TOUCH ? 0 : (e->code - BTN_TOOL_DOUBLETAP + 1);
288 tp->fake_touches |= 1 << shift;
290 tp->fake_touches &= ~(0x1 << shift);
292 fake_touches = tp->fake_touches;
294 while (fake_touches) {
299 for (i = 0; i < tp->ntouches; i++) {
300 t = tp_get_touch(tp, i);
301 if (i >= nfake_touches) {
302 if (t->state != TOUCH_NONE) {
306 } else if (t->state != TOUCH_UPDATE &&
307 t->state != TOUCH_BEGIN) {
308 t->state = TOUCH_NONE;
309 tp_begin_touch(tp, t);
315 assert(tp->nfingers_down == nfake_touches);
319 tp_process_key(struct tp_dispatch *tp,
320 const struct input_event *e,
329 mask = 1 << (e->code - BTN_LEFT);
331 tp->buttons.state |= mask;
332 tp->queued |= TOUCHPAD_EVENT_BUTTON_PRESS;
334 tp->buttons.state &= ~mask;
335 tp->queued |= TOUCHPAD_EVENT_BUTTON_RELEASE;
339 case BTN_TOOL_DOUBLETAP:
340 case BTN_TOOL_TRIPLETAP:
341 case BTN_TOOL_QUADTAP:
343 tp_process_fake_touch(tp, e, time);
349 tp_unpin_finger(struct tp_dispatch *tp)
352 tp_for_each_touch(tp, t) {
354 t->is_pinned = false;
356 if (t->state != TOUCH_END &&
357 tp->nfingers_down == 1)
358 t->is_pointer = true;
365 tp_pin_finger(struct tp_dispatch *tp)
370 tp_for_each_touch(tp, t) {
379 pinned = tp_current_touch(tp);
381 if (tp->nfingers_down != 1) {
382 tp_for_each_touch(tp, t) {
386 if (t->y > pinned->y)
391 pinned->is_pinned = true;
392 pinned->is_pointer = false;
396 tp_process_state(struct tp_dispatch *tp, uint32_t time)
399 struct tp_touch *first = tp_get_touch(tp, 0);
401 tp_for_each_touch(tp, t) {
402 if (!tp->has_mt && t != first && first->fake) {
406 t->dirty = first->dirty;
407 } else if (!t->dirty)
410 tp_motion_hysteresis(tp, t);
411 tp_motion_history_push(t);
414 /* We have a physical button down event on a clickpad. For drag and
415 drop, this means we try to identify which finger pressed the
416 physical button and "pin" it, i.e. remove pointer-moving
417 capabilities from it.
419 if ((tp->queued & TOUCHPAD_EVENT_BUTTON_PRESS) &&
420 !tp->buttons.has_buttons)
425 tp_post_process_state(struct tp_dispatch *tp, uint32_t time)
429 tp_for_each_touch(tp, t) {
433 if (t->state == TOUCH_END) {
434 t->state = TOUCH_NONE;
436 } else if (t->state == TOUCH_BEGIN)
437 t->state = TOUCH_UPDATE;
442 tp->buttons.old_state = tp->buttons.state;
444 if (tp->queued & TOUCHPAD_EVENT_BUTTON_RELEASE)
447 tp->queued = TOUCHPAD_EVENT_NONE;
451 tp_post_twofinger_scroll(struct tp_dispatch *tp, uint32_t time)
455 double dx = 0, dy =0;
458 tp_for_each_touch(tp, t) {
461 tp_get_delta(t, &tmpx, &tmpy);
474 tp_filter_motion(tp, &dx, &dy, time);
476 if (tp->scroll.state == SCROLL_STATE_NONE) {
477 /* Require at least one px scrolling to start */
478 if (dx <= -1.0 || dx >= 1.0) {
479 tp->scroll.state = SCROLL_STATE_SCROLLING;
480 tp->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL);
483 if (dy <= -1.0 || dy >= 1.0) {
484 tp->scroll.state = SCROLL_STATE_SCROLLING;
485 tp->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL);
488 if (tp->scroll.state == SCROLL_STATE_NONE)
493 (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL))) {
494 pointer_notify_axis(&tp->device->base,
496 LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
497 li_fixed_from_double(dy));
501 (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL))) {
502 pointer_notify_axis(&tp->device->base,
504 LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
505 li_fixed_from_double(dx));
510 tp_post_scroll_events(struct tp_dispatch *tp, uint32_t time)
512 /* don't scroll if a clickpad is held down */
513 if (!tp->buttons.has_buttons &&
514 (tp->buttons.state || tp->buttons.old_state))
517 if (tp->nfingers_down != 2) {
518 /* terminate scrolling with a zero scroll event to notify
519 * caller that it really ended now */
520 if (tp->scroll.state != SCROLL_STATE_NONE) {
521 tp->scroll.state = SCROLL_STATE_NONE;
522 tp->scroll.direction = 0;
523 if (tp->scroll.direction & LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL)
524 pointer_notify_axis(&tp->device->base,
526 LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
528 if (tp->scroll.direction & LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL)
529 pointer_notify_axis(&tp->device->base,
531 LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
535 tp_post_twofinger_scroll(tp, time);
542 tp_post_clickfinger_buttons(struct tp_dispatch *tp, uint32_t time)
544 uint32_t current, old, button;
545 enum libinput_pointer_button_state state;
547 current = tp->buttons.state;
548 old = tp->buttons.old_state;
553 switch (tp->nfingers_down) {
554 case 1: button = BTN_LEFT; break;
555 case 2: button = BTN_RIGHT; break;
556 case 3: button = BTN_MIDDLE; break;
562 state = LIBINPUT_POINTER_BUTTON_STATE_PRESSED;
564 state = LIBINPUT_POINTER_BUTTON_STATE_RELEASED;
566 pointer_notify_button(&tp->device->base,
574 tp_post_physical_buttons(struct tp_dispatch *tp, uint32_t time)
576 uint32_t current, old, button;
578 current = tp->buttons.state;
579 old = tp->buttons.old_state;
582 while (current || old) {
583 enum libinput_pointer_button_state state;
585 if ((current & 0x1) ^ (old & 0x1)) {
586 if (!!(current & 0x1))
587 state = LIBINPUT_POINTER_BUTTON_STATE_PRESSED;
589 state = LIBINPUT_POINTER_BUTTON_STATE_RELEASED;
591 pointer_notify_button(&tp->device->base,
606 tp_post_button_events(struct tp_dispatch *tp, uint32_t time)
611 (TOUCHPAD_EVENT_BUTTON_PRESS|TOUCHPAD_EVENT_BUTTON_RELEASE)) == 0)
614 if (tp->buttons.has_buttons)
615 rc = tp_post_physical_buttons(tp, time);
617 rc = tp_post_clickfinger_buttons(tp, time);
623 tp_post_events(struct tp_dispatch *tp, uint32_t time)
625 struct tp_touch *t = tp_current_touch(tp);
628 if (tp_post_button_events(tp, time) != 0)
631 if (tp_tap_handle_state(tp, time) != 0)
634 if (tp_post_scroll_events(tp, time) != 0)
637 if (t->history.count >= TOUCHPAD_MIN_SAMPLES) {
638 if (!t->is_pointer) {
639 tp_for_each_touch(tp, t) {
648 tp_get_delta(t, &dx, &dy);
649 tp_filter_motion(tp, &dx, &dy, time);
651 if (dx != 0 || dy != 0)
652 pointer_notify_motion(
655 li_fixed_from_double(dx),
656 li_fixed_from_double(dy));
661 tp_process(struct evdev_dispatch *dispatch,
662 struct evdev_device *device,
663 struct input_event *e,
666 struct tp_dispatch *tp =
667 (struct tp_dispatch *)dispatch;
672 tp_process_absolute(tp, e, time);
674 tp_process_absolute_st(tp, e, time);
677 tp_process_key(tp, e, time);
680 tp_process_state(tp, time);
681 tp_post_events(tp, time);
682 tp_post_process_state(tp, time);
688 tp_destroy(struct evdev_dispatch *dispatch)
690 struct tp_dispatch *tp =
691 (struct tp_dispatch*)dispatch;
694 tp->filter->interface->destroy(tp->filter);
699 static struct evdev_dispatch_interface tp_interface = {
705 tp_init_slots(struct tp_dispatch *tp,
706 struct evdev_device *device)
708 const struct input_absinfo *absinfo;
710 absinfo = libevdev_get_abs_info(device->evdev, ABS_MT_SLOT);
712 tp->ntouches = absinfo->maximum + 1;
713 tp->slot = absinfo->value;
716 tp->ntouches = 5; /* FIXME: based on DOUBLETAP, etc. */
720 tp->touches = calloc(tp->ntouches,
721 sizeof(struct tp_touch));
727 tp_init_accel(struct tp_dispatch *touchpad, double diagonal)
729 struct motion_filter *accel;
731 touchpad->accel.constant_factor =
732 DEFAULT_CONSTANT_ACCEL_NUMERATOR / diagonal;
733 touchpad->accel.min_factor = DEFAULT_MIN_ACCEL_FACTOR;
734 touchpad->accel.max_factor = DEFAULT_MAX_ACCEL_FACTOR;
736 accel = create_pointer_accelator_filter(tp_accel_profile);
740 touchpad->filter = accel;
746 tp_init_scroll(struct tp_dispatch *tp)
748 tp->scroll.direction = 0;
749 tp->scroll.state = SCROLL_STATE_NONE;
755 tp_init(struct tp_dispatch *tp,
756 struct evdev_device *device)
761 tp->base.interface = &tp_interface;
764 if (tp_init_slots(tp, device) != 0)
767 width = abs(device->abs.max_x - device->abs.min_x);
768 height = abs(device->abs.max_y - device->abs.min_y);
769 diagonal = sqrt(width*width + height*height);
771 tp->hysteresis.margin_x =
772 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
773 tp->hysteresis.margin_y =
774 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
776 if (libevdev_has_event_code(device->evdev, EV_KEY, BTN_MIDDLE) ||
777 libevdev_has_event_code(device->evdev, EV_KEY, BTN_RIGHT))
778 tp->buttons.has_buttons = true;
780 if (tp_init_scroll(tp) != 0)
783 if (tp_init_accel(tp, diagonal) != 0)
786 if (tp_init_tap(tp) != 0)
792 struct evdev_dispatch *
793 evdev_mt_touchpad_create(struct evdev_device *device)
795 struct tp_dispatch *tp;
797 tp = zalloc(sizeof *tp);
801 if (tp_init(tp, device) != 0) {
802 tp_destroy(&tp->base);