evdev: Ignore key/button release events if key was never pressed
[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 <stdlib.h>
28 #include <string.h>
29 #include "linux/input.h"
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <mtdev-plumbing.h>
33 #include <assert.h>
34 #include <time.h>
35 #include <math.h>
36
37 #include "libinput.h"
38 #include "evdev.h"
39 #include "filter.h"
40 #include "libinput-private.h"
41
42 #define DEFAULT_AXIS_STEP_DISTANCE 10
43
44 enum evdev_key_type {
45         EVDEV_KEY_TYPE_NONE,
46         EVDEV_KEY_TYPE_KEY,
47         EVDEV_KEY_TYPE_BUTTON,
48 };
49
50 static void
51 set_key_down(struct evdev_device *device, int code, int pressed)
52 {
53         long_set_bit_state(device->key_mask, code, pressed);
54 }
55
56 static int
57 is_key_down(struct evdev_device *device, int code)
58 {
59         return long_bit_is_set(device->key_mask, code);
60 }
61
62 void
63 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)
64 {
65         static const struct {
66                 enum libinput_led weston;
67                 int evdev;
68         } map[] = {
69                 { LIBINPUT_LED_NUM_LOCK, LED_NUML },
70                 { LIBINPUT_LED_CAPS_LOCK, LED_CAPSL },
71                 { LIBINPUT_LED_SCROLL_LOCK, LED_SCROLLL },
72         };
73         struct input_event ev[ARRAY_LENGTH(map) + 1];
74         unsigned int i;
75
76         if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
77                 return;
78
79         memset(ev, 0, sizeof(ev));
80         for (i = 0; i < ARRAY_LENGTH(map); i++) {
81                 ev[i].type = EV_LED;
82                 ev[i].code = map[i].evdev;
83                 ev[i].value = !!(leds & map[i].weston);
84         }
85         ev[i].type = EV_SYN;
86         ev[i].code = SYN_REPORT;
87
88         i = write(device->fd, ev, sizeof ev);
89         (void)i; /* no, we really don't care about the return value */
90 }
91
92 static void
93 transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
94 {
95         if (!device->abs.apply_calibration) {
96                 *x = device->abs.x;
97                 *y = device->abs.y;
98                 return;
99         } else {
100                 *x = device->abs.x * device->abs.calibration[0] +
101                         device->abs.y * device->abs.calibration[1] +
102                         device->abs.calibration[2];
103
104                 *y = device->abs.x * device->abs.calibration[3] +
105                         device->abs.y * device->abs.calibration[4] +
106                         device->abs.calibration[5];
107         }
108 }
109
110 static inline double
111 scale_axis(const struct input_absinfo *absinfo, double val, double to_range)
112 {
113         return (val - absinfo->minimum) * to_range /
114                 (absinfo->maximum - absinfo->minimum + 1);
115 }
116
117 double
118 evdev_device_transform_x(struct evdev_device *device,
119                          double x,
120                          uint32_t width)
121 {
122         return scale_axis(device->abs.absinfo_x, x, width);
123 }
124
125 double
126 evdev_device_transform_y(struct evdev_device *device,
127                          double y,
128                          uint32_t height)
129 {
130         return scale_axis(device->abs.absinfo_y, y, height);
131 }
132
133 static void
134 evdev_flush_pending_event(struct evdev_device *device, uint64_t time)
135 {
136         struct libinput *libinput = device->base.seat->libinput;
137         struct motion_params motion;
138         int32_t cx, cy;
139         double x, y;
140         int slot;
141         int seat_slot;
142         struct libinput_device *base = &device->base;
143         struct libinput_seat *seat = base->seat;
144
145         slot = device->mt.slot;
146
147         switch (device->pending_event) {
148         case EVDEV_NONE:
149                 return;
150         case EVDEV_RELATIVE_MOTION:
151                 motion.dx = device->rel.dx;
152                 motion.dy = device->rel.dy;
153                 device->rel.dx = 0;
154                 device->rel.dy = 0;
155
156                 /* Apply pointer acceleration. */
157                 filter_dispatch(device->pointer.filter, &motion, device, time);
158
159                 if (motion.dx == 0.0 && motion.dy == 0.0)
160                         break;
161
162                 pointer_notify_motion(base, time, motion.dx, motion.dy);
163                 break;
164         case EVDEV_ABSOLUTE_MT_DOWN:
165                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
166                         break;
167
168                 if (device->mt.slots[slot].seat_slot != -1) {
169                         log_bug_kernel(libinput,
170                                        "%s: Driver sent multiple touch down for the "
171                                        "same slot", device->devnode);
172                         break;
173                 }
174
175                 seat_slot = ffs(~seat->slot_map) - 1;
176                 device->mt.slots[slot].seat_slot = seat_slot;
177
178                 if (seat_slot == -1)
179                         break;
180
181                 seat->slot_map |= 1 << seat_slot;
182                 x = device->mt.slots[slot].x;
183                 y = device->mt.slots[slot].y;
184
185                 touch_notify_touch_down(base, time, slot, seat_slot, x, y);
186                 break;
187         case EVDEV_ABSOLUTE_MT_MOTION:
188                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
189                         break;
190
191                 seat_slot = device->mt.slots[slot].seat_slot;
192                 x = device->mt.slots[slot].x;
193                 y = device->mt.slots[slot].y;
194
195                 if (seat_slot == -1)
196                         break;
197
198                 touch_notify_touch_motion(base, time, slot, seat_slot, x, y);
199                 break;
200         case EVDEV_ABSOLUTE_MT_UP:
201                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
202                         break;
203
204                 seat_slot = device->mt.slots[slot].seat_slot;
205                 device->mt.slots[slot].seat_slot = -1;
206
207                 if (seat_slot == -1)
208                         break;
209
210                 seat->slot_map &= ~(1 << seat_slot);
211
212                 touch_notify_touch_up(base, time, slot, seat_slot);
213                 break;
214         case EVDEV_ABSOLUTE_TOUCH_DOWN:
215                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
216                         break;
217
218                 if (device->abs.seat_slot != -1) {
219                         log_bug_kernel(libinput,
220                                        "%s: Driver sent multiple touch down for the "
221                                        "same slot", device->devnode);
222                         break;
223                 }
224
225                 seat_slot = ffs(~seat->slot_map) - 1;
226                 device->abs.seat_slot = seat_slot;
227
228                 if (seat_slot == -1)
229                         break;
230
231                 seat->slot_map |= 1 << seat_slot;
232
233                 transform_absolute(device, &cx, &cy);
234
235                 touch_notify_touch_down(base, time, -1, seat_slot, cx, cy);
236                 break;
237         case EVDEV_ABSOLUTE_MOTION:
238                 transform_absolute(device, &cx, &cy);
239                 x = cx;
240                 y = cy;
241
242                 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
243                         seat_slot = device->abs.seat_slot;
244
245                         if (seat_slot == -1)
246                                 break;
247
248                         touch_notify_touch_motion(base, time, -1, seat_slot, x, y);
249                 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
250                         pointer_notify_motion_absolute(base, time, x, y);
251                 }
252                 break;
253         case EVDEV_ABSOLUTE_TOUCH_UP:
254                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
255                         break;
256
257                 seat_slot = device->abs.seat_slot;
258                 device->abs.seat_slot = -1;
259
260                 if (seat_slot == -1)
261                         break;
262
263                 seat->slot_map &= ~(1 << seat_slot);
264
265                 touch_notify_touch_up(base, time, -1, seat_slot);
266                 break;
267         default:
268                 assert(0 && "Unknown pending event type");
269                 break;
270         }
271
272         device->pending_event = EVDEV_NONE;
273 }
274
275 static enum evdev_key_type
276 get_key_type(uint16_t code)
277 {
278         if (code == BTN_TOUCH)
279                 return EVDEV_KEY_TYPE_NONE;
280
281         if (code >= KEY_ESC && code <= KEY_MICMUTE)
282                 return EVDEV_KEY_TYPE_KEY;
283         if (code >= BTN_MISC && code <= BTN_GEAR_UP)
284                 return EVDEV_KEY_TYPE_BUTTON;
285         if (code >= KEY_OK && code <= KEY_LIGHTS_TOGGLE)
286                 return EVDEV_KEY_TYPE_KEY;
287         if (code >= BTN_DPAD_UP && code <= BTN_TRIGGER_HAPPY40)
288                 return EVDEV_KEY_TYPE_BUTTON;
289         return EVDEV_KEY_TYPE_NONE;
290 }
291
292 static void
293 evdev_process_touch_button(struct evdev_device *device,
294                            uint64_t time, int value)
295 {
296         if (device->pending_event != EVDEV_NONE &&
297             device->pending_event != EVDEV_ABSOLUTE_MOTION)
298                 evdev_flush_pending_event(device, time);
299
300         device->pending_event = (value ?
301                                  EVDEV_ABSOLUTE_TOUCH_DOWN :
302                                  EVDEV_ABSOLUTE_TOUCH_UP);
303 }
304
305 static inline void
306 evdev_process_key(struct evdev_device *device,
307                   struct input_event *e, uint64_t time)
308 {
309         enum evdev_key_type type;
310
311         /* ignore kernel key repeat */
312         if (e->value == 2)
313                 return;
314
315         if (e->code == BTN_TOUCH) {
316                 if (!device->is_mt)
317                         evdev_process_touch_button(device, time, e->value);
318                 return;
319         }
320
321         evdev_flush_pending_event(device, time);
322
323         type = get_key_type(e->code);
324
325         /* Ignore key release events from the kernel for keys that libinput
326          * never got a pressed event for. */
327         if (e->value == 0) {
328                 switch (type) {
329                 case EVDEV_KEY_TYPE_NONE:
330                         break;
331                 case EVDEV_KEY_TYPE_KEY:
332                 case EVDEV_KEY_TYPE_BUTTON:
333                         if (!is_key_down(device, e->code))
334                                 return;
335                 }
336         }
337
338         set_key_down(device, e->code, e->value);
339
340         switch (type) {
341         case EVDEV_KEY_TYPE_NONE:
342                 break;
343         case EVDEV_KEY_TYPE_KEY:
344                 keyboard_notify_key(
345                         &device->base,
346                         time,
347                         e->code,
348                         e->value ? LIBINPUT_KEY_STATE_PRESSED :
349                                    LIBINPUT_KEY_STATE_RELEASED);
350                 break;
351         case EVDEV_KEY_TYPE_BUTTON:
352                 pointer_notify_button(
353                         &device->base,
354                         time,
355                         e->code,
356                         e->value ? LIBINPUT_BUTTON_STATE_PRESSED :
357                                    LIBINPUT_BUTTON_STATE_RELEASED);
358                 break;
359         }
360 }
361
362 static void
363 evdev_process_touch(struct evdev_device *device,
364                     struct input_event *e,
365                     uint64_t time)
366 {
367         switch (e->code) {
368         case ABS_MT_SLOT:
369                 evdev_flush_pending_event(device, time);
370                 device->mt.slot = e->value;
371                 break;
372         case ABS_MT_TRACKING_ID:
373                 if (device->pending_event != EVDEV_NONE &&
374                     device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
375                         evdev_flush_pending_event(device, time);
376                 if (e->value >= 0)
377                         device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
378                 else
379                         device->pending_event = EVDEV_ABSOLUTE_MT_UP;
380                 break;
381         case ABS_MT_POSITION_X:
382                 device->mt.slots[device->mt.slot].x = e->value;
383                 if (device->pending_event == EVDEV_NONE)
384                         device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
385                 break;
386         case ABS_MT_POSITION_Y:
387                 device->mt.slots[device->mt.slot].y = e->value;
388                 if (device->pending_event == EVDEV_NONE)
389                         device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
390                 break;
391         }
392 }
393
394 static inline void
395 evdev_process_absolute_motion(struct evdev_device *device,
396                               struct input_event *e)
397 {
398         switch (e->code) {
399         case ABS_X:
400                 device->abs.x = e->value;
401                 if (device->pending_event == EVDEV_NONE)
402                         device->pending_event = EVDEV_ABSOLUTE_MOTION;
403                 break;
404         case ABS_Y:
405                 device->abs.y = e->value;
406                 if (device->pending_event == EVDEV_NONE)
407                         device->pending_event = EVDEV_ABSOLUTE_MOTION;
408                 break;
409         }
410 }
411
412 static inline void
413 evdev_process_relative(struct evdev_device *device,
414                        struct input_event *e, uint64_t time)
415 {
416         struct libinput_device *base = &device->base;
417
418         switch (e->code) {
419         case REL_X:
420                 if (device->pending_event != EVDEV_RELATIVE_MOTION)
421                         evdev_flush_pending_event(device, time);
422                 device->rel.dx += e->value;
423                 device->pending_event = EVDEV_RELATIVE_MOTION;
424                 break;
425         case REL_Y:
426                 if (device->pending_event != EVDEV_RELATIVE_MOTION)
427                         evdev_flush_pending_event(device, time);
428                 device->rel.dy += e->value;
429                 device->pending_event = EVDEV_RELATIVE_MOTION;
430                 break;
431         case REL_WHEEL:
432                 evdev_flush_pending_event(device, time);
433                 pointer_notify_axis(
434                         base,
435                         time,
436                         LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
437                         -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
438                 break;
439         case REL_HWHEEL:
440                 evdev_flush_pending_event(device, time);
441                 switch (e->value) {
442                 case -1:
443                         /* Scroll left */
444                 case 1:
445                         /* Scroll right */
446                         pointer_notify_axis(
447                                 base,
448                                 time,
449                                 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
450                                 e->value * DEFAULT_AXIS_STEP_DISTANCE);
451                         break;
452                 default:
453                         break;
454
455                 }
456         }
457 }
458
459 static inline void
460 evdev_process_absolute(struct evdev_device *device,
461                        struct input_event *e,
462                        uint64_t time)
463 {
464         if (device->is_mt) {
465                 evdev_process_touch(device, e, time);
466         } else {
467                 evdev_process_absolute_motion(device, e);
468         }
469 }
470
471 static inline int
472 evdev_need_touch_frame(struct evdev_device *device)
473 {
474         if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
475                 return 0;
476
477         switch (device->pending_event) {
478         case EVDEV_NONE:
479         case EVDEV_RELATIVE_MOTION:
480                 break;
481         case EVDEV_ABSOLUTE_MT_DOWN:
482         case EVDEV_ABSOLUTE_MT_MOTION:
483         case EVDEV_ABSOLUTE_MT_UP:
484         case EVDEV_ABSOLUTE_TOUCH_DOWN:
485         case EVDEV_ABSOLUTE_TOUCH_UP:
486         case EVDEV_ABSOLUTE_MOTION:
487                 return 1;
488         }
489
490         return 0;
491 }
492
493 static void
494 fallback_process(struct evdev_dispatch *dispatch,
495                  struct evdev_device *device,
496                  struct input_event *event,
497                  uint64_t time)
498 {
499         int need_frame = 0;
500
501         switch (event->type) {
502         case EV_REL:
503                 evdev_process_relative(device, event, time);
504                 break;
505         case EV_ABS:
506                 evdev_process_absolute(device, event, time);
507                 break;
508         case EV_KEY:
509                 evdev_process_key(device, event, time);
510                 break;
511         case EV_SYN:
512                 need_frame = evdev_need_touch_frame(device);
513                 evdev_flush_pending_event(device, time);
514                 if (need_frame)
515                         touch_notify_frame(&device->base, time);
516                 break;
517         }
518 }
519
520 static void
521 fallback_destroy(struct evdev_dispatch *dispatch)
522 {
523         free(dispatch);
524 }
525
526 struct evdev_dispatch_interface fallback_interface = {
527         fallback_process,
528         fallback_destroy
529 };
530
531 static struct evdev_dispatch *
532 fallback_dispatch_create(void)
533 {
534         struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
535         if (dispatch == NULL)
536                 return NULL;
537
538         dispatch->interface = &fallback_interface;
539
540         return dispatch;
541 }
542
543 static inline void
544 evdev_process_event(struct evdev_device *device, struct input_event *e)
545 {
546         struct evdev_dispatch *dispatch = device->dispatch;
547         uint64_t time = e->time.tv_sec * 1000ULL + e->time.tv_usec / 1000;
548
549         dispatch->interface->process(dispatch, device, e, time);
550 }
551
552 static inline void
553 evdev_device_dispatch_one(struct evdev_device *device,
554                           struct input_event *ev)
555 {
556         if (!device->mtdev) {
557                 evdev_process_event(device, ev);
558         } else {
559                 mtdev_put_event(device->mtdev, ev);
560                 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
561                         while (!mtdev_empty(device->mtdev)) {
562                                 struct input_event e;
563                                 mtdev_get_event(device->mtdev, &e);
564                                 evdev_process_event(device, &e);
565                         }
566                 }
567         }
568 }
569
570 static int
571 evdev_sync_device(struct evdev_device *device)
572 {
573         struct input_event ev;
574         int rc;
575
576         do {
577                 rc = libevdev_next_event(device->evdev,
578                                          LIBEVDEV_READ_FLAG_SYNC, &ev);
579                 if (rc < 0)
580                         break;
581                 evdev_device_dispatch_one(device, &ev);
582         } while (rc == LIBEVDEV_READ_STATUS_SYNC);
583
584         return rc == -EAGAIN ? 0 : rc;
585 }
586
587 static void
588 evdev_device_dispatch(void *data)
589 {
590         struct evdev_device *device = data;
591         struct libinput *libinput = device->base.seat->libinput;
592         struct input_event ev;
593         int rc;
594
595         /* If the compositor is repainting, this function is called only once
596          * per frame and we have to process all the events available on the
597          * fd, otherwise there will be input lag. */
598         do {
599                 rc = libevdev_next_event(device->evdev,
600                                          LIBEVDEV_READ_FLAG_NORMAL, &ev);
601                 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
602                         /* send one more sync event so we handle all
603                            currently pending events before we sync up
604                            to the current state */
605                         ev.code = SYN_REPORT;
606                         evdev_device_dispatch_one(device, &ev);
607
608                         rc = evdev_sync_device(device);
609                         if (rc == 0)
610                                 rc = LIBEVDEV_READ_STATUS_SUCCESS;
611                 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
612                         evdev_device_dispatch_one(device, &ev);
613                 }
614         } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
615
616         if (rc != -EAGAIN && rc != -EINTR) {
617                 libinput_remove_source(libinput, device->source);
618                 device->source = NULL;
619         }
620 }
621
622 static int
623 configure_pointer_acceleration(struct evdev_device *device)
624 {
625         device->pointer.filter =
626                 create_pointer_accelator_filter(
627                         pointer_accel_profile_smooth_simple);
628         if (!device->pointer.filter)
629                 return -1;
630
631         return 0;
632 }
633
634 static int
635 evdev_configure_device(struct evdev_device *device)
636 {
637         struct libinput *libinput = device->base.seat->libinput;
638         struct libevdev *evdev = device->evdev;
639         const struct input_absinfo *absinfo;
640         struct input_absinfo fixed;
641         int has_abs, has_rel, has_mt;
642         int has_button, has_keyboard, has_touch;
643         struct mt_slot *slots;
644         int num_slots;
645         int active_slot;
646         int slot;
647         unsigned int i;
648
649         has_rel = 0;
650         has_abs = 0;
651         has_mt = 0;
652         has_button = 0;
653         has_keyboard = 0;
654         has_touch = 0;
655
656         if (libevdev_has_event_type(evdev, EV_ABS)) {
657
658                 if ((absinfo = libevdev_get_abs_info(evdev, ABS_X))) {
659                         if (absinfo->resolution == 0) {
660                                 fixed = *absinfo;
661                                 fixed.resolution = 1;
662                                 libevdev_set_abs_info(evdev, ABS_X, &fixed);
663                                 device->abs.fake_resolution = 1;
664                         }
665                         device->abs.absinfo_x = absinfo;
666                         has_abs = 1;
667                 }
668                 if ((absinfo = libevdev_get_abs_info(evdev, ABS_Y))) {
669                         if (absinfo->resolution == 0) {
670                                 fixed = *absinfo;
671                                 fixed.resolution = 1;
672                                 libevdev_set_abs_info(evdev, ABS_Y, &fixed);
673                                 device->abs.fake_resolution = 1;
674                         }
675                         device->abs.absinfo_y = absinfo;
676                         has_abs = 1;
677                 }
678                 /* We only handle the slotted Protocol B in weston.
679                    Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
680                    require mtdev for conversion. */
681                 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
682                     libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y)) {
683                         absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
684                         if (absinfo->resolution == 0) {
685                                 fixed = *absinfo;
686                                 fixed.resolution = 1;
687                                 libevdev_set_abs_info(evdev,
688                                                       ABS_MT_POSITION_X,
689                                                       &fixed);
690                                 device->abs.fake_resolution = 1;
691                         }
692                         device->abs.absinfo_x = absinfo;
693                         absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
694                         if (absinfo->resolution == 0) {
695                                 fixed = *absinfo;
696                                 fixed.resolution = 1;
697                                 libevdev_set_abs_info(evdev,
698                                                       ABS_MT_POSITION_Y,
699                                                       &fixed);
700                                 device->abs.fake_resolution = 1;
701                         }
702                         device->abs.absinfo_y = absinfo;
703                         device->is_mt = 1;
704                         has_touch = 1;
705                         has_mt = 1;
706
707                         if (!libevdev_has_event_code(evdev,
708                                                      EV_ABS, ABS_MT_SLOT)) {
709                                 device->mtdev = mtdev_new_open(device->fd);
710                                 if (!device->mtdev)
711                                         return -1;
712
713                                 num_slots = device->mtdev->caps.slot.maximum;
714                                 if (device->mtdev->caps.slot.minimum < 0 ||
715                                     num_slots <= 0)
716                                         return -1;
717                                 active_slot = device->mtdev->caps.slot.value;
718                         } else {
719                                 num_slots = libevdev_get_num_slots(device->evdev);
720                                 active_slot = libevdev_get_current_slot(evdev);
721                         }
722
723                         slots = calloc(num_slots, sizeof(struct mt_slot));
724                         if (!slots)
725                                 return -1;
726
727                         for (slot = 0; slot < num_slots; ++slot) {
728                                 slots[slot].seat_slot = -1;
729                                 slots[slot].x = 0;
730                                 slots[slot].y = 0;
731                         }
732                         device->mt.slots = slots;
733                         device->mt.slots_len = num_slots;
734                         device->mt.slot = active_slot;
735                 }
736         }
737         if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
738             libevdev_has_event_code(evdev, EV_REL, REL_Y))
739                 has_rel = 1;
740
741         if (libevdev_has_event_type(evdev, EV_KEY)) {
742                 if (!libevdev_has_property(evdev, INPUT_PROP_DIRECT) &&
743                     libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_FINGER) &&
744                     !libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_PEN) &&
745                     (has_abs || has_mt)) {
746                         device->dispatch = evdev_mt_touchpad_create(device);
747                         log_info(libinput,
748                                  "input device '%s', %s is a touchpad\n",
749                                  device->devname, device->devnode);
750                         return device->dispatch == NULL ? -1 : 0;
751                 }
752
753                 for (i = 0; i < KEY_MAX; i++) {
754                         if (libevdev_has_event_code(evdev, EV_KEY, i)) {
755                                 switch (get_key_type(i)) {
756                                 case EVDEV_KEY_TYPE_NONE:
757                                         break;
758                                 case EVDEV_KEY_TYPE_KEY:
759                                         has_keyboard = 1;
760                                         break;
761                                 case EVDEV_KEY_TYPE_BUTTON:
762                                         has_button = 1;
763                                         break;
764                                 }
765                         }
766                 }
767
768                 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH))
769                         has_touch = 1;
770         }
771         if (libevdev_has_event_type(evdev, EV_LED))
772                 has_keyboard = 1;
773
774         if ((has_abs || has_rel) && has_button) {
775                 if (configure_pointer_acceleration(device) == -1)
776                         return -1;
777
778                 device->seat_caps |= EVDEV_DEVICE_POINTER;
779
780                 log_info(libinput,
781                          "input device '%s', %s is a pointer caps =%s%s%s\n",
782                          device->devname, device->devnode,
783                          has_abs ? " absolute-motion" : "",
784                          has_rel ? " relative-motion": "",
785                          has_button ? " button" : "");
786         }
787         if (has_keyboard) {
788                 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
789                 log_info(libinput,
790                          "input device '%s', %s is a keyboard\n",
791                          device->devname, device->devnode);
792         }
793         if (has_touch && !has_button) {
794                 device->seat_caps |= EVDEV_DEVICE_TOUCH;
795                 log_info(libinput,
796                          "input device '%s', %s is a touch device\n",
797                          device->devname, device->devnode);
798         }
799
800         return 0;
801 }
802
803 struct evdev_device *
804 evdev_device_create(struct libinput_seat *seat,
805                     const char *devnode,
806                     const char *sysname)
807 {
808         struct libinput *libinput = seat->libinput;
809         struct evdev_device *device;
810         int rc;
811         int fd;
812         int unhandled_device = 0;
813
814         /* Use non-blocking mode so that we can loop on read on
815          * evdev_device_data() until all events on the fd are
816          * read.  mtdev_get() also expects this. */
817         fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
818         if (fd < 0) {
819                 log_info(libinput,
820                          "opening input device '%s' failed (%s).\n",
821                          devnode, strerror(-fd));
822                 return NULL;
823         }
824
825         device = zalloc(sizeof *device);
826         if (device == NULL)
827                 return NULL;
828
829         libinput_device_init(&device->base, seat);
830
831         rc = libevdev_new_from_fd(fd, &device->evdev);
832         if (rc != 0)
833                 return NULL;
834
835         libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
836
837         device->seat_caps = 0;
838         device->is_mt = 0;
839         device->mtdev = NULL;
840         device->devnode = strdup(devnode);
841         device->sysname = strdup(sysname);
842         device->rel.dx = 0;
843         device->rel.dy = 0;
844         device->abs.seat_slot = -1;
845         device->dispatch = NULL;
846         device->fd = fd;
847         device->pending_event = EVDEV_NONE;
848         device->devname = libevdev_get_name(device->evdev);
849
850         libinput_seat_ref(seat);
851
852         if (evdev_configure_device(device) == -1)
853                 goto err;
854
855         if (device->seat_caps == 0) {
856                 unhandled_device = 1;
857                 goto err;
858         }
859
860         /* If the dispatch was not set up use the fallback. */
861         if (device->dispatch == NULL)
862                 device->dispatch = fallback_dispatch_create();
863         if (device->dispatch == NULL)
864                 goto err;
865
866         device->source =
867                 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
868         if (!device->source)
869                 goto err;
870
871         list_insert(seat->devices_list.prev, &device->base.link);
872         notify_added_device(&device->base);
873
874         return device;
875
876 err:
877         if (fd >= 0)
878                 close_restricted(libinput, fd);
879         evdev_device_destroy(device);
880
881         return unhandled_device ? EVDEV_UNHANDLED_DEVICE :  NULL;
882 }
883
884 int
885 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
886 {
887         memset(keys, 0, size);
888         return 0;
889 }
890
891 const char *
892 evdev_device_get_output(struct evdev_device *device)
893 {
894         return device->output_name;
895 }
896
897 const char *
898 evdev_device_get_sysname(struct evdev_device *device)
899 {
900         return device->sysname;
901 }
902
903 const char *
904 evdev_device_get_name(struct evdev_device *device)
905 {
906         return device->devname;
907 }
908
909 unsigned int
910 evdev_device_get_id_product(struct evdev_device *device)
911 {
912         return libevdev_get_id_product(device->evdev);
913 }
914
915 unsigned int
916 evdev_device_get_id_vendor(struct evdev_device *device)
917 {
918         return libevdev_get_id_vendor(device->evdev);
919 }
920
921 void
922 evdev_device_calibrate(struct evdev_device *device, float calibration[6])
923 {
924         device->abs.apply_calibration = 1;
925         memcpy(device->abs.calibration, calibration, sizeof device->abs.calibration);
926 }
927
928 int
929 evdev_device_has_capability(struct evdev_device *device,
930                             enum libinput_device_capability capability)
931 {
932         switch (capability) {
933         case LIBINPUT_DEVICE_CAP_POINTER:
934                 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
935         case LIBINPUT_DEVICE_CAP_KEYBOARD:
936                 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
937         case LIBINPUT_DEVICE_CAP_TOUCH:
938                 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
939         default:
940                 return 0;
941         }
942 }
943
944 int
945 evdev_device_get_size(struct evdev_device *device,
946                       double *width,
947                       double *height)
948 {
949         const struct input_absinfo *x, *y;
950
951         x = libevdev_get_abs_info(device->evdev, ABS_X);
952         y = libevdev_get_abs_info(device->evdev, ABS_Y);
953
954         if (!x || !y || device->abs.fake_resolution ||
955             !x->resolution || !y->resolution)
956                 return -1;
957
958         *width = evdev_convert_to_mm(x, x->maximum);
959         *height = evdev_convert_to_mm(y, y->maximum);
960
961         return 0;
962 }
963
964 void
965 evdev_device_remove(struct evdev_device *device)
966 {
967         if (device->source)
968                 libinput_remove_source(device->base.seat->libinput,
969                                        device->source);
970
971         if (device->mtdev)
972                 mtdev_close_delete(device->mtdev);
973         close_restricted(device->base.seat->libinput, device->fd);
974         device->fd = -1;
975         list_remove(&device->base.link);
976
977         notify_removed_device(&device->base);
978         libinput_device_unref(&device->base);
979 }
980
981 void
982 evdev_device_destroy(struct evdev_device *device)
983 {
984         struct evdev_dispatch *dispatch;
985
986         dispatch = device->dispatch;
987         if (dispatch)
988                 dispatch->interface->destroy(dispatch);
989
990         filter_destroy(device->pointer.filter);
991         libinput_seat_unref(device->base.seat);
992         libevdev_free(device->evdev);
993         free(device->mt.slots);
994         free(device->devnode);
995         free(device->sysname);
996         free(device);
997 }