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