Don't sync mt axes that don't exist
[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 <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <stdarg.h>
29 #include <linux/uinput.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
39 init_event_queue(struct libevdev *dev)
40 {
41         /* FIXME: count the number of axes, keys, etc. to get a better idea at how many events per
42            EV_SYN we could possibly get. Then multiply that by the actual buffer size we care about */
43
44         const int QUEUE_SIZE = 256;
45
46         return queue_alloc(dev, QUEUE_SIZE);
47 }
48
49 static void
50 _libevdev_log(struct libevdev *dev, const char *format, ...)
51 {
52         va_list args;
53
54         va_start(args, format);
55         dev->log(format, args);
56         va_end(args);
57 }
58
59 static void
60 libevdev_noop_log_func(const char *format, va_list args)
61 {
62 }
63
64 struct libevdev*
65 libevdev_new(void)
66 {
67         struct libevdev *dev;
68
69         dev = calloc(1, sizeof(*dev));
70         if (!dev)
71                 return NULL;
72         dev->fd = -1;
73         dev->num_slots = -1;
74         dev->current_slot = -1;
75         dev->log = libevdev_noop_log_func;
76         dev->grabbed = LIBEVDEV_UNGRAB;
77
78         return dev;
79 }
80
81 int
82 libevdev_new_from_fd(int fd, struct libevdev **dev)
83 {
84         struct libevdev *d;
85         int rc;
86
87         d = libevdev_new();
88         if (!d)
89                 return -ENOSPC;
90
91         rc = libevdev_set_fd(d, fd);
92         if (rc < 0)
93                 libevdev_free(d);
94         else
95                 *dev = d;
96         return rc;
97 }
98
99 void
100 libevdev_free(struct libevdev *dev)
101 {
102         if (!dev)
103                 return;
104
105         free(dev->name);
106         free(dev->phys);
107         free(dev->uniq);
108         queue_free(dev);
109         free(dev);
110 }
111
112 void
113 libevdev_set_log_handler(struct libevdev *dev, libevdev_log_func_t logfunc)
114 {
115         if (dev == NULL)
116                 return;
117
118         dev->log = logfunc ? logfunc : libevdev_noop_log_func;
119 }
120
121 int
122 libevdev_change_fd(struct libevdev *dev, int fd)
123 {
124         if (dev->fd == -1)
125                 return -1;
126         dev->fd = fd;
127         return 0;
128 }
129
130 int
131 libevdev_set_fd(struct libevdev* dev, int fd)
132 {
133         int rc;
134         int i;
135         char buf[256];
136
137         if (dev->fd != -1)
138                 return -EBADF;
139
140         rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
141         if (rc < 0)
142                 goto out;
143
144         memset(buf, 0, sizeof(buf));
145         rc = ioctl(fd, EVIOCGNAME(sizeof(buf) - 1), buf);
146         if (rc < 0)
147                 goto out;
148
149         dev->name = strdup(buf);
150         if (!dev->name) {
151                 errno = ENOSPC;
152                 goto out;
153         }
154
155         memset(buf, 0, sizeof(buf));
156         rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
157         if (rc < 0) {
158                 /* uinput has no phys */
159                 if (errno != ENOENT)
160                         goto out;
161         } else {
162                 dev->phys = strdup(buf);
163                 if (!dev->phys) {
164                         errno = ENOSPC;
165                         goto out;
166                 }
167         }
168
169         memset(buf, 0, sizeof(buf));
170         rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
171         if (rc < 0) {
172                 if (errno != ENOENT)
173                         goto out;
174         } else  {
175                 dev->uniq = strdup(buf);
176                 if (!dev->uniq) {
177                         errno = ENOSPC;
178                         goto out;
179                 }
180         }
181
182         rc = ioctl(fd, EVIOCGID, &dev->ids);
183         if (rc < 0)
184                 goto out;
185
186         rc = ioctl(fd, EVIOCGVERSION, &dev->driver_version);
187         if (rc < 0)
188                 goto out;
189
190         rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
191         if (rc < 0)
192                 goto out;
193
194         rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
195         if (rc < 0)
196                 goto out;
197
198         rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
199         if (rc < 0)
200                 goto out;
201
202         rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
203         if (rc < 0)
204                 goto out;
205
206         rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
207         if (rc < 0)
208                 goto out;
209
210         rc = ioctl(fd, EVIOCGBIT(EV_SW, sizeof(dev->sw_bits)), dev->sw_bits);
211         if (rc < 0)
212                 goto out;
213
214         rc = ioctl(fd, EVIOCGBIT(EV_MSC, sizeof(dev->msc_bits)), dev->msc_bits);
215         if (rc < 0)
216                 goto out;
217
218         rc = ioctl(fd, EVIOCGBIT(EV_FF, sizeof(dev->ff_bits)), dev->ff_bits);
219         if (rc < 0)
220                 goto out;
221
222         rc = ioctl(fd, EVIOCGBIT(EV_SND, sizeof(dev->snd_bits)), dev->snd_bits);
223         if (rc < 0)
224                 goto out;
225
226         /* rep is a special case, always set it to 1 for both values if EV_REP is set */
227         if (bit_is_set(dev->bits, EV_REP)) {
228                 for (i = 0; i < REP_MAX; i++)
229                         set_bit(dev->rep_bits, i);
230                 rc = ioctl(fd, EVIOCGREP, dev->rep_values);
231                 if (rc < 0)
232                         goto out;
233         }
234
235         for (i = ABS_X; i <= ABS_MAX; i++) {
236                 if (bit_is_set(dev->abs_bits, i)) {
237                         struct input_absinfo abs_info;
238                         rc = ioctl(fd, EVIOCGABS(i), &abs_info);
239                         if (rc < 0)
240                                 goto out;
241
242                         dev->abs_info[i] = abs_info;
243                         if (i == ABS_MT_SLOT) {
244                                 dev->num_slots = abs_info.maximum + 1;
245                                 dev->current_slot = abs_info.value;
246                         }
247
248                 }
249         }
250
251         rc = init_event_queue(dev);
252         if (rc < 0)
253                 return -rc;
254
255         /* not copying key state because we won't know when we'll start to
256          * use this fd and key's are likely to change state by then.
257          * Same with the valuators, really, but they may not change.
258          */
259
260         dev->fd = fd;
261
262 out:
263         return rc ? -errno : 0;
264 }
265
266 int
267 libevdev_get_fd(const struct libevdev* dev)
268 {
269         return dev->fd;
270 }
271
272 static inline void
273 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
274 {
275         ev->time = dev->last_event_time;
276         ev->type = type;
277         ev->code = code;
278         ev->value = value;
279 }
280
281 static int
282 sync_key_state(struct libevdev *dev)
283 {
284         int rc;
285         int i;
286         unsigned long keystate[NLONGS(KEY_MAX)];
287
288         rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
289         if (rc < 0)
290                 goto out;
291
292         for (i = 0; i < KEY_MAX; i++) {
293                 int old, new;
294                 old = bit_is_set(dev->key_values, i);
295                 new = bit_is_set(keystate, i);
296                 if (old ^ new) {
297                         struct input_event *ev = queue_push(dev);
298                         init_event(dev, ev, EV_KEY, i, new ? 1 : 0);
299                 }
300                 set_bit_state(dev->key_values, i, new);
301         }
302
303         rc = 0;
304 out:
305         return rc ? -errno : 0;
306 }
307
308 static int
309 sync_abs_state(struct libevdev *dev)
310 {
311         int rc;
312         int i;
313
314         for (i = ABS_X; i <= ABS_MAX; i++) {
315                 struct input_absinfo abs_info;
316
317                 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
318                         continue;
319
320                 if (!bit_is_set(dev->abs_bits, i))
321                         continue;
322
323                 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
324                 if (rc < 0)
325                         goto out;
326
327                 if (dev->abs_info[i].value != abs_info.value) {
328                         struct input_event *ev = queue_push(dev);
329
330                         init_event(dev, ev, EV_ABS, i, abs_info.value);
331                         dev->abs_info[i].value = abs_info.value;
332                 }
333         }
334
335         rc = 0;
336 out:
337         return rc ? -errno : 0;
338 }
339
340 static int
341 sync_mt_state(struct libevdev *dev)
342 {
343         int rc;
344         int i;
345         struct mt_state {
346                 int code;
347                 int val[MAX_SLOTS];
348         } mt_state[ABS_MT_CNT];
349
350         for (i = ABS_MT_MIN; i < ABS_MT_MAX; i++) {
351                 int idx;
352                 if (i == ABS_MT_SLOT)
353                         continue;
354
355                 if (!libevdev_has_event_code(dev, EV_ABS, i))
356                         continue;
357
358                 idx = i - ABS_MT_MIN;
359                 mt_state[idx].code = i;
360                 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
361                 if (rc < 0)
362                         goto out;
363         }
364
365         for (i = 0; i < dev->num_slots; i++) {
366                 int j;
367                 struct input_event *ev;
368
369                 ev = queue_push(dev);
370                 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, i);
371                 for (j = ABS_MT_MIN; j < ABS_MT_MAX; j++) {
372                         int jdx = j - ABS_MT_MIN;
373
374                         if (j == ABS_MT_SLOT)
375                                 continue;
376
377                         if (!libevdev_has_event_code(dev, EV_ABS, j))
378                                 continue;
379
380                         if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
381                                 continue;
382
383                         ev = queue_push(dev);
384                         init_event(dev, ev, EV_ABS, j, mt_state[jdx].val[i]);
385                         dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
386                 }
387         }
388
389         rc = 0;
390 out:
391         return rc ? -errno : 0;
392 }
393
394 static int
395 sync_state(struct libevdev *dev)
396 {
397         int i;
398         int rc = 0;
399         struct input_event *ev;
400
401         /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
402            queue[0]) we need to shift this backwards. Except that chances are that the
403            queue may be either full or too full to prepend all the events needed for
404            syncing.
405
406            so we search for the last sync event in the queue and drop everything before
407            including that event and rely on the kernel to tell us the right value for that
408            bitfield during the sync process.
409          */
410
411         for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
412                 struct input_event e;
413                 queue_peek(dev, i, &e);
414                 if (e.type == EV_SYN)
415                         break;
416         }
417
418         if (i > 0)
419                 queue_shift_multiple(dev, i + 1, NULL);
420
421         if (libevdev_has_event_type(dev, EV_KEY))
422                 rc = sync_key_state(dev);
423         if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
424                 rc = sync_abs_state(dev);
425         if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
426                 rc = sync_mt_state(dev);
427
428         ev = queue_push(dev);
429         init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
430
431         dev->queue_nsync = queue_num_elements(dev);
432         dev->need_sync = 0;
433
434         return rc;
435 }
436
437 static int
438 update_key_state(struct libevdev *dev, const struct input_event *e)
439 {
440         if (!libevdev_has_event_type(dev, EV_KEY))
441                 return 1;
442
443         if (e->code > KEY_MAX)
444                 return 1;
445
446         if (e->value == 0)
447                 clear_bit(dev->key_values, e->code);
448         else
449                 set_bit(dev->key_values, e->code);
450
451         return 0;
452 }
453
454 static int
455 update_mt_state(struct libevdev *dev, const struct input_event *e)
456 {
457         if (e->code == ABS_MT_SLOT) {
458                 dev->current_slot = e->value;
459                 return 0;
460         } else if (dev->current_slot == -1)
461                 return 1;
462
463         dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
464
465         return 0;
466 }
467
468 static int
469 update_abs_state(struct libevdev *dev, const struct input_event *e)
470 {
471         if (!libevdev_has_event_type(dev, EV_ABS))
472                 return 1;
473
474         if (e->code > ABS_MAX)
475                 return 1;
476
477         if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
478                 return update_mt_state(dev, e);
479
480         dev->abs_info[e->code].value = e->value;
481
482         return 0;
483 }
484
485 static int
486 update_state(struct libevdev *dev, const struct input_event *e)
487 {
488         int rc = 0;
489
490         switch(e->type) {
491                 case EV_SYN:
492                 case EV_REL:
493                         break;
494                 case EV_KEY:
495                         rc = update_key_state(dev, e);
496                         break;
497                 case EV_ABS:
498                         rc = update_abs_state(dev, e);
499                         break;
500         }
501
502         dev->last_event_time = e->time;
503
504         return rc;
505 }
506
507 static int
508 read_more_events(struct libevdev *dev)
509 {
510         int free_elem;
511         int len;
512         struct input_event *next;
513
514         free_elem = queue_num_free_elements(dev);
515         if (free_elem <= 0)
516                 return 0;
517
518         next = queue_next_element(dev);
519         len = read(dev->fd, next, free_elem * sizeof(struct input_event));
520         if (len < 0) {
521                 return -errno;
522         } else if (len > 0 && len % sizeof(struct input_event) != 0)
523                 return -EINVAL;
524         else if (len > 0) {
525                 int nev = len/sizeof(struct input_event);
526                 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
527         }
528
529         return 0;
530 }
531
532 int libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
533 {
534         int rc = 0;
535
536         if (dev->fd < 0)
537                 return -ENODEV;
538
539         if (!(flags & (LIBEVDEV_READ_NORMAL|LIBEVDEV_READ_SYNC|LIBEVDEV_FORCE_SYNC)))
540                 return -EINVAL;
541
542         if (flags & LIBEVDEV_READ_SYNC) {
543                 if (!dev->need_sync && dev->queue_nsync == 0)
544                         return -EAGAIN;
545                 else if (dev->need_sync) {
546                         rc = sync_state(dev);
547                         if (rc != 0)
548                                 return rc;
549                 }
550         } else if (dev->need_sync) {
551                 /* FIXME: still need to call update_state for all events
552                  * here, otherwise the library has the wrong view of the
553                  * device too */
554                 queue_shift_multiple(dev, dev->queue_nsync, NULL);
555         }
556
557         /* FIXME: check for O_NONBLOCK and if not set, skip if we have an
558          * event in the queue from the previous read.
559          */
560
561         /* FIXME: if the first event after syncing is a SYN_DROPPED, log this */
562
563         /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
564            worst case we don't read fast enough and end up with SYN_DROPPED anyway */
565         do {
566                 rc = read_more_events(dev);
567                 if (rc < 0 && rc != -EAGAIN)
568                         goto out;
569
570                 if (flags & LIBEVDEV_FORCE_SYNC) {
571                         dev->need_sync = 1;
572                         rc = 1;
573                         goto out;
574                 }
575
576
577                 if (queue_shift(dev, ev) != 0)
578                         return -EAGAIN;
579
580                 update_state(dev, ev);
581
582         /* if we disabled a code, get the next event instead */
583         } while(!libevdev_has_event_code(dev, ev->type, ev->code));
584
585         rc = 0;
586         if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
587                 dev->need_sync = 1;
588                 rc = 1;
589         }
590
591         if (flags & LIBEVDEV_READ_SYNC && dev->queue_nsync > 0) {
592                 dev->queue_nsync--;
593                 rc = 1;
594         }
595
596 out:
597         return rc;
598 }
599
600 const char *
601 libevdev_get_name(const struct libevdev *dev)
602 {
603         return dev->name ? dev->name : "";
604 }
605
606 const char *
607 libevdev_get_phys(const struct libevdev *dev)
608 {
609         return dev->phys;
610 }
611
612 const char *
613 libevdev_get_uniq(const struct libevdev *dev)
614 {
615         return dev->uniq;
616 }
617
618 int libevdev_get_product_id(const struct libevdev *dev)
619 {
620         return dev->ids.product;
621 }
622
623 int libevdev_get_vendor_id(const struct libevdev *dev)
624 {
625         return dev->ids.vendor;
626 }
627
628 int libevdev_get_bustype(const struct libevdev *dev)
629 {
630         return dev->ids.bustype;
631 }
632
633 int libevdev_get_version(const struct libevdev *dev)
634 {
635         return dev->ids.version;
636 }
637
638 int libevdev_get_driver_version(const struct libevdev *dev)
639 {
640         return dev->driver_version;
641 }
642
643 int
644 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
645 {
646         return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
647 }
648
649 int
650 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
651 {
652         return (type <= EV_MAX) && bit_is_set(dev->bits, type);
653 }
654
655 int
656 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
657 {
658         const unsigned long *mask;
659         int max;
660
661         if (!libevdev_has_event_type(dev, type))
662                 return 0;
663
664         if (type == EV_SYN)
665                 return 1;
666
667         max = type_to_mask_const(dev, type, &mask);
668
669         if (max == -1 || code > max)
670                 return 0;
671
672         return bit_is_set(mask, code);
673 }
674
675 int
676 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
677 {
678         int value;
679
680         if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
681                 return 0;
682
683         switch (type) {
684                 case EV_ABS: value = dev->abs_info[code].value; break;
685                 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
686                 default:
687                         value = 0;
688                         break;
689         }
690
691         return value;
692 }
693
694 int
695 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
696 {
697         if (libevdev_has_event_type(dev, type) &&
698             libevdev_has_event_code(dev, type, code)) {
699                 *value = libevdev_get_event_value(dev, type, code);
700                 return 1;
701         } else
702                 return 0;
703 }
704
705 int
706 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
707 {
708         if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
709                 return 0;
710
711         if (slot >= dev->num_slots || slot >= MAX_SLOTS)
712                 return 0;
713
714         if (code > ABS_MT_MAX || code < ABS_MT_MIN)
715                 return 0;
716
717         return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
718 }
719
720 int
721 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
722 {
723         if (libevdev_has_event_type(dev, EV_ABS) &&
724             libevdev_has_event_code(dev, EV_ABS, code) &&
725             slot < dev->num_slots && slot < MAX_SLOTS) {
726                 *value = libevdev_get_slot_value(dev, slot, code);
727                 return 1;
728         } else
729                 return 0;
730 }
731
732 int
733 libevdev_get_num_slots(const struct libevdev *dev)
734 {
735         return dev->num_slots;
736 }
737
738 int
739 libevdev_get_current_slot(const struct libevdev *dev)
740 {
741         return dev->current_slot;
742 }
743
744 const struct input_absinfo*
745 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
746 {
747         if (!libevdev_has_event_type(dev, EV_ABS) ||
748             !libevdev_has_event_code(dev, EV_ABS, code))
749                 return NULL;
750
751         return &dev->abs_info[code];
752 }
753
754 int
755 libevdev_get_abs_min(const struct libevdev *dev, unsigned int code)
756 {
757         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
758
759         return absinfo ? absinfo->minimum : 0;
760 }
761
762 int
763 libevdev_get_abs_max(const struct libevdev *dev, unsigned int code)
764 {
765         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
766
767         return absinfo ? absinfo->maximum : 0;
768 }
769
770 int
771 libevdev_get_abs_fuzz(const struct libevdev *dev, unsigned int code)
772 {
773         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
774
775         return absinfo ? absinfo->fuzz : 0;
776 }
777
778 int
779 libevdev_get_abs_flat(const struct libevdev *dev, unsigned int code)
780 {
781         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
782
783         return absinfo ? absinfo->flat : 0;
784 }
785
786 int
787 libevdev_get_abs_resolution(const struct libevdev *dev, unsigned int code)
788 {
789         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
790
791         return absinfo ? absinfo->resolution : 0;
792 }
793
794 int
795 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
796 {
797         if (type > EV_MAX)
798                 return -1;
799
800         set_bit(dev->bits, type);
801
802         return 0;
803 }
804
805 int
806 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
807 {
808         if (type > EV_MAX || type == EV_SYN)
809                 return -1;
810
811         clear_bit(dev->bits, type);
812
813         return 0;
814 }
815
816 int
817 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
818                            unsigned int code, const void *data)
819 {
820         unsigned int max;
821         unsigned long *mask;
822
823         if (libevdev_enable_event_type(dev, type))
824                 return -1;
825
826         if (type != EV_ABS && data != NULL)
827                 return -1;
828
829         max = type_to_mask(dev, type, &mask);
830
831         if (code > max)
832                 return -1;
833
834         set_bit(mask, code);
835
836         if (type == EV_ABS) {
837                 const struct input_absinfo *abs = data;
838                 dev->abs_info[code] = *abs;
839         }
840
841         return 0;
842 }
843
844 int
845 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
846 {
847         unsigned int max;
848         unsigned long *mask;
849
850         if (type > EV_MAX)
851                 return -1;
852
853         max = type_to_mask(dev, type, &mask);
854
855         if (code > max)
856                 return -1;
857
858         clear_bit(mask, code);
859
860         return 0;
861 }
862
863 int
864 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
865 {
866         int rc;
867
868         if (code > ABS_MAX)
869                 return -EINVAL;
870
871         rc = ioctl(dev->fd, EVIOCSABS(code), abs);
872         if (rc < 0)
873                 rc = -errno;
874         else
875                 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
876
877         return rc;
878 }
879
880 int
881 libevdev_grab(struct libevdev *dev, int grab)
882 {
883         int rc = 0;
884
885         if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB)
886                 return -EINVAL;
887
888         if (grab == dev->grabbed)
889                 return 0;
890
891         if (grab == LIBEVDEV_GRAB)
892                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
893         else if (grab == LIBEVDEV_UNGRAB)
894                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
895
896         if (rc == 0)
897                 dev->grabbed = grab;
898
899         return rc < 0 ? -errno : 0;
900 }
901
902 const char*
903 libevdev_get_event_type_name(unsigned int type)
904 {
905         if (type > EV_MAX)
906                 return NULL;
907
908         return ev_map[type];
909 }
910
911 const char*
912 libevdev_get_event_code_name(unsigned int type, unsigned int code)
913 {
914         if (type > EV_MAX)
915                 return NULL;
916
917         if (code > ev_max[type])
918                 return NULL;
919
920         return event_type_map[type][code];
921 }
922
923 const char*
924 libevdev_get_input_prop_name(unsigned int prop)
925 {
926         if (prop > INPUT_PROP_MAX)
927                 return NULL;
928
929         return input_prop_map[prop];
930 }
931
932 int
933 libevdev_get_event_type_max(unsigned int type)
934 {
935         if (type > EV_MAX)
936                 return -1;
937
938         return ev_max[type];
939 }
940
941 int
942 libevdev_get_repeat(struct libevdev *dev, int *delay, int *period)
943 {
944         if (!libevdev_has_event_type(dev, EV_REP))
945                 return -1;
946
947         if (delay != NULL)
948                 *delay = dev->rep_values[REP_DELAY];
949         if (period != NULL)
950                 *period = dev->rep_values[REP_PERIOD];
951
952         return 0;
953 }