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