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