Skip over ABS_MT_SLOT when syncing MT axis 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
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), &abs_info);
292                         if (rc < 0)
293                                 goto out;
294
295                         dev->abs_info[i] = abs_info;
296                         if (i == ABS_MT_SLOT) {
297                                 dev->num_slots = abs_info.maximum + 1; /* FIXME: non-zero min? */
298                                 dev->current_slot = abs_info.value;
299                         }
300
301                 }
302         }
303
304         rc = init_event_queue(dev);
305         if (rc < 0)
306                 return -rc;
307
308         /* not copying key state because we won't know when we'll start to
309          * use this fd and key's are likely to change state by then.
310          * Same with the valuators, really, but they may not change.
311          */
312
313         dev->fd = fd;
314
315 out:
316         return rc ? -errno : 0;
317 }
318
319 int
320 libevdev_get_fd(const struct libevdev* dev)
321 {
322         return dev->fd;
323 }
324
325 static inline void
326 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
327 {
328         ev->time = dev->last_event_time;
329         ev->type = type;
330         ev->code = code;
331         ev->value = value;
332 }
333
334 static int
335 sync_key_state(struct libevdev *dev)
336 {
337         int rc;
338         int i;
339         unsigned long keystate[NLONGS(KEY_MAX)];
340
341         rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
342         if (rc < 0)
343                 goto out;
344
345         for (i = 0; i < KEY_MAX; i++) {
346                 int old, new;
347                 old = bit_is_set(dev->key_values, i);
348                 new = bit_is_set(keystate, i);
349                 if (old ^ new) {
350                         struct input_event *ev = queue_push(dev);
351                         init_event(dev, ev, EV_KEY, i, new ? 1 : 0);
352                 }
353                 set_bit_state(dev->key_values, i, new);
354         }
355
356         rc = 0;
357 out:
358         return rc ? -errno : 0;
359 }
360
361 static int
362 sync_abs_state(struct libevdev *dev)
363 {
364         int rc;
365         int i;
366
367         for (i = ABS_X; i <= ABS_MAX; i++) {
368                 struct input_absinfo abs_info;
369
370                 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
371                         continue;
372
373                 if (!bit_is_set(dev->abs_bits, i))
374                         continue;
375
376                 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
377                 if (rc < 0)
378                         goto out;
379
380                 if (dev->abs_info[i].value != abs_info.value) {
381                         struct input_event *ev = queue_push(dev);
382
383                         init_event(dev, ev, EV_ABS, i, abs_info.value);
384                         dev->abs_info[i].value = abs_info.value;
385                 }
386         }
387
388         rc = 0;
389 out:
390         return rc ? -errno : 0;
391 }
392
393 static int
394 sync_mt_state(struct libevdev *dev)
395 {
396         int rc;
397         int i;
398         struct mt_state {
399                 int code;
400                 int val[MAX_SLOTS];
401         } mt_state[ABS_MT_CNT];
402
403         for (i = ABS_MT_MIN; i < ABS_MT_MAX; i++) {
404                 int idx;
405                 if (i == ABS_MT_SLOT)
406                         continue;
407
408                 idx = i - ABS_MT_MIN;
409                 mt_state[idx].code = i;
410                 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
411                 if (rc < 0)
412                         goto out;
413         }
414
415         for (i = 0; i < dev->num_slots; i++) {
416                 int j;
417                 struct input_event *ev;
418
419                 ev = queue_push(dev);
420                 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, i);
421                 for (j = ABS_MT_MIN; j < ABS_MT_MAX; j++) {
422                         int jdx = j - ABS_MT_MIN;
423
424                         if (j == ABS_MT_SLOT)
425                                 continue;
426
427                         if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
428                                 continue;
429
430                         ev = queue_push(dev);
431                         init_event(dev, ev, EV_ABS, j, mt_state[jdx].val[i]);
432                         dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
433                 }
434         }
435
436         rc = 0;
437 out:
438         return rc ? -errno : 0;
439 }
440
441 static int
442 sync_state(struct libevdev *dev)
443 {
444         int i;
445         int rc = 0;
446         struct input_event *ev;
447
448         /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
449            queue[0]) we need to shift this backwards. Except that chances are that the
450            queue may be either full or too full to prepend all the events needed for
451            syncing.
452
453            so we search for the last sync event in the queue and drop everything before
454            including that event and rely on the kernel to tell us the right value for that
455            bitfield during the sync process.
456          */
457
458         for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
459                 struct input_event e;
460                 queue_peek(dev, i, &e);
461                 if (e.type == EV_SYN)
462                         break;
463         }
464
465         if (i > 0)
466                 queue_shift_multiple(dev, i + 1, NULL);
467
468         if (libevdev_has_event_type(dev, EV_KEY))
469                 rc = sync_key_state(dev);
470         if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
471                 rc = sync_abs_state(dev);
472         if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
473                 rc = sync_mt_state(dev);
474
475         ev = queue_push(dev);
476         init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
477
478         dev->queue_nsync = queue_num_elements(dev);
479         dev->need_sync = 0;
480
481         return rc;
482 }
483
484 static int
485 update_key_state(struct libevdev *dev, const struct input_event *e)
486 {
487         if (!libevdev_has_event_type(dev, EV_KEY))
488                 return 1;
489
490         if (e->code > KEY_MAX)
491                 return 1;
492
493         if (e->value == 0)
494                 clear_bit(dev->key_values, e->code);
495         else
496                 set_bit(dev->key_values, e->code);
497
498         return 0;
499 }
500
501 static int
502 update_mt_state(struct libevdev *dev, const struct input_event *e)
503 {
504         if (e->code == ABS_MT_SLOT) {
505                 dev->current_slot = e->value;
506                 return 0;
507         } else if (dev->current_slot == -1)
508                 return 1;
509
510         dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
511
512         return 0;
513 }
514
515 static int
516 update_abs_state(struct libevdev *dev, const struct input_event *e)
517 {
518         if (!libevdev_has_event_type(dev, EV_ABS))
519                 return 1;
520
521         if (e->code > ABS_MAX)
522                 return 1;
523
524         if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
525                 return update_mt_state(dev, e);
526
527         dev->abs_info[e->code].value = e->value;
528
529         return 0;
530 }
531
532 static int
533 update_state(struct libevdev *dev, const struct input_event *e)
534 {
535         int rc = 0;
536
537         switch(e->type) {
538                 case EV_SYN:
539                 case EV_REL:
540                         break;
541                 case EV_KEY:
542                         rc = update_key_state(dev, e);
543                         break;
544                 case EV_ABS:
545                         rc = update_abs_state(dev, e);
546                         break;
547         }
548
549         dev->last_event_time = e->time;
550
551         return rc;
552 }
553
554 static int
555 read_more_events(struct libevdev *dev)
556 {
557         int free_elem;
558         int len;
559         struct input_event *next;
560
561         free_elem = queue_num_free_elements(dev);
562         if (free_elem <= 0)
563                 return 0;
564
565         next = queue_next_element(dev);
566         len = read(dev->fd, next, free_elem * sizeof(struct input_event));
567         if (len < 0) {
568                 return -errno;
569         } else if (len > 0 && len % sizeof(struct input_event) != 0)
570                 return -EINVAL;
571         else if (len > 0) {
572                 int nev = len/sizeof(struct input_event);
573                 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
574         }
575
576         return 0;
577 }
578
579 int libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
580 {
581         int rc = 0;
582
583         if (dev->fd < 0)
584                 return -ENODEV;
585
586         if (flags & LIBEVDEV_READ_SYNC) {
587                 if (!dev->need_sync && dev->queue_nsync == 0)
588                         return -EAGAIN;
589                 else if (dev->need_sync) {
590                         rc = sync_state(dev);
591                         if (rc != 0)
592                                 return rc;
593                 }
594         } else if (dev->need_sync) {
595                 /* FIXME: still need to call update_state for all events
596                  * here, otherwise the library has the wrong view of the
597                  * device too */
598                 queue_shift_multiple(dev, dev->queue_nsync, NULL);
599         }
600
601         /* FIXME: check for O_NONBLOCK and if not set, skip if we have an
602          * event in the queue from the previous read.
603          */
604
605         /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
606            worst case we don't read fast enough and end up with SYN_DROPPED anyway */
607         rc = read_more_events(dev);
608         if (rc < 0 && rc != -EAGAIN)
609                 goto out;
610
611         if (queue_shift(dev, ev) != 0)
612                 return -EAGAIN;
613
614         update_state(dev, ev);
615
616         rc = 0;
617         if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
618                 dev->need_sync = 1;
619                 rc = 1;
620         }
621
622         if (flags & LIBEVDEV_READ_SYNC && dev->queue_nsync > 0) {
623                 dev->queue_nsync--;
624                 rc = 1;
625         }
626
627 out:
628         return rc;
629 }
630
631 const char *
632 libevdev_get_name(const struct libevdev *dev)
633 {
634         return dev->name;
635 }
636
637 const char *
638 libevdev_get_phys(const struct libevdev *dev)
639 {
640         return dev->phys;
641 }
642
643 const char *
644 libevdev_get_uniq(const struct libevdev *dev)
645 {
646         return dev->uniq;
647 }
648
649 int libevdev_get_product_id(const struct libevdev *dev)
650 {
651         return dev->ids.product;
652 }
653
654 int libevdev_get_vendor_id(const struct libevdev *dev)
655 {
656         return dev->ids.vendor;
657 }
658
659 int libevdev_get_bustype(const struct libevdev *dev)
660 {
661         return dev->ids.bustype;
662 }
663
664 int libevdev_get_version(const struct libevdev *dev)
665 {
666         return dev->ids.version;
667 }
668
669 int libevdev_get_driver_version(const struct libevdev *dev)
670 {
671         return dev->driver_version;
672 }
673
674 int
675 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
676 {
677         return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
678 }
679
680 int
681 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
682 {
683         return (type <= EV_MAX) && bit_is_set(dev->bits, type);
684 }
685
686 int
687 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
688 {
689         const unsigned long *mask;
690         unsigned int max;
691
692         if (!libevdev_has_event_type(dev, type))
693                 return 0;
694
695         if (type == EV_SYN)
696                 return 1;
697
698         max = type_to_mask_const(dev, type, &mask);
699
700         if (code > max)
701                 return 0;
702
703         return bit_is_set(mask, code);
704 }
705
706 int
707 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
708 {
709         int value;
710
711         if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
712                 return 0;
713
714         switch (type) {
715                 case EV_ABS: value = dev->abs_info[code].value; break;
716                 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
717                 default:
718                         value = 0;
719                         break;
720         }
721
722         return value;
723 }
724
725 int
726 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
727 {
728         if (libevdev_has_event_type(dev, type) &&
729             libevdev_has_event_code(dev, type, code)) {
730                 *value = libevdev_get_event_value(dev, type, code);
731                 return 1;
732         } else
733                 return 0;
734 }
735
736 int
737 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
738 {
739         if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
740                 return 0;
741
742         if (slot >= dev->num_slots || slot >= MAX_SLOTS)
743                 return 0;
744
745         if (code > ABS_MT_MAX || code < ABS_MT_MIN)
746                 return 0;
747
748         return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
749 }
750
751 int
752 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
753 {
754         if (libevdev_has_event_type(dev, EV_ABS) &&
755             libevdev_has_event_code(dev, EV_ABS, code) &&
756             slot < dev->num_slots && slot < MAX_SLOTS) {
757                 *value = libevdev_get_slot_value(dev, slot, code);
758                 return 1;
759         } else
760                 return 0;
761 }
762
763 int
764 libevdev_get_num_slots(const struct libevdev *dev)
765 {
766         return dev->num_slots;
767 }
768
769 int
770 libevdev_get_current_slot(const struct libevdev *dev)
771 {
772         return dev->current_slot;
773 }
774
775 const struct input_absinfo*
776 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
777 {
778         if (!libevdev_has_event_type(dev, EV_ABS) ||
779             !libevdev_has_event_code(dev, EV_ABS, code))
780                 return NULL;
781
782         return &dev->abs_info[code];
783 }
784
785 int
786 libevdev_get_abs_min(const struct libevdev *dev, unsigned int code)
787 {
788         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
789
790         return absinfo ? absinfo->minimum : 0;
791 }
792
793 int
794 libevdev_get_abs_max(const struct libevdev *dev, unsigned int code)
795 {
796         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
797
798         return absinfo ? absinfo->maximum : 0;
799 }
800
801 int
802 libevdev_get_abs_fuzz(const struct libevdev *dev, unsigned int code)
803 {
804         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
805
806         return absinfo ? absinfo->fuzz : 0;
807 }
808
809 int
810 libevdev_get_abs_flat(const struct libevdev *dev, unsigned int code)
811 {
812         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
813
814         return absinfo ? absinfo->flat : 0;
815 }
816
817 int
818 libevdev_get_abs_resolution(const struct libevdev *dev, unsigned int code)
819 {
820         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
821
822         return absinfo ? absinfo->resolution : 0;
823 }
824
825 int
826 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
827 {
828         if (type > EV_MAX)
829                 return 1;
830
831         set_bit(dev->bits, type);
832
833         /* FIXME: pass through to kernel */
834
835         return 0;
836 }
837
838 int
839 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
840 {
841         if (type > EV_MAX)
842                 return 1;
843
844         clear_bit(dev->bits, type);
845
846         /* FIXME: pass through to kernel */
847
848         return 0;
849 }
850
851 int
852 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
853                            unsigned int code, const void *data)
854 {
855         unsigned int max;
856         unsigned long *mask;
857
858         if (libevdev_enable_event_type(dev, type))
859                 return 1;
860
861         max = type_to_mask(dev, type, &mask);
862
863         if (code > max)
864                 return 1;
865
866         set_bit(mask, code);
867
868         if (type == EV_ABS) {
869                 const struct input_absinfo *abs = data;
870                 dev->abs_info[code] = *abs;
871         }
872
873         /* FIXME: pass through to kernel */
874
875         return 0;
876 }
877
878 int
879 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
880 {
881         unsigned int max;
882         unsigned long *mask;
883
884         if (type > EV_MAX)
885                 return 1;
886
887         max = type_to_mask(dev, type, &mask);
888
889         if (code > max)
890                 return 1;
891
892         clear_bit(mask, code);
893
894         /* FIXME: pass through to kernel */
895
896         return 0;
897 }
898
899 int
900 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
901 {
902         int rc;
903
904         if (code > ABS_MAX)
905                 return -EINVAL;
906
907         rc = ioctl(dev->fd, EVIOCSABS(code), *abs);
908         if (rc < 0)
909                 rc = -errno;
910         else
911                 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
912
913         return rc;
914 }
915
916 int
917 libevdev_grab(struct libevdev *dev, int grab)
918 {
919         int rc = 0;
920
921         if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB)
922                 return -EINVAL;
923
924         if (grab == dev->grabbed)
925                 return 0;
926
927         if (grab == LIBEVDEV_GRAB)
928                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
929         else if (grab == LIBEVDEV_UNGRAB)
930                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
931
932         if (rc == 0)
933                 dev->grabbed = grab;
934
935         return rc < 0 ? -errno : 0;
936 }