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