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"
38 static int sync_mt_state(struct libevdev *dev, int create_events);
41 init_event_queue(struct libevdev *dev)
43 /* FIXME: count the number of axes, keys, etc. to get a better idea at how many events per
44 EV_SYN we could possibly get. Then multiply that by the actual buffer size we care about */
46 const int QUEUE_SIZE = 256;
48 return queue_alloc(dev, QUEUE_SIZE);
52 libevdev_dflt_log_func(enum libevdev_log_priority priority,
54 const char *file, int line, const char *func,
55 const char *format, va_list args)
59 case LIBEVDEV_LOG_ERROR: prefix = "libevdev error"; break;
60 case LIBEVDEV_LOG_INFO: prefix = "libevdev info"; break;
61 case LIBEVDEV_LOG_DEBUG:
62 prefix = "libevdev debug";
67 /* default logging format:
68 libevev error in libevdev_some_func: blah blah
69 libevev info in libevdev_some_func: blah blah
70 libevev debug in file.c:123:libevdev_some_func: blah blah
73 fprintf(stderr, "%s in ", prefix);
74 if (priority == LIBEVDEV_LOG_DEBUG)
75 fprintf(stderr, "%s:%d:", file, line);
76 fprintf(stderr, "%s: ", func);
77 vfprintf(stderr, format, args);
81 * Global logging settings.
83 struct logdata log_data = {
85 libevdev_dflt_log_func,
90 log_msg(enum libevdev_log_priority priority,
92 const char *file, int line, const char *func,
93 const char *format, ...)
97 if (!log_data.handler)
100 va_start(args, format);
101 log_data.handler(priority, data, file, line, func, format, args);
105 LIBEVDEV_EXPORT struct libevdev*
108 struct libevdev *dev;
110 dev = calloc(1, sizeof(*dev));
115 dev->current_slot = -1;
116 dev->grabbed = LIBEVDEV_UNGRAB;
117 dev->sync_state = SYNC_NONE;
123 libevdev_new_from_fd(int fd, struct libevdev **dev)
132 rc = libevdev_set_fd(d, fd);
141 libevdev_free(struct libevdev *dev)
155 libevdev_set_log_handler(struct libevdev *dev, libevdev_log_func_t logfunc)
157 /* Can't be backwards compatible to this yet, so don't even try */
158 fprintf(stderr, "libevdev: ABI change. Log function will not be honored.\n");
162 libevdev_set_log_function(libevdev_log_func_t logfunc, void *data)
164 log_data.handler = logfunc;
165 log_data.userdata = data;
169 libevdev_set_log_priority(enum libevdev_log_priority priority)
171 if (priority > LIBEVDEV_LOG_DEBUG)
172 priority = LIBEVDEV_LOG_DEBUG;
173 log_data.priority = priority;
176 LIBEVDEV_EXPORT enum libevdev_log_priority
177 libevdev_get_log_priority(void)
179 return log_data.priority;
183 libevdev_change_fd(struct libevdev *dev, int fd)
186 log_bug("device not initialized. call libevdev_set_fd() first\n");
194 libevdev_set_fd(struct libevdev* dev, int fd)
201 log_bug("device already initialized.\n");
205 rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
209 memset(buf, 0, sizeof(buf));
210 rc = ioctl(fd, EVIOCGNAME(sizeof(buf) - 1), buf);
215 dev->name = strdup(buf);
223 memset(buf, 0, sizeof(buf));
224 rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
226 /* uinput has no phys */
230 dev->phys = strdup(buf);
239 memset(buf, 0, sizeof(buf));
240 rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
245 dev->uniq = strdup(buf);
252 rc = ioctl(fd, EVIOCGID, &dev->ids);
256 rc = ioctl(fd, EVIOCGVERSION, &dev->driver_version);
260 rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
264 rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
268 rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
272 rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
276 rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
280 rc = ioctl(fd, EVIOCGBIT(EV_SW, sizeof(dev->sw_bits)), dev->sw_bits);
284 rc = ioctl(fd, EVIOCGBIT(EV_MSC, sizeof(dev->msc_bits)), dev->msc_bits);
288 rc = ioctl(fd, EVIOCGBIT(EV_FF, sizeof(dev->ff_bits)), dev->ff_bits);
292 rc = ioctl(fd, EVIOCGBIT(EV_SND, sizeof(dev->snd_bits)), dev->snd_bits);
296 rc = ioctl(fd, EVIOCGKEY(sizeof(dev->key_values)), dev->key_values);
300 rc = ioctl(fd, EVIOCGLED(sizeof(dev->led_values)), dev->led_values);
304 rc = ioctl(fd, EVIOCGSW(sizeof(dev->sw_values)), dev->sw_values);
308 /* rep is a special case, always set it to 1 for both values if EV_REP is set */
309 if (bit_is_set(dev->bits, EV_REP)) {
310 for (i = 0; i < REP_CNT; i++)
311 set_bit(dev->rep_bits, i);
312 rc = ioctl(fd, EVIOCGREP, dev->rep_values);
317 for (i = ABS_X; i <= ABS_MAX; i++) {
318 if (bit_is_set(dev->abs_bits, i)) {
319 struct input_absinfo abs_info;
320 rc = ioctl(fd, EVIOCGABS(i), &abs_info);
324 dev->abs_info[i] = abs_info;
325 if (i == ABS_MT_SLOT) {
326 dev->num_slots = abs_info.maximum + 1;
327 dev->current_slot = abs_info.value;
334 sync_mt_state(dev, 0);
336 rc = init_event_queue(dev);
342 /* not copying key state because we won't know when we'll start to
343 * use this fd and key's are likely to change state by then.
344 * Same with the valuators, really, but they may not change.
348 return rc ? -errno : 0;
352 libevdev_get_fd(const struct libevdev* dev)
358 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
360 ev->time = dev->last_event_time;
367 sync_key_state(struct libevdev *dev)
371 unsigned long keystate[NLONGS(KEY_CNT)] = {0};
373 rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
377 for (i = 0; i < KEY_CNT; i++) {
379 old = bit_is_set(dev->key_values, i);
380 new = bit_is_set(keystate, i);
382 struct input_event *ev = queue_push(dev);
383 init_event(dev, ev, EV_KEY, i, new ? 1 : 0);
387 memcpy(dev->key_values, keystate, rc);
391 return rc ? -errno : 0;
395 sync_sw_state(struct libevdev *dev)
399 unsigned long swstate[NLONGS(SW_CNT)] = {0};
401 rc = ioctl(dev->fd, EVIOCGSW(sizeof(swstate)), swstate);
405 for (i = 0; i < SW_CNT; i++) {
407 old = bit_is_set(dev->sw_values, i);
408 new = bit_is_set(swstate, i);
410 struct input_event *ev = queue_push(dev);
411 init_event(dev, ev, EV_SW, i, new ? 1 : 0);
415 memcpy(dev->sw_values, swstate, rc);
419 return rc ? -errno : 0;
423 sync_led_state(struct libevdev *dev)
427 unsigned long ledstate[NLONGS(LED_CNT)] = {0};
429 rc = ioctl(dev->fd, EVIOCGLED(sizeof(ledstate)), ledstate);
433 for (i = 0; i < LED_CNT; i++) {
435 old = bit_is_set(dev->led_values, i);
436 new = bit_is_set(ledstate, i);
438 struct input_event *ev = queue_push(dev);
439 init_event(dev, ev, EV_LED, i, new ? 1 : 0);
443 memcpy(dev->led_values, ledstate, rc);
447 return rc ? -errno : 0;
450 sync_abs_state(struct libevdev *dev)
455 for (i = ABS_X; i < ABS_CNT; i++) {
456 struct input_absinfo abs_info;
458 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
461 if (!bit_is_set(dev->abs_bits, i))
464 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
468 if (dev->abs_info[i].value != abs_info.value) {
469 struct input_event *ev = queue_push(dev);
471 init_event(dev, ev, EV_ABS, i, abs_info.value);
472 dev->abs_info[i].value = abs_info.value;
478 return rc ? -errno : 0;
482 sync_mt_state(struct libevdev *dev, int create_events)
489 } mt_state[ABS_MT_CNT];
491 for (i = ABS_MT_MIN; i <= ABS_MT_MAX; i++) {
493 if (i == ABS_MT_SLOT)
496 if (!libevdev_has_event_code(dev, EV_ABS, i))
499 idx = i - ABS_MT_MIN;
500 mt_state[idx].code = i;
501 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
506 for (i = 0; i < dev->num_slots; i++) {
508 struct input_event *ev;
511 ev = queue_push(dev);
512 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, i);
515 for (j = ABS_MT_MIN; j <= ABS_MT_MAX; j++) {
516 int jdx = j - ABS_MT_MIN;
518 if (j == ABS_MT_SLOT)
521 if (!libevdev_has_event_code(dev, EV_ABS, j))
524 if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
528 ev = queue_push(dev);
529 init_event(dev, ev, EV_ABS, j, mt_state[jdx].val[i]);
531 dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
537 return rc ? -errno : 0;
541 sync_state(struct libevdev *dev)
545 struct input_event *ev;
547 /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
548 queue[0]) we need to shift this backwards. Except that chances are that the
549 queue may be either full or too full to prepend all the events needed for
552 so we search for the last sync event in the queue and drop everything before
553 including that event and rely on the kernel to tell us the right value for that
554 bitfield during the sync process.
557 for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
558 struct input_event e = {{0,0}, 0, 0, 0};
559 queue_peek(dev, i, &e);
560 if (e.type == EV_SYN)
565 queue_shift_multiple(dev, i + 1, NULL);
567 if (libevdev_has_event_type(dev, EV_KEY))
568 rc = sync_key_state(dev);
569 if (libevdev_has_event_type(dev, EV_LED))
570 rc = sync_led_state(dev);
571 if (libevdev_has_event_type(dev, EV_SW))
572 rc = sync_sw_state(dev);
573 if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
574 rc = sync_abs_state(dev);
575 if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
576 rc = sync_mt_state(dev, 1);
578 dev->queue_nsync = queue_num_elements(dev);
580 if (dev->queue_nsync > 0) {
581 ev = queue_push(dev);
582 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
590 update_key_state(struct libevdev *dev, const struct input_event *e)
592 if (!libevdev_has_event_type(dev, EV_KEY))
595 if (e->code > KEY_MAX)
598 set_bit_state(dev->key_values, e->code, e->value != 0);
604 update_mt_state(struct libevdev *dev, const struct input_event *e)
606 if (e->code == ABS_MT_SLOT) {
608 dev->current_slot = e->value;
609 /* sync abs_info with the current slot values */
610 for (i = ABS_MT_SLOT + 1; i <= ABS_MT_MAX; i++) {
611 if (libevdev_has_event_code(dev, EV_ABS, i))
612 dev->abs_info[i].value = dev->mt_slot_vals[dev->current_slot][i - ABS_MT_MIN];
616 } else if (dev->current_slot == -1)
619 dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
625 update_abs_state(struct libevdev *dev, const struct input_event *e)
627 if (!libevdev_has_event_type(dev, EV_ABS))
630 if (e->code > ABS_MAX)
633 if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
634 update_mt_state(dev, e);
636 dev->abs_info[e->code].value = e->value;
642 update_led_state(struct libevdev *dev, const struct input_event *e)
644 if (!libevdev_has_event_type(dev, EV_LED))
647 if (e->code > LED_MAX)
650 set_bit_state(dev->led_values, e->code, e->value != 0);
656 update_sw_state(struct libevdev *dev, const struct input_event *e)
658 if (!libevdev_has_event_type(dev, EV_SW))
661 if (e->code > SW_MAX)
664 set_bit_state(dev->sw_values, e->code, e->value != 0);
670 update_state(struct libevdev *dev, const struct input_event *e)
679 rc = update_key_state(dev, e);
682 rc = update_abs_state(dev, e);
685 rc = update_led_state(dev, e);
688 rc = update_sw_state(dev, e);
692 dev->last_event_time = e->time;
698 read_more_events(struct libevdev *dev)
702 struct input_event *next;
704 free_elem = queue_num_free_elements(dev);
708 next = queue_next_element(dev);
709 len = read(dev->fd, next, free_elem * sizeof(struct input_event));
712 } else if (len > 0 && len % sizeof(struct input_event) != 0)
715 int nev = len/sizeof(struct input_event);
716 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
723 libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
728 log_bug("device not initialized. call libevdev_set_fd() first\n");
732 if (!(flags & (LIBEVDEV_READ_NORMAL|LIBEVDEV_READ_SYNC|LIBEVDEV_FORCE_SYNC))) {
733 log_bug("invalid flags %#x\n.\n", flags);
737 if (flags & LIBEVDEV_READ_SYNC) {
738 if (dev->sync_state == SYNC_NEEDED) {
739 rc = sync_state(dev);
742 dev->sync_state = SYNC_IN_PROGRESS;
745 if (dev->queue_nsync == 0) {
746 dev->sync_state = SYNC_NONE;
750 } else if (dev->sync_state != SYNC_NONE) {
751 struct input_event e;
753 /* call update_state for all events here, otherwise the library has the wrong view
755 while (queue_shift(dev, &e) == 0) {
757 update_state(dev, &e);
760 dev->sync_state = SYNC_NONE;
763 /* FIXME: if the first event after SYNC_IN_PROGRESS is a SYN_DROPPED, log this */
765 /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
766 worst case we don't read fast enough and end up with SYN_DROPPED anyway.
768 Except if the fd is in blocking mode and we still have events from the last read, don't
772 if (!(flags & LIBEVDEV_READ_BLOCKING) ||
773 queue_num_elements(dev) == 0) {
774 rc = read_more_events(dev);
775 if (rc < 0 && rc != -EAGAIN)
779 if (flags & LIBEVDEV_FORCE_SYNC) {
780 dev->sync_state = SYNC_NEEDED;
786 if (queue_shift(dev, ev) != 0)
789 update_state(dev, ev);
791 /* if we disabled a code, get the next event instead */
792 } while(!libevdev_has_event_code(dev, ev->type, ev->code));
795 if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
796 dev->sync_state = SYNC_NEEDED;
800 if (flags & LIBEVDEV_READ_SYNC && dev->queue_nsync > 0) {
803 if (dev->queue_nsync == 0)
804 dev->sync_state = SYNC_NONE;
812 libevdev_has_event_pending(struct libevdev *dev)
814 struct pollfd fds = { dev->fd, POLLIN, 0 };
818 log_bug("device not initialized. call libevdev_set_fd() first\n");
822 if (queue_num_elements(dev) != 0)
825 rc = poll(&fds, 1, 0);
826 return (rc >= 0) ? rc : -errno;
829 LIBEVDEV_EXPORT const char *
830 libevdev_get_name(const struct libevdev *dev)
832 return dev->name ? dev->name : "";
835 LIBEVDEV_EXPORT const char *
836 libevdev_get_phys(const struct libevdev *dev)
841 LIBEVDEV_EXPORT const char *
842 libevdev_get_uniq(const struct libevdev *dev)
847 #define STRING_SETTER(field) \
848 LIBEVDEV_EXPORT void libevdev_set_##field(struct libevdev *dev, const char *field) \
853 dev->field = strdup(field); \
861 #define PRODUCT_GETTER(name) \
862 LIBEVDEV_EXPORT int libevdev_get_id_##name(const struct libevdev *dev) \
864 return dev->ids.name; \
867 PRODUCT_GETTER(product);
868 PRODUCT_GETTER(vendor);
869 PRODUCT_GETTER(bustype);
870 PRODUCT_GETTER(version);
872 #define PRODUCT_SETTER(field) \
873 LIBEVDEV_EXPORT void libevdev_set_id_##field(struct libevdev *dev, int field) \
875 dev->ids.field = field;\
878 PRODUCT_SETTER(product);
879 PRODUCT_SETTER(vendor);
880 PRODUCT_SETTER(bustype);
881 PRODUCT_SETTER(version);
884 libevdev_get_driver_version(const struct libevdev *dev)
886 return dev->driver_version;
890 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
892 return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
896 libevdev_enable_property(struct libevdev *dev, unsigned int prop)
898 if (prop > INPUT_PROP_MAX)
901 set_bit(dev->props, prop);
906 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
908 return (type <= EV_MAX) && bit_is_set(dev->bits, type);
912 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
914 const unsigned long *mask;
917 if (!libevdev_has_event_type(dev, type))
923 max = type_to_mask_const(dev, type, &mask);
925 if (max == -1 || code > (unsigned int)max)
928 return bit_is_set(mask, code);
932 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
936 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
940 case EV_ABS: value = dev->abs_info[code].value; break;
941 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
942 case EV_LED: value = bit_is_set(dev->led_values, code); break;
943 case EV_SW: value = bit_is_set(dev->sw_values, code); break;
953 libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value)
956 struct input_event e;
958 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
966 case EV_ABS: rc = update_abs_state(dev, &e); break;
967 case EV_KEY: rc = update_key_state(dev, &e); break;
968 case EV_LED: rc = update_led_state(dev, &e); break;
969 case EV_SW: rc = update_sw_state(dev, &e); break;
979 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
981 if (libevdev_has_event_type(dev, type) &&
982 libevdev_has_event_code(dev, type, code)) {
983 *value = libevdev_get_event_value(dev, type, code);
990 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
992 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
995 if (dev->num_slots < 0 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
998 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1001 return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
1005 libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value)
1007 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1010 if (dev->num_slots == -1 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
1013 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1016 if (code == ABS_MT_SLOT) {
1017 if (value < 0 || value >= libevdev_get_num_slots(dev))
1019 dev->current_slot = value;
1022 dev->mt_slot_vals[slot][code - ABS_MT_MIN] = value;
1029 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
1031 if (libevdev_has_event_type(dev, EV_ABS) &&
1032 libevdev_has_event_code(dev, EV_ABS, code) &&
1033 dev->num_slots >= 0 &&
1034 slot < (unsigned int)dev->num_slots && slot < MAX_SLOTS) {
1035 *value = libevdev_get_slot_value(dev, slot, code);
1042 libevdev_get_num_slots(const struct libevdev *dev)
1044 return dev->num_slots;
1048 libevdev_get_current_slot(const struct libevdev *dev)
1050 return dev->current_slot;
1053 LIBEVDEV_EXPORT const struct input_absinfo*
1054 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
1056 if (!libevdev_has_event_type(dev, EV_ABS) ||
1057 !libevdev_has_event_code(dev, EV_ABS, code))
1060 return &dev->abs_info[code];
1063 #define ABS_GETTER(name) \
1064 LIBEVDEV_EXPORT int libevdev_get_abs_##name(const struct libevdev *dev, unsigned int code) \
1066 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code); \
1067 return absinfo ? absinfo->name : 0; \
1070 ABS_GETTER(maximum);
1071 ABS_GETTER(minimum);
1074 ABS_GETTER(resolution);
1076 #define ABS_SETTER(field) \
1077 LIBEVDEV_EXPORT void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
1079 if (!libevdev_has_event_code(dev, EV_ABS, code)) \
1081 dev->abs_info[code].field = val; \
1088 ABS_SETTER(resolution)
1090 LIBEVDEV_EXPORT void
1091 libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1093 if (!libevdev_has_event_code(dev, EV_ABS, code))
1096 dev->abs_info[code] = *abs;
1100 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
1105 if (libevdev_has_event_type(dev, type))
1108 set_bit(dev->bits, type);
1110 if (type == EV_REP) {
1111 int delay = 0, period = 0;
1112 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
1113 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
1119 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
1121 if (type > EV_MAX || type == EV_SYN)
1124 clear_bit(dev->bits, type);
1130 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
1131 unsigned int code, const void *data)
1134 unsigned long *mask = NULL;
1136 if (libevdev_enable_event_type(dev, type))
1153 max = type_to_mask(dev, type, &mask);
1158 set_bit(mask, code);
1160 if (type == EV_ABS) {
1161 const struct input_absinfo *abs = data;
1162 dev->abs_info[code] = *abs;
1163 } else if (type == EV_REP) {
1164 const int *value = data;
1165 dev->rep_values[code] = *value;
1172 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
1175 unsigned long *mask = NULL;
1180 max = type_to_mask(dev, type, &mask);
1185 clear_bit(mask, code);
1191 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1193 return libevdev_kernel_set_abs_info(dev, code, abs);
1197 libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1202 log_bug("device not initialized. call libevdev_set_fd() first\n");
1209 rc = ioctl(dev->fd, EVIOCSABS(code), abs);
1213 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
1219 libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
1224 log_bug("device not initialized. call libevdev_set_fd() first\n");
1228 if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB) {
1229 log_bug("invalid grab parameter %#x\n", grab);
1233 if (grab == dev->grabbed)
1236 if (grab == LIBEVDEV_GRAB)
1237 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
1238 else if (grab == LIBEVDEV_UNGRAB)
1239 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
1242 dev->grabbed = grab;
1244 return rc < 0 ? -errno : 0;
1249 libevdev_is_event_type(const struct input_event *ev, unsigned int type)
1250 ALIAS(libevdev_event_is_type);
1253 libevdev_event_is_type(const struct input_event *ev, unsigned int type)
1255 return type < EV_CNT && ev->type == type;
1260 libevdev_is_event_code(const struct input_event *ev, unsigned int type, unsigned int code)
1261 ALIAS(libevdev_event_is_code);
1264 libevdev_event_is_code(const struct input_event *ev, unsigned int type, unsigned int code)
1268 if (!libevdev_event_is_type(ev, type))
1271 max = libevdev_event_type_get_max(type);
1272 return (max > -1 && code <= (unsigned int)max && ev->code == code);
1276 LIBEVDEV_EXPORT const char*
1277 libevdev_get_event_type_name(unsigned int type)
1278 ALIAS(libevdev_event_type_get_name);
1280 LIBEVDEV_EXPORT const char*
1281 libevdev_event_type_get_name(unsigned int type)
1286 return ev_map[type];
1290 LIBEVDEV_EXPORT const char*
1291 libevdev_get_event_code_name(unsigned int type, unsigned int code)
1292 ALIAS(libevdev_event_code_get_name);
1294 LIBEVDEV_EXPORT const char*
1295 libevdev_event_code_get_name(unsigned int type, unsigned int code)
1297 int max = libevdev_event_type_get_max(type);
1299 if (max == -1 || code > (unsigned int)max)
1302 return event_type_map[type][code];
1306 LIBEVDEV_EXPORT const char*
1307 libevdev_get_input_prop_name(unsigned int prop)
1308 ALIAS(libevdev_property_get_name);
1311 LIBEVDEV_EXPORT const char*
1312 libevdev_get_property_name(unsigned int prop)
1313 ALIAS(libevdev_property_get_name);
1315 LIBEVDEV_EXPORT const char*
1316 libevdev_property_get_name(unsigned int prop)
1318 if (prop > INPUT_PROP_MAX)
1321 return input_prop_map[prop];
1326 libevdev_get_event_type_max(unsigned int type)
1327 ALIAS(libevdev_event_type_get_max);
1330 libevdev_event_type_get_max(unsigned int type)
1335 return ev_max[type];
1339 libevdev_get_repeat(struct libevdev *dev, int *delay, int *period)
1341 if (!libevdev_has_event_type(dev, EV_REP))
1345 *delay = dev->rep_values[REP_DELAY];
1347 *period = dev->rep_values[REP_PERIOD];
1353 libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value)
1355 return libevdev_kernel_set_led_values(dev, code, value, -1);
1359 libevdev_kernel_set_led_values(struct libevdev *dev, ...)
1361 struct input_event ev[LED_MAX + 1];
1362 enum libevdev_led_value val;
1369 log_bug("device not initialized. call libevdev_set_fd() first\n");
1373 memset(ev, 0, sizeof(ev));
1375 va_start(args, dev);
1376 code = va_arg(args, unsigned int);
1377 while (code != -1) {
1378 if (code > LED_MAX) {
1382 val = va_arg(args, enum libevdev_led_value);
1383 if (val != LIBEVDEV_LED_ON && val != LIBEVDEV_LED_OFF) {
1388 if (libevdev_has_event_code(dev, EV_LED, code)) {
1389 struct input_event *e = ev;
1391 while (e->type > 0 && e->code != code)
1398 e->value = (val == LIBEVDEV_LED_ON);
1400 code = va_arg(args, unsigned int);
1404 if (rc == 0 && nleds > 0) {
1405 ev[nleds].type = EV_SYN;
1406 ev[nleds++].code = SYN_REPORT;
1408 rc = write(libevdev_get_fd(dev), ev, nleds * sizeof(ev[0]));
1410 nleds--; /* last is EV_SYN */
1412 update_led_state(dev, &ev[nleds]);
1414 rc = (rc != -1) ? 0 : -errno;