Fix some compiler warnings about maybe uninitialized values
[platform/upstream/libevdev.git] / libevdev / libevdev.c
1 /*
2  * Copyright © 2013 Red Hat, Inc.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #include <config.h>
24 #include <errno.h>
25 #include <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 = {{0,0}, 0, 0, 0};
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) \
789 int libevdev_get_id_##name(const struct libevdev *dev) \
790 { \
791         return dev->ids.name; \
792 }
793
794 PRODUCT_GETTER(product);
795 PRODUCT_GETTER(vendor);
796 PRODUCT_GETTER(bustype);
797 PRODUCT_GETTER(version);
798
799 #define PRODUCT_SETTER(field) \
800 void libevdev_set_id_##field(struct libevdev *dev, int field) \
801 { \
802         dev->ids.field = field;\
803 }
804
805 PRODUCT_SETTER(product);
806 PRODUCT_SETTER(vendor);
807 PRODUCT_SETTER(bustype);
808 PRODUCT_SETTER(version);
809
810 int libevdev_get_driver_version(const struct libevdev *dev)
811 {
812         return dev->driver_version;
813 }
814
815 int
816 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
817 {
818         return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
819 }
820
821 int
822 libevdev_enable_property(struct libevdev *dev, unsigned int prop)
823 {
824         if (prop > INPUT_PROP_MAX)
825                 return -1;
826
827         set_bit(dev->props, prop);
828         return 0;
829 }
830
831 int
832 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
833 {
834         return (type <= EV_MAX) && bit_is_set(dev->bits, type);
835 }
836
837 int
838 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
839 {
840         const unsigned long *mask;
841         int max;
842
843         if (!libevdev_has_event_type(dev, type))
844                 return 0;
845
846         if (type == EV_SYN)
847                 return 1;
848
849         max = type_to_mask_const(dev, type, &mask);
850
851         if (max == -1 || code > (unsigned int)max)
852                 return 0;
853
854         return bit_is_set(mask, code);
855 }
856
857 int
858 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
859 {
860         int value;
861
862         if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
863                 return 0;
864
865         switch (type) {
866                 case EV_ABS: value = dev->abs_info[code].value; break;
867                 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
868                 case EV_LED: value = bit_is_set(dev->led_values, code); break;
869                 case EV_SW: value = bit_is_set(dev->sw_values, code); break;
870                 default:
871                         value = 0;
872                         break;
873         }
874
875         return value;
876 }
877
878 int libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value)
879 {
880         int rc = 0;
881         struct input_event e;
882
883         if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
884                 return -1;
885
886         e.type = type;
887         e.code = code;
888         e.value = value;
889
890         switch(type) {
891                 case EV_ABS: rc = update_abs_state(dev, &e); break;
892                 case EV_KEY: rc = update_key_state(dev, &e); break;
893                 case EV_LED: rc = update_led_state(dev, &e); break;
894                 case EV_SW: rc = update_sw_state(dev, &e); break;
895                 default:
896                              rc = -1;
897                              break;
898         }
899
900         return rc;
901 }
902
903 int
904 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
905 {
906         if (libevdev_has_event_type(dev, type) &&
907             libevdev_has_event_code(dev, type, code)) {
908                 *value = libevdev_get_event_value(dev, type, code);
909                 return 1;
910         } else
911                 return 0;
912 }
913
914 int
915 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
916 {
917         if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
918                 return 0;
919
920         if (dev->num_slots < 0 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
921                 return 0;
922
923         if (code > ABS_MT_MAX || code < ABS_MT_MIN)
924                 return 0;
925
926         return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
927 }
928
929 int
930 libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value)
931 {
932         if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
933                 return -1;
934
935         if (slot >= dev->num_slots || slot >= MAX_SLOTS)
936                 return -1;
937
938         if (code > ABS_MT_MAX || code < ABS_MT_MIN)
939                 return -1;
940
941         if (code == ABS_MT_SLOT) {
942                 if (value < 0 || value >= libevdev_get_num_slots(dev))
943                         return -1;
944                 dev->current_slot = value;
945         }
946
947         dev->mt_slot_vals[slot][code - ABS_MT_MIN] = value;
948
949
950         return 0;
951 }
952
953 int
954 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
955 {
956         if (libevdev_has_event_type(dev, EV_ABS) &&
957             libevdev_has_event_code(dev, EV_ABS, code) &&
958             dev->num_slots >= 0 &&
959             slot < (unsigned int)dev->num_slots && slot < MAX_SLOTS) {
960                 *value = libevdev_get_slot_value(dev, slot, code);
961                 return 1;
962         } else
963                 return 0;
964 }
965
966 int
967 libevdev_get_num_slots(const struct libevdev *dev)
968 {
969         return dev->num_slots;
970 }
971
972 int
973 libevdev_get_current_slot(const struct libevdev *dev)
974 {
975         return dev->current_slot;
976 }
977
978 const struct input_absinfo*
979 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
980 {
981         if (!libevdev_has_event_type(dev, EV_ABS) ||
982             !libevdev_has_event_code(dev, EV_ABS, code))
983                 return NULL;
984
985         return &dev->abs_info[code];
986 }
987
988 #define ABS_GETTER(name) \
989 int libevdev_get_abs_##name(const struct libevdev *dev, unsigned int code) \
990 { \
991         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code); \
992         return absinfo ? absinfo->name : 0; \
993 }
994
995 ABS_GETTER(maximum);
996 ABS_GETTER(minimum);
997 ABS_GETTER(fuzz);
998 ABS_GETTER(flat);
999 ABS_GETTER(resolution);
1000
1001 #define ABS_SETTER(field) \
1002 void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
1003 { \
1004         if (!libevdev_has_event_code(dev, EV_ABS, code)) \
1005                 return; \
1006         dev->abs_info[code].field = val; \
1007 }
1008
1009 ABS_SETTER(maximum)
1010 ABS_SETTER(minimum)
1011 ABS_SETTER(fuzz)
1012 ABS_SETTER(flat)
1013 ABS_SETTER(resolution)
1014
1015 void libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1016 {
1017         if (!libevdev_has_event_code(dev, EV_ABS, code))
1018                 return;
1019
1020         dev->abs_info[code] = *abs;
1021 }
1022
1023 int
1024 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
1025 {
1026         if (type > EV_MAX)
1027                 return -1;
1028
1029         if (libevdev_has_event_type(dev, type))
1030                 return 0;
1031
1032         set_bit(dev->bits, type);
1033
1034         if (type == EV_REP) {
1035                 int delay = 0, period = 0;
1036                 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
1037                 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
1038         }
1039         return 0;
1040 }
1041
1042 int
1043 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
1044 {
1045         if (type > EV_MAX || type == EV_SYN)
1046                 return -1;
1047
1048         clear_bit(dev->bits, type);
1049
1050         return 0;
1051 }
1052
1053 int
1054 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
1055                            unsigned int code, const void *data)
1056 {
1057         unsigned int max;
1058         unsigned long *mask = NULL;
1059
1060         if (libevdev_enable_event_type(dev, type))
1061                 return -1;
1062
1063         switch(type) {
1064                 case EV_SYN:
1065                         return 0;
1066                 case EV_ABS:
1067                 case EV_REP:
1068                         if (data == NULL)
1069                                 return -1;
1070                         break;
1071                 default:
1072                         if (data != NULL)
1073                                 return -1;
1074                         break;
1075         }
1076
1077         max = type_to_mask(dev, type, &mask);
1078
1079         if (code > max)
1080                 return -1;
1081
1082         set_bit(mask, code);
1083
1084         if (type == EV_ABS) {
1085                 const struct input_absinfo *abs = data;
1086                 dev->abs_info[code] = *abs;
1087         } else if (type == EV_REP) {
1088                 const int *value = data;
1089                 dev->rep_values[code] = *value;
1090         }
1091
1092         return 0;
1093 }
1094
1095 int
1096 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
1097 {
1098         unsigned int max;
1099         unsigned long *mask = NULL;
1100
1101         if (type > EV_MAX)
1102                 return -1;
1103
1104         max = type_to_mask(dev, type, &mask);
1105
1106         if (code > max)
1107                 return -1;
1108
1109         clear_bit(mask, code);
1110
1111         return 0;
1112 }
1113
1114 /* DEPRECATED */
1115 int
1116 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1117 {
1118         return libevdev_kernel_set_abs_info(dev, code, abs);
1119 }
1120
1121 int
1122 libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1123 {
1124         int rc;
1125
1126         if (code > ABS_MAX)
1127                 return -EINVAL;
1128
1129         rc = ioctl(dev->fd, EVIOCSABS(code), abs);
1130         if (rc < 0)
1131                 rc = -errno;
1132         else
1133                 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
1134
1135         return rc;
1136 }
1137
1138 int
1139 libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
1140 {
1141         int rc = 0;
1142
1143         if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB)
1144                 return -EINVAL;
1145
1146         if (grab == dev->grabbed)
1147                 return 0;
1148
1149         if (grab == LIBEVDEV_GRAB)
1150                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
1151         else if (grab == LIBEVDEV_UNGRAB)
1152                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
1153
1154         if (rc == 0)
1155                 dev->grabbed = grab;
1156
1157         return rc < 0 ? -errno : 0;
1158 }
1159
1160 int
1161 libevdev_is_event_type(const struct input_event *ev, unsigned int type)
1162 {
1163         return type < EV_CNT && ev->type == type;
1164 }
1165
1166 int
1167 libevdev_is_event_code(const struct input_event *ev, unsigned int type, unsigned int code)
1168 {
1169         int max;
1170
1171         if (!libevdev_is_event_type(ev, type))
1172                 return 0;
1173
1174         max = libevdev_get_event_type_max(type);
1175         return (max > -1 && code <= (unsigned int)max && ev->code == code);
1176 }
1177
1178 const char*
1179 libevdev_get_event_type_name(unsigned int type)
1180 {
1181         if (type > EV_MAX)
1182                 return NULL;
1183
1184         return ev_map[type];
1185 }
1186
1187 const char*
1188 libevdev_get_event_code_name(unsigned int type, unsigned int code)
1189 {
1190         int max = libevdev_get_event_type_max(type);
1191
1192         if (max == -1 || code > (unsigned int)max)
1193                 return NULL;
1194
1195         return event_type_map[type][code];
1196 }
1197
1198 const char*
1199 libevdev_get_property_name(unsigned int prop)
1200 {
1201         if (prop > INPUT_PROP_MAX)
1202                 return NULL;
1203
1204         return input_prop_map[prop];
1205 }
1206
1207 int
1208 libevdev_get_event_type_max(unsigned int type)
1209 {
1210         if (type > EV_MAX)
1211                 return -1;
1212
1213         return ev_max[type];
1214 }
1215
1216 int
1217 libevdev_get_repeat(struct libevdev *dev, int *delay, int *period)
1218 {
1219         if (!libevdev_has_event_type(dev, EV_REP))
1220                 return -1;
1221
1222         if (delay != NULL)
1223                 *delay = dev->rep_values[REP_DELAY];
1224         if (period != NULL)
1225                 *period = dev->rep_values[REP_PERIOD];
1226
1227         return 0;
1228 }
1229
1230 int
1231 libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value)
1232 {
1233         return libevdev_kernel_set_led_values(dev, code, value, -1);
1234 }
1235
1236 int
1237 libevdev_kernel_set_led_values(struct libevdev *dev, ...)
1238 {
1239         struct input_event ev[LED_MAX + 1];
1240         enum libevdev_led_value val;
1241         va_list args;
1242         int code;
1243         int rc = 0;
1244         size_t nleds = 0;
1245
1246         memset(ev, 0, sizeof(ev));
1247
1248         va_start(args, dev);
1249         code = va_arg(args, unsigned int);
1250         while (code != -1) {
1251                 if (code > LED_MAX) {
1252                         rc = -EINVAL;
1253                         break;
1254                 }
1255                 val = va_arg(args, enum libevdev_led_value);
1256                 if (val != LIBEVDEV_LED_ON && val != LIBEVDEV_LED_OFF) {
1257                         rc = -EINVAL;
1258                         break;
1259                 }
1260
1261                 if (libevdev_has_event_code(dev, EV_LED, code)) {
1262                         struct input_event *e = ev;
1263
1264                         while (e->type > 0 && e->code != code)
1265                                 e++;
1266
1267                         if (e->type == 0)
1268                                 nleds++;
1269                         e->type = EV_LED;
1270                         e->code = code;
1271                         e->value = (val == LIBEVDEV_LED_ON);
1272                 }
1273                 code = va_arg(args, unsigned int);
1274         }
1275         va_end(args);
1276
1277         if (rc == 0 && nleds > 0) {
1278                 ev[nleds].type = EV_SYN;
1279                 ev[nleds++].code = SYN_REPORT;
1280
1281                 rc = write(libevdev_get_fd(dev), ev, nleds * sizeof(ev[0]));
1282                 if (rc > 0) {
1283                         nleds--; /* last is EV_SYN */
1284                         while (nleds--)
1285                                 update_led_state(dev, &ev[nleds]);
1286                 }
1287                 rc = (rc != -1) ? 0 : -errno;
1288         }
1289
1290         return rc;
1291 }