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