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