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
31 #include "libevdev-int.h"
36 bit_is_set(const unsigned long *array, int bit)
38 return !!(array[bit / LONG_BITS] & (1LL << (bit % LONG_BITS)));
42 set_bit(unsigned long *array, int bit)
44 array[bit / LONG_BITS] |= (1LL << (bit % LONG_BITS));
48 clear_bit(unsigned long *array, int bit)
50 array[bit / LONG_BITS] &= ~(1LL << (bit % LONG_BITS));
54 set_bit_state(unsigned long *array, int bit, int state)
59 clear_bit(array, bit);
63 type_to_mask_const(const struct libevdev *dev, unsigned int type, const unsigned long **mask)
69 *mask = dev->abs_bits;
73 *mask = dev->rel_bits;
77 *mask = dev->key_bits;
81 *mask = dev->led_bits;
92 type_to_mask(struct libevdev *dev, unsigned int type, unsigned long **mask)
98 *mask = dev->abs_bits;
102 *mask = dev->rel_bits;
106 *mask = dev->key_bits;
110 *mask = dev->led_bits;
121 init_event_queue(struct libevdev *dev)
123 /* FIXME: count the number of axes, keys, etc. to get a better idea at how many events per
124 EV_SYN we could possibly get. Then multiply that by the actual buffer size we care about */
126 const int QUEUE_SIZE = 256;
128 return queue_alloc(dev, QUEUE_SIZE);
132 _libevdev_log(struct libevdev *dev, const char *format, ...)
136 va_start(args, format);
137 dev->log(format, args);
142 libevdev_noop_log_func(const char *format, va_list args)
149 struct libevdev *dev;
151 dev = calloc(1, sizeof(*dev));
156 dev->current_slot = -1;
157 dev->log = libevdev_noop_log_func;
163 libevdev_new_from_fd(int fd, struct libevdev **dev)
172 rc = libevdev_set_fd(d, fd);
181 libevdev_free(struct libevdev *dev)
191 libevdev_set_log_handler(struct libevdev *dev, libevdev_log_func_t logfunc)
193 dev->log = logfunc ? logfunc : libevdev_noop_log_func;
197 libevdev_change_fd(struct libevdev *dev, int fd)
206 libevdev_set_fd(struct libevdev* dev, int fd)
215 rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
219 memset(buf, 0, sizeof(buf));
220 rc = ioctl(fd, EVIOCGNAME(sizeof(buf) - 1), buf);
224 dev->name = calloc(strlen(buf) + 1, sizeof(char));
229 strcpy(dev->name, buf);
231 memset(buf, 0, sizeof(buf));
232 rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
234 /* uinput has no phys */
238 dev->phys = calloc(strlen(buf) + 1, sizeof(char));
243 strcpy(dev->phys, buf);
246 memset(buf, 0, sizeof(buf));
247 rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
252 dev->uniq = calloc(strlen(buf) + 1, sizeof(char));
257 strcpy(dev->uniq, buf);
260 rc = ioctl(fd, EVIOCGID, &dev->ids);
264 rc = ioctl(fd, EVIOCGVERSION, &dev->driver_version);
268 rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
272 rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
276 rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
280 rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
284 rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
288 for (i = ABS_X; i <= ABS_MAX; i++) {
289 if (bit_is_set(dev->abs_bits, i)) {
290 struct input_absinfo abs_info;
291 rc = ioctl(fd, EVIOCGABS(i), &dev->abs_info[i]);
295 if (i == ABS_MT_SLOT) {
296 dev->num_slots = abs_info.maximum + 1; /* FIXME: non-zero min? */
297 dev->current_slot = abs_info.value;
303 rc = init_event_queue(dev);
307 /* not copying key state because we won't know when we'll start to
308 * use this fd and key's are likely to change state by then.
309 * Same with the valuators, really, but they may not change.
315 return rc ? -errno : 0;
319 libevdev_get_fd(const struct libevdev* dev)
325 init_event(struct input_event *ev, int type, int code, int value)
327 ev->time.tv_sec = 0; /* FIXME: blah! */
328 ev->time.tv_usec = 0; /* FIXME: blah! */
335 sync_key_state(struct libevdev *dev)
339 unsigned long keystate[NLONGS(KEY_MAX)];
341 rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
345 for (i = 0; i < KEY_MAX; i++) {
347 old = bit_is_set(dev->key_values, i);
348 new = bit_is_set(keystate, i);
350 struct input_event *ev = queue_push(dev);
351 init_event(ev, EV_KEY, i, new ? 1 : 0);
353 set_bit_state(dev->key_values, i, new);
358 return rc ? -errno : 0;
362 sync_abs_state(struct libevdev *dev)
367 for (i = ABS_X; i <= ABS_MAX; i++) {
368 struct input_absinfo abs_info;
370 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
373 if (!bit_is_set(dev->abs_bits, i))
376 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
380 if (dev->abs_info[i].value != abs_info.value) {
381 struct input_event *ev = queue_push(dev);
383 init_event(ev, EV_ABS, i, abs_info.value);
384 dev->abs_info[i].value = abs_info.value;
390 return rc ? -errno : 0;
394 sync_mt_state(struct libevdev *dev)
401 } mt_state[ABS_MT_CNT];
403 for (i = ABS_MT_MIN; i < ABS_MT_MAX; i++) {
405 if (i == ABS_MT_SLOT)
408 idx = i - ABS_MT_MIN;
409 mt_state[idx].code = i;
410 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
415 for (i = 0; i < dev->num_slots; i++) {
417 struct input_event *ev;
419 ev = queue_push(dev);
420 init_event(ev, EV_ABS, ABS_MT_SLOT, i);
421 for (j = ABS_MT_MIN; j < ABS_MT_MAX; j++) {
422 int jdx = j - ABS_MT_MIN;
424 if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
427 ev = queue_push(dev);
428 init_event(ev, EV_ABS, j, mt_state[jdx].val[i]);
429 dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
435 return rc ? -errno : 0;
439 sync_state(struct libevdev *dev)
443 struct input_event *ev;
445 /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
446 queue[0]) we need to shift this backwards. Except that chances are that the
447 queue may be either full or too full to prepend all the events needed for
450 so we search for the last sync event in the queue and drop everything before
451 including that event and rely on the kernel to tell us the right value for that
452 bitfield during the sync process.
455 for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
456 struct input_event e;
457 queue_peek(dev, i, &e);
458 if (e.type == EV_SYN)
463 queue_shift_multiple(dev, i + 1, NULL);
465 if (libevdev_has_event_type(dev, EV_KEY))
466 rc = sync_key_state(dev);
467 if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
468 rc = sync_abs_state(dev);
469 if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
470 rc = sync_mt_state(dev);
472 ev = queue_push(dev);
473 init_event(ev, EV_SYN, SYN_REPORT, 0);
475 dev->queue_nsync = queue_num_elements(dev);
482 update_key_state(struct libevdev *dev, const struct input_event *e)
484 if (!libevdev_has_event_type(dev, EV_KEY))
487 if (e->code > KEY_MAX)
491 clear_bit(dev->key_values, e->code);
493 set_bit(dev->key_values, e->code);
499 update_mt_state(struct libevdev *dev, const struct input_event *e)
501 if (e->code == ABS_MT_SLOT) {
502 dev->current_slot = e->value;
504 } else if (dev->current_slot == -1)
507 dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
513 update_abs_state(struct libevdev *dev, const struct input_event *e)
515 if (!libevdev_has_event_type(dev, EV_ABS))
518 if (e->code > ABS_MAX)
521 if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
522 return update_mt_state(dev, e);
524 dev->abs_info[e->code].value = e->value;
530 update_state(struct libevdev *dev, const struct input_event *e)
539 rc = update_key_state(dev, e);
542 rc = update_abs_state(dev, e);
550 read_more_events(struct libevdev *dev)
554 struct input_event *next;
556 free_elem = queue_num_free_elements(dev);
560 next = queue_next_element(dev);
561 len = read(dev->fd, next, free_elem * sizeof(struct input_event));
564 } else if (len > 0 && len % sizeof(struct input_event) != 0)
567 int nev = len/sizeof(struct input_event);
568 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
574 int libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
581 if (flags & LIBEVDEV_READ_SYNC) {
582 if (!dev->need_sync && dev->queue_nsync == 0)
584 else if (dev->need_sync) {
585 rc = sync_state(dev);
589 } else if (dev->need_sync) {
590 /* FIXME: still need to call update_state for all events
591 * here, otherwise the library has the wrong view of the
593 queue_shift_multiple(dev, dev->queue_nsync, NULL);
596 /* FIXME: check for O_NONBLOCK and if not set, skip if we have an
597 * event in the queue from the previous read.
600 /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
601 worst case we don't read fast enough and end up with SYN_DROPPED anyway */
602 rc = read_more_events(dev);
603 if (rc < 0 && rc != -EAGAIN)
606 if (queue_shift(dev, ev) != 0)
609 update_state(dev, ev);
612 if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
617 if (flags & LIBEVDEV_READ_SYNC && dev->queue_nsync > 0) {
627 libevdev_get_name(const struct libevdev *dev)
633 libevdev_get_phys(const struct libevdev *dev)
639 libevdev_get_uniq(const struct libevdev *dev)
644 int libevdev_get_product_id(const struct libevdev *dev)
646 return dev->ids.product;
649 int libevdev_get_vendor_id(const struct libevdev *dev)
651 return dev->ids.vendor;
654 int libevdev_get_bustype(const struct libevdev *dev)
656 return dev->ids.bustype;
659 int libevdev_get_version(const struct libevdev *dev)
661 return dev->ids.version;
664 int libevdev_get_driver_version(const struct libevdev *dev)
666 return dev->driver_version;
670 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
672 return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
676 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
678 return (type <= EV_MAX) && bit_is_set(dev->bits, type);
682 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
684 const unsigned long *mask;
687 if (!libevdev_has_event_type(dev, type))
693 max = type_to_mask_const(dev, type, &mask);
698 return bit_is_set(mask, code);
702 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
706 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
710 case EV_ABS: value = dev->abs_info[code].value; break;
711 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
721 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
723 if (libevdev_has_event_type(dev, type) &&
724 libevdev_has_event_code(dev, type, code)) {
725 *value = libevdev_get_event_value(dev, type, code);
732 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
734 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
737 if (slot >= dev->num_slots || slot >= MAX_SLOTS)
740 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
743 return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
747 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
749 if (libevdev_has_event_type(dev, EV_ABS) &&
750 libevdev_has_event_code(dev, EV_ABS, code) &&
751 slot < dev->num_slots && slot < MAX_SLOTS) {
752 *value = libevdev_get_slot_value(dev, slot, code);
759 libevdev_get_num_slots(const struct libevdev *dev)
761 return dev->num_slots;
765 libevdev_get_current_slot(const struct libevdev *dev)
767 return dev->current_slot;
770 const struct input_absinfo*
771 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
773 if (!libevdev_has_event_type(dev, EV_ABS) ||
774 !libevdev_has_event_code(dev, EV_ABS, code))
777 return &dev->abs_info[code];
781 libevdev_get_abs_min(const struct libevdev *dev, unsigned int code)
783 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
785 return absinfo ? absinfo->minimum : 0;
789 libevdev_get_abs_max(const struct libevdev *dev, unsigned int code)
791 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
793 return absinfo ? absinfo->maximum : 0;
797 libevdev_get_abs_fuzz(const struct libevdev *dev, unsigned int code)
799 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
801 return absinfo ? absinfo->fuzz : 0;
805 libevdev_get_abs_flat(const struct libevdev *dev, unsigned int code)
807 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
809 return absinfo ? absinfo->flat : 0;
813 libevdev_get_abs_resolution(const struct libevdev *dev, unsigned int code)
815 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
817 return absinfo ? absinfo->resolution : 0;
821 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
826 set_bit(dev->bits, type);
828 /* FIXME: pass through to kernel */
834 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
839 clear_bit(dev->bits, type);
841 /* FIXME: pass through to kernel */
847 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
848 unsigned int code, const void *data)
853 if (libevdev_enable_event_type(dev, type))
856 max = type_to_mask(dev, type, &mask);
863 if (type == EV_ABS) {
864 const struct input_absinfo *abs = data;
865 dev->abs_info[code] = *abs;
868 /* FIXME: pass through to kernel */
874 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
882 max = type_to_mask(dev, type, &mask);
887 clear_bit(mask, code);
889 /* FIXME: pass through to kernel */
895 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
902 rc = ioctl(dev->fd, EVIOCSABS(code), *abs);
906 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
912 libevdev_grab(struct libevdev *dev, int grab)
916 if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB)
919 if (grab == dev->grabbed)
922 if (grab == LIBEVDEV_GRAB)
923 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
924 else if (grab == LIBEVDEV_UNGRAB)
925 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
930 return rc < 0 ? -errno : 0;