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