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 rc = ioctl(fd, EVIOCGBIT(EV_SW, sizeof(dev->sw_bits)), dev->sw_bits);
216 rc = ioctl(fd, EVIOCGBIT(EV_MSC, sizeof(dev->msc_bits)), dev->msc_bits);
220 rc = ioctl(fd, EVIOCGBIT(EV_FF, sizeof(dev->ff_bits)), dev->ff_bits);
224 rc = ioctl(fd, EVIOCGBIT(EV_SND, sizeof(dev->snd_bits)), dev->snd_bits);
228 /* rep is a special case, always set it to 1 for both values if EV_REP is set */
229 if (bit_is_set(dev->bits, EV_REP)) {
230 for (i = 0; i < REP_MAX; i++)
231 set_bit(dev->rep_bits, i);
232 rc = ioctl(fd, EVIOCGREP, dev->rep_values);
237 for (i = ABS_X; i <= ABS_MAX; i++) {
238 if (bit_is_set(dev->abs_bits, i)) {
239 struct input_absinfo abs_info;
240 rc = ioctl(fd, EVIOCGABS(i), &abs_info);
244 dev->abs_info[i] = abs_info;
245 if (i == ABS_MT_SLOT) {
246 dev->num_slots = abs_info.maximum + 1; /* FIXME: non-zero min? */
247 dev->current_slot = abs_info.value;
253 rc = init_event_queue(dev);
257 /* not copying key state because we won't know when we'll start to
258 * use this fd and key's are likely to change state by then.
259 * Same with the valuators, really, but they may not change.
265 return rc ? -errno : 0;
269 libevdev_get_fd(const struct libevdev* dev)
275 init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
277 ev->time = dev->last_event_time;
284 sync_key_state(struct libevdev *dev)
288 unsigned long keystate[NLONGS(KEY_MAX)];
290 rc = ioctl(dev->fd, EVIOCGKEY(sizeof(keystate)), keystate);
294 for (i = 0; i < KEY_MAX; i++) {
296 old = bit_is_set(dev->key_values, i);
297 new = bit_is_set(keystate, i);
299 struct input_event *ev = queue_push(dev);
300 init_event(dev, ev, EV_KEY, i, new ? 1 : 0);
302 set_bit_state(dev->key_values, i, new);
307 return rc ? -errno : 0;
311 sync_abs_state(struct libevdev *dev)
316 for (i = ABS_X; i <= ABS_MAX; i++) {
317 struct input_absinfo abs_info;
319 if (i >= ABS_MT_MIN && i <= ABS_MT_MAX)
322 if (!bit_is_set(dev->abs_bits, i))
325 rc = ioctl(dev->fd, EVIOCGABS(i), &abs_info);
329 if (dev->abs_info[i].value != abs_info.value) {
330 struct input_event *ev = queue_push(dev);
332 init_event(dev, ev, EV_ABS, i, abs_info.value);
333 dev->abs_info[i].value = abs_info.value;
339 return rc ? -errno : 0;
343 sync_mt_state(struct libevdev *dev)
350 } mt_state[ABS_MT_CNT];
352 for (i = ABS_MT_MIN; i < ABS_MT_MAX; i++) {
354 if (i == ABS_MT_SLOT)
357 idx = i - ABS_MT_MIN;
358 mt_state[idx].code = i;
359 rc = ioctl(dev->fd, EVIOCGMTSLOTS(sizeof(struct mt_state)), &mt_state[idx]);
364 for (i = 0; i < dev->num_slots; i++) {
366 struct input_event *ev;
368 ev = queue_push(dev);
369 init_event(dev, ev, EV_ABS, ABS_MT_SLOT, i);
370 for (j = ABS_MT_MIN; j < ABS_MT_MAX; j++) {
371 int jdx = j - ABS_MT_MIN;
373 if (j == ABS_MT_SLOT)
376 if (dev->mt_slot_vals[i][jdx] == mt_state[jdx].val[i])
379 ev = queue_push(dev);
380 init_event(dev, ev, EV_ABS, j, mt_state[jdx].val[i]);
381 dev->mt_slot_vals[i][jdx] = mt_state[jdx].val[i];
387 return rc ? -errno : 0;
391 sync_state(struct libevdev *dev)
395 struct input_event *ev;
397 /* FIXME: if we have events in the queue after the SYN_DROPPED (which was
398 queue[0]) we need to shift this backwards. Except that chances are that the
399 queue may be either full or too full to prepend all the events needed for
402 so we search for the last sync event in the queue and drop everything before
403 including that event and rely on the kernel to tell us the right value for that
404 bitfield during the sync process.
407 for (i = queue_num_elements(dev) - 1; i >= 0; i--) {
408 struct input_event e;
409 queue_peek(dev, i, &e);
410 if (e.type == EV_SYN)
415 queue_shift_multiple(dev, i + 1, NULL);
417 if (libevdev_has_event_type(dev, EV_KEY))
418 rc = sync_key_state(dev);
419 if (rc == 0 && libevdev_has_event_type(dev, EV_ABS))
420 rc = sync_abs_state(dev);
421 if (rc == 0 && libevdev_has_event_code(dev, EV_ABS, ABS_MT_SLOT))
422 rc = sync_mt_state(dev);
424 ev = queue_push(dev);
425 init_event(dev, ev, EV_SYN, SYN_REPORT, 0);
427 dev->queue_nsync = queue_num_elements(dev);
434 update_key_state(struct libevdev *dev, const struct input_event *e)
436 if (!libevdev_has_event_type(dev, EV_KEY))
439 if (e->code > KEY_MAX)
443 clear_bit(dev->key_values, e->code);
445 set_bit(dev->key_values, e->code);
451 update_mt_state(struct libevdev *dev, const struct input_event *e)
453 if (e->code == ABS_MT_SLOT) {
454 dev->current_slot = e->value;
456 } else if (dev->current_slot == -1)
459 dev->mt_slot_vals[dev->current_slot][e->code - ABS_MT_MIN] = e->value;
465 update_abs_state(struct libevdev *dev, const struct input_event *e)
467 if (!libevdev_has_event_type(dev, EV_ABS))
470 if (e->code > ABS_MAX)
473 if (e->code >= ABS_MT_MIN && e->code <= ABS_MT_MAX)
474 return update_mt_state(dev, e);
476 dev->abs_info[e->code].value = e->value;
482 update_state(struct libevdev *dev, const struct input_event *e)
491 rc = update_key_state(dev, e);
494 rc = update_abs_state(dev, e);
498 dev->last_event_time = e->time;
504 read_more_events(struct libevdev *dev)
508 struct input_event *next;
510 free_elem = queue_num_free_elements(dev);
514 next = queue_next_element(dev);
515 len = read(dev->fd, next, free_elem * sizeof(struct input_event));
518 } else if (len > 0 && len % sizeof(struct input_event) != 0)
521 int nev = len/sizeof(struct input_event);
522 queue_set_num_elements(dev, queue_num_elements(dev) + nev);
528 int libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
535 if (flags & LIBEVDEV_READ_SYNC) {
536 if (!dev->need_sync && dev->queue_nsync == 0)
538 else if (dev->need_sync) {
539 rc = sync_state(dev);
543 } else if (dev->need_sync) {
544 /* FIXME: still need to call update_state for all events
545 * here, otherwise the library has the wrong view of the
547 queue_shift_multiple(dev, dev->queue_nsync, NULL);
550 /* FIXME: check for O_NONBLOCK and if not set, skip if we have an
551 * event in the queue from the previous read.
554 /* Always read in some more events. Best case this smoothes over a potential SYN_DROPPED,
555 worst case we don't read fast enough and end up with SYN_DROPPED anyway */
556 rc = read_more_events(dev);
557 if (rc < 0 && rc != -EAGAIN)
560 if (queue_shift(dev, ev) != 0)
563 update_state(dev, ev);
566 if (ev->type == EV_SYN && ev->code == SYN_DROPPED) {
571 if (flags & LIBEVDEV_READ_SYNC && dev->queue_nsync > 0) {
581 libevdev_get_name(const struct libevdev *dev)
587 libevdev_get_phys(const struct libevdev *dev)
593 libevdev_get_uniq(const struct libevdev *dev)
598 int libevdev_get_product_id(const struct libevdev *dev)
600 return dev->ids.product;
603 int libevdev_get_vendor_id(const struct libevdev *dev)
605 return dev->ids.vendor;
608 int libevdev_get_bustype(const struct libevdev *dev)
610 return dev->ids.bustype;
613 int libevdev_get_version(const struct libevdev *dev)
615 return dev->ids.version;
618 int libevdev_get_driver_version(const struct libevdev *dev)
620 return dev->driver_version;
624 libevdev_has_property(const struct libevdev *dev, unsigned int prop)
626 return (prop <= INPUT_PROP_MAX) && bit_is_set(dev->props, prop);
630 libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
632 return (type <= EV_MAX) && bit_is_set(dev->bits, type);
636 libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
638 const unsigned long *mask;
641 if (!libevdev_has_event_type(dev, type))
647 max = type_to_mask_const(dev, type, &mask);
649 if (max == -1 || code > max)
652 return bit_is_set(mask, code);
656 libevdev_get_event_value(const struct libevdev *dev, unsigned int type, unsigned int code)
660 if (!libevdev_has_event_type(dev, type) || !libevdev_has_event_code(dev, type, code))
664 case EV_ABS: value = dev->abs_info[code].value; break;
665 case EV_KEY: value = bit_is_set(dev->key_values, code); break;
675 libevdev_fetch_event_value(const struct libevdev *dev, unsigned int type, unsigned int code, int *value)
677 if (libevdev_has_event_type(dev, type) &&
678 libevdev_has_event_code(dev, type, code)) {
679 *value = libevdev_get_event_value(dev, type, code);
686 libevdev_get_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code)
688 if (!libevdev_has_event_type(dev, EV_ABS) || !libevdev_has_event_code(dev, EV_ABS, code))
691 if (slot >= dev->num_slots || slot >= MAX_SLOTS)
694 if (code > ABS_MT_MAX || code < ABS_MT_MIN)
697 return dev->mt_slot_vals[slot][code - ABS_MT_MIN];
701 libevdev_fetch_slot_value(const struct libevdev *dev, unsigned int slot, unsigned int code, int *value)
703 if (libevdev_has_event_type(dev, EV_ABS) &&
704 libevdev_has_event_code(dev, EV_ABS, code) &&
705 slot < dev->num_slots && slot < MAX_SLOTS) {
706 *value = libevdev_get_slot_value(dev, slot, code);
713 libevdev_get_num_slots(const struct libevdev *dev)
715 return dev->num_slots;
719 libevdev_get_current_slot(const struct libevdev *dev)
721 return dev->current_slot;
724 const struct input_absinfo*
725 libevdev_get_abs_info(const struct libevdev *dev, unsigned int code)
727 if (!libevdev_has_event_type(dev, EV_ABS) ||
728 !libevdev_has_event_code(dev, EV_ABS, code))
731 return &dev->abs_info[code];
735 libevdev_get_abs_min(const struct libevdev *dev, unsigned int code)
737 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
739 return absinfo ? absinfo->minimum : 0;
743 libevdev_get_abs_max(const struct libevdev *dev, unsigned int code)
745 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
747 return absinfo ? absinfo->maximum : 0;
751 libevdev_get_abs_fuzz(const struct libevdev *dev, unsigned int code)
753 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
755 return absinfo ? absinfo->fuzz : 0;
759 libevdev_get_abs_flat(const struct libevdev *dev, unsigned int code)
761 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
763 return absinfo ? absinfo->flat : 0;
767 libevdev_get_abs_resolution(const struct libevdev *dev, unsigned int code)
769 const struct input_absinfo *absinfo = libevdev_get_abs_info(dev, code);
771 return absinfo ? absinfo->resolution : 0;
775 libevdev_enable_event_type(struct libevdev *dev, unsigned int type)
780 set_bit(dev->bits, type);
786 libevdev_disable_event_type(struct libevdev *dev, unsigned int type)
791 clear_bit(dev->bits, type);
797 libevdev_enable_event_code(struct libevdev *dev, unsigned int type,
798 unsigned int code, const void *data)
803 if (libevdev_enable_event_type(dev, type))
806 max = type_to_mask(dev, type, &mask);
813 if (type == EV_ABS) {
814 const struct input_absinfo *abs = data;
815 dev->abs_info[code] = *abs;
822 libevdev_disable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
830 max = type_to_mask(dev, type, &mask);
835 clear_bit(mask, code);
841 libevdev_kernel_enable_event_type(struct libevdev *dev, unsigned int type)
848 rc = ioctl(dev->fd, UI_SET_EVBIT, type);
850 libevdev_enable_event_type(dev, type);
852 return (rc != -1) ? 0 : -errno;
856 libevdev_kernel_enable_event_code(struct libevdev *dev, unsigned int type, unsigned int code)
861 const unsigned long *mask;
863 rc = libevdev_kernel_enable_event_type(dev, type);
867 max = type_to_mask_const(dev, type, &mask);
868 if (max == -1 || code > max)
872 case EV_KEY: uinput_bit = UI_SET_KEYBIT; break;
873 case EV_REL: uinput_bit = UI_SET_RELBIT; break;
874 case EV_ABS: uinput_bit = UI_SET_ABSBIT; break;
875 case EV_MSC: uinput_bit = UI_SET_MSCBIT; break;
876 case EV_LED: uinput_bit = UI_SET_LEDBIT; break;
877 case EV_SND: uinput_bit = UI_SET_SNDBIT; break;
878 case EV_FF: uinput_bit = UI_SET_FFBIT; break;
879 case EV_SW: uinput_bit = UI_SET_SWBIT; break;
882 rc = ioctl(dev->fd, uinput_bit, type);
884 libevdev_enable_event_type(dev, type);
886 return (rc != -1) ? 0 : -errno;
890 libevdev_kernel_set_abs_value(struct libevdev *dev, unsigned int code, const struct input_absinfo *abs)
897 rc = ioctl(dev->fd, EVIOCSABS(code), *abs);
901 rc = libevdev_enable_event_code(dev, EV_ABS, code, abs);
907 libevdev_grab(struct libevdev *dev, int grab)
911 if (grab != LIBEVDEV_GRAB && grab != LIBEVDEV_UNGRAB)
914 if (grab == dev->grabbed)
917 if (grab == LIBEVDEV_GRAB)
918 rc = ioctl(dev->fd, EVIOCGRAB, (void *)1);
919 else if (grab == LIBEVDEV_UNGRAB)
920 rc = ioctl(dev->fd, EVIOCGRAB, (void *)0);
925 return rc < 0 ? -errno : 0;
929 libevdev_get_event_type_name(unsigned int type)
938 libevdev_get_event_code_name(unsigned int type, unsigned int code)
943 if (code > ev_max[type])
946 return event_type_map[type][code];
950 libevdev_get_input_prop_name(unsigned int prop)
952 if (prop > INPUT_PROP_MAX)
955 return input_prop_map[prop];
959 libevdev_get_event_type_max(unsigned int type)
968 libevdev_get_repeat(struct libevdev *dev, int *delay, int *period)
970 if (!libevdev_has_event_type(dev, EV_REP))
974 *delay = dev->rep_values[REP_DELAY];
976 *period = dev->rep_values[REP_PERIOD];