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_noop_log_func(enum libevdev_log_priority priority,
54 const char *file, int line, const char *func,
55 const char *format, va_list args)
60 * Global logging settings.
62 struct logdata log_data = {
64 libevdev_noop_log_func,
69 log_msg(enum libevdev_log_priority priority,
71 const char *file, int line, const char *func,
72 const char *format, ...)
76 va_start(args, format);
77 log_data.handler(priority, data, file, line, func, format, args);
81 LIBEVDEV_EXPORT struct libevdev*
86 dev = calloc(1, sizeof(*dev));
91 dev->current_slot = -1;
92 dev->grabbed = LIBEVDEV_UNGRAB;
93 dev->sync_state = SYNC_NONE;
99 libevdev_new_from_fd(int fd, struct libevdev **dev)
108 rc = libevdev_set_fd(d, fd);
117 libevdev_free(struct libevdev *dev)
131 libevdev_set_log_handler(struct libevdev *dev, libevdev_log_func_t logfunc)
133 /* Can't be backwards compatible to this yet, so don't even try */
134 fprintf(stderr, "libevdev: ABI change. Log function will not be honored.\n");
138 libevdev_set_log_function(libevdev_log_func_t logfunc, void *data)
140 log_data.handler = logfunc ? logfunc : libevdev_noop_log_func;
141 log_data.userdata = data;
145 libevdev_set_log_priority(enum libevdev_log_priority priority)
147 if (priority > LIBEVDEV_LOG_DEBUG)
148 priority = LIBEVDEV_LOG_DEBUG;
149 log_data.priority = priority;
152 LIBEVDEV_EXPORT enum libevdev_log_priority
153 libevdev_get_log_priority(void)
155 return log_data.priority;
159 libevdev_change_fd(struct libevdev *dev, int fd)
162 log_bug("device not initialized. call libevdev_set_fd() first\n");
170 libevdev_set_fd(struct libevdev* dev, int fd)
177 log_bug("device already initialized.\n");
181 rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
185 memset(buf, 0, sizeof(buf));
186 rc = ioctl(fd, EVIOCGNAME(sizeof(buf) - 1), buf);
191 dev->name = strdup(buf);
199 memset(buf, 0, sizeof(buf));
200 rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
202 /* uinput has no phys */
206 dev->phys = strdup(buf);
215 memset(buf, 0, sizeof(buf));
216 rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
221 dev->uniq = strdup(buf);
228 rc = ioctl(fd, EVIOCGID, &dev->ids);
232 rc = ioctl(fd, EVIOCGVERSION, &dev->driver_version);
236 rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
240 rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
244 rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
248 rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
252 rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
256 rc = ioctl(fd, EVIOCGBIT(EV_SW, sizeof(dev->sw_bits)), dev->sw_bits);
260 rc = ioctl(fd, EVIOCGBIT(EV_MSC, sizeof(dev->msc_bits)), dev->msc_bits);
264 rc = ioctl(fd, EVIOCGBIT(EV_FF, sizeof(dev->ff_bits)), dev->ff_bits);
268 rc = ioctl(fd, EVIOCGBIT(EV_SND, sizeof(dev->snd_bits)), dev->snd_bits);
272 rc = ioctl(fd, EVIOCGKEY(sizeof(dev->key_values)), dev->key_values);
276 rc = ioctl(fd, EVIOCGLED(sizeof(dev->led_values)), dev->led_values);
280 rc = ioctl(fd, EVIOCGSW(sizeof(dev->sw_values)), dev->sw_values);
284 /* rep is a special case, always set it to 1 for both values if EV_REP is set */
285 if (bit_is_set(dev->bits, EV_REP)) {
286 for (i = 0; i < REP_CNT; i++)
287 set_bit(dev->rep_bits, i);
288 rc = ioctl(fd, EVIOCGREP, dev->rep_values);
293 for (i = ABS_X; i <= ABS_MAX; i++) {
294 if (bit_is_set(dev->abs_bits, i)) {
295 struct input_absinfo abs_info;
296 rc = ioctl(fd, EVIOCGABS(i), &abs_info);
300 dev->abs_info[i] = abs_info;
301 if (i == ABS_MT_SLOT) {
302 dev->num_slots = abs_info.maximum + 1;
303 dev->current_slot = abs_info.value;
310 sync_mt_state(dev, 0);
312 rc = init_event_queue(dev);
318 /* not copying key state because we won't know when we'll start to
319 * use this fd and key's are likely to change state by then.
320 * Same with the valuators, really, but they may not change.
324 return rc ? -errno : 0;
328 libevdev_get_fd(const struct libevdev* dev)
334 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
336 ev->time = dev->last_event_time;
343 sync_key_state(struct libevdev *dev)
347 unsigned long keystate[NLONGS(KEY_CNT)] = {0};
349 rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
353 for (i = 0; i < KEY_CNT; i++) {
355 old = bit_is_set(dev->key_values, i);
356 new = bit_is_set(keystate, i);
358 struct input_event *ev = queue_push(dev);
359 init_event(dev, ev, EV_KEY, i, new ? 1 : 0);
363 memcpy(dev->key_values, keystate, rc);
367 return rc ? -errno : 0;
371 sync_sw_state(struct libevdev *dev)
375 unsigned long swstate[NLONGS(SW_CNT)] = {0};
377 rc = ioctl(dev->fd, EVIOCGSW(sizeof(swstate)), swstate);
381 for (i = 0; i < SW_CNT; i++) {
383 old = bit_is_set(dev->sw_values, i);
384 new = bit_is_set(swstate, i);
386 struct input_event *ev = queue_push(dev);
387 init_event(dev, ev, EV_SW, i, new ? 1 : 0);
391 memcpy(dev->sw_values, swstate, rc);
395 return rc ? -errno : 0;
399 sync_led_state(struct libevdev *dev)
403 unsigned long ledstate[NLONGS(LED_CNT)] = {0};
405 rc = ioctl(dev->fd, EVIOCGLED(sizeof(ledstate)), ledstate);
409 for (i = 0; i < LED_CNT; i++) {
411 old = bit_is_set(dev->led_values, i);
412 new = bit_is_set(ledstate, i);
414 struct input_event *ev = queue_push(dev);
415 init_event(dev, ev, EV_LED, i, new ? 1 : 0);
419 memcpy(dev->led_values, ledstate, rc);
423 return rc ? -errno : 0;
426 sync_abs_state(struct libevdev *dev)
431 for (i = ABS_X; i < ABS_CNT; i++) {
432 struct input_absinfo abs_info;
434 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
437 if (!bit_is_set(dev->abs_bits, i))
440 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
444 if (dev->abs_info[i].value != abs_info.value) {
445 struct input_event *ev = queue_push(dev);
447 init_event(dev, ev, EV_ABS, i, abs_info.value);
448 dev->abs_info[i].value = abs_info.value;
454 return rc ? -errno : 0;
458 sync_mt_state(struct libevdev *dev, int create_events)
465 } mt_state[ABS_MT_CNT];
467 for (i = ABS_MT_MIN; i <= ABS_MT_MAX; i++) {
469 if (i == ABS_MT_SLOT)
472 if (!libevdev_has_event_code(dev, EV_ABS, i))
475 idx = i - ABS_MT_MIN;
476 mt_state[idx].code = i;
477 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
482 for (i = 0; i < dev->num_slots; i++) {
484 struct input_event *ev;
487 ev = queue_push(dev);
488 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, i);
491 for (j = ABS_MT_MIN; j <= ABS_MT_MAX; j++) {
492 int jdx = j - ABS_MT_MIN;
494 if (j == ABS_MT_SLOT)
497 if (!libevdev_has_event_code(dev, EV_ABS, j))
500 if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
504 ev = queue_push(dev);
505 init_event(dev, ev, EV_ABS, j, mt_state[jdx].val[i]);
507 dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
513 return rc ? -errno : 0;
517 sync_state(struct libevdev *dev)
521 struct input_event *ev;
523 /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
524 queue[0]) we need to shift this backwards. Except that chances are that the
525 queue may be either full or too full to prepend all the events needed for
528 so we search for the last sync event in the queue and drop everything before
529 including that event and rely on the kernel to tell us the right value for that
530 bitfield during the sync process.
533 for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
534 struct input_event e = {{0,0}, 0, 0, 0};
535 queue_peek(dev, i, &e);
536 if (e.type == EV_SYN)
541 queue_shift_multiple(dev, i + 1, NULL);
543 if (libevdev_has_event_type(dev, EV_KEY))
544 rc = sync_key_state(dev);
545 if (libevdev_has_event_type(dev, EV_LED))
546 rc = sync_led_state(dev);
547 if (libevdev_has_event_type(dev, EV_SW))
548 rc = sync_sw_state(dev);
549 if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
550 rc = sync_abs_state(dev);
551 if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
552 rc = sync_mt_state(dev, 1);
554 dev->queue_nsync = queue_num_elements(dev);
556 if (dev->queue_nsync > 0) {
557 ev = queue_push(dev);
558 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
566 update_key_state(struct libevdev *dev, const struct input_event *e)
568 if (!libevdev_has_event_type(dev, EV_KEY))
571 if (e->code > KEY_MAX)
574 set_bit_state(dev->key_values, e->code, e->value != 0);
580 update_mt_state(struct libevdev *dev, const struct input_event *e)
582 if (e->code == ABS_MT_SLOT) {
584 dev->current_slot = e->value;
585 /* sync abs_info with the current slot values */
586 for (i = ABS_MT_SLOT + 1; i <= ABS_MT_MAX; i++) {
587 if (libevdev_has_event_code(dev, EV_ABS, i))
588 dev->abs_info[i].value = dev->mt_slot_vals[dev->current_slot][i - ABS_MT_MIN];
592 } else if (dev->current_slot == -1)
595 dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
601 update_abs_state(struct libevdev *dev, const struct input_event *e)
603 if (!libevdev_has_event_type(dev, EV_ABS))
606 if (e->code > ABS_MAX)
609 if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
610 update_mt_state(dev, e);
612 dev->abs_info[e->code].value = e->value;
618 update_led_state(struct libevdev *dev, const struct input_event *e)
620 if (!libevdev_has_event_type(dev, EV_LED))
623 if (e->code > LED_MAX)
626 set_bit_state(dev->led_values, e->code, e->value != 0);
632 update_sw_state(struct libevdev *dev, const struct input_event *e)
634 if (!libevdev_has_event_type(dev, EV_SW))
637 if (e->code > SW_MAX)
640 set_bit_state(dev->sw_values, e->code, e->value != 0);
646 update_state(struct libevdev *dev, const struct input_event *e)
655 rc = update_key_state(dev, e);
658 rc = update_abs_state(dev, e);
661 rc = update_led_state(dev, e);
664 rc = update_sw_state(dev, e);
668 dev->last_event_time = e->time;
674 read_more_events(struct libevdev *dev)
678 struct input_event *next;
680 free_elem = queue_num_free_elements(dev);
684 next = queue_next_element(dev);
685 len = read(dev->fd, next, free_elem * sizeof(struct input_event));
688 } else if (len > 0 && len % sizeof(struct input_event) != 0)
691 int nev = len/sizeof(struct input_event);
692 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
699 libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
704 log_bug("device not initialized. call libevdev_set_fd() first\n");
708 if (!(flags & (LIBEVDEV_READ_NORMAL|LIBEVDEV_READ_SYNC|LIBEVDEV_FORCE_SYNC))) {
709 log_bug("invalid flags %#x\n.\n", flags);
713 if (flags & LIBEVDEV_READ_SYNC) {
714 if (dev->sync_state == SYNC_NEEDED) {
715 rc = sync_state(dev);
718 dev->sync_state = SYNC_IN_PROGRESS;
721 if (dev->queue_nsync == 0) {
722 dev->sync_state = SYNC_NONE;
726 } else if (dev->sync_state != SYNC_NONE) {
727 struct input_event e;
729 /* call update_state for all events here, otherwise the library has the wrong view
731 while (queue_shift(dev, &e) == 0) {
733 update_state(dev, &e);
736 dev->sync_state = SYNC_NONE;
739 /* FIXME: if the first event after SYNC_IN_PROGRESS is a SYN_DROPPED, log this */
741 /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
742 worst case we don't read fast enough and end up with SYN_DROPPED anyway.
744 Except if the fd is in blocking mode and we still have events from the last read, don't
748 if (!(flags & LIBEVDEV_READ_BLOCKING) ||
749 queue_num_elements(dev) == 0) {
750 rc = read_more_events(dev);
751 if (rc < 0 && rc != -EAGAIN)
755 if (flags & LIBEVDEV_FORCE_SYNC) {
756 dev->sync_state = SYNC_NEEDED;
762 if (queue_shift(dev, ev) != 0)
765 update_state(dev, ev);
767 /* if we disabled a code, get the next event instead */
768 } while(!libevdev_has_event_code(dev, ev->type, ev->code));
771 if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
772 dev->sync_state = SYNC_NEEDED;
776 if (flags & LIBEVDEV_READ_SYNC && dev->queue_nsync > 0) {
779 if (dev->queue_nsync == 0)
780 dev->sync_state = SYNC_NONE;
788 libevdev_has_event_pending(struct libevdev *dev)
790 struct pollfd fds = { dev->fd, POLLIN, 0 };
794 log_bug("device not initialized. call libevdev_set_fd() first\n");
798 if (queue_num_elements(dev) != 0)
801 rc = poll(&fds, 1, 0);
802 return (rc >= 0) ? rc : -errno;
805 LIBEVDEV_EXPORT const char *
806 libevdev_get_name(const struct libevdev *dev)
808 return dev->name ? dev->name : "";
811 LIBEVDEV_EXPORT const char *
812 libevdev_get_phys(const struct libevdev *dev)
817 LIBEVDEV_EXPORT const char *
818 libevdev_get_uniq(const struct libevdev *dev)
823 #define STRING_SETTER(field) \
824 LIBEVDEV_EXPORT void libevdev_set_##field(struct libevdev *dev, const char *field) \
829 dev->field = strdup(field); \
837 #define PRODUCT_GETTER(name) \
838 LIBEVDEV_EXPORT int libevdev_get_id_##name(const struct libevdev *dev) \
840 return dev->ids.name; \
843 PRODUCT_GETTER(product);
844 PRODUCT_GETTER(vendor);
845 PRODUCT_GETTER(bustype);
846 PRODUCT_GETTER(version);
848 #define PRODUCT_SETTER(field) \
849 LIBEVDEV_EXPORT void libevdev_set_id_##field(struct libevdev *dev, int field) \
851 dev->ids.field = field;\
854 PRODUCT_SETTER(product);
855 PRODUCT_SETTER(vendor);
856 PRODUCT_SETTER(bustype);
857 PRODUCT_SETTER(version);
860 libevdev_get_driver_version(const struct libevdev *dev)
862 return dev->driver_version;
866 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
868 return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
872 libevdev_enable_property(struct libevdev *dev, unsigned int prop)
874 if (prop > INPUT_PROP_MAX)
877 set_bit(dev->props, prop);
882 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
884 return (type <= EV_MAX) && bit_is_set(dev->bits, type);
888 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
890 const unsigned long *mask;
893 if (!libevdev_has_event_type(dev, type))
899 max = type_to_mask_const(dev, type, &mask);
901 if (max == -1 || code > (unsigned int)max)
904 return bit_is_set(mask, code);
908 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
912 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
916 case EV_ABS: value = dev->abs_info[code].value; break;
917 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
918 case EV_LED: value = bit_is_set(dev->led_values, code); break;
919 case EV_SW: value = bit_is_set(dev->sw_values, code); break;
929 libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value)
932 struct input_event e;
934 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
942 case EV_ABS: rc = update_abs_state(dev, &e); break;
943 case EV_KEY: rc = update_key_state(dev, &e); break;
944 case EV_LED: rc = update_led_state(dev, &e); break;
945 case EV_SW: rc = update_sw_state(dev, &e); break;
955 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
957 if (libevdev_has_event_type(dev, type) &&
958 libevdev_has_event_code(dev, type, code)) {
959 *value = libevdev_get_event_value(dev, type, code);
966 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
968 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
971 if (dev->num_slots < 0 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
974 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
977 return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
981 libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value)
983 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
986 if (dev->num_slots == -1 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
989 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
992 if (code == ABS_MT_SLOT) {
993 if (value < 0 || value >= libevdev_get_num_slots(dev))
995 dev->current_slot = value;
998 dev->mt_slot_vals[slot][code - ABS_MT_MIN] = value;
1005 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
1007 if (libevdev_has_event_type(dev, EV_ABS) &&
1008 libevdev_has_event_code(dev, EV_ABS, code) &&
1009 dev->num_slots >= 0 &&
1010 slot < (unsigned int)dev->num_slots && slot < MAX_SLOTS) {
1011 *value = libevdev_get_slot_value(dev, slot, code);
1018 libevdev_get_num_slots(const struct libevdev *dev)
1020 return dev->num_slots;
1024 libevdev_get_current_slot(const struct libevdev *dev)
1026 return dev->current_slot;
1029 LIBEVDEV_EXPORT const struct input_absinfo*
1030 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
1032 if (!libevdev_has_event_type(dev, EV_ABS) ||
1033 !libevdev_has_event_code(dev, EV_ABS, code))
1036 return &dev->abs_info[code];
1039 #define ABS_GETTER(name) \
1040 LIBEVDEV_EXPORT int libevdev_get_abs_##name(const struct libevdev *dev, unsigned int code) \
1042 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code); \
1043 return absinfo ? absinfo->name : 0; \
1046 ABS_GETTER(maximum);
1047 ABS_GETTER(minimum);
1050 ABS_GETTER(resolution);
1052 #define ABS_SETTER(field) \
1053 LIBEVDEV_EXPORT void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
1055 if (!libevdev_has_event_code(dev, EV_ABS, code)) \
1057 dev->abs_info[code].field = val; \
1064 ABS_SETTER(resolution)
1066 LIBEVDEV_EXPORT void
1067 libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1069 if (!libevdev_has_event_code(dev, EV_ABS, code))
1072 dev->abs_info[code] = *abs;
1076 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
1081 if (libevdev_has_event_type(dev, type))
1084 set_bit(dev->bits, type);
1086 if (type == EV_REP) {
1087 int delay = 0, period = 0;
1088 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
1089 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
1095 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
1097 if (type > EV_MAX || type == EV_SYN)
1100 clear_bit(dev->bits, type);
1106 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
1107 unsigned int code, const void *data)
1110 unsigned long *mask = NULL;
1112 if (libevdev_enable_event_type(dev, type))
1129 max = type_to_mask(dev, type, &mask);
1134 set_bit(mask, code);
1136 if (type == EV_ABS) {
1137 const struct input_absinfo *abs = data;
1138 dev->abs_info[code] = *abs;
1139 } else if (type == EV_REP) {
1140 const int *value = data;
1141 dev->rep_values[code] = *value;
1148 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
1151 unsigned long *mask = NULL;
1156 max = type_to_mask(dev, type, &mask);
1161 clear_bit(mask, code);
1167 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1169 return libevdev_kernel_set_abs_info(dev, code, abs);
1173 libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1178 log_bug("device not initialized. call libevdev_set_fd() first\n");
1185 rc = ioctl(dev->fd, EVIOCSABS(code), abs);
1189 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
1195 libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
1200 log_bug("device not initialized. call libevdev_set_fd() first\n");
1204 if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB) {
1205 log_bug("invalid grab parameter %#x\n", grab);
1209 if (grab == dev->grabbed)
1212 if (grab == LIBEVDEV_GRAB)
1213 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
1214 else if (grab == LIBEVDEV_UNGRAB)
1215 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
1218 dev->grabbed = grab;
1220 return rc < 0 ? -errno : 0;
1224 libevdev_is_event_type(const struct input_event *ev, unsigned int type)
1226 return type < EV_CNT && ev->type == type;
1230 libevdev_is_event_code(const struct input_event *ev, unsigned int type, unsigned int code)
1234 if (!libevdev_is_event_type(ev, type))
1237 max = libevdev_get_event_type_max(type);
1238 return (max > -1 && code <= (unsigned int)max && ev->code == code);
1241 LIBEVDEV_EXPORT const char*
1242 libevdev_get_event_type_name(unsigned int type)
1247 return ev_map[type];
1250 LIBEVDEV_EXPORT const char*
1251 libevdev_get_event_code_name(unsigned int type, unsigned int code)
1253 int max = libevdev_get_event_type_max(type);
1255 if (max == -1 || code > (unsigned int)max)
1258 return event_type_map[type][code];
1261 LIBEVDEV_EXPORT const char*
1262 libevdev_get_property_name(unsigned int prop)
1264 if (prop > INPUT_PROP_MAX)
1267 return input_prop_map[prop];
1271 libevdev_get_event_type_max(unsigned int type)
1276 return ev_max[type];
1280 libevdev_get_repeat(struct libevdev *dev, int *delay, int *period)
1282 if (!libevdev_has_event_type(dev, EV_REP))
1286 *delay = dev->rep_values[REP_DELAY];
1288 *period = dev->rep_values[REP_PERIOD];
1294 libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value)
1296 return libevdev_kernel_set_led_values(dev, code, value, -1);
1300 libevdev_kernel_set_led_values(struct libevdev *dev, ...)
1302 struct input_event ev[LED_MAX + 1];
1303 enum libevdev_led_value val;
1310 log_bug("device not initialized. call libevdev_set_fd() first\n");
1314 memset(ev, 0, sizeof(ev));
1316 va_start(args, dev);
1317 code = va_arg(args, unsigned int);
1318 while (code != -1) {
1319 if (code > LED_MAX) {
1323 val = va_arg(args, enum libevdev_led_value);
1324 if (val != LIBEVDEV_LED_ON && val != LIBEVDEV_LED_OFF) {
1329 if (libevdev_has_event_code(dev, EV_LED, code)) {
1330 struct input_event *e = ev;
1332 while (e->type > 0 && e->code != code)
1339 e->value = (val == LIBEVDEV_LED_ON);
1341 code = va_arg(args, unsigned int);
1345 if (rc == 0 && nleds > 0) {
1346 ev[nleds].type = EV_SYN;
1347 ev[nleds++].code = SYN_REPORT;
1349 rc = write(libevdev_get_fd(dev), ev, nleds * sizeof(ev[0]));
1351 nleds--; /* last is EV_SYN */
1353 update_led_state(dev, &ev[nleds]);
1355 rc = (rc != -1) ? 0 : -errno;