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
35 #include "libevdev-int.h"
36 #include "libevdev-util.h"
37 #include "event-names.h"
41 enum event_filter_status {
42 EVENT_FILTER_NONE, /**< Event untouched by filters */
43 EVENT_FILTER_MODIFIED, /**< Event was modified */
44 EVENT_FILTER_DISCARD, /**< Discard current event */
47 static int sync_mt_state(struct libevdev *dev, int create_events);
50 slot_value(const struct libevdev *dev, int slot, int axis)
52 if (unlikely(slot > dev->num_slots)) {
53 log_bug(dev, "Slot %d exceeds number of slots (%d)\n", slot, dev->num_slots);
56 if (unlikely(axis < ABS_MT_MIN || axis > ABS_MT_MAX)) {
57 log_bug(dev, "MT axis %d is outside the valid range [%d,%d]\n",
58 axis, ABS_MT_MIN, ABS_MT_MAX);
61 return &dev->mt_slot_vals[slot * ABS_MT_CNT + axis - ABS_MT_MIN];
65 init_event_queue(struct libevdev *dev)
67 const int MIN_QUEUE_SIZE = 256;
68 int nevents = 1; /* terminating SYN_REPORT */
70 unsigned int type, code;
72 /* count the number of axes, keys, etc. to get a better idea at how
73 many events per EV_SYN we could possibly get. That's the max we
74 may get during SYN_DROPPED too. Use double that, just so we have
75 room for events while syncing a device.
77 for (type = EV_KEY; type < EV_MAX; type++) {
78 int max = libevdev_event_type_get_max(type);
79 for (code = 0; max > 0 && code < (unsigned int) max; code++) {
80 if (libevdev_has_event_code(dev, type, code))
85 nslots = libevdev_get_num_slots(dev);
89 for (code = ABS_MT_SLOT; code <= ABS_MAX; code++) {
90 if (libevdev_has_event_code(dev, EV_ABS, code))
94 /* We already counted the first slot in the initial count */
95 nevents += num_mt_axes * (nslots - 1);
98 return queue_alloc(dev, max(MIN_QUEUE_SIZE, nevents * 2));
102 libevdev_dflt_log_func(enum libevdev_log_priority priority,
104 const char *file, int line, const char *func,
105 const char *format, va_list args)
109 case LIBEVDEV_LOG_ERROR: prefix = "libevdev error"; break;
110 case LIBEVDEV_LOG_INFO: prefix = "libevdev info"; break;
111 case LIBEVDEV_LOG_DEBUG:
112 prefix = "libevdev debug";
115 prefix = "libevdev INVALID LOG PRIORITY";
118 /* default logging format:
119 libevev error in libevdev_some_func: blah blah
120 libevev info in libevdev_some_func: blah blah
121 libevev debug in file.c:123:libevdev_some_func: blah blah
124 fprintf(stderr, "%s in ", prefix);
125 if (priority == LIBEVDEV_LOG_DEBUG)
126 fprintf(stderr, "%s:%d:", file, line);
127 fprintf(stderr, "%s: ", func);
128 vfprintf(stderr, format, args);
132 fix_invalid_absinfo(const struct libevdev *dev,
134 struct input_absinfo* abs_info)
137 * The reported absinfo for ABS_MT_TRACKING_ID is sometimes
138 * uninitialized for certain mtk-soc, due to init code mangling
139 * in the vendor kernel.
141 if (axis == ABS_MT_TRACKING_ID &&
142 abs_info->maximum == abs_info->minimum) {
143 abs_info->minimum = -1;
144 abs_info->maximum = 0xFFFF;
146 "Device \"%s\" has invalid ABS_MT_TRACKING_ID range",
152 * Global logging settings.
154 static struct logdata log_data = {
155 .priority = LIBEVDEV_LOG_INFO,
156 .global_handler = libevdev_dflt_log_func,
161 _libevdev_log_msg(const struct libevdev *dev,
162 enum libevdev_log_priority priority,
163 const char *file, int line, const char *func,
164 const char *format, ...)
168 if (dev && dev->log.device_handler) {
170 * if both global handler and device handler are set
171 * we've set up the handlers wrong. And that means we'll
172 * likely get the printf args wrong and cause all sorts of
173 * mayhem. Seppuku is called for.
175 if (unlikely(dev->log.global_handler))
178 if (priority > dev->log.priority)
180 } else if (!log_data.global_handler || priority > log_data.priority)
182 else if (unlikely(log_data.device_handler))
183 abort(); /* Seppuku, see above */
185 va_start(args, format);
186 if (dev && dev->log.device_handler)
187 dev->log.device_handler(dev, priority, dev->log.userdata, file, line, func, format, args);
189 log_data.global_handler(priority, log_data.userdata, file, line, func, format, args);
194 libevdev_reset(struct libevdev *dev)
196 enum libevdev_log_priority pri = dev->log.priority;
197 libevdev_device_log_func_t handler = dev->log.device_handler;
202 free(dev->mt_slot_vals);
203 memset(dev, 0, sizeof(*dev));
205 dev->initialized = false;
207 dev->current_slot = -1;
208 dev->grabbed = LIBEVDEV_UNGRAB;
209 dev->sync_state = SYNC_NONE;
210 dev->log.priority = pri;
211 dev->log.device_handler = handler;
212 libevdev_enable_event_type(dev, EV_SYN);
215 LIBEVDEV_EXPORT struct libevdev*
218 struct libevdev *dev;
220 dev = calloc(1, sizeof(*dev));
230 libevdev_new_from_fd(int fd, struct libevdev **dev)
239 rc = libevdev_set_fd(d, fd);
248 libevdev_free(struct libevdev *dev)
259 libevdev_set_log_function(libevdev_log_func_t logfunc, void *data)
261 log_data.global_handler = logfunc;
262 log_data.userdata = data;
266 libevdev_set_log_priority(enum libevdev_log_priority priority)
268 if (priority > LIBEVDEV_LOG_DEBUG)
269 priority = LIBEVDEV_LOG_DEBUG;
270 log_data.priority = priority;
273 LIBEVDEV_EXPORT enum libevdev_log_priority
274 libevdev_get_log_priority(void)
276 return log_data.priority;
280 libevdev_set_device_log_function(struct libevdev *dev,
281 libevdev_device_log_func_t logfunc,
282 enum libevdev_log_priority priority,
286 log_bug(NULL, "device must not be NULL\n");
290 dev->log.priority = priority;
291 dev->log.device_handler = logfunc;
292 dev->log.userdata = data;
295 enum libevdev_log_priority
296 _libevdev_log_priority(const struct libevdev *dev)
298 if (dev && dev->log.device_handler)
299 return dev->log.priority;
301 return libevdev_get_log_priority();
305 libevdev_change_fd(struct libevdev *dev, int fd)
307 if (!dev->initialized) {
308 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
312 dev->grabbed = LIBEVDEV_UNGRAB;
317 reset_tracking_ids(struct libevdev *dev)
319 if (dev->num_slots == -1 ||
320 !libevdev_has_event_code(dev, EV_ABS, ABS_MT_TRACKING_ID))
323 for (int slot = 0; slot < dev->num_slots; slot++)
324 libevdev_set_slot_value(dev, slot, ABS_MT_TRACKING_ID, -1);
328 free_slots(struct libevdev *dev)
331 free(dev->mt_slot_vals);
332 dev->mt_slot_vals = NULL;
336 init_slots(struct libevdev *dev)
338 const struct input_absinfo *abs_info;
341 free(dev->mt_slot_vals);
342 dev->mt_slot_vals = NULL;
344 /* devices with ABS_RESERVED aren't MT devices,
345 see the documentation for multitouch-related
346 functions for more details */
347 if (libevdev_has_event_code(dev, EV_ABS, ABS_RESERVED) ||
348 !libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT)) {
349 if (dev->num_slots != -1) {
355 abs_info = libevdev_get_abs_info(dev, ABS_MT_SLOT);
358 dev->num_slots = abs_info->maximum + 1;
359 dev->mt_slot_vals = calloc(dev->num_slots * ABS_MT_CNT, sizeof(int));
360 if (!dev->mt_slot_vals) {
364 dev->current_slot = abs_info->value;
366 reset_tracking_ids(dev);
372 libevdev_set_fd(struct libevdev* dev, int fd)
378 if (dev->initialized) {
379 log_bug(dev, "device already initialized.\n");
386 rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
390 memset(buf, 0, sizeof(buf));
391 rc = ioctl(fd, EVIOCGNAME(sizeof(buf) - 1), buf);
396 dev->name = strdup(buf);
404 memset(buf, 0, sizeof(buf));
405 rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
407 /* uinput has no phys */
411 dev->phys = strdup(buf);
420 memset(buf, 0, sizeof(buf));
421 rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
426 dev->uniq = strdup(buf);
433 rc = ioctl(fd, EVIOCGID, &dev->ids);
437 rc = ioctl(fd, EVIOCGVERSION, &dev->driver_version);
441 /* Built on a kernel with props, running against a kernel without property
442 support. This should not be a fatal case, we'll be missing properties but other
443 than that everything is as expected.
445 rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
446 if (rc < 0 && errno != EINVAL)
449 rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
453 rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
457 rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
461 rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
465 rc = ioctl(fd, EVIOCGBIT(EV_SW, sizeof(dev->sw_bits)), dev->sw_bits);
469 rc = ioctl(fd, EVIOCGBIT(EV_MSC, sizeof(dev->msc_bits)), dev->msc_bits);
473 rc = ioctl(fd, EVIOCGBIT(EV_FF, sizeof(dev->ff_bits)), dev->ff_bits);
477 rc = ioctl(fd, EVIOCGBIT(EV_SND, sizeof(dev->snd_bits)), dev->snd_bits);
481 rc = ioctl(fd, EVIOCGKEY(sizeof(dev->key_values)), dev->key_values);
485 rc = ioctl(fd, EVIOCGLED(sizeof(dev->led_values)), dev->led_values);
489 rc = ioctl(fd, EVIOCGSW(sizeof(dev->sw_values)), dev->sw_values);
493 /* rep is a special case, always set it to 1 for both values if EV_REP is set */
494 if (bit_is_set(dev->bits, EV_REP)) {
495 for (i = 0; i < REP_CNT; i++)
496 set_bit(dev->rep_bits, i);
497 rc = ioctl(fd, EVIOCGREP, dev->rep_values);
502 for (i = ABS_X; i <= ABS_MAX; i++) {
503 if (bit_is_set(dev->abs_bits, i)) {
504 struct input_absinfo abs_info;
505 rc = ioctl(fd, EVIOCGABS(i), &abs_info);
509 fix_invalid_absinfo(dev, i, &abs_info);
511 dev->abs_info[i] = abs_info;
517 rc = init_slots(dev);
521 if (dev->num_slots != -1)
522 sync_mt_state(dev, 0);
524 rc = init_event_queue(dev);
530 /* not copying key state because we won't know when we'll start to
531 * use this fd and key's are likely to change state by then.
532 * Same with the valuators, really, but they may not change.
535 dev->initialized = true;
539 return rc ? -errno : 0;
543 libevdev_get_fd(const struct libevdev* dev)
549 sync_key_state(struct libevdev *dev)
553 unsigned long keystate[NLONGS(KEY_CNT)] = {0};
555 rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
559 for (i = 0; i < KEY_CNT; i++) {
561 old = bit_is_set(dev->key_values, i);
562 new = bit_is_set(keystate, i);
564 queue_push_event(dev, EV_KEY, i, new ? 1 : 0);
567 memcpy(dev->key_values, keystate, rc);
571 return rc ? -errno : 0;
575 sync_sw_state(struct libevdev *dev)
579 unsigned long swstate[NLONGS(SW_CNT)] = {0};
581 rc = ioctl(dev->fd, EVIOCGSW(sizeof(swstate)), swstate);
585 for (i = 0; i < SW_CNT; i++) {
587 old = bit_is_set(dev->sw_values, i);
588 new = bit_is_set(swstate, i);
590 queue_push_event(dev, EV_SW, i, new ? 1 : 0);
593 memcpy(dev->sw_values, swstate, rc);
597 return rc ? -errno : 0;
601 sync_led_state(struct libevdev *dev)
605 unsigned long ledstate[NLONGS(LED_CNT)] = {0};
607 rc = ioctl(dev->fd, EVIOCGLED(sizeof(ledstate)), ledstate);
611 for (i = 0; i < LED_CNT; i++) {
613 old = bit_is_set(dev->led_values, i);
614 new = bit_is_set(ledstate, i);
616 queue_push_event(dev, EV_LED, i, new ? 1 : 0);
620 memcpy(dev->led_values, ledstate, rc);
624 return rc ? -errno : 0;
627 sync_abs_state(struct libevdev *dev)
632 for (i = ABS_X; i < ABS_CNT; i++) {
633 struct input_absinfo abs_info;
635 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
638 if (!bit_is_set(dev->abs_bits, i))
641 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
645 if (dev->abs_info[i].value != abs_info.value) {
646 queue_push_event(dev, EV_ABS, i, abs_info.value);
647 dev->abs_info[i].value = abs_info.value;
653 return rc ? -errno : 0;
657 sync_mt_state(struct libevdev *dev, int create_events)
659 struct input_absinfo abs_info;
661 int last_reported_slot = 0;
662 unsigned long slot_update[dev->num_slots][NLONGS(ABS_CNT)];
663 unsigned long tracking_id_changes[NLONGS(dev->num_slots)];
665 int need_tracking_id_changes = 0;
667 memset(slot_update, 0, sizeof(slot_update));
668 memset(tracking_id_changes, 0, sizeof(tracking_id_changes));
670 for (int axis = ABS_MT_MIN; axis <= ABS_MT_MAX; axis++) {
671 /* EVIOCGMTSLOTS required format */
672 struct mt_sync_state {
674 int32_t val[dev->num_slots];
677 if (axis == ABS_MT_SLOT ||
678 !libevdev_has_event_code(dev, EV_ABS, axis))
681 mt_state.code = axis;
682 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(mt_state)), &mt_state);
686 for (int slot = 0; slot < dev->num_slots; slot++) {
687 if (*slot_value(dev, slot, axis) == mt_state.val[slot])
690 if (axis == ABS_MT_TRACKING_ID &&
691 *slot_value(dev, slot, axis) != -1 &&
692 mt_state.val[slot] != -1) {
693 set_bit(tracking_id_changes, slot);
694 need_tracking_id_changes = 1;
697 *slot_value(dev, slot, axis) = mt_state.val[slot];
699 set_bit(slot_update[slot], axis);
700 /* note that this slot has updates */
701 set_bit(slot_update[slot], ABS_MT_SLOT);
705 if (!create_events) {
710 if (need_tracking_id_changes) {
711 for (int slot = 0; slot < dev->num_slots; slot++) {
712 if (!bit_is_set(tracking_id_changes, slot))
715 queue_push_event(dev, EV_ABS, ABS_MT_SLOT, slot);
716 queue_push_event(dev, EV_ABS, ABS_MT_TRACKING_ID, -1);
718 last_reported_slot = slot;
721 queue_push_event(dev, EV_SYN, SYN_REPORT, 0);
724 for (int slot = 0; slot < dev->num_slots; slot++) {
725 if (!bit_is_set(slot_update[slot], ABS_MT_SLOT))
728 queue_push_event(dev, EV_ABS, ABS_MT_SLOT, slot);
729 last_reported_slot = slot;
731 for (int axis = ABS_MT_MIN; axis <= ABS_MT_MAX; axis++) {
732 if (axis == ABS_MT_SLOT ||
733 !libevdev_has_event_code(dev, EV_ABS, axis))
736 if (bit_is_set(slot_update[slot], axis))
737 queue_push_event(dev, EV_ABS, axis,
738 *slot_value(dev, slot, axis));
742 /* add one last slot event to make sure the client is on the same
743 slot as the kernel */
745 rc = ioctl(dev->fd, EVIOCGABS(ABS_MT_SLOT), &abs_info);
749 dev->current_slot = abs_info.value;
751 if (dev->current_slot != last_reported_slot)
752 queue_push_event(dev, EV_ABS, ABS_MT_SLOT, dev->current_slot);
756 return rc ? -errno : 0;
760 read_more_events(struct libevdev *dev)
764 struct input_event *next;
766 free_elem = queue_num_free_elements(dev);
770 next = queue_next_element(dev);
771 len = read(dev->fd, next, free_elem * sizeof(struct input_event));
774 } else if (len > 0 && len % sizeof(struct input_event) != 0)
777 int nev = len/sizeof(struct input_event);
778 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
785 drain_events(struct libevdev *dev)
790 const int max_iterations = 8; /* EVDEV_BUF_PACKETS in
791 kernel/drivers/input/evedev.c */
793 queue_shift_multiple(dev, queue_num_elements(dev), NULL);
796 rc = read_more_events(dev);
801 log_error(dev, "Failed to drain events before sync.\n");
805 nelem = queue_num_elements(dev);
806 queue_shift_multiple(dev, nelem, NULL);
807 } while (iterations++ < max_iterations && nelem >= queue_size(dev));
809 /* Our buffer should be roughly the same or bigger than the kernel
810 buffer in most cases, so we usually don't expect to recurse. If
811 we do, make sure we stop after max_iterations and proceed with
812 what we have. This could happen if events queue up faster than
815 if (iterations >= max_iterations)
816 log_info(dev, "Unable to drain events, buffer size mismatch.\n");
820 sync_state(struct libevdev *dev)
824 /* see section "Discarding events before synchronizing" in
825 * libevdev/libevdev.h */
828 if (libevdev_has_event_type(dev, EV_KEY))
829 rc = sync_key_state(dev);
830 if (libevdev_has_event_type(dev, EV_LED))
831 rc = sync_led_state(dev);
832 if (libevdev_has_event_type(dev, EV_SW))
833 rc = sync_sw_state(dev);
834 if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
835 rc = sync_abs_state(dev);
836 if (rc == 0 && dev->num_slots > -1 &&
837 libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
838 rc = sync_mt_state(dev, 1);
840 dev->queue_nsync = queue_num_elements(dev);
842 if (dev->queue_nsync > 0) {
843 queue_push_event(dev, EV_SYN, SYN_REPORT, 0);
851 update_key_state(struct libevdev *dev, const struct input_event *e)
853 if (!libevdev_has_event_type(dev, EV_KEY))
856 if (e->code > KEY_MAX)
859 set_bit_state(dev->key_values, e->code, e->value != 0);
865 update_mt_state(struct libevdev *dev, const struct input_event *e)
867 if (e->code == ABS_MT_SLOT && dev->num_slots > -1) {
869 dev->current_slot = e->value;
870 /* sync abs_info with the current slot values */
871 for (i = ABS_MT_SLOT + 1; i <= ABS_MT_MAX; i++) {
872 if (libevdev_has_event_code(dev, EV_ABS, i))
873 dev->abs_info[i].value = *slot_value(dev, dev->current_slot, i);
877 } else if (dev->current_slot == -1)
880 *slot_value(dev, dev->current_slot, e->code) = e->value;
886 update_abs_state(struct libevdev *dev, const struct input_event *e)
888 if (!libevdev_has_event_type(dev, EV_ABS))
891 if (e->code > ABS_MAX)
894 if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
895 update_mt_state(dev, e);
897 dev->abs_info[e->code].value = e->value;
903 update_led_state(struct libevdev *dev, const struct input_event *e)
905 if (!libevdev_has_event_type(dev, EV_LED))
908 if (e->code > LED_MAX)
911 set_bit_state(dev->led_values, e->code, e->value != 0);
917 update_sw_state(struct libevdev *dev, const struct input_event *e)
919 if (!libevdev_has_event_type(dev, EV_SW))
922 if (e->code > SW_MAX)
925 set_bit_state(dev->sw_values, e->code, e->value != 0);
931 update_state(struct libevdev *dev, const struct input_event *e)
940 rc = update_key_state(dev, e);
943 rc = update_abs_state(dev, e);
946 rc = update_led_state(dev, e);
949 rc = update_sw_state(dev, e);
953 dev->last_event_time.tv_sec = e->input_event_sec;
954 dev->last_event_time.tv_usec = e->input_event_usec;
960 * Sanitize/modify events where needed.
962 static inline enum event_filter_status
963 sanitize_event(const struct libevdev *dev,
964 struct input_event *ev,
965 enum SyncState sync_state)
967 if (!libevdev_has_event_code(dev, ev->type, ev->code))
968 return EVENT_FILTER_DISCARD;
970 if (unlikely(dev->num_slots > -1 &&
971 libevdev_event_is_code(ev, EV_ABS, ABS_MT_SLOT) &&
972 (ev->value < 0 || ev->value >= dev->num_slots))) {
973 log_bug(dev, "Device \"%s\" received an invalid slot index %d."
974 "Capping to announced max slot number %d.\n",
975 dev->name, ev->value, dev->num_slots - 1);
976 ev->value = dev->num_slots - 1;
977 return EVENT_FILTER_MODIFIED;
979 /* Drop any invalid tracking IDs, they are only supposed to go from
980 N to -1 or from -1 to N. Never from -1 to -1, or N to M. Very
981 unlikely to ever happen from a real device.
983 } else if (unlikely(sync_state == SYNC_NONE &&
984 dev->num_slots > -1 &&
985 libevdev_event_is_code(ev, EV_ABS, ABS_MT_TRACKING_ID) &&
987 *slot_value(dev, dev->current_slot, ABS_MT_TRACKING_ID) == -1) ||
989 *slot_value(dev, dev->current_slot, ABS_MT_TRACKING_ID) != -1)))) {
990 log_bug(dev, "Device \"%s\" received a double tracking ID %d in slot %d.\n",
991 dev->name, ev->value, dev->current_slot);
992 return EVENT_FILTER_DISCARD;
995 return EVENT_FILTER_NONE;
999 libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
1001 int rc = LIBEVDEV_READ_STATUS_SUCCESS;
1002 enum event_filter_status filter_status;
1003 const unsigned int valid_flags = LIBEVDEV_READ_FLAG_NORMAL |
1004 LIBEVDEV_READ_FLAG_SYNC |
1005 LIBEVDEV_READ_FLAG_FORCE_SYNC |
1006 LIBEVDEV_READ_FLAG_BLOCKING;
1008 if (!dev->initialized) {
1009 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
1011 } else if (dev->fd < 0)
1014 if ((flags & valid_flags) == 0) {
1015 log_bug(dev, "invalid flags %#x.\n", flags);
1019 if (flags & LIBEVDEV_READ_FLAG_SYNC) {
1020 if (dev->sync_state == SYNC_NEEDED) {
1021 rc = sync_state(dev);
1024 dev->sync_state = SYNC_IN_PROGRESS;
1027 if (dev->queue_nsync == 0) {
1028 dev->sync_state = SYNC_NONE;
1032 } else if (dev->sync_state != SYNC_NONE) {
1033 struct input_event e;
1035 /* call update_state for all events here, otherwise the library has the wrong view
1036 of the device too */
1037 while (queue_shift(dev, &e) == 0) {
1039 if (sanitize_event(dev, &e, dev->sync_state) != EVENT_FILTER_DISCARD)
1040 update_state(dev, &e);
1043 dev->sync_state = SYNC_NONE;
1046 /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
1047 worst case we don't read fast enough and end up with SYN_DROPPED anyway.
1049 Except if the fd is in blocking mode and we still have events from the last read, don't
1053 if (queue_num_elements(dev) == 0) {
1054 rc = read_more_events(dev);
1055 if (rc < 0 && rc != -EAGAIN)
1059 if (flags & LIBEVDEV_READ_FLAG_FORCE_SYNC) {
1060 dev->sync_state = SYNC_NEEDED;
1061 rc = LIBEVDEV_READ_STATUS_SYNC;
1065 if (queue_shift(dev, ev) != 0)
1068 filter_status = sanitize_event(dev, ev, dev->sync_state);
1069 if (filter_status != EVENT_FILTER_DISCARD)
1070 update_state(dev, ev);
1072 /* if we disabled a code, get the next event instead */
1073 } while(filter_status == EVENT_FILTER_DISCARD ||
1074 !libevdev_has_event_code(dev, ev->type, ev->code));
1076 rc = LIBEVDEV_READ_STATUS_SUCCESS;
1077 if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
1078 dev->sync_state = SYNC_NEEDED;
1079 rc = LIBEVDEV_READ_STATUS_SYNC;
1082 if (flags & LIBEVDEV_READ_FLAG_SYNC && dev->queue_nsync > 0) {
1084 rc = LIBEVDEV_READ_STATUS_SYNC;
1085 if (dev->queue_nsync == 0)
1086 dev->sync_state = SYNC_NONE;
1094 libevdev_has_event_pending(struct libevdev *dev)
1096 struct pollfd fds = { dev->fd, POLLIN, 0 };
1099 if (!dev->initialized) {
1100 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
1102 } else if (dev->fd < 0)
1105 if (queue_num_elements(dev) != 0)
1108 rc = poll(&fds, 1, 0);
1109 return (rc >= 0) ? rc : -errno;
1112 LIBEVDEV_EXPORT const char *
1113 libevdev_get_name(const struct libevdev *dev)
1115 return dev->name ? dev->name : "";
1118 LIBEVDEV_EXPORT const char *
1119 libevdev_get_phys(const struct libevdev *dev)
1124 LIBEVDEV_EXPORT const char *
1125 libevdev_get_uniq(const struct libevdev *dev)
1130 #define STRING_SETTER(field) \
1131 LIBEVDEV_EXPORT void libevdev_set_##field(struct libevdev *dev, const char *field) \
1133 if (field == NULL) \
1136 dev->field = strdup(field); \
1143 #define PRODUCT_GETTER(name) \
1144 LIBEVDEV_EXPORT int libevdev_get_id_##name(const struct libevdev *dev) \
1146 return dev->ids.name; \
1149 PRODUCT_GETTER(product)
1150 PRODUCT_GETTER(vendor)
1151 PRODUCT_GETTER(bustype)
1152 PRODUCT_GETTER(version)
1154 #define PRODUCT_SETTER(field) \
1155 LIBEVDEV_EXPORT void libevdev_set_id_##field(struct libevdev *dev, int field) \
1157 dev->ids.field = field;\
1160 PRODUCT_SETTER(product)
1161 PRODUCT_SETTER(vendor)
1162 PRODUCT_SETTER(bustype)
1163 PRODUCT_SETTER(version)
1166 libevdev_get_driver_version(const struct libevdev *dev)
1168 return dev->driver_version;
1172 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
1174 return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
1178 libevdev_enable_property(struct libevdev *dev, unsigned int prop)
1180 if (prop > INPUT_PROP_MAX)
1183 set_bit(dev->props, prop);
1188 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
1190 return type == EV_SYN ||(type <= EV_MAX && bit_is_set(dev->bits, type));
1194 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
1196 const unsigned long *mask = NULL;
1199 if (!libevdev_has_event_type(dev, type))
1205 max = type_to_mask_const(dev, type, &mask);
1207 if (max == -1 || code > (unsigned int)max)
1210 return bit_is_set(mask, code);
1214 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
1218 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
1222 case EV_ABS: value = dev->abs_info[code].value; break;
1223 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
1224 case EV_LED: value = bit_is_set(dev->led_values, code); break;
1225 case EV_SW: value = bit_is_set(dev->sw_values, code); break;
1229 libevdev_get_repeat(dev, &value, NULL);
1232 libevdev_get_repeat(dev, NULL, &value);
1248 libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value)
1251 struct input_event e;
1253 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
1260 if (sanitize_event(dev, &e, SYNC_NONE) != EVENT_FILTER_NONE)
1264 case EV_ABS: rc = update_abs_state(dev, &e); break;
1265 case EV_KEY: rc = update_key_state(dev, &e); break;
1266 case EV_LED: rc = update_led_state(dev, &e); break;
1267 case EV_SW: rc = update_sw_state(dev, &e); break;
1277 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
1279 if (libevdev_has_event_type(dev, type) &&
1280 libevdev_has_event_code(dev, type, code)) {
1281 *value = libevdev_get_event_value(dev, type, code);
1288 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
1290 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1293 if (dev->num_slots < 0 || slot >= (unsigned int)dev->num_slots)
1296 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1299 return *slot_value(dev, slot, code);
1303 libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value)
1305 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1308 if (dev->num_slots == -1 || slot >= (unsigned int)dev->num_slots)
1311 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1314 if (code == ABS_MT_SLOT) {
1315 if (value < 0 || value >= libevdev_get_num_slots(dev))
1317 dev->current_slot = value;
1320 *slot_value(dev, slot, code) = value;
1326 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
1328 if (libevdev_has_event_type(dev, EV_ABS) &&
1329 libevdev_has_event_code(dev, EV_ABS, code) &&
1330 dev->num_slots >= 0 &&
1331 slot < (unsigned int)dev->num_slots) {
1332 *value = libevdev_get_slot_value(dev, slot, code);
1339 libevdev_get_num_slots(const struct libevdev *dev)
1341 return dev->num_slots;
1345 libevdev_get_current_slot(const struct libevdev *dev)
1347 return dev->current_slot;
1350 LIBEVDEV_EXPORT const struct input_absinfo*
1351 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
1353 if (!libevdev_has_event_type(dev, EV_ABS) ||
1354 !libevdev_has_event_code(dev, EV_ABS, code))
1357 return &dev->abs_info[code];
1360 #define ABS_GETTER(name) \
1361 LIBEVDEV_EXPORT int libevdev_get_abs_##name(const struct libevdev *dev, unsigned int code) \
1363 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code); \
1364 return absinfo ? absinfo->name : 0; \
1371 ABS_GETTER(resolution)
1373 #define ABS_SETTER(field) \
1374 LIBEVDEV_EXPORT void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
1376 if (!libevdev_has_event_code(dev, EV_ABS, code)) \
1378 dev->abs_info[code].field = val; \
1385 ABS_SETTER(resolution)
1387 LIBEVDEV_EXPORT void
1388 libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1390 if (!libevdev_has_event_code(dev, EV_ABS, code))
1393 dev->abs_info[code] = *abs;
1397 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
1404 if (libevdev_has_event_type(dev, type))
1407 max = libevdev_event_type_get_max(type);
1411 set_bit(dev->bits, type);
1413 if (type == EV_REP) {
1414 int delay = 0, period = 0;
1415 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
1416 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
1422 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
1426 if (type > EV_MAX || type == EV_SYN)
1429 max = libevdev_event_type_get_max(type);
1433 clear_bit(dev->bits, type);
1439 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
1440 unsigned int code, const void *data)
1443 unsigned long *mask = NULL;
1445 if (libevdev_enable_event_type(dev, type))
1462 max = type_to_mask(dev, type, &mask);
1464 if (code > max || (int)max == -1)
1467 set_bit(mask, code);
1469 if (type == EV_ABS) {
1470 const struct input_absinfo *abs = data;
1471 dev->abs_info[code] = *abs;
1472 if (code == ABS_MT_SLOT) {
1473 if (init_slots(dev) != 0)
1475 } else if (code == ABS_MT_TRACKING_ID) {
1476 reset_tracking_ids(dev);
1478 } else if (type == EV_REP) {
1479 const int *value = data;
1480 dev->rep_values[code] = *value;
1487 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
1490 unsigned long *mask = NULL;
1492 if (type > EV_MAX || type == EV_SYN)
1495 max = type_to_mask(dev, type, &mask);
1497 if (code > max || (int)max == -1)
1500 clear_bit(mask, code);
1502 if (type == EV_ABS) {
1503 if (code == ABS_MT_SLOT) {
1504 if (init_slots(dev) != 0)
1506 } else if (code == ABS_MT_TRACKING_ID) {
1507 reset_tracking_ids(dev);
1515 libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1519 if (!dev->initialized) {
1520 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
1522 } else if (dev->fd < 0)
1528 rc = ioctl(dev->fd, EVIOCSABS(code), abs);
1532 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
1538 libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
1542 if (!dev->initialized) {
1543 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
1545 } else if (dev->fd < 0)
1548 if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB) {
1549 log_bug(dev, "invalid grab parameter %#x\n", grab);
1553 if (grab == dev->grabbed)
1556 if (grab == LIBEVDEV_GRAB)
1557 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
1558 else if (grab == LIBEVDEV_UNGRAB)
1559 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
1562 dev->grabbed = grab;
1564 return rc < 0 ? -errno : 0;
1568 libevdev_event_is_type(const struct input_event *ev, unsigned int type)
1570 return type < EV_CNT && ev->type == type;
1574 libevdev_event_is_code(const struct input_event *ev, unsigned int type, unsigned int code)
1578 if (!libevdev_event_is_type(ev, type))
1581 max = libevdev_event_type_get_max(type);
1582 return (max > -1 && code <= (unsigned int)max && ev->code == code);
1585 LIBEVDEV_EXPORT const char*
1586 libevdev_event_type_get_name(unsigned int type)
1591 return ev_map[type];
1594 LIBEVDEV_EXPORT const char*
1595 libevdev_event_code_get_name(unsigned int type, unsigned int code)
1597 int max = libevdev_event_type_get_max(type);
1599 if (max == -1 || code > (unsigned int)max)
1602 return event_type_map[type][code];
1605 LIBEVDEV_EXPORT const char *
1606 libevdev_event_value_get_name(unsigned int type,
1610 /* This is a simplified version because nothing else
1611 is an enum like ABS_MT_TOOL_TYPE so we don't need
1613 if (type != EV_ABS || code != ABS_MT_TOOL_TYPE)
1616 if (value > MT_TOOL_MAX)
1619 return mt_tool_map[value];
1622 LIBEVDEV_EXPORT const char*
1623 libevdev_property_get_name(unsigned int prop)
1625 if (prop > INPUT_PROP_MAX)
1628 return input_prop_map[prop];
1632 libevdev_event_type_get_max(unsigned int type)
1637 return ev_max[type];
1641 libevdev_get_repeat(const struct libevdev *dev, int *delay, int *period)
1643 if (!libevdev_has_event_type(dev, EV_REP))
1647 *delay = dev->rep_values[REP_DELAY];
1649 *period = dev->rep_values[REP_PERIOD];
1655 libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value)
1657 return libevdev_kernel_set_led_values(dev, code, value, -1);
1661 libevdev_kernel_set_led_values(struct libevdev *dev, ...)
1663 struct input_event ev[LED_MAX + 1];
1664 enum libevdev_led_value val;
1670 if (!dev->initialized) {
1671 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
1673 } else if (dev->fd < 0)
1676 memset(ev, 0, sizeof(ev));
1678 va_start(args, dev);
1679 code = va_arg(args, unsigned int);
1680 while (code != -1) {
1681 if (code > LED_MAX) {
1685 val = va_arg(args, enum libevdev_led_value);
1686 if (val != LIBEVDEV_LED_ON && val != LIBEVDEV_LED_OFF) {
1691 if (libevdev_has_event_code(dev, EV_LED, code)) {
1692 struct input_event *e = ev;
1694 while (e->type > 0 && e->code != code)
1701 e->value = (val == LIBEVDEV_LED_ON);
1703 code = va_arg(args, unsigned int);
1707 if (rc == 0 && nleds > 0) {
1708 ev[nleds].type = EV_SYN;
1709 ev[nleds++].code = SYN_REPORT;
1711 rc = write(libevdev_get_fd(dev), ev, nleds * sizeof(ev[0]));
1713 nleds--; /* last is EV_SYN */
1715 update_led_state(dev, &ev[nleds]);
1717 rc = (rc != -1) ? 0 : -errno;
1724 libevdev_set_clock_id(struct libevdev *dev, int clockid)
1726 if (!dev->initialized) {
1727 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
1729 } else if (dev->fd < 0)
1732 return ioctl(dev->fd, EVIOCSCLOCKID, &clockid) ? -errno : 0;