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