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