Add libevdev_has_event_pending()
[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_has_event_type(const struct libevdev *dev, unsigned int type)
696 {
697         return (type <= EV_MAX) && bit_is_set(dev->bits, type);
698 }
699
700 int
701 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
702 {
703         const unsigned long *mask;
704         int max;
705
706         if (!libevdev_has_event_type(dev, type))
707                 return 0;
708
709         if (type == EV_SYN)
710                 return 1;
711
712         max = type_to_mask_const(dev, type, &mask);
713
714         if (max == -1 || code > max)
715                 return 0;
716
717         return bit_is_set(mask, code);
718 }
719
720 int
721 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
722 {
723         int value;
724
725         if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
726                 return 0;
727
728         switch (type) {
729                 case EV_ABS: value = dev->abs_info[code].value; break;
730                 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
731                 default:
732                         value = 0;
733                         break;
734         }
735
736         return value;
737 }
738
739 int
740 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
741 {
742         if (libevdev_has_event_type(dev, type) &&
743             libevdev_has_event_code(dev, type, code)) {
744                 *value = libevdev_get_event_value(dev, type, code);
745                 return 1;
746         } else
747                 return 0;
748 }
749
750 int
751 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
752 {
753         if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
754                 return 0;
755
756         if (slot >= dev->num_slots || slot >= MAX_SLOTS)
757                 return 0;
758
759         if (code > ABS_MT_MAX || code < ABS_MT_MIN)
760                 return 0;
761
762         return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
763 }
764
765 int
766 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
767 {
768         if (libevdev_has_event_type(dev, EV_ABS) &&
769             libevdev_has_event_code(dev, EV_ABS, code) &&
770             slot < dev->num_slots && slot < MAX_SLOTS) {
771                 *value = libevdev_get_slot_value(dev, slot, code);
772                 return 1;
773         } else
774                 return 0;
775 }
776
777 int
778 libevdev_get_num_slots(const struct libevdev *dev)
779 {
780         return dev->num_slots;
781 }
782
783 int
784 libevdev_get_current_slot(const struct libevdev *dev)
785 {
786         return dev->current_slot;
787 }
788
789 const struct input_absinfo*
790 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
791 {
792         if (!libevdev_has_event_type(dev, EV_ABS) ||
793             !libevdev_has_event_code(dev, EV_ABS, code))
794                 return NULL;
795
796         return &dev->abs_info[code];
797 }
798
799 int
800 libevdev_get_abs_min(const struct libevdev *dev, unsigned int code)
801 {
802         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
803
804         return absinfo ? absinfo->minimum : 0;
805 }
806
807 int
808 libevdev_get_abs_max(const struct libevdev *dev, unsigned int code)
809 {
810         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
811
812         return absinfo ? absinfo->maximum : 0;
813 }
814
815 int
816 libevdev_get_abs_fuzz(const struct libevdev *dev, unsigned int code)
817 {
818         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
819
820         return absinfo ? absinfo->fuzz : 0;
821 }
822
823 int
824 libevdev_get_abs_flat(const struct libevdev *dev, unsigned int code)
825 {
826         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
827
828         return absinfo ? absinfo->flat : 0;
829 }
830
831 int
832 libevdev_get_abs_resolution(const struct libevdev *dev, unsigned int code)
833 {
834         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
835
836         return absinfo ? absinfo->resolution : 0;
837 }
838
839 int
840 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
841 {
842         if (type > EV_MAX)
843                 return -1;
844
845         if (libevdev_has_event_type(dev, type))
846                 return 0;
847
848         set_bit(dev->bits, type);
849
850         if (type == EV_REP) {
851                 int delay = 0, period = 0;
852                 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
853                 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
854         }
855         return 0;
856 }
857
858 int
859 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
860 {
861         if (type > EV_MAX || type == EV_SYN)
862                 return -1;
863
864         clear_bit(dev->bits, type);
865
866         return 0;
867 }
868
869 int
870 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
871                            unsigned int code, const void *data)
872 {
873         unsigned int max;
874         unsigned long *mask;
875
876         if (libevdev_enable_event_type(dev, type))
877                 return -1;
878
879         switch(type) {
880                 case EV_SYN:
881                         return 0;
882                 case EV_ABS:
883                 case EV_REP:
884                         if (data == NULL)
885                                 return -1;
886                         break;
887                 default:
888                         if (data != NULL)
889                                 return -1;
890                         break;
891         }
892
893         max = type_to_mask(dev, type, &mask);
894
895         if (code > max)
896                 return -1;
897
898         set_bit(mask, code);
899
900         if (type == EV_ABS) {
901                 const struct input_absinfo *abs = data;
902                 dev->abs_info[code] = *abs;
903         } else if (type == EV_REP) {
904                 const int *value = data;
905                 dev->rep_values[code] = *value;
906         }
907
908         return 0;
909 }
910
911 int
912 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
913 {
914         unsigned int max;
915         unsigned long *mask;
916
917         if (type > EV_MAX)
918                 return -1;
919
920         max = type_to_mask(dev, type, &mask);
921
922         if (code > max)
923                 return -1;
924
925         clear_bit(mask, code);
926
927         return 0;
928 }
929
930 int
931 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
932 {
933         int rc;
934
935         if (code > ABS_MAX)
936                 return -EINVAL;
937
938         rc = ioctl(dev->fd, EVIOCSABS(code), abs);
939         if (rc < 0)
940                 rc = -errno;
941         else
942                 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
943
944         return rc;
945 }
946
947 int
948 libevdev_grab(struct libevdev *dev, int grab)
949 {
950         int rc = 0;
951
952         if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB)
953                 return -EINVAL;
954
955         if (grab == dev->grabbed)
956                 return 0;
957
958         if (grab == LIBEVDEV_GRAB)
959                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
960         else if (grab == LIBEVDEV_UNGRAB)
961                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
962
963         if (rc == 0)
964                 dev->grabbed = grab;
965
966         return rc < 0 ? -errno : 0;
967 }
968
969 int
970 libevdev_is_event_type(const struct input_event *ev, unsigned int type)
971 {
972         return type < EV_MAX && ev->type == type;
973 }
974
975 int
976 libevdev_is_event_code(const struct input_event *ev, unsigned int type, unsigned int code)
977 {
978         return type < EV_MAX &&
979                 ev->type == type &&
980                 (type == EV_SYN || code <= libevdev_get_event_type_max(type)) &&
981                 ev->code == code;
982 }
983
984 const char*
985 libevdev_get_event_type_name(unsigned int type)
986 {
987         if (type > EV_MAX)
988                 return NULL;
989
990         return ev_map[type];
991 }
992
993 const char*
994 libevdev_get_event_code_name(unsigned int type, unsigned int code)
995 {
996         if (type > EV_MAX)
997                 return NULL;
998
999         if (code > ev_max[type])
1000                 return NULL;
1001
1002         return event_type_map[type][code];
1003 }
1004
1005 const char*
1006 libevdev_get_input_prop_name(unsigned int prop)
1007 {
1008         if (prop > INPUT_PROP_MAX)
1009                 return NULL;
1010
1011         return input_prop_map[prop];
1012 }
1013
1014 int
1015 libevdev_get_event_type_max(unsigned int type)
1016 {
1017         if (type > EV_MAX)
1018                 return -1;
1019
1020         return ev_max[type];
1021 }
1022
1023 int
1024 libevdev_get_repeat(struct libevdev *dev, int *delay, int *period)
1025 {
1026         if (!libevdev_has_event_type(dev, EV_REP))
1027                 return -1;
1028
1029         if (delay != NULL)
1030                 *delay = dev->rep_values[REP_DELAY];
1031         if (period != NULL)
1032                 *period = dev->rep_values[REP_PERIOD];
1033
1034         return 0;
1035 }