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