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