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