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