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