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