Require a flag for normal mode too
[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                 idx = i - ABS_MT_MIN;
356                 mt_state[idx].code = i;
357                 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
358                 if (rc < 0)
359                         goto out;
360         }
361
362         for (i = 0; i < dev->num_slots; i++) {
363                 int j;
364                 struct input_event *ev;
365
366                 ev = queue_push(dev);
367                 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, i);
368                 for (j = ABS_MT_MIN; j < ABS_MT_MAX; j++) {
369                         int jdx = j - ABS_MT_MIN;
370
371                         if (j == ABS_MT_SLOT)
372                                 continue;
373
374                         if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
375                                 continue;
376
377                         ev = queue_push(dev);
378                         init_event(dev, ev, EV_ABS, j, mt_state[jdx].val[i]);
379                         dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
380                 }
381         }
382
383         rc = 0;
384 out:
385         return rc ? -errno : 0;
386 }
387
388 static int
389 sync_state(struct libevdev *dev)
390 {
391         int i;
392         int rc = 0;
393         struct input_event *ev;
394
395         /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
396            queue[0]) we need to shift this backwards. Except that chances are that the
397            queue may be either full or too full to prepend all the events needed for
398            syncing.
399
400            so we search for the last sync event in the queue and drop everything before
401            including that event and rely on the kernel to tell us the right value for that
402            bitfield during the sync process.
403          */
404
405         for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
406                 struct input_event e;
407                 queue_peek(dev, i, &e);
408                 if (e.type == EV_SYN)
409                         break;
410         }
411
412         if (i > 0)
413                 queue_shift_multiple(dev, i + 1, NULL);
414
415         if (libevdev_has_event_type(dev, EV_KEY))
416                 rc = sync_key_state(dev);
417         if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
418                 rc = sync_abs_state(dev);
419         if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
420                 rc = sync_mt_state(dev);
421
422         ev = queue_push(dev);
423         init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
424
425         dev->queue_nsync = queue_num_elements(dev);
426         dev->need_sync = 0;
427
428         return rc;
429 }
430
431 static int
432 update_key_state(struct libevdev *dev, const struct input_event *e)
433 {
434         if (!libevdev_has_event_type(dev, EV_KEY))
435                 return 1;
436
437         if (e->code > KEY_MAX)
438                 return 1;
439
440         if (e->value == 0)
441                 clear_bit(dev->key_values, e->code);
442         else
443                 set_bit(dev->key_values, e->code);
444
445         return 0;
446 }
447
448 static int
449 update_mt_state(struct libevdev *dev, const struct input_event *e)
450 {
451         if (e->code == ABS_MT_SLOT) {
452                 dev->current_slot = e->value;
453                 return 0;
454         } else if (dev->current_slot == -1)
455                 return 1;
456
457         dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
458
459         return 0;
460 }
461
462 static int
463 update_abs_state(struct libevdev *dev, const struct input_event *e)
464 {
465         if (!libevdev_has_event_type(dev, EV_ABS))
466                 return 1;
467
468         if (e->code > ABS_MAX)
469                 return 1;
470
471         if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
472                 return update_mt_state(dev, e);
473
474         dev->abs_info[e->code].value = e->value;
475
476         return 0;
477 }
478
479 static int
480 update_state(struct libevdev *dev, const struct input_event *e)
481 {
482         int rc = 0;
483
484         switch(e->type) {
485                 case EV_SYN:
486                 case EV_REL:
487                         break;
488                 case EV_KEY:
489                         rc = update_key_state(dev, e);
490                         break;
491                 case EV_ABS:
492                         rc = update_abs_state(dev, e);
493                         break;
494         }
495
496         dev->last_event_time = e->time;
497
498         return rc;
499 }
500
501 static int
502 read_more_events(struct libevdev *dev)
503 {
504         int free_elem;
505         int len;
506         struct input_event *next;
507
508         free_elem = queue_num_free_elements(dev);
509         if (free_elem <= 0)
510                 return 0;
511
512         next = queue_next_element(dev);
513         len = read(dev->fd, next, free_elem * sizeof(struct input_event));
514         if (len < 0) {
515                 return -errno;
516         } else if (len > 0 && len % sizeof(struct input_event) != 0)
517                 return -EINVAL;
518         else if (len > 0) {
519                 int nev = len/sizeof(struct input_event);
520                 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
521         }
522
523         return 0;
524 }
525
526 int libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
527 {
528         int rc = 0;
529
530         if (dev->fd < 0)
531                 return -ENODEV;
532
533         if (!(flags & (LIBEVDEV_READ_NORMAL|LIBEVDEV_READ_SYNC)))
534                 return -EINVAL;
535
536         if (flags & LIBEVDEV_READ_SYNC) {
537                 if (!dev->need_sync && dev->queue_nsync == 0)
538                         return -EAGAIN;
539                 else if (dev->need_sync) {
540                         rc = sync_state(dev);
541                         if (rc != 0)
542                                 return rc;
543                 }
544         } else if (dev->need_sync) {
545                 /* FIXME: still need to call update_state for all events
546                  * here, otherwise the library has the wrong view of the
547                  * device too */
548                 queue_shift_multiple(dev, dev->queue_nsync, NULL);
549         }
550
551         /* FIXME: check for O_NONBLOCK and if not set, skip if we have an
552          * event in the queue from the previous read.
553          */
554
555         /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
556            worst case we don't read fast enough and end up with SYN_DROPPED anyway */
557         rc = read_more_events(dev);
558         if (rc < 0 && rc != -EAGAIN)
559                 goto out;
560
561         if (queue_shift(dev, ev) != 0)
562                 return -EAGAIN;
563
564         update_state(dev, ev);
565
566         rc = 0;
567         if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
568                 dev->need_sync = 1;
569                 rc = 1;
570         }
571
572         if (flags & LIBEVDEV_READ_SYNC && dev->queue_nsync > 0) {
573                 dev->queue_nsync--;
574                 rc = 1;
575         }
576
577 out:
578         return rc;
579 }
580
581 const char *
582 libevdev_get_name(const struct libevdev *dev)
583 {
584         return dev->name ? dev->name : "";
585 }
586
587 const char *
588 libevdev_get_phys(const struct libevdev *dev)
589 {
590         return dev->phys;
591 }
592
593 const char *
594 libevdev_get_uniq(const struct libevdev *dev)
595 {
596         return dev->uniq;
597 }
598
599 int libevdev_get_product_id(const struct libevdev *dev)
600 {
601         return dev->ids.product;
602 }
603
604 int libevdev_get_vendor_id(const struct libevdev *dev)
605 {
606         return dev->ids.vendor;
607 }
608
609 int libevdev_get_bustype(const struct libevdev *dev)
610 {
611         return dev->ids.bustype;
612 }
613
614 int libevdev_get_version(const struct libevdev *dev)
615 {
616         return dev->ids.version;
617 }
618
619 int libevdev_get_driver_version(const struct libevdev *dev)
620 {
621         return dev->driver_version;
622 }
623
624 int
625 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
626 {
627         return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
628 }
629
630 int
631 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
632 {
633         return (type <= EV_MAX) && bit_is_set(dev->bits, type);
634 }
635
636 int
637 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
638 {
639         const unsigned long *mask;
640         int max;
641
642         if (!libevdev_has_event_type(dev, type))
643                 return 0;
644
645         if (type == EV_SYN)
646                 return 1;
647
648         max = type_to_mask_const(dev, type, &mask);
649
650         if (max == -1 || code > max)
651                 return 0;
652
653         return bit_is_set(mask, code);
654 }
655
656 int
657 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
658 {
659         int value;
660
661         if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
662                 return 0;
663
664         switch (type) {
665                 case EV_ABS: value = dev->abs_info[code].value; break;
666                 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
667                 default:
668                         value = 0;
669                         break;
670         }
671
672         return value;
673 }
674
675 int
676 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
677 {
678         if (libevdev_has_event_type(dev, type) &&
679             libevdev_has_event_code(dev, type, code)) {
680                 *value = libevdev_get_event_value(dev, type, code);
681                 return 1;
682         } else
683                 return 0;
684 }
685
686 int
687 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
688 {
689         if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
690                 return 0;
691
692         if (slot >= dev->num_slots || slot >= MAX_SLOTS)
693                 return 0;
694
695         if (code > ABS_MT_MAX || code < ABS_MT_MIN)
696                 return 0;
697
698         return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
699 }
700
701 int
702 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
703 {
704         if (libevdev_has_event_type(dev, EV_ABS) &&
705             libevdev_has_event_code(dev, EV_ABS, code) &&
706             slot < dev->num_slots && slot < MAX_SLOTS) {
707                 *value = libevdev_get_slot_value(dev, slot, code);
708                 return 1;
709         } else
710                 return 0;
711 }
712
713 int
714 libevdev_get_num_slots(const struct libevdev *dev)
715 {
716         return dev->num_slots;
717 }
718
719 int
720 libevdev_get_current_slot(const struct libevdev *dev)
721 {
722         return dev->current_slot;
723 }
724
725 const struct input_absinfo*
726 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
727 {
728         if (!libevdev_has_event_type(dev, EV_ABS) ||
729             !libevdev_has_event_code(dev, EV_ABS, code))
730                 return NULL;
731
732         return &dev->abs_info[code];
733 }
734
735 int
736 libevdev_get_abs_min(const struct libevdev *dev, unsigned int code)
737 {
738         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
739
740         return absinfo ? absinfo->minimum : 0;
741 }
742
743 int
744 libevdev_get_abs_max(const struct libevdev *dev, unsigned int code)
745 {
746         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
747
748         return absinfo ? absinfo->maximum : 0;
749 }
750
751 int
752 libevdev_get_abs_fuzz(const struct libevdev *dev, unsigned int code)
753 {
754         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
755
756         return absinfo ? absinfo->fuzz : 0;
757 }
758
759 int
760 libevdev_get_abs_flat(const struct libevdev *dev, unsigned int code)
761 {
762         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
763
764         return absinfo ? absinfo->flat : 0;
765 }
766
767 int
768 libevdev_get_abs_resolution(const struct libevdev *dev, unsigned int code)
769 {
770         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
771
772         return absinfo ? absinfo->resolution : 0;
773 }
774
775 int
776 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
777 {
778         if (type > EV_MAX)
779                 return 1;
780
781         set_bit(dev->bits, type);
782
783         return 0;
784 }
785
786 int
787 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
788 {
789         if (type > EV_MAX)
790                 return 1;
791
792         clear_bit(dev->bits, type);
793
794         return 0;
795 }
796
797 int
798 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
799                            unsigned int code, const void *data)
800 {
801         unsigned int max;
802         unsigned long *mask;
803
804         if (libevdev_enable_event_type(dev, type))
805                 return 1;
806
807         max = type_to_mask(dev, type, &mask);
808
809         if (code > max)
810                 return 1;
811
812         set_bit(mask, code);
813
814         if (type == EV_ABS) {
815                 const struct input_absinfo *abs = data;
816                 dev->abs_info[code] = *abs;
817         }
818
819         return 0;
820 }
821
822 int
823 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
824 {
825         unsigned int max;
826         unsigned long *mask;
827
828         if (type > EV_MAX)
829                 return 1;
830
831         max = type_to_mask(dev, type, &mask);
832
833         if (code > max)
834                 return 1;
835
836         clear_bit(mask, code);
837
838         return 0;
839 }
840
841 int
842 libevdev_kernel_enable_event_type(struct libevdev *dev, unsigned int type)
843 {
844         int rc;
845
846         if (type > EV_MAX)
847                 return -1;
848
849         rc = ioctl(dev->fd, UI_SET_EVBIT, type);
850         if (rc != -1)
851                 libevdev_enable_event_type(dev, type);
852
853         return (rc != -1) ? 0 : -errno;
854 }
855
856 int
857 libevdev_kernel_enable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
858 {
859         int rc;
860         int uinput_bit;
861         int max;
862         const unsigned long *mask;
863
864         rc = libevdev_kernel_enable_event_type(dev, type);
865         if (rc != 0)
866                 return rc;
867
868         max = type_to_mask_const(dev, type, &mask);
869         if (max == -1 || code > max)
870                 return -EINVAL;
871
872         switch(type) {
873                 case EV_KEY: uinput_bit = UI_SET_KEYBIT; break;
874                 case EV_REL: uinput_bit = UI_SET_RELBIT; break;
875                 case EV_ABS: uinput_bit = UI_SET_ABSBIT; break;
876                 case EV_MSC: uinput_bit = UI_SET_MSCBIT; break;
877                 case EV_LED: uinput_bit = UI_SET_LEDBIT; break;
878                 case EV_SND: uinput_bit = UI_SET_SNDBIT; break;
879                 case EV_FF: uinput_bit = UI_SET_FFBIT; break;
880                 case EV_SW: uinput_bit = UI_SET_SWBIT; break;
881         }
882
883         rc = ioctl(dev->fd, uinput_bit, type);
884         if (rc != -1)
885                 libevdev_enable_event_type(dev, type);
886
887         return (rc != -1) ? 0 : -errno;
888 }
889
890 int
891 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
892 {
893         int rc;
894
895         if (code > ABS_MAX)
896                 return -EINVAL;
897
898         rc = ioctl(dev->fd, EVIOCSABS(code), *abs);
899         if (rc < 0)
900                 rc = -errno;
901         else
902                 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
903
904         return rc;
905 }
906
907 int
908 libevdev_grab(struct libevdev *dev, int grab)
909 {
910         int rc = 0;
911
912         if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB)
913                 return -EINVAL;
914
915         if (grab == dev->grabbed)
916                 return 0;
917
918         if (grab == LIBEVDEV_GRAB)
919                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
920         else if (grab == LIBEVDEV_UNGRAB)
921                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
922
923         if (rc == 0)
924                 dev->grabbed = grab;
925
926         return rc < 0 ? -errno : 0;
927 }
928
929 const char*
930 libevdev_get_event_type_name(unsigned int type)
931 {
932         if (type > EV_MAX)
933                 return NULL;
934
935         return ev_map[type];
936 }
937
938 const char*
939 libevdev_get_event_code_name(unsigned int type, unsigned int code)
940 {
941         if (type > EV_MAX)
942                 return NULL;
943
944         if (code > ev_max[type])
945                 return NULL;
946
947         return event_type_map[type][code];
948 }
949
950 const char*
951 libevdev_get_input_prop_name(unsigned int prop)
952 {
953         if (prop > INPUT_PROP_MAX)
954                 return NULL;
955
956         return input_prop_map[prop];
957 }
958
959 int
960 libevdev_get_event_type_max(unsigned int type)
961 {
962         if (type > EV_MAX)
963                 return -1;
964
965         return ev_max[type];
966 }
967
968 int
969 libevdev_get_repeat(struct libevdev *dev, int *delay, int *period)
970 {
971         if (!libevdev_has_event_type(dev, EV_REP))
972                 return -1;
973
974         if (delay != NULL)
975                 *delay = dev->rep_values[REP_DELAY];
976         if (period != NULL)
977                 *period = dev->rep_values[REP_PERIOD];
978
979         return 0;
980 }