-
Notifications
You must be signed in to change notification settings - Fork 920
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
On X11, filter out tiny device mouse events
Usually, if mouse events are equal to (0, 0) we filter them out. However, if the event is very close to zero it will still be given to the user. In some cases this can be caused by bad float math on the X11 server side. Fix it by filtering absolute values smaller than floating point epsilon. Signed-off-by: John Nunley <[email protected]> Closes: #3500
- Loading branch information
Showing
4 changed files
with
64 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//! Utilities for handling mouse events. | ||
/// Recorded mouse delta designed to filter out noise. | ||
pub struct Delta<T> { | ||
x: T, | ||
y: T, | ||
} | ||
|
||
impl<T: Default> Default for Delta<T> { | ||
fn default() -> Self { | ||
Self { | ||
x: Default::default(), | ||
y: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
impl<T: Default> Delta<T> { | ||
pub(crate) fn set_x(&mut self, x: T) { | ||
self.x = x; | ||
} | ||
|
||
pub(crate) fn set_y(&mut self, y: T) { | ||
self.y = y; | ||
} | ||
} | ||
|
||
macro_rules! consume { | ||
($this:expr, $ty:ty) => {{ | ||
let this = $this; | ||
let (x, y) = match (this.x.abs() < <$ty>::EPSILON, this.y.abs() < <$ty>::EPSILON) { | ||
(true, true) => return None, | ||
(true, false) => (this.x, 0.0), | ||
(false, true) => (0.0, this.y), | ||
(false, false) => (this.x, this.y), | ||
}; | ||
|
||
Some((x, y)) | ||
}}; | ||
} | ||
|
||
impl Delta<f32> { | ||
pub(crate) fn consume(self) -> Option<(f32, f32)> { | ||
consume!(self, f32) | ||
} | ||
} | ||
|
||
impl Delta<f64> { | ||
pub(crate) fn consume(self) -> Option<(f64, f64)> { | ||
consume!(self, f64) | ||
} | ||
} |