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