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