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