Constify libevdev_get_repeat
[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                 default:
971                         value = 0;
972                         break;
973         }
974
975         return value;
976 }
977
978 LIBEVDEV_EXPORT int
979 libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value)
980 {
981         int rc = 0;
982         struct input_event e;
983
984         if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
985                 return -1;
986
987         e.type = type;
988         e.code = code;
989         e.value = value;
990
991         switch(type) {
992                 case EV_ABS: rc = update_abs_state(dev, &e); break;
993                 case EV_KEY: rc = update_key_state(dev, &e); break;
994                 case EV_LED: rc = update_led_state(dev, &e); break;
995                 case EV_SW: rc = update_sw_state(dev, &e); break;
996                 default:
997                              rc = -1;
998                              break;
999         }
1000
1001         return rc;
1002 }
1003
1004 LIBEVDEV_EXPORT int
1005 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
1006 {
1007         if (libevdev_has_event_type(dev, type) &&
1008             libevdev_has_event_code(dev, type, code)) {
1009                 *value = libevdev_get_event_value(dev, type, code);
1010                 return 1;
1011         } else
1012                 return 0;
1013 }
1014
1015 LIBEVDEV_EXPORT int
1016 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
1017 {
1018         if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1019                 return 0;
1020
1021         if (dev->num_slots < 0 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
1022                 return 0;
1023
1024         if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1025                 return 0;
1026
1027         return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
1028 }
1029
1030 LIBEVDEV_EXPORT int
1031 libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value)
1032 {
1033         if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1034                 return -1;
1035
1036         if (dev->num_slots == -1 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
1037                 return -1;
1038
1039         if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1040                 return -1;
1041
1042         if (code == ABS_MT_SLOT) {
1043                 if (value < 0 || value >= libevdev_get_num_slots(dev))
1044                         return -1;
1045                 dev->current_slot = value;
1046         }
1047
1048         dev->mt_slot_vals[slot][code - ABS_MT_MIN] = value;
1049
1050
1051         return 0;
1052 }
1053
1054 LIBEVDEV_EXPORT int
1055 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
1056 {
1057         if (libevdev_has_event_type(dev, EV_ABS) &&
1058             libevdev_has_event_code(dev, EV_ABS, code) &&
1059             dev->num_slots >= 0 &&
1060             slot < (unsigned int)dev->num_slots && slot < MAX_SLOTS) {
1061                 *value = libevdev_get_slot_value(dev, slot, code);
1062                 return 1;
1063         } else
1064                 return 0;
1065 }
1066
1067 LIBEVDEV_EXPORT int
1068 libevdev_get_num_slots(const struct libevdev *dev)
1069 {
1070         return dev->num_slots;
1071 }
1072
1073 LIBEVDEV_EXPORT int
1074 libevdev_get_current_slot(const struct libevdev *dev)
1075 {
1076         return dev->current_slot;
1077 }
1078
1079 LIBEVDEV_EXPORT const struct input_absinfo*
1080 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
1081 {
1082         if (!libevdev_has_event_type(dev, EV_ABS) ||
1083             !libevdev_has_event_code(dev, EV_ABS, code))
1084                 return NULL;
1085
1086         return &dev->abs_info[code];
1087 }
1088
1089 #define ABS_GETTER(name) \
1090 LIBEVDEV_EXPORT int libevdev_get_abs_##name(const struct libevdev *dev, unsigned int code) \
1091 { \
1092         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code); \
1093         return absinfo ? absinfo->name : 0; \
1094 }
1095
1096 ABS_GETTER(maximum)
1097 ABS_GETTER(minimum)
1098 ABS_GETTER(fuzz)
1099 ABS_GETTER(flat)
1100 ABS_GETTER(resolution)
1101
1102 #define ABS_SETTER(field) \
1103 LIBEVDEV_EXPORT void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
1104 { \
1105         if (!libevdev_has_event_code(dev, EV_ABS, code)) \
1106                 return; \
1107         dev->abs_info[code].field = val; \
1108 }
1109
1110 ABS_SETTER(maximum)
1111 ABS_SETTER(minimum)
1112 ABS_SETTER(fuzz)
1113 ABS_SETTER(flat)
1114 ABS_SETTER(resolution)
1115
1116 LIBEVDEV_EXPORT void
1117 libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1118 {
1119         if (!libevdev_has_event_code(dev, EV_ABS, code))
1120                 return;
1121
1122         dev->abs_info[code] = *abs;
1123 }
1124
1125 LIBEVDEV_EXPORT int
1126 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
1127 {
1128         int max;
1129
1130         if (type > EV_MAX)
1131                 return -1;
1132
1133         if (libevdev_has_event_type(dev, type))
1134                 return 0;
1135
1136         max = libevdev_event_type_get_max(type);
1137         if (max == -1)
1138                 return -1;
1139
1140         set_bit(dev->bits, type);
1141
1142         if (type == EV_REP) {
1143                 int delay = 0, period = 0;
1144                 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
1145                 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
1146         }
1147         return 0;
1148 }
1149
1150 LIBEVDEV_EXPORT int
1151 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
1152 {
1153         int max;
1154
1155         if (type > EV_MAX || type == EV_SYN)
1156                 return -1;
1157
1158         max = libevdev_event_type_get_max(type);
1159         if (max == -1)
1160                 return -1;
1161
1162         clear_bit(dev->bits, type);
1163
1164         return 0;
1165 }
1166
1167 LIBEVDEV_EXPORT int
1168 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
1169                            unsigned int code, const void *data)
1170 {
1171         unsigned int max;
1172         unsigned long *mask = NULL;
1173
1174         if (libevdev_enable_event_type(dev, type))
1175                 return -1;
1176
1177         switch(type) {
1178                 case EV_SYN:
1179                         return 0;
1180                 case EV_ABS:
1181                 case EV_REP:
1182                         if (data == NULL)
1183                                 return -1;
1184                         break;
1185                 default:
1186                         if (data != NULL)
1187                                 return -1;
1188                         break;
1189         }
1190
1191         max = type_to_mask(dev, type, &mask);
1192
1193         if (code > max || (int)max == -1)
1194                 return -1;
1195
1196         set_bit(mask, code);
1197
1198         if (type == EV_ABS) {
1199                 const struct input_absinfo *abs = data;
1200                 dev->abs_info[code] = *abs;
1201         } else if (type == EV_REP) {
1202                 const int *value = data;
1203                 dev->rep_values[code] = *value;
1204         }
1205
1206         return 0;
1207 }
1208
1209 LIBEVDEV_EXPORT int
1210 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
1211 {
1212         unsigned int max;
1213         unsigned long *mask = NULL;
1214
1215         if (type > EV_MAX || type == EV_SYN)
1216                 return -1;
1217
1218         max = type_to_mask(dev, type, &mask);
1219
1220         if (code > max || (int)max == -1)
1221                 return -1;
1222
1223         clear_bit(mask, code);
1224
1225         return 0;
1226 }
1227
1228 LIBEVDEV_EXPORT int
1229 libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1230 {
1231         int rc;
1232
1233         if (!dev->initialized) {
1234                 log_bug("device not initialized. call libevdev_set_fd() first\n");
1235                 return -EBADF;
1236         } else if (dev->fd < 0)
1237                 return -EBADF;
1238
1239         if (code > ABS_MAX)
1240                 return -EINVAL;
1241
1242         rc = ioctl(dev->fd, EVIOCSABS(code), abs);
1243         if (rc < 0)
1244                 rc = -errno;
1245         else
1246                 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
1247
1248         return rc;
1249 }
1250
1251 LIBEVDEV_EXPORT int
1252 libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
1253 {
1254         int rc = 0;
1255
1256         if (!dev->initialized) {
1257                 log_bug("device not initialized. call libevdev_set_fd() first\n");
1258                 return -EBADF;
1259         } else if (dev->fd < 0)
1260                 return -EBADF;
1261
1262         if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB) {
1263                 log_bug("invalid grab parameter %#x\n", grab);
1264                 return -EINVAL;
1265         }
1266
1267         if (grab == dev->grabbed)
1268                 return 0;
1269
1270         if (grab == LIBEVDEV_GRAB)
1271                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
1272         else if (grab == LIBEVDEV_UNGRAB)
1273                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
1274
1275         if (rc == 0)
1276                 dev->grabbed = grab;
1277
1278         return rc < 0 ? -errno : 0;
1279 }
1280
1281 LIBEVDEV_EXPORT int
1282 libevdev_event_is_type(const struct input_event *ev, unsigned int type)
1283 {
1284         return type < EV_CNT && ev->type == type;
1285 }
1286
1287 LIBEVDEV_EXPORT int
1288 libevdev_event_is_code(const struct input_event *ev, unsigned int type, unsigned int code)
1289 {
1290         int max;
1291
1292         if (!libevdev_event_is_type(ev, type))
1293                 return 0;
1294
1295         max = libevdev_event_type_get_max(type);
1296         return (max > -1 && code <= (unsigned int)max && ev->code == code);
1297 }
1298
1299 LIBEVDEV_EXPORT const char*
1300 libevdev_event_type_get_name(unsigned int type)
1301 {
1302         if (type > EV_MAX)
1303                 return NULL;
1304
1305         return ev_map[type];
1306 }
1307
1308 LIBEVDEV_EXPORT const char*
1309 libevdev_event_code_get_name(unsigned int type, unsigned int code)
1310 {
1311         int max = libevdev_event_type_get_max(type);
1312
1313         if (max == -1 || code > (unsigned int)max)
1314                 return NULL;
1315
1316         return event_type_map[type][code];
1317 }
1318
1319 LIBEVDEV_EXPORT const char*
1320 libevdev_property_get_name(unsigned int prop)
1321 {
1322         if (prop > INPUT_PROP_MAX)
1323                 return NULL;
1324
1325         return input_prop_map[prop];
1326 }
1327
1328 LIBEVDEV_EXPORT int
1329 libevdev_event_type_get_max(unsigned int type)
1330 {
1331         if (type > EV_MAX)
1332                 return -1;
1333
1334         return ev_max[type];
1335 }
1336
1337 LIBEVDEV_EXPORT int
1338 libevdev_get_repeat(const struct libevdev *dev, int *delay, int *period)
1339 {
1340         if (!libevdev_has_event_type(dev, EV_REP))
1341                 return -1;
1342
1343         if (delay != NULL)
1344                 *delay = dev->rep_values[REP_DELAY];
1345         if (period != NULL)
1346                 *period = dev->rep_values[REP_PERIOD];
1347
1348         return 0;
1349 }
1350
1351 LIBEVDEV_EXPORT int
1352 libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value)
1353 {
1354         return libevdev_kernel_set_led_values(dev, code, value, -1);
1355 }
1356
1357 LIBEVDEV_EXPORT int
1358 libevdev_kernel_set_led_values(struct libevdev *dev, ...)
1359 {
1360         struct input_event ev[LED_MAX + 1];
1361         enum libevdev_led_value val;
1362         va_list args;
1363         int code;
1364         int rc = 0;
1365         size_t nleds = 0;
1366
1367         if (!dev->initialized) {
1368                 log_bug("device not initialized. call libevdev_set_fd() first\n");
1369                 return -EBADF;
1370         } else if (dev->fd < 0)
1371                 return -EBADF;
1372
1373         memset(ev, 0, sizeof(ev));
1374
1375         va_start(args, dev);
1376         code = va_arg(args, unsigned int);
1377         while (code != -1) {
1378                 if (code > LED_MAX) {
1379                         rc = -EINVAL;
1380                         break;
1381                 }
1382                 val = va_arg(args, enum libevdev_led_value);
1383                 if (val != LIBEVDEV_LED_ON && val != LIBEVDEV_LED_OFF) {
1384                         rc = -EINVAL;
1385                         break;
1386                 }
1387
1388                 if (libevdev_has_event_code(dev, EV_LED, code)) {
1389                         struct input_event *e = ev;
1390
1391                         while (e->type > 0 && e->code != code)
1392                                 e++;
1393
1394                         if (e->type == 0)
1395                                 nleds++;
1396                         e->type = EV_LED;
1397                         e->code = code;
1398                         e->value = (val == LIBEVDEV_LED_ON);
1399                 }
1400                 code = va_arg(args, unsigned int);
1401         }
1402         va_end(args);
1403
1404         if (rc == 0 && nleds > 0) {
1405                 ev[nleds].type = EV_SYN;
1406                 ev[nleds++].code = SYN_REPORT;
1407
1408                 rc = write(libevdev_get_fd(dev), ev, nleds * sizeof(ev[0]));
1409                 if (rc > 0) {
1410                         nleds--; /* last is EV_SYN */
1411                         while (nleds--)
1412                                 update_led_state(dev, &ev[nleds]);
1413                 }
1414                 rc = (rc != -1) ? 0 : -errno;
1415         }
1416
1417         return rc;
1418 }
1419
1420 LIBEVDEV_EXPORT int
1421 libevdev_set_clock_id(struct libevdev *dev, int clockid)
1422 {
1423         if (!dev->initialized) {
1424                 log_bug("device not initialized. call libevdev_set_fd() first\n");
1425                 return -EBADF;
1426         } else if (dev->fd < 0)
1427                 return -EBADF;
1428
1429         return ioctl(dev->fd, EVIOCSCLOCKID, &clockid) ? -errno : 0;
1430 }