libevdev Documentation

libevdev is a library for handling evdev kernel devices. It abstracts the evdev ioctls through type-safe interfaces and provides functions to change the appearance of the device.

Development

The git repository is available here:

Development of libevdev is discussed on input-tools@lists.freedesktop.org. Please submit patches, questions or general comments there.

Handling events and SYN_DROPPED

libevdev provides an interface for handling events, including most notably SYN_DROPPED events. SYN_DROPPED events are sent by the kernel when the process does not read events fast enough and the kernel is forced to drop some events. This causes the device to get out of sync with the process' view of it. libevdev handles this by telling the caller that a * SYN_DROPPED has been received and that the state of the device is different to what is to be expected. It then provides the delta between the previous state and the actual state of the device as a set of events. See libevdev_next_event() and SYN_DROPPED handling for more information on how SYN_DROPPED is handled.

Signal safety

libevdev is signal-safe for the majority of its operations, i.e. many of its functions are safe to be called from within a signal handler. Check the API documentation to make sure, unless explicitly stated a call is not signal safe.

Device handling

A libevdev context is valid for a given file descriptor and its duration. Closing the file descriptor will not destroy the libevdev device but libevdev will not be able to read further events.

libevdev does not attempt duplicate detection. Initializing two libevdev devices for the same fd is valid and behaves the same as for two different devices.

libevdev does not handle the file descriptors directly, it merely uses them. The caller is responsible for opening the file descriptors, setting them to O_NONBLOCK and handling permissions. A caller should drain any events pending on the file descriptor before passing it to libevdev.

Where does libevdev sit?

libevdev is essentially a read(2) on steroids for /dev/input/eventX devices. It sits below the process that handles input events, in between the kernel and that process. In the simplest case, e.g. an evtest-like tool the stack would look like this:

 kernel → libevdev → evtest

For X.Org input modules, the stack would look like this:

 kernel → libevdev → xf86-input-evdev → X server → X client

For anything using libinput (e.g. most Wayland compositors), the stack the stack would look like this:

 kernel → libevdev → libinput → Compositor → Wayland client

libevdev does not have knowledge of X clients or Wayland clients, it is too low in the stack.

Example

Below is a simple example that shows how libevdev could be used. This example opens a device, checks for relative axes and a left mouse button and if it finds them monitors the device to print the event.

struct libevdev *dev = NULL;
int fd;
int rc = 1;
fd = open("/dev/input/event0", O_RDONLY|O_NONBLOCK);
rc = libevdev_new_from_fd(fd, &dev);
if (rc < 0) {
fprintf(stderr, "Failed to init libevdev (%s)\n", strerror(-rc));
exit(1);
}
printf("Input device name: \"%s\"\n", libevdev_get_name(dev));
printf("Input device ID: bus %#x vendor %#x product %#x\n",
if (!libevdev_has_event_type(dev, EV_REL) ||
!libevdev_has_event_code(dev, EV_KEY, BTN_LEFT)) {
printf("This device does not look like a mouse\n");
exit(1);
}
do {
struct input_event ev;
if (rc == 0)
printf("Event: %s %s %d\n",
libevdev_event_code_get_name(ev.type, ev.code),
ev.value);
} while (rc == 1 || rc == 0 || rc == -EAGAIN);
int libevdev_get_id_vendor(const struct libevdev *dev)
int libevdev_get_id_product(const struct libevdev *dev)
int libevdev_has_event_type(const struct libevdev *dev, unsigned int type)
int libevdev_get_id_bustype(const struct libevdev *dev)
int libevdev_has_event_code(const struct libevdev *dev, unsigned int type, unsigned int code)
const char * libevdev_get_name(const struct libevdev *dev)
Retrieve the device's name, either as set by the caller or as read from the kernel.
int libevdev_next_event(struct libevdev *dev, unsigned int flags, struct input_event *ev)
Get the next event from the device.
@ LIBEVDEV_READ_FLAG_NORMAL
Process data in normal mode.
Definition: libevdev.h:762
int libevdev_new_from_fd(int fd, struct libevdev **dev)
Initialize a new libevdev device from the given fd.
const char * libevdev_event_code_get_name(unsigned int type, unsigned int code)
const char * libevdev_event_type_get_name(unsigned int type)

A more complete example is available with the libevdev-events tool here: https://gitlab.freedesktop.org/libevdev/libevdev/blob/master/tools/libevdev-events.c

Backwards compatibility with older kernel

libevdev attempts to build and run correctly on a number of kernel versions. If features required are not available, libevdev attempts to work around them in the most reasonable way. For more details see Compatibility and Behavior across kernel versions.

License information

libevdev is licensed under the X11 license.

Bindings

Reporting bugs

Please report bugs in the freedesktop.org GitLab instance: https://gitlab.freedesktop.org/libevdev/libevdev/issues/