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"
32 #include "libevdev-util.h"
33 #include "event-names.h"
38 init_event_queue(struct libevdev *dev)
40 /* FIXME: count the number of axes, keys, etc. to get a better idea at how many events per
41 EV_SYN we could possibly get. Then multiply that by the actual buffer size we care about */
43 const int QUEUE_SIZE = 256;
45 return queue_alloc(dev, QUEUE_SIZE);
49 _libevdev_log(struct libevdev *dev, const char *format, ...)
53 va_start(args, format);
54 dev->log(format, args);
59 libevdev_noop_log_func(const char *format, va_list args)
68 dev = calloc(1, sizeof(*dev));
73 dev->current_slot = -1;
74 dev->log = libevdev_noop_log_func;
80 libevdev_new_from_fd(int fd, struct libevdev **dev)
89 rc = libevdev_set_fd(d, fd);
98 libevdev_free(struct libevdev *dev)
111 libevdev_set_log_handler(struct libevdev *dev, libevdev_log_func_t logfunc)
116 dev->log = logfunc ? logfunc : libevdev_noop_log_func;
120 libevdev_change_fd(struct libevdev *dev, int fd)
129 libevdev_set_fd(struct libevdev* dev, int fd)
138 rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
142 memset(buf, 0, sizeof(buf));
143 rc = ioctl(fd, EVIOCGNAME(sizeof(buf) - 1), buf);
147 dev->name = calloc(strlen(buf) + 1, sizeof(char));
152 strcpy(dev->name, buf);
154 memset(buf, 0, sizeof(buf));
155 rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
157 /* uinput has no phys */
161 dev->phys = calloc(strlen(buf) + 1, sizeof(char));
166 strcpy(dev->phys, buf);
169 memset(buf, 0, sizeof(buf));
170 rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
175 dev->uniq = calloc(strlen(buf) + 1, sizeof(char));
180 strcpy(dev->uniq, buf);
183 rc = ioctl(fd, EVIOCGID, &dev->ids);
187 rc = ioctl(fd, EVIOCGVERSION, &dev->driver_version);
191 rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
195 rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
199 rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
203 rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
207 rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
211 for (i = ABS_X; i <= ABS_MAX; i++) {
212 if (bit_is_set(dev->abs_bits, i)) {
213 struct input_absinfo abs_info;
214 rc = ioctl(fd, EVIOCGABS(i), &abs_info);
218 dev->abs_info[i] = abs_info;
219 if (i == ABS_MT_SLOT) {
220 dev->num_slots = abs_info.maximum + 1; /* FIXME: non-zero min? */
221 dev->current_slot = abs_info.value;
227 rc = init_event_queue(dev);
231 /* not copying key state because we won't know when we'll start to
232 * use this fd and key's are likely to change state by then.
233 * Same with the valuators, really, but they may not change.
239 return rc ? -errno : 0;
243 libevdev_get_fd(const struct libevdev* dev)
249 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
251 ev->time = dev->last_event_time;
258 sync_key_state(struct libevdev *dev)
262 unsigned long keystate[NLONGS(KEY_MAX)];
264 rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
268 for (i = 0; i < KEY_MAX; i++) {
270 old = bit_is_set(dev->key_values, i);
271 new = bit_is_set(keystate, i);
273 struct input_event *ev = queue_push(dev);
274 init_event(dev, ev, EV_KEY, i, new ? 1 : 0);
276 set_bit_state(dev->key_values, i, new);
281 return rc ? -errno : 0;
285 sync_abs_state(struct libevdev *dev)
290 for (i = ABS_X; i <= ABS_MAX; i++) {
291 struct input_absinfo abs_info;
293 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
296 if (!bit_is_set(dev->abs_bits, i))
299 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
303 if (dev->abs_info[i].value != abs_info.value) {
304 struct input_event *ev = queue_push(dev);
306 init_event(dev, ev, EV_ABS, i, abs_info.value);
307 dev->abs_info[i].value = abs_info.value;
313 return rc ? -errno : 0;
317 sync_mt_state(struct libevdev *dev)
324 } mt_state[ABS_MT_CNT];
326 for (i = ABS_MT_MIN; i < ABS_MT_MAX; i++) {
328 if (i == ABS_MT_SLOT)
331 idx = i - ABS_MT_MIN;
332 mt_state[idx].code = i;
333 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
338 for (i = 0; i < dev->num_slots; i++) {
340 struct input_event *ev;
342 ev = queue_push(dev);
343 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, i);
344 for (j = ABS_MT_MIN; j < ABS_MT_MAX; j++) {
345 int jdx = j - ABS_MT_MIN;
347 if (j == ABS_MT_SLOT)
350 if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
353 ev = queue_push(dev);
354 init_event(dev, ev, EV_ABS, j, mt_state[jdx].val[i]);
355 dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
361 return rc ? -errno : 0;
365 sync_state(struct libevdev *dev)
369 struct input_event *ev;
371 /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
372 queue[0]) we need to shift this backwards. Except that chances are that the
373 queue may be either full or too full to prepend all the events needed for
376 so we search for the last sync event in the queue and drop everything before
377 including that event and rely on the kernel to tell us the right value for that
378 bitfield during the sync process.
381 for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
382 struct input_event e;
383 queue_peek(dev, i, &e);
384 if (e.type == EV_SYN)
389 queue_shift_multiple(dev, i + 1, NULL);
391 if (libevdev_has_event_type(dev, EV_KEY))
392 rc = sync_key_state(dev);
393 if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
394 rc = sync_abs_state(dev);
395 if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
396 rc = sync_mt_state(dev);
398 ev = queue_push(dev);
399 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
401 dev->queue_nsync = queue_num_elements(dev);
408 update_key_state(struct libevdev *dev, const struct input_event *e)
410 if (!libevdev_has_event_type(dev, EV_KEY))
413 if (e->code > KEY_MAX)
417 clear_bit(dev->key_values, e->code);
419 set_bit(dev->key_values, e->code);
425 update_mt_state(struct libevdev *dev, const struct input_event *e)
427 if (e->code == ABS_MT_SLOT) {
428 dev->current_slot = e->value;
430 } else if (dev->current_slot == -1)
433 dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
439 update_abs_state(struct libevdev *dev, const struct input_event *e)
441 if (!libevdev_has_event_type(dev, EV_ABS))
444 if (e->code > ABS_MAX)
447 if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
448 return update_mt_state(dev, e);
450 dev->abs_info[e->code].value = e->value;
456 update_state(struct libevdev *dev, const struct input_event *e)
465 rc = update_key_state(dev, e);
468 rc = update_abs_state(dev, e);
472 dev->last_event_time = e->time;
478 read_more_events(struct libevdev *dev)
482 struct input_event *next;
484 free_elem = queue_num_free_elements(dev);
488 next = queue_next_element(dev);
489 len = read(dev->fd, next, free_elem * sizeof(struct input_event));
492 } else if (len > 0 && len % sizeof(struct input_event) != 0)
495 int nev = len/sizeof(struct input_event);
496 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
502 int libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
509 if (flags & LIBEVDEV_READ_SYNC) {
510 if (!dev->need_sync && dev->queue_nsync == 0)
512 else if (dev->need_sync) {
513 rc = sync_state(dev);
517 } else if (dev->need_sync) {
518 /* FIXME: still need to call update_state for all events
519 * here, otherwise the library has the wrong view of the
521 queue_shift_multiple(dev, dev->queue_nsync, NULL);
524 /* FIXME: check for O_NONBLOCK and if not set, skip if we have an
525 * event in the queue from the previous read.
528 /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
529 worst case we don't read fast enough and end up with SYN_DROPPED anyway */
530 rc = read_more_events(dev);
531 if (rc < 0 && rc != -EAGAIN)
534 if (queue_shift(dev, ev) != 0)
537 update_state(dev, ev);
540 if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
545 if (flags & LIBEVDEV_READ_SYNC && dev->queue_nsync > 0) {
555 libevdev_get_name(const struct libevdev *dev)
561 libevdev_get_phys(const struct libevdev *dev)
567 libevdev_get_uniq(const struct libevdev *dev)
572 int libevdev_get_product_id(const struct libevdev *dev)
574 return dev->ids.product;
577 int libevdev_get_vendor_id(const struct libevdev *dev)
579 return dev->ids.vendor;
582 int libevdev_get_bustype(const struct libevdev *dev)
584 return dev->ids.bustype;
587 int libevdev_get_version(const struct libevdev *dev)
589 return dev->ids.version;
592 int libevdev_get_driver_version(const struct libevdev *dev)
594 return dev->driver_version;
598 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
600 return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
604 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
606 return (type <= EV_MAX) && bit_is_set(dev->bits, type);
610 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
612 const unsigned long *mask;
615 if (!libevdev_has_event_type(dev, type))
621 max = type_to_mask_const(dev, type, &mask);
626 return bit_is_set(mask, code);
630 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
634 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
638 case EV_ABS: value = dev->abs_info[code].value; break;
639 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
649 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
651 if (libevdev_has_event_type(dev, type) &&
652 libevdev_has_event_code(dev, type, code)) {
653 *value = libevdev_get_event_value(dev, type, code);
660 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
662 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
665 if (slot >= dev->num_slots || slot >= MAX_SLOTS)
668 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
671 return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
675 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
677 if (libevdev_has_event_type(dev, EV_ABS) &&
678 libevdev_has_event_code(dev, EV_ABS, code) &&
679 slot < dev->num_slots && slot < MAX_SLOTS) {
680 *value = libevdev_get_slot_value(dev, slot, code);
687 libevdev_get_num_slots(const struct libevdev *dev)
689 return dev->num_slots;
693 libevdev_get_current_slot(const struct libevdev *dev)
695 return dev->current_slot;
698 const struct input_absinfo*
699 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
701 if (!libevdev_has_event_type(dev, EV_ABS) ||
702 !libevdev_has_event_code(dev, EV_ABS, code))
705 return &dev->abs_info[code];
709 libevdev_get_abs_min(const struct libevdev *dev, unsigned int code)
711 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
713 return absinfo ? absinfo->minimum : 0;
717 libevdev_get_abs_max(const struct libevdev *dev, unsigned int code)
719 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
721 return absinfo ? absinfo->maximum : 0;
725 libevdev_get_abs_fuzz(const struct libevdev *dev, unsigned int code)
727 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
729 return absinfo ? absinfo->fuzz : 0;
733 libevdev_get_abs_flat(const struct libevdev *dev, unsigned int code)
735 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
737 return absinfo ? absinfo->flat : 0;
741 libevdev_get_abs_resolution(const struct libevdev *dev, unsigned int code)
743 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
745 return absinfo ? absinfo->resolution : 0;
749 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
754 set_bit(dev->bits, type);
756 /* FIXME: pass through to kernel */
762 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
767 clear_bit(dev->bits, type);
769 /* FIXME: pass through to kernel */
775 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
776 unsigned int code, const void *data)
781 if (libevdev_enable_event_type(dev, type))
784 max = type_to_mask(dev, type, &mask);
791 if (type == EV_ABS) {
792 const struct input_absinfo *abs = data;
793 dev->abs_info[code] = *abs;
796 /* FIXME: pass through to kernel */
802 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
810 max = type_to_mask(dev, type, &mask);
815 clear_bit(mask, code);
817 /* FIXME: pass through to kernel */
823 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
830 rc = ioctl(dev->fd, EVIOCSABS(code), *abs);
834 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
840 libevdev_grab(struct libevdev *dev, int grab)
844 if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB)
847 if (grab == dev->grabbed)
850 if (grab == LIBEVDEV_GRAB)
851 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
852 else if (grab == LIBEVDEV_UNGRAB)
853 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
858 return rc < 0 ? -errno : 0;
862 libevdev_get_event_type_name(unsigned int type)
871 libevdev_get_event_code_name(unsigned int type, unsigned int code)
876 if (code > ev_max[type])
879 return event_type_map[type][code];
883 libevdev_get_input_prop_name(unsigned int prop)
885 if (prop > INPUT_PROP_MAX)
888 return input_prop_map[prop];