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