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