evdev: factor out setting up the MT slots/mtdev
[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                                        accel_profile_func_t profile)
1271 {
1272         device->pointer.filter = create_pointer_accelerator_filter(profile);
1273         if (!device->pointer.filter)
1274                 return -1;
1275
1276         device->pointer.config.available = evdev_accel_config_available;
1277         device->pointer.config.set_speed = evdev_accel_config_set_speed;
1278         device->pointer.config.get_speed = evdev_accel_config_get_speed;
1279         device->pointer.config.get_default_speed = evdev_accel_config_get_default_speed;
1280         device->base.config.accel = &device->pointer.config;
1281
1282         evdev_accel_config_set_speed(&device->base,
1283                      evdev_accel_config_get_default_speed(&device->base));
1284
1285         return 0;
1286 }
1287
1288 static inline int
1289 evdev_need_mtdev(struct evdev_device *device)
1290 {
1291         struct libevdev *evdev = device->evdev;
1292
1293         return (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
1294                 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y) &&
1295                 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT));
1296 }
1297
1298 static void
1299 evdev_tag_device(struct evdev_device *device)
1300 {
1301         if (device->dispatch->interface->tag_device)
1302                 device->dispatch->interface->tag_device(device,
1303                                                         device->udev_device);
1304 }
1305
1306 static inline int
1307 evdev_read_wheel_click_prop(struct evdev_device *device)
1308 {
1309         struct libinput *libinput = device->base.seat->libinput;
1310         const char *prop;
1311         int angle = DEFAULT_WHEEL_CLICK_ANGLE;
1312
1313         prop = udev_device_get_property_value(device->udev_device,
1314                                               "MOUSE_WHEEL_CLICK_ANGLE");
1315         if (prop) {
1316                 angle = parse_mouse_wheel_click_angle_property(prop);
1317                 if (!angle) {
1318                         log_error(libinput,
1319                                   "Mouse wheel click angle '%s' is present but invalid,"
1320                                   "using %d degrees instead\n",
1321                                   device->devname,
1322                                   DEFAULT_WHEEL_CLICK_ANGLE);
1323                         angle = DEFAULT_WHEEL_CLICK_ANGLE;
1324                 }
1325         }
1326
1327         return angle;
1328 }
1329 static inline int
1330 evdev_read_dpi_prop(struct evdev_device *device)
1331 {
1332         struct libinput *libinput = device->base.seat->libinput;
1333         const char *mouse_dpi;
1334         int dpi = DEFAULT_MOUSE_DPI;
1335
1336         mouse_dpi = udev_device_get_property_value(device->udev_device,
1337                                                    "MOUSE_DPI");
1338         if (mouse_dpi) {
1339                 dpi = parse_mouse_dpi_property(mouse_dpi);
1340                 if (!dpi) {
1341                         log_error(libinput, "Mouse DPI property for '%s' is "
1342                                             "present but invalid, using %d "
1343                                             "DPI instead\n",
1344                                             device->devname,
1345                                             DEFAULT_MOUSE_DPI);
1346                         dpi = DEFAULT_MOUSE_DPI;
1347                 }
1348         }
1349
1350         return dpi;
1351 }
1352
1353 /* Return 1 if the given resolutions have been set, or 0 otherwise */
1354 inline int
1355 evdev_fix_abs_resolution(struct evdev_device *device,
1356                          unsigned int xcode,
1357                          unsigned int ycode,
1358                          int xresolution,
1359                          int yresolution)
1360 {
1361         struct libinput *libinput = device->base.seat->libinput;
1362         struct libevdev *evdev = device->evdev;
1363         const struct input_absinfo *absx, *absy;
1364         struct input_absinfo fixed;
1365         int rc = 0;
1366
1367         if (!(xcode == ABS_X && ycode == ABS_Y)  &&
1368             !(xcode == ABS_MT_POSITION_X && ycode == ABS_MT_POSITION_Y)) {
1369                 log_bug_libinput(libinput,
1370                                  "Invalid x/y code combination %d/%d\n",
1371                                  xcode, ycode);
1372                 return 0;
1373         }
1374
1375         if (xresolution == 0 || yresolution == 0 ||
1376             (xresolution == EVDEV_FAKE_RESOLUTION && xresolution != yresolution) ||
1377             (yresolution == EVDEV_FAKE_RESOLUTION && xresolution != yresolution)) {
1378                 log_bug_libinput(libinput,
1379                                  "Invalid x/y resolutions %d/%d\n",
1380                                  xresolution, yresolution);
1381                 return 0;
1382         }
1383
1384         absx = libevdev_get_abs_info(evdev, xcode);
1385         absy = libevdev_get_abs_info(evdev, ycode);
1386
1387         if ((absx->resolution == 0 && absy->resolution != 0) ||
1388             (absx->resolution != 0 && absy->resolution == 0)) {
1389                 log_bug_kernel(libinput,
1390                                "Kernel has only x or y resolution, not both.\n");
1391                 return 0;
1392         }
1393
1394         if (absx->resolution == 0 || absx->resolution == EVDEV_FAKE_RESOLUTION) {
1395                 fixed = *absx;
1396                 fixed.resolution = xresolution;
1397                 /* libevdev_set_abs_info() changes the absinfo we already
1398                    have a pointer to, no need to fetch it again */
1399                 libevdev_set_abs_info(evdev, xcode, &fixed);
1400                 rc = 1;
1401         }
1402
1403         if (absy->resolution == 0 || absy->resolution == EVDEV_FAKE_RESOLUTION) {
1404                 fixed = *absy;
1405                 fixed.resolution = yresolution;
1406                 /* libevdev_set_abs_info() changes the absinfo we already
1407                    have a pointer to, no need to fetch it again */
1408                 libevdev_set_abs_info(evdev, ycode, &fixed);
1409                 rc = 1;
1410         }
1411
1412         return rc;
1413 }
1414
1415 static enum evdev_device_udev_tags
1416 evdev_device_get_udev_tags(struct evdev_device *device,
1417                            struct udev_device *udev_device)
1418 {
1419         const char *prop;
1420         enum evdev_device_udev_tags tags = 0;
1421         const struct evdev_udev_tag_match *match;
1422         int i;
1423
1424         for (i = 0; i < 2 && udev_device; i++) {
1425                 match = evdev_udev_tag_matches;
1426                 while (match->name) {
1427                         prop = udev_device_get_property_value(
1428                                                       udev_device,
1429                                                       match->name);
1430                         if (prop)
1431                                 tags |= match->tag;
1432
1433                         match++;
1434                 }
1435                 udev_device = udev_device_get_parent(udev_device);
1436         }
1437
1438         return tags;
1439 }
1440
1441 static inline void
1442 evdev_fix_android_mt(struct evdev_device *device)
1443 {
1444         struct libevdev *evdev = device->evdev;
1445
1446         if (libevdev_has_event_code(evdev, EV_ABS, ABS_X) ||
1447             libevdev_has_event_code(evdev, EV_ABS, ABS_Y))
1448                 return;
1449
1450         if (!libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) ||
1451             !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y))
1452                 return;
1453
1454         libevdev_set_abs_info(evdev, ABS_X,
1455                       libevdev_get_abs_info(evdev, ABS_MT_POSITION_X));
1456         libevdev_set_abs_info(evdev, ABS_Y,
1457                       libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y));
1458 }
1459
1460 static int
1461 evdev_reject_device(struct evdev_device *device)
1462 {
1463         struct libevdev *evdev = device->evdev;
1464
1465         if (libevdev_has_event_code(evdev, EV_ABS, ABS_X) ^
1466             libevdev_has_event_code(evdev, EV_ABS, ABS_Y))
1467                 return -1;
1468
1469         if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) ^
1470             libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y))
1471                 return -1;
1472
1473         return 0;
1474 }
1475
1476 static int
1477 evdev_configure_mt_device(struct evdev_device *device)
1478 {
1479         struct libevdev *evdev = device->evdev;
1480         struct mt_slot *slots;
1481         int num_slots;
1482         int active_slot;
1483         int slot;
1484
1485         if (!libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) ||
1486             !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y))
1487                  return 0;
1488
1489         if (evdev_fix_abs_resolution(device,
1490                                      ABS_MT_POSITION_X,
1491                                      ABS_MT_POSITION_Y,
1492                                      EVDEV_FAKE_RESOLUTION,
1493                                      EVDEV_FAKE_RESOLUTION))
1494                 device->abs.fake_resolution = 1;
1495
1496         device->abs.absinfo_x = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
1497         device->abs.absinfo_y = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
1498         device->is_mt = 1;
1499
1500         /* We only handle the slotted Protocol B in libinput.
1501            Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
1502            require mtdev for conversion. */
1503         if (evdev_need_mtdev(device)) {
1504                 device->mtdev = mtdev_new_open(device->fd);
1505                 if (!device->mtdev)
1506                         return -1;
1507
1508                 /* pick 10 slots as default for type A
1509                    devices. */
1510                 num_slots = 10;
1511                 active_slot = device->mtdev->caps.slot.value;
1512         } else {
1513                 num_slots = libevdev_get_num_slots(device->evdev);
1514                 active_slot = libevdev_get_current_slot(evdev);
1515         }
1516
1517         slots = calloc(num_slots, sizeof(struct mt_slot));
1518         if (!slots)
1519                 return -1;
1520
1521         for (slot = 0; slot < num_slots; ++slot) {
1522                 slots[slot].seat_slot = -1;
1523                 slots[slot].point.x = 0;
1524                 slots[slot].point.y = 0;
1525         }
1526         device->mt.slots = slots;
1527         device->mt.slots_len = num_slots;
1528         device->mt.slot = active_slot;
1529
1530         return 0;
1531 }
1532
1533 static int
1534 evdev_configure_device(struct evdev_device *device)
1535 {
1536         struct libinput *libinput = device->base.seat->libinput;
1537         struct libevdev *evdev = device->evdev;
1538         const char *devnode = udev_device_get_devnode(device->udev_device);
1539         enum evdev_device_udev_tags udev_tags;
1540
1541         udev_tags = evdev_device_get_udev_tags(device, device->udev_device);
1542
1543         if ((udev_tags & EVDEV_UDEV_TAG_INPUT) == 0 ||
1544             (udev_tags & ~EVDEV_UDEV_TAG_INPUT) == 0) {
1545                 log_info(libinput,
1546                          "input device '%s', %s not tagged as input device\n",
1547                          device->devname, devnode);
1548                 return -1;
1549         }
1550
1551         log_info(libinput,
1552                  "input device '%s', %s is tagged by udev as:%s%s%s%s%s%s%s%s\n",
1553                  device->devname, devnode,
1554                  udev_tags & EVDEV_UDEV_TAG_KEYBOARD ? " Keyboard" : "",
1555                  udev_tags & EVDEV_UDEV_TAG_MOUSE ? " Mouse" : "",
1556                  udev_tags & EVDEV_UDEV_TAG_TOUCHPAD ? " Touchpad" : "",
1557                  udev_tags & EVDEV_UDEV_TAG_TOUCHSCREEN ? " Touchscreen" : "",
1558                  udev_tags & EVDEV_UDEV_TAG_TABLET ? " Tablet" : "",
1559                  udev_tags & EVDEV_UDEV_TAG_JOYSTICK ? " Joystick" : "",
1560                  udev_tags & EVDEV_UDEV_TAG_ACCELEROMETER ? " Accelerometer" : "",
1561                  udev_tags & EVDEV_UDEV_TAG_BUTTONSET ? " Buttonset" : "");
1562
1563         /* libwacom *adds* TABLET, TOUCHPAD but leaves JOYSTICK in place, so
1564            make sure we only ignore real joystick devices */
1565         if ((udev_tags & EVDEV_UDEV_TAG_JOYSTICK) == udev_tags) {
1566                 log_info(libinput,
1567                          "input device '%s', %s is a joystick, ignoring\n",
1568                          device->devname, devnode);
1569                 return -1;
1570         }
1571
1572         /* libwacom assigns tablet _and_ tablet_pad to the pad devices */
1573         if (udev_tags & EVDEV_UDEV_TAG_BUTTONSET) {
1574                 log_info(libinput,
1575                          "input device '%s', %s is a buttonset, ignoring\n",
1576                          device->devname, devnode);
1577                 return -1;
1578         }
1579
1580         if (evdev_reject_device(device) == -1) {
1581                 log_info(libinput,
1582                          "input device '%s', %s was rejected.\n",
1583                          device->devname, devnode);
1584                 return -1;
1585         }
1586
1587         if (libevdev_has_event_code(evdev, EV_ABS, ABS_X) ||
1588             libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X)) {
1589                 evdev_fix_android_mt(device);
1590
1591                 if (evdev_fix_abs_resolution(device,
1592                                              ABS_X,
1593                                              ABS_Y,
1594                                              EVDEV_FAKE_RESOLUTION,
1595                                              EVDEV_FAKE_RESOLUTION))
1596                         device->abs.fake_resolution = 1;
1597                 device->abs.absinfo_x = libevdev_get_abs_info(evdev, ABS_X);
1598                 device->abs.absinfo_y = libevdev_get_abs_info(evdev, ABS_Y);
1599
1600                 /* Fake MT devices have the ABS_MT_SLOT bit set because of
1601                    the limited ABS_* range - they aren't MT devices, they
1602                    just have too many ABS_ axes */
1603                 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT) &&
1604                     libevdev_get_num_slots(evdev) == -1) {
1605                         udev_tags &= ~EVDEV_UDEV_TAG_TOUCHSCREEN;
1606                 } else if (evdev_configure_mt_device(device) == -1) {
1607                         return -1;
1608                 }
1609         }
1610
1611         if (udev_tags & EVDEV_UDEV_TAG_TOUCHPAD) {
1612                 device->dispatch = evdev_mt_touchpad_create(device);
1613                 log_info(libinput,
1614                          "input device '%s', %s is a touchpad\n",
1615                          device->devname, devnode);
1616                 return device->dispatch == NULL ? -1 : 0;
1617         }
1618
1619         if (udev_tags & EVDEV_UDEV_TAG_MOUSE) {
1620                 if (!libevdev_has_event_code(evdev, EV_ABS, ABS_X) &&
1621                     !libevdev_has_event_code(evdev, EV_ABS, ABS_Y) &&
1622                     evdev_device_init_pointer_acceleration(
1623                                         device,
1624                                         pointer_accel_profile_linear) == -1)
1625                         return -1;
1626
1627                 device->seat_caps |= EVDEV_DEVICE_POINTER;
1628
1629                 log_info(libinput,
1630                          "input device '%s', %s is a pointer caps\n",
1631                          device->devname, devnode);
1632
1633                 /* want left-handed config option */
1634                 device->left_handed.want_enabled = true;
1635                 /* want natural-scroll config option */
1636                 device->scroll.natural_scrolling_enabled = true;
1637                 /* want button scrolling config option */
1638                 device->scroll.want_button = 1;
1639         }
1640
1641         if (udev_tags & EVDEV_UDEV_TAG_KEYBOARD) {
1642                 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
1643                 log_info(libinput,
1644                          "input device '%s', %s is a keyboard\n",
1645                          device->devname, devnode);
1646         }
1647
1648         if (udev_tags & EVDEV_UDEV_TAG_TOUCHSCREEN) {
1649                 device->seat_caps |= EVDEV_DEVICE_TOUCH;
1650                 log_info(libinput,
1651                          "input device '%s', %s is a touch device\n",
1652                          device->devname, devnode);
1653         }
1654
1655         return 0;
1656 }
1657
1658 static void
1659 evdev_notify_added_device(struct evdev_device *device)
1660 {
1661         struct libinput_device *dev;
1662
1663         list_for_each(dev, &device->base.seat->devices_list, link) {
1664                 struct evdev_device *d = (struct evdev_device*)dev;
1665                 if (dev == &device->base)
1666                         continue;
1667
1668                 /* Notify existing device d about addition of device device */
1669                 if (d->dispatch->interface->device_added)
1670                         d->dispatch->interface->device_added(d, device);
1671
1672                 /* Notify new device device about existing device d */
1673                 if (device->dispatch->interface->device_added)
1674                         device->dispatch->interface->device_added(device, d);
1675
1676                 /* Notify new device device if existing device d is suspended */
1677                 if (d->suspended && device->dispatch->interface->device_suspended)
1678                         device->dispatch->interface->device_suspended(device, d);
1679         }
1680
1681         notify_added_device(&device->base);
1682 }
1683
1684 static int
1685 evdev_device_compare_syspath(struct udev_device *udev_device, int fd)
1686 {
1687         struct udev *udev = udev_device_get_udev(udev_device);
1688         struct udev_device *udev_device_new = NULL;
1689         struct stat st;
1690         int rc = 1;
1691
1692         if (fstat(fd, &st) < 0)
1693                 goto out;
1694
1695         udev_device_new = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
1696         if (!udev_device_new)
1697                 goto out;
1698
1699         rc = strcmp(udev_device_get_syspath(udev_device_new),
1700                     udev_device_get_syspath(udev_device));
1701 out:
1702         if (udev_device_new)
1703                 udev_device_unref(udev_device_new);
1704         return rc;
1705 }
1706
1707 static int
1708 evdev_set_device_group(struct evdev_device *device,
1709                        struct udev_device *udev_device)
1710 {
1711         struct libinput_device_group *group = NULL;
1712         const char *udev_group;
1713
1714         udev_group = udev_device_get_property_value(udev_device,
1715                                                     "LIBINPUT_DEVICE_GROUP");
1716         if (udev_group) {
1717                 struct libinput_device *d;
1718
1719                 list_for_each(d, &device->base.seat->devices_list, link) {
1720                         const char *identifier = d->group->identifier;
1721
1722                         if (identifier &&
1723                             strcmp(identifier, udev_group) == 0) {
1724                                 group = d->group;
1725                                 break;
1726                         }
1727                 }
1728         }
1729
1730         if (!group) {
1731                 group = libinput_device_group_create(udev_group);
1732                 if (!group)
1733                         return 1;
1734                 libinput_device_set_device_group(&device->base, group);
1735                 libinput_device_group_unref(group);
1736         } else {
1737                 libinput_device_set_device_group(&device->base, group);
1738         }
1739
1740         return 0;
1741 }
1742
1743 struct evdev_device *
1744 evdev_device_create(struct libinput_seat *seat,
1745                     struct udev_device *udev_device)
1746 {
1747         struct libinput *libinput = seat->libinput;
1748         struct evdev_device *device = NULL;
1749         int rc;
1750         int fd;
1751         int unhandled_device = 0;
1752         const char *devnode = udev_device_get_devnode(udev_device);
1753
1754         /* Use non-blocking mode so that we can loop on read on
1755          * evdev_device_data() until all events on the fd are
1756          * read.  mtdev_get() also expects this. */
1757         fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
1758         if (fd < 0) {
1759                 log_info(libinput,
1760                          "opening input device '%s' failed (%s).\n",
1761                          devnode, strerror(-fd));
1762                 return NULL;
1763         }
1764
1765         if (evdev_device_compare_syspath(udev_device, fd) != 0)
1766                 goto err;
1767
1768         device = zalloc(sizeof *device);
1769         if (device == NULL)
1770                 goto err;
1771
1772         libinput_device_init(&device->base, seat);
1773         libinput_seat_ref(seat);
1774
1775         rc = libevdev_new_from_fd(fd, &device->evdev);
1776         if (rc != 0)
1777                 goto err;
1778
1779         libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
1780
1781         device->seat_caps = 0;
1782         device->is_mt = 0;
1783         device->mtdev = NULL;
1784         device->udev_device = udev_device_ref(udev_device);
1785         device->rel.x = 0;
1786         device->rel.y = 0;
1787         device->abs.seat_slot = -1;
1788         device->dispatch = NULL;
1789         device->fd = fd;
1790         device->pending_event = EVDEV_NONE;
1791         device->devname = libevdev_get_name(device->evdev);
1792         device->scroll.threshold = 5.0; /* Default may be overridden */
1793         device->scroll.direction = 0;
1794         device->scroll.wheel_click_angle =
1795                 evdev_read_wheel_click_prop(device);
1796         device->dpi = evdev_read_dpi_prop(device);
1797         /* at most 5 SYN_DROPPED log-messages per 30s */
1798         ratelimit_init(&device->syn_drop_limit, 30ULL * 1000, 5);
1799
1800         matrix_init_identity(&device->abs.calibration);
1801         matrix_init_identity(&device->abs.usermatrix);
1802         matrix_init_identity(&device->abs.default_calibration);
1803
1804         if (evdev_configure_device(device) == -1)
1805                 goto err;
1806
1807         if (device->seat_caps == 0) {
1808                 unhandled_device = 1;
1809                 goto err;
1810         }
1811
1812         /* If the dispatch was not set up use the fallback. */
1813         if (device->dispatch == NULL)
1814                 device->dispatch = fallback_dispatch_create(&device->base);
1815         if (device->dispatch == NULL)
1816                 goto err;
1817
1818         device->source =
1819                 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1820         if (!device->source)
1821                 goto err;
1822
1823         if (evdev_set_device_group(device, udev_device))
1824                 goto err;
1825
1826         list_insert(seat->devices_list.prev, &device->base.link);
1827
1828         evdev_tag_device(device);
1829         evdev_notify_added_device(device);
1830
1831         return device;
1832
1833 err:
1834         if (fd >= 0)
1835                 close_restricted(libinput, fd);
1836         if (device)
1837                 evdev_device_destroy(device);
1838
1839         return unhandled_device ? EVDEV_UNHANDLED_DEVICE :  NULL;
1840 }
1841
1842 const char *
1843 evdev_device_get_output(struct evdev_device *device)
1844 {
1845         return device->output_name;
1846 }
1847
1848 const char *
1849 evdev_device_get_sysname(struct evdev_device *device)
1850 {
1851         return udev_device_get_sysname(device->udev_device);
1852 }
1853
1854 const char *
1855 evdev_device_get_name(struct evdev_device *device)
1856 {
1857         return device->devname;
1858 }
1859
1860 unsigned int
1861 evdev_device_get_id_product(struct evdev_device *device)
1862 {
1863         return libevdev_get_id_product(device->evdev);
1864 }
1865
1866 unsigned int
1867 evdev_device_get_id_vendor(struct evdev_device *device)
1868 {
1869         return libevdev_get_id_vendor(device->evdev);
1870 }
1871
1872 struct udev_device *
1873 evdev_device_get_udev_device(struct evdev_device *device)
1874 {
1875         return udev_device_ref(device->udev_device);
1876 }
1877
1878 void
1879 evdev_device_set_default_calibration(struct evdev_device *device,
1880                                      const float calibration[6])
1881 {
1882         matrix_from_farray6(&device->abs.default_calibration, calibration);
1883         evdev_device_calibrate(device, calibration);
1884 }
1885
1886 void
1887 evdev_device_calibrate(struct evdev_device *device,
1888                        const float calibration[6])
1889 {
1890         struct matrix scale,
1891                       translate,
1892                       transform;
1893         double sx, sy;
1894
1895         matrix_from_farray6(&transform, calibration);
1896         device->abs.apply_calibration = !matrix_is_identity(&transform);
1897
1898         if (!device->abs.apply_calibration) {
1899                 matrix_init_identity(&device->abs.calibration);
1900                 return;
1901         }
1902
1903         sx = device->abs.absinfo_x->maximum - device->abs.absinfo_x->minimum + 1;
1904         sy = device->abs.absinfo_y->maximum - device->abs.absinfo_y->minimum + 1;
1905
1906         /* The transformation matrix is in the form:
1907          *  [ a b c ]
1908          *  [ d e f ]
1909          *  [ 0 0 1 ]
1910          * Where a, e are the scale components, a, b, d, e are the rotation
1911          * component (combined with scale) and c and f are the translation
1912          * component. The translation component in the input matrix must be
1913          * normalized to multiples of the device width and height,
1914          * respectively. e.g. c == 1 shifts one device-width to the right.
1915          *
1916          * We pre-calculate a single matrix to apply to event coordinates:
1917          *     M = Un-Normalize * Calibration * Normalize
1918          *
1919          * Normalize: scales the device coordinates to [0,1]
1920          * Calibration: user-supplied matrix
1921          * Un-Normalize: scales back up to device coordinates
1922          * Matrix maths requires the normalize/un-normalize in reverse
1923          * order.
1924          */
1925
1926         /* back up the user matrix so we can return it on request */
1927         matrix_from_farray6(&device->abs.usermatrix, calibration);
1928
1929         /* Un-Normalize */
1930         matrix_init_translate(&translate,
1931                               device->abs.absinfo_x->minimum,
1932                               device->abs.absinfo_y->minimum);
1933         matrix_init_scale(&scale, sx, sy);
1934         matrix_mult(&scale, &translate, &scale);
1935
1936         /* Calibration */
1937         matrix_mult(&transform, &scale, &transform);
1938
1939         /* Normalize */
1940         matrix_init_translate(&translate,
1941                               -device->abs.absinfo_x->minimum/sx,
1942                               -device->abs.absinfo_y->minimum/sy);
1943         matrix_init_scale(&scale, 1.0/sx, 1.0/sy);
1944         matrix_mult(&scale, &translate, &scale);
1945
1946         /* store final matrix in device */
1947         matrix_mult(&device->abs.calibration, &transform, &scale);
1948 }
1949
1950 int
1951 evdev_device_has_capability(struct evdev_device *device,
1952                             enum libinput_device_capability capability)
1953 {
1954         switch (capability) {
1955         case LIBINPUT_DEVICE_CAP_POINTER:
1956                 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
1957         case LIBINPUT_DEVICE_CAP_KEYBOARD:
1958                 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
1959         case LIBINPUT_DEVICE_CAP_TOUCH:
1960                 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
1961         default:
1962                 return 0;
1963         }
1964 }
1965
1966 int
1967 evdev_device_get_size(struct evdev_device *device,
1968                       double *width,
1969                       double *height)
1970 {
1971         const struct input_absinfo *x, *y;
1972
1973         x = libevdev_get_abs_info(device->evdev, ABS_X);
1974         y = libevdev_get_abs_info(device->evdev, ABS_Y);
1975
1976         if (!x || !y || device->abs.fake_resolution ||
1977             !x->resolution || !y->resolution)
1978                 return -1;
1979
1980         *width = evdev_convert_to_mm(x, x->maximum);
1981         *height = evdev_convert_to_mm(y, y->maximum);
1982
1983         return 0;
1984 }
1985
1986 int
1987 evdev_device_has_button(struct evdev_device *device, uint32_t code)
1988 {
1989         if (!(device->seat_caps & EVDEV_DEVICE_POINTER))
1990                 return -1;
1991
1992         return libevdev_has_event_code(device->evdev, EV_KEY, code);
1993 }
1994
1995 static inline bool
1996 evdev_is_scrolling(const struct evdev_device *device,
1997                    enum libinput_pointer_axis axis)
1998 {
1999         assert(axis == LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL ||
2000                axis == LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2001
2002         return (device->scroll.direction & AS_MASK(axis)) != 0;
2003 }
2004
2005 static inline void
2006 evdev_start_scrolling(struct evdev_device *device,
2007                       enum libinput_pointer_axis axis)
2008 {
2009         assert(axis == LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL ||
2010                axis == LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2011
2012         device->scroll.direction |= AS_MASK(axis);
2013 }
2014
2015 void
2016 evdev_post_scroll(struct evdev_device *device,
2017                   uint64_t time,
2018                   enum libinput_pointer_axis_source source,
2019                   const struct normalized_coords *delta)
2020 {
2021         const struct normalized_coords *trigger;
2022         struct normalized_coords event;
2023
2024         if (!evdev_is_scrolling(device,
2025                                 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
2026                 device->scroll.buildup.y += delta->y;
2027         if (!evdev_is_scrolling(device,
2028                                 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
2029                 device->scroll.buildup.x += delta->x;
2030
2031         trigger = &device->scroll.buildup;
2032
2033         /* If we're not scrolling yet, use a distance trigger: moving
2034            past a certain distance starts scrolling */
2035         if (!evdev_is_scrolling(device,
2036                                 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL) &&
2037             !evdev_is_scrolling(device,
2038                                 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
2039                 if (fabs(trigger->y) >= device->scroll.threshold)
2040                         evdev_start_scrolling(device,
2041                                               LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2042                 if (fabs(trigger->x) >= device->scroll.threshold)
2043                         evdev_start_scrolling(device,
2044                                               LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
2045         /* We're already scrolling in one direction. Require some
2046            trigger speed to start scrolling in the other direction */
2047         } else if (!evdev_is_scrolling(device,
2048                                LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
2049                 if (fabs(delta->y) >= device->scroll.threshold)
2050                         evdev_start_scrolling(device,
2051                                       LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2052         } else if (!evdev_is_scrolling(device,
2053                                 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL)) {
2054                 if (fabs(delta->x) >= device->scroll.threshold)
2055                         evdev_start_scrolling(device,
2056                                       LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
2057         }
2058
2059         event = *delta;
2060
2061         /* We use the trigger to enable, but the delta from this event for
2062          * the actual scroll movement. Otherwise we get a jump once
2063          * scrolling engages */
2064         if (!evdev_is_scrolling(device,
2065                                LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
2066                 event.y = 0.0;
2067
2068         if (!evdev_is_scrolling(device,
2069                                LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
2070                 event.x = 0.0;
2071
2072         if (event.x != 0.0 || event.y != 0.0) {
2073                 const struct discrete_coords zero_discrete = { 0.0, 0.0 };
2074                 evdev_notify_axis(device,
2075                                   time,
2076                                   device->scroll.direction,
2077                                   source,
2078                                   &event,
2079                                   &zero_discrete);
2080         }
2081 }
2082
2083 void
2084 evdev_stop_scroll(struct evdev_device *device,
2085                   uint64_t time,
2086                   enum libinput_pointer_axis_source source)
2087 {
2088         const struct normalized_coords zero = { 0.0, 0.0 };
2089         const struct discrete_coords zero_discrete = { 0.0, 0.0 };
2090
2091         /* terminate scrolling with a zero scroll event */
2092         if (device->scroll.direction != 0)
2093                 pointer_notify_axis(&device->base,
2094                                     time,
2095                                     device->scroll.direction,
2096                                     source,
2097                                     &zero,
2098                                     &zero_discrete);
2099
2100         device->scroll.buildup.x = 0;
2101         device->scroll.buildup.y = 0;
2102         device->scroll.direction = 0;
2103 }
2104
2105 static void
2106 release_pressed_keys(struct evdev_device *device)
2107 {
2108         struct libinput *libinput = device->base.seat->libinput;
2109         uint64_t time;
2110         int code;
2111
2112         if ((time = libinput_now(libinput)) == 0)
2113                 return;
2114
2115         for (code = 0; code < KEY_CNT; code++) {
2116                 int count = get_key_down_count(device, code);
2117
2118                 if (count > 1) {
2119                         log_bug_libinput(libinput,
2120                                          "Key %d is down %d times.\n",
2121                                          code,
2122                                          count);
2123                 }
2124
2125                 while (get_key_down_count(device, code) > 0) {
2126                         switch (get_key_type(code)) {
2127                         case EVDEV_KEY_TYPE_NONE:
2128                                 break;
2129                         case EVDEV_KEY_TYPE_KEY:
2130                                 evdev_keyboard_notify_key(
2131                                         device,
2132                                         time,
2133                                         code,
2134                                         LIBINPUT_KEY_STATE_RELEASED);
2135                                 break;
2136                         case EVDEV_KEY_TYPE_BUTTON:
2137                                 evdev_pointer_notify_button(
2138                                         device,
2139                                         time,
2140                                         evdev_to_left_handed(device, code),
2141                                         LIBINPUT_BUTTON_STATE_RELEASED);
2142                                 break;
2143                         }
2144                 }
2145         }
2146 }
2147
2148 void
2149 evdev_notify_suspended_device(struct evdev_device *device)
2150 {
2151         struct libinput_device *it;
2152
2153         if (device->suspended)
2154                 return;
2155
2156         list_for_each(it, &device->base.seat->devices_list, link) {
2157                 struct evdev_device *d = (struct evdev_device*)it;
2158                 if (it == &device->base)
2159                         continue;
2160
2161                 if (d->dispatch->interface->device_suspended)
2162                         d->dispatch->interface->device_suspended(d, device);
2163         }
2164
2165         device->suspended = 1;
2166 }
2167
2168 void
2169 evdev_notify_resumed_device(struct evdev_device *device)
2170 {
2171         struct libinput_device *it;
2172
2173         if (!device->suspended)
2174                 return;
2175
2176         list_for_each(it, &device->base.seat->devices_list, link) {
2177                 struct evdev_device *d = (struct evdev_device*)it;
2178                 if (it == &device->base)
2179                         continue;
2180
2181                 if (d->dispatch->interface->device_resumed)
2182                         d->dispatch->interface->device_resumed(d, device);
2183         }
2184
2185         device->suspended = 0;
2186 }
2187
2188 int
2189 evdev_device_suspend(struct evdev_device *device)
2190 {
2191         evdev_notify_suspended_device(device);
2192
2193         if (device->source) {
2194                 libinput_remove_source(device->base.seat->libinput,
2195                                        device->source);
2196                 device->source = NULL;
2197         }
2198
2199         release_pressed_keys(device);
2200
2201         if (device->mtdev) {
2202                 mtdev_close_delete(device->mtdev);
2203                 device->mtdev = NULL;
2204         }
2205
2206         if (device->fd != -1) {
2207                 close_restricted(device->base.seat->libinput, device->fd);
2208                 device->fd = -1;
2209         }
2210
2211         return 0;
2212 }
2213
2214 int
2215 evdev_device_resume(struct evdev_device *device)
2216 {
2217         struct libinput *libinput = device->base.seat->libinput;
2218         int fd;
2219         const char *devnode;
2220         struct input_event ev;
2221         enum libevdev_read_status status;
2222
2223         if (device->fd != -1)
2224                 return 0;
2225
2226         if (device->was_removed)
2227                 return -ENODEV;
2228
2229         devnode = udev_device_get_devnode(device->udev_device);
2230         fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
2231
2232         if (fd < 0)
2233                 return -errno;
2234
2235         if (evdev_device_compare_syspath(device->udev_device, fd)) {
2236                 close_restricted(libinput, fd);
2237                 return -ENODEV;
2238         }
2239
2240         device->fd = fd;
2241
2242         if (evdev_need_mtdev(device)) {
2243                 device->mtdev = mtdev_new_open(device->fd);
2244                 if (!device->mtdev)
2245                         return -ENODEV;
2246         }
2247
2248         libevdev_change_fd(device->evdev, fd);
2249         libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
2250
2251         /* re-sync libevdev's view of the device, but discard the actual
2252            events. Our device is in a neutral state already */
2253         libevdev_next_event(device->evdev,
2254                             LIBEVDEV_READ_FLAG_FORCE_SYNC,
2255                             &ev);
2256         do {
2257                 status = libevdev_next_event(device->evdev,
2258                                              LIBEVDEV_READ_FLAG_SYNC,
2259                                              &ev);
2260         } while (status == LIBEVDEV_READ_STATUS_SYNC);
2261
2262         device->source =
2263                 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
2264         if (!device->source) {
2265                 mtdev_close_delete(device->mtdev);
2266                 return -ENOMEM;
2267         }
2268
2269         memset(device->hw_key_mask, 0, sizeof(device->hw_key_mask));
2270
2271         evdev_notify_resumed_device(device);
2272
2273         return 0;
2274 }
2275
2276 void
2277 evdev_device_remove(struct evdev_device *device)
2278 {
2279         struct libinput_device *dev;
2280
2281         list_for_each(dev, &device->base.seat->devices_list, link) {
2282                 struct evdev_device *d = (struct evdev_device*)dev;
2283                 if (dev == &device->base)
2284                         continue;
2285
2286                 if (d->dispatch->interface->device_removed)
2287                         d->dispatch->interface->device_removed(d, device);
2288         }
2289
2290         evdev_device_suspend(device);
2291
2292         if (device->dispatch->interface->remove)
2293                 device->dispatch->interface->remove(device->dispatch);
2294
2295         /* A device may be removed while suspended, mark it to
2296          * skip re-opening a different device with the same node */
2297         device->was_removed = true;
2298
2299         list_remove(&device->base.link);
2300
2301         notify_removed_device(&device->base);
2302         libinput_device_unref(&device->base);
2303 }
2304
2305 void
2306 evdev_device_destroy(struct evdev_device *device)
2307 {
2308         struct evdev_dispatch *dispatch;
2309
2310         dispatch = device->dispatch;
2311         if (dispatch)
2312                 dispatch->interface->destroy(dispatch);
2313
2314         if (device->base.group)
2315                 libinput_device_group_unref(device->base.group);
2316
2317         filter_destroy(device->pointer.filter);
2318         libinput_seat_unref(device->base.seat);
2319         libevdev_free(device->evdev);
2320         udev_device_unref(device->udev_device);
2321         free(device->mt.slots);
2322         free(device);
2323 }