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
33 #include "libevdev-int.h"
34 #include "libevdev-util.h"
35 #include "event-names.h"
39 static int sync_mt_state(struct libevdev *dev, int create_events);
42 init_event_queue(struct libevdev *dev)
44 /* FIXME: count the number of axes, keys, etc. to get a better idea at how many events per
45 EV_SYN we could possibly get. Then multiply that by the actual buffer size we care about */
47 const int QUEUE_SIZE = 256;
49 return queue_alloc(dev, QUEUE_SIZE);
53 libevdev_dflt_log_func(enum libevdev_log_priority priority,
55 const char *file, int line, const char *func,
56 const char *format, va_list args)
60 case LIBEVDEV_LOG_ERROR: prefix = "libevdev error"; break;
61 case LIBEVDEV_LOG_INFO: prefix = "libevdev info"; break;
62 case LIBEVDEV_LOG_DEBUG:
63 prefix = "libevdev debug";
66 prefix = "libevdev INVALID LOG PRIORITY";
69 /* default logging format:
70 libevev error in libevdev_some_func: blah blah
71 libevev info in libevdev_some_func: blah blah
72 libevev debug in file.c:123:libevdev_some_func: blah blah
75 fprintf(stderr, "%s in ", prefix);
76 if (priority == LIBEVDEV_LOG_DEBUG)
77 fprintf(stderr, "%s:%d:", file, line);
78 fprintf(stderr, "%s: ", func);
79 vfprintf(stderr, format, args);
83 * Global logging settings.
85 struct logdata log_data = {
87 libevdev_dflt_log_func,
92 log_msg(enum libevdev_log_priority priority,
94 const char *file, int line, const char *func,
95 const char *format, ...)
99 if (!log_data.handler)
102 va_start(args, format);
103 log_data.handler(priority, data, file, line, func, format, args);
108 libevdev_reset(struct libevdev *dev)
110 memset(dev, 0, sizeof(*dev));
112 dev->initialized = false;
114 dev->current_slot = -1;
115 dev->grabbed = LIBEVDEV_UNGRAB;
116 dev->sync_state = SYNC_NONE;
117 libevdev_enable_event_type(dev, EV_SYN);
120 LIBEVDEV_EXPORT struct libevdev*
123 struct libevdev *dev;
125 dev = malloc(sizeof(*dev));
135 libevdev_new_from_fd(int fd, struct libevdev **dev)
144 rc = libevdev_set_fd(d, fd);
153 libevdev_free(struct libevdev *dev)
166 libevdev_set_log_function(libevdev_log_func_t logfunc, void *data)
168 log_data.handler = logfunc;
169 log_data.userdata = data;
173 libevdev_set_log_priority(enum libevdev_log_priority priority)
175 if (priority > LIBEVDEV_LOG_DEBUG)
176 priority = LIBEVDEV_LOG_DEBUG;
177 log_data.priority = priority;
180 LIBEVDEV_EXPORT enum libevdev_log_priority
181 libevdev_get_log_priority(void)
183 return log_data.priority;
187 libevdev_change_fd(struct libevdev *dev, int fd)
189 if (!dev->initialized) {
190 log_bug("device not initialized. call libevdev_set_fd() first\n");
198 libevdev_set_fd(struct libevdev* dev, int fd)
204 if (dev->initialized) {
205 log_bug("device already initialized.\n");
212 rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
216 memset(buf, 0, sizeof(buf));
217 rc = ioctl(fd, EVIOCGNAME(sizeof(buf) - 1), buf);
222 dev->name = strdup(buf);
230 memset(buf, 0, sizeof(buf));
231 rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
233 /* uinput has no phys */
237 dev->phys = strdup(buf);
246 memset(buf, 0, sizeof(buf));
247 rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
252 dev->uniq = strdup(buf);
259 rc = ioctl(fd, EVIOCGID, &dev->ids);
263 rc = ioctl(fd, EVIOCGVERSION, &dev->driver_version);
267 /* Built on a kernel with props, running against a kernel without property
268 support. This should not be a fatal case, we'll be missing properties but other
269 than that everything is as expected.
271 rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
272 if (rc < 0 && errno != EINVAL)
275 rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
279 rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
283 rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
287 rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
291 rc = ioctl(fd, EVIOCGBIT(EV_SW, sizeof(dev->sw_bits)), dev->sw_bits);
295 rc = ioctl(fd, EVIOCGBIT(EV_MSC, sizeof(dev->msc_bits)), dev->msc_bits);
299 rc = ioctl(fd, EVIOCGBIT(EV_FF, sizeof(dev->ff_bits)), dev->ff_bits);
303 rc = ioctl(fd, EVIOCGBIT(EV_SND, sizeof(dev->snd_bits)), dev->snd_bits);
307 rc = ioctl(fd, EVIOCGKEY(sizeof(dev->key_values)), dev->key_values);
311 rc = ioctl(fd, EVIOCGLED(sizeof(dev->led_values)), dev->led_values);
315 rc = ioctl(fd, EVIOCGSW(sizeof(dev->sw_values)), dev->sw_values);
319 /* rep is a special case, always set it to 1 for both values if EV_REP is set */
320 if (bit_is_set(dev->bits, EV_REP)) {
321 for (i = 0; i < REP_CNT; i++)
322 set_bit(dev->rep_bits, i);
323 rc = ioctl(fd, EVIOCGREP, dev->rep_values);
328 for (i = ABS_X; i <= ABS_MAX; i++) {
329 if (bit_is_set(dev->abs_bits, i)) {
330 struct input_absinfo abs_info;
331 rc = ioctl(fd, EVIOCGABS(i), &abs_info);
335 dev->abs_info[i] = abs_info;
336 if (i == ABS_MT_SLOT) {
337 dev->num_slots = abs_info.maximum + 1;
338 dev->current_slot = abs_info.value;
345 sync_mt_state(dev, 0);
347 rc = init_event_queue(dev);
353 /* not copying key state because we won't know when we'll start to
354 * use this fd and key's are likely to change state by then.
355 * Same with the valuators, really, but they may not change.
358 dev->initialized = true;
362 return rc ? -errno : 0;
366 libevdev_get_fd(const struct libevdev* dev)
372 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
374 ev->time = dev->last_event_time;
381 sync_key_state(struct libevdev *dev)
385 unsigned long keystate[NLONGS(KEY_CNT)] = {0};
387 rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
391 for (i = 0; i < KEY_CNT; i++) {
393 old = bit_is_set(dev->key_values, i);
394 new = bit_is_set(keystate, i);
396 struct input_event *ev = queue_push(dev);
397 init_event(dev, ev, EV_KEY, i, new ? 1 : 0);
401 memcpy(dev->key_values, keystate, rc);
405 return rc ? -errno : 0;
409 sync_sw_state(struct libevdev *dev)
413 unsigned long swstate[NLONGS(SW_CNT)] = {0};
415 rc = ioctl(dev->fd, EVIOCGSW(sizeof(swstate)), swstate);
419 for (i = 0; i < SW_CNT; i++) {
421 old = bit_is_set(dev->sw_values, i);
422 new = bit_is_set(swstate, i);
424 struct input_event *ev = queue_push(dev);
425 init_event(dev, ev, EV_SW, i, new ? 1 : 0);
429 memcpy(dev->sw_values, swstate, rc);
433 return rc ? -errno : 0;
437 sync_led_state(struct libevdev *dev)
441 unsigned long ledstate[NLONGS(LED_CNT)] = {0};
443 rc = ioctl(dev->fd, EVIOCGLED(sizeof(ledstate)), ledstate);
447 for (i = 0; i < LED_CNT; i++) {
449 old = bit_is_set(dev->led_values, i);
450 new = bit_is_set(ledstate, i);
452 struct input_event *ev = queue_push(dev);
453 init_event(dev, ev, EV_LED, i, new ? 1 : 0);
457 memcpy(dev->led_values, ledstate, rc);
461 return rc ? -errno : 0;
464 sync_abs_state(struct libevdev *dev)
469 for (i = ABS_X; i < ABS_CNT; i++) {
470 struct input_absinfo abs_info;
472 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
475 if (!bit_is_set(dev->abs_bits, i))
478 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
482 if (dev->abs_info[i].value != abs_info.value) {
483 struct input_event *ev = queue_push(dev);
485 init_event(dev, ev, EV_ABS, i, abs_info.value);
486 dev->abs_info[i].value = abs_info.value;
492 return rc ? -errno : 0;
496 sync_mt_state(struct libevdev *dev, int create_events)
500 int ioctl_success = 0;
504 } mt_state[ABS_MT_CNT];
506 memset(&mt_state, 0, sizeof(mt_state));
508 for (i = ABS_MT_MIN; i <= ABS_MT_MAX; i++) {
510 if (i == ABS_MT_SLOT)
513 if (!libevdev_has_event_code(dev, EV_ABS, i))
516 idx = i - ABS_MT_MIN;
517 mt_state[idx].code = i;
518 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
520 /* if the first ioctl fails with -EINVAL, chances are the kernel
521 doesn't support the ioctl. Simply continue */
522 if (errno == -EINVAL && !ioctl_success) {
524 } else /* if the second, ... ioctl fails, really fail */
526 } else if (ioctl_success == 0)
530 for (i = 0; i < dev->num_slots; i++) {
532 struct input_event *ev;
535 ev = queue_push(dev);
536 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, i);
539 for (j = ABS_MT_MIN; j <= ABS_MT_MAX; j++) {
540 int jdx = j - ABS_MT_MIN;
542 if (j == ABS_MT_SLOT)
545 if (!libevdev_has_event_code(dev, EV_ABS, j))
548 if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
552 ev = queue_push(dev);
553 init_event(dev, ev, EV_ABS, j, mt_state[jdx].val[i]);
555 dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
561 return rc ? -errno : 0;
565 sync_state(struct libevdev *dev)
569 struct input_event *ev;
571 /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
572 queue[0]) we need to shift this backwards. Except that chances are that the
573 queue may be either full or too full to prepend all the events needed for
576 so we search for the last sync event in the queue and drop everything before
577 including that event and rely on the kernel to tell us the right value for that
578 bitfield during the sync process.
581 for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
582 struct input_event e = {{0,0}, 0, 0, 0};
583 queue_peek(dev, i, &e);
584 if (e.type == EV_SYN)
589 queue_shift_multiple(dev, i + 1, NULL);
591 if (libevdev_has_event_type(dev, EV_KEY))
592 rc = sync_key_state(dev);
593 if (libevdev_has_event_type(dev, EV_LED))
594 rc = sync_led_state(dev);
595 if (libevdev_has_event_type(dev, EV_SW))
596 rc = sync_sw_state(dev);
597 if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
598 rc = sync_abs_state(dev);
599 if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
600 rc = sync_mt_state(dev, 1);
602 dev->queue_nsync = queue_num_elements(dev);
604 if (dev->queue_nsync > 0) {
605 ev = queue_push(dev);
606 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
614 update_key_state(struct libevdev *dev, const struct input_event *e)
616 if (!libevdev_has_event_type(dev, EV_KEY))
619 if (e->code > KEY_MAX)
622 set_bit_state(dev->key_values, e->code, e->value != 0);
628 update_mt_state(struct libevdev *dev, const struct input_event *e)
630 if (e->code == ABS_MT_SLOT) {
632 dev->current_slot = e->value;
633 /* sync abs_info with the current slot values */
634 for (i = ABS_MT_SLOT + 1; i <= ABS_MT_MAX; i++) {
635 if (libevdev_has_event_code(dev, EV_ABS, i))
636 dev->abs_info[i].value = dev->mt_slot_vals[dev->current_slot][i - ABS_MT_MIN];
640 } else if (dev->current_slot == -1)
643 dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
649 update_abs_state(struct libevdev *dev, const struct input_event *e)
651 if (!libevdev_has_event_type(dev, EV_ABS))
654 if (e->code > ABS_MAX)
657 if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
658 update_mt_state(dev, e);
660 dev->abs_info[e->code].value = e->value;
666 update_led_state(struct libevdev *dev, const struct input_event *e)
668 if (!libevdev_has_event_type(dev, EV_LED))
671 if (e->code > LED_MAX)
674 set_bit_state(dev->led_values, e->code, e->value != 0);
680 update_sw_state(struct libevdev *dev, const struct input_event *e)
682 if (!libevdev_has_event_type(dev, EV_SW))
685 if (e->code > SW_MAX)
688 set_bit_state(dev->sw_values, e->code, e->value != 0);
694 update_state(struct libevdev *dev, const struct input_event *e)
703 rc = update_key_state(dev, e);
706 rc = update_abs_state(dev, e);
709 rc = update_led_state(dev, e);
712 rc = update_sw_state(dev, e);
716 dev->last_event_time = e->time;
722 read_more_events(struct libevdev *dev)
726 struct input_event *next;
728 free_elem = queue_num_free_elements(dev);
732 next = queue_next_element(dev);
733 len = read(dev->fd, next, free_elem * sizeof(struct input_event));
736 } else if (len > 0 && len % sizeof(struct input_event) != 0)
739 int nev = len/sizeof(struct input_event);
740 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
747 libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
749 int rc = LIBEVDEV_READ_STATUS_SUCCESS;
751 if (!dev->initialized) {
752 log_bug("device not initialized. call libevdev_set_fd() first\n");
754 } else if (dev->fd < 0)
757 if (!(flags & (LIBEVDEV_READ_FLAG_NORMAL|LIBEVDEV_READ_FLAG_SYNC|LIBEVDEV_READ_FLAG_FORCE_SYNC))) {
758 log_bug("invalid flags %#x\n.\n", flags);
762 if (flags & LIBEVDEV_READ_FLAG_SYNC) {
763 if (dev->sync_state == SYNC_NEEDED) {
764 rc = sync_state(dev);
767 dev->sync_state = SYNC_IN_PROGRESS;
770 if (dev->queue_nsync == 0) {
771 dev->sync_state = SYNC_NONE;
775 } else if (dev->sync_state != SYNC_NONE) {
776 struct input_event e;
778 /* call update_state for all events here, otherwise the library has the wrong view
780 while (queue_shift(dev, &e) == 0) {
782 update_state(dev, &e);
785 dev->sync_state = SYNC_NONE;
788 /* FIXME: if the first event after SYNC_IN_PROGRESS is a SYN_DROPPED, log this */
790 /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
791 worst case we don't read fast enough and end up with SYN_DROPPED anyway.
793 Except if the fd is in blocking mode and we still have events from the last read, don't
797 if (!(flags & LIBEVDEV_READ_FLAG_BLOCKING) ||
798 queue_num_elements(dev) == 0) {
799 rc = read_more_events(dev);
800 if (rc < 0 && rc != -EAGAIN)
804 if (flags & LIBEVDEV_READ_FLAG_FORCE_SYNC) {
805 dev->sync_state = SYNC_NEEDED;
806 rc = LIBEVDEV_READ_STATUS_SYNC;
811 if (queue_shift(dev, ev) != 0)
814 update_state(dev, ev);
816 /* if we disabled a code, get the next event instead */
817 } while(!libevdev_has_event_code(dev, ev->type, ev->code));
820 if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
821 dev->sync_state = SYNC_NEEDED;
822 rc = LIBEVDEV_READ_STATUS_SYNC;
825 if (flags & LIBEVDEV_READ_FLAG_SYNC && dev->queue_nsync > 0) {
827 rc = LIBEVDEV_READ_STATUS_SYNC;
828 if (dev->queue_nsync == 0)
829 dev->sync_state = SYNC_NONE;
837 libevdev_has_event_pending(struct libevdev *dev)
839 struct pollfd fds = { dev->fd, POLLIN, 0 };
842 if (!dev->initialized) {
843 log_bug("device not initialized. call libevdev_set_fd() first\n");
845 } else if (dev->fd < 0)
848 if (queue_num_elements(dev) != 0)
851 rc = poll(&fds, 1, 0);
852 return (rc >= 0) ? rc : -errno;
855 LIBEVDEV_EXPORT const char *
856 libevdev_get_name(const struct libevdev *dev)
858 return dev->name ? dev->name : "";
861 LIBEVDEV_EXPORT const char *
862 libevdev_get_phys(const struct libevdev *dev)
867 LIBEVDEV_EXPORT const char *
868 libevdev_get_uniq(const struct libevdev *dev)
873 #define STRING_SETTER(field) \
874 LIBEVDEV_EXPORT void libevdev_set_##field(struct libevdev *dev, const char *field) \
879 dev->field = strdup(field); \
887 #define PRODUCT_GETTER(name) \
888 LIBEVDEV_EXPORT int libevdev_get_id_##name(const struct libevdev *dev) \
890 return dev->ids.name; \
893 PRODUCT_GETTER(product)
894 PRODUCT_GETTER(vendor)
895 PRODUCT_GETTER(bustype)
896 PRODUCT_GETTER(version)
898 #define PRODUCT_SETTER(field) \
899 LIBEVDEV_EXPORT void libevdev_set_id_##field(struct libevdev *dev, int field) \
901 dev->ids.field = field;\
904 PRODUCT_SETTER(product)
905 PRODUCT_SETTER(vendor)
906 PRODUCT_SETTER(bustype)
907 PRODUCT_SETTER(version)
910 libevdev_get_driver_version(const struct libevdev *dev)
912 return dev->driver_version;
916 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
918 return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
922 libevdev_enable_property(struct libevdev *dev, unsigned int prop)
924 if (prop > INPUT_PROP_MAX)
927 set_bit(dev->props, prop);
932 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
934 return type == EV_SYN ||(type <= EV_MAX && bit_is_set(dev->bits, type));
938 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
940 const unsigned long *mask;
943 if (!libevdev_has_event_type(dev, type))
949 max = type_to_mask_const(dev, type, &mask);
951 if (max == -1 || code > (unsigned int)max)
954 return bit_is_set(mask, code);
958 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
962 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
966 case EV_ABS: value = dev->abs_info[code].value; break;
967 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
968 case EV_LED: value = bit_is_set(dev->led_values, code); break;
969 case EV_SW: value = bit_is_set(dev->sw_values, code); break;
979 libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value)
982 struct input_event e;
984 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
992 case EV_ABS: rc = update_abs_state(dev, &e); break;
993 case EV_KEY: rc = update_key_state(dev, &e); break;
994 case EV_LED: rc = update_led_state(dev, &e); break;
995 case EV_SW: rc = update_sw_state(dev, &e); break;
1005 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
1007 if (libevdev_has_event_type(dev, type) &&
1008 libevdev_has_event_code(dev, type, code)) {
1009 *value = libevdev_get_event_value(dev, type, code);
1016 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
1018 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1021 if (dev->num_slots < 0 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
1024 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1027 return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
1031 libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value)
1033 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1036 if (dev->num_slots == -1 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
1039 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1042 if (code == ABS_MT_SLOT) {
1043 if (value < 0 || value >= libevdev_get_num_slots(dev))
1045 dev->current_slot = value;
1048 dev->mt_slot_vals[slot][code - ABS_MT_MIN] = value;
1055 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
1057 if (libevdev_has_event_type(dev, EV_ABS) &&
1058 libevdev_has_event_code(dev, EV_ABS, code) &&
1059 dev->num_slots >= 0 &&
1060 slot < (unsigned int)dev->num_slots && slot < MAX_SLOTS) {
1061 *value = libevdev_get_slot_value(dev, slot, code);
1068 libevdev_get_num_slots(const struct libevdev *dev)
1070 return dev->num_slots;
1074 libevdev_get_current_slot(const struct libevdev *dev)
1076 return dev->current_slot;
1079 LIBEVDEV_EXPORT const struct input_absinfo*
1080 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
1082 if (!libevdev_has_event_type(dev, EV_ABS) ||
1083 !libevdev_has_event_code(dev, EV_ABS, code))
1086 return &dev->abs_info[code];
1089 #define ABS_GETTER(name) \
1090 LIBEVDEV_EXPORT int libevdev_get_abs_##name(const struct libevdev *dev, unsigned int code) \
1092 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code); \
1093 return absinfo ? absinfo->name : 0; \
1100 ABS_GETTER(resolution)
1102 #define ABS_SETTER(field) \
1103 LIBEVDEV_EXPORT void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
1105 if (!libevdev_has_event_code(dev, EV_ABS, code)) \
1107 dev->abs_info[code].field = val; \
1114 ABS_SETTER(resolution)
1116 LIBEVDEV_EXPORT void
1117 libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1119 if (!libevdev_has_event_code(dev, EV_ABS, code))
1122 dev->abs_info[code] = *abs;
1126 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
1133 if (libevdev_has_event_type(dev, type))
1136 max = libevdev_event_type_get_max(type);
1140 set_bit(dev->bits, type);
1142 if (type == EV_REP) {
1143 int delay = 0, period = 0;
1144 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
1145 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
1151 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
1155 if (type > EV_MAX || type == EV_SYN)
1158 max = libevdev_event_type_get_max(type);
1162 clear_bit(dev->bits, type);
1168 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
1169 unsigned int code, const void *data)
1172 unsigned long *mask = NULL;
1174 if (libevdev_enable_event_type(dev, type))
1191 max = type_to_mask(dev, type, &mask);
1193 if (code > max || (int)max == -1)
1196 set_bit(mask, code);
1198 if (type == EV_ABS) {
1199 const struct input_absinfo *abs = data;
1200 dev->abs_info[code] = *abs;
1201 } else if (type == EV_REP) {
1202 const int *value = data;
1203 dev->rep_values[code] = *value;
1210 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
1213 unsigned long *mask = NULL;
1215 if (type > EV_MAX || type == EV_SYN)
1218 max = type_to_mask(dev, type, &mask);
1220 if (code > max || (int)max == -1)
1223 clear_bit(mask, code);
1229 libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1233 if (!dev->initialized) {
1234 log_bug("device not initialized. call libevdev_set_fd() first\n");
1236 } else if (dev->fd < 0)
1242 rc = ioctl(dev->fd, EVIOCSABS(code), abs);
1246 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
1252 libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
1256 if (!dev->initialized) {
1257 log_bug("device not initialized. call libevdev_set_fd() first\n");
1259 } else if (dev->fd < 0)
1262 if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB) {
1263 log_bug("invalid grab parameter %#x\n", grab);
1267 if (grab == dev->grabbed)
1270 if (grab == LIBEVDEV_GRAB)
1271 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
1272 else if (grab == LIBEVDEV_UNGRAB)
1273 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
1276 dev->grabbed = grab;
1278 return rc < 0 ? -errno : 0;
1282 libevdev_event_is_type(const struct input_event *ev, unsigned int type)
1284 return type < EV_CNT && ev->type == type;
1288 libevdev_event_is_code(const struct input_event *ev, unsigned int type, unsigned int code)
1292 if (!libevdev_event_is_type(ev, type))
1295 max = libevdev_event_type_get_max(type);
1296 return (max > -1 && code <= (unsigned int)max && ev->code == code);
1299 LIBEVDEV_EXPORT const char*
1300 libevdev_event_type_get_name(unsigned int type)
1305 return ev_map[type];
1308 LIBEVDEV_EXPORT const char*
1309 libevdev_event_code_get_name(unsigned int type, unsigned int code)
1311 int max = libevdev_event_type_get_max(type);
1313 if (max == -1 || code > (unsigned int)max)
1316 return event_type_map[type][code];
1319 LIBEVDEV_EXPORT const char*
1320 libevdev_property_get_name(unsigned int prop)
1322 if (prop > INPUT_PROP_MAX)
1325 return input_prop_map[prop];
1329 libevdev_event_type_get_max(unsigned int type)
1334 return ev_max[type];
1338 libevdev_get_repeat(const struct libevdev *dev, int *delay, int *period)
1340 if (!libevdev_has_event_type(dev, EV_REP))
1344 *delay = dev->rep_values[REP_DELAY];
1346 *period = dev->rep_values[REP_PERIOD];
1352 libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value)
1354 return libevdev_kernel_set_led_values(dev, code, value, -1);
1358 libevdev_kernel_set_led_values(struct libevdev *dev, ...)
1360 struct input_event ev[LED_MAX + 1];
1361 enum libevdev_led_value val;
1367 if (!dev->initialized) {
1368 log_bug("device not initialized. call libevdev_set_fd() first\n");
1370 } else if (dev->fd < 0)
1373 memset(ev, 0, sizeof(ev));
1375 va_start(args, dev);
1376 code = va_arg(args, unsigned int);
1377 while (code != -1) {
1378 if (code > LED_MAX) {
1382 val = va_arg(args, enum libevdev_led_value);
1383 if (val != LIBEVDEV_LED_ON && val != LIBEVDEV_LED_OFF) {
1388 if (libevdev_has_event_code(dev, EV_LED, code)) {
1389 struct input_event *e = ev;
1391 while (e->type > 0 && e->code != code)
1398 e->value = (val == LIBEVDEV_LED_ON);
1400 code = va_arg(args, unsigned int);
1404 if (rc == 0 && nleds > 0) {
1405 ev[nleds].type = EV_SYN;
1406 ev[nleds++].code = SYN_REPORT;
1408 rc = write(libevdev_get_fd(dev), ev, nleds * sizeof(ev[0]));
1410 nleds--; /* last is EV_SYN */
1412 update_led_state(dev, &ev[nleds]);
1414 rc = (rc != -1) ? 0 : -errno;
1421 libevdev_set_clock_id(struct libevdev *dev, int clockid)
1423 if (!dev->initialized) {
1424 log_bug("device not initialized. call libevdev_set_fd() first\n");
1426 } else if (dev->fd < 0)
1429 return ioctl(dev->fd, EVIOCSCLOCKID, &clockid) ? -errno : 0;