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