Thursday, May 5, 2016

A recurring question I encounter is the question whether uinput or evdev should be the approach do implement some feature the user cares about. This question is unfortunately wrongly framed as uinput and evdev have no real overlap and work independent of each other. This post outlines what the differences are. Note that "evdev" here refers to the kernel API, not to the X.Org evdev driver.

First, the easy flowchart: do you have to create a new virtual device that has a set of specific capabilities? Use uinput. Do you have to read and handle events from an existing device? Use evdev. Do you have to create a device and read events from that device? You (probably) need two processes, one doing the uinput bit, one doing the evdev bit.

Ok, let's talk about the difference between evdev and uinput. evdev is
the default input API that all kernel input device nodes provide. Each
device provides one or more /dev/input/eventN nodes that a process can
interact with. This usually means checking a few capability bits ("does this
device have a left mouse button?") and reading events from the device. The
events themselves are in the form of struct input_event, defined in
linux/input.h and consist of a event type (relative, absolute, key,
...) and an event code specific to the type (x axis, left button, etc.). See
linux/input-event-codes.h for a list or linux/input.h in older
kernels.Specific to evdev is that events are serialised - framed by events
of type EV_SYN and code SYN_REPORT. Anything before a SYN_REPORT should be
considered one logical hardware event. For example, if you receive an x and
y movement within the same SYN_REPORT frame, the device has moved
diagonally.

Any event coming from the physical hardware goes into the kernel's input subsystem and is converted to an evdev event that is then available on the event node. That's pretty much it for evdev. It's a fairly simple API but it does have some quirks that are not immediately obvious so I recommend using libevdev whenever you actually need to communicate with a kernel device directly.

uinput is something completely different. uinput is an kernel device
driver that provides the /dev/uinput node. A process can open this
node, write a bunch of custom commands to it and the kernel then creates a
virtual input device. That device, like all others, presents an
/dev/input/eventN node. Any event written to the /dev/uinput node will
re-appear in that /dev/input/eventN node and a device created through uinput
looks just pretty much like a physical device to a process. You can detect
uinput-created virtual devices, but usually a process doesn't need to care
so all the common userspace (libinput, Xorg) doesn't bother. The evemu tool
is one of the most commonly used applications using uinput.

Now, there is one thing that may cause confusion: first, to set up a uinput device you'll have to use the familiar evdev type/code combinations (followed-by a couple of uinput-specific ioctls). Events written to uinput also use the struct input_event form, so looking at uinput code one can easily mistake it for evdev code. Nevertheless, the two serve a completely different purpose.
As with evdev, I recommend using libevdev to initalise uinput devices. libevdev has a couple of uinput-related functions that make life easier.

Below is a basic illustration of how things work together. The physical devices send their events through the event nodes and libinput is a process that reads those events. evemu talks to the uinput module and creates a virtual device which then too sends events through its event node - for libinput to read.