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