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