Implement grab and ungrab

This commit is contained in:
Taiki Sugawara 2021-08-25 12:05:17 +09:00 committed by Jeff Hiner
parent a458675fb8
commit ecd7d1a7e4
2 changed files with 35 additions and 0 deletions

View file

@ -66,6 +66,7 @@ pub struct RawDevice {
// ff_stat: Option<FFStatus>, // ff_stat: Option<FFStatus>,
supported_snd: Option<AttributeSet<SoundType>>, supported_snd: Option<AttributeSet<SoundType>>,
pub(crate) event_buf: Vec<libc::input_event>, pub(crate) event_buf: Vec<libc::input_event>,
grabbed: bool,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -222,6 +223,7 @@ impl RawDevice {
supported_snd, supported_snd,
auto_repeat, auto_repeat,
event_buf: Vec::new(), event_buf: Vec::new(),
grabbed: false,
}) })
} }
@ -588,6 +590,26 @@ impl RawDevice {
pub fn into_event_stream(self) -> io::Result<EventStream> { pub fn into_event_stream(self) -> io::Result<EventStream> {
EventStream::new(self) 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 { impl AsRawFd for RawDevice {

View file

@ -329,6 +329,19 @@ impl Device {
pub fn into_event_stream(self) -> io::Result<EventStream> { pub fn into_event_stream(self) -> io::Result<EventStream> {
EventStream::new(self) 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 { impl AsRawFd for Device {