EventReader

Struct EventReader 

Source
pub struct EventReader { /* private fields */ }
Expand description

Stores a userspace view of a device, and reads events emitted by it.

Created by Evdev::into_reader.

This is the recommended way of ingesting input events from an evdev.

In addition to reading the raw events emitted by the device, EventReader will:

  • Keep a view of the current device state that the user can query.
  • Fetch the current device state on creation and when a SYN_DROPPED event is received (indicating that one or more events have been lost due to the buffer filling up).
  • Synthesize events so that the consumer will see an up-to-date state.

The current device state from the EventReader’s PoV can be queried via EventReader::key_state, EventReader::abs_state, EventReader::slot_state, and similar methods. These methods are faster than the equivalent methods on Evdev, since they do not have to perform a system call to fetch the data (they just return data already stored in the EventReader). The reader’s view of the device state is automatically updated as events are pulled from it, but can also be manually updated by calling EventReader::update, which will pull and discard all available events.

Implementations§

Source§

impl EventReader

Source

pub fn into_evdev(self) -> Evdev

Destroys this EventReader and returns the original Evdev.

This will drop all input events buffered in the EventReader.

Source

pub fn evdev(&self) -> &Evdev

Returns a reference to the Evdev this EventReader was created from.

Source

pub fn update(&mut self) -> Result<()>

Update the local device state by reading all available events from the kernel, and discarding them.

This does not block.

This method can be used when the application isn’t interested in processing events or reports itself, and only wants to know what the current state of the input device is. EventReader::update is potentially faster than calling Evdev::key_state and other Evdev getters, since each of the Evdev getters perform a syscall.

After a call to EventReader::update, the up-to-date device state can be retrieved with the EventReader::key_state, EventReader::led_state, and other EventReader methods without incurring any additional syscalls.

Source

pub fn key_state(&self) -> &BitSet<Key>

Returns a BitSet of all Keys that are currently pressed.

Source

pub fn led_state(&self) -> &BitSet<Led>

Returns a BitSet of all Leds that are currently on.

Source

pub fn sound_state(&self) -> &BitSet<Sound>

Returns a BitSet of all Sounds that have been requested to play.

Source

pub fn switch_state(&self) -> &BitSet<Switch>

Returns a BitSet of all Switches that are currently active or closed.

Source

pub fn abs_state(&self, abs: Abs) -> i32

Returns the current value of an absolute axis.

abs must be less than Abs::MT_SLOT, or this method will panic. To access multitouch slots, use EventReader::slot_state instead.

Call EventReader::update, or drain incoming events using the iterator interface in order to update the multitouch slot state.

Source

pub fn valid_slots(&self) -> impl Iterator<Item = Slot> + '_

Returns an iterator that yields all Slots that have valid data in them.

A Slot is considered valid if its value of Abs::MT_TRACKING_ID is non-negative.

Call EventReader::update, or drain incoming events using the iterator interface in order to update the multitouch slot state.

Source

pub fn slot_state(&self, slot: impl TryInto<Slot>, code: Abs) -> Option<i32>

Returns an Abs axis value for a multitouch slot.

code must be one of the Abs::MT_* codes (but not Abs::MT_SLOT), as only those are associated with a multitouch slot. Non-MT Abs codes can be queried via EventReader::abs_state.

Returns None if code isn’t advertised by the device (ie. the property does not exist) or if slot is out of range (ie. the device does not have the requested slot).

If slot isn’t valid (yielded by EventReader::valid_slots), invalid stale data may be returned.

Source

pub fn current_slot(&self) -> Slot

Returns the currently selected multitouch slot.

Events with ABS_MT_* code affect this slot, but not other slots.

Source

pub fn events(&mut self) -> Events<'_>

Returns an iterator over incoming events.

Events read from the iterator will automatically update the state of the EventReader.

If the underlying device is in non-blocking mode, the iterator will return None when no more events are available. If the device is not in non-blocking mode, the iterator will block until more events arrive.

Note: Retrieving an event with this iterator will remove that event from the Report it belongs to if that report is later fetched with EventReader::reports. It is best to stick to either per-event or per-report processing in your program to avoid this.

Source

pub fn reports(&mut self) -> Reports<'_>

Returns an iterator over incoming device reports.

Reports are groups of InputEvents that belong together.

If the underlying device is in non-blocking mode, the iterator will return None when no more events are available. If the device is not in non-blocking mode, the iterator will block until more events arrive.

Note: Retrieving an event individually (for example, via EventReader::events) will remove that event from the Report it belongs to if that report is later fetched with EventReader::reports. It is best to stick to either per-event or per-report processing in your program to avoid this.

Source

pub fn async_events(&mut self) -> Result<AsyncEvents<'_>>

Available on crate features tokio or async-io only.

Returns an async iterator over incoming events.

Events read from the iterator will automatically update the state of the EventReader.

The underlying device will be put in non-blocking mode while the returned AsyncEvents is alive (if it isn’t already).

When using the "tokio" Cargo feature, this must be called while inside a tokio context.

Source

pub fn async_reports(&mut self) -> Result<AsyncReports<'_>>

Available on crate features tokio or async-io only.

Returns an async iterator over incoming device reports.

The underlying device will be put in non-blocking mode while the returned AsyncReports is alive (if it isn’t already).

When using the "tokio" Cargo feature, this must be called while inside a tokio context.

Trait Implementations§

Source§

impl AsFd for EventReader

Source§

fn as_fd(&self) -> BorrowedFd<'_>

Borrows the file descriptor. Read more
Source§

impl AsRawFd for EventReader

Source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
Source§

impl Debug for EventReader

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> IntoIterator for &'a mut EventReader

Source§

type Item = Result<InputEvent, Error>

The type of the elements being iterated over.
Source§

type IntoIter = Events<'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl IntoIterator for EventReader

Source§

type Item = Result<InputEvent, Error>

The type of the elements being iterated over.
Source§

type IntoIter = IntoEvents

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl IntoRawFd for EventReader

Source§

fn into_raw_fd(self) -> RawFd

Consumes this object, returning the raw underlying file descriptor. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsSource for T
where T: AsFd,

Source§

fn source(&self) -> BorrowedFd<'_>

Returns the borrowed file descriptor.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more