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_ACCEL_NUMERATOR 1200.0
32 #define DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR 700.0
35 tp_hysteresis(int in, int center, int margin)
37 int diff = in - center;
38 if (abs(diff) <= margin)
42 return center + diff - margin;
44 return center + diff + margin;
47 static inline struct tp_motion *
48 tp_motion_history_offset(struct tp_touch *t, int offset)
51 (t->history.index - offset + TOUCHPAD_HISTORY_LENGTH) %
52 TOUCHPAD_HISTORY_LENGTH;
54 return &t->history.samples[offset_index];
58 tp_filter_motion(struct tp_dispatch *tp,
59 double *dx, double *dy, uint64_t time)
61 struct motion_params motion;
63 motion.dx = *dx * tp->accel.x_scale_coeff;
64 motion.dy = *dy * tp->accel.y_scale_coeff;
66 if (motion.dx != 0.0 || motion.dy != 0.0)
67 filter_dispatch(tp->filter, &motion, tp, time);
74 tp_motion_history_push(struct tp_touch *t)
76 int motion_index = (t->history.index + 1) % TOUCHPAD_HISTORY_LENGTH;
78 if (t->history.count < TOUCHPAD_HISTORY_LENGTH)
81 t->history.samples[motion_index].x = t->x;
82 t->history.samples[motion_index].y = t->y;
83 t->history.index = motion_index;
87 tp_motion_hysteresis(struct tp_dispatch *tp,
93 if (t->history.count == 0) {
94 t->hysteresis.center_x = t->x;
95 t->hysteresis.center_y = t->y;
98 t->hysteresis.center_x,
99 tp->hysteresis.margin_x);
101 t->hysteresis.center_y,
102 tp->hysteresis.margin_y);
103 t->hysteresis.center_x = x;
104 t->hysteresis.center_y = y;
111 tp_motion_history_reset(struct tp_touch *t)
113 t->history.count = 0;
116 static inline struct tp_touch *
117 tp_current_touch(struct tp_dispatch *tp)
119 return &tp->touches[min(tp->slot, tp->ntouches - 1)];
122 static inline struct tp_touch *
123 tp_get_touch(struct tp_dispatch *tp, unsigned int slot)
125 assert(slot < tp->ntouches);
126 return &tp->touches[slot];
130 tp_begin_touch(struct tp_dispatch *tp, struct tp_touch *t)
132 if (t->state != TOUCH_UPDATE) {
133 tp_motion_history_reset(t);
135 t->state = TOUCH_BEGIN;
136 t->pinned.is_pinned = false;
138 assert(tp->nfingers_down >= 1);
139 tp->queued |= TOUCHPAD_EVENT_MOTION;
144 tp_end_touch(struct tp_dispatch *tp, struct tp_touch *t)
146 if (t->state == TOUCH_NONE)
150 t->is_pointer = false;
151 t->state = TOUCH_END;
152 t->pinned.is_pinned = false;
153 assert(tp->nfingers_down >= 1);
155 tp->queued |= TOUCHPAD_EVENT_MOTION;
159 tp_estimate_delta(int x0, int x1, int x2, int x3)
161 return (x0 + x1 - x2 - x3) / 4;
165 tp_get_delta(struct tp_touch *t, double *dx, double *dy)
167 if (t->history.count < 4) {
173 *dx = tp_estimate_delta(tp_motion_history_offset(t, 0)->x,
174 tp_motion_history_offset(t, 1)->x,
175 tp_motion_history_offset(t, 2)->x,
176 tp_motion_history_offset(t, 3)->x);
177 *dy = tp_estimate_delta(tp_motion_history_offset(t, 0)->y,
178 tp_motion_history_offset(t, 1)->y,
179 tp_motion_history_offset(t, 2)->y,
180 tp_motion_history_offset(t, 3)->y);
184 tp_process_absolute(struct tp_dispatch *tp,
185 const struct input_event *e,
188 struct tp_touch *t = tp_current_touch(tp);
191 case ABS_MT_POSITION_X:
195 tp->queued |= TOUCHPAD_EVENT_MOTION;
197 case ABS_MT_POSITION_Y:
201 tp->queued |= TOUCHPAD_EVENT_MOTION;
206 case ABS_MT_TRACKING_ID:
209 tp_begin_touch(tp, t);
216 tp_process_absolute_st(struct tp_dispatch *tp,
217 const struct input_event *e,
220 struct tp_touch *t = tp_current_touch(tp);
227 tp->queued |= TOUCHPAD_EVENT_MOTION;
233 tp->queued |= TOUCHPAD_EVENT_MOTION;
239 tp_process_fake_touch(struct tp_dispatch *tp,
240 const struct input_event *e,
244 unsigned int fake_touches;
245 unsigned int nfake_touches;
249 if (e->code != BTN_TOUCH &&
250 (e->code < BTN_TOOL_DOUBLETAP || e->code > BTN_TOOL_QUADTAP))
253 shift = e->code == BTN_TOUCH ? 0 : (e->code - BTN_TOOL_DOUBLETAP + 1);
256 tp->fake_touches |= 1 << shift;
258 tp->fake_touches &= ~(0x1 << shift);
260 fake_touches = tp->fake_touches;
262 while (fake_touches) {
267 for (i = 0; i < tp->ntouches; i++) {
268 t = tp_get_touch(tp, i);
269 if (i >= nfake_touches) {
270 if (t->state != TOUCH_NONE) {
274 } else if (t->state != TOUCH_UPDATE &&
275 t->state != TOUCH_BEGIN) {
276 t->state = TOUCH_NONE;
277 tp_begin_touch(tp, t);
283 assert(tp->nfingers_down == nfake_touches);
287 tp_process_key(struct tp_dispatch *tp,
288 const struct input_event *e,
295 tp_process_button(tp, e, time);
298 case BTN_TOOL_DOUBLETAP:
299 case BTN_TOOL_TRIPLETAP:
300 case BTN_TOOL_QUADTAP:
302 tp_process_fake_touch(tp, e, time);
308 tp_unpin_finger(struct tp_dispatch *tp, struct tp_touch *t)
310 unsigned int xdist, ydist;
312 if (!t->pinned.is_pinned)
315 xdist = abs(t->x - t->pinned.center_x);
316 ydist = abs(t->y - t->pinned.center_y);
318 if (xdist * xdist + ydist * ydist >=
319 tp->buttons.motion_dist * tp->buttons.motion_dist) {
320 t->pinned.is_pinned = false;
321 tp_set_pointer(tp, t);
326 tp_pin_fingers(struct tp_dispatch *tp)
330 tp_for_each_touch(tp, t) {
331 t->is_pointer = false;
332 t->pinned.is_pinned = true;
333 t->pinned.center_x = t->x;
334 t->pinned.center_y = t->y;
339 tp_touch_active(struct tp_dispatch *tp, struct tp_touch *t)
341 return (t->state == TOUCH_BEGIN || t->state == TOUCH_UPDATE) &&
342 !t->pinned.is_pinned && tp_button_touch_active(tp, t);
346 tp_set_pointer(struct tp_dispatch *tp, struct tp_touch *t)
348 struct tp_touch *tmp = NULL;
350 /* Only set the touch as pointer if we don't have one yet */
351 tp_for_each_touch(tp, tmp) {
356 if (tp_touch_active(tp, t))
357 t->is_pointer = true;
361 tp_process_state(struct tp_dispatch *tp, uint64_t time)
364 struct tp_touch *first = tp_get_touch(tp, 0);
366 tp_for_each_touch(tp, t) {
367 if (!tp->has_mt && t != first && first->fake) {
371 t->dirty = first->dirty;
372 } else if (!t->dirty) {
376 tp_motion_hysteresis(tp, t);
377 tp_motion_history_push(t);
379 tp_unpin_finger(tp, t);
382 tp_button_handle_state(tp, time);
385 * We have a physical button down event on a clickpad. To avoid
386 * spurious pointer moves by the clicking finger we pin all fingers.
387 * We unpin fingers when they move more then a certain threshold to
388 * to allow drag and drop.
390 if ((tp->queued & TOUCHPAD_EVENT_BUTTON_PRESS) &&
391 tp->buttons.is_clickpad)
396 tp_post_process_state(struct tp_dispatch *tp, uint64_t time)
400 tp_for_each_touch(tp, t) {
404 if (t->state == TOUCH_END) {
405 t->state = TOUCH_NONE;
407 } else if (t->state == TOUCH_BEGIN)
408 t->state = TOUCH_UPDATE;
413 tp->buttons.old_state = tp->buttons.state;
415 tp->queued = TOUCHPAD_EVENT_NONE;
419 tp_post_twofinger_scroll(struct tp_dispatch *tp, uint64_t time)
423 double dx = 0, dy =0;
426 tp_for_each_touch(tp, t) {
427 if (tp_touch_active(tp, t) && t->dirty) {
429 tp_get_delta(t, &tmpx, &tmpy);
434 /* Stop spurious MOTION events at the end of scrolling */
435 t->is_pointer = false;
444 tp_filter_motion(tp, &dx, &dy, time);
446 /* Require at least five px scrolling to start */
447 if (dy <= -5.0 || dy >= 5.0)
448 tp->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
450 if (dx <= -5.0 || dx >= 5.0)
451 tp->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
454 (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))) {
455 pointer_notify_axis(&tp->device->base,
457 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
462 (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))) {
463 pointer_notify_axis(&tp->device->base,
465 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
471 tp_stop_scroll_events(struct tp_dispatch *tp, uint64_t time)
473 /* terminate scrolling with a zero scroll event */
474 if (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
475 pointer_notify_axis(&tp->device->base,
477 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
479 if (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
480 pointer_notify_axis(&tp->device->base,
482 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
485 tp->scroll.direction = 0;
489 tp_post_scroll_events(struct tp_dispatch *tp, uint64_t time)
492 int nfingers_down = 0;
494 /* Only count active touches for 2 finger scrolling */
495 tp_for_each_touch(tp, t) {
496 if (tp_touch_active(tp, t))
500 if (nfingers_down != 2) {
501 tp_stop_scroll_events(tp, time);
505 tp_post_twofinger_scroll(tp, time);
510 tp_post_events(struct tp_dispatch *tp, uint64_t time)
512 struct tp_touch *t = tp_current_touch(tp);
516 consumed |= tp_tap_handle_state(tp, time);
517 consumed |= tp_post_button_events(tp, time);
520 tp_stop_scroll_events(tp, time);
524 if (tp_post_scroll_events(tp, time) != 0)
527 if (t->history.count >= TOUCHPAD_MIN_SAMPLES) {
528 if (!t->is_pointer) {
529 tp_for_each_touch(tp, t) {
538 tp_get_delta(t, &dx, &dy);
539 tp_filter_motion(tp, &dx, &dy, time);
541 if (dx != 0.0 || dy != 0.0)
542 pointer_notify_motion(&tp->device->base, time, dx, dy);
547 tp_process(struct evdev_dispatch *dispatch,
548 struct evdev_device *device,
549 struct input_event *e,
552 struct tp_dispatch *tp =
553 (struct tp_dispatch *)dispatch;
558 tp_process_absolute(tp, e, time);
560 tp_process_absolute_st(tp, e, time);
563 tp_process_key(tp, e, time);
566 tp_process_state(tp, time);
567 tp_post_events(tp, time);
568 tp_post_process_state(tp, time);
574 tp_destroy(struct evdev_dispatch *dispatch)
576 struct tp_dispatch *tp =
577 (struct tp_dispatch*)dispatch;
580 tp_destroy_buttons(tp);
582 filter_destroy(tp->filter);
587 static struct evdev_dispatch_interface tp_interface = {
593 tp_init_touch(struct tp_dispatch *tp,
600 tp_init_slots(struct tp_dispatch *tp,
601 struct evdev_device *device)
604 const struct input_absinfo *absinfo;
606 absinfo = libevdev_get_abs_info(device->evdev, ABS_MT_SLOT);
608 tp->ntouches = absinfo->maximum + 1;
609 tp->slot = absinfo->value;
616 { BTN_TOOL_QUINTTAP, 5 },
617 { BTN_TOOL_QUADTAP, 4 },
618 { BTN_TOOL_TRIPLETAP, 3 },
619 { BTN_TOOL_DOUBLETAP, 2 },
627 ARRAY_FOR_EACH(max_touches, m) {
628 if (libevdev_has_event_code(device->evdev,
631 tp->ntouches = m->ntouches;
636 tp->touches = calloc(tp->ntouches,
637 sizeof(struct tp_touch));
641 for (i = 0; i < tp->ntouches; i++)
642 tp_init_touch(tp, &tp->touches[i]);
648 tp_init_accel(struct tp_dispatch *tp, double diagonal)
650 struct motion_filter *accel;
654 res_x = libevdev_get_abs_resolution(tp->device->evdev,
656 res_y = libevdev_get_abs_resolution(tp->device->evdev,
659 res_x = libevdev_get_abs_resolution(tp->device->evdev,
661 res_y = libevdev_get_abs_resolution(tp->device->evdev,
666 * Not all touchpads report the same amount of units/mm (resolution).
667 * Normalize motion events to a resolution of 15.74 units/mm
668 * (== 400 dpi) as base (unaccelerated) speed. This also evens out any
669 * differences in x and y resolution, so that a circle on the
670 * touchpad does not turn into an elipse on the screen.
672 * We pick 400dpi as thats one of the many default resolutions
673 * for USB mice, so we end up with a similar base speed on the device.
675 if (res_x > 1 && res_y > 1) {
676 tp->accel.x_scale_coeff = (400/25.4) / res_x;
677 tp->accel.y_scale_coeff = (400/25.4) / res_y;
680 * For touchpads where the driver does not provide resolution, fall
681 * back to scaling motion events based on the diagonal size in units.
683 tp->accel.x_scale_coeff = DEFAULT_ACCEL_NUMERATOR / diagonal;
684 tp->accel.y_scale_coeff = DEFAULT_ACCEL_NUMERATOR / diagonal;
687 accel = create_pointer_accelator_filter(
688 pointer_accel_profile_smooth_simple);
698 tp_init_scroll(struct tp_dispatch *tp)
700 tp->scroll.direction = 0;
706 tp_init(struct tp_dispatch *tp,
707 struct evdev_device *device)
712 tp->base.interface = &tp_interface;
715 if (tp_init_slots(tp, device) != 0)
718 width = abs(device->abs.absinfo_x->maximum -
719 device->abs.absinfo_x->minimum);
720 height = abs(device->abs.absinfo_y->maximum -
721 device->abs.absinfo_y->minimum);
722 diagonal = sqrt(width*width + height*height);
724 tp->hysteresis.margin_x =
725 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
726 tp->hysteresis.margin_y =
727 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
729 if (tp_init_scroll(tp) != 0)
732 if (tp_init_accel(tp, diagonal) != 0)
735 if (tp_init_tap(tp) != 0)
738 if (tp_init_buttons(tp, device) != 0)
744 struct evdev_dispatch *
745 evdev_mt_touchpad_create(struct evdev_device *device)
747 struct tp_dispatch *tp;
749 tp = zalloc(sizeof *tp);
753 if (tp_init(tp, device) != 0) {
754 tp_destroy(&tp->base);