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
34 #include "libevdev-int.h"
35 #include "libevdev-util.h"
36 #include "event-names.h"
40 enum event_filter_status {
41 EVENT_FILTER_NONE, /**< Event untouched by filters */
42 EVENT_FILTER_MODIFIED, /**< Event was modified */
43 EVENT_FILTER_DISCARD, /**< Discard current event */
46 static int sync_mt_state(struct libevdev *dev, int create_events);
49 slot_value(const struct libevdev *dev, int slot, int axis)
51 if (unlikely(slot > dev->num_slots)) {
52 log_bug(dev, "Slot %d exceeds number of slots (%d)\n", slot, dev->num_slots);
55 if (unlikely(axis < ABS_MT_MIN || axis > ABS_MT_MAX)) {
56 log_bug(dev, "MT axis %d is outside the valid range [%d,%d]\n",
57 axis, ABS_MT_MIN, ABS_MT_MAX);
60 return &dev->mt_slot_vals[slot * ABS_MT_CNT + axis - ABS_MT_MIN];
64 init_event_queue(struct libevdev *dev)
66 const int MIN_QUEUE_SIZE = 256;
67 int nevents = 1; /* terminating SYN_REPORT */
69 unsigned int type, code;
71 /* count the number of axes, keys, etc. to get a better idea at how
72 many events per EV_SYN we could possibly get. That's the max we
73 may get during SYN_DROPPED too. Use double that, just so we have
74 room for events while syncing a device.
76 for (type = EV_KEY; type < EV_MAX; type++) {
77 int max = libevdev_event_type_get_max(type);
78 for (code = 0; max > 0 && code < (unsigned int) max; code++) {
79 if (libevdev_has_event_code(dev, type, code))
84 nslots = libevdev_get_num_slots(dev);
88 for (code = ABS_MT_SLOT; code <= ABS_MAX; code++) {
89 if (libevdev_has_event_code(dev, EV_ABS, code))
93 /* We already counted the first slot in the initial count */
94 nevents += num_mt_axes * (nslots - 1);
97 return queue_alloc(dev, max(MIN_QUEUE_SIZE, nevents * 2));
101 libevdev_dflt_log_func(enum libevdev_log_priority priority,
103 const char *file, int line, const char *func,
104 const char *format, va_list args)
108 case LIBEVDEV_LOG_ERROR: prefix = "libevdev error"; break;
109 case LIBEVDEV_LOG_INFO: prefix = "libevdev info"; break;
110 case LIBEVDEV_LOG_DEBUG:
111 prefix = "libevdev debug";
114 prefix = "libevdev INVALID LOG PRIORITY";
117 /* default logging format:
118 libevev error in libevdev_some_func: blah blah
119 libevev info in libevdev_some_func: blah blah
120 libevev debug in file.c:123:libevdev_some_func: blah blah
123 fprintf(stderr, "%s in ", prefix);
124 if (priority == LIBEVDEV_LOG_DEBUG)
125 fprintf(stderr, "%s:%d:", file, line);
126 fprintf(stderr, "%s: ", func);
127 vfprintf(stderr, format, args);
131 fix_invalid_absinfo(const struct libevdev *dev,
133 struct input_absinfo* abs_info)
136 * The reported absinfo for ABS_MT_TRACKING_ID is sometimes
137 * uninitialized for certain mtk-soc, due to init code mangling
138 * in the vendor kernel.
140 if (axis == ABS_MT_TRACKING_ID &&
141 abs_info->maximum == abs_info->minimum) {
142 abs_info->minimum = -1;
143 abs_info->maximum = 0xFFFF;
145 "Device \"%s\" has invalid ABS_MT_TRACKING_ID range",
151 * Global logging settings.
153 static struct logdata log_data = {
154 .priority = LIBEVDEV_LOG_INFO,
155 .global_handler = libevdev_dflt_log_func,
160 _libevdev_log_msg(const struct libevdev *dev,
161 enum libevdev_log_priority priority,
162 const char *file, int line, const char *func,
163 const char *format, ...)
167 if (dev && dev->log.device_handler) {
169 * if both global handler and device handler are set
170 * we've set up the handlers wrong. And that means we'll
171 * likely get the printf args wrong and cause all sorts of
172 * mayhem. Seppuku is called for.
174 if (unlikely(dev->log.global_handler))
177 if (priority > dev->log.priority)
179 } else if (!log_data.global_handler || priority > log_data.priority)
181 else if (unlikely(log_data.device_handler))
182 abort(); /* Seppuku, see above */
184 va_start(args, format);
185 if (dev && dev->log.device_handler)
186 dev->log.device_handler(dev, priority, dev->log.userdata, file, line, func, format, args);
188 log_data.global_handler(priority, log_data.userdata, file, line, func, format, args);
193 libevdev_reset(struct libevdev *dev)
195 enum libevdev_log_priority pri = dev->log.priority;
196 libevdev_device_log_func_t handler = dev->log.device_handler;
201 free(dev->mt_slot_vals);
202 free(dev->mt_sync.mt_state);
203 free(dev->mt_sync.tracking_id_changes);
204 free(dev->mt_sync.slot_update);
205 memset(dev, 0, sizeof(*dev));
207 dev->initialized = false;
209 dev->current_slot = -1;
210 dev->grabbed = LIBEVDEV_UNGRAB;
211 dev->sync_state = SYNC_NONE;
212 dev->log.priority = pri;
213 dev->log.device_handler = handler;
214 libevdev_enable_event_type(dev, EV_SYN);
217 LIBEVDEV_EXPORT struct libevdev*
220 struct libevdev *dev;
222 dev = calloc(1, sizeof(*dev));
232 libevdev_new_from_fd(int fd, struct libevdev **dev)
241 rc = libevdev_set_fd(d, fd);
250 libevdev_free(struct libevdev *dev)
261 libevdev_set_log_function(libevdev_log_func_t logfunc, void *data)
263 log_data.global_handler = logfunc;
264 log_data.userdata = data;
268 libevdev_set_log_priority(enum libevdev_log_priority priority)
270 if (priority > LIBEVDEV_LOG_DEBUG)
271 priority = LIBEVDEV_LOG_DEBUG;
272 log_data.priority = priority;
275 LIBEVDEV_EXPORT enum libevdev_log_priority
276 libevdev_get_log_priority(void)
278 return log_data.priority;
282 libevdev_set_device_log_function(struct libevdev *dev,
283 libevdev_device_log_func_t logfunc,
284 enum libevdev_log_priority priority,
288 log_bug(NULL, "device must not be NULL\n");
292 dev->log.priority = priority;
293 dev->log.device_handler = logfunc;
294 dev->log.userdata = data;
297 enum libevdev_log_priority
298 _libevdev_log_priority(const struct libevdev *dev)
300 if (dev && dev->log.device_handler)
301 return dev->log.priority;
303 return libevdev_get_log_priority();
307 libevdev_change_fd(struct libevdev *dev, int fd)
309 if (!dev->initialized) {
310 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
314 dev->grabbed = LIBEVDEV_UNGRAB;
319 reset_tracking_ids(struct libevdev *dev)
321 if (dev->num_slots == -1 ||
322 !libevdev_has_event_code(dev, EV_ABS, ABS_MT_TRACKING_ID))
325 for (int slot = 0; slot < dev->num_slots; slot++)
326 libevdev_set_slot_value(dev, slot, ABS_MT_TRACKING_ID, -1);
330 free_slots(struct libevdev *dev)
333 free(dev->mt_slot_vals);
334 free(dev->mt_sync.tracking_id_changes);
335 free(dev->mt_sync.slot_update);
336 free(dev->mt_sync.mt_state);
337 dev->mt_slot_vals = NULL;
338 dev->mt_sync.tracking_id_changes = NULL;
339 dev->mt_sync.slot_update = NULL;
340 dev->mt_sync.mt_state = NULL;
344 init_slots(struct libevdev *dev)
346 const struct input_absinfo *abs_info;
349 free(dev->mt_slot_vals);
350 free(dev->mt_sync.tracking_id_changes);
351 free(dev->mt_sync.slot_update);
352 free(dev->mt_sync.mt_state);
353 dev->mt_slot_vals = NULL;
354 dev->mt_sync.tracking_id_changes = NULL;
355 dev->mt_sync.slot_update = NULL;
356 dev->mt_sync.mt_state = NULL;
358 /* devices with ABS_RESERVED aren't MT devices,
359 see the documentation for multitouch-related
360 functions for more details */
361 if (libevdev_has_event_code(dev, EV_ABS, ABS_RESERVED) ||
362 !libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT)) {
363 if (dev->num_slots != -1) {
369 abs_info = libevdev_get_abs_info(dev, ABS_MT_SLOT);
372 dev->num_slots = abs_info->maximum + 1;
373 dev->mt_slot_vals = calloc(dev->num_slots * ABS_MT_CNT, sizeof(int));
374 if (!dev->mt_slot_vals) {
378 dev->current_slot = abs_info->value;
380 dev->mt_sync.mt_state_sz = sizeof(*dev->mt_sync.mt_state) +
381 (dev->num_slots) * sizeof(int);
382 dev->mt_sync.mt_state = calloc(1, dev->mt_sync.mt_state_sz);
384 dev->mt_sync.tracking_id_changes_sz = NLONGS(dev->num_slots) * sizeof(long);
385 dev->mt_sync.tracking_id_changes = malloc(dev->mt_sync.tracking_id_changes_sz);
387 dev->mt_sync.slot_update_sz = NLONGS(dev->num_slots * ABS_MT_CNT) * sizeof(long);
388 dev->mt_sync.slot_update = malloc(dev->mt_sync.slot_update_sz);
390 if (!dev->mt_sync.tracking_id_changes ||
391 !dev->mt_sync.slot_update ||
392 !dev->mt_sync.mt_state) {
397 reset_tracking_ids(dev);
403 libevdev_set_fd(struct libevdev* dev, int fd)
409 if (dev->initialized) {
410 log_bug(dev, "device already initialized.\n");
417 rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
421 memset(buf, 0, sizeof(buf));
422 rc = ioctl(fd, EVIOCGNAME(sizeof(buf) - 1), buf);
427 dev->name = strdup(buf);
435 memset(buf, 0, sizeof(buf));
436 rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
438 /* uinput has no phys */
442 dev->phys = strdup(buf);
451 memset(buf, 0, sizeof(buf));
452 rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
457 dev->uniq = strdup(buf);
464 rc = ioctl(fd, EVIOCGID, &dev->ids);
468 rc = ioctl(fd, EVIOCGVERSION, &dev->driver_version);
472 /* Built on a kernel with props, running against a kernel without property
473 support. This should not be a fatal case, we'll be missing properties but other
474 than that everything is as expected.
476 rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
477 if (rc < 0 && errno != EINVAL)
480 rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
484 rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
488 rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
492 rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
496 rc = ioctl(fd, EVIOCGBIT(EV_SW, sizeof(dev->sw_bits)), dev->sw_bits);
500 rc = ioctl(fd, EVIOCGBIT(EV_MSC, sizeof(dev->msc_bits)), dev->msc_bits);
504 rc = ioctl(fd, EVIOCGBIT(EV_FF, sizeof(dev->ff_bits)), dev->ff_bits);
508 rc = ioctl(fd, EVIOCGBIT(EV_SND, sizeof(dev->snd_bits)), dev->snd_bits);
512 rc = ioctl(fd, EVIOCGKEY(sizeof(dev->key_values)), dev->key_values);
516 rc = ioctl(fd, EVIOCGLED(sizeof(dev->led_values)), dev->led_values);
520 rc = ioctl(fd, EVIOCGSW(sizeof(dev->sw_values)), dev->sw_values);
524 /* rep is a special case, always set it to 1 for both values if EV_REP is set */
525 if (bit_is_set(dev->bits, EV_REP)) {
526 for (i = 0; i < REP_CNT; i++)
527 set_bit(dev->rep_bits, i);
528 rc = ioctl(fd, EVIOCGREP, dev->rep_values);
533 for (i = ABS_X; i <= ABS_MAX; i++) {
534 if (bit_is_set(dev->abs_bits, i)) {
535 struct input_absinfo abs_info;
536 rc = ioctl(fd, EVIOCGABS(i), &abs_info);
540 fix_invalid_absinfo(dev, i, &abs_info);
542 dev->abs_info[i] = abs_info;
548 rc = init_slots(dev);
552 if (dev->num_slots != -1)
553 sync_mt_state(dev, 0);
555 rc = init_event_queue(dev);
561 /* not copying key state because we won't know when we'll start to
562 * use this fd and key's are likely to change state by then.
563 * Same with the valuators, really, but they may not change.
566 dev->initialized = true;
570 return rc ? -errno : 0;
574 libevdev_get_fd(const struct libevdev* dev)
580 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
582 ev->input_event_sec = dev->last_event_time.tv_sec;
583 ev->input_event_usec = dev->last_event_time.tv_usec;
590 sync_key_state(struct libevdev *dev)
594 unsigned long keystate[NLONGS(KEY_CNT)] = {0};
596 rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
600 for (i = 0; i < KEY_CNT; i++) {
602 old = bit_is_set(dev->key_values, i);
603 new = bit_is_set(keystate, i);
605 struct input_event *ev = queue_push(dev);
606 init_event(dev, ev, EV_KEY, i, new ? 1 : 0);
610 memcpy(dev->key_values, keystate, rc);
614 return rc ? -errno : 0;
618 sync_sw_state(struct libevdev *dev)
622 unsigned long swstate[NLONGS(SW_CNT)] = {0};
624 rc = ioctl(dev->fd, EVIOCGSW(sizeof(swstate)), swstate);
628 for (i = 0; i < SW_CNT; i++) {
630 old = bit_is_set(dev->sw_values, i);
631 new = bit_is_set(swstate, i);
633 struct input_event *ev = queue_push(dev);
634 init_event(dev, ev, EV_SW, i, new ? 1 : 0);
638 memcpy(dev->sw_values, swstate, rc);
642 return rc ? -errno : 0;
646 sync_led_state(struct libevdev *dev)
650 unsigned long ledstate[NLONGS(LED_CNT)] = {0};
652 rc = ioctl(dev->fd, EVIOCGLED(sizeof(ledstate)), ledstate);
656 for (i = 0; i < LED_CNT; i++) {
658 old = bit_is_set(dev->led_values, i);
659 new = bit_is_set(ledstate, i);
661 struct input_event *ev = queue_push(dev);
662 init_event(dev, ev, EV_LED, i, new ? 1 : 0);
666 memcpy(dev->led_values, ledstate, rc);
670 return rc ? -errno : 0;
673 sync_abs_state(struct libevdev *dev)
678 for (i = ABS_X; i < ABS_CNT; i++) {
679 struct input_absinfo abs_info;
681 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
684 if (!bit_is_set(dev->abs_bits, i))
687 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
691 if (dev->abs_info[i].value != abs_info.value) {
692 struct input_event *ev = queue_push(dev);
694 init_event(dev, ev, EV_ABS, i, abs_info.value);
695 dev->abs_info[i].value = abs_info.value;
701 return rc ? -errno : 0;
705 sync_mt_state(struct libevdev *dev, int create_events)
707 struct input_event *ev;
708 struct input_absinfo abs_info;
711 int last_reported_slot = 0;
712 struct mt_sync_state *mt_state = dev->mt_sync.mt_state;
713 unsigned long *slot_update = dev->mt_sync.slot_update;
714 unsigned long *tracking_id_changes = dev->mt_sync.tracking_id_changes;
715 int need_tracking_id_changes = 0;
717 memset(dev->mt_sync.slot_update, 0, dev->mt_sync.slot_update_sz);
718 memset(dev->mt_sync.tracking_id_changes, 0,
719 dev->mt_sync.tracking_id_changes_sz);
721 #define AXISBIT(_slot, _axis) (_slot * ABS_MT_CNT + _axis - ABS_MT_MIN)
723 for (axis = ABS_MT_MIN; axis <= ABS_MT_MAX; axis++) {
724 if (axis == ABS_MT_SLOT)
727 if (!libevdev_has_event_code(dev, EV_ABS, axis))
730 mt_state->code = axis;
731 rc = ioctl(dev->fd, EVIOCGMTSLOTS(dev->mt_sync.mt_state_sz), mt_state);
735 for (slot = 0; slot < dev->num_slots; slot++) {
737 if (*slot_value(dev, slot, axis) == mt_state->val[slot])
740 if (axis == ABS_MT_TRACKING_ID &&
741 *slot_value(dev, slot, axis) != -1 &&
742 mt_state->val[slot] != -1) {
743 set_bit(tracking_id_changes, slot);
744 need_tracking_id_changes = 1;
747 *slot_value(dev, slot, axis) = mt_state->val[slot];
749 set_bit(slot_update, AXISBIT(slot, axis));
750 /* note that this slot has updates */
751 set_bit(slot_update, AXISBIT(slot, ABS_MT_SLOT));
755 if (!create_events) {
760 if (need_tracking_id_changes) {
761 for (slot = 0; slot < dev->num_slots; slot++) {
762 if (!bit_is_set(tracking_id_changes, slot))
765 ev = queue_push(dev);
766 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, slot);
767 ev = queue_push(dev);
768 init_event(dev, ev, EV_ABS, ABS_MT_TRACKING_ID, -1);
770 last_reported_slot = slot;
773 ev = queue_push(dev);
774 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
777 for (slot = 0; slot < dev->num_slots; slot++) {
778 if (!bit_is_set(slot_update, AXISBIT(slot, ABS_MT_SLOT)))
781 ev = queue_push(dev);
782 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, slot);
783 last_reported_slot = slot;
785 for (axis = ABS_MT_MIN; axis <= ABS_MT_MAX; axis++) {
786 if (axis == ABS_MT_SLOT ||
787 !libevdev_has_event_code(dev, EV_ABS, axis))
790 if (bit_is_set(slot_update, AXISBIT(slot, axis))) {
791 ev = queue_push(dev);
792 init_event(dev, ev, EV_ABS, axis, *slot_value(dev, slot, axis));
797 /* add one last slot event to make sure the client is on the same
798 slot as the kernel */
800 rc = ioctl(dev->fd, EVIOCGABS(ABS_MT_SLOT), &abs_info);
804 dev->current_slot = abs_info.value;
806 if (dev->current_slot != last_reported_slot) {
807 ev = queue_push(dev);
808 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, dev->current_slot);
815 return rc ? -errno : 0;
819 read_more_events(struct libevdev *dev)
823 struct input_event *next;
825 free_elem = queue_num_free_elements(dev);
829 next = queue_next_element(dev);
830 len = read(dev->fd, next, free_elem * sizeof(struct input_event));
833 } else if (len > 0 && len % sizeof(struct input_event) != 0)
836 int nev = len/sizeof(struct input_event);
837 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
844 drain_events(struct libevdev *dev)
849 const int max_iterations = 8; /* EVDEV_BUF_PACKETS in
850 kernel/drivers/input/evedev.c */
852 queue_shift_multiple(dev, queue_num_elements(dev), NULL);
855 rc = read_more_events(dev);
860 log_error(dev, "Failed to drain events before sync.\n");
864 nelem = queue_num_elements(dev);
865 queue_shift_multiple(dev, nelem, NULL);
866 } while (iterations++ < max_iterations && nelem >= queue_size(dev));
868 /* Our buffer should be roughly the same or bigger than the kernel
869 buffer in most cases, so we usually don't expect to recurse. If
870 we do, make sure we stop after max_iterations and proceed with
871 what we have. This could happen if events queue up faster than
874 if (iterations >= max_iterations)
875 log_info(dev, "Unable to drain events, buffer size mismatch.\n");
879 sync_state(struct libevdev *dev)
882 struct input_event *ev;
884 /* see section "Discarding events before synchronizing" in
885 * libevdev/libevdev.h */
888 if (libevdev_has_event_type(dev, EV_KEY))
889 rc = sync_key_state(dev);
890 if (libevdev_has_event_type(dev, EV_LED))
891 rc = sync_led_state(dev);
892 if (libevdev_has_event_type(dev, EV_SW))
893 rc = sync_sw_state(dev);
894 if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
895 rc = sync_abs_state(dev);
896 if (rc == 0 && dev->num_slots > -1 &&
897 libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
898 rc = sync_mt_state(dev, 1);
900 dev->queue_nsync = queue_num_elements(dev);
902 if (dev->queue_nsync > 0) {
903 ev = queue_push(dev);
904 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
912 update_key_state(struct libevdev *dev, const struct input_event *e)
914 if (!libevdev_has_event_type(dev, EV_KEY))
917 if (e->code > KEY_MAX)
920 set_bit_state(dev->key_values, e->code, e->value != 0);
926 update_mt_state(struct libevdev *dev, const struct input_event *e)
928 if (e->code == ABS_MT_SLOT && dev->num_slots > -1) {
930 dev->current_slot = e->value;
931 /* sync abs_info with the current slot values */
932 for (i = ABS_MT_SLOT + 1; i <= ABS_MT_MAX; i++) {
933 if (libevdev_has_event_code(dev, EV_ABS, i))
934 dev->abs_info[i].value = *slot_value(dev, dev->current_slot, i);
938 } else if (dev->current_slot == -1)
941 *slot_value(dev, dev->current_slot, e->code) = e->value;
947 update_abs_state(struct libevdev *dev, const struct input_event *e)
949 if (!libevdev_has_event_type(dev, EV_ABS))
952 if (e->code > ABS_MAX)
955 if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
956 update_mt_state(dev, e);
958 dev->abs_info[e->code].value = e->value;
964 update_led_state(struct libevdev *dev, const struct input_event *e)
966 if (!libevdev_has_event_type(dev, EV_LED))
969 if (e->code > LED_MAX)
972 set_bit_state(dev->led_values, e->code, e->value != 0);
978 update_sw_state(struct libevdev *dev, const struct input_event *e)
980 if (!libevdev_has_event_type(dev, EV_SW))
983 if (e->code > SW_MAX)
986 set_bit_state(dev->sw_values, e->code, e->value != 0);
992 update_state(struct libevdev *dev, const struct input_event *e)
1001 rc = update_key_state(dev, e);
1004 rc = update_abs_state(dev, e);
1007 rc = update_led_state(dev, e);
1010 rc = update_sw_state(dev, e);
1014 dev->last_event_time.tv_sec = e->input_event_sec;
1015 dev->last_event_time.tv_usec = e->input_event_usec;
1021 * Sanitize/modify events where needed.
1023 static inline enum event_filter_status
1024 sanitize_event(const struct libevdev *dev,
1025 struct input_event *ev,
1026 enum SyncState sync_state)
1028 if (!libevdev_has_event_code(dev, ev->type, ev->code))
1029 return EVENT_FILTER_DISCARD;
1031 if (unlikely(dev->num_slots > -1 &&
1032 libevdev_event_is_code(ev, EV_ABS, ABS_MT_SLOT) &&
1033 (ev->value < 0 || ev->value >= dev->num_slots))) {
1034 log_bug(dev, "Device \"%s\" received an invalid slot index %d."
1035 "Capping to announced max slot number %d.\n",
1036 dev->name, ev->value, dev->num_slots - 1);
1037 ev->value = dev->num_slots - 1;
1038 return EVENT_FILTER_MODIFIED;
1040 /* Drop any invalid tracking IDs, they are only supposed to go from
1041 N to -1 or from -1 to N. Never from -1 to -1, or N to M. Very
1042 unlikely to ever happen from a real device.
1044 } else if (unlikely(sync_state == SYNC_NONE &&
1045 dev->num_slots > -1 &&
1046 libevdev_event_is_code(ev, EV_ABS, ABS_MT_TRACKING_ID) &&
1047 ((ev->value == -1 &&
1048 *slot_value(dev, dev->current_slot, ABS_MT_TRACKING_ID) == -1) ||
1050 *slot_value(dev, dev->current_slot, ABS_MT_TRACKING_ID) != -1)))) {
1051 log_bug(dev, "Device \"%s\" received a double tracking ID %d in slot %d.\n",
1052 dev->name, ev->value, dev->current_slot);
1053 return EVENT_FILTER_DISCARD;
1056 return EVENT_FILTER_NONE;
1060 libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
1062 int rc = LIBEVDEV_READ_STATUS_SUCCESS;
1063 enum event_filter_status filter_status;
1064 const unsigned int valid_flags = LIBEVDEV_READ_FLAG_NORMAL |
1065 LIBEVDEV_READ_FLAG_SYNC |
1066 LIBEVDEV_READ_FLAG_FORCE_SYNC |
1067 LIBEVDEV_READ_FLAG_BLOCKING;
1069 if (!dev->initialized) {
1070 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
1072 } else if (dev->fd < 0)
1075 if ((flags & valid_flags) == 0) {
1076 log_bug(dev, "invalid flags %#x.\n", flags);
1080 if (flags & LIBEVDEV_READ_FLAG_SYNC) {
1081 if (dev->sync_state == SYNC_NEEDED) {
1082 rc = sync_state(dev);
1085 dev->sync_state = SYNC_IN_PROGRESS;
1088 if (dev->queue_nsync == 0) {
1089 dev->sync_state = SYNC_NONE;
1093 } else if (dev->sync_state != SYNC_NONE) {
1094 struct input_event e;
1096 /* call update_state for all events here, otherwise the library has the wrong view
1097 of the device too */
1098 while (queue_shift(dev, &e) == 0) {
1100 if (sanitize_event(dev, &e, dev->sync_state) != EVENT_FILTER_DISCARD)
1101 update_state(dev, &e);
1104 dev->sync_state = SYNC_NONE;
1107 /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
1108 worst case we don't read fast enough and end up with SYN_DROPPED anyway.
1110 Except if the fd is in blocking mode and we still have events from the last read, don't
1114 if (queue_num_elements(dev) == 0) {
1115 rc = read_more_events(dev);
1116 if (rc < 0 && rc != -EAGAIN)
1120 if (flags & LIBEVDEV_READ_FLAG_FORCE_SYNC) {
1121 dev->sync_state = SYNC_NEEDED;
1122 rc = LIBEVDEV_READ_STATUS_SYNC;
1126 if (queue_shift(dev, ev) != 0)
1129 filter_status = sanitize_event(dev, ev, dev->sync_state);
1130 if (filter_status != EVENT_FILTER_DISCARD)
1131 update_state(dev, ev);
1133 /* if we disabled a code, get the next event instead */
1134 } while(filter_status == EVENT_FILTER_DISCARD ||
1135 !libevdev_has_event_code(dev, ev->type, ev->code));
1137 rc = LIBEVDEV_READ_STATUS_SUCCESS;
1138 if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
1139 dev->sync_state = SYNC_NEEDED;
1140 rc = LIBEVDEV_READ_STATUS_SYNC;
1143 if (flags & LIBEVDEV_READ_FLAG_SYNC && dev->queue_nsync > 0) {
1145 rc = LIBEVDEV_READ_STATUS_SYNC;
1146 if (dev->queue_nsync == 0)
1147 dev->sync_state = SYNC_NONE;
1155 libevdev_has_event_pending(struct libevdev *dev)
1157 struct pollfd fds = { dev->fd, POLLIN, 0 };
1160 if (!dev->initialized) {
1161 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
1163 } else if (dev->fd < 0)
1166 if (queue_num_elements(dev) != 0)
1169 rc = poll(&fds, 1, 0);
1170 return (rc >= 0) ? rc : -errno;
1173 LIBEVDEV_EXPORT const char *
1174 libevdev_get_name(const struct libevdev *dev)
1176 return dev->name ? dev->name : "";
1179 LIBEVDEV_EXPORT const char *
1180 libevdev_get_phys(const struct libevdev *dev)
1185 LIBEVDEV_EXPORT const char *
1186 libevdev_get_uniq(const struct libevdev *dev)
1191 #define STRING_SETTER(field) \
1192 LIBEVDEV_EXPORT void libevdev_set_##field(struct libevdev *dev, const char *field) \
1194 if (field == NULL) \
1197 dev->field = strdup(field); \
1204 #define PRODUCT_GETTER(name) \
1205 LIBEVDEV_EXPORT int libevdev_get_id_##name(const struct libevdev *dev) \
1207 return dev->ids.name; \
1210 PRODUCT_GETTER(product)
1211 PRODUCT_GETTER(vendor)
1212 PRODUCT_GETTER(bustype)
1213 PRODUCT_GETTER(version)
1215 #define PRODUCT_SETTER(field) \
1216 LIBEVDEV_EXPORT void libevdev_set_id_##field(struct libevdev *dev, int field) \
1218 dev->ids.field = field;\
1221 PRODUCT_SETTER(product)
1222 PRODUCT_SETTER(vendor)
1223 PRODUCT_SETTER(bustype)
1224 PRODUCT_SETTER(version)
1227 libevdev_get_driver_version(const struct libevdev *dev)
1229 return dev->driver_version;
1233 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
1235 return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
1239 libevdev_enable_property(struct libevdev *dev, unsigned int prop)
1241 if (prop > INPUT_PROP_MAX)
1244 set_bit(dev->props, prop);
1249 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
1251 return type == EV_SYN ||(type <= EV_MAX && bit_is_set(dev->bits, type));
1255 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
1257 const unsigned long *mask = NULL;
1260 if (!libevdev_has_event_type(dev, type))
1266 max = type_to_mask_const(dev, type, &mask);
1268 if (max == -1 || code > (unsigned int)max)
1271 return bit_is_set(mask, code);
1275 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
1279 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
1283 case EV_ABS: value = dev->abs_info[code].value; break;
1284 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
1285 case EV_LED: value = bit_is_set(dev->led_values, code); break;
1286 case EV_SW: value = bit_is_set(dev->sw_values, code); break;
1290 libevdev_get_repeat(dev, &value, NULL);
1293 libevdev_get_repeat(dev, NULL, &value);
1309 libevdev_set_event_value(struct libevdev *dev, unsigned int type, unsigned int code, int value)
1312 struct input_event e;
1314 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
1321 if (sanitize_event(dev, &e, SYNC_NONE) != EVENT_FILTER_NONE)
1325 case EV_ABS: rc = update_abs_state(dev, &e); break;
1326 case EV_KEY: rc = update_key_state(dev, &e); break;
1327 case EV_LED: rc = update_led_state(dev, &e); break;
1328 case EV_SW: rc = update_sw_state(dev, &e); break;
1338 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
1340 if (libevdev_has_event_type(dev, type) &&
1341 libevdev_has_event_code(dev, type, code)) {
1342 *value = libevdev_get_event_value(dev, type, code);
1349 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
1351 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1354 if (dev->num_slots < 0 || slot >= (unsigned int)dev->num_slots)
1357 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1360 return *slot_value(dev, slot, code);
1364 libevdev_set_slot_value(struct libevdev *dev, unsigned int slot, unsigned int code, int value)
1366 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
1369 if (dev->num_slots == -1 || slot >= (unsigned int)dev->num_slots)
1372 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
1375 if (code == ABS_MT_SLOT) {
1376 if (value < 0 || value >= libevdev_get_num_slots(dev))
1378 dev->current_slot = value;
1381 *slot_value(dev, slot, code) = value;
1387 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
1389 if (libevdev_has_event_type(dev, EV_ABS) &&
1390 libevdev_has_event_code(dev, EV_ABS, code) &&
1391 dev->num_slots >= 0 &&
1392 slot < (unsigned int)dev->num_slots) {
1393 *value = libevdev_get_slot_value(dev, slot, code);
1400 libevdev_get_num_slots(const struct libevdev *dev)
1402 return dev->num_slots;
1406 libevdev_get_current_slot(const struct libevdev *dev)
1408 return dev->current_slot;
1411 LIBEVDEV_EXPORT const struct input_absinfo*
1412 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
1414 if (!libevdev_has_event_type(dev, EV_ABS) ||
1415 !libevdev_has_event_code(dev, EV_ABS, code))
1418 return &dev->abs_info[code];
1421 #define ABS_GETTER(name) \
1422 LIBEVDEV_EXPORT int libevdev_get_abs_##name(const struct libevdev *dev, unsigned int code) \
1424 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code); \
1425 return absinfo ? absinfo->name : 0; \
1432 ABS_GETTER(resolution)
1434 #define ABS_SETTER(field) \
1435 LIBEVDEV_EXPORT void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
1437 if (!libevdev_has_event_code(dev, EV_ABS, code)) \
1439 dev->abs_info[code].field = val; \
1446 ABS_SETTER(resolution)
1448 LIBEVDEV_EXPORT void
1449 libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1451 if (!libevdev_has_event_code(dev, EV_ABS, code))
1454 dev->abs_info[code] = *abs;
1458 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
1465 if (libevdev_has_event_type(dev, type))
1468 max = libevdev_event_type_get_max(type);
1472 set_bit(dev->bits, type);
1474 if (type == EV_REP) {
1475 int delay = 0, period = 0;
1476 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
1477 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
1483 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
1487 if (type > EV_MAX || type == EV_SYN)
1490 max = libevdev_event_type_get_max(type);
1494 clear_bit(dev->bits, type);
1500 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
1501 unsigned int code, const void *data)
1504 unsigned long *mask = NULL;
1506 if (libevdev_enable_event_type(dev, type))
1523 max = type_to_mask(dev, type, &mask);
1525 if (code > max || (int)max == -1)
1528 set_bit(mask, code);
1530 if (type == EV_ABS) {
1531 const struct input_absinfo *abs = data;
1532 dev->abs_info[code] = *abs;
1533 if (code == ABS_MT_SLOT) {
1534 if (init_slots(dev) != 0)
1536 } else if (code == ABS_MT_TRACKING_ID) {
1537 reset_tracking_ids(dev);
1539 } else if (type == EV_REP) {
1540 const int *value = data;
1541 dev->rep_values[code] = *value;
1548 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
1551 unsigned long *mask = NULL;
1553 if (type > EV_MAX || type == EV_SYN)
1556 max = type_to_mask(dev, type, &mask);
1558 if (code > max || (int)max == -1)
1561 clear_bit(mask, code);
1563 if (type == EV_ABS) {
1564 if (code == ABS_MT_SLOT) {
1565 if (init_slots(dev) != 0)
1567 } else if (code == ABS_MT_TRACKING_ID) {
1568 reset_tracking_ids(dev);
1576 libevdev_kernel_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
1580 if (!dev->initialized) {
1581 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
1583 } else if (dev->fd < 0)
1589 rc = ioctl(dev->fd, EVIOCSABS(code), abs);
1593 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
1599 libevdev_grab(struct libevdev *dev, enum libevdev_grab_mode grab)
1603 if (!dev->initialized) {
1604 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
1606 } else if (dev->fd < 0)
1609 if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB) {
1610 log_bug(dev, "invalid grab parameter %#x\n", grab);
1614 if (grab == dev->grabbed)
1617 if (grab == LIBEVDEV_GRAB)
1618 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
1619 else if (grab == LIBEVDEV_UNGRAB)
1620 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
1623 dev->grabbed = grab;
1625 return rc < 0 ? -errno : 0;
1629 libevdev_event_is_type(const struct input_event *ev, unsigned int type)
1631 return type < EV_CNT && ev->type == type;
1635 libevdev_event_is_code(const struct input_event *ev, unsigned int type, unsigned int code)
1639 if (!libevdev_event_is_type(ev, type))
1642 max = libevdev_event_type_get_max(type);
1643 return (max > -1 && code <= (unsigned int)max && ev->code == code);
1646 LIBEVDEV_EXPORT const char*
1647 libevdev_event_type_get_name(unsigned int type)
1652 return ev_map[type];
1655 LIBEVDEV_EXPORT const char*
1656 libevdev_event_code_get_name(unsigned int type, unsigned int code)
1658 int max = libevdev_event_type_get_max(type);
1660 if (max == -1 || code > (unsigned int)max)
1663 return event_type_map[type][code];
1666 LIBEVDEV_EXPORT const char *
1667 libevdev_event_value_get_name(unsigned int type,
1671 /* This is a simplified version because nothing else
1672 is an enum like ABS_MT_TOOL_TYPE so we don't need
1674 if (type != EV_ABS || code != ABS_MT_TOOL_TYPE)
1677 if (value > MT_TOOL_MAX)
1680 return mt_tool_map[value];
1683 LIBEVDEV_EXPORT const char*
1684 libevdev_property_get_name(unsigned int prop)
1686 if (prop > INPUT_PROP_MAX)
1689 return input_prop_map[prop];
1693 libevdev_event_type_get_max(unsigned int type)
1698 return ev_max[type];
1702 libevdev_get_repeat(const struct libevdev *dev, int *delay, int *period)
1704 if (!libevdev_has_event_type(dev, EV_REP))
1708 *delay = dev->rep_values[REP_DELAY];
1710 *period = dev->rep_values[REP_PERIOD];
1716 libevdev_kernel_set_led_value(struct libevdev *dev, unsigned int code, enum libevdev_led_value value)
1718 return libevdev_kernel_set_led_values(dev, code, value, -1);
1722 libevdev_kernel_set_led_values(struct libevdev *dev, ...)
1724 struct input_event ev[LED_MAX + 1];
1725 enum libevdev_led_value val;
1731 if (!dev->initialized) {
1732 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
1734 } else if (dev->fd < 0)
1737 memset(ev, 0, sizeof(ev));
1739 va_start(args, dev);
1740 code = va_arg(args, unsigned int);
1741 while (code != -1) {
1742 if (code > LED_MAX) {
1746 val = va_arg(args, enum libevdev_led_value);
1747 if (val != LIBEVDEV_LED_ON && val != LIBEVDEV_LED_OFF) {
1752 if (libevdev_has_event_code(dev, EV_LED, code)) {
1753 struct input_event *e = ev;
1755 while (e->type > 0 && e->code != code)
1762 e->value = (val == LIBEVDEV_LED_ON);
1764 code = va_arg(args, unsigned int);
1768 if (rc == 0 && nleds > 0) {
1769 ev[nleds].type = EV_SYN;
1770 ev[nleds++].code = SYN_REPORT;
1772 rc = write(libevdev_get_fd(dev), ev, nleds * sizeof(ev[0]));
1774 nleds--; /* last is EV_SYN */
1776 update_led_state(dev, &ev[nleds]);
1778 rc = (rc != -1) ? 0 : -errno;
1785 libevdev_set_clock_id(struct libevdev *dev, int clockid)
1787 if (!dev->initialized) {
1788 log_bug(dev, "device not initialized. call libevdev_set_fd() first\n");
1790 } else if (dev->fd < 0)
1793 return ioctl(dev->fd, EVIOCSCLOCKID, &clockid) ? -errno : 0;