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"
40 LIBEVDEV_EXPORT const enum libevdev_read_flag LIBEVDEV_READ_SYNC = LIBEVDEV_READ_FLAG_SYNC;
41 LIBEVDEV_EXPORT const enum libevdev_read_flag LIBEVDEV_READ_NORMAL = LIBEVDEV_READ_FLAG_NORMAL;
42 LIBEVDEV_EXPORT const enum libevdev_read_flag LIBEVDEV_FORCE_SYNC = LIBEVDEV_READ_FLAG_FORCE_SYNC;
43 LIBEVDEV_EXPORT const enum libevdev_read_flag LIBEVDEV_READ_BLOCKING = LIBEVDEV_READ_FLAG_BLOCKING;
45 static int sync_mt_state(struct libevdev *dev, int create_events);
48 init_event_queue(struct libevdev *dev)
50 /* FIXME: count the number of axes, keys, etc. to get a better idea at how many events per
51 EV_SYN we could possibly get. Then multiply that by the actual buffer size we care about */
53 const int QUEUE_SIZE = 256;
55 return queue_alloc(dev, QUEUE_SIZE);
59 libevdev_dflt_log_func(enum libevdev_log_priority priority,
61 const char *file, int line, const char *func,
62 const char *format, va_list args)
66 case LIBEVDEV_LOG_ERROR: prefix = "libevdev error"; break;
67 case LIBEVDEV_LOG_INFO: prefix = "libevdev info"; break;
68 case LIBEVDEV_LOG_DEBUG:
69 prefix = "libevdev debug";
72 prefix = "libevdev INVALID LOG PRIORITY";
75 /* default logging format:
76 libevev error in libevdev_some_func: blah blah
77 libevev info in libevdev_some_func: blah blah
78 libevev debug in file.c:123:libevdev_some_func: blah blah
81 fprintf(stderr, "%s in ", prefix);
82 if (priority == LIBEVDEV_LOG_DEBUG)
83 fprintf(stderr, "%s:%d:", file, line);
84 fprintf(stderr, "%s: ", func);
85 vfprintf(stderr, format, args);
89 * Global logging settings.
91 struct logdata log_data = {
93 libevdev_dflt_log_func,
98 log_msg(enum libevdev_log_priority priority,
100 const char *file, int line, const char *func,
101 const char *format, ...)
105 if (!log_data.handler)
108 va_start(args, format);
109 log_data.handler(priority, data, file, line, func, format, args);
113 LIBEVDEV_EXPORT struct libevdev*
116 struct libevdev *dev;
118 dev = calloc(1, sizeof(*dev));
122 dev->initialized = false;
124 dev->current_slot = -1;
125 dev->grabbed = LIBEVDEV_UNGRAB;
126 dev->sync_state = SYNC_NONE;
132 libevdev_new_from_fd(int fd, struct libevdev **dev)
141 rc = libevdev_set_fd(d, fd);
150 libevdev_free(struct libevdev *dev)
164 libevdev_set_log_handler(struct libevdev *dev, libevdev_log_func_t logfunc)
166 /* Can't be backwards compatible to this yet, so don't even try */
167 fprintf(stderr, "libevdev: ABI change. Log function will not be honored.\n");
171 libevdev_set_log_function(libevdev_log_func_t logfunc, void *data)
173 log_data.handler = logfunc;
174 log_data.userdata = data;
178 libevdev_set_log_priority(enum libevdev_log_priority priority)
180 if (priority > LIBEVDEV_LOG_DEBUG)
181 priority = LIBEVDEV_LOG_DEBUG;
182 log_data.priority = priority;
185 LIBEVDEV_EXPORT enum libevdev_log_priority
186 libevdev_get_log_priority(void)
188 return log_data.priority;
192 libevdev_change_fd(struct libevdev *dev, int fd)
194 if (!dev->initialized) {
195 log_bug("device not initialized. call libevdev_set_fd() first\n");
203 libevdev_set_fd(struct libevdev* dev, int fd)
209 if (dev->initialized) {
210 log_bug("device already initialized.\n");
214 rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
218 memset(buf, 0, sizeof(buf));
219 rc = ioctl(fd, EVIOCGNAME(sizeof(buf) - 1), buf);
224 dev->name = strdup(buf);
232 memset(buf, 0, sizeof(buf));
233 rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
235 /* uinput has no phys */
239 dev->phys = strdup(buf);
248 memset(buf, 0, sizeof(buf));
249 rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
254 dev->uniq = strdup(buf);
261 rc = ioctl(fd, EVIOCGID, &dev->ids);
265 rc = ioctl(fd, EVIOCGVERSION, &dev->driver_version);
269 rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
273 rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
277 rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
281 rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
285 rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
289 rc = ioctl(fd, EVIOCGBIT(EV_SW, sizeof(dev->sw_bits)), dev->sw_bits);
293 rc = ioctl(fd, EVIOCGBIT(EV_MSC, sizeof(dev->msc_bits)), dev->msc_bits);
297 rc = ioctl(fd, EVIOCGBIT(EV_FF, sizeof(dev->ff_bits)), dev->ff_bits);
301 rc = ioctl(fd, EVIOCGBIT(EV_SND, sizeof(dev->snd_bits)), dev->snd_bits);
305 rc = ioctl(fd, EVIOCGKEY(sizeof(dev->key_values)), dev->key_values);
309 rc = ioctl(fd, EVIOCGLED(sizeof(dev->led_values)), dev->led_values);
313 rc = ioctl(fd, EVIOCGSW(sizeof(dev->sw_values)), dev->sw_values);
317 /* rep is a special case, always set it to 1 for both values if EV_REP is set */
318 if (bit_is_set(dev->bits, EV_REP)) {
319 for (i = 0; i < REP_CNT; i++)
320 set_bit(dev->rep_bits, i);
321 rc = ioctl(fd, EVIOCGREP, dev->rep_values);
326 for (i = ABS_X; i <= ABS_MAX; i++) {
327 if (bit_is_set(dev->abs_bits, i)) {
328 struct input_absinfo abs_info;
329 rc = ioctl(fd, EVIOCGABS(i), &abs_info);
333 dev->abs_info[i] = abs_info;
334 if (i == ABS_MT_SLOT) {
335 dev->num_slots = abs_info.maximum + 1;
336 dev->current_slot = abs_info.value;
343 sync_mt_state(dev, 0);
345 rc = init_event_queue(dev);
351 /* not copying key state because we won't know when we'll start to
352 * use this fd and key's are likely to change state by then.
353 * Same with the valuators, really, but they may not change.
356 dev->initialized = true;
358 return rc ? -errno : 0;
362 libevdev_get_fd(const struct libevdev* dev)
368 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
370 ev->time = dev->last_event_time;
377 sync_key_state(struct libevdev *dev)
381 unsigned long keystate[NLONGS(KEY_CNT)] = {0};
383 rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
387 for (i = 0; i < KEY_CNT; i++) {
389 old = bit_is_set(dev->key_values, i);
390 new = bit_is_set(keystate, i);
392 struct input_event *ev = queue_push(dev);
393 init_event(dev, ev, EV_KEY, i, new ? 1 : 0);
397 memcpy(dev->key_values, keystate, rc);
401 return rc ? -errno : 0;
405 sync_sw_state(struct libevdev *dev)
409 unsigned long swstate[NLONGS(SW_CNT)] = {0};
411 rc = ioctl(dev->fd, EVIOCGSW(sizeof(swstate)), swstate);
415 for (i = 0; i < SW_CNT; i++) {
417 old = bit_is_set(dev->sw_values, i);
418 new = bit_is_set(swstate, i);
420 struct input_event *ev = queue_push(dev);
421 init_event(dev, ev, EV_SW, i, new ? 1 : 0);
425 memcpy(dev->sw_values, swstate, rc);
429 return rc ? -errno : 0;
433 sync_led_state(struct libevdev *dev)
437 unsigned long ledstate[NLONGS(LED_CNT)] = {0};
439 rc = ioctl(dev->fd, EVIOCGLED(sizeof(ledstate)), ledstate);
443 for (i = 0; i < LED_CNT; i++) {
445 old = bit_is_set(dev->led_values, i);
446 new = bit_is_set(ledstate, i);
448 struct input_event *ev = queue_push(dev);
449 init_event(dev, ev, EV_LED, i, new ? 1 : 0);
453 memcpy(dev->led_values, ledstate, rc);
457 return rc ? -errno : 0;
460 sync_abs_state(struct libevdev *dev)
465 for (i = ABS_X; i < ABS_CNT; i++) {
466 struct input_absinfo abs_info;
468 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
471 if (!bit_is_set(dev->abs_bits, i))
474 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
478 if (dev->abs_info[i].value != abs_info.value) {
479 struct input_event *ev = queue_push(dev);
481 init_event(dev, ev, EV_ABS, i, abs_info.value);
482 dev->abs_info[i].value = abs_info.value;
488 return rc ? -errno : 0;
492 sync_mt_state(struct libevdev *dev, int create_events)
499 } mt_state[ABS_MT_CNT];
501 for (i = ABS_MT_MIN; i <= ABS_MT_MAX; i++) {
503 if (i == ABS_MT_SLOT)
506 if (!libevdev_has_event_code(dev, EV_ABS, i))
509 idx = i - ABS_MT_MIN;
510 mt_state[idx].code = i;
511 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
516 for (i = 0; i < dev->num_slots; i++) {
518 struct input_event *ev;
521 ev = queue_push(dev);
522 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, i);
525 for (j = ABS_MT_MIN; j <= ABS_MT_MAX; j++) {
526 int jdx = j - ABS_MT_MIN;
528 if (j == ABS_MT_SLOT)
531 if (!libevdev_has_event_code(dev, EV_ABS, j))
534 if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
538 ev = queue_push(dev);
539 init_event(dev, ev, EV_ABS, j, mt_state[jdx].val[i]);
541 dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
547 return rc ? -errno : 0;
551 sync_state(struct libevdev *dev)
555 struct input_event *ev;
557 /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
558 queue[0]) we need to shift this backwards. Except that chances are that the
559 queue may be either full or too full to prepend all the events needed for
562 so we search for the last sync event in the queue and drop everything before
563 including that event and rely on the kernel to tell us the right value for that
564 bitfield during the sync process.
567 for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
568 struct input_event e = {{0,0}, 0, 0, 0};
569 queue_peek(dev, i, &e);
570 if (e.type == EV_SYN)
575 queue_shift_multiple(dev, i + 1, NULL);
577 if (libevdev_has_event_type(dev, EV_KEY))
578 rc = sync_key_state(dev);
579 if (libevdev_has_event_type(dev, EV_LED))
580 rc = sync_led_state(dev);
581 if (libevdev_has_event_type(dev, EV_SW))
582 rc = sync_sw_state(dev);
583 if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
584 rc = sync_abs_state(dev);
585 if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
586 rc = sync_mt_state(dev, 1);
588 dev->queue_nsync = queue_num_elements(dev);
590 if (dev->queue_nsync > 0) {
591 ev = queue_push(dev);
592 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
600 update_key_state(struct libevdev *dev, const struct input_event *e)
602 if (!libevdev_has_event_type(dev, EV_KEY))
605 if (e->code > KEY_MAX)
608 set_bit_state(dev->key_values, e->code, e->value != 0);
614 update_mt_state(struct libevdev *dev, const struct input_event *e)
616 if (e->code == ABS_MT_SLOT) {
618 dev->current_slot = e->value;
619 /* sync abs_info with the current slot values */
620 for (i = ABS_MT_SLOT + 1; i <= ABS_MT_MAX; i++) {
621 if (libevdev_has_event_code(dev, EV_ABS, i))
622 dev->abs_info[i].value = dev->mt_slot_vals[dev->current_slot][i - ABS_MT_MIN];
626 } else if (dev->current_slot == -1)
629 dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
635 update_abs_state(struct libevdev *dev, const struct input_event *e)
637 if (!libevdev_has_event_type(dev, EV_ABS))
640 if (e->code > ABS_MAX)
643 if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
644 update_mt_state(dev, e);
646 dev->abs_info[e->code].value = e->value;
652 update_led_state(struct libevdev *dev, const struct input_event *e)
654 if (!libevdev_has_event_type(dev, EV_LED))
657 if (e->code > LED_MAX)
660 set_bit_state(dev->led_values, e->code, e->value != 0);
666 update_sw_state(struct libevdev *dev, const struct input_event *e)
668 if (!libevdev_has_event_type(dev, EV_SW))
671 if (e->code > SW_MAX)
674 set_bit_state(dev->sw_values, e->code, e->value != 0);
680 update_state(struct libevdev *dev, const struct input_event *e)
689 rc = update_key_state(dev, e);
692 rc = update_abs_state(dev, e);
695 rc = update_led_state(dev, e);
698 rc = update_sw_state(dev, e);
702 dev->last_event_time = e->time;
708 read_more_events(struct libevdev *dev)
712 struct input_event *next;
714 free_elem = queue_num_free_elements(dev);
718 next = queue_next_element(dev);
719 len = read(dev->fd, next, free_elem * sizeof(struct input_event));
722 } else if (len > 0 && len % sizeof(struct input_event) != 0)
725 int nev = len/sizeof(struct input_event);
726 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
733 libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
735 int rc = LIBEVDEV_READ_STATUS_SUCCESS;
737 if (!dev->initialized) {
738 log_bug("device not initialized. call libevdev_set_fd() first\n");
740 } else if (dev->fd < 0)
743 if (!(flags & (LIBEVDEV_READ_FLAG_NORMAL|LIBEVDEV_READ_FLAG_SYNC|LIBEVDEV_READ_FLAG_FORCE_SYNC))) {
744 log_bug("invalid flags %#x\n.\n", flags);
748 if (flags & LIBEVDEV_READ_FLAG_SYNC) {
749 if (dev->sync_state == SYNC_NEEDED) {
750 rc = sync_state(dev);
753 dev->sync_state = SYNC_IN_PROGRESS;
756 if (dev->queue_nsync == 0) {
757 dev->sync_state = SYNC_NONE;
761 } else if (dev->sync_state != SYNC_NONE) {
762 struct input_event e;
764 /* call update_state for all events here, otherwise the library has the wrong view
766 while (queue_shift(dev, &e) == 0) {
768 update_state(dev, &e);
771 dev->sync_state = SYNC_NONE;
774 /* FIXME: if the first event after SYNC_IN_PROGRESS is a SYN_DROPPED, log this */
776 /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
777 worst case we don't read fast enough and end up with SYN_DROPPED anyway.
779 Except if the fd is in blocking mode and we still have events from the last read, don't
783 if (!(flags & LIBEVDEV_READ_FLAG_BLOCKING) ||
784 queue_num_elements(dev) == 0) {
785 rc = read_more_events(dev);
786 if (rc < 0 && rc != -EAGAIN)
790 if (flags & LIBEVDEV_READ_FLAG_FORCE_SYNC) {
791 dev->sync_state = SYNC_NEEDED;
792 rc = LIBEVDEV_READ_STATUS_SYNC;
797 if (queue_shift(dev, ev) != 0)
800 update_state(dev, ev);
802 /* if we disabled a code, get the next event instead */
803 } while(!libevdev_has_event_code(dev, ev->type, ev->code));
806 if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
807 dev->sync_state = SYNC_NEEDED;
808 rc = LIBEVDEV_READ_STATUS_SYNC;
811 if (flags & LIBEVDEV_READ_FLAG_SYNC && dev->queue_nsync > 0) {
813 rc = LIBEVDEV_READ_STATUS_SYNC;
814 if (dev->queue_nsync == 0)
815 dev->sync_state = SYNC_NONE;
823 libevdev_has_event_pending(struct libevdev *dev)
825 struct pollfd fds = { dev->fd, POLLIN, 0 };
828 if (!dev->initialized) {
829 log_bug("device not initialized. call libevdev_set_fd() first\n");
831 } else if (dev->fd < 0)
834 if (queue_num_elements(dev) != 0)
837 rc = poll(&fds, 1, 0);
838 return (rc >= 0) ? rc : -errno;
841 LIBEVDEV_EXPORT const char *
842 libevdev_get_name(const struct libevdev *dev)
844 return dev->name ? dev->name : "";
847 LIBEVDEV_EXPORT const char *
848 libevdev_get_phys(const struct libevdev *dev)
853 LIBEVDEV_EXPORT const char *
854 libevdev_get_uniq(const struct libevdev *dev)
859 #define STRING_SETTER(field) \
860 LIBEVDEV_EXPORT void libevdev_set_##field(struct libevdev *dev, const char *field) \
865 dev->field = strdup(field); \
873 #define PRODUCT_GETTER(name) \
874 LIBEVDEV_EXPORT int libevdev_get_id_##name(const struct libevdev *dev) \
876 return dev->ids.name; \
879 PRODUCT_GETTER(product);
880 PRODUCT_GETTER(vendor);
881 PRODUCT_GETTER(bustype);
882 PRODUCT_GETTER(version);
884 #define PRODUCT_SETTER(field) \
885 LIBEVDEV_EXPORT void libevdev_set_id_##field(struct libevdev *dev, int field) \
887 dev->ids.field = field;\
890 PRODUCT_SETTER(product);
891 PRODUCT_SETTER(vendor);
892 PRODUCT_SETTER(bustype);
893 PRODUCT_SETTER(version);
896 libevdev_get_driver_version(const struct libevdev *dev)
898 return dev->driver_version;
902 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
904 return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
908 libevdev_enable_property(struct libevdev *dev, unsigned int prop)
910 if (prop > INPUT_PROP_MAX)
913 set_bit(dev->props, prop);
918 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
920 return (type <= EV_MAX) && bit_is_set(dev->bits, type);
924 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
926 const unsigned long *mask;
929 if (!libevdev_has_event_type(dev, type))
935 max = type_to_mask_const(dev, type, &mask);
937 if (max == -1 || code > (unsigned int)max)
940 return bit_is_set(mask, code);
944 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
948 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
952 case EV_ABS: value = dev->abs_info[code].value; break;
953 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
954 case EV_LED: value = bit_is_set(dev->led_values, code); break;
955 case EV_SW: value = bit_is_set(dev->sw_values, code); break;
965 libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value)
968 struct input_event e;
970 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
978 case EV_ABS: rc = update_abs_state(dev, &e); break;
979 case EV_KEY: rc = update_key_state(dev, &e); break;
980 case EV_LED: rc = update_led_state(dev, &e); break;
981 case EV_SW: rc = update_sw_state(dev, &e); break;
991 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
993 if (libevdev_has_event_type(dev, type) &&
994 libevdev_has_event_code(dev, type, code)) {
995 *value = libevdev_get_event_value(dev, type, code);
1002 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
1004 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1007 if (dev->num_slots < 0 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
1010 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1013 return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
1017 libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value)
1019 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1022 if (dev->num_slots == -1 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
1025 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1028 if (code == ABS_MT_SLOT) {
1029 if (value < 0 || value >= libevdev_get_num_slots(dev))
1031 dev->current_slot = value;
1034 dev->mt_slot_vals[slot][code - ABS_MT_MIN] = value;
1041 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
1043 if (libevdev_has_event_type(dev, EV_ABS) &&
1044 libevdev_has_event_code(dev, EV_ABS, code) &&
1045 dev->num_slots >= 0 &&
1046 slot < (unsigned int)dev->num_slots && slot < MAX_SLOTS) {
1047 *value = libevdev_get_slot_value(dev, slot, code);
1054 libevdev_get_num_slots(const struct libevdev *dev)
1056 return dev->num_slots;
1060 libevdev_get_current_slot(const struct libevdev *dev)
1062 return dev->current_slot;
1065 LIBEVDEV_EXPORT const struct input_absinfo*
1066 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
1068 if (!libevdev_has_event_type(dev, EV_ABS) ||
1069 !libevdev_has_event_code(dev, EV_ABS, code))
1072 return &dev->abs_info[code];
1075 #define ABS_GETTER(name) \
1076 LIBEVDEV_EXPORT int libevdev_get_abs_##name(const struct libevdev *dev, unsigned int code) \
1078 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code); \
1079 return absinfo ? absinfo->name : 0; \
1082 ABS_GETTER(maximum);
1083 ABS_GETTER(minimum);
1086 ABS_GETTER(resolution);
1088 #define ABS_SETTER(field) \
1089 LIBEVDEV_EXPORT void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
1091 if (!libevdev_has_event_code(dev, EV_ABS, code)) \
1093 dev->abs_info[code].field = val; \
1100 ABS_SETTER(resolution)
1102 LIBEVDEV_EXPORT void
1103 libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1105 if (!libevdev_has_event_code(dev, EV_ABS, code))
1108 dev->abs_info[code] = *abs;
1112 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
1117 if (libevdev_has_event_type(dev, type))
1120 set_bit(dev->bits, type);
1122 if (type == EV_REP) {
1123 int delay = 0, period = 0;
1124 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
1125 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
1131 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
1133 if (type > EV_MAX || type == EV_SYN)
1136 clear_bit(dev->bits, type);
1142 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
1143 unsigned int code, const void *data)
1146 unsigned long *mask = NULL;
1148 if (libevdev_enable_event_type(dev, type))
1165 max = type_to_mask(dev, type, &mask);
1170 set_bit(mask, code);
1172 if (type == EV_ABS) {
1173 const struct input_absinfo *abs = data;
1174 dev->abs_info[code] = *abs;
1175 } else if (type == EV_REP) {
1176 const int *value = data;
1177 dev->rep_values[code] = *value;
1184 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
1187 unsigned long *mask = NULL;
1192 max = type_to_mask(dev, type, &mask);
1197 clear_bit(mask, code);
1203 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1205 return libevdev_kernel_set_abs_info(dev, code, abs);
1209 libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1213 if (!dev->initialized) {
1214 log_bug("device not initialized. call libevdev_set_fd() first\n");
1216 } else if (dev->fd < 0)
1222 rc = ioctl(dev->fd, EVIOCSABS(code), abs);
1226 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
1232 libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
1236 if (!dev->initialized) {
1237 log_bug("device not initialized. call libevdev_set_fd() first\n");
1239 } else if (dev->fd < 0)
1242 if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB) {
1243 log_bug("invalid grab parameter %#x\n", grab);
1247 if (grab == dev->grabbed)
1250 if (grab == LIBEVDEV_GRAB)
1251 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
1252 else if (grab == LIBEVDEV_UNGRAB)
1253 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
1256 dev->grabbed = grab;
1258 return rc < 0 ? -errno : 0;
1263 libevdev_is_event_type(const struct input_event *ev, unsigned int type)
1264 ALIAS(libevdev_event_is_type);
1267 libevdev_event_is_type(const struct input_event *ev, unsigned int type)
1269 return type < EV_CNT && ev->type == type;
1274 libevdev_is_event_code(const struct input_event *ev, unsigned int type, unsigned int code)
1275 ALIAS(libevdev_event_is_code);
1278 libevdev_event_is_code(const struct input_event *ev, unsigned int type, unsigned int code)
1282 if (!libevdev_event_is_type(ev, type))
1285 max = libevdev_event_type_get_max(type);
1286 return (max > -1 && code <= (unsigned int)max && ev->code == code);
1290 LIBEVDEV_EXPORT const char*
1291 libevdev_get_event_type_name(unsigned int type)
1292 ALIAS(libevdev_event_type_get_name);
1294 LIBEVDEV_EXPORT const char*
1295 libevdev_event_type_get_name(unsigned int type)
1300 return ev_map[type];
1304 LIBEVDEV_EXPORT const char*
1305 libevdev_get_event_code_name(unsigned int type, unsigned int code)
1306 ALIAS(libevdev_event_code_get_name);
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];
1320 LIBEVDEV_EXPORT const char*
1321 libevdev_get_input_prop_name(unsigned int prop)
1322 ALIAS(libevdev_property_get_name);
1325 LIBEVDEV_EXPORT const char*
1326 libevdev_get_property_name(unsigned int prop)
1327 ALIAS(libevdev_property_get_name);
1329 LIBEVDEV_EXPORT const char*
1330 libevdev_property_get_name(unsigned int prop)
1332 if (prop > INPUT_PROP_MAX)
1335 return input_prop_map[prop];
1340 libevdev_get_event_type_max(unsigned int type)
1341 ALIAS(libevdev_event_type_get_max);
1344 libevdev_event_type_get_max(unsigned int type)
1349 return ev_max[type];
1353 libevdev_get_repeat(struct libevdev *dev, int *delay, int *period)
1355 if (!libevdev_has_event_type(dev, EV_REP))
1359 *delay = dev->rep_values[REP_DELAY];
1361 *period = dev->rep_values[REP_PERIOD];
1367 libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value)
1369 return libevdev_kernel_set_led_values(dev, code, value, -1);
1373 libevdev_kernel_set_led_values(struct libevdev *dev, ...)
1375 struct input_event ev[LED_MAX + 1];
1376 enum libevdev_led_value val;
1382 if (!dev->initialized) {
1383 log_bug("device not initialized. call libevdev_set_fd() first\n");
1385 } else if (dev->fd < 0)
1388 memset(ev, 0, sizeof(ev));
1390 va_start(args, dev);
1391 code = va_arg(args, unsigned int);
1392 while (code != -1) {
1393 if (code > LED_MAX) {
1397 val = va_arg(args, enum libevdev_led_value);
1398 if (val != LIBEVDEV_LED_ON && val != LIBEVDEV_LED_OFF) {
1403 if (libevdev_has_event_code(dev, EV_LED, code)) {
1404 struct input_event *e = ev;
1406 while (e->type > 0 && e->code != code)
1413 e->value = (val == LIBEVDEV_LED_ON);
1415 code = va_arg(args, unsigned int);
1419 if (rc == 0 && nleds > 0) {
1420 ev[nleds].type = EV_SYN;
1421 ev[nleds++].code = SYN_REPORT;
1423 rc = write(libevdev_get_fd(dev), ev, nleds * sizeof(ev[0]));
1425 nleds--; /* last is EV_SYN */
1427 update_led_state(dev, &ev[nleds]);
1429 rc = (rc != -1) ? 0 : -errno;
1436 libevdev_set_clock_id(struct libevdev *dev, int clockid)
1438 if (!dev->initialized) {
1439 log_bug("device not initialized. call libevdev_set_fd() first\n");
1441 } else if (dev->fd < 0)
1444 return ioctl(dev->fd, EVIOCSCLOCKID, &clockid) ? -errno : 0;