Fix two doxygen errors
[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 #include <math.h>
36
37 #include "libinput.h"
38 #include "evdev.h"
39 #include "filter.h"
40 #include "libinput-private.h"
41
42 #define DEFAULT_AXIS_STEP_DISTANCE 10
43
44 void
45 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)
46 {
47         static const struct {
48                 enum libinput_led weston;
49                 int evdev;
50         } map[] = {
51                 { LIBINPUT_LED_NUM_LOCK, LED_NUML },
52                 { LIBINPUT_LED_CAPS_LOCK, LED_CAPSL },
53                 { LIBINPUT_LED_SCROLL_LOCK, LED_SCROLLL },
54         };
55         struct input_event ev[ARRAY_LENGTH(map) + 1];
56         unsigned int i;
57
58         if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
59                 return;
60
61         memset(ev, 0, sizeof(ev));
62         for (i = 0; i < ARRAY_LENGTH(map); i++) {
63                 ev[i].type = EV_LED;
64                 ev[i].code = map[i].evdev;
65                 ev[i].value = !!(leds & map[i].weston);
66         }
67         ev[i].type = EV_SYN;
68         ev[i].code = SYN_REPORT;
69
70         i = write(device->fd, ev, sizeof ev);
71         (void)i; /* no, we really don't care about the return value */
72 }
73
74 static void
75 transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
76 {
77         if (!device->abs.apply_calibration) {
78                 *x = device->abs.x;
79                 *y = device->abs.y;
80                 return;
81         } else {
82                 *x = device->abs.x * device->abs.calibration[0] +
83                         device->abs.y * device->abs.calibration[1] +
84                         device->abs.calibration[2];
85
86                 *y = device->abs.x * device->abs.calibration[3] +
87                         device->abs.y * device->abs.calibration[4] +
88                         device->abs.calibration[5];
89         }
90 }
91
92 static inline double
93 scale_axis(const struct input_absinfo *absinfo, double val, double to_range)
94 {
95         return (val - absinfo->minimum) * to_range /
96                 (absinfo->maximum - absinfo->minimum + 1);
97 }
98
99 double
100 evdev_device_transform_x(struct evdev_device *device,
101                          double x,
102                          uint32_t width)
103 {
104         return scale_axis(device->abs.absinfo_x, x, width);
105 }
106
107 double
108 evdev_device_transform_y(struct evdev_device *device,
109                          double y,
110                          uint32_t height)
111 {
112         return scale_axis(device->abs.absinfo_y, y, height);
113 }
114
115 static void
116 evdev_flush_pending_event(struct evdev_device *device, uint64_t time)
117 {
118         struct libinput *libinput = device->base.seat->libinput;
119         struct motion_params motion;
120         int32_t cx, cy;
121         double x, y;
122         int slot;
123         int seat_slot;
124         struct libinput_device *base = &device->base;
125         struct libinput_seat *seat = base->seat;
126
127         slot = device->mt.slot;
128
129         switch (device->pending_event) {
130         case EVDEV_NONE:
131                 return;
132         case EVDEV_RELATIVE_MOTION:
133                 motion.dx = device->rel.dx;
134                 motion.dy = device->rel.dy;
135                 device->rel.dx = 0;
136                 device->rel.dy = 0;
137
138                 /* Apply pointer acceleration. */
139                 filter_dispatch(device->pointer.filter, &motion, device, time);
140
141                 if (motion.dx == 0.0 && motion.dy == 0.0)
142                         break;
143
144                 pointer_notify_motion(base, time, motion.dx, motion.dy);
145                 break;
146         case EVDEV_ABSOLUTE_MT_DOWN:
147                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
148                         break;
149
150                 if (device->mt.slots[slot].seat_slot != -1) {
151                         log_bug_kernel(libinput,
152                                        "%s: Driver sent multiple touch down for the "
153                                        "same slot", device->devnode);
154                         break;
155                 }
156
157                 seat_slot = ffs(~seat->slot_map) - 1;
158                 device->mt.slots[slot].seat_slot = seat_slot;
159
160                 if (seat_slot == -1)
161                         break;
162
163                 seat->slot_map |= 1 << seat_slot;
164                 x = device->mt.slots[slot].x;
165                 y = device->mt.slots[slot].y;
166
167                 touch_notify_touch_down(base, time, slot, seat_slot, x, y);
168                 break;
169         case EVDEV_ABSOLUTE_MT_MOTION:
170                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
171                         break;
172
173                 seat_slot = device->mt.slots[slot].seat_slot;
174                 x = device->mt.slots[slot].x;
175                 y = device->mt.slots[slot].y;
176
177                 if (seat_slot == -1)
178                         break;
179
180                 touch_notify_touch_motion(base, time, slot, seat_slot, x, y);
181                 break;
182         case EVDEV_ABSOLUTE_MT_UP:
183                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
184                         break;
185
186                 seat_slot = device->mt.slots[slot].seat_slot;
187                 device->mt.slots[slot].seat_slot = -1;
188
189                 if (seat_slot == -1)
190                         break;
191
192                 seat->slot_map &= ~(1 << seat_slot);
193
194                 touch_notify_touch_up(base, time, slot, seat_slot);
195                 break;
196         case EVDEV_ABSOLUTE_TOUCH_DOWN:
197                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
198                         break;
199
200                 if (device->abs.seat_slot != -1) {
201                         log_bug_kernel(libinput,
202                                        "%s: Driver sent multiple touch down for the "
203                                        "same slot", device->devnode);
204                         break;
205                 }
206
207                 seat_slot = ffs(~seat->slot_map) - 1;
208                 device->abs.seat_slot = seat_slot;
209
210                 if (seat_slot == -1)
211                         break;
212
213                 seat->slot_map |= 1 << seat_slot;
214
215                 transform_absolute(device, &cx, &cy);
216
217                 touch_notify_touch_down(base, time, -1, seat_slot, cx, cy);
218                 break;
219         case EVDEV_ABSOLUTE_MOTION:
220                 transform_absolute(device, &cx, &cy);
221                 x = cx;
222                 y = cy;
223
224                 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
225                         seat_slot = device->abs.seat_slot;
226
227                         if (seat_slot == -1)
228                                 break;
229
230                         touch_notify_touch_motion(base, time, -1, seat_slot, x, y);
231                 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
232                         pointer_notify_motion_absolute(base, time, x, y);
233                 }
234                 break;
235         case EVDEV_ABSOLUTE_TOUCH_UP:
236                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
237                         break;
238
239                 seat_slot = device->abs.seat_slot;
240                 device->abs.seat_slot = -1;
241
242                 if (seat_slot == -1)
243                         break;
244
245                 seat->slot_map &= ~(1 << seat_slot);
246
247                 touch_notify_touch_up(base, time, -1, seat_slot);
248                 break;
249         default:
250                 assert(0 && "Unknown pending event type");
251                 break;
252         }
253
254         device->pending_event = EVDEV_NONE;
255 }
256
257 static void
258 evdev_process_touch_button(struct evdev_device *device,
259                            uint64_t time, int value)
260 {
261         if (device->pending_event != EVDEV_NONE &&
262             device->pending_event != EVDEV_ABSOLUTE_MOTION)
263                 evdev_flush_pending_event(device, time);
264
265         device->pending_event = (value ?
266                                  EVDEV_ABSOLUTE_TOUCH_DOWN :
267                                  EVDEV_ABSOLUTE_TOUCH_UP);
268 }
269
270 static inline void
271 evdev_process_key(struct evdev_device *device,
272                   struct input_event *e, uint64_t time)
273 {
274         /* ignore kernel key repeat */
275         if (e->value == 2)
276                 return;
277
278         if (e->code > KEY_MAX)
279                 return;
280
281         if (e->code == BTN_TOUCH) {
282                 if (!device->is_mt)
283                         evdev_process_touch_button(device, time, e->value);
284                 return;
285         }
286
287         evdev_flush_pending_event(device, time);
288
289         switch (e->code) {
290         case BTN_LEFT:
291         case BTN_RIGHT:
292         case BTN_MIDDLE:
293         case BTN_SIDE:
294         case BTN_EXTRA:
295         case BTN_FORWARD:
296         case BTN_BACK:
297         case BTN_TASK:
298                 pointer_notify_button(
299                         &device->base,
300                         time,
301                         e->code,
302                         e->value ? LIBINPUT_BUTTON_STATE_PRESSED :
303                                    LIBINPUT_BUTTON_STATE_RELEASED);
304                 break;
305
306         default:
307                 /* Only let KEY_* codes pass through. */
308                 if (!(e->code <= KEY_MICMUTE ||
309                       (e->code >= KEY_OK && e->code <= KEY_LIGHTS_TOGGLE)))
310                         break;
311
312                 keyboard_notify_key(
313                         &device->base,
314                         time,
315                         e->code,
316                         e->value ? LIBINPUT_KEY_STATE_PRESSED :
317                                    LIBINPUT_KEY_STATE_RELEASED);
318                 break;
319         }
320 }
321
322 static void
323 evdev_process_touch(struct evdev_device *device,
324                     struct input_event *e,
325                     uint64_t time)
326 {
327         switch (e->code) {
328         case ABS_MT_SLOT:
329                 evdev_flush_pending_event(device, time);
330                 device->mt.slot = e->value;
331                 break;
332         case ABS_MT_TRACKING_ID:
333                 if (device->pending_event != EVDEV_NONE &&
334                     device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
335                         evdev_flush_pending_event(device, time);
336                 if (e->value >= 0)
337                         device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
338                 else
339                         device->pending_event = EVDEV_ABSOLUTE_MT_UP;
340                 break;
341         case ABS_MT_POSITION_X:
342                 device->mt.slots[device->mt.slot].x = e->value;
343                 if (device->pending_event == EVDEV_NONE)
344                         device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
345                 break;
346         case ABS_MT_POSITION_Y:
347                 device->mt.slots[device->mt.slot].y = e->value;
348                 if (device->pending_event == EVDEV_NONE)
349                         device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
350                 break;
351         }
352 }
353
354 static inline void
355 evdev_process_absolute_motion(struct evdev_device *device,
356                               struct input_event *e)
357 {
358         switch (e->code) {
359         case ABS_X:
360                 device->abs.x = e->value;
361                 if (device->pending_event == EVDEV_NONE)
362                         device->pending_event = EVDEV_ABSOLUTE_MOTION;
363                 break;
364         case ABS_Y:
365                 device->abs.y = e->value;
366                 if (device->pending_event == EVDEV_NONE)
367                         device->pending_event = EVDEV_ABSOLUTE_MOTION;
368                 break;
369         }
370 }
371
372 static inline void
373 evdev_process_relative(struct evdev_device *device,
374                        struct input_event *e, uint64_t time)
375 {
376         struct libinput_device *base = &device->base;
377
378         switch (e->code) {
379         case REL_X:
380                 if (device->pending_event != EVDEV_RELATIVE_MOTION)
381                         evdev_flush_pending_event(device, time);
382                 device->rel.dx += e->value;
383                 device->pending_event = EVDEV_RELATIVE_MOTION;
384                 break;
385         case REL_Y:
386                 if (device->pending_event != EVDEV_RELATIVE_MOTION)
387                         evdev_flush_pending_event(device, time);
388                 device->rel.dy += e->value;
389                 device->pending_event = EVDEV_RELATIVE_MOTION;
390                 break;
391         case REL_WHEEL:
392                 evdev_flush_pending_event(device, time);
393                 pointer_notify_axis(
394                         base,
395                         time,
396                         LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
397                         -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
398                 break;
399         case REL_HWHEEL:
400                 evdev_flush_pending_event(device, time);
401                 switch (e->value) {
402                 case -1:
403                         /* Scroll left */
404                 case 1:
405                         /* Scroll right */
406                         pointer_notify_axis(
407                                 base,
408                                 time,
409                                 LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
410                                 e->value * DEFAULT_AXIS_STEP_DISTANCE);
411                         break;
412                 default:
413                         break;
414
415                 }
416         }
417 }
418
419 static inline void
420 evdev_process_absolute(struct evdev_device *device,
421                        struct input_event *e,
422                        uint64_t time)
423 {
424         if (device->is_mt) {
425                 evdev_process_touch(device, e, time);
426         } else {
427                 evdev_process_absolute_motion(device, e);
428         }
429 }
430
431 static inline int
432 evdev_need_touch_frame(struct evdev_device *device)
433 {
434         if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
435                 return 0;
436
437         switch (device->pending_event) {
438         case EVDEV_NONE:
439         case EVDEV_RELATIVE_MOTION:
440                 break;
441         case EVDEV_ABSOLUTE_MT_DOWN:
442         case EVDEV_ABSOLUTE_MT_MOTION:
443         case EVDEV_ABSOLUTE_MT_UP:
444         case EVDEV_ABSOLUTE_TOUCH_DOWN:
445         case EVDEV_ABSOLUTE_TOUCH_UP:
446         case EVDEV_ABSOLUTE_MOTION:
447                 return 1;
448         }
449
450         return 0;
451 }
452
453 static void
454 fallback_process(struct evdev_dispatch *dispatch,
455                  struct evdev_device *device,
456                  struct input_event *event,
457                  uint64_t time)
458 {
459         int need_frame = 0;
460
461         switch (event->type) {
462         case EV_REL:
463                 evdev_process_relative(device, event, time);
464                 break;
465         case EV_ABS:
466                 evdev_process_absolute(device, event, time);
467                 break;
468         case EV_KEY:
469                 evdev_process_key(device, event, time);
470                 break;
471         case EV_SYN:
472                 need_frame = evdev_need_touch_frame(device);
473                 evdev_flush_pending_event(device, time);
474                 if (need_frame)
475                         touch_notify_frame(&device->base, time);
476                 break;
477         }
478 }
479
480 static void
481 fallback_destroy(struct evdev_dispatch *dispatch)
482 {
483         free(dispatch);
484 }
485
486 struct evdev_dispatch_interface fallback_interface = {
487         fallback_process,
488         fallback_destroy
489 };
490
491 static struct evdev_dispatch *
492 fallback_dispatch_create(void)
493 {
494         struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
495         if (dispatch == NULL)
496                 return NULL;
497
498         dispatch->interface = &fallback_interface;
499
500         return dispatch;
501 }
502
503 static inline void
504 evdev_process_event(struct evdev_device *device, struct input_event *e)
505 {
506         struct evdev_dispatch *dispatch = device->dispatch;
507         uint64_t time = e->time.tv_sec * 1000ULL + e->time.tv_usec / 1000;
508
509         dispatch->interface->process(dispatch, device, e, time);
510 }
511
512 static inline void
513 evdev_device_dispatch_one(struct evdev_device *device,
514                           struct input_event *ev)
515 {
516         if (!device->mtdev) {
517                 evdev_process_event(device, ev);
518         } else {
519                 mtdev_put_event(device->mtdev, ev);
520                 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
521                         while (!mtdev_empty(device->mtdev)) {
522                                 struct input_event e;
523                                 mtdev_get_event(device->mtdev, &e);
524                                 evdev_process_event(device, &e);
525                         }
526                 }
527         }
528 }
529
530 static int
531 evdev_sync_device(struct evdev_device *device)
532 {
533         struct input_event ev;
534         int rc;
535
536         do {
537                 rc = libevdev_next_event(device->evdev,
538                                          LIBEVDEV_READ_FLAG_SYNC, &ev);
539                 if (rc < 0)
540                         break;
541                 evdev_device_dispatch_one(device, &ev);
542         } while (rc == LIBEVDEV_READ_STATUS_SYNC);
543
544         return rc == -EAGAIN ? 0 : rc;
545 }
546
547 static void
548 evdev_device_dispatch(void *data)
549 {
550         struct evdev_device *device = data;
551         struct libinput *libinput = device->base.seat->libinput;
552         struct input_event ev;
553         int rc;
554
555         /* If the compositor is repainting, this function is called only once
556          * per frame and we have to process all the events available on the
557          * fd, otherwise there will be input lag. */
558         do {
559                 rc = libevdev_next_event(device->evdev,
560                                          LIBEVDEV_READ_FLAG_NORMAL, &ev);
561                 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
562                         /* send one more sync event so we handle all
563                            currently pending events before we sync up
564                            to the current state */
565                         ev.code = SYN_REPORT;
566                         evdev_device_dispatch_one(device, &ev);
567
568                         rc = evdev_sync_device(device);
569                         if (rc == 0)
570                                 rc = LIBEVDEV_READ_STATUS_SUCCESS;
571                 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
572                         evdev_device_dispatch_one(device, &ev);
573                 }
574         } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
575
576         if (rc != -EAGAIN && rc != -EINTR) {
577                 libinput_remove_source(libinput, device->source);
578                 device->source = NULL;
579         }
580 }
581
582 static int
583 configure_pointer_acceleration(struct evdev_device *device)
584 {
585         device->pointer.filter =
586                 create_pointer_accelator_filter(
587                         pointer_accel_profile_smooth_simple);
588         if (!device->pointer.filter)
589                 return -1;
590
591         return 0;
592 }
593
594 static int
595 evdev_configure_device(struct evdev_device *device)
596 {
597         struct libinput *libinput = device->base.seat->libinput;
598         struct libevdev *evdev = device->evdev;
599         const struct input_absinfo *absinfo;
600         struct input_absinfo fixed;
601         int has_abs, has_rel, has_mt;
602         int has_button, has_keyboard, has_touch;
603         struct mt_slot *slots;
604         int num_slots;
605         int active_slot;
606         int slot;
607         unsigned int i;
608
609         has_rel = 0;
610         has_abs = 0;
611         has_mt = 0;
612         has_button = 0;
613         has_keyboard = 0;
614         has_touch = 0;
615
616         if (libevdev_has_event_type(evdev, EV_ABS)) {
617
618                 if ((absinfo = libevdev_get_abs_info(evdev, ABS_X))) {
619                         if (absinfo->resolution == 0) {
620                                 fixed = *absinfo;
621                                 fixed.resolution = 1;
622                                 libevdev_set_abs_info(evdev, ABS_X, &fixed);
623                         }
624                         device->abs.absinfo_x = absinfo;
625                         has_abs = 1;
626                 }
627                 if ((absinfo = libevdev_get_abs_info(evdev, ABS_Y))) {
628                         if (absinfo->resolution == 0) {
629                                 fixed = *absinfo;
630                                 fixed.resolution = 1;
631                                 libevdev_set_abs_info(evdev, ABS_Y, &fixed);
632                         }
633                         device->abs.absinfo_y = absinfo;
634                         has_abs = 1;
635                 }
636                 /* We only handle the slotted Protocol B in weston.
637                    Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
638                    require mtdev for conversion. */
639                 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
640                     libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y)) {
641                         absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
642                         if (absinfo->resolution == 0) {
643                                 fixed = *absinfo;
644                                 fixed.resolution = 1;
645                                 libevdev_set_abs_info(evdev,
646                                                       ABS_MT_POSITION_X,
647                                                       &fixed);
648                         }
649                         device->abs.absinfo_x = absinfo;
650                         absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
651                         if (absinfo->resolution == 0) {
652                                 fixed = *absinfo;
653                                 fixed.resolution = 1;
654                                 libevdev_set_abs_info(evdev,
655                                                       ABS_MT_POSITION_Y,
656                                                       &fixed);
657                         }
658                         device->abs.absinfo_y = absinfo;
659                         device->is_mt = 1;
660                         has_touch = 1;
661                         has_mt = 1;
662
663                         if (!libevdev_has_event_code(evdev,
664                                                      EV_ABS, ABS_MT_SLOT)) {
665                                 device->mtdev = mtdev_new_open(device->fd);
666                                 if (!device->mtdev)
667                                         return -1;
668
669                                 num_slots = device->mtdev->caps.slot.maximum;
670                                 if (device->mtdev->caps.slot.minimum < 0 ||
671                                     num_slots <= 0)
672                                         return -1;
673                                 active_slot = device->mtdev->caps.slot.value;
674                         } else {
675                                 num_slots = libevdev_get_num_slots(device->evdev);
676                                 active_slot = libevdev_get_current_slot(evdev);
677                         }
678
679                         slots = calloc(num_slots, sizeof(struct mt_slot));
680                         if (!slots)
681                                 return -1;
682
683                         for (slot = 0; slot < num_slots; ++slot) {
684                                 slots[slot].seat_slot = -1;
685                                 slots[slot].x = 0;
686                                 slots[slot].y = 0;
687                         }
688                         device->mt.slots = slots;
689                         device->mt.slots_len = num_slots;
690                         device->mt.slot = active_slot;
691                 }
692         }
693         if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
694             libevdev_has_event_code(evdev, EV_REL, REL_Y))
695                 has_rel = 1;
696
697         if (libevdev_has_event_type(evdev, EV_KEY)) {
698                 if (!libevdev_has_property(evdev, INPUT_PROP_DIRECT) &&
699                     libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_FINGER) &&
700                     !libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_PEN) &&
701                     (has_abs || has_mt)) {
702                         device->dispatch = evdev_mt_touchpad_create(device);
703                         log_info(libinput,
704                                  "input device '%s', %s is a touchpad\n",
705                                  device->devname, device->devnode);
706                 }
707                 for (i = KEY_ESC; i < KEY_MAX; i++) {
708                         if (i >= BTN_MISC && i < KEY_OK)
709                                 continue;
710                         if (libevdev_has_event_code(evdev, EV_KEY, i)) {
711                                 has_keyboard = 1;
712                                 break;
713                         }
714                 }
715                 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH))
716                         has_touch = 1;
717                 for (i = BTN_MISC; i < BTN_JOYSTICK; i++) {
718                         if (libevdev_has_event_code(evdev, EV_KEY, i)) {
719                                 has_button = 1;
720                                 break;
721                         }
722                 }
723         }
724         if (libevdev_has_event_type(evdev, EV_LED))
725                 has_keyboard = 1;
726
727         if ((has_abs || has_rel) && has_button) {
728                 if (configure_pointer_acceleration(device) == -1)
729                         return -1;
730
731                 device->seat_caps |= EVDEV_DEVICE_POINTER;
732
733                 log_info(libinput,
734                          "input device '%s', %s is a pointer caps =%s%s%s\n",
735                          device->devname, device->devnode,
736                          has_abs ? " absolute-motion" : "",
737                          has_rel ? " relative-motion": "",
738                          has_button ? " button" : "");
739         }
740         if (has_keyboard) {
741                 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
742                 log_info(libinput,
743                          "input device '%s', %s is a keyboard\n",
744                          device->devname, device->devnode);
745         }
746         if (has_touch && !has_button) {
747                 device->seat_caps |= EVDEV_DEVICE_TOUCH;
748                 log_info(libinput,
749                          "input device '%s', %s is a touch device\n",
750                          device->devname, device->devnode);
751         }
752
753         return 0;
754 }
755
756 struct evdev_device *
757 evdev_device_create(struct libinput_seat *seat,
758                     const char *devnode,
759                     const char *sysname)
760 {
761         struct libinput *libinput = seat->libinput;
762         struct evdev_device *device;
763         int rc;
764         int fd;
765         int unhandled_device = 0;
766
767         /* Use non-blocking mode so that we can loop on read on
768          * evdev_device_data() until all events on the fd are
769          * read.  mtdev_get() also expects this. */
770         fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
771         if (fd < 0) {
772                 log_info(libinput,
773                          "opening input device '%s' failed (%s).\n",
774                          devnode, strerror(-fd));
775                 return NULL;
776         }
777
778         device = zalloc(sizeof *device);
779         if (device == NULL)
780                 return NULL;
781
782         libinput_device_init(&device->base, seat);
783
784         rc = libevdev_new_from_fd(fd, &device->evdev);
785         if (rc != 0)
786                 return NULL;
787
788         libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
789
790         device->seat_caps = 0;
791         device->is_mt = 0;
792         device->mtdev = NULL;
793         device->devnode = strdup(devnode);
794         device->sysname = strdup(sysname);
795         device->rel.dx = 0;
796         device->rel.dy = 0;
797         device->abs.seat_slot = -1;
798         device->dispatch = NULL;
799         device->fd = fd;
800         device->pending_event = EVDEV_NONE;
801         device->devname = libevdev_get_name(device->evdev);
802
803         libinput_seat_ref(seat);
804
805         if (evdev_configure_device(device) == -1)
806                 goto err;
807
808         if (device->seat_caps == 0) {
809                 unhandled_device = 1;
810                 goto err;
811         }
812
813         /* If the dispatch was not set up use the fallback. */
814         if (device->dispatch == NULL)
815                 device->dispatch = fallback_dispatch_create();
816         if (device->dispatch == NULL)
817                 goto err;
818
819         device->source =
820                 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
821         if (!device->source)
822                 goto err;
823
824         list_insert(seat->devices_list.prev, &device->base.link);
825         notify_added_device(&device->base);
826
827         return device;
828
829 err:
830         if (fd >= 0)
831                 close_restricted(libinput, fd);
832         evdev_device_destroy(device);
833
834         return unhandled_device ? EVDEV_UNHANDLED_DEVICE :  NULL;
835 }
836
837 int
838 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
839 {
840         int len;
841
842         memset(keys, 0, size);
843         len = ioctl(device->fd, EVIOCGKEY(size), keys);
844
845         return (len == -1) ? -errno : len;
846 }
847
848 const char *
849 evdev_device_get_output(struct evdev_device *device)
850 {
851         return device->output_name;
852 }
853
854 const char *
855 evdev_device_get_sysname(struct evdev_device *device)
856 {
857         return device->sysname;
858 }
859
860 const char *
861 evdev_device_get_name(struct evdev_device *device)
862 {
863         return device->devname;
864 }
865
866 unsigned int
867 evdev_device_get_id_product(struct evdev_device *device)
868 {
869         return libevdev_get_id_product(device->evdev);
870 }
871
872 unsigned int
873 evdev_device_get_id_vendor(struct evdev_device *device)
874 {
875         return libevdev_get_id_vendor(device->evdev);
876 }
877
878 void
879 evdev_device_calibrate(struct evdev_device *device, float calibration[6])
880 {
881         device->abs.apply_calibration = 1;
882         memcpy(device->abs.calibration, calibration, sizeof device->abs.calibration);
883 }
884
885 int
886 evdev_device_has_capability(struct evdev_device *device,
887                             enum libinput_device_capability capability)
888 {
889         switch (capability) {
890         case LIBINPUT_DEVICE_CAP_POINTER:
891                 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
892         case LIBINPUT_DEVICE_CAP_KEYBOARD:
893                 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
894         case LIBINPUT_DEVICE_CAP_TOUCH:
895                 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
896         default:
897                 return 0;
898         }
899 }
900
901 int
902 evdev_device_get_size(struct evdev_device *device,
903                       double *width,
904                       double *height)
905 {
906         const struct input_absinfo *x, *y;
907
908         x = libevdev_get_abs_info(device->evdev, ABS_X);
909         y = libevdev_get_abs_info(device->evdev, ABS_Y);
910
911         if (!x || !y || !x->resolution || !y->resolution)
912                 return -1;
913
914         *width = evdev_convert_to_mm(x, x->maximum);
915         *height = evdev_convert_to_mm(y, y->maximum);
916
917         return 0;
918 }
919
920 void
921 evdev_device_remove(struct evdev_device *device)
922 {
923         if (device->source)
924                 libinput_remove_source(device->base.seat->libinput,
925                                        device->source);
926
927         if (device->mtdev)
928                 mtdev_close_delete(device->mtdev);
929         close_restricted(device->base.seat->libinput, device->fd);
930         device->fd = -1;
931         list_remove(&device->base.link);
932
933         notify_removed_device(&device->base);
934         libinput_device_unref(&device->base);
935 }
936
937 void
938 evdev_device_destroy(struct evdev_device *device)
939 {
940         struct evdev_dispatch *dispatch;
941
942         dispatch = device->dispatch;
943         if (dispatch)
944                 dispatch->interface->destroy(dispatch);
945
946         filter_destroy(device->pointer.filter);
947         libinput_seat_unref(device->base.seat);
948         libevdev_free(device->evdev);
949         free(device->mt.slots);
950         free(device->devnode);
951         free(device->sysname);
952         free(device);
953 }