Mark all external symbols with LIBEVDEV_EXPORT
[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 -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 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 = 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 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)];
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 LIBEVDEV_EXPORT int
658 libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
659 {
660         int rc = 0;
661
662         if (dev->fd < 0)
663                 return -ENODEV;
664
665         if (!(flags & (LIBEVDEV_READ_NORMAL|LIBEVDEV_READ_SYNC|LIBEVDEV_FORCE_SYNC)))
666                 return -EINVAL;
667
668         if (flags & LIBEVDEV_READ_SYNC) {
669                 if (dev->sync_state == SYNC_NEEDED) {
670                         rc = sync_state(dev);
671                         if (rc != 0)
672                                 return rc;
673                         dev->sync_state = SYNC_IN_PROGRESS;
674                 }
675
676                 if (dev->queue_nsync == 0) {
677                         dev->sync_state = SYNC_NONE;
678                         return -EAGAIN;
679                 }
680
681         } else if (dev->sync_state != SYNC_NONE) {
682                 struct input_event e;
683
684                 /* call update_state for all events here, otherwise the library has the wrong view
685                    of the device too */
686                 while (queue_shift(dev, &e) == 0) {
687                         dev->queue_nsync--;
688                         update_state(dev, &e);
689                 }
690
691                 dev->sync_state = SYNC_NONE;
692         }
693
694         /* FIXME: if the first event after SYNC_IN_PROGRESS is a SYN_DROPPED, log this */
695
696         /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
697            worst case we don't read fast enough and end up with SYN_DROPPED anyway.
698
699            Except if the fd is in blocking mode and we still have events from the last read, don't
700            read in any more.
701          */
702         do {
703                 if (!(flags & LIBEVDEV_READ_BLOCKING) ||
704                     queue_num_elements(dev) == 0) {
705                         rc = read_more_events(dev);
706                         if (rc < 0 && rc != -EAGAIN)
707                                 goto out;
708                 }
709
710                 if (flags & LIBEVDEV_FORCE_SYNC) {
711                         dev->sync_state = SYNC_NEEDED;
712                         rc = 1;
713                         goto out;
714                 }
715
716
717                 if (queue_shift(dev, ev) != 0)
718                         return -EAGAIN;
719
720                 update_state(dev, ev);
721
722         /* if we disabled a code, get the next event instead */
723         } while(!libevdev_has_event_code(dev, ev->type, ev->code));
724
725         rc = 0;
726         if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
727                 dev->sync_state = SYNC_NEEDED;
728                 rc = 1;
729         }
730
731         if (flags & LIBEVDEV_READ_SYNC && dev->queue_nsync > 0) {
732                 dev->queue_nsync--;
733                 rc = 1;
734                 if (dev->queue_nsync == 0)
735                         dev->sync_state = SYNC_NONE;
736         }
737
738 out:
739         return rc;
740 }
741
742 LIBEVDEV_EXPORT int
743 libevdev_has_event_pending(struct libevdev *dev)
744 {
745         struct pollfd fds = { dev->fd, POLLIN, 0 };
746         int rc;
747
748         if (dev->fd < 0)
749                 return -EBADF;
750
751         if (queue_num_elements(dev) != 0)
752                 return 1;
753
754         rc = poll(&fds, 1, 0);
755         return (rc >= 0) ? rc : -errno;
756 }
757
758 LIBEVDEV_EXPORT const char *
759 libevdev_get_name(const struct libevdev *dev)
760 {
761         return dev->name ? dev->name : "";
762 }
763
764 LIBEVDEV_EXPORT const char *
765 libevdev_get_phys(const struct libevdev *dev)
766 {
767         return dev->phys;
768 }
769
770 LIBEVDEV_EXPORT const char *
771 libevdev_get_uniq(const struct libevdev *dev)
772 {
773         return dev->uniq;
774 }
775
776 #define STRING_SETTER(field) \
777 LIBEVDEV_EXPORT void libevdev_set_##field(struct libevdev *dev, const char *field) \
778 { \
779         if (field == NULL) \
780                 return; \
781         free(dev->field); \
782         dev->field = strdup(field); \
783 }
784
785 STRING_SETTER(name);
786 STRING_SETTER(phys);
787 STRING_SETTER(uniq);
788
789
790 #define PRODUCT_GETTER(name) \
791 LIBEVDEV_EXPORT int libevdev_get_id_##name(const struct libevdev *dev) \
792 { \
793         return dev->ids.name; \
794 }
795
796 PRODUCT_GETTER(product);
797 PRODUCT_GETTER(vendor);
798 PRODUCT_GETTER(bustype);
799 PRODUCT_GETTER(version);
800
801 #define PRODUCT_SETTER(field) \
802 LIBEVDEV_EXPORT void libevdev_set_id_##field(struct libevdev *dev, int field) \
803 { \
804         dev->ids.field = field;\
805 }
806
807 PRODUCT_SETTER(product);
808 PRODUCT_SETTER(vendor);
809 PRODUCT_SETTER(bustype);
810 PRODUCT_SETTER(version);
811
812 LIBEVDEV_EXPORT int
813 libevdev_get_driver_version(const struct libevdev *dev)
814 {
815         return dev->driver_version;
816 }
817
818 LIBEVDEV_EXPORT int
819 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
820 {
821         return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
822 }
823
824 LIBEVDEV_EXPORT int
825 libevdev_enable_property(struct libevdev *dev, unsigned int prop)
826 {
827         if (prop > INPUT_PROP_MAX)
828                 return -1;
829
830         set_bit(dev->props, prop);
831         return 0;
832 }
833
834 LIBEVDEV_EXPORT int
835 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
836 {
837         return (type <= EV_MAX) && bit_is_set(dev->bits, type);
838 }
839
840 LIBEVDEV_EXPORT int
841 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
842 {
843         const unsigned long *mask;
844         int max;
845
846         if (!libevdev_has_event_type(dev, type))
847                 return 0;
848
849         if (type == EV_SYN)
850                 return 1;
851
852         max = type_to_mask_const(dev, type, &mask);
853
854         if (max == -1 || code > (unsigned int)max)
855                 return 0;
856
857         return bit_is_set(mask, code);
858 }
859
860 LIBEVDEV_EXPORT int
861 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
862 {
863         int value;
864
865         if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
866                 return 0;
867
868         switch (type) {
869                 case EV_ABS: value = dev->abs_info[code].value; break;
870                 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
871                 case EV_LED: value = bit_is_set(dev->led_values, code); break;
872                 case EV_SW: value = bit_is_set(dev->sw_values, code); break;
873                 default:
874                         value = 0;
875                         break;
876         }
877
878         return value;
879 }
880
881 LIBEVDEV_EXPORT int
882 libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value)
883 {
884         int rc = 0;
885         struct input_event e;
886
887         if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
888                 return -1;
889
890         e.type = type;
891         e.code = code;
892         e.value = value;
893
894         switch(type) {
895                 case EV_ABS: rc = update_abs_state(dev, &e); break;
896                 case EV_KEY: rc = update_key_state(dev, &e); break;
897                 case EV_LED: rc = update_led_state(dev, &e); break;
898                 case EV_SW: rc = update_sw_state(dev, &e); break;
899                 default:
900                              rc = -1;
901                              break;
902         }
903
904         return rc;
905 }
906
907 LIBEVDEV_EXPORT int
908 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
909 {
910         if (libevdev_has_event_type(dev, type) &&
911             libevdev_has_event_code(dev, type, code)) {
912                 *value = libevdev_get_event_value(dev, type, code);
913                 return 1;
914         } else
915                 return 0;
916 }
917
918 LIBEVDEV_EXPORT int
919 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
920 {
921         if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
922                 return 0;
923
924         if (dev->num_slots < 0 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
925                 return 0;
926
927         if (code > ABS_MT_MAX || code < ABS_MT_MIN)
928                 return 0;
929
930         return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
931 }
932
933 LIBEVDEV_EXPORT int
934 libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value)
935 {
936         if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
937                 return -1;
938
939         if (slot >= dev->num_slots || slot >= MAX_SLOTS)
940                 return -1;
941
942         if (code > ABS_MT_MAX || code < ABS_MT_MIN)
943                 return -1;
944
945         if (code == ABS_MT_SLOT) {
946                 if (value < 0 || value >= libevdev_get_num_slots(dev))
947                         return -1;
948                 dev->current_slot = value;
949         }
950
951         dev->mt_slot_vals[slot][code - ABS_MT_MIN] = value;
952
953
954         return 0;
955 }
956
957 LIBEVDEV_EXPORT int
958 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
959 {
960         if (libevdev_has_event_type(dev, EV_ABS) &&
961             libevdev_has_event_code(dev, EV_ABS, code) &&
962             dev->num_slots >= 0 &&
963             slot < (unsigned int)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 LIBEVDEV_EXPORT int
971 libevdev_get_num_slots(const struct libevdev *dev)
972 {
973         return dev->num_slots;
974 }
975
976 LIBEVDEV_EXPORT int
977 libevdev_get_current_slot(const struct libevdev *dev)
978 {
979         return dev->current_slot;
980 }
981
982 LIBEVDEV_EXPORT 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) \
993 LIBEVDEV_EXPORT 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->name : 0; \
997 }
998
999 ABS_GETTER(maximum);
1000 ABS_GETTER(minimum);
1001 ABS_GETTER(fuzz);
1002 ABS_GETTER(flat);
1003 ABS_GETTER(resolution);
1004
1005 #define ABS_SETTER(field) \
1006 LIBEVDEV_EXPORT void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
1007 { \
1008         if (!libevdev_has_event_code(dev, EV_ABS, code)) \
1009                 return; \
1010         dev->abs_info[code].field = val; \
1011 }
1012
1013 ABS_SETTER(maximum)
1014 ABS_SETTER(minimum)
1015 ABS_SETTER(fuzz)
1016 ABS_SETTER(flat)
1017 ABS_SETTER(resolution)
1018
1019 LIBEVDEV_EXPORT void
1020 libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1021 {
1022         if (!libevdev_has_event_code(dev, EV_ABS, code))
1023                 return;
1024
1025         dev->abs_info[code] = *abs;
1026 }
1027
1028 LIBEVDEV_EXPORT int
1029 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
1030 {
1031         if (type > EV_MAX)
1032                 return -1;
1033
1034         if (libevdev_has_event_type(dev, type))
1035                 return 0;
1036
1037         set_bit(dev->bits, type);
1038
1039         if (type == EV_REP) {
1040                 int delay = 0, period = 0;
1041                 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
1042                 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
1043         }
1044         return 0;
1045 }
1046
1047 LIBEVDEV_EXPORT int
1048 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
1049 {
1050         if (type > EV_MAX || type == EV_SYN)
1051                 return -1;
1052
1053         clear_bit(dev->bits, type);
1054
1055         return 0;
1056 }
1057
1058 LIBEVDEV_EXPORT int
1059 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
1060                            unsigned int code, const void *data)
1061 {
1062         unsigned int max;
1063         unsigned long *mask = NULL;
1064
1065         if (libevdev_enable_event_type(dev, type))
1066                 return -1;
1067
1068         switch(type) {
1069                 case EV_SYN:
1070                         return 0;
1071                 case EV_ABS:
1072                 case EV_REP:
1073                         if (data == NULL)
1074                                 return -1;
1075                         break;
1076                 default:
1077                         if (data != NULL)
1078                                 return -1;
1079                         break;
1080         }
1081
1082         max = type_to_mask(dev, type, &mask);
1083
1084         if (code > max)
1085                 return -1;
1086
1087         set_bit(mask, code);
1088
1089         if (type == EV_ABS) {
1090                 const struct input_absinfo *abs = data;
1091                 dev->abs_info[code] = *abs;
1092         } else if (type == EV_REP) {
1093                 const int *value = data;
1094                 dev->rep_values[code] = *value;
1095         }
1096
1097         return 0;
1098 }
1099
1100 LIBEVDEV_EXPORT int
1101 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
1102 {
1103         unsigned int max;
1104         unsigned long *mask = NULL;
1105
1106         if (type > EV_MAX)
1107                 return -1;
1108
1109         max = type_to_mask(dev, type, &mask);
1110
1111         if (code > max)
1112                 return -1;
1113
1114         clear_bit(mask, code);
1115
1116         return 0;
1117 }
1118
1119 LIBEVDEV_EXPORT int
1120 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1121 {
1122         return libevdev_kernel_set_abs_info(dev, code, abs);
1123 }
1124
1125 LIBEVDEV_EXPORT int
1126 libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1127 {
1128         int rc;
1129
1130         if (code > ABS_MAX)
1131                 return -EINVAL;
1132
1133         rc = ioctl(dev->fd, EVIOCSABS(code), abs);
1134         if (rc < 0)
1135                 rc = -errno;
1136         else
1137                 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
1138
1139         return rc;
1140 }
1141
1142 LIBEVDEV_EXPORT int
1143 libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
1144 {
1145         int rc = 0;
1146
1147         if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB)
1148                 return -EINVAL;
1149
1150         if (grab == dev->grabbed)
1151                 return 0;
1152
1153         if (grab == LIBEVDEV_GRAB)
1154                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
1155         else if (grab == LIBEVDEV_UNGRAB)
1156                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
1157
1158         if (rc == 0)
1159                 dev->grabbed = grab;
1160
1161         return rc < 0 ? -errno : 0;
1162 }
1163
1164 LIBEVDEV_EXPORT int
1165 libevdev_is_event_type(const struct input_event *ev, unsigned int type)
1166 {
1167         return type < EV_CNT && ev->type == type;
1168 }
1169
1170 LIBEVDEV_EXPORT int
1171 libevdev_is_event_code(const struct input_event *ev, unsigned int type, unsigned int code)
1172 {
1173         int max;
1174
1175         if (!libevdev_is_event_type(ev, type))
1176                 return 0;
1177
1178         max = libevdev_get_event_type_max(type);
1179         return (max > -1 && code <= (unsigned int)max && ev->code == code);
1180 }
1181
1182 LIBEVDEV_EXPORT const char*
1183 libevdev_get_event_type_name(unsigned int type)
1184 {
1185         if (type > EV_MAX)
1186                 return NULL;
1187
1188         return ev_map[type];
1189 }
1190
1191 LIBEVDEV_EXPORT const char*
1192 libevdev_get_event_code_name(unsigned int type, unsigned int code)
1193 {
1194         int max = libevdev_get_event_type_max(type);
1195
1196         if (max == -1 || code > (unsigned int)max)
1197                 return NULL;
1198
1199         return event_type_map[type][code];
1200 }
1201
1202 LIBEVDEV_EXPORT const char*
1203 libevdev_get_property_name(unsigned int prop)
1204 {
1205         if (prop > INPUT_PROP_MAX)
1206                 return NULL;
1207
1208         return input_prop_map[prop];
1209 }
1210
1211 LIBEVDEV_EXPORT int
1212 libevdev_get_event_type_max(unsigned int type)
1213 {
1214         if (type > EV_MAX)
1215                 return -1;
1216
1217         return ev_max[type];
1218 }
1219
1220 LIBEVDEV_EXPORT int
1221 libevdev_get_repeat(struct libevdev *dev, int *delay, int *period)
1222 {
1223         if (!libevdev_has_event_type(dev, EV_REP))
1224                 return -1;
1225
1226         if (delay != NULL)
1227                 *delay = dev->rep_values[REP_DELAY];
1228         if (period != NULL)
1229                 *period = dev->rep_values[REP_PERIOD];
1230
1231         return 0;
1232 }
1233
1234 LIBEVDEV_EXPORT int
1235 libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value)
1236 {
1237         return libevdev_kernel_set_led_values(dev, code, value, -1);
1238 }
1239
1240 LIBEVDEV_EXPORT int
1241 libevdev_kernel_set_led_values(struct libevdev *dev, ...)
1242 {
1243         struct input_event ev[LED_MAX + 1];
1244         enum libevdev_led_value val;
1245         va_list args;
1246         int code;
1247         int rc = 0;
1248         size_t nleds = 0;
1249
1250         memset(ev, 0, sizeof(ev));
1251
1252         va_start(args, dev);
1253         code = va_arg(args, unsigned int);
1254         while (code != -1) {
1255                 if (code > LED_MAX) {
1256                         rc = -EINVAL;
1257                         break;
1258                 }
1259                 val = va_arg(args, enum libevdev_led_value);
1260                 if (val != LIBEVDEV_LED_ON && val != LIBEVDEV_LED_OFF) {
1261                         rc = -EINVAL;
1262                         break;
1263                 }
1264
1265                 if (libevdev_has_event_code(dev, EV_LED, code)) {
1266                         struct input_event *e = ev;
1267
1268                         while (e->type > 0 && e->code != code)
1269                                 e++;
1270
1271                         if (e->type == 0)
1272                                 nleds++;
1273                         e->type = EV_LED;
1274                         e->code = code;
1275                         e->value = (val == LIBEVDEV_LED_ON);
1276                 }
1277                 code = va_arg(args, unsigned int);
1278         }
1279         va_end(args);
1280
1281         if (rc == 0 && nleds > 0) {
1282                 ev[nleds].type = EV_SYN;
1283                 ev[nleds++].code = SYN_REPORT;
1284
1285                 rc = write(libevdev_get_fd(dev), ev, nleds * sizeof(ev[0]));
1286                 if (rc > 0) {
1287                         nleds--; /* last is EV_SYN */
1288                         while (nleds--)
1289                                 update_led_state(dev, &ev[nleds]);
1290                 }
1291                 rc = (rc != -1) ? 0 : -errno;
1292         }
1293
1294         return rc;
1295 }