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