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);
153 dev->name = strdup(buf);
159 memset(buf, 0, sizeof(buf));
160 rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
162 /* uinput has no phys */
166 dev->phys = strdup(buf);
173 memset(buf, 0, sizeof(buf));
174 rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
179 dev->uniq = strdup(buf);
186 rc = ioctl(fd, EVIOCGID, &dev->ids);
190 rc = ioctl(fd, EVIOCGVERSION, &dev->driver_version);
194 rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
198 rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
202 rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
206 rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
210 rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
214 rc = ioctl(fd, EVIOCGBIT(EV_SW, sizeof(dev->sw_bits)), dev->sw_bits);
218 rc = ioctl(fd, EVIOCGBIT(EV_MSC, sizeof(dev->msc_bits)), dev->msc_bits);
222 rc = ioctl(fd, EVIOCGBIT(EV_FF, sizeof(dev->ff_bits)), dev->ff_bits);
226 rc = ioctl(fd, EVIOCGBIT(EV_SND, sizeof(dev->snd_bits)), dev->snd_bits);
230 /* rep is a special case, always set it to 1 for both values if EV_REP is set */
231 if (bit_is_set(dev->bits, EV_REP)) {
232 for (i = 0; i < REP_CNT; i++)
233 set_bit(dev->rep_bits, i);
234 rc = ioctl(fd, EVIOCGREP, dev->rep_values);
239 for (i = ABS_X; i <= ABS_MAX; i++) {
240 if (bit_is_set(dev->abs_bits, i)) {
241 struct input_absinfo abs_info;
242 rc = ioctl(fd, EVIOCGABS(i), &abs_info);
246 dev->abs_info[i] = abs_info;
247 if (i == ABS_MT_SLOT) {
248 dev->num_slots = abs_info.maximum + 1;
249 dev->current_slot = abs_info.value;
256 sync_mt_state(dev, 0);
258 rc = init_event_queue(dev);
264 /* not copying key state because we won't know when we'll start to
265 * use this fd and key's are likely to change state by then.
266 * Same with the valuators, really, but they may not change.
270 return rc ? -errno : 0;
274 libevdev_get_fd(const struct libevdev* dev)
280 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
282 ev->time = dev->last_event_time;
289 sync_key_state(struct libevdev *dev)
293 unsigned long keystate[NLONGS(KEY_MAX)];
295 rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
299 for (i = 0; i < KEY_MAX; i++) {
301 old = bit_is_set(dev->key_values, i);
302 new = bit_is_set(keystate, i);
304 struct input_event *ev = queue_push(dev);
305 init_event(dev, ev, EV_KEY, i, new ? 1 : 0);
307 set_bit_state(dev->key_values, i, new);
312 return rc ? -errno : 0;
316 sync_abs_state(struct libevdev *dev)
321 for (i = ABS_X; i <= ABS_MAX; i++) {
322 struct input_absinfo abs_info;
324 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
327 if (!bit_is_set(dev->abs_bits, i))
330 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
334 if (dev->abs_info[i].value != abs_info.value) {
335 struct input_event *ev = queue_push(dev);
337 init_event(dev, ev, EV_ABS, i, abs_info.value);
338 dev->abs_info[i].value = abs_info.value;
344 return rc ? -errno : 0;
348 sync_mt_state(struct libevdev *dev, int create_events)
355 } mt_state[ABS_MT_CNT];
357 for (i = ABS_MT_MIN; i < ABS_MT_MAX; i++) {
359 if (i == ABS_MT_SLOT)
362 if (!libevdev_has_event_code(dev, EV_ABS, i))
365 idx = i - ABS_MT_MIN;
366 mt_state[idx].code = i;
367 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
372 for (i = 0; i < dev->num_slots; i++) {
374 struct input_event *ev;
377 ev = queue_push(dev);
378 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, i);
381 for (j = ABS_MT_MIN; j < ABS_MT_MAX; j++) {
382 int jdx = j - ABS_MT_MIN;
384 if (j == ABS_MT_SLOT)
387 if (!libevdev_has_event_code(dev, EV_ABS, j))
390 if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
394 ev = queue_push(dev);
395 init_event(dev, ev, EV_ABS, j, mt_state[jdx].val[i]);
397 dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
403 return rc ? -errno : 0;
407 sync_state(struct libevdev *dev)
411 struct input_event *ev;
413 /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
414 queue[0]) we need to shift this backwards. Except that chances are that the
415 queue may be either full or too full to prepend all the events needed for
418 so we search for the last sync event in the queue and drop everything before
419 including that event and rely on the kernel to tell us the right value for that
420 bitfield during the sync process.
423 for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
424 struct input_event e;
425 queue_peek(dev, i, &e);
426 if (e.type == EV_SYN)
431 queue_shift_multiple(dev, i + 1, NULL);
433 if (libevdev_has_event_type(dev, EV_KEY))
434 rc = sync_key_state(dev);
435 if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
436 rc = sync_abs_state(dev);
437 if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
438 rc = sync_mt_state(dev, 1);
440 dev->queue_nsync = queue_num_elements(dev);
442 if (dev->queue_nsync > 0) {
443 ev = queue_push(dev);
444 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
452 update_key_state(struct libevdev *dev, const struct input_event *e)
454 if (!libevdev_has_event_type(dev, EV_KEY))
457 if (e->code > KEY_MAX)
461 clear_bit(dev->key_values, e->code);
463 set_bit(dev->key_values, e->code);
469 update_mt_state(struct libevdev *dev, const struct input_event *e)
471 if (e->code == ABS_MT_SLOT) {
472 dev->current_slot = e->value;
474 } else if (dev->current_slot == -1)
477 dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
483 update_abs_state(struct libevdev *dev, const struct input_event *e)
485 if (!libevdev_has_event_type(dev, EV_ABS))
488 if (e->code > ABS_MAX)
491 if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
492 return update_mt_state(dev, e);
494 dev->abs_info[e->code].value = e->value;
500 update_state(struct libevdev *dev, const struct input_event *e)
509 rc = update_key_state(dev, e);
512 rc = update_abs_state(dev, e);
516 dev->last_event_time = e->time;
522 read_more_events(struct libevdev *dev)
526 struct input_event *next;
528 free_elem = queue_num_free_elements(dev);
532 next = queue_next_element(dev);
533 len = read(dev->fd, next, free_elem * sizeof(struct input_event));
536 } else if (len > 0 && len % sizeof(struct input_event) != 0)
539 int nev = len/sizeof(struct input_event);
540 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
546 int libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
553 if (!(flags & (LIBEVDEV_READ_NORMAL|LIBEVDEV_READ_SYNC|LIBEVDEV_FORCE_SYNC)))
556 if (flags & LIBEVDEV_READ_SYNC) {
557 if (dev->sync_state == SYNC_NEEDED) {
558 rc = sync_state(dev);
561 dev->sync_state = SYNC_IN_PROGRESS;
564 if (dev->queue_nsync == 0) {
565 dev->sync_state = SYNC_NONE;
569 } else if (dev->sync_state != SYNC_NONE) {
570 struct input_event e;
572 /* call update_state for all events here, otherwise the library has the wrong view
574 while (queue_shift(dev, &e) == 0) {
576 update_state(dev, &e);
579 dev->sync_state = SYNC_NONE;
582 /* FIXME: if the first event after SYNC_IN_PROGRESS is a SYN_DROPPED, log this */
584 /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
585 worst case we don't read fast enough and end up with SYN_DROPPED anyway.
587 Except if the fd is in blocking mode and we still have events from the last read, don't
591 if (!(flags & LIBEVDEV_READ_BLOCKING) ||
592 queue_num_elements(dev) == 0) {
593 rc = read_more_events(dev);
594 if (rc < 0 && rc != -EAGAIN)
598 if (flags & LIBEVDEV_FORCE_SYNC) {
599 dev->sync_state = SYNC_NEEDED;
605 if (queue_shift(dev, ev) != 0)
608 update_state(dev, ev);
610 /* if we disabled a code, get the next event instead */
611 } while(!libevdev_has_event_code(dev, ev->type, ev->code));
614 if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
615 dev->sync_state = SYNC_NEEDED;
619 if (flags & LIBEVDEV_READ_SYNC && dev->queue_nsync > 0) {
622 if (dev->queue_nsync == 0)
623 dev->sync_state = SYNC_NONE;
630 int libevdev_has_event_pending(struct libevdev *dev)
632 struct pollfd fds = { dev->fd, POLLIN, 0 };
638 if (queue_num_elements(dev) != 0)
641 rc = poll(&fds, 1, 0);
642 return (rc >= 0) ? rc : -errno;
646 libevdev_get_name(const struct libevdev *dev)
648 return dev->name ? dev->name : "";
652 libevdev_get_phys(const struct libevdev *dev)
658 libevdev_get_uniq(const struct libevdev *dev)
663 int libevdev_get_product_id(const struct libevdev *dev)
665 return dev->ids.product;
668 int libevdev_get_vendor_id(const struct libevdev *dev)
670 return dev->ids.vendor;
673 int libevdev_get_bustype(const struct libevdev *dev)
675 return dev->ids.bustype;
678 int libevdev_get_version(const struct libevdev *dev)
680 return dev->ids.version;
683 int libevdev_get_driver_version(const struct libevdev *dev)
685 return dev->driver_version;
689 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
691 return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
695 libevdev_enable_property(struct libevdev *dev, unsigned int prop)
697 if (prop > INPUT_PROP_MAX)
700 set_bit(dev->props, prop);
705 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
707 return (type <= EV_MAX) && bit_is_set(dev->bits, type);
711 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
713 const unsigned long *mask;
716 if (!libevdev_has_event_type(dev, type))
722 max = type_to_mask_const(dev, type, &mask);
724 if (max == -1 || code > max)
727 return bit_is_set(mask, code);
731 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
735 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
739 case EV_ABS: value = dev->abs_info[code].value; break;
740 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
750 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
752 if (libevdev_has_event_type(dev, type) &&
753 libevdev_has_event_code(dev, type, code)) {
754 *value = libevdev_get_event_value(dev, type, code);
761 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
763 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
766 if (slot >= dev->num_slots || slot >= MAX_SLOTS)
769 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
772 return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
776 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
778 if (libevdev_has_event_type(dev, EV_ABS) &&
779 libevdev_has_event_code(dev, EV_ABS, code) &&
780 slot < dev->num_slots && slot < MAX_SLOTS) {
781 *value = libevdev_get_slot_value(dev, slot, code);
788 libevdev_get_num_slots(const struct libevdev *dev)
790 return dev->num_slots;
794 libevdev_get_current_slot(const struct libevdev *dev)
796 return dev->current_slot;
799 const struct input_absinfo*
800 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
802 if (!libevdev_has_event_type(dev, EV_ABS) ||
803 !libevdev_has_event_code(dev, EV_ABS, code))
806 return &dev->abs_info[code];
810 libevdev_get_abs_min(const struct libevdev *dev, unsigned int code)
812 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
814 return absinfo ? absinfo->minimum : 0;
818 libevdev_get_abs_max(const struct libevdev *dev, unsigned int code)
820 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
822 return absinfo ? absinfo->maximum : 0;
826 libevdev_get_abs_fuzz(const struct libevdev *dev, unsigned int code)
828 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
830 return absinfo ? absinfo->fuzz : 0;
834 libevdev_get_abs_flat(const struct libevdev *dev, unsigned int code)
836 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
838 return absinfo ? absinfo->flat : 0;
842 libevdev_get_abs_resolution(const struct libevdev *dev, unsigned int code)
844 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
846 return absinfo ? absinfo->resolution : 0;
850 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
855 if (libevdev_has_event_type(dev, type))
858 set_bit(dev->bits, type);
860 if (type == EV_REP) {
861 int delay = 0, period = 0;
862 libevdev_enable_event_code(dev, EV_REP, REP_DELAY, &delay);
863 libevdev_enable_event_code(dev, EV_REP, REP_PERIOD, &period);
869 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
871 if (type > EV_MAX || type == EV_SYN)
874 clear_bit(dev->bits, type);
880 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
881 unsigned int code, const void *data)
886 if (libevdev_enable_event_type(dev, type))
903 max = type_to_mask(dev, type, &mask);
910 if (type == EV_ABS) {
911 const struct input_absinfo *abs = data;
912 dev->abs_info[code] = *abs;
913 } else if (type == EV_REP) {
914 const int *value = data;
915 dev->rep_values[code] = *value;
922 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
930 max = type_to_mask(dev, type, &mask);
935 clear_bit(mask, code);
941 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
948 rc = ioctl(dev->fd, EVIOCSABS(code), abs);
952 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
958 libevdev_grab(struct libevdev *dev, int grab)
962 if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB)
965 if (grab == dev->grabbed)
968 if (grab == LIBEVDEV_GRAB)
969 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
970 else if (grab == LIBEVDEV_UNGRAB)
971 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
976 return rc < 0 ? -errno : 0;
980 libevdev_is_event_type(const struct input_event *ev, unsigned int type)
982 return type < EV_MAX && ev->type == type;
986 libevdev_is_event_code(const struct input_event *ev, unsigned int type, unsigned int code)
988 return type < EV_MAX &&
990 (type == EV_SYN || code <= libevdev_get_event_type_max(type)) &&
995 libevdev_get_event_type_name(unsigned int type)
1000 return ev_map[type];
1004 libevdev_get_event_code_name(unsigned int type, unsigned int code)
1009 if (code > ev_max[type])
1012 return event_type_map[type][code];
1016 libevdev_get_input_prop_name(unsigned int prop)
1018 if (prop > INPUT_PROP_MAX)
1021 return input_prop_map[prop];
1025 libevdev_get_event_type_max(unsigned int type)
1030 return ev_max[type];
1034 libevdev_get_repeat(struct libevdev *dev, int *delay, int *period)
1036 if (!libevdev_has_event_type(dev, EV_REP))
1040 *delay = dev->rep_values[REP_DELAY];
1042 *period = dev->rep_values[REP_PERIOD];