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)
188 libevdev_set_log_handler(struct libevdev *dev, libevdev_log_func_t logfunc)
190 dev->log = logfunc ? logfunc : libevdev_noop_log_func;
194 libevdev_change_fd(struct libevdev *dev, int fd)
203 libevdev_set_fd(struct libevdev* dev, int fd)
211 rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
215 rc = ioctl(fd, EVIOCGNAME(sizeof(dev->name) - 1), dev->name);
219 rc = ioctl(fd, EVIOCGID, &dev->ids);
223 rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
227 rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
231 rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
235 rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
239 rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
243 for (i = ABS_X; i <= ABS_MAX; i++) {
244 if (bit_is_set(dev->abs_bits, i)) {
245 struct input_absinfo abs_info;
246 rc = ioctl(fd, EVIOCGABS(i), &dev->abs_info[i]);
250 if (i == ABS_MT_SLOT) {
251 dev->num_slots = abs_info.maximum + 1; /* FIXME: non-zero min? */
252 dev->current_slot = abs_info.value;
258 rc = init_event_queue(dev);
262 /* not copying key state because we won't know when we'll start to
263 * use this fd and key's are likely to change state by then.
264 * 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 input_event *ev, int type, int code, int value)
282 ev->time.tv_sec = 0; /* FIXME: blah! */
283 ev->time.tv_usec = 0; /* FIXME: blah! */
290 sync_key_state(struct libevdev *dev)
294 unsigned long keystate[NLONGS(KEY_MAX)];
296 rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
300 for (i = 0; i < KEY_MAX; i++) {
302 old = bit_is_set(dev->key_values, i);
303 new = bit_is_set(keystate, i);
305 struct input_event *ev = queue_push(dev);
306 init_event(ev, EV_KEY, i, new ? 1 : 0);
308 set_bit_state(dev->key_values, i, new);
313 return rc ? -errno : 0;
317 sync_abs_state(struct libevdev *dev)
322 for (i = ABS_X; i <= ABS_MAX; i++) {
323 struct input_absinfo abs_info;
325 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
328 if (!bit_is_set(dev->abs_bits, i))
331 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
335 if (dev->abs_info[i].value != abs_info.value) {
336 struct input_event *ev = queue_push(dev);
338 init_event(ev, EV_ABS, i, abs_info.value);
339 dev->abs_info[i].value = abs_info.value;
345 return rc ? -errno : 0;
349 sync_mt_state(struct libevdev *dev)
356 } mt_state[ABS_MT_CNT];
358 for (i = ABS_MT_MIN; i < ABS_MT_MAX; i++) {
360 if (i == ABS_MT_SLOT)
363 idx = i - ABS_MT_MIN;
364 mt_state[idx].code = i;
365 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
370 for (i = 0; i < dev->num_slots; i++) {
372 struct input_event *ev;
374 ev = queue_push(dev);
375 init_event(ev, EV_ABS, ABS_MT_SLOT, i);
376 for (j = ABS_MT_MIN; j < ABS_MT_MAX; j++) {
377 int jdx = j - ABS_MT_MIN;
379 if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
382 ev = queue_push(dev);
383 init_event(ev, EV_ABS, j, mt_state[jdx].val[i]);
384 dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
390 return rc ? -errno : 0;
394 sync_state(struct libevdev *dev)
398 struct input_event *ev;
400 /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
401 queue[0]) we need to shift this backwards. Except that chances are that the
402 queue may be either full or too full to prepend all the events needed for
405 so we search for the last sync event in the queue and drop everything before
406 including that event and rely on the kernel to tell us the right value for that
407 bitfield during the sync process.
410 for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
411 struct input_event e;
412 queue_peek(dev, i, &e);
413 if (e.type == EV_SYN)
418 queue_shift_multiple(dev, i + 1, NULL);
420 if (libevdev_has_event_type(dev, EV_KEY))
421 rc = sync_key_state(dev);
422 if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
423 rc = sync_abs_state(dev);
424 if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
425 rc = sync_mt_state(dev);
427 ev = queue_push(dev);
428 init_event(ev, EV_SYN, SYN_REPORT, 0);
430 dev->queue_nsync = queue_num_elements(dev);
437 update_key_state(struct libevdev *dev, const struct input_event *e)
439 if (!libevdev_has_event_type(dev, EV_KEY))
442 if (e->code > KEY_MAX)
446 clear_bit(dev->key_values, e->code);
448 set_bit(dev->key_values, e->code);
454 update_mt_state(struct libevdev *dev, const struct input_event *e)
456 if (e->code == ABS_MT_SLOT) {
457 dev->current_slot = e->value;
459 } else if (dev->current_slot == -1)
462 dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
468 update_abs_state(struct libevdev *dev, const struct input_event *e)
470 if (!libevdev_has_event_type(dev, EV_ABS))
473 if (e->code > ABS_MAX)
476 if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
477 return update_mt_state(dev, e);
479 dev->abs_info[e->code].value = e->value;
485 update_state(struct libevdev *dev, const struct input_event *e)
494 rc = update_key_state(dev, e);
497 rc = update_abs_state(dev, e);
505 read_more_events(struct libevdev *dev)
509 struct input_event *next;
511 free_elem = queue_num_free_elements(dev);
515 next = queue_next_element(dev);
516 len = read(dev->fd, next, free_elem * sizeof(struct input_event));
519 } else if (len > 0 && len % sizeof(struct input_event) != 0)
522 int nev = len/sizeof(struct input_event);
523 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
529 int libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
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)
587 int libevdev_get_pid(const struct libevdev *dev)
589 return dev->ids.product;
592 int libevdev_get_vid(const struct libevdev *dev)
594 return dev->ids.vendor;
597 int libevdev_get_bustype(const struct libevdev *dev)
599 return dev->ids.bustype;
603 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
605 return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
609 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
611 return (type <= EV_MAX) && bit_is_set(dev->bits, type);
615 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
617 const unsigned long *mask;
620 if (!libevdev_has_event_type(dev, type))
626 max = type_to_mask_const(dev, type, &mask);
631 return bit_is_set(mask, code);
635 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
639 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
643 case EV_ABS: value = dev->abs_info[code].value; break;
644 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
654 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
656 if (libevdev_has_event_type(dev, type) &&
657 libevdev_has_event_code(dev, type, code)) {
658 *value = libevdev_get_event_value(dev, type, code);
665 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
667 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
670 if (slot >= dev->num_slots || slot >= MAX_SLOTS)
673 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
676 return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
680 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
682 if (libevdev_has_event_type(dev, EV_ABS) &&
683 libevdev_has_event_code(dev, EV_ABS, code) &&
684 slot < dev->num_slots && slot < MAX_SLOTS) {
685 *value = libevdev_get_slot_value(dev, slot, code);
692 libevdev_get_num_slots(const struct libevdev *dev)
694 return dev->num_slots;
698 libevdev_get_current_slot(const struct libevdev *dev)
700 return dev->current_slot;
703 const struct input_absinfo*
704 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
706 if (!libevdev_has_event_type(dev, EV_ABS) ||
707 !libevdev_has_event_code(dev, EV_ABS, code))
710 return &dev->abs_info[code];
714 libevdev_get_abs_min(const struct libevdev *dev, unsigned int code)
716 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
718 return absinfo ? absinfo->minimum : 0;
722 libevdev_get_abs_max(const struct libevdev *dev, unsigned int code)
724 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
726 return absinfo ? absinfo->maximum : 0;
730 libevdev_get_abs_fuzz(const struct libevdev *dev, unsigned int code)
732 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
734 return absinfo ? absinfo->fuzz : 0;
738 libevdev_get_abs_flat(const struct libevdev *dev, unsigned int code)
740 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
742 return absinfo ? absinfo->flat : 0;
746 libevdev_get_abs_resolution(const struct libevdev *dev, unsigned int code)
748 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
750 return absinfo ? absinfo->resolution : 0;
754 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
759 set_bit(dev->bits, type);
761 /* FIXME: pass through to kernel */
767 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
772 clear_bit(dev->bits, type);
774 /* FIXME: pass through to kernel */
780 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
781 unsigned int code, const void *data)
786 if (libevdev_enable_event_type(dev, type))
789 max = type_to_mask(dev, type, &mask);
796 if (type == EV_ABS) {
797 const struct input_absinfo *abs = data;
798 dev->abs_info[code] = *abs;
801 /* FIXME: pass through to kernel */
807 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
815 max = type_to_mask(dev, type, &mask);
820 clear_bit(mask, code);
822 /* FIXME: pass through to kernel */
828 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
835 rc = ioctl(dev->fd, EVIOCSABS(code), *abs);
839 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
845 libevdev_grab(struct libevdev *dev, int grab)
849 if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB)
852 if (grab == dev->grabbed)
855 if (grab == LIBEVDEV_GRAB)
856 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
857 else if (grab == LIBEVDEV_UNGRAB)
858 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
863 return rc < 0 ? -errno : 0;