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;
81 libevdev_new_from_fd(int fd, struct libevdev **dev)
90 rc = libevdev_set_fd(d, fd);
99 libevdev_free(struct libevdev *dev)
112 libevdev_set_log_handler(struct libevdev *dev, libevdev_log_func_t logfunc)
117 dev->log = logfunc ? logfunc : libevdev_noop_log_func;
121 libevdev_change_fd(struct libevdev *dev, int fd)
130 libevdev_set_fd(struct libevdev* dev, int fd)
139 rc = ioctl(fd, EVIOCGBIT(0, sizeof(dev->bits)), dev->bits);
143 memset(buf, 0, sizeof(buf));
144 rc = ioctl(fd, EVIOCGNAME(sizeof(buf) - 1), buf);
148 dev->name = calloc(strlen(buf) + 1, sizeof(char));
153 strcpy(dev->name, buf);
155 memset(buf, 0, sizeof(buf));
156 rc = ioctl(fd, EVIOCGPHYS(sizeof(buf) - 1), buf);
158 /* uinput has no phys */
162 dev->phys = calloc(strlen(buf) + 1, sizeof(char));
167 strcpy(dev->phys, buf);
170 memset(buf, 0, sizeof(buf));
171 rc = ioctl(fd, EVIOCGUNIQ(sizeof(buf) - 1), buf);
176 dev->uniq = calloc(strlen(buf) + 1, sizeof(char));
181 strcpy(dev->uniq, buf);
184 rc = ioctl(fd, EVIOCGID, &dev->ids);
188 rc = ioctl(fd, EVIOCGVERSION, &dev->driver_version);
192 rc = ioctl(fd, EVIOCGPROP(sizeof(dev->props)), dev->props);
196 rc = ioctl(fd, EVIOCGBIT(EV_REL, sizeof(dev->rel_bits)), dev->rel_bits);
200 rc = ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(dev->abs_bits)), dev->abs_bits);
204 rc = ioctl(fd, EVIOCGBIT(EV_LED, sizeof(dev->led_bits)), dev->led_bits);
208 rc = ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(dev->key_bits)), dev->key_bits);
212 for (i = ABS_X; i <= ABS_MAX; i++) {
213 if (bit_is_set(dev->abs_bits, i)) {
214 struct input_absinfo abs_info;
215 rc = ioctl(fd, EVIOCGABS(i), &abs_info);
219 dev->abs_info[i] = abs_info;
220 if (i == ABS_MT_SLOT) {
221 dev->num_slots = abs_info.maximum + 1; /* FIXME: non-zero min? */
222 dev->current_slot = abs_info.value;
228 rc = init_event_queue(dev);
232 /* not copying key state because we won't know when we'll start to
233 * use this fd and key's are likely to change state by then.
234 * Same with the valuators, really, but they may not change.
240 return rc ? -errno : 0;
244 libevdev_get_fd(const struct libevdev* dev)
250 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
252 ev->time = dev->last_event_time;
259 sync_key_state(struct libevdev *dev)
263 unsigned long keystate[NLONGS(KEY_MAX)];
265 rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
269 for (i = 0; i < KEY_MAX; i++) {
271 old = bit_is_set(dev->key_values, i);
272 new = bit_is_set(keystate, i);
274 struct input_event *ev = queue_push(dev);
275 init_event(dev, ev, EV_KEY, i, new ? 1 : 0);
277 set_bit_state(dev->key_values, i, new);
282 return rc ? -errno : 0;
286 sync_abs_state(struct libevdev *dev)
291 for (i = ABS_X; i <= ABS_MAX; i++) {
292 struct input_absinfo abs_info;
294 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
297 if (!bit_is_set(dev->abs_bits, i))
300 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
304 if (dev->abs_info[i].value != abs_info.value) {
305 struct input_event *ev = queue_push(dev);
307 init_event(dev, ev, EV_ABS, i, abs_info.value);
308 dev->abs_info[i].value = abs_info.value;
314 return rc ? -errno : 0;
318 sync_mt_state(struct libevdev *dev)
325 } mt_state[ABS_MT_CNT];
327 for (i = ABS_MT_MIN; i < ABS_MT_MAX; i++) {
329 if (i == ABS_MT_SLOT)
332 idx = i - ABS_MT_MIN;
333 mt_state[idx].code = i;
334 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
339 for (i = 0; i < dev->num_slots; i++) {
341 struct input_event *ev;
343 ev = queue_push(dev);
344 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, i);
345 for (j = ABS_MT_MIN; j < ABS_MT_MAX; j++) {
346 int jdx = j - ABS_MT_MIN;
348 if (j == ABS_MT_SLOT)
351 if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
354 ev = queue_push(dev);
355 init_event(dev, ev, EV_ABS, j, mt_state[jdx].val[i]);
356 dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
362 return rc ? -errno : 0;
366 sync_state(struct libevdev *dev)
370 struct input_event *ev;
372 /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
373 queue[0]) we need to shift this backwards. Except that chances are that the
374 queue may be either full or too full to prepend all the events needed for
377 so we search for the last sync event in the queue and drop everything before
378 including that event and rely on the kernel to tell us the right value for that
379 bitfield during the sync process.
382 for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
383 struct input_event e;
384 queue_peek(dev, i, &e);
385 if (e.type == EV_SYN)
390 queue_shift_multiple(dev, i + 1, NULL);
392 if (libevdev_has_event_type(dev, EV_KEY))
393 rc = sync_key_state(dev);
394 if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
395 rc = sync_abs_state(dev);
396 if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
397 rc = sync_mt_state(dev);
399 ev = queue_push(dev);
400 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
402 dev->queue_nsync = queue_num_elements(dev);
409 update_key_state(struct libevdev *dev, const struct input_event *e)
411 if (!libevdev_has_event_type(dev, EV_KEY))
414 if (e->code > KEY_MAX)
418 clear_bit(dev->key_values, e->code);
420 set_bit(dev->key_values, e->code);
426 update_mt_state(struct libevdev *dev, const struct input_event *e)
428 if (e->code == ABS_MT_SLOT) {
429 dev->current_slot = e->value;
431 } else if (dev->current_slot == -1)
434 dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
440 update_abs_state(struct libevdev *dev, const struct input_event *e)
442 if (!libevdev_has_event_type(dev, EV_ABS))
445 if (e->code > ABS_MAX)
448 if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
449 return update_mt_state(dev, e);
451 dev->abs_info[e->code].value = e->value;
457 update_state(struct libevdev *dev, const struct input_event *e)
466 rc = update_key_state(dev, e);
469 rc = update_abs_state(dev, e);
473 dev->last_event_time = e->time;
479 read_more_events(struct libevdev *dev)
483 struct input_event *next;
485 free_elem = queue_num_free_elements(dev);
489 next = queue_next_element(dev);
490 len = read(dev->fd, next, free_elem * sizeof(struct input_event));
493 } else if (len > 0 && len % sizeof(struct input_event) != 0)
496 int nev = len/sizeof(struct input_event);
497 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
503 int libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
510 if (flags & LIBEVDEV_READ_SYNC) {
511 if (!dev->need_sync && dev->queue_nsync == 0)
513 else if (dev->need_sync) {
514 rc = sync_state(dev);
518 } else if (dev->need_sync) {
519 /* FIXME: still need to call update_state for all events
520 * here, otherwise the library has the wrong view of the
522 queue_shift_multiple(dev, dev->queue_nsync, NULL);
525 /* FIXME: check for O_NONBLOCK and if not set, skip if we have an
526 * event in the queue from the previous read.
529 /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
530 worst case we don't read fast enough and end up with SYN_DROPPED anyway */
531 rc = read_more_events(dev);
532 if (rc < 0 && rc != -EAGAIN)
535 if (queue_shift(dev, ev) != 0)
538 update_state(dev, ev);
541 if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
546 if (flags & LIBEVDEV_READ_SYNC && dev->queue_nsync > 0) {
556 libevdev_get_name(const struct libevdev *dev)
562 libevdev_get_phys(const struct libevdev *dev)
568 libevdev_get_uniq(const struct libevdev *dev)
573 int libevdev_get_product_id(const struct libevdev *dev)
575 return dev->ids.product;
578 int libevdev_get_vendor_id(const struct libevdev *dev)
580 return dev->ids.vendor;
583 int libevdev_get_bustype(const struct libevdev *dev)
585 return dev->ids.bustype;
588 int libevdev_get_version(const struct libevdev *dev)
590 return dev->ids.version;
593 int libevdev_get_driver_version(const struct libevdev *dev)
595 return dev->driver_version;
599 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
601 return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
605 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
607 return (type <= EV_MAX) && bit_is_set(dev->bits, type);
611 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
613 const unsigned long *mask;
616 if (!libevdev_has_event_type(dev, type))
622 max = type_to_mask_const(dev, type, &mask);
627 return bit_is_set(mask, code);
631 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
635 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
639 case EV_ABS: value = dev->abs_info[code].value; break;
640 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
650 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
652 if (libevdev_has_event_type(dev, type) &&
653 libevdev_has_event_code(dev, type, code)) {
654 *value = libevdev_get_event_value(dev, type, code);
661 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
663 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
666 if (slot >= dev->num_slots || slot >= MAX_SLOTS)
669 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
672 return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
676 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
678 if (libevdev_has_event_type(dev, EV_ABS) &&
679 libevdev_has_event_code(dev, EV_ABS, code) &&
680 slot < dev->num_slots && slot < MAX_SLOTS) {
681 *value = libevdev_get_slot_value(dev, slot, code);
688 libevdev_get_num_slots(const struct libevdev *dev)
690 return dev->num_slots;
694 libevdev_get_current_slot(const struct libevdev *dev)
696 return dev->current_slot;
699 const struct input_absinfo*
700 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
702 if (!libevdev_has_event_type(dev, EV_ABS) ||
703 !libevdev_has_event_code(dev, EV_ABS, code))
706 return &dev->abs_info[code];
710 libevdev_get_abs_min(const struct libevdev *dev, unsigned int code)
712 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
714 return absinfo ? absinfo->minimum : 0;
718 libevdev_get_abs_max(const struct libevdev *dev, unsigned int code)
720 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
722 return absinfo ? absinfo->maximum : 0;
726 libevdev_get_abs_fuzz(const struct libevdev *dev, unsigned int code)
728 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
730 return absinfo ? absinfo->fuzz : 0;
734 libevdev_get_abs_flat(const struct libevdev *dev, unsigned int code)
736 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
738 return absinfo ? absinfo->flat : 0;
742 libevdev_get_abs_resolution(const struct libevdev *dev, unsigned int code)
744 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
746 return absinfo ? absinfo->resolution : 0;
750 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
755 set_bit(dev->bits, type);
761 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
766 clear_bit(dev->bits, type);
772 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
773 unsigned int code, const void *data)
778 if (libevdev_enable_event_type(dev, type))
781 max = type_to_mask(dev, type, &mask);
788 if (type == EV_ABS) {
789 const struct input_absinfo *abs = data;
790 dev->abs_info[code] = *abs;
797 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
805 max = type_to_mask(dev, type, &mask);
810 clear_bit(mask, code);
816 libevdev_kernel_enable_event_type(struct libevdev *dev, unsigned int type)
823 rc = ioctl(dev->fd, UI_SET_EVBIT, type);
825 libevdev_enable_event_type(dev, type);
827 return (rc != -1) ? 0 : -errno;
831 libevdev_kernel_enable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
836 const unsigned long *mask;
838 rc = libevdev_kernel_enable_event_type(dev, type);
842 max = type_to_mask_const(dev, type, &mask);
847 case EV_KEY: uinput_bit = UI_SET_KEYBIT; break;
848 case EV_REL: uinput_bit = UI_SET_RELBIT; break;
849 case EV_ABS: uinput_bit = UI_SET_ABSBIT; break;
850 case EV_MSC: uinput_bit = UI_SET_MSCBIT; break;
851 case EV_LED: uinput_bit = UI_SET_LEDBIT; break;
852 case EV_SND: uinput_bit = UI_SET_SNDBIT; break;
853 case EV_FF: uinput_bit = UI_SET_FFBIT; break;
854 case EV_SW: uinput_bit = UI_SET_SWBIT; break;
857 rc = ioctl(dev->fd, uinput_bit, type);
859 libevdev_enable_event_type(dev, type);
861 return (rc != -1) ? 0 : -errno;
865 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
872 rc = ioctl(dev->fd, EVIOCSABS(code), *abs);
876 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
882 libevdev_grab(struct libevdev *dev, int grab)
886 if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB)
889 if (grab == dev->grabbed)
892 if (grab == LIBEVDEV_GRAB)
893 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
894 else if (grab == LIBEVDEV_UNGRAB)
895 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
900 return rc < 0 ? -errno : 0;
904 libevdev_get_event_type_name(unsigned int type)
913 libevdev_get_event_code_name(unsigned int type, unsigned int code)
918 if (code > ev_max[type])
921 return event_type_map[type][code];
925 libevdev_get_input_prop_name(unsigned int prop)
927 if (prop > INPUT_PROP_MAX)
930 return input_prop_map[prop];