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;
46 return center + diff + margin;
50 tp_accel_profile(struct motion_filter *filter,
55 struct tp_dispatch *tp =
56 (struct tp_dispatch *) data;
60 accel_factor = velocity * tp->accel.constant_factor;
62 if (accel_factor > tp->accel.max_factor)
63 accel_factor = tp->accel.max_factor;
64 else if (accel_factor < tp->accel.min_factor)
65 accel_factor = tp->accel.min_factor;
70 static inline struct tp_motion *
71 tp_motion_history_offset(struct tp_touch *t, int offset)
74 (t->history.index - offset + TOUCHPAD_HISTORY_LENGTH) %
75 TOUCHPAD_HISTORY_LENGTH;
77 return &t->history.samples[offset_index];
81 tp_filter_motion(struct tp_dispatch *tp,
82 double *dx, double *dy, uint64_t time)
84 struct motion_params motion;
86 motion.dx = *dx * tp->accel.x_scale_coeff;
87 motion.dy = *dy * tp->accel.y_scale_coeff;
89 filter_dispatch(tp->filter, &motion, tp, time);
96 tp_motion_history_push(struct tp_touch *t)
98 int motion_index = (t->history.index + 1) % TOUCHPAD_HISTORY_LENGTH;
100 if (t->history.count < TOUCHPAD_HISTORY_LENGTH)
103 t->history.samples[motion_index].x = t->x;
104 t->history.samples[motion_index].y = t->y;
105 t->history.index = motion_index;
109 tp_motion_hysteresis(struct tp_dispatch *tp,
115 if (t->history.count == 0) {
116 t->hysteresis.center_x = t->x;
117 t->hysteresis.center_y = t->y;
120 t->hysteresis.center_x,
121 tp->hysteresis.margin_x);
123 t->hysteresis.center_y,
124 tp->hysteresis.margin_y);
125 t->hysteresis.center_x = x;
126 t->hysteresis.center_y = y;
133 tp_motion_history_reset(struct tp_touch *t)
135 t->history.count = 0;
138 static inline struct tp_touch *
139 tp_current_touch(struct tp_dispatch *tp)
141 return &tp->touches[min(tp->slot, tp->ntouches - 1)];
144 static inline struct tp_touch *
145 tp_get_touch(struct tp_dispatch *tp, unsigned int slot)
147 assert(slot < tp->ntouches);
148 return &tp->touches[slot];
152 tp_begin_touch(struct tp_dispatch *tp, struct tp_touch *t)
154 if (t->state != TOUCH_UPDATE) {
155 tp_motion_history_reset(t);
157 t->state = TOUCH_BEGIN;
158 t->pinned.is_pinned = false;
160 assert(tp->nfingers_down >= 1);
161 tp->queued |= TOUCHPAD_EVENT_MOTION;
166 tp_end_touch(struct tp_dispatch *tp, struct tp_touch *t)
168 if (t->state == TOUCH_NONE)
172 t->is_pointer = false;
173 t->state = TOUCH_END;
174 t->pinned.is_pinned = false;
175 assert(tp->nfingers_down >= 1);
177 tp->queued |= TOUCHPAD_EVENT_MOTION;
181 tp_estimate_delta(int x0, int x1, int x2, int x3)
183 return (x0 + x1 - x2 - x3) / 4;
187 tp_get_delta(struct tp_touch *t, double *dx, double *dy)
189 if (t->history.count < 4) {
195 *dx = tp_estimate_delta(tp_motion_history_offset(t, 0)->x,
196 tp_motion_history_offset(t, 1)->x,
197 tp_motion_history_offset(t, 2)->x,
198 tp_motion_history_offset(t, 3)->x);
199 *dy = tp_estimate_delta(tp_motion_history_offset(t, 0)->y,
200 tp_motion_history_offset(t, 1)->y,
201 tp_motion_history_offset(t, 2)->y,
202 tp_motion_history_offset(t, 3)->y);
206 tp_process_absolute(struct tp_dispatch *tp,
207 const struct input_event *e,
210 struct tp_touch *t = tp_current_touch(tp);
213 case ABS_MT_POSITION_X:
217 tp->queued |= TOUCHPAD_EVENT_MOTION;
219 case ABS_MT_POSITION_Y:
223 tp->queued |= TOUCHPAD_EVENT_MOTION;
228 case ABS_MT_TRACKING_ID:
231 tp_begin_touch(tp, t);
238 tp_process_absolute_st(struct tp_dispatch *tp,
239 const struct input_event *e,
242 struct tp_touch *t = tp_current_touch(tp);
249 tp->queued |= TOUCHPAD_EVENT_MOTION;
255 tp->queued |= TOUCHPAD_EVENT_MOTION;
261 tp_process_fake_touch(struct tp_dispatch *tp,
262 const struct input_event *e,
266 unsigned int fake_touches;
267 unsigned int nfake_touches;
271 if (e->code != BTN_TOUCH &&
272 (e->code < BTN_TOOL_DOUBLETAP || e->code > BTN_TOOL_QUADTAP))
275 shift = e->code == BTN_TOUCH ? 0 : (e->code - BTN_TOOL_DOUBLETAP + 1);
278 tp->fake_touches |= 1 << shift;
280 tp->fake_touches &= ~(0x1 << shift);
282 fake_touches = tp->fake_touches;
284 while (fake_touches) {
289 for (i = 0; i < tp->ntouches; i++) {
290 t = tp_get_touch(tp, i);
291 if (i >= nfake_touches) {
292 if (t->state != TOUCH_NONE) {
296 } else if (t->state != TOUCH_UPDATE &&
297 t->state != TOUCH_BEGIN) {
298 t->state = TOUCH_NONE;
299 tp_begin_touch(tp, t);
305 assert(tp->nfingers_down == nfake_touches);
309 tp_process_key(struct tp_dispatch *tp,
310 const struct input_event *e,
317 tp_process_button(tp, e, time);
320 case BTN_TOOL_DOUBLETAP:
321 case BTN_TOOL_TRIPLETAP:
322 case BTN_TOOL_QUADTAP:
324 tp_process_fake_touch(tp, e, time);
330 tp_unpin_finger(struct tp_dispatch *tp, struct tp_touch *t)
332 unsigned int xdist, ydist;
334 if (!t->pinned.is_pinned)
337 xdist = abs(t->x - t->pinned.center_x);
338 ydist = abs(t->y - t->pinned.center_y);
340 if (xdist * xdist + ydist * ydist >=
341 tp->buttons.motion_dist * tp->buttons.motion_dist) {
342 t->pinned.is_pinned = false;
343 tp_set_pointer(tp, t);
348 tp_pin_fingers(struct tp_dispatch *tp)
352 tp_for_each_touch(tp, t) {
353 t->is_pointer = false;
354 t->pinned.is_pinned = true;
355 t->pinned.center_x = t->x;
356 t->pinned.center_y = t->y;
361 tp_touch_active(struct tp_dispatch *tp, struct tp_touch *t)
363 return (t->state == TOUCH_BEGIN || t->state == TOUCH_UPDATE) &&
364 !t->pinned.is_pinned && tp_button_touch_active(tp, t);
368 tp_set_pointer(struct tp_dispatch *tp, struct tp_touch *t)
370 struct tp_touch *tmp = NULL;
372 /* Only set the touch as pointer if we don't have one yet */
373 tp_for_each_touch(tp, tmp) {
378 if (tp_touch_active(tp, t))
379 t->is_pointer = true;
383 tp_process_state(struct tp_dispatch *tp, uint64_t time)
386 struct tp_touch *first = tp_get_touch(tp, 0);
388 tp_for_each_touch(tp, t) {
389 if (!tp->has_mt && t != first && first->fake) {
393 t->dirty = first->dirty;
394 } else if (!t->dirty)
397 tp_motion_hysteresis(tp, t);
398 tp_motion_history_push(t);
400 tp_unpin_finger(tp, t);
403 tp_button_handle_state(tp, time);
406 * We have a physical button down event on a clickpad. To avoid
407 * spurious pointer moves by the clicking finger we pin all fingers.
408 * We unpin fingers when they move more then a certain threshold to
409 * to allow drag and drop.
411 if ((tp->queued & TOUCHPAD_EVENT_BUTTON_PRESS) &&
412 tp->buttons.is_clickpad)
417 tp_post_process_state(struct tp_dispatch *tp, uint64_t time)
421 tp_for_each_touch(tp, t) {
425 if (t->state == TOUCH_END) {
426 t->state = TOUCH_NONE;
428 } else if (t->state == TOUCH_BEGIN)
429 t->state = TOUCH_UPDATE;
434 tp->buttons.old_state = tp->buttons.state;
436 tp->queued = TOUCHPAD_EVENT_NONE;
440 tp_post_twofinger_scroll(struct tp_dispatch *tp, uint64_t time)
444 double dx = 0, dy =0;
447 tp_for_each_touch(tp, t) {
448 if (tp_touch_active(tp, t) && t->dirty) {
450 tp_get_delta(t, &tmpx, &tmpy);
455 /* Stop spurious MOTION events at the end of scrolling */
456 t->is_pointer = false;
465 tp_filter_motion(tp, &dx, &dy, time);
467 /* Require at least three px scrolling to start */
468 if (dy <= -3.0 || dy >= 3.0)
469 tp->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
471 if (dx <= -3.0 || dx >= 3.0)
472 tp->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
475 (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))) {
476 pointer_notify_axis(&tp->device->base,
478 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
483 (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))) {
484 pointer_notify_axis(&tp->device->base,
486 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
492 tp_stop_scroll_events(struct tp_dispatch *tp, uint64_t time)
494 /* terminate scrolling with a zero scroll event */
495 if (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
496 pointer_notify_axis(&tp->device->base,
498 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
500 if (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
501 pointer_notify_axis(&tp->device->base,
503 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
506 tp->scroll.direction = 0;
510 tp_post_scroll_events(struct tp_dispatch *tp, uint64_t time)
513 int nfingers_down = 0;
515 /* Only count active touches for 2 finger scrolling */
516 tp_for_each_touch(tp, t) {
517 if (tp_touch_active(tp, t))
521 if (nfingers_down != 2) {
522 tp_stop_scroll_events(tp, time);
526 tp_post_twofinger_scroll(tp, time);
531 tp_post_events(struct tp_dispatch *tp, uint64_t time)
533 struct tp_touch *t = tp_current_touch(tp);
537 consumed |= tp_tap_handle_state(tp, time);
538 consumed |= tp_post_button_events(tp, time);
541 tp_stop_scroll_events(tp, time);
545 if (tp_post_scroll_events(tp, time) != 0)
548 if (t->history.count >= TOUCHPAD_MIN_SAMPLES) {
549 if (!t->is_pointer) {
550 tp_for_each_touch(tp, t) {
559 tp_get_delta(t, &dx, &dy);
560 tp_filter_motion(tp, &dx, &dy, time);
562 if (dx != 0.0 || dy != 0.0)
563 pointer_notify_motion(&tp->device->base, time, dx, dy);
568 tp_process(struct evdev_dispatch *dispatch,
569 struct evdev_device *device,
570 struct input_event *e,
573 struct tp_dispatch *tp =
574 (struct tp_dispatch *)dispatch;
579 tp_process_absolute(tp, e, time);
581 tp_process_absolute_st(tp, e, time);
584 tp_process_key(tp, e, time);
587 tp_process_state(tp, time);
588 tp_post_events(tp, time);
589 tp_post_process_state(tp, time);
595 tp_destroy(struct evdev_dispatch *dispatch)
597 struct tp_dispatch *tp =
598 (struct tp_dispatch*)dispatch;
601 tp_destroy_buttons(tp);
603 motion_filter_destroy(tp->filter);
608 static struct evdev_dispatch_interface tp_interface = {
614 tp_init_touch(struct tp_dispatch *tp,
621 tp_init_slots(struct tp_dispatch *tp,
622 struct evdev_device *device)
625 const struct input_absinfo *absinfo;
627 absinfo = libevdev_get_abs_info(device->evdev, ABS_MT_SLOT);
629 tp->ntouches = absinfo->maximum + 1;
630 tp->slot = absinfo->value;
637 { BTN_TOOL_QUINTTAP, 5 },
638 { BTN_TOOL_QUADTAP, 4 },
639 { BTN_TOOL_TRIPLETAP, 3 },
640 { BTN_TOOL_DOUBLETAP, 2 },
648 ARRAY_FOR_EACH(max_touches, m) {
649 if (libevdev_has_event_code(device->evdev,
652 tp->ntouches = m->ntouches;
657 tp->touches = calloc(tp->ntouches,
658 sizeof(struct tp_touch));
662 for (i = 0; i < tp->ntouches; i++)
663 tp_init_touch(tp, &tp->touches[i]);
669 calculate_scale_coefficients(struct tp_dispatch *tp)
674 res_x = libevdev_get_abs_resolution(tp->device->evdev,
676 res_y = libevdev_get_abs_resolution(tp->device->evdev,
679 res_x = libevdev_get_abs_resolution(tp->device->evdev,
681 res_y = libevdev_get_abs_resolution(tp->device->evdev,
685 if (res_x <= 0 || res_y <= 0) {
686 tp->accel.x_scale_coeff = 1.0;
687 tp->accel.y_scale_coeff = 1.0;
688 } else if (res_x > res_y) {
689 tp->accel.x_scale_coeff = res_y / (double) res_x;
690 tp->accel.y_scale_coeff = 1.0f;
692 tp->accel.y_scale_coeff = res_x / (double) res_y;
693 tp->accel.x_scale_coeff = 1.0f;
698 tp_init_accel(struct tp_dispatch *touchpad, double diagonal)
700 struct motion_filter *accel;
702 calculate_scale_coefficients(touchpad);
704 touchpad->accel.constant_factor =
705 DEFAULT_CONSTANT_ACCEL_NUMERATOR / diagonal;
706 touchpad->accel.min_factor = DEFAULT_MIN_ACCEL_FACTOR;
707 touchpad->accel.max_factor = DEFAULT_MAX_ACCEL_FACTOR;
709 accel = create_pointer_accelator_filter(tp_accel_profile);
713 touchpad->filter = accel;
719 tp_init_scroll(struct tp_dispatch *tp)
721 tp->scroll.direction = 0;
727 tp_init(struct tp_dispatch *tp,
728 struct evdev_device *device)
733 tp->base.interface = &tp_interface;
736 if (tp_init_slots(tp, device) != 0)
739 width = abs(device->abs.absinfo_x->maximum -
740 device->abs.absinfo_x->minimum);
741 height = abs(device->abs.absinfo_y->maximum -
742 device->abs.absinfo_y->minimum);
743 diagonal = sqrt(width*width + height*height);
745 tp->hysteresis.margin_x =
746 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
747 tp->hysteresis.margin_y =
748 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
750 if (tp_init_scroll(tp) != 0)
753 if (tp_init_accel(tp, diagonal) != 0)
756 if (tp_init_tap(tp) != 0)
759 if (tp_init_buttons(tp, device) != 0)
765 struct evdev_dispatch *
766 evdev_mt_touchpad_create(struct evdev_device *device)
768 struct tp_dispatch *tp;
770 tp = zalloc(sizeof *tp);
774 if (tp_init(tp, device) != 0) {
775 tp_destroy(&tp->base);