touchpad: Add clickpad-style software buttons
[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         struct tp_touch *tmp = NULL;
156
157         if (t->state != TOUCH_UPDATE) {
158                 tp_motion_history_reset(t);
159                 t->dirty = true;
160                 t->state = TOUCH_BEGIN;
161                 t->pinned.is_pinned = false;
162                 tp->nfingers_down++;
163                 assert(tp->nfingers_down >= 1);
164                 tp->queued |= TOUCHPAD_EVENT_MOTION;
165
166                 tp_for_each_touch(tp, tmp) {
167                         if (tmp->is_pointer)
168                                 break;
169                 }
170
171                 if (!tmp->is_pointer) {
172                         t->is_pointer = true;
173                 }
174         }
175 }
176
177 static inline void
178 tp_end_touch(struct tp_dispatch *tp, struct tp_touch *t)
179 {
180         if (t->state == TOUCH_NONE)
181                 return;
182
183         t->dirty = true;
184         t->is_pointer = false;
185         t->state = TOUCH_END;
186         t->pinned.is_pinned = false;
187         assert(tp->nfingers_down >= 1);
188         tp->nfingers_down--;
189         tp->queued |= TOUCHPAD_EVENT_MOTION;
190 }
191
192 static double
193 tp_estimate_delta(int x0, int x1, int x2, int x3)
194 {
195         return (x0 + x1 - x2 - x3) / 4;
196 }
197
198 void
199 tp_get_delta(struct tp_touch *t, double *dx, double *dy)
200 {
201         if (t->history.count < 4) {
202                 *dx = 0;
203                 *dy = 0;
204                 return;
205         }
206
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);
215 }
216
217 static void
218 tp_process_absolute(struct tp_dispatch *tp,
219                     const struct input_event *e,
220                     uint32_t time)
221 {
222         struct tp_touch *t = tp_current_touch(tp);
223
224         switch(e->code) {
225         case ABS_MT_POSITION_X:
226                 t->x = e->value;
227                 t->millis = time;
228                 t->dirty = true;
229                 tp->queued |= TOUCHPAD_EVENT_MOTION;
230                 break;
231         case ABS_MT_POSITION_Y:
232                 t->y = e->value;
233                 t->millis = time;
234                 t->dirty = true;
235                 tp->queued |= TOUCHPAD_EVENT_MOTION;
236                 break;
237         case ABS_MT_SLOT:
238                 tp->slot = e->value;
239                 break;
240         case ABS_MT_TRACKING_ID:
241                 t->millis = time;
242                 if (e->value != -1)
243                         tp_begin_touch(tp, t);
244                 else
245                         tp_end_touch(tp, t);
246         }
247 }
248
249 static void
250 tp_process_absolute_st(struct tp_dispatch *tp,
251                        const struct input_event *e,
252                        uint32_t time)
253 {
254         struct tp_touch *t = tp_current_touch(tp);
255
256         switch(e->code) {
257         case ABS_X:
258                 t->x = e->value;
259                 t->millis = time;
260                 t->dirty = true;
261                 tp->queued |= TOUCHPAD_EVENT_MOTION;
262                 break;
263         case ABS_Y:
264                 t->y = e->value;
265                 t->millis = time;
266                 t->dirty = true;
267                 tp->queued |= TOUCHPAD_EVENT_MOTION;
268                 break;
269         }
270 }
271
272 static void
273 tp_process_fake_touch(struct tp_dispatch *tp,
274                       const struct input_event *e,
275                       uint32_t time)
276 {
277         struct tp_touch *t;
278         unsigned int fake_touches;
279         unsigned int nfake_touches;
280         unsigned int i;
281         unsigned int shift;
282
283         if (e->code != BTN_TOUCH &&
284             (e->code < BTN_TOOL_DOUBLETAP || e->code > BTN_TOOL_QUADTAP))
285                 return;
286
287         shift = e->code == BTN_TOUCH ? 0 : (e->code - BTN_TOOL_DOUBLETAP + 1);
288
289         if (e->value)
290                 tp->fake_touches |= 1 << shift;
291         else
292                 tp->fake_touches &= ~(0x1 << shift);
293
294         fake_touches = tp->fake_touches;
295         nfake_touches = 0;
296         while (fake_touches) {
297                 nfake_touches++;
298                 fake_touches >>= 1;
299         }
300
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) {
305                                 tp_end_touch(tp, t);
306                                 t->millis = time;
307                         }
308                 } else if (t->state != TOUCH_UPDATE &&
309                            t->state != TOUCH_BEGIN) {
310                         t->state = TOUCH_NONE;
311                         tp_begin_touch(tp, t);
312                         t->millis = time;
313                         t->fake =true;
314                 }
315         }
316
317         assert(tp->nfingers_down == nfake_touches);
318 }
319
320 static void
321 tp_process_key(struct tp_dispatch *tp,
322                const struct input_event *e,
323                uint32_t time)
324 {
325         switch (e->code) {
326                 case BTN_LEFT:
327                 case BTN_MIDDLE:
328                 case BTN_RIGHT:
329                         tp_process_button(tp, e, time);
330                         break;
331                 case BTN_TOUCH:
332                 case BTN_TOOL_DOUBLETAP:
333                 case BTN_TOOL_TRIPLETAP:
334                 case BTN_TOOL_QUADTAP:
335                         if (!tp->has_mt)
336                                 tp_process_fake_touch(tp, e, time);
337                         break;
338         }
339 }
340
341 static void
342 tp_unpin_finger(struct tp_dispatch *tp, struct tp_touch *t)
343 {
344         unsigned int xdist, ydist;
345         struct tp_touch *tmp = NULL;
346
347         if (!t->pinned.is_pinned)
348                 return;
349
350         xdist = abs(t->x - t->pinned.center_x);
351         ydist = abs(t->y - t->pinned.center_y);
352
353         if (xdist * xdist + ydist * ydist <
354                         tp->buttons.motion_dist * tp->buttons.motion_dist)
355                 return;
356
357         t->pinned.is_pinned = false;
358
359         tp_for_each_touch(tp, tmp) {
360                 if (tmp->is_pointer)
361                         break;
362         }
363
364         if (t->state != TOUCH_END && !tmp->is_pointer)
365                 t->is_pointer = true;
366 }
367
368 static void
369 tp_pin_fingers(struct tp_dispatch *tp)
370 {
371         struct tp_touch *t;
372
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;
378         }
379 }
380
381 static void
382 tp_process_state(struct tp_dispatch *tp, uint32_t time)
383 {
384         struct tp_touch *t;
385         struct tp_touch *first = tp_get_touch(tp, 0);
386
387         tp_for_each_touch(tp, t) {
388                 if (!tp->has_mt && t != first && first->fake) {
389                         t->x = first->x;
390                         t->y = first->y;
391                         if (!t->dirty)
392                                 t->dirty = first->dirty;
393                 } else if (!t->dirty)
394                         continue;
395
396                 tp_motion_hysteresis(tp, t);
397                 tp_motion_history_push(t);
398
399                 tp_unpin_finger(tp, t);
400         }
401
402         tp_button_handle_state(tp, time);
403
404         /*
405          * We have a physical button down event on a clickpad. To avoid
406          * spurious pointer moves by the clicking finger we pin all fingers.
407          * We unpin fingers when they move more then a certain threshold to
408          * to allow drag and drop.
409          */
410         if ((tp->queued & TOUCHPAD_EVENT_BUTTON_PRESS) &&
411             !tp->buttons.has_buttons)
412                 tp_pin_fingers(tp);
413 }
414
415 static void
416 tp_post_process_state(struct tp_dispatch *tp, uint32_t time)
417 {
418         struct tp_touch *t;
419
420         tp_for_each_touch(tp, t) {
421                 if (!t->dirty)
422                         continue;
423
424                 if (t->state == TOUCH_END) {
425                         t->state = TOUCH_NONE;
426                         t->fake = false;
427                 } else if (t->state == TOUCH_BEGIN)
428                         t->state = TOUCH_UPDATE;
429
430                 t->dirty = false;
431         }
432
433         tp->buttons.old_state = tp->buttons.state;
434
435         tp->queued = TOUCHPAD_EVENT_NONE;
436 }
437
438 static void
439 tp_post_twofinger_scroll(struct tp_dispatch *tp, uint32_t time)
440 {
441         struct tp_touch *t;
442         int nchanged = 0;
443         double dx = 0, dy =0;
444         double tmpx, tmpy;
445
446         tp_for_each_touch(tp, t) {
447                 if (t->dirty) {
448                         nchanged++;
449                         tp_get_delta(t, &tmpx, &tmpy);
450
451                         dx += tmpx;
452                         dy += tmpy;
453                 }
454         }
455
456         if (nchanged == 0)
457                 return;
458
459         dx /= nchanged;
460         dy /= nchanged;
461
462         tp_filter_motion(tp, &dx, &dy, time);
463
464         if (tp->scroll.state == SCROLL_STATE_NONE) {
465                 /* Require at least one px scrolling to start */
466                 if (dx <= -1.0 || dx >= 1.0) {
467                         tp->scroll.state = SCROLL_STATE_SCROLLING;
468                         tp->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL);
469                 }
470
471                 if (dy <= -1.0 || dy >= 1.0) {
472                         tp->scroll.state = SCROLL_STATE_SCROLLING;
473                         tp->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL);
474                 }
475
476                 if (tp->scroll.state == SCROLL_STATE_NONE)
477                         return;
478         }
479
480         if (dy != 0.0 &&
481             (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL))) {
482                 pointer_notify_axis(&tp->device->base,
483                                     time,
484                                     LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
485                                     li_fixed_from_double(dy));
486         }
487
488         if (dx != 0.0 &&
489             (tp->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL))) {
490                 pointer_notify_axis(&tp->device->base,
491                                     time,
492                                     LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
493                                     li_fixed_from_double(dx));
494         }
495 }
496
497 static int
498 tp_post_scroll_events(struct tp_dispatch *tp, uint32_t time)
499 {
500         /* don't scroll if a clickpad is held down */
501         if (!tp->buttons.has_buttons &&
502             (tp->buttons.state || tp->buttons.old_state))
503                 return 0;
504
505         if (tp->nfingers_down != 2) {
506                 /* terminate scrolling with a zero scroll event to notify
507                  * caller that it really ended now */
508                 if (tp->scroll.state != SCROLL_STATE_NONE) {
509                         tp->scroll.state = SCROLL_STATE_NONE;
510                         tp->scroll.direction = 0;
511                         if (tp->scroll.direction & LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL)
512                                 pointer_notify_axis(&tp->device->base,
513                                                     time,
514                                                     LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
515                                                     0);
516                         if (tp->scroll.direction & LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL)
517                                 pointer_notify_axis(&tp->device->base,
518                                                     time,
519                                                     LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
520                                                     0);
521                 }
522         } else {
523                 tp_post_twofinger_scroll(tp, time);
524                 return 1;
525         }
526         return 0;
527 }
528
529 static void
530 tp_post_events(struct tp_dispatch *tp, uint32_t time)
531 {
532         struct tp_touch *t = tp_current_touch(tp);
533         double dx, dy;
534
535         if (tp_post_button_events(tp, time) != 0)
536                 return;
537
538         if (tp_tap_handle_state(tp, time) != 0)
539                 return;
540
541         if (tp_post_scroll_events(tp, time) != 0)
542                 return;
543
544         if (t->history.count >= TOUCHPAD_MIN_SAMPLES) {
545                 if (!t->is_pointer) {
546                         tp_for_each_touch(tp, t) {
547                                 if (t->is_pointer)
548                                         break;
549                         }
550                 }
551
552                 if (!t->is_pointer)
553                         return;
554
555                 tp_get_delta(t, &dx, &dy);
556                 tp_filter_motion(tp, &dx, &dy, time);
557
558                 if (dx != 0 || dy != 0)
559                         pointer_notify_motion(
560                                 &tp->device->base,
561                                 time,
562                                 li_fixed_from_double(dx),
563                                 li_fixed_from_double(dy));
564         }
565 }
566
567 static void
568 tp_process(struct evdev_dispatch *dispatch,
569            struct evdev_device *device,
570            struct input_event *e,
571            uint32_t time)
572 {
573         struct tp_dispatch *tp =
574                 (struct tp_dispatch *)dispatch;
575
576         switch (e->type) {
577         case EV_ABS:
578                 if (tp->has_mt)
579                         tp_process_absolute(tp, e, time);
580                 else
581                         tp_process_absolute_st(tp, e, time);
582                 break;
583         case EV_KEY:
584                 tp_process_key(tp, e, time);
585                 break;
586         case EV_SYN:
587                 tp_process_state(tp, time);
588                 tp_post_events(tp, time);
589                 tp_post_process_state(tp, time);
590                 break;
591         }
592 }
593
594 static void
595 tp_destroy(struct evdev_dispatch *dispatch)
596 {
597         struct tp_dispatch *tp =
598                 (struct tp_dispatch*)dispatch;
599
600         tp_destroy_tap(tp);
601         tp_destroy_buttons(tp);
602
603         if (tp->filter)
604                 tp->filter->interface->destroy(tp->filter);
605         free(tp->touches);
606         free(tp);
607 }
608
609 static struct evdev_dispatch_interface tp_interface = {
610         tp_process,
611         tp_destroy
612 };
613
614 static void
615 tp_init_touch(struct tp_dispatch *tp,
616               struct tp_touch *t)
617 {
618         t->button.state = BUTTON_STATE_NONE;
619 }
620
621 static int
622 tp_init_slots(struct tp_dispatch *tp,
623               struct evdev_device *device)
624 {
625         size_t i;
626         const struct input_absinfo *absinfo;
627
628         absinfo = libevdev_get_abs_info(device->evdev, ABS_MT_SLOT);
629         if (absinfo) {
630                 tp->ntouches = absinfo->maximum + 1;
631                 tp->slot = absinfo->value;
632                 tp->has_mt = true;
633         } else {
634                 struct map {
635                         unsigned int code;
636                         int ntouches;
637                 } max_touches[] = {
638                         { BTN_TOOL_QUINTTAP, 5 },
639                         { BTN_TOOL_QUADTAP, 4 },
640                         { BTN_TOOL_TRIPLETAP, 3 },
641                         { BTN_TOOL_DOUBLETAP, 2 },
642                 };
643                 struct map *m;
644
645                 tp->slot = 0;
646                 tp->has_mt = false;
647                 tp->ntouches = 1;
648
649                 ARRAY_FOR_EACH(max_touches, m) {
650                         if (libevdev_has_event_code(device->evdev,
651                                                     EV_KEY,
652                                                     m->code)) {
653                                 tp->ntouches = m->ntouches;
654                                 break;
655                         }
656                 }
657         }
658         tp->touches = calloc(tp->ntouches,
659                              sizeof(struct tp_touch));
660         if (!tp->touches)
661                 return -1;
662
663         for (i = 0; i < tp->ntouches; i++)
664                 tp_init_touch(tp, &tp->touches[i]);
665
666         return 0;
667 }
668
669 static int
670 tp_init_accel(struct tp_dispatch *touchpad, double diagonal)
671 {
672         struct motion_filter *accel;
673
674         touchpad->accel.constant_factor =
675                 DEFAULT_CONSTANT_ACCEL_NUMERATOR / diagonal;
676         touchpad->accel.min_factor = DEFAULT_MIN_ACCEL_FACTOR;
677         touchpad->accel.max_factor = DEFAULT_MAX_ACCEL_FACTOR;
678
679         accel = create_pointer_accelator_filter(tp_accel_profile);
680         if (accel == NULL)
681                 return -1;
682
683         touchpad->filter = accel;
684
685         return 0;
686 }
687
688 static int
689 tp_init_scroll(struct tp_dispatch *tp)
690 {
691         tp->scroll.direction = 0;
692         tp->scroll.state = SCROLL_STATE_NONE;
693
694         return 0;
695 }
696
697 static int
698 tp_init(struct tp_dispatch *tp,
699         struct evdev_device *device)
700 {
701         int width, height;
702         double diagonal;
703
704         tp->base.interface = &tp_interface;
705         tp->device = device;
706         tp->tap.timer_fd = -1;
707         tp->buttons.timer_fd = -1;
708
709         if (tp_init_slots(tp, device) != 0)
710                 return -1;
711
712         width = abs(device->abs.max_x - device->abs.min_x);
713         height = abs(device->abs.max_y - device->abs.min_y);
714         diagonal = sqrt(width*width + height*height);
715
716         tp->hysteresis.margin_x =
717                 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
718         tp->hysteresis.margin_y =
719                 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
720
721         if (tp_init_scroll(tp) != 0)
722                 return -1;
723
724         if (tp_init_accel(tp, diagonal) != 0)
725                 return -1;
726
727         if (tp_init_tap(tp) != 0)
728                 return -1;
729
730         if (tp_init_buttons(tp, device) != 0)
731                 return -1;
732
733         return 0;
734 }
735
736 struct evdev_dispatch *
737 evdev_mt_touchpad_create(struct evdev_device *device)
738 {
739         struct tp_dispatch *tp;
740
741         tp = zalloc(sizeof *tp);
742         if (!tp)
743                 return NULL;
744
745         if (tp_init(tp, device) != 0) {
746                 tp_destroy(&tp->base);
747                 return NULL;
748         }
749
750         return  &tp->base;
751 }