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
30 #include <linux/uinput.h>
33 #include "libevdev-int.h"
34 #include "libevdev-util.h"
35 #include "event-names.h"
39 static int sync_mt_state(struct libevdev *dev, int create_events);
42 init_event_queue(struct libevdev *dev)
44 /* FIXME: count the number of axes, keys, etc. to get a better idea at how many events per
45 EV_SYN we could possibly get. Then multiply that by the actual buffer size we care about */
47 const int QUEUE_SIZE = 256;
49 return queue_alloc(dev, QUEUE_SIZE);
53 _libevdev_log(struct libevdev *dev, const char *format, ...)
57 va_start(args, format);
58 dev->log(format, args);
63 libevdev_noop_log_func(const char *format, va_list args)
72 dev = calloc(1, sizeof(*dev));
77 dev->current_slot = -1;
78 dev->log = libevdev_noop_log_func;
79 dev->grabbed = LIBEVDEV_UNGRAB;
80 dev->sync_state = SYNC_NONE;
86 libevdev_new_from_fd(int fd, struct libevdev **dev)
95 rc = libevdev_set_fd(d, fd);
104 libevdev_free(struct libevdev *dev)
117 libevdev_set_log_handler(struct libevdev *dev, libevdev_log_func_t logfunc)
122 dev->log = logfunc ? logfunc : libevdev_noop_log_func;
126 libevdev_change_fd(struct libevdev *dev, int fd)
135 libevdev_set_fd(struct libevdev* dev, int fd)
144 rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
148 memset(buf, 0, sizeof(buf));
149 rc = ioctl(fd, EVIOCGNAME(sizeof(buf) - 1), buf);
154 dev->name = strdup(buf);
162 memset(buf, 0, sizeof(buf));
163 rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
165 /* uinput has no phys */
169 dev->phys = strdup(buf);
178 memset(buf, 0, sizeof(buf));
179 rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
184 dev->uniq = strdup(buf);
191 rc = ioctl(fd, EVIOCGID, &dev->ids);
195 rc = ioctl(fd, EVIOCGVERSION, &dev->driver_version);
199 rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
203 rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
207 rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
211 rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
215 rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
219 rc = ioctl(fd, EVIOCGBIT(EV_SW, sizeof(dev->sw_bits)), dev->sw_bits);
223 rc = ioctl(fd, EVIOCGBIT(EV_MSC, sizeof(dev->msc_bits)), dev->msc_bits);
227 rc = ioctl(fd, EVIOCGBIT(EV_FF, sizeof(dev->ff_bits)), dev->ff_bits);
231 rc = ioctl(fd, EVIOCGBIT(EV_SND, sizeof(dev->snd_bits)), dev->snd_bits);
235 /* rep is a special case, always set it to 1 for both values if EV_REP is set */
236 if (bit_is_set(dev->bits, EV_REP)) {
237 for (i = 0; i < REP_CNT; i++)
238 set_bit(dev->rep_bits, i);
239 rc = ioctl(fd, EVIOCGREP, dev->rep_values);
244 for (i = ABS_X; i <= ABS_MAX; i++) {
245 if (bit_is_set(dev->abs_bits, i)) {
246 struct input_absinfo abs_info;
247 rc = ioctl(fd, EVIOCGABS(i), &abs_info);
251 dev->abs_info[i] = abs_info;
252 if (i == ABS_MT_SLOT) {
253 dev->num_slots = abs_info.maximum + 1;
254 dev->current_slot = abs_info.value;
261 sync_mt_state(dev, 0);
263 rc = init_event_queue(dev);
269 /* not copying key state because we won't know when we'll start to
270 * use this fd and key's are likely to change state by then.
271 * Same with the valuators, really, but they may not change.
275 return rc ? -errno : 0;
279 libevdev_get_fd(const struct libevdev* dev)
285 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
287 ev->time = dev->last_event_time;
294 sync_key_state(struct libevdev *dev)
298 unsigned long keystate[NLONGS(KEY_MAX)];
300 rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
304 for (i = 0; i < KEY_MAX; i++) {
306 old = bit_is_set(dev->key_values, i);
307 new = bit_is_set(keystate, i);
309 struct input_event *ev = queue_push(dev);
310 init_event(dev, ev, EV_KEY, i, new ? 1 : 0);
312 set_bit_state(dev->key_values, i, new);
317 return rc ? -errno : 0;
321 sync_abs_state(struct libevdev *dev)
326 for (i = ABS_X; i <= ABS_MAX; i++) {
327 struct input_absinfo abs_info;
329 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
332 if (!bit_is_set(dev->abs_bits, i))
335 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
339 if (dev->abs_info[i].value != abs_info.value) {
340 struct input_event *ev = queue_push(dev);
342 init_event(dev, ev, EV_ABS, i, abs_info.value);
343 dev->abs_info[i].value = abs_info.value;
349 return rc ? -errno : 0;
353 sync_mt_state(struct libevdev *dev, int create_events)
360 } mt_state[ABS_MT_CNT];
362 for (i = ABS_MT_MIN; i < ABS_MT_MAX; i++) {
364 if (i == ABS_MT_SLOT)
367 if (!libevdev_has_event_code(dev, EV_ABS, i))
370 idx = i - ABS_MT_MIN;
371 mt_state[idx].code = i;
372 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
377 for (i = 0; i < dev->num_slots; i++) {
379 struct input_event *ev;
382 ev = queue_push(dev);
383 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, i);
386 for (j = ABS_MT_MIN; j < ABS_MT_MAX; j++) {
387 int jdx = j - ABS_MT_MIN;
389 if (j == ABS_MT_SLOT)
392 if (!libevdev_has_event_code(dev, EV_ABS, j))
395 if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
399 ev = queue_push(dev);
400 init_event(dev, ev, EV_ABS, j, mt_state[jdx].val[i]);
402 dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
408 return rc ? -errno : 0;
412 sync_state(struct libevdev *dev)
416 struct input_event *ev;
418 /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
419 queue[0]) we need to shift this backwards. Except that chances are that the
420 queue may be either full or too full to prepend all the events needed for
423 so we search for the last sync event in the queue and drop everything before
424 including that event and rely on the kernel to tell us the right value for that
425 bitfield during the sync process.
428 for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
429 struct input_event e;
430 queue_peek(dev, i, &e);
431 if (e.type == EV_SYN)
436 queue_shift_multiple(dev, i + 1, NULL);
438 if (libevdev_has_event_type(dev, EV_KEY))
439 rc = sync_key_state(dev);
440 if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
441 rc = sync_abs_state(dev);
442 if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
443 rc = sync_mt_state(dev, 1);
445 dev->queue_nsync = queue_num_elements(dev);
447 if (dev->queue_nsync > 0) {
448 ev = queue_push(dev);
449 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
457 update_key_state(struct libevdev *dev, const struct input_event *e)
459 if (!libevdev_has_event_type(dev, EV_KEY))
462 if (e->code > KEY_MAX)
466 clear_bit(dev->key_values, e->code);
468 set_bit(dev->key_values, e->code);
474 update_mt_state(struct libevdev *dev, const struct input_event *e)
476 if (e->code == ABS_MT_SLOT) {
477 dev->current_slot = e->value;
479 } else if (dev->current_slot == -1)
482 dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
488 update_abs_state(struct libevdev *dev, const struct input_event *e)
490 if (!libevdev_has_event_type(dev, EV_ABS))
493 if (e->code > ABS_MAX)
496 if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
497 return update_mt_state(dev, e);
499 dev->abs_info[e->code].value = e->value;
505 update_state(struct libevdev *dev, const struct input_event *e)
514 rc = update_key_state(dev, e);
517 rc = update_abs_state(dev, e);
521 dev->last_event_time = e->time;
527 read_more_events(struct libevdev *dev)
531 struct input_event *next;
533 free_elem = queue_num_free_elements(dev);
537 next = queue_next_element(dev);
538 len = read(dev->fd, next, free_elem * sizeof(struct input_event));
541 } else if (len > 0 && len % sizeof(struct input_event) != 0)
544 int nev = len/sizeof(struct input_event);
545 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
551 int libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
558 if (!(flags & (LIBEVDEV_READ_NORMAL|LIBEVDEV_READ_SYNC|LIBEVDEV_FORCE_SYNC)))
561 if (flags & LIBEVDEV_READ_SYNC) {
562 if (dev->sync_state == SYNC_NEEDED) {
563 rc = sync_state(dev);
566 dev->sync_state = SYNC_IN_PROGRESS;
569 if (dev->queue_nsync == 0) {
570 dev->sync_state = SYNC_NONE;
574 } else if (dev->sync_state != SYNC_NONE) {
575 struct input_event e;
577 /* call update_state for all events here, otherwise the library has the wrong view
579 while (queue_shift(dev, &e) == 0) {
581 update_state(dev, &e);
584 dev->sync_state = SYNC_NONE;
587 /* FIXME: if the first event after SYNC_IN_PROGRESS is a SYN_DROPPED, log this */
589 /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
590 worst case we don't read fast enough and end up with SYN_DROPPED anyway.
592 Except if the fd is in blocking mode and we still have events from the last read, don't
596 if (!(flags & LIBEVDEV_READ_BLOCKING) ||
597 queue_num_elements(dev) == 0) {
598 rc = read_more_events(dev);
599 if (rc < 0 && rc != -EAGAIN)
603 if (flags & LIBEVDEV_FORCE_SYNC) {
604 dev->sync_state = SYNC_NEEDED;
610 if (queue_shift(dev, ev) != 0)
613 update_state(dev, ev);
615 /* if we disabled a code, get the next event instead */
616 } while(!libevdev_has_event_code(dev, ev->type, ev->code));
619 if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
620 dev->sync_state = SYNC_NEEDED;
624 if (flags & LIBEVDEV_READ_SYNC && dev->queue_nsync > 0) {
627 if (dev->queue_nsync == 0)
628 dev->sync_state = SYNC_NONE;
635 int libevdev_has_event_pending(struct libevdev *dev)
637 struct pollfd fds = { dev->fd, POLLIN, 0 };
643 if (queue_num_elements(dev) != 0)
646 rc = poll(&fds, 1, 0);
647 return (rc >= 0) ? rc : -errno;
651 libevdev_get_name(const struct libevdev *dev)
653 return dev->name ? dev->name : "";
657 libevdev_get_phys(const struct libevdev *dev)
663 libevdev_get_uniq(const struct libevdev *dev)
668 #define STRING_SETTER(field) \
669 void libevdev_set_##field(struct libevdev *dev, const char *field) \
674 dev->field = strdup(field); \
681 #define PRODUCT_GETTER(name, field) \
682 int libevdev_get_##name(const struct libevdev *dev) \
684 return dev->ids.field; \
687 PRODUCT_GETTER(product_id, product);
688 PRODUCT_GETTER(vendor_id, vendor);
689 PRODUCT_GETTER(bustype, bustype);
690 PRODUCT_GETTER(version, version);
692 int libevdev_get_driver_version(const struct libevdev *dev)
694 return dev->driver_version;
698 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
700 return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
704 libevdev_enable_property(struct libevdev *dev, unsigned int prop)
706 if (prop > INPUT_PROP_MAX)
709 set_bit(dev->props, prop);
714 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
716 return (type <= EV_MAX) && bit_is_set(dev->bits, type);
720 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
722 const unsigned long *mask;
725 if (!libevdev_has_event_type(dev, type))
731 max = type_to_mask_const(dev, type, &mask);
733 if (max == -1 || code > max)
736 return bit_is_set(mask, code);
740 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
744 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
748 case EV_ABS: value = dev->abs_info[code].value; break;
749 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
759 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
761 if (libevdev_has_event_type(dev, type) &&
762 libevdev_has_event_code(dev, type, code)) {
763 *value = libevdev_get_event_value(dev, type, code);
770 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
772 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
775 if (slot >= dev->num_slots || slot >= MAX_SLOTS)
778 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
781 return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
785 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
787 if (libevdev_has_event_type(dev, EV_ABS) &&
788 libevdev_has_event_code(dev, EV_ABS, code) &&
789 slot < dev->num_slots && slot < MAX_SLOTS) {
790 *value = libevdev_get_slot_value(dev, slot, code);
797 libevdev_get_num_slots(const struct libevdev *dev)
799 return dev->num_slots;
803 libevdev_get_current_slot(const struct libevdev *dev)
805 return dev->current_slot;
808 const struct input_absinfo*
809 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
811 if (!libevdev_has_event_type(dev, EV_ABS) ||
812 !libevdev_has_event_code(dev, EV_ABS, code))
815 return &dev->abs_info[code];
818 #define ABS_GETTER(name, field) \
819 int libevdev_get_abs_##name(const struct libevdev *dev, unsigned int code) \
821 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code); \
822 return absinfo ? absinfo->field : 0; \
825 ABS_GETTER(max, maximum); /* DEPRECATED */
826 ABS_GETTER(min, minimum); /* DEPRECATED */
827 ABS_GETTER(maximum, maximum);
828 ABS_GETTER(minimum, minimum);
829 ABS_GETTER(fuzz, fuzz)
830 ABS_GETTER(flat, flat)
831 ABS_GETTER(resolution, resolution)
833 #define ABS_SETTER(field) \
834 void libevdev_set_abs_##field(struct libevdev *dev, unsigned int code, int val) \
836 if (!libevdev_has_event_code(dev, EV_ABS, code)) \
838 dev->abs_info[code].field = val; \
845 ABS_SETTER(resolution)
847 void libevdev_set_abs_info(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
849 if (!libevdev_has_event_code(dev, EV_ABS, code))
852 dev->abs_info[code] = *abs;
856 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
861 if (libevdev_has_event_type(dev, type))
864 set_bit(dev->bits, type);
866 if (type == EV_REP) {
867 int delay = 0, period = 0;
868 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
869 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
875 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
877 if (type > EV_MAX || type == EV_SYN)
880 clear_bit(dev->bits, type);
886 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
887 unsigned int code, const void *data)
892 if (libevdev_enable_event_type(dev, type))
909 max = type_to_mask(dev, type, &mask);
916 if (type == EV_ABS) {
917 const struct input_absinfo *abs = data;
918 dev->abs_info[code] = *abs;
919 } else if (type == EV_REP) {
920 const int *value = data;
921 dev->rep_values[code] = *value;
928 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
936 max = type_to_mask(dev, type, &mask);
941 clear_bit(mask, code);
947 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
954 rc = ioctl(dev->fd, EVIOCSABS(code), abs);
958 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
964 libevdev_grab(struct libevdev *dev, int grab)
968 if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB)
971 if (grab == dev->grabbed)
974 if (grab == LIBEVDEV_GRAB)
975 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
976 else if (grab == LIBEVDEV_UNGRAB)
977 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
982 return rc < 0 ? -errno : 0;
986 libevdev_is_event_type(const struct input_event *ev, unsigned int type)
988 return type < EV_MAX && ev->type == type;
992 libevdev_is_event_code(const struct input_event *ev, unsigned int type, unsigned int code)
994 return type < EV_MAX &&
996 (type == EV_SYN || code <= libevdev_get_event_type_max(type)) &&
1001 libevdev_get_event_type_name(unsigned int type)
1006 return ev_map[type];
1010 libevdev_get_event_code_name(unsigned int type, unsigned int code)
1015 if (code > ev_max[type])
1018 return event_type_map[type][code];
1023 libevdev_get_input_prop_name(unsigned int prop)
1025 return libevdev_get_property_name(prop);
1029 libevdev_get_property_name(unsigned int prop)
1031 if (prop > INPUT_PROP_MAX)
1034 return input_prop_map[prop];
1038 libevdev_get_event_type_max(unsigned int type)
1043 return ev_max[type];
1047 libevdev_get_repeat(struct libevdev *dev, int *delay, int *period)
1049 if (!libevdev_has_event_type(dev, EV_REP))
1053 *delay = dev->rep_values[REP_DELAY];
1055 *period = dev->rep_values[REP_PERIOD];