Struct evdev::Device

source ·
pub struct Device { /* private fields */ }
Available on Unix only.
Expand description

A physical or virtual device supported by evdev.

Each device corresponds to a path typically found in /dev/input, and supports access via one or more “types”. For example, an optical mouse has buttons that are represented by “keys”, and reflects changes in its position via “relative axis” reports.

This type specifically is a wrapper over RawDevice,that synchronizes with the kernel’s state when events are dropped.

If fetch_events() isn’t called often enough and the kernel drops events from its internal buffer, synthetic events will be injected into the iterator returned by fetch_events() and Device::cached_state() will be kept up to date when fetch_events() is called.

Implementations§

source§

impl Device

source

pub fn open(path: impl AsRef<Path>) -> Result<Device>

Opens a device, given its system path.

Paths are typically something like /dev/input/event0.

source

pub fn cached_state(&self) -> &DeviceState

Returns the synchronization engine’s current understanding (cache) of the device state.

Note that this represents the internal cache of the synchronization engine as of the last entry that was pulled out. The advantage to calling this instead of invoking get_key_state and the like directly is speed: because reading this cache doesn’t require any syscalls it’s easy to do inside a tight loop. The downside is that if the stream is not being driven quickly, this can very quickly get desynchronized from the kernel and provide inaccurate data.

source

pub fn name(&self) -> Option<&str>

Returns the device’s name as read from the kernel.

source

pub fn physical_path(&self) -> Option<&str>

Returns the device’s physical location, either as set by the caller or as read from the kernel.

source

pub fn unique_name(&self) -> Option<&str>

Returns the user-defined “unique name” of the device, if one has been set.

source

pub fn input_id(&self) -> InputId

Returns a struct containing bustype, vendor, product, and version identifiers

source

pub fn get_auto_repeat(&self) -> Option<AutoRepeat>

Returns a struct containing the delay and period for auto repeat

source

pub fn update_auto_repeat(&mut self, repeat: &AutoRepeat) -> Result<()>

Update the delay and period for autorepeat

source

pub fn get_scancode_by_keycode(&self, keycode: Key) -> Result<Vec<u8>>

Retrieve the scancode for a keycode, if any

source

pub fn get_scancode_by_index(&self, index: u16) -> Result<(u32, Vec<u8>)>

Retrieve the keycode and scancode by index, starting at 0

source

pub fn update_scancode(&self, keycode: Key, scancode: &[u8]) -> Result<Key>

Update a scancode. The return value is the previous keycode

source

pub fn update_scancode_by_index( &self, index: u16, keycode: Key, scancode: &[u8] ) -> Result<u32>

Update a scancode by index. The return value is the previous keycode

source

pub fn properties(&self) -> &AttributeSetRef<PropType>

Returns the set of supported “properties” for the device (see INPUT_PROP_* in kernel headers)

source

pub fn driver_version(&self) -> (u8, u8, u8)

Returns a tuple of the driver version containing major, minor, rev

source

pub fn supported_events(&self) -> &AttributeSetRef<EventType>

Returns a set of the event types supported by this device (Key, Switch, etc)

If you’re interested in the individual keys or switches supported, it’s probably easier to just call the appropriate supported_* function instead.

source

pub fn supported_keys(&self) -> Option<&AttributeSetRef<Key>>

Returns the set of supported keys reported by the device.

For keyboards, this is the set of all possible keycodes the keyboard may emit. Controllers, mice, and other peripherals may also report buttons as keys.

§Examples
use evdev::{Device, Key};
let device = Device::open("/dev/input/event0")?;

// Does this device have an ENTER key?
let supported = device.supported_keys().map_or(false, |keys| keys.contains(Key::KEY_ENTER));
source

pub fn supported_relative_axes( &self ) -> Option<&AttributeSetRef<RelativeAxisType>>

Returns the set of supported “relative axes” reported by the device.

Standard mice will generally report REL_X and REL_Y along with wheel if supported.

§Examples
use evdev::{Device, RelativeAxisType};
let device = Device::open("/dev/input/event0")?;

// Does the device have a scroll wheel?
let supported = device
    .supported_relative_axes()
    .map_or(false, |axes| axes.contains(RelativeAxisType::REL_WHEEL));
source

pub fn supported_absolute_axes( &self ) -> Option<&AttributeSetRef<AbsoluteAxisType>>

Returns the set of supported “absolute axes” reported by the device.

These are most typically supported by joysticks and touchpads.

§Examples
use evdev::{Device, AbsoluteAxisType};
let device = Device::open("/dev/input/event0")?;

// Does the device have an absolute X axis?
let supported = device
    .supported_absolute_axes()
    .map_or(false, |axes| axes.contains(AbsoluteAxisType::ABS_X));
source

pub fn supported_switches(&self) -> Option<&AttributeSetRef<SwitchType>>

Returns the set of supported switches reported by the device.

These are typically used for things like software switches on laptop lids (which the system reacts to by suspending or locking), or virtual switches to indicate whether a headphone jack is plugged in (used to disable external speakers).

§Examples
use evdev::{Device, SwitchType};
let device = Device::open("/dev/input/event0")?;

// Does the device report a laptop lid switch?
let supported = device
    .supported_switches()
    .map_or(false, |axes| axes.contains(SwitchType::SW_LID));
source

pub fn supported_leds(&self) -> Option<&AttributeSetRef<LedType>>

Returns a set of supported LEDs on the device.

Most commonly these are state indicator lights for things like Scroll Lock, but they can also be found in cameras and other devices.

source

pub fn misc_properties(&self) -> Option<&AttributeSetRef<MiscType>>

Returns a set of supported “miscellaneous” capabilities.

Aside from vendor-specific key scancodes, most of these are uncommon.

source

pub fn supported_ff(&self) -> Option<&AttributeSetRef<FFEffectType>>

Returns the set of supported force feedback effects supported by a device.

source

pub fn max_ff_effects(&self) -> usize

Returns the maximum number of force feedback effects that can be played simultaneously.

source

pub fn supported_sounds(&self) -> Option<&AttributeSetRef<SoundType>>

Returns the set of supported simple sounds supported by a device.

You can use these to make really annoying beep sounds come from an internal self-test speaker, for instance.

source

pub fn get_key_state(&self) -> Result<AttributeSet<Key>>

Retrieve the current keypress state directly via kernel syscall.

source

pub fn get_abs_state(&self) -> Result<[input_absinfo; 64]>

Retrieve the current absolute axis state directly via kernel syscall.

source

pub fn get_switch_state(&self) -> Result<AttributeSet<SwitchType>>

Retrieve the current switch state directly via kernel syscall.

source

pub fn get_led_state(&self) -> Result<AttributeSet<LedType>>

Retrieve the current LED state directly via kernel syscall.

source

pub fn fetch_events(&mut self) -> Result<FetchEventsSynced<'_>>

Fetches and returns events from the kernel ring buffer, doing synchronization on SYN_DROPPED.

By default this will block until events are available. Typically, users will want to call this in a tight loop within a thread. Will insert “fake” events.

source

pub fn into_event_stream(self) -> Result<EventStream>

Available on crate feature tokio only.
source

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

Grab the device through a kernel syscall.

This prevents other clients (including kernel-internal ones such as rfkill) from receiving events from this device.

source

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

Ungrab the device through a kernel syscall.

source

pub fn send_events(&mut self, events: &[InputEvent]) -> Result<()>

Send an event to the device.

Events that are typically sent to devices are EventType::LED (turn device LEDs on and off), EventType::SOUND (play a sound on the device) and EventType::FORCEFEEDBACK (play force feedback effects on the device, i.e. rumble).

source

pub fn upload_ff_effect(&mut self, data: FFEffectData) -> Result<FFEffect>

Uploads a force feedback effect to the device.

source

pub fn set_ff_gain(&mut self, value: u16) -> Result<()>

Sets the force feedback gain, i.e. how strong the force feedback effects should be for the device. A gain of 0 means no gain, whereas u16::MAX is the maximum gain.

source

pub fn set_ff_autocenter(&mut self, value: u16) -> Result<()>

Enables or disables autocenter for the force feedback device.

Trait Implementations§

source§

impl AsRawFd for Device

source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
source§

impl Display for Device

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Device

§

impl RefUnwindSafe for Device

§

impl Send for Device

§

impl Sync for Device

§

impl Unpin for Device

§

impl UnwindSafe for Device

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> 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> Conv for T

source§

fn conv<T>(self) -> T
where Self: Into<T>,

Converts self into T using Into<T>. Read more
source§

impl<T> FmtForward for T

source§

fn fmt_binary(self) -> FmtBinary<Self>
where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
source§

fn fmt_display(self) -> FmtDisplay<Self>
where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
source§

fn fmt_lower_exp(self) -> FmtLowerExp<Self>
where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
source§

fn fmt_lower_hex(self) -> FmtLowerHex<Self>
where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
source§

fn fmt_octal(self) -> FmtOctal<Self>
where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
source§

fn fmt_pointer(self) -> FmtPointer<Self>
where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
source§

fn fmt_upper_exp(self) -> FmtUpperExp<Self>
where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
source§

fn fmt_upper_hex(self) -> FmtUpperHex<Self>
where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
source§

fn fmt_list(self) -> FmtList<Self>
where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Pipe for T
where T: ?Sized,

source§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
source§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
source§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
source§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
where Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
source§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R ) -> R
where Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
source§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
where Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows self, then passes self.as_ref() into the pipe function.
source§

fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
where Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.as_mut() into the pipe function.
source§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
where Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
source§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R ) -> R
where Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
source§

impl<T> Tap for T

source§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
source§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
source§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
source§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
source§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
source§

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
source§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
source§

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
source§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
source§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
source§

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Calls .tap_borrow() only in debug builds, and is erased in release builds.
source§

fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
source§

fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Calls .tap_ref() only in debug builds, and is erased in release builds.
source§

fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
source§

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
source§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T> TryConv for T

source§

fn try_conv<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. Read more
source§

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

§

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>,

§

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.