touchpad: add fake-touch support for BTN_TOOL_DOUBLETAP and friends
[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_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
35
36 static inline int
37 tp_hysteresis(int in, int center, int margin)
38 {
39         int diff = in - center;
40         if (abs(diff) <= margin)
41                 return center;
42
43         if (diff > margin)
44                 return center + diff - margin;
45         else if (diff < -margin)
46                 return center + diff + margin;
47         return center + diff;
48 }
49
50 static double
51 tp_accel_profile(struct motion_filter *filter,
52                  void *data,
53                  double velocity,
54                  uint32_t time)
55 {
56         struct tp_dispatch *tp =
57                 (struct tp_dispatch *) data;
58
59         double accel_factor;
60
61         accel_factor = velocity * tp->accel.constant_factor;
62
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;
67
68         return accel_factor;
69 }
70
71 static inline struct tp_motion *
72 tp_motion_history_offset(struct tp_touch *t, int offset)
73 {
74         int offset_index =
75                 (t->history.index - offset + TOUCHPAD_HISTORY_LENGTH) %
76                 TOUCHPAD_HISTORY_LENGTH;
77
78         return &t->history.samples[offset_index];
79 }
80
81 static void
82 tp_filter_motion(struct tp_dispatch *tp,
83                  double *dx, double *dy, uint32_t time)
84 {
85         struct motion_params motion;
86
87         motion.dx = *dx;
88         motion.dy = *dy;
89
90         filter_dispatch(tp->filter, &motion, tp, time);
91
92         *dx = motion.dx;
93         *dy = motion.dy;
94 }
95
96 static inline void
97 tp_motion_history_push(struct tp_touch *t)
98 {
99         int motion_index = (t->history.index + 1) % TOUCHPAD_HISTORY_LENGTH;
100
101         if (t->history.count < TOUCHPAD_HISTORY_LENGTH)
102                 t->history.count++;
103
104         t->history.samples[motion_index].x = t->x;
105         t->history.samples[motion_index].y = t->y;
106         t->history.index = motion_index;
107 }
108
109 static inline void
110 tp_motion_hysteresis(struct tp_dispatch *tp,
111                      struct tp_touch *t)
112 {
113         int x = t->x,
114             y = t->y;
115
116         if (t->history.count == 0) {
117                 t->hysteresis.center_x = t->x;
118                 t->hysteresis.center_y = t->y;
119         } else {
120                 x = tp_hysteresis(x,
121                                   t->hysteresis.center_x,
122                                   tp->hysteresis.margin_x);
123                 y = tp_hysteresis(y,
124                                   t->hysteresis.center_y,
125                                   tp->hysteresis.margin_y);
126                 t->hysteresis.center_x = x;
127                 t->hysteresis.center_y = y;
128                 t->x = x;
129                 t->y = y;
130         }
131 }
132
133 static inline void
134 tp_motion_history_reset(struct tp_touch *t)
135 {
136         t->history.count = 0;
137 }
138
139 static inline struct tp_touch *
140 tp_current_touch(struct tp_dispatch *tp)
141 {
142         return &tp->touches[min(tp->slot, tp->ntouches)];
143 }
144
145 static inline struct tp_touch *
146 tp_get_touch(struct tp_dispatch *tp, unsigned int slot)
147 {
148         assert(slot < tp->ntouches);
149         return &tp->touches[slot];
150 }
151
152 static inline void
153 tp_begin_touch(struct tp_dispatch *tp, struct tp_touch *t)
154 {
155         if (t->state != TOUCH_UPDATE) {
156                 tp_motion_history_reset(t);
157                 t->dirty = true;
158                 t->state = TOUCH_BEGIN;
159                 tp->nfingers_down++;
160                 assert(tp->nfingers_down >= 1);
161                 tp->queued |= TOUCHPAD_EVENT_MOTION;
162         }
163 }
164
165 static inline void
166 tp_end_touch(struct tp_dispatch *tp, struct tp_touch *t)
167 {
168         if (t->state == TOUCH_NONE)
169                 return;
170
171         t->dirty = true;
172         t->state = TOUCH_END;
173         assert(tp->nfingers_down >= 1);
174         tp->nfingers_down--;
175         tp->queued |= TOUCHPAD_EVENT_MOTION;
176 }
177
178 static double
179 tp_estimate_delta(int x0, int x1, int x2, int x3)
180 {
181         return (x0 + x1 - x2 - x3) / 4;
182 }
183
184 void
185 tp_get_delta(struct tp_touch *t, double *dx, double *dy)
186 {
187         if (t->history.count < 4) {
188                 *dx = 0;
189                 *dy = 0;
190                 return;
191         }
192
193         *dx = tp_estimate_delta(tp_motion_history_offset(t, 0)->x,
194                                 tp_motion_history_offset(t, 1)->x,
195                                 tp_motion_history_offset(t, 2)->x,
196                                 tp_motion_history_offset(t, 3)->x);
197         *dy = tp_estimate_delta(tp_motion_history_offset(t, 0)->y,
198                                 tp_motion_history_offset(t, 1)->y,
199                                 tp_motion_history_offset(t, 2)->y,
200                                 tp_motion_history_offset(t, 3)->y);
201 }
202
203 static void
204 tp_process_absolute(struct tp_dispatch *tp,
205                     const struct input_event *e,
206                     uint32_t time)
207 {
208         struct tp_touch *t = tp_current_touch(tp);
209
210         switch(e->code) {
211         case ABS_MT_POSITION_X:
212                 t->x = e->value;
213                 t->millis = time;
214                 t->dirty = true;
215                 tp->queued |= TOUCHPAD_EVENT_MOTION;
216                 break;
217         case ABS_MT_POSITION_Y:
218                 t->y = e->value;
219                 t->millis = time;
220                 t->dirty = true;
221                 tp->queued |= TOUCHPAD_EVENT_MOTION;
222                 break;
223         case ABS_MT_SLOT:
224                 tp->slot = e->value;
225                 break;
226         case ABS_MT_TRACKING_ID:
227                 t->millis = time;
228                 if (e->value != -1)
229                         tp_begin_touch(tp, t);
230                 else
231                         tp_end_touch(tp, t);
232         }
233 }
234
235 static void
236 tp_process_absolute_st(struct tp_dispatch *tp,
237                        const struct input_event *e,
238                        uint32_t time)
239 {
240         struct tp_touch *t = tp_current_touch(tp);
241
242         switch(e->code) {
243         case ABS_X:
244                 t->x = e->value;
245                 t->millis = time;
246                 t->dirty = true;
247                 break;
248         case ABS_Y:
249                 t->y = e->value;
250                 t->millis = time;
251                 t->dirty = true;
252                 tp->queued |= TOUCHPAD_EVENT_MOTION;
253                 break;
254         }
255 }
256
257 static void
258 tp_process_fake_touch(struct tp_dispatch *tp,
259                       const struct input_event *e,
260                       uint32_t time)
261 {
262         struct tp_touch *t;
263         unsigned int fake_touches;
264         unsigned int nfake_touches;
265         unsigned int i;
266         unsigned int shift;
267
268         if (e->code != BTN_TOUCH &&
269             (e->code < BTN_TOOL_DOUBLETAP || e->code > BTN_TOOL_QUADTAP))
270                 return;
271
272         shift = e->code == BTN_TOUCH ? 0 : (e->code - BTN_TOOL_DOUBLETAP + 1);
273
274         if (e->value)
275                 tp->fake_touches |= 1 << shift;
276         else
277                 tp->fake_touches &= ~(0x1 << shift);
278
279         fake_touches = tp->fake_touches;
280         nfake_touches = 0;
281         while (fake_touches) {
282                 nfake_touches++;
283                 fake_touches >>= 1;
284         }
285
286         for (i = 0; i < tp->ntouches; i++) {
287                 t = tp_get_touch(tp, i);
288                 if (i >= nfake_touches) {
289                         if (t->state != TOUCH_NONE) {
290                                 tp_end_touch(tp, t);
291                                 t->millis = time;
292                         }
293                 } else if (t->state != TOUCH_UPDATE &&
294                            t->state != TOUCH_BEGIN) {
295                         t->state = TOUCH_NONE;
296                         tp_begin_touch(tp, t);
297                         t->millis = time;
298                         t->fake =true;
299                 }
300         }
301
302         assert(tp->nfingers_down == nfake_touches);
303 }
304
305 static void
306 tp_process_key(struct tp_dispatch *tp,
307                const struct input_event *e,
308                uint32_t time)
309 {
310         uint32_t mask;
311
312         switch (e->code) {
313                 case BTN_LEFT:
314                 case BTN_MIDDLE:
315                 case BTN_RIGHT:
316                         mask = 1 << (e->code - BTN_LEFT);
317                         if (e->value) {
318                                 tp->buttons.state |= mask;
319                                 tp->queued |= TOUCHPAD_EVENT_BUTTON_PRESS;
320                         } else {
321                                 tp->buttons.state &= ~mask;
322                                 tp->queued |= TOUCHPAD_EVENT_BUTTON_RELEASE;
323                         }
324                         break;
325                 case BTN_TOUCH:
326                 case BTN_TOOL_DOUBLETAP:
327                 case BTN_TOOL_TRIPLETAP:
328                 case BTN_TOOL_QUADTAP:
329                         if (!tp->has_mt)
330                                 tp_process_fake_touch(tp, e, time);
331                         break;
332         }
333 }
334
335 static void
336 tp_process_state(struct tp_dispatch *tp, uint32_t time)
337 {
338         struct tp_touch *t;
339         struct tp_touch *first = tp_get_touch(tp, 0);
340
341         tp_for_each_touch(tp, t) {
342                 if (!tp->has_mt && t != first && first->fake) {
343                         t->x = first->x;
344                         t->y = first->y;
345                         if (!t->dirty)
346                                 t->dirty = first->dirty;
347                 } else if (!t->dirty)
348                         continue;
349
350                 tp_motion_hysteresis(tp, t);
351                 tp_motion_history_push(t);
352         }
353 }
354
355 static void
356 tp_post_process_state(struct tp_dispatch *tp, uint32_t time)
357 {
358         struct tp_touch *t;
359
360         tp_for_each_touch(tp, t) {
361                 if (!t->dirty)
362                         continue;
363
364                 if (t->state == TOUCH_END) {
365                         t->state = TOUCH_NONE;
366                         t->fake = false;
367                 } else if (t->state == TOUCH_BEGIN)
368                         t->state = TOUCH_UPDATE;
369
370                 t->dirty = false;
371         }
372
373         tp->buttons.old_state = tp->buttons.state;
374
375         tp->queued = TOUCHPAD_EVENT_NONE;
376 }
377
378 static void
379 tp_post_twofinger_scroll(struct tp_dispatch *tp, uint32_t time)
380 {
381         struct tp_touch *t;
382         int nchanged = 0;
383         double dx = 0, dy =0;
384         double tmpx, tmpy;
385
386         tp_for_each_touch(tp, t) {
387                 if (t->dirty) {
388                         nchanged++;
389                         tp_get_delta(t, &tmpx, &tmpy);
390
391                         dx += tmpx;
392                         dy += tmpy;
393                 }
394         }
395
396         if (nchanged == 0)
397                 return;
398
399         dx /= nchanged;
400         dy /= nchanged;
401
402         tp_filter_motion(tp, &dx, &dy, time);
403
404         if (tp->scroll.state == SCROLL_STATE_NONE) {
405                 /* Require at least one px scrolling to start */
406                 if (dx <= -1.0 || dx >= 1.0) {
407                         tp->scroll.state = SCROLL_STATE_SCROLLING;
408                         tp->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL);
409                 }
410
411                 if (dy <= -1.0 || dy >= 1.0) {
412                         tp->scroll.state = SCROLL_STATE_SCROLLING;
413                         tp->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL);
414                 }
415
416                 if (tp->scroll.state == SCROLL_STATE_NONE)
417                         return;
418         }
419
420         if (dy != 0.0 &&
421             (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL))) {
422                 pointer_notify_axis(&tp->device->base,
423                                     time,
424                                     LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
425                                     li_fixed_from_double(dy));
426         }
427
428         if (dx != 0.0 &&
429             (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL))) {
430                 pointer_notify_axis(&tp->device->base,
431                                     time,
432                                     LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
433                                     li_fixed_from_double(dx));
434         }
435 }
436
437 static int
438 tp_post_scroll_events(struct tp_dispatch *tp, uint32_t time)
439 {
440         if (tp->nfingers_down != 2) {
441                 /* terminate scrolling with a zero scroll event to notify
442                  * caller that it really ended now */
443                 if (tp->scroll.state != SCROLL_STATE_NONE) {
444                         tp->scroll.state = SCROLL_STATE_NONE;
445                         tp->scroll.direction = 0;
446                         if (tp->scroll.direction & LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL)
447                                 pointer_notify_axis(&tp->device->base,
448                                                     time,
449                                                     LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
450                                                     0);
451                         if (tp->scroll.direction & LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL)
452                                 pointer_notify_axis(&tp->device->base,
453                                                     time,
454                                                     LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
455                                                     0);
456                 }
457         } else {
458                 tp_post_twofinger_scroll(tp, time);
459                 return 1;
460         }
461         return 0;
462 }
463
464 static void
465 tp_post_button_events(struct tp_dispatch *tp, uint32_t time)
466 {
467         uint32_t current, old, button;
468
469         if ((tp->queued &
470                 (TOUCHPAD_EVENT_BUTTON_PRESS|TOUCHPAD_EVENT_BUTTON_RELEASE)) == 0)
471                                 return;
472
473         current = tp->buttons.state;
474         old = tp->buttons.old_state;
475         button = BTN_LEFT;
476
477         while (current || old) {
478                 enum libinput_pointer_button_state state;
479
480                 if ((current & 0x1) ^ (old & 0x1)) {
481                         if (!!(current & 0x1))
482                                 state = LIBINPUT_POINTER_BUTTON_STATE_PRESSED;
483                         else
484                                 state = LIBINPUT_POINTER_BUTTON_STATE_RELEASED;
485
486                         pointer_notify_button(&tp->device->base,
487                                               time,
488                                               button,
489                                               state);
490                 }
491
492                 button++;
493                 current >>= 1;
494                 old >>= 1;
495         }
496 }
497
498 static void
499 tp_post_events(struct tp_dispatch *tp, uint32_t time)
500 {
501         struct tp_touch *t = tp_current_touch(tp);
502         double dx, dy;
503
504         if (tp_tap_handle_state(tp, time) != 0)
505                 return;
506
507         if (tp_post_scroll_events(tp, time) != 0)
508                 return;
509
510         if (t->history.count >= TOUCHPAD_MIN_SAMPLES &&
511             tp->nfingers_down == 1) {
512                 tp_get_delta(t, &dx, &dy);
513                 tp_filter_motion(tp, &dx, &dy, time);
514
515                 if (dx != 0 || dy != 0)
516                         pointer_notify_motion(
517                                 &tp->device->base,
518                                 time,
519                                 li_fixed_from_double(dx),
520                                 li_fixed_from_double(dy));
521         }
522
523         tp_post_button_events(tp, time);
524 }
525
526 static void
527 tp_process(struct evdev_dispatch *dispatch,
528            struct evdev_device *device,
529            struct input_event *e,
530            uint32_t time)
531 {
532         struct tp_dispatch *tp =
533                 (struct tp_dispatch *)dispatch;
534
535         switch (e->type) {
536         case EV_ABS:
537                 if (tp->has_mt)
538                         tp_process_absolute(tp, e, time);
539                 else
540                         tp_process_absolute_st(tp, e, time);
541                 break;
542         case EV_KEY:
543                 tp_process_key(tp, e, time);
544                 break;
545         case EV_SYN:
546                 tp_process_state(tp, time);
547                 tp_post_events(tp, time);
548                 tp_post_process_state(tp, time);
549                 break;
550         }
551 }
552
553 static void
554 tp_destroy(struct evdev_dispatch *dispatch)
555 {
556         struct tp_dispatch *tp =
557                 (struct tp_dispatch*)dispatch;
558
559         if (tp->filter)
560                 tp->filter->interface->destroy(tp->filter);
561         free(tp->touches);
562         free(tp);
563 }
564
565 static struct evdev_dispatch_interface tp_interface = {
566         tp_process,
567         tp_destroy
568 };
569
570 static int
571 tp_init_slots(struct tp_dispatch *tp,
572               struct evdev_device *device)
573 {
574         const struct input_absinfo *absinfo;
575
576         absinfo = libevdev_get_abs_info(device->evdev, ABS_MT_SLOT);
577         if (absinfo) {
578                 tp->ntouches = absinfo->maximum + 1;
579                 tp->slot = absinfo->value;
580                 tp->has_mt = true;
581         } else {
582                 tp->ntouches = 5; /* FIXME: based on DOUBLETAP, etc. */
583                 tp->slot = 0;
584                 tp->has_mt = false;
585         }
586         tp->touches = calloc(tp->ntouches,
587                              sizeof(struct tp_touch));
588
589         return 0;
590 }
591
592 static int
593 tp_init_accel(struct tp_dispatch *touchpad, double diagonal)
594 {
595         struct motion_filter *accel;
596
597         touchpad->accel.constant_factor =
598                 DEFAULT_CONSTANT_ACCEL_NUMERATOR / diagonal;
599         touchpad->accel.min_factor = DEFAULT_MIN_ACCEL_FACTOR;
600         touchpad->accel.max_factor = DEFAULT_MAX_ACCEL_FACTOR;
601
602         accel = create_pointer_accelator_filter(tp_accel_profile);
603         if (accel == NULL)
604                 return -1;
605
606         touchpad->filter = accel;
607
608         return 0;
609 }
610
611 static int
612 tp_init_scroll(struct tp_dispatch *tp)
613 {
614         tp->scroll.direction = 0;
615         tp->scroll.state = SCROLL_STATE_NONE;
616
617         return 0;
618 }
619
620 static int
621 tp_init(struct tp_dispatch *tp,
622         struct evdev_device *device)
623 {
624         int width, height;
625         double diagonal;
626
627         tp->base.interface = &tp_interface;
628         tp->device = device;
629
630         if (tp_init_slots(tp, device) != 0)
631                 return -1;
632
633         width = abs(device->abs.max_x - device->abs.min_x);
634         height = abs(device->abs.max_y - device->abs.min_y);
635         diagonal = sqrt(width*width + height*height);
636
637         tp->hysteresis.margin_x =
638                 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
639         tp->hysteresis.margin_y =
640                 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
641
642         if (tp_init_scroll(tp) != 0)
643                 return -1;
644
645         if (tp_init_accel(tp, diagonal) != 0)
646                 return -1;
647
648         if (tp_init_tap(tp) != 0)
649                 return -1;
650
651         return 0;
652 }
653
654 struct evdev_dispatch *
655 evdev_mt_touchpad_create(struct evdev_device *device)
656 {
657         struct tp_dispatch *tp;
658
659         tp = zalloc(sizeof *tp);
660         if (!tp)
661                 return NULL;
662
663         if (tp_init(tp, device) != 0) {
664                 tp_destroy(&tp->base);
665                 return NULL;
666         }
667
668         return  &tp->base;
669 }