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