evdev: use a different filter for low resolution touchpad on the Lenovo X230
[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 #include <limits.h>
29
30 #include "evdev-mt-touchpad.h"
31
32 /* Number found by trial-and error, seems to be 1200, divided by the
33  * TP_MAGIC_SLOWDOWN in filter.c */
34 #define DEFAULT_ACCEL_NUMERATOR 3000.0
35 #define DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR 700.0
36 #define DEFAULT_TRACKPOINT_ACTIVITY_TIMEOUT 500 /* ms */
37 #define FAKE_FINGER_OVERFLOW (1 << 7)
38
39 static inline int
40 tp_hysteresis(int in, int center, int margin)
41 {
42         int diff = in - center;
43         if (abs(diff) <= margin)
44                 return center;
45
46         if (diff > margin)
47                 return center + diff - margin;
48         else
49                 return center + diff + margin;
50 }
51
52 static inline struct device_coords *
53 tp_motion_history_offset(struct tp_touch *t, int offset)
54 {
55         int offset_index =
56                 (t->history.index - offset + TOUCHPAD_HISTORY_LENGTH) %
57                 TOUCHPAD_HISTORY_LENGTH;
58
59         return &t->history.samples[offset_index];
60 }
61
62 struct normalized_coords
63 tp_filter_motion(struct tp_dispatch *tp,
64                  const struct normalized_coords *unaccelerated,
65                  uint64_t time)
66 {
67         if (normalized_is_zero(*unaccelerated))
68                 return *unaccelerated;
69
70         return filter_dispatch(tp->device->pointer.filter,
71                                unaccelerated, tp, time);
72 }
73
74 static inline void
75 tp_motion_history_push(struct tp_touch *t)
76 {
77         int motion_index = (t->history.index + 1) % TOUCHPAD_HISTORY_LENGTH;
78
79         if (t->history.count < TOUCHPAD_HISTORY_LENGTH)
80                 t->history.count++;
81
82         t->history.samples[motion_index] = t->point;
83         t->history.index = motion_index;
84 }
85
86 static inline void
87 tp_motion_hysteresis(struct tp_dispatch *tp,
88                      struct tp_touch *t)
89 {
90         int x = t->point.x,
91             y = t->point.y;
92
93         if (t->history.count == 0) {
94                 t->hysteresis_center = t->point;
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->point.x = x;
105                 t->point.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 unsigned int
129 tp_fake_finger_count(struct tp_dispatch *tp)
130 {
131         if (tp->fake_touches & FAKE_FINGER_OVERFLOW)
132                 return FAKE_FINGER_OVERFLOW;
133         else /* don't count BTN_TOUCH */
134                 return ffs(tp->fake_touches >> 1);
135 }
136
137 static inline bool
138 tp_fake_finger_is_touching(struct tp_dispatch *tp)
139 {
140         return tp->fake_touches & 0x1;
141 }
142
143 static inline void
144 tp_fake_finger_set(struct tp_dispatch *tp,
145                    unsigned int code,
146                    bool is_press)
147 {
148         unsigned int shift;
149
150         switch (code) {
151         case BTN_TOUCH:
152                 if (!is_press)
153                         tp->fake_touches &= ~FAKE_FINGER_OVERFLOW;
154                 shift = 0;
155                 break;
156         case BTN_TOOL_FINGER:
157                 shift = 1;
158                 break;
159         case BTN_TOOL_DOUBLETAP:
160         case BTN_TOOL_TRIPLETAP:
161         case BTN_TOOL_QUADTAP:
162                 shift = code - BTN_TOOL_DOUBLETAP + 2;
163                 break;
164         /* when QUINTTAP is released we're either switching to 6 fingers
165            (flag stays in place until BTN_TOUCH is released) or
166            one of DOUBLE/TRIPLE/QUADTAP (will clear the flag on press) */
167         case BTN_TOOL_QUINTTAP:
168                 if (is_press)
169                         tp->fake_touches |= FAKE_FINGER_OVERFLOW;
170                 return;
171         default:
172                 return;
173         }
174
175         if (is_press) {
176                 tp->fake_touches &= ~FAKE_FINGER_OVERFLOW;
177                 tp->fake_touches |= 1 << shift;
178
179         } else {
180                 tp->fake_touches &= ~(0x1 << shift);
181         }
182 }
183
184 static inline void
185 tp_new_touch(struct tp_dispatch *tp, struct tp_touch *t, uint64_t time)
186 {
187         if (t->state == TOUCH_BEGIN ||
188             t->state == TOUCH_UPDATE ||
189             t->state == TOUCH_HOVERING)
190                 return;
191
192         /* we begin the touch as hovering because until BTN_TOUCH happens we
193          * don't know if it's a touch down or not. And BTN_TOUCH may happen
194          * after ABS_MT_TRACKING_ID */
195         tp_motion_history_reset(t);
196         t->dirty = true;
197         t->has_ended = false;
198         t->state = TOUCH_HOVERING;
199         t->pinned.is_pinned = false;
200         t->millis = time;
201         tp->queued |= TOUCHPAD_EVENT_MOTION;
202 }
203
204 static inline void
205 tp_begin_touch(struct tp_dispatch *tp, struct tp_touch *t, uint64_t time)
206 {
207         t->dirty = true;
208         t->state = TOUCH_BEGIN;
209         t->millis = time;
210         tp->nfingers_down++;
211         assert(tp->nfingers_down >= 1);
212 }
213
214 /**
215  * End a touch, even if the touch sequence is still active.
216  */
217 static inline void
218 tp_end_touch(struct tp_dispatch *tp, struct tp_touch *t, uint64_t time)
219 {
220         switch (t->state) {
221         case TOUCH_HOVERING:
222                 t->state = TOUCH_NONE;
223                 /* fallthough */
224         case TOUCH_NONE:
225         case TOUCH_END:
226                 return;
227         case TOUCH_BEGIN:
228         case TOUCH_UPDATE:
229                 break;
230
231         }
232
233         t->dirty = true;
234         t->palm.is_palm = false;
235         t->state = TOUCH_END;
236         t->pinned.is_pinned = false;
237         t->millis = time;
238         assert(tp->nfingers_down >= 1);
239         tp->nfingers_down--;
240         tp->queued |= TOUCHPAD_EVENT_MOTION;
241 }
242
243 /**
244  * End the touch sequence on ABS_MT_TRACKING_ID -1 or when the BTN_TOOL_* 0 is received.
245  */
246 static inline void
247 tp_end_sequence(struct tp_dispatch *tp, struct tp_touch *t, uint64_t time)
248 {
249         t->has_ended = true;
250         tp_end_touch(tp, t, time);
251 }
252
253 static double
254 tp_estimate_delta(int x0, int x1, int x2, int x3)
255 {
256         return (x0 + x1 - x2 - x3) / 4.0;
257 }
258
259 struct normalized_coords
260 tp_get_delta(struct tp_touch *t)
261 {
262         struct device_float_coords delta;
263         const struct normalized_coords zero = { 0.0, 0.0 };
264
265         if (t->history.count < TOUCHPAD_MIN_SAMPLES)
266                 return zero;
267
268         delta.x = tp_estimate_delta(tp_motion_history_offset(t, 0)->x,
269                                     tp_motion_history_offset(t, 1)->x,
270                                     tp_motion_history_offset(t, 2)->x,
271                                     tp_motion_history_offset(t, 3)->x);
272         delta.y = tp_estimate_delta(tp_motion_history_offset(t, 0)->y,
273                                     tp_motion_history_offset(t, 1)->y,
274                                     tp_motion_history_offset(t, 2)->y,
275                                     tp_motion_history_offset(t, 3)->y);
276
277         return tp_normalize_delta(t->tp, delta);
278 }
279
280 static void
281 tp_process_absolute(struct tp_dispatch *tp,
282                     const struct input_event *e,
283                     uint64_t time)
284 {
285         struct tp_touch *t = tp_current_touch(tp);
286
287         switch(e->code) {
288         case ABS_MT_POSITION_X:
289                 t->point.x = e->value;
290                 t->millis = time;
291                 t->dirty = true;
292                 tp->queued |= TOUCHPAD_EVENT_MOTION;
293                 break;
294         case ABS_MT_POSITION_Y:
295                 t->point.y = e->value;
296                 t->millis = time;
297                 t->dirty = true;
298                 tp->queued |= TOUCHPAD_EVENT_MOTION;
299                 break;
300         case ABS_MT_SLOT:
301                 tp->slot = e->value;
302                 break;
303         case ABS_MT_TRACKING_ID:
304                 if (e->value != -1)
305                         tp_new_touch(tp, t, time);
306                 else
307                         tp_end_sequence(tp, t, time);
308         }
309 }
310
311 static void
312 tp_process_absolute_st(struct tp_dispatch *tp,
313                        const struct input_event *e,
314                        uint64_t time)
315 {
316         struct tp_touch *t = tp_current_touch(tp);
317
318         switch(e->code) {
319         case ABS_X:
320                 t->point.x = e->value;
321                 t->millis = time;
322                 t->dirty = true;
323                 tp->queued |= TOUCHPAD_EVENT_MOTION;
324                 break;
325         case ABS_Y:
326                 t->point.y = e->value;
327                 t->millis = time;
328                 t->dirty = true;
329                 tp->queued |= TOUCHPAD_EVENT_MOTION;
330                 break;
331         }
332 }
333
334 static void
335 tp_process_fake_touches(struct tp_dispatch *tp,
336                         uint64_t time)
337 {
338         struct tp_touch *t;
339         unsigned int nfake_touches;
340         unsigned int i, start;
341
342         nfake_touches = tp_fake_finger_count(tp);
343         if (nfake_touches == FAKE_FINGER_OVERFLOW)
344                 return;
345
346         start = tp->has_mt ? tp->num_slots : 0;
347         for (i = start; i < tp->ntouches; i++) {
348                 t = tp_get_touch(tp, i);
349                 if (i < nfake_touches)
350                         tp_new_touch(tp, t, time);
351                 else
352                         tp_end_sequence(tp, t, time);
353         }
354 }
355
356 static void
357 tp_process_trackpoint_button(struct tp_dispatch *tp,
358                              const struct input_event *e,
359                              uint64_t time)
360 {
361         struct evdev_dispatch *dispatch;
362         struct input_event event;
363
364         if (!tp->buttons.trackpoint ||
365             (tp->device->tags & EVDEV_TAG_TOUCHPAD_TRACKPOINT) == 0)
366                 return;
367
368         dispatch = tp->buttons.trackpoint->dispatch;
369
370         event = *e;
371
372         switch (event.code) {
373         case BTN_0:
374                 event.code = BTN_LEFT;
375                 break;
376         case BTN_1:
377                 event.code = BTN_RIGHT;
378                 break;
379         case BTN_2:
380                 event.code = BTN_MIDDLE;
381                 break;
382         default:
383                 return;
384         }
385
386         dispatch->interface->process(dispatch,
387                                      tp->buttons.trackpoint,
388                                      &event, time);
389 }
390
391 static void
392 tp_process_key(struct tp_dispatch *tp,
393                const struct input_event *e,
394                uint64_t time)
395 {
396         switch (e->code) {
397                 case BTN_LEFT:
398                 case BTN_MIDDLE:
399                 case BTN_RIGHT:
400                         tp_process_button(tp, e, time);
401                         break;
402                 case BTN_TOUCH:
403                 case BTN_TOOL_FINGER:
404                 case BTN_TOOL_DOUBLETAP:
405                 case BTN_TOOL_TRIPLETAP:
406                 case BTN_TOOL_QUADTAP:
407                 case BTN_TOOL_QUINTTAP:
408                         tp_fake_finger_set(tp, e->code, !!e->value);
409                         break;
410                 case BTN_0:
411                 case BTN_1:
412                 case BTN_2:
413                         tp_process_trackpoint_button(tp, e, time);
414                         break;
415         }
416 }
417
418 static void
419 tp_unpin_finger(struct tp_dispatch *tp, struct tp_touch *t)
420 {
421         unsigned int xdist, ydist;
422
423         if (!t->pinned.is_pinned)
424                 return;
425
426         xdist = abs(t->point.x - t->pinned.center.x);
427         ydist = abs(t->point.y - t->pinned.center.y);
428
429         if (xdist * xdist + ydist * ydist >=
430                         tp->buttons.motion_dist * tp->buttons.motion_dist) {
431                 t->pinned.is_pinned = false;
432                 return;
433         }
434
435         /* The finger may slowly drift, adjust the center */
436         t->pinned.center.x = t->point.x + t->pinned.center.x / 2;
437         t->pinned.center.y = t->point.y + t->pinned.center.y / 2;
438 }
439
440 static void
441 tp_pin_fingers(struct tp_dispatch *tp)
442 {
443         struct tp_touch *t;
444
445         tp_for_each_touch(tp, t) {
446                 t->pinned.is_pinned = true;
447                 t->pinned.center = t->point;
448         }
449 }
450
451 int
452 tp_touch_active(struct tp_dispatch *tp, struct tp_touch *t)
453 {
454         return (t->state == TOUCH_BEGIN || t->state == TOUCH_UPDATE) &&
455                 !t->palm.is_palm &&
456                 !t->pinned.is_pinned &&
457                 tp_button_touch_active(tp, t) &&
458                 tp_edge_scroll_touch_active(tp, t);
459 }
460
461 bool
462 tp_palm_tap_is_palm(struct tp_dispatch *tp, struct tp_touch *t)
463 {
464         if (t->state != TOUCH_BEGIN)
465                 return false;
466
467         if (t->point.x > tp->palm.left_edge &&
468             t->point.x < tp->palm.right_edge)
469                 return false;
470
471         /* We're inside the left/right palm edge and in the northern half of
472          * the touchpad - this tap is a palm */
473         if (t->point.y < tp->palm.vert_center)
474                 return true;
475
476         return false;
477 }
478
479 static void
480 tp_palm_detect(struct tp_dispatch *tp, struct tp_touch *t, uint64_t time)
481 {
482         const int PALM_TIMEOUT = 200; /* ms */
483         const int DIRECTIONS = NE|E|SE|SW|W|NW;
484         struct device_float_coords delta;
485         int dirs;
486
487         /* If labelled a touch as palm, we unlabel as palm when
488            we move out of the palm edge zone within the timeout, provided
489            the direction is within 45 degrees of the horizontal.
490          */
491         if (t->palm.is_palm) {
492                 if (time < t->palm.time + PALM_TIMEOUT &&
493                     (t->point.x > tp->palm.left_edge && t->point.x < tp->palm.right_edge)) {
494                         delta = device_delta(t->point, t->palm.first);
495                         dirs = normalized_get_direction(
496                                                 tp_normalize_delta(tp, delta));
497                         if ((dirs & DIRECTIONS) && !(dirs & ~DIRECTIONS)) {
498                                 t->palm.is_palm = false;
499                         }
500                 }
501                 return;
502         }
503
504         /* palm must start in exclusion zone, it's ok to move into
505            the zone without being a palm */
506         if (t->state != TOUCH_BEGIN ||
507             (t->point.x > tp->palm.left_edge && t->point.x < tp->palm.right_edge))
508                 return;
509
510         /* don't detect palm in software button areas, it's
511            likely that legitimate touches start in the area
512            covered by the exclusion zone */
513         if (tp->buttons.is_clickpad &&
514             tp_button_is_inside_softbutton_area(tp, t))
515                 return;
516
517         t->palm.is_palm = true;
518         t->palm.time = time;
519         t->palm.first = t->point;
520 }
521
522 static void
523 tp_unhover_touches(struct tp_dispatch *tp, uint64_t time)
524 {
525         struct tp_touch *t;
526         unsigned int nfake_touches;
527         int i;
528
529         if (!tp->fake_touches && !tp->nfingers_down)
530                 return;
531
532         nfake_touches = tp_fake_finger_count(tp);
533         if (nfake_touches == FAKE_FINGER_OVERFLOW)
534                 return;
535
536         if (tp->nfingers_down == nfake_touches &&
537             ((tp->nfingers_down == 0 && !tp_fake_finger_is_touching(tp)) ||
538              (tp->nfingers_down > 0 && tp_fake_finger_is_touching(tp))))
539                 return;
540
541         /* if BTN_TOUCH is set and we have less fingers down than fake
542          * touches, switch each hovering touch to BEGIN
543          * until nfingers_down matches nfake_touches
544          */
545         if (tp_fake_finger_is_touching(tp) &&
546             tp->nfingers_down < nfake_touches) {
547                 for (i = 0; i < (int)tp->ntouches; i++) {
548                         t = tp_get_touch(tp, i);
549
550                         if (t->state == TOUCH_HOVERING) {
551                                 tp_begin_touch(tp, t, time);
552
553                                 if (tp->nfingers_down >= nfake_touches)
554                                         break;
555                         }
556                 }
557         }
558
559         /* if BTN_TOUCH is unset end all touches, we're hovering now. If we
560          * have too many touches also end some of them. This is done in
561          * reverse order.
562          */
563         if (tp->nfingers_down > nfake_touches ||
564             !tp_fake_finger_is_touching(tp)) {
565                 for (i = tp->ntouches - 1; i >= 0; i--) {
566                         t = tp_get_touch(tp, i);
567
568                         if (t->state == TOUCH_HOVERING ||
569                             t->state == TOUCH_NONE)
570                                 continue;
571
572                         tp_end_touch(tp, t, time);
573
574                         if (tp_fake_finger_is_touching(tp) &&
575                             tp->nfingers_down == nfake_touches)
576                                 break;
577                 }
578         }
579 }
580
581 static void
582 tp_process_state(struct tp_dispatch *tp, uint64_t time)
583 {
584         struct tp_touch *t;
585         struct tp_touch *first = tp_get_touch(tp, 0);
586         unsigned int i;
587
588         tp_process_fake_touches(tp, time);
589         tp_unhover_touches(tp, time);
590
591         for (i = 0; i < tp->ntouches; i++) {
592                 t = tp_get_touch(tp, i);
593
594                 /* semi-mt finger postions may "jump" when nfingers changes */
595                 if (tp->semi_mt && tp->nfingers_down != tp->old_nfingers_down)
596                         tp_motion_history_reset(t);
597
598                 if (i >= tp->num_slots && t->state != TOUCH_NONE) {
599                         t->point = first->point;
600                         if (!t->dirty)
601                                 t->dirty = first->dirty;
602                 }
603
604                 if (!t->dirty)
605                         continue;
606
607                 tp_palm_detect(tp, t, time);
608
609                 tp_motion_hysteresis(tp, t);
610                 tp_motion_history_push(t);
611
612                 tp_unpin_finger(tp, t);
613         }
614
615         tp_button_handle_state(tp, time);
616         tp_edge_scroll_handle_state(tp, time);
617
618         /*
619          * We have a physical button down event on a clickpad. To avoid
620          * spurious pointer moves by the clicking finger we pin all fingers.
621          * We unpin fingers when they move more then a certain threshold to
622          * to allow drag and drop.
623          */
624         if ((tp->queued & TOUCHPAD_EVENT_BUTTON_PRESS) &&
625             tp->buttons.is_clickpad)
626                 tp_pin_fingers(tp);
627
628         tp_gesture_handle_state(tp, time);
629 }
630
631 static void
632 tp_post_process_state(struct tp_dispatch *tp, uint64_t time)
633 {
634         struct tp_touch *t;
635
636         tp_for_each_touch(tp, t) {
637
638                 if (!t->dirty)
639                         continue;
640
641                 if (t->state == TOUCH_END) {
642                         if (t->has_ended)
643                                 t->state = TOUCH_NONE;
644                         else
645                                 t->state = TOUCH_HOVERING;
646                 } else if (t->state == TOUCH_BEGIN) {
647                         t->state = TOUCH_UPDATE;
648                 }
649
650                 t->dirty = false;
651         }
652
653         tp->old_nfingers_down = tp->nfingers_down;
654         tp->buttons.old_state = tp->buttons.state;
655
656         tp->queued = TOUCHPAD_EVENT_NONE;
657 }
658
659 static void
660 tp_post_events(struct tp_dispatch *tp, uint64_t time)
661 {
662         int filter_motion = 0;
663
664         /* Only post (top) button events while suspended */
665         if (tp->device->suspended) {
666                 tp_post_button_events(tp, time);
667                 return;
668         }
669
670         filter_motion |= tp_tap_handle_state(tp, time);
671         filter_motion |= tp_post_button_events(tp, time);
672
673         if (filter_motion || tp->sendevents.trackpoint_active) {
674                 tp_edge_scroll_stop_events(tp, time);
675                 tp_gesture_stop(tp, time);
676                 return;
677         }
678
679         if (tp_edge_scroll_post_events(tp, time) != 0)
680                 return;
681
682         tp_gesture_post_events(tp, time);
683 }
684
685 static void
686 tp_handle_state(struct tp_dispatch *tp,
687                 uint64_t time)
688 {
689         tp_process_state(tp, time);
690         tp_post_events(tp, time);
691         tp_post_process_state(tp, time);
692 }
693
694 static void
695 tp_process(struct evdev_dispatch *dispatch,
696            struct evdev_device *device,
697            struct input_event *e,
698            uint64_t time)
699 {
700         struct tp_dispatch *tp =
701                 (struct tp_dispatch *)dispatch;
702
703         switch (e->type) {
704         case EV_ABS:
705                 if (tp->has_mt)
706                         tp_process_absolute(tp, e, time);
707                 else
708                         tp_process_absolute_st(tp, e, time);
709                 break;
710         case EV_KEY:
711                 tp_process_key(tp, e, time);
712                 break;
713         case EV_SYN:
714                 tp_handle_state(tp, time);
715                 break;
716         }
717 }
718
719 static void
720 tp_remove_sendevents(struct tp_dispatch *tp)
721 {
722         libinput_timer_cancel(&tp->sendevents.trackpoint_timer);
723
724         if (tp->buttons.trackpoint)
725                 libinput_device_remove_event_listener(
726                                         &tp->sendevents.trackpoint_listener);
727 }
728
729 static void
730 tp_remove(struct evdev_dispatch *dispatch)
731 {
732         struct tp_dispatch *tp =
733                 (struct tp_dispatch*)dispatch;
734
735         tp_remove_tap(tp);
736         tp_remove_buttons(tp);
737         tp_remove_sendevents(tp);
738         tp_remove_edge_scroll(tp);
739         tp_remove_gesture(tp);
740 }
741
742 static void
743 tp_destroy(struct evdev_dispatch *dispatch)
744 {
745         struct tp_dispatch *tp =
746                 (struct tp_dispatch*)dispatch;
747
748         free(tp->touches);
749         free(tp);
750 }
751
752 static void
753 tp_clear_state(struct tp_dispatch *tp)
754 {
755         uint64_t now = libinput_now(tp->device->base.seat->libinput);
756         struct tp_touch *t;
757
758         /* Unroll the touchpad state.
759          * Release buttons first. If tp is a clickpad, the button event
760          * must come before the touch up. If it isn't, the order doesn't
761          * matter anyway
762          *
763          * Then cancel all timeouts on the taps, triggering the last set
764          * of events.
765          *
766          * Then lift all touches so the touchpad is in a neutral state.
767          *
768          */
769         tp_release_all_buttons(tp, now);
770         tp_release_all_taps(tp, now);
771
772         tp_for_each_touch(tp, t) {
773                 tp_end_sequence(tp, t, now);
774         }
775
776         tp_handle_state(tp, now);
777 }
778
779 static void
780 tp_suspend(struct tp_dispatch *tp, struct evdev_device *device)
781 {
782         tp_clear_state(tp);
783
784         /* On devices with top softwarebuttons we don't actually suspend the
785          * device, to keep the "trackpoint" buttons working. tp_post_events()
786          * will only send events for the trackpoint while suspended.
787          */
788         if (tp->buttons.has_topbuttons) {
789                 evdev_notify_suspended_device(device);
790                 /* Enlarge topbutton area while suspended */
791                 tp_init_top_softbuttons(tp, device, 1.5);
792         } else {
793                 evdev_device_suspend(device);
794         }
795 }
796
797 static void
798 tp_resume(struct tp_dispatch *tp, struct evdev_device *device)
799 {
800         if (tp->buttons.has_topbuttons) {
801                 /* tap state-machine is offline while suspended, reset state */
802                 tp_clear_state(tp);
803                 /* restore original topbutton area size */
804                 tp_init_top_softbuttons(tp, device, 1.0);
805                 evdev_notify_resumed_device(device);
806         } else {
807                 evdev_device_resume(device);
808         }
809 }
810
811 static void
812 tp_trackpoint_timeout(uint64_t now, void *data)
813 {
814         struct tp_dispatch *tp = data;
815
816         tp_tap_resume(tp, now);
817         tp->sendevents.trackpoint_active = false;
818 }
819
820 static void
821 tp_trackpoint_event(uint64_t time, struct libinput_event *event, void *data)
822 {
823         struct tp_dispatch *tp = data;
824
825         /* Buttons do not count as trackpad activity, as people may use
826            the trackpoint buttons in combination with the touchpad. */
827         if (event->type == LIBINPUT_EVENT_POINTER_BUTTON)
828                 return;
829
830         if (!tp->sendevents.trackpoint_active) {
831                 tp_edge_scroll_stop_events(tp, time);
832                 tp_gesture_stop(tp, time);
833                 tp_tap_suspend(tp, time);
834                 tp->sendevents.trackpoint_active = true;
835         }
836
837         libinput_timer_set(&tp->sendevents.trackpoint_timer,
838                            time + DEFAULT_TRACKPOINT_ACTIVITY_TIMEOUT);
839 }
840
841 static void
842 tp_device_added(struct evdev_device *device,
843                 struct evdev_device *added_device)
844 {
845         struct tp_dispatch *tp = (struct tp_dispatch*)device->dispatch;
846         unsigned int bus_tp = libevdev_get_id_bustype(device->evdev),
847                      bus_trp = libevdev_get_id_bustype(added_device->evdev);
848         bool tp_is_internal, trp_is_internal;
849
850         tp_is_internal = bus_tp != BUS_USB && bus_tp != BUS_BLUETOOTH;
851         trp_is_internal = bus_trp != BUS_USB && bus_trp != BUS_BLUETOOTH;
852
853         if (tp->buttons.trackpoint == NULL &&
854             (added_device->tags & EVDEV_TAG_TRACKPOINT) &&
855             tp_is_internal && trp_is_internal) {
856                 /* Don't send any pending releases to the new trackpoint */
857                 tp->buttons.active_is_topbutton = false;
858                 tp->buttons.trackpoint = added_device;
859                 libinput_device_add_event_listener(&added_device->base,
860                                         &tp->sendevents.trackpoint_listener,
861                                         tp_trackpoint_event, tp);
862         }
863
864         if (tp->sendevents.current_mode !=
865             LIBINPUT_CONFIG_SEND_EVENTS_DISABLED_ON_EXTERNAL_MOUSE)
866                 return;
867
868         if (added_device->tags & EVDEV_TAG_EXTERNAL_MOUSE)
869                 tp_suspend(tp, device);
870 }
871
872 static void
873 tp_device_removed(struct evdev_device *device,
874                   struct evdev_device *removed_device)
875 {
876         struct tp_dispatch *tp = (struct tp_dispatch*)device->dispatch;
877         struct libinput_device *dev;
878
879         if (removed_device == tp->buttons.trackpoint) {
880                 /* Clear any pending releases for the trackpoint */
881                 if (tp->buttons.active && tp->buttons.active_is_topbutton) {
882                         tp->buttons.active = 0;
883                         tp->buttons.active_is_topbutton = false;
884                 }
885                 libinput_device_remove_event_listener(
886                                         &tp->sendevents.trackpoint_listener);
887                 tp->buttons.trackpoint = NULL;
888         }
889
890         if (tp->sendevents.current_mode !=
891             LIBINPUT_CONFIG_SEND_EVENTS_DISABLED_ON_EXTERNAL_MOUSE)
892                 return;
893
894         list_for_each(dev, &device->base.seat->devices_list, link) {
895                 struct evdev_device *d = (struct evdev_device*)dev;
896                 if (d != removed_device &&
897                     (d->tags & EVDEV_TAG_EXTERNAL_MOUSE)) {
898                         return;
899                 }
900         }
901
902         tp_resume(tp, device);
903 }
904
905 static void
906 tp_tag_device(struct evdev_device *device,
907               struct udev_device *udev_device)
908 {
909         int bustype;
910
911         /* simple approach: touchpads on USB or Bluetooth are considered
912          * external, anything else is internal. Exception is Apple -
913          * internal touchpads are connected over USB and it doesn't have
914          * external USB touchpads anyway.
915          */
916         bustype = libevdev_get_id_bustype(device->evdev);
917         if (bustype == BUS_USB) {
918                  if (libevdev_get_id_vendor(device->evdev) == VENDOR_ID_APPLE)
919                          device->tags |= EVDEV_TAG_INTERNAL_TOUCHPAD;
920         } else if (bustype != BUS_BLUETOOTH)
921                 device->tags |= EVDEV_TAG_INTERNAL_TOUCHPAD;
922
923         if (udev_device_get_property_value(udev_device,
924                                            "TOUCHPAD_HAS_TRACKPOINT_BUTTONS"))
925                 device->tags |= EVDEV_TAG_TOUCHPAD_TRACKPOINT;
926 }
927
928 static struct evdev_dispatch_interface tp_interface = {
929         tp_process,
930         tp_remove,
931         tp_destroy,
932         tp_device_added,
933         tp_device_removed,
934         tp_device_removed, /* device_suspended, treat as remove */
935         tp_device_added,   /* device_resumed, treat as add */
936         tp_tag_device,
937 };
938
939 static void
940 tp_init_touch(struct tp_dispatch *tp,
941               struct tp_touch *t)
942 {
943         t->tp = tp;
944         t->has_ended = true;
945 }
946
947 static int
948 tp_init_slots(struct tp_dispatch *tp,
949               struct evdev_device *device)
950 {
951         const struct input_absinfo *absinfo;
952         struct map {
953                 unsigned int code;
954                 int ntouches;
955         } max_touches[] = {
956                 { BTN_TOOL_QUINTTAP, 5 },
957                 { BTN_TOOL_QUADTAP, 4 },
958                 { BTN_TOOL_TRIPLETAP, 3 },
959                 { BTN_TOOL_DOUBLETAP, 2 },
960         };
961         struct map *m;
962         unsigned int i, n_btn_tool_touches = 1;
963
964         absinfo = libevdev_get_abs_info(device->evdev, ABS_MT_SLOT);
965         if (absinfo) {
966                 tp->num_slots = absinfo->maximum + 1;
967                 tp->slot = absinfo->value;
968                 tp->has_mt = true;
969         } else {
970                 tp->num_slots = 1;
971                 tp->slot = 0;
972                 tp->has_mt = false;
973         }
974
975         tp->semi_mt = libevdev_has_property(device->evdev, INPUT_PROP_SEMI_MT);
976
977         ARRAY_FOR_EACH(max_touches, m) {
978                 if (libevdev_has_event_code(device->evdev,
979                                             EV_KEY,
980                                             m->code)) {
981                         n_btn_tool_touches = m->ntouches;
982                         break;
983                 }
984         }
985
986         tp->ntouches = max(tp->num_slots, n_btn_tool_touches);
987         tp->touches = calloc(tp->ntouches, sizeof(struct tp_touch));
988         if (!tp->touches)
989                 return -1;
990
991         for (i = 0; i < tp->ntouches; i++)
992                 tp_init_touch(tp, &tp->touches[i]);
993
994         return 0;
995 }
996
997 static int
998 tp_init_accel(struct tp_dispatch *tp, double diagonal)
999 {
1000         int res_x, res_y;
1001         accel_profile_func_t profile;
1002
1003         res_x = tp->device->abs.absinfo_x->resolution;
1004         res_y = tp->device->abs.absinfo_y->resolution;
1005
1006         /*
1007          * Not all touchpads report the same amount of units/mm (resolution).
1008          * Normalize motion events to the default mouse DPI as base
1009          * (unaccelerated) speed. This also evens out any differences in x
1010          * and y resolution, so that a circle on the
1011          * touchpad does not turn into an elipse on the screen.
1012          */
1013         if (res_x > 1 && res_y > 1) {
1014                 tp->accel.x_scale_coeff = (DEFAULT_MOUSE_DPI/25.4) / res_x;
1015                 tp->accel.y_scale_coeff = (DEFAULT_MOUSE_DPI/25.4) / res_y;
1016         } else {
1017         /*
1018          * For touchpads where the driver does not provide resolution, fall
1019          * back to scaling motion events based on the diagonal size in units.
1020          */
1021                 tp->accel.x_scale_coeff = DEFAULT_ACCEL_NUMERATOR / diagonal;
1022                 tp->accel.y_scale_coeff = DEFAULT_ACCEL_NUMERATOR / diagonal;
1023         }
1024
1025         switch (tp->device->model) {
1026         case EVDEV_MODEL_LENOVO_X230:
1027                 profile = touchpad_lenovo_x230_accel_profile;
1028                 break;
1029         default:
1030                 profile = touchpad_accel_profile_linear;
1031                 break;
1032         }
1033
1034         if (evdev_device_init_pointer_acceleration(tp->device, profile) == -1)
1035                 return -1;
1036
1037         return 0;
1038 }
1039
1040 static uint32_t
1041 tp_scroll_config_scroll_method_get_methods(struct libinput_device *device)
1042 {
1043         struct evdev_device *evdev = (struct evdev_device*)device;
1044         struct tp_dispatch *tp = (struct tp_dispatch*)evdev->dispatch;
1045         uint32_t methods = LIBINPUT_CONFIG_SCROLL_NO_SCROLL;
1046
1047         if (tp->ntouches >= 2)
1048                 methods |= LIBINPUT_CONFIG_SCROLL_2FG;
1049
1050         if (!tp->buttons.is_clickpad)
1051                 methods |= LIBINPUT_CONFIG_SCROLL_EDGE;
1052
1053         return methods;
1054 }
1055
1056 static enum libinput_config_status
1057 tp_scroll_config_scroll_method_set_method(struct libinput_device *device,
1058                         enum libinput_config_scroll_method method)
1059 {
1060         struct evdev_device *evdev = (struct evdev_device*)device;
1061         struct tp_dispatch *tp = (struct tp_dispatch*)evdev->dispatch;
1062         uint64_t time = libinput_now(device->seat->libinput);
1063
1064         if (method == tp->scroll.method)
1065                 return LIBINPUT_CONFIG_STATUS_SUCCESS;
1066
1067         tp_edge_scroll_stop_events(tp, time);
1068         tp_gesture_stop_twofinger_scroll(tp, time);
1069
1070         tp->scroll.method = method;
1071
1072         return LIBINPUT_CONFIG_STATUS_SUCCESS;
1073 }
1074
1075 static enum libinput_config_scroll_method
1076 tp_scroll_config_scroll_method_get_method(struct libinput_device *device)
1077 {
1078         struct evdev_device *evdev = (struct evdev_device*)device;
1079         struct tp_dispatch *tp = (struct tp_dispatch*)evdev->dispatch;
1080
1081         return tp->scroll.method;
1082 }
1083
1084 static enum libinput_config_scroll_method
1085 tp_scroll_get_default_method(struct tp_dispatch *tp)
1086 {
1087         if (tp->ntouches >= 2)
1088                 return LIBINPUT_CONFIG_SCROLL_2FG;
1089         else
1090                 return LIBINPUT_CONFIG_SCROLL_EDGE;
1091 }
1092
1093 static enum libinput_config_scroll_method
1094 tp_scroll_config_scroll_method_get_default_method(struct libinput_device *device)
1095 {
1096         struct evdev_device *evdev = (struct evdev_device*)device;
1097         struct tp_dispatch *tp = (struct tp_dispatch*)evdev->dispatch;
1098
1099         return tp_scroll_get_default_method(tp);
1100 }
1101
1102 static int
1103 tp_init_scroll(struct tp_dispatch *tp, struct evdev_device *device)
1104 {
1105         if (tp_edge_scroll_init(tp, device) != 0)
1106                 return -1;
1107
1108         evdev_init_natural_scroll(device);
1109
1110         tp->scroll.config_method.get_methods = tp_scroll_config_scroll_method_get_methods;
1111         tp->scroll.config_method.set_method = tp_scroll_config_scroll_method_set_method;
1112         tp->scroll.config_method.get_method = tp_scroll_config_scroll_method_get_method;
1113         tp->scroll.config_method.get_default_method = tp_scroll_config_scroll_method_get_default_method;
1114         tp->scroll.method = tp_scroll_get_default_method(tp);
1115         tp->device->base.config.scroll_method = &tp->scroll.config_method;
1116
1117         /* In mm for touchpads with valid resolution, see tp_init_accel() */
1118         tp->device->scroll.threshold = 5.0;
1119
1120         return 0;
1121 }
1122
1123 static int
1124 tp_init_palmdetect(struct tp_dispatch *tp,
1125                    struct evdev_device *device)
1126 {
1127         int width, height;
1128
1129         tp->palm.right_edge = INT_MAX;
1130         tp->palm.left_edge = INT_MIN;
1131         tp->palm.vert_center = INT_MIN;
1132
1133         width = abs(device->abs.absinfo_x->maximum -
1134                     device->abs.absinfo_x->minimum);
1135         height = abs(device->abs.absinfo_y->maximum -
1136                     device->abs.absinfo_y->minimum);
1137
1138         /* Apple touchpads are always big enough to warrant palm detection */
1139         if (evdev_device_get_id_vendor(device) != VENDOR_ID_APPLE) {
1140                 /* We don't know how big the touchpad is */
1141                 if (device->abs.absinfo_x->resolution == 1)
1142                         return 0;
1143
1144                 /* Enable palm detection on touchpads >= 70 mm. Anything smaller
1145                    probably won't need it, until we find out it does */
1146                 if (width/device->abs.absinfo_x->resolution < 70)
1147                         return 0;
1148         }
1149
1150         /* palm edges are 5% of the width on each side */
1151         tp->palm.right_edge = device->abs.absinfo_x->maximum - width * 0.05;
1152         tp->palm.left_edge = device->abs.absinfo_x->minimum + width * 0.05;
1153         tp->palm.vert_center = device->abs.absinfo_y->minimum + height/2;
1154
1155         return 0;
1156 }
1157
1158 static int
1159 tp_init_sendevents(struct tp_dispatch *tp,
1160                    struct evdev_device *device)
1161 {
1162         libinput_timer_init(&tp->sendevents.trackpoint_timer,
1163                             tp->device->base.seat->libinput,
1164                             tp_trackpoint_timeout, tp);
1165         return 0;
1166 }
1167
1168 static int
1169 tp_init(struct tp_dispatch *tp,
1170         struct evdev_device *device)
1171 {
1172         int width, height;
1173         double diagonal;
1174
1175         tp->base.interface = &tp_interface;
1176         tp->device = device;
1177
1178         if (tp_init_slots(tp, device) != 0)
1179                 return -1;
1180
1181         width = abs(device->abs.absinfo_x->maximum -
1182                     device->abs.absinfo_x->minimum);
1183         height = abs(device->abs.absinfo_y->maximum -
1184                      device->abs.absinfo_y->minimum);
1185         diagonal = sqrt(width*width + height*height);
1186
1187         tp->hysteresis_margin.x =
1188                 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
1189         tp->hysteresis_margin.y =
1190                 diagonal / DEFAULT_HYSTERESIS_MARGIN_DENOMINATOR;
1191
1192         if (tp_init_accel(tp, diagonal) != 0)
1193                 return -1;
1194
1195         if (tp_init_tap(tp) != 0)
1196                 return -1;
1197
1198         if (tp_init_buttons(tp, device) != 0)
1199                 return -1;
1200
1201         if (tp_init_palmdetect(tp, device) != 0)
1202                 return -1;
1203
1204         if (tp_init_sendevents(tp, device) != 0)
1205                 return -1;
1206
1207         if (tp_init_scroll(tp, device) != 0)
1208                 return -1;
1209
1210         if (tp_init_gesture(tp) != 0)
1211                 return -1;
1212
1213         device->seat_caps |= EVDEV_DEVICE_POINTER;
1214
1215         return 0;
1216 }
1217
1218 static uint32_t
1219 tp_sendevents_get_modes(struct libinput_device *device)
1220 {
1221         struct evdev_device *evdev = (struct evdev_device*)device;
1222         uint32_t modes = LIBINPUT_CONFIG_SEND_EVENTS_DISABLED;
1223
1224         if (evdev->tags & EVDEV_TAG_INTERNAL_TOUCHPAD)
1225                 modes |= LIBINPUT_CONFIG_SEND_EVENTS_DISABLED_ON_EXTERNAL_MOUSE;
1226
1227         return modes;
1228 }
1229
1230 static void
1231 tp_suspend_conditional(struct tp_dispatch *tp,
1232                        struct evdev_device *device)
1233 {
1234         struct libinput_device *dev;
1235
1236         list_for_each(dev, &device->base.seat->devices_list, link) {
1237                 struct evdev_device *d = (struct evdev_device*)dev;
1238                 if (d->tags & EVDEV_TAG_EXTERNAL_MOUSE) {
1239                         tp_suspend(tp, device);
1240                         return;
1241                 }
1242         }
1243 }
1244
1245 static enum libinput_config_status
1246 tp_sendevents_set_mode(struct libinput_device *device,
1247                        enum libinput_config_send_events_mode mode)
1248 {
1249         struct evdev_device *evdev = (struct evdev_device*)device;
1250         struct tp_dispatch *tp = (struct tp_dispatch*)evdev->dispatch;
1251
1252         /* DISABLED overrides any DISABLED_ON_ */
1253         if ((mode & LIBINPUT_CONFIG_SEND_EVENTS_DISABLED) &&
1254             (mode & LIBINPUT_CONFIG_SEND_EVENTS_DISABLED_ON_EXTERNAL_MOUSE))
1255             mode &= ~LIBINPUT_CONFIG_SEND_EVENTS_DISABLED_ON_EXTERNAL_MOUSE;
1256
1257         if (mode == tp->sendevents.current_mode)
1258                 return LIBINPUT_CONFIG_STATUS_SUCCESS;
1259
1260         switch(mode) {
1261         case LIBINPUT_CONFIG_SEND_EVENTS_ENABLED:
1262                 tp_resume(tp, evdev);
1263                 break;
1264         case LIBINPUT_CONFIG_SEND_EVENTS_DISABLED:
1265                 tp_suspend(tp, evdev);
1266                 break;
1267         case LIBINPUT_CONFIG_SEND_EVENTS_DISABLED_ON_EXTERNAL_MOUSE:
1268                 tp_suspend_conditional(tp, evdev);
1269                 break;
1270         default:
1271                 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
1272         }
1273
1274         tp->sendevents.current_mode = mode;
1275
1276         return LIBINPUT_CONFIG_STATUS_SUCCESS;
1277 }
1278
1279 static enum libinput_config_send_events_mode
1280 tp_sendevents_get_mode(struct libinput_device *device)
1281 {
1282         struct evdev_device *evdev = (struct evdev_device*)device;
1283         struct tp_dispatch *dispatch = (struct tp_dispatch*)evdev->dispatch;
1284
1285         return dispatch->sendevents.current_mode;
1286 }
1287
1288 static enum libinput_config_send_events_mode
1289 tp_sendevents_get_default_mode(struct libinput_device *device)
1290 {
1291         return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
1292 }
1293
1294 static void
1295 tp_change_to_left_handed(struct evdev_device *device)
1296 {
1297         struct tp_dispatch *tp = (struct tp_dispatch *)device->dispatch;
1298
1299         if (device->left_handed.want_enabled == device->left_handed.enabled)
1300                 return;
1301
1302         if (tp->buttons.state & 0x3) /* BTN_LEFT|BTN_RIGHT */
1303                 return;
1304
1305         /* tapping and clickfinger aren't affected by left-handed config,
1306          * so checking physical buttons is enough */
1307
1308         device->left_handed.enabled = device->left_handed.want_enabled;
1309 }
1310
1311 struct model_lookup_t {
1312         uint16_t vendor;
1313         uint16_t product_start;
1314         uint16_t product_end;
1315         enum touchpad_model model;
1316 };
1317
1318 static struct model_lookup_t model_lookup_table[] = {
1319         { 0x0002, 0x0007, 0x0007, MODEL_SYNAPTICS },
1320         { 0x0002, 0x0008, 0x0008, MODEL_ALPS },
1321         { 0x0002, 0x000e, 0x000e, MODEL_ELANTECH },
1322         { 0x05ac,      0, 0x0222, MODEL_APPLETOUCH },
1323         { 0x05ac, 0x0223, 0x0228, MODEL_UNIBODY_MACBOOK },
1324         { 0x05ac, 0x0229, 0x022b, MODEL_APPLETOUCH },
1325         { 0x05ac, 0x022c, 0xffff, MODEL_UNIBODY_MACBOOK },
1326         { 0, 0, 0, 0 }
1327 };
1328
1329 static enum touchpad_model
1330 tp_get_model(struct evdev_device *device)
1331 {
1332         struct model_lookup_t *lookup;
1333         uint16_t vendor  = libevdev_get_id_vendor(device->evdev);
1334         uint16_t product = libevdev_get_id_product(device->evdev);
1335
1336         for (lookup = model_lookup_table; lookup->vendor; lookup++) {
1337                 if (lookup->vendor == vendor &&
1338                     lookup->product_start <= product &&
1339                     product <= lookup->product_end)
1340                         return lookup->model;
1341         }
1342         return MODEL_UNKNOWN;
1343 }
1344
1345 struct evdev_dispatch *
1346 evdev_mt_touchpad_create(struct evdev_device *device)
1347 {
1348         struct tp_dispatch *tp;
1349
1350         tp = zalloc(sizeof *tp);
1351         if (!tp)
1352                 return NULL;
1353
1354         tp->model = tp_get_model(device);
1355
1356         if (tp_init(tp, device) != 0) {
1357                 tp_destroy(&tp->base);
1358                 return NULL;
1359         }
1360
1361         device->base.config.sendevents = &tp->sendevents.config;
1362
1363         tp->sendevents.current_mode = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
1364         tp->sendevents.config.get_modes = tp_sendevents_get_modes;
1365         tp->sendevents.config.set_mode = tp_sendevents_set_mode;
1366         tp->sendevents.config.get_mode = tp_sendevents_get_mode;
1367         tp->sendevents.config.get_default_mode = tp_sendevents_get_default_mode;
1368
1369         evdev_init_left_handed(device, tp_change_to_left_handed);
1370
1371         return  &tp->base;
1372 }