tizen 2.4 release
[framework/uifw/libevdev.git] / libevdev / libevdev.c
1 /*
2  * Copyright © 2013 Red Hat, Inc.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #include <config.h>
24 #include <errno.h>
25 #include <poll.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <stdarg.h>
30 #include <stdbool.h>
31
32 #include "libevdev.h"
33 #include "libevdev-int.h"
34 #include "libevdev-util.h"
35 #include "event-names.h"
36
37 #define MAXEVENTS 64
38
39 enum event_filter_status {
40         EVENT_FILTER_NONE,      /**< Event untouched by filters */
41         EVENT_FILTER_MODIFIED,  /**< Event was modified */
42         EVENT_FILTER_DISCARD,   /**< Discard current event */
43 };
44
45 static int sync_mt_state(struct libevdev *dev, int create_events);
46
47 static inline int*
48 slot_value(const struct libevdev *dev, int slot, int axis)
49 {
50         if (unlikely(slot > dev->num_slots)) {
51                 log_bug("Slot %d exceeds number of slots (%d)\n", slot, dev->num_slots);
52                 slot = 0;
53         }
54         if (unlikely(axis < ABS_MT_MIN || axis > ABS_MT_MAX)) {
55                 log_bug("MT axis %d is outside the valid range [%d,%d]\n",
56                         axis, ABS_MT_MIN, ABS_MT_MAX);
57                 axis = ABS_MT_MIN;
58         }
59         return &dev->mt_slot_vals[slot * ABS_MT_CNT + axis - ABS_MT_MIN];
60 }
61
62 static int
63 init_event_queue(struct libevdev *dev)
64 {
65         const int MIN_QUEUE_SIZE = 256;
66         int nevents = 1; /* terminating SYN_REPORT */
67         int nslots;
68         unsigned int type, code;
69
70         /* count the number of axes, keys, etc. to get a better idea at how
71            many events per EV_SYN we could possibly get. That's the max we
72            may get during SYN_DROPPED too. Use double that, just so we have
73            room for events while syncing an event.
74          */
75         for (type = EV_KEY; type < EV_MAX; type++) {
76                 int max = libevdev_event_type_get_max(type);
77                 for (code = 0; max > 0 && code < (unsigned int) max; code++) {
78                         if (libevdev_has_event_code(dev, type, code))
79                                 nevents++;
80                 }
81         }
82
83         nslots = libevdev_get_num_slots(dev);
84         if (nslots > 1) {
85                 int num_mt_axes = 0;
86
87                 for (code = ABS_MT_SLOT; code < ABS_MAX; code++) {
88                         if (libevdev_has_event_code(dev, EV_ABS, code))
89                                 num_mt_axes++;
90                 }
91
92                 /* We already counted the first slot in the initial count */
93                 nevents += num_mt_axes * (nslots - 1);
94         }
95
96         return queue_alloc(dev, max(MIN_QUEUE_SIZE, nevents * 2));
97 }
98
99 static void
100 libevdev_dflt_log_func(enum libevdev_log_priority priority,
101                        void *data,
102                        const char *file, int line, const char *func,
103                        const char *format, va_list args)
104 {
105         const char *prefix;
106         switch(priority) {
107                 case LIBEVDEV_LOG_ERROR: prefix = "libevdev error"; break;
108                 case LIBEVDEV_LOG_INFO: prefix = "libevdev info"; break;
109                 case LIBEVDEV_LOG_DEBUG:
110                                         prefix = "libevdev debug";
111                                         break;
112                 default:
113                                         prefix = "libevdev INVALID LOG PRIORITY";
114                                         break;
115         }
116         /* default logging format:
117            libevev error in libevdev_some_func: blah blah
118            libevev info in libevdev_some_func: blah blah
119            libevev debug in file.c:123:libevdev_some_func: blah blah
120          */
121
122         fprintf(stderr, "%s in ", prefix);
123         if (priority == LIBEVDEV_LOG_DEBUG)
124                 fprintf(stderr, "%s:%d:", file, line);
125         fprintf(stderr, "%s: ", func);
126         vfprintf(stderr, format, args);
127 }
128
129 /*
130  * Global logging settings.
131  */
132 struct logdata log_data = {
133         LIBEVDEV_LOG_INFO,
134         libevdev_dflt_log_func,
135         NULL,
136 };
137
138 void
139 log_msg(enum libevdev_log_priority priority,
140         void *data,
141         const char *file, int line, const char *func,
142         const char *format, ...)
143 {
144         va_list args;
145
146         if (!log_data.handler || priority > log_data.priority)
147                 return;
148
149         va_start(args, format);
150         log_data.handler(priority, data, file, line, func, format, args);
151         va_end(args);
152 }
153
154 static void
155 libevdev_reset(struct libevdev *dev)
156 {
157         free(dev->name);
158         free(dev->phys);
159         free(dev->uniq);
160         free(dev->mt_slot_vals);
161         free(dev->mt_sync.mt_state);
162         free(dev->mt_sync.tracking_id_changes);
163         free(dev->mt_sync.slot_update);
164         memset(dev, 0, sizeof(*dev));
165         dev->fd = -1;
166         dev->initialized = false;
167         dev->num_slots = -1;
168         dev->current_slot = -1;
169         dev->grabbed = LIBEVDEV_UNGRAB;
170         dev->sync_state = SYNC_NONE;
171         libevdev_enable_event_type(dev, EV_SYN);
172 }
173
174 LIBEVDEV_EXPORT struct libevdev*
175 libevdev_new(void)
176 {
177         struct libevdev *dev;
178
179         dev = calloc(1, sizeof(*dev));
180         if (!dev)
181                 return NULL;
182
183         libevdev_reset(dev);
184
185         return dev;
186 }
187
188 LIBEVDEV_EXPORT int
189 libevdev_new_from_fd(int fd, struct libevdev **dev)
190 {
191         struct libevdev *d;
192         int rc;
193
194         d = libevdev_new();
195         if (!d)
196                 return -ENOMEM;
197
198         rc = libevdev_set_fd(d, fd);
199         if (rc < 0)
200                 libevdev_free(d);
201         else
202                 *dev = d;
203         return rc;
204 }
205
206 LIBEVDEV_EXPORT void
207 libevdev_free(struct libevdev *dev)
208 {
209         if (!dev)
210                 return;
211
212         queue_free(dev);
213         libevdev_reset(dev);
214         free(dev);
215 }
216
217 LIBEVDEV_EXPORT void
218 libevdev_set_log_function(libevdev_log_func_t logfunc, void *data)
219 {
220         log_data.handler = logfunc;
221         log_data.userdata = data;
222 }
223
224 LIBEVDEV_EXPORT void
225 libevdev_set_log_priority(enum libevdev_log_priority priority)
226 {
227         if (priority > LIBEVDEV_LOG_DEBUG)
228                 priority = LIBEVDEV_LOG_DEBUG;
229         log_data.priority = priority;
230 }
231
232 LIBEVDEV_EXPORT enum libevdev_log_priority
233 libevdev_get_log_priority(void)
234 {
235         return log_data.priority;
236 }
237
238 LIBEVDEV_EXPORT int
239 libevdev_change_fd(struct libevdev *dev, int fd)
240 {
241         if (!dev->initialized) {
242                 log_bug("device not initialized. call libevdev_set_fd() first\n");
243                 return -1;
244         }
245         dev->fd = fd;
246         return 0;
247 }
248
249 LIBEVDEV_EXPORT int
250 libevdev_set_fd(struct libevdev* dev, int fd)
251 {
252         int rc;
253         int i;
254         char buf[256];
255
256         if (dev->initialized) {
257                 log_bug("device already initialized.\n");
258                 return -EBADF;
259         } else if (fd < 0)
260                 return -EBADF;
261
262         libevdev_reset(dev);
263
264         rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
265         if (rc < 0)
266                 goto out;
267
268         memset(buf, 0, sizeof(buf));
269         rc = ioctl(fd, EVIOCGNAME(sizeof(buf) - 1), buf);
270         if (rc < 0)
271                 goto out;
272
273         free(dev->name);
274         dev->name = strdup(buf);
275         if (!dev->name) {
276                 errno = ENOMEM;
277                 goto out;
278         }
279
280         free(dev->phys);
281         dev->phys = NULL;
282         memset(buf, 0, sizeof(buf));
283         rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
284         if (rc < 0) {
285                 /* uinput has no phys */
286                 if (errno != ENOENT)
287                         goto out;
288         } else {
289                 dev->phys = strdup(buf);
290                 if (!dev->phys) {
291                         errno = ENOMEM;
292                         goto out;
293                 }
294         }
295
296         free(dev->uniq);
297         dev->uniq = NULL;
298         memset(buf, 0, sizeof(buf));
299         rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
300         if (rc < 0) {
301                 if (errno != ENOENT)
302                         goto out;
303         } else  {
304                 dev->uniq = strdup(buf);
305                 if (!dev->uniq) {
306                         errno = ENOMEM;
307                         goto out;
308                 }
309         }
310
311         rc = ioctl(fd, EVIOCGID, &dev->ids);
312         if (rc < 0)
313                 goto out;
314
315         rc = ioctl(fd, EVIOCGVERSION, &dev->driver_version);
316         if (rc < 0)
317                 goto out;
318
319         /* Built on a kernel with props, running against a kernel without property
320            support. This should not be a fatal case, we'll be missing properties but other
321            than that everything is as expected.
322          */
323         rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
324         if (rc < 0 && errno != EINVAL)
325                 goto out;
326
327         rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
328         if (rc < 0)
329                 goto out;
330
331         rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
332         if (rc < 0)
333                 goto out;
334
335         rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
336         if (rc < 0)
337                 goto out;
338
339         rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
340         if (rc < 0)
341                 goto out;
342
343         rc = ioctl(fd, EVIOCGBIT(EV_SW, sizeof(dev->sw_bits)), dev->sw_bits);
344         if (rc < 0)
345                 goto out;
346
347         rc = ioctl(fd, EVIOCGBIT(EV_MSC, sizeof(dev->msc_bits)), dev->msc_bits);
348         if (rc < 0)
349                 goto out;
350
351         rc = ioctl(fd, EVIOCGBIT(EV_FF, sizeof(dev->ff_bits)), dev->ff_bits);
352         if (rc < 0)
353                 goto out;
354
355         rc = ioctl(fd, EVIOCGBIT(EV_SND, sizeof(dev->snd_bits)), dev->snd_bits);
356         if (rc < 0)
357                 goto out;
358
359         rc = ioctl(fd, EVIOCGKEY(sizeof(dev->key_values)), dev->key_values);
360         if (rc < 0)
361                 goto out;
362
363         rc = ioctl(fd, EVIOCGLED(sizeof(dev->led_values)), dev->led_values);
364         if (rc < 0)
365                 goto out;
366
367         rc = ioctl(fd, EVIOCGSW(sizeof(dev->sw_values)), dev->sw_values);
368         if (rc < 0)
369                 goto out;
370
371         /* rep is a special case, always set it to 1 for both values if EV_REP is set */
372         if (bit_is_set(dev->bits, EV_REP)) {
373                 for (i = 0; i < REP_CNT; i++)
374                         set_bit(dev->rep_bits, i);
375                 rc = ioctl(fd, EVIOCGREP, dev->rep_values);
376                 if (rc < 0)
377                         goto out;
378         }
379
380         for (i = ABS_X; i <= ABS_MAX; i++) {
381                 if (bit_is_set(dev->abs_bits, i)) {
382                         struct input_absinfo abs_info;
383                         rc = ioctl(fd, EVIOCGABS(i), &abs_info);
384                         if (rc < 0)
385                                 goto out;
386
387                         dev->abs_info[i] = abs_info;
388                 }
389         }
390
391         dev->fd = fd;
392
393         /* devices with ABS_MT_SLOT - 1 aren't MT devices,
394            see the documentation for multitouch-related
395            functions for more details */
396         if (!libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT - 1) &&
397             libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT)) {
398                 const struct input_absinfo *abs_info;
399
400                 abs_info = libevdev_get_abs_info(dev, ABS_MT_SLOT);
401
402                 dev->num_slots = abs_info->maximum + 1;
403                 dev->mt_slot_vals = calloc(dev->num_slots * ABS_MT_CNT, sizeof(int));
404                 if (!dev->mt_slot_vals) {
405                         rc = -ENOMEM;
406                         goto out;
407                 }
408                 dev->current_slot = abs_info->value;
409
410                 dev->mt_sync.mt_state_sz = sizeof(*dev->mt_sync.mt_state) +
411                                            (dev->num_slots) * sizeof(int);
412                 dev->mt_sync.mt_state = calloc(1, dev->mt_sync.mt_state_sz);
413
414                 dev->mt_sync.tracking_id_changes_sz = NLONGS(dev->num_slots) * sizeof(long);
415                 dev->mt_sync.tracking_id_changes = malloc(dev->mt_sync.tracking_id_changes_sz);
416
417                 dev->mt_sync.slot_update_sz = NLONGS(dev->num_slots * ABS_MT_CNT) * sizeof(long);
418                 dev->mt_sync.slot_update = malloc(dev->mt_sync.slot_update_sz);
419
420                 if (!dev->mt_sync.tracking_id_changes ||
421                     !dev->mt_sync.slot_update ||
422                     !dev->mt_sync.mt_state) {
423                         rc = -ENOMEM;
424                         goto out;
425                 }
426
427             sync_mt_state(dev, 0);
428         }
429
430         rc = init_event_queue(dev);
431         if (rc < 0) {
432                 dev->fd = -1;
433                 return -rc;
434         }
435
436         /* not copying key state because we won't know when we'll start to
437          * use this fd and key's are likely to change state by then.
438          * Same with the valuators, really, but they may not change.
439          */
440
441         dev->initialized = true;
442 out:
443         if (rc)
444                 libevdev_reset(dev);
445         return rc ? -errno : 0;
446 }
447
448 LIBEVDEV_EXPORT int
449 libevdev_get_fd(const struct libevdev* dev)
450 {
451         return dev->fd;
452 }
453
454 static inline void
455 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
456 {
457         ev->time = dev->last_event_time;
458         ev->type = type;
459         ev->code = code;
460         ev->value = value;
461 }
462
463 static int
464 sync_key_state(struct libevdev *dev)
465 {
466         int rc;
467         int i;
468         unsigned long keystate[NLONGS(KEY_CNT)] = {0};
469
470         rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
471         if (rc < 0)
472                 goto out;
473
474         for (i = 0; i < KEY_CNT; i++) {
475                 int old, new;
476                 old = bit_is_set(dev->key_values, i);
477                 new = bit_is_set(keystate, i);
478                 if (old ^ new) {
479                         struct input_event *ev = queue_push(dev);
480                         init_event(dev, ev, EV_KEY, i, new ? 1 : 0);
481                 }
482         }
483
484         memcpy(dev->key_values, keystate, rc);
485
486         rc = 0;
487 out:
488         return rc ? -errno : 0;
489 }
490
491 static int
492 sync_sw_state(struct libevdev *dev)
493 {
494         int rc;
495         int i;
496         unsigned long swstate[NLONGS(SW_CNT)] = {0};
497
498         rc = ioctl(dev->fd, EVIOCGSW(sizeof(swstate)), swstate);
499         if (rc < 0)
500                 goto out;
501
502         for (i = 0; i < SW_CNT; i++) {
503                 int old, new;
504                 old = bit_is_set(dev->sw_values, i);
505                 new = bit_is_set(swstate, i);
506                 if (old ^ new) {
507                         struct input_event *ev = queue_push(dev);
508                         init_event(dev, ev, EV_SW, i, new ? 1 : 0);
509                 }
510         }
511
512         memcpy(dev->sw_values, swstate, rc);
513
514         rc = 0;
515 out:
516         return rc ? -errno : 0;
517 }
518
519 static int
520 sync_led_state(struct libevdev *dev)
521 {
522         int rc;
523         int i;
524         unsigned long ledstate[NLONGS(LED_CNT)] = {0};
525
526         rc = ioctl(dev->fd, EVIOCGLED(sizeof(ledstate)), ledstate);
527         if (rc < 0)
528                 goto out;
529
530         for (i = 0; i < LED_CNT; i++) {
531                 int old, new;
532                 old = bit_is_set(dev->led_values, i);
533                 new = bit_is_set(ledstate, i);
534                 if (old ^ new) {
535                         struct input_event *ev = queue_push(dev);
536                         init_event(dev, ev, EV_LED, i, new ? 1 : 0);
537                 }
538         }
539
540         memcpy(dev->led_values, ledstate, rc);
541
542         rc = 0;
543 out:
544         return rc ? -errno : 0;
545 }
546 static int
547 sync_abs_state(struct libevdev *dev)
548 {
549         int rc;
550         int i;
551
552         for (i = ABS_X; i < ABS_CNT; i++) {
553                 struct input_absinfo abs_info;
554
555                 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
556                         continue;
557
558                 if (!bit_is_set(dev->abs_bits, i))
559                         continue;
560
561                 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
562                 if (rc < 0)
563                         goto out;
564
565                 if (dev->abs_info[i].value != abs_info.value) {
566                         struct input_event *ev = queue_push(dev);
567
568                         init_event(dev, ev, EV_ABS, i, abs_info.value);
569                         dev->abs_info[i].value = abs_info.value;
570                 }
571         }
572
573         rc = 0;
574 out:
575         return rc ? -errno : 0;
576 }
577
578 static int
579 sync_mt_state(struct libevdev *dev, int create_events)
580 {
581         struct input_event *ev;
582         struct input_absinfo abs_info;
583         int rc;
584         int axis, slot;
585         int ioctl_success = 0;
586         int last_reported_slot = 0;
587         struct mt_sync_state *mt_state = dev->mt_sync.mt_state;
588         unsigned long *slot_update = dev->mt_sync.slot_update;
589         unsigned long *tracking_id_changes = dev->mt_sync.tracking_id_changes;
590         int need_tracking_id_changes = 0;
591
592         memset(dev->mt_sync.slot_update, 0, dev->mt_sync.slot_update_sz);
593         memset(dev->mt_sync.tracking_id_changes, 0,
594                dev->mt_sync.tracking_id_changes_sz);
595
596 #define AXISBIT(_slot, _axis) (_slot * ABS_MT_CNT + _axis - ABS_MT_MIN)
597
598         for (axis = ABS_MT_MIN; axis <= ABS_MT_MAX; axis++) {
599                 if (axis == ABS_MT_SLOT)
600                         continue;
601
602                 if (!libevdev_has_event_code(dev, EV_ABS, axis))
603                         continue;
604
605                 mt_state->code = axis;
606                 rc = ioctl(dev->fd, EVIOCGMTSLOTS(dev->mt_sync.mt_state_sz), mt_state);
607                 if (rc < 0) {
608                         /* if the first ioctl fails with -EINVAL, chances are the kernel
609                            doesn't support the ioctl. Simply continue */
610                         if (errno == -EINVAL && !ioctl_success) {
611                                 rc = 0;
612                         } else /* if the second, ... ioctl fails, really fail */
613                                 goto out;
614                 } else {
615                         if (ioctl_success == 0)
616                                 ioctl_success = 1;
617
618                         for (slot = 0; slot < dev->num_slots; slot++) {
619
620                                 if (*slot_value(dev, slot, axis) == mt_state->val[slot])
621                                         continue;
622
623                                 if (axis == ABS_MT_TRACKING_ID &&
624                                     *slot_value(dev, slot, axis) != -1 &&
625                                     mt_state->val[slot] != -1) {
626                                         set_bit(tracking_id_changes, slot);
627                                         need_tracking_id_changes = 1;
628                                 }
629
630                                 *slot_value(dev, slot, axis) = mt_state->val[slot];
631
632                                 set_bit(slot_update, AXISBIT(slot, axis));
633                                 /* note that this slot has updates */
634                                 set_bit(slot_update, AXISBIT(slot, ABS_MT_SLOT));
635                         }
636
637
638                 }
639         }
640
641         if (!create_events) {
642                 rc = 0;
643                 goto out;
644         }
645
646         if (need_tracking_id_changes) {
647                 for (slot = 0; slot < dev->num_slots;  slot++) {
648                         if (!bit_is_set(tracking_id_changes, slot))
649                                 continue;
650
651                         ev = queue_push(dev);
652                         init_event(dev, ev, EV_ABS, ABS_MT_SLOT, slot);
653                         ev = queue_push(dev);
654                         init_event(dev, ev, EV_ABS, ABS_MT_TRACKING_ID, -1);
655
656                         last_reported_slot = slot;
657                 }
658
659                 ev = queue_push(dev);
660                 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
661         }
662
663         for (slot = 0; slot < dev->num_slots;  slot++) {
664                 if (!bit_is_set(slot_update, AXISBIT(slot, ABS_MT_SLOT)))
665                         continue;
666
667                 ev = queue_push(dev);
668                 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, slot);
669                 last_reported_slot = slot;
670
671                 for (axis = ABS_MT_MIN; axis <= ABS_MT_MAX; axis++) {
672                         if (axis == ABS_MT_SLOT ||
673                             !libevdev_has_event_code(dev, EV_ABS, axis))
674                                 continue;
675
676                         if (bit_is_set(slot_update, AXISBIT(slot, axis))) {
677                                 ev = queue_push(dev);
678                                 init_event(dev, ev, EV_ABS, axis, *slot_value(dev, slot, axis));
679                         }
680                 }
681         }
682
683         /* add one last slot event to make sure the client is on the same
684            slot as the kernel */
685
686         rc = ioctl(dev->fd, EVIOCGABS(ABS_MT_SLOT), &abs_info);
687         if (rc < 0)
688                 goto out;
689
690         dev->current_slot = abs_info.value;
691
692         if (dev->current_slot != last_reported_slot) {
693                 ev = queue_push(dev);
694                 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, dev->current_slot);
695         }
696
697 #undef AXISBIT
698
699         rc = 0;
700 out:
701         return rc ? -errno : 0;
702 }
703
704 static int
705 read_more_events(struct libevdev *dev)
706 {
707         int free_elem;
708         int len;
709         struct input_event *next;
710
711         free_elem = queue_num_free_elements(dev);
712         if (free_elem <= 0)
713                 return 0;
714
715         next = queue_next_element(dev);
716         len = read(dev->fd, next, free_elem * sizeof(struct input_event));
717         if (len < 0) {
718                 return -errno;
719         } else if (len > 0 && len % sizeof(struct input_event) != 0)
720                 return -EINVAL;
721         else if (len > 0) {
722                 int nev = len/sizeof(struct input_event);
723                 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
724         }
725
726         return 0;
727 }
728
729 static inline void
730 drain_events(struct libevdev *dev)
731 {
732         int rc;
733         size_t nelem;
734         int iterations = 0;
735         const int max_iterations = 8; /* EVDEV_BUF_PACKETS in
736                                          kernel/drivers/input/evedev.c */
737
738         queue_shift_multiple(dev, queue_num_elements(dev), NULL);
739
740         do {
741                 rc = read_more_events(dev);
742                 if (rc == -EAGAIN)
743                         return;
744
745                 if (rc < 0) {
746                         log_error("Failed to drain events before sync.\n");
747                         return;
748                 }
749
750                 nelem = queue_num_elements(dev);
751                 queue_shift_multiple(dev, nelem, NULL);
752         } while (iterations++ < max_iterations && nelem >= queue_size(dev));
753
754         /* Our buffer should be roughly the same or bigger than the kernel
755            buffer in most cases, so we usually don't expect to recurse. If
756            we do, make sure we stop after max_iterations and proceed with
757            what we have.  This could happen if events queue up faster than
758            we can drain them.
759          */
760         if (iterations >= max_iterations)
761                 log_info("Unable to drain events, buffer size mismatch.\n");
762 }
763
764 static int
765 sync_state(struct libevdev *dev)
766 {
767         int rc = 0;
768         struct input_event *ev;
769
770          /* see section "Discarding events before synchronizing" in
771           * libevdev/libevdev.h */
772         drain_events(dev);
773
774         if (libevdev_has_event_type(dev, EV_KEY))
775                 rc = sync_key_state(dev);
776         if (libevdev_has_event_type(dev, EV_LED))
777                 rc = sync_led_state(dev);
778         if (libevdev_has_event_type(dev, EV_SW))
779                 rc = sync_sw_state(dev);
780         if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
781                 rc = sync_abs_state(dev);
782         if (rc == 0 && dev->num_slots > -1 &&
783             libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
784                 rc = sync_mt_state(dev, 1);
785
786         dev->queue_nsync = queue_num_elements(dev);
787
788         if (dev->queue_nsync > 0) {
789                 ev = queue_push(dev);
790                 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
791                 dev->queue_nsync++;
792         }
793
794         return rc;
795 }
796
797 static int
798 update_key_state(struct libevdev *dev, const struct input_event *e)
799 {
800         if (!libevdev_has_event_type(dev, EV_KEY))
801                 return 1;
802
803         if (e->code > KEY_MAX)
804                 return 1;
805
806         set_bit_state(dev->key_values, e->code, e->value != 0);
807
808         return 0;
809 }
810
811 static int
812 update_mt_state(struct libevdev *dev, const struct input_event *e)
813 {
814         if (e->code == ABS_MT_SLOT && dev->num_slots > -1) {
815                 int i;
816                 dev->current_slot = e->value;
817                 /* sync abs_info with the current slot values */
818                 for (i = ABS_MT_SLOT + 1; i <= ABS_MT_MAX; i++) {
819                         if (libevdev_has_event_code(dev, EV_ABS, i))
820                                 dev->abs_info[i].value = *slot_value(dev, dev->current_slot, i);
821                 }
822
823                 return 0;
824         } else if (dev->current_slot == -1)
825                 return 1;
826
827         *slot_value(dev, dev->current_slot, e->code) = e->value;
828
829         return 0;
830 }
831
832 static int
833 update_abs_state(struct libevdev *dev, const struct input_event *e)
834 {
835         if (!libevdev_has_event_type(dev, EV_ABS))
836                 return 1;
837
838         if (e->code > ABS_MAX)
839                 return 1;
840
841         if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
842                 update_mt_state(dev, e);
843
844         dev->abs_info[e->code].value = e->value;
845
846         return 0;
847 }
848
849 static int
850 update_led_state(struct libevdev *dev, const struct input_event *e)
851 {
852         if (!libevdev_has_event_type(dev, EV_LED))
853                 return 1;
854
855         if (e->code > LED_MAX)
856                 return 1;
857
858         set_bit_state(dev->led_values, e->code, e->value != 0);
859
860         return 0;
861 }
862
863 static int
864 update_sw_state(struct libevdev *dev, const struct input_event *e)
865 {
866         if (!libevdev_has_event_type(dev, EV_SW))
867                 return 1;
868
869         if (e->code > SW_MAX)
870                 return 1;
871
872         set_bit_state(dev->sw_values, e->code, e->value != 0);
873
874         return 0;
875 }
876
877 static int
878 update_state(struct libevdev *dev, const struct input_event *e)
879 {
880         int rc = 0;
881
882         switch(e->type) {
883                 case EV_SYN:
884                 case EV_REL:
885                         break;
886                 case EV_KEY:
887                         rc = update_key_state(dev, e);
888                         break;
889                 case EV_ABS:
890                         rc = update_abs_state(dev, e);
891                         break;
892                 case EV_LED:
893                         rc = update_led_state(dev, e);
894                         break;
895                 case EV_SW:
896                         rc = update_sw_state(dev, e);
897                         break;
898         }
899
900         dev->last_event_time = e->time;
901
902         return rc;
903 }
904
905 /**
906  * Sanitize/modify events where needed.
907  */
908 static inline enum event_filter_status
909 sanitize_event(const struct libevdev *dev,
910                struct input_event *ev,
911                enum SyncState sync_state)
912 {
913         if (unlikely(dev->num_slots > -1 &&
914                      libevdev_event_is_code(ev, EV_ABS, ABS_MT_SLOT) &&
915                      (ev->value < 0 || ev->value >= dev->num_slots))) {
916                 log_bug("Device \"%s\" received an invalid slot index %d."
917                                 "Capping to announced max slot number %d.\n",
918                                 dev->name, ev->value, dev->num_slots - 1);
919                 ev->value = dev->num_slots - 1;
920                 return EVENT_FILTER_MODIFIED;
921
922         /* Drop any invalid tracking IDs, they are only supposed to go from
923            N to -1 or from -1 to N. Never from -1 to -1, or N to M. Very
924            unlikely to ever happen from a real device.
925            */
926         } else if (unlikely(sync_state == SYNC_NONE &&
927                             dev->num_slots > -1 &&
928                             libevdev_event_is_code(ev, EV_ABS, ABS_MT_TRACKING_ID) &&
929                             ((ev->value == -1 &&
930                              *slot_value(dev, dev->current_slot, ABS_MT_TRACKING_ID) == -1) ||
931                              (ev->value != -1 &&
932                              *slot_value(dev, dev->current_slot, ABS_MT_TRACKING_ID) != -1)))) {
933                 log_bug("Device \"%s\" received a double tracking ID %d in slot %d.\n",
934                         dev->name, ev->value, dev->current_slot);
935                 return EVENT_FILTER_DISCARD;
936         }
937
938         return EVENT_FILTER_NONE;
939 }
940
941 LIBEVDEV_EXPORT int
942 libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
943 {
944         int rc = LIBEVDEV_READ_STATUS_SUCCESS;
945         enum event_filter_status filter_status;
946
947         if (!dev->initialized) {
948                 log_bug("device not initialized. call libevdev_set_fd() first\n");
949                 return -EBADF;
950         } else if (dev->fd < 0)
951                 return -EBADF;
952
953         if (!(flags & (LIBEVDEV_READ_FLAG_NORMAL|LIBEVDEV_READ_FLAG_SYNC|LIBEVDEV_READ_FLAG_FORCE_SYNC))) {
954                 log_bug("invalid flags %#x\n.\n", flags);
955                 return -EINVAL;
956         }
957
958         if (flags & LIBEVDEV_READ_FLAG_SYNC) {
959                 if (dev->sync_state == SYNC_NEEDED) {
960                         rc = sync_state(dev);
961                         if (rc != 0)
962                                 return rc;
963                         dev->sync_state = SYNC_IN_PROGRESS;
964                 }
965
966                 if (dev->queue_nsync == 0) {
967                         dev->sync_state = SYNC_NONE;
968                         return -EAGAIN;
969                 }
970
971         } else if (dev->sync_state != SYNC_NONE) {
972                 struct input_event e;
973
974                 /* call update_state for all events here, otherwise the library has the wrong view
975                    of the device too */
976                 while (queue_shift(dev, &e) == 0) {
977                         dev->queue_nsync--;
978                         if (sanitize_event(dev, &e, dev->sync_state) != EVENT_FILTER_DISCARD)
979                                 update_state(dev, &e);
980                 }
981
982                 dev->sync_state = SYNC_NONE;
983         }
984
985         /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
986            worst case we don't read fast enough and end up with SYN_DROPPED anyway.
987
988            Except if the fd is in blocking mode and we still have events from the last read, don't
989            read in any more.
990          */
991         do {
992                 if (!(flags & LIBEVDEV_READ_FLAG_BLOCKING) ||
993                     queue_num_elements(dev) == 0) {
994                         rc = read_more_events(dev);
995                         if (rc < 0 && rc != -EAGAIN)
996                                 goto out;
997                 }
998
999                 if (flags & LIBEVDEV_READ_FLAG_FORCE_SYNC) {
1000                         dev->sync_state = SYNC_NEEDED;
1001                         rc = LIBEVDEV_READ_STATUS_SYNC;
1002                         goto out;
1003                 }
1004
1005
1006                 if (queue_shift(dev, ev) != 0)
1007                         return -EAGAIN;
1008
1009                 filter_status = sanitize_event(dev, ev, dev->sync_state);
1010                 if (filter_status != EVENT_FILTER_DISCARD)
1011                         update_state(dev, ev);
1012
1013         /* if we disabled a code, get the next event instead */
1014         } while(filter_status == EVENT_FILTER_DISCARD ||
1015                 !libevdev_has_event_code(dev, ev->type, ev->code));
1016
1017         rc = LIBEVDEV_READ_STATUS_SUCCESS;
1018         if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
1019                 dev->sync_state = SYNC_NEEDED;
1020                 rc = LIBEVDEV_READ_STATUS_SYNC;
1021         }
1022
1023         if (flags & LIBEVDEV_READ_FLAG_SYNC && dev->queue_nsync > 0) {
1024                 dev->queue_nsync--;
1025                 rc = LIBEVDEV_READ_STATUS_SYNC;
1026                 if (dev->queue_nsync == 0) {
1027                         struct input_event next;
1028                         dev->sync_state = SYNC_NONE;
1029
1030                         if (queue_peek(dev, 0, &next) == 0 &&
1031                             next.type == EV_SYN && next.code == SYN_DROPPED)
1032                                 log_info("SYN_DROPPED received after finished "
1033                                          "sync - you're not keeping up\n");
1034                 }
1035         }
1036
1037 out:
1038         return rc;
1039 }
1040
1041 LIBEVDEV_EXPORT int
1042 libevdev_has_event_pending(struct libevdev *dev)
1043 {
1044         struct pollfd fds = { dev->fd, POLLIN, 0 };
1045         int rc;
1046
1047         if (!dev->initialized) {
1048                 log_bug("device not initialized. call libevdev_set_fd() first\n");
1049                 return -EBADF;
1050         } else if (dev->fd < 0)
1051                 return -EBADF;
1052
1053         if (queue_num_elements(dev) != 0)
1054                 return 1;
1055
1056         rc = poll(&fds, 1, 0);
1057         return (rc >= 0) ? rc : -errno;
1058 }
1059
1060 LIBEVDEV_EXPORT const char *
1061 libevdev_get_name(const struct libevdev *dev)
1062 {
1063         return dev->name ? dev->name : "";
1064 }
1065
1066 LIBEVDEV_EXPORT const char *
1067 libevdev_get_phys(const struct libevdev *dev)
1068 {
1069         return dev->phys;
1070 }
1071
1072 LIBEVDEV_EXPORT const char *
1073 libevdev_get_uniq(const struct libevdev *dev)
1074 {
1075         return dev->uniq;
1076 }
1077
1078 #define STRING_SETTER(field) \
1079 LIBEVDEV_EXPORT void libevdev_set_##field(struct libevdev *dev, const char *field) \
1080 { \
1081         if (field == NULL) \
1082                 return; \
1083         free(dev->field); \
1084         dev->field = strdup(field); \
1085 }
1086
1087 STRING_SETTER(name)
1088 STRING_SETTER(phys)
1089 STRING_SETTER(uniq)
1090
1091
1092 #define PRODUCT_GETTER(name) \
1093 LIBEVDEV_EXPORT int libevdev_get_id_##name(const struct libevdev *dev) \
1094 { \
1095         return dev->ids.name; \
1096 }
1097
1098 PRODUCT_GETTER(product)
1099 PRODUCT_GETTER(vendor)
1100 PRODUCT_GETTER(bustype)
1101 PRODUCT_GETTER(version)
1102
1103 #define PRODUCT_SETTER(field) \
1104 LIBEVDEV_EXPORT void libevdev_set_id_##field(struct libevdev *dev, int field) \
1105 { \
1106         dev->ids.field = field;\
1107 }
1108
1109 PRODUCT_SETTER(product)
1110 PRODUCT_SETTER(vendor)
1111 PRODUCT_SETTER(bustype)
1112 PRODUCT_SETTER(version)
1113
1114 LIBEVDEV_EXPORT int
1115 libevdev_get_driver_version(const struct libevdev *dev)
1116 {
1117         return dev->driver_version;
1118 }
1119
1120 LIBEVDEV_EXPORT int
1121 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
1122 {
1123         return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
1124 }
1125
1126 LIBEVDEV_EXPORT int
1127 libevdev_enable_property(struct libevdev *dev, unsigned int prop)
1128 {
1129         if (prop > INPUT_PROP_MAX)
1130                 return -1;
1131
1132         set_bit(dev->props, prop);
1133         return 0;
1134 }
1135
1136 LIBEVDEV_EXPORT int
1137 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
1138 {
1139         return type == EV_SYN ||(type <= EV_MAX && bit_is_set(dev->bits, type));
1140 }
1141
1142 LIBEVDEV_EXPORT int
1143 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
1144 {
1145         const unsigned long *mask = NULL;
1146         int max;
1147
1148         if (!libevdev_has_event_type(dev, type))
1149                 return 0;
1150
1151         if (type == EV_SYN)
1152                 return 1;
1153
1154         max = type_to_mask_const(dev, type, &mask);
1155
1156         if (max == -1 || code > (unsigned int)max)
1157                 return 0;
1158
1159         return bit_is_set(mask, code);
1160 }
1161
1162 LIBEVDEV_EXPORT int
1163 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
1164 {
1165         int value = 0;
1166
1167         if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
1168                 return 0;
1169
1170         switch (type) {
1171                 case EV_ABS: value = dev->abs_info[code].value; break;
1172                 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
1173                 case EV_LED: value = bit_is_set(dev->led_values, code); break;
1174                 case EV_SW: value = bit_is_set(dev->sw_values, code); break;
1175                 case EV_REP:
1176                             switch(code) {
1177                                     case REP_DELAY:
1178                                             libevdev_get_repeat(dev, &value, NULL);
1179                                             break;
1180                                     case REP_PERIOD:
1181                                             libevdev_get_repeat(dev, NULL, &value);
1182                                             break;
1183                                     default:
1184                                             value = 0;
1185                                             break;
1186                             }
1187                             break;
1188                 default:
1189                         value = 0;
1190                         break;
1191         }
1192
1193         return value;
1194 }
1195
1196 LIBEVDEV_EXPORT int
1197 libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value)
1198 {
1199         int rc = 0;
1200         struct input_event e;
1201
1202         if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
1203                 return -1;
1204
1205         e.type = type;
1206         e.code = code;
1207         e.value = value;
1208
1209         if (sanitize_event(dev, &e, SYNC_NONE) != EVENT_FILTER_NONE)
1210                 return -1;
1211
1212         switch(type) {
1213                 case EV_ABS: rc = update_abs_state(dev, &e); break;
1214                 case EV_KEY: rc = update_key_state(dev, &e); break;
1215                 case EV_LED: rc = update_led_state(dev, &e); break;
1216                 case EV_SW: rc = update_sw_state(dev, &e); break;
1217                 default:
1218                              rc = -1;
1219                              break;
1220         }
1221
1222         return rc;
1223 }
1224
1225 LIBEVDEV_EXPORT int
1226 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
1227 {
1228         if (libevdev_has_event_type(dev, type) &&
1229             libevdev_has_event_code(dev, type, code)) {
1230                 *value = libevdev_get_event_value(dev, type, code);
1231                 return 1;
1232         } else
1233                 return 0;
1234 }
1235
1236 LIBEVDEV_EXPORT int
1237 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
1238 {
1239         if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1240                 return 0;
1241
1242         if (dev->num_slots < 0 || slot >= (unsigned int)dev->num_slots)
1243                 return 0;
1244
1245         if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1246                 return 0;
1247
1248         return *slot_value(dev, slot, code);
1249 }
1250
1251 LIBEVDEV_EXPORT int
1252 libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value)
1253 {
1254         if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1255                 return -1;
1256
1257         if (dev->num_slots == -1 || slot >= (unsigned int)dev->num_slots)
1258                 return -1;
1259
1260         if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1261                 return -1;
1262
1263         if (code == ABS_MT_SLOT) {
1264                 if (value < 0 || value >= libevdev_get_num_slots(dev))
1265                         return -1;
1266                 dev->current_slot = value;
1267         }
1268
1269         *slot_value(dev, slot, code) = value;
1270
1271         return 0;
1272 }
1273
1274 LIBEVDEV_EXPORT int
1275 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
1276 {
1277         if (libevdev_has_event_type(dev, EV_ABS) &&
1278             libevdev_has_event_code(dev, EV_ABS, code) &&
1279             dev->num_slots >= 0 &&
1280             slot < (unsigned int)dev->num_slots) {
1281                 *value = libevdev_get_slot_value(dev, slot, code);
1282                 return 1;
1283         } else
1284                 return 0;
1285 }
1286
1287 LIBEVDEV_EXPORT int
1288 libevdev_get_num_slots(const struct libevdev *dev)
1289 {
1290         return dev->num_slots;
1291 }
1292
1293 LIBEVDEV_EXPORT int
1294 libevdev_get_current_slot(const struct libevdev *dev)
1295 {
1296         return dev->current_slot;
1297 }
1298
1299 LIBEVDEV_EXPORT const struct input_absinfo*
1300 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
1301 {
1302         if (!libevdev_has_event_type(dev, EV_ABS) ||
1303             !libevdev_has_event_code(dev, EV_ABS, code))
1304                 return NULL;
1305
1306         return &dev->abs_info[code];
1307 }
1308
1309 #define ABS_GETTER(name) \
1310 LIBEVDEV_EXPORT int libevdev_get_abs_##name(const struct libevdev *dev, unsigned int code) \
1311 { \
1312         const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code); \
1313         return absinfo ? absinfo->name : 0; \
1314 }
1315
1316 ABS_GETTER(maximum)
1317 ABS_GETTER(minimum)
1318 ABS_GETTER(fuzz)
1319 ABS_GETTER(flat)
1320 ABS_GETTER(resolution)
1321
1322 #define ABS_SETTER(field) \
1323 LIBEVDEV_EXPORT void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
1324 { \
1325         if (!libevdev_has_event_code(dev, EV_ABS, code)) \
1326                 return; \
1327         dev->abs_info[code].field = val; \
1328 }
1329
1330 ABS_SETTER(maximum)
1331 ABS_SETTER(minimum)
1332 ABS_SETTER(fuzz)
1333 ABS_SETTER(flat)
1334 ABS_SETTER(resolution)
1335
1336 LIBEVDEV_EXPORT void
1337 libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1338 {
1339         if (!libevdev_has_event_code(dev, EV_ABS, code))
1340                 return;
1341
1342         dev->abs_info[code] = *abs;
1343 }
1344
1345 LIBEVDEV_EXPORT int
1346 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
1347 {
1348         int max;
1349
1350         if (type > EV_MAX)
1351                 return -1;
1352
1353         if (libevdev_has_event_type(dev, type))
1354                 return 0;
1355
1356         max = libevdev_event_type_get_max(type);
1357         if (max == -1)
1358                 return -1;
1359
1360         set_bit(dev->bits, type);
1361
1362         if (type == EV_REP) {
1363                 int delay = 0, period = 0;
1364                 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
1365                 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
1366         }
1367         return 0;
1368 }
1369
1370 LIBEVDEV_EXPORT int
1371 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
1372 {
1373         int max;
1374
1375         if (type > EV_MAX || type == EV_SYN)
1376                 return -1;
1377
1378         max = libevdev_event_type_get_max(type);
1379         if (max == -1)
1380                 return -1;
1381
1382         clear_bit(dev->bits, type);
1383
1384         return 0;
1385 }
1386
1387 LIBEVDEV_EXPORT int
1388 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
1389                            unsigned int code, const void *data)
1390 {
1391         unsigned int max;
1392         unsigned long *mask = NULL;
1393
1394         if (libevdev_enable_event_type(dev, type))
1395                 return -1;
1396
1397         switch(type) {
1398                 case EV_SYN:
1399                         return 0;
1400                 case EV_ABS:
1401                 case EV_REP:
1402                         if (data == NULL)
1403                                 return -1;
1404                         break;
1405                 default:
1406                         if (data != NULL)
1407                                 return -1;
1408                         break;
1409         }
1410
1411         max = type_to_mask(dev, type, &mask);
1412
1413         if (code > max || (int)max == -1)
1414                 return -1;
1415
1416         set_bit(mask, code);
1417
1418         if (type == EV_ABS) {
1419                 const struct input_absinfo *abs = data;
1420                 dev->abs_info[code] = *abs;
1421         } else if (type == EV_REP) {
1422                 const int *value = data;
1423                 dev->rep_values[code] = *value;
1424         }
1425
1426         return 0;
1427 }
1428
1429 LIBEVDEV_EXPORT int
1430 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
1431 {
1432         unsigned int max;
1433         unsigned long *mask = NULL;
1434
1435         if (type > EV_MAX || type == EV_SYN)
1436                 return -1;
1437
1438         max = type_to_mask(dev, type, &mask);
1439
1440         if (code > max || (int)max == -1)
1441                 return -1;
1442
1443         clear_bit(mask, code);
1444
1445         return 0;
1446 }
1447
1448 LIBEVDEV_EXPORT int
1449 libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1450 {
1451         int rc;
1452
1453         if (!dev->initialized) {
1454                 log_bug("device not initialized. call libevdev_set_fd() first\n");
1455                 return -EBADF;
1456         } else if (dev->fd < 0)
1457                 return -EBADF;
1458
1459         if (code > ABS_MAX)
1460                 return -EINVAL;
1461
1462         rc = ioctl(dev->fd, EVIOCSABS(code), abs);
1463         if (rc < 0)
1464                 rc = -errno;
1465         else
1466                 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
1467
1468         return rc;
1469 }
1470
1471 LIBEVDEV_EXPORT int
1472 libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
1473 {
1474         int rc = 0;
1475
1476         if (!dev->initialized) {
1477                 log_bug("device not initialized. call libevdev_set_fd() first\n");
1478                 return -EBADF;
1479         } else if (dev->fd < 0)
1480                 return -EBADF;
1481
1482         if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB) {
1483                 log_bug("invalid grab parameter %#x\n", grab);
1484                 return -EINVAL;
1485         }
1486
1487         if (grab == dev->grabbed)
1488                 return 0;
1489
1490         if (grab == LIBEVDEV_GRAB)
1491                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
1492         else if (grab == LIBEVDEV_UNGRAB)
1493                 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
1494
1495         if (rc == 0)
1496                 dev->grabbed = grab;
1497
1498         return rc < 0 ? -errno : 0;
1499 }
1500
1501 LIBEVDEV_EXPORT int
1502 libevdev_event_is_type(const struct input_event *ev, unsigned int type)
1503 {
1504         return type < EV_CNT && ev->type == type;
1505 }
1506
1507 LIBEVDEV_EXPORT int
1508 libevdev_event_is_code(const struct input_event *ev, unsigned int type, unsigned int code)
1509 {
1510         int max;
1511
1512         if (!libevdev_event_is_type(ev, type))
1513                 return 0;
1514
1515         max = libevdev_event_type_get_max(type);
1516         return (max > -1 && code <= (unsigned int)max && ev->code == code);
1517 }
1518
1519 LIBEVDEV_EXPORT const char*
1520 libevdev_event_type_get_name(unsigned int type)
1521 {
1522         if (type > EV_MAX)
1523                 return NULL;
1524
1525         return ev_map[type];
1526 }
1527
1528 LIBEVDEV_EXPORT const char*
1529 libevdev_event_code_get_name(unsigned int type, unsigned int code)
1530 {
1531         int max = libevdev_event_type_get_max(type);
1532
1533         if (max == -1 || code > (unsigned int)max)
1534                 return NULL;
1535
1536         return event_type_map[type][code];
1537 }
1538
1539 LIBEVDEV_EXPORT const char*
1540 libevdev_property_get_name(unsigned int prop)
1541 {
1542         if (prop > INPUT_PROP_MAX)
1543                 return NULL;
1544
1545         return input_prop_map[prop];
1546 }
1547
1548 LIBEVDEV_EXPORT int
1549 libevdev_event_type_get_max(unsigned int type)
1550 {
1551         if (type > EV_MAX)
1552                 return -1;
1553
1554         return ev_max[type];
1555 }
1556
1557 LIBEVDEV_EXPORT int
1558 libevdev_get_repeat(const struct libevdev *dev, int *delay, int *period)
1559 {
1560         if (!libevdev_has_event_type(dev, EV_REP))
1561                 return -1;
1562
1563         if (delay != NULL)
1564                 *delay = dev->rep_values[REP_DELAY];
1565         if (period != NULL)
1566                 *period = dev->rep_values[REP_PERIOD];
1567
1568         return 0;
1569 }
1570
1571 LIBEVDEV_EXPORT int
1572 libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value)
1573 {
1574         return libevdev_kernel_set_led_values(dev, code, value, -1);
1575 }
1576
1577 LIBEVDEV_EXPORT int
1578 libevdev_kernel_set_led_values(struct libevdev *dev, ...)
1579 {
1580         struct input_event ev[LED_MAX + 1];
1581         enum libevdev_led_value val;
1582         va_list args;
1583         int code;
1584         int rc = 0;
1585         size_t nleds = 0;
1586
1587         if (!dev->initialized) {
1588                 log_bug("device not initialized. call libevdev_set_fd() first\n");
1589                 return -EBADF;
1590         } else if (dev->fd < 0)
1591                 return -EBADF;
1592
1593         memset(ev, 0, sizeof(ev));
1594
1595         va_start(args, dev);
1596         code = va_arg(args, unsigned int);
1597         while (code != -1) {
1598                 if (code > LED_MAX) {
1599                         rc = -EINVAL;
1600                         break;
1601                 }
1602                 val = va_arg(args, enum libevdev_led_value);
1603                 if (val != LIBEVDEV_LED_ON && val != LIBEVDEV_LED_OFF) {
1604                         rc = -EINVAL;
1605                         break;
1606                 }
1607
1608                 if (libevdev_has_event_code(dev, EV_LED, code)) {
1609                         struct input_event *e = ev;
1610
1611                         while (e->type > 0 && e->code != code)
1612                                 e++;
1613
1614                         if (e->type == 0)
1615                                 nleds++;
1616                         e->type = EV_LED;
1617                         e->code = code;
1618                         e->value = (val == LIBEVDEV_LED_ON);
1619                 }
1620                 code = va_arg(args, unsigned int);
1621         }
1622         va_end(args);
1623
1624         if (rc == 0 && nleds > 0) {
1625                 ev[nleds].type = EV_SYN;
1626                 ev[nleds++].code = SYN_REPORT;
1627
1628                 rc = write(libevdev_get_fd(dev), ev, nleds * sizeof(ev[0]));
1629                 if (rc > 0) {
1630                         nleds--; /* last is EV_SYN */
1631                         while (nleds--)
1632                                 update_led_state(dev, &ev[nleds]);
1633                 }
1634                 rc = (rc != -1) ? 0 : -errno;
1635         }
1636
1637         return rc;
1638 }
1639
1640 LIBEVDEV_EXPORT int
1641 libevdev_set_clock_id(struct libevdev *dev, int clockid)
1642 {
1643         if (!dev->initialized) {
1644                 log_bug("device not initialized. call libevdev_set_fd() first\n");
1645                 return -EBADF;
1646         } else if (dev->fd < 0)
1647                 return -EBADF;
1648
1649         return ioctl(dev->fd, EVIOCSCLOCKID, &clockid) ? -errno : 0;
1650 }