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