pub struct Device { /* private fields */ }
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
impl Device
sourcepub fn open(path: impl AsRef<Path>) -> Result<Device>
pub fn open(path: impl AsRef<Path>) -> Result<Device>
Opens a device, given its system path.
Paths are typically something like /dev/input/event0
.
sourcepub fn cached_state(&self) -> &DeviceState
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.
sourcepub fn physical_path(&self) -> Option<&str>
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.
sourcepub fn unique_name(&self) -> Option<&str>
pub fn unique_name(&self) -> Option<&str>
Returns the user-defined “unique name” of the device, if one has been set.
sourcepub fn input_id(&self) -> InputId
pub fn input_id(&self) -> InputId
Returns a struct containing bustype, vendor, product, and version identifiers
sourcepub fn get_auto_repeat(&self) -> Option<AutoRepeat>
pub fn get_auto_repeat(&self) -> Option<AutoRepeat>
Returns a struct containing the delay and period for auto repeat
sourcepub fn update_auto_repeat(&mut self, repeat: &AutoRepeat) -> Result<()>
pub fn update_auto_repeat(&mut self, repeat: &AutoRepeat) -> Result<()>
Update the delay and period for autorepeat
sourcepub fn get_scancode_by_keycode(&self, keycode: Key) -> Result<Vec<u8>>
pub fn get_scancode_by_keycode(&self, keycode: Key) -> Result<Vec<u8>>
Retrieve the scancode for a keycode, if any
sourcepub fn get_scancode_by_index(&self, index: u16) -> Result<(u32, Vec<u8>)>
pub fn get_scancode_by_index(&self, index: u16) -> Result<(u32, Vec<u8>)>
Retrieve the keycode and scancode by index, starting at 0
sourcepub fn update_scancode(&self, keycode: Key, scancode: &[u8]) -> Result<Key>
pub fn update_scancode(&self, keycode: Key, scancode: &[u8]) -> Result<Key>
Update a scancode. The return value is the previous keycode
sourcepub fn update_scancode_by_index(
&self,
index: u16,
keycode: Key,
scancode: &[u8]
) -> Result<u32>
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
sourcepub fn properties(&self) -> &AttributeSetRef<PropType>
pub fn properties(&self) -> &AttributeSetRef<PropType>
Returns the set of supported “properties” for the device (see INPUT_PROP_*
in kernel headers)
sourcepub fn driver_version(&self) -> (u8, u8, u8)
pub fn driver_version(&self) -> (u8, u8, u8)
Returns a tuple of the driver version containing major, minor, rev
sourcepub fn supported_events(&self) -> &AttributeSetRef<EventType>
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.
sourcepub fn supported_keys(&self) -> Option<&AttributeSetRef<Key>>
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));
sourcepub fn supported_relative_axes(
&self
) -> Option<&AttributeSetRef<RelativeAxisType>>
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));
sourcepub fn supported_absolute_axes(
&self
) -> Option<&AttributeSetRef<AbsoluteAxisType>>
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));
sourcepub fn supported_switches(&self) -> Option<&AttributeSetRef<SwitchType>>
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));
sourcepub fn supported_leds(&self) -> Option<&AttributeSetRef<LedType>>
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.
sourcepub fn misc_properties(&self) -> Option<&AttributeSetRef<MiscType>>
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.
sourcepub fn supported_ff(&self) -> Option<&AttributeSetRef<FFEffectType>>
pub fn supported_ff(&self) -> Option<&AttributeSetRef<FFEffectType>>
Returns the set of supported force feedback effects supported by a device.
sourcepub fn max_ff_effects(&self) -> usize
pub fn max_ff_effects(&self) -> usize
Returns the maximum number of force feedback effects that can be played simultaneously.
sourcepub fn supported_sounds(&self) -> Option<&AttributeSetRef<SoundType>>
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.
sourcepub fn get_key_state(&self) -> Result<AttributeSet<Key>>
pub fn get_key_state(&self) -> Result<AttributeSet<Key>>
Retrieve the current keypress state directly via kernel syscall.
sourcepub fn get_abs_state(&self) -> Result<[input_absinfo; 64]>
pub fn get_abs_state(&self) -> Result<[input_absinfo; 64]>
Retrieve the current absolute axis state directly via kernel syscall.
sourcepub fn get_switch_state(&self) -> Result<AttributeSet<SwitchType>>
pub fn get_switch_state(&self) -> Result<AttributeSet<SwitchType>>
Retrieve the current switch state directly via kernel syscall.
sourcepub fn get_led_state(&self) -> Result<AttributeSet<LedType>>
pub fn get_led_state(&self) -> Result<AttributeSet<LedType>>
Retrieve the current LED state directly via kernel syscall.
sourcepub fn fetch_events(&mut self) -> Result<FetchEventsSynced<'_>>
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.
pub fn into_event_stream(self) -> Result<EventStream>
tokio
only.sourcepub fn grab(&mut self) -> Result<()>
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.
sourcepub fn send_events(&mut self, events: &[InputEvent]) -> Result<()>
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).
sourcepub fn upload_ff_effect(&mut self, data: FFEffectData) -> Result<FFEffect>
pub fn upload_ff_effect(&mut self, data: FFEffectData) -> Result<FFEffect>
Uploads a force feedback effect to the device.
sourcepub fn set_ff_gain(&mut self, value: u16) -> Result<()>
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.
sourcepub fn set_ff_autocenter(&mut self, value: u16) -> Result<()>
pub fn set_ff_autocenter(&mut self, value: u16) -> Result<()>
Enables or disables autocenter for the force feedback device.
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> FmtForward for T
impl<T> FmtForward for T
source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R ) -> R
source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
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
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
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
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.source§impl<T> Tap for T
impl<T> Tap for T
source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moresource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moresource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moresource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moresource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moresource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moresource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.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
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.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
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.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
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.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
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.