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_noop_log_func(enum libevdev_log_priority priority,
54 const char *file, int line, const char *func,
55 const char *format, va_list args)
60 * Global logging settings.
62 struct logdata log_data = {
64 libevdev_noop_log_func,
69 log_msg(enum libevdev_log_priority priority,
71 const char *file, int line, const char *func,
72 const char *format, ...)
76 va_start(args, format);
77 log_data.handler(priority, data, file, line, func, format, args);
81 LIBEVDEV_EXPORT struct libevdev*
86 dev = calloc(1, sizeof(*dev));
91 dev->current_slot = -1;
92 dev->grabbed = LIBEVDEV_UNGRAB;
93 dev->sync_state = SYNC_NONE;
99 libevdev_new_from_fd(int fd, struct libevdev **dev)
108 rc = libevdev_set_fd(d, fd);
117 libevdev_free(struct libevdev *dev)
131 libevdev_set_log_handler(struct libevdev *dev, libevdev_log_func_t logfunc)
133 /* Can't be backwards compatible to this yet, so don't even try */
134 fprintf(stderr, "libevdev: ABI change. Log function will not be honored.\n");
138 libevdev_set_log_function(libevdev_log_func_t logfunc, void *data)
140 log_data.handler = logfunc ? logfunc : libevdev_noop_log_func;
141 log_data.userdata = data;
145 libevdev_set_log_priority(enum libevdev_log_priority priority)
147 if (priority > LIBEVDEV_LOG_DEBUG)
148 priority = LIBEVDEV_LOG_DEBUG;
149 log_data.priority = priority;
152 LIBEVDEV_EXPORT enum libevdev_log_priority
153 libevdev_get_log_priority(void)
155 return log_data.priority;
159 libevdev_change_fd(struct libevdev *dev, int fd)
168 libevdev_set_fd(struct libevdev* dev, int fd)
177 rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
181 memset(buf, 0, sizeof(buf));
182 rc = ioctl(fd, EVIOCGNAME(sizeof(buf) - 1), buf);
187 dev->name = strdup(buf);
195 memset(buf, 0, sizeof(buf));
196 rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
198 /* uinput has no phys */
202 dev->phys = strdup(buf);
211 memset(buf, 0, sizeof(buf));
212 rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
217 dev->uniq = strdup(buf);
224 rc = ioctl(fd, EVIOCGID, &dev->ids);
228 rc = ioctl(fd, EVIOCGVERSION, &dev->driver_version);
232 rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
236 rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
240 rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
244 rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
248 rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
252 rc = ioctl(fd, EVIOCGBIT(EV_SW, sizeof(dev->sw_bits)), dev->sw_bits);
256 rc = ioctl(fd, EVIOCGBIT(EV_MSC, sizeof(dev->msc_bits)), dev->msc_bits);
260 rc = ioctl(fd, EVIOCGBIT(EV_FF, sizeof(dev->ff_bits)), dev->ff_bits);
264 rc = ioctl(fd, EVIOCGBIT(EV_SND, sizeof(dev->snd_bits)), dev->snd_bits);
268 rc = ioctl(fd, EVIOCGKEY(sizeof(dev->key_values)), dev->key_values);
272 rc = ioctl(fd, EVIOCGLED(sizeof(dev->led_values)), dev->led_values);
276 rc = ioctl(fd, EVIOCGSW(sizeof(dev->sw_values)), dev->sw_values);
280 /* rep is a special case, always set it to 1 for both values if EV_REP is set */
281 if (bit_is_set(dev->bits, EV_REP)) {
282 for (i = 0; i < REP_CNT; i++)
283 set_bit(dev->rep_bits, i);
284 rc = ioctl(fd, EVIOCGREP, dev->rep_values);
289 for (i = ABS_X; i <= ABS_MAX; i++) {
290 if (bit_is_set(dev->abs_bits, i)) {
291 struct input_absinfo abs_info;
292 rc = ioctl(fd, EVIOCGABS(i), &abs_info);
296 dev->abs_info[i] = abs_info;
297 if (i == ABS_MT_SLOT) {
298 dev->num_slots = abs_info.maximum + 1;
299 dev->current_slot = abs_info.value;
306 sync_mt_state(dev, 0);
308 rc = init_event_queue(dev);
314 /* not copying key state because we won't know when we'll start to
315 * use this fd and key's are likely to change state by then.
316 * Same with the valuators, really, but they may not change.
320 return rc ? -errno : 0;
324 libevdev_get_fd(const struct libevdev* dev)
330 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
332 ev->time = dev->last_event_time;
339 sync_key_state(struct libevdev *dev)
343 unsigned long keystate[NLONGS(KEY_CNT)] = {0};
345 rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
349 for (i = 0; i < KEY_CNT; i++) {
351 old = bit_is_set(dev->key_values, i);
352 new = bit_is_set(keystate, i);
354 struct input_event *ev = queue_push(dev);
355 init_event(dev, ev, EV_KEY, i, new ? 1 : 0);
359 memcpy(dev->key_values, keystate, rc);
363 return rc ? -errno : 0;
367 sync_sw_state(struct libevdev *dev)
371 unsigned long swstate[NLONGS(SW_CNT)] = {0};
373 rc = ioctl(dev->fd, EVIOCGSW(sizeof(swstate)), swstate);
377 for (i = 0; i < SW_CNT; i++) {
379 old = bit_is_set(dev->sw_values, i);
380 new = bit_is_set(swstate, i);
382 struct input_event *ev = queue_push(dev);
383 init_event(dev, ev, EV_SW, i, new ? 1 : 0);
387 memcpy(dev->sw_values, swstate, rc);
391 return rc ? -errno : 0;
395 sync_led_state(struct libevdev *dev)
399 unsigned long ledstate[NLONGS(LED_CNT)] = {0};
401 rc = ioctl(dev->fd, EVIOCGLED(sizeof(ledstate)), ledstate);
405 for (i = 0; i < LED_CNT; i++) {
407 old = bit_is_set(dev->led_values, i);
408 new = bit_is_set(ledstate, i);
410 struct input_event *ev = queue_push(dev);
411 init_event(dev, ev, EV_LED, i, new ? 1 : 0);
415 memcpy(dev->led_values, ledstate, rc);
419 return rc ? -errno : 0;
422 sync_abs_state(struct libevdev *dev)
427 for (i = ABS_X; i < ABS_CNT; i++) {
428 struct input_absinfo abs_info;
430 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
433 if (!bit_is_set(dev->abs_bits, i))
436 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
440 if (dev->abs_info[i].value != abs_info.value) {
441 struct input_event *ev = queue_push(dev);
443 init_event(dev, ev, EV_ABS, i, abs_info.value);
444 dev->abs_info[i].value = abs_info.value;
450 return rc ? -errno : 0;
454 sync_mt_state(struct libevdev *dev, int create_events)
461 } mt_state[ABS_MT_CNT];
463 for (i = ABS_MT_MIN; i <= ABS_MT_MAX; i++) {
465 if (i == ABS_MT_SLOT)
468 if (!libevdev_has_event_code(dev, EV_ABS, i))
471 idx = i - ABS_MT_MIN;
472 mt_state[idx].code = i;
473 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
478 for (i = 0; i < dev->num_slots; i++) {
480 struct input_event *ev;
483 ev = queue_push(dev);
484 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, i);
487 for (j = ABS_MT_MIN; j <= ABS_MT_MAX; j++) {
488 int jdx = j - ABS_MT_MIN;
490 if (j == ABS_MT_SLOT)
493 if (!libevdev_has_event_code(dev, EV_ABS, j))
496 if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
500 ev = queue_push(dev);
501 init_event(dev, ev, EV_ABS, j, mt_state[jdx].val[i]);
503 dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
509 return rc ? -errno : 0;
513 sync_state(struct libevdev *dev)
517 struct input_event *ev;
519 /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
520 queue[0]) we need to shift this backwards. Except that chances are that the
521 queue may be either full or too full to prepend all the events needed for
524 so we search for the last sync event in the queue and drop everything before
525 including that event and rely on the kernel to tell us the right value for that
526 bitfield during the sync process.
529 for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
530 struct input_event e = {{0,0}, 0, 0, 0};
531 queue_peek(dev, i, &e);
532 if (e.type == EV_SYN)
537 queue_shift_multiple(dev, i + 1, NULL);
539 if (libevdev_has_event_type(dev, EV_KEY))
540 rc = sync_key_state(dev);
541 if (libevdev_has_event_type(dev, EV_LED))
542 rc = sync_led_state(dev);
543 if (libevdev_has_event_type(dev, EV_SW))
544 rc = sync_sw_state(dev);
545 if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
546 rc = sync_abs_state(dev);
547 if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
548 rc = sync_mt_state(dev, 1);
550 dev->queue_nsync = queue_num_elements(dev);
552 if (dev->queue_nsync > 0) {
553 ev = queue_push(dev);
554 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
562 update_key_state(struct libevdev *dev, const struct input_event *e)
564 if (!libevdev_has_event_type(dev, EV_KEY))
567 if (e->code > KEY_MAX)
570 set_bit_state(dev->key_values, e->code, e->value != 0);
576 update_mt_state(struct libevdev *dev, const struct input_event *e)
578 if (e->code == ABS_MT_SLOT) {
580 dev->current_slot = e->value;
581 /* sync abs_info with the current slot values */
582 for (i = ABS_MT_SLOT + 1; i <= ABS_MT_MAX; i++) {
583 if (libevdev_has_event_code(dev, EV_ABS, i))
584 dev->abs_info[i].value = dev->mt_slot_vals[dev->current_slot][i - ABS_MT_MIN];
588 } else if (dev->current_slot == -1)
591 dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
597 update_abs_state(struct libevdev *dev, const struct input_event *e)
599 if (!libevdev_has_event_type(dev, EV_ABS))
602 if (e->code > ABS_MAX)
605 if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
606 update_mt_state(dev, e);
608 dev->abs_info[e->code].value = e->value;
614 update_led_state(struct libevdev *dev, const struct input_event *e)
616 if (!libevdev_has_event_type(dev, EV_LED))
619 if (e->code > LED_MAX)
622 set_bit_state(dev->led_values, e->code, e->value != 0);
628 update_sw_state(struct libevdev *dev, const struct input_event *e)
630 if (!libevdev_has_event_type(dev, EV_SW))
633 if (e->code > SW_MAX)
636 set_bit_state(dev->sw_values, e->code, e->value != 0);
642 update_state(struct libevdev *dev, const struct input_event *e)
651 rc = update_key_state(dev, e);
654 rc = update_abs_state(dev, e);
657 rc = update_led_state(dev, e);
660 rc = update_sw_state(dev, e);
664 dev->last_event_time = e->time;
670 read_more_events(struct libevdev *dev)
674 struct input_event *next;
676 free_elem = queue_num_free_elements(dev);
680 next = queue_next_element(dev);
681 len = read(dev->fd, next, free_elem * sizeof(struct input_event));
684 } else if (len > 0 && len % sizeof(struct input_event) != 0)
687 int nev = len/sizeof(struct input_event);
688 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
695 libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
702 if (!(flags & (LIBEVDEV_READ_NORMAL|LIBEVDEV_READ_SYNC|LIBEVDEV_FORCE_SYNC)))
705 if (flags & LIBEVDEV_READ_SYNC) {
706 if (dev->sync_state == SYNC_NEEDED) {
707 rc = sync_state(dev);
710 dev->sync_state = SYNC_IN_PROGRESS;
713 if (dev->queue_nsync == 0) {
714 dev->sync_state = SYNC_NONE;
718 } else if (dev->sync_state != SYNC_NONE) {
719 struct input_event e;
721 /* call update_state for all events here, otherwise the library has the wrong view
723 while (queue_shift(dev, &e) == 0) {
725 update_state(dev, &e);
728 dev->sync_state = SYNC_NONE;
731 /* FIXME: if the first event after SYNC_IN_PROGRESS is a SYN_DROPPED, log this */
733 /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
734 worst case we don't read fast enough and end up with SYN_DROPPED anyway.
736 Except if the fd is in blocking mode and we still have events from the last read, don't
740 if (!(flags & LIBEVDEV_READ_BLOCKING) ||
741 queue_num_elements(dev) == 0) {
742 rc = read_more_events(dev);
743 if (rc < 0 && rc != -EAGAIN)
747 if (flags & LIBEVDEV_FORCE_SYNC) {
748 dev->sync_state = SYNC_NEEDED;
754 if (queue_shift(dev, ev) != 0)
757 update_state(dev, ev);
759 /* if we disabled a code, get the next event instead */
760 } while(!libevdev_has_event_code(dev, ev->type, ev->code));
763 if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
764 dev->sync_state = SYNC_NEEDED;
768 if (flags & LIBEVDEV_READ_SYNC && dev->queue_nsync > 0) {
771 if (dev->queue_nsync == 0)
772 dev->sync_state = SYNC_NONE;
780 libevdev_has_event_pending(struct libevdev *dev)
782 struct pollfd fds = { dev->fd, POLLIN, 0 };
788 if (queue_num_elements(dev) != 0)
791 rc = poll(&fds, 1, 0);
792 return (rc >= 0) ? rc : -errno;
795 LIBEVDEV_EXPORT const char *
796 libevdev_get_name(const struct libevdev *dev)
798 return dev->name ? dev->name : "";
801 LIBEVDEV_EXPORT const char *
802 libevdev_get_phys(const struct libevdev *dev)
807 LIBEVDEV_EXPORT const char *
808 libevdev_get_uniq(const struct libevdev *dev)
813 #define STRING_SETTER(field) \
814 LIBEVDEV_EXPORT void libevdev_set_##field(struct libevdev *dev, const char *field) \
819 dev->field = strdup(field); \
827 #define PRODUCT_GETTER(name) \
828 LIBEVDEV_EXPORT int libevdev_get_id_##name(const struct libevdev *dev) \
830 return dev->ids.name; \
833 PRODUCT_GETTER(product);
834 PRODUCT_GETTER(vendor);
835 PRODUCT_GETTER(bustype);
836 PRODUCT_GETTER(version);
838 #define PRODUCT_SETTER(field) \
839 LIBEVDEV_EXPORT void libevdev_set_id_##field(struct libevdev *dev, int field) \
841 dev->ids.field = field;\
844 PRODUCT_SETTER(product);
845 PRODUCT_SETTER(vendor);
846 PRODUCT_SETTER(bustype);
847 PRODUCT_SETTER(version);
850 libevdev_get_driver_version(const struct libevdev *dev)
852 return dev->driver_version;
856 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
858 return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
862 libevdev_enable_property(struct libevdev *dev, unsigned int prop)
864 if (prop > INPUT_PROP_MAX)
867 set_bit(dev->props, prop);
872 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
874 return (type <= EV_MAX) && bit_is_set(dev->bits, type);
878 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
880 const unsigned long *mask;
883 if (!libevdev_has_event_type(dev, type))
889 max = type_to_mask_const(dev, type, &mask);
891 if (max == -1 || code > (unsigned int)max)
894 return bit_is_set(mask, code);
898 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
902 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
906 case EV_ABS: value = dev->abs_info[code].value; break;
907 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
908 case EV_LED: value = bit_is_set(dev->led_values, code); break;
909 case EV_SW: value = bit_is_set(dev->sw_values, code); break;
919 libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value)
922 struct input_event e;
924 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
932 case EV_ABS: rc = update_abs_state(dev, &e); break;
933 case EV_KEY: rc = update_key_state(dev, &e); break;
934 case EV_LED: rc = update_led_state(dev, &e); break;
935 case EV_SW: rc = update_sw_state(dev, &e); break;
945 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
947 if (libevdev_has_event_type(dev, type) &&
948 libevdev_has_event_code(dev, type, code)) {
949 *value = libevdev_get_event_value(dev, type, code);
956 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
958 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
961 if (dev->num_slots < 0 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
964 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
967 return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
971 libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value)
973 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
976 if (dev->num_slots == -1 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
979 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
982 if (code == ABS_MT_SLOT) {
983 if (value < 0 || value >= libevdev_get_num_slots(dev))
985 dev->current_slot = value;
988 dev->mt_slot_vals[slot][code - ABS_MT_MIN] = value;
995 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
997 if (libevdev_has_event_type(dev, EV_ABS) &&
998 libevdev_has_event_code(dev, EV_ABS, code) &&
999 dev->num_slots >= 0 &&
1000 slot < (unsigned int)dev->num_slots && slot < MAX_SLOTS) {
1001 *value = libevdev_get_slot_value(dev, slot, code);
1008 libevdev_get_num_slots(const struct libevdev *dev)
1010 return dev->num_slots;
1014 libevdev_get_current_slot(const struct libevdev *dev)
1016 return dev->current_slot;
1019 LIBEVDEV_EXPORT const struct input_absinfo*
1020 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
1022 if (!libevdev_has_event_type(dev, EV_ABS) ||
1023 !libevdev_has_event_code(dev, EV_ABS, code))
1026 return &dev->abs_info[code];
1029 #define ABS_GETTER(name) \
1030 LIBEVDEV_EXPORT int libevdev_get_abs_##name(const struct libevdev *dev, unsigned int code) \
1032 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code); \
1033 return absinfo ? absinfo->name : 0; \
1036 ABS_GETTER(maximum);
1037 ABS_GETTER(minimum);
1040 ABS_GETTER(resolution);
1042 #define ABS_SETTER(field) \
1043 LIBEVDEV_EXPORT void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
1045 if (!libevdev_has_event_code(dev, EV_ABS, code)) \
1047 dev->abs_info[code].field = val; \
1054 ABS_SETTER(resolution)
1056 LIBEVDEV_EXPORT void
1057 libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1059 if (!libevdev_has_event_code(dev, EV_ABS, code))
1062 dev->abs_info[code] = *abs;
1066 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
1071 if (libevdev_has_event_type(dev, type))
1074 set_bit(dev->bits, type);
1076 if (type == EV_REP) {
1077 int delay = 0, period = 0;
1078 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
1079 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
1085 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
1087 if (type > EV_MAX || type == EV_SYN)
1090 clear_bit(dev->bits, type);
1096 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
1097 unsigned int code, const void *data)
1100 unsigned long *mask = NULL;
1102 if (libevdev_enable_event_type(dev, type))
1119 max = type_to_mask(dev, type, &mask);
1124 set_bit(mask, code);
1126 if (type == EV_ABS) {
1127 const struct input_absinfo *abs = data;
1128 dev->abs_info[code] = *abs;
1129 } else if (type == EV_REP) {
1130 const int *value = data;
1131 dev->rep_values[code] = *value;
1138 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
1141 unsigned long *mask = NULL;
1146 max = type_to_mask(dev, type, &mask);
1151 clear_bit(mask, code);
1157 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1159 return libevdev_kernel_set_abs_info(dev, code, abs);
1163 libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1173 rc = ioctl(dev->fd, EVIOCSABS(code), abs);
1177 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
1183 libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
1190 if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB)
1193 if (grab == dev->grabbed)
1196 if (grab == LIBEVDEV_GRAB)
1197 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
1198 else if (grab == LIBEVDEV_UNGRAB)
1199 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
1202 dev->grabbed = grab;
1204 return rc < 0 ? -errno : 0;
1208 libevdev_is_event_type(const struct input_event *ev, unsigned int type)
1210 return type < EV_CNT && ev->type == type;
1214 libevdev_is_event_code(const struct input_event *ev, unsigned int type, unsigned int code)
1218 if (!libevdev_is_event_type(ev, type))
1221 max = libevdev_get_event_type_max(type);
1222 return (max > -1 && code <= (unsigned int)max && ev->code == code);
1225 LIBEVDEV_EXPORT const char*
1226 libevdev_get_event_type_name(unsigned int type)
1231 return ev_map[type];
1234 LIBEVDEV_EXPORT const char*
1235 libevdev_get_event_code_name(unsigned int type, unsigned int code)
1237 int max = libevdev_get_event_type_max(type);
1239 if (max == -1 || code > (unsigned int)max)
1242 return event_type_map[type][code];
1245 LIBEVDEV_EXPORT const char*
1246 libevdev_get_property_name(unsigned int prop)
1248 if (prop > INPUT_PROP_MAX)
1251 return input_prop_map[prop];
1255 libevdev_get_event_type_max(unsigned int type)
1260 return ev_max[type];
1264 libevdev_get_repeat(struct libevdev *dev, int *delay, int *period)
1266 if (!libevdev_has_event_type(dev, EV_REP))
1270 *delay = dev->rep_values[REP_DELAY];
1272 *period = dev->rep_values[REP_PERIOD];
1278 libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value)
1280 return libevdev_kernel_set_led_values(dev, code, value, -1);
1284 libevdev_kernel_set_led_values(struct libevdev *dev, ...)
1286 struct input_event ev[LED_MAX + 1];
1287 enum libevdev_led_value val;
1296 memset(ev, 0, sizeof(ev));
1298 va_start(args, dev);
1299 code = va_arg(args, unsigned int);
1300 while (code != -1) {
1301 if (code > LED_MAX) {
1305 val = va_arg(args, enum libevdev_led_value);
1306 if (val != LIBEVDEV_LED_ON && val != LIBEVDEV_LED_OFF) {
1311 if (libevdev_has_event_code(dev, EV_LED, code)) {
1312 struct input_event *e = ev;
1314 while (e->type > 0 && e->code != code)
1321 e->value = (val == LIBEVDEV_LED_ON);
1323 code = va_arg(args, unsigned int);
1327 if (rc == 0 && nleds > 0) {
1328 ev[nleds].type = EV_SYN;
1329 ev[nleds++].code = SYN_REPORT;
1331 rc = write(libevdev_get_fd(dev), ev, nleds * sizeof(ev[0]));
1333 nleds--; /* last is EV_SYN */
1335 update_led_state(dev, &ev[nleds]);
1337 rc = (rc != -1) ? 0 : -errno;