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)];
146 tp_begin_touch(struct tp_dispatch *tp, struct tp_touch *t)
148 if (t->state != TOUCH_UPDATE) {
149 tp_motion_history_reset(t);
151 t->state = TOUCH_BEGIN;
153 assert(tp->nfingers_down >= 1);
154 tp->queued |= TOUCHPAD_EVENT_MOTION;
159 tp_end_touch(struct tp_dispatch *tp, struct tp_touch *t)
161 if (t->state == TOUCH_NONE)
165 t->state = TOUCH_END;
166 assert(tp->nfingers_down >= 1);
168 tp->queued |= TOUCHPAD_EVENT_MOTION;
172 tp_estimate_delta(int x0, int x1, int x2, int x3)
174 return (x0 + x1 - x2 - x3) / 4;
178 tp_get_delta(struct tp_touch *t, double *dx, double *dy)
180 if (t->history.count < 4) {
186 *dx = tp_estimate_delta(tp_motion_history_offset(t, 0)->x,
187 tp_motion_history_offset(t, 1)->x,
188 tp_motion_history_offset(t, 2)->x,
189 tp_motion_history_offset(t, 3)->x);
190 *dy = tp_estimate_delta(tp_motion_history_offset(t, 0)->y,
191 tp_motion_history_offset(t, 1)->y,
192 tp_motion_history_offset(t, 2)->y,
193 tp_motion_history_offset(t, 3)->y);
197 tp_process_absolute(struct tp_dispatch *tp,
198 const struct input_event *e,
201 struct tp_touch *t = tp_current_touch(tp);
204 case ABS_MT_POSITION_X:
208 tp->queued |= TOUCHPAD_EVENT_MOTION;
210 case ABS_MT_POSITION_Y:
214 tp->queued |= TOUCHPAD_EVENT_MOTION;
219 case ABS_MT_TRACKING_ID:
222 tp_begin_touch(tp, t);
229 tp_process_key(struct tp_dispatch *tp,
230 const struct input_event *e,
239 mask = 1 << (e->code - BTN_LEFT);
241 tp->buttons.state |= mask;
242 tp->queued |= TOUCHPAD_EVENT_BUTTON_PRESS;
244 tp->buttons.state &= ~mask;
245 tp->queued |= TOUCHPAD_EVENT_BUTTON_RELEASE;
252 tp_process_state(struct tp_dispatch *tp, uint32_t time)
256 tp_for_each_touch(tp, t) {
260 tp_motion_hysteresis(tp, t);
261 tp_motion_history_push(t);
266 tp_post_process_state(struct tp_dispatch *tp, uint32_t time)
270 tp_for_each_touch(tp, t) {
274 if (t->state == TOUCH_END)
275 t->state = TOUCH_NONE;
276 else if (t->state == TOUCH_BEGIN)
277 t->state = TOUCH_UPDATE;
282 tp->buttons.old_state = tp->buttons.state;
284 tp->queued = TOUCHPAD_EVENT_NONE;
288 tp_post_twofinger_scroll(struct tp_dispatch *tp, uint32_t time)
292 double dx = 0, dy =0;
295 tp_for_each_touch(tp, t) {
298 tp_get_delta(t, &tmpx, &tmpy);
311 tp_filter_motion(tp, &dx, &dy, time);
314 pointer_notify_axis(&tp->device->base,
316 LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
317 li_fixed_from_double(dx));
319 pointer_notify_axis(&tp->device->base,
321 LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
322 li_fixed_from_double(dy));
326 tp_post_button_events(struct tp_dispatch *tp, uint32_t time)
328 uint32_t current, old, button;
331 (TOUCHPAD_EVENT_BUTTON_PRESS|TOUCHPAD_EVENT_BUTTON_RELEASE)) == 0)
334 current = tp->buttons.state;
335 old = tp->buttons.old_state;
338 while (current || old) {
339 enum libinput_pointer_button_state state;
341 if ((current & 0x1) ^ (old & 0x1)) {
342 if (!!(current & 0x1))
343 state = LIBINPUT_POINTER_BUTTON_STATE_PRESSED;
345 state = LIBINPUT_POINTER_BUTTON_STATE_RELEASED;
347 pointer_notify_button(&tp->device->base,
360 tp_post_events(struct tp_dispatch *tp, uint32_t time)
362 struct tp_touch *t = tp_current_touch(tp);
365 if (tp->nfingers_down > 2) {
367 } else if (tp->nfingers_down == 2) {
368 tp_post_twofinger_scroll(tp, time);
372 tp_tap_handle_state(tp, time);
374 if (t->history.count < 4)
377 tp_get_delta(t, &dx, &dy);
378 tp_filter_motion(tp, &dx, &dy, time);
380 if (dx != 0 || dy != 0)
381 pointer_notify_motion(
384 li_fixed_from_double(dx),
385 li_fixed_from_double(dy));
387 tp_post_button_events(tp, time);
391 tp_process(struct evdev_dispatch *dispatch,
392 struct evdev_device *device,
393 struct input_event *e,
396 struct tp_dispatch *tp =
397 (struct tp_dispatch *)dispatch;
401 tp_process_absolute(tp, e, time);
404 tp_process_key(tp, e, time);
407 tp_process_state(tp, time);
408 tp_post_events(tp, time);
409 tp_post_process_state(tp, time);
415 tp_destroy(struct evdev_dispatch *dispatch)
417 struct tp_dispatch *tp =
418 (struct tp_dispatch*)dispatch;
421 tp->filter->interface->destroy(tp->filter);
426 static struct evdev_dispatch_interface tp_interface = {
432 tp_init_slots(struct tp_dispatch *tp,
433 struct evdev_device *device)
435 struct input_absinfo absinfo = {0};
437 ioctl(device->fd, EVIOCGABS(ABS_MT_SLOT), &absinfo);
439 tp->ntouches = absinfo.maximum + 1;
440 tp->touches = calloc(tp->ntouches,
441 sizeof(struct tp_touch));
442 tp->slot = absinfo.value;
448 tp_init_accel(struct tp_dispatch *touchpad, double diagonal)
450 struct motion_filter *accel;
452 touchpad->accel.constant_factor =
453 DEFAULT_CONSTANT_ACCEL_NUMERATOR / diagonal;
454 touchpad->accel.min_factor = DEFAULT_MIN_ACCEL_FACTOR;
455 touchpad->accel.max_factor = DEFAULT_MAX_ACCEL_FACTOR;
457 accel = create_pointer_accelator_filter(tp_accel_profile);
461 touchpad->filter = accel;
467 tp_init(struct tp_dispatch *tp,
468 struct evdev_device *device)
473 tp->base.interface = &tp_interface;
476 if (tp_init_slots(tp, device) != 0)
479 width = abs(device->abs.max_x - device->abs.min_x);
480 height = abs(device->abs.max_y - device->abs.min_y);
481 diagonal = sqrt(width*width + height*height);
483 tp->hysteresis.margin_x =
484 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
485 tp->hysteresis.margin_y =
486 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
488 if (tp_init_accel(tp, diagonal) != 0)
491 if (tp_init_tap(tp) != 0)
497 struct evdev_dispatch *
498 evdev_mt_touchpad_create(struct evdev_device *device)
500 struct tp_dispatch *tp;
502 tp = zalloc(sizeof *tp);
506 if (tp_init(tp, device) != 0) {
507 tp_destroy(&tp->base);