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