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