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