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