diff --git a/src/raw_stream.rs b/src/raw_stream.rs index 0a3f807..8714bda 100644 --- a/src/raw_stream.rs +++ b/src/raw_stream.rs @@ -662,6 +662,11 @@ impl RawDevice { Ok(()) } + /// Whether the device is currently grabbed for exclusive use or not. + pub fn is_grabbed(&self) -> bool { + self.grabbed + } + /// Send an event to the device. /// /// Events that are typically sent to devices are diff --git a/src/sync_stream.rs b/src/sync_stream.rs index c45c176..1c5c99b 100644 --- a/src/sync_stream.rs +++ b/src/sync_stream.rs @@ -27,6 +27,7 @@ use std::{fmt, io}; /// 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. +#[derive(Debug)] pub struct Device { raw: RawDevice, prev_state: DeviceState, @@ -383,6 +384,11 @@ impl Device { self.raw.ungrab() } + /// Whether the device is currently grabbed for exclusive use or not. + pub fn is_grabbed(&self) -> bool { + self.raw.is_grabbed() + } + /// Send an event to the device. /// /// Events that are typically sent to devices are @@ -410,6 +416,14 @@ impl Device { } } +impl Drop for Device { + fn drop(&mut self) { + if let Err(error) = self.ungrab() { + eprintln!("Failed to ungrab device: {error}"); + } + } +} + impl AsFd for Device { fn as_fd(&self) -> BorrowedFd<'_> { self.raw.as_fd()