Introduce unaccelerated motion event vectors
[platform/upstream/libinput.git] / src / evdev.c
1 /*
2  * Copyright © 2010 Intel Corporation
3  * Copyright © 2013 Jonas Ådahl
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and
6  * its documentation for any purpose is hereby granted without fee, provided
7  * that the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation, and that the name of the copyright holders not be used in
10  * advertising or publicity pertaining to distribution of the software
11  * without specific, written prior permission.  The copyright holders make
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied warranty.
14  *
15  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  */
23
24 #include "config.h"
25
26 #include <errno.h>
27 #include <stdbool.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include "linux/input.h"
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <mtdev-plumbing.h>
34 #include <assert.h>
35 #include <time.h>
36 #include <math.h>
37
38 #include "libinput.h"
39 #include "evdev.h"
40 #include "filter.h"
41 #include "libinput-private.h"
42
43 #define DEFAULT_AXIS_STEP_DISTANCE 10
44 #define DEFAULT_MIDDLE_BUTTON_SCROLL_TIMEOUT 200
45
46 enum evdev_key_type {
47         EVDEV_KEY_TYPE_NONE,
48         EVDEV_KEY_TYPE_KEY,
49         EVDEV_KEY_TYPE_BUTTON,
50 };
51
52 static void
53 hw_set_key_down(struct evdev_device *device, int code, int pressed)
54 {
55         long_set_bit_state(device->hw_key_mask, code, pressed);
56 }
57
58 static int
59 hw_is_key_down(struct evdev_device *device, int code)
60 {
61         return long_bit_is_set(device->hw_key_mask, code);
62 }
63
64 static int
65 get_key_down_count(struct evdev_device *device, int code)
66 {
67         return device->key_count[code];
68 }
69
70 static int
71 update_key_down_count(struct evdev_device *device, int code, int pressed)
72 {
73         int key_count;
74         assert(code >= 0 && code < KEY_CNT);
75
76         if (pressed) {
77                 key_count = ++device->key_count[code];
78         } else {
79                 assert(device->key_count[code] > 0);
80                 key_count = --device->key_count[code];
81         }
82
83         if (key_count > 32) {
84                 log_bug_libinput(device->base.seat->libinput,
85                                  "Key count for %s reached abnormal values\n",
86                                  libevdev_event_code_get_name(EV_KEY, code));
87         }
88
89         return key_count;
90 }
91
92 void
93 evdev_keyboard_notify_key(struct evdev_device *device,
94                           uint32_t time,
95                           int key,
96                           enum libinput_key_state state)
97 {
98         int down_count;
99
100         down_count = update_key_down_count(device, key, state);
101
102         if ((state == LIBINPUT_KEY_STATE_PRESSED && down_count == 1) ||
103             (state == LIBINPUT_KEY_STATE_RELEASED && down_count == 0))
104                 keyboard_notify_key(&device->base, time, key, state);
105 }
106
107 void
108 evdev_pointer_notify_button(struct evdev_device *device,
109                             uint32_t time,
110                             int button,
111                             enum libinput_button_state state)
112 {
113         int down_count;
114
115         down_count = update_key_down_count(device, button, state);
116
117         if ((state == LIBINPUT_BUTTON_STATE_PRESSED && down_count == 1) ||
118             (state == LIBINPUT_BUTTON_STATE_RELEASED && down_count == 0)) {
119                 pointer_notify_button(&device->base, time, button, state);
120
121                 if (state == LIBINPUT_BUTTON_STATE_RELEASED &&
122                     device->buttons.change_to_left_handed)
123                         device->buttons.change_to_left_handed(device);
124
125                 if (state == LIBINPUT_BUTTON_STATE_RELEASED &&
126                     device->scroll.change_scroll_method)
127                         device->scroll.change_scroll_method(device);
128         }
129
130 }
131
132 void
133 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)
134 {
135         static const struct {
136                 enum libinput_led weston;
137                 int evdev;
138         } map[] = {
139                 { LIBINPUT_LED_NUM_LOCK, LED_NUML },
140                 { LIBINPUT_LED_CAPS_LOCK, LED_CAPSL },
141                 { LIBINPUT_LED_SCROLL_LOCK, LED_SCROLLL },
142         };
143         struct input_event ev[ARRAY_LENGTH(map) + 1];
144         unsigned int i;
145
146         if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
147                 return;
148
149         memset(ev, 0, sizeof(ev));
150         for (i = 0; i < ARRAY_LENGTH(map); i++) {
151                 ev[i].type = EV_LED;
152                 ev[i].code = map[i].evdev;
153                 ev[i].value = !!(leds & map[i].weston);
154         }
155         ev[i].type = EV_SYN;
156         ev[i].code = SYN_REPORT;
157
158         i = write(device->fd, ev, sizeof ev);
159         (void)i; /* no, we really don't care about the return value */
160 }
161
162 static void
163 transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
164 {
165         if (!device->abs.apply_calibration)
166                 return;
167
168         matrix_mult_vec(&device->abs.calibration, x, y);
169 }
170
171 static inline double
172 scale_axis(const struct input_absinfo *absinfo, double val, double to_range)
173 {
174         return (val - absinfo->minimum) * to_range /
175                 (absinfo->maximum - absinfo->minimum + 1);
176 }
177
178 double
179 evdev_device_transform_x(struct evdev_device *device,
180                          double x,
181                          uint32_t width)
182 {
183         return scale_axis(device->abs.absinfo_x, x, width);
184 }
185
186 double
187 evdev_device_transform_y(struct evdev_device *device,
188                          double y,
189                          uint32_t height)
190 {
191         return scale_axis(device->abs.absinfo_y, y, height);
192 }
193
194 static void
195 evdev_flush_pending_event(struct evdev_device *device, uint64_t time)
196 {
197         struct libinput *libinput = device->base.seat->libinput;
198         struct motion_params motion;
199         double dx_unaccel, dy_unaccel;
200         int32_t cx, cy;
201         int32_t x, y;
202         int slot;
203         int seat_slot;
204         struct libinput_device *base = &device->base;
205         struct libinput_seat *seat = base->seat;
206
207         slot = device->mt.slot;
208
209         switch (device->pending_event) {
210         case EVDEV_NONE:
211                 return;
212         case EVDEV_RELATIVE_MOTION:
213                 dx_unaccel = device->rel.dx / ((double) device->dpi /
214                                                DEFAULT_MOUSE_DPI);
215                 dy_unaccel = device->rel.dy / ((double) device->dpi /
216                                                DEFAULT_MOUSE_DPI);
217                 device->rel.dx = 0;
218                 device->rel.dy = 0;
219
220                 /* Use unaccelerated deltas for pointing stick scroll */
221                 if (device->scroll.method == LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN &&
222                     hw_is_key_down(device, device->scroll.button)) {
223                         if (device->scroll.button_scroll_active)
224                                 evdev_post_scroll(device, time,
225                                                   dx_unaccel, dy_unaccel);
226                         break;
227                 }
228
229                 /* Apply pointer acceleration. */
230                 motion.dx = dx_unaccel;
231                 motion.dy = dy_unaccel;
232                 filter_dispatch(device->pointer.filter, &motion, device, time);
233
234                 if (motion.dx == 0.0 && motion.dy == 0.0 &&
235                     dx_unaccel == 0.0 && dy_unaccel == 0.0) {
236                         break;
237                 }
238
239                 pointer_notify_motion(base, time,
240                                       motion.dx, motion.dy,
241                                       dx_unaccel, dy_unaccel);
242                 break;
243         case EVDEV_ABSOLUTE_MT_DOWN:
244                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
245                         break;
246
247                 if (device->mt.slots[slot].seat_slot != -1) {
248                         log_bug_kernel(libinput,
249                                        "%s: Driver sent multiple touch down for the "
250                                        "same slot",
251                                        udev_device_get_devnode(device->udev_device));
252                         break;
253                 }
254
255                 seat_slot = ffs(~seat->slot_map) - 1;
256                 device->mt.slots[slot].seat_slot = seat_slot;
257
258                 if (seat_slot == -1)
259                         break;
260
261                 seat->slot_map |= 1 << seat_slot;
262                 x = device->mt.slots[slot].x;
263                 y = device->mt.slots[slot].y;
264                 transform_absolute(device, &x, &y);
265
266                 touch_notify_touch_down(base, time, slot, seat_slot, x, y);
267                 break;
268         case EVDEV_ABSOLUTE_MT_MOTION:
269                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
270                         break;
271
272                 seat_slot = device->mt.slots[slot].seat_slot;
273                 x = device->mt.slots[slot].x;
274                 y = device->mt.slots[slot].y;
275
276                 if (seat_slot == -1)
277                         break;
278
279                 transform_absolute(device, &x, &y);
280                 touch_notify_touch_motion(base, time, slot, seat_slot, x, y);
281                 break;
282         case EVDEV_ABSOLUTE_MT_UP:
283                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
284                         break;
285
286                 seat_slot = device->mt.slots[slot].seat_slot;
287                 device->mt.slots[slot].seat_slot = -1;
288
289                 if (seat_slot == -1)
290                         break;
291
292                 seat->slot_map &= ~(1 << seat_slot);
293
294                 touch_notify_touch_up(base, time, slot, seat_slot);
295                 break;
296         case EVDEV_ABSOLUTE_TOUCH_DOWN:
297                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
298                         break;
299
300                 if (device->abs.seat_slot != -1) {
301                         log_bug_kernel(libinput,
302                                        "%s: Driver sent multiple touch down for the "
303                                        "same slot",
304                                        udev_device_get_devnode(device->udev_device));
305                         break;
306                 }
307
308                 seat_slot = ffs(~seat->slot_map) - 1;
309                 device->abs.seat_slot = seat_slot;
310
311                 if (seat_slot == -1)
312                         break;
313
314                 seat->slot_map |= 1 << seat_slot;
315
316                 cx = device->abs.x;
317                 cy = device->abs.y;
318                 transform_absolute(device, &cx, &cy);
319
320                 touch_notify_touch_down(base, time, -1, seat_slot, cx, cy);
321                 break;
322         case EVDEV_ABSOLUTE_MOTION:
323                 cx = device->abs.x;
324                 cy = device->abs.y;
325                 transform_absolute(device, &cx, &cy);
326                 x = cx;
327                 y = cy;
328
329                 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
330                         seat_slot = device->abs.seat_slot;
331
332                         if (seat_slot == -1)
333                                 break;
334
335                         touch_notify_touch_motion(base, time, -1, seat_slot, x, y);
336                 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
337                         pointer_notify_motion_absolute(base, time, x, y);
338                 }
339                 break;
340         case EVDEV_ABSOLUTE_TOUCH_UP:
341                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
342                         break;
343
344                 seat_slot = device->abs.seat_slot;
345                 device->abs.seat_slot = -1;
346
347                 if (seat_slot == -1)
348                         break;
349
350                 seat->slot_map &= ~(1 << seat_slot);
351
352                 touch_notify_touch_up(base, time, -1, seat_slot);
353                 break;
354         default:
355                 assert(0 && "Unknown pending event type");
356                 break;
357         }
358
359         device->pending_event = EVDEV_NONE;
360 }
361
362 static enum evdev_key_type
363 get_key_type(uint16_t code)
364 {
365         if (code == BTN_TOUCH)
366                 return EVDEV_KEY_TYPE_NONE;
367
368         if (code >= KEY_ESC && code <= KEY_MICMUTE)
369                 return EVDEV_KEY_TYPE_KEY;
370         if (code >= BTN_MISC && code <= BTN_GEAR_UP)
371                 return EVDEV_KEY_TYPE_BUTTON;
372         if (code >= KEY_OK && code <= KEY_LIGHTS_TOGGLE)
373                 return EVDEV_KEY_TYPE_KEY;
374         if (code >= BTN_DPAD_UP && code <= BTN_TRIGGER_HAPPY40)
375                 return EVDEV_KEY_TYPE_BUTTON;
376         return EVDEV_KEY_TYPE_NONE;
377 }
378
379 static void
380 evdev_button_scroll_timeout(uint64_t time, void *data)
381 {
382         struct evdev_device *device = data;
383
384         device->scroll.button_scroll_active = true;
385 }
386
387 static void
388 evdev_button_scroll_button(struct evdev_device *device,
389                            uint64_t time, int is_press)
390 {
391         if (is_press) {
392                 libinput_timer_set(&device->scroll.timer,
393                                 time + DEFAULT_MIDDLE_BUTTON_SCROLL_TIMEOUT);
394         } else {
395                 libinput_timer_cancel(&device->scroll.timer);
396                 if (device->scroll.button_scroll_active) {
397                         evdev_stop_scroll(device, time);
398                         device->scroll.button_scroll_active = false;
399                 } else {
400                         /* If the button is released quickly enough emit the
401                          * button press/release events. */
402                         evdev_pointer_notify_button(device, time,
403                                         device->scroll.button,
404                                         LIBINPUT_BUTTON_STATE_PRESSED);
405                         evdev_pointer_notify_button(device, time,
406                                         device->scroll.button,
407                                         LIBINPUT_BUTTON_STATE_RELEASED);
408                 }
409         }
410 }
411
412 static void
413 evdev_process_touch_button(struct evdev_device *device,
414                            uint64_t time, int value)
415 {
416         if (device->pending_event != EVDEV_NONE &&
417             device->pending_event != EVDEV_ABSOLUTE_MOTION)
418                 evdev_flush_pending_event(device, time);
419
420         device->pending_event = (value ?
421                                  EVDEV_ABSOLUTE_TOUCH_DOWN :
422                                  EVDEV_ABSOLUTE_TOUCH_UP);
423 }
424
425 static inline void
426 evdev_process_key(struct evdev_device *device,
427                   struct input_event *e, uint64_t time)
428 {
429         enum evdev_key_type type;
430
431         /* ignore kernel key repeat */
432         if (e->value == 2)
433                 return;
434
435         if (e->code == BTN_TOUCH) {
436                 if (!device->is_mt)
437                         evdev_process_touch_button(device, time, e->value);
438                 return;
439         }
440
441         evdev_flush_pending_event(device, time);
442
443         type = get_key_type(e->code);
444
445         /* Ignore key release events from the kernel for keys that libinput
446          * never got a pressed event for. */
447         if (e->value == 0) {
448                 switch (type) {
449                 case EVDEV_KEY_TYPE_NONE:
450                         break;
451                 case EVDEV_KEY_TYPE_KEY:
452                 case EVDEV_KEY_TYPE_BUTTON:
453                         if (!hw_is_key_down(device, e->code))
454                                 return;
455                 }
456         }
457
458         hw_set_key_down(device, e->code, e->value);
459
460         switch (type) {
461         case EVDEV_KEY_TYPE_NONE:
462                 break;
463         case EVDEV_KEY_TYPE_KEY:
464                 evdev_keyboard_notify_key(
465                         device,
466                         time,
467                         e->code,
468                         e->value ? LIBINPUT_KEY_STATE_PRESSED :
469                                    LIBINPUT_KEY_STATE_RELEASED);
470                 break;
471         case EVDEV_KEY_TYPE_BUTTON:
472                 if (device->scroll.method == LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN &&
473                     e->code == device->scroll.button) {
474                         evdev_button_scroll_button(device, time, e->value);
475                         break;
476                 }
477                 evdev_pointer_notify_button(
478                         device,
479                         time,
480                         evdev_to_left_handed(device, e->code),
481                         e->value ? LIBINPUT_BUTTON_STATE_PRESSED :
482                                    LIBINPUT_BUTTON_STATE_RELEASED);
483                 break;
484         }
485 }
486
487 static void
488 evdev_process_touch(struct evdev_device *device,
489                     struct input_event *e,
490                     uint64_t time)
491 {
492         switch (e->code) {
493         case ABS_MT_SLOT:
494                 evdev_flush_pending_event(device, time);
495                 device->mt.slot = e->value;
496                 break;
497         case ABS_MT_TRACKING_ID:
498                 if (device->pending_event != EVDEV_NONE &&
499                     device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
500                         evdev_flush_pending_event(device, time);
501                 if (e->value >= 0)
502                         device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
503                 else
504                         device->pending_event = EVDEV_ABSOLUTE_MT_UP;
505                 break;
506         case ABS_MT_POSITION_X:
507                 device->mt.slots[device->mt.slot].x = e->value;
508                 if (device->pending_event == EVDEV_NONE)
509                         device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
510                 break;
511         case ABS_MT_POSITION_Y:
512                 device->mt.slots[device->mt.slot].y = e->value;
513                 if (device->pending_event == EVDEV_NONE)
514                         device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
515                 break;
516         }
517 }
518
519 static inline void
520 evdev_process_absolute_motion(struct evdev_device *device,
521                               struct input_event *e)
522 {
523         switch (e->code) {
524         case ABS_X:
525                 device->abs.x = e->value;
526                 if (device->pending_event == EVDEV_NONE)
527                         device->pending_event = EVDEV_ABSOLUTE_MOTION;
528                 break;
529         case ABS_Y:
530                 device->abs.y = e->value;
531                 if (device->pending_event == EVDEV_NONE)
532                         device->pending_event = EVDEV_ABSOLUTE_MOTION;
533                 break;
534         }
535 }
536
537 static void
538 evdev_notify_axis(struct evdev_device *device,
539                   uint64_t time,
540                   enum libinput_pointer_axis axis,
541                   double value)
542 {
543         if (device->scroll.natural_scrolling_enabled)
544                 value *= -1;
545
546         pointer_notify_axis(&device->base,
547                             time,
548                             axis,
549                             value);
550 }
551
552 static inline void
553 evdev_process_relative(struct evdev_device *device,
554                        struct input_event *e, uint64_t time)
555 {
556         switch (e->code) {
557         case REL_X:
558                 if (device->pending_event != EVDEV_RELATIVE_MOTION)
559                         evdev_flush_pending_event(device, time);
560                 device->rel.dx += e->value;
561                 device->pending_event = EVDEV_RELATIVE_MOTION;
562                 break;
563         case REL_Y:
564                 if (device->pending_event != EVDEV_RELATIVE_MOTION)
565                         evdev_flush_pending_event(device, time);
566                 device->rel.dy += e->value;
567                 device->pending_event = EVDEV_RELATIVE_MOTION;
568                 break;
569         case REL_WHEEL:
570                 evdev_flush_pending_event(device, time);
571                 evdev_notify_axis(
572                         device,
573                         time,
574                         LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
575                         -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
576                 break;
577         case REL_HWHEEL:
578                 evdev_flush_pending_event(device, time);
579                 evdev_notify_axis(
580                         device,
581                         time,
582                         LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
583                         e->value * DEFAULT_AXIS_STEP_DISTANCE);
584                 break;
585         }
586 }
587
588 static inline void
589 evdev_process_absolute(struct evdev_device *device,
590                        struct input_event *e,
591                        uint64_t time)
592 {
593         if (device->is_mt) {
594                 evdev_process_touch(device, e, time);
595         } else {
596                 evdev_process_absolute_motion(device, e);
597         }
598 }
599
600 static inline bool
601 evdev_any_button_down(struct evdev_device *device)
602 {
603         unsigned int button;
604
605         for (button = BTN_LEFT; button < BTN_JOYSTICK; button++) {
606                 if (libevdev_has_event_code(device->evdev, EV_KEY, button) &&
607                     hw_is_key_down(device, button))
608                         return true;
609         }
610         return false;
611 }
612
613 static inline bool
614 evdev_need_touch_frame(struct evdev_device *device)
615 {
616         if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
617                 return false;
618
619         switch (device->pending_event) {
620         case EVDEV_NONE:
621         case EVDEV_RELATIVE_MOTION:
622                 break;
623         case EVDEV_ABSOLUTE_MT_DOWN:
624         case EVDEV_ABSOLUTE_MT_MOTION:
625         case EVDEV_ABSOLUTE_MT_UP:
626         case EVDEV_ABSOLUTE_TOUCH_DOWN:
627         case EVDEV_ABSOLUTE_TOUCH_UP:
628         case EVDEV_ABSOLUTE_MOTION:
629                 return true;
630         }
631
632         return false;
633 }
634
635 static void
636 evdev_tag_external_mouse(struct evdev_device *device,
637                          struct udev_device *udev_device)
638 {
639         int bustype;
640
641         bustype = libevdev_get_id_bustype(device->evdev);
642         if (bustype == BUS_USB || bustype == BUS_BLUETOOTH) {
643                 if (device->seat_caps & EVDEV_DEVICE_POINTER)
644                         device->tags |= EVDEV_TAG_EXTERNAL_MOUSE;
645         }
646 }
647
648 static void
649 evdev_tag_trackpoint(struct evdev_device *device,
650                      struct udev_device *udev_device)
651 {
652         if (libevdev_has_property(device->evdev, INPUT_PROP_POINTING_STICK))
653                 device->tags |= EVDEV_TAG_TRACKPOINT;
654 }
655
656 static void
657 fallback_process(struct evdev_dispatch *dispatch,
658                  struct evdev_device *device,
659                  struct input_event *event,
660                  uint64_t time)
661 {
662         bool need_frame = false;
663
664         switch (event->type) {
665         case EV_REL:
666                 evdev_process_relative(device, event, time);
667                 break;
668         case EV_ABS:
669                 evdev_process_absolute(device, event, time);
670                 break;
671         case EV_KEY:
672                 evdev_process_key(device, event, time);
673                 break;
674         case EV_SYN:
675                 need_frame = evdev_need_touch_frame(device);
676                 evdev_flush_pending_event(device, time);
677                 if (need_frame)
678                         touch_notify_frame(&device->base, time);
679                 break;
680         }
681 }
682
683 static void
684 fallback_destroy(struct evdev_dispatch *dispatch)
685 {
686         free(dispatch);
687 }
688
689 static void
690 fallback_tag_device(struct evdev_device *device,
691                     struct udev_device *udev_device)
692 {
693         evdev_tag_external_mouse(device, udev_device);
694         evdev_tag_trackpoint(device, udev_device);
695 }
696
697 static int
698 evdev_calibration_has_matrix(struct libinput_device *libinput_device)
699 {
700         struct evdev_device *device = (struct evdev_device*)libinput_device;
701
702         return device->abs.absinfo_x && device->abs.absinfo_y;
703 }
704
705 static enum libinput_config_status
706 evdev_calibration_set_matrix(struct libinput_device *libinput_device,
707                              const float matrix[6])
708 {
709         struct evdev_device *device = (struct evdev_device*)libinput_device;
710
711         evdev_device_calibrate(device, matrix);
712
713         return LIBINPUT_CONFIG_STATUS_SUCCESS;
714 }
715
716 static int
717 evdev_calibration_get_matrix(struct libinput_device *libinput_device,
718                              float matrix[6])
719 {
720         struct evdev_device *device = (struct evdev_device*)libinput_device;
721
722         matrix_to_farray6(&device->abs.usermatrix, matrix);
723
724         return !matrix_is_identity(&device->abs.usermatrix);
725 }
726
727 static int
728 evdev_calibration_get_default_matrix(struct libinput_device *libinput_device,
729                                      float matrix[6])
730 {
731         struct evdev_device *device = (struct evdev_device*)libinput_device;
732
733         matrix_to_farray6(&device->abs.default_calibration, matrix);
734
735         return !matrix_is_identity(&device->abs.default_calibration);
736 }
737
738 struct evdev_dispatch_interface fallback_interface = {
739         fallback_process,
740         fallback_destroy,
741         NULL, /* device_added */
742         NULL, /* device_removed */
743         NULL, /* device_suspended */
744         NULL, /* device_resumed */
745         fallback_tag_device,
746 };
747
748 static uint32_t
749 evdev_sendevents_get_modes(struct libinput_device *device)
750 {
751         return LIBINPUT_CONFIG_SEND_EVENTS_DISABLED;
752 }
753
754 static enum libinput_config_status
755 evdev_sendevents_set_mode(struct libinput_device *device,
756                           enum libinput_config_send_events_mode mode)
757 {
758         struct evdev_device *evdev = (struct evdev_device*)device;
759         struct evdev_dispatch *dispatch = evdev->dispatch;
760
761         if (mode == dispatch->sendevents.current_mode)
762                 return LIBINPUT_CONFIG_STATUS_SUCCESS;
763
764         switch(mode) {
765         case LIBINPUT_CONFIG_SEND_EVENTS_ENABLED:
766                 evdev_device_resume(evdev);
767                 break;
768         case LIBINPUT_CONFIG_SEND_EVENTS_DISABLED:
769                 evdev_device_suspend(evdev);
770                 break;
771         default: /* no support for combined modes yet */
772                 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
773         }
774
775         dispatch->sendevents.current_mode = mode;
776
777         return LIBINPUT_CONFIG_STATUS_SUCCESS;
778 }
779
780 static enum libinput_config_send_events_mode
781 evdev_sendevents_get_mode(struct libinput_device *device)
782 {
783         struct evdev_device *evdev = (struct evdev_device*)device;
784         struct evdev_dispatch *dispatch = evdev->dispatch;
785
786         return dispatch->sendevents.current_mode;
787 }
788
789 static enum libinput_config_send_events_mode
790 evdev_sendevents_get_default_mode(struct libinput_device *device)
791 {
792         return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
793 }
794
795 static int
796 evdev_left_handed_has(struct libinput_device *device)
797 {
798         /* This is only hooked up when we have left-handed configuration, so we
799          * can hardcode 1 here */
800         return 1;
801 }
802
803 static void
804 evdev_change_to_left_handed(struct evdev_device *device)
805 {
806         if (device->buttons.want_left_handed == device->buttons.left_handed)
807                 return;
808
809         if (evdev_any_button_down(device))
810                 return;
811
812         device->buttons.left_handed = device->buttons.want_left_handed;
813 }
814
815 static enum libinput_config_status
816 evdev_left_handed_set(struct libinput_device *device, int left_handed)
817 {
818         struct evdev_device *evdev_device = (struct evdev_device *)device;
819
820         evdev_device->buttons.want_left_handed = left_handed ? true : false;
821
822         evdev_device->buttons.change_to_left_handed(evdev_device);
823
824         return LIBINPUT_CONFIG_STATUS_SUCCESS;
825 }
826
827 static int
828 evdev_left_handed_get(struct libinput_device *device)
829 {
830         struct evdev_device *evdev_device = (struct evdev_device *)device;
831
832         /* return the wanted configuration, even if it hasn't taken
833          * effect yet! */
834         return evdev_device->buttons.want_left_handed;
835 }
836
837 static int
838 evdev_left_handed_get_default(struct libinput_device *device)
839 {
840         return 0;
841 }
842
843 int
844 evdev_init_left_handed(struct evdev_device *device,
845                        void (*change_to_left_handed)(struct evdev_device *))
846 {
847         device->buttons.config_left_handed.has = evdev_left_handed_has;
848         device->buttons.config_left_handed.set = evdev_left_handed_set;
849         device->buttons.config_left_handed.get = evdev_left_handed_get;
850         device->buttons.config_left_handed.get_default = evdev_left_handed_get_default;
851         device->base.config.left_handed = &device->buttons.config_left_handed;
852         device->buttons.left_handed = false;
853         device->buttons.want_left_handed = false;
854         device->buttons.change_to_left_handed = change_to_left_handed;
855
856         return 0;
857 }
858
859 static uint32_t
860 evdev_scroll_get_methods(struct libinput_device *device)
861 {
862         return LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
863 }
864
865 static void
866 evdev_change_scroll_method(struct evdev_device *device)
867 {
868         if (device->scroll.want_method == device->scroll.method &&
869             device->scroll.want_button == device->scroll.button)
870                 return;
871
872         if (evdev_any_button_down(device))
873                 return;
874
875         device->scroll.method = device->scroll.want_method;
876         device->scroll.button = device->scroll.want_button;
877 }
878
879 static enum libinput_config_status
880 evdev_scroll_set_method(struct libinput_device *device,
881                         enum libinput_config_scroll_method method)
882 {
883         struct evdev_device *evdev = (struct evdev_device*)device;
884
885         evdev->scroll.want_method = method;
886         evdev->scroll.change_scroll_method(evdev);
887
888         return LIBINPUT_CONFIG_STATUS_SUCCESS;
889 }
890
891 static enum libinput_config_scroll_method
892 evdev_scroll_get_method(struct libinput_device *device)
893 {
894         struct evdev_device *evdev = (struct evdev_device *)device;
895
896         /* return the wanted configuration, even if it hasn't taken
897          * effect yet! */
898         return evdev->scroll.want_method;
899 }
900
901 static enum libinput_config_scroll_method
902 evdev_scroll_get_default_method(struct libinput_device *device)
903 {
904         struct evdev_device *evdev = (struct evdev_device *)device;
905
906         if (libevdev_has_property(evdev->evdev, INPUT_PROP_POINTING_STICK))
907                 return LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
908         else
909                 return LIBINPUT_CONFIG_SCROLL_NO_SCROLL;
910 }
911
912 static enum libinput_config_status
913 evdev_scroll_set_button(struct libinput_device *device,
914                         uint32_t button)
915 {
916         struct evdev_device *evdev = (struct evdev_device*)device;
917
918         evdev->scroll.want_button = button;
919         evdev->scroll.change_scroll_method(evdev);
920
921         return LIBINPUT_CONFIG_STATUS_SUCCESS;
922 }
923
924 static uint32_t
925 evdev_scroll_get_button(struct libinput_device *device)
926 {
927         struct evdev_device *evdev = (struct evdev_device *)device;
928
929         /* return the wanted configuration, even if it hasn't taken
930          * effect yet! */
931         return evdev->scroll.want_button;
932 }
933
934 static uint32_t
935 evdev_scroll_get_default_button(struct libinput_device *device)
936 {
937         struct evdev_device *evdev = (struct evdev_device *)device;
938
939         if (libevdev_has_property(evdev->evdev, INPUT_PROP_POINTING_STICK))
940                 return BTN_MIDDLE;
941         else
942                 return 0;
943 }
944
945 static int
946 evdev_init_button_scroll(struct evdev_device *device,
947                          void (*change_scroll_method)(struct evdev_device *))
948 {
949         libinput_timer_init(&device->scroll.timer, device->base.seat->libinput,
950                             evdev_button_scroll_timeout, device);
951         device->scroll.config.get_methods = evdev_scroll_get_methods;
952         device->scroll.config.set_method = evdev_scroll_set_method;
953         device->scroll.config.get_method = evdev_scroll_get_method;
954         device->scroll.config.get_default_method = evdev_scroll_get_default_method;
955         device->scroll.config.set_button = evdev_scroll_set_button;
956         device->scroll.config.get_button = evdev_scroll_get_button;
957         device->scroll.config.get_default_button = evdev_scroll_get_default_button;
958         device->base.config.scroll_method = &device->scroll.config;
959         device->scroll.method = evdev_scroll_get_default_method((struct libinput_device *)device);
960         device->scroll.want_method = device->scroll.method;
961         device->scroll.button = evdev_scroll_get_default_button((struct libinput_device *)device);
962         device->scroll.want_button = device->scroll.button;
963         device->scroll.change_scroll_method = change_scroll_method;
964
965         return 0;
966 }
967
968 static void
969 evdev_init_calibration(struct evdev_device *device,
970                        struct evdev_dispatch *dispatch)
971 {
972         device->base.config.calibration = &dispatch->calibration;
973
974         dispatch->calibration.has_matrix = evdev_calibration_has_matrix;
975         dispatch->calibration.set_matrix = evdev_calibration_set_matrix;
976         dispatch->calibration.get_matrix = evdev_calibration_get_matrix;
977         dispatch->calibration.get_default_matrix = evdev_calibration_get_default_matrix;
978 }
979
980 static void
981 evdev_init_sendevents(struct evdev_device *device,
982                       struct evdev_dispatch *dispatch)
983 {
984         device->base.config.sendevents = &dispatch->sendevents.config;
985
986         dispatch->sendevents.current_mode = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
987         dispatch->sendevents.config.get_modes = evdev_sendevents_get_modes;
988         dispatch->sendevents.config.set_mode = evdev_sendevents_set_mode;
989         dispatch->sendevents.config.get_mode = evdev_sendevents_get_mode;
990         dispatch->sendevents.config.get_default_mode = evdev_sendevents_get_default_mode;
991 }
992
993 static int
994 evdev_scroll_config_natural_has(struct libinput_device *device)
995 {
996         return 1;
997 }
998
999 static enum libinput_config_status
1000 evdev_scroll_config_natural_set(struct libinput_device *device,
1001                                 int enabled)
1002 {
1003         struct evdev_device *dev = (struct evdev_device *)device;
1004
1005         dev->scroll.natural_scrolling_enabled = enabled ? true : false;
1006
1007         return LIBINPUT_CONFIG_STATUS_SUCCESS;
1008 }
1009
1010 static int
1011 evdev_scroll_config_natural_get(struct libinput_device *device)
1012 {
1013         struct evdev_device *dev = (struct evdev_device *)device;
1014
1015         return dev->scroll.natural_scrolling_enabled ? 1 : 0;
1016 }
1017
1018 static int
1019 evdev_scroll_config_natural_get_default(struct libinput_device *device)
1020 {
1021         /* could enable this on Apple touchpads. could do that, could
1022          * very well do that... */
1023         return 0;
1024 }
1025
1026 void
1027 evdev_init_natural_scroll(struct evdev_device *device)
1028 {
1029         device->scroll.config_natural.has = evdev_scroll_config_natural_has;
1030         device->scroll.config_natural.set_enabled = evdev_scroll_config_natural_set;
1031         device->scroll.config_natural.get_enabled = evdev_scroll_config_natural_get;
1032         device->scroll.config_natural.get_default_enabled = evdev_scroll_config_natural_get_default;
1033         device->scroll.natural_scrolling_enabled = false;
1034         device->base.config.natural_scroll = &device->scroll.config_natural;
1035 }
1036
1037 static struct evdev_dispatch *
1038 fallback_dispatch_create(struct libinput_device *device)
1039 {
1040         struct evdev_dispatch *dispatch = zalloc(sizeof *dispatch);
1041         struct evdev_device *evdev_device = (struct evdev_device *)device;
1042
1043         if (dispatch == NULL)
1044                 return NULL;
1045
1046         dispatch->interface = &fallback_interface;
1047
1048         if (evdev_device->buttons.want_left_handed &&
1049             evdev_init_left_handed(evdev_device,
1050                                    evdev_change_to_left_handed) == -1) {
1051                 free(dispatch);
1052                 return NULL;
1053         }
1054
1055         if (evdev_device->scroll.want_button &&
1056             evdev_init_button_scroll(evdev_device,
1057                                      evdev_change_scroll_method) == -1) {
1058                 free(dispatch);
1059                 return NULL;
1060         }
1061
1062         if (evdev_device->scroll.natural_scrolling_enabled)
1063                 evdev_init_natural_scroll(evdev_device);
1064
1065         evdev_init_calibration(evdev_device, dispatch);
1066         evdev_init_sendevents(evdev_device, dispatch);
1067
1068         return dispatch;
1069 }
1070
1071 static inline void
1072 evdev_process_event(struct evdev_device *device, struct input_event *e)
1073 {
1074         struct evdev_dispatch *dispatch = device->dispatch;
1075         uint64_t time = e->time.tv_sec * 1000ULL + e->time.tv_usec / 1000;
1076
1077         dispatch->interface->process(dispatch, device, e, time);
1078 }
1079
1080 static inline void
1081 evdev_device_dispatch_one(struct evdev_device *device,
1082                           struct input_event *ev)
1083 {
1084         if (!device->mtdev) {
1085                 evdev_process_event(device, ev);
1086         } else {
1087                 mtdev_put_event(device->mtdev, ev);
1088                 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
1089                         while (!mtdev_empty(device->mtdev)) {
1090                                 struct input_event e;
1091                                 mtdev_get_event(device->mtdev, &e);
1092                                 evdev_process_event(device, &e);
1093                         }
1094                 }
1095         }
1096 }
1097
1098 static int
1099 evdev_sync_device(struct evdev_device *device)
1100 {
1101         struct input_event ev;
1102         int rc;
1103
1104         do {
1105                 rc = libevdev_next_event(device->evdev,
1106                                          LIBEVDEV_READ_FLAG_SYNC, &ev);
1107                 if (rc < 0)
1108                         break;
1109                 evdev_device_dispatch_one(device, &ev);
1110         } while (rc == LIBEVDEV_READ_STATUS_SYNC);
1111
1112         return rc == -EAGAIN ? 0 : rc;
1113 }
1114
1115 static void
1116 evdev_device_dispatch(void *data)
1117 {
1118         struct evdev_device *device = data;
1119         struct libinput *libinput = device->base.seat->libinput;
1120         struct input_event ev;
1121         int rc;
1122
1123         /* If the compositor is repainting, this function is called only once
1124          * per frame and we have to process all the events available on the
1125          * fd, otherwise there will be input lag. */
1126         do {
1127                 rc = libevdev_next_event(device->evdev,
1128                                          LIBEVDEV_READ_FLAG_NORMAL, &ev);
1129                 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
1130                         switch (ratelimit_test(&device->syn_drop_limit)) {
1131                         case RATELIMIT_PASS:
1132                                 log_info(libinput, "SYN_DROPPED event from "
1133                                          "\"%s\" - some input events have "
1134                                          "been lost.\n", device->devname);
1135                                 break;
1136                         case RATELIMIT_THRESHOLD:
1137                                 log_info(libinput, "SYN_DROPPED flood "
1138                                          "from \"%s\"\n",
1139                                          device->devname);
1140                                 break;
1141                         case RATELIMIT_EXCEEDED:
1142                                 break;
1143                         }
1144
1145                         /* send one more sync event so we handle all
1146                            currently pending events before we sync up
1147                            to the current state */
1148                         ev.code = SYN_REPORT;
1149                         evdev_device_dispatch_one(device, &ev);
1150
1151                         rc = evdev_sync_device(device);
1152                         if (rc == 0)
1153                                 rc = LIBEVDEV_READ_STATUS_SUCCESS;
1154                 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
1155                         evdev_device_dispatch_one(device, &ev);
1156                 }
1157         } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
1158
1159         if (rc != -EAGAIN && rc != -EINTR) {
1160                 libinput_remove_source(libinput, device->source);
1161                 device->source = NULL;
1162         }
1163 }
1164
1165 static int
1166 evdev_accel_config_available(struct libinput_device *device)
1167 {
1168         /* this function is only called if we set up ptraccel, so we can
1169            reply with a resounding "Yes" */
1170         return 1;
1171 }
1172
1173 static enum libinput_config_status
1174 evdev_accel_config_set_speed(struct libinput_device *device, double speed)
1175 {
1176         struct evdev_device *dev = (struct evdev_device *)device;
1177
1178         if (!filter_set_speed(dev->pointer.filter, speed))
1179                 return LIBINPUT_CONFIG_STATUS_INVALID;
1180
1181         return LIBINPUT_CONFIG_STATUS_SUCCESS;
1182 }
1183
1184 static double
1185 evdev_accel_config_get_speed(struct libinput_device *device)
1186 {
1187         struct evdev_device *dev = (struct evdev_device *)device;
1188
1189         return filter_get_speed(dev->pointer.filter);
1190 }
1191
1192 static double
1193 evdev_accel_config_get_default_speed(struct libinput_device *device)
1194 {
1195         return 0.0;
1196 }
1197
1198 int
1199 evdev_device_init_pointer_acceleration(struct evdev_device *device)
1200 {
1201         device->pointer.filter =
1202                 create_pointer_accelerator_filter(
1203                         pointer_accel_profile_linear);
1204         if (!device->pointer.filter)
1205                 return -1;
1206
1207         device->pointer.config.available = evdev_accel_config_available;
1208         device->pointer.config.set_speed = evdev_accel_config_set_speed;
1209         device->pointer.config.get_speed = evdev_accel_config_get_speed;
1210         device->pointer.config.get_default_speed = evdev_accel_config_get_default_speed;
1211         device->base.config.accel = &device->pointer.config;
1212
1213         return 0;
1214 }
1215
1216
1217 static inline int
1218 evdev_need_mtdev(struct evdev_device *device)
1219 {
1220         struct libevdev *evdev = device->evdev;
1221
1222         return (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
1223                 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y) &&
1224                 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT));
1225 }
1226
1227 static void
1228 evdev_tag_device(struct evdev_device *device)
1229 {
1230         if (device->dispatch->interface->tag_device)
1231                 device->dispatch->interface->tag_device(device,
1232                                                         device->udev_device);
1233 }
1234
1235 static inline int
1236 evdev_read_dpi_prop(struct evdev_device *device)
1237 {
1238         struct libinput *libinput = device->base.seat->libinput;
1239         const char *mouse_dpi;
1240         int dpi = DEFAULT_MOUSE_DPI;
1241
1242         mouse_dpi = udev_device_get_property_value(device->udev_device,
1243                                                    "MOUSE_DPI");
1244         if (mouse_dpi) {
1245                 dpi = parse_mouse_dpi_property(mouse_dpi);
1246                 if (!dpi) {
1247                         log_error(libinput, "Mouse DPI property for '%s' is "
1248                                             "present but invalid, using %d "
1249                                             "DPI instead\n",
1250                                             device->devname,
1251                                             DEFAULT_MOUSE_DPI);
1252                         dpi = DEFAULT_MOUSE_DPI;
1253                 }
1254         }
1255
1256         return dpi;
1257 }
1258
1259 static inline int
1260 evdev_fix_abs_resolution(struct libevdev *evdev,
1261                          unsigned int code,
1262                          const struct input_absinfo *absinfo)
1263 {
1264         struct input_absinfo fixed;
1265
1266         if (absinfo->resolution == 0) {
1267                 fixed = *absinfo;
1268                 fixed.resolution = 1;
1269                 /* libevdev_set_abs_info() changes the absinfo we already
1270                    have a pointer to, no need to fetch it again */
1271                 libevdev_set_abs_info(evdev, code, &fixed);
1272                 return 1;
1273         } else {
1274                 return 0;
1275         }
1276 }
1277
1278 static int
1279 evdev_configure_device(struct evdev_device *device)
1280 {
1281         struct libinput *libinput = device->base.seat->libinput;
1282         struct libevdev *evdev = device->evdev;
1283         const struct input_absinfo *absinfo;
1284         int has_abs, has_rel, has_mt;
1285         int has_button, has_keyboard, has_touch;
1286         struct mt_slot *slots;
1287         int num_slots;
1288         int active_slot;
1289         int slot;
1290         unsigned int i;
1291         const char *devnode = udev_device_get_devnode(device->udev_device);
1292
1293         has_rel = 0;
1294         has_abs = 0;
1295         has_mt = 0;
1296         has_button = 0;
1297         has_keyboard = 0;
1298         has_touch = 0;
1299
1300         for (i = BTN_JOYSTICK; i < BTN_DIGI; i++) {
1301                 if (libevdev_has_event_code(evdev, EV_KEY, i)) {
1302                         log_info(libinput,
1303                                  "input device '%s', %s is a joystick, ignoring\n",
1304                                  device->devname, devnode);
1305                         return -1;
1306                 }
1307         }
1308
1309         if (libevdev_has_event_type(evdev, EV_ABS)) {
1310
1311                 if ((absinfo = libevdev_get_abs_info(evdev, ABS_X))) {
1312                         if (evdev_fix_abs_resolution(evdev,
1313                                                      ABS_X,
1314                                                      absinfo))
1315                                 device->abs.fake_resolution = 1;
1316                         device->abs.absinfo_x = absinfo;
1317                         has_abs = 1;
1318                 }
1319                 if ((absinfo = libevdev_get_abs_info(evdev, ABS_Y))) {
1320                         if (evdev_fix_abs_resolution(evdev,
1321                                                      ABS_Y,
1322                                                      absinfo))
1323                                 device->abs.fake_resolution = 1;
1324                         device->abs.absinfo_y = absinfo;
1325                         has_abs = 1;
1326                 }
1327
1328                 /* Fake MT devices have the ABS_MT_SLOT bit set because of
1329                    the limited ABS_* range - they aren't MT devices, they
1330                    just have too many ABS_ axes */
1331                 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT) &&
1332                     libevdev_get_num_slots(evdev) == -1) {
1333                         has_mt = 0;
1334                         has_touch = 0;
1335                 } else if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
1336                            libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y)) {
1337                         absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
1338                         if (evdev_fix_abs_resolution(evdev,
1339                                                      ABS_MT_POSITION_X,
1340                                                      absinfo))
1341                                 device->abs.fake_resolution = 1;
1342                         device->abs.absinfo_x = absinfo;
1343
1344                         absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
1345                         if (evdev_fix_abs_resolution(evdev,
1346                                                      ABS_MT_POSITION_Y,
1347                                                      absinfo))
1348                                 device->abs.fake_resolution = 1;
1349                         device->abs.absinfo_y = absinfo;
1350                         device->is_mt = 1;
1351                         has_touch = 1;
1352                         has_mt = 1;
1353
1354                         /* We only handle the slotted Protocol B in libinput.
1355                            Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
1356                            require mtdev for conversion. */
1357                         if (evdev_need_mtdev(device)) {
1358                                 device->mtdev = mtdev_new_open(device->fd);
1359                                 if (!device->mtdev)
1360                                         return -1;
1361
1362                                 num_slots = device->mtdev->caps.slot.maximum;
1363                                 if (device->mtdev->caps.slot.minimum < 0 ||
1364                                     num_slots <= 0)
1365                                         return -1;
1366                                 active_slot = device->mtdev->caps.slot.value;
1367                         } else {
1368                                 num_slots = libevdev_get_num_slots(device->evdev);
1369                                 active_slot = libevdev_get_current_slot(evdev);
1370                         }
1371
1372                         slots = calloc(num_slots, sizeof(struct mt_slot));
1373                         if (!slots)
1374                                 return -1;
1375
1376                         for (slot = 0; slot < num_slots; ++slot) {
1377                                 slots[slot].seat_slot = -1;
1378                                 slots[slot].x = 0;
1379                                 slots[slot].y = 0;
1380                         }
1381                         device->mt.slots = slots;
1382                         device->mt.slots_len = num_slots;
1383                         device->mt.slot = active_slot;
1384                 }
1385         }
1386
1387         if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
1388             libevdev_has_event_code(evdev, EV_REL, REL_Y))
1389                 has_rel = 1;
1390
1391         if (libevdev_has_event_type(evdev, EV_KEY)) {
1392                 if (!libevdev_has_property(evdev, INPUT_PROP_DIRECT) &&
1393                     libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_FINGER) &&
1394                     !libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_PEN) &&
1395                     (has_abs || has_mt)) {
1396                         device->dispatch = evdev_mt_touchpad_create(device);
1397                         log_info(libinput,
1398                                  "input device '%s', %s is a touchpad\n",
1399                                  device->devname, devnode);
1400                         return device->dispatch == NULL ? -1 : 0;
1401                 }
1402
1403                 for (i = 0; i < KEY_MAX; i++) {
1404                         if (libevdev_has_event_code(evdev, EV_KEY, i)) {
1405                                 switch (get_key_type(i)) {
1406                                 case EVDEV_KEY_TYPE_NONE:
1407                                         break;
1408                                 case EVDEV_KEY_TYPE_KEY:
1409                                         has_keyboard = 1;
1410                                         break;
1411                                 case EVDEV_KEY_TYPE_BUTTON:
1412                                         has_button = 1;
1413                                         break;
1414                                 }
1415                         }
1416                 }
1417
1418                 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH))
1419                         has_touch = 1;
1420         }
1421         if (libevdev_has_event_type(evdev, EV_LED))
1422                 has_keyboard = 1;
1423
1424         if ((has_abs || has_rel) && has_button) {
1425                 if (evdev_device_init_pointer_acceleration(device) == -1)
1426                         return -1;
1427
1428                 device->seat_caps |= EVDEV_DEVICE_POINTER;
1429
1430                 log_info(libinput,
1431                          "input device '%s', %s is a pointer caps =%s%s%s\n",
1432                          device->devname, devnode,
1433                          has_abs ? " absolute-motion" : "",
1434                          has_rel ? " relative-motion": "",
1435                          has_button ? " button" : "");
1436
1437                 /* want left-handed config option */
1438                 device->buttons.want_left_handed = true;
1439                 /* want natural-scroll config option */
1440                 device->scroll.natural_scrolling_enabled = true;
1441         }
1442
1443         if (has_rel && has_button) {
1444                 /* want button scrolling config option */
1445                 device->scroll.want_button = 1;
1446         }
1447
1448         if (has_keyboard) {
1449                 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
1450                 log_info(libinput,
1451                          "input device '%s', %s is a keyboard\n",
1452                          device->devname, devnode);
1453         }
1454         if (has_touch && !has_button) {
1455                 device->seat_caps |= EVDEV_DEVICE_TOUCH;
1456                 log_info(libinput,
1457                          "input device '%s', %s is a touch device\n",
1458                          device->devname, devnode);
1459         }
1460
1461         return 0;
1462 }
1463
1464 static void
1465 evdev_notify_added_device(struct evdev_device *device)
1466 {
1467         struct libinput_device *dev;
1468
1469         list_for_each(dev, &device->base.seat->devices_list, link) {
1470                 struct evdev_device *d = (struct evdev_device*)dev;
1471                 if (dev == &device->base)
1472                         continue;
1473
1474                 /* Notify existing device d about addition of device device */
1475                 if (d->dispatch->interface->device_added)
1476                         d->dispatch->interface->device_added(d, device);
1477
1478                 /* Notify new device device about existing device d */
1479                 if (device->dispatch->interface->device_added)
1480                         device->dispatch->interface->device_added(device, d);
1481
1482                 /* Notify new device device if existing device d is suspended */
1483                 if (d->suspended && device->dispatch->interface->device_suspended)
1484                         device->dispatch->interface->device_suspended(device, d);
1485         }
1486
1487         notify_added_device(&device->base);
1488 }
1489
1490 static int
1491 evdev_device_compare_syspath(struct udev_device *udev_device, int fd)
1492 {
1493         struct udev *udev = udev_device_get_udev(udev_device);
1494         struct udev_device *udev_device_new = NULL;
1495         struct stat st;
1496         int rc = 1;
1497
1498         if (fstat(fd, &st) < 0)
1499                 goto out;
1500
1501         udev_device_new = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
1502         if (!udev_device_new)
1503                 goto out;
1504
1505         rc = strcmp(udev_device_get_syspath(udev_device_new),
1506                     udev_device_get_syspath(udev_device));
1507 out:
1508         if (udev_device_new)
1509                 udev_device_unref(udev_device_new);
1510         return rc;
1511 }
1512
1513 struct evdev_device *
1514 evdev_device_create(struct libinput_seat *seat,
1515                     struct udev_device *udev_device)
1516 {
1517         struct libinput *libinput = seat->libinput;
1518         struct evdev_device *device = NULL;
1519         int rc;
1520         int fd;
1521         int unhandled_device = 0;
1522         const char *devnode = udev_device_get_devnode(udev_device);
1523
1524         /* Use non-blocking mode so that we can loop on read on
1525          * evdev_device_data() until all events on the fd are
1526          * read.  mtdev_get() also expects this. */
1527         fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
1528         if (fd < 0) {
1529                 log_info(libinput,
1530                          "opening input device '%s' failed (%s).\n",
1531                          devnode, strerror(-fd));
1532                 return NULL;
1533         }
1534
1535         if (evdev_device_compare_syspath(udev_device, fd) != 0)
1536                 goto err;
1537
1538         device = zalloc(sizeof *device);
1539         if (device == NULL)
1540                 goto err;
1541
1542         libinput_device_init(&device->base, seat);
1543         libinput_seat_ref(seat);
1544
1545         rc = libevdev_new_from_fd(fd, &device->evdev);
1546         if (rc != 0)
1547                 goto err;
1548
1549         libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
1550
1551         device->seat_caps = 0;
1552         device->is_mt = 0;
1553         device->mtdev = NULL;
1554         device->udev_device = udev_device_ref(udev_device);
1555         device->rel.dx = 0;
1556         device->rel.dy = 0;
1557         device->abs.seat_slot = -1;
1558         device->dispatch = NULL;
1559         device->fd = fd;
1560         device->pending_event = EVDEV_NONE;
1561         device->devname = libevdev_get_name(device->evdev);
1562         device->scroll.threshold = 5.0; /* Default may be overridden */
1563         device->scroll.direction = 0;
1564         device->dpi = evdev_read_dpi_prop(device);
1565         /* at most 5 SYN_DROPPED log-messages per 30s */
1566         ratelimit_init(&device->syn_drop_limit, 30ULL * 1000, 5);
1567
1568         matrix_init_identity(&device->abs.calibration);
1569         matrix_init_identity(&device->abs.usermatrix);
1570         matrix_init_identity(&device->abs.default_calibration);
1571
1572         if (evdev_configure_device(device) == -1)
1573                 goto err;
1574
1575         if (device->seat_caps == 0) {
1576                 unhandled_device = 1;
1577                 goto err;
1578         }
1579
1580         /* If the dispatch was not set up use the fallback. */
1581         if (device->dispatch == NULL)
1582                 device->dispatch = fallback_dispatch_create(&device->base);
1583         if (device->dispatch == NULL)
1584                 goto err;
1585
1586         device->source =
1587                 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1588         if (!device->source)
1589                 goto err;
1590
1591         list_insert(seat->devices_list.prev, &device->base.link);
1592
1593         evdev_tag_device(device);
1594         evdev_notify_added_device(device);
1595
1596         return device;
1597
1598 err:
1599         if (fd >= 0)
1600                 close_restricted(libinput, fd);
1601         if (device)
1602                 evdev_device_destroy(device);
1603
1604         return unhandled_device ? EVDEV_UNHANDLED_DEVICE :  NULL;
1605 }
1606
1607 int
1608 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
1609 {
1610         memset(keys, 0, size);
1611         return 0;
1612 }
1613
1614 const char *
1615 evdev_device_get_output(struct evdev_device *device)
1616 {
1617         return device->output_name;
1618 }
1619
1620 const char *
1621 evdev_device_get_sysname(struct evdev_device *device)
1622 {
1623         return udev_device_get_sysname(device->udev_device);
1624 }
1625
1626 const char *
1627 evdev_device_get_name(struct evdev_device *device)
1628 {
1629         return device->devname;
1630 }
1631
1632 unsigned int
1633 evdev_device_get_id_product(struct evdev_device *device)
1634 {
1635         return libevdev_get_id_product(device->evdev);
1636 }
1637
1638 unsigned int
1639 evdev_device_get_id_vendor(struct evdev_device *device)
1640 {
1641         return libevdev_get_id_vendor(device->evdev);
1642 }
1643
1644 struct udev_device *
1645 evdev_device_get_udev_device(struct evdev_device *device)
1646 {
1647         return udev_device_ref(device->udev_device);
1648 }
1649
1650 void
1651 evdev_device_set_default_calibration(struct evdev_device *device,
1652                                      const float calibration[6])
1653 {
1654         matrix_from_farray6(&device->abs.default_calibration, calibration);
1655         evdev_device_calibrate(device, calibration);
1656 }
1657
1658 void
1659 evdev_device_calibrate(struct evdev_device *device,
1660                        const float calibration[6])
1661 {
1662         struct matrix scale,
1663                       translate,
1664                       transform;
1665         double sx, sy;
1666
1667         matrix_from_farray6(&transform, calibration);
1668         device->abs.apply_calibration = !matrix_is_identity(&transform);
1669
1670         if (!device->abs.apply_calibration) {
1671                 matrix_init_identity(&device->abs.calibration);
1672                 return;
1673         }
1674
1675         sx = device->abs.absinfo_x->maximum - device->abs.absinfo_x->minimum + 1;
1676         sy = device->abs.absinfo_y->maximum - device->abs.absinfo_y->minimum + 1;
1677
1678         /* The transformation matrix is in the form:
1679          *  [ a b c ]
1680          *  [ d e f ]
1681          *  [ 0 0 1 ]
1682          * Where a, e are the scale components, a, b, d, e are the rotation
1683          * component (combined with scale) and c and f are the translation
1684          * component. The translation component in the input matrix must be
1685          * normalized to multiples of the device width and height,
1686          * respectively. e.g. c == 1 shifts one device-width to the right.
1687          *
1688          * We pre-calculate a single matrix to apply to event coordinates:
1689          *     M = Un-Normalize * Calibration * Normalize
1690          *
1691          * Normalize: scales the device coordinates to [0,1]
1692          * Calibration: user-supplied matrix
1693          * Un-Normalize: scales back up to device coordinates
1694          * Matrix maths requires the normalize/un-normalize in reverse
1695          * order.
1696          */
1697
1698         /* back up the user matrix so we can return it on request */
1699         matrix_from_farray6(&device->abs.usermatrix, calibration);
1700
1701         /* Un-Normalize */
1702         matrix_init_translate(&translate,
1703                               device->abs.absinfo_x->minimum,
1704                               device->abs.absinfo_y->minimum);
1705         matrix_init_scale(&scale, sx, sy);
1706         matrix_mult(&scale, &translate, &scale);
1707
1708         /* Calibration */
1709         matrix_mult(&transform, &scale, &transform);
1710
1711         /* Normalize */
1712         matrix_init_translate(&translate,
1713                               -device->abs.absinfo_x->minimum/sx,
1714                               -device->abs.absinfo_y->minimum/sy);
1715         matrix_init_scale(&scale, 1.0/sx, 1.0/sy);
1716         matrix_mult(&scale, &translate, &scale);
1717
1718         /* store final matrix in device */
1719         matrix_mult(&device->abs.calibration, &transform, &scale);
1720 }
1721
1722 int
1723 evdev_device_has_capability(struct evdev_device *device,
1724                             enum libinput_device_capability capability)
1725 {
1726         switch (capability) {
1727         case LIBINPUT_DEVICE_CAP_POINTER:
1728                 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
1729         case LIBINPUT_DEVICE_CAP_KEYBOARD:
1730                 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
1731         case LIBINPUT_DEVICE_CAP_TOUCH:
1732                 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
1733         default:
1734                 return 0;
1735         }
1736 }
1737
1738 int
1739 evdev_device_get_size(struct evdev_device *device,
1740                       double *width,
1741                       double *height)
1742 {
1743         const struct input_absinfo *x, *y;
1744
1745         x = libevdev_get_abs_info(device->evdev, ABS_X);
1746         y = libevdev_get_abs_info(device->evdev, ABS_Y);
1747
1748         if (!x || !y || device->abs.fake_resolution ||
1749             !x->resolution || !y->resolution)
1750                 return -1;
1751
1752         *width = evdev_convert_to_mm(x, x->maximum);
1753         *height = evdev_convert_to_mm(y, y->maximum);
1754
1755         return 0;
1756 }
1757
1758 int
1759 evdev_device_has_button(struct evdev_device *device, uint32_t code)
1760 {
1761         if (!(device->seat_caps & EVDEV_DEVICE_POINTER))
1762                 return -1;
1763
1764         return libevdev_has_event_code(device->evdev, EV_KEY, code);
1765 }
1766
1767 static inline bool
1768 evdev_is_scrolling(const struct evdev_device *device,
1769                    enum libinput_pointer_axis axis)
1770 {
1771         assert(axis == LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL ||
1772                axis == LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1773
1774         return (device->scroll.direction & (1 << axis)) != 0;
1775 }
1776
1777 static inline void
1778 evdev_start_scrolling(struct evdev_device *device,
1779                       enum libinput_pointer_axis axis)
1780 {
1781         assert(axis == LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL ||
1782                axis == LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1783
1784         device->scroll.direction |= (1 << axis);
1785 }
1786
1787 void
1788 evdev_post_scroll(struct evdev_device *device,
1789                   uint64_t time,
1790                   double dx,
1791                   double dy)
1792 {
1793         double trigger_horiz, trigger_vert;
1794
1795         if (!evdev_is_scrolling(device,
1796                                 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
1797                 device->scroll.buildup_vertical += dy;
1798         if (!evdev_is_scrolling(device,
1799                                 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
1800                 device->scroll.buildup_horizontal += dx;
1801
1802         trigger_vert = device->scroll.buildup_vertical;
1803         trigger_horiz = device->scroll.buildup_horizontal;
1804
1805         /* If we're not scrolling yet, use a distance trigger: moving
1806            past a certain distance starts scrolling */
1807         if (!evdev_is_scrolling(device,
1808                                 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL) &&
1809             !evdev_is_scrolling(device,
1810                                 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
1811                 if (fabs(trigger_vert) >= device->scroll.threshold)
1812                         evdev_start_scrolling(device,
1813                                               LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1814                 if (fabs(trigger_horiz) >= device->scroll.threshold)
1815                         evdev_start_scrolling(device,
1816                                               LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
1817         /* We're already scrolling in one direction. Require some
1818            trigger speed to start scrolling in the other direction */
1819         } else if (!evdev_is_scrolling(device,
1820                                LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
1821                 if (fabs(dy) >= device->scroll.threshold)
1822                         evdev_start_scrolling(device,
1823                                       LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1824         } else if (!evdev_is_scrolling(device,
1825                                 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL)) {
1826                 if (fabs(dx) >= device->scroll.threshold)
1827                         evdev_start_scrolling(device,
1828                                       LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
1829         }
1830
1831         /* We use the trigger to enable, but the delta from this event for
1832          * the actual scroll movement. Otherwise we get a jump once
1833          * scrolling engages */
1834         if (dy != 0.0 &&
1835             evdev_is_scrolling(device,
1836                                LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
1837                 evdev_notify_axis(device,
1838                                   time,
1839                                   LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
1840                                   dy);
1841         }
1842
1843         if (dx != 0.0 &&
1844             evdev_is_scrolling(device,
1845                                LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL)) {
1846                 evdev_notify_axis(device,
1847                                   time,
1848                                   LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
1849                                   dx);
1850         }
1851 }
1852
1853 void
1854 evdev_stop_scroll(struct evdev_device *device, uint64_t time)
1855 {
1856         /* terminate scrolling with a zero scroll event */
1857         if (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
1858                 pointer_notify_axis(&device->base,
1859                                     time,
1860                                     LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
1861                                     0);
1862         if (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
1863                 pointer_notify_axis(&device->base,
1864                                     time,
1865                                     LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
1866                                     0);
1867
1868         device->scroll.buildup_horizontal = 0;
1869         device->scroll.buildup_vertical = 0;
1870         device->scroll.direction = 0;
1871 }
1872
1873 static void
1874 release_pressed_keys(struct evdev_device *device)
1875 {
1876         struct libinput *libinput = device->base.seat->libinput;
1877         uint64_t time;
1878         int code;
1879
1880         if ((time = libinput_now(libinput)) == 0)
1881                 return;
1882
1883         for (code = 0; code < KEY_CNT; code++) {
1884                 int count = get_key_down_count(device, code);
1885
1886                 if (count > 1) {
1887                         log_bug_libinput(libinput,
1888                                          "Key %d is down %d times.\n",
1889                                          code,
1890                                          count);
1891                 }
1892
1893                 while (get_key_down_count(device, code) > 0) {
1894                         switch (get_key_type(code)) {
1895                         case EVDEV_KEY_TYPE_NONE:
1896                                 break;
1897                         case EVDEV_KEY_TYPE_KEY:
1898                                 evdev_keyboard_notify_key(
1899                                         device,
1900                                         time,
1901                                         code,
1902                                         LIBINPUT_KEY_STATE_RELEASED);
1903                                 break;
1904                         case EVDEV_KEY_TYPE_BUTTON:
1905                                 evdev_pointer_notify_button(
1906                                         device,
1907                                         time,
1908                                         evdev_to_left_handed(device, code),
1909                                         LIBINPUT_BUTTON_STATE_RELEASED);
1910                                 break;
1911                         }
1912                 }
1913         }
1914 }
1915
1916 void
1917 evdev_notify_suspended_device(struct evdev_device *device)
1918 {
1919         struct libinput_device *it;
1920
1921         if (device->suspended)
1922                 return;
1923
1924         list_for_each(it, &device->base.seat->devices_list, link) {
1925                 struct evdev_device *d = (struct evdev_device*)it;
1926                 if (it == &device->base)
1927                         continue;
1928
1929                 if (d->dispatch->interface->device_suspended)
1930                         d->dispatch->interface->device_suspended(d, device);
1931         }
1932
1933         device->suspended = 1;
1934 }
1935
1936 void
1937 evdev_notify_resumed_device(struct evdev_device *device)
1938 {
1939         struct libinput_device *it;
1940
1941         if (!device->suspended)
1942                 return;
1943
1944         list_for_each(it, &device->base.seat->devices_list, link) {
1945                 struct evdev_device *d = (struct evdev_device*)it;
1946                 if (it == &device->base)
1947                         continue;
1948
1949                 if (d->dispatch->interface->device_resumed)
1950                         d->dispatch->interface->device_resumed(d, device);
1951         }
1952
1953         device->suspended = 0;
1954 }
1955
1956 int
1957 evdev_device_suspend(struct evdev_device *device)
1958 {
1959         evdev_notify_suspended_device(device);
1960
1961         if (device->source) {
1962                 libinput_remove_source(device->base.seat->libinput,
1963                                        device->source);
1964                 device->source = NULL;
1965         }
1966
1967         release_pressed_keys(device);
1968
1969         if (device->mtdev) {
1970                 mtdev_close_delete(device->mtdev);
1971                 device->mtdev = NULL;
1972         }
1973
1974         if (device->fd != -1) {
1975                 close_restricted(device->base.seat->libinput, device->fd);
1976                 device->fd = -1;
1977         }
1978
1979         return 0;
1980 }
1981
1982 int
1983 evdev_device_resume(struct evdev_device *device)
1984 {
1985         struct libinput *libinput = device->base.seat->libinput;
1986         int fd;
1987         const char *devnode;
1988
1989         if (device->fd != -1)
1990                 return 0;
1991
1992         if (device->was_removed)
1993                 return -ENODEV;
1994
1995         devnode = udev_device_get_devnode(device->udev_device);
1996         fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
1997
1998         if (fd < 0)
1999                 return -errno;
2000
2001         if (evdev_device_compare_syspath(device->udev_device, fd)) {
2002                 close_restricted(libinput, fd);
2003                 return -ENODEV;
2004         }
2005
2006         device->fd = fd;
2007
2008         if (evdev_need_mtdev(device)) {
2009                 device->mtdev = mtdev_new_open(device->fd);
2010                 if (!device->mtdev)
2011                         return -ENODEV;
2012         }
2013
2014         device->source =
2015                 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
2016         if (!device->source) {
2017                 mtdev_close_delete(device->mtdev);
2018                 return -ENOMEM;
2019         }
2020
2021         memset(device->hw_key_mask, 0, sizeof(device->hw_key_mask));
2022
2023         evdev_notify_resumed_device(device);
2024
2025         return 0;
2026 }
2027
2028 void
2029 evdev_device_remove(struct evdev_device *device)
2030 {
2031         struct libinput_device *dev;
2032
2033         list_for_each(dev, &device->base.seat->devices_list, link) {
2034                 struct evdev_device *d = (struct evdev_device*)dev;;
2035                 if (dev == &device->base)
2036                         continue;
2037
2038                 if (d->dispatch->interface->device_removed)
2039                         d->dispatch->interface->device_removed(d, device);
2040         }
2041
2042         evdev_device_suspend(device);
2043
2044         /* A device may be removed while suspended, mark it to
2045          * skip re-opening a different device with the same node */
2046         device->was_removed = true;
2047
2048         list_remove(&device->base.link);
2049
2050         notify_removed_device(&device->base);
2051         libinput_device_unref(&device->base);
2052 }
2053
2054 void
2055 evdev_device_destroy(struct evdev_device *device)
2056 {
2057         struct evdev_dispatch *dispatch;
2058
2059         dispatch = device->dispatch;
2060         if (dispatch)
2061                 dispatch->interface->destroy(dispatch);
2062
2063         filter_destroy(device->pointer.filter);
2064         libinput_seat_unref(device->base.seat);
2065         libevdev_free(device->evdev);
2066         udev_device_unref(device->udev_device);
2067         free(device->mt.slots);
2068         free(device);
2069 }