From ecd7d1a7e4084b6a30c8d50b00f41d783923d682 Mon Sep 17 00:00:00 2001 From: Taiki Sugawara Date: Wed, 25 Aug 2021 12:05:17 +0900 Subject: [PATCH] Implement grab and ungrab --- src/raw_stream.rs | 22 ++++++++++++++++++++++ src/sync_stream.rs | 13 +++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/raw_stream.rs b/src/raw_stream.rs index 00579b0..984a113 100644 --- a/src/raw_stream.rs +++ b/src/raw_stream.rs @@ -66,6 +66,7 @@ pub struct RawDevice { // ff_stat: Option, supported_snd: Option>, pub(crate) event_buf: Vec, + grabbed: bool, } #[derive(Debug, Clone)] @@ -222,6 +223,7 @@ impl RawDevice { supported_snd, auto_repeat, event_buf: Vec::new(), + grabbed: false, }) } @@ -588,6 +590,26 @@ impl RawDevice { pub fn into_event_stream(self) -> io::Result { EventStream::new(self) } + + pub fn grab(&mut self) -> io::Result<()> { + if !self.grabbed { + unsafe { + sys::eviocgrab(self.as_raw_fd(), 1)?; + } + self.grabbed = true; + } + Ok(()) + } + + pub fn ungrab(&mut self) -> io::Result<()> { + if self.grabbed { + unsafe { + sys::eviocgrab(self.as_raw_fd(), 0)?; + } + self.grabbed = false; + } + Ok(()) + } } impl AsRawFd for RawDevice { diff --git a/src/sync_stream.rs b/src/sync_stream.rs index 564e1ca..14dba2f 100644 --- a/src/sync_stream.rs +++ b/src/sync_stream.rs @@ -329,6 +329,19 @@ impl Device { pub fn into_event_stream(self) -> io::Result { EventStream::new(self) } + + /// Grab the device through a kernel syscall. + /// + /// This prevents other clients (including kernel-internal ones such as rfkill) from receiving + /// events from this device. + pub fn grab(&mut self) -> io::Result<()> { + self.raw.grab() + } + + /// Ungrab the device through a kernel syscall. + pub fn ungrab(&mut self) -> io::Result<()> { + self.raw.ungrab() + } } impl AsRawFd for Device {