8d11e7fcd67e6f2867f17b687fe6dcbe7bad860c
[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         struct mt_slot *slots;
551         int num_slots;
552         int active_slot;
553         int slot;
554         unsigned int i;
555
556         has_rel = 0;
557         has_abs = 0;
558         has_mt = 0;
559         has_button = 0;
560         has_keyboard = 0;
561         has_touch = 0;
562
563         if (libevdev_has_event_type(evdev, EV_ABS)) {
564
565                 if ((absinfo = libevdev_get_abs_info(evdev, ABS_X))) {
566                         device->abs.min_x = absinfo->minimum;
567                         device->abs.max_x = absinfo->maximum;
568                         has_abs = 1;
569                 }
570                 if ((absinfo = libevdev_get_abs_info(evdev, ABS_Y))) {
571                         device->abs.min_y = absinfo->minimum;
572                         device->abs.max_y = absinfo->maximum;
573                         has_abs = 1;
574                 }
575                 /* We only handle the slotted Protocol B in weston.
576                    Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
577                    require mtdev for conversion. */
578                 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
579                     libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y)) {
580                         absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
581                         device->abs.min_x = absinfo->minimum;
582                         device->abs.max_x = absinfo->maximum;
583                         absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
584                         device->abs.min_y = absinfo->minimum;
585                         device->abs.max_y = absinfo->maximum;
586                         device->is_mt = 1;
587                         has_touch = 1;
588                         has_mt = 1;
589
590                         if (!libevdev_has_event_code(evdev,
591                                                      EV_ABS, ABS_MT_SLOT)) {
592                                 device->mtdev = mtdev_new_open(device->fd);
593                                 if (!device->mtdev)
594                                         return -1;
595
596                                 num_slots = device->mtdev->caps.slot.maximum;
597                                 if (device->mtdev->caps.slot.minimum < 0 ||
598                                     num_slots <= 0)
599                                         return -1;
600                                 active_slot = device->mtdev->caps.slot.value;
601                         } else {
602                                 num_slots = libevdev_get_num_slots(device->evdev);
603                                 active_slot = libevdev_get_current_slot(evdev);
604                         }
605
606                         slots = calloc(num_slots, sizeof(struct mt_slot));
607                         if (!slots)
608                                 return -1;
609
610                         for (slot = 0; slot < num_slots; ++slot) {
611                                 slots[slot].seat_slot = -1;
612                                 slots[slot].x = 0;
613                                 slots[slot].y = 0;
614                         }
615                         device->mt.slots = slots;
616                         device->mt.slots_len = num_slots;
617                         device->mt.slot = active_slot;
618                 }
619         }
620         if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
621             libevdev_has_event_code(evdev, EV_REL, REL_Y))
622                         has_rel = 1;
623
624         if (libevdev_has_event_type(evdev, EV_KEY)) {
625                 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_FINGER) &&
626                     !libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_PEN) &&
627                     (has_abs || has_mt)) {
628                         device->dispatch = evdev_mt_touchpad_create(device);
629                         log_info("input device '%s', %s is a touchpad\n",
630                                  device->devname, device->devnode);
631                 }
632                 for (i = KEY_ESC; i < KEY_MAX; i++) {
633                         if (i >= BTN_MISC && i < KEY_OK)
634                                 continue;
635                         if (libevdev_has_event_code(evdev, EV_KEY, i)) {
636                                 has_keyboard = 1;
637                                 break;
638                         }
639                 }
640                 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH))
641                         has_touch = 1;
642                 for (i = BTN_MISC; i < BTN_JOYSTICK; i++) {
643                         if (libevdev_has_event_code(evdev, EV_KEY, i)) {
644                                 has_button = 1;
645                                 break;
646                         }
647                 }
648         }
649         if (libevdev_has_event_type(evdev, EV_LED))
650                 has_keyboard = 1;
651
652         if ((has_abs || has_rel) && has_button) {
653                 device->seat_caps |= EVDEV_DEVICE_POINTER;
654                 log_info("input device '%s', %s is a pointer caps =%s%s%s\n",
655                          device->devname, device->devnode,
656                          has_abs ? " absolute-motion" : "",
657                          has_rel ? " relative-motion": "",
658                          has_button ? " button" : "");
659         }
660         if (has_keyboard) {
661                 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
662                 log_info("input device '%s', %s is a keyboard\n",
663                          device->devname, device->devnode);
664         }
665         if (has_touch && !has_button) {
666                 device->seat_caps |= EVDEV_DEVICE_TOUCH;
667                 log_info("input device '%s', %s is a touch device\n",
668                          device->devname, device->devnode);
669         }
670
671         return 0;
672 }
673
674 struct evdev_device *
675 evdev_device_create(struct libinput_seat *seat,
676                     const char *devnode,
677                     const char *sysname)
678 {
679         struct libinput *libinput = seat->libinput;
680         struct evdev_device *device;
681         int rc;
682         int fd;
683         int unhandled_device = 0;
684
685         /* Use non-blocking mode so that we can loop on read on
686          * evdev_device_data() until all events on the fd are
687          * read.  mtdev_get() also expects this. */
688         fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
689         if (fd < 0) {
690                 log_info("opening input device '%s' failed (%s).\n",
691                          devnode, strerror(-fd));
692                 return NULL;
693         }
694
695         device = zalloc(sizeof *device);
696         if (device == NULL)
697                 return NULL;
698
699         libinput_device_init(&device->base, seat);
700
701         rc = libevdev_new_from_fd(fd, &device->evdev);
702         if (rc != 0)
703                 return NULL;
704
705         libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
706
707         device->seat_caps = 0;
708         device->is_mt = 0;
709         device->mtdev = NULL;
710         device->devnode = strdup(devnode);
711         device->sysname = strdup(sysname);
712         device->rel.dx = 0;
713         device->rel.dy = 0;
714         device->dispatch = NULL;
715         device->fd = fd;
716         device->pending_event = EVDEV_NONE;
717         device->devname = libevdev_get_name(device->evdev);
718
719         libinput_seat_ref(seat);
720
721         if (evdev_configure_device(device) == -1)
722                 goto err;
723
724         if (device->seat_caps == 0) {
725                 unhandled_device = 1;
726                 goto err;
727         }
728
729         /* If the dispatch was not set up use the fallback. */
730         if (device->dispatch == NULL)
731                 device->dispatch = fallback_dispatch_create();
732         if (device->dispatch == NULL)
733                 goto err;
734
735         device->source =
736                 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
737         if (!device->source)
738                 goto err;
739
740         list_insert(seat->devices_list.prev, &device->base.link);
741         notify_added_device(&device->base);
742
743         return device;
744
745 err:
746         if (fd >= 0)
747                 close_restricted(libinput, fd);
748         evdev_device_destroy(device);
749
750         return unhandled_device ? EVDEV_UNHANDLED_DEVICE :  NULL;
751 }
752
753 int
754 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
755 {
756         int len;
757
758         memset(keys, 0, size);
759         len = ioctl(device->fd, EVIOCGKEY(size), keys);
760
761         return (len == -1) ? -errno : len;
762 }
763
764 const char *
765 evdev_device_get_output(struct evdev_device *device)
766 {
767         return device->output_name;
768 }
769
770 const char *
771 evdev_device_get_sysname(struct evdev_device *device)
772 {
773         return device->sysname;
774 }
775
776 void
777 evdev_device_calibrate(struct evdev_device *device, float calibration[6])
778 {
779         device->abs.apply_calibration = 1;
780         memcpy(device->abs.calibration, calibration, sizeof device->abs.calibration);
781 }
782
783 int
784 evdev_device_has_capability(struct evdev_device *device,
785                             enum libinput_device_capability capability)
786 {
787         switch (capability) {
788         case LIBINPUT_DEVICE_CAP_POINTER:
789                 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
790         case LIBINPUT_DEVICE_CAP_KEYBOARD:
791                 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
792         case LIBINPUT_DEVICE_CAP_TOUCH:
793                 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
794         default:
795                 return 0;
796         }
797 }
798
799 void
800 evdev_device_remove(struct evdev_device *device)
801 {
802         if (device->source)
803                 libinput_remove_source(device->base.seat->libinput,
804                                        device->source);
805
806         if (device->mtdev)
807                 mtdev_close_delete(device->mtdev);
808         close_restricted(device->base.seat->libinput, device->fd);
809         device->fd = -1;
810         list_remove(&device->base.link);
811
812         notify_removed_device(&device->base);
813         libinput_device_unref(&device->base);
814 }
815
816 void
817 evdev_device_destroy(struct evdev_device *device)
818 {
819         struct evdev_dispatch *dispatch;
820
821         dispatch = device->dispatch;
822         if (dispatch)
823                 dispatch->interface->destroy(dispatch);
824
825         libinput_seat_unref(device->base.seat);
826         libevdev_free(device->evdev);
827         free(device->mt.slots);
828         free(device->devnode);
829         free(device->sysname);
830         free(device);
831 }