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