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 100
32 #define DEFAULT_MIN_ACCEL_FACTOR 0.20
33 #define DEFAULT_MAX_ACCEL_FACTOR 0.40
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, uint64_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 if (t->state != TOUCH_UPDATE) {
156 tp_motion_history_reset(t);
158 t->state = TOUCH_BEGIN;
159 t->pinned.is_pinned = false;
161 assert(tp->nfingers_down >= 1);
162 tp->queued |= TOUCHPAD_EVENT_MOTION;
167 tp_end_touch(struct tp_dispatch *tp, struct tp_touch *t)
169 if (t->state == TOUCH_NONE)
173 t->is_pointer = false;
174 t->state = TOUCH_END;
175 t->pinned.is_pinned = false;
176 assert(tp->nfingers_down >= 1);
178 tp->queued |= TOUCHPAD_EVENT_MOTION;
182 tp_estimate_delta(int x0, int x1, int x2, int x3)
184 return (x0 + x1 - x2 - x3) / 4;
188 tp_get_delta(struct tp_touch *t, double *dx, double *dy)
190 if (t->history.count < 4) {
196 *dx = tp_estimate_delta(tp_motion_history_offset(t, 0)->x,
197 tp_motion_history_offset(t, 1)->x,
198 tp_motion_history_offset(t, 2)->x,
199 tp_motion_history_offset(t, 3)->x);
200 *dy = tp_estimate_delta(tp_motion_history_offset(t, 0)->y,
201 tp_motion_history_offset(t, 1)->y,
202 tp_motion_history_offset(t, 2)->y,
203 tp_motion_history_offset(t, 3)->y);
207 tp_process_absolute(struct tp_dispatch *tp,
208 const struct input_event *e,
211 struct tp_touch *t = tp_current_touch(tp);
214 case ABS_MT_POSITION_X:
218 tp->queued |= TOUCHPAD_EVENT_MOTION;
220 case ABS_MT_POSITION_Y:
224 tp->queued |= TOUCHPAD_EVENT_MOTION;
229 case ABS_MT_TRACKING_ID:
232 tp_begin_touch(tp, t);
239 tp_process_absolute_st(struct tp_dispatch *tp,
240 const struct input_event *e,
243 struct tp_touch *t = tp_current_touch(tp);
250 tp->queued |= TOUCHPAD_EVENT_MOTION;
256 tp->queued |= TOUCHPAD_EVENT_MOTION;
262 tp_process_fake_touch(struct tp_dispatch *tp,
263 const struct input_event *e,
267 unsigned int fake_touches;
268 unsigned int nfake_touches;
272 if (e->code != BTN_TOUCH &&
273 (e->code < BTN_TOOL_DOUBLETAP || e->code > BTN_TOOL_QUADTAP))
276 shift = e->code == BTN_TOUCH ? 0 : (e->code - BTN_TOOL_DOUBLETAP + 1);
279 tp->fake_touches |= 1 << shift;
281 tp->fake_touches &= ~(0x1 << shift);
283 fake_touches = tp->fake_touches;
285 while (fake_touches) {
290 for (i = 0; i < tp->ntouches; i++) {
291 t = tp_get_touch(tp, i);
292 if (i >= nfake_touches) {
293 if (t->state != TOUCH_NONE) {
297 } else if (t->state != TOUCH_UPDATE &&
298 t->state != TOUCH_BEGIN) {
299 t->state = TOUCH_NONE;
300 tp_begin_touch(tp, t);
306 assert(tp->nfingers_down == nfake_touches);
310 tp_process_key(struct tp_dispatch *tp,
311 const struct input_event *e,
318 tp_process_button(tp, e, time);
321 case BTN_TOOL_DOUBLETAP:
322 case BTN_TOOL_TRIPLETAP:
323 case BTN_TOOL_QUADTAP:
325 tp_process_fake_touch(tp, e, time);
331 tp_unpin_finger(struct tp_dispatch *tp, struct tp_touch *t)
333 unsigned int xdist, ydist;
335 if (!t->pinned.is_pinned)
338 xdist = abs(t->x - t->pinned.center_x);
339 ydist = abs(t->y - t->pinned.center_y);
341 if (xdist * xdist + ydist * ydist >=
342 tp->buttons.motion_dist * tp->buttons.motion_dist) {
343 t->pinned.is_pinned = false;
344 tp_set_pointer(tp, t);
349 tp_pin_fingers(struct tp_dispatch *tp)
353 tp_for_each_touch(tp, t) {
354 t->is_pointer = false;
355 t->pinned.is_pinned = true;
356 t->pinned.center_x = t->x;
357 t->pinned.center_y = t->y;
362 tp_touch_active(struct tp_dispatch *tp, struct tp_touch *t)
364 return (t->state == TOUCH_BEGIN || t->state == TOUCH_UPDATE) &&
365 !t->pinned.is_pinned && tp_button_touch_active(tp, t);
369 tp_set_pointer(struct tp_dispatch *tp, struct tp_touch *t)
371 struct tp_touch *tmp = NULL;
373 /* Only set the touch as pointer if we don't have one yet */
374 tp_for_each_touch(tp, tmp) {
379 if (tp_touch_active(tp, t))
380 t->is_pointer = true;
384 tp_process_state(struct tp_dispatch *tp, uint64_t time)
387 struct tp_touch *first = tp_get_touch(tp, 0);
389 tp_for_each_touch(tp, t) {
390 if (!tp->has_mt && t != first && first->fake) {
394 t->dirty = first->dirty;
395 } else if (!t->dirty)
398 tp_motion_hysteresis(tp, t);
399 tp_motion_history_push(t);
401 tp_unpin_finger(tp, t);
404 tp_button_handle_state(tp, time);
407 * We have a physical button down event on a clickpad. To avoid
408 * spurious pointer moves by the clicking finger we pin all fingers.
409 * We unpin fingers when they move more then a certain threshold to
410 * to allow drag and drop.
412 if ((tp->queued & TOUCHPAD_EVENT_BUTTON_PRESS) &&
413 tp->buttons.is_clickpad)
418 tp_post_process_state(struct tp_dispatch *tp, uint64_t time)
422 tp_for_each_touch(tp, t) {
426 if (t->state == TOUCH_END) {
427 t->state = TOUCH_NONE;
429 } else if (t->state == TOUCH_BEGIN)
430 t->state = TOUCH_UPDATE;
435 tp->buttons.old_state = tp->buttons.state;
437 tp->queued = TOUCHPAD_EVENT_NONE;
441 tp_post_twofinger_scroll(struct tp_dispatch *tp, uint64_t time)
445 double dx = 0, dy =0;
448 tp_for_each_touch(tp, t) {
449 if (tp_touch_active(tp, t) && t->dirty) {
451 tp_get_delta(t, &tmpx, &tmpy);
464 tp_filter_motion(tp, &dx, &dy, time);
466 if (tp->scroll.state == SCROLL_STATE_NONE) {
467 /* Require at least one px scrolling to start */
468 if (dx <= -1.0 || dx >= 1.0) {
469 tp->scroll.state = SCROLL_STATE_SCROLLING;
470 tp->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL);
473 if (dy <= -1.0 || dy >= 1.0) {
474 tp->scroll.state = SCROLL_STATE_SCROLLING;
475 tp->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL);
478 if (tp->scroll.state == SCROLL_STATE_NONE)
482 /* Stop spurious MOTION events at the end of scrolling */
483 tp_for_each_touch(tp, t)
484 t->is_pointer = false;
487 (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL))) {
488 pointer_notify_axis(&tp->device->base,
490 LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
491 li_fixed_from_double(dy));
495 (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL))) {
496 pointer_notify_axis(&tp->device->base,
498 LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
499 li_fixed_from_double(dx));
504 tp_stop_scroll_events(struct tp_dispatch *tp, uint64_t time)
506 if (tp->scroll.state == SCROLL_STATE_NONE)
509 /* terminate scrolling with a zero scroll event */
510 if (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL))
511 pointer_notify_axis(&tp->device->base,
513 LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
515 if (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL))
516 pointer_notify_axis(&tp->device->base,
518 LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
521 tp->scroll.state = SCROLL_STATE_NONE;
522 tp->scroll.direction = 0;
526 tp_post_scroll_events(struct tp_dispatch *tp, uint64_t time)
529 int nfingers_down = 0;
531 /* Only count active touches for 2 finger scrolling */
532 tp_for_each_touch(tp, t) {
533 if (tp_touch_active(tp, t))
537 if (nfingers_down != 2) {
538 tp_stop_scroll_events(tp, time);
542 tp_post_twofinger_scroll(tp, time);
547 tp_post_events(struct tp_dispatch *tp, uint64_t time)
549 struct tp_touch *t = tp_current_touch(tp);
552 if (tp_post_button_events(tp, time) != 0) {
553 tp_stop_scroll_events(tp, time);
557 if (tp_tap_handle_state(tp, time) != 0) {
558 tp_stop_scroll_events(tp, time);
562 if (tp_post_scroll_events(tp, time) != 0)
565 if (t->history.count >= TOUCHPAD_MIN_SAMPLES) {
566 if (!t->is_pointer) {
567 tp_for_each_touch(tp, t) {
576 tp_get_delta(t, &dx, &dy);
577 tp_filter_motion(tp, &dx, &dy, time);
579 if (dx != 0 || dy != 0)
580 pointer_notify_motion(
583 li_fixed_from_double(dx),
584 li_fixed_from_double(dy));
589 tp_process(struct evdev_dispatch *dispatch,
590 struct evdev_device *device,
591 struct input_event *e,
594 struct tp_dispatch *tp =
595 (struct tp_dispatch *)dispatch;
600 tp_process_absolute(tp, e, time);
602 tp_process_absolute_st(tp, e, time);
605 tp_process_key(tp, e, time);
608 tp_process_state(tp, time);
609 tp_post_events(tp, time);
610 tp_post_process_state(tp, time);
616 tp_destroy(struct evdev_dispatch *dispatch)
618 struct tp_dispatch *tp =
619 (struct tp_dispatch*)dispatch;
622 tp_destroy_buttons(tp);
624 motion_filter_destroy(tp->filter);
629 static struct evdev_dispatch_interface tp_interface = {
635 tp_init_touch(struct tp_dispatch *tp,
638 t->button.state = BUTTON_STATE_NONE;
642 tp_init_slots(struct tp_dispatch *tp,
643 struct evdev_device *device)
646 const struct input_absinfo *absinfo;
648 absinfo = libevdev_get_abs_info(device->evdev, ABS_MT_SLOT);
650 tp->ntouches = absinfo->maximum + 1;
651 tp->slot = absinfo->value;
658 { BTN_TOOL_QUINTTAP, 5 },
659 { BTN_TOOL_QUADTAP, 4 },
660 { BTN_TOOL_TRIPLETAP, 3 },
661 { BTN_TOOL_DOUBLETAP, 2 },
669 ARRAY_FOR_EACH(max_touches, m) {
670 if (libevdev_has_event_code(device->evdev,
673 tp->ntouches = m->ntouches;
678 tp->touches = calloc(tp->ntouches,
679 sizeof(struct tp_touch));
683 for (i = 0; i < tp->ntouches; i++)
684 tp_init_touch(tp, &tp->touches[i]);
690 tp_init_accel(struct tp_dispatch *touchpad, double diagonal)
692 struct motion_filter *accel;
694 touchpad->accel.constant_factor =
695 DEFAULT_CONSTANT_ACCEL_NUMERATOR / diagonal;
696 touchpad->accel.min_factor = DEFAULT_MIN_ACCEL_FACTOR;
697 touchpad->accel.max_factor = DEFAULT_MAX_ACCEL_FACTOR;
699 accel = create_pointer_accelator_filter(tp_accel_profile);
703 touchpad->filter = accel;
709 tp_init_scroll(struct tp_dispatch *tp)
711 tp->scroll.direction = 0;
712 tp->scroll.state = SCROLL_STATE_NONE;
718 tp_init(struct tp_dispatch *tp,
719 struct evdev_device *device)
724 tp->base.interface = &tp_interface;
726 tp->tap.timer_fd = -1;
727 tp->buttons.timer_fd = -1;
729 if (tp_init_slots(tp, device) != 0)
732 width = abs(device->abs.max_x - device->abs.min_x);
733 height = abs(device->abs.max_y - device->abs.min_y);
734 diagonal = sqrt(width*width + height*height);
736 tp->hysteresis.margin_x =
737 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
738 tp->hysteresis.margin_y =
739 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
741 if (tp_init_scroll(tp) != 0)
744 if (tp_init_accel(tp, diagonal) != 0)
747 if (tp_init_tap(tp) != 0)
750 if (tp_init_buttons(tp, device) != 0)
756 struct evdev_dispatch *
757 evdev_mt_touchpad_create(struct evdev_device *device)
759 struct tp_dispatch *tp;
761 tp = zalloc(sizeof *tp);
765 if (tp_init(tp, device) != 0) {
766 tp_destroy(&tp->base);