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_log(struct libevdev *dev, const char *format, ...)
56 va_start(args, format);
57 dev->log(format, args);
62 libevdev_noop_log_func(const char *format, va_list args)
71 dev = calloc(1, sizeof(*dev));
76 dev->current_slot = -1;
77 dev->log = libevdev_noop_log_func;
78 dev->grabbed = LIBEVDEV_UNGRAB;
79 dev->sync_state = SYNC_NONE;
85 libevdev_new_from_fd(int fd, struct libevdev **dev)
94 rc = libevdev_set_fd(d, fd);
103 libevdev_free(struct libevdev *dev)
116 libevdev_set_log_handler(struct libevdev *dev, libevdev_log_func_t logfunc)
121 dev->log = logfunc ? logfunc : libevdev_noop_log_func;
125 libevdev_change_fd(struct libevdev *dev, int fd)
134 libevdev_set_fd(struct libevdev* dev, int fd)
143 rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
147 memset(buf, 0, sizeof(buf));
148 rc = ioctl(fd, EVIOCGNAME(sizeof(buf) - 1), buf);
153 dev->name = strdup(buf);
161 memset(buf, 0, sizeof(buf));
162 rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
164 /* uinput has no phys */
168 dev->phys = strdup(buf);
177 memset(buf, 0, sizeof(buf));
178 rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
183 dev->uniq = strdup(buf);
190 rc = ioctl(fd, EVIOCGID, &dev->ids);
194 rc = ioctl(fd, EVIOCGVERSION, &dev->driver_version);
198 rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
202 rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
206 rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
210 rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
214 rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
218 rc = ioctl(fd, EVIOCGBIT(EV_SW, sizeof(dev->sw_bits)), dev->sw_bits);
222 rc = ioctl(fd, EVIOCGBIT(EV_MSC, sizeof(dev->msc_bits)), dev->msc_bits);
226 rc = ioctl(fd, EVIOCGBIT(EV_FF, sizeof(dev->ff_bits)), dev->ff_bits);
230 rc = ioctl(fd, EVIOCGBIT(EV_SND, sizeof(dev->snd_bits)), dev->snd_bits);
234 rc = ioctl(fd, EVIOCGKEY(sizeof(dev->key_values)), dev->key_values);
238 rc = ioctl(fd, EVIOCGLED(sizeof(dev->led_values)), dev->led_values);
242 rc = ioctl(fd, EVIOCGSW(sizeof(dev->sw_values)), dev->sw_values);
246 /* rep is a special case, always set it to 1 for both values if EV_REP is set */
247 if (bit_is_set(dev->bits, EV_REP)) {
248 for (i = 0; i < REP_CNT; i++)
249 set_bit(dev->rep_bits, i);
250 rc = ioctl(fd, EVIOCGREP, dev->rep_values);
255 for (i = ABS_X; i <= ABS_MAX; i++) {
256 if (bit_is_set(dev->abs_bits, i)) {
257 struct input_absinfo abs_info;
258 rc = ioctl(fd, EVIOCGABS(i), &abs_info);
262 dev->abs_info[i] = abs_info;
263 if (i == ABS_MT_SLOT) {
264 dev->num_slots = abs_info.maximum + 1;
265 dev->current_slot = abs_info.value;
272 sync_mt_state(dev, 0);
274 rc = init_event_queue(dev);
280 /* not copying key state because we won't know when we'll start to
281 * use this fd and key's are likely to change state by then.
282 * Same with the valuators, really, but they may not change.
286 return rc ? -errno : 0;
290 libevdev_get_fd(const struct libevdev* dev)
296 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
298 ev->time = dev->last_event_time;
305 sync_key_state(struct libevdev *dev)
309 unsigned long keystate[NLONGS(KEY_CNT)];
311 rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
315 for (i = 0; i < KEY_MAX; i++) {
317 old = bit_is_set(dev->key_values, i);
318 new = bit_is_set(keystate, i);
320 struct input_event *ev = queue_push(dev);
321 init_event(dev, ev, EV_KEY, i, new ? 1 : 0);
323 set_bit_state(dev->key_values, i, new);
328 return rc ? -errno : 0;
332 sync_sw_state(struct libevdev *dev)
336 unsigned long swstate[NLONGS(SW_CNT)];
338 rc = ioctl(dev->fd, EVIOCGSW(sizeof(swstate)), swstate);
342 for (i = 0; i < SW_CNT; i++) {
344 old = bit_is_set(dev->sw_values, i);
345 new = bit_is_set(swstate, i);
347 struct input_event *ev = queue_push(dev);
348 init_event(dev, ev, EV_SW, i, new ? 1 : 0);
350 set_bit_state(dev->sw_values, i, new);
355 return rc ? -errno : 0;
359 sync_led_state(struct libevdev *dev)
363 unsigned long ledstate[NLONGS(LED_CNT)];
365 rc = ioctl(dev->fd, EVIOCGLED(sizeof(ledstate)), ledstate);
369 for (i = 0; i < LED_MAX; i++) {
371 old = bit_is_set(dev->led_values, i);
372 new = bit_is_set(ledstate, i);
374 struct input_event *ev = queue_push(dev);
375 init_event(dev, ev, EV_LED, i, new ? 1 : 0);
377 set_bit_state(dev->led_values, i, new);
382 return rc ? -errno : 0;
385 sync_abs_state(struct libevdev *dev)
390 for (i = ABS_X; i <= ABS_MAX; i++) {
391 struct input_absinfo abs_info;
393 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
396 if (!bit_is_set(dev->abs_bits, i))
399 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
403 if (dev->abs_info[i].value != abs_info.value) {
404 struct input_event *ev = queue_push(dev);
406 init_event(dev, ev, EV_ABS, i, abs_info.value);
407 dev->abs_info[i].value = abs_info.value;
413 return rc ? -errno : 0;
417 sync_mt_state(struct libevdev *dev, int create_events)
424 } mt_state[ABS_MT_CNT];
426 for (i = ABS_MT_MIN; i < ABS_MT_MAX; i++) {
428 if (i == ABS_MT_SLOT)
431 if (!libevdev_has_event_code(dev, EV_ABS, i))
434 idx = i - ABS_MT_MIN;
435 mt_state[idx].code = i;
436 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
441 for (i = 0; i < dev->num_slots; i++) {
443 struct input_event *ev;
446 ev = queue_push(dev);
447 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, i);
450 for (j = ABS_MT_MIN; j < ABS_MT_MAX; j++) {
451 int jdx = j - ABS_MT_MIN;
453 if (j == ABS_MT_SLOT)
456 if (!libevdev_has_event_code(dev, EV_ABS, j))
459 if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
463 ev = queue_push(dev);
464 init_event(dev, ev, EV_ABS, j, mt_state[jdx].val[i]);
466 dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
472 return rc ? -errno : 0;
476 sync_state(struct libevdev *dev)
480 struct input_event *ev;
482 /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
483 queue[0]) we need to shift this backwards. Except that chances are that the
484 queue may be either full or too full to prepend all the events needed for
487 so we search for the last sync event in the queue and drop everything before
488 including that event and rely on the kernel to tell us the right value for that
489 bitfield during the sync process.
492 for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
493 struct input_event e;
494 queue_peek(dev, i, &e);
495 if (e.type == EV_SYN)
500 queue_shift_multiple(dev, i + 1, NULL);
502 if (libevdev_has_event_type(dev, EV_KEY))
503 rc = sync_key_state(dev);
504 if (libevdev_has_event_type(dev, EV_LED))
505 rc = sync_led_state(dev);
506 if (libevdev_has_event_type(dev, EV_SW))
507 rc = sync_sw_state(dev);
508 if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
509 rc = sync_abs_state(dev);
510 if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
511 rc = sync_mt_state(dev, 1);
513 dev->queue_nsync = queue_num_elements(dev);
515 if (dev->queue_nsync > 0) {
516 ev = queue_push(dev);
517 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
525 update_key_state(struct libevdev *dev, const struct input_event *e)
527 if (!libevdev_has_event_type(dev, EV_KEY))
530 if (e->code > KEY_MAX)
533 set_bit_state(dev->key_values, e->code, e->value != 0);
539 update_mt_state(struct libevdev *dev, const struct input_event *e)
541 if (e->code == ABS_MT_SLOT) {
543 dev->current_slot = e->value;
544 /* sync abs_info with the current slot values */
545 for (i = ABS_MT_SLOT + 1; i <= ABS_MT_MAX; i++) {
546 if (libevdev_has_event_code(dev, EV_ABS, i))
547 dev->abs_info[i].value = dev->mt_slot_vals[dev->current_slot][i - ABS_MT_MIN];
551 } else if (dev->current_slot == -1)
554 dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
560 update_abs_state(struct libevdev *dev, const struct input_event *e)
562 if (!libevdev_has_event_type(dev, EV_ABS))
565 if (e->code > ABS_MAX)
568 if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
569 update_mt_state(dev, e);
571 dev->abs_info[e->code].value = e->value;
577 update_led_state(struct libevdev *dev, const struct input_event *e)
579 if (!libevdev_has_event_type(dev, EV_LED))
582 if (e->code > LED_MAX)
585 set_bit_state(dev->led_values, e->code, e->value != 0);
591 update_sw_state(struct libevdev *dev, const struct input_event *e)
593 if (!libevdev_has_event_type(dev, EV_SW))
596 if (e->code > SW_MAX)
599 set_bit_state(dev->sw_values, e->code, e->value != 0);
605 update_state(struct libevdev *dev, const struct input_event *e)
614 rc = update_key_state(dev, e);
617 rc = update_abs_state(dev, e);
620 rc = update_led_state(dev, e);
623 rc = update_sw_state(dev, e);
627 dev->last_event_time = e->time;
633 read_more_events(struct libevdev *dev)
637 struct input_event *next;
639 free_elem = queue_num_free_elements(dev);
643 next = queue_next_element(dev);
644 len = read(dev->fd, next, free_elem * sizeof(struct input_event));
647 } else if (len > 0 && len % sizeof(struct input_event) != 0)
650 int nev = len/sizeof(struct input_event);
651 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
657 int libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
664 if (!(flags & (LIBEVDEV_READ_NORMAL|LIBEVDEV_READ_SYNC|LIBEVDEV_FORCE_SYNC)))
667 if (flags & LIBEVDEV_READ_SYNC) {
668 if (dev->sync_state == SYNC_NEEDED) {
669 rc = sync_state(dev);
672 dev->sync_state = SYNC_IN_PROGRESS;
675 if (dev->queue_nsync == 0) {
676 dev->sync_state = SYNC_NONE;
680 } else if (dev->sync_state != SYNC_NONE) {
681 struct input_event e;
683 /* call update_state for all events here, otherwise the library has the wrong view
685 while (queue_shift(dev, &e) == 0) {
687 update_state(dev, &e);
690 dev->sync_state = SYNC_NONE;
693 /* FIXME: if the first event after SYNC_IN_PROGRESS is a SYN_DROPPED, log this */
695 /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
696 worst case we don't read fast enough and end up with SYN_DROPPED anyway.
698 Except if the fd is in blocking mode and we still have events from the last read, don't
702 if (!(flags & LIBEVDEV_READ_BLOCKING) ||
703 queue_num_elements(dev) == 0) {
704 rc = read_more_events(dev);
705 if (rc < 0 && rc != -EAGAIN)
709 if (flags & LIBEVDEV_FORCE_SYNC) {
710 dev->sync_state = SYNC_NEEDED;
716 if (queue_shift(dev, ev) != 0)
719 update_state(dev, ev);
721 /* if we disabled a code, get the next event instead */
722 } while(!libevdev_has_event_code(dev, ev->type, ev->code));
725 if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
726 dev->sync_state = SYNC_NEEDED;
730 if (flags & LIBEVDEV_READ_SYNC && dev->queue_nsync > 0) {
733 if (dev->queue_nsync == 0)
734 dev->sync_state = SYNC_NONE;
741 int libevdev_has_event_pending(struct libevdev *dev)
743 struct pollfd fds = { dev->fd, POLLIN, 0 };
749 if (queue_num_elements(dev) != 0)
752 rc = poll(&fds, 1, 0);
753 return (rc >= 0) ? rc : -errno;
757 libevdev_get_name(const struct libevdev *dev)
759 return dev->name ? dev->name : "";
763 libevdev_get_phys(const struct libevdev *dev)
769 libevdev_get_uniq(const struct libevdev *dev)
774 #define STRING_SETTER(field) \
775 void libevdev_set_##field(struct libevdev *dev, const char *field) \
780 dev->field = strdup(field); \
788 #define PRODUCT_GETTER(name) \
789 int libevdev_get_id_##name(const struct libevdev *dev) \
791 return dev->ids.name; \
794 PRODUCT_GETTER(product);
795 PRODUCT_GETTER(vendor);
796 PRODUCT_GETTER(bustype);
797 PRODUCT_GETTER(version);
799 #define PRODUCT_SETTER(field) \
800 void libevdev_set_id_##field(struct libevdev *dev, int field) \
802 dev->ids.field = field;\
805 PRODUCT_SETTER(product);
806 PRODUCT_SETTER(vendor);
807 PRODUCT_SETTER(bustype);
808 PRODUCT_SETTER(version);
810 int libevdev_get_driver_version(const struct libevdev *dev)
812 return dev->driver_version;
816 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
818 return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
822 libevdev_enable_property(struct libevdev *dev, unsigned int prop)
824 if (prop > INPUT_PROP_MAX)
827 set_bit(dev->props, prop);
832 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
834 return (type <= EV_MAX) && bit_is_set(dev->bits, type);
838 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
840 const unsigned long *mask;
843 if (!libevdev_has_event_type(dev, type))
849 max = type_to_mask_const(dev, type, &mask);
851 if (max == -1 || code > (unsigned int)max)
854 return bit_is_set(mask, code);
858 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
862 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
866 case EV_ABS: value = dev->abs_info[code].value; break;
867 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
868 case EV_LED: value = bit_is_set(dev->led_values, code); break;
869 case EV_SW: value = bit_is_set(dev->sw_values, code); break;
878 int libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value)
881 struct input_event e;
883 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
891 case EV_ABS: rc = update_abs_state(dev, &e); break;
892 case EV_KEY: rc = update_key_state(dev, &e); break;
893 case EV_LED: rc = update_led_state(dev, &e); break;
894 case EV_SW: rc = update_sw_state(dev, &e); break;
904 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
906 if (libevdev_has_event_type(dev, type) &&
907 libevdev_has_event_code(dev, type, code)) {
908 *value = libevdev_get_event_value(dev, type, code);
915 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
917 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
920 if (dev->num_slots < 0 || slot >= (unsigned int)dev->num_slots || slot >= MAX_SLOTS)
923 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
926 return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
930 libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value)
932 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
935 if (slot >= dev->num_slots || slot >= MAX_SLOTS)
938 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
941 if (code == ABS_MT_SLOT) {
942 if (value < 0 || value >= libevdev_get_num_slots(dev))
944 dev->current_slot = value;
947 dev->mt_slot_vals[slot][code - ABS_MT_MIN] = value;
954 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
956 if (libevdev_has_event_type(dev, EV_ABS) &&
957 libevdev_has_event_code(dev, EV_ABS, code) &&
958 dev->num_slots >= 0 &&
959 slot < (unsigned int)dev->num_slots && slot < MAX_SLOTS) {
960 *value = libevdev_get_slot_value(dev, slot, code);
967 libevdev_get_num_slots(const struct libevdev *dev)
969 return dev->num_slots;
973 libevdev_get_current_slot(const struct libevdev *dev)
975 return dev->current_slot;
978 const struct input_absinfo*
979 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
981 if (!libevdev_has_event_type(dev, EV_ABS) ||
982 !libevdev_has_event_code(dev, EV_ABS, code))
985 return &dev->abs_info[code];
988 #define ABS_GETTER(name) \
989 int libevdev_get_abs_##name(const struct libevdev *dev, unsigned int code) \
991 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code); \
992 return absinfo ? absinfo->name : 0; \
999 ABS_GETTER(resolution);
1001 #define ABS_SETTER(field) \
1002 void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
1004 if (!libevdev_has_event_code(dev, EV_ABS, code)) \
1006 dev->abs_info[code].field = val; \
1013 ABS_SETTER(resolution)
1015 void libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1017 if (!libevdev_has_event_code(dev, EV_ABS, code))
1020 dev->abs_info[code] = *abs;
1024 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
1029 if (libevdev_has_event_type(dev, type))
1032 set_bit(dev->bits, type);
1034 if (type == EV_REP) {
1035 int delay = 0, period = 0;
1036 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
1037 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
1043 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
1045 if (type > EV_MAX || type == EV_SYN)
1048 clear_bit(dev->bits, type);
1054 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
1055 unsigned int code, const void *data)
1058 unsigned long *mask;
1060 if (libevdev_enable_event_type(dev, type))
1077 max = type_to_mask(dev, type, &mask);
1082 set_bit(mask, code);
1084 if (type == EV_ABS) {
1085 const struct input_absinfo *abs = data;
1086 dev->abs_info[code] = *abs;
1087 } else if (type == EV_REP) {
1088 const int *value = data;
1089 dev->rep_values[code] = *value;
1096 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
1099 unsigned long *mask;
1104 max = type_to_mask(dev, type, &mask);
1109 clear_bit(mask, code);
1116 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1118 return libevdev_kernel_set_abs_info(dev, code, abs);
1122 libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1129 rc = ioctl(dev->fd, EVIOCSABS(code), abs);
1133 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
1139 libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
1143 if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB)
1146 if (grab == dev->grabbed)
1149 if (grab == LIBEVDEV_GRAB)
1150 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
1151 else if (grab == LIBEVDEV_UNGRAB)
1152 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
1155 dev->grabbed = grab;
1157 return rc < 0 ? -errno : 0;
1161 libevdev_is_event_type(const struct input_event *ev, unsigned int type)
1163 return type < EV_CNT && ev->type == type;
1167 libevdev_is_event_code(const struct input_event *ev, unsigned int type, unsigned int code)
1171 if (!libevdev_is_event_type(ev, type))
1174 max = libevdev_get_event_type_max(type);
1175 return (max > -1 && code <= (unsigned int)max && ev->code == code);
1179 libevdev_get_event_type_name(unsigned int type)
1184 return ev_map[type];
1188 libevdev_get_event_code_name(unsigned int type, unsigned int code)
1190 int max = libevdev_get_event_type_max(type);
1192 if (max == -1 || code > (unsigned int)max)
1195 return event_type_map[type][code];
1199 libevdev_get_property_name(unsigned int prop)
1201 if (prop > INPUT_PROP_MAX)
1204 return input_prop_map[prop];
1208 libevdev_get_event_type_max(unsigned int type)
1213 return ev_max[type];
1217 libevdev_get_repeat(struct libevdev *dev, int *delay, int *period)
1219 if (!libevdev_has_event_type(dev, EV_REP))
1223 *delay = dev->rep_values[REP_DELAY];
1225 *period = dev->rep_values[REP_PERIOD];
1231 libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value)
1233 return libevdev_kernel_set_led_values(dev, code, value, -1);
1237 libevdev_kernel_set_led_values(struct libevdev *dev, ...)
1239 struct input_event ev[LED_MAX + 1];
1240 enum libevdev_led_value val;
1246 memset(ev, 0, sizeof(ev));
1248 va_start(args, dev);
1249 code = va_arg(args, unsigned int);
1250 while (code != -1) {
1251 if (code > LED_MAX) {
1255 val = va_arg(args, enum libevdev_led_value);
1256 if (val != LIBEVDEV_LED_ON && val != LIBEVDEV_LED_OFF) {
1261 if (libevdev_has_event_code(dev, EV_LED, code)) {
1262 struct input_event *e = ev;
1264 while (e->type > 0 && e->code != code)
1271 e->value = (val == LIBEVDEV_LED_ON);
1273 code = va_arg(args, unsigned int);
1277 if (rc == 0 && nleds > 0) {
1278 ev[nleds].type = EV_SYN;
1279 ev[nleds++].code = SYN_REPORT;
1281 rc = write(libevdev_get_fd(dev), ev, nleds * sizeof(ev[0]));
1283 nleds--; /* last is EV_SYN */
1285 update_led_state(dev, &ev[nleds]);
1287 rc = (rc != -1) ? 0 : -errno;