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