Rename enums to match lower_case format
[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         rc = ioctl(fd, EVIOCGLED(sizeof(dev->led_values)), dev->led_values);
240         if (rc < 0)
241                 goto out;
242
243         /* rep is a special case, always set it to 1 for both values if EV_REP is set */
244         if (bit_is_set(dev->bits, EV_REP)) {
245                 for (i = 0; i < REP_CNT; i++)
246                         set_bit(dev->rep_bits, i);
247                 rc = ioctl(fd, EVIOCGREP, dev->rep_values);
248                 if (rc < 0)
249                         goto out;
250         }
251
252         for (i = ABS_X; i <= ABS_MAX; i++) {
253                 if (bit_is_set(dev->abs_bits, i)) {
254                         struct input_absinfo abs_info;
255                         rc = ioctl(fd, EVIOCGABS(i), &abs_info);
256                         if (rc < 0)
257                                 goto out;
258
259                         dev->abs_info[i] = abs_info;
260                         if (i == ABS_MT_SLOT) {
261                                 dev->num_slots = abs_info.maximum + 1;
262                                 dev->current_slot = abs_info.value;
263                         }
264
265                 }
266         }
267
268         dev->fd = fd;
269         sync_mt_state(dev, 0);
270
271         rc = init_event_queue(dev);
272         if (rc < 0) {
273                 dev->fd = -1;
274                 return -rc;
275         }
276
277         /* not copying key state because we won't know when we'll start to
278          * use this fd and key's are likely to change state by then.
279          * Same with the valuators, really, but they may not change.
280          */
281
282 out:
283         return rc ? -errno : 0;
284 }
285
286 int
287 libevdev_get_fd(const struct libevdev* dev)
288 {
289         return dev->fd;
290 }
291
292 static inline void
293 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
294 {
295         ev->time = dev->last_event_time;
296         ev->type = type;
297         ev->code = code;
298         ev->value = value;
299 }
300
301 static int
302 sync_key_state(struct libevdev *dev)
303 {
304         int rc;
305         int i;
306         unsigned long keystate[NLONGS(KEY_MAX)];
307
308         rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
309         if (rc < 0)
310                 goto out;
311
312         for (i = 0; i < KEY_MAX; i++) {
313                 int old, new;
314                 old = bit_is_set(dev->key_values, i);
315                 new = bit_is_set(keystate, i);
316                 if (old ^ new) {
317                         struct input_event *ev = queue_push(dev);
318                         init_event(dev, ev, EV_KEY, i, new ? 1 : 0);
319                 }
320                 set_bit_state(dev->key_values, i, new);
321         }
322
323         rc = 0;
324 out:
325         return rc ? -errno : 0;
326 }
327
328 static int
329 sync_led_state(struct libevdev *dev)
330 {
331         int rc;
332         int i;
333         unsigned long ledstate[NLONGS(LED_MAX)];
334
335         rc = ioctl(dev->fd, EVIOCGLED(sizeof(ledstate)), ledstate);
336         if (rc < 0)
337                 goto out;
338
339         for (i = 0; i < LED_MAX; i++) {
340                 int old, new;
341                 old = bit_is_set(dev->led_values, i);
342                 new = bit_is_set(ledstate, i);
343                 if (old ^ new) {
344                         struct input_event *ev = queue_push(dev);
345                         init_event(dev, ev, EV_LED, i, new ? 1 : 0);
346                 }
347                 set_bit_state(dev->led_values, i, new);
348         }
349
350         rc = 0;
351 out:
352         return rc ? -errno : 0;
353 }
354 static int
355 sync_abs_state(struct libevdev *dev)
356 {
357         int rc;
358         int i;
359
360         for (i = ABS_X; i <= ABS_MAX; i++) {
361                 struct input_absinfo abs_info;
362
363                 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
364                         continue;
365
366                 if (!bit_is_set(dev->abs_bits, i))
367                         continue;
368
369                 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
370                 if (rc < 0)
371                         goto out;
372
373                 if (dev->abs_info[i].value != abs_info.value) {
374                         struct input_event *ev = queue_push(dev);
375
376                         init_event(dev, ev, EV_ABS, i, abs_info.value);
377                         dev->abs_info[i].value = abs_info.value;
378                 }
379         }
380
381         rc = 0;
382 out:
383         return rc ? -errno : 0;
384 }
385
386 static int
387 sync_mt_state(struct libevdev *dev, int create_events)
388 {
389         int rc;
390         int i;
391         struct mt_state {
392                 int code;
393                 int val[MAX_SLOTS];
394         } mt_state[ABS_MT_CNT];
395
396         for (i = ABS_MT_MIN; i < ABS_MT_MAX; i++) {
397                 int idx;
398                 if (i == ABS_MT_SLOT)
399                         continue;
400
401                 if (!libevdev_has_event_code(dev, EV_ABS, i))
402                         continue;
403
404                 idx = i - ABS_MT_MIN;
405                 mt_state[idx].code = i;
406                 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
407                 if (rc < 0)
408                         goto out;
409         }
410
411         for (i = 0; i < dev->num_slots; i++) {
412                 int j;
413                 struct input_event *ev;
414
415                 if (create_events) {
416                         ev = queue_push(dev);
417                         init_event(dev, ev, EV_ABS, ABS_MT_SLOT, i);
418                 }
419
420                 for (j = ABS_MT_MIN; j < ABS_MT_MAX; j++) {
421                         int jdx = j - ABS_MT_MIN;
422
423                         if (j == ABS_MT_SLOT)
424                                 continue;
425
426                         if (!libevdev_has_event_code(dev, EV_ABS, j))
427                                 continue;
428
429                         if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
430                                 continue;
431
432                         if (create_events) {
433                                 ev = queue_push(dev);
434                                 init_event(dev, ev, EV_ABS, j, mt_state[jdx].val[i]);
435                         }
436                         dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
437                 }
438         }
439
440         rc = 0;
441 out:
442         return rc ? -errno : 0;
443 }
444
445 static int
446 sync_state(struct libevdev *dev)
447 {
448         int i;
449         int rc = 0;
450         struct input_event *ev;
451
452         /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
453            queue[0]) we need to shift this backwards. Except that chances are that the
454            queue may be either full or too full to prepend all the events needed for
455            SYNC_IN_PROGRESS.
456
457            so we search for the last sync event in the queue and drop everything before
458            including that event and rely on the kernel to tell us the right value for that
459            bitfield during the sync process.
460          */
461
462         for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
463                 struct input_event e;
464                 queue_peek(dev, i, &e);
465                 if (e.type == EV_SYN)
466                         break;
467         }
468
469         if (i > 0)
470                 queue_shift_multiple(dev, i + 1, NULL);
471
472         if (libevdev_has_event_type(dev, EV_KEY))
473                 rc = sync_key_state(dev);
474         if (libevdev_has_event_type(dev, EV_LED))
475                 rc = sync_led_state(dev);
476         if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
477                 rc = sync_abs_state(dev);
478         if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
479                 rc = sync_mt_state(dev, 1);
480
481         dev->queue_nsync = queue_num_elements(dev);
482
483         if (dev->queue_nsync > 0) {
484                 ev = queue_push(dev);
485                 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
486                 dev->queue_nsync++;
487         }
488
489         return rc;
490 }
491
492 static int
493 update_key_state(struct libevdev *dev, const struct input_event *e)
494 {
495         if (!libevdev_has_event_type(dev, EV_KEY))
496                 return 1;
497
498         if (e->code > KEY_MAX)
499                 return 1;
500
501         set_bit_state(dev->key_values, e->code, e->value != 0);
502
503         return 0;
504 }
505
506 static int
507 update_mt_state(struct libevdev *dev, const struct input_event *e)
508 {
509         if (e->code == ABS_MT_SLOT) {
510                 int i;
511                 dev->current_slot = e->value;
512                 /* sync abs_info with the current slot values */
513                 for (i = ABS_MT_SLOT + 1; i <= ABS_MT_MAX; i++) {
514                         if (libevdev_has_event_code(dev, EV_ABS, i))
515                                 dev->abs_info[i].value = dev->mt_slot_vals[dev->current_slot][i - ABS_MT_MIN];
516                 }
517
518                 return 0;
519         } else if (dev->current_slot == -1)
520                 return 1;
521
522         dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
523
524         return 0;
525 }
526
527 static int
528 update_abs_state(struct libevdev *dev, const struct input_event *e)
529 {
530         if (!libevdev_has_event_type(dev, EV_ABS))
531                 return 1;
532
533         if (e->code > ABS_MAX)
534                 return 1;
535
536         if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
537                 update_mt_state(dev, e);
538
539         dev->abs_info[e->code].value = e->value;
540
541         return 0;
542 }
543
544 static int
545 update_led_state(struct libevdev *dev, const struct input_event *e)
546 {
547         if (!libevdev_has_event_type(dev, EV_LED))
548                 return 1;
549
550         if (e->code > LED_MAX)
551                 return 1;
552
553         set_bit_state(dev->led_values, e->code, e->value != 0);
554
555         return 0;
556 }
557
558 static int
559 update_state(struct libevdev *dev, const struct input_event *e)
560 {
561         int rc = 0;
562
563         switch(e->type) {
564                 case EV_SYN:
565                 case EV_REL:
566                         break;
567                 case EV_KEY:
568                         rc = update_key_state(dev, e);
569                         break;
570                 case EV_ABS:
571                         rc = update_abs_state(dev, e);
572                         break;
573                 case EV_LED:
574                         rc = update_led_state(dev, e);
575                         break;
576         }
577
578         dev->last_event_time = e->time;
579
580         return rc;
581 }
582
583 static int
584 read_more_events(struct libevdev *dev)
585 {
586         int free_elem;
587         int len;
588         struct input_event *next;
589
590         free_elem = queue_num_free_elements(dev);
591         if (free_elem <= 0)
592                 return 0;
593
594         next = queue_next_element(dev);
595         len = read(dev->fd, next, free_elem * sizeof(struct input_event));
596         if (len < 0) {
597                 return -errno;
598         } else if (len > 0 && len % sizeof(struct input_event) != 0)
599                 return -EINVAL;
600         else if (len > 0) {
601                 int nev = len/sizeof(struct input_event);
602                 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
603         }
604
605         return 0;
606 }
607
608 int libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
609 {
610         int rc = 0;
611
612         if (dev->fd < 0)
613                 return -ENODEV;
614
615         if (!(flags & (LIBEVDEV_READ_NORMAL|LIBEVDEV_READ_SYNC|LIBEVDEV_FORCE_SYNC)))
616                 return -EINVAL;
617
618         if (flags & LIBEVDEV_READ_SYNC) {
619                 if (dev->sync_state == SYNC_NEEDED) {
620                         rc = sync_state(dev);
621                         if (rc != 0)
622                                 return rc;
623                         dev->sync_state = SYNC_IN_PROGRESS;
624                 }
625
626                 if (dev->queue_nsync == 0) {
627                         dev->sync_state = SYNC_NONE;
628                         return -EAGAIN;
629                 }
630
631         } else if (dev->sync_state != SYNC_NONE) {
632                 struct input_event e;
633
634                 /* call update_state for all events here, otherwise the library has the wrong view
635                    of the device too */
636                 while (queue_shift(dev, &e) == 0) {
637                         dev->queue_nsync--;
638                         update_state(dev, &e);
639                 }
640
641                 dev->sync_state = SYNC_NONE;
642         }
643
644         /* FIXME: if the first event after SYNC_IN_PROGRESS is a SYN_DROPPED, log this */
645
646         /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
647            worst case we don't read fast enough and end up with SYN_DROPPED anyway.
648
649            Except if the fd is in blocking mode and we still have events from the last read, don't
650            read in any more.
651          */
652         do {
653                 if (!(flags & LIBEVDEV_READ_BLOCKING) ||
654                     queue_num_elements(dev) == 0) {
655                         rc = read_more_events(dev);
656                         if (rc < 0 && rc != -EAGAIN)
657                                 goto out;
658                 }
659
660                 if (flags & LIBEVDEV_FORCE_SYNC) {
661                         dev->sync_state = SYNC_NEEDED;
662                         rc = 1;
663                         goto out;
664                 }
665
666
667                 if (queue_shift(dev, ev) != 0)
668                         return -EAGAIN;
669
670                 update_state(dev, ev);
671
672         /* if we disabled a code, get the next event instead */
673         } while(!libevdev_has_event_code(dev, ev->type, ev->code));
674
675         rc = 0;
676         if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
677                 dev->sync_state = SYNC_NEEDED;
678                 rc = 1;
679         }
680
681         if (flags & LIBEVDEV_READ_SYNC && dev->queue_nsync > 0) {
682                 dev->queue_nsync--;
683                 rc = 1;
684                 if (dev->queue_nsync == 0)
685                         dev->sync_state = SYNC_NONE;
686         }
687
688 out:
689         return rc;
690 }
691
692 int libevdev_has_event_pending(struct libevdev *dev)
693 {
694         struct pollfd fds = { dev->fd, POLLIN, 0 };
695         int rc;
696
697         if (dev->fd < 0)
698                 return -EBADF;
699
700         if (queue_num_elements(dev) != 0)
701                 return 1;
702
703         rc = poll(&fds, 1, 0);
704         return (rc >= 0) ? rc : -errno;
705 }
706
707 const char *
708 libevdev_get_name(const struct libevdev *dev)
709 {
710         return dev->name ? dev->name : "";
711 }
712
713 const char *
714 libevdev_get_phys(const struct libevdev *dev)
715 {
716         return dev->phys;
717 }
718
719 const char *
720 libevdev_get_uniq(const struct libevdev *dev)
721 {
722         return dev->uniq;
723 }
724
725 #define STRING_SETTER(field) \
726 void libevdev_set_##field(struct libevdev *dev, const char *field) \
727 { \
728         if (field == NULL) \
729                 return; \
730         free(dev->field); \
731         dev->field = strdup(field); \
732 }
733
734 STRING_SETTER(name);
735 STRING_SETTER(phys);
736 STRING_SETTER(uniq);
737
738
739 #define PRODUCT_GETTER(name, field) \
740 int libevdev_get_##name(const struct libevdev *dev) \
741 { \
742         return dev->ids.field; \
743 }
744
745 PRODUCT_GETTER(product_id, product); /* DEPRECATED */
746 PRODUCT_GETTER(vendor_id, vendor); /* DEPRECATED */
747 PRODUCT_GETTER(bustype, bustype); /* DEPRECATED */
748 PRODUCT_GETTER(version, version); /* DEPRECATED */
749
750 PRODUCT_GETTER(id_product, product);
751 PRODUCT_GETTER(id_vendor, vendor);
752 PRODUCT_GETTER(id_bustype, bustype);
753 PRODUCT_GETTER(id_version, version);
754
755 #define PRODUCT_SETTER(field) \
756 void libevdev_set_id_##field(struct libevdev *dev, int field) \
757 { \
758         dev->ids.field = field;\
759 }
760
761 PRODUCT_SETTER(product);
762 PRODUCT_SETTER(vendor);
763 PRODUCT_SETTER(bustype);
764 PRODUCT_SETTER(version);
765
766 int libevdev_get_driver_version(const struct libevdev *dev)
767 {
768         return dev->driver_version;
769 }
770
771 int
772 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
773 {
774         return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
775 }
776
777 int
778 libevdev_enable_property(struct libevdev *dev, unsigned int prop)
779 {
780         if (prop > INPUT_PROP_MAX)
781                 return -1;
782
783         set_bit(dev->props, prop);
784         return 0;
785 }
786
787 int
788 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
789 {
790         return (type <= EV_MAX) && bit_is_set(dev->bits, type);
791 }
792
793 int
794 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
795 {
796         const unsigned long *mask;
797         int max;
798
799         if (!libevdev_has_event_type(dev, type))
800                 return 0;
801
802         if (type == EV_SYN)
803                 return 1;
804
805         max = type_to_mask_const(dev, type, &mask);
806
807         if (max == -1 || code > max)
808                 return 0;
809
810         return bit_is_set(mask, code);
811 }
812
813 int
814 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
815 {
816         int value;
817
818         if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
819                 return 0;
820
821         switch (type) {
822                 case EV_ABS: value = dev->abs_info[code].value; break;
823                 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
824                 case EV_LED: value = bit_is_set(dev->led_values, code); break;
825                 default:
826                         value = 0;
827                         break;
828         }
829
830         return value;
831 }
832
833 int libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value)
834 {
835         int rc = 0;
836         struct input_event e;
837
838         if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
839                 return -1;
840
841         e.type = type;
842         e.code = code;
843         e.value = value;
844
845         switch(type) {
846                 case EV_ABS: rc = update_abs_state(dev, &e); break;
847                 case EV_KEY: rc = update_key_state(dev, &e); break;
848                 case EV_LED: rc = update_led_state(dev, &e); break;
849                 default:
850                              rc = -1;
851                              break;
852         }
853
854         return rc;
855 }
856
857 int
858 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
859 {
860         if (libevdev_has_event_type(dev, type) &&
861             libevdev_has_event_code(dev, type, code)) {
862                 *value = libevdev_get_event_value(dev, type, code);
863                 return 1;
864         } else
865                 return 0;
866 }
867
868 int
869 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
870 {
871         if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
872                 return 0;
873
874         if (slot >= dev->num_slots || slot >= MAX_SLOTS)
875                 return 0;
876
877         if (code > ABS_MT_MAX || code < ABS_MT_MIN)
878                 return 0;
879
880         return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
881 }
882
883 int
884 libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value)
885 {
886         if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
887                 return -1;
888
889         if (slot >= dev->num_slots || slot >= MAX_SLOTS)
890                 return -1;
891
892         if (code > ABS_MT_MAX || code < ABS_MT_MIN)
893                 return -1;
894
895         if (code == ABS_MT_SLOT) {
896                 if (value < 0 || value >= libevdev_get_num_slots(dev))
897                         return -1;
898                 dev->current_slot = value;
899         }
900
901         dev->mt_slot_vals[slot][code - ABS_MT_MIN] = value;
902
903
904         return 0;
905 }
906
907 int
908 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
909 {
910         if (libevdev_has_event_type(dev, EV_ABS) &&
911             libevdev_has_event_code(dev, EV_ABS, code) &&
912             slot < dev->num_slots && slot < MAX_SLOTS) {
913                 *value = libevdev_get_slot_value(dev, slot, code);
914                 return 1;
915         } else
916                 return 0;
917 }
918
919 int
920 libevdev_get_num_slots(const struct libevdev *dev)
921 {
922         return dev->num_slots;
923 }
924
925 int
926 libevdev_get_current_slot(const struct libevdev *dev)
927 {
928         return dev->current_slot;
929 }
930
931 const struct input_absinfo*
932 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
933 {
934         if (!libevdev_has_event_type(dev, EV_ABS) ||
935             !libevdev_has_event_code(dev, EV_ABS, code))
936                 return NULL;
937
938         return &dev->abs_info[code];
939 }
940
941 #define ABS_GETTER(name, field) \
942 int libevdev_get_abs_##name(const struct libevdev *dev, unsigned int code) \
943 { \
944         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code); \
945         return absinfo ? absinfo->field : 0; \
946 }
947
948 ABS_GETTER(max, maximum); /* DEPRECATED */
949 ABS_GETTER(min, minimum); /* DEPRECATED */
950 ABS_GETTER(maximum, maximum);
951 ABS_GETTER(minimum, minimum);
952 ABS_GETTER(fuzz, fuzz)
953 ABS_GETTER(flat, flat)
954 ABS_GETTER(resolution, resolution)
955
956 #define ABS_SETTER(field) \
957 void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
958 { \
959         if (!libevdev_has_event_code(dev, EV_ABS, code)) \
960                 return; \
961         dev->abs_info[code].field = val; \
962 }
963
964 ABS_SETTER(maximum)
965 ABS_SETTER(minimum)
966 ABS_SETTER(fuzz)
967 ABS_SETTER(flat)
968 ABS_SETTER(resolution)
969
970 void libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
971 {
972         if (!libevdev_has_event_code(dev, EV_ABS, code))
973                 return;
974
975         dev->abs_info[code] = *abs;
976 }
977
978 int
979 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
980 {
981         if (type > EV_MAX)
982                 return -1;
983
984         if (libevdev_has_event_type(dev, type))
985                 return 0;
986
987         set_bit(dev->bits, type);
988
989         if (type == EV_REP) {
990                 int delay = 0, period = 0;
991                 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
992                 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
993         }
994         return 0;
995 }
996
997 int
998 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
999 {
1000         if (type > EV_MAX || type == EV_SYN)
1001                 return -1;
1002
1003         clear_bit(dev->bits, type);
1004
1005         return 0;
1006 }
1007
1008 int
1009 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
1010                            unsigned int code, const void *data)
1011 {
1012         unsigned int max;
1013         unsigned long *mask;
1014
1015         if (libevdev_enable_event_type(dev, type))
1016                 return -1;
1017
1018         switch(type) {
1019                 case EV_SYN:
1020                         return 0;
1021                 case EV_ABS:
1022                 case EV_REP:
1023                         if (data == NULL)
1024                                 return -1;
1025                         break;
1026                 default:
1027                         if (data != NULL)
1028                                 return -1;
1029                         break;
1030         }
1031
1032         max = type_to_mask(dev, type, &mask);
1033
1034         if (code > max)
1035                 return -1;
1036
1037         set_bit(mask, code);
1038
1039         if (type == EV_ABS) {
1040                 const struct input_absinfo *abs = data;
1041                 dev->abs_info[code] = *abs;
1042         } else if (type == EV_REP) {
1043                 const int *value = data;
1044                 dev->rep_values[code] = *value;
1045         }
1046
1047         return 0;
1048 }
1049
1050 int
1051 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
1052 {
1053         unsigned int max;
1054         unsigned long *mask;
1055
1056         if (type > EV_MAX)
1057                 return -1;
1058
1059         max = type_to_mask(dev, type, &mask);
1060
1061         if (code > max)
1062                 return -1;
1063
1064         clear_bit(mask, code);
1065
1066         return 0;
1067 }
1068
1069 /* DEPRECATED */
1070 int
1071 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1072 {
1073         return libevdev_kernel_set_abs_info(dev, code, abs);
1074 }
1075
1076 int
1077 libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1078 {
1079         int rc;
1080
1081         if (code > ABS_MAX)
1082                 return -EINVAL;
1083
1084         rc = ioctl(dev->fd, EVIOCSABS(code), abs);
1085         if (rc < 0)
1086                 rc = -errno;
1087         else
1088                 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
1089
1090         return rc;
1091 }
1092
1093 int
1094 libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
1095 {
1096         int rc = 0;
1097
1098         if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB)
1099                 return -EINVAL;
1100
1101         if (grab == dev->grabbed)
1102                 return 0;
1103
1104         if (grab == LIBEVDEV_GRAB)
1105                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
1106         else if (grab == LIBEVDEV_UNGRAB)
1107                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
1108
1109         if (rc == 0)
1110                 dev->grabbed = grab;
1111
1112         return rc < 0 ? -errno : 0;
1113 }
1114
1115 int
1116 libevdev_is_event_type(const struct input_event *ev, unsigned int type)
1117 {
1118         return type < EV_MAX && ev->type == type;
1119 }
1120
1121 int
1122 libevdev_is_event_code(const struct input_event *ev, unsigned int type, unsigned int code)
1123 {
1124         return type < EV_MAX &&
1125                 ev->type == type &&
1126                 (type == EV_SYN || code <= libevdev_get_event_type_max(type)) &&
1127                 ev->code == code;
1128 }
1129
1130 const char*
1131 libevdev_get_event_type_name(unsigned int type)
1132 {
1133         if (type > EV_MAX)
1134                 return NULL;
1135
1136         return ev_map[type];
1137 }
1138
1139 const char*
1140 libevdev_get_event_code_name(unsigned int type, unsigned int code)
1141 {
1142         if (type > EV_MAX)
1143                 return NULL;
1144
1145         if (code > ev_max[type])
1146                 return NULL;
1147
1148         return event_type_map[type][code];
1149 }
1150
1151 /* DEPRECATED */
1152 const char*
1153 libevdev_get_input_prop_name(unsigned int prop)
1154 {
1155         return libevdev_get_property_name(prop);
1156 }
1157
1158 const char*
1159 libevdev_get_property_name(unsigned int prop)
1160 {
1161         if (prop > INPUT_PROP_MAX)
1162                 return NULL;
1163
1164         return input_prop_map[prop];
1165 }
1166
1167 int
1168 libevdev_get_event_type_max(unsigned int type)
1169 {
1170         if (type > EV_MAX)
1171                 return -1;
1172
1173         return ev_max[type];
1174 }
1175
1176 int
1177 libevdev_get_repeat(struct libevdev *dev, int *delay, int *period)
1178 {
1179         if (!libevdev_has_event_type(dev, EV_REP))
1180                 return -1;
1181
1182         if (delay != NULL)
1183                 *delay = dev->rep_values[REP_DELAY];
1184         if (period != NULL)
1185                 *period = dev->rep_values[REP_PERIOD];
1186
1187         return 0;
1188 }
1189
1190 int
1191 libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value)
1192 {
1193         return libevdev_kernel_set_led_values(dev, code, value, -1);
1194 }
1195
1196 int
1197 libevdev_kernel_set_led_values(struct libevdev *dev, ...)
1198 {
1199         struct input_event ev[LED_MAX + 1];
1200         enum libevdev_led_value val;
1201         va_list args;
1202         int code;
1203         int rc = 0;
1204         size_t nleds = 0;
1205
1206         memset(ev, 0, sizeof(ev));
1207
1208         va_start(args, dev);
1209         code = va_arg(args, unsigned int);
1210         while (code != -1) {
1211                 if (code > LED_MAX) {
1212                         rc = -EINVAL;
1213                         break;
1214                 }
1215                 val = va_arg(args, enum libevdev_led_value);
1216                 if (val != LIBEVDEV_LED_ON && val != LIBEVDEV_LED_OFF) {
1217                         rc = -EINVAL;
1218                         break;
1219                 }
1220
1221                 if (libevdev_has_event_code(dev, EV_LED, code)) {
1222                         struct input_event *e = ev;
1223
1224                         while (e->type > 0 && e->code != code)
1225                                 e++;
1226
1227                         if (e->type == 0)
1228                                 nleds++;
1229                         e->type = EV_LED;
1230                         e->code = code;
1231                         e->value = (val == LIBEVDEV_LED_ON);
1232                 }
1233                 code = va_arg(args, unsigned int);
1234         }
1235         va_end(args);
1236
1237         if (rc == 0 && nleds > 0) {
1238                 ev[nleds].type = EV_SYN;
1239                 ev[nleds++].code = SYN_REPORT;
1240
1241                 rc = write(libevdev_get_fd(dev), ev, nleds * sizeof(ev[0]));
1242                 if (rc > 0) {
1243                         nleds--; /* last is EV_SYN */
1244                         while (nleds--)
1245                                 update_led_state(dev, &ev[nleds]);
1246                 }
1247                 rc = (rc != -1) ? 0 : -errno;
1248         }
1249
1250         return rc;
1251 }