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