Loading...
Searching...
No Matches
Event handling

Functions to handle events and fetch the current state of the event. More...

Enumerations

enum  libevdev_read_flag { LIBEVDEV_READ_FLAG_SYNC , LIBEVDEV_READ_FLAG_NORMAL , LIBEVDEV_READ_FLAG_FORCE_SYNC , LIBEVDEV_READ_FLAG_BLOCKING }
 
enum  libevdev_read_status { LIBEVDEV_READ_STATUS_SUCCESS , LIBEVDEV_READ_STATUS_SYNC }
 

Functions

int libevdev_next_event (struct libevdev *dev, unsigned int flags, struct input_event *ev)
 Get the next event from the device.
 
int libevdev_has_event_pending (struct libevdev *dev)
 Check if there are events waiting for us.
 

Detailed Description

Functions to handle events and fetch the current state of the event.

libevdev updates its internal state as the event is processed and forwarded to the caller. Thus, the libevdev state of the device should always be identical to the caller's state. It may however lag behind the actual state of the device.

Enumeration Type Documentation

◆ libevdev_read_flag

Enumerator
LIBEVDEV_READ_FLAG_SYNC 

Process data in sync mode.

LIBEVDEV_READ_FLAG_NORMAL 

Process data in normal mode.

LIBEVDEV_READ_FLAG_FORCE_SYNC 

Pretend the next event is a SYN_DROPPED and require the caller to sync.

LIBEVDEV_READ_FLAG_BLOCKING 

The fd is not in O_NONBLOCK and a read may block.

◆ libevdev_read_status

Enumerator
LIBEVDEV_READ_STATUS_SUCCESS 

libevdev_next_event() has finished without an error and an event is available for processing.

See also
libevdev_next_event
LIBEVDEV_READ_STATUS_SYNC 

Depending on the libevdev_next_event() read flag:

  • libevdev received a SYN_DROPPED from the device, and the caller should now resync the device, or,
  • an event has been read in sync mode.
See also
libevdev_next_event

Function Documentation

◆ libevdev_has_event_pending()

int libevdev_has_event_pending ( struct libevdev *  dev)

Check if there are events waiting for us.

This function does not read an event off the fd and may not access the fd at all. If there are events queued internally this function will return non-zero. If the internal queue is empty, this function will poll the file descriptor for data.

This is a convenience function for simple processes, most complex programs are expected to use select(2) or poll(2) on the file descriptor. The kernel guarantees that if data is available, it is a multiple of sizeof(struct input_event), and thus calling libevdev_next_event() when select(2) or poll(2) return is safe. You do not need libevdev_has_event_pending() if you're using select(2) or poll(2).

Parameters
devThe evdev device, already initialized with libevdev_set_fd()
Returns
On failure, a negative errno is returned.
Return values
0No event is currently available
1One or more events are available on the fd
Note
This function is signal-safe.

◆ libevdev_next_event()

int libevdev_next_event ( struct libevdev *  dev,
unsigned int  flags,
struct input_event *  ev 
)

Get the next event from the device.

This function operates in two different modes: normal mode or sync mode.

In normal mode (when flags has LIBEVDEV_READ_FLAG_NORMAL set), this function returns LIBEVDEV_READ_STATUS_SUCCESS and returns the event in the argument ev. If no events are available at this time, it returns -EAGAIN and ev is undefined.

If the current event is an EV_SYN SYN_DROPPED event, this function returns LIBEVDEV_READ_STATUS_SYNC and ev is set to the EV_SYN event. The caller should now call this function with the LIBEVDEV_READ_FLAG_SYNC flag set, to get the set of events that make up the device state delta. This function returns LIBEVDEV_READ_STATUS_SYNC for each event part of that delta, until it returns -EAGAIN once all events have been synced. For more details on what libevdev does to sync after a SYN_DROPPED event, see SYN_DROPPED handling.

If a device needs to be synced by the caller but the caller does not call with the LIBEVDEV_READ_FLAG_SYNC flag set, all events from the diff are dropped after libevdev updates its internal state and event processing continues as normal. Note that the current slot and the state of touch points may have updated during the SYN_DROPPED event, it is strongly recommended that a caller ignoring all sync events calls libevdev_get_current_slot() and checks the ABS_MT_TRACKING_ID values for all slots.

If a device has changed state without events being enqueued in libevdev, e.g. after changing the file descriptor, use the LIBEVDEV_READ_FLAG_FORCE_SYNC flag. This triggers an internal sync of the device and libevdev_next_event() returns LIBEVDEV_READ_STATUS_SYNC. Any state changes are available as events as described above. If LIBEVDEV_READ_FLAG_FORCE_SYNC is set, the value of ev is undefined.

Parameters
devThe evdev device, already initialized with libevdev_set_fd()
flagsSet of flags to determine behaviour. If LIBEVDEV_READ_FLAG_NORMAL is set, the next event is read in normal mode. If LIBEVDEV_READ_FLAG_SYNC is set, the next event is read in sync mode.
evOn success, set to the current event.
Returns
On failure, a negative errno is returned.
Return values
LIBEVDEV_READ_STATUS_SUCCESSOne or more events were read of the device and ev points to the next event in the queue
-EAGAINNo events are currently available on the device
LIBEVDEV_READ_STATUS_SYNCA SYN_DROPPED event was received, or a synced event was returned and ev points to the SYN_DROPPED event
Note
This function is signal-safe.