de7ee16c7e48c9d2682bbef1f6ef485c84e61351
[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 configure_pointer_acceleration(struct evdev_device *device)
865 {
866         device->pointer.filter =
867                 create_pointer_accelator_filter(
868                         pointer_accel_profile_linear);
869         if (!device->pointer.filter)
870                 return -1;
871
872         return 0;
873 }
874
875
876 static inline int
877 evdev_need_mtdev(struct evdev_device *device)
878 {
879         struct libevdev *evdev = device->evdev;
880
881         return (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
882                 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y) &&
883                 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT));
884 }
885
886 static void
887 evdev_tag_device(struct evdev_device *device)
888 {
889         struct udev *udev;
890         struct udev_device *udev_device = NULL;
891
892         udev = udev_new();
893         if (!udev)
894                 return;
895
896         udev_device = udev_device_new_from_syspath(udev, device->syspath);
897         if (udev_device) {
898                 if (device->dispatch->interface->tag_device)
899                         device->dispatch->interface->tag_device(device, udev_device);
900                 udev_device_unref(udev_device);
901         }
902         udev_unref(udev);
903 }
904
905 static int
906 evdev_configure_device(struct evdev_device *device)
907 {
908         struct libinput *libinput = device->base.seat->libinput;
909         struct libevdev *evdev = device->evdev;
910         const struct input_absinfo *absinfo;
911         struct input_absinfo fixed;
912         int has_abs, has_rel, has_mt;
913         int has_button, has_keyboard, has_touch;
914         struct mt_slot *slots;
915         int num_slots;
916         int active_slot;
917         int slot;
918         unsigned int i;
919
920         has_rel = 0;
921         has_abs = 0;
922         has_mt = 0;
923         has_button = 0;
924         has_keyboard = 0;
925         has_touch = 0;
926
927         if (libevdev_has_event_type(evdev, EV_ABS)) {
928
929                 if ((absinfo = libevdev_get_abs_info(evdev, ABS_X))) {
930                         if (absinfo->resolution == 0) {
931                                 fixed = *absinfo;
932                                 fixed.resolution = 1;
933                                 libevdev_set_abs_info(evdev, ABS_X, &fixed);
934                                 device->abs.fake_resolution = 1;
935                         }
936                         device->abs.absinfo_x = absinfo;
937                         has_abs = 1;
938                 }
939                 if ((absinfo = libevdev_get_abs_info(evdev, ABS_Y))) {
940                         if (absinfo->resolution == 0) {
941                                 fixed = *absinfo;
942                                 fixed.resolution = 1;
943                                 libevdev_set_abs_info(evdev, ABS_Y, &fixed);
944                                 device->abs.fake_resolution = 1;
945                         }
946                         device->abs.absinfo_y = absinfo;
947                         has_abs = 1;
948                 }
949                 /* We only handle the slotted Protocol B in weston.
950                    Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
951                    require mtdev for conversion. */
952                 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
953                     libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y)) {
954                         absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
955                         if (absinfo->resolution == 0) {
956                                 fixed = *absinfo;
957                                 fixed.resolution = 1;
958                                 libevdev_set_abs_info(evdev,
959                                                       ABS_MT_POSITION_X,
960                                                       &fixed);
961                                 device->abs.fake_resolution = 1;
962                         }
963                         device->abs.absinfo_x = absinfo;
964                         absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
965                         if (absinfo->resolution == 0) {
966                                 fixed = *absinfo;
967                                 fixed.resolution = 1;
968                                 libevdev_set_abs_info(evdev,
969                                                       ABS_MT_POSITION_Y,
970                                                       &fixed);
971                                 device->abs.fake_resolution = 1;
972                         }
973                         device->abs.absinfo_y = absinfo;
974                         device->is_mt = 1;
975                         has_touch = 1;
976                         has_mt = 1;
977
978                         if (evdev_need_mtdev(device)) {
979                                 device->mtdev = mtdev_new_open(device->fd);
980                                 if (!device->mtdev)
981                                         return -1;
982
983                                 num_slots = device->mtdev->caps.slot.maximum;
984                                 if (device->mtdev->caps.slot.minimum < 0 ||
985                                     num_slots <= 0)
986                                         return -1;
987                                 active_slot = device->mtdev->caps.slot.value;
988                         } else {
989                                 num_slots = libevdev_get_num_slots(device->evdev);
990                                 active_slot = libevdev_get_current_slot(evdev);
991                         }
992
993                         slots = calloc(num_slots, sizeof(struct mt_slot));
994                         if (!slots)
995                                 return -1;
996
997                         for (slot = 0; slot < num_slots; ++slot) {
998                                 slots[slot].seat_slot = -1;
999                                 slots[slot].x = 0;
1000                                 slots[slot].y = 0;
1001                         }
1002                         device->mt.slots = slots;
1003                         device->mt.slots_len = num_slots;
1004                         device->mt.slot = active_slot;
1005                 }
1006         }
1007
1008         if (libevdev_has_property(evdev, INPUT_PROP_POINTING_STICK)) {
1009                 libinput_timer_init(&device->scroll.timer,
1010                                     device->base.seat->libinput,
1011                                     evdev_middle_button_scroll_timeout,
1012                                     device);
1013                 device->scroll.has_middle_button_scroll = true;
1014         }
1015
1016         if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
1017             libevdev_has_event_code(evdev, EV_REL, REL_Y))
1018                 has_rel = 1;
1019
1020         if (libevdev_has_event_type(evdev, EV_KEY)) {
1021                 if (!libevdev_has_property(evdev, INPUT_PROP_DIRECT) &&
1022                     libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_FINGER) &&
1023                     !libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_PEN) &&
1024                     (has_abs || has_mt)) {
1025                         device->dispatch = evdev_mt_touchpad_create(device);
1026                         log_info(libinput,
1027                                  "input device '%s', %s is a touchpad\n",
1028                                  device->devname, device->devnode);
1029                         return device->dispatch == NULL ? -1 : 0;
1030                 }
1031
1032                 for (i = 0; i < KEY_MAX; i++) {
1033                         if (libevdev_has_event_code(evdev, EV_KEY, i)) {
1034                                 switch (get_key_type(i)) {
1035                                 case EVDEV_KEY_TYPE_NONE:
1036                                         break;
1037                                 case EVDEV_KEY_TYPE_KEY:
1038                                         has_keyboard = 1;
1039                                         break;
1040                                 case EVDEV_KEY_TYPE_BUTTON:
1041                                         has_button = 1;
1042                                         break;
1043                                 }
1044                         }
1045                 }
1046
1047                 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH))
1048                         has_touch = 1;
1049         }
1050         if (libevdev_has_event_type(evdev, EV_LED))
1051                 has_keyboard = 1;
1052
1053         if ((has_abs || has_rel) && has_button) {
1054                 if (configure_pointer_acceleration(device) == -1)
1055                         return -1;
1056
1057                 device->seat_caps |= EVDEV_DEVICE_POINTER;
1058
1059                 log_info(libinput,
1060                          "input device '%s', %s is a pointer caps =%s%s%s\n",
1061                          device->devname, device->devnode,
1062                          has_abs ? " absolute-motion" : "",
1063                          has_rel ? " relative-motion": "",
1064                          has_button ? " button" : "");
1065         }
1066         if (has_keyboard) {
1067                 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
1068                 log_info(libinput,
1069                          "input device '%s', %s is a keyboard\n",
1070                          device->devname, device->devnode);
1071         }
1072         if (has_touch && !has_button) {
1073                 device->seat_caps |= EVDEV_DEVICE_TOUCH;
1074                 log_info(libinput,
1075                          "input device '%s', %s is a touch device\n",
1076                          device->devname, device->devnode);
1077         }
1078
1079         return 0;
1080 }
1081
1082 static void
1083 evdev_notify_added_device(struct evdev_device *device)
1084 {
1085         struct libinput_device *dev;
1086
1087         list_for_each(dev, &device->base.seat->devices_list, link) {
1088                 struct evdev_device *d = (struct evdev_device*)dev;
1089                 if (dev == &device->base)
1090                         continue;
1091
1092                 /* Notify existing device d about addition of device device */
1093                 if (d->dispatch->interface->device_added)
1094                         d->dispatch->interface->device_added(d, device);
1095
1096                 /* Notify new device device about existing device d */
1097                 if (device->dispatch->interface->device_added)
1098                         device->dispatch->interface->device_added(device, d);
1099
1100                 /* Notify new device device if existing device d is suspended */
1101                 if (d->suspended && device->dispatch->interface->device_suspended)
1102                         device->dispatch->interface->device_suspended(device, d);
1103         }
1104
1105         notify_added_device(&device->base);
1106 }
1107
1108 struct evdev_device *
1109 evdev_device_create(struct libinput_seat *seat,
1110                     const char *devnode,
1111                     const char *sysname,
1112                     const char *syspath)
1113 {
1114         struct libinput *libinput = seat->libinput;
1115         struct evdev_device *device;
1116         int rc;
1117         int fd;
1118         int unhandled_device = 0;
1119
1120         /* Use non-blocking mode so that we can loop on read on
1121          * evdev_device_data() until all events on the fd are
1122          * read.  mtdev_get() also expects this. */
1123         fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
1124         if (fd < 0) {
1125                 log_info(libinput,
1126                          "opening input device '%s' failed (%s).\n",
1127                          devnode, strerror(-fd));
1128                 return NULL;
1129         }
1130
1131         device = zalloc(sizeof *device);
1132         if (device == NULL)
1133                 return NULL;
1134
1135         libinput_device_init(&device->base, seat);
1136         libinput_seat_ref(seat);
1137
1138         rc = libevdev_new_from_fd(fd, &device->evdev);
1139         if (rc != 0)
1140                 goto err;
1141
1142         libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
1143
1144         device->seat_caps = 0;
1145         device->is_mt = 0;
1146         device->mtdev = NULL;
1147         device->devnode = strdup(devnode);
1148         device->sysname = strdup(sysname);
1149         device->syspath = strdup(syspath);
1150         device->rel.dx = 0;
1151         device->rel.dy = 0;
1152         device->abs.seat_slot = -1;
1153         device->dispatch = NULL;
1154         device->fd = fd;
1155         device->pending_event = EVDEV_NONE;
1156         device->devname = libevdev_get_name(device->evdev);
1157         device->scroll.threshold = 5.0; /* Default may be overridden */
1158         device->scroll.direction = 0;
1159
1160         matrix_init_identity(&device->abs.calibration);
1161         matrix_init_identity(&device->abs.usermatrix);
1162         matrix_init_identity(&device->abs.default_calibration);
1163
1164         if (evdev_configure_device(device) == -1)
1165                 goto err;
1166
1167         if (device->seat_caps == 0) {
1168                 unhandled_device = 1;
1169                 goto err;
1170         }
1171
1172         /* If the dispatch was not set up use the fallback. */
1173         if (device->dispatch == NULL)
1174                 device->dispatch = fallback_dispatch_create(&device->base);
1175         if (device->dispatch == NULL)
1176                 goto err;
1177
1178         device->source =
1179                 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1180         if (!device->source)
1181                 goto err;
1182
1183         list_insert(seat->devices_list.prev, &device->base.link);
1184
1185         evdev_tag_device(device);
1186         evdev_notify_added_device(device);
1187
1188         return device;
1189
1190 err:
1191         if (fd >= 0)
1192                 close_restricted(libinput, fd);
1193         evdev_device_destroy(device);
1194
1195         return unhandled_device ? EVDEV_UNHANDLED_DEVICE :  NULL;
1196 }
1197
1198 int
1199 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
1200 {
1201         memset(keys, 0, size);
1202         return 0;
1203 }
1204
1205 const char *
1206 evdev_device_get_output(struct evdev_device *device)
1207 {
1208         return device->output_name;
1209 }
1210
1211 const char *
1212 evdev_device_get_sysname(struct evdev_device *device)
1213 {
1214         return device->sysname;
1215 }
1216
1217 const char *
1218 evdev_device_get_name(struct evdev_device *device)
1219 {
1220         return device->devname;
1221 }
1222
1223 unsigned int
1224 evdev_device_get_id_product(struct evdev_device *device)
1225 {
1226         return libevdev_get_id_product(device->evdev);
1227 }
1228
1229 unsigned int
1230 evdev_device_get_id_vendor(struct evdev_device *device)
1231 {
1232         return libevdev_get_id_vendor(device->evdev);
1233 }
1234
1235 void
1236 evdev_device_set_default_calibration(struct evdev_device *device,
1237                                      const float calibration[6])
1238 {
1239         matrix_from_farray6(&device->abs.default_calibration, calibration);
1240         evdev_device_calibrate(device, calibration);
1241 }
1242
1243 void
1244 evdev_device_calibrate(struct evdev_device *device,
1245                        const float calibration[6])
1246 {
1247         struct matrix scale,
1248                       translate,
1249                       transform;
1250         double sx, sy;
1251
1252         matrix_from_farray6(&transform, calibration);
1253         device->abs.apply_calibration = !matrix_is_identity(&transform);
1254
1255         if (!device->abs.apply_calibration) {
1256                 matrix_init_identity(&device->abs.calibration);
1257                 return;
1258         }
1259
1260         sx = device->abs.absinfo_x->maximum - device->abs.absinfo_x->minimum + 1;
1261         sy = device->abs.absinfo_y->maximum - device->abs.absinfo_y->minimum + 1;
1262
1263         /* The transformation matrix is in the form:
1264          *  [ a b c ]
1265          *  [ d e f ]
1266          *  [ 0 0 1 ]
1267          * Where a, e are the scale components, a, b, d, e are the rotation
1268          * component (combined with scale) and c and f are the translation
1269          * component. The translation component in the input matrix must be
1270          * normalized to multiples of the device width and height,
1271          * respectively. e.g. c == 1 shifts one device-width to the right.
1272          *
1273          * We pre-calculate a single matrix to apply to event coordinates:
1274          *     M = Un-Normalize * Calibration * Normalize
1275          *
1276          * Normalize: scales the device coordinates to [0,1]
1277          * Calibration: user-supplied matrix
1278          * Un-Normalize: scales back up to device coordinates
1279          * Matrix maths requires the normalize/un-normalize in reverse
1280          * order.
1281          */
1282
1283         /* back up the user matrix so we can return it on request */
1284         matrix_from_farray6(&device->abs.usermatrix, calibration);
1285
1286         /* Un-Normalize */
1287         matrix_init_translate(&translate,
1288                               device->abs.absinfo_x->minimum,
1289                               device->abs.absinfo_y->minimum);
1290         matrix_init_scale(&scale, sx, sy);
1291         matrix_mult(&scale, &translate, &scale);
1292
1293         /* Calibration */
1294         matrix_mult(&transform, &scale, &transform);
1295
1296         /* Normalize */
1297         matrix_init_translate(&translate,
1298                               -device->abs.absinfo_x->minimum/sx,
1299                               -device->abs.absinfo_y->minimum/sy);
1300         matrix_init_scale(&scale, 1.0/sx, 1.0/sy);
1301         matrix_mult(&scale, &translate, &scale);
1302
1303         /* store final matrix in device */
1304         matrix_mult(&device->abs.calibration, &transform, &scale);
1305 }
1306
1307 int
1308 evdev_device_has_capability(struct evdev_device *device,
1309                             enum libinput_device_capability capability)
1310 {
1311         switch (capability) {
1312         case LIBINPUT_DEVICE_CAP_POINTER:
1313                 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
1314         case LIBINPUT_DEVICE_CAP_KEYBOARD:
1315                 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
1316         case LIBINPUT_DEVICE_CAP_TOUCH:
1317                 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
1318         default:
1319                 return 0;
1320         }
1321 }
1322
1323 int
1324 evdev_device_get_size(struct evdev_device *device,
1325                       double *width,
1326                       double *height)
1327 {
1328         const struct input_absinfo *x, *y;
1329
1330         x = libevdev_get_abs_info(device->evdev, ABS_X);
1331         y = libevdev_get_abs_info(device->evdev, ABS_Y);
1332
1333         if (!x || !y || device->abs.fake_resolution ||
1334             !x->resolution || !y->resolution)
1335                 return -1;
1336
1337         *width = evdev_convert_to_mm(x, x->maximum);
1338         *height = evdev_convert_to_mm(y, y->maximum);
1339
1340         return 0;
1341 }
1342
1343 void
1344 evdev_post_scroll(struct evdev_device *device,
1345                   uint64_t time,
1346                   double dx,
1347                   double dy)
1348 {
1349         if (dy <= -device->scroll.threshold || dy >= device->scroll.threshold)
1350                 device->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1351
1352         if (dx <= -device->scroll.threshold || dx >= device->scroll.threshold)
1353                 device->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
1354
1355         if (dy != 0.0 &&
1356             (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))) {
1357                 pointer_notify_axis(&device->base,
1358                                     time,
1359                                     LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
1360                                     dy);
1361         }
1362
1363         if (dx != 0.0 &&
1364             (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))) {
1365                 pointer_notify_axis(&device->base,
1366                                     time,
1367                                     LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
1368                                     dx);
1369         }
1370 }
1371
1372 void
1373 evdev_stop_scroll(struct evdev_device *device, uint64_t time)
1374 {
1375         /* terminate scrolling with a zero scroll event */
1376         if (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
1377                 pointer_notify_axis(&device->base,
1378                                     time,
1379                                     LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
1380                                     0);
1381         if (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
1382                 pointer_notify_axis(&device->base,
1383                                     time,
1384                                     LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
1385                                     0);
1386
1387         device->scroll.direction = 0;
1388 }
1389
1390 static void
1391 release_pressed_keys(struct evdev_device *device)
1392 {
1393         struct libinput *libinput = device->base.seat->libinput;
1394         uint64_t time;
1395         int code;
1396
1397         if ((time = libinput_now(libinput)) == 0)
1398                 return;
1399
1400         for (code = 0; code < KEY_CNT; code++) {
1401                 int count = get_key_down_count(device, code);
1402
1403                 if (count > 1) {
1404                         log_bug_libinput(libinput,
1405                                          "Key %d is down %d times.\n",
1406                                          code,
1407                                          count);
1408                 }
1409
1410                 while (get_key_down_count(device, code) > 0) {
1411                         switch (get_key_type(code)) {
1412                         case EVDEV_KEY_TYPE_NONE:
1413                                 break;
1414                         case EVDEV_KEY_TYPE_KEY:
1415                                 evdev_keyboard_notify_key(
1416                                         device,
1417                                         time,
1418                                         code,
1419                                         LIBINPUT_KEY_STATE_RELEASED);
1420                                 break;
1421                         case EVDEV_KEY_TYPE_BUTTON:
1422                                 evdev_pointer_notify_button(
1423                                         device,
1424                                         time,
1425                                         code,
1426                                         LIBINPUT_BUTTON_STATE_RELEASED);
1427                                 break;
1428                         }
1429                 }
1430         }
1431 }
1432
1433 void
1434 evdev_notify_suspended_device(struct evdev_device *device)
1435 {
1436         struct libinput_device *it;
1437
1438         if (device->suspended)
1439                 return;
1440
1441         list_for_each(it, &device->base.seat->devices_list, link) {
1442                 struct evdev_device *d = (struct evdev_device*)it;
1443                 if (it == &device->base)
1444                         continue;
1445
1446                 if (d->dispatch->interface->device_suspended)
1447                         d->dispatch->interface->device_suspended(d, device);
1448         }
1449
1450         device->suspended = 1;
1451 }
1452
1453 void
1454 evdev_notify_resumed_device(struct evdev_device *device)
1455 {
1456         struct libinput_device *it;
1457
1458         if (!device->suspended)
1459                 return;
1460
1461         list_for_each(it, &device->base.seat->devices_list, link) {
1462                 struct evdev_device *d = (struct evdev_device*)it;
1463                 if (it == &device->base)
1464                         continue;
1465
1466                 if (d->dispatch->interface->device_resumed)
1467                         d->dispatch->interface->device_resumed(d, device);
1468         }
1469
1470         device->suspended = 0;
1471 }
1472
1473 int
1474 evdev_device_suspend(struct evdev_device *device)
1475 {
1476         evdev_notify_suspended_device(device);
1477
1478         if (device->source) {
1479                 libinput_remove_source(device->base.seat->libinput,
1480                                        device->source);
1481                 device->source = NULL;
1482         }
1483
1484         release_pressed_keys(device);
1485
1486         if (device->mtdev) {
1487                 mtdev_close_delete(device->mtdev);
1488                 device->mtdev = NULL;
1489         }
1490
1491         if (device->fd != -1) {
1492                 close_restricted(device->base.seat->libinput, device->fd);
1493                 device->fd = -1;
1494         }
1495
1496         return 0;
1497 }
1498
1499 static int
1500 evdev_device_compare_syspath(struct evdev_device *device, int fd)
1501 {
1502         struct udev *udev = NULL;
1503         struct udev_device *udev_device = NULL;
1504         const char *syspath;
1505         struct stat st;
1506         int rc = 1;
1507
1508         udev = udev_new();
1509         if (!udev)
1510                 goto out;
1511
1512         if (fstat(fd, &st) < 0)
1513                 goto out;
1514
1515         udev_device = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
1516         if (!device)
1517                 goto out;
1518
1519         syspath = udev_device_get_syspath(udev_device);
1520         rc = strcmp(syspath, device->syspath);
1521 out:
1522         if (udev_device)
1523                 udev_device_unref(udev_device);
1524         if (udev)
1525                 udev_unref(udev);
1526         return rc;
1527 }
1528
1529 int
1530 evdev_device_resume(struct evdev_device *device)
1531 {
1532         struct libinput *libinput = device->base.seat->libinput;
1533         int fd;
1534
1535         if (device->fd != -1)
1536                 return 0;
1537
1538         if (device->syspath == NULL)
1539                 return -ENODEV;
1540
1541         fd = open_restricted(libinput, device->devnode, O_RDWR | O_NONBLOCK);
1542
1543         if (fd < 0)
1544                 return -errno;
1545
1546         if (evdev_device_compare_syspath(device, fd)) {
1547                 close_restricted(libinput, fd);
1548                 return -ENODEV;
1549         }
1550
1551         device->fd = fd;
1552
1553         if (evdev_need_mtdev(device)) {
1554                 device->mtdev = mtdev_new_open(device->fd);
1555                 if (!device->mtdev)
1556                         return -ENODEV;
1557         }
1558
1559         device->source =
1560                 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1561         if (!device->source) {
1562                 mtdev_close_delete(device->mtdev);
1563                 return -ENOMEM;
1564         }
1565
1566         memset(device->hw_key_mask, 0, sizeof(device->hw_key_mask));
1567
1568         evdev_notify_resumed_device(device);
1569
1570         return 0;
1571 }
1572
1573 void
1574 evdev_device_remove(struct evdev_device *device)
1575 {
1576         struct libinput_device *dev;
1577
1578         list_for_each(dev, &device->base.seat->devices_list, link) {
1579                 struct evdev_device *d = (struct evdev_device*)dev;;
1580                 if (dev == &device->base)
1581                         continue;
1582
1583                 if (d->dispatch->interface->device_removed)
1584                         d->dispatch->interface->device_removed(d, device);
1585         }
1586
1587         evdev_device_suspend(device);
1588
1589         /* A device may be removed while suspended. Free the syspath to
1590          * skip re-opening a different device with the same node */
1591         free(device->syspath);
1592         device->syspath = NULL;
1593
1594         list_remove(&device->base.link);
1595
1596         notify_removed_device(&device->base);
1597         libinput_device_unref(&device->base);
1598 }
1599
1600 void
1601 evdev_device_destroy(struct evdev_device *device)
1602 {
1603         struct evdev_dispatch *dispatch;
1604
1605         dispatch = device->dispatch;
1606         if (dispatch)
1607                 dispatch->interface->destroy(dispatch);
1608
1609         filter_destroy(device->pointer.filter);
1610         libinput_seat_unref(device->base.seat);
1611         libevdev_free(device->evdev);
1612         free(device->mt.slots);
1613         free(device->devnode);
1614         free(device->sysname);
1615         free(device->syspath);
1616         free(device);
1617 }