Simplify some error handling by assuming a >=3.4 kernel
[platform/upstream/libevdev.git] / libevdev / libevdev.c
1 /*
2  * Copyright © 2013 Red Hat, Inc.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #include <config.h>
24 #include <errno.h>
25 #include <poll.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <limits.h>
29 #include <unistd.h>
30 #include <stdarg.h>
31 #include <stdbool.h>
32
33 #include "libevdev.h"
34 #include "libevdev-int.h"
35 #include "libevdev-util.h"
36 #include "event-names.h"
37
38 #define MAXEVENTS 64
39
40 enum event_filter_status {
41         EVENT_FILTER_NONE,      /**< Event untouched by filters */
42         EVENT_FILTER_MODIFIED,  /**< Event was modified */
43         EVENT_FILTER_DISCARD,   /**< Discard current event */
44 };
45
46 static int sync_mt_state(struct libevdev *dev, int create_events);
47
48 static inline int*
49 slot_value(const struct libevdev *dev, int slot, int axis)
50 {
51         if (unlikely(slot > dev->num_slots)) {
52                 log_bug(dev, "Slot %d exceeds number of slots (%d)\n", slot, dev->num_slots);
53                 slot = 0;
54         }
55         if (unlikely(axis < ABS_MT_MIN || axis > ABS_MT_MAX)) {
56                 log_bug(dev, "MT axis %d is outside the valid range [%d,%d]\n",
57                         axis, ABS_MT_MIN, ABS_MT_MAX);
58                 axis = ABS_MT_MIN;
59         }
60         return &dev->mt_slot_vals[slot * ABS_MT_CNT + axis - ABS_MT_MIN];
61 }
62
63 static int
64 init_event_queue(struct libevdev *dev)
65 {
66         const int MIN_QUEUE_SIZE = 256;
67         int nevents = 1; /* terminating SYN_REPORT */
68         int nslots;
69         unsigned int type, code;
70
71         /* count the number of axes, keys, etc. to get a better idea at how
72            many events per EV_SYN we could possibly get. That's the max we
73            may get during SYN_DROPPED too. Use double that, just so we have
74            room for events while syncing a device.
75          */
76         for (type = EV_KEY; type < EV_MAX; type++) {
77                 int max = libevdev_event_type_get_max(type);
78                 for (code = 0; max > 0 && code < (unsigned int) max; code++) {
79                         if (libevdev_has_event_code(dev, type, code))
80                                 nevents++;
81                 }
82         }
83
84         nslots = libevdev_get_num_slots(dev);
85         if (nslots > 1) {
86                 int num_mt_axes = 0;
87
88                 for (code = ABS_MT_SLOT; code <= ABS_MAX; code++) {
89                         if (libevdev_has_event_code(dev, EV_ABS, code))
90                                 num_mt_axes++;
91                 }
92
93                 /* We already counted the first slot in the initial count */
94                 nevents += num_mt_axes * (nslots - 1);
95         }
96
97         return queue_alloc(dev, max(MIN_QUEUE_SIZE, nevents * 2));
98 }
99
100 static void
101 libevdev_dflt_log_func(enum libevdev_log_priority priority,
102                        void *data,
103                        const char *file, int line, const char *func,
104                        const char *format, va_list args)
105 {
106         const char *prefix;
107         switch(priority) {
108                 case LIBEVDEV_LOG_ERROR: prefix = "libevdev error"; break;
109                 case LIBEVDEV_LOG_INFO: prefix = "libevdev info"; break;
110                 case LIBEVDEV_LOG_DEBUG:
111                                         prefix = "libevdev debug";
112                                         break;
113                 default:
114                                         prefix = "libevdev INVALID LOG PRIORITY";
115                                         break;
116         }
117         /* default logging format:
118            libevev error in libevdev_some_func: blah blah
119            libevev info in libevdev_some_func: blah blah
120            libevev debug in file.c:123:libevdev_some_func: blah blah
121          */
122
123         fprintf(stderr, "%s in ", prefix);
124         if (priority == LIBEVDEV_LOG_DEBUG)
125                 fprintf(stderr, "%s:%d:", file, line);
126         fprintf(stderr, "%s: ", func);
127         vfprintf(stderr, format, args);
128 }
129
130 static void
131 fix_invalid_absinfo(const struct libevdev *dev,
132                     int axis,
133                     struct input_absinfo* abs_info)
134 {
135         /*
136          * The reported absinfo for ABS_MT_TRACKING_ID is sometimes
137          * uninitialized for certain mtk-soc, due to init code mangling
138          * in the vendor kernel.
139          */
140         if (axis == ABS_MT_TRACKING_ID &&
141             abs_info->maximum == abs_info->minimum) {
142                 abs_info->minimum = -1;
143                 abs_info->maximum = 0xFFFF;
144                 log_bug(dev,
145                         "Device \"%s\" has invalid ABS_MT_TRACKING_ID range",
146                         dev->name);
147         }
148 }
149
150 /*
151  * Global logging settings.
152  */
153 static struct logdata log_data = {
154         .priority = LIBEVDEV_LOG_INFO,
155         .global_handler = libevdev_dflt_log_func,
156         .userdata = NULL,
157 };
158
159 void
160 _libevdev_log_msg(const struct libevdev *dev,
161                   enum libevdev_log_priority priority,
162                   const char *file, int line, const char *func,
163                   const char *format, ...)
164 {
165         va_list args;
166
167         if (dev && dev->log.device_handler) {
168                 /**
169                  * if both global handler and device handler are set
170                  * we've set up the handlers wrong.  And that means we'll
171                  * likely get the printf args wrong and cause all sorts of
172                  * mayhem. Seppuku is called for.
173                  */
174                 if (unlikely(dev->log.global_handler))
175                         abort();
176
177                 if (priority > dev->log.priority)
178                         return;
179         } else if (!log_data.global_handler || priority > log_data.priority)
180                 return;
181         else if (unlikely(log_data.device_handler))
182                 abort(); /* Seppuku, see above */
183
184         va_start(args, format);
185         if (dev && dev->log.device_handler)
186                 dev->log.device_handler(dev, priority, dev->log.userdata, file, line, func, format, args);
187         else
188                 log_data.global_handler(priority, log_data.userdata, file, line, func, format, args);
189         va_end(args);
190 }
191
192 static void
193 libevdev_reset(struct libevdev *dev)
194 {
195         enum libevdev_log_priority pri = dev->log.priority;
196         libevdev_device_log_func_t handler = dev->log.device_handler;
197
198         free(dev->name);
199         free(dev->phys);
200         free(dev->uniq);
201         free(dev->mt_slot_vals);
202         free(dev->mt_sync.mt_state);
203         free(dev->mt_sync.tracking_id_changes);
204         free(dev->mt_sync.slot_update);
205         memset(dev, 0, sizeof(*dev));
206         dev->fd = -1;
207         dev->initialized = false;
208         dev->num_slots = -1;
209         dev->current_slot = -1;
210         dev->grabbed = LIBEVDEV_UNGRAB;
211         dev->sync_state = SYNC_NONE;
212         dev->log.priority = pri;
213         dev->log.device_handler = handler;
214         libevdev_enable_event_type(dev, EV_SYN);
215 }
216
217 LIBEVDEV_EXPORT struct libevdev*
218 libevdev_new(void)
219 {
220         struct libevdev *dev;
221
222         dev = calloc(1, sizeof(*dev));
223         if (!dev)
224                 return NULL;
225
226         libevdev_reset(dev);
227
228         return dev;
229 }
230
231 LIBEVDEV_EXPORT int
232 libevdev_new_from_fd(int fd, struct libevdev **dev)
233 {
234         struct libevdev *d;
235         int rc;
236
237         d = libevdev_new();
238         if (!d)
239                 return -ENOMEM;
240
241         rc = libevdev_set_fd(d, fd);
242         if (rc < 0)
243                 libevdev_free(d);
244         else
245                 *dev = d;
246         return rc;
247 }
248
249 LIBEVDEV_EXPORT void
250 libevdev_free(struct libevdev *dev)
251 {
252         if (!dev)
253                 return;
254
255         queue_free(dev);
256         libevdev_reset(dev);
257         free(dev);
258 }
259
260 LIBEVDEV_EXPORT void
261 libevdev_set_log_function(libevdev_log_func_t logfunc, void *data)
262 {
263         log_data.global_handler = logfunc;
264         log_data.userdata = data;
265 }
266
267 LIBEVDEV_EXPORT void
268 libevdev_set_log_priority(enum libevdev_log_priority priority)
269 {
270         if (priority > LIBEVDEV_LOG_DEBUG)
271                 priority = LIBEVDEV_LOG_DEBUG;
272         log_data.priority = priority;
273 }
274
275 LIBEVDEV_EXPORT enum libevdev_log_priority
276 libevdev_get_log_priority(void)
277 {
278         return log_data.priority;
279 }
280
281 LIBEVDEV_EXPORT void
282 libevdev_set_device_log_function(struct libevdev *dev,
283                                  libevdev_device_log_func_t logfunc,
284                                  enum libevdev_log_priority priority,
285                                  void *data)
286 {
287         if (!dev) {
288                 log_bug(NULL, "device must not be NULL\n");
289                 return;
290         }
291
292         dev->log.priority = priority;
293         dev->log.device_handler = logfunc;
294         dev->log.userdata = data;
295 }
296
297 enum libevdev_log_priority
298 _libevdev_log_priority(const struct libevdev *dev)
299 {
300         if (dev && dev->log.device_handler)
301                 return dev->log.priority;
302         else
303                 return libevdev_get_log_priority();
304 }
305
306 LIBEVDEV_EXPORT int
307 libevdev_change_fd(struct libevdev *dev, int fd)
308 {
309         if (!dev->initialized) {
310                 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
311                 return -1;
312         }
313         dev->fd = fd;
314         dev->grabbed = LIBEVDEV_UNGRAB;
315         return 0;
316 }
317
318 static void
319 reset_tracking_ids(struct libevdev *dev)
320 {
321         if (dev->num_slots == -1 ||
322             !libevdev_has_event_code(dev, EV_ABS, ABS_MT_TRACKING_ID))
323                 return;
324
325         for (int slot = 0; slot < dev->num_slots; slot++)
326                 libevdev_set_slot_value(dev, slot, ABS_MT_TRACKING_ID, -1);
327 }
328
329 static inline void
330 free_slots(struct libevdev *dev)
331 {
332         dev->num_slots = -1;
333         free(dev->mt_slot_vals);
334         free(dev->mt_sync.tracking_id_changes);
335         free(dev->mt_sync.slot_update);
336         free(dev->mt_sync.mt_state);
337         dev->mt_slot_vals = NULL;
338         dev->mt_sync.tracking_id_changes = NULL;
339         dev->mt_sync.slot_update = NULL;
340         dev->mt_sync.mt_state = NULL;
341 }
342
343 static int
344 init_slots(struct libevdev *dev)
345 {
346         const struct input_absinfo *abs_info;
347         int rc = 0;
348
349         free(dev->mt_slot_vals);
350         free(dev->mt_sync.tracking_id_changes);
351         free(dev->mt_sync.slot_update);
352         free(dev->mt_sync.mt_state);
353         dev->mt_slot_vals = NULL;
354         dev->mt_sync.tracking_id_changes = NULL;
355         dev->mt_sync.slot_update = NULL;
356         dev->mt_sync.mt_state = NULL;
357
358         /* devices with ABS_RESERVED aren't MT devices,
359            see the documentation for multitouch-related
360            functions for more details */
361         if (libevdev_has_event_code(dev, EV_ABS, ABS_RESERVED) ||
362             !libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT)) {
363                 if (dev->num_slots != -1) {
364                         free_slots(dev);
365                 }
366                 return rc;
367         }
368
369         abs_info = libevdev_get_abs_info(dev, ABS_MT_SLOT);
370
371         free_slots(dev);
372         dev->num_slots = abs_info->maximum + 1;
373         dev->mt_slot_vals = calloc(dev->num_slots * ABS_MT_CNT, sizeof(int));
374         if (!dev->mt_slot_vals) {
375                 rc = -ENOMEM;
376                 goto out;
377         }
378         dev->current_slot = abs_info->value;
379
380         dev->mt_sync.mt_state_sz = sizeof(*dev->mt_sync.mt_state) +
381                                    (dev->num_slots) * sizeof(int);
382         dev->mt_sync.mt_state = calloc(1, dev->mt_sync.mt_state_sz);
383
384         dev->mt_sync.tracking_id_changes_sz = NLONGS(dev->num_slots) * sizeof(long);
385         dev->mt_sync.tracking_id_changes = malloc(dev->mt_sync.tracking_id_changes_sz);
386
387         dev->mt_sync.slot_update_sz = NLONGS(dev->num_slots * ABS_MT_CNT) * sizeof(long);
388         dev->mt_sync.slot_update = malloc(dev->mt_sync.slot_update_sz);
389
390         if (!dev->mt_sync.tracking_id_changes ||
391             !dev->mt_sync.slot_update ||
392             !dev->mt_sync.mt_state) {
393                 rc = -ENOMEM;
394                 goto out;
395         }
396
397         reset_tracking_ids(dev);
398 out:
399         return rc;
400 }
401
402 LIBEVDEV_EXPORT int
403 libevdev_set_fd(struct libevdev* dev, int fd)
404 {
405         int rc;
406         int i;
407         char buf[256];
408
409         if (dev->initialized) {
410                 log_bug(dev, "device already initialized.\n");
411                 return -EBADF;
412         } else if (fd < 0)
413                 return -EBADF;
414
415         libevdev_reset(dev);
416
417         rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
418         if (rc < 0)
419                 goto out;
420
421         memset(buf, 0, sizeof(buf));
422         rc = ioctl(fd, EVIOCGNAME(sizeof(buf) - 1), buf);
423         if (rc < 0)
424                 goto out;
425
426         free(dev->name);
427         dev->name = strdup(buf);
428         if (!dev->name) {
429                 errno = ENOMEM;
430                 goto out;
431         }
432
433         free(dev->phys);
434         dev->phys = NULL;
435         memset(buf, 0, sizeof(buf));
436         rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
437         if (rc < 0) {
438                 /* uinput has no phys */
439                 if (errno != ENOENT)
440                         goto out;
441         } else {
442                 dev->phys = strdup(buf);
443                 if (!dev->phys) {
444                         errno = ENOMEM;
445                         goto out;
446                 }
447         }
448
449         free(dev->uniq);
450         dev->uniq = NULL;
451         memset(buf, 0, sizeof(buf));
452         rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
453         if (rc < 0) {
454                 if (errno != ENOENT)
455                         goto out;
456         } else  {
457                 dev->uniq = strdup(buf);
458                 if (!dev->uniq) {
459                         errno = ENOMEM;
460                         goto out;
461                 }
462         }
463
464         rc = ioctl(fd, EVIOCGID, &dev->ids);
465         if (rc < 0)
466                 goto out;
467
468         rc = ioctl(fd, EVIOCGVERSION, &dev->driver_version);
469         if (rc < 0)
470                 goto out;
471
472         /* Built on a kernel with props, running against a kernel without property
473            support. This should not be a fatal case, we'll be missing properties but other
474            than that everything is as expected.
475          */
476         rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
477         if (rc < 0 && errno != EINVAL)
478                 goto out;
479
480         rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
481         if (rc < 0)
482                 goto out;
483
484         rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
485         if (rc < 0)
486                 goto out;
487
488         rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
489         if (rc < 0)
490                 goto out;
491
492         rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
493         if (rc < 0)
494                 goto out;
495
496         rc = ioctl(fd, EVIOCGBIT(EV_SW, sizeof(dev->sw_bits)), dev->sw_bits);
497         if (rc < 0)
498                 goto out;
499
500         rc = ioctl(fd, EVIOCGBIT(EV_MSC, sizeof(dev->msc_bits)), dev->msc_bits);
501         if (rc < 0)
502                 goto out;
503
504         rc = ioctl(fd, EVIOCGBIT(EV_FF, sizeof(dev->ff_bits)), dev->ff_bits);
505         if (rc < 0)
506                 goto out;
507
508         rc = ioctl(fd, EVIOCGBIT(EV_SND, sizeof(dev->snd_bits)), dev->snd_bits);
509         if (rc < 0)
510                 goto out;
511
512         rc = ioctl(fd, EVIOCGKEY(sizeof(dev->key_values)), dev->key_values);
513         if (rc < 0)
514                 goto out;
515
516         rc = ioctl(fd, EVIOCGLED(sizeof(dev->led_values)), dev->led_values);
517         if (rc < 0)
518                 goto out;
519
520         rc = ioctl(fd, EVIOCGSW(sizeof(dev->sw_values)), dev->sw_values);
521         if (rc < 0)
522                 goto out;
523
524         /* rep is a special case, always set it to 1 for both values if EV_REP is set */
525         if (bit_is_set(dev->bits, EV_REP)) {
526                 for (i = 0; i < REP_CNT; i++)
527                         set_bit(dev->rep_bits, i);
528                 rc = ioctl(fd, EVIOCGREP, dev->rep_values);
529                 if (rc < 0)
530                         goto out;
531         }
532
533         for (i = ABS_X; i <= ABS_MAX; i++) {
534                 if (bit_is_set(dev->abs_bits, i)) {
535                         struct input_absinfo abs_info;
536                         rc = ioctl(fd, EVIOCGABS(i), &abs_info);
537                         if (rc < 0)
538                                 goto out;
539
540                         fix_invalid_absinfo(dev, i, &abs_info);
541
542                         dev->abs_info[i] = abs_info;
543                 }
544         }
545
546         dev->fd = fd;
547
548         rc = init_slots(dev);
549         if (rc != 0)
550                 goto out;
551
552         if (dev->num_slots != -1)
553                 sync_mt_state(dev, 0);
554
555         rc = init_event_queue(dev);
556         if (rc < 0) {
557                 dev->fd = -1;
558                 return -rc;
559         }
560
561         /* not copying key state because we won't know when we'll start to
562          * use this fd and key's are likely to change state by then.
563          * Same with the valuators, really, but they may not change.
564          */
565
566         dev->initialized = true;
567 out:
568         if (rc)
569                 libevdev_reset(dev);
570         return rc ? -errno : 0;
571 }
572
573 LIBEVDEV_EXPORT int
574 libevdev_get_fd(const struct libevdev* dev)
575 {
576         return dev->fd;
577 }
578
579 static inline void
580 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
581 {
582         ev->input_event_sec = dev->last_event_time.tv_sec;
583         ev->input_event_usec = dev->last_event_time.tv_usec;
584         ev->type = type;
585         ev->code = code;
586         ev->value = value;
587 }
588
589 static int
590 sync_key_state(struct libevdev *dev)
591 {
592         int rc;
593         int i;
594         unsigned long keystate[NLONGS(KEY_CNT)] = {0};
595
596         rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
597         if (rc < 0)
598                 goto out;
599
600         for (i = 0; i < KEY_CNT; i++) {
601                 int old, new;
602                 old = bit_is_set(dev->key_values, i);
603                 new = bit_is_set(keystate, i);
604                 if (old ^ new) {
605                         struct input_event *ev = queue_push(dev);
606                         init_event(dev, ev, EV_KEY, i, new ? 1 : 0);
607                 }
608         }
609
610         memcpy(dev->key_values, keystate, rc);
611
612         rc = 0;
613 out:
614         return rc ? -errno : 0;
615 }
616
617 static int
618 sync_sw_state(struct libevdev *dev)
619 {
620         int rc;
621         int i;
622         unsigned long swstate[NLONGS(SW_CNT)] = {0};
623
624         rc = ioctl(dev->fd, EVIOCGSW(sizeof(swstate)), swstate);
625         if (rc < 0)
626                 goto out;
627
628         for (i = 0; i < SW_CNT; i++) {
629                 int old, new;
630                 old = bit_is_set(dev->sw_values, i);
631                 new = bit_is_set(swstate, i);
632                 if (old ^ new) {
633                         struct input_event *ev = queue_push(dev);
634                         init_event(dev, ev, EV_SW, i, new ? 1 : 0);
635                 }
636         }
637
638         memcpy(dev->sw_values, swstate, rc);
639
640         rc = 0;
641 out:
642         return rc ? -errno : 0;
643 }
644
645 static int
646 sync_led_state(struct libevdev *dev)
647 {
648         int rc;
649         int i;
650         unsigned long ledstate[NLONGS(LED_CNT)] = {0};
651
652         rc = ioctl(dev->fd, EVIOCGLED(sizeof(ledstate)), ledstate);
653         if (rc < 0)
654                 goto out;
655
656         for (i = 0; i < LED_CNT; i++) {
657                 int old, new;
658                 old = bit_is_set(dev->led_values, i);
659                 new = bit_is_set(ledstate, i);
660                 if (old ^ new) {
661                         struct input_event *ev = queue_push(dev);
662                         init_event(dev, ev, EV_LED, i, new ? 1 : 0);
663                 }
664         }
665
666         memcpy(dev->led_values, ledstate, rc);
667
668         rc = 0;
669 out:
670         return rc ? -errno : 0;
671 }
672 static int
673 sync_abs_state(struct libevdev *dev)
674 {
675         int rc;
676         int i;
677
678         for (i = ABS_X; i < ABS_CNT; i++) {
679                 struct input_absinfo abs_info;
680
681                 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
682                         continue;
683
684                 if (!bit_is_set(dev->abs_bits, i))
685                         continue;
686
687                 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
688                 if (rc < 0)
689                         goto out;
690
691                 if (dev->abs_info[i].value != abs_info.value) {
692                         struct input_event *ev = queue_push(dev);
693
694                         init_event(dev, ev, EV_ABS, i, abs_info.value);
695                         dev->abs_info[i].value = abs_info.value;
696                 }
697         }
698
699         rc = 0;
700 out:
701         return rc ? -errno : 0;
702 }
703
704 static int
705 sync_mt_state(struct libevdev *dev, int create_events)
706 {
707         struct input_event *ev;
708         struct input_absinfo abs_info;
709         int rc;
710         int axis, slot;
711         int last_reported_slot = 0;
712         struct mt_sync_state *mt_state = dev->mt_sync.mt_state;
713         unsigned long *slot_update = dev->mt_sync.slot_update;
714         unsigned long *tracking_id_changes = dev->mt_sync.tracking_id_changes;
715         int need_tracking_id_changes = 0;
716
717         memset(dev->mt_sync.slot_update, 0, dev->mt_sync.slot_update_sz);
718         memset(dev->mt_sync.tracking_id_changes, 0,
719                dev->mt_sync.tracking_id_changes_sz);
720
721 #define AXISBIT(_slot, _axis) (_slot * ABS_MT_CNT + _axis - ABS_MT_MIN)
722
723         for (axis = ABS_MT_MIN; axis <= ABS_MT_MAX; axis++) {
724                 if (axis == ABS_MT_SLOT)
725                         continue;
726
727                 if (!libevdev_has_event_code(dev, EV_ABS, axis))
728                         continue;
729
730                 mt_state->code = axis;
731                 rc = ioctl(dev->fd, EVIOCGMTSLOTS(dev->mt_sync.mt_state_sz), mt_state);
732                 if (rc < 0)
733                         goto out;
734
735                 for (slot = 0; slot < dev->num_slots; slot++) {
736
737                         if (*slot_value(dev, slot, axis) == mt_state->val[slot])
738                                 continue;
739
740                         if (axis == ABS_MT_TRACKING_ID &&
741                             *slot_value(dev, slot, axis) != -1 &&
742                             mt_state->val[slot] != -1) {
743                                 set_bit(tracking_id_changes, slot);
744                                 need_tracking_id_changes = 1;
745                         }
746
747                         *slot_value(dev, slot, axis) = mt_state->val[slot];
748
749                         set_bit(slot_update, AXISBIT(slot, axis));
750                         /* note that this slot has updates */
751                         set_bit(slot_update, AXISBIT(slot, ABS_MT_SLOT));
752                 }
753         }
754
755         if (!create_events) {
756                 rc = 0;
757                 goto out;
758         }
759
760         if (need_tracking_id_changes) {
761                 for (slot = 0; slot < dev->num_slots;  slot++) {
762                         if (!bit_is_set(tracking_id_changes, slot))
763                                 continue;
764
765                         ev = queue_push(dev);
766                         init_event(dev, ev, EV_ABS, ABS_MT_SLOT, slot);
767                         ev = queue_push(dev);
768                         init_event(dev, ev, EV_ABS, ABS_MT_TRACKING_ID, -1);
769
770                         last_reported_slot = slot;
771                 }
772
773                 ev = queue_push(dev);
774                 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
775         }
776
777         for (slot = 0; slot < dev->num_slots;  slot++) {
778                 if (!bit_is_set(slot_update, AXISBIT(slot, ABS_MT_SLOT)))
779                         continue;
780
781                 ev = queue_push(dev);
782                 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, slot);
783                 last_reported_slot = slot;
784
785                 for (axis = ABS_MT_MIN; axis <= ABS_MT_MAX; axis++) {
786                         if (axis == ABS_MT_SLOT ||
787                             !libevdev_has_event_code(dev, EV_ABS, axis))
788                                 continue;
789
790                         if (bit_is_set(slot_update, AXISBIT(slot, axis))) {
791                                 ev = queue_push(dev);
792                                 init_event(dev, ev, EV_ABS, axis, *slot_value(dev, slot, axis));
793                         }
794                 }
795         }
796
797         /* add one last slot event to make sure the client is on the same
798            slot as the kernel */
799
800         rc = ioctl(dev->fd, EVIOCGABS(ABS_MT_SLOT), &abs_info);
801         if (rc < 0)
802                 goto out;
803
804         dev->current_slot = abs_info.value;
805
806         if (dev->current_slot != last_reported_slot) {
807                 ev = queue_push(dev);
808                 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, dev->current_slot);
809         }
810
811 #undef AXISBIT
812
813         rc = 0;
814 out:
815         return rc ? -errno : 0;
816 }
817
818 static int
819 read_more_events(struct libevdev *dev)
820 {
821         int free_elem;
822         int len;
823         struct input_event *next;
824
825         free_elem = queue_num_free_elements(dev);
826         if (free_elem <= 0)
827                 return 0;
828
829         next = queue_next_element(dev);
830         len = read(dev->fd, next, free_elem * sizeof(struct input_event));
831         if (len < 0) {
832                 return -errno;
833         } else if (len > 0 && len % sizeof(struct input_event) != 0)
834                 return -EINVAL;
835         else if (len > 0) {
836                 int nev = len/sizeof(struct input_event);
837                 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
838         }
839
840         return 0;
841 }
842
843 static inline void
844 drain_events(struct libevdev *dev)
845 {
846         int rc;
847         size_t nelem;
848         int iterations = 0;
849         const int max_iterations = 8; /* EVDEV_BUF_PACKETS in
850                                          kernel/drivers/input/evedev.c */
851
852         queue_shift_multiple(dev, queue_num_elements(dev), NULL);
853
854         do {
855                 rc = read_more_events(dev);
856                 if (rc == -EAGAIN)
857                         return;
858
859                 if (rc < 0) {
860                         log_error(dev, "Failed to drain events before sync.\n");
861                         return;
862                 }
863
864                 nelem = queue_num_elements(dev);
865                 queue_shift_multiple(dev, nelem, NULL);
866         } while (iterations++ < max_iterations && nelem >= queue_size(dev));
867
868         /* Our buffer should be roughly the same or bigger than the kernel
869            buffer in most cases, so we usually don't expect to recurse. If
870            we do, make sure we stop after max_iterations and proceed with
871            what we have.  This could happen if events queue up faster than
872            we can drain them.
873          */
874         if (iterations >= max_iterations)
875                 log_info(dev, "Unable to drain events, buffer size mismatch.\n");
876 }
877
878 static int
879 sync_state(struct libevdev *dev)
880 {
881         int rc = 0;
882         struct input_event *ev;
883
884          /* see section "Discarding events before synchronizing" in
885           * libevdev/libevdev.h */
886         drain_events(dev);
887
888         if (libevdev_has_event_type(dev, EV_KEY))
889                 rc = sync_key_state(dev);
890         if (libevdev_has_event_type(dev, EV_LED))
891                 rc = sync_led_state(dev);
892         if (libevdev_has_event_type(dev, EV_SW))
893                 rc = sync_sw_state(dev);
894         if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
895                 rc = sync_abs_state(dev);
896         if (rc == 0 && dev->num_slots > -1 &&
897             libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
898                 rc = sync_mt_state(dev, 1);
899
900         dev->queue_nsync = queue_num_elements(dev);
901
902         if (dev->queue_nsync > 0) {
903                 ev = queue_push(dev);
904                 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
905                 dev->queue_nsync++;
906         }
907
908         return rc;
909 }
910
911 static int
912 update_key_state(struct libevdev *dev, const struct input_event *e)
913 {
914         if (!libevdev_has_event_type(dev, EV_KEY))
915                 return 1;
916
917         if (e->code > KEY_MAX)
918                 return 1;
919
920         set_bit_state(dev->key_values, e->code, e->value != 0);
921
922         return 0;
923 }
924
925 static int
926 update_mt_state(struct libevdev *dev, const struct input_event *e)
927 {
928         if (e->code == ABS_MT_SLOT && dev->num_slots > -1) {
929                 int i;
930                 dev->current_slot = e->value;
931                 /* sync abs_info with the current slot values */
932                 for (i = ABS_MT_SLOT + 1; i <= ABS_MT_MAX; i++) {
933                         if (libevdev_has_event_code(dev, EV_ABS, i))
934                                 dev->abs_info[i].value = *slot_value(dev, dev->current_slot, i);
935                 }
936
937                 return 0;
938         } else if (dev->current_slot == -1)
939                 return 1;
940
941         *slot_value(dev, dev->current_slot, e->code) = e->value;
942
943         return 0;
944 }
945
946 static int
947 update_abs_state(struct libevdev *dev, const struct input_event *e)
948 {
949         if (!libevdev_has_event_type(dev, EV_ABS))
950                 return 1;
951
952         if (e->code > ABS_MAX)
953                 return 1;
954
955         if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
956                 update_mt_state(dev, e);
957
958         dev->abs_info[e->code].value = e->value;
959
960         return 0;
961 }
962
963 static int
964 update_led_state(struct libevdev *dev, const struct input_event *e)
965 {
966         if (!libevdev_has_event_type(dev, EV_LED))
967                 return 1;
968
969         if (e->code > LED_MAX)
970                 return 1;
971
972         set_bit_state(dev->led_values, e->code, e->value != 0);
973
974         return 0;
975 }
976
977 static int
978 update_sw_state(struct libevdev *dev, const struct input_event *e)
979 {
980         if (!libevdev_has_event_type(dev, EV_SW))
981                 return 1;
982
983         if (e->code > SW_MAX)
984                 return 1;
985
986         set_bit_state(dev->sw_values, e->code, e->value != 0);
987
988         return 0;
989 }
990
991 static int
992 update_state(struct libevdev *dev, const struct input_event *e)
993 {
994         int rc = 0;
995
996         switch(e->type) {
997                 case EV_SYN:
998                 case EV_REL:
999                         break;
1000                 case EV_KEY:
1001                         rc = update_key_state(dev, e);
1002                         break;
1003                 case EV_ABS:
1004                         rc = update_abs_state(dev, e);
1005                         break;
1006                 case EV_LED:
1007                         rc = update_led_state(dev, e);
1008                         break;
1009                 case EV_SW:
1010                         rc = update_sw_state(dev, e);
1011                         break;
1012         }
1013
1014         dev->last_event_time.tv_sec = e->input_event_sec;
1015         dev->last_event_time.tv_usec = e->input_event_usec;
1016
1017         return rc;
1018 }
1019
1020 /**
1021  * Sanitize/modify events where needed.
1022  */
1023 static inline enum event_filter_status
1024 sanitize_event(const struct libevdev *dev,
1025                struct input_event *ev,
1026                enum SyncState sync_state)
1027 {
1028         if (!libevdev_has_event_code(dev, ev->type, ev->code))
1029                 return EVENT_FILTER_DISCARD;
1030
1031         if (unlikely(dev->num_slots > -1 &&
1032                      libevdev_event_is_code(ev, EV_ABS, ABS_MT_SLOT) &&
1033                      (ev->value < 0 || ev->value >= dev->num_slots))) {
1034                 log_bug(dev, "Device \"%s\" received an invalid slot index %d."
1035                                 "Capping to announced max slot number %d.\n",
1036                                 dev->name, ev->value, dev->num_slots - 1);
1037                 ev->value = dev->num_slots - 1;
1038                 return EVENT_FILTER_MODIFIED;
1039
1040         /* Drop any invalid tracking IDs, they are only supposed to go from
1041            N to -1 or from -1 to N. Never from -1 to -1, or N to M. Very
1042            unlikely to ever happen from a real device.
1043            */
1044         } else if (unlikely(sync_state == SYNC_NONE &&
1045                             dev->num_slots > -1 &&
1046                             libevdev_event_is_code(ev, EV_ABS, ABS_MT_TRACKING_ID) &&
1047                             ((ev->value == -1 &&
1048                              *slot_value(dev, dev->current_slot, ABS_MT_TRACKING_ID) == -1) ||
1049                              (ev->value != -1 &&
1050                              *slot_value(dev, dev->current_slot, ABS_MT_TRACKING_ID) != -1)))) {
1051                 log_bug(dev, "Device \"%s\" received a double tracking ID %d in slot %d.\n",
1052                         dev->name, ev->value, dev->current_slot);
1053                 return EVENT_FILTER_DISCARD;
1054         }
1055
1056         return EVENT_FILTER_NONE;
1057 }
1058
1059 LIBEVDEV_EXPORT int
1060 libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
1061 {
1062         int rc = LIBEVDEV_READ_STATUS_SUCCESS;
1063         enum event_filter_status filter_status;
1064         const unsigned int valid_flags = LIBEVDEV_READ_FLAG_NORMAL |
1065                                          LIBEVDEV_READ_FLAG_SYNC |
1066                                          LIBEVDEV_READ_FLAG_FORCE_SYNC |
1067                                          LIBEVDEV_READ_FLAG_BLOCKING;
1068
1069         if (!dev->initialized) {
1070                 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
1071                 return -EBADF;
1072         } else if (dev->fd < 0)
1073                 return -EBADF;
1074
1075         if ((flags & valid_flags) == 0) {
1076                 log_bug(dev, "invalid flags %#x.\n", flags);
1077                 return -EINVAL;
1078         }
1079
1080         if (flags & LIBEVDEV_READ_FLAG_SYNC) {
1081                 if (dev->sync_state == SYNC_NEEDED) {
1082                         rc = sync_state(dev);
1083                         if (rc != 0)
1084                                 return rc;
1085                         dev->sync_state = SYNC_IN_PROGRESS;
1086                 }
1087
1088                 if (dev->queue_nsync == 0) {
1089                         dev->sync_state = SYNC_NONE;
1090                         return -EAGAIN;
1091                 }
1092
1093         } else if (dev->sync_state != SYNC_NONE) {
1094                 struct input_event e;
1095
1096                 /* call update_state for all events here, otherwise the library has the wrong view
1097                    of the device too */
1098                 while (queue_shift(dev, &e) == 0) {
1099                         dev->queue_nsync--;
1100                         if (sanitize_event(dev, &e, dev->sync_state) != EVENT_FILTER_DISCARD)
1101                                 update_state(dev, &e);
1102                 }
1103
1104                 dev->sync_state = SYNC_NONE;
1105         }
1106
1107         /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
1108            worst case we don't read fast enough and end up with SYN_DROPPED anyway.
1109
1110            Except if the fd is in blocking mode and we still have events from the last read, don't
1111            read in any more.
1112          */
1113         do {
1114                 if (queue_num_elements(dev) == 0) {
1115                         rc = read_more_events(dev);
1116                         if (rc < 0 && rc != -EAGAIN)
1117                                 goto out;
1118                 }
1119
1120                 if (flags & LIBEVDEV_READ_FLAG_FORCE_SYNC) {
1121                         dev->sync_state = SYNC_NEEDED;
1122                         rc = LIBEVDEV_READ_STATUS_SYNC;
1123                         goto out;
1124                 }
1125
1126                 if (queue_shift(dev, ev) != 0)
1127                         return -EAGAIN;
1128
1129                 filter_status = sanitize_event(dev, ev, dev->sync_state);
1130                 if (filter_status != EVENT_FILTER_DISCARD)
1131                         update_state(dev, ev);
1132
1133         /* if we disabled a code, get the next event instead */
1134         } while(filter_status == EVENT_FILTER_DISCARD ||
1135                 !libevdev_has_event_code(dev, ev->type, ev->code));
1136
1137         rc = LIBEVDEV_READ_STATUS_SUCCESS;
1138         if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
1139                 dev->sync_state = SYNC_NEEDED;
1140                 rc = LIBEVDEV_READ_STATUS_SYNC;
1141         }
1142
1143         if (flags & LIBEVDEV_READ_FLAG_SYNC && dev->queue_nsync > 0) {
1144                 dev->queue_nsync--;
1145                 rc = LIBEVDEV_READ_STATUS_SYNC;
1146                 if (dev->queue_nsync == 0)
1147                         dev->sync_state = SYNC_NONE;
1148         }
1149
1150 out:
1151         return rc;
1152 }
1153
1154 LIBEVDEV_EXPORT int
1155 libevdev_has_event_pending(struct libevdev *dev)
1156 {
1157         struct pollfd fds = { dev->fd, POLLIN, 0 };
1158         int rc;
1159
1160         if (!dev->initialized) {
1161                 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
1162                 return -EBADF;
1163         } else if (dev->fd < 0)
1164                 return -EBADF;
1165
1166         if (queue_num_elements(dev) != 0)
1167                 return 1;
1168
1169         rc = poll(&fds, 1, 0);
1170         return (rc >= 0) ? rc : -errno;
1171 }
1172
1173 LIBEVDEV_EXPORT const char *
1174 libevdev_get_name(const struct libevdev *dev)
1175 {
1176         return dev->name ? dev->name : "";
1177 }
1178
1179 LIBEVDEV_EXPORT const char *
1180 libevdev_get_phys(const struct libevdev *dev)
1181 {
1182         return dev->phys;
1183 }
1184
1185 LIBEVDEV_EXPORT const char *
1186 libevdev_get_uniq(const struct libevdev *dev)
1187 {
1188         return dev->uniq;
1189 }
1190
1191 #define STRING_SETTER(field) \
1192 LIBEVDEV_EXPORT void libevdev_set_##field(struct libevdev *dev, const char *field) \
1193 { \
1194         if (field == NULL) \
1195                 return; \
1196         free(dev->field); \
1197         dev->field = strdup(field); \
1198 }
1199
1200 STRING_SETTER(name)
1201 STRING_SETTER(phys)
1202 STRING_SETTER(uniq)
1203
1204 #define PRODUCT_GETTER(name) \
1205 LIBEVDEV_EXPORT int libevdev_get_id_##name(const struct libevdev *dev) \
1206 { \
1207         return dev->ids.name; \
1208 }
1209
1210 PRODUCT_GETTER(product)
1211 PRODUCT_GETTER(vendor)
1212 PRODUCT_GETTER(bustype)
1213 PRODUCT_GETTER(version)
1214
1215 #define PRODUCT_SETTER(field) \
1216 LIBEVDEV_EXPORT void libevdev_set_id_##field(struct libevdev *dev, int field) \
1217 { \
1218         dev->ids.field = field;\
1219 }
1220
1221 PRODUCT_SETTER(product)
1222 PRODUCT_SETTER(vendor)
1223 PRODUCT_SETTER(bustype)
1224 PRODUCT_SETTER(version)
1225
1226 LIBEVDEV_EXPORT int
1227 libevdev_get_driver_version(const struct libevdev *dev)
1228 {
1229         return dev->driver_version;
1230 }
1231
1232 LIBEVDEV_EXPORT int
1233 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
1234 {
1235         return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
1236 }
1237
1238 LIBEVDEV_EXPORT int
1239 libevdev_enable_property(struct libevdev *dev, unsigned int prop)
1240 {
1241         if (prop > INPUT_PROP_MAX)
1242                 return -1;
1243
1244         set_bit(dev->props, prop);
1245         return 0;
1246 }
1247
1248 LIBEVDEV_EXPORT int
1249 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
1250 {
1251         return type == EV_SYN ||(type <= EV_MAX && bit_is_set(dev->bits, type));
1252 }
1253
1254 LIBEVDEV_EXPORT int
1255 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
1256 {
1257         const unsigned long *mask = NULL;
1258         int max;
1259
1260         if (!libevdev_has_event_type(dev, type))
1261                 return 0;
1262
1263         if (type == EV_SYN)
1264                 return 1;
1265
1266         max = type_to_mask_const(dev, type, &mask);
1267
1268         if (max == -1 || code > (unsigned int)max)
1269                 return 0;
1270
1271         return bit_is_set(mask, code);
1272 }
1273
1274 LIBEVDEV_EXPORT int
1275 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
1276 {
1277         int value = 0;
1278
1279         if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
1280                 return 0;
1281
1282         switch (type) {
1283                 case EV_ABS: value = dev->abs_info[code].value; break;
1284                 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
1285                 case EV_LED: value = bit_is_set(dev->led_values, code); break;
1286                 case EV_SW: value = bit_is_set(dev->sw_values, code); break;
1287                 case EV_REP:
1288                             switch(code) {
1289                                     case REP_DELAY:
1290                                             libevdev_get_repeat(dev, &value, NULL);
1291                                             break;
1292                                     case REP_PERIOD:
1293                                             libevdev_get_repeat(dev, NULL, &value);
1294                                             break;
1295                                     default:
1296                                             value = 0;
1297                                             break;
1298                             }
1299                             break;
1300                 default:
1301                         value = 0;
1302                         break;
1303         }
1304
1305         return value;
1306 }
1307
1308 LIBEVDEV_EXPORT int
1309 libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value)
1310 {
1311         int rc = 0;
1312         struct input_event e;
1313
1314         if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
1315                 return -1;
1316
1317         e.type = type;
1318         e.code = code;
1319         e.value = value;
1320
1321         if (sanitize_event(dev, &e, SYNC_NONE) != EVENT_FILTER_NONE)
1322                 return -1;
1323
1324         switch(type) {
1325                 case EV_ABS: rc = update_abs_state(dev, &e); break;
1326                 case EV_KEY: rc = update_key_state(dev, &e); break;
1327                 case EV_LED: rc = update_led_state(dev, &e); break;
1328                 case EV_SW: rc = update_sw_state(dev, &e); break;
1329                 default:
1330                              rc = -1;
1331                              break;
1332         }
1333
1334         return rc;
1335 }
1336
1337 LIBEVDEV_EXPORT int
1338 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
1339 {
1340         if (libevdev_has_event_type(dev, type) &&
1341             libevdev_has_event_code(dev, type, code)) {
1342                 *value = libevdev_get_event_value(dev, type, code);
1343                 return 1;
1344         } else
1345                 return 0;
1346 }
1347
1348 LIBEVDEV_EXPORT int
1349 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
1350 {
1351         if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1352                 return 0;
1353
1354         if (dev->num_slots < 0 || slot >= (unsigned int)dev->num_slots)
1355                 return 0;
1356
1357         if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1358                 return 0;
1359
1360         return *slot_value(dev, slot, code);
1361 }
1362
1363 LIBEVDEV_EXPORT int
1364 libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value)
1365 {
1366         if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1367                 return -1;
1368
1369         if (dev->num_slots == -1 || slot >= (unsigned int)dev->num_slots)
1370                 return -1;
1371
1372         if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1373                 return -1;
1374
1375         if (code == ABS_MT_SLOT) {
1376                 if (value < 0 || value >= libevdev_get_num_slots(dev))
1377                         return -1;
1378                 dev->current_slot = value;
1379         }
1380
1381         *slot_value(dev, slot, code) = value;
1382
1383         return 0;
1384 }
1385
1386 LIBEVDEV_EXPORT int
1387 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
1388 {
1389         if (libevdev_has_event_type(dev, EV_ABS) &&
1390             libevdev_has_event_code(dev, EV_ABS, code) &&
1391             dev->num_slots >= 0 &&
1392             slot < (unsigned int)dev->num_slots) {
1393                 *value = libevdev_get_slot_value(dev, slot, code);
1394                 return 1;
1395         } else
1396                 return 0;
1397 }
1398
1399 LIBEVDEV_EXPORT int
1400 libevdev_get_num_slots(const struct libevdev *dev)
1401 {
1402         return dev->num_slots;
1403 }
1404
1405 LIBEVDEV_EXPORT int
1406 libevdev_get_current_slot(const struct libevdev *dev)
1407 {
1408         return dev->current_slot;
1409 }
1410
1411 LIBEVDEV_EXPORT const struct input_absinfo*
1412 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
1413 {
1414         if (!libevdev_has_event_type(dev, EV_ABS) ||
1415             !libevdev_has_event_code(dev, EV_ABS, code))
1416                 return NULL;
1417
1418         return &dev->abs_info[code];
1419 }
1420
1421 #define ABS_GETTER(name) \
1422 LIBEVDEV_EXPORT int libevdev_get_abs_##name(const struct libevdev *dev, unsigned int code) \
1423 { \
1424         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code); \
1425         return absinfo ? absinfo->name : 0; \
1426 }
1427
1428 ABS_GETTER(maximum)
1429 ABS_GETTER(minimum)
1430 ABS_GETTER(fuzz)
1431 ABS_GETTER(flat)
1432 ABS_GETTER(resolution)
1433
1434 #define ABS_SETTER(field) \
1435 LIBEVDEV_EXPORT void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
1436 { \
1437         if (!libevdev_has_event_code(dev, EV_ABS, code)) \
1438                 return; \
1439         dev->abs_info[code].field = val; \
1440 }
1441
1442 ABS_SETTER(maximum)
1443 ABS_SETTER(minimum)
1444 ABS_SETTER(fuzz)
1445 ABS_SETTER(flat)
1446 ABS_SETTER(resolution)
1447
1448 LIBEVDEV_EXPORT void
1449 libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1450 {
1451         if (!libevdev_has_event_code(dev, EV_ABS, code))
1452                 return;
1453
1454         dev->abs_info[code] = *abs;
1455 }
1456
1457 LIBEVDEV_EXPORT int
1458 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
1459 {
1460         int max;
1461
1462         if (type > EV_MAX)
1463                 return -1;
1464
1465         if (libevdev_has_event_type(dev, type))
1466                 return 0;
1467
1468         max = libevdev_event_type_get_max(type);
1469         if (max == -1)
1470                 return -1;
1471
1472         set_bit(dev->bits, type);
1473
1474         if (type == EV_REP) {
1475                 int delay = 0, period = 0;
1476                 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
1477                 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
1478         }
1479         return 0;
1480 }
1481
1482 LIBEVDEV_EXPORT int
1483 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
1484 {
1485         int max;
1486
1487         if (type > EV_MAX || type == EV_SYN)
1488                 return -1;
1489
1490         max = libevdev_event_type_get_max(type);
1491         if (max == -1)
1492                 return -1;
1493
1494         clear_bit(dev->bits, type);
1495
1496         return 0;
1497 }
1498
1499 LIBEVDEV_EXPORT int
1500 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
1501                            unsigned int code, const void *data)
1502 {
1503         unsigned int max;
1504         unsigned long *mask = NULL;
1505
1506         if (libevdev_enable_event_type(dev, type))
1507                 return -1;
1508
1509         switch(type) {
1510                 case EV_SYN:
1511                         return 0;
1512                 case EV_ABS:
1513                 case EV_REP:
1514                         if (data == NULL)
1515                                 return -1;
1516                         break;
1517                 default:
1518                         if (data != NULL)
1519                                 return -1;
1520                         break;
1521         }
1522
1523         max = type_to_mask(dev, type, &mask);
1524
1525         if (code > max || (int)max == -1)
1526                 return -1;
1527
1528         set_bit(mask, code);
1529
1530         if (type == EV_ABS) {
1531                 const struct input_absinfo *abs = data;
1532                 dev->abs_info[code] = *abs;
1533                 if (code == ABS_MT_SLOT) {
1534                         if (init_slots(dev) != 0)
1535                                 return -1;
1536                 } else if (code == ABS_MT_TRACKING_ID) {
1537                         reset_tracking_ids(dev);
1538                 }
1539         } else if (type == EV_REP) {
1540                 const int *value = data;
1541                 dev->rep_values[code] = *value;
1542         }
1543
1544         return 0;
1545 }
1546
1547 LIBEVDEV_EXPORT int
1548 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
1549 {
1550         unsigned int max;
1551         unsigned long *mask = NULL;
1552
1553         if (type > EV_MAX || type == EV_SYN)
1554                 return -1;
1555
1556         max = type_to_mask(dev, type, &mask);
1557
1558         if (code > max || (int)max == -1)
1559                 return -1;
1560
1561         clear_bit(mask, code);
1562
1563         if (type == EV_ABS) {
1564                 if (code == ABS_MT_SLOT) {
1565                         if (init_slots(dev) != 0)
1566                                 return -1;
1567                 } else if (code == ABS_MT_TRACKING_ID) {
1568                         reset_tracking_ids(dev);
1569                 }
1570         }
1571
1572         return 0;
1573 }
1574
1575 LIBEVDEV_EXPORT int
1576 libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1577 {
1578         int rc;
1579
1580         if (!dev->initialized) {
1581                 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
1582                 return -EBADF;
1583         } else if (dev->fd < 0)
1584                 return -EBADF;
1585
1586         if (code > ABS_MAX)
1587                 return -EINVAL;
1588
1589         rc = ioctl(dev->fd, EVIOCSABS(code), abs);
1590         if (rc < 0)
1591                 rc = -errno;
1592         else
1593                 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
1594
1595         return rc;
1596 }
1597
1598 LIBEVDEV_EXPORT int
1599 libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
1600 {
1601         int rc = 0;
1602
1603         if (!dev->initialized) {
1604                 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
1605                 return -EBADF;
1606         } else if (dev->fd < 0)
1607                 return -EBADF;
1608
1609         if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB) {
1610                 log_bug(dev, "invalid grab parameter %#x\n", grab);
1611                 return -EINVAL;
1612         }
1613
1614         if (grab == dev->grabbed)
1615                 return 0;
1616
1617         if (grab == LIBEVDEV_GRAB)
1618                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
1619         else if (grab == LIBEVDEV_UNGRAB)
1620                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
1621
1622         if (rc == 0)
1623                 dev->grabbed = grab;
1624
1625         return rc < 0 ? -errno : 0;
1626 }
1627
1628 LIBEVDEV_EXPORT int
1629 libevdev_event_is_type(const struct input_event *ev, unsigned int type)
1630 {
1631         return type < EV_CNT && ev->type == type;
1632 }
1633
1634 LIBEVDEV_EXPORT int
1635 libevdev_event_is_code(const struct input_event *ev, unsigned int type, unsigned int code)
1636 {
1637         int max;
1638
1639         if (!libevdev_event_is_type(ev, type))
1640                 return 0;
1641
1642         max = libevdev_event_type_get_max(type);
1643         return (max > -1 && code <= (unsigned int)max && ev->code == code);
1644 }
1645
1646 LIBEVDEV_EXPORT const char*
1647 libevdev_event_type_get_name(unsigned int type)
1648 {
1649         if (type > EV_MAX)
1650                 return NULL;
1651
1652         return ev_map[type];
1653 }
1654
1655 LIBEVDEV_EXPORT const char*
1656 libevdev_event_code_get_name(unsigned int type, unsigned int code)
1657 {
1658         int max = libevdev_event_type_get_max(type);
1659
1660         if (max == -1 || code > (unsigned int)max)
1661                 return NULL;
1662
1663         return event_type_map[type][code];
1664 }
1665
1666 LIBEVDEV_EXPORT const char *
1667 libevdev_event_value_get_name(unsigned int type,
1668                               unsigned int code,
1669                               int value)
1670 {
1671         /* This is a simplified version because nothing else
1672            is an enum like ABS_MT_TOOL_TYPE so we don't need
1673            a generic lookup */
1674         if (type != EV_ABS || code != ABS_MT_TOOL_TYPE)
1675                 return NULL;
1676
1677         if (value > MT_TOOL_MAX)
1678                 return NULL;
1679
1680         return mt_tool_map[value];
1681 }
1682
1683 LIBEVDEV_EXPORT const char*
1684 libevdev_property_get_name(unsigned int prop)
1685 {
1686         if (prop > INPUT_PROP_MAX)
1687                 return NULL;
1688
1689         return input_prop_map[prop];
1690 }
1691
1692 LIBEVDEV_EXPORT int
1693 libevdev_event_type_get_max(unsigned int type)
1694 {
1695         if (type > EV_MAX)
1696                 return -1;
1697
1698         return ev_max[type];
1699 }
1700
1701 LIBEVDEV_EXPORT int
1702 libevdev_get_repeat(const struct libevdev *dev, int *delay, int *period)
1703 {
1704         if (!libevdev_has_event_type(dev, EV_REP))
1705                 return -1;
1706
1707         if (delay != NULL)
1708                 *delay = dev->rep_values[REP_DELAY];
1709         if (period != NULL)
1710                 *period = dev->rep_values[REP_PERIOD];
1711
1712         return 0;
1713 }
1714
1715 LIBEVDEV_EXPORT int
1716 libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value)
1717 {
1718         return libevdev_kernel_set_led_values(dev, code, value, -1);
1719 }
1720
1721 LIBEVDEV_EXPORT int
1722 libevdev_kernel_set_led_values(struct libevdev *dev, ...)
1723 {
1724         struct input_event ev[LED_MAX + 1];
1725         enum libevdev_led_value val;
1726         va_list args;
1727         int code;
1728         int rc = 0;
1729         size_t nleds = 0;
1730
1731         if (!dev->initialized) {
1732                 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
1733                 return -EBADF;
1734         } else if (dev->fd < 0)
1735                 return -EBADF;
1736
1737         memset(ev, 0, sizeof(ev));
1738
1739         va_start(args, dev);
1740         code = va_arg(args, unsigned int);
1741         while (code != -1) {
1742                 if (code > LED_MAX) {
1743                         rc = -EINVAL;
1744                         break;
1745                 }
1746                 val = va_arg(args, enum libevdev_led_value);
1747                 if (val != LIBEVDEV_LED_ON && val != LIBEVDEV_LED_OFF) {
1748                         rc = -EINVAL;
1749                         break;
1750                 }
1751
1752                 if (libevdev_has_event_code(dev, EV_LED, code)) {
1753                         struct input_event *e = ev;
1754
1755                         while (e->type > 0 && e->code != code)
1756                                 e++;
1757
1758                         if (e->type == 0)
1759                                 nleds++;
1760                         e->type = EV_LED;
1761                         e->code = code;
1762                         e->value = (val == LIBEVDEV_LED_ON);
1763                 }
1764                 code = va_arg(args, unsigned int);
1765         }
1766         va_end(args);
1767
1768         if (rc == 0 && nleds > 0) {
1769                 ev[nleds].type = EV_SYN;
1770                 ev[nleds++].code = SYN_REPORT;
1771
1772                 rc = write(libevdev_get_fd(dev), ev, nleds * sizeof(ev[0]));
1773                 if (rc > 0) {
1774                         nleds--; /* last is EV_SYN */
1775                         while (nleds--)
1776                                 update_led_state(dev, &ev[nleds]);
1777                 }
1778                 rc = (rc != -1) ? 0 : -errno;
1779         }
1780
1781         return rc;
1782 }
1783
1784 LIBEVDEV_EXPORT int
1785 libevdev_set_clock_id(struct libevdev *dev, int clockid)
1786 {
1787         if (!dev->initialized) {
1788                 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
1789                 return -EBADF;
1790         } else if (dev->fd < 0)
1791                 return -EBADF;
1792
1793         return ioctl(dev->fd, EVIOCSCLOCKID, &clockid) ? -errno : 0;
1794 }