2 * Copyright © 2013 Red Hat, Inc.
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.
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
32 #include "libevdev-int.h"
33 #include "libevdev-util.h"
34 #include "event-names.h"
38 static int sync_mt_state(struct libevdev *dev, int create_events);
41 init_event_queue(struct libevdev *dev)
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 */
46 const int QUEUE_SIZE = 256;
48 return queue_alloc(dev, QUEUE_SIZE);
52 _libevdev_log(struct libevdev *dev, const char *format, ...)
56 va_start(args, format);
57 dev->log(format, args);
62 libevdev_noop_log_func(const char *format, va_list args)
66 LIBEVDEV_EXPORT struct libevdev*
71 dev = calloc(1, sizeof(*dev));
76 dev->current_slot = -1;
77 dev->log = libevdev_noop_log_func;
78 dev->grabbed = LIBEVDEV_UNGRAB;
79 dev->sync_state = SYNC_NONE;
85 libevdev_new_from_fd(int fd, struct libevdev **dev)
94 rc = libevdev_set_fd(d, fd);
103 libevdev_free(struct libevdev *dev)
116 libevdev_set_log_handler(struct libevdev *dev, libevdev_log_func_t logfunc)
121 dev->log = logfunc ? logfunc : libevdev_noop_log_func;
125 libevdev_change_fd(struct libevdev *dev, int fd)
134 libevdev_set_fd(struct libevdev* dev, int fd)
143 rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
147 memset(buf, 0, sizeof(buf));
148 rc = ioctl(fd, EVIOCGNAME(sizeof(buf) - 1), buf);
153 dev->name = strdup(buf);
161 memset(buf, 0, sizeof(buf));
162 rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
164 /* uinput has no phys */
168 dev->phys = strdup(buf);
177 memset(buf, 0, sizeof(buf));
178 rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
183 dev->uniq = strdup(buf);
190 rc = ioctl(fd, EVIOCGID, &dev->ids);
194 rc = ioctl(fd, EVIOCGVERSION, &dev->driver_version);
198 rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
202 rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
206 rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
210 rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
214 rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
218 rc = ioctl(fd, EVIOCGBIT(EV_SW, sizeof(dev->sw_bits)), dev->sw_bits);
222 rc = ioctl(fd, EVIOCGBIT(EV_MSC, sizeof(dev->msc_bits)), dev->msc_bits);
226 rc = ioctl(fd, EVIOCGBIT(EV_FF, sizeof(dev->ff_bits)), dev->ff_bits);
230 rc = ioctl(fd, EVIOCGBIT(EV_SND, sizeof(dev->snd_bits)), dev->snd_bits);
234 rc = ioctl(fd, EVIOCGKEY(sizeof(dev->key_values)), dev->key_values);
238 rc = ioctl(fd, EVIOCGLED(sizeof(dev->led_values)), dev->led_values);
242 rc = ioctl(fd, EVIOCGSW(sizeof(dev->sw_values)), dev->sw_values);
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);
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);
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;
272 sync_mt_state(dev, 0);
274 rc = init_event_queue(dev);
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.
286 return rc ? -errno : 0;
290 libevdev_get_fd(const struct libevdev* dev)
296 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
298 ev->time = dev->last_event_time;
305 sync_key_state(struct libevdev *dev)
309 unsigned long keystate[NLONGS(KEY_CNT)];
311 rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
315 for (i = 0; i < KEY_MAX; i++) {
317 old = bit_is_set(dev->key_values, i);
318 new = bit_is_set(keystate, i);
320 struct input_event *ev = queue_push(dev);
321 init_event(dev, ev, EV_KEY, i, new ? 1 : 0);
323 set_bit_state(dev->key_values, i, new);
328 return rc ? -errno : 0;
332 sync_sw_state(struct libevdev *dev)
336 unsigned long swstate[NLONGS(SW_CNT)];
338 rc = ioctl(dev->fd, EVIOCGSW(sizeof(swstate)), swstate);
342 for (i = 0; i < SW_CNT; i++) {
344 old = bit_is_set(dev->sw_values, i);
345 new = bit_is_set(swstate, i);
347 struct input_event *ev = queue_push(dev);
348 init_event(dev, ev, EV_SW, i, new ? 1 : 0);
350 set_bit_state(dev->sw_values, i, new);
355 return rc ? -errno : 0;
359 sync_led_state(struct libevdev *dev)
363 unsigned long ledstate[NLONGS(LED_CNT)];
365 rc = ioctl(dev->fd, EVIOCGLED(sizeof(ledstate)), ledstate);
369 for (i = 0; i < LED_MAX; i++) {
371 old = bit_is_set(dev->led_values, i);
372 new = bit_is_set(ledstate, i);
374 struct input_event *ev = queue_push(dev);
375 init_event(dev, ev, EV_LED, i, new ? 1 : 0);
377 set_bit_state(dev->led_values, i, new);
382 return rc ? -errno : 0;
385 sync_abs_state(struct libevdev *dev)
390 for (i = ABS_X; i <= ABS_MAX; i++) {
391 struct input_absinfo abs_info;
393 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
396 if (!bit_is_set(dev->abs_bits, i))
399 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
403 if (dev->abs_info[i].value != abs_info.value) {
404 struct input_event *ev = queue_push(dev);
406 init_event(dev, ev, EV_ABS, i, abs_info.value);
407 dev->abs_info[i].value = abs_info.value;
413 return rc ? -errno : 0;
417 sync_mt_state(struct libevdev *dev, int create_events)
424 } mt_state[ABS_MT_CNT];
426 for (i = ABS_MT_MIN; i < ABS_MT_MAX; i++) {
428 if (i == ABS_MT_SLOT)
431 if (!libevdev_has_event_code(dev, EV_ABS, i))
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]);
441 for (i = 0; i < dev->num_slots; i++) {
443 struct input_event *ev;
446 ev = queue_push(dev);
447 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, i);
450 for (j = ABS_MT_MIN; j < ABS_MT_MAX; j++) {
451 int jdx = j - ABS_MT_MIN;
453 if (j == ABS_MT_SLOT)
456 if (!libevdev_has_event_code(dev, EV_ABS, j))
459 if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
463 ev = queue_push(dev);
464 init_event(dev, ev, EV_ABS, j, mt_state[jdx].val[i]);
466 dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
472 return rc ? -errno : 0;
476 sync_state(struct libevdev *dev)
480 struct input_event *ev;
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
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.
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)
500 queue_shift_multiple(dev, i + 1, NULL);
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);
513 dev->queue_nsync = queue_num_elements(dev);
515 if (dev->queue_nsync > 0) {
516 ev = queue_push(dev);
517 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
525 update_key_state(struct libevdev *dev, const struct input_event *e)
527 if (!libevdev_has_event_type(dev, EV_KEY))
530 if (e->code > KEY_MAX)
533 set_bit_state(dev->key_values, e->code, e->value != 0);
539 update_mt_state(struct libevdev *dev, const struct input_event *e)
541 if (e->code == ABS_MT_SLOT) {
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];
551 } else if (dev->current_slot == -1)
554 dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
560 update_abs_state(struct libevdev *dev, const struct input_event *e)
562 if (!libevdev_has_event_type(dev, EV_ABS))
565 if (e->code > ABS_MAX)
568 if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
569 update_mt_state(dev, e);
571 dev->abs_info[e->code].value = e->value;
577 update_led_state(struct libevdev *dev, const struct input_event *e)
579 if (!libevdev_has_event_type(dev, EV_LED))
582 if (e->code > LED_MAX)
585 set_bit_state(dev->led_values, e->code, e->value != 0);
591 update_sw_state(struct libevdev *dev, const struct input_event *e)
593 if (!libevdev_has_event_type(dev, EV_SW))
596 if (e->code > SW_MAX)
599 set_bit_state(dev->sw_values, e->code, e->value != 0);
605 update_state(struct libevdev *dev, const struct input_event *e)
614 rc = update_key_state(dev, e);
617 rc = update_abs_state(dev, e);
620 rc = update_led_state(dev, e);
623 rc = update_sw_state(dev, e);
627 dev->last_event_time = e->time;
633 read_more_events(struct libevdev *dev)
637 struct input_event *next;
639 free_elem = queue_num_free_elements(dev);
643 next = queue_next_element(dev);
644 len = read(dev->fd, next, free_elem * sizeof(struct input_event));
647 } else if (len > 0 && len % sizeof(struct input_event) != 0)
650 int nev = len/sizeof(struct input_event);
651 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
658 libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
665 if (!(flags & (LIBEVDEV_READ_NORMAL|LIBEVDEV_READ_SYNC|LIBEVDEV_FORCE_SYNC)))
668 if (flags & LIBEVDEV_READ_SYNC) {
669 if (dev->sync_state == SYNC_NEEDED) {
670 rc = sync_state(dev);
673 dev->sync_state = SYNC_IN_PROGRESS;
676 if (dev->queue_nsync == 0) {
677 dev->sync_state = SYNC_NONE;
681 } else if (dev->sync_state != SYNC_NONE) {
682 struct input_event e;
684 /* call update_state for all events here, otherwise the library has the wrong view
686 while (queue_shift(dev, &e) == 0) {
688 update_state(dev, &e);
691 dev->sync_state = SYNC_NONE;
694 /* FIXME: if the first event after SYNC_IN_PROGRESS is a SYN_DROPPED, log this */
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.
699 Except if the fd is in blocking mode and we still have events from the last read, don't
703 if (!(flags & LIBEVDEV_READ_BLOCKING) ||
704 queue_num_elements(dev) == 0) {
705 rc = read_more_events(dev);
706 if (rc < 0 && rc != -EAGAIN)
710 if (flags & LIBEVDEV_FORCE_SYNC) {
711 dev->sync_state = SYNC_NEEDED;
717 if (queue_shift(dev, ev) != 0)
720 update_state(dev, ev);
722 /* if we disabled a code, get the next event instead */
723 } while(!libevdev_has_event_code(dev, ev->type, ev->code));
726 if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
727 dev->sync_state = SYNC_NEEDED;
731 if (flags & LIBEVDEV_READ_SYNC && dev->queue_nsync > 0) {
734 if (dev->queue_nsync == 0)
735 dev->sync_state = SYNC_NONE;
743 libevdev_has_event_pending(struct libevdev *dev)
745 struct pollfd fds = { dev->fd, POLLIN, 0 };
751 if (queue_num_elements(dev) != 0)
754 rc = poll(&fds, 1, 0);
755 return (rc >= 0) ? rc : -errno;
758 LIBEVDEV_EXPORT const char *
759 libevdev_get_name(const struct libevdev *dev)
761 return dev->name ? dev->name : "";
764 LIBEVDEV_EXPORT const char *
765 libevdev_get_phys(const struct libevdev *dev)
770 LIBEVDEV_EXPORT const char *
771 libevdev_get_uniq(const struct libevdev *dev)
776 #define STRING_SETTER(field) \
777 LIBEVDEV_EXPORT void libevdev_set_##field(struct libevdev *dev, const char *field) \
782 dev->field = strdup(field); \
790 #define PRODUCT_GETTER(name) \
791 LIBEVDEV_EXPORT int libevdev_get_id_##name(const struct libevdev *dev) \
793 return dev->ids.name; \
796 PRODUCT_GETTER(product);
797 PRODUCT_GETTER(vendor);
798 PRODUCT_GETTER(bustype);
799 PRODUCT_GETTER(version);
801 #define PRODUCT_SETTER(field) \
802 LIBEVDEV_EXPORT void libevdev_set_id_##field(struct libevdev *dev, int field) \
804 dev->ids.field = field;\
807 PRODUCT_SETTER(product);
808 PRODUCT_SETTER(vendor);
809 PRODUCT_SETTER(bustype);
810 PRODUCT_SETTER(version);
813 libevdev_get_driver_version(const struct libevdev *dev)
815 return dev->driver_version;
819 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
821 return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
825 libevdev_enable_property(struct libevdev *dev, unsigned int prop)
827 if (prop > INPUT_PROP_MAX)
830 set_bit(dev->props, prop);
835 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
837 return (type <= EV_MAX) && bit_is_set(dev->bits, type);
841 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
843 const unsigned long *mask;
846 if (!libevdev_has_event_type(dev, type))
852 max = type_to_mask_const(dev, type, &mask);
854 if (max == -1 || code > (unsigned int)max)
857 return bit_is_set(mask, code);
861 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
865 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
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;
882 libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value)
885 struct input_event e;
887 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
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;
908 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
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);
919 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
921 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
924 if (dev->num_slots < 0 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
927 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
930 return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
934 libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value)
936 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
939 if (slot >= dev->num_slots || slot >= MAX_SLOTS)
942 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
945 if (code == ABS_MT_SLOT) {
946 if (value < 0 || value >= libevdev_get_num_slots(dev))
948 dev->current_slot = value;
951 dev->mt_slot_vals[slot][code - ABS_MT_MIN] = value;
958 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
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);
971 libevdev_get_num_slots(const struct libevdev *dev)
973 return dev->num_slots;
977 libevdev_get_current_slot(const struct libevdev *dev)
979 return dev->current_slot;
982 LIBEVDEV_EXPORT const struct input_absinfo*
983 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
985 if (!libevdev_has_event_type(dev, EV_ABS) ||
986 !libevdev_has_event_code(dev, EV_ABS, code))
989 return &dev->abs_info[code];
992 #define ABS_GETTER(name) \
993 LIBEVDEV_EXPORT int libevdev_get_abs_##name(const struct libevdev *dev, unsigned int code) \
995 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code); \
996 return absinfo ? absinfo->name : 0; \
1000 ABS_GETTER(minimum);
1003 ABS_GETTER(resolution);
1005 #define ABS_SETTER(field) \
1006 LIBEVDEV_EXPORT void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
1008 if (!libevdev_has_event_code(dev, EV_ABS, code)) \
1010 dev->abs_info[code].field = val; \
1017 ABS_SETTER(resolution)
1019 LIBEVDEV_EXPORT void
1020 libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1022 if (!libevdev_has_event_code(dev, EV_ABS, code))
1025 dev->abs_info[code] = *abs;
1029 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
1034 if (libevdev_has_event_type(dev, type))
1037 set_bit(dev->bits, type);
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);
1048 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
1050 if (type > EV_MAX || type == EV_SYN)
1053 clear_bit(dev->bits, type);
1059 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
1060 unsigned int code, const void *data)
1063 unsigned long *mask = NULL;
1065 if (libevdev_enable_event_type(dev, type))
1082 max = type_to_mask(dev, type, &mask);
1087 set_bit(mask, code);
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;
1101 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
1104 unsigned long *mask = NULL;
1109 max = type_to_mask(dev, type, &mask);
1114 clear_bit(mask, code);
1120 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1122 return libevdev_kernel_set_abs_info(dev, code, abs);
1126 libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1133 rc = ioctl(dev->fd, EVIOCSABS(code), abs);
1137 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
1143 libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
1147 if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB)
1150 if (grab == dev->grabbed)
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);
1159 dev->grabbed = grab;
1161 return rc < 0 ? -errno : 0;
1165 libevdev_is_event_type(const struct input_event *ev, unsigned int type)
1167 return type < EV_CNT && ev->type == type;
1171 libevdev_is_event_code(const struct input_event *ev, unsigned int type, unsigned int code)
1175 if (!libevdev_is_event_type(ev, type))
1178 max = libevdev_get_event_type_max(type);
1179 return (max > -1 && code <= (unsigned int)max && ev->code == code);
1182 LIBEVDEV_EXPORT const char*
1183 libevdev_get_event_type_name(unsigned int type)
1188 return ev_map[type];
1191 LIBEVDEV_EXPORT const char*
1192 libevdev_get_event_code_name(unsigned int type, unsigned int code)
1194 int max = libevdev_get_event_type_max(type);
1196 if (max == -1 || code > (unsigned int)max)
1199 return event_type_map[type][code];
1202 LIBEVDEV_EXPORT const char*
1203 libevdev_get_property_name(unsigned int prop)
1205 if (prop > INPUT_PROP_MAX)
1208 return input_prop_map[prop];
1212 libevdev_get_event_type_max(unsigned int type)
1217 return ev_max[type];
1221 libevdev_get_repeat(struct libevdev *dev, int *delay, int *period)
1223 if (!libevdev_has_event_type(dev, EV_REP))
1227 *delay = dev->rep_values[REP_DELAY];
1229 *period = dev->rep_values[REP_PERIOD];
1235 libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value)
1237 return libevdev_kernel_set_led_values(dev, code, value, -1);
1241 libevdev_kernel_set_led_values(struct libevdev *dev, ...)
1243 struct input_event ev[LED_MAX + 1];
1244 enum libevdev_led_value val;
1250 memset(ev, 0, sizeof(ev));
1252 va_start(args, dev);
1253 code = va_arg(args, unsigned int);
1254 while (code != -1) {
1255 if (code > LED_MAX) {
1259 val = va_arg(args, enum libevdev_led_value);
1260 if (val != LIBEVDEV_LED_ON && val != LIBEVDEV_LED_OFF) {
1265 if (libevdev_has_event_code(dev, EV_LED, code)) {
1266 struct input_event *e = ev;
1268 while (e->type > 0 && e->code != code)
1275 e->value = (val == LIBEVDEV_LED_ON);
1277 code = va_arg(args, unsigned int);
1281 if (rc == 0 && nleds > 0) {
1282 ev[nleds].type = EV_SYN;
1283 ev[nleds++].code = SYN_REPORT;
1285 rc = write(libevdev_get_fd(dev), ev, nleds * sizeof(ev[0]));
1287 nleds--; /* last is EV_SYN */
1289 update_led_state(dev, &ev[nleds]);
1291 rc = (rc != -1) ? 0 : -errno;