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;
973 libevdev_get_repeat(dev, &value, NULL);
976 libevdev_get_repeat(dev, NULL, &value);
992 libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value)
995 struct input_event e;
997 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
1005 case EV_ABS: rc = update_abs_state(dev, &e); break;
1006 case EV_KEY: rc = update_key_state(dev, &e); break;
1007 case EV_LED: rc = update_led_state(dev, &e); break;
1008 case EV_SW: rc = update_sw_state(dev, &e); break;
1018 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
1020 if (libevdev_has_event_type(dev, type) &&
1021 libevdev_has_event_code(dev, type, code)) {
1022 *value = libevdev_get_event_value(dev, type, code);
1029 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
1031 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1034 if (dev->num_slots < 0 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
1037 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1040 return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
1044 libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value)
1046 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1049 if (dev->num_slots == -1 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
1052 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1055 if (code == ABS_MT_SLOT) {
1056 if (value < 0 || value >= libevdev_get_num_slots(dev))
1058 dev->current_slot = value;
1061 dev->mt_slot_vals[slot][code - ABS_MT_MIN] = value;
1068 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
1070 if (libevdev_has_event_type(dev, EV_ABS) &&
1071 libevdev_has_event_code(dev, EV_ABS, code) &&
1072 dev->num_slots >= 0 &&
1073 slot < (unsigned int)dev->num_slots && slot < MAX_SLOTS) {
1074 *value = libevdev_get_slot_value(dev, slot, code);
1081 libevdev_get_num_slots(const struct libevdev *dev)
1083 return dev->num_slots;
1087 libevdev_get_current_slot(const struct libevdev *dev)
1089 return dev->current_slot;
1092 LIBEVDEV_EXPORT const struct input_absinfo*
1093 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
1095 if (!libevdev_has_event_type(dev, EV_ABS) ||
1096 !libevdev_has_event_code(dev, EV_ABS, code))
1099 return &dev->abs_info[code];
1102 #define ABS_GETTER(name) \
1103 LIBEVDEV_EXPORT int libevdev_get_abs_##name(const struct libevdev *dev, unsigned int code) \
1105 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code); \
1106 return absinfo ? absinfo->name : 0; \
1113 ABS_GETTER(resolution)
1115 #define ABS_SETTER(field) \
1116 LIBEVDEV_EXPORT void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
1118 if (!libevdev_has_event_code(dev, EV_ABS, code)) \
1120 dev->abs_info[code].field = val; \
1127 ABS_SETTER(resolution)
1129 LIBEVDEV_EXPORT void
1130 libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1132 if (!libevdev_has_event_code(dev, EV_ABS, code))
1135 dev->abs_info[code] = *abs;
1139 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
1146 if (libevdev_has_event_type(dev, type))
1149 max = libevdev_event_type_get_max(type);
1153 set_bit(dev->bits, type);
1155 if (type == EV_REP) {
1156 int delay = 0, period = 0;
1157 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
1158 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
1164 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
1168 if (type > EV_MAX || type == EV_SYN)
1171 max = libevdev_event_type_get_max(type);
1175 clear_bit(dev->bits, type);
1181 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
1182 unsigned int code, const void *data)
1185 unsigned long *mask = NULL;
1187 if (libevdev_enable_event_type(dev, type))
1204 max = type_to_mask(dev, type, &mask);
1206 if (code > max || (int)max == -1)
1209 set_bit(mask, code);
1211 if (type == EV_ABS) {
1212 const struct input_absinfo *abs = data;
1213 dev->abs_info[code] = *abs;
1214 } else if (type == EV_REP) {
1215 const int *value = data;
1216 dev->rep_values[code] = *value;
1223 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
1226 unsigned long *mask = NULL;
1228 if (type > EV_MAX || type == EV_SYN)
1231 max = type_to_mask(dev, type, &mask);
1233 if (code > max || (int)max == -1)
1236 clear_bit(mask, code);
1242 libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1246 if (!dev->initialized) {
1247 log_bug("device not initialized. call libevdev_set_fd() first\n");
1249 } else if (dev->fd < 0)
1255 rc = ioctl(dev->fd, EVIOCSABS(code), abs);
1259 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
1265 libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
1269 if (!dev->initialized) {
1270 log_bug("device not initialized. call libevdev_set_fd() first\n");
1272 } else if (dev->fd < 0)
1275 if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB) {
1276 log_bug("invalid grab parameter %#x\n", grab);
1280 if (grab == dev->grabbed)
1283 if (grab == LIBEVDEV_GRAB)
1284 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
1285 else if (grab == LIBEVDEV_UNGRAB)
1286 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
1289 dev->grabbed = grab;
1291 return rc < 0 ? -errno : 0;
1295 libevdev_event_is_type(const struct input_event *ev, unsigned int type)
1297 return type < EV_CNT && ev->type == type;
1301 libevdev_event_is_code(const struct input_event *ev, unsigned int type, unsigned int code)
1305 if (!libevdev_event_is_type(ev, type))
1308 max = libevdev_event_type_get_max(type);
1309 return (max > -1 && code <= (unsigned int)max && ev->code == code);
1312 LIBEVDEV_EXPORT const char*
1313 libevdev_event_type_get_name(unsigned int type)
1318 return ev_map[type];
1321 LIBEVDEV_EXPORT const char*
1322 libevdev_event_code_get_name(unsigned int type, unsigned int code)
1324 int max = libevdev_event_type_get_max(type);
1326 if (max == -1 || code > (unsigned int)max)
1329 return event_type_map[type][code];
1332 LIBEVDEV_EXPORT const char*
1333 libevdev_property_get_name(unsigned int prop)
1335 if (prop > INPUT_PROP_MAX)
1338 return input_prop_map[prop];
1342 libevdev_event_type_get_max(unsigned int type)
1347 return ev_max[type];
1351 libevdev_get_repeat(const struct libevdev *dev, int *delay, int *period)
1353 if (!libevdev_has_event_type(dev, EV_REP))
1357 *delay = dev->rep_values[REP_DELAY];
1359 *period = dev->rep_values[REP_PERIOD];
1365 libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value)
1367 return libevdev_kernel_set_led_values(dev, code, value, -1);
1371 libevdev_kernel_set_led_values(struct libevdev *dev, ...)
1373 struct input_event ev[LED_MAX + 1];
1374 enum libevdev_led_value val;
1380 if (!dev->initialized) {
1381 log_bug("device not initialized. call libevdev_set_fd() first\n");
1383 } else if (dev->fd < 0)
1386 memset(ev, 0, sizeof(ev));
1388 va_start(args, dev);
1389 code = va_arg(args, unsigned int);
1390 while (code != -1) {
1391 if (code > LED_MAX) {
1395 val = va_arg(args, enum libevdev_led_value);
1396 if (val != LIBEVDEV_LED_ON && val != LIBEVDEV_LED_OFF) {
1401 if (libevdev_has_event_code(dev, EV_LED, code)) {
1402 struct input_event *e = ev;
1404 while (e->type > 0 && e->code != code)
1411 e->value = (val == LIBEVDEV_LED_ON);
1413 code = va_arg(args, unsigned int);
1417 if (rc == 0 && nleds > 0) {
1418 ev[nleds].type = EV_SYN;
1419 ev[nleds++].code = SYN_REPORT;
1421 rc = write(libevdev_get_fd(dev), ev, nleds * sizeof(ev[0]));
1423 nleds--; /* last is EV_SYN */
1425 update_led_state(dev, &ev[nleds]);
1427 rc = (rc != -1) ? 0 : -errno;
1434 libevdev_set_clock_id(struct libevdev *dev, int clockid)
1436 if (!dev->initialized) {
1437 log_bug("device not initialized. call libevdev_set_fd() first\n");
1439 } else if (dev->fd < 0)
1442 return ioctl(dev->fd, EVIOCSCLOCKID, &clockid) ? -errno : 0;