evdev: merge two conditions into one with a sub condition
[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_WHEEL_CLICK_ANGLE 15
44 #define DEFAULT_MIDDLE_BUTTON_SCROLL_TIMEOUT 200
45
46 enum evdev_key_type {
47         EVDEV_KEY_TYPE_NONE,
48         EVDEV_KEY_TYPE_KEY,
49         EVDEV_KEY_TYPE_BUTTON,
50 };
51
52 enum evdev_device_udev_tags {
53         EVDEV_UDEV_TAG_INPUT = (1 << 0),
54         EVDEV_UDEV_TAG_KEYBOARD = (1 << 1),
55         EVDEV_UDEV_TAG_MOUSE = (1 << 2),
56         EVDEV_UDEV_TAG_TOUCHPAD = (1 << 3),
57         EVDEV_UDEV_TAG_TOUCHSCREEN = (1 << 4),
58         EVDEV_UDEV_TAG_TABLET = (1 << 5),
59         EVDEV_UDEV_TAG_JOYSTICK = (1 << 6),
60         EVDEV_UDEV_TAG_ACCELEROMETER = (1 << 7),
61         EVDEV_UDEV_TAG_BUTTONSET = (1 << 8),
62 };
63
64 struct evdev_udev_tag_match {
65         const char *name;
66         enum evdev_device_udev_tags tag;
67 };
68
69 static const struct evdev_udev_tag_match evdev_udev_tag_matches[] = {
70         {"ID_INPUT",                    EVDEV_UDEV_TAG_INPUT},
71         {"ID_INPUT_KEYBOARD",           EVDEV_UDEV_TAG_KEYBOARD},
72         {"ID_INPUT_KEY",                EVDEV_UDEV_TAG_KEYBOARD},
73         {"ID_INPUT_MOUSE",              EVDEV_UDEV_TAG_MOUSE},
74         {"ID_INPUT_TOUCHPAD",           EVDEV_UDEV_TAG_TOUCHPAD},
75         {"ID_INPUT_TOUCHSCREEN",        EVDEV_UDEV_TAG_TOUCHSCREEN},
76         {"ID_INPUT_TABLET",             EVDEV_UDEV_TAG_TABLET},
77         {"ID_INPUT_TABLET_PAD",         EVDEV_UDEV_TAG_BUTTONSET},
78         {"ID_INPUT_JOYSTICK",           EVDEV_UDEV_TAG_JOYSTICK},
79         {"ID_INPUT_ACCELEROMETER",      EVDEV_UDEV_TAG_ACCELEROMETER},
80
81         /* sentinel value */
82         { 0 },
83 };
84
85 static void
86 hw_set_key_down(struct evdev_device *device, int code, int pressed)
87 {
88         long_set_bit_state(device->hw_key_mask, code, pressed);
89 }
90
91 static int
92 hw_is_key_down(struct evdev_device *device, int code)
93 {
94         return long_bit_is_set(device->hw_key_mask, code);
95 }
96
97 static int
98 get_key_down_count(struct evdev_device *device, int code)
99 {
100         return device->key_count[code];
101 }
102
103 static int
104 update_key_down_count(struct evdev_device *device, int code, int pressed)
105 {
106         int key_count;
107         assert(code >= 0 && code < KEY_CNT);
108
109         if (pressed) {
110                 key_count = ++device->key_count[code];
111         } else {
112                 assert(device->key_count[code] > 0);
113                 key_count = --device->key_count[code];
114         }
115
116         if (key_count > 32) {
117                 log_bug_libinput(device->base.seat->libinput,
118                                  "Key count for %s reached abnormal values\n",
119                                  libevdev_event_code_get_name(EV_KEY, code));
120         }
121
122         return key_count;
123 }
124
125 void
126 evdev_keyboard_notify_key(struct evdev_device *device,
127                           uint32_t time,
128                           int key,
129                           enum libinput_key_state state)
130 {
131         int down_count;
132
133         down_count = update_key_down_count(device, key, state);
134
135         if ((state == LIBINPUT_KEY_STATE_PRESSED && down_count == 1) ||
136             (state == LIBINPUT_KEY_STATE_RELEASED && down_count == 0))
137                 keyboard_notify_key(&device->base, time, key, state);
138 }
139
140 void
141 evdev_pointer_notify_button(struct evdev_device *device,
142                             uint32_t time,
143                             int button,
144                             enum libinput_button_state state)
145 {
146         int down_count;
147
148         down_count = update_key_down_count(device, button, state);
149
150         if ((state == LIBINPUT_BUTTON_STATE_PRESSED && down_count == 1) ||
151             (state == LIBINPUT_BUTTON_STATE_RELEASED && down_count == 0)) {
152                 pointer_notify_button(&device->base, time, button, state);
153
154                 if (state == LIBINPUT_BUTTON_STATE_RELEASED) {
155                         if (device->left_handed.change_to_enabled)
156                                 device->left_handed.change_to_enabled(device);
157
158                         if (device->scroll.change_scroll_method)
159                                 device->scroll.change_scroll_method(device);
160                 }
161         }
162
163 }
164
165 void
166 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)
167 {
168         static const struct {
169                 enum libinput_led weston;
170                 int evdev;
171         } map[] = {
172                 { LIBINPUT_LED_NUM_LOCK, LED_NUML },
173                 { LIBINPUT_LED_CAPS_LOCK, LED_CAPSL },
174                 { LIBINPUT_LED_SCROLL_LOCK, LED_SCROLLL },
175         };
176         struct input_event ev[ARRAY_LENGTH(map) + 1];
177         unsigned int i;
178
179         if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
180                 return;
181
182         memset(ev, 0, sizeof(ev));
183         for (i = 0; i < ARRAY_LENGTH(map); i++) {
184                 ev[i].type = EV_LED;
185                 ev[i].code = map[i].evdev;
186                 ev[i].value = !!(leds & map[i].weston);
187         }
188         ev[i].type = EV_SYN;
189         ev[i].code = SYN_REPORT;
190
191         i = write(device->fd, ev, sizeof ev);
192         (void)i; /* no, we really don't care about the return value */
193 }
194
195 static void
196 transform_absolute(struct evdev_device *device,
197                    struct device_coords *point)
198 {
199         if (!device->abs.apply_calibration)
200                 return;
201
202         matrix_mult_vec(&device->abs.calibration, &point->x, &point->y);
203 }
204
205 static inline double
206 scale_axis(const struct input_absinfo *absinfo, double val, double to_range)
207 {
208         return (val - absinfo->minimum) * to_range /
209                 (absinfo->maximum - absinfo->minimum + 1);
210 }
211
212 double
213 evdev_device_transform_x(struct evdev_device *device,
214                          double x,
215                          uint32_t width)
216 {
217         return scale_axis(device->abs.absinfo_x, x, width);
218 }
219
220 double
221 evdev_device_transform_y(struct evdev_device *device,
222                          double y,
223                          uint32_t height)
224 {
225         return scale_axis(device->abs.absinfo_y, y, height);
226 }
227
228 static inline void
229 normalize_delta(struct evdev_device *device,
230                 const struct device_coords *delta,
231                 struct normalized_coords *normalized)
232 {
233         normalized->x = delta->x * DEFAULT_MOUSE_DPI / (double)device->dpi;
234         normalized->y = delta->y * DEFAULT_MOUSE_DPI / (double)device->dpi;
235 }
236
237 static void
238 evdev_flush_pending_event(struct evdev_device *device, uint64_t time)
239 {
240         struct libinput *libinput = device->base.seat->libinput;
241         int slot;
242         int seat_slot;
243         struct libinput_device *base = &device->base;
244         struct libinput_seat *seat = base->seat;
245         struct normalized_coords accel, unaccel;
246         struct device_coords point;
247
248         slot = device->mt.slot;
249
250         switch (device->pending_event) {
251         case EVDEV_NONE:
252                 return;
253         case EVDEV_RELATIVE_MOTION:
254                 normalize_delta(device, &device->rel, &unaccel);
255                 device->rel.x = 0;
256                 device->rel.y = 0;
257
258                 /* Use unaccelerated deltas for pointing stick scroll */
259                 if (device->scroll.method == LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN &&
260                     hw_is_key_down(device, device->scroll.button)) {
261                         if (device->scroll.button_scroll_active)
262                                 evdev_post_scroll(device, time,
263                                                   LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS,
264                                                   &unaccel);
265                         break;
266                 }
267
268                 /* Apply pointer acceleration. */
269                 accel = filter_dispatch(device->pointer.filter, &unaccel, device, time);
270
271                 if (normalized_is_zero(accel) && normalized_is_zero(unaccel))
272                         break;
273
274                 pointer_notify_motion(base, time, &accel, &unaccel);
275                 break;
276         case EVDEV_ABSOLUTE_MT_DOWN:
277                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
278                         break;
279
280                 if (device->mt.slots[slot].seat_slot != -1) {
281                         log_bug_kernel(libinput,
282                                        "%s: Driver sent multiple touch down for the "
283                                        "same slot",
284                                        udev_device_get_devnode(device->udev_device));
285                         break;
286                 }
287
288                 seat_slot = ffs(~seat->slot_map) - 1;
289                 device->mt.slots[slot].seat_slot = seat_slot;
290
291                 if (seat_slot == -1)
292                         break;
293
294                 seat->slot_map |= 1 << seat_slot;
295                 point = device->mt.slots[slot].point;
296                 transform_absolute(device, &point);
297
298                 touch_notify_touch_down(base, time, slot, seat_slot,
299                                         &point);
300                 break;
301         case EVDEV_ABSOLUTE_MT_MOTION:
302                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
303                         break;
304
305                 seat_slot = device->mt.slots[slot].seat_slot;
306                 point = device->mt.slots[slot].point;
307
308                 if (seat_slot == -1)
309                         break;
310
311                 transform_absolute(device, &point);
312                 touch_notify_touch_motion(base, time, slot, seat_slot,
313                                           &point);
314                 break;
315         case EVDEV_ABSOLUTE_MT_UP:
316                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
317                         break;
318
319                 seat_slot = device->mt.slots[slot].seat_slot;
320                 device->mt.slots[slot].seat_slot = -1;
321
322                 if (seat_slot == -1)
323                         break;
324
325                 seat->slot_map &= ~(1 << seat_slot);
326
327                 touch_notify_touch_up(base, time, slot, seat_slot);
328                 break;
329         case EVDEV_ABSOLUTE_TOUCH_DOWN:
330                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
331                         break;
332
333                 if (device->abs.seat_slot != -1) {
334                         log_bug_kernel(libinput,
335                                        "%s: Driver sent multiple touch down for the "
336                                        "same slot",
337                                        udev_device_get_devnode(device->udev_device));
338                         break;
339                 }
340
341                 seat_slot = ffs(~seat->slot_map) - 1;
342                 device->abs.seat_slot = seat_slot;
343
344                 if (seat_slot == -1)
345                         break;
346
347                 seat->slot_map |= 1 << seat_slot;
348
349                 point = device->abs.point;
350                 transform_absolute(device, &point);
351
352                 touch_notify_touch_down(base, time, -1, seat_slot, &point);
353                 break;
354         case EVDEV_ABSOLUTE_MOTION:
355                 point = device->abs.point;
356                 transform_absolute(device, &point);
357
358                 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
359                         seat_slot = device->abs.seat_slot;
360
361                         if (seat_slot == -1)
362                                 break;
363
364                         touch_notify_touch_motion(base, time, -1, seat_slot,
365                                                   &point);
366                 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
367                         pointer_notify_motion_absolute(base, time, &point);
368                 }
369                 break;
370         case EVDEV_ABSOLUTE_TOUCH_UP:
371                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
372                         break;
373
374                 seat_slot = device->abs.seat_slot;
375                 device->abs.seat_slot = -1;
376
377                 if (seat_slot == -1)
378                         break;
379
380                 seat->slot_map &= ~(1 << seat_slot);
381
382                 touch_notify_touch_up(base, time, -1, seat_slot);
383                 break;
384         default:
385                 assert(0 && "Unknown pending event type");
386                 break;
387         }
388
389         device->pending_event = EVDEV_NONE;
390 }
391
392 static enum evdev_key_type
393 get_key_type(uint16_t code)
394 {
395         if (code == BTN_TOUCH)
396                 return EVDEV_KEY_TYPE_NONE;
397
398         if (code >= KEY_ESC && code <= KEY_MICMUTE)
399                 return EVDEV_KEY_TYPE_KEY;
400         if (code >= BTN_MISC && code <= BTN_GEAR_UP)
401                 return EVDEV_KEY_TYPE_BUTTON;
402         if (code >= KEY_OK && code <= KEY_LIGHTS_TOGGLE)
403                 return EVDEV_KEY_TYPE_KEY;
404         if (code >= BTN_DPAD_UP && code <= BTN_TRIGGER_HAPPY40)
405                 return EVDEV_KEY_TYPE_BUTTON;
406         return EVDEV_KEY_TYPE_NONE;
407 }
408
409 static void
410 evdev_button_scroll_timeout(uint64_t time, void *data)
411 {
412         struct evdev_device *device = data;
413
414         device->scroll.button_scroll_active = true;
415 }
416
417 static void
418 evdev_button_scroll_button(struct evdev_device *device,
419                            uint64_t time, int is_press)
420 {
421         if (is_press) {
422                 libinput_timer_set(&device->scroll.timer,
423                                 time + DEFAULT_MIDDLE_BUTTON_SCROLL_TIMEOUT);
424         } else {
425                 libinput_timer_cancel(&device->scroll.timer);
426                 if (device->scroll.button_scroll_active) {
427                         evdev_stop_scroll(device, time,
428                                           LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS);
429                         device->scroll.button_scroll_active = false;
430                 } else {
431                         /* If the button is released quickly enough emit the
432                          * button press/release events. */
433                         evdev_pointer_notify_button(device, time,
434                                         device->scroll.button,
435                                         LIBINPUT_BUTTON_STATE_PRESSED);
436                         evdev_pointer_notify_button(device, time,
437                                         device->scroll.button,
438                                         LIBINPUT_BUTTON_STATE_RELEASED);
439                 }
440         }
441 }
442
443 static void
444 evdev_process_touch_button(struct evdev_device *device,
445                            uint64_t time, int value)
446 {
447         if (device->pending_event != EVDEV_NONE &&
448             device->pending_event != EVDEV_ABSOLUTE_MOTION)
449                 evdev_flush_pending_event(device, time);
450
451         device->pending_event = (value ?
452                                  EVDEV_ABSOLUTE_TOUCH_DOWN :
453                                  EVDEV_ABSOLUTE_TOUCH_UP);
454 }
455
456 static inline void
457 evdev_process_key(struct evdev_device *device,
458                   struct input_event *e, uint64_t time)
459 {
460         enum evdev_key_type type;
461
462         /* ignore kernel key repeat */
463         if (e->value == 2)
464                 return;
465
466         if (e->code == BTN_TOUCH) {
467                 if (!device->is_mt)
468                         evdev_process_touch_button(device, time, e->value);
469                 return;
470         }
471
472         evdev_flush_pending_event(device, time);
473
474         type = get_key_type(e->code);
475
476         /* Ignore key release events from the kernel for keys that libinput
477          * never got a pressed event for. */
478         if (e->value == 0) {
479                 switch (type) {
480                 case EVDEV_KEY_TYPE_NONE:
481                         break;
482                 case EVDEV_KEY_TYPE_KEY:
483                 case EVDEV_KEY_TYPE_BUTTON:
484                         if (!hw_is_key_down(device, e->code))
485                                 return;
486                 }
487         }
488
489         hw_set_key_down(device, e->code, e->value);
490
491         switch (type) {
492         case EVDEV_KEY_TYPE_NONE:
493                 break;
494         case EVDEV_KEY_TYPE_KEY:
495                 evdev_keyboard_notify_key(
496                         device,
497                         time,
498                         e->code,
499                         e->value ? LIBINPUT_KEY_STATE_PRESSED :
500                                    LIBINPUT_KEY_STATE_RELEASED);
501                 break;
502         case EVDEV_KEY_TYPE_BUTTON:
503                 if (device->scroll.method == LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN &&
504                     e->code == device->scroll.button) {
505                         evdev_button_scroll_button(device, time, e->value);
506                         break;
507                 }
508                 evdev_pointer_notify_button(
509                         device,
510                         time,
511                         evdev_to_left_handed(device, e->code),
512                         e->value ? LIBINPUT_BUTTON_STATE_PRESSED :
513                                    LIBINPUT_BUTTON_STATE_RELEASED);
514                 break;
515         }
516 }
517
518 static void
519 evdev_process_touch(struct evdev_device *device,
520                     struct input_event *e,
521                     uint64_t time)
522 {
523         switch (e->code) {
524         case ABS_MT_SLOT:
525                 if ((size_t)e->value >= device->mt.slots_len) {
526                         log_bug_libinput(device->base.seat->libinput,
527                                          "%s exceeds slots (%d of %d)\n",
528                                          device->devname,
529                                          e->value,
530                                          device->mt.slots_len);
531                         e->value = device->mt.slots_len - 1;
532                 }
533                 evdev_flush_pending_event(device, time);
534                 device->mt.slot = e->value;
535                 break;
536         case ABS_MT_TRACKING_ID:
537                 if (device->pending_event != EVDEV_NONE &&
538                     device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
539                         evdev_flush_pending_event(device, time);
540                 if (e->value >= 0)
541                         device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
542                 else
543                         device->pending_event = EVDEV_ABSOLUTE_MT_UP;
544                 break;
545         case ABS_MT_POSITION_X:
546                 device->mt.slots[device->mt.slot].point.x = e->value;
547                 if (device->pending_event == EVDEV_NONE)
548                         device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
549                 break;
550         case ABS_MT_POSITION_Y:
551                 device->mt.slots[device->mt.slot].point.y = e->value;
552                 if (device->pending_event == EVDEV_NONE)
553                         device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
554                 break;
555         }
556 }
557
558 static inline void
559 evdev_process_absolute_motion(struct evdev_device *device,
560                               struct input_event *e)
561 {
562         switch (e->code) {
563         case ABS_X:
564                 device->abs.point.x = e->value;
565                 if (device->pending_event == EVDEV_NONE)
566                         device->pending_event = EVDEV_ABSOLUTE_MOTION;
567                 break;
568         case ABS_Y:
569                 device->abs.point.y = e->value;
570                 if (device->pending_event == EVDEV_NONE)
571                         device->pending_event = EVDEV_ABSOLUTE_MOTION;
572                 break;
573         }
574 }
575
576 static void
577 evdev_notify_axis(struct evdev_device *device,
578                   uint64_t time,
579                   uint32_t axes,
580                   enum libinput_pointer_axis_source source,
581                   const struct normalized_coords *delta_in,
582                   const struct discrete_coords *discrete_in)
583 {
584         struct normalized_coords delta = *delta_in;
585         struct discrete_coords discrete = *discrete_in;
586
587         if (device->scroll.natural_scrolling_enabled) {
588                 delta.x *= -1;
589                 delta.y *= -1;
590                 discrete.x *= -1;
591                 discrete.y *= -1;
592         }
593
594         pointer_notify_axis(&device->base,
595                             time,
596                             axes,
597                             source,
598                             &delta,
599                             &discrete);
600 }
601
602 static inline void
603 evdev_process_relative(struct evdev_device *device,
604                        struct input_event *e, uint64_t time)
605 {
606         struct normalized_coords wheel_degrees = { 0.0, 0.0 };
607         struct discrete_coords discrete = { 0.0, 0.0 };
608
609         switch (e->code) {
610         case REL_X:
611                 if (device->pending_event != EVDEV_RELATIVE_MOTION)
612                         evdev_flush_pending_event(device, time);
613                 device->rel.x += e->value;
614                 device->pending_event = EVDEV_RELATIVE_MOTION;
615                 break;
616         case REL_Y:
617                 if (device->pending_event != EVDEV_RELATIVE_MOTION)
618                         evdev_flush_pending_event(device, time);
619                 device->rel.y += e->value;
620                 device->pending_event = EVDEV_RELATIVE_MOTION;
621                 break;
622         case REL_WHEEL:
623                 evdev_flush_pending_event(device, time);
624                 wheel_degrees.y = -1 * e->value *
625                                         device->scroll.wheel_click_angle;
626                 discrete.y = -1 * e->value;
627                 evdev_notify_axis(
628                         device,
629                         time,
630                         AS_MASK(LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL),
631                         LIBINPUT_POINTER_AXIS_SOURCE_WHEEL,
632                         &wheel_degrees,
633                         &discrete);
634                 break;
635         case REL_HWHEEL:
636                 evdev_flush_pending_event(device, time);
637                 wheel_degrees.x = e->value * device->scroll.wheel_click_angle;
638                 discrete.x = e->value;
639                 evdev_notify_axis(
640                         device,
641                         time,
642                         AS_MASK(LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL),
643                         LIBINPUT_POINTER_AXIS_SOURCE_WHEEL,
644                         &wheel_degrees,
645                         &discrete);
646                 break;
647         }
648 }
649
650 static inline void
651 evdev_process_absolute(struct evdev_device *device,
652                        struct input_event *e,
653                        uint64_t time)
654 {
655         if (device->is_mt) {
656                 evdev_process_touch(device, e, time);
657         } else {
658                 evdev_process_absolute_motion(device, e);
659         }
660 }
661
662 static inline bool
663 evdev_any_button_down(struct evdev_device *device)
664 {
665         unsigned int button;
666
667         for (button = BTN_LEFT; button < BTN_JOYSTICK; button++) {
668                 if (libevdev_has_event_code(device->evdev, EV_KEY, button) &&
669                     hw_is_key_down(device, button))
670                         return true;
671         }
672         return false;
673 }
674
675 static inline bool
676 evdev_need_touch_frame(struct evdev_device *device)
677 {
678         if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
679                 return false;
680
681         switch (device->pending_event) {
682         case EVDEV_NONE:
683         case EVDEV_RELATIVE_MOTION:
684                 break;
685         case EVDEV_ABSOLUTE_MT_DOWN:
686         case EVDEV_ABSOLUTE_MT_MOTION:
687         case EVDEV_ABSOLUTE_MT_UP:
688         case EVDEV_ABSOLUTE_TOUCH_DOWN:
689         case EVDEV_ABSOLUTE_TOUCH_UP:
690         case EVDEV_ABSOLUTE_MOTION:
691                 return true;
692         }
693
694         return false;
695 }
696
697 static void
698 evdev_tag_external_mouse(struct evdev_device *device,
699                          struct udev_device *udev_device)
700 {
701         int bustype;
702
703         bustype = libevdev_get_id_bustype(device->evdev);
704         if (bustype == BUS_USB || bustype == BUS_BLUETOOTH) {
705                 if (device->seat_caps & EVDEV_DEVICE_POINTER)
706                         device->tags |= EVDEV_TAG_EXTERNAL_MOUSE;
707         }
708 }
709
710 static void
711 evdev_tag_trackpoint(struct evdev_device *device,
712                      struct udev_device *udev_device)
713 {
714         if (libevdev_has_property(device->evdev, INPUT_PROP_POINTING_STICK))
715                 device->tags |= EVDEV_TAG_TRACKPOINT;
716 }
717
718 static void
719 fallback_process(struct evdev_dispatch *dispatch,
720                  struct evdev_device *device,
721                  struct input_event *event,
722                  uint64_t time)
723 {
724         bool need_frame = false;
725
726         switch (event->type) {
727         case EV_REL:
728                 evdev_process_relative(device, event, time);
729                 break;
730         case EV_ABS:
731                 evdev_process_absolute(device, event, time);
732                 break;
733         case EV_KEY:
734                 evdev_process_key(device, event, time);
735                 break;
736         case EV_SYN:
737                 need_frame = evdev_need_touch_frame(device);
738                 evdev_flush_pending_event(device, time);
739                 if (need_frame)
740                         touch_notify_frame(&device->base, time);
741                 break;
742         }
743 }
744
745 static void
746 fallback_destroy(struct evdev_dispatch *dispatch)
747 {
748         free(dispatch);
749 }
750
751 static void
752 fallback_tag_device(struct evdev_device *device,
753                     struct udev_device *udev_device)
754 {
755         evdev_tag_external_mouse(device, udev_device);
756         evdev_tag_trackpoint(device, udev_device);
757 }
758
759 static int
760 evdev_calibration_has_matrix(struct libinput_device *libinput_device)
761 {
762         struct evdev_device *device = (struct evdev_device*)libinput_device;
763
764         return device->abs.absinfo_x && device->abs.absinfo_y;
765 }
766
767 static enum libinput_config_status
768 evdev_calibration_set_matrix(struct libinput_device *libinput_device,
769                              const float matrix[6])
770 {
771         struct evdev_device *device = (struct evdev_device*)libinput_device;
772
773         evdev_device_calibrate(device, matrix);
774
775         return LIBINPUT_CONFIG_STATUS_SUCCESS;
776 }
777
778 static int
779 evdev_calibration_get_matrix(struct libinput_device *libinput_device,
780                              float matrix[6])
781 {
782         struct evdev_device *device = (struct evdev_device*)libinput_device;
783
784         matrix_to_farray6(&device->abs.usermatrix, matrix);
785
786         return !matrix_is_identity(&device->abs.usermatrix);
787 }
788
789 static int
790 evdev_calibration_get_default_matrix(struct libinput_device *libinput_device,
791                                      float matrix[6])
792 {
793         struct evdev_device *device = (struct evdev_device*)libinput_device;
794
795         matrix_to_farray6(&device->abs.default_calibration, matrix);
796
797         return !matrix_is_identity(&device->abs.default_calibration);
798 }
799
800 struct evdev_dispatch_interface fallback_interface = {
801         fallback_process,
802         NULL, /* remove */
803         fallback_destroy,
804         NULL, /* device_added */
805         NULL, /* device_removed */
806         NULL, /* device_suspended */
807         NULL, /* device_resumed */
808         fallback_tag_device,
809 };
810
811 static uint32_t
812 evdev_sendevents_get_modes(struct libinput_device *device)
813 {
814         return LIBINPUT_CONFIG_SEND_EVENTS_DISABLED;
815 }
816
817 static enum libinput_config_status
818 evdev_sendevents_set_mode(struct libinput_device *device,
819                           enum libinput_config_send_events_mode mode)
820 {
821         struct evdev_device *evdev = (struct evdev_device*)device;
822         struct evdev_dispatch *dispatch = evdev->dispatch;
823
824         if (mode == dispatch->sendevents.current_mode)
825                 return LIBINPUT_CONFIG_STATUS_SUCCESS;
826
827         switch(mode) {
828         case LIBINPUT_CONFIG_SEND_EVENTS_ENABLED:
829                 evdev_device_resume(evdev);
830                 break;
831         case LIBINPUT_CONFIG_SEND_EVENTS_DISABLED:
832                 evdev_device_suspend(evdev);
833                 break;
834         default: /* no support for combined modes yet */
835                 return LIBINPUT_CONFIG_STATUS_UNSUPPORTED;
836         }
837
838         dispatch->sendevents.current_mode = mode;
839
840         return LIBINPUT_CONFIG_STATUS_SUCCESS;
841 }
842
843 static enum libinput_config_send_events_mode
844 evdev_sendevents_get_mode(struct libinput_device *device)
845 {
846         struct evdev_device *evdev = (struct evdev_device*)device;
847         struct evdev_dispatch *dispatch = evdev->dispatch;
848
849         return dispatch->sendevents.current_mode;
850 }
851
852 static enum libinput_config_send_events_mode
853 evdev_sendevents_get_default_mode(struct libinput_device *device)
854 {
855         return LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
856 }
857
858 static int
859 evdev_left_handed_has(struct libinput_device *device)
860 {
861         /* This is only hooked up when we have left-handed configuration, so we
862          * can hardcode 1 here */
863         return 1;
864 }
865
866 static void
867 evdev_change_to_left_handed(struct evdev_device *device)
868 {
869         if (device->left_handed.want_enabled == device->left_handed.enabled)
870                 return;
871
872         if (evdev_any_button_down(device))
873                 return;
874
875         device->left_handed.enabled = device->left_handed.want_enabled;
876 }
877
878 static enum libinput_config_status
879 evdev_left_handed_set(struct libinput_device *device, int left_handed)
880 {
881         struct evdev_device *evdev_device = (struct evdev_device *)device;
882
883         evdev_device->left_handed.want_enabled = left_handed ? true : false;
884
885         evdev_device->left_handed.change_to_enabled(evdev_device);
886
887         return LIBINPUT_CONFIG_STATUS_SUCCESS;
888 }
889
890 static int
891 evdev_left_handed_get(struct libinput_device *device)
892 {
893         struct evdev_device *evdev_device = (struct evdev_device *)device;
894
895         /* return the wanted configuration, even if it hasn't taken
896          * effect yet! */
897         return evdev_device->left_handed.want_enabled;
898 }
899
900 static int
901 evdev_left_handed_get_default(struct libinput_device *device)
902 {
903         return 0;
904 }
905
906 int
907 evdev_init_left_handed(struct evdev_device *device,
908                        void (*change_to_left_handed)(struct evdev_device *))
909 {
910         device->left_handed.config.has = evdev_left_handed_has;
911         device->left_handed.config.set = evdev_left_handed_set;
912         device->left_handed.config.get = evdev_left_handed_get;
913         device->left_handed.config.get_default = evdev_left_handed_get_default;
914         device->base.config.left_handed = &device->left_handed.config;
915         device->left_handed.enabled = false;
916         device->left_handed.want_enabled = false;
917         device->left_handed.change_to_enabled = change_to_left_handed;
918
919         return 0;
920 }
921
922 static uint32_t
923 evdev_scroll_get_methods(struct libinput_device *device)
924 {
925         return LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
926 }
927
928 static void
929 evdev_change_scroll_method(struct evdev_device *device)
930 {
931         if (device->scroll.want_method == device->scroll.method &&
932             device->scroll.want_button == device->scroll.button)
933                 return;
934
935         if (evdev_any_button_down(device))
936                 return;
937
938         device->scroll.method = device->scroll.want_method;
939         device->scroll.button = device->scroll.want_button;
940 }
941
942 static enum libinput_config_status
943 evdev_scroll_set_method(struct libinput_device *device,
944                         enum libinput_config_scroll_method method)
945 {
946         struct evdev_device *evdev = (struct evdev_device*)device;
947
948         evdev->scroll.want_method = method;
949         evdev->scroll.change_scroll_method(evdev);
950
951         return LIBINPUT_CONFIG_STATUS_SUCCESS;
952 }
953
954 static enum libinput_config_scroll_method
955 evdev_scroll_get_method(struct libinput_device *device)
956 {
957         struct evdev_device *evdev = (struct evdev_device *)device;
958
959         /* return the wanted configuration, even if it hasn't taken
960          * effect yet! */
961         return evdev->scroll.want_method;
962 }
963
964 static enum libinput_config_scroll_method
965 evdev_scroll_get_default_method(struct libinput_device *device)
966 {
967         struct evdev_device *evdev = (struct evdev_device *)device;
968
969         if (libevdev_has_property(evdev->evdev, INPUT_PROP_POINTING_STICK))
970                 return LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
971         else
972                 return LIBINPUT_CONFIG_SCROLL_NO_SCROLL;
973 }
974
975 static enum libinput_config_status
976 evdev_scroll_set_button(struct libinput_device *device,
977                         uint32_t button)
978 {
979         struct evdev_device *evdev = (struct evdev_device*)device;
980
981         evdev->scroll.want_button = button;
982         evdev->scroll.change_scroll_method(evdev);
983
984         return LIBINPUT_CONFIG_STATUS_SUCCESS;
985 }
986
987 static uint32_t
988 evdev_scroll_get_button(struct libinput_device *device)
989 {
990         struct evdev_device *evdev = (struct evdev_device *)device;
991
992         /* return the wanted configuration, even if it hasn't taken
993          * effect yet! */
994         return evdev->scroll.want_button;
995 }
996
997 static uint32_t
998 evdev_scroll_get_default_button(struct libinput_device *device)
999 {
1000         struct evdev_device *evdev = (struct evdev_device *)device;
1001
1002         if (libevdev_has_property(evdev->evdev, INPUT_PROP_POINTING_STICK))
1003                 return BTN_MIDDLE;
1004         else
1005                 return 0;
1006 }
1007
1008 static int
1009 evdev_init_button_scroll(struct evdev_device *device,
1010                          void (*change_scroll_method)(struct evdev_device *))
1011 {
1012         libinput_timer_init(&device->scroll.timer, device->base.seat->libinput,
1013                             evdev_button_scroll_timeout, device);
1014         device->scroll.config.get_methods = evdev_scroll_get_methods;
1015         device->scroll.config.set_method = evdev_scroll_set_method;
1016         device->scroll.config.get_method = evdev_scroll_get_method;
1017         device->scroll.config.get_default_method = evdev_scroll_get_default_method;
1018         device->scroll.config.set_button = evdev_scroll_set_button;
1019         device->scroll.config.get_button = evdev_scroll_get_button;
1020         device->scroll.config.get_default_button = evdev_scroll_get_default_button;
1021         device->base.config.scroll_method = &device->scroll.config;
1022         device->scroll.method = evdev_scroll_get_default_method((struct libinput_device *)device);
1023         device->scroll.want_method = device->scroll.method;
1024         device->scroll.button = evdev_scroll_get_default_button((struct libinput_device *)device);
1025         device->scroll.want_button = device->scroll.button;
1026         device->scroll.change_scroll_method = change_scroll_method;
1027
1028         return 0;
1029 }
1030
1031 static void
1032 evdev_init_calibration(struct evdev_device *device,
1033                        struct evdev_dispatch *dispatch)
1034 {
1035         device->base.config.calibration = &dispatch->calibration;
1036
1037         dispatch->calibration.has_matrix = evdev_calibration_has_matrix;
1038         dispatch->calibration.set_matrix = evdev_calibration_set_matrix;
1039         dispatch->calibration.get_matrix = evdev_calibration_get_matrix;
1040         dispatch->calibration.get_default_matrix = evdev_calibration_get_default_matrix;
1041 }
1042
1043 static void
1044 evdev_init_sendevents(struct evdev_device *device,
1045                       struct evdev_dispatch *dispatch)
1046 {
1047         device->base.config.sendevents = &dispatch->sendevents.config;
1048
1049         dispatch->sendevents.current_mode = LIBINPUT_CONFIG_SEND_EVENTS_ENABLED;
1050         dispatch->sendevents.config.get_modes = evdev_sendevents_get_modes;
1051         dispatch->sendevents.config.set_mode = evdev_sendevents_set_mode;
1052         dispatch->sendevents.config.get_mode = evdev_sendevents_get_mode;
1053         dispatch->sendevents.config.get_default_mode = evdev_sendevents_get_default_mode;
1054 }
1055
1056 static int
1057 evdev_scroll_config_natural_has(struct libinput_device *device)
1058 {
1059         return 1;
1060 }
1061
1062 static enum libinput_config_status
1063 evdev_scroll_config_natural_set(struct libinput_device *device,
1064                                 int enabled)
1065 {
1066         struct evdev_device *dev = (struct evdev_device *)device;
1067
1068         dev->scroll.natural_scrolling_enabled = enabled ? true : false;
1069
1070         return LIBINPUT_CONFIG_STATUS_SUCCESS;
1071 }
1072
1073 static int
1074 evdev_scroll_config_natural_get(struct libinput_device *device)
1075 {
1076         struct evdev_device *dev = (struct evdev_device *)device;
1077
1078         return dev->scroll.natural_scrolling_enabled ? 1 : 0;
1079 }
1080
1081 static int
1082 evdev_scroll_config_natural_get_default(struct libinput_device *device)
1083 {
1084         /* could enable this on Apple touchpads. could do that, could
1085          * very well do that... */
1086         return 0;
1087 }
1088
1089 void
1090 evdev_init_natural_scroll(struct evdev_device *device)
1091 {
1092         device->scroll.config_natural.has = evdev_scroll_config_natural_has;
1093         device->scroll.config_natural.set_enabled = evdev_scroll_config_natural_set;
1094         device->scroll.config_natural.get_enabled = evdev_scroll_config_natural_get;
1095         device->scroll.config_natural.get_default_enabled = evdev_scroll_config_natural_get_default;
1096         device->scroll.natural_scrolling_enabled = false;
1097         device->base.config.natural_scroll = &device->scroll.config_natural;
1098 }
1099
1100 static struct evdev_dispatch *
1101 fallback_dispatch_create(struct libinput_device *device)
1102 {
1103         struct evdev_dispatch *dispatch = zalloc(sizeof *dispatch);
1104         struct evdev_device *evdev_device = (struct evdev_device *)device;
1105
1106         if (dispatch == NULL)
1107                 return NULL;
1108
1109         dispatch->interface = &fallback_interface;
1110
1111         if (evdev_device->left_handed.want_enabled &&
1112             evdev_init_left_handed(evdev_device,
1113                                    evdev_change_to_left_handed) == -1) {
1114                 free(dispatch);
1115                 return NULL;
1116         }
1117
1118         if (evdev_device->scroll.want_button &&
1119             evdev_init_button_scroll(evdev_device,
1120                                      evdev_change_scroll_method) == -1) {
1121                 free(dispatch);
1122                 return NULL;
1123         }
1124
1125         if (evdev_device->scroll.natural_scrolling_enabled)
1126                 evdev_init_natural_scroll(evdev_device);
1127
1128         evdev_init_calibration(evdev_device, dispatch);
1129         evdev_init_sendevents(evdev_device, dispatch);
1130
1131         return dispatch;
1132 }
1133
1134 static inline void
1135 evdev_process_event(struct evdev_device *device, struct input_event *e)
1136 {
1137         struct evdev_dispatch *dispatch = device->dispatch;
1138         uint64_t time = e->time.tv_sec * 1000ULL + e->time.tv_usec / 1000;
1139
1140 #if 0
1141         if (libevdev_event_is_code(e, EV_SYN, SYN_REPORT))
1142                 log_debug(device->base.seat->libinput,
1143                           "-------------- EV_SYN ------------\n");
1144         else
1145                 log_debug(device->base.seat->libinput,
1146                           "%-7s %-16s %-20s %4d\n",
1147                           evdev_device_get_sysname(device),
1148                           libevdev_event_type_get_name(e->type),
1149                           libevdev_event_code_get_name(e->type, e->code),
1150                           e->value);
1151 #endif
1152
1153         dispatch->interface->process(dispatch, device, e, time);
1154 }
1155
1156 static inline void
1157 evdev_device_dispatch_one(struct evdev_device *device,
1158                           struct input_event *ev)
1159 {
1160         if (!device->mtdev) {
1161                 evdev_process_event(device, ev);
1162         } else {
1163                 mtdev_put_event(device->mtdev, ev);
1164                 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
1165                         while (!mtdev_empty(device->mtdev)) {
1166                                 struct input_event e;
1167                                 mtdev_get_event(device->mtdev, &e);
1168                                 evdev_process_event(device, &e);
1169                         }
1170                 }
1171         }
1172 }
1173
1174 static int
1175 evdev_sync_device(struct evdev_device *device)
1176 {
1177         struct input_event ev;
1178         int rc;
1179
1180         do {
1181                 rc = libevdev_next_event(device->evdev,
1182                                          LIBEVDEV_READ_FLAG_SYNC, &ev);
1183                 if (rc < 0)
1184                         break;
1185                 evdev_device_dispatch_one(device, &ev);
1186         } while (rc == LIBEVDEV_READ_STATUS_SYNC);
1187
1188         return rc == -EAGAIN ? 0 : rc;
1189 }
1190
1191 static void
1192 evdev_device_dispatch(void *data)
1193 {
1194         struct evdev_device *device = data;
1195         struct libinput *libinput = device->base.seat->libinput;
1196         struct input_event ev;
1197         int rc;
1198
1199         /* If the compositor is repainting, this function is called only once
1200          * per frame and we have to process all the events available on the
1201          * fd, otherwise there will be input lag. */
1202         do {
1203                 rc = libevdev_next_event(device->evdev,
1204                                          LIBEVDEV_READ_FLAG_NORMAL, &ev);
1205                 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
1206                         switch (ratelimit_test(&device->syn_drop_limit)) {
1207                         case RATELIMIT_PASS:
1208                                 log_info(libinput, "SYN_DROPPED event from "
1209                                          "\"%s\" - some input events have "
1210                                          "been lost.\n", device->devname);
1211                                 break;
1212                         case RATELIMIT_THRESHOLD:
1213                                 log_info(libinput, "SYN_DROPPED flood "
1214                                          "from \"%s\"\n",
1215                                          device->devname);
1216                                 break;
1217                         case RATELIMIT_EXCEEDED:
1218                                 break;
1219                         }
1220
1221                         /* send one more sync event so we handle all
1222                            currently pending events before we sync up
1223                            to the current state */
1224                         ev.code = SYN_REPORT;
1225                         evdev_device_dispatch_one(device, &ev);
1226
1227                         rc = evdev_sync_device(device);
1228                         if (rc == 0)
1229                                 rc = LIBEVDEV_READ_STATUS_SUCCESS;
1230                 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
1231                         evdev_device_dispatch_one(device, &ev);
1232                 }
1233         } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
1234
1235         if (rc != -EAGAIN && rc != -EINTR) {
1236                 libinput_remove_source(libinput, device->source);
1237                 device->source = NULL;
1238         }
1239 }
1240
1241 static int
1242 evdev_accel_config_available(struct libinput_device *device)
1243 {
1244         /* this function is only called if we set up ptraccel, so we can
1245            reply with a resounding "Yes" */
1246         return 1;
1247 }
1248
1249 static enum libinput_config_status
1250 evdev_accel_config_set_speed(struct libinput_device *device, double speed)
1251 {
1252         struct evdev_device *dev = (struct evdev_device *)device;
1253
1254         if (!filter_set_speed(dev->pointer.filter, speed))
1255                 return LIBINPUT_CONFIG_STATUS_INVALID;
1256
1257         return LIBINPUT_CONFIG_STATUS_SUCCESS;
1258 }
1259
1260 static double
1261 evdev_accel_config_get_speed(struct libinput_device *device)
1262 {
1263         struct evdev_device *dev = (struct evdev_device *)device;
1264
1265         return filter_get_speed(dev->pointer.filter);
1266 }
1267
1268 static double
1269 evdev_accel_config_get_default_speed(struct libinput_device *device)
1270 {
1271         return 0.0;
1272 }
1273
1274 int
1275 evdev_device_init_pointer_acceleration(struct evdev_device *device,
1276                                        accel_profile_func_t profile)
1277 {
1278         device->pointer.filter = create_pointer_accelerator_filter(profile);
1279         if (!device->pointer.filter)
1280                 return -1;
1281
1282         device->pointer.config.available = evdev_accel_config_available;
1283         device->pointer.config.set_speed = evdev_accel_config_set_speed;
1284         device->pointer.config.get_speed = evdev_accel_config_get_speed;
1285         device->pointer.config.get_default_speed = evdev_accel_config_get_default_speed;
1286         device->base.config.accel = &device->pointer.config;
1287
1288         evdev_accel_config_set_speed(&device->base,
1289                      evdev_accel_config_get_default_speed(&device->base));
1290
1291         return 0;
1292 }
1293
1294 static inline int
1295 evdev_need_mtdev(struct evdev_device *device)
1296 {
1297         struct libevdev *evdev = device->evdev;
1298
1299         return (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
1300                 libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y) &&
1301                 !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT));
1302 }
1303
1304 static void
1305 evdev_tag_device(struct evdev_device *device)
1306 {
1307         if (device->dispatch->interface->tag_device)
1308                 device->dispatch->interface->tag_device(device,
1309                                                         device->udev_device);
1310 }
1311
1312 static inline int
1313 evdev_read_wheel_click_prop(struct evdev_device *device)
1314 {
1315         struct libinput *libinput = device->base.seat->libinput;
1316         const char *prop;
1317         int angle = DEFAULT_WHEEL_CLICK_ANGLE;
1318
1319         prop = udev_device_get_property_value(device->udev_device,
1320                                               "MOUSE_WHEEL_CLICK_ANGLE");
1321         if (prop) {
1322                 angle = parse_mouse_wheel_click_angle_property(prop);
1323                 if (!angle) {
1324                         log_error(libinput,
1325                                   "Mouse wheel click angle '%s' is present but invalid,"
1326                                   "using %d degrees instead\n",
1327                                   device->devname,
1328                                   DEFAULT_WHEEL_CLICK_ANGLE);
1329                         angle = DEFAULT_WHEEL_CLICK_ANGLE;
1330                 }
1331         }
1332
1333         return angle;
1334 }
1335 static inline int
1336 evdev_read_dpi_prop(struct evdev_device *device)
1337 {
1338         struct libinput *libinput = device->base.seat->libinput;
1339         const char *mouse_dpi;
1340         int dpi = DEFAULT_MOUSE_DPI;
1341
1342         mouse_dpi = udev_device_get_property_value(device->udev_device,
1343                                                    "MOUSE_DPI");
1344         if (mouse_dpi) {
1345                 dpi = parse_mouse_dpi_property(mouse_dpi);
1346                 if (!dpi) {
1347                         log_error(libinput, "Mouse DPI property for '%s' is "
1348                                             "present but invalid, using %d "
1349                                             "DPI instead\n",
1350                                             device->devname,
1351                                             DEFAULT_MOUSE_DPI);
1352                         dpi = DEFAULT_MOUSE_DPI;
1353                 }
1354         }
1355
1356         return dpi;
1357 }
1358
1359 /* Return 1 if the given resolutions have been set, or 0 otherwise */
1360 inline int
1361 evdev_fix_abs_resolution(struct evdev_device *device,
1362                          unsigned int xcode,
1363                          unsigned int ycode,
1364                          int xresolution,
1365                          int yresolution)
1366 {
1367         struct libinput *libinput = device->base.seat->libinput;
1368         struct libevdev *evdev = device->evdev;
1369         const struct input_absinfo *absx, *absy;
1370         struct input_absinfo fixed;
1371         int rc = 0;
1372
1373         if (!(xcode == ABS_X && ycode == ABS_Y)  &&
1374             !(xcode == ABS_MT_POSITION_X && ycode == ABS_MT_POSITION_Y)) {
1375                 log_bug_libinput(libinput,
1376                                  "Invalid x/y code combination %d/%d\n",
1377                                  xcode, ycode);
1378                 return 0;
1379         }
1380
1381         if (xresolution == 0 || yresolution == 0 ||
1382             (xresolution == EVDEV_FAKE_RESOLUTION && xresolution != yresolution) ||
1383             (yresolution == EVDEV_FAKE_RESOLUTION && xresolution != yresolution)) {
1384                 log_bug_libinput(libinput,
1385                                  "Invalid x/y resolutions %d/%d\n",
1386                                  xresolution, yresolution);
1387                 return 0;
1388         }
1389
1390         absx = libevdev_get_abs_info(evdev, xcode);
1391         absy = libevdev_get_abs_info(evdev, ycode);
1392
1393         if (absx->resolution == 0 || absx->resolution == EVDEV_FAKE_RESOLUTION) {
1394                 fixed = *absx;
1395                 fixed.resolution = xresolution;
1396                 /* libevdev_set_abs_info() changes the absinfo we already
1397                    have a pointer to, no need to fetch it again */
1398                 libevdev_set_abs_info(evdev, xcode, &fixed);
1399                 rc = 1;
1400         }
1401
1402         if (absy->resolution == 0 || absy->resolution == EVDEV_FAKE_RESOLUTION) {
1403                 fixed = *absy;
1404                 fixed.resolution = yresolution;
1405                 /* libevdev_set_abs_info() changes the absinfo we already
1406                    have a pointer to, no need to fetch it again */
1407                 libevdev_set_abs_info(evdev, ycode, &fixed);
1408                 rc = 1;
1409         }
1410
1411         return rc;
1412 }
1413
1414 static enum evdev_device_udev_tags
1415 evdev_device_get_udev_tags(struct evdev_device *device,
1416                            struct udev_device *udev_device)
1417 {
1418         const char *prop;
1419         enum evdev_device_udev_tags tags = 0;
1420         const struct evdev_udev_tag_match *match;
1421         int i;
1422
1423         for (i = 0; i < 2 && udev_device; i++) {
1424                 match = evdev_udev_tag_matches;
1425                 while (match->name) {
1426                         prop = udev_device_get_property_value(
1427                                                       udev_device,
1428                                                       match->name);
1429                         if (prop)
1430                                 tags |= match->tag;
1431
1432                         match++;
1433                 }
1434                 udev_device = udev_device_get_parent(udev_device);
1435         }
1436
1437         return tags;
1438 }
1439
1440 /* Fake MT devices have the ABS_MT_SLOT bit set because of
1441    the limited ABS_* range - they aren't MT devices, they
1442    just have too many ABS_ axes */
1443 static inline bool
1444 evdev_is_fake_mt_device(struct evdev_device *device)
1445 {
1446         struct libevdev *evdev = device->evdev;
1447
1448         return libevdev_has_event_code(evdev, EV_ABS, ABS_MT_SLOT) &&
1449                 libevdev_get_num_slots(evdev) == -1;
1450 }
1451
1452 static inline void
1453 evdev_fix_android_mt(struct evdev_device *device)
1454 {
1455         struct libevdev *evdev = device->evdev;
1456
1457         if (libevdev_has_event_code(evdev, EV_ABS, ABS_X) ||
1458             libevdev_has_event_code(evdev, EV_ABS, ABS_Y))
1459                 return;
1460
1461         if (!libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) ||
1462             !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y) ||
1463             evdev_is_fake_mt_device(device))
1464                 return;
1465
1466         libevdev_enable_event_code(evdev, EV_ABS, ABS_X,
1467                       libevdev_get_abs_info(evdev, ABS_MT_POSITION_X));
1468         libevdev_enable_event_code(evdev, EV_ABS, ABS_Y,
1469                       libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y));
1470 }
1471
1472 static inline int
1473 evdev_check_min_max(struct evdev_device *device, unsigned int code)
1474 {
1475         struct libevdev *evdev = device->evdev;
1476         const struct input_absinfo *absinfo;
1477
1478         if (!libevdev_has_event_code(evdev, EV_ABS, code))
1479                 return 0;
1480
1481         absinfo = libevdev_get_abs_info(evdev, code);
1482         if (absinfo->minimum == absinfo->maximum) {
1483                 log_bug_kernel(device->base.seat->libinput,
1484                                "Device '%s' has min == max on %s\n",
1485                                device->devname,
1486                                libevdev_event_code_get_name(EV_ABS, code));
1487                 return -1;
1488         }
1489
1490         return 0;
1491 }
1492
1493 static int
1494 evdev_reject_device(struct evdev_device *device)
1495 {
1496         struct libinput *libinput = device->base.seat->libinput;
1497         struct libevdev *evdev = device->evdev;
1498         unsigned int code;
1499         const struct input_absinfo *absx, *absy;
1500
1501         if (libevdev_has_event_code(evdev, EV_ABS, ABS_X) ^
1502             libevdev_has_event_code(evdev, EV_ABS, ABS_Y))
1503                 return -1;
1504
1505         if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) ^
1506             libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y))
1507                 return -1;
1508
1509         if (libevdev_has_event_code(evdev, EV_ABS, ABS_X)) {
1510                 absx = libevdev_get_abs_info(evdev, ABS_X);
1511                 absy = libevdev_get_abs_info(evdev, ABS_Y);
1512                 if ((absx->resolution == 0 && absy->resolution != 0) ||
1513                     (absx->resolution != 0 && absy->resolution == 0)) {
1514                         log_bug_kernel(libinput,
1515                                        "Kernel has only x or y resolution, not both.\n");
1516                         return -1;
1517                 }
1518         }
1519
1520         if (!evdev_is_fake_mt_device(device) &&
1521             libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X)) {
1522                 absx = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
1523                 absy = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
1524                 if ((absx->resolution == 0 && absy->resolution != 0) ||
1525                     (absx->resolution != 0 && absy->resolution == 0)) {
1526                         log_bug_kernel(libinput,
1527                                        "Kernel has only x or y MT resolution, not both.\n");
1528                         return -1;
1529                 }
1530         }
1531
1532         for (code = 0; code < ABS_CNT; code++) {
1533                 switch (code) {
1534                 case ABS_MISC:
1535                 case ABS_MT_SLOT:
1536                 case ABS_MT_TOOL_TYPE:
1537                         break;
1538                 default:
1539                         if (evdev_check_min_max(device, code) == -1)
1540                                 return -1;
1541                 }
1542         }
1543
1544         return 0;
1545 }
1546
1547 static int
1548 evdev_configure_mt_device(struct evdev_device *device)
1549 {
1550         struct libevdev *evdev = device->evdev;
1551         struct mt_slot *slots;
1552         int num_slots;
1553         int active_slot;
1554         int slot;
1555
1556         if (!libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) ||
1557             !libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y))
1558                  return 0;
1559
1560         if (evdev_fix_abs_resolution(device,
1561                                      ABS_MT_POSITION_X,
1562                                      ABS_MT_POSITION_Y,
1563                                      EVDEV_FAKE_RESOLUTION,
1564                                      EVDEV_FAKE_RESOLUTION))
1565                 device->abs.fake_resolution = 1;
1566
1567         device->abs.absinfo_x = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
1568         device->abs.absinfo_y = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
1569         device->is_mt = 1;
1570
1571         /* We only handle the slotted Protocol B in libinput.
1572            Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
1573            require mtdev for conversion. */
1574         if (evdev_need_mtdev(device)) {
1575                 device->mtdev = mtdev_new_open(device->fd);
1576                 if (!device->mtdev)
1577                         return -1;
1578
1579                 /* pick 10 slots as default for type A
1580                    devices. */
1581                 num_slots = 10;
1582                 active_slot = device->mtdev->caps.slot.value;
1583         } else {
1584                 num_slots = libevdev_get_num_slots(device->evdev);
1585                 active_slot = libevdev_get_current_slot(evdev);
1586         }
1587
1588         slots = calloc(num_slots, sizeof(struct mt_slot));
1589         if (!slots)
1590                 return -1;
1591
1592         for (slot = 0; slot < num_slots; ++slot) {
1593                 slots[slot].seat_slot = -1;
1594                 slots[slot].point.x = 0;
1595                 slots[slot].point.y = 0;
1596         }
1597         device->mt.slots = slots;
1598         device->mt.slots_len = num_slots;
1599         device->mt.slot = active_slot;
1600
1601         return 0;
1602 }
1603
1604 static int
1605 evdev_configure_device(struct evdev_device *device)
1606 {
1607         struct libinput *libinput = device->base.seat->libinput;
1608         struct libevdev *evdev = device->evdev;
1609         const char *devnode = udev_device_get_devnode(device->udev_device);
1610         enum evdev_device_udev_tags udev_tags;
1611
1612         udev_tags = evdev_device_get_udev_tags(device, device->udev_device);
1613
1614         if ((udev_tags & EVDEV_UDEV_TAG_INPUT) == 0 ||
1615             (udev_tags & ~EVDEV_UDEV_TAG_INPUT) == 0) {
1616                 log_info(libinput,
1617                          "input device '%s', %s not tagged as input device\n",
1618                          device->devname, devnode);
1619                 return -1;
1620         }
1621
1622         log_info(libinput,
1623                  "input device '%s', %s is tagged by udev as:%s%s%s%s%s%s%s%s\n",
1624                  device->devname, devnode,
1625                  udev_tags & EVDEV_UDEV_TAG_KEYBOARD ? " Keyboard" : "",
1626                  udev_tags & EVDEV_UDEV_TAG_MOUSE ? " Mouse" : "",
1627                  udev_tags & EVDEV_UDEV_TAG_TOUCHPAD ? " Touchpad" : "",
1628                  udev_tags & EVDEV_UDEV_TAG_TOUCHSCREEN ? " Touchscreen" : "",
1629                  udev_tags & EVDEV_UDEV_TAG_TABLET ? " Tablet" : "",
1630                  udev_tags & EVDEV_UDEV_TAG_JOYSTICK ? " Joystick" : "",
1631                  udev_tags & EVDEV_UDEV_TAG_ACCELEROMETER ? " Accelerometer" : "",
1632                  udev_tags & EVDEV_UDEV_TAG_BUTTONSET ? " Buttonset" : "");
1633
1634         /* libwacom *adds* TABLET, TOUCHPAD but leaves JOYSTICK in place, so
1635            make sure we only ignore real joystick devices */
1636         if ((udev_tags & EVDEV_UDEV_TAG_JOYSTICK) == udev_tags) {
1637                 log_info(libinput,
1638                          "input device '%s', %s is a joystick, ignoring\n",
1639                          device->devname, devnode);
1640                 return -1;
1641         }
1642
1643         /* libwacom assigns tablet _and_ tablet_pad to the pad devices */
1644         if (udev_tags & EVDEV_UDEV_TAG_BUTTONSET) {
1645                 log_info(libinput,
1646                          "input device '%s', %s is a buttonset, ignoring\n",
1647                          device->devname, devnode);
1648                 return -1;
1649         }
1650
1651         if (evdev_reject_device(device) == -1) {
1652                 log_info(libinput,
1653                          "input device '%s', %s was rejected.\n",
1654                          device->devname, devnode);
1655                 return -1;
1656         }
1657
1658         if (!evdev_is_fake_mt_device(device))
1659                 evdev_fix_android_mt(device);
1660
1661         if (libevdev_has_event_code(evdev, EV_ABS, ABS_X)) {
1662                 if (evdev_fix_abs_resolution(device,
1663                                              ABS_X,
1664                                              ABS_Y,
1665                                              EVDEV_FAKE_RESOLUTION,
1666                                              EVDEV_FAKE_RESOLUTION))
1667                         device->abs.fake_resolution = 1;
1668                 device->abs.absinfo_x = libevdev_get_abs_info(evdev, ABS_X);
1669                 device->abs.absinfo_y = libevdev_get_abs_info(evdev, ABS_Y);
1670
1671                 if (evdev_is_fake_mt_device(device)) {
1672                         udev_tags &= ~EVDEV_UDEV_TAG_TOUCHSCREEN;
1673                 } else if (evdev_configure_mt_device(device) == -1) {
1674                         return -1;
1675                 }
1676         }
1677
1678         if (udev_tags & EVDEV_UDEV_TAG_TOUCHPAD) {
1679                 device->dispatch = evdev_mt_touchpad_create(device);
1680                 log_info(libinput,
1681                          "input device '%s', %s is a touchpad\n",
1682                          device->devname, devnode);
1683                 return device->dispatch == NULL ? -1 : 0;
1684         }
1685
1686         if (udev_tags & EVDEV_UDEV_TAG_MOUSE) {
1687                 if (!libevdev_has_event_code(evdev, EV_ABS, ABS_X) &&
1688                     !libevdev_has_event_code(evdev, EV_ABS, ABS_Y) &&
1689                     evdev_device_init_pointer_acceleration(
1690                                         device,
1691                                         pointer_accel_profile_linear) == -1)
1692                         return -1;
1693
1694                 device->seat_caps |= EVDEV_DEVICE_POINTER;
1695
1696                 log_info(libinput,
1697                          "input device '%s', %s is a pointer caps\n",
1698                          device->devname, devnode);
1699
1700                 /* want left-handed config option */
1701                 device->left_handed.want_enabled = true;
1702                 /* want natural-scroll config option */
1703                 device->scroll.natural_scrolling_enabled = true;
1704                 /* want button scrolling config option */
1705                 device->scroll.want_button = 1;
1706         }
1707
1708         if (udev_tags & EVDEV_UDEV_TAG_KEYBOARD) {
1709                 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
1710                 log_info(libinput,
1711                          "input device '%s', %s is a keyboard\n",
1712                          device->devname, devnode);
1713
1714                 /* want natural-scroll config option */
1715                 if (libevdev_has_event_code(evdev, EV_REL, REL_WHEEL) ||
1716                     libevdev_has_event_code(evdev, EV_REL, REL_HWHEEL)) {
1717                         device->scroll.natural_scrolling_enabled = true;
1718                         device->seat_caps |= EVDEV_DEVICE_POINTER;
1719                 }
1720         }
1721
1722         if (udev_tags & EVDEV_UDEV_TAG_TOUCHSCREEN) {
1723                 device->seat_caps |= EVDEV_DEVICE_TOUCH;
1724                 log_info(libinput,
1725                          "input device '%s', %s is a touch device\n",
1726                          device->devname, devnode);
1727         }
1728
1729         return 0;
1730 }
1731
1732 static void
1733 evdev_notify_added_device(struct evdev_device *device)
1734 {
1735         struct libinput_device *dev;
1736
1737         list_for_each(dev, &device->base.seat->devices_list, link) {
1738                 struct evdev_device *d = (struct evdev_device*)dev;
1739                 if (dev == &device->base)
1740                         continue;
1741
1742                 /* Notify existing device d about addition of device device */
1743                 if (d->dispatch->interface->device_added)
1744                         d->dispatch->interface->device_added(d, device);
1745
1746                 /* Notify new device device about existing device d */
1747                 if (device->dispatch->interface->device_added)
1748                         device->dispatch->interface->device_added(device, d);
1749
1750                 /* Notify new device device if existing device d is suspended */
1751                 if (d->suspended && device->dispatch->interface->device_suspended)
1752                         device->dispatch->interface->device_suspended(device, d);
1753         }
1754
1755         notify_added_device(&device->base);
1756 }
1757
1758 static int
1759 evdev_device_compare_syspath(struct udev_device *udev_device, int fd)
1760 {
1761         struct udev *udev = udev_device_get_udev(udev_device);
1762         struct udev_device *udev_device_new = NULL;
1763         struct stat st;
1764         int rc = 1;
1765
1766         if (fstat(fd, &st) < 0)
1767                 goto out;
1768
1769         udev_device_new = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
1770         if (!udev_device_new)
1771                 goto out;
1772
1773         rc = strcmp(udev_device_get_syspath(udev_device_new),
1774                     udev_device_get_syspath(udev_device));
1775 out:
1776         if (udev_device_new)
1777                 udev_device_unref(udev_device_new);
1778         return rc;
1779 }
1780
1781 static int
1782 evdev_set_device_group(struct evdev_device *device,
1783                        struct udev_device *udev_device)
1784 {
1785         struct libinput_device_group *group = NULL;
1786         const char *udev_group;
1787
1788         udev_group = udev_device_get_property_value(udev_device,
1789                                                     "LIBINPUT_DEVICE_GROUP");
1790         if (udev_group) {
1791                 struct libinput_device *d;
1792
1793                 list_for_each(d, &device->base.seat->devices_list, link) {
1794                         const char *identifier = d->group->identifier;
1795
1796                         if (identifier &&
1797                             strcmp(identifier, udev_group) == 0) {
1798                                 group = d->group;
1799                                 break;
1800                         }
1801                 }
1802         }
1803
1804         if (!group) {
1805                 group = libinput_device_group_create(udev_group);
1806                 if (!group)
1807                         return 1;
1808                 libinput_device_set_device_group(&device->base, group);
1809                 libinput_device_group_unref(group);
1810         } else {
1811                 libinput_device_set_device_group(&device->base, group);
1812         }
1813
1814         return 0;
1815 }
1816
1817 struct evdev_device *
1818 evdev_device_create(struct libinput_seat *seat,
1819                     struct udev_device *udev_device)
1820 {
1821         struct libinput *libinput = seat->libinput;
1822         struct evdev_device *device = NULL;
1823         int rc;
1824         int fd;
1825         int unhandled_device = 0;
1826         const char *devnode = udev_device_get_devnode(udev_device);
1827
1828         /* Use non-blocking mode so that we can loop on read on
1829          * evdev_device_data() until all events on the fd are
1830          * read.  mtdev_get() also expects this. */
1831         fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
1832         if (fd < 0) {
1833                 log_info(libinput,
1834                          "opening input device '%s' failed (%s).\n",
1835                          devnode, strerror(-fd));
1836                 return NULL;
1837         }
1838
1839         if (evdev_device_compare_syspath(udev_device, fd) != 0)
1840                 goto err;
1841
1842         device = zalloc(sizeof *device);
1843         if (device == NULL)
1844                 goto err;
1845
1846         libinput_device_init(&device->base, seat);
1847         libinput_seat_ref(seat);
1848
1849         rc = libevdev_new_from_fd(fd, &device->evdev);
1850         if (rc != 0)
1851                 goto err;
1852
1853         libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
1854
1855         device->seat_caps = 0;
1856         device->is_mt = 0;
1857         device->mtdev = NULL;
1858         device->udev_device = udev_device_ref(udev_device);
1859         device->rel.x = 0;
1860         device->rel.y = 0;
1861         device->abs.seat_slot = -1;
1862         device->dispatch = NULL;
1863         device->fd = fd;
1864         device->pending_event = EVDEV_NONE;
1865         device->devname = libevdev_get_name(device->evdev);
1866         device->scroll.threshold = 5.0; /* Default may be overridden */
1867         device->scroll.direction = 0;
1868         device->scroll.wheel_click_angle =
1869                 evdev_read_wheel_click_prop(device);
1870         device->dpi = evdev_read_dpi_prop(device);
1871         /* at most 5 SYN_DROPPED log-messages per 30s */
1872         ratelimit_init(&device->syn_drop_limit, 30ULL * 1000, 5);
1873
1874         matrix_init_identity(&device->abs.calibration);
1875         matrix_init_identity(&device->abs.usermatrix);
1876         matrix_init_identity(&device->abs.default_calibration);
1877
1878         if (evdev_configure_device(device) == -1)
1879                 goto err;
1880
1881         if (device->seat_caps == 0) {
1882                 unhandled_device = 1;
1883                 goto err;
1884         }
1885
1886         /* If the dispatch was not set up use the fallback. */
1887         if (device->dispatch == NULL)
1888                 device->dispatch = fallback_dispatch_create(&device->base);
1889         if (device->dispatch == NULL)
1890                 goto err;
1891
1892         device->source =
1893                 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
1894         if (!device->source)
1895                 goto err;
1896
1897         if (evdev_set_device_group(device, udev_device))
1898                 goto err;
1899
1900         list_insert(seat->devices_list.prev, &device->base.link);
1901
1902         evdev_tag_device(device);
1903         evdev_notify_added_device(device);
1904
1905         return device;
1906
1907 err:
1908         if (fd >= 0)
1909                 close_restricted(libinput, fd);
1910         if (device)
1911                 evdev_device_destroy(device);
1912
1913         return unhandled_device ? EVDEV_UNHANDLED_DEVICE :  NULL;
1914 }
1915
1916 const char *
1917 evdev_device_get_output(struct evdev_device *device)
1918 {
1919         return device->output_name;
1920 }
1921
1922 const char *
1923 evdev_device_get_sysname(struct evdev_device *device)
1924 {
1925         return udev_device_get_sysname(device->udev_device);
1926 }
1927
1928 const char *
1929 evdev_device_get_name(struct evdev_device *device)
1930 {
1931         return device->devname;
1932 }
1933
1934 unsigned int
1935 evdev_device_get_id_product(struct evdev_device *device)
1936 {
1937         return libevdev_get_id_product(device->evdev);
1938 }
1939
1940 unsigned int
1941 evdev_device_get_id_vendor(struct evdev_device *device)
1942 {
1943         return libevdev_get_id_vendor(device->evdev);
1944 }
1945
1946 struct udev_device *
1947 evdev_device_get_udev_device(struct evdev_device *device)
1948 {
1949         return udev_device_ref(device->udev_device);
1950 }
1951
1952 void
1953 evdev_device_set_default_calibration(struct evdev_device *device,
1954                                      const float calibration[6])
1955 {
1956         matrix_from_farray6(&device->abs.default_calibration, calibration);
1957         evdev_device_calibrate(device, calibration);
1958 }
1959
1960 void
1961 evdev_device_calibrate(struct evdev_device *device,
1962                        const float calibration[6])
1963 {
1964         struct matrix scale,
1965                       translate,
1966                       transform;
1967         double sx, sy;
1968
1969         matrix_from_farray6(&transform, calibration);
1970         device->abs.apply_calibration = !matrix_is_identity(&transform);
1971
1972         if (!device->abs.apply_calibration) {
1973                 matrix_init_identity(&device->abs.calibration);
1974                 return;
1975         }
1976
1977         sx = device->abs.absinfo_x->maximum - device->abs.absinfo_x->minimum + 1;
1978         sy = device->abs.absinfo_y->maximum - device->abs.absinfo_y->minimum + 1;
1979
1980         /* The transformation matrix is in the form:
1981          *  [ a b c ]
1982          *  [ d e f ]
1983          *  [ 0 0 1 ]
1984          * Where a, e are the scale components, a, b, d, e are the rotation
1985          * component (combined with scale) and c and f are the translation
1986          * component. The translation component in the input matrix must be
1987          * normalized to multiples of the device width and height,
1988          * respectively. e.g. c == 1 shifts one device-width to the right.
1989          *
1990          * We pre-calculate a single matrix to apply to event coordinates:
1991          *     M = Un-Normalize * Calibration * Normalize
1992          *
1993          * Normalize: scales the device coordinates to [0,1]
1994          * Calibration: user-supplied matrix
1995          * Un-Normalize: scales back up to device coordinates
1996          * Matrix maths requires the normalize/un-normalize in reverse
1997          * order.
1998          */
1999
2000         /* back up the user matrix so we can return it on request */
2001         matrix_from_farray6(&device->abs.usermatrix, calibration);
2002
2003         /* Un-Normalize */
2004         matrix_init_translate(&translate,
2005                               device->abs.absinfo_x->minimum,
2006                               device->abs.absinfo_y->minimum);
2007         matrix_init_scale(&scale, sx, sy);
2008         matrix_mult(&scale, &translate, &scale);
2009
2010         /* Calibration */
2011         matrix_mult(&transform, &scale, &transform);
2012
2013         /* Normalize */
2014         matrix_init_translate(&translate,
2015                               -device->abs.absinfo_x->minimum/sx,
2016                               -device->abs.absinfo_y->minimum/sy);
2017         matrix_init_scale(&scale, 1.0/sx, 1.0/sy);
2018         matrix_mult(&scale, &translate, &scale);
2019
2020         /* store final matrix in device */
2021         matrix_mult(&device->abs.calibration, &transform, &scale);
2022 }
2023
2024 int
2025 evdev_device_has_capability(struct evdev_device *device,
2026                             enum libinput_device_capability capability)
2027 {
2028         switch (capability) {
2029         case LIBINPUT_DEVICE_CAP_POINTER:
2030                 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
2031         case LIBINPUT_DEVICE_CAP_KEYBOARD:
2032                 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
2033         case LIBINPUT_DEVICE_CAP_TOUCH:
2034                 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
2035         default:
2036                 return 0;
2037         }
2038 }
2039
2040 int
2041 evdev_device_get_size(struct evdev_device *device,
2042                       double *width,
2043                       double *height)
2044 {
2045         const struct input_absinfo *x, *y;
2046
2047         x = libevdev_get_abs_info(device->evdev, ABS_X);
2048         y = libevdev_get_abs_info(device->evdev, ABS_Y);
2049
2050         if (!x || !y || device->abs.fake_resolution ||
2051             !x->resolution || !y->resolution)
2052                 return -1;
2053
2054         *width = evdev_convert_to_mm(x, x->maximum);
2055         *height = evdev_convert_to_mm(y, y->maximum);
2056
2057         return 0;
2058 }
2059
2060 int
2061 evdev_device_has_button(struct evdev_device *device, uint32_t code)
2062 {
2063         if (!(device->seat_caps & EVDEV_DEVICE_POINTER))
2064                 return -1;
2065
2066         return libevdev_has_event_code(device->evdev, EV_KEY, code);
2067 }
2068
2069 static inline bool
2070 evdev_is_scrolling(const struct evdev_device *device,
2071                    enum libinput_pointer_axis axis)
2072 {
2073         assert(axis == LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL ||
2074                axis == LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2075
2076         return (device->scroll.direction & AS_MASK(axis)) != 0;
2077 }
2078
2079 static inline void
2080 evdev_start_scrolling(struct evdev_device *device,
2081                       enum libinput_pointer_axis axis)
2082 {
2083         assert(axis == LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL ||
2084                axis == LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2085
2086         device->scroll.direction |= AS_MASK(axis);
2087 }
2088
2089 void
2090 evdev_post_scroll(struct evdev_device *device,
2091                   uint64_t time,
2092                   enum libinput_pointer_axis_source source,
2093                   const struct normalized_coords *delta)
2094 {
2095         const struct normalized_coords *trigger;
2096         struct normalized_coords event;
2097
2098         if (!evdev_is_scrolling(device,
2099                                 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
2100                 device->scroll.buildup.y += delta->y;
2101         if (!evdev_is_scrolling(device,
2102                                 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
2103                 device->scroll.buildup.x += delta->x;
2104
2105         trigger = &device->scroll.buildup;
2106
2107         /* If we're not scrolling yet, use a distance trigger: moving
2108            past a certain distance starts scrolling */
2109         if (!evdev_is_scrolling(device,
2110                                 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL) &&
2111             !evdev_is_scrolling(device,
2112                                 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
2113                 if (fabs(trigger->y) >= device->scroll.threshold)
2114                         evdev_start_scrolling(device,
2115                                               LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2116                 if (fabs(trigger->x) >= device->scroll.threshold)
2117                         evdev_start_scrolling(device,
2118                                               LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
2119         /* We're already scrolling in one direction. Require some
2120            trigger speed to start scrolling in the other direction */
2121         } else if (!evdev_is_scrolling(device,
2122                                LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL)) {
2123                 if (fabs(delta->y) >= device->scroll.threshold)
2124                         evdev_start_scrolling(device,
2125                                       LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
2126         } else if (!evdev_is_scrolling(device,
2127                                 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL)) {
2128                 if (fabs(delta->x) >= device->scroll.threshold)
2129                         evdev_start_scrolling(device,
2130                                       LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
2131         }
2132
2133         event = *delta;
2134
2135         /* We use the trigger to enable, but the delta from this event for
2136          * the actual scroll movement. Otherwise we get a jump once
2137          * scrolling engages */
2138         if (!evdev_is_scrolling(device,
2139                                LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
2140                 event.y = 0.0;
2141
2142         if (!evdev_is_scrolling(device,
2143                                LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
2144                 event.x = 0.0;
2145
2146         if (!normalized_is_zero(event)) {
2147                 const struct discrete_coords zero_discrete = { 0.0, 0.0 };
2148                 evdev_notify_axis(device,
2149                                   time,
2150                                   device->scroll.direction,
2151                                   source,
2152                                   &event,
2153                                   &zero_discrete);
2154         }
2155 }
2156
2157 void
2158 evdev_stop_scroll(struct evdev_device *device,
2159                   uint64_t time,
2160                   enum libinput_pointer_axis_source source)
2161 {
2162         const struct normalized_coords zero = { 0.0, 0.0 };
2163         const struct discrete_coords zero_discrete = { 0.0, 0.0 };
2164
2165         /* terminate scrolling with a zero scroll event */
2166         if (device->scroll.direction != 0)
2167                 pointer_notify_axis(&device->base,
2168                                     time,
2169                                     device->scroll.direction,
2170                                     source,
2171                                     &zero,
2172                                     &zero_discrete);
2173
2174         device->scroll.buildup.x = 0;
2175         device->scroll.buildup.y = 0;
2176         device->scroll.direction = 0;
2177 }
2178
2179 static void
2180 release_pressed_keys(struct evdev_device *device)
2181 {
2182         struct libinput *libinput = device->base.seat->libinput;
2183         uint64_t time;
2184         int code;
2185
2186         if ((time = libinput_now(libinput)) == 0)
2187                 return;
2188
2189         for (code = 0; code < KEY_CNT; code++) {
2190                 int count = get_key_down_count(device, code);
2191
2192                 if (count > 1) {
2193                         log_bug_libinput(libinput,
2194                                          "Key %d is down %d times.\n",
2195                                          code,
2196                                          count);
2197                 }
2198
2199                 while (get_key_down_count(device, code) > 0) {
2200                         switch (get_key_type(code)) {
2201                         case EVDEV_KEY_TYPE_NONE:
2202                                 break;
2203                         case EVDEV_KEY_TYPE_KEY:
2204                                 evdev_keyboard_notify_key(
2205                                         device,
2206                                         time,
2207                                         code,
2208                                         LIBINPUT_KEY_STATE_RELEASED);
2209                                 break;
2210                         case EVDEV_KEY_TYPE_BUTTON:
2211                                 evdev_pointer_notify_button(
2212                                         device,
2213                                         time,
2214                                         evdev_to_left_handed(device, code),
2215                                         LIBINPUT_BUTTON_STATE_RELEASED);
2216                                 break;
2217                         }
2218                 }
2219         }
2220 }
2221
2222 void
2223 evdev_notify_suspended_device(struct evdev_device *device)
2224 {
2225         struct libinput_device *it;
2226
2227         if (device->suspended)
2228                 return;
2229
2230         list_for_each(it, &device->base.seat->devices_list, link) {
2231                 struct evdev_device *d = (struct evdev_device*)it;
2232                 if (it == &device->base)
2233                         continue;
2234
2235                 if (d->dispatch->interface->device_suspended)
2236                         d->dispatch->interface->device_suspended(d, device);
2237         }
2238
2239         device->suspended = 1;
2240 }
2241
2242 void
2243 evdev_notify_resumed_device(struct evdev_device *device)
2244 {
2245         struct libinput_device *it;
2246
2247         if (!device->suspended)
2248                 return;
2249
2250         list_for_each(it, &device->base.seat->devices_list, link) {
2251                 struct evdev_device *d = (struct evdev_device*)it;
2252                 if (it == &device->base)
2253                         continue;
2254
2255                 if (d->dispatch->interface->device_resumed)
2256                         d->dispatch->interface->device_resumed(d, device);
2257         }
2258
2259         device->suspended = 0;
2260 }
2261
2262 int
2263 evdev_device_suspend(struct evdev_device *device)
2264 {
2265         evdev_notify_suspended_device(device);
2266
2267         if (device->source) {
2268                 libinput_remove_source(device->base.seat->libinput,
2269                                        device->source);
2270                 device->source = NULL;
2271         }
2272
2273         release_pressed_keys(device);
2274
2275         if (device->mtdev) {
2276                 mtdev_close_delete(device->mtdev);
2277                 device->mtdev = NULL;
2278         }
2279
2280         if (device->fd != -1) {
2281                 close_restricted(device->base.seat->libinput, device->fd);
2282                 device->fd = -1;
2283         }
2284
2285         return 0;
2286 }
2287
2288 int
2289 evdev_device_resume(struct evdev_device *device)
2290 {
2291         struct libinput *libinput = device->base.seat->libinput;
2292         int fd;
2293         const char *devnode;
2294         struct input_event ev;
2295         enum libevdev_read_status status;
2296
2297         if (device->fd != -1)
2298                 return 0;
2299
2300         if (device->was_removed)
2301                 return -ENODEV;
2302
2303         devnode = udev_device_get_devnode(device->udev_device);
2304         fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
2305
2306         if (fd < 0)
2307                 return -errno;
2308
2309         if (evdev_device_compare_syspath(device->udev_device, fd)) {
2310                 close_restricted(libinput, fd);
2311                 return -ENODEV;
2312         }
2313
2314         device->fd = fd;
2315
2316         if (evdev_need_mtdev(device)) {
2317                 device->mtdev = mtdev_new_open(device->fd);
2318                 if (!device->mtdev)
2319                         return -ENODEV;
2320         }
2321
2322         libevdev_change_fd(device->evdev, fd);
2323         libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
2324
2325         /* re-sync libevdev's view of the device, but discard the actual
2326            events. Our device is in a neutral state already */
2327         libevdev_next_event(device->evdev,
2328                             LIBEVDEV_READ_FLAG_FORCE_SYNC,
2329                             &ev);
2330         do {
2331                 status = libevdev_next_event(device->evdev,
2332                                              LIBEVDEV_READ_FLAG_SYNC,
2333                                              &ev);
2334         } while (status == LIBEVDEV_READ_STATUS_SYNC);
2335
2336         device->source =
2337                 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
2338         if (!device->source) {
2339                 mtdev_close_delete(device->mtdev);
2340                 return -ENOMEM;
2341         }
2342
2343         memset(device->hw_key_mask, 0, sizeof(device->hw_key_mask));
2344
2345         evdev_notify_resumed_device(device);
2346
2347         return 0;
2348 }
2349
2350 void
2351 evdev_device_remove(struct evdev_device *device)
2352 {
2353         struct libinput_device *dev;
2354
2355         list_for_each(dev, &device->base.seat->devices_list, link) {
2356                 struct evdev_device *d = (struct evdev_device*)dev;
2357                 if (dev == &device->base)
2358                         continue;
2359
2360                 if (d->dispatch->interface->device_removed)
2361                         d->dispatch->interface->device_removed(d, device);
2362         }
2363
2364         evdev_device_suspend(device);
2365
2366         if (device->dispatch->interface->remove)
2367                 device->dispatch->interface->remove(device->dispatch);
2368
2369         /* A device may be removed while suspended, mark it to
2370          * skip re-opening a different device with the same node */
2371         device->was_removed = true;
2372
2373         list_remove(&device->base.link);
2374
2375         notify_removed_device(&device->base);
2376         libinput_device_unref(&device->base);
2377 }
2378
2379 void
2380 evdev_device_destroy(struct evdev_device *device)
2381 {
2382         struct evdev_dispatch *dispatch;
2383
2384         dispatch = device->dispatch;
2385         if (dispatch)
2386                 dispatch->interface->destroy(dispatch);
2387
2388         if (device->base.group)
2389                 libinput_device_group_unref(device->base.group);
2390
2391         filter_destroy(device->pointer.filter);
2392         libinput_seat_unref(device->base.seat);
2393         libevdev_free(device->evdev);
2394         udev_device_unref(device->udev_device);
2395         free(device->mt.slots);
2396         free(device);
2397 }