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");
215 rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
219 memset(buf, 0, sizeof(buf));
220 rc = ioctl(fd, EVIOCGNAME(sizeof(buf) - 1), buf);
225 dev->name = strdup(buf);
233 memset(buf, 0, sizeof(buf));
234 rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
236 /* uinput has no phys */
240 dev->phys = strdup(buf);
249 memset(buf, 0, sizeof(buf));
250 rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
255 dev->uniq = strdup(buf);
262 rc = ioctl(fd, EVIOCGID, &dev->ids);
266 rc = ioctl(fd, EVIOCGVERSION, &dev->driver_version);
270 rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
274 rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
278 rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
282 rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
286 rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
290 rc = ioctl(fd, EVIOCGBIT(EV_SW, sizeof(dev->sw_bits)), dev->sw_bits);
294 rc = ioctl(fd, EVIOCGBIT(EV_MSC, sizeof(dev->msc_bits)), dev->msc_bits);
298 rc = ioctl(fd, EVIOCGBIT(EV_FF, sizeof(dev->ff_bits)), dev->ff_bits);
302 rc = ioctl(fd, EVIOCGBIT(EV_SND, sizeof(dev->snd_bits)), dev->snd_bits);
306 rc = ioctl(fd, EVIOCGKEY(sizeof(dev->key_values)), dev->key_values);
310 rc = ioctl(fd, EVIOCGLED(sizeof(dev->led_values)), dev->led_values);
314 rc = ioctl(fd, EVIOCGSW(sizeof(dev->sw_values)), dev->sw_values);
318 /* rep is a special case, always set it to 1 for both values if EV_REP is set */
319 if (bit_is_set(dev->bits, EV_REP)) {
320 for (i = 0; i < REP_CNT; i++)
321 set_bit(dev->rep_bits, i);
322 rc = ioctl(fd, EVIOCGREP, dev->rep_values);
327 for (i = ABS_X; i <= ABS_MAX; i++) {
328 if (bit_is_set(dev->abs_bits, i)) {
329 struct input_absinfo abs_info;
330 rc = ioctl(fd, EVIOCGABS(i), &abs_info);
334 dev->abs_info[i] = abs_info;
335 if (i == ABS_MT_SLOT) {
336 dev->num_slots = abs_info.maximum + 1;
337 dev->current_slot = abs_info.value;
344 sync_mt_state(dev, 0);
346 rc = init_event_queue(dev);
352 /* not copying key state because we won't know when we'll start to
353 * use this fd and key's are likely to change state by then.
354 * Same with the valuators, really, but they may not change.
357 dev->initialized = true;
359 return rc ? -errno : 0;
363 libevdev_get_fd(const struct libevdev* dev)
369 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
371 ev->time = dev->last_event_time;
378 sync_key_state(struct libevdev *dev)
382 unsigned long keystate[NLONGS(KEY_CNT)] = {0};
384 rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
388 for (i = 0; i < KEY_CNT; i++) {
390 old = bit_is_set(dev->key_values, i);
391 new = bit_is_set(keystate, i);
393 struct input_event *ev = queue_push(dev);
394 init_event(dev, ev, EV_KEY, i, new ? 1 : 0);
398 memcpy(dev->key_values, keystate, rc);
402 return rc ? -errno : 0;
406 sync_sw_state(struct libevdev *dev)
410 unsigned long swstate[NLONGS(SW_CNT)] = {0};
412 rc = ioctl(dev->fd, EVIOCGSW(sizeof(swstate)), swstate);
416 for (i = 0; i < SW_CNT; i++) {
418 old = bit_is_set(dev->sw_values, i);
419 new = bit_is_set(swstate, i);
421 struct input_event *ev = queue_push(dev);
422 init_event(dev, ev, EV_SW, i, new ? 1 : 0);
426 memcpy(dev->sw_values, swstate, rc);
430 return rc ? -errno : 0;
434 sync_led_state(struct libevdev *dev)
438 unsigned long ledstate[NLONGS(LED_CNT)] = {0};
440 rc = ioctl(dev->fd, EVIOCGLED(sizeof(ledstate)), ledstate);
444 for (i = 0; i < LED_CNT; i++) {
446 old = bit_is_set(dev->led_values, i);
447 new = bit_is_set(ledstate, i);
449 struct input_event *ev = queue_push(dev);
450 init_event(dev, ev, EV_LED, i, new ? 1 : 0);
454 memcpy(dev->led_values, ledstate, rc);
458 return rc ? -errno : 0;
461 sync_abs_state(struct libevdev *dev)
466 for (i = ABS_X; i < ABS_CNT; i++) {
467 struct input_absinfo abs_info;
469 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
472 if (!bit_is_set(dev->abs_bits, i))
475 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
479 if (dev->abs_info[i].value != abs_info.value) {
480 struct input_event *ev = queue_push(dev);
482 init_event(dev, ev, EV_ABS, i, abs_info.value);
483 dev->abs_info[i].value = abs_info.value;
489 return rc ? -errno : 0;
493 sync_mt_state(struct libevdev *dev, int create_events)
500 } mt_state[ABS_MT_CNT];
502 for (i = ABS_MT_MIN; i <= ABS_MT_MAX; i++) {
504 if (i == ABS_MT_SLOT)
507 if (!libevdev_has_event_code(dev, EV_ABS, i))
510 idx = i - ABS_MT_MIN;
511 mt_state[idx].code = i;
512 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
517 for (i = 0; i < dev->num_slots; i++) {
519 struct input_event *ev;
522 ev = queue_push(dev);
523 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, i);
526 for (j = ABS_MT_MIN; j <= ABS_MT_MAX; j++) {
527 int jdx = j - ABS_MT_MIN;
529 if (j == ABS_MT_SLOT)
532 if (!libevdev_has_event_code(dev, EV_ABS, j))
535 if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
539 ev = queue_push(dev);
540 init_event(dev, ev, EV_ABS, j, mt_state[jdx].val[i]);
542 dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
548 return rc ? -errno : 0;
552 sync_state(struct libevdev *dev)
556 struct input_event *ev;
558 /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
559 queue[0]) we need to shift this backwards. Except that chances are that the
560 queue may be either full or too full to prepend all the events needed for
563 so we search for the last sync event in the queue and drop everything before
564 including that event and rely on the kernel to tell us the right value for that
565 bitfield during the sync process.
568 for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
569 struct input_event e = {{0,0}, 0, 0, 0};
570 queue_peek(dev, i, &e);
571 if (e.type == EV_SYN)
576 queue_shift_multiple(dev, i + 1, NULL);
578 if (libevdev_has_event_type(dev, EV_KEY))
579 rc = sync_key_state(dev);
580 if (libevdev_has_event_type(dev, EV_LED))
581 rc = sync_led_state(dev);
582 if (libevdev_has_event_type(dev, EV_SW))
583 rc = sync_sw_state(dev);
584 if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
585 rc = sync_abs_state(dev);
586 if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
587 rc = sync_mt_state(dev, 1);
589 dev->queue_nsync = queue_num_elements(dev);
591 if (dev->queue_nsync > 0) {
592 ev = queue_push(dev);
593 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
601 update_key_state(struct libevdev *dev, const struct input_event *e)
603 if (!libevdev_has_event_type(dev, EV_KEY))
606 if (e->code > KEY_MAX)
609 set_bit_state(dev->key_values, e->code, e->value != 0);
615 update_mt_state(struct libevdev *dev, const struct input_event *e)
617 if (e->code == ABS_MT_SLOT) {
619 dev->current_slot = e->value;
620 /* sync abs_info with the current slot values */
621 for (i = ABS_MT_SLOT + 1; i <= ABS_MT_MAX; i++) {
622 if (libevdev_has_event_code(dev, EV_ABS, i))
623 dev->abs_info[i].value = dev->mt_slot_vals[dev->current_slot][i - ABS_MT_MIN];
627 } else if (dev->current_slot == -1)
630 dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
636 update_abs_state(struct libevdev *dev, const struct input_event *e)
638 if (!libevdev_has_event_type(dev, EV_ABS))
641 if (e->code > ABS_MAX)
644 if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
645 update_mt_state(dev, e);
647 dev->abs_info[e->code].value = e->value;
653 update_led_state(struct libevdev *dev, const struct input_event *e)
655 if (!libevdev_has_event_type(dev, EV_LED))
658 if (e->code > LED_MAX)
661 set_bit_state(dev->led_values, e->code, e->value != 0);
667 update_sw_state(struct libevdev *dev, const struct input_event *e)
669 if (!libevdev_has_event_type(dev, EV_SW))
672 if (e->code > SW_MAX)
675 set_bit_state(dev->sw_values, e->code, e->value != 0);
681 update_state(struct libevdev *dev, const struct input_event *e)
690 rc = update_key_state(dev, e);
693 rc = update_abs_state(dev, e);
696 rc = update_led_state(dev, e);
699 rc = update_sw_state(dev, e);
703 dev->last_event_time = e->time;
709 read_more_events(struct libevdev *dev)
713 struct input_event *next;
715 free_elem = queue_num_free_elements(dev);
719 next = queue_next_element(dev);
720 len = read(dev->fd, next, free_elem * sizeof(struct input_event));
723 } else if (len > 0 && len % sizeof(struct input_event) != 0)
726 int nev = len/sizeof(struct input_event);
727 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
734 libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
736 int rc = LIBEVDEV_READ_STATUS_SUCCESS;
738 if (!dev->initialized) {
739 log_bug("device not initialized. call libevdev_set_fd() first\n");
741 } else if (dev->fd < 0)
744 if (!(flags & (LIBEVDEV_READ_FLAG_NORMAL|LIBEVDEV_READ_FLAG_SYNC|LIBEVDEV_READ_FLAG_FORCE_SYNC))) {
745 log_bug("invalid flags %#x\n.\n", flags);
749 if (flags & LIBEVDEV_READ_FLAG_SYNC) {
750 if (dev->sync_state == SYNC_NEEDED) {
751 rc = sync_state(dev);
754 dev->sync_state = SYNC_IN_PROGRESS;
757 if (dev->queue_nsync == 0) {
758 dev->sync_state = SYNC_NONE;
762 } else if (dev->sync_state != SYNC_NONE) {
763 struct input_event e;
765 /* call update_state for all events here, otherwise the library has the wrong view
767 while (queue_shift(dev, &e) == 0) {
769 update_state(dev, &e);
772 dev->sync_state = SYNC_NONE;
775 /* FIXME: if the first event after SYNC_IN_PROGRESS is a SYN_DROPPED, log this */
777 /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
778 worst case we don't read fast enough and end up with SYN_DROPPED anyway.
780 Except if the fd is in blocking mode and we still have events from the last read, don't
784 if (!(flags & LIBEVDEV_READ_FLAG_BLOCKING) ||
785 queue_num_elements(dev) == 0) {
786 rc = read_more_events(dev);
787 if (rc < 0 && rc != -EAGAIN)
791 if (flags & LIBEVDEV_READ_FLAG_FORCE_SYNC) {
792 dev->sync_state = SYNC_NEEDED;
793 rc = LIBEVDEV_READ_STATUS_SYNC;
798 if (queue_shift(dev, ev) != 0)
801 update_state(dev, ev);
803 /* if we disabled a code, get the next event instead */
804 } while(!libevdev_has_event_code(dev, ev->type, ev->code));
807 if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
808 dev->sync_state = SYNC_NEEDED;
809 rc = LIBEVDEV_READ_STATUS_SYNC;
812 if (flags & LIBEVDEV_READ_FLAG_SYNC && dev->queue_nsync > 0) {
814 rc = LIBEVDEV_READ_STATUS_SYNC;
815 if (dev->queue_nsync == 0)
816 dev->sync_state = SYNC_NONE;
824 libevdev_has_event_pending(struct libevdev *dev)
826 struct pollfd fds = { dev->fd, POLLIN, 0 };
829 if (!dev->initialized) {
830 log_bug("device not initialized. call libevdev_set_fd() first\n");
832 } else if (dev->fd < 0)
835 if (queue_num_elements(dev) != 0)
838 rc = poll(&fds, 1, 0);
839 return (rc >= 0) ? rc : -errno;
842 LIBEVDEV_EXPORT const char *
843 libevdev_get_name(const struct libevdev *dev)
845 return dev->name ? dev->name : "";
848 LIBEVDEV_EXPORT const char *
849 libevdev_get_phys(const struct libevdev *dev)
854 LIBEVDEV_EXPORT const char *
855 libevdev_get_uniq(const struct libevdev *dev)
860 #define STRING_SETTER(field) \
861 LIBEVDEV_EXPORT void libevdev_set_##field(struct libevdev *dev, const char *field) \
866 dev->field = strdup(field); \
874 #define PRODUCT_GETTER(name) \
875 LIBEVDEV_EXPORT int libevdev_get_id_##name(const struct libevdev *dev) \
877 return dev->ids.name; \
880 PRODUCT_GETTER(product);
881 PRODUCT_GETTER(vendor);
882 PRODUCT_GETTER(bustype);
883 PRODUCT_GETTER(version);
885 #define PRODUCT_SETTER(field) \
886 LIBEVDEV_EXPORT void libevdev_set_id_##field(struct libevdev *dev, int field) \
888 dev->ids.field = field;\
891 PRODUCT_SETTER(product);
892 PRODUCT_SETTER(vendor);
893 PRODUCT_SETTER(bustype);
894 PRODUCT_SETTER(version);
897 libevdev_get_driver_version(const struct libevdev *dev)
899 return dev->driver_version;
903 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
905 return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
909 libevdev_enable_property(struct libevdev *dev, unsigned int prop)
911 if (prop > INPUT_PROP_MAX)
914 set_bit(dev->props, prop);
919 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
921 return (type <= EV_MAX) && bit_is_set(dev->bits, type);
925 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
927 const unsigned long *mask;
930 if (!libevdev_has_event_type(dev, type))
936 max = type_to_mask_const(dev, type, &mask);
938 if (max == -1 || code > (unsigned int)max)
941 return bit_is_set(mask, code);
945 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
949 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
953 case EV_ABS: value = dev->abs_info[code].value; break;
954 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
955 case EV_LED: value = bit_is_set(dev->led_values, code); break;
956 case EV_SW: value = bit_is_set(dev->sw_values, code); break;
966 libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value)
969 struct input_event e;
971 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
979 case EV_ABS: rc = update_abs_state(dev, &e); break;
980 case EV_KEY: rc = update_key_state(dev, &e); break;
981 case EV_LED: rc = update_led_state(dev, &e); break;
982 case EV_SW: rc = update_sw_state(dev, &e); break;
992 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
994 if (libevdev_has_event_type(dev, type) &&
995 libevdev_has_event_code(dev, type, code)) {
996 *value = libevdev_get_event_value(dev, type, code);
1003 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
1005 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1008 if (dev->num_slots < 0 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
1011 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1014 return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
1018 libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value)
1020 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1023 if (dev->num_slots == -1 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
1026 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1029 if (code == ABS_MT_SLOT) {
1030 if (value < 0 || value >= libevdev_get_num_slots(dev))
1032 dev->current_slot = value;
1035 dev->mt_slot_vals[slot][code - ABS_MT_MIN] = value;
1042 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
1044 if (libevdev_has_event_type(dev, EV_ABS) &&
1045 libevdev_has_event_code(dev, EV_ABS, code) &&
1046 dev->num_slots >= 0 &&
1047 slot < (unsigned int)dev->num_slots && slot < MAX_SLOTS) {
1048 *value = libevdev_get_slot_value(dev, slot, code);
1055 libevdev_get_num_slots(const struct libevdev *dev)
1057 return dev->num_slots;
1061 libevdev_get_current_slot(const struct libevdev *dev)
1063 return dev->current_slot;
1066 LIBEVDEV_EXPORT const struct input_absinfo*
1067 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
1069 if (!libevdev_has_event_type(dev, EV_ABS) ||
1070 !libevdev_has_event_code(dev, EV_ABS, code))
1073 return &dev->abs_info[code];
1076 #define ABS_GETTER(name) \
1077 LIBEVDEV_EXPORT int libevdev_get_abs_##name(const struct libevdev *dev, unsigned int code) \
1079 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code); \
1080 return absinfo ? absinfo->name : 0; \
1083 ABS_GETTER(maximum);
1084 ABS_GETTER(minimum);
1087 ABS_GETTER(resolution);
1089 #define ABS_SETTER(field) \
1090 LIBEVDEV_EXPORT void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
1092 if (!libevdev_has_event_code(dev, EV_ABS, code)) \
1094 dev->abs_info[code].field = val; \
1101 ABS_SETTER(resolution)
1103 LIBEVDEV_EXPORT void
1104 libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1106 if (!libevdev_has_event_code(dev, EV_ABS, code))
1109 dev->abs_info[code] = *abs;
1113 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
1118 if (libevdev_has_event_type(dev, type))
1121 set_bit(dev->bits, type);
1123 if (type == EV_REP) {
1124 int delay = 0, period = 0;
1125 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
1126 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
1132 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
1134 if (type > EV_MAX || type == EV_SYN)
1137 clear_bit(dev->bits, type);
1143 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
1144 unsigned int code, const void *data)
1147 unsigned long *mask = NULL;
1149 if (libevdev_enable_event_type(dev, type))
1166 max = type_to_mask(dev, type, &mask);
1171 set_bit(mask, code);
1173 if (type == EV_ABS) {
1174 const struct input_absinfo *abs = data;
1175 dev->abs_info[code] = *abs;
1176 } else if (type == EV_REP) {
1177 const int *value = data;
1178 dev->rep_values[code] = *value;
1185 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
1188 unsigned long *mask = NULL;
1193 max = type_to_mask(dev, type, &mask);
1198 clear_bit(mask, code);
1204 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1206 return libevdev_kernel_set_abs_info(dev, code, abs);
1210 libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1214 if (!dev->initialized) {
1215 log_bug("device not initialized. call libevdev_set_fd() first\n");
1217 } else if (dev->fd < 0)
1223 rc = ioctl(dev->fd, EVIOCSABS(code), abs);
1227 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
1233 libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
1237 if (!dev->initialized) {
1238 log_bug("device not initialized. call libevdev_set_fd() first\n");
1240 } else if (dev->fd < 0)
1243 if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB) {
1244 log_bug("invalid grab parameter %#x\n", grab);
1248 if (grab == dev->grabbed)
1251 if (grab == LIBEVDEV_GRAB)
1252 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
1253 else if (grab == LIBEVDEV_UNGRAB)
1254 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
1257 dev->grabbed = grab;
1259 return rc < 0 ? -errno : 0;
1264 libevdev_is_event_type(const struct input_event *ev, unsigned int type)
1265 ALIAS(libevdev_event_is_type);
1268 libevdev_event_is_type(const struct input_event *ev, unsigned int type)
1270 return type < EV_CNT && ev->type == type;
1275 libevdev_is_event_code(const struct input_event *ev, unsigned int type, unsigned int code)
1276 ALIAS(libevdev_event_is_code);
1279 libevdev_event_is_code(const struct input_event *ev, unsigned int type, unsigned int code)
1283 if (!libevdev_event_is_type(ev, type))
1286 max = libevdev_event_type_get_max(type);
1287 return (max > -1 && code <= (unsigned int)max && ev->code == code);
1291 LIBEVDEV_EXPORT const char*
1292 libevdev_get_event_type_name(unsigned int type)
1293 ALIAS(libevdev_event_type_get_name);
1295 LIBEVDEV_EXPORT const char*
1296 libevdev_event_type_get_name(unsigned int type)
1301 return ev_map[type];
1305 LIBEVDEV_EXPORT const char*
1306 libevdev_get_event_code_name(unsigned int type, unsigned int code)
1307 ALIAS(libevdev_event_code_get_name);
1309 LIBEVDEV_EXPORT const char*
1310 libevdev_event_code_get_name(unsigned int type, unsigned int code)
1312 int max = libevdev_event_type_get_max(type);
1314 if (max == -1 || code > (unsigned int)max)
1317 return event_type_map[type][code];
1321 LIBEVDEV_EXPORT const char*
1322 libevdev_get_input_prop_name(unsigned int prop)
1323 ALIAS(libevdev_property_get_name);
1326 LIBEVDEV_EXPORT const char*
1327 libevdev_get_property_name(unsigned int prop)
1328 ALIAS(libevdev_property_get_name);
1330 LIBEVDEV_EXPORT const char*
1331 libevdev_property_get_name(unsigned int prop)
1333 if (prop > INPUT_PROP_MAX)
1336 return input_prop_map[prop];
1341 libevdev_get_event_type_max(unsigned int type)
1342 ALIAS(libevdev_event_type_get_max);
1345 libevdev_event_type_get_max(unsigned int type)
1350 return ev_max[type];
1354 libevdev_get_repeat(struct libevdev *dev, int *delay, int *period)
1356 if (!libevdev_has_event_type(dev, EV_REP))
1360 *delay = dev->rep_values[REP_DELAY];
1362 *period = dev->rep_values[REP_PERIOD];
1368 libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value)
1370 return libevdev_kernel_set_led_values(dev, code, value, -1);
1374 libevdev_kernel_set_led_values(struct libevdev *dev, ...)
1376 struct input_event ev[LED_MAX + 1];
1377 enum libevdev_led_value val;
1383 if (!dev->initialized) {
1384 log_bug("device not initialized. call libevdev_set_fd() first\n");
1386 } else if (dev->fd < 0)
1389 memset(ev, 0, sizeof(ev));
1391 va_start(args, dev);
1392 code = va_arg(args, unsigned int);
1393 while (code != -1) {
1394 if (code > LED_MAX) {
1398 val = va_arg(args, enum libevdev_led_value);
1399 if (val != LIBEVDEV_LED_ON && val != LIBEVDEV_LED_OFF) {
1404 if (libevdev_has_event_code(dev, EV_LED, code)) {
1405 struct input_event *e = ev;
1407 while (e->type > 0 && e->code != code)
1414 e->value = (val == LIBEVDEV_LED_ON);
1416 code = va_arg(args, unsigned int);
1420 if (rc == 0 && nleds > 0) {
1421 ev[nleds].type = EV_SYN;
1422 ev[nleds++].code = SYN_REPORT;
1424 rc = write(libevdev_get_fd(dev), ev, nleds * sizeof(ev[0]));
1426 nleds--; /* last is EV_SYN */
1428 update_led_state(dev, &ev[nleds]);
1430 rc = (rc != -1) ? 0 : -errno;
1437 libevdev_set_clock_id(struct libevdev *dev, int clockid)
1439 if (!dev->initialized) {
1440 log_bug("device not initialized. call libevdev_set_fd() first\n");
1442 } else if (dev->fd < 0)
1445 return ioctl(dev->fd, EVIOCSCLOCKID, &clockid) ? -errno : 0;