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
29 #include <linux/uinput.h>
32 #include "libevdev-int.h"
33 #include "libevdev-util.h"
34 #include "event-names.h"
39 init_event_queue(struct libevdev *dev)
41 /* FIXME: count the number of axes, keys, etc. to get a better idea at how many events per
42 EV_SYN we could possibly get. Then multiply that by the actual buffer size we care about */
44 const int QUEUE_SIZE = 256;
46 return queue_alloc(dev, QUEUE_SIZE);
50 _libevdev_log(struct libevdev *dev, const char *format, ...)
54 va_start(args, format);
55 dev->log(format, args);
60 libevdev_noop_log_func(const char *format, va_list args)
69 dev = calloc(1, sizeof(*dev));
74 dev->current_slot = -1;
75 dev->log = libevdev_noop_log_func;
76 dev->grabbed = LIBEVDEV_UNGRAB;
82 libevdev_new_from_fd(int fd, struct libevdev **dev)
91 rc = libevdev_set_fd(d, fd);
100 libevdev_free(struct libevdev *dev)
113 libevdev_set_log_handler(struct libevdev *dev, libevdev_log_func_t logfunc)
118 dev->log = logfunc ? logfunc : libevdev_noop_log_func;
122 libevdev_change_fd(struct libevdev *dev, int fd)
131 libevdev_set_fd(struct libevdev* dev, int fd)
140 rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
144 memset(buf, 0, sizeof(buf));
145 rc = ioctl(fd, EVIOCGNAME(sizeof(buf) - 1), buf);
149 dev->name = strdup(buf);
155 memset(buf, 0, sizeof(buf));
156 rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
158 /* uinput has no phys */
162 dev->phys = strdup(buf);
169 memset(buf, 0, sizeof(buf));
170 rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
175 dev->uniq = strdup(buf);
182 rc = ioctl(fd, EVIOCGID, &dev->ids);
186 rc = ioctl(fd, EVIOCGVERSION, &dev->driver_version);
190 rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
194 rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
198 rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
202 rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
206 rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
210 rc = ioctl(fd, EVIOCGBIT(EV_SW, sizeof(dev->sw_bits)), dev->sw_bits);
214 rc = ioctl(fd, EVIOCGBIT(EV_MSC, sizeof(dev->msc_bits)), dev->msc_bits);
218 rc = ioctl(fd, EVIOCGBIT(EV_FF, sizeof(dev->ff_bits)), dev->ff_bits);
222 rc = ioctl(fd, EVIOCGBIT(EV_SND, sizeof(dev->snd_bits)), dev->snd_bits);
226 /* rep is a special case, always set it to 1 for both values if EV_REP is set */
227 if (bit_is_set(dev->bits, EV_REP)) {
228 for (i = 0; i < REP_MAX; i++)
229 set_bit(dev->rep_bits, i);
230 rc = ioctl(fd, EVIOCGREP, dev->rep_values);
235 for (i = ABS_X; i <= ABS_MAX; i++) {
236 if (bit_is_set(dev->abs_bits, i)) {
237 struct input_absinfo abs_info;
238 rc = ioctl(fd, EVIOCGABS(i), &abs_info);
242 dev->abs_info[i] = abs_info;
243 if (i == ABS_MT_SLOT) {
244 dev->num_slots = abs_info.maximum + 1;
245 dev->current_slot = abs_info.value;
251 rc = init_event_queue(dev);
255 /* not copying key state because we won't know when we'll start to
256 * use this fd and key's are likely to change state by then.
257 * Same with the valuators, really, but they may not change.
263 return rc ? -errno : 0;
267 libevdev_get_fd(const struct libevdev* dev)
273 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
275 ev->time = dev->last_event_time;
282 sync_key_state(struct libevdev *dev)
286 unsigned long keystate[NLONGS(KEY_MAX)];
288 rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
292 for (i = 0; i < KEY_MAX; i++) {
294 old = bit_is_set(dev->key_values, i);
295 new = bit_is_set(keystate, i);
297 struct input_event *ev = queue_push(dev);
298 init_event(dev, ev, EV_KEY, i, new ? 1 : 0);
300 set_bit_state(dev->key_values, i, new);
305 return rc ? -errno : 0;
309 sync_abs_state(struct libevdev *dev)
314 for (i = ABS_X; i <= ABS_MAX; i++) {
315 struct input_absinfo abs_info;
317 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
320 if (!bit_is_set(dev->abs_bits, i))
323 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
327 if (dev->abs_info[i].value != abs_info.value) {
328 struct input_event *ev = queue_push(dev);
330 init_event(dev, ev, EV_ABS, i, abs_info.value);
331 dev->abs_info[i].value = abs_info.value;
337 return rc ? -errno : 0;
341 sync_mt_state(struct libevdev *dev)
348 } mt_state[ABS_MT_CNT];
350 for (i = ABS_MT_MIN; i < ABS_MT_MAX; i++) {
352 if (i == ABS_MT_SLOT)
355 idx = i - ABS_MT_MIN;
356 mt_state[idx].code = i;
357 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
362 for (i = 0; i < dev->num_slots; i++) {
364 struct input_event *ev;
366 ev = queue_push(dev);
367 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, i);
368 for (j = ABS_MT_MIN; j < ABS_MT_MAX; j++) {
369 int jdx = j - ABS_MT_MIN;
371 if (j == ABS_MT_SLOT)
374 if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
377 ev = queue_push(dev);
378 init_event(dev, ev, EV_ABS, j, mt_state[jdx].val[i]);
379 dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
385 return rc ? -errno : 0;
389 sync_state(struct libevdev *dev)
393 struct input_event *ev;
395 /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
396 queue[0]) we need to shift this backwards. Except that chances are that the
397 queue may be either full or too full to prepend all the events needed for
400 so we search for the last sync event in the queue and drop everything before
401 including that event and rely on the kernel to tell us the right value for that
402 bitfield during the sync process.
405 for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
406 struct input_event e;
407 queue_peek(dev, i, &e);
408 if (e.type == EV_SYN)
413 queue_shift_multiple(dev, i + 1, NULL);
415 if (libevdev_has_event_type(dev, EV_KEY))
416 rc = sync_key_state(dev);
417 if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
418 rc = sync_abs_state(dev);
419 if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
420 rc = sync_mt_state(dev);
422 ev = queue_push(dev);
423 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
425 dev->queue_nsync = queue_num_elements(dev);
432 update_key_state(struct libevdev *dev, const struct input_event *e)
434 if (!libevdev_has_event_type(dev, EV_KEY))
437 if (e->code > KEY_MAX)
441 clear_bit(dev->key_values, e->code);
443 set_bit(dev->key_values, e->code);
449 update_mt_state(struct libevdev *dev, const struct input_event *e)
451 if (e->code == ABS_MT_SLOT) {
452 dev->current_slot = e->value;
454 } else if (dev->current_slot == -1)
457 dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
463 update_abs_state(struct libevdev *dev, const struct input_event *e)
465 if (!libevdev_has_event_type(dev, EV_ABS))
468 if (e->code > ABS_MAX)
471 if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
472 return update_mt_state(dev, e);
474 dev->abs_info[e->code].value = e->value;
480 update_state(struct libevdev *dev, const struct input_event *e)
489 rc = update_key_state(dev, e);
492 rc = update_abs_state(dev, e);
496 dev->last_event_time = e->time;
502 read_more_events(struct libevdev *dev)
506 struct input_event *next;
508 free_elem = queue_num_free_elements(dev);
512 next = queue_next_element(dev);
513 len = read(dev->fd, next, free_elem * sizeof(struct input_event));
516 } else if (len > 0 && len % sizeof(struct input_event) != 0)
519 int nev = len/sizeof(struct input_event);
520 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
526 int libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
533 if (!(flags & (LIBEVDEV_READ_NORMAL|LIBEVDEV_READ_SYNC)))
536 if (flags & LIBEVDEV_READ_SYNC) {
537 if (!dev->need_sync && dev->queue_nsync == 0)
539 else if (dev->need_sync) {
540 rc = sync_state(dev);
544 } else if (dev->need_sync) {
545 /* FIXME: still need to call update_state for all events
546 * here, otherwise the library has the wrong view of the
548 queue_shift_multiple(dev, dev->queue_nsync, NULL);
551 /* FIXME: check for O_NONBLOCK and if not set, skip if we have an
552 * event in the queue from the previous read.
555 /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
556 worst case we don't read fast enough and end up with SYN_DROPPED anyway */
557 rc = read_more_events(dev);
558 if (rc < 0 && rc != -EAGAIN)
561 if (queue_shift(dev, ev) != 0)
564 update_state(dev, ev);
567 if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
572 if (flags & LIBEVDEV_READ_SYNC && dev->queue_nsync > 0) {
582 libevdev_get_name(const struct libevdev *dev)
584 return dev->name ? dev->name : "";
588 libevdev_get_phys(const struct libevdev *dev)
594 libevdev_get_uniq(const struct libevdev *dev)
599 int libevdev_get_product_id(const struct libevdev *dev)
601 return dev->ids.product;
604 int libevdev_get_vendor_id(const struct libevdev *dev)
606 return dev->ids.vendor;
609 int libevdev_get_bustype(const struct libevdev *dev)
611 return dev->ids.bustype;
614 int libevdev_get_version(const struct libevdev *dev)
616 return dev->ids.version;
619 int libevdev_get_driver_version(const struct libevdev *dev)
621 return dev->driver_version;
625 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
627 return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
631 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
633 return (type <= EV_MAX) && bit_is_set(dev->bits, type);
637 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
639 const unsigned long *mask;
642 if (!libevdev_has_event_type(dev, type))
648 max = type_to_mask_const(dev, type, &mask);
650 if (max == -1 || code > max)
653 return bit_is_set(mask, code);
657 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
661 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
665 case EV_ABS: value = dev->abs_info[code].value; break;
666 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
676 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
678 if (libevdev_has_event_type(dev, type) &&
679 libevdev_has_event_code(dev, type, code)) {
680 *value = libevdev_get_event_value(dev, type, code);
687 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
689 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
692 if (slot >= dev->num_slots || slot >= MAX_SLOTS)
695 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
698 return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
702 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
704 if (libevdev_has_event_type(dev, EV_ABS) &&
705 libevdev_has_event_code(dev, EV_ABS, code) &&
706 slot < dev->num_slots && slot < MAX_SLOTS) {
707 *value = libevdev_get_slot_value(dev, slot, code);
714 libevdev_get_num_slots(const struct libevdev *dev)
716 return dev->num_slots;
720 libevdev_get_current_slot(const struct libevdev *dev)
722 return dev->current_slot;
725 const struct input_absinfo*
726 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
728 if (!libevdev_has_event_type(dev, EV_ABS) ||
729 !libevdev_has_event_code(dev, EV_ABS, code))
732 return &dev->abs_info[code];
736 libevdev_get_abs_min(const struct libevdev *dev, unsigned int code)
738 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
740 return absinfo ? absinfo->minimum : 0;
744 libevdev_get_abs_max(const struct libevdev *dev, unsigned int code)
746 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
748 return absinfo ? absinfo->maximum : 0;
752 libevdev_get_abs_fuzz(const struct libevdev *dev, unsigned int code)
754 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
756 return absinfo ? absinfo->fuzz : 0;
760 libevdev_get_abs_flat(const struct libevdev *dev, unsigned int code)
762 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
764 return absinfo ? absinfo->flat : 0;
768 libevdev_get_abs_resolution(const struct libevdev *dev, unsigned int code)
770 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
772 return absinfo ? absinfo->resolution : 0;
776 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
781 set_bit(dev->bits, type);
787 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
792 clear_bit(dev->bits, type);
798 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
799 unsigned int code, const void *data)
804 if (libevdev_enable_event_type(dev, type))
807 max = type_to_mask(dev, type, &mask);
814 if (type == EV_ABS) {
815 const struct input_absinfo *abs = data;
816 dev->abs_info[code] = *abs;
823 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
831 max = type_to_mask(dev, type, &mask);
836 clear_bit(mask, code);
842 libevdev_kernel_enable_event_type(struct libevdev *dev, unsigned int type)
849 rc = ioctl(dev->fd, UI_SET_EVBIT, type);
851 libevdev_enable_event_type(dev, type);
853 return (rc != -1) ? 0 : -errno;
857 libevdev_kernel_enable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
862 const unsigned long *mask;
864 rc = libevdev_kernel_enable_event_type(dev, type);
868 max = type_to_mask_const(dev, type, &mask);
869 if (max == -1 || code > max)
873 case EV_KEY: uinput_bit = UI_SET_KEYBIT; break;
874 case EV_REL: uinput_bit = UI_SET_RELBIT; break;
875 case EV_ABS: uinput_bit = UI_SET_ABSBIT; break;
876 case EV_MSC: uinput_bit = UI_SET_MSCBIT; break;
877 case EV_LED: uinput_bit = UI_SET_LEDBIT; break;
878 case EV_SND: uinput_bit = UI_SET_SNDBIT; break;
879 case EV_FF: uinput_bit = UI_SET_FFBIT; break;
880 case EV_SW: uinput_bit = UI_SET_SWBIT; break;
883 rc = ioctl(dev->fd, uinput_bit, type);
885 libevdev_enable_event_type(dev, type);
887 return (rc != -1) ? 0 : -errno;
891 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
898 rc = ioctl(dev->fd, EVIOCSABS(code), *abs);
902 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
908 libevdev_grab(struct libevdev *dev, int grab)
912 if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB)
915 if (grab == dev->grabbed)
918 if (grab == LIBEVDEV_GRAB)
919 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
920 else if (grab == LIBEVDEV_UNGRAB)
921 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
926 return rc < 0 ? -errno : 0;
930 libevdev_get_event_type_name(unsigned int type)
939 libevdev_get_event_code_name(unsigned int type, unsigned int code)
944 if (code > ev_max[type])
947 return event_type_map[type][code];
951 libevdev_get_input_prop_name(unsigned int prop)
953 if (prop > INPUT_PROP_MAX)
956 return input_prop_map[prop];
960 libevdev_get_event_type_max(unsigned int type)
969 libevdev_get_repeat(struct libevdev *dev, int *delay, int *period)
971 if (!libevdev_has_event_type(dev, EV_REP))
975 *delay = dev->rep_values[REP_DELAY];
977 *period = dev->rep_values[REP_PERIOD];