Log a few errors, specifically application bugs
[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 <unistd.h>
29 #include <stdarg.h>
30
31 #include "libevdev.h"
32 #include "libevdev-int.h"
33 #include "libevdev-util.h"
34 #include "event-names.h"
35
36 #define MAXEVENTS 64
37
38 static int sync_mt_state(struct libevdev *dev, int create_events);
39
40 static int
41 init_event_queue(struct libevdev *dev)
42 {
43         /* FIXME: count the number of axes, keys, etc. to get a better idea at how many events per
44            EV_SYN we could possibly get. Then multiply that by the actual buffer size we care about */
45
46         const int QUEUE_SIZE = 256;
47
48         return queue_alloc(dev, QUEUE_SIZE);
49 }
50
51 static void
52 libevdev_noop_log_func(enum libevdev_log_priority priority,
53                        void *data,
54                        const char *file, int line, const char *func,
55                        const char *format, va_list args)
56 {
57 }
58
59 /*
60  * Global logging settings.
61  */
62 struct logdata log_data = {
63         LIBEVDEV_LOG_INFO,
64         libevdev_noop_log_func,
65         NULL,
66 };
67
68 void
69 log_msg(enum libevdev_log_priority priority,
70         void *data,
71         const char *file, int line, const char *func,
72         const char *format, ...)
73 {
74         va_list args;
75
76         va_start(args, format);
77         log_data.handler(priority, data, file, line, func, format, args);
78         va_end(args);
79 }
80
81 LIBEVDEV_EXPORT struct libevdev*
82 libevdev_new(void)
83 {
84         struct libevdev *dev;
85
86         dev = calloc(1, sizeof(*dev));
87         if (!dev)
88                 return NULL;
89         dev->fd = -1;
90         dev->num_slots = -1;
91         dev->current_slot = -1;
92         dev->grabbed = LIBEVDEV_UNGRAB;
93         dev->sync_state = SYNC_NONE;
94
95         return dev;
96 }
97
98 LIBEVDEV_EXPORT int
99 libevdev_new_from_fd(int fd, struct libevdev **dev)
100 {
101         struct libevdev *d;
102         int rc;
103
104         d = libevdev_new();
105         if (!d)
106                 return -ENOMEM;
107
108         rc = libevdev_set_fd(d, fd);
109         if (rc < 0)
110                 libevdev_free(d);
111         else
112                 *dev = d;
113         return rc;
114 }
115
116 LIBEVDEV_EXPORT void
117 libevdev_free(struct libevdev *dev)
118 {
119         if (!dev)
120                 return;
121
122         free(dev->name);
123         free(dev->phys);
124         free(dev->uniq);
125         queue_free(dev);
126         free(dev);
127 }
128
129 /* DEPRECATED */
130 LIBEVDEV_EXPORT void
131 libevdev_set_log_handler(struct libevdev *dev, libevdev_log_func_t logfunc)
132 {
133         /* Can't be backwards compatible to this yet, so don't even try */
134         fprintf(stderr, "libevdev: ABI change. Log function will not be honored.\n");
135 }
136
137 LIBEVDEV_EXPORT void
138 libevdev_set_log_function(libevdev_log_func_t logfunc, void *data)
139 {
140         log_data.handler = logfunc ? logfunc : libevdev_noop_log_func;
141         log_data.userdata = data;
142 }
143
144 LIBEVDEV_EXPORT void
145 libevdev_set_log_priority(enum libevdev_log_priority priority)
146 {
147         if (priority > LIBEVDEV_LOG_DEBUG)
148                 priority = LIBEVDEV_LOG_DEBUG;
149         log_data.priority = priority;
150 }
151
152 LIBEVDEV_EXPORT enum libevdev_log_priority
153 libevdev_get_log_priority(void)
154 {
155         return log_data.priority;
156 }
157
158 LIBEVDEV_EXPORT int
159 libevdev_change_fd(struct libevdev *dev, int fd)
160 {
161         if (dev->fd == -1) {
162                 log_bug("device not initialized. call libevdev_set_fd() first\n");
163                 return -1;
164         }
165         dev->fd = fd;
166         return 0;
167 }
168
169 LIBEVDEV_EXPORT int
170 libevdev_set_fd(struct libevdev* dev, int fd)
171 {
172         int rc;
173         int i;
174         char buf[256];
175
176         if (dev->fd != -1) {
177                 log_bug("device already initialized.\n");
178                 return -EBADF;
179         }
180
181         rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
182         if (rc < 0)
183                 goto out;
184
185         memset(buf, 0, sizeof(buf));
186         rc = ioctl(fd, EVIOCGNAME(sizeof(buf) - 1), buf);
187         if (rc < 0)
188                 goto out;
189
190         free(dev->name);
191         dev->name = strdup(buf);
192         if (!dev->name) {
193                 errno = ENOMEM;
194                 goto out;
195         }
196
197         free(dev->phys);
198         dev->phys = NULL;
199         memset(buf, 0, sizeof(buf));
200         rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
201         if (rc < 0) {
202                 /* uinput has no phys */
203                 if (errno != ENOENT)
204                         goto out;
205         } else {
206                 dev->phys = strdup(buf);
207                 if (!dev->phys) {
208                         errno = ENOMEM;
209                         goto out;
210                 }
211         }
212
213         free(dev->uniq);
214         dev->uniq = NULL;
215         memset(buf, 0, sizeof(buf));
216         rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
217         if (rc < 0) {
218                 if (errno != ENOENT)
219                         goto out;
220         } else  {
221                 dev->uniq = strdup(buf);
222                 if (!dev->uniq) {
223                         errno = ENOMEM;
224                         goto out;
225                 }
226         }
227
228         rc = ioctl(fd, EVIOCGID, &dev->ids);
229         if (rc < 0)
230                 goto out;
231
232         rc = ioctl(fd, EVIOCGVERSION, &dev->driver_version);
233         if (rc < 0)
234                 goto out;
235
236         rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
237         if (rc < 0)
238                 goto out;
239
240         rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
241         if (rc < 0)
242                 goto out;
243
244         rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
245         if (rc < 0)
246                 goto out;
247
248         rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
249         if (rc < 0)
250                 goto out;
251
252         rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
253         if (rc < 0)
254                 goto out;
255
256         rc = ioctl(fd, EVIOCGBIT(EV_SW, sizeof(dev->sw_bits)), dev->sw_bits);
257         if (rc < 0)
258                 goto out;
259
260         rc = ioctl(fd, EVIOCGBIT(EV_MSC, sizeof(dev->msc_bits)), dev->msc_bits);
261         if (rc < 0)
262                 goto out;
263
264         rc = ioctl(fd, EVIOCGBIT(EV_FF, sizeof(dev->ff_bits)), dev->ff_bits);
265         if (rc < 0)
266                 goto out;
267
268         rc = ioctl(fd, EVIOCGBIT(EV_SND, sizeof(dev->snd_bits)), dev->snd_bits);
269         if (rc < 0)
270                 goto out;
271
272         rc = ioctl(fd, EVIOCGKEY(sizeof(dev->key_values)), dev->key_values);
273         if (rc < 0)
274                 goto out;
275
276         rc = ioctl(fd, EVIOCGLED(sizeof(dev->led_values)), dev->led_values);
277         if (rc < 0)
278                 goto out;
279
280         rc = ioctl(fd, EVIOCGSW(sizeof(dev->sw_values)), dev->sw_values);
281         if (rc < 0)
282                 goto out;
283
284         /* rep is a special case, always set it to 1 for both values if EV_REP is set */
285         if (bit_is_set(dev->bits, EV_REP)) {
286                 for (i = 0; i < REP_CNT; i++)
287                         set_bit(dev->rep_bits, i);
288                 rc = ioctl(fd, EVIOCGREP, dev->rep_values);
289                 if (rc < 0)
290                         goto out;
291         }
292
293         for (i = ABS_X; i <= ABS_MAX; i++) {
294                 if (bit_is_set(dev->abs_bits, i)) {
295                         struct input_absinfo abs_info;
296                         rc = ioctl(fd, EVIOCGABS(i), &abs_info);
297                         if (rc < 0)
298                                 goto out;
299
300                         dev->abs_info[i] = abs_info;
301                         if (i == ABS_MT_SLOT) {
302                                 dev->num_slots = abs_info.maximum + 1;
303                                 dev->current_slot = abs_info.value;
304                         }
305
306                 }
307         }
308
309         dev->fd = fd;
310         sync_mt_state(dev, 0);
311
312         rc = init_event_queue(dev);
313         if (rc < 0) {
314                 dev->fd = -1;
315                 return -rc;
316         }
317
318         /* not copying key state because we won't know when we'll start to
319          * use this fd and key's are likely to change state by then.
320          * Same with the valuators, really, but they may not change.
321          */
322
323 out:
324         return rc ? -errno : 0;
325 }
326
327 LIBEVDEV_EXPORT int
328 libevdev_get_fd(const struct libevdev* dev)
329 {
330         return dev->fd;
331 }
332
333 static inline void
334 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
335 {
336         ev->time = dev->last_event_time;
337         ev->type = type;
338         ev->code = code;
339         ev->value = value;
340 }
341
342 static int
343 sync_key_state(struct libevdev *dev)
344 {
345         int rc;
346         int i;
347         unsigned long keystate[NLONGS(KEY_CNT)] = {0};
348
349         rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
350         if (rc < 0)
351                 goto out;
352
353         for (i = 0; i < KEY_CNT; i++) {
354                 int old, new;
355                 old = bit_is_set(dev->key_values, i);
356                 new = bit_is_set(keystate, i);
357                 if (old ^ new) {
358                         struct input_event *ev = queue_push(dev);
359                         init_event(dev, ev, EV_KEY, i, new ? 1 : 0);
360                 }
361         }
362
363         memcpy(dev->key_values, keystate, rc);
364
365         rc = 0;
366 out:
367         return rc ? -errno : 0;
368 }
369
370 static int
371 sync_sw_state(struct libevdev *dev)
372 {
373         int rc;
374         int i;
375         unsigned long swstate[NLONGS(SW_CNT)] = {0};
376
377         rc = ioctl(dev->fd, EVIOCGSW(sizeof(swstate)), swstate);
378         if (rc < 0)
379                 goto out;
380
381         for (i = 0; i < SW_CNT; i++) {
382                 int old, new;
383                 old = bit_is_set(dev->sw_values, i);
384                 new = bit_is_set(swstate, i);
385                 if (old ^ new) {
386                         struct input_event *ev = queue_push(dev);
387                         init_event(dev, ev, EV_SW, i, new ? 1 : 0);
388                 }
389         }
390
391         memcpy(dev->sw_values, swstate, rc);
392
393         rc = 0;
394 out:
395         return rc ? -errno : 0;
396 }
397
398 static int
399 sync_led_state(struct libevdev *dev)
400 {
401         int rc;
402         int i;
403         unsigned long ledstate[NLONGS(LED_CNT)] = {0};
404
405         rc = ioctl(dev->fd, EVIOCGLED(sizeof(ledstate)), ledstate);
406         if (rc < 0)
407                 goto out;
408
409         for (i = 0; i < LED_CNT; i++) {
410                 int old, new;
411                 old = bit_is_set(dev->led_values, i);
412                 new = bit_is_set(ledstate, i);
413                 if (old ^ new) {
414                         struct input_event *ev = queue_push(dev);
415                         init_event(dev, ev, EV_LED, i, new ? 1 : 0);
416                 }
417         }
418
419         memcpy(dev->led_values, ledstate, rc);
420
421         rc = 0;
422 out:
423         return rc ? -errno : 0;
424 }
425 static int
426 sync_abs_state(struct libevdev *dev)
427 {
428         int rc;
429         int i;
430
431         for (i = ABS_X; i < ABS_CNT; i++) {
432                 struct input_absinfo abs_info;
433
434                 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
435                         continue;
436
437                 if (!bit_is_set(dev->abs_bits, i))
438                         continue;
439
440                 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
441                 if (rc < 0)
442                         goto out;
443
444                 if (dev->abs_info[i].value != abs_info.value) {
445                         struct input_event *ev = queue_push(dev);
446
447                         init_event(dev, ev, EV_ABS, i, abs_info.value);
448                         dev->abs_info[i].value = abs_info.value;
449                 }
450         }
451
452         rc = 0;
453 out:
454         return rc ? -errno : 0;
455 }
456
457 static int
458 sync_mt_state(struct libevdev *dev, int create_events)
459 {
460         int rc;
461         int i;
462         struct mt_state {
463                 int code;
464                 int val[MAX_SLOTS];
465         } mt_state[ABS_MT_CNT];
466
467         for (i = ABS_MT_MIN; i <= ABS_MT_MAX; i++) {
468                 int idx;
469                 if (i == ABS_MT_SLOT)
470                         continue;
471
472                 if (!libevdev_has_event_code(dev, EV_ABS, i))
473                         continue;
474
475                 idx = i - ABS_MT_MIN;
476                 mt_state[idx].code = i;
477                 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
478                 if (rc < 0)
479                         goto out;
480         }
481
482         for (i = 0; i < dev->num_slots; i++) {
483                 int j;
484                 struct input_event *ev;
485
486                 if (create_events) {
487                         ev = queue_push(dev);
488                         init_event(dev, ev, EV_ABS, ABS_MT_SLOT, i);
489                 }
490
491                 for (j = ABS_MT_MIN; j <= ABS_MT_MAX; j++) {
492                         int jdx = j - ABS_MT_MIN;
493
494                         if (j == ABS_MT_SLOT)
495                                 continue;
496
497                         if (!libevdev_has_event_code(dev, EV_ABS, j))
498                                 continue;
499
500                         if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
501                                 continue;
502
503                         if (create_events) {
504                                 ev = queue_push(dev);
505                                 init_event(dev, ev, EV_ABS, j, mt_state[jdx].val[i]);
506                         }
507                         dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
508                 }
509         }
510
511         rc = 0;
512 out:
513         return rc ? -errno : 0;
514 }
515
516 static int
517 sync_state(struct libevdev *dev)
518 {
519         int i;
520         int rc = 0;
521         struct input_event *ev;
522
523         /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
524            queue[0]) we need to shift this backwards. Except that chances are that the
525            queue may be either full or too full to prepend all the events needed for
526            SYNC_IN_PROGRESS.
527
528            so we search for the last sync event in the queue and drop everything before
529            including that event and rely on the kernel to tell us the right value for that
530            bitfield during the sync process.
531          */
532
533         for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
534                 struct input_event e = {{0,0}, 0, 0, 0};
535                 queue_peek(dev, i, &e);
536                 if (e.type == EV_SYN)
537                         break;
538         }
539
540         if (i > 0)
541                 queue_shift_multiple(dev, i + 1, NULL);
542
543         if (libevdev_has_event_type(dev, EV_KEY))
544                 rc = sync_key_state(dev);
545         if (libevdev_has_event_type(dev, EV_LED))
546                 rc = sync_led_state(dev);
547         if (libevdev_has_event_type(dev, EV_SW))
548                 rc = sync_sw_state(dev);
549         if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
550                 rc = sync_abs_state(dev);
551         if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
552                 rc = sync_mt_state(dev, 1);
553
554         dev->queue_nsync = queue_num_elements(dev);
555
556         if (dev->queue_nsync > 0) {
557                 ev = queue_push(dev);
558                 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
559                 dev->queue_nsync++;
560         }
561
562         return rc;
563 }
564
565 static int
566 update_key_state(struct libevdev *dev, const struct input_event *e)
567 {
568         if (!libevdev_has_event_type(dev, EV_KEY))
569                 return 1;
570
571         if (e->code > KEY_MAX)
572                 return 1;
573
574         set_bit_state(dev->key_values, e->code, e->value != 0);
575
576         return 0;
577 }
578
579 static int
580 update_mt_state(struct libevdev *dev, const struct input_event *e)
581 {
582         if (e->code == ABS_MT_SLOT) {
583                 int i;
584                 dev->current_slot = e->value;
585                 /* sync abs_info with the current slot values */
586                 for (i = ABS_MT_SLOT + 1; i <= ABS_MT_MAX; i++) {
587                         if (libevdev_has_event_code(dev, EV_ABS, i))
588                                 dev->abs_info[i].value = dev->mt_slot_vals[dev->current_slot][i - ABS_MT_MIN];
589                 }
590
591                 return 0;
592         } else if (dev->current_slot == -1)
593                 return 1;
594
595         dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
596
597         return 0;
598 }
599
600 static int
601 update_abs_state(struct libevdev *dev, const struct input_event *e)
602 {
603         if (!libevdev_has_event_type(dev, EV_ABS))
604                 return 1;
605
606         if (e->code > ABS_MAX)
607                 return 1;
608
609         if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
610                 update_mt_state(dev, e);
611
612         dev->abs_info[e->code].value = e->value;
613
614         return 0;
615 }
616
617 static int
618 update_led_state(struct libevdev *dev, const struct input_event *e)
619 {
620         if (!libevdev_has_event_type(dev, EV_LED))
621                 return 1;
622
623         if (e->code > LED_MAX)
624                 return 1;
625
626         set_bit_state(dev->led_values, e->code, e->value != 0);
627
628         return 0;
629 }
630
631 static int
632 update_sw_state(struct libevdev *dev, const struct input_event *e)
633 {
634         if (!libevdev_has_event_type(dev, EV_SW))
635                 return 1;
636
637         if (e->code > SW_MAX)
638                 return 1;
639
640         set_bit_state(dev->sw_values, e->code, e->value != 0);
641
642         return 0;
643 }
644
645 static int
646 update_state(struct libevdev *dev, const struct input_event *e)
647 {
648         int rc = 0;
649
650         switch(e->type) {
651                 case EV_SYN:
652                 case EV_REL:
653                         break;
654                 case EV_KEY:
655                         rc = update_key_state(dev, e);
656                         break;
657                 case EV_ABS:
658                         rc = update_abs_state(dev, e);
659                         break;
660                 case EV_LED:
661                         rc = update_led_state(dev, e);
662                         break;
663                 case EV_SW:
664                         rc = update_sw_state(dev, e);
665                         break;
666         }
667
668         dev->last_event_time = e->time;
669
670         return rc;
671 }
672
673 static int
674 read_more_events(struct libevdev *dev)
675 {
676         int free_elem;
677         int len;
678         struct input_event *next;
679
680         free_elem = queue_num_free_elements(dev);
681         if (free_elem <= 0)
682                 return 0;
683
684         next = queue_next_element(dev);
685         len = read(dev->fd, next, free_elem * sizeof(struct input_event));
686         if (len < 0) {
687                 return -errno;
688         } else if (len > 0 && len % sizeof(struct input_event) != 0)
689                 return -EINVAL;
690         else if (len > 0) {
691                 int nev = len/sizeof(struct input_event);
692                 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
693         }
694
695         return 0;
696 }
697
698 LIBEVDEV_EXPORT int
699 libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
700 {
701         int rc = 0;
702
703         if (dev->fd < 0) {
704                 log_bug("device not initialized. call libevdev_set_fd() first\n");
705                 return -EBADF;
706         }
707
708         if (!(flags & (LIBEVDEV_READ_NORMAL|LIBEVDEV_READ_SYNC|LIBEVDEV_FORCE_SYNC))) {
709                 log_bug("invalid flags %#x\n.\n", flags);
710                 return -EINVAL;
711         }
712
713         if (flags & LIBEVDEV_READ_SYNC) {
714                 if (dev->sync_state == SYNC_NEEDED) {
715                         rc = sync_state(dev);
716                         if (rc != 0)
717                                 return rc;
718                         dev->sync_state = SYNC_IN_PROGRESS;
719                 }
720
721                 if (dev->queue_nsync == 0) {
722                         dev->sync_state = SYNC_NONE;
723                         return -EAGAIN;
724                 }
725
726         } else if (dev->sync_state != SYNC_NONE) {
727                 struct input_event e;
728
729                 /* call update_state for all events here, otherwise the library has the wrong view
730                    of the device too */
731                 while (queue_shift(dev, &e) == 0) {
732                         dev->queue_nsync--;
733                         update_state(dev, &e);
734                 }
735
736                 dev->sync_state = SYNC_NONE;
737         }
738
739         /* FIXME: if the first event after SYNC_IN_PROGRESS is a SYN_DROPPED, log this */
740
741         /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
742            worst case we don't read fast enough and end up with SYN_DROPPED anyway.
743
744            Except if the fd is in blocking mode and we still have events from the last read, don't
745            read in any more.
746          */
747         do {
748                 if (!(flags & LIBEVDEV_READ_BLOCKING) ||
749                     queue_num_elements(dev) == 0) {
750                         rc = read_more_events(dev);
751                         if (rc < 0 && rc != -EAGAIN)
752                                 goto out;
753                 }
754
755                 if (flags & LIBEVDEV_FORCE_SYNC) {
756                         dev->sync_state = SYNC_NEEDED;
757                         rc = 1;
758                         goto out;
759                 }
760
761
762                 if (queue_shift(dev, ev) != 0)
763                         return -EAGAIN;
764
765                 update_state(dev, ev);
766
767         /* if we disabled a code, get the next event instead */
768         } while(!libevdev_has_event_code(dev, ev->type, ev->code));
769
770         rc = 0;
771         if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
772                 dev->sync_state = SYNC_NEEDED;
773                 rc = 1;
774         }
775
776         if (flags & LIBEVDEV_READ_SYNC && dev->queue_nsync > 0) {
777                 dev->queue_nsync--;
778                 rc = 1;
779                 if (dev->queue_nsync == 0)
780                         dev->sync_state = SYNC_NONE;
781         }
782
783 out:
784         return rc;
785 }
786
787 LIBEVDEV_EXPORT int
788 libevdev_has_event_pending(struct libevdev *dev)
789 {
790         struct pollfd fds = { dev->fd, POLLIN, 0 };
791         int rc;
792
793         if (dev->fd < 0) {
794                 log_bug("device not initialized. call libevdev_set_fd() first\n");
795                 return -EBADF;
796         }
797
798         if (queue_num_elements(dev) != 0)
799                 return 1;
800
801         rc = poll(&fds, 1, 0);
802         return (rc >= 0) ? rc : -errno;
803 }
804
805 LIBEVDEV_EXPORT const char *
806 libevdev_get_name(const struct libevdev *dev)
807 {
808         return dev->name ? dev->name : "";
809 }
810
811 LIBEVDEV_EXPORT const char *
812 libevdev_get_phys(const struct libevdev *dev)
813 {
814         return dev->phys;
815 }
816
817 LIBEVDEV_EXPORT const char *
818 libevdev_get_uniq(const struct libevdev *dev)
819 {
820         return dev->uniq;
821 }
822
823 #define STRING_SETTER(field) \
824 LIBEVDEV_EXPORT void libevdev_set_##field(struct libevdev *dev, const char *field) \
825 { \
826         if (field == NULL) \
827                 return; \
828         free(dev->field); \
829         dev->field = strdup(field); \
830 }
831
832 STRING_SETTER(name);
833 STRING_SETTER(phys);
834 STRING_SETTER(uniq);
835
836
837 #define PRODUCT_GETTER(name) \
838 LIBEVDEV_EXPORT int libevdev_get_id_##name(const struct libevdev *dev) \
839 { \
840         return dev->ids.name; \
841 }
842
843 PRODUCT_GETTER(product);
844 PRODUCT_GETTER(vendor);
845 PRODUCT_GETTER(bustype);
846 PRODUCT_GETTER(version);
847
848 #define PRODUCT_SETTER(field) \
849 LIBEVDEV_EXPORT void libevdev_set_id_##field(struct libevdev *dev, int field) \
850 { \
851         dev->ids.field = field;\
852 }
853
854 PRODUCT_SETTER(product);
855 PRODUCT_SETTER(vendor);
856 PRODUCT_SETTER(bustype);
857 PRODUCT_SETTER(version);
858
859 LIBEVDEV_EXPORT int
860 libevdev_get_driver_version(const struct libevdev *dev)
861 {
862         return dev->driver_version;
863 }
864
865 LIBEVDEV_EXPORT int
866 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
867 {
868         return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
869 }
870
871 LIBEVDEV_EXPORT int
872 libevdev_enable_property(struct libevdev *dev, unsigned int prop)
873 {
874         if (prop > INPUT_PROP_MAX)
875                 return -1;
876
877         set_bit(dev->props, prop);
878         return 0;
879 }
880
881 LIBEVDEV_EXPORT int
882 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
883 {
884         return (type <= EV_MAX) && bit_is_set(dev->bits, type);
885 }
886
887 LIBEVDEV_EXPORT int
888 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
889 {
890         const unsigned long *mask;
891         int max;
892
893         if (!libevdev_has_event_type(dev, type))
894                 return 0;
895
896         if (type == EV_SYN)
897                 return 1;
898
899         max = type_to_mask_const(dev, type, &mask);
900
901         if (max == -1 || code > (unsigned int)max)
902                 return 0;
903
904         return bit_is_set(mask, code);
905 }
906
907 LIBEVDEV_EXPORT int
908 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
909 {
910         int value;
911
912         if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
913                 return 0;
914
915         switch (type) {
916                 case EV_ABS: value = dev->abs_info[code].value; break;
917                 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
918                 case EV_LED: value = bit_is_set(dev->led_values, code); break;
919                 case EV_SW: value = bit_is_set(dev->sw_values, code); break;
920                 default:
921                         value = 0;
922                         break;
923         }
924
925         return value;
926 }
927
928 LIBEVDEV_EXPORT int
929 libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value)
930 {
931         int rc = 0;
932         struct input_event e;
933
934         if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
935                 return -1;
936
937         e.type = type;
938         e.code = code;
939         e.value = value;
940
941         switch(type) {
942                 case EV_ABS: rc = update_abs_state(dev, &e); break;
943                 case EV_KEY: rc = update_key_state(dev, &e); break;
944                 case EV_LED: rc = update_led_state(dev, &e); break;
945                 case EV_SW: rc = update_sw_state(dev, &e); break;
946                 default:
947                              rc = -1;
948                              break;
949         }
950
951         return rc;
952 }
953
954 LIBEVDEV_EXPORT int
955 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
956 {
957         if (libevdev_has_event_type(dev, type) &&
958             libevdev_has_event_code(dev, type, code)) {
959                 *value = libevdev_get_event_value(dev, type, code);
960                 return 1;
961         } else
962                 return 0;
963 }
964
965 LIBEVDEV_EXPORT int
966 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
967 {
968         if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
969                 return 0;
970
971         if (dev->num_slots < 0 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
972                 return 0;
973
974         if (code > ABS_MT_MAX || code < ABS_MT_MIN)
975                 return 0;
976
977         return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
978 }
979
980 LIBEVDEV_EXPORT int
981 libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value)
982 {
983         if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
984                 return -1;
985
986         if (dev->num_slots == -1 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
987                 return -1;
988
989         if (code > ABS_MT_MAX || code < ABS_MT_MIN)
990                 return -1;
991
992         if (code == ABS_MT_SLOT) {
993                 if (value < 0 || value >= libevdev_get_num_slots(dev))
994                         return -1;
995                 dev->current_slot = value;
996         }
997
998         dev->mt_slot_vals[slot][code - ABS_MT_MIN] = value;
999
1000
1001         return 0;
1002 }
1003
1004 LIBEVDEV_EXPORT int
1005 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
1006 {
1007         if (libevdev_has_event_type(dev, EV_ABS) &&
1008             libevdev_has_event_code(dev, EV_ABS, code) &&
1009             dev->num_slots >= 0 &&
1010             slot < (unsigned int)dev->num_slots && slot < MAX_SLOTS) {
1011                 *value = libevdev_get_slot_value(dev, slot, code);
1012                 return 1;
1013         } else
1014                 return 0;
1015 }
1016
1017 LIBEVDEV_EXPORT int
1018 libevdev_get_num_slots(const struct libevdev *dev)
1019 {
1020         return dev->num_slots;
1021 }
1022
1023 LIBEVDEV_EXPORT int
1024 libevdev_get_current_slot(const struct libevdev *dev)
1025 {
1026         return dev->current_slot;
1027 }
1028
1029 LIBEVDEV_EXPORT const struct input_absinfo*
1030 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
1031 {
1032         if (!libevdev_has_event_type(dev, EV_ABS) ||
1033             !libevdev_has_event_code(dev, EV_ABS, code))
1034                 return NULL;
1035
1036         return &dev->abs_info[code];
1037 }
1038
1039 #define ABS_GETTER(name) \
1040 LIBEVDEV_EXPORT int libevdev_get_abs_##name(const struct libevdev *dev, unsigned int code) \
1041 { \
1042         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code); \
1043         return absinfo ? absinfo->name : 0; \
1044 }
1045
1046 ABS_GETTER(maximum);
1047 ABS_GETTER(minimum);
1048 ABS_GETTER(fuzz);
1049 ABS_GETTER(flat);
1050 ABS_GETTER(resolution);
1051
1052 #define ABS_SETTER(field) \
1053 LIBEVDEV_EXPORT void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
1054 { \
1055         if (!libevdev_has_event_code(dev, EV_ABS, code)) \
1056                 return; \
1057         dev->abs_info[code].field = val; \
1058 }
1059
1060 ABS_SETTER(maximum)
1061 ABS_SETTER(minimum)
1062 ABS_SETTER(fuzz)
1063 ABS_SETTER(flat)
1064 ABS_SETTER(resolution)
1065
1066 LIBEVDEV_EXPORT void
1067 libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1068 {
1069         if (!libevdev_has_event_code(dev, EV_ABS, code))
1070                 return;
1071
1072         dev->abs_info[code] = *abs;
1073 }
1074
1075 LIBEVDEV_EXPORT int
1076 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
1077 {
1078         if (type > EV_MAX)
1079                 return -1;
1080
1081         if (libevdev_has_event_type(dev, type))
1082                 return 0;
1083
1084         set_bit(dev->bits, type);
1085
1086         if (type == EV_REP) {
1087                 int delay = 0, period = 0;
1088                 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
1089                 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
1090         }
1091         return 0;
1092 }
1093
1094 LIBEVDEV_EXPORT int
1095 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
1096 {
1097         if (type > EV_MAX || type == EV_SYN)
1098                 return -1;
1099
1100         clear_bit(dev->bits, type);
1101
1102         return 0;
1103 }
1104
1105 LIBEVDEV_EXPORT int
1106 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
1107                            unsigned int code, const void *data)
1108 {
1109         unsigned int max;
1110         unsigned long *mask = NULL;
1111
1112         if (libevdev_enable_event_type(dev, type))
1113                 return -1;
1114
1115         switch(type) {
1116                 case EV_SYN:
1117                         return 0;
1118                 case EV_ABS:
1119                 case EV_REP:
1120                         if (data == NULL)
1121                                 return -1;
1122                         break;
1123                 default:
1124                         if (data != NULL)
1125                                 return -1;
1126                         break;
1127         }
1128
1129         max = type_to_mask(dev, type, &mask);
1130
1131         if (code > max)
1132                 return -1;
1133
1134         set_bit(mask, code);
1135
1136         if (type == EV_ABS) {
1137                 const struct input_absinfo *abs = data;
1138                 dev->abs_info[code] = *abs;
1139         } else if (type == EV_REP) {
1140                 const int *value = data;
1141                 dev->rep_values[code] = *value;
1142         }
1143
1144         return 0;
1145 }
1146
1147 LIBEVDEV_EXPORT int
1148 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
1149 {
1150         unsigned int max;
1151         unsigned long *mask = NULL;
1152
1153         if (type > EV_MAX)
1154                 return -1;
1155
1156         max = type_to_mask(dev, type, &mask);
1157
1158         if (code > max)
1159                 return -1;
1160
1161         clear_bit(mask, code);
1162
1163         return 0;
1164 }
1165
1166 LIBEVDEV_EXPORT int
1167 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1168 {
1169         return libevdev_kernel_set_abs_info(dev, code, abs);
1170 }
1171
1172 LIBEVDEV_EXPORT int
1173 libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1174 {
1175         int rc;
1176
1177         if (dev->fd < 0) {
1178                 log_bug("device not initialized. call libevdev_set_fd() first\n");
1179                 return -EBADF;
1180         }
1181
1182         if (code > ABS_MAX)
1183                 return -EINVAL;
1184
1185         rc = ioctl(dev->fd, EVIOCSABS(code), abs);
1186         if (rc < 0)
1187                 rc = -errno;
1188         else
1189                 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
1190
1191         return rc;
1192 }
1193
1194 LIBEVDEV_EXPORT int
1195 libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
1196 {
1197         int rc = 0;
1198
1199         if (dev->fd < 0) {
1200                 log_bug("device not initialized. call libevdev_set_fd() first\n");
1201                 return -EBADF;
1202         }
1203
1204         if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB) {
1205                 log_bug("invalid grab parameter %#x\n", grab);
1206                 return -EINVAL;
1207         }
1208
1209         if (grab == dev->grabbed)
1210                 return 0;
1211
1212         if (grab == LIBEVDEV_GRAB)
1213                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
1214         else if (grab == LIBEVDEV_UNGRAB)
1215                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
1216
1217         if (rc == 0)
1218                 dev->grabbed = grab;
1219
1220         return rc < 0 ? -errno : 0;
1221 }
1222
1223 LIBEVDEV_EXPORT int
1224 libevdev_is_event_type(const struct input_event *ev, unsigned int type)
1225 {
1226         return type < EV_CNT && ev->type == type;
1227 }
1228
1229 LIBEVDEV_EXPORT int
1230 libevdev_is_event_code(const struct input_event *ev, unsigned int type, unsigned int code)
1231 {
1232         int max;
1233
1234         if (!libevdev_is_event_type(ev, type))
1235                 return 0;
1236
1237         max = libevdev_get_event_type_max(type);
1238         return (max > -1 && code <= (unsigned int)max && ev->code == code);
1239 }
1240
1241 LIBEVDEV_EXPORT const char*
1242 libevdev_get_event_type_name(unsigned int type)
1243 {
1244         if (type > EV_MAX)
1245                 return NULL;
1246
1247         return ev_map[type];
1248 }
1249
1250 LIBEVDEV_EXPORT const char*
1251 libevdev_get_event_code_name(unsigned int type, unsigned int code)
1252 {
1253         int max = libevdev_get_event_type_max(type);
1254
1255         if (max == -1 || code > (unsigned int)max)
1256                 return NULL;
1257
1258         return event_type_map[type][code];
1259 }
1260
1261 LIBEVDEV_EXPORT const char*
1262 libevdev_get_property_name(unsigned int prop)
1263 {
1264         if (prop > INPUT_PROP_MAX)
1265                 return NULL;
1266
1267         return input_prop_map[prop];
1268 }
1269
1270 LIBEVDEV_EXPORT int
1271 libevdev_get_event_type_max(unsigned int type)
1272 {
1273         if (type > EV_MAX)
1274                 return -1;
1275
1276         return ev_max[type];
1277 }
1278
1279 LIBEVDEV_EXPORT int
1280 libevdev_get_repeat(struct libevdev *dev, int *delay, int *period)
1281 {
1282         if (!libevdev_has_event_type(dev, EV_REP))
1283                 return -1;
1284
1285         if (delay != NULL)
1286                 *delay = dev->rep_values[REP_DELAY];
1287         if (period != NULL)
1288                 *period = dev->rep_values[REP_PERIOD];
1289
1290         return 0;
1291 }
1292
1293 LIBEVDEV_EXPORT int
1294 libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value)
1295 {
1296         return libevdev_kernel_set_led_values(dev, code, value, -1);
1297 }
1298
1299 LIBEVDEV_EXPORT int
1300 libevdev_kernel_set_led_values(struct libevdev *dev, ...)
1301 {
1302         struct input_event ev[LED_MAX + 1];
1303         enum libevdev_led_value val;
1304         va_list args;
1305         int code;
1306         int rc = 0;
1307         size_t nleds = 0;
1308
1309         if (dev->fd < 0) {
1310                 log_bug("device not initialized. call libevdev_set_fd() first\n");
1311                 return -EBADF;
1312         }
1313
1314         memset(ev, 0, sizeof(ev));
1315
1316         va_start(args, dev);
1317         code = va_arg(args, unsigned int);
1318         while (code != -1) {
1319                 if (code > LED_MAX) {
1320                         rc = -EINVAL;
1321                         break;
1322                 }
1323                 val = va_arg(args, enum libevdev_led_value);
1324                 if (val != LIBEVDEV_LED_ON && val != LIBEVDEV_LED_OFF) {
1325                         rc = -EINVAL;
1326                         break;
1327                 }
1328
1329                 if (libevdev_has_event_code(dev, EV_LED, code)) {
1330                         struct input_event *e = ev;
1331
1332                         while (e->type > 0 && e->code != code)
1333                                 e++;
1334
1335                         if (e->type == 0)
1336                                 nleds++;
1337                         e->type = EV_LED;
1338                         e->code = code;
1339                         e->value = (val == LIBEVDEV_LED_ON);
1340                 }
1341                 code = va_arg(args, unsigned int);
1342         }
1343         va_end(args);
1344
1345         if (rc == 0 && nleds > 0) {
1346                 ev[nleds].type = EV_SYN;
1347                 ev[nleds++].code = SYN_REPORT;
1348
1349                 rc = write(libevdev_get_fd(dev), ev, nleds * sizeof(ev[0]));
1350                 if (rc > 0) {
1351                         nleds--; /* last is EV_SYN */
1352                         while (nleds--)
1353                                 update_led_state(dev, &ev[nleds]);
1354                 }
1355                 rc = (rc != -1) ? 0 : -errno;
1356         }
1357
1358         return rc;
1359 }