Remove old touchpad code
[platform/upstream/libinput.git] / src / evdev.c
1 /*
2  * Copyright © 2010 Intel Corporation
3  * Copyright © 2013 Jonas Ådahl
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and
6  * its documentation for any purpose is hereby granted without fee, provided
7  * that the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation, and that the name of the copyright holders not be used in
10  * advertising or publicity pertaining to distribution of the software
11  * without specific, written prior permission.  The copyright holders make
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied warranty.
14  *
15  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  */
23
24 #include "config.h"
25
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <linux/input.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <mtdev-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 li_fixed_from_int(10)
43
44 #ifndef KEY_LIGHTS_TOGGLE
45 #define KEY_LIGHTS_TOGGLE 0x160
46 #endif
47
48 void
49 evdev_device_led_update(struct evdev_device *device, enum libinput_led leds)
50 {
51         static const struct {
52                 enum libinput_led weston;
53                 int evdev;
54         } map[] = {
55                 { LIBINPUT_LED_NUM_LOCK, LED_NUML },
56                 { LIBINPUT_LED_CAPS_LOCK, LED_CAPSL },
57                 { LIBINPUT_LED_SCROLL_LOCK, LED_SCROLLL },
58         };
59         struct input_event ev[ARRAY_LENGTH(map) + 1];
60         unsigned int i;
61
62         if (!(device->seat_caps & EVDEV_DEVICE_KEYBOARD))
63                 return;
64
65         memset(ev, 0, sizeof(ev));
66         for (i = 0; i < ARRAY_LENGTH(map); i++) {
67                 ev[i].type = EV_LED;
68                 ev[i].code = map[i].evdev;
69                 ev[i].value = !!(leds & map[i].weston);
70         }
71         ev[i].type = EV_SYN;
72         ev[i].code = SYN_REPORT;
73
74         i = write(device->fd, ev, sizeof ev);
75         (void)i; /* no, we really don't care about the return value */
76 }
77
78 static void
79 transform_absolute(struct evdev_device *device, int32_t *x, int32_t *y)
80 {
81         if (!device->abs.apply_calibration) {
82                 *x = device->abs.x;
83                 *y = device->abs.y;
84                 return;
85         } else {
86                 *x = device->abs.x * device->abs.calibration[0] +
87                         device->abs.y * device->abs.calibration[1] +
88                         device->abs.calibration[2];
89
90                 *y = device->abs.x * device->abs.calibration[3] +
91                         device->abs.y * device->abs.calibration[4] +
92                         device->abs.calibration[5];
93         }
94 }
95
96 li_fixed_t
97 evdev_device_transform_x(struct evdev_device *device,
98                          li_fixed_t x,
99                          uint32_t width)
100 {
101         return ((uint64_t)x - li_fixed_from_int(device->abs.min_x)) * width /
102                 (device->abs.max_x - device->abs.min_x + 1);
103 }
104
105 li_fixed_t
106 evdev_device_transform_y(struct evdev_device *device,
107                          li_fixed_t y,
108                          uint32_t height)
109 {
110         return ((uint64_t)y - li_fixed_from_int(device->abs.min_y)) * height /
111                 (device->abs.max_y - device->abs.min_y + 1);
112 }
113
114 static void
115 evdev_flush_pending_event(struct evdev_device *device, uint64_t time)
116 {
117         struct motion_params motion;
118         int32_t cx, cy;
119         li_fixed_t x, y;
120         li_fixed_t dx, dy;
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 = li_fixed_to_double(device->rel.dx);
133                 motion.dy = li_fixed_to_double(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                 dx = li_fixed_from_double(motion.dx);
141                 dy = li_fixed_from_double(motion.dy);
142                 if (dx == 0 && dy == 0)
143                         break;
144
145                 pointer_notify_motion(base, time, dx, dy);
146                 break;
147         case EVDEV_ABSOLUTE_MT_DOWN:
148                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
149                         break;
150
151                 if (device->mt.slots[slot].seat_slot != -1) {
152                         log_bug("%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 = li_fixed_from_int(device->mt.slots[slot].x);
165                 y = li_fixed_from_int(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 = li_fixed_from_int(device->mt.slots[slot].x);
175                 y = li_fixed_from_int(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("%s: Driver sent multiple touch down for the "
202                                 "same slot", device->devnode);
203                         break;
204                 }
205
206                 seat_slot = ffs(~seat->slot_map) - 1;
207                 device->abs.seat_slot = seat_slot;
208
209                 if (seat_slot == -1)
210                         break;
211
212                 seat->slot_map |= 1 << seat_slot;
213
214                 transform_absolute(device, &cx, &cy);
215                 x = li_fixed_from_int(cx);
216                 y = li_fixed_from_int(cy);
217
218                 touch_notify_touch_down(base, time, -1, seat_slot, x, y);
219                 break;
220         case EVDEV_ABSOLUTE_MOTION:
221                 transform_absolute(device, &cx, &cy);
222                 x = li_fixed_from_int(cx);
223                 y = li_fixed_from_int(cy);
224
225                 if (device->seat_caps & EVDEV_DEVICE_TOUCH) {
226                         seat_slot = device->abs.seat_slot;
227
228                         if (seat_slot == -1)
229                                 break;
230
231                         touch_notify_touch_motion(base, time, -1, seat_slot, x, y);
232                 } else if (device->seat_caps & EVDEV_DEVICE_POINTER) {
233                         pointer_notify_motion_absolute(base, time, x, y);
234                 }
235                 break;
236         case EVDEV_ABSOLUTE_TOUCH_UP:
237                 if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
238                         break;
239
240                 seat_slot = device->abs.seat_slot;
241                 device->abs.seat_slot = -1;
242
243                 if (seat_slot == -1)
244                         break;
245
246                 seat->slot_map &= ~(1 << seat_slot);
247
248                 touch_notify_touch_up(base, time, -1, seat_slot);
249                 break;
250         default:
251                 assert(0 && "Unknown pending event type");
252                 break;
253         }
254
255         device->pending_event = EVDEV_NONE;
256 }
257
258 static void
259 evdev_process_touch_button(struct evdev_device *device, int 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, struct input_event *e, int time)
272 {
273         /* ignore kernel key repeat */
274         if (e->value == 2)
275                 return;
276
277         if (e->code > KEY_MAX)
278                 return;
279
280         if (e->code == BTN_TOUCH) {
281                 if (!device->is_mt)
282                         evdev_process_touch_button(device, time, e->value);
283                 return;
284         }
285
286         evdev_flush_pending_event(device, time);
287
288         switch (e->code) {
289         case BTN_LEFT:
290         case BTN_RIGHT:
291         case BTN_MIDDLE:
292         case BTN_SIDE:
293         case BTN_EXTRA:
294         case BTN_FORWARD:
295         case BTN_BACK:
296         case BTN_TASK:
297                 pointer_notify_button(
298                         &device->base,
299                         time,
300                         e->code,
301                         e->value ? LIBINPUT_POINTER_BUTTON_STATE_PRESSED :
302                                    LIBINPUT_POINTER_BUTTON_STATE_RELEASED);
303                 break;
304
305         default:
306                 /* Only let KEY_* codes pass through. */
307                 if (!(e->code <= KEY_MICMUTE ||
308                       (e->code >= KEY_OK && e->code <= KEY_LIGHTS_TOGGLE)))
309                         break;
310
311                 keyboard_notify_key(
312                         &device->base,
313                         time,
314                         e->code,
315                         e->value ? LIBINPUT_KEYBOARD_KEY_STATE_PRESSED :
316                                    LIBINPUT_KEYBOARD_KEY_STATE_RELEASED);
317                 break;
318         }
319 }
320
321 static void
322 evdev_process_touch(struct evdev_device *device,
323                     struct input_event *e,
324                     uint64_t time)
325 {
326         switch (e->code) {
327         case ABS_MT_SLOT:
328                 evdev_flush_pending_event(device, time);
329                 device->mt.slot = e->value;
330                 break;
331         case ABS_MT_TRACKING_ID:
332                 if (device->pending_event != EVDEV_NONE &&
333                     device->pending_event != EVDEV_ABSOLUTE_MT_MOTION)
334                         evdev_flush_pending_event(device, time);
335                 if (e->value >= 0)
336                         device->pending_event = EVDEV_ABSOLUTE_MT_DOWN;
337                 else
338                         device->pending_event = EVDEV_ABSOLUTE_MT_UP;
339                 break;
340         case ABS_MT_POSITION_X:
341                 device->mt.slots[device->mt.slot].x = e->value;
342                 if (device->pending_event == EVDEV_NONE)
343                         device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
344                 break;
345         case ABS_MT_POSITION_Y:
346                 device->mt.slots[device->mt.slot].y = e->value;
347                 if (device->pending_event == EVDEV_NONE)
348                         device->pending_event = EVDEV_ABSOLUTE_MT_MOTION;
349                 break;
350         }
351 }
352
353 static inline void
354 evdev_process_absolute_motion(struct evdev_device *device,
355                               struct input_event *e)
356 {
357         switch (e->code) {
358         case ABS_X:
359                 device->abs.x = e->value;
360                 if (device->pending_event == EVDEV_NONE)
361                         device->pending_event = EVDEV_ABSOLUTE_MOTION;
362                 break;
363         case ABS_Y:
364                 device->abs.y = e->value;
365                 if (device->pending_event == EVDEV_NONE)
366                         device->pending_event = EVDEV_ABSOLUTE_MOTION;
367                 break;
368         }
369 }
370
371 static inline void
372 evdev_process_relative(struct evdev_device *device,
373                        struct input_event *e, uint64_t time)
374 {
375         struct libinput_device *base = &device->base;
376
377         switch (e->code) {
378         case REL_X:
379                 if (device->pending_event != EVDEV_RELATIVE_MOTION)
380                         evdev_flush_pending_event(device, time);
381                 device->rel.dx += li_fixed_from_int(e->value);
382                 device->pending_event = EVDEV_RELATIVE_MOTION;
383                 break;
384         case REL_Y:
385                 if (device->pending_event != EVDEV_RELATIVE_MOTION)
386                         evdev_flush_pending_event(device, time);
387                 device->rel.dy += li_fixed_from_int(e->value);
388                 device->pending_event = EVDEV_RELATIVE_MOTION;
389                 break;
390         case REL_WHEEL:
391                 evdev_flush_pending_event(device, time);
392                 pointer_notify_axis(
393                         base,
394                         time,
395                         LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL,
396                         -1 * e->value * DEFAULT_AXIS_STEP_DISTANCE);
397                 break;
398         case REL_HWHEEL:
399                 evdev_flush_pending_event(device, time);
400                 switch (e->value) {
401                 case -1:
402                         /* Scroll left */
403                 case 1:
404                         /* Scroll right */
405                         pointer_notify_axis(
406                                 base,
407                                 time,
408                                 LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL,
409                                 e->value * DEFAULT_AXIS_STEP_DISTANCE);
410                         break;
411                 default:
412                         break;
413
414                 }
415         }
416 }
417
418 static inline void
419 evdev_process_absolute(struct evdev_device *device,
420                        struct input_event *e,
421                        uint64_t time)
422 {
423         if (device->is_mt) {
424                 evdev_process_touch(device, e, time);
425         } else {
426                 evdev_process_absolute_motion(device, e);
427         }
428 }
429
430 static inline int
431 evdev_need_touch_frame(struct evdev_device *device)
432 {
433         if (!(device->seat_caps & EVDEV_DEVICE_TOUCH))
434                 return 0;
435
436         switch (device->pending_event) {
437         case EVDEV_NONE:
438         case EVDEV_RELATIVE_MOTION:
439                 break;
440         case EVDEV_ABSOLUTE_MT_DOWN:
441         case EVDEV_ABSOLUTE_MT_MOTION:
442         case EVDEV_ABSOLUTE_MT_UP:
443         case EVDEV_ABSOLUTE_TOUCH_DOWN:
444         case EVDEV_ABSOLUTE_TOUCH_UP:
445         case EVDEV_ABSOLUTE_MOTION:
446                 return 1;
447         }
448
449         return 0;
450 }
451
452 static void
453 fallback_process(struct evdev_dispatch *dispatch,
454                  struct evdev_device *device,
455                  struct input_event *event,
456                  uint64_t time)
457 {
458         int need_frame = 0;
459
460         switch (event->type) {
461         case EV_REL:
462                 evdev_process_relative(device, event, time);
463                 break;
464         case EV_ABS:
465                 evdev_process_absolute(device, event, time);
466                 break;
467         case EV_KEY:
468                 evdev_process_key(device, event, time);
469                 break;
470         case EV_SYN:
471                 need_frame = evdev_need_touch_frame(device);
472                 evdev_flush_pending_event(device, time);
473                 if (need_frame)
474                         touch_notify_frame(&device->base, time);
475                 break;
476         }
477 }
478
479 static void
480 fallback_destroy(struct evdev_dispatch *dispatch)
481 {
482         free(dispatch);
483 }
484
485 struct evdev_dispatch_interface fallback_interface = {
486         fallback_process,
487         fallback_destroy
488 };
489
490 static struct evdev_dispatch *
491 fallback_dispatch_create(void)
492 {
493         struct evdev_dispatch *dispatch = malloc(sizeof *dispatch);
494         if (dispatch == NULL)
495                 return NULL;
496
497         dispatch->interface = &fallback_interface;
498
499         return dispatch;
500 }
501
502 static inline void
503 evdev_process_event(struct evdev_device *device, struct input_event *e)
504 {
505         struct evdev_dispatch *dispatch = device->dispatch;
506         uint64_t time = e->time.tv_sec * 1000ULL + e->time.tv_usec / 1000;
507
508         dispatch->interface->process(dispatch, device, e, time);
509 }
510
511 static inline void
512 evdev_device_dispatch_one(struct evdev_device *device,
513                           struct input_event *ev)
514 {
515         if (!device->mtdev) {
516                 evdev_process_event(device, ev);
517         } else {
518                 mtdev_put_event(device->mtdev, ev);
519                 if (libevdev_event_is_code(ev, EV_SYN, SYN_REPORT)) {
520                         while (!mtdev_empty(device->mtdev)) {
521                                 struct input_event e;
522                                 mtdev_get_event(device->mtdev, &e);
523                                 evdev_process_event(device, &e);
524                         }
525                 }
526         }
527 }
528
529 static int
530 evdev_sync_device(struct evdev_device *device)
531 {
532         struct input_event ev;
533         int rc;
534
535         do {
536                 rc = libevdev_next_event(device->evdev,
537                                          LIBEVDEV_READ_FLAG_SYNC, &ev);
538                 if (rc < 0)
539                         break;
540                 evdev_device_dispatch_one(device, &ev);
541         } while (rc == LIBEVDEV_READ_STATUS_SYNC);
542
543         return rc == -EAGAIN ? 0 : rc;
544 }
545
546 static void
547 evdev_device_dispatch(void *data)
548 {
549         struct evdev_device *device = data;
550         struct libinput *libinput = device->base.seat->libinput;
551         struct input_event ev;
552         int rc;
553
554         /* If the compositor is repainting, this function is called only once
555          * per frame and we have to process all the events available on the
556          * fd, otherwise there will be input lag. */
557         do {
558                 rc = libevdev_next_event(device->evdev,
559                                          LIBEVDEV_READ_FLAG_NORMAL, &ev);
560                 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
561                         /* send one more sync event so we handle all
562                            currently pending events before we sync up
563                            to the current state */
564                         ev.code = SYN_REPORT;
565                         evdev_device_dispatch_one(device, &ev);
566
567                         rc = evdev_sync_device(device);
568                         if (rc == 0)
569                                 rc = LIBEVDEV_READ_STATUS_SUCCESS;
570                 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS) {
571                         evdev_device_dispatch_one(device, &ev);
572                 }
573         } while (rc == LIBEVDEV_READ_STATUS_SUCCESS);
574
575         if (rc != -EAGAIN && rc != -EINTR) {
576                 libinput_remove_source(libinput, device->source);
577                 device->source = NULL;
578         }
579 }
580
581 static int
582 configure_pointer_acceleration(struct evdev_device *device)
583 {
584         device->pointer.filter =
585                 create_pointer_accelator_filter(
586                         pointer_accel_profile_smooth_simple);
587         if (!device->pointer.filter)
588                 return -1;
589
590         return 0;
591 }
592
593 static int
594 evdev_configure_device(struct evdev_device *device)
595 {
596         struct libevdev *evdev = device->evdev;
597         const struct input_absinfo *absinfo;
598         int has_abs, has_rel, has_mt;
599         int has_button, has_keyboard, has_touch;
600         struct mt_slot *slots;
601         int num_slots;
602         int active_slot;
603         int slot;
604         unsigned int i;
605
606         has_rel = 0;
607         has_abs = 0;
608         has_mt = 0;
609         has_button = 0;
610         has_keyboard = 0;
611         has_touch = 0;
612
613         if (libevdev_has_event_type(evdev, EV_ABS)) {
614
615                 if ((absinfo = libevdev_get_abs_info(evdev, ABS_X))) {
616                         device->abs.min_x = absinfo->minimum;
617                         device->abs.max_x = absinfo->maximum;
618                         has_abs = 1;
619                 }
620                 if ((absinfo = libevdev_get_abs_info(evdev, ABS_Y))) {
621                         device->abs.min_y = absinfo->minimum;
622                         device->abs.max_y = absinfo->maximum;
623                         has_abs = 1;
624                 }
625                 /* We only handle the slotted Protocol B in weston.
626                    Devices with ABS_MT_POSITION_* but not ABS_MT_SLOT
627                    require mtdev for conversion. */
628                 if (libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_X) &&
629                     libevdev_has_event_code(evdev, EV_ABS, ABS_MT_POSITION_Y)) {
630                         absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_X);
631                         device->abs.min_x = absinfo->minimum;
632                         device->abs.max_x = absinfo->maximum;
633                         absinfo = libevdev_get_abs_info(evdev, ABS_MT_POSITION_Y);
634                         device->abs.min_y = absinfo->minimum;
635                         device->abs.max_y = absinfo->maximum;
636                         device->is_mt = 1;
637                         has_touch = 1;
638                         has_mt = 1;
639
640                         if (!libevdev_has_event_code(evdev,
641                                                      EV_ABS, ABS_MT_SLOT)) {
642                                 device->mtdev = mtdev_new_open(device->fd);
643                                 if (!device->mtdev)
644                                         return -1;
645
646                                 num_slots = device->mtdev->caps.slot.maximum;
647                                 if (device->mtdev->caps.slot.minimum < 0 ||
648                                     num_slots <= 0)
649                                         return -1;
650                                 active_slot = device->mtdev->caps.slot.value;
651                         } else {
652                                 num_slots = libevdev_get_num_slots(device->evdev);
653                                 active_slot = libevdev_get_current_slot(evdev);
654                         }
655
656                         slots = calloc(num_slots, sizeof(struct mt_slot));
657                         if (!slots)
658                                 return -1;
659
660                         for (slot = 0; slot < num_slots; ++slot) {
661                                 slots[slot].seat_slot = -1;
662                                 slots[slot].x = 0;
663                                 slots[slot].y = 0;
664                         }
665                         device->mt.slots = slots;
666                         device->mt.slots_len = num_slots;
667                         device->mt.slot = active_slot;
668                 }
669         }
670         if (libevdev_has_event_code(evdev, EV_REL, REL_X) ||
671             libevdev_has_event_code(evdev, EV_REL, REL_Y))
672                 has_rel = 1;
673
674         if (libevdev_has_event_type(evdev, EV_KEY)) {
675                 if (!libevdev_has_property(evdev, INPUT_PROP_DIRECT) &&
676                     libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_FINGER) &&
677                     !libevdev_has_event_code(evdev, EV_KEY, BTN_TOOL_PEN) &&
678                     (has_abs || has_mt)) {
679                         device->dispatch = evdev_mt_touchpad_create(device);
680                         log_info("input device '%s', %s is a touchpad\n",
681                                  device->devname, device->devnode);
682                 }
683                 for (i = KEY_ESC; i < KEY_MAX; i++) {
684                         if (i >= BTN_MISC && i < KEY_OK)
685                                 continue;
686                         if (libevdev_has_event_code(evdev, EV_KEY, i)) {
687                                 has_keyboard = 1;
688                                 break;
689                         }
690                 }
691                 if (libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH))
692                         has_touch = 1;
693                 for (i = BTN_MISC; i < BTN_JOYSTICK; i++) {
694                         if (libevdev_has_event_code(evdev, EV_KEY, i)) {
695                                 has_button = 1;
696                                 break;
697                         }
698                 }
699         }
700         if (libevdev_has_event_type(evdev, EV_LED))
701                 has_keyboard = 1;
702
703         if ((has_abs || has_rel) && has_button) {
704                 if (configure_pointer_acceleration(device) == -1)
705                         return -1;
706
707                 device->seat_caps |= EVDEV_DEVICE_POINTER;
708
709                 log_info("input device '%s', %s is a pointer caps =%s%s%s\n",
710                          device->devname, device->devnode,
711                          has_abs ? " absolute-motion" : "",
712                          has_rel ? " relative-motion": "",
713                          has_button ? " button" : "");
714         }
715         if (has_keyboard) {
716                 device->seat_caps |= EVDEV_DEVICE_KEYBOARD;
717                 log_info("input device '%s', %s is a keyboard\n",
718                          device->devname, device->devnode);
719         }
720         if (has_touch && !has_button) {
721                 device->seat_caps |= EVDEV_DEVICE_TOUCH;
722                 log_info("input device '%s', %s is a touch device\n",
723                          device->devname, device->devnode);
724         }
725
726         return 0;
727 }
728
729 struct evdev_device *
730 evdev_device_create(struct libinput_seat *seat,
731                     const char *devnode,
732                     const char *sysname)
733 {
734         struct libinput *libinput = seat->libinput;
735         struct evdev_device *device;
736         int rc;
737         int fd;
738         int unhandled_device = 0;
739
740         /* Use non-blocking mode so that we can loop on read on
741          * evdev_device_data() until all events on the fd are
742          * read.  mtdev_get() also expects this. */
743         fd = open_restricted(libinput, devnode, O_RDWR | O_NONBLOCK);
744         if (fd < 0) {
745                 log_info("opening input device '%s' failed (%s).\n",
746                          devnode, strerror(-fd));
747                 return NULL;
748         }
749
750         device = zalloc(sizeof *device);
751         if (device == NULL)
752                 return NULL;
753
754         libinput_device_init(&device->base, seat);
755
756         rc = libevdev_new_from_fd(fd, &device->evdev);
757         if (rc != 0)
758                 return NULL;
759
760         libevdev_set_clock_id(device->evdev, CLOCK_MONOTONIC);
761
762         device->seat_caps = 0;
763         device->is_mt = 0;
764         device->mtdev = NULL;
765         device->devnode = strdup(devnode);
766         device->sysname = strdup(sysname);
767         device->rel.dx = 0;
768         device->rel.dy = 0;
769         device->abs.seat_slot = -1;
770         device->dispatch = NULL;
771         device->fd = fd;
772         device->pending_event = EVDEV_NONE;
773         device->devname = libevdev_get_name(device->evdev);
774
775         libinput_seat_ref(seat);
776
777         if (evdev_configure_device(device) == -1)
778                 goto err;
779
780         if (device->seat_caps == 0) {
781                 unhandled_device = 1;
782                 goto err;
783         }
784
785         /* If the dispatch was not set up use the fallback. */
786         if (device->dispatch == NULL)
787                 device->dispatch = fallback_dispatch_create();
788         if (device->dispatch == NULL)
789                 goto err;
790
791         device->source =
792                 libinput_add_fd(libinput, fd, evdev_device_dispatch, device);
793         if (!device->source)
794                 goto err;
795
796         list_insert(seat->devices_list.prev, &device->base.link);
797         notify_added_device(&device->base);
798
799         return device;
800
801 err:
802         if (fd >= 0)
803                 close_restricted(libinput, fd);
804         evdev_device_destroy(device);
805
806         return unhandled_device ? EVDEV_UNHANDLED_DEVICE :  NULL;
807 }
808
809 int
810 evdev_device_get_keys(struct evdev_device *device, char *keys, size_t size)
811 {
812         int len;
813
814         memset(keys, 0, size);
815         len = ioctl(device->fd, EVIOCGKEY(size), keys);
816
817         return (len == -1) ? -errno : len;
818 }
819
820 const char *
821 evdev_device_get_output(struct evdev_device *device)
822 {
823         return device->output_name;
824 }
825
826 const char *
827 evdev_device_get_sysname(struct evdev_device *device)
828 {
829         return device->sysname;
830 }
831
832 void
833 evdev_device_calibrate(struct evdev_device *device, float calibration[6])
834 {
835         device->abs.apply_calibration = 1;
836         memcpy(device->abs.calibration, calibration, sizeof device->abs.calibration);
837 }
838
839 int
840 evdev_device_has_capability(struct evdev_device *device,
841                             enum libinput_device_capability capability)
842 {
843         switch (capability) {
844         case LIBINPUT_DEVICE_CAP_POINTER:
845                 return !!(device->seat_caps & EVDEV_DEVICE_POINTER);
846         case LIBINPUT_DEVICE_CAP_KEYBOARD:
847                 return !!(device->seat_caps & EVDEV_DEVICE_KEYBOARD);
848         case LIBINPUT_DEVICE_CAP_TOUCH:
849                 return !!(device->seat_caps & EVDEV_DEVICE_TOUCH);
850         default:
851                 return 0;
852         }
853 }
854
855 void
856 evdev_device_remove(struct evdev_device *device)
857 {
858         if (device->source)
859                 libinput_remove_source(device->base.seat->libinput,
860                                        device->source);
861
862         if (device->mtdev)
863                 mtdev_close_delete(device->mtdev);
864         close_restricted(device->base.seat->libinput, device->fd);
865         device->fd = -1;
866         list_remove(&device->base.link);
867
868         notify_removed_device(&device->base);
869         libinput_device_unref(&device->base);
870 }
871
872 void
873 evdev_device_destroy(struct evdev_device *device)
874 {
875         struct evdev_dispatch *dispatch;
876
877         dispatch = device->dispatch;
878         if (dispatch)
879                 dispatch->interface->destroy(dispatch);
880
881         motion_filter_destroy(device->pointer.filter);
882         libinput_seat_unref(device->base.seat);
883         libevdev_free(device->evdev);
884         free(device->mt.slots);
885         free(device->devnode);
886         free(device->sysname);
887         free(device);
888 }