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)] = {0};
311 rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
315 for (i = 0; i < KEY_CNT; 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);
325 memcpy(dev->key_values, keystate, rc);
329 return rc ? -errno : 0;
333 sync_sw_state(struct libevdev *dev)
337 unsigned long swstate[NLONGS(SW_CNT)] = {0};
339 rc = ioctl(dev->fd, EVIOCGSW(sizeof(swstate)), swstate);
343 for (i = 0; i < SW_CNT; i++) {
345 old = bit_is_set(dev->sw_values, i);
346 new = bit_is_set(swstate, i);
348 struct input_event *ev = queue_push(dev);
349 init_event(dev, ev, EV_SW, i, new ? 1 : 0);
353 memcpy(dev->sw_values, swstate, rc);
357 return rc ? -errno : 0;
361 sync_led_state(struct libevdev *dev)
365 unsigned long ledstate[NLONGS(LED_CNT)] = {0};
367 rc = ioctl(dev->fd, EVIOCGLED(sizeof(ledstate)), ledstate);
371 for (i = 0; i < LED_CNT; i++) {
373 old = bit_is_set(dev->led_values, i);
374 new = bit_is_set(ledstate, i);
376 struct input_event *ev = queue_push(dev);
377 init_event(dev, ev, EV_LED, i, new ? 1 : 0);
381 memcpy(dev->led_values, ledstate, rc);
385 return rc ? -errno : 0;
388 sync_abs_state(struct libevdev *dev)
393 for (i = ABS_X; i < ABS_CNT; i++) {
394 struct input_absinfo abs_info;
396 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
399 if (!bit_is_set(dev->abs_bits, i))
402 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
406 if (dev->abs_info[i].value != abs_info.value) {
407 struct input_event *ev = queue_push(dev);
409 init_event(dev, ev, EV_ABS, i, abs_info.value);
410 dev->abs_info[i].value = abs_info.value;
416 return rc ? -errno : 0;
420 sync_mt_state(struct libevdev *dev, int create_events)
427 } mt_state[ABS_MT_CNT];
429 for (i = ABS_MT_MIN; i <= ABS_MT_MAX; i++) {
431 if (i == ABS_MT_SLOT)
434 if (!libevdev_has_event_code(dev, EV_ABS, i))
437 idx = i - ABS_MT_MIN;
438 mt_state[idx].code = i;
439 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
444 for (i = 0; i < dev->num_slots; i++) {
446 struct input_event *ev;
449 ev = queue_push(dev);
450 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, i);
453 for (j = ABS_MT_MIN; j <= ABS_MT_MAX; j++) {
454 int jdx = j - ABS_MT_MIN;
456 if (j == ABS_MT_SLOT)
459 if (!libevdev_has_event_code(dev, EV_ABS, j))
462 if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
466 ev = queue_push(dev);
467 init_event(dev, ev, EV_ABS, j, mt_state[jdx].val[i]);
469 dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
475 return rc ? -errno : 0;
479 sync_state(struct libevdev *dev)
483 struct input_event *ev;
485 /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
486 queue[0]) we need to shift this backwards. Except that chances are that the
487 queue may be either full or too full to prepend all the events needed for
490 so we search for the last sync event in the queue and drop everything before
491 including that event and rely on the kernel to tell us the right value for that
492 bitfield during the sync process.
495 for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
496 struct input_event e = {{0,0}, 0, 0, 0};
497 queue_peek(dev, i, &e);
498 if (e.type == EV_SYN)
503 queue_shift_multiple(dev, i + 1, NULL);
505 if (libevdev_has_event_type(dev, EV_KEY))
506 rc = sync_key_state(dev);
507 if (libevdev_has_event_type(dev, EV_LED))
508 rc = sync_led_state(dev);
509 if (libevdev_has_event_type(dev, EV_SW))
510 rc = sync_sw_state(dev);
511 if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
512 rc = sync_abs_state(dev);
513 if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
514 rc = sync_mt_state(dev, 1);
516 dev->queue_nsync = queue_num_elements(dev);
518 if (dev->queue_nsync > 0) {
519 ev = queue_push(dev);
520 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
528 update_key_state(struct libevdev *dev, const struct input_event *e)
530 if (!libevdev_has_event_type(dev, EV_KEY))
533 if (e->code > KEY_MAX)
536 set_bit_state(dev->key_values, e->code, e->value != 0);
542 update_mt_state(struct libevdev *dev, const struct input_event *e)
544 if (e->code == ABS_MT_SLOT) {
546 dev->current_slot = e->value;
547 /* sync abs_info with the current slot values */
548 for (i = ABS_MT_SLOT + 1; i <= ABS_MT_MAX; i++) {
549 if (libevdev_has_event_code(dev, EV_ABS, i))
550 dev->abs_info[i].value = dev->mt_slot_vals[dev->current_slot][i - ABS_MT_MIN];
554 } else if (dev->current_slot == -1)
557 dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
563 update_abs_state(struct libevdev *dev, const struct input_event *e)
565 if (!libevdev_has_event_type(dev, EV_ABS))
568 if (e->code > ABS_MAX)
571 if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
572 update_mt_state(dev, e);
574 dev->abs_info[e->code].value = e->value;
580 update_led_state(struct libevdev *dev, const struct input_event *e)
582 if (!libevdev_has_event_type(dev, EV_LED))
585 if (e->code > LED_MAX)
588 set_bit_state(dev->led_values, e->code, e->value != 0);
594 update_sw_state(struct libevdev *dev, const struct input_event *e)
596 if (!libevdev_has_event_type(dev, EV_SW))
599 if (e->code > SW_MAX)
602 set_bit_state(dev->sw_values, e->code, e->value != 0);
608 update_state(struct libevdev *dev, const struct input_event *e)
617 rc = update_key_state(dev, e);
620 rc = update_abs_state(dev, e);
623 rc = update_led_state(dev, e);
626 rc = update_sw_state(dev, e);
630 dev->last_event_time = e->time;
636 read_more_events(struct libevdev *dev)
640 struct input_event *next;
642 free_elem = queue_num_free_elements(dev);
646 next = queue_next_element(dev);
647 len = read(dev->fd, next, free_elem * sizeof(struct input_event));
650 } else if (len > 0 && len % sizeof(struct input_event) != 0)
653 int nev = len/sizeof(struct input_event);
654 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
661 libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
668 if (!(flags & (LIBEVDEV_READ_NORMAL|LIBEVDEV_READ_SYNC|LIBEVDEV_FORCE_SYNC)))
671 if (flags & LIBEVDEV_READ_SYNC) {
672 if (dev->sync_state == SYNC_NEEDED) {
673 rc = sync_state(dev);
676 dev->sync_state = SYNC_IN_PROGRESS;
679 if (dev->queue_nsync == 0) {
680 dev->sync_state = SYNC_NONE;
684 } else if (dev->sync_state != SYNC_NONE) {
685 struct input_event e;
687 /* call update_state for all events here, otherwise the library has the wrong view
689 while (queue_shift(dev, &e) == 0) {
691 update_state(dev, &e);
694 dev->sync_state = SYNC_NONE;
697 /* FIXME: if the first event after SYNC_IN_PROGRESS is a SYN_DROPPED, log this */
699 /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
700 worst case we don't read fast enough and end up with SYN_DROPPED anyway.
702 Except if the fd is in blocking mode and we still have events from the last read, don't
706 if (!(flags & LIBEVDEV_READ_BLOCKING) ||
707 queue_num_elements(dev) == 0) {
708 rc = read_more_events(dev);
709 if (rc < 0 && rc != -EAGAIN)
713 if (flags & LIBEVDEV_FORCE_SYNC) {
714 dev->sync_state = SYNC_NEEDED;
720 if (queue_shift(dev, ev) != 0)
723 update_state(dev, ev);
725 /* if we disabled a code, get the next event instead */
726 } while(!libevdev_has_event_code(dev, ev->type, ev->code));
729 if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
730 dev->sync_state = SYNC_NEEDED;
734 if (flags & LIBEVDEV_READ_SYNC && dev->queue_nsync > 0) {
737 if (dev->queue_nsync == 0)
738 dev->sync_state = SYNC_NONE;
746 libevdev_has_event_pending(struct libevdev *dev)
748 struct pollfd fds = { dev->fd, POLLIN, 0 };
754 if (queue_num_elements(dev) != 0)
757 rc = poll(&fds, 1, 0);
758 return (rc >= 0) ? rc : -errno;
761 LIBEVDEV_EXPORT const char *
762 libevdev_get_name(const struct libevdev *dev)
764 return dev->name ? dev->name : "";
767 LIBEVDEV_EXPORT const char *
768 libevdev_get_phys(const struct libevdev *dev)
773 LIBEVDEV_EXPORT const char *
774 libevdev_get_uniq(const struct libevdev *dev)
779 #define STRING_SETTER(field) \
780 LIBEVDEV_EXPORT void libevdev_set_##field(struct libevdev *dev, const char *field) \
785 dev->field = strdup(field); \
793 #define PRODUCT_GETTER(name) \
794 LIBEVDEV_EXPORT int libevdev_get_id_##name(const struct libevdev *dev) \
796 return dev->ids.name; \
799 PRODUCT_GETTER(product);
800 PRODUCT_GETTER(vendor);
801 PRODUCT_GETTER(bustype);
802 PRODUCT_GETTER(version);
804 #define PRODUCT_SETTER(field) \
805 LIBEVDEV_EXPORT void libevdev_set_id_##field(struct libevdev *dev, int field) \
807 dev->ids.field = field;\
810 PRODUCT_SETTER(product);
811 PRODUCT_SETTER(vendor);
812 PRODUCT_SETTER(bustype);
813 PRODUCT_SETTER(version);
816 libevdev_get_driver_version(const struct libevdev *dev)
818 return dev->driver_version;
822 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
824 return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
828 libevdev_enable_property(struct libevdev *dev, unsigned int prop)
830 if (prop > INPUT_PROP_MAX)
833 set_bit(dev->props, prop);
838 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
840 return (type <= EV_MAX) && bit_is_set(dev->bits, type);
844 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
846 const unsigned long *mask;
849 if (!libevdev_has_event_type(dev, type))
855 max = type_to_mask_const(dev, type, &mask);
857 if (max == -1 || code > (unsigned int)max)
860 return bit_is_set(mask, code);
864 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
868 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
872 case EV_ABS: value = dev->abs_info[code].value; break;
873 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
874 case EV_LED: value = bit_is_set(dev->led_values, code); break;
875 case EV_SW: value = bit_is_set(dev->sw_values, code); break;
885 libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value)
888 struct input_event e;
890 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
898 case EV_ABS: rc = update_abs_state(dev, &e); break;
899 case EV_KEY: rc = update_key_state(dev, &e); break;
900 case EV_LED: rc = update_led_state(dev, &e); break;
901 case EV_SW: rc = update_sw_state(dev, &e); break;
911 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
913 if (libevdev_has_event_type(dev, type) &&
914 libevdev_has_event_code(dev, type, code)) {
915 *value = libevdev_get_event_value(dev, type, code);
922 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
924 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
927 if (dev->num_slots < 0 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
930 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
933 return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
937 libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value)
939 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
942 if (dev->num_slots == -1 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
945 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
948 if (code == ABS_MT_SLOT) {
949 if (value < 0 || value >= libevdev_get_num_slots(dev))
951 dev->current_slot = value;
954 dev->mt_slot_vals[slot][code - ABS_MT_MIN] = value;
961 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
963 if (libevdev_has_event_type(dev, EV_ABS) &&
964 libevdev_has_event_code(dev, EV_ABS, code) &&
965 dev->num_slots >= 0 &&
966 slot < (unsigned int)dev->num_slots && slot < MAX_SLOTS) {
967 *value = libevdev_get_slot_value(dev, slot, code);
974 libevdev_get_num_slots(const struct libevdev *dev)
976 return dev->num_slots;
980 libevdev_get_current_slot(const struct libevdev *dev)
982 return dev->current_slot;
985 LIBEVDEV_EXPORT const struct input_absinfo*
986 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
988 if (!libevdev_has_event_type(dev, EV_ABS) ||
989 !libevdev_has_event_code(dev, EV_ABS, code))
992 return &dev->abs_info[code];
995 #define ABS_GETTER(name) \
996 LIBEVDEV_EXPORT int libevdev_get_abs_##name(const struct libevdev *dev, unsigned int code) \
998 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code); \
999 return absinfo ? absinfo->name : 0; \
1002 ABS_GETTER(maximum);
1003 ABS_GETTER(minimum);
1006 ABS_GETTER(resolution);
1008 #define ABS_SETTER(field) \
1009 LIBEVDEV_EXPORT void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
1011 if (!libevdev_has_event_code(dev, EV_ABS, code)) \
1013 dev->abs_info[code].field = val; \
1020 ABS_SETTER(resolution)
1022 LIBEVDEV_EXPORT void
1023 libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1025 if (!libevdev_has_event_code(dev, EV_ABS, code))
1028 dev->abs_info[code] = *abs;
1032 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
1037 if (libevdev_has_event_type(dev, type))
1040 set_bit(dev->bits, type);
1042 if (type == EV_REP) {
1043 int delay = 0, period = 0;
1044 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
1045 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
1051 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
1053 if (type > EV_MAX || type == EV_SYN)
1056 clear_bit(dev->bits, type);
1062 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
1063 unsigned int code, const void *data)
1066 unsigned long *mask = NULL;
1068 if (libevdev_enable_event_type(dev, type))
1085 max = type_to_mask(dev, type, &mask);
1090 set_bit(mask, code);
1092 if (type == EV_ABS) {
1093 const struct input_absinfo *abs = data;
1094 dev->abs_info[code] = *abs;
1095 } else if (type == EV_REP) {
1096 const int *value = data;
1097 dev->rep_values[code] = *value;
1104 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
1107 unsigned long *mask = NULL;
1112 max = type_to_mask(dev, type, &mask);
1117 clear_bit(mask, code);
1123 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1125 return libevdev_kernel_set_abs_info(dev, code, abs);
1129 libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1139 rc = ioctl(dev->fd, EVIOCSABS(code), abs);
1143 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
1149 libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
1156 if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB)
1159 if (grab == dev->grabbed)
1162 if (grab == LIBEVDEV_GRAB)
1163 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
1164 else if (grab == LIBEVDEV_UNGRAB)
1165 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
1168 dev->grabbed = grab;
1170 return rc < 0 ? -errno : 0;
1174 libevdev_is_event_type(const struct input_event *ev, unsigned int type)
1176 return type < EV_CNT && ev->type == type;
1180 libevdev_is_event_code(const struct input_event *ev, unsigned int type, unsigned int code)
1184 if (!libevdev_is_event_type(ev, type))
1187 max = libevdev_get_event_type_max(type);
1188 return (max > -1 && code <= (unsigned int)max && ev->code == code);
1191 LIBEVDEV_EXPORT const char*
1192 libevdev_get_event_type_name(unsigned int type)
1197 return ev_map[type];
1200 LIBEVDEV_EXPORT const char*
1201 libevdev_get_event_code_name(unsigned int type, unsigned int code)
1203 int max = libevdev_get_event_type_max(type);
1205 if (max == -1 || code > (unsigned int)max)
1208 return event_type_map[type][code];
1211 LIBEVDEV_EXPORT const char*
1212 libevdev_get_property_name(unsigned int prop)
1214 if (prop > INPUT_PROP_MAX)
1217 return input_prop_map[prop];
1221 libevdev_get_event_type_max(unsigned int type)
1226 return ev_max[type];
1230 libevdev_get_repeat(struct libevdev *dev, int *delay, int *period)
1232 if (!libevdev_has_event_type(dev, EV_REP))
1236 *delay = dev->rep_values[REP_DELAY];
1238 *period = dev->rep_values[REP_PERIOD];
1244 libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value)
1246 return libevdev_kernel_set_led_values(dev, code, value, -1);
1250 libevdev_kernel_set_led_values(struct libevdev *dev, ...)
1252 struct input_event ev[LED_MAX + 1];
1253 enum libevdev_led_value val;
1262 memset(ev, 0, sizeof(ev));
1264 va_start(args, dev);
1265 code = va_arg(args, unsigned int);
1266 while (code != -1) {
1267 if (code > LED_MAX) {
1271 val = va_arg(args, enum libevdev_led_value);
1272 if (val != LIBEVDEV_LED_ON && val != LIBEVDEV_LED_OFF) {
1277 if (libevdev_has_event_code(dev, EV_LED, code)) {
1278 struct input_event *e = ev;
1280 while (e->type > 0 && e->code != code)
1287 e->value = (val == LIBEVDEV_LED_ON);
1289 code = va_arg(args, unsigned int);
1293 if (rc == 0 && nleds > 0) {
1294 ev[nleds].type = EV_SYN;
1295 ev[nleds++].code = SYN_REPORT;
1297 rc = write(libevdev_get_fd(dev), ev, nleds * sizeof(ev[0]));
1299 nleds--; /* last is EV_SYN */
1301 update_led_state(dev, &ev[nleds]);
1303 rc = (rc != -1) ? 0 : -errno;