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"
39 LIBEVDEV_EXPORT const enum libevdev_read_flag LIBEVDEV_READ_SYNC = LIBEVDEV_READ_FLAG_SYNC;
40 LIBEVDEV_EXPORT const enum libevdev_read_flag LIBEVDEV_READ_NORMAL = LIBEVDEV_READ_FLAG_NORMAL;
41 LIBEVDEV_EXPORT const enum libevdev_read_flag LIBEVDEV_FORCE_SYNC = LIBEVDEV_READ_FLAG_FORCE_SYNC;
42 LIBEVDEV_EXPORT const enum libevdev_read_flag LIBEVDEV_READ_BLOCKING = LIBEVDEV_READ_FLAG_BLOCKING;
44 static int sync_mt_state(struct libevdev *dev, int create_events);
47 init_event_queue(struct libevdev *dev)
49 /* FIXME: count the number of axes, keys, etc. to get a better idea at how many events per
50 EV_SYN we could possibly get. Then multiply that by the actual buffer size we care about */
52 const int QUEUE_SIZE = 256;
54 return queue_alloc(dev, QUEUE_SIZE);
58 libevdev_dflt_log_func(enum libevdev_log_priority priority,
60 const char *file, int line, const char *func,
61 const char *format, va_list args)
65 case LIBEVDEV_LOG_ERROR: prefix = "libevdev error"; break;
66 case LIBEVDEV_LOG_INFO: prefix = "libevdev info"; break;
67 case LIBEVDEV_LOG_DEBUG:
68 prefix = "libevdev debug";
71 prefix = "libevdev INVALID LOG PRIORITY";
74 /* default logging format:
75 libevev error in libevdev_some_func: blah blah
76 libevev info in libevdev_some_func: blah blah
77 libevev debug in file.c:123:libevdev_some_func: blah blah
80 fprintf(stderr, "%s in ", prefix);
81 if (priority == LIBEVDEV_LOG_DEBUG)
82 fprintf(stderr, "%s:%d:", file, line);
83 fprintf(stderr, "%s: ", func);
84 vfprintf(stderr, format, args);
88 * Global logging settings.
90 struct logdata log_data = {
92 libevdev_dflt_log_func,
97 log_msg(enum libevdev_log_priority priority,
99 const char *file, int line, const char *func,
100 const char *format, ...)
104 if (!log_data.handler)
107 va_start(args, format);
108 log_data.handler(priority, data, file, line, func, format, args);
112 LIBEVDEV_EXPORT struct libevdev*
115 struct libevdev *dev;
117 dev = calloc(1, sizeof(*dev));
122 dev->current_slot = -1;
123 dev->grabbed = LIBEVDEV_UNGRAB;
124 dev->sync_state = SYNC_NONE;
130 libevdev_new_from_fd(int fd, struct libevdev **dev)
139 rc = libevdev_set_fd(d, fd);
148 libevdev_free(struct libevdev *dev)
162 libevdev_set_log_handler(struct libevdev *dev, libevdev_log_func_t logfunc)
164 /* Can't be backwards compatible to this yet, so don't even try */
165 fprintf(stderr, "libevdev: ABI change. Log function will not be honored.\n");
169 libevdev_set_log_function(libevdev_log_func_t logfunc, void *data)
171 log_data.handler = logfunc;
172 log_data.userdata = data;
176 libevdev_set_log_priority(enum libevdev_log_priority priority)
178 if (priority > LIBEVDEV_LOG_DEBUG)
179 priority = LIBEVDEV_LOG_DEBUG;
180 log_data.priority = priority;
183 LIBEVDEV_EXPORT enum libevdev_log_priority
184 libevdev_get_log_priority(void)
186 return log_data.priority;
190 libevdev_change_fd(struct libevdev *dev, int fd)
193 log_bug("device not initialized. call libevdev_set_fd() first\n");
201 libevdev_set_fd(struct libevdev* dev, int fd)
208 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 rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
271 rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
275 rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
279 rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
283 rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
287 rc = ioctl(fd, EVIOCGBIT(EV_SW, sizeof(dev->sw_bits)), dev->sw_bits);
291 rc = ioctl(fd, EVIOCGBIT(EV_MSC, sizeof(dev->msc_bits)), dev->msc_bits);
295 rc = ioctl(fd, EVIOCGBIT(EV_FF, sizeof(dev->ff_bits)), dev->ff_bits);
299 rc = ioctl(fd, EVIOCGBIT(EV_SND, sizeof(dev->snd_bits)), dev->snd_bits);
303 rc = ioctl(fd, EVIOCGKEY(sizeof(dev->key_values)), dev->key_values);
307 rc = ioctl(fd, EVIOCGLED(sizeof(dev->led_values)), dev->led_values);
311 rc = ioctl(fd, EVIOCGSW(sizeof(dev->sw_values)), dev->sw_values);
315 /* rep is a special case, always set it to 1 for both values if EV_REP is set */
316 if (bit_is_set(dev->bits, EV_REP)) {
317 for (i = 0; i < REP_CNT; i++)
318 set_bit(dev->rep_bits, i);
319 rc = ioctl(fd, EVIOCGREP, dev->rep_values);
324 for (i = ABS_X; i <= ABS_MAX; i++) {
325 if (bit_is_set(dev->abs_bits, i)) {
326 struct input_absinfo abs_info;
327 rc = ioctl(fd, EVIOCGABS(i), &abs_info);
331 dev->abs_info[i] = abs_info;
332 if (i == ABS_MT_SLOT) {
333 dev->num_slots = abs_info.maximum + 1;
334 dev->current_slot = abs_info.value;
341 sync_mt_state(dev, 0);
343 rc = init_event_queue(dev);
349 /* not copying key state because we won't know when we'll start to
350 * use this fd and key's are likely to change state by then.
351 * Same with the valuators, really, but they may not change.
355 return rc ? -errno : 0;
359 libevdev_get_fd(const struct libevdev* dev)
365 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
367 ev->time = dev->last_event_time;
374 sync_key_state(struct libevdev *dev)
378 unsigned long keystate[NLONGS(KEY_CNT)] = {0};
380 rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
384 for (i = 0; i < KEY_CNT; i++) {
386 old = bit_is_set(dev->key_values, i);
387 new = bit_is_set(keystate, i);
389 struct input_event *ev = queue_push(dev);
390 init_event(dev, ev, EV_KEY, i, new ? 1 : 0);
394 memcpy(dev->key_values, keystate, rc);
398 return rc ? -errno : 0;
402 sync_sw_state(struct libevdev *dev)
406 unsigned long swstate[NLONGS(SW_CNT)] = {0};
408 rc = ioctl(dev->fd, EVIOCGSW(sizeof(swstate)), swstate);
412 for (i = 0; i < SW_CNT; i++) {
414 old = bit_is_set(dev->sw_values, i);
415 new = bit_is_set(swstate, i);
417 struct input_event *ev = queue_push(dev);
418 init_event(dev, ev, EV_SW, i, new ? 1 : 0);
422 memcpy(dev->sw_values, swstate, rc);
426 return rc ? -errno : 0;
430 sync_led_state(struct libevdev *dev)
434 unsigned long ledstate[NLONGS(LED_CNT)] = {0};
436 rc = ioctl(dev->fd, EVIOCGLED(sizeof(ledstate)), ledstate);
440 for (i = 0; i < LED_CNT; i++) {
442 old = bit_is_set(dev->led_values, i);
443 new = bit_is_set(ledstate, i);
445 struct input_event *ev = queue_push(dev);
446 init_event(dev, ev, EV_LED, i, new ? 1 : 0);
450 memcpy(dev->led_values, ledstate, rc);
454 return rc ? -errno : 0;
457 sync_abs_state(struct libevdev *dev)
462 for (i = ABS_X; i < ABS_CNT; i++) {
463 struct input_absinfo abs_info;
465 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
468 if (!bit_is_set(dev->abs_bits, i))
471 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
475 if (dev->abs_info[i].value != abs_info.value) {
476 struct input_event *ev = queue_push(dev);
478 init_event(dev, ev, EV_ABS, i, abs_info.value);
479 dev->abs_info[i].value = abs_info.value;
485 return rc ? -errno : 0;
489 sync_mt_state(struct libevdev *dev, int create_events)
496 } mt_state[ABS_MT_CNT];
498 for (i = ABS_MT_MIN; i <= ABS_MT_MAX; i++) {
500 if (i == ABS_MT_SLOT)
503 if (!libevdev_has_event_code(dev, EV_ABS, i))
506 idx = i - ABS_MT_MIN;
507 mt_state[idx].code = i;
508 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
513 for (i = 0; i < dev->num_slots; i++) {
515 struct input_event *ev;
518 ev = queue_push(dev);
519 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, i);
522 for (j = ABS_MT_MIN; j <= ABS_MT_MAX; j++) {
523 int jdx = j - ABS_MT_MIN;
525 if (j == ABS_MT_SLOT)
528 if (!libevdev_has_event_code(dev, EV_ABS, j))
531 if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
535 ev = queue_push(dev);
536 init_event(dev, ev, EV_ABS, j, mt_state[jdx].val[i]);
538 dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
544 return rc ? -errno : 0;
548 sync_state(struct libevdev *dev)
552 struct input_event *ev;
554 /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
555 queue[0]) we need to shift this backwards. Except that chances are that the
556 queue may be either full or too full to prepend all the events needed for
559 so we search for the last sync event in the queue and drop everything before
560 including that event and rely on the kernel to tell us the right value for that
561 bitfield during the sync process.
564 for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
565 struct input_event e = {{0,0}, 0, 0, 0};
566 queue_peek(dev, i, &e);
567 if (e.type == EV_SYN)
572 queue_shift_multiple(dev, i + 1, NULL);
574 if (libevdev_has_event_type(dev, EV_KEY))
575 rc = sync_key_state(dev);
576 if (libevdev_has_event_type(dev, EV_LED))
577 rc = sync_led_state(dev);
578 if (libevdev_has_event_type(dev, EV_SW))
579 rc = sync_sw_state(dev);
580 if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
581 rc = sync_abs_state(dev);
582 if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
583 rc = sync_mt_state(dev, 1);
585 dev->queue_nsync = queue_num_elements(dev);
587 if (dev->queue_nsync > 0) {
588 ev = queue_push(dev);
589 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
597 update_key_state(struct libevdev *dev, const struct input_event *e)
599 if (!libevdev_has_event_type(dev, EV_KEY))
602 if (e->code > KEY_MAX)
605 set_bit_state(dev->key_values, e->code, e->value != 0);
611 update_mt_state(struct libevdev *dev, const struct input_event *e)
613 if (e->code == ABS_MT_SLOT) {
615 dev->current_slot = e->value;
616 /* sync abs_info with the current slot values */
617 for (i = ABS_MT_SLOT + 1; i <= ABS_MT_MAX; i++) {
618 if (libevdev_has_event_code(dev, EV_ABS, i))
619 dev->abs_info[i].value = dev->mt_slot_vals[dev->current_slot][i - ABS_MT_MIN];
623 } else if (dev->current_slot == -1)
626 dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
632 update_abs_state(struct libevdev *dev, const struct input_event *e)
634 if (!libevdev_has_event_type(dev, EV_ABS))
637 if (e->code > ABS_MAX)
640 if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
641 update_mt_state(dev, e);
643 dev->abs_info[e->code].value = e->value;
649 update_led_state(struct libevdev *dev, const struct input_event *e)
651 if (!libevdev_has_event_type(dev, EV_LED))
654 if (e->code > LED_MAX)
657 set_bit_state(dev->led_values, e->code, e->value != 0);
663 update_sw_state(struct libevdev *dev, const struct input_event *e)
665 if (!libevdev_has_event_type(dev, EV_SW))
668 if (e->code > SW_MAX)
671 set_bit_state(dev->sw_values, e->code, e->value != 0);
677 update_state(struct libevdev *dev, const struct input_event *e)
686 rc = update_key_state(dev, e);
689 rc = update_abs_state(dev, e);
692 rc = update_led_state(dev, e);
695 rc = update_sw_state(dev, e);
699 dev->last_event_time = e->time;
705 read_more_events(struct libevdev *dev)
709 struct input_event *next;
711 free_elem = queue_num_free_elements(dev);
715 next = queue_next_element(dev);
716 len = read(dev->fd, next, free_elem * sizeof(struct input_event));
719 } else if (len > 0 && len % sizeof(struct input_event) != 0)
722 int nev = len/sizeof(struct input_event);
723 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
730 libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
732 int rc = LIBEVDEV_READ_STATUS_SUCCESS;
735 log_bug("device not initialized. call libevdev_set_fd() first\n");
739 if (!(flags & (LIBEVDEV_READ_FLAG_NORMAL|LIBEVDEV_READ_FLAG_SYNC|LIBEVDEV_READ_FLAG_FORCE_SYNC))) {
740 log_bug("invalid flags %#x\n.\n", flags);
744 if (flags & LIBEVDEV_READ_FLAG_SYNC) {
745 if (dev->sync_state == SYNC_NEEDED) {
746 rc = sync_state(dev);
749 dev->sync_state = SYNC_IN_PROGRESS;
752 if (dev->queue_nsync == 0) {
753 dev->sync_state = SYNC_NONE;
757 } else if (dev->sync_state != SYNC_NONE) {
758 struct input_event e;
760 /* call update_state for all events here, otherwise the library has the wrong view
762 while (queue_shift(dev, &e) == 0) {
764 update_state(dev, &e);
767 dev->sync_state = SYNC_NONE;
770 /* FIXME: if the first event after SYNC_IN_PROGRESS is a SYN_DROPPED, log this */
772 /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
773 worst case we don't read fast enough and end up with SYN_DROPPED anyway.
775 Except if the fd is in blocking mode and we still have events from the last read, don't
779 if (!(flags & LIBEVDEV_READ_FLAG_BLOCKING) ||
780 queue_num_elements(dev) == 0) {
781 rc = read_more_events(dev);
782 if (rc < 0 && rc != -EAGAIN)
786 if (flags & LIBEVDEV_READ_FLAG_FORCE_SYNC) {
787 dev->sync_state = SYNC_NEEDED;
788 rc = LIBEVDEV_READ_STATUS_SYNC;
793 if (queue_shift(dev, ev) != 0)
796 update_state(dev, ev);
798 /* if we disabled a code, get the next event instead */
799 } while(!libevdev_has_event_code(dev, ev->type, ev->code));
802 if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
803 dev->sync_state = SYNC_NEEDED;
804 rc = LIBEVDEV_READ_STATUS_SYNC;
807 if (flags & LIBEVDEV_READ_FLAG_SYNC && dev->queue_nsync > 0) {
809 rc = LIBEVDEV_READ_STATUS_SYNC;
810 if (dev->queue_nsync == 0)
811 dev->sync_state = SYNC_NONE;
819 libevdev_has_event_pending(struct libevdev *dev)
821 struct pollfd fds = { dev->fd, POLLIN, 0 };
825 log_bug("device not initialized. call libevdev_set_fd() first\n");
829 if (queue_num_elements(dev) != 0)
832 rc = poll(&fds, 1, 0);
833 return (rc >= 0) ? rc : -errno;
836 LIBEVDEV_EXPORT const char *
837 libevdev_get_name(const struct libevdev *dev)
839 return dev->name ? dev->name : "";
842 LIBEVDEV_EXPORT const char *
843 libevdev_get_phys(const struct libevdev *dev)
848 LIBEVDEV_EXPORT const char *
849 libevdev_get_uniq(const struct libevdev *dev)
854 #define STRING_SETTER(field) \
855 LIBEVDEV_EXPORT void libevdev_set_##field(struct libevdev *dev, const char *field) \
860 dev->field = strdup(field); \
868 #define PRODUCT_GETTER(name) \
869 LIBEVDEV_EXPORT int libevdev_get_id_##name(const struct libevdev *dev) \
871 return dev->ids.name; \
874 PRODUCT_GETTER(product);
875 PRODUCT_GETTER(vendor);
876 PRODUCT_GETTER(bustype);
877 PRODUCT_GETTER(version);
879 #define PRODUCT_SETTER(field) \
880 LIBEVDEV_EXPORT void libevdev_set_id_##field(struct libevdev *dev, int field) \
882 dev->ids.field = field;\
885 PRODUCT_SETTER(product);
886 PRODUCT_SETTER(vendor);
887 PRODUCT_SETTER(bustype);
888 PRODUCT_SETTER(version);
891 libevdev_get_driver_version(const struct libevdev *dev)
893 return dev->driver_version;
897 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
899 return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
903 libevdev_enable_property(struct libevdev *dev, unsigned int prop)
905 if (prop > INPUT_PROP_MAX)
908 set_bit(dev->props, prop);
913 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
915 return (type <= EV_MAX) && bit_is_set(dev->bits, type);
919 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
921 const unsigned long *mask;
924 if (!libevdev_has_event_type(dev, type))
930 max = type_to_mask_const(dev, type, &mask);
932 if (max == -1 || code > (unsigned int)max)
935 return bit_is_set(mask, code);
939 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
943 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
947 case EV_ABS: value = dev->abs_info[code].value; break;
948 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
949 case EV_LED: value = bit_is_set(dev->led_values, code); break;
950 case EV_SW: value = bit_is_set(dev->sw_values, code); break;
960 libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value)
963 struct input_event e;
965 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
973 case EV_ABS: rc = update_abs_state(dev, &e); break;
974 case EV_KEY: rc = update_key_state(dev, &e); break;
975 case EV_LED: rc = update_led_state(dev, &e); break;
976 case EV_SW: rc = update_sw_state(dev, &e); break;
986 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
988 if (libevdev_has_event_type(dev, type) &&
989 libevdev_has_event_code(dev, type, code)) {
990 *value = libevdev_get_event_value(dev, type, code);
997 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
999 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1002 if (dev->num_slots < 0 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
1005 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1008 return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
1012 libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value)
1014 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1017 if (dev->num_slots == -1 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
1020 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1023 if (code == ABS_MT_SLOT) {
1024 if (value < 0 || value >= libevdev_get_num_slots(dev))
1026 dev->current_slot = value;
1029 dev->mt_slot_vals[slot][code - ABS_MT_MIN] = value;
1036 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
1038 if (libevdev_has_event_type(dev, EV_ABS) &&
1039 libevdev_has_event_code(dev, EV_ABS, code) &&
1040 dev->num_slots >= 0 &&
1041 slot < (unsigned int)dev->num_slots && slot < MAX_SLOTS) {
1042 *value = libevdev_get_slot_value(dev, slot, code);
1049 libevdev_get_num_slots(const struct libevdev *dev)
1051 return dev->num_slots;
1055 libevdev_get_current_slot(const struct libevdev *dev)
1057 return dev->current_slot;
1060 LIBEVDEV_EXPORT const struct input_absinfo*
1061 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
1063 if (!libevdev_has_event_type(dev, EV_ABS) ||
1064 !libevdev_has_event_code(dev, EV_ABS, code))
1067 return &dev->abs_info[code];
1070 #define ABS_GETTER(name) \
1071 LIBEVDEV_EXPORT int libevdev_get_abs_##name(const struct libevdev *dev, unsigned int code) \
1073 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code); \
1074 return absinfo ? absinfo->name : 0; \
1077 ABS_GETTER(maximum);
1078 ABS_GETTER(minimum);
1081 ABS_GETTER(resolution);
1083 #define ABS_SETTER(field) \
1084 LIBEVDEV_EXPORT void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
1086 if (!libevdev_has_event_code(dev, EV_ABS, code)) \
1088 dev->abs_info[code].field = val; \
1095 ABS_SETTER(resolution)
1097 LIBEVDEV_EXPORT void
1098 libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1100 if (!libevdev_has_event_code(dev, EV_ABS, code))
1103 dev->abs_info[code] = *abs;
1107 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
1112 if (libevdev_has_event_type(dev, type))
1115 set_bit(dev->bits, type);
1117 if (type == EV_REP) {
1118 int delay = 0, period = 0;
1119 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
1120 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
1126 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
1128 if (type > EV_MAX || type == EV_SYN)
1131 clear_bit(dev->bits, type);
1137 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
1138 unsigned int code, const void *data)
1141 unsigned long *mask = NULL;
1143 if (libevdev_enable_event_type(dev, type))
1160 max = type_to_mask(dev, type, &mask);
1165 set_bit(mask, code);
1167 if (type == EV_ABS) {
1168 const struct input_absinfo *abs = data;
1169 dev->abs_info[code] = *abs;
1170 } else if (type == EV_REP) {
1171 const int *value = data;
1172 dev->rep_values[code] = *value;
1179 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
1182 unsigned long *mask = NULL;
1187 max = type_to_mask(dev, type, &mask);
1192 clear_bit(mask, code);
1198 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1200 return libevdev_kernel_set_abs_info(dev, code, abs);
1204 libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1209 log_bug("device not initialized. call libevdev_set_fd() first\n");
1216 rc = ioctl(dev->fd, EVIOCSABS(code), abs);
1220 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
1226 libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
1231 log_bug("device not initialized. call libevdev_set_fd() first\n");
1235 if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB) {
1236 log_bug("invalid grab parameter %#x\n", grab);
1240 if (grab == dev->grabbed)
1243 if (grab == LIBEVDEV_GRAB)
1244 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
1245 else if (grab == LIBEVDEV_UNGRAB)
1246 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
1249 dev->grabbed = grab;
1251 return rc < 0 ? -errno : 0;
1256 libevdev_is_event_type(const struct input_event *ev, unsigned int type)
1257 ALIAS(libevdev_event_is_type);
1260 libevdev_event_is_type(const struct input_event *ev, unsigned int type)
1262 return type < EV_CNT && ev->type == type;
1267 libevdev_is_event_code(const struct input_event *ev, unsigned int type, unsigned int code)
1268 ALIAS(libevdev_event_is_code);
1271 libevdev_event_is_code(const struct input_event *ev, unsigned int type, unsigned int code)
1275 if (!libevdev_event_is_type(ev, type))
1278 max = libevdev_event_type_get_max(type);
1279 return (max > -1 && code <= (unsigned int)max && ev->code == code);
1283 LIBEVDEV_EXPORT const char*
1284 libevdev_get_event_type_name(unsigned int type)
1285 ALIAS(libevdev_event_type_get_name);
1287 LIBEVDEV_EXPORT const char*
1288 libevdev_event_type_get_name(unsigned int type)
1293 return ev_map[type];
1297 LIBEVDEV_EXPORT const char*
1298 libevdev_get_event_code_name(unsigned int type, unsigned int code)
1299 ALIAS(libevdev_event_code_get_name);
1301 LIBEVDEV_EXPORT const char*
1302 libevdev_event_code_get_name(unsigned int type, unsigned int code)
1304 int max = libevdev_event_type_get_max(type);
1306 if (max == -1 || code > (unsigned int)max)
1309 return event_type_map[type][code];
1313 LIBEVDEV_EXPORT const char*
1314 libevdev_get_input_prop_name(unsigned int prop)
1315 ALIAS(libevdev_property_get_name);
1318 LIBEVDEV_EXPORT const char*
1319 libevdev_get_property_name(unsigned int prop)
1320 ALIAS(libevdev_property_get_name);
1322 LIBEVDEV_EXPORT const char*
1323 libevdev_property_get_name(unsigned int prop)
1325 if (prop > INPUT_PROP_MAX)
1328 return input_prop_map[prop];
1333 libevdev_get_event_type_max(unsigned int type)
1334 ALIAS(libevdev_event_type_get_max);
1337 libevdev_event_type_get_max(unsigned int type)
1342 return ev_max[type];
1346 libevdev_get_repeat(struct libevdev *dev, int *delay, int *period)
1348 if (!libevdev_has_event_type(dev, EV_REP))
1352 *delay = dev->rep_values[REP_DELAY];
1354 *period = dev->rep_values[REP_PERIOD];
1360 libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value)
1362 return libevdev_kernel_set_led_values(dev, code, value, -1);
1366 libevdev_kernel_set_led_values(struct libevdev *dev, ...)
1368 struct input_event ev[LED_MAX + 1];
1369 enum libevdev_led_value val;
1376 log_bug("device not initialized. call libevdev_set_fd() first\n");
1380 memset(ev, 0, sizeof(ev));
1382 va_start(args, dev);
1383 code = va_arg(args, unsigned int);
1384 while (code != -1) {
1385 if (code > LED_MAX) {
1389 val = va_arg(args, enum libevdev_led_value);
1390 if (val != LIBEVDEV_LED_ON && val != LIBEVDEV_LED_OFF) {
1395 if (libevdev_has_event_code(dev, EV_LED, code)) {
1396 struct input_event *e = ev;
1398 while (e->type > 0 && e->code != code)
1405 e->value = (val == LIBEVDEV_LED_ON);
1407 code = va_arg(args, unsigned int);
1411 if (rc == 0 && nleds > 0) {
1412 ev[nleds].type = EV_SYN;
1413 ev[nleds++].code = SYN_REPORT;
1415 rc = write(libevdev_get_fd(dev), ev, nleds * sizeof(ev[0]));
1417 nleds--; /* last is EV_SYN */
1419 update_led_state(dev, &ev[nleds]);
1421 rc = (rc != -1) ? 0 : -errno;
1428 libevdev_set_clock_id(struct libevdev *dev, int clockid)
1431 log_bug("device not initialized. call libevdev_set_fd() first\n");
1435 return ioctl(dev->fd, EVIOCSCLOCKID, &clockid) ? -errno : 0;