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