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";
73 /* default logging format:
74 libevev error in libevdev_some_func: blah blah
75 libevev info in libevdev_some_func: blah blah
76 libevev debug in file.c:123:libevdev_some_func: blah blah
79 fprintf(stderr, "%s in ", prefix);
80 if (priority == LIBEVDEV_LOG_DEBUG)
81 fprintf(stderr, "%s:%d:", file, line);
82 fprintf(stderr, "%s: ", func);
83 vfprintf(stderr, format, args);
87 * Global logging settings.
89 struct logdata log_data = {
91 libevdev_dflt_log_func,
96 log_msg(enum libevdev_log_priority priority,
98 const char *file, int line, const char *func,
99 const char *format, ...)
103 if (!log_data.handler)
106 va_start(args, format);
107 log_data.handler(priority, data, file, line, func, format, args);
111 LIBEVDEV_EXPORT struct libevdev*
114 struct libevdev *dev;
116 dev = calloc(1, sizeof(*dev));
121 dev->current_slot = -1;
122 dev->grabbed = LIBEVDEV_UNGRAB;
123 dev->sync_state = SYNC_NONE;
129 libevdev_new_from_fd(int fd, struct libevdev **dev)
138 rc = libevdev_set_fd(d, fd);
147 libevdev_free(struct libevdev *dev)
161 libevdev_set_log_handler(struct libevdev *dev, libevdev_log_func_t logfunc)
163 /* Can't be backwards compatible to this yet, so don't even try */
164 fprintf(stderr, "libevdev: ABI change. Log function will not be honored.\n");
168 libevdev_set_log_function(libevdev_log_func_t logfunc, void *data)
170 log_data.handler = logfunc;
171 log_data.userdata = data;
175 libevdev_set_log_priority(enum libevdev_log_priority priority)
177 if (priority > LIBEVDEV_LOG_DEBUG)
178 priority = LIBEVDEV_LOG_DEBUG;
179 log_data.priority = priority;
182 LIBEVDEV_EXPORT enum libevdev_log_priority
183 libevdev_get_log_priority(void)
185 return log_data.priority;
189 libevdev_change_fd(struct libevdev *dev, int fd)
192 log_bug("device not initialized. call libevdev_set_fd() first\n");
200 libevdev_set_fd(struct libevdev* dev, int fd)
207 log_bug("device already initialized.\n");
211 rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
215 memset(buf, 0, sizeof(buf));
216 rc = ioctl(fd, EVIOCGNAME(sizeof(buf) - 1), buf);
221 dev->name = strdup(buf);
229 memset(buf, 0, sizeof(buf));
230 rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
232 /* uinput has no phys */
236 dev->phys = strdup(buf);
245 memset(buf, 0, sizeof(buf));
246 rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
251 dev->uniq = strdup(buf);
258 rc = ioctl(fd, EVIOCGID, &dev->ids);
262 rc = ioctl(fd, EVIOCGVERSION, &dev->driver_version);
266 rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
270 rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
274 rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
278 rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
282 rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
286 rc = ioctl(fd, EVIOCGBIT(EV_SW, sizeof(dev->sw_bits)), dev->sw_bits);
290 rc = ioctl(fd, EVIOCGBIT(EV_MSC, sizeof(dev->msc_bits)), dev->msc_bits);
294 rc = ioctl(fd, EVIOCGBIT(EV_FF, sizeof(dev->ff_bits)), dev->ff_bits);
298 rc = ioctl(fd, EVIOCGBIT(EV_SND, sizeof(dev->snd_bits)), dev->snd_bits);
302 rc = ioctl(fd, EVIOCGKEY(sizeof(dev->key_values)), dev->key_values);
306 rc = ioctl(fd, EVIOCGLED(sizeof(dev->led_values)), dev->led_values);
310 rc = ioctl(fd, EVIOCGSW(sizeof(dev->sw_values)), dev->sw_values);
314 /* rep is a special case, always set it to 1 for both values if EV_REP is set */
315 if (bit_is_set(dev->bits, EV_REP)) {
316 for (i = 0; i < REP_CNT; i++)
317 set_bit(dev->rep_bits, i);
318 rc = ioctl(fd, EVIOCGREP, dev->rep_values);
323 for (i = ABS_X; i <= ABS_MAX; i++) {
324 if (bit_is_set(dev->abs_bits, i)) {
325 struct input_absinfo abs_info;
326 rc = ioctl(fd, EVIOCGABS(i), &abs_info);
330 dev->abs_info[i] = abs_info;
331 if (i == ABS_MT_SLOT) {
332 dev->num_slots = abs_info.maximum + 1;
333 dev->current_slot = abs_info.value;
340 sync_mt_state(dev, 0);
342 rc = init_event_queue(dev);
348 /* not copying key state because we won't know when we'll start to
349 * use this fd and key's are likely to change state by then.
350 * Same with the valuators, really, but they may not change.
354 return rc ? -errno : 0;
358 libevdev_get_fd(const struct libevdev* dev)
364 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
366 ev->time = dev->last_event_time;
373 sync_key_state(struct libevdev *dev)
377 unsigned long keystate[NLONGS(KEY_CNT)] = {0};
379 rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
383 for (i = 0; i < KEY_CNT; i++) {
385 old = bit_is_set(dev->key_values, i);
386 new = bit_is_set(keystate, i);
388 struct input_event *ev = queue_push(dev);
389 init_event(dev, ev, EV_KEY, i, new ? 1 : 0);
393 memcpy(dev->key_values, keystate, rc);
397 return rc ? -errno : 0;
401 sync_sw_state(struct libevdev *dev)
405 unsigned long swstate[NLONGS(SW_CNT)] = {0};
407 rc = ioctl(dev->fd, EVIOCGSW(sizeof(swstate)), swstate);
411 for (i = 0; i < SW_CNT; i++) {
413 old = bit_is_set(dev->sw_values, i);
414 new = bit_is_set(swstate, i);
416 struct input_event *ev = queue_push(dev);
417 init_event(dev, ev, EV_SW, i, new ? 1 : 0);
421 memcpy(dev->sw_values, swstate, rc);
425 return rc ? -errno : 0;
429 sync_led_state(struct libevdev *dev)
433 unsigned long ledstate[NLONGS(LED_CNT)] = {0};
435 rc = ioctl(dev->fd, EVIOCGLED(sizeof(ledstate)), ledstate);
439 for (i = 0; i < LED_CNT; i++) {
441 old = bit_is_set(dev->led_values, i);
442 new = bit_is_set(ledstate, i);
444 struct input_event *ev = queue_push(dev);
445 init_event(dev, ev, EV_LED, i, new ? 1 : 0);
449 memcpy(dev->led_values, ledstate, rc);
453 return rc ? -errno : 0;
456 sync_abs_state(struct libevdev *dev)
461 for (i = ABS_X; i < ABS_CNT; i++) {
462 struct input_absinfo abs_info;
464 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
467 if (!bit_is_set(dev->abs_bits, i))
470 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
474 if (dev->abs_info[i].value != abs_info.value) {
475 struct input_event *ev = queue_push(dev);
477 init_event(dev, ev, EV_ABS, i, abs_info.value);
478 dev->abs_info[i].value = abs_info.value;
484 return rc ? -errno : 0;
488 sync_mt_state(struct libevdev *dev, int create_events)
495 } mt_state[ABS_MT_CNT];
497 for (i = ABS_MT_MIN; i <= ABS_MT_MAX; i++) {
499 if (i == ABS_MT_SLOT)
502 if (!libevdev_has_event_code(dev, EV_ABS, i))
505 idx = i - ABS_MT_MIN;
506 mt_state[idx].code = i;
507 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
512 for (i = 0; i < dev->num_slots; i++) {
514 struct input_event *ev;
517 ev = queue_push(dev);
518 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, i);
521 for (j = ABS_MT_MIN; j <= ABS_MT_MAX; j++) {
522 int jdx = j - ABS_MT_MIN;
524 if (j == ABS_MT_SLOT)
527 if (!libevdev_has_event_code(dev, EV_ABS, j))
530 if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
534 ev = queue_push(dev);
535 init_event(dev, ev, EV_ABS, j, mt_state[jdx].val[i]);
537 dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
543 return rc ? -errno : 0;
547 sync_state(struct libevdev *dev)
551 struct input_event *ev;
553 /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
554 queue[0]) we need to shift this backwards. Except that chances are that the
555 queue may be either full or too full to prepend all the events needed for
558 so we search for the last sync event in the queue and drop everything before
559 including that event and rely on the kernel to tell us the right value for that
560 bitfield during the sync process.
563 for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
564 struct input_event e = {{0,0}, 0, 0, 0};
565 queue_peek(dev, i, &e);
566 if (e.type == EV_SYN)
571 queue_shift_multiple(dev, i + 1, NULL);
573 if (libevdev_has_event_type(dev, EV_KEY))
574 rc = sync_key_state(dev);
575 if (libevdev_has_event_type(dev, EV_LED))
576 rc = sync_led_state(dev);
577 if (libevdev_has_event_type(dev, EV_SW))
578 rc = sync_sw_state(dev);
579 if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
580 rc = sync_abs_state(dev);
581 if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
582 rc = sync_mt_state(dev, 1);
584 dev->queue_nsync = queue_num_elements(dev);
586 if (dev->queue_nsync > 0) {
587 ev = queue_push(dev);
588 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
596 update_key_state(struct libevdev *dev, const struct input_event *e)
598 if (!libevdev_has_event_type(dev, EV_KEY))
601 if (e->code > KEY_MAX)
604 set_bit_state(dev->key_values, e->code, e->value != 0);
610 update_mt_state(struct libevdev *dev, const struct input_event *e)
612 if (e->code == ABS_MT_SLOT) {
614 dev->current_slot = e->value;
615 /* sync abs_info with the current slot values */
616 for (i = ABS_MT_SLOT + 1; i <= ABS_MT_MAX; i++) {
617 if (libevdev_has_event_code(dev, EV_ABS, i))
618 dev->abs_info[i].value = dev->mt_slot_vals[dev->current_slot][i - ABS_MT_MIN];
622 } else if (dev->current_slot == -1)
625 dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
631 update_abs_state(struct libevdev *dev, const struct input_event *e)
633 if (!libevdev_has_event_type(dev, EV_ABS))
636 if (e->code > ABS_MAX)
639 if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
640 update_mt_state(dev, e);
642 dev->abs_info[e->code].value = e->value;
648 update_led_state(struct libevdev *dev, const struct input_event *e)
650 if (!libevdev_has_event_type(dev, EV_LED))
653 if (e->code > LED_MAX)
656 set_bit_state(dev->led_values, e->code, e->value != 0);
662 update_sw_state(struct libevdev *dev, const struct input_event *e)
664 if (!libevdev_has_event_type(dev, EV_SW))
667 if (e->code > SW_MAX)
670 set_bit_state(dev->sw_values, e->code, e->value != 0);
676 update_state(struct libevdev *dev, const struct input_event *e)
685 rc = update_key_state(dev, e);
688 rc = update_abs_state(dev, e);
691 rc = update_led_state(dev, e);
694 rc = update_sw_state(dev, e);
698 dev->last_event_time = e->time;
704 read_more_events(struct libevdev *dev)
708 struct input_event *next;
710 free_elem = queue_num_free_elements(dev);
714 next = queue_next_element(dev);
715 len = read(dev->fd, next, free_elem * sizeof(struct input_event));
718 } else if (len > 0 && len % sizeof(struct input_event) != 0)
721 int nev = len/sizeof(struct input_event);
722 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
729 libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
731 int rc = LIBEVDEV_READ_STATUS_SUCCESS;
734 log_bug("device not initialized. call libevdev_set_fd() first\n");
738 if (!(flags & (LIBEVDEV_READ_FLAG_NORMAL|LIBEVDEV_READ_FLAG_SYNC|LIBEVDEV_READ_FLAG_FORCE_SYNC))) {
739 log_bug("invalid flags %#x\n.\n", flags);
743 if (flags & LIBEVDEV_READ_FLAG_SYNC) {
744 if (dev->sync_state == SYNC_NEEDED) {
745 rc = sync_state(dev);
748 dev->sync_state = SYNC_IN_PROGRESS;
751 if (dev->queue_nsync == 0) {
752 dev->sync_state = SYNC_NONE;
756 } else if (dev->sync_state != SYNC_NONE) {
757 struct input_event e;
759 /* call update_state for all events here, otherwise the library has the wrong view
761 while (queue_shift(dev, &e) == 0) {
763 update_state(dev, &e);
766 dev->sync_state = SYNC_NONE;
769 /* FIXME: if the first event after SYNC_IN_PROGRESS is a SYN_DROPPED, log this */
771 /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
772 worst case we don't read fast enough and end up with SYN_DROPPED anyway.
774 Except if the fd is in blocking mode and we still have events from the last read, don't
778 if (!(flags & LIBEVDEV_READ_FLAG_BLOCKING) ||
779 queue_num_elements(dev) == 0) {
780 rc = read_more_events(dev);
781 if (rc < 0 && rc != -EAGAIN)
785 if (flags & LIBEVDEV_READ_FLAG_FORCE_SYNC) {
786 dev->sync_state = SYNC_NEEDED;
787 rc = LIBEVDEV_READ_STATUS_SYNC;
792 if (queue_shift(dev, ev) != 0)
795 update_state(dev, ev);
797 /* if we disabled a code, get the next event instead */
798 } while(!libevdev_has_event_code(dev, ev->type, ev->code));
801 if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
802 dev->sync_state = SYNC_NEEDED;
803 rc = LIBEVDEV_READ_STATUS_SYNC;
806 if (flags & LIBEVDEV_READ_FLAG_SYNC && dev->queue_nsync > 0) {
808 rc = LIBEVDEV_READ_STATUS_SYNC;
809 if (dev->queue_nsync == 0)
810 dev->sync_state = SYNC_NONE;
818 libevdev_has_event_pending(struct libevdev *dev)
820 struct pollfd fds = { dev->fd, POLLIN, 0 };
824 log_bug("device not initialized. call libevdev_set_fd() first\n");
828 if (queue_num_elements(dev) != 0)
831 rc = poll(&fds, 1, 0);
832 return (rc >= 0) ? rc : -errno;
835 LIBEVDEV_EXPORT const char *
836 libevdev_get_name(const struct libevdev *dev)
838 return dev->name ? dev->name : "";
841 LIBEVDEV_EXPORT const char *
842 libevdev_get_phys(const struct libevdev *dev)
847 LIBEVDEV_EXPORT const char *
848 libevdev_get_uniq(const struct libevdev *dev)
853 #define STRING_SETTER(field) \
854 LIBEVDEV_EXPORT void libevdev_set_##field(struct libevdev *dev, const char *field) \
859 dev->field = strdup(field); \
867 #define PRODUCT_GETTER(name) \
868 LIBEVDEV_EXPORT int libevdev_get_id_##name(const struct libevdev *dev) \
870 return dev->ids.name; \
873 PRODUCT_GETTER(product);
874 PRODUCT_GETTER(vendor);
875 PRODUCT_GETTER(bustype);
876 PRODUCT_GETTER(version);
878 #define PRODUCT_SETTER(field) \
879 LIBEVDEV_EXPORT void libevdev_set_id_##field(struct libevdev *dev, int field) \
881 dev->ids.field = field;\
884 PRODUCT_SETTER(product);
885 PRODUCT_SETTER(vendor);
886 PRODUCT_SETTER(bustype);
887 PRODUCT_SETTER(version);
890 libevdev_get_driver_version(const struct libevdev *dev)
892 return dev->driver_version;
896 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
898 return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
902 libevdev_enable_property(struct libevdev *dev, unsigned int prop)
904 if (prop > INPUT_PROP_MAX)
907 set_bit(dev->props, prop);
912 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
914 return (type <= EV_MAX) && bit_is_set(dev->bits, type);
918 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
920 const unsigned long *mask;
923 if (!libevdev_has_event_type(dev, type))
929 max = type_to_mask_const(dev, type, &mask);
931 if (max == -1 || code > (unsigned int)max)
934 return bit_is_set(mask, code);
938 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
942 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
946 case EV_ABS: value = dev->abs_info[code].value; break;
947 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
948 case EV_LED: value = bit_is_set(dev->led_values, code); break;
949 case EV_SW: value = bit_is_set(dev->sw_values, code); break;
959 libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value)
962 struct input_event e;
964 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
972 case EV_ABS: rc = update_abs_state(dev, &e); break;
973 case EV_KEY: rc = update_key_state(dev, &e); break;
974 case EV_LED: rc = update_led_state(dev, &e); break;
975 case EV_SW: rc = update_sw_state(dev, &e); break;
985 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
987 if (libevdev_has_event_type(dev, type) &&
988 libevdev_has_event_code(dev, type, code)) {
989 *value = libevdev_get_event_value(dev, type, code);
996 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
998 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1001 if (dev->num_slots < 0 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
1004 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1007 return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
1011 libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value)
1013 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1016 if (dev->num_slots == -1 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
1019 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1022 if (code == ABS_MT_SLOT) {
1023 if (value < 0 || value >= libevdev_get_num_slots(dev))
1025 dev->current_slot = value;
1028 dev->mt_slot_vals[slot][code - ABS_MT_MIN] = value;
1035 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
1037 if (libevdev_has_event_type(dev, EV_ABS) &&
1038 libevdev_has_event_code(dev, EV_ABS, code) &&
1039 dev->num_slots >= 0 &&
1040 slot < (unsigned int)dev->num_slots && slot < MAX_SLOTS) {
1041 *value = libevdev_get_slot_value(dev, slot, code);
1048 libevdev_get_num_slots(const struct libevdev *dev)
1050 return dev->num_slots;
1054 libevdev_get_current_slot(const struct libevdev *dev)
1056 return dev->current_slot;
1059 LIBEVDEV_EXPORT const struct input_absinfo*
1060 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
1062 if (!libevdev_has_event_type(dev, EV_ABS) ||
1063 !libevdev_has_event_code(dev, EV_ABS, code))
1066 return &dev->abs_info[code];
1069 #define ABS_GETTER(name) \
1070 LIBEVDEV_EXPORT int libevdev_get_abs_##name(const struct libevdev *dev, unsigned int code) \
1072 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code); \
1073 return absinfo ? absinfo->name : 0; \
1076 ABS_GETTER(maximum);
1077 ABS_GETTER(minimum);
1080 ABS_GETTER(resolution);
1082 #define ABS_SETTER(field) \
1083 LIBEVDEV_EXPORT void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
1085 if (!libevdev_has_event_code(dev, EV_ABS, code)) \
1087 dev->abs_info[code].field = val; \
1094 ABS_SETTER(resolution)
1096 LIBEVDEV_EXPORT void
1097 libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1099 if (!libevdev_has_event_code(dev, EV_ABS, code))
1102 dev->abs_info[code] = *abs;
1106 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
1111 if (libevdev_has_event_type(dev, type))
1114 set_bit(dev->bits, type);
1116 if (type == EV_REP) {
1117 int delay = 0, period = 0;
1118 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
1119 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
1125 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
1127 if (type > EV_MAX || type == EV_SYN)
1130 clear_bit(dev->bits, type);
1136 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
1137 unsigned int code, const void *data)
1140 unsigned long *mask = NULL;
1142 if (libevdev_enable_event_type(dev, type))
1159 max = type_to_mask(dev, type, &mask);
1164 set_bit(mask, code);
1166 if (type == EV_ABS) {
1167 const struct input_absinfo *abs = data;
1168 dev->abs_info[code] = *abs;
1169 } else if (type == EV_REP) {
1170 const int *value = data;
1171 dev->rep_values[code] = *value;
1178 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
1181 unsigned long *mask = NULL;
1186 max = type_to_mask(dev, type, &mask);
1191 clear_bit(mask, code);
1197 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1199 return libevdev_kernel_set_abs_info(dev, code, abs);
1203 libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1208 log_bug("device not initialized. call libevdev_set_fd() first\n");
1215 rc = ioctl(dev->fd, EVIOCSABS(code), abs);
1219 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
1225 libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
1230 log_bug("device not initialized. call libevdev_set_fd() first\n");
1234 if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB) {
1235 log_bug("invalid grab parameter %#x\n", grab);
1239 if (grab == dev->grabbed)
1242 if (grab == LIBEVDEV_GRAB)
1243 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
1244 else if (grab == LIBEVDEV_UNGRAB)
1245 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
1248 dev->grabbed = grab;
1250 return rc < 0 ? -errno : 0;
1255 libevdev_is_event_type(const struct input_event *ev, unsigned int type)
1256 ALIAS(libevdev_event_is_type);
1259 libevdev_event_is_type(const struct input_event *ev, unsigned int type)
1261 return type < EV_CNT && ev->type == type;
1266 libevdev_is_event_code(const struct input_event *ev, unsigned int type, unsigned int code)
1267 ALIAS(libevdev_event_is_code);
1270 libevdev_event_is_code(const struct input_event *ev, unsigned int type, unsigned int code)
1274 if (!libevdev_event_is_type(ev, type))
1277 max = libevdev_event_type_get_max(type);
1278 return (max > -1 && code <= (unsigned int)max && ev->code == code);
1282 LIBEVDEV_EXPORT const char*
1283 libevdev_get_event_type_name(unsigned int type)
1284 ALIAS(libevdev_event_type_get_name);
1286 LIBEVDEV_EXPORT const char*
1287 libevdev_event_type_get_name(unsigned int type)
1292 return ev_map[type];
1296 LIBEVDEV_EXPORT const char*
1297 libevdev_get_event_code_name(unsigned int type, unsigned int code)
1298 ALIAS(libevdev_event_code_get_name);
1300 LIBEVDEV_EXPORT const char*
1301 libevdev_event_code_get_name(unsigned int type, unsigned int code)
1303 int max = libevdev_event_type_get_max(type);
1305 if (max == -1 || code > (unsigned int)max)
1308 return event_type_map[type][code];
1312 LIBEVDEV_EXPORT const char*
1313 libevdev_get_input_prop_name(unsigned int prop)
1314 ALIAS(libevdev_property_get_name);
1317 LIBEVDEV_EXPORT const char*
1318 libevdev_get_property_name(unsigned int prop)
1319 ALIAS(libevdev_property_get_name);
1321 LIBEVDEV_EXPORT const char*
1322 libevdev_property_get_name(unsigned int prop)
1324 if (prop > INPUT_PROP_MAX)
1327 return input_prop_map[prop];
1332 libevdev_get_event_type_max(unsigned int type)
1333 ALIAS(libevdev_event_type_get_max);
1336 libevdev_event_type_get_max(unsigned int type)
1341 return ev_max[type];
1345 libevdev_get_repeat(struct libevdev *dev, int *delay, int *period)
1347 if (!libevdev_has_event_type(dev, EV_REP))
1351 *delay = dev->rep_values[REP_DELAY];
1353 *period = dev->rep_values[REP_PERIOD];
1359 libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value)
1361 return libevdev_kernel_set_led_values(dev, code, value, -1);
1365 libevdev_kernel_set_led_values(struct libevdev *dev, ...)
1367 struct input_event ev[LED_MAX + 1];
1368 enum libevdev_led_value val;
1375 log_bug("device not initialized. call libevdev_set_fd() first\n");
1379 memset(ev, 0, sizeof(ev));
1381 va_start(args, dev);
1382 code = va_arg(args, unsigned int);
1383 while (code != -1) {
1384 if (code > LED_MAX) {
1388 val = va_arg(args, enum libevdev_led_value);
1389 if (val != LIBEVDEV_LED_ON && val != LIBEVDEV_LED_OFF) {
1394 if (libevdev_has_event_code(dev, EV_LED, code)) {
1395 struct input_event *e = ev;
1397 while (e->type > 0 && e->code != code)
1404 e->value = (val == LIBEVDEV_LED_ON);
1406 code = va_arg(args, unsigned int);
1410 if (rc == 0 && nleds > 0) {
1411 ev[nleds].type = EV_SYN;
1412 ev[nleds++].code = SYN_REPORT;
1414 rc = write(libevdev_get_fd(dev), ev, nleds * sizeof(ev[0]));
1416 nleds--; /* last is EV_SYN */
1418 update_led_state(dev, &ev[nleds]);
1420 rc = (rc != -1) ? 0 : -errno;