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