Fix documentation for libinput_log_set_handler
[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                 switch (e->value) {
553                 case -1:
554                         /* Scroll left */
555                 case 1:
556                         /* Scroll right */
557                         pointer_notify_axis(
558                                 base,
559                                 time,
560                                 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
561                                 e->value * DEFAULT_AXIS_STEP_DISTANCE);
562                         break;
563                 default:
564                         break;
565
566                 }
567         }
568 }
569
570 static inline void
571 evdev_process_absolute(struct evdev_device *device,
572                        struct input_event *e,
573                        uint64_t time)
574 {
575         if (device->is_mt) {
576                 evdev_process_touch(device, e, time);
577         } else {
578                 evdev_process_absolute_motion(device, e);
579         }
580 }
581
582 static inline bool
583 evdev_need_touch_frame(struct evdev_device *device)
584 {
585         if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
586                 return false;
587
588         switch (device->pending_event) {
589         case EVDEV_NONE:
590         case EVDEV_RELATIVE_MOTION:
591                 break;
592         case EVDEV_ABSOLUTE_MT_DOWN:
593         case EVDEV_ABSOLUTE_MT_MOTION:
594         case EVDEV_ABSOLUTE_MT_UP:
595         case EVDEV_ABSOLUTE_TOUCH_DOWN:
596         case EVDEV_ABSOLUTE_TOUCH_UP:
597         case EVDEV_ABSOLUTE_MOTION:
598                 return true;
599         }
600
601         return false;
602 }
603
604 static void
605 evdev_tag_external_mouse(struct evdev_device *device,
606                          struct udev_device *udev_device)
607 {
608         int bustype;
609
610         bustype = libevdev_get_id_bustype(device->evdev);
611         if (bustype == BUS_USB || bustype == BUS_BLUETOOTH) {
612                 if (device->seat_caps & EVDEV_DEVICE_POINTER)
613                         device->tags |= EVDEV_TAG_EXTERNAL_MOUSE;
614         }
615 }
616
617 static void
618 evdev_tag_trackpoint(struct evdev_device *device,
619                      struct udev_device *udev_device)
620 {
621         if (libevdev_has_property(device->evdev, INPUT_PROP_POINTING_STICK))
622                 device->tags |= EVDEV_TAG_TRACKPOINT;
623 }
624
625 static void
626 fallback_process(struct evdev_dispatch *dispatch,
627                  struct evdev_device *device,
628                  struct input_event *event,
629                  uint64_t time)
630 {
631         bool need_frame = false;
632
633         switch (event->type) {
634         case EV_REL:
635                 evdev_process_relative(device, event, time);
636                 break;
637         case EV_ABS:
638                 evdev_process_absolute(device, event, time);
639                 break;
640         case EV_KEY:
641                 evdev_process_key(device, event, time);
642                 break;
643         case EV_SYN:
644                 need_frame = evdev_need_touch_frame(device);
645                 evdev_flush_pending_event(device, time);
646                 if (need_frame)
647                         touch_notify_frame(&device->base, time);
648                 break;
649         }
650 }
651
652 static void
653 fallback_destroy(struct evdev_dispatch *dispatch)
654 {
655         free(dispatch);
656 }
657
658 static void
659 fallback_tag_device(struct evdev_device *device,
660                     struct udev_device *udev_device)
661 {
662         evdev_tag_external_mouse(device, udev_device);
663         evdev_tag_trackpoint(device, udev_device);
664 }
665
666 static int
667 evdev_calibration_has_matrix(struct libinput_device *libinput_device)
668 {
669         struct evdev_device *device = (struct evdev_device*)libinput_device;
670
671         return device->abs.absinfo_x && device->abs.absinfo_y;
672 }
673
674 static enum libinput_config_status
675 evdev_calibration_set_matrix(struct libinput_device *libinput_device,
676                              const float matrix[6])
677 {
678         struct evdev_device *device = (struct evdev_device*)libinput_device;
679
680         evdev_device_calibrate(device, matrix);
681
682         return LIBINPUT_CONFIG_STATUS_SUCCESS;
683 }
684
685 static int
686 evdev_calibration_get_matrix(struct libinput_device *libinput_device,
687                              float matrix[6])
688 {
689         struct evdev_device *device = (struct evdev_device*)libinput_device;
690
691         matrix_to_farray6(&device->abs.usermatrix, matrix);
692
693         return !matrix_is_identity(&device->abs.usermatrix);
694 }
695
696 static int
697 evdev_calibration_get_default_matrix(struct libinput_device *libinput_device,
698                                      float matrix[6])
699 {
700         struct evdev_device *device = (struct evdev_device*)libinput_device;
701
702         matrix_to_farray6(&device->abs.default_calibration, matrix);
703
704         return !matrix_is_identity(&device->abs.default_calibration);
705 }
706
707 struct evdev_dispatch_interface fallback_interface = {
708         fallback_process,
709         fallback_destroy,
710         NULL, /* device_added */
711         NULL, /* device_removed */
712         NULL, /* device_suspended */
713         NULL, /* device_resumed */
714         fallback_tag_device,
715 };
716
717 static uint32_t
718 evdev_sendevents_get_modes(struct libinput_device *device)
719 {
720         return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED |
721                LIBINPUT_CONFIG_SEND_EVENTS_DISABLED;
722 }
723
724 static enum libinput_config_status
725 evdev_sendevents_set_mode(struct libinput_device *device,
726                           enum libinput_config_send_events_mode mode)
727 {
728         struct evdev_device *evdev = (struct evdev_device*)device;
729         struct evdev_dispatch *dispatch = evdev->dispatch;
730
731         if (mode == dispatch->sendevents.current_mode)
732                 return LIBINPUT_CONFIG_STATUS_SUCCESS;
733
734         switch(mode) {
735         case LIBINPUT_CONFIG_SEND_EVENTS_ENABLED:
736                 evdev_device_resume(evdev);
737                 break;
738         case LIBINPUT_CONFIG_SEND_EVENTS_DISABLED:
739                 evdev_device_suspend(evdev);
740                 break;
741         default:
742                 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
743         }
744
745         dispatch->sendevents.current_mode = mode;
746
747         return LIBINPUT_CONFIG_STATUS_SUCCESS;
748 }
749
750 static enum libinput_config_send_events_mode
751 evdev_sendevents_get_mode(struct libinput_device *device)
752 {
753         struct evdev_device *evdev = (struct evdev_device*)device;
754         struct evdev_dispatch *dispatch = evdev->dispatch;
755
756         return dispatch->sendevents.current_mode;
757 }
758
759 static enum libinput_config_send_events_mode
760 evdev_sendevents_get_default_mode(struct libinput_device *device)
761 {
762         return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
763 }
764
765 static int
766 evdev_left_handed_has(struct libinput_device *device)
767 {
768         /* This is only hooked up when we have left-handed configuration, so we
769          * can hardcode 1 here */
770         return 1;
771 }
772
773 static void
774 evdev_change_to_left_handed(struct evdev_device *device)
775 {
776         unsigned int button;
777
778         if (device->buttons.want_left_handed == device->buttons.left_handed)
779                 return;
780
781         for (button = BTN_LEFT; button < BTN_JOYSTICK; button++) {
782                 if (libevdev_has_event_code(device->evdev, EV_KEY, button) &&
783                     hw_is_key_down(device, button))
784                         return;
785         }
786
787         device->buttons.left_handed = device->buttons.want_left_handed;
788 }
789
790 static enum libinput_config_status
791 evdev_left_handed_set(struct libinput_device *device, int left_handed)
792 {
793         struct evdev_device *evdev_device = (struct evdev_device *)device;
794
795         evdev_device->buttons.want_left_handed = left_handed ? true : false;
796
797         evdev_device->buttons.change_to_left_handed(evdev_device);
798
799         return LIBINPUT_CONFIG_STATUS_SUCCESS;
800 }
801
802 static int
803 evdev_left_handed_get(struct libinput_device *device)
804 {
805         struct evdev_device *evdev_device = (struct evdev_device *)device;
806
807         /* return the wanted configuration, even if it hasn't taken
808          * effect yet! */
809         return evdev_device->buttons.want_left_handed;
810 }
811
812 static int
813 evdev_left_handed_get_default(struct libinput_device *device)
814 {
815         return 0;
816 }
817
818 int
819 evdev_init_left_handed(struct evdev_device *device,
820                        void (*change_to_left_handed)(struct evdev_device *))
821 {
822         device->buttons.config_left_handed.has = evdev_left_handed_has;
823         device->buttons.config_left_handed.set = evdev_left_handed_set;
824         device->buttons.config_left_handed.get = evdev_left_handed_get;
825         device->buttons.config_left_handed.get_default = evdev_left_handed_get_default;
826         device->base.config.left_handed = &device->buttons.config_left_handed;
827         device->buttons.left_handed = false;
828         device->buttons.want_left_handed = false;
829         device->buttons.change_to_left_handed = change_to_left_handed;
830
831         return 0;
832 }
833
834 static struct evdev_dispatch *
835 fallback_dispatch_create(struct libinput_device *device)
836 {
837         struct evdev_dispatch *dispatch = zalloc(sizeof *dispatch);
838         struct evdev_device *evdev_device = (struct evdev_device *)device;
839
840         if (dispatch == NULL)
841                 return NULL;
842
843         dispatch->interface = &fallback_interface;
844
845         if (evdev_device->buttons.want_left_handed &&
846             evdev_init_left_handed(evdev_device,
847                                    evdev_change_to_left_handed) == -1) {
848                 free(dispatch);
849                 return NULL;
850         }
851
852         device->config.calibration = &dispatch->calibration;
853
854         dispatch->calibration.has_matrix = evdev_calibration_has_matrix;
855         dispatch->calibration.set_matrix = evdev_calibration_set_matrix;
856         dispatch->calibration.get_matrix = evdev_calibration_get_matrix;
857         dispatch->calibration.get_default_matrix = evdev_calibration_get_default_matrix;
858
859         device->config.sendevents = &dispatch->sendevents.config;
860
861         dispatch->sendevents.current_mode = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
862         dispatch->sendevents.config.get_modes = evdev_sendevents_get_modes;
863         dispatch->sendevents.config.set_mode = evdev_sendevents_set_mode;
864         dispatch->sendevents.config.get_mode = evdev_sendevents_get_mode;
865         dispatch->sendevents.config.get_default_mode = evdev_sendevents_get_default_mode;
866
867         return dispatch;
868 }
869
870 static inline void
871 evdev_process_event(struct evdev_device *device, struct input_event *e)
872 {
873         struct evdev_dispatch *dispatch = device->dispatch;
874         uint64_t time = e->time.tv_sec * 1000ULL + e->time.tv_usec / 1000;
875
876         dispatch->interface->process(dispatch, device, e, time);
877 }
878
879 static inline void
880 evdev_device_dispatch_one(struct evdev_device *device,
881                           struct input_event *ev)
882 {
883         if (!device->mtdev) {
884                 evdev_process_event(device, ev);
885         } else {
886                 mtdev_put_event(device->mtdev, ev);
887                 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
888                         while (!mtdev_empty(device->mtdev)) {
889                                 struct input_event e;
890                                 mtdev_get_event(device->mtdev, &e);
891                                 evdev_process_event(device, &e);
892                         }
893                 }
894         }
895 }
896
897 static int
898 evdev_sync_device(struct evdev_device *device)
899 {
900         struct input_event ev;
901         int rc;
902
903         do {
904                 rc = libevdev_next_event(device->evdev,
905                                          LIBEVDEV_READ_FLAG_SYNC, &ev);
906                 if (rc < 0)
907                         break;
908                 evdev_device_dispatch_one(device, &ev);
909         } while (rc == LIBEVDEV_READ_STATUS_SYNC);
910
911         return rc == -EAGAIN ? 0 : rc;
912 }
913
914 static void
915 evdev_device_dispatch(void *data)
916 {
917         struct evdev_device *device = data;
918         struct libinput *libinput = device->base.seat->libinput;
919         struct input_event ev;
920         int rc;
921
922         /* If the compositor is repainting, this function is called only once
923          * per frame and we have to process all the events available on the
924          * fd, otherwise there will be input lag. */
925         do {
926                 rc = libevdev_next_event(device->evdev,
927                                          LIBEVDEV_READ_FLAG_NORMAL, &ev);
928                 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
929                         if (device->syn_drops_received < 10) {
930                                 device->syn_drops_received++;
931                                 log_info(libinput, "SYN_DROPPED event from "
932                                          "\"%s\" - some input events have "
933                                          "been lost.\n", device->devname);
934                                 if (device->syn_drops_received == 10)
935                                         log_info(libinput, "No longer logging "
936                                                  "SYN_DROPPED events for "
937                                                  "\"%s\"\n", device->devname);
938                         }
939
940                         /* send one more sync event so we handle all
941                            currently pending events before we sync up
942                            to the current state */
943                         ev.code = SYN_REPORT;
944                         evdev_device_dispatch_one(device, &ev);
945
946                         rc = evdev_sync_device(device);
947                         if (rc == 0)
948                                 rc = LIBEVDEV_READ_STATUS_SUCCESS;
949                 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
950                         evdev_device_dispatch_one(device, &ev);
951                 }
952         } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
953
954         if (rc != -EAGAIN && rc != -EINTR) {
955                 libinput_remove_source(libinput, device->source);
956                 device->source = NULL;
957         }
958 }
959
960 static int
961 evdev_accel_config_available(struct libinput_device *device)
962 {
963         /* this function is only called if we set up ptraccel, so we can
964            reply with a resounding "Yes" */
965         return 1;
966 }
967
968 static enum libinput_config_status
969 evdev_accel_config_set_speed(struct libinput_device *device, double speed)
970 {
971         struct evdev_device *dev = (struct evdev_device *)device;
972
973         if (!filter_set_speed(dev->pointer.filter, speed))
974                 return LIBINPUT_CONFIG_STATUS_INVALID;
975
976         return LIBINPUT_CONFIG_STATUS_SUCCESS;
977 }
978
979 static double
980 evdev_accel_config_get_speed(struct libinput_device *device)
981 {
982         struct evdev_device *dev = (struct evdev_device *)device;
983
984         return filter_get_speed(dev->pointer.filter);
985 }
986
987 static double
988 evdev_accel_config_get_default_speed(struct libinput_device *device)
989 {
990         return 0.0;
991 }
992
993 int
994 evdev_device_init_pointer_acceleration(struct evdev_device *device)
995 {
996         device->pointer.filter =
997                 create_pointer_accelerator_filter(
998                         pointer_accel_profile_linear);
999         if (!device->pointer.filter)
1000                 return -1;
1001
1002         device->pointer.config.available = evdev_accel_config_available;
1003         device->pointer.config.set_speed = evdev_accel_config_set_speed;
1004         device->pointer.config.get_speed = evdev_accel_config_get_speed;
1005         device->pointer.config.get_default_speed = evdev_accel_config_get_default_speed;
1006         device->base.config.accel = &device->pointer.config;
1007
1008         return 0;
1009 }
1010
1011
1012 static inline int
1013 evdev_need_mtdev(struct evdev_device *device)
1014 {
1015         struct libevdev *evdev = device->evdev;
1016
1017         return (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
1018                 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y) &&
1019                 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT));
1020 }
1021
1022 static void
1023 evdev_tag_device(struct evdev_device *device)
1024 {
1025         struct udev *udev;
1026         struct udev_device *udev_device = NULL;
1027
1028         udev = udev_new();
1029         if (!udev)
1030                 return;
1031
1032         udev_device = udev_device_new_from_syspath(udev, device->syspath);
1033         if (udev_device) {
1034                 if (device->dispatch->interface->tag_device)
1035                         device->dispatch->interface->tag_device(device, udev_device);
1036                 udev_device_unref(udev_device);
1037         }
1038         udev_unref(udev);
1039 }
1040
1041 static int
1042 evdev_configure_device(struct evdev_device *device)
1043 {
1044         struct libinput *libinput = device->base.seat->libinput;
1045         struct libevdev *evdev = device->evdev;
1046         const struct input_absinfo *absinfo;
1047         struct input_absinfo fixed;
1048         int has_abs, has_rel, has_mt;
1049         int has_button, has_keyboard, has_touch;
1050         struct mt_slot *slots;
1051         int num_slots;
1052         int active_slot;
1053         int slot;
1054         unsigned int i;
1055
1056         has_rel = 0;
1057         has_abs = 0;
1058         has_mt = 0;
1059         has_button = 0;
1060         has_keyboard = 0;
1061         has_touch = 0;
1062
1063         if (libevdev_has_event_type(evdev, EV_ABS)) {
1064
1065                 if ((absinfo = libevdev_get_abs_info(evdev, ABS_X))) {
1066                         if (absinfo->resolution == 0) {
1067                                 fixed = *absinfo;
1068                                 fixed.resolution = 1;
1069                                 libevdev_set_abs_info(evdev, ABS_X, &fixed);
1070                                 device->abs.fake_resolution = 1;
1071                         }
1072                         device->abs.absinfo_x = absinfo;
1073                         has_abs = 1;
1074                 }
1075                 if ((absinfo = libevdev_get_abs_info(evdev, ABS_Y))) {
1076                         if (absinfo->resolution == 0) {
1077                                 fixed = *absinfo;
1078                                 fixed.resolution = 1;
1079                                 libevdev_set_abs_info(evdev, ABS_Y, &fixed);
1080                                 device->abs.fake_resolution = 1;
1081                         }
1082                         device->abs.absinfo_y = absinfo;
1083                         has_abs = 1;
1084                 }
1085                 /* We only handle the slotted Protocol B in weston.
1086                    Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
1087                    require mtdev for conversion. */
1088                 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
1089                     libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y)) {
1090                         absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
1091                         if (absinfo->resolution == 0) {
1092                                 fixed = *absinfo;
1093                                 fixed.resolution = 1;
1094                                 libevdev_set_abs_info(evdev,
1095                                                       ABS_MT_POSITION_X,
1096                                                       &fixed);
1097                                 device->abs.fake_resolution = 1;
1098                         }
1099                         device->abs.absinfo_x = absinfo;
1100                         absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
1101                         if (absinfo->resolution == 0) {
1102                                 fixed = *absinfo;
1103                                 fixed.resolution = 1;
1104                                 libevdev_set_abs_info(evdev,
1105                                                       ABS_MT_POSITION_Y,
1106                                                       &fixed);
1107                                 device->abs.fake_resolution = 1;
1108                         }
1109                         device->abs.absinfo_y = absinfo;
1110                         device->is_mt = 1;
1111                         has_touch = 1;
1112                         has_mt = 1;
1113
1114                         if (evdev_need_mtdev(device)) {
1115                                 device->mtdev = mtdev_new_open(device->fd);
1116                                 if (!device->mtdev)
1117                                         return -1;
1118
1119                                 num_slots = device->mtdev->caps.slot.maximum;
1120                                 if (device->mtdev->caps.slot.minimum < 0 ||
1121                                     num_slots <= 0)
1122                                         return -1;
1123                                 active_slot = device->mtdev->caps.slot.value;
1124                         } else {
1125                                 num_slots = libevdev_get_num_slots(device->evdev);
1126                                 active_slot = libevdev_get_current_slot(evdev);
1127                         }
1128
1129                         slots = calloc(num_slots, sizeof(struct mt_slot));
1130                         if (!slots)
1131                                 return -1;
1132
1133                         for (slot = 0; slot < num_slots; ++slot) {
1134                                 slots[slot].seat_slot = -1;
1135                                 slots[slot].x = 0;
1136                                 slots[slot].y = 0;
1137                         }
1138                         device->mt.slots = slots;
1139                         device->mt.slots_len = num_slots;
1140                         device->mt.slot = active_slot;
1141                 }
1142         }
1143
1144         if (libevdev_has_property(evdev, INPUT_PROP_POINTING_STICK)) {
1145                 libinput_timer_init(&device->scroll.timer,
1146                                     device->base.seat->libinput,
1147                                     evdev_middle_button_scroll_timeout,
1148                                     device);
1149                 device->scroll.has_middle_button_scroll = true;
1150         }
1151
1152         if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
1153             libevdev_has_event_code(evdev, EV_REL, REL_Y))
1154                 has_rel = 1;
1155
1156         if (libevdev_has_event_type(evdev, EV_KEY)) {
1157                 if (!libevdev_has_property(evdev, INPUT_PROP_DIRECT) &&
1158                     libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_FINGER) &&
1159                     !libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_PEN) &&
1160                     (has_abs || has_mt)) {
1161                         device->dispatch = evdev_mt_touchpad_create(device);
1162                         log_info(libinput,
1163                                  "input device '%s', %s is a touchpad\n",
1164                                  device->devname, device->devnode);
1165                         return device->dispatch == NULL ? -1 : 0;
1166                 }
1167
1168                 for (i = 0; i < KEY_MAX; i++) {
1169                         if (libevdev_has_event_code(evdev, EV_KEY, i)) {
1170                                 switch (get_key_type(i)) {
1171                                 case EVDEV_KEY_TYPE_NONE:
1172                                         break;
1173                                 case EVDEV_KEY_TYPE_KEY:
1174                                         has_keyboard = 1;
1175                                         break;
1176                                 case EVDEV_KEY_TYPE_BUTTON:
1177                                         has_button = 1;
1178                                         break;
1179                                 }
1180                         }
1181                 }
1182
1183                 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH))
1184                         has_touch = 1;
1185         }
1186         if (libevdev_has_event_type(evdev, EV_LED))
1187                 has_keyboard = 1;
1188
1189         if ((has_abs || has_rel) && has_button) {
1190                 if (evdev_device_init_pointer_acceleration(device) == -1)
1191                         return -1;
1192
1193                 device->seat_caps |= EVDEV_DEVICE_POINTER;
1194
1195                 log_info(libinput,
1196                          "input device '%s', %s is a pointer caps =%s%s%s\n",
1197                          device->devname, device->devnode,
1198                          has_abs ? " absolute-motion" : "",
1199                          has_rel ? " relative-motion": "",
1200                          has_button ? " button" : "");
1201
1202                 /* want left-handed config option */
1203                 device->buttons.want_left_handed = true;
1204         }
1205         if (has_keyboard) {
1206                 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
1207                 log_info(libinput,
1208                          "input device '%s', %s is a keyboard\n",
1209                          device->devname, device->devnode);
1210         }
1211         if (has_touch && !has_button) {
1212                 device->seat_caps |= EVDEV_DEVICE_TOUCH;
1213                 log_info(libinput,
1214                          "input device '%s', %s is a touch device\n",
1215                          device->devname, device->devnode);
1216         }
1217
1218         return 0;
1219 }
1220
1221 static void
1222 evdev_notify_added_device(struct evdev_device *device)
1223 {
1224         struct libinput_device *dev;
1225
1226         list_for_each(dev, &device->base.seat->devices_list, link) {
1227                 struct evdev_device *d = (struct evdev_device*)dev;
1228                 if (dev == &device->base)
1229                         continue;
1230
1231                 /* Notify existing device d about addition of device device */
1232                 if (d->dispatch->interface->device_added)
1233                         d->dispatch->interface->device_added(d, device);
1234
1235                 /* Notify new device device about existing device d */
1236                 if (device->dispatch->interface->device_added)
1237                         device->dispatch->interface->device_added(device, d);
1238
1239                 /* Notify new device device if existing device d is suspended */
1240                 if (d->suspended && device->dispatch->interface->device_suspended)
1241                         device->dispatch->interface->device_suspended(device, d);
1242         }
1243
1244         notify_added_device(&device->base);
1245 }
1246
1247 struct evdev_device *
1248 evdev_device_create(struct libinput_seat *seat,
1249                     const char *devnode,
1250                     const char *sysname,
1251                     const char *syspath)
1252 {
1253         struct libinput *libinput = seat->libinput;
1254         struct evdev_device *device;
1255         int rc;
1256         int fd;
1257         int unhandled_device = 0;
1258
1259         /* Use non-blocking mode so that we can loop on read on
1260          * evdev_device_data() until all events on the fd are
1261          * read.  mtdev_get() also expects this. */
1262         fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
1263         if (fd < 0) {
1264                 log_info(libinput,
1265                          "opening input device '%s' failed (%s).\n",
1266                          devnode, strerror(-fd));
1267                 return NULL;
1268         }
1269
1270         device = zalloc(sizeof *device);
1271         if (device == NULL)
1272                 return NULL;
1273
1274         libinput_device_init(&device->base, seat);
1275         libinput_seat_ref(seat);
1276
1277         rc = libevdev_new_from_fd(fd, &device->evdev);
1278         if (rc != 0)
1279                 goto err;
1280
1281         libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
1282
1283         device->seat_caps = 0;
1284         device->is_mt = 0;
1285         device->mtdev = NULL;
1286         device->devnode = strdup(devnode);
1287         device->sysname = strdup(sysname);
1288         device->syspath = strdup(syspath);
1289         device->rel.dx = 0;
1290         device->rel.dy = 0;
1291         device->abs.seat_slot = -1;
1292         device->dispatch = NULL;
1293         device->fd = fd;
1294         device->pending_event = EVDEV_NONE;
1295         device->devname = libevdev_get_name(device->evdev);
1296         device->scroll.threshold = 5.0; /* Default may be overridden */
1297         device->scroll.direction = 0;
1298         device->dpi = DEFAULT_MOUSE_DPI;
1299
1300         matrix_init_identity(&device->abs.calibration);
1301         matrix_init_identity(&device->abs.usermatrix);
1302         matrix_init_identity(&device->abs.default_calibration);
1303
1304         if (evdev_configure_device(device) == -1)
1305                 goto err;
1306
1307         if (device->seat_caps == 0) {
1308                 unhandled_device = 1;
1309                 goto err;
1310         }
1311
1312         /* If the dispatch was not set up use the fallback. */
1313         if (device->dispatch == NULL)
1314                 device->dispatch = fallback_dispatch_create(&device->base);
1315         if (device->dispatch == NULL)
1316                 goto err;
1317
1318         device->source =
1319                 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1320         if (!device->source)
1321                 goto err;
1322
1323         list_insert(seat->devices_list.prev, &device->base.link);
1324
1325         evdev_tag_device(device);
1326         evdev_notify_added_device(device);
1327
1328         return device;
1329
1330 err:
1331         if (fd >= 0)
1332                 close_restricted(libinput, fd);
1333         evdev_device_destroy(device);
1334
1335         return unhandled_device ? EVDEV_UNHANDLED_DEVICE :  NULL;
1336 }
1337
1338 int
1339 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
1340 {
1341         memset(keys, 0, size);
1342         return 0;
1343 }
1344
1345 const char *
1346 evdev_device_get_output(struct evdev_device *device)
1347 {
1348         return device->output_name;
1349 }
1350
1351 const char *
1352 evdev_device_get_sysname(struct evdev_device *device)
1353 {
1354         return device->sysname;
1355 }
1356
1357 const char *
1358 evdev_device_get_name(struct evdev_device *device)
1359 {
1360         return device->devname;
1361 }
1362
1363 unsigned int
1364 evdev_device_get_id_product(struct evdev_device *device)
1365 {
1366         return libevdev_get_id_product(device->evdev);
1367 }
1368
1369 unsigned int
1370 evdev_device_get_id_vendor(struct evdev_device *device)
1371 {
1372         return libevdev_get_id_vendor(device->evdev);
1373 }
1374
1375 void
1376 evdev_device_set_default_calibration(struct evdev_device *device,
1377                                      const float calibration[6])
1378 {
1379         matrix_from_farray6(&device->abs.default_calibration, calibration);
1380         evdev_device_calibrate(device, calibration);
1381 }
1382
1383 void
1384 evdev_device_calibrate(struct evdev_device *device,
1385                        const float calibration[6])
1386 {
1387         struct matrix scale,
1388                       translate,
1389                       transform;
1390         double sx, sy;
1391
1392         matrix_from_farray6(&transform, calibration);
1393         device->abs.apply_calibration = !matrix_is_identity(&transform);
1394
1395         if (!device->abs.apply_calibration) {
1396                 matrix_init_identity(&device->abs.calibration);
1397                 return;
1398         }
1399
1400         sx = device->abs.absinfo_x->maximum - device->abs.absinfo_x->minimum + 1;
1401         sy = device->abs.absinfo_y->maximum - device->abs.absinfo_y->minimum + 1;
1402
1403         /* The transformation matrix is in the form:
1404          *  [ a b c ]
1405          *  [ d e f ]
1406          *  [ 0 0 1 ]
1407          * Where a, e are the scale components, a, b, d, e are the rotation
1408          * component (combined with scale) and c and f are the translation
1409          * component. The translation component in the input matrix must be
1410          * normalized to multiples of the device width and height,
1411          * respectively. e.g. c == 1 shifts one device-width to the right.
1412          *
1413          * We pre-calculate a single matrix to apply to event coordinates:
1414          *     M = Un-Normalize * Calibration * Normalize
1415          *
1416          * Normalize: scales the device coordinates to [0,1]
1417          * Calibration: user-supplied matrix
1418          * Un-Normalize: scales back up to device coordinates
1419          * Matrix maths requires the normalize/un-normalize in reverse
1420          * order.
1421          */
1422
1423         /* back up the user matrix so we can return it on request */
1424         matrix_from_farray6(&device->abs.usermatrix, calibration);
1425
1426         /* Un-Normalize */
1427         matrix_init_translate(&translate,
1428                               device->abs.absinfo_x->minimum,
1429                               device->abs.absinfo_y->minimum);
1430         matrix_init_scale(&scale, sx, sy);
1431         matrix_mult(&scale, &translate, &scale);
1432
1433         /* Calibration */
1434         matrix_mult(&transform, &scale, &transform);
1435
1436         /* Normalize */
1437         matrix_init_translate(&translate,
1438                               -device->abs.absinfo_x->minimum/sx,
1439                               -device->abs.absinfo_y->minimum/sy);
1440         matrix_init_scale(&scale, 1.0/sx, 1.0/sy);
1441         matrix_mult(&scale, &translate, &scale);
1442
1443         /* store final matrix in device */
1444         matrix_mult(&device->abs.calibration, &transform, &scale);
1445 }
1446
1447 int
1448 evdev_device_has_capability(struct evdev_device *device,
1449                             enum libinput_device_capability capability)
1450 {
1451         switch (capability) {
1452         case LIBINPUT_DEVICE_CAP_POINTER:
1453                 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
1454         case LIBINPUT_DEVICE_CAP_KEYBOARD:
1455                 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
1456         case LIBINPUT_DEVICE_CAP_TOUCH:
1457                 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
1458         default:
1459                 return 0;
1460         }
1461 }
1462
1463 int
1464 evdev_device_get_size(struct evdev_device *device,
1465                       double *width,
1466                       double *height)
1467 {
1468         const struct input_absinfo *x, *y;
1469
1470         x = libevdev_get_abs_info(device->evdev, ABS_X);
1471         y = libevdev_get_abs_info(device->evdev, ABS_Y);
1472
1473         if (!x || !y || device->abs.fake_resolution ||
1474             !x->resolution || !y->resolution)
1475                 return -1;
1476
1477         *width = evdev_convert_to_mm(x, x->maximum);
1478         *height = evdev_convert_to_mm(y, y->maximum);
1479
1480         return 0;
1481 }
1482
1483 void
1484 evdev_post_scroll(struct evdev_device *device,
1485                   uint64_t time,
1486                   double dx,
1487                   double dy)
1488 {
1489         if (dy <= -device->scroll.threshold || dy >= device->scroll.threshold)
1490                 device->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
1491
1492         if (dx <= -device->scroll.threshold || dx >= device->scroll.threshold)
1493                 device->scroll.direction |= (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
1494
1495         if (dy != 0.0 &&
1496             (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))) {
1497                 pointer_notify_axis(&device->base,
1498                                     time,
1499                                     LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
1500                                     dy);
1501         }
1502
1503         if (dx != 0.0 &&
1504             (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))) {
1505                 pointer_notify_axis(&device->base,
1506                                     time,
1507                                     LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
1508                                     dx);
1509         }
1510 }
1511
1512 void
1513 evdev_stop_scroll(struct evdev_device *device, uint64_t time)
1514 {
1515         /* terminate scrolling with a zero scroll event */
1516         if (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
1517                 pointer_notify_axis(&device->base,
1518                                     time,
1519                                     LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
1520                                     0);
1521         if (device->scroll.direction & (1 << LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
1522                 pointer_notify_axis(&device->base,
1523                                     time,
1524                                     LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
1525                                     0);
1526
1527         device->scroll.direction = 0;
1528 }
1529
1530 static void
1531 release_pressed_keys(struct evdev_device *device)
1532 {
1533         struct libinput *libinput = device->base.seat->libinput;
1534         uint64_t time;
1535         int code;
1536
1537         if ((time = libinput_now(libinput)) == 0)
1538                 return;
1539
1540         for (code = 0; code < KEY_CNT; code++) {
1541                 int count = get_key_down_count(device, code);
1542
1543                 if (count > 1) {
1544                         log_bug_libinput(libinput,
1545                                          "Key %d is down %d times.\n",
1546                                          code,
1547                                          count);
1548                 }
1549
1550                 while (get_key_down_count(device, code) > 0) {
1551                         switch (get_key_type(code)) {
1552                         case EVDEV_KEY_TYPE_NONE:
1553                                 break;
1554                         case EVDEV_KEY_TYPE_KEY:
1555                                 evdev_keyboard_notify_key(
1556                                         device,
1557                                         time,
1558                                         code,
1559                                         LIBINPUT_KEY_STATE_RELEASED);
1560                                 break;
1561                         case EVDEV_KEY_TYPE_BUTTON:
1562                                 evdev_pointer_notify_button(
1563                                         device,
1564                                         time,
1565                                         evdev_to_left_handed(device, code),
1566                                         LIBINPUT_BUTTON_STATE_RELEASED);
1567                                 break;
1568                         }
1569                 }
1570         }
1571 }
1572
1573 void
1574 evdev_notify_suspended_device(struct evdev_device *device)
1575 {
1576         struct libinput_device *it;
1577
1578         if (device->suspended)
1579                 return;
1580
1581         list_for_each(it, &device->base.seat->devices_list, link) {
1582                 struct evdev_device *d = (struct evdev_device*)it;
1583                 if (it == &device->base)
1584                         continue;
1585
1586                 if (d->dispatch->interface->device_suspended)
1587                         d->dispatch->interface->device_suspended(d, device);
1588         }
1589
1590         device->suspended = 1;
1591 }
1592
1593 void
1594 evdev_notify_resumed_device(struct evdev_device *device)
1595 {
1596         struct libinput_device *it;
1597
1598         if (!device->suspended)
1599                 return;
1600
1601         list_for_each(it, &device->base.seat->devices_list, link) {
1602                 struct evdev_device *d = (struct evdev_device*)it;
1603                 if (it == &device->base)
1604                         continue;
1605
1606                 if (d->dispatch->interface->device_resumed)
1607                         d->dispatch->interface->device_resumed(d, device);
1608         }
1609
1610         device->suspended = 0;
1611 }
1612
1613 int
1614 evdev_device_suspend(struct evdev_device *device)
1615 {
1616         evdev_notify_suspended_device(device);
1617
1618         if (device->source) {
1619                 libinput_remove_source(device->base.seat->libinput,
1620                                        device->source);
1621                 device->source = NULL;
1622         }
1623
1624         release_pressed_keys(device);
1625
1626         if (device->mtdev) {
1627                 mtdev_close_delete(device->mtdev);
1628                 device->mtdev = NULL;
1629         }
1630
1631         if (device->fd != -1) {
1632                 close_restricted(device->base.seat->libinput, device->fd);
1633                 device->fd = -1;
1634         }
1635
1636         return 0;
1637 }
1638
1639 static int
1640 evdev_device_compare_syspath(struct evdev_device *device, int fd)
1641 {
1642         struct udev *udev = NULL;
1643         struct udev_device *udev_device = NULL;
1644         const char *syspath;
1645         struct stat st;
1646         int rc = 1;
1647
1648         udev = udev_new();
1649         if (!udev)
1650                 goto out;
1651
1652         if (fstat(fd, &st) < 0)
1653                 goto out;
1654
1655         udev_device = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
1656         if (!device)
1657                 goto out;
1658
1659         syspath = udev_device_get_syspath(udev_device);
1660         rc = strcmp(syspath, device->syspath);
1661 out:
1662         if (udev_device)
1663                 udev_device_unref(udev_device);
1664         if (udev)
1665                 udev_unref(udev);
1666         return rc;
1667 }
1668
1669 int
1670 evdev_device_resume(struct evdev_device *device)
1671 {
1672         struct libinput *libinput = device->base.seat->libinput;
1673         int fd;
1674
1675         if (device->fd != -1)
1676                 return 0;
1677
1678         if (device->syspath == NULL)
1679                 return -ENODEV;
1680
1681         fd = open_restricted(libinput, device->devnode, O_RDWR | O_NONBLOCK);
1682
1683         if (fd < 0)
1684                 return -errno;
1685
1686         if (evdev_device_compare_syspath(device, fd)) {
1687                 close_restricted(libinput, fd);
1688                 return -ENODEV;
1689         }
1690
1691         device->fd = fd;
1692
1693         if (evdev_need_mtdev(device)) {
1694                 device->mtdev = mtdev_new_open(device->fd);
1695                 if (!device->mtdev)
1696                         return -ENODEV;
1697         }
1698
1699         device->source =
1700                 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1701         if (!device->source) {
1702                 mtdev_close_delete(device->mtdev);
1703                 return -ENOMEM;
1704         }
1705
1706         memset(device->hw_key_mask, 0, sizeof(device->hw_key_mask));
1707
1708         evdev_notify_resumed_device(device);
1709
1710         return 0;
1711 }
1712
1713 void
1714 evdev_device_remove(struct evdev_device *device)
1715 {
1716         struct libinput_device *dev;
1717
1718         list_for_each(dev, &device->base.seat->devices_list, link) {
1719                 struct evdev_device *d = (struct evdev_device*)dev;;
1720                 if (dev == &device->base)
1721                         continue;
1722
1723                 if (d->dispatch->interface->device_removed)
1724                         d->dispatch->interface->device_removed(d, device);
1725         }
1726
1727         evdev_device_suspend(device);
1728
1729         /* A device may be removed while suspended. Free the syspath to
1730          * skip re-opening a different device with the same node */
1731         free(device->syspath);
1732         device->syspath = NULL;
1733
1734         list_remove(&device->base.link);
1735
1736         notify_removed_device(&device->base);
1737         libinput_device_unref(&device->base);
1738 }
1739
1740 void
1741 evdev_device_destroy(struct evdev_device *device)
1742 {
1743         struct evdev_dispatch *dispatch;
1744
1745         dispatch = device->dispatch;
1746         if (dispatch)
1747                 dispatch->interface->destroy(dispatch);
1748
1749         filter_destroy(device->pointer.filter);
1750         libinput_seat_unref(device->base.seat);
1751         libevdev_free(device->evdev);
1752         free(device->mt.slots);
1753         free(device->devnode);
1754         free(device->sysname);
1755         free(device->syspath);
1756         free(device);
1757 }