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