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 = NULL;
157 if (t->state != TOUCH_UPDATE) {
158 tp_motion_history_reset(t);
160 t->state = TOUCH_BEGIN;
161 t->pinned.is_pinned = false;
163 assert(tp->nfingers_down >= 1);
164 tp->queued |= TOUCHPAD_EVENT_MOTION;
166 tp_for_each_touch(tp, tmp) {
171 if (!tmp->is_pointer) {
172 t->is_pointer = true;
178 tp_end_touch(struct tp_dispatch *tp, struct tp_touch *t)
180 if (t->state == TOUCH_NONE)
184 t->is_pointer = false;
185 t->state = TOUCH_END;
186 t->pinned.is_pinned = false;
187 assert(tp->nfingers_down >= 1);
189 tp->queued |= TOUCHPAD_EVENT_MOTION;
193 tp_estimate_delta(int x0, int x1, int x2, int x3)
195 return (x0 + x1 - x2 - x3) / 4;
199 tp_get_delta(struct tp_touch *t, double *dx, double *dy)
201 if (t->history.count < 4) {
207 *dx = tp_estimate_delta(tp_motion_history_offset(t, 0)->x,
208 tp_motion_history_offset(t, 1)->x,
209 tp_motion_history_offset(t, 2)->x,
210 tp_motion_history_offset(t, 3)->x);
211 *dy = tp_estimate_delta(tp_motion_history_offset(t, 0)->y,
212 tp_motion_history_offset(t, 1)->y,
213 tp_motion_history_offset(t, 2)->y,
214 tp_motion_history_offset(t, 3)->y);
218 tp_process_absolute(struct tp_dispatch *tp,
219 const struct input_event *e,
222 struct tp_touch *t = tp_current_touch(tp);
225 case ABS_MT_POSITION_X:
229 tp->queued |= TOUCHPAD_EVENT_MOTION;
231 case ABS_MT_POSITION_Y:
235 tp->queued |= TOUCHPAD_EVENT_MOTION;
240 case ABS_MT_TRACKING_ID:
243 tp_begin_touch(tp, t);
250 tp_process_absolute_st(struct tp_dispatch *tp,
251 const struct input_event *e,
254 struct tp_touch *t = tp_current_touch(tp);
261 tp->queued |= TOUCHPAD_EVENT_MOTION;
267 tp->queued |= TOUCHPAD_EVENT_MOTION;
273 tp_process_fake_touch(struct tp_dispatch *tp,
274 const struct input_event *e,
278 unsigned int fake_touches;
279 unsigned int nfake_touches;
283 if (e->code != BTN_TOUCH &&
284 (e->code < BTN_TOOL_DOUBLETAP || e->code > BTN_TOOL_QUADTAP))
287 shift = e->code == BTN_TOUCH ? 0 : (e->code - BTN_TOOL_DOUBLETAP + 1);
290 tp->fake_touches |= 1 << shift;
292 tp->fake_touches &= ~(0x1 << shift);
294 fake_touches = tp->fake_touches;
296 while (fake_touches) {
301 for (i = 0; i < tp->ntouches; i++) {
302 t = tp_get_touch(tp, i);
303 if (i >= nfake_touches) {
304 if (t->state != TOUCH_NONE) {
308 } else if (t->state != TOUCH_UPDATE &&
309 t->state != TOUCH_BEGIN) {
310 t->state = TOUCH_NONE;
311 tp_begin_touch(tp, t);
317 assert(tp->nfingers_down == nfake_touches);
321 tp_process_key(struct tp_dispatch *tp,
322 const struct input_event *e,
329 tp_process_button(tp, e, time);
332 case BTN_TOOL_DOUBLETAP:
333 case BTN_TOOL_TRIPLETAP:
334 case BTN_TOOL_QUADTAP:
336 tp_process_fake_touch(tp, e, time);
342 tp_unpin_finger(struct tp_dispatch *tp, struct tp_touch *t)
344 unsigned int xdist, ydist;
345 struct tp_touch *tmp = NULL;
347 if (!t->pinned.is_pinned)
350 xdist = abs(t->x - t->pinned.center_x);
351 ydist = abs(t->y - t->pinned.center_y);
353 if (xdist * xdist + ydist * ydist <
354 tp->buttons.motion_dist * tp->buttons.motion_dist)
357 t->pinned.is_pinned = false;
359 tp_for_each_touch(tp, tmp) {
364 if (t->state != TOUCH_END && !tmp->is_pointer)
365 t->is_pointer = true;
369 tp_pin_fingers(struct tp_dispatch *tp)
373 tp_for_each_touch(tp, t) {
374 t->is_pointer = false;
375 t->pinned.is_pinned = true;
376 t->pinned.center_x = t->x;
377 t->pinned.center_y = t->y;
382 tp_process_state(struct tp_dispatch *tp, uint32_t time)
385 struct tp_touch *first = tp_get_touch(tp, 0);
387 tp_for_each_touch(tp, t) {
388 if (!tp->has_mt && t != first && first->fake) {
392 t->dirty = first->dirty;
393 } else if (!t->dirty)
396 tp_motion_hysteresis(tp, t);
397 tp_motion_history_push(t);
399 tp_unpin_finger(tp, t);
403 * We have a physical button down event on a clickpad. To avoid
404 * spurious pointer moves by the clicking finger we pin all fingers.
405 * We unpin fingers when they move more then a certain threshold to
406 * to allow drag and drop.
408 if ((tp->queued & TOUCHPAD_EVENT_BUTTON_PRESS) &&
409 !tp->buttons.has_buttons)
414 tp_post_process_state(struct tp_dispatch *tp, uint32_t time)
418 tp_for_each_touch(tp, t) {
422 if (t->state == TOUCH_END) {
423 t->state = TOUCH_NONE;
425 } else if (t->state == TOUCH_BEGIN)
426 t->state = TOUCH_UPDATE;
431 tp->buttons.old_state = tp->buttons.state;
433 tp->queued = TOUCHPAD_EVENT_NONE;
437 tp_post_twofinger_scroll(struct tp_dispatch *tp, uint32_t time)
441 double dx = 0, dy =0;
444 tp_for_each_touch(tp, t) {
447 tp_get_delta(t, &tmpx, &tmpy);
460 tp_filter_motion(tp, &dx, &dy, time);
462 if (tp->scroll.state == SCROLL_STATE_NONE) {
463 /* Require at least one px scrolling to start */
464 if (dx <= -1.0 || dx >= 1.0) {
465 tp->scroll.state = SCROLL_STATE_SCROLLING;
466 tp->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL);
469 if (dy <= -1.0 || dy >= 1.0) {
470 tp->scroll.state = SCROLL_STATE_SCROLLING;
471 tp->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL);
474 if (tp->scroll.state == SCROLL_STATE_NONE)
479 (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL))) {
480 pointer_notify_axis(&tp->device->base,
482 LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
483 li_fixed_from_double(dy));
487 (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL))) {
488 pointer_notify_axis(&tp->device->base,
490 LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
491 li_fixed_from_double(dx));
496 tp_post_scroll_events(struct tp_dispatch *tp, uint32_t time)
498 /* don't scroll if a clickpad is held down */
499 if (!tp->buttons.has_buttons &&
500 (tp->buttons.state || tp->buttons.old_state))
503 if (tp->nfingers_down != 2) {
504 /* terminate scrolling with a zero scroll event to notify
505 * caller that it really ended now */
506 if (tp->scroll.state != SCROLL_STATE_NONE) {
507 tp->scroll.state = SCROLL_STATE_NONE;
508 tp->scroll.direction = 0;
509 if (tp->scroll.direction & LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL)
510 pointer_notify_axis(&tp->device->base,
512 LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
514 if (tp->scroll.direction & LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL)
515 pointer_notify_axis(&tp->device->base,
517 LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
521 tp_post_twofinger_scroll(tp, time);
528 tp_post_events(struct tp_dispatch *tp, uint32_t time)
530 struct tp_touch *t = tp_current_touch(tp);
533 if (tp_post_button_events(tp, time) != 0)
536 if (tp_tap_handle_state(tp, time) != 0)
539 if (tp_post_scroll_events(tp, time) != 0)
542 if (t->history.count >= TOUCHPAD_MIN_SAMPLES) {
543 if (!t->is_pointer) {
544 tp_for_each_touch(tp, t) {
553 tp_get_delta(t, &dx, &dy);
554 tp_filter_motion(tp, &dx, &dy, time);
556 if (dx != 0 || dy != 0)
557 pointer_notify_motion(
560 li_fixed_from_double(dx),
561 li_fixed_from_double(dy));
566 tp_process(struct evdev_dispatch *dispatch,
567 struct evdev_device *device,
568 struct input_event *e,
571 struct tp_dispatch *tp =
572 (struct tp_dispatch *)dispatch;
577 tp_process_absolute(tp, e, time);
579 tp_process_absolute_st(tp, e, time);
582 tp_process_key(tp, e, time);
585 tp_process_state(tp, time);
586 tp_post_events(tp, time);
587 tp_post_process_state(tp, time);
593 tp_destroy(struct evdev_dispatch *dispatch)
595 struct tp_dispatch *tp =
596 (struct tp_dispatch*)dispatch;
601 tp->filter->interface->destroy(tp->filter);
606 static struct evdev_dispatch_interface tp_interface = {
612 tp_init_slots(struct tp_dispatch *tp,
613 struct evdev_device *device)
615 const struct input_absinfo *absinfo;
617 absinfo = libevdev_get_abs_info(device->evdev, ABS_MT_SLOT);
619 tp->ntouches = absinfo->maximum + 1;
620 tp->slot = absinfo->value;
627 { BTN_TOOL_QUINTTAP, 5 },
628 { BTN_TOOL_QUADTAP, 4 },
629 { BTN_TOOL_TRIPLETAP, 3 },
630 { BTN_TOOL_DOUBLETAP, 2 },
638 ARRAY_FOR_EACH(max_touches, m) {
639 if (libevdev_has_event_code(device->evdev,
642 tp->ntouches = m->ntouches;
647 tp->touches = calloc(tp->ntouches,
648 sizeof(struct tp_touch));
656 tp_init_accel(struct tp_dispatch *touchpad, double diagonal)
658 struct motion_filter *accel;
660 touchpad->accel.constant_factor =
661 DEFAULT_CONSTANT_ACCEL_NUMERATOR / diagonal;
662 touchpad->accel.min_factor = DEFAULT_MIN_ACCEL_FACTOR;
663 touchpad->accel.max_factor = DEFAULT_MAX_ACCEL_FACTOR;
665 accel = create_pointer_accelator_filter(tp_accel_profile);
669 touchpad->filter = accel;
675 tp_init_scroll(struct tp_dispatch *tp)
677 tp->scroll.direction = 0;
678 tp->scroll.state = SCROLL_STATE_NONE;
684 tp_init(struct tp_dispatch *tp,
685 struct evdev_device *device)
690 tp->base.interface = &tp_interface;
692 tp->tap.timer_fd = -1;
694 if (tp_init_slots(tp, device) != 0)
697 width = abs(device->abs.max_x - device->abs.min_x);
698 height = abs(device->abs.max_y - device->abs.min_y);
699 diagonal = sqrt(width*width + height*height);
701 tp->hysteresis.margin_x =
702 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
703 tp->hysteresis.margin_y =
704 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
706 if (tp_init_scroll(tp) != 0)
709 if (tp_init_accel(tp, diagonal) != 0)
712 if (tp_init_tap(tp) != 0)
715 if (tp_init_buttons(tp, device) != 0)
721 struct evdev_dispatch *
722 evdev_mt_touchpad_create(struct evdev_device *device)
724 struct tp_dispatch *tp;
726 tp = zalloc(sizeof *tp);
730 if (tp_init(tp, device) != 0) {
731 tp_destroy(&tp->base);