accel_profile_smooth_simple: Fix jump in acceleration curve
[platform/upstream/libinput.git] / src / evdev-mt-touchpad.c
1 /*
2  * Copyright © 2014 Red Hat, Inc.
3  *
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.
13  *
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.
21  */
22
23 #include "config.h"
24
25 #include <assert.h>
26 #include <math.h>
27 #include <stdbool.h>
28
29 #include "evdev-mt-touchpad.h"
30
31 #define DEFAULT_ACCEL_NUMERATOR 1200.0
32 #define DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR 700.0
33
34 static inline int
35 tp_hysteresis(int in, int center, int margin)
36 {
37         int diff = in - center;
38         if (abs(diff) <= margin)
39                 return center;
40
41         if (diff > margin)
42                 return center + diff - margin;
43         else
44                 return center + diff + margin;
45 }
46
47 static inline struct tp_motion *
48 tp_motion_history_offset(struct tp_touch *t, int offset)
49 {
50         int offset_index =
51                 (t->history.index - offset + TOUCHPAD_HISTORY_LENGTH) %
52                 TOUCHPAD_HISTORY_LENGTH;
53
54         return &t->history.samples[offset_index];
55 }
56
57 static void
58 tp_filter_motion(struct tp_dispatch *tp,
59                  double *dx, double *dy, uint64_t time)
60 {
61         struct motion_params motion;
62
63         motion.dx = *dx * tp->accel.x_scale_coeff;
64         motion.dy = *dy * tp->accel.y_scale_coeff;
65
66         filter_dispatch(tp->filter, &motion, tp, time);
67
68         *dx = motion.dx;
69         *dy = motion.dy;
70 }
71
72 static inline void
73 tp_motion_history_push(struct tp_touch *t)
74 {
75         int motion_index = (t->history.index + 1) % TOUCHPAD_HISTORY_LENGTH;
76
77         if (t->history.count < TOUCHPAD_HISTORY_LENGTH)
78                 t->history.count++;
79
80         t->history.samples[motion_index].x = t->x;
81         t->history.samples[motion_index].y = t->y;
82         t->history.index = motion_index;
83 }
84
85 static inline void
86 tp_motion_hysteresis(struct tp_dispatch *tp,
87                      struct tp_touch *t)
88 {
89         int x = t->x,
90             y = t->y;
91
92         if (t->history.count == 0) {
93                 t->hysteresis.center_x = t->x;
94                 t->hysteresis.center_y = t->y;
95         } else {
96                 x = tp_hysteresis(x,
97                                   t->hysteresis.center_x,
98                                   tp->hysteresis.margin_x);
99                 y = tp_hysteresis(y,
100                                   t->hysteresis.center_y,
101                                   tp->hysteresis.margin_y);
102                 t->hysteresis.center_x = x;
103                 t->hysteresis.center_y = y;
104                 t->x = x;
105                 t->y = y;
106         }
107 }
108
109 static inline void
110 tp_motion_history_reset(struct tp_touch *t)
111 {
112         t->history.count = 0;
113 }
114
115 static inline struct tp_touch *
116 tp_current_touch(struct tp_dispatch *tp)
117 {
118         return &tp->touches[min(tp->slot, tp->ntouches - 1)];
119 }
120
121 static inline struct tp_touch *
122 tp_get_touch(struct tp_dispatch *tp, unsigned int slot)
123 {
124         assert(slot < tp->ntouches);
125         return &tp->touches[slot];
126 }
127
128 static inline void
129 tp_begin_touch(struct tp_dispatch *tp, struct tp_touch *t)
130 {
131         if (t->state != TOUCH_UPDATE) {
132                 tp_motion_history_reset(t);
133                 t->dirty = true;
134                 t->state = TOUCH_BEGIN;
135                 t->pinned.is_pinned = false;
136                 tp->nfingers_down++;
137                 assert(tp->nfingers_down >= 1);
138                 tp->queued |= TOUCHPAD_EVENT_MOTION;
139         }
140 }
141
142 static inline void
143 tp_end_touch(struct tp_dispatch *tp, struct tp_touch *t)
144 {
145         if (t->state == TOUCH_NONE)
146                 return;
147
148         t->dirty = true;
149         t->is_pointer = false;
150         t->state = TOUCH_END;
151         t->pinned.is_pinned = false;
152         assert(tp->nfingers_down >= 1);
153         tp->nfingers_down--;
154         tp->queued |= TOUCHPAD_EVENT_MOTION;
155 }
156
157 static double
158 tp_estimate_delta(int x0, int x1, int x2, int x3)
159 {
160         return (x0 + x1 - x2 - x3) / 4;
161 }
162
163 void
164 tp_get_delta(struct tp_touch *t, double *dx, double *dy)
165 {
166         if (t->history.count < 4) {
167                 *dx = 0;
168                 *dy = 0;
169                 return;
170         }
171
172         *dx = tp_estimate_delta(tp_motion_history_offset(t, 0)->x,
173                                 tp_motion_history_offset(t, 1)->x,
174                                 tp_motion_history_offset(t, 2)->x,
175                                 tp_motion_history_offset(t, 3)->x);
176         *dy = tp_estimate_delta(tp_motion_history_offset(t, 0)->y,
177                                 tp_motion_history_offset(t, 1)->y,
178                                 tp_motion_history_offset(t, 2)->y,
179                                 tp_motion_history_offset(t, 3)->y);
180 }
181
182 static void
183 tp_process_absolute(struct tp_dispatch *tp,
184                     const struct input_event *e,
185                     uint64_t time)
186 {
187         struct tp_touch *t = tp_current_touch(tp);
188
189         switch(e->code) {
190         case ABS_MT_POSITION_X:
191                 t->x = e->value;
192                 t->millis = time;
193                 t->dirty = true;
194                 tp->queued |= TOUCHPAD_EVENT_MOTION;
195                 break;
196         case ABS_MT_POSITION_Y:
197                 t->y = e->value;
198                 t->millis = time;
199                 t->dirty = true;
200                 tp->queued |= TOUCHPAD_EVENT_MOTION;
201                 break;
202         case ABS_MT_SLOT:
203                 tp->slot = e->value;
204                 break;
205         case ABS_MT_TRACKING_ID:
206                 t->millis = time;
207                 if (e->value != -1)
208                         tp_begin_touch(tp, t);
209                 else
210                         tp_end_touch(tp, t);
211         }
212 }
213
214 static void
215 tp_process_absolute_st(struct tp_dispatch *tp,
216                        const struct input_event *e,
217                        uint64_t time)
218 {
219         struct tp_touch *t = tp_current_touch(tp);
220
221         switch(e->code) {
222         case ABS_X:
223                 t->x = e->value;
224                 t->millis = time;
225                 t->dirty = true;
226                 tp->queued |= TOUCHPAD_EVENT_MOTION;
227                 break;
228         case ABS_Y:
229                 t->y = e->value;
230                 t->millis = time;
231                 t->dirty = true;
232                 tp->queued |= TOUCHPAD_EVENT_MOTION;
233                 break;
234         }
235 }
236
237 static void
238 tp_process_fake_touch(struct tp_dispatch *tp,
239                       const struct input_event *e,
240                       uint64_t time)
241 {
242         struct tp_touch *t;
243         unsigned int fake_touches;
244         unsigned int nfake_touches;
245         unsigned int i;
246         unsigned int shift;
247
248         if (e->code != BTN_TOUCH &&
249             (e->code < BTN_TOOL_DOUBLETAP || e->code > BTN_TOOL_QUADTAP))
250                 return;
251
252         shift = e->code == BTN_TOUCH ? 0 : (e->code - BTN_TOOL_DOUBLETAP + 1);
253
254         if (e->value)
255                 tp->fake_touches |= 1 << shift;
256         else
257                 tp->fake_touches &= ~(0x1 << shift);
258
259         fake_touches = tp->fake_touches;
260         nfake_touches = 0;
261         while (fake_touches) {
262                 nfake_touches++;
263                 fake_touches >>= 1;
264         }
265
266         for (i = 0; i < tp->ntouches; i++) {
267                 t = tp_get_touch(tp, i);
268                 if (i >= nfake_touches) {
269                         if (t->state != TOUCH_NONE) {
270                                 tp_end_touch(tp, t);
271                                 t->millis = time;
272                         }
273                 } else if (t->state != TOUCH_UPDATE &&
274                            t->state != TOUCH_BEGIN) {
275                         t->state = TOUCH_NONE;
276                         tp_begin_touch(tp, t);
277                         t->millis = time;
278                         t->fake =true;
279                 }
280         }
281
282         assert(tp->nfingers_down == nfake_touches);
283 }
284
285 static void
286 tp_process_key(struct tp_dispatch *tp,
287                const struct input_event *e,
288                uint64_t time)
289 {
290         switch (e->code) {
291                 case BTN_LEFT:
292                 case BTN_MIDDLE:
293                 case BTN_RIGHT:
294                         tp_process_button(tp, e, time);
295                         break;
296                 case BTN_TOUCH:
297                 case BTN_TOOL_DOUBLETAP:
298                 case BTN_TOOL_TRIPLETAP:
299                 case BTN_TOOL_QUADTAP:
300                         if (!tp->has_mt)
301                                 tp_process_fake_touch(tp, e, time);
302                         break;
303         }
304 }
305
306 static void
307 tp_unpin_finger(struct tp_dispatch *tp, struct tp_touch *t)
308 {
309         unsigned int xdist, ydist;
310
311         if (!t->pinned.is_pinned)
312                 return;
313
314         xdist = abs(t->x - t->pinned.center_x);
315         ydist = abs(t->y - t->pinned.center_y);
316
317         if (xdist * xdist + ydist * ydist >=
318                         tp->buttons.motion_dist * tp->buttons.motion_dist) {
319                 t->pinned.is_pinned = false;
320                 tp_set_pointer(tp, t);
321         }
322 }
323
324 static void
325 tp_pin_fingers(struct tp_dispatch *tp)
326 {
327         struct tp_touch *t;
328
329         tp_for_each_touch(tp, t) {
330                 t->is_pointer = false;
331                 t->pinned.is_pinned = true;
332                 t->pinned.center_x = t->x;
333                 t->pinned.center_y = t->y;
334         }
335 }
336
337 static int
338 tp_touch_active(struct tp_dispatch *tp, struct tp_touch *t)
339 {
340         return (t->state == TOUCH_BEGIN || t->state == TOUCH_UPDATE) &&
341                 !t->pinned.is_pinned && tp_button_touch_active(tp, t);
342 }
343
344 void
345 tp_set_pointer(struct tp_dispatch *tp, struct tp_touch *t)
346 {
347         struct tp_touch *tmp = NULL;
348
349         /* Only set the touch as pointer if we don't have one yet */
350         tp_for_each_touch(tp, tmp) {
351                 if (tmp->is_pointer)
352                         return;
353         }
354
355         if (tp_touch_active(tp, t))
356                 t->is_pointer = true;
357 }
358
359 static void
360 tp_process_state(struct tp_dispatch *tp, uint64_t time)
361 {
362         struct tp_touch *t;
363         struct tp_touch *first = tp_get_touch(tp, 0);
364
365         tp_for_each_touch(tp, t) {
366                 if (!tp->has_mt && t != first && first->fake) {
367                         t->x = first->x;
368                         t->y = first->y;
369                         if (!t->dirty)
370                                 t->dirty = first->dirty;
371                 } else if (!t->dirty)
372                         continue;
373
374                 tp_motion_hysteresis(tp, t);
375                 tp_motion_history_push(t);
376
377                 tp_unpin_finger(tp, t);
378         }
379
380         tp_button_handle_state(tp, time);
381
382         /*
383          * We have a physical button down event on a clickpad. To avoid
384          * spurious pointer moves by the clicking finger we pin all fingers.
385          * We unpin fingers when they move more then a certain threshold to
386          * to allow drag and drop.
387          */
388         if ((tp->queued & TOUCHPAD_EVENT_BUTTON_PRESS) &&
389             tp->buttons.is_clickpad)
390                 tp_pin_fingers(tp);
391 }
392
393 static void
394 tp_post_process_state(struct tp_dispatch *tp, uint64_t time)
395 {
396         struct tp_touch *t;
397
398         tp_for_each_touch(tp, t) {
399                 if (!t->dirty)
400                         continue;
401
402                 if (t->state == TOUCH_END) {
403                         t->state = TOUCH_NONE;
404                         t->fake = false;
405                 } else if (t->state == TOUCH_BEGIN)
406                         t->state = TOUCH_UPDATE;
407
408                 t->dirty = false;
409         }
410
411         tp->buttons.old_state = tp->buttons.state;
412
413         tp->queued = TOUCHPAD_EVENT_NONE;
414 }
415
416 static void
417 tp_post_twofinger_scroll(struct tp_dispatch *tp, uint64_t time)
418 {
419         struct tp_touch *t;
420         int nchanged = 0;
421         double dx = 0, dy =0;
422         double tmpx, tmpy;
423
424         tp_for_each_touch(tp, t) {
425                 if (tp_touch_active(tp, t) && t->dirty) {
426                         nchanged++;
427                         tp_get_delta(t, &tmpx, &tmpy);
428
429                         dx += tmpx;
430                         dy += tmpy;
431                 }
432                 /* Stop spurious MOTION events at the end of scrolling */
433                 t->is_pointer = false;
434         }
435
436         if (nchanged == 0)
437                 return;
438
439         dx /= nchanged;
440         dy /= nchanged;
441
442         tp_filter_motion(tp, &dx, &dy, time);
443
444         /* Require at least five px scrolling to start */
445         if (dy <= -5.0 || dy >= 5.0)
446                 tp->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
447
448         if (dx <= -5.0 || dx >= 5.0)
449                 tp->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
450
451         if (dy != 0.0 &&
452             (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))) {
453                 pointer_notify_axis(&tp->device->base,
454                                     time,
455                                     LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
456                                     dy);
457         }
458
459         if (dx != 0.0 &&
460             (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))) {
461                 pointer_notify_axis(&tp->device->base,
462                                     time,
463                                     LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
464                                     dx);
465         }
466 }
467
468 static void
469 tp_stop_scroll_events(struct tp_dispatch *tp, uint64_t time)
470 {
471         /* terminate scrolling with a zero scroll event */
472         if (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
473                 pointer_notify_axis(&tp->device->base,
474                                     time,
475                                     LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
476                                     0);
477         if (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
478                 pointer_notify_axis(&tp->device->base,
479                                     time,
480                                     LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
481                                     0);
482
483         tp->scroll.direction = 0;
484 }
485
486 static int
487 tp_post_scroll_events(struct tp_dispatch *tp, uint64_t time)
488 {
489         struct tp_touch *t;
490         int nfingers_down = 0;
491
492         /* Only count active touches for 2 finger scrolling */
493         tp_for_each_touch(tp, t) {
494                 if (tp_touch_active(tp, t))
495                         nfingers_down++;
496         }
497
498         if (nfingers_down != 2) {
499                 tp_stop_scroll_events(tp, time);
500                 return 0;
501         }
502
503         tp_post_twofinger_scroll(tp, time);
504         return 1;
505 }
506
507 static void
508 tp_post_events(struct tp_dispatch *tp, uint64_t time)
509 {
510         struct tp_touch *t = tp_current_touch(tp);
511         double dx, dy;
512         int consumed = 0;
513
514         consumed |= tp_tap_handle_state(tp, time);
515         consumed |= tp_post_button_events(tp, time);
516
517         if (consumed) {
518                 tp_stop_scroll_events(tp, time);
519                 return;
520         }
521
522         if (tp_post_scroll_events(tp, time) != 0)
523                 return;
524
525         if (t->history.count >= TOUCHPAD_MIN_SAMPLES) {
526                 if (!t->is_pointer) {
527                         tp_for_each_touch(tp, t) {
528                                 if (t->is_pointer)
529                                         break;
530                         }
531                 }
532
533                 if (!t->is_pointer)
534                         return;
535
536                 tp_get_delta(t, &dx, &dy);
537                 tp_filter_motion(tp, &dx, &dy, time);
538
539                 if (dx != 0.0 || dy != 0.0)
540                         pointer_notify_motion(&tp->device->base, time, dx, dy);
541         }
542 }
543
544 static void
545 tp_process(struct evdev_dispatch *dispatch,
546            struct evdev_device *device,
547            struct input_event *e,
548            uint64_t time)
549 {
550         struct tp_dispatch *tp =
551                 (struct tp_dispatch *)dispatch;
552
553         switch (e->type) {
554         case EV_ABS:
555                 if (tp->has_mt)
556                         tp_process_absolute(tp, e, time);
557                 else
558                         tp_process_absolute_st(tp, e, time);
559                 break;
560         case EV_KEY:
561                 tp_process_key(tp, e, time);
562                 break;
563         case EV_SYN:
564                 tp_process_state(tp, time);
565                 tp_post_events(tp, time);
566                 tp_post_process_state(tp, time);
567                 break;
568         }
569 }
570
571 static void
572 tp_destroy(struct evdev_dispatch *dispatch)
573 {
574         struct tp_dispatch *tp =
575                 (struct tp_dispatch*)dispatch;
576
577         tp_destroy_tap(tp);
578         tp_destroy_buttons(tp);
579
580         filter_destroy(tp->filter);
581         free(tp->touches);
582         free(tp);
583 }
584
585 static struct evdev_dispatch_interface tp_interface = {
586         tp_process,
587         tp_destroy
588 };
589
590 static void
591 tp_init_touch(struct tp_dispatch *tp,
592               struct tp_touch *t)
593 {
594         t->tp = tp;
595 }
596
597 static int
598 tp_init_slots(struct tp_dispatch *tp,
599               struct evdev_device *device)
600 {
601         size_t i;
602         const struct input_absinfo *absinfo;
603
604         absinfo = libevdev_get_abs_info(device->evdev, ABS_MT_SLOT);
605         if (absinfo) {
606                 tp->ntouches = absinfo->maximum + 1;
607                 tp->slot = absinfo->value;
608                 tp->has_mt = true;
609         } else {
610                 struct map {
611                         unsigned int code;
612                         int ntouches;
613                 } max_touches[] = {
614                         { BTN_TOOL_QUINTTAP, 5 },
615                         { BTN_TOOL_QUADTAP, 4 },
616                         { BTN_TOOL_TRIPLETAP, 3 },
617                         { BTN_TOOL_DOUBLETAP, 2 },
618                 };
619                 struct map *m;
620
621                 tp->slot = 0;
622                 tp->has_mt = false;
623                 tp->ntouches = 1;
624
625                 ARRAY_FOR_EACH(max_touches, m) {
626                         if (libevdev_has_event_code(device->evdev,
627                                                     EV_KEY,
628                                                     m->code)) {
629                                 tp->ntouches = m->ntouches;
630                                 break;
631                         }
632                 }
633         }
634         tp->touches = calloc(tp->ntouches,
635                              sizeof(struct tp_touch));
636         if (!tp->touches)
637                 return -1;
638
639         for (i = 0; i < tp->ntouches; i++)
640                 tp_init_touch(tp, &tp->touches[i]);
641
642         return 0;
643 }
644
645 static int
646 tp_init_accel(struct tp_dispatch *tp, double diagonal)
647 {
648         struct motion_filter *accel;
649         int res_x, res_y;
650
651         if (tp->has_mt) {
652                 res_x = libevdev_get_abs_resolution(tp->device->evdev,
653                                                     ABS_MT_POSITION_X);
654                 res_y = libevdev_get_abs_resolution(tp->device->evdev,
655                                                     ABS_MT_POSITION_Y);
656         } else {
657                 res_x = libevdev_get_abs_resolution(tp->device->evdev,
658                                                     ABS_X);
659                 res_y = libevdev_get_abs_resolution(tp->device->evdev,
660                                                     ABS_Y);
661         }
662
663         /*
664          * Not all touchpads report the same amount of units/mm (resolution).
665          * Normalize motion events to a resolution of 10 units/mm as base
666          * (unaccelerated) speed. This also evens out any differences in x
667          * and y resolution, so that a circle on the touchpad does not turn
668          * into an elipse on the screen.
669          */
670         if (res_x > 1 && res_y > 1) {
671                 tp->accel.x_scale_coeff = 10.0 / res_x;
672                 tp->accel.y_scale_coeff = 10.0 / res_y;
673         } else {
674         /*
675          * For touchpads where the driver does not provide resolution, fall
676          * back to scaling motion events based on the diagonal size in units.
677          */
678                 tp->accel.x_scale_coeff = DEFAULT_ACCEL_NUMERATOR / diagonal;
679                 tp->accel.y_scale_coeff = DEFAULT_ACCEL_NUMERATOR / diagonal;
680         }
681
682         accel = create_pointer_accelator_filter(
683                         pointer_accel_profile_smooth_simple);
684         if (accel == NULL)
685                 return -1;
686
687         tp->filter = accel;
688
689         return 0;
690 }
691
692 static int
693 tp_init_scroll(struct tp_dispatch *tp)
694 {
695         tp->scroll.direction = 0;
696
697         return 0;
698 }
699
700 static int
701 tp_init(struct tp_dispatch *tp,
702         struct evdev_device *device)
703 {
704         int width, height;
705         double diagonal;
706
707         tp->base.interface = &tp_interface;
708         tp->device = device;
709
710         if (tp_init_slots(tp, device) != 0)
711                 return -1;
712
713         width = abs(device->abs.absinfo_x->maximum -
714                     device->abs.absinfo_x->minimum);
715         height = abs(device->abs.absinfo_y->maximum -
716                      device->abs.absinfo_y->minimum);
717         diagonal = sqrt(width*width + height*height);
718
719         tp->hysteresis.margin_x =
720                 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
721         tp->hysteresis.margin_y =
722                 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
723
724         if (tp_init_scroll(tp) != 0)
725                 return -1;
726
727         if (tp_init_accel(tp, diagonal) != 0)
728                 return -1;
729
730         if (tp_init_tap(tp) != 0)
731                 return -1;
732
733         if (tp_init_buttons(tp, device) != 0)
734                 return -1;
735
736         return 0;
737 }
738
739 struct evdev_dispatch *
740 evdev_mt_touchpad_create(struct evdev_device *device)
741 {
742         struct tp_dispatch *tp;
743
744         tp = zalloc(sizeof *tp);
745         if (!tp)
746                 return NULL;
747
748         if (tp_init(tp, device) != 0) {
749                 tp_destroy(&tp->base);
750                 return NULL;
751         }
752
753         return  &tp->base;
754 }