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