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