Skip to content

Commit c4fadbe

Browse files
tgrushkacoolreader18
authored andcommitted
impl Drop for Device to call Device::ungrab() on drop; derive Debug for Device; expose is_grabbed() on Device and RawDevice
1 parent 8a498d4 commit c4fadbe

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/raw_stream.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,11 @@ impl RawDevice {
662662
Ok(())
663663
}
664664

665+
/// Whether the device is currently grabbed for exclusive use or not.
666+
pub fn is_grabbed(&self) -> bool {
667+
self.grabbed
668+
}
669+
665670
/// Send an event to the device.
666671
///
667672
/// Events that are typically sent to devices are

src/sync_stream.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use std::{fmt, io};
2727
/// If `fetch_events()` isn't called often enough and the kernel drops events from its internal
2828
/// buffer, synthetic events will be injected into the iterator returned by `fetch_events()` and
2929
/// [`Device::cached_state()`] will be kept up to date when `fetch_events()` is called.
30+
#[derive(Debug)]
3031
pub struct Device {
3132
raw: RawDevice,
3233
prev_state: DeviceState,
@@ -383,6 +384,11 @@ impl Device {
383384
self.raw.ungrab()
384385
}
385386

387+
/// Whether the device is currently grabbed for exclusive use or not.
388+
pub fn is_grabbed(&self) -> bool {
389+
self.raw.is_grabbed()
390+
}
391+
386392
/// Send an event to the device.
387393
///
388394
/// Events that are typically sent to devices are
@@ -410,6 +416,14 @@ impl Device {
410416
}
411417
}
412418

419+
impl Drop for Device {
420+
fn drop(&mut self) {
421+
if let Err(error) = self.ungrab() {
422+
eprintln!("Failed to ungrab device: {error}");
423+
}
424+
}
425+
}
426+
413427
impl AsFd for Device {
414428
fn as_fd(&self) -> BorrowedFd<'_> {
415429
self.raw.as_fd()

0 commit comments

Comments
 (0)