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