-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eff6913
commit 0c8ae6e
Showing
3 changed files
with
90 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,99 @@ | ||
use crate::layout::CustomEvent; | ||
use keyberon::layout::CustomEvent as KbCustomEvent; | ||
use usbd_hid::descriptor::MouseReport; | ||
|
||
/// Mouse handler | ||
#[derive(Debug, Default)] | ||
pub struct MouseHandler { | ||
/// X position | ||
pub x: i8, | ||
/// Y position | ||
pub y: i8, | ||
/// Buttons | ||
pub buttons: u8, | ||
/// Going Up | ||
pub up: bool, | ||
/// Going Down | ||
pub down: bool, | ||
/// Going Left | ||
pub left: bool, | ||
/// Going Right | ||
pub right: bool, | ||
|
||
/// Left click is pressed | ||
pub left_click: bool, | ||
/// Right click is pressed | ||
pub right_click: bool, | ||
/// Middle click is pressed | ||
pub middle_click: bool, | ||
|
||
/// Wheel up | ||
pub wheel_up: bool, | ||
/// Wheel down | ||
pub wheel_down: bool, | ||
|
||
/// How fast the mouse moves | ||
pub rate: i8, | ||
} | ||
|
||
impl MouseHandler { | ||
/// Create a new mouse handler | ||
pub fn new() -> Self { | ||
MouseHandler { | ||
x: 0, | ||
y: 0, | ||
buttons: 0, | ||
rate: 1, | ||
..Default::default() | ||
} | ||
} | ||
|
||
pub fn new_event(&mut self, kb_cs_event: keyberon::layout::CustomEvent<CustomEvent>) { | ||
if let Some((event, _is_pressed)) = match kb_cs_event { | ||
keyberon::layout::CustomEvent::Press(event) => Some((event, true)), | ||
keyberon::layout::CustomEvent::Release(event) => Some((event, false)), | ||
/// Process a custom event | ||
pub fn process_event(&mut self, kb_cs_event: keyberon::layout::CustomEvent<CustomEvent>) { | ||
if let Some((event, is_pressed)) = match kb_cs_event { | ||
KbCustomEvent::Press(event) => Some((event, true)), | ||
KbCustomEvent::Release(event) => Some((event, false)), | ||
_ => None, | ||
} { | ||
defmt::info!( | ||
"Mouse event: {:?} (is_pressed:{:?})", | ||
defmt::Debug2Format(&event), | ||
is_pressed | ||
); | ||
match event { | ||
CustomEvent::MouseNorth => self.y -= 1, | ||
CustomEvent::MouseNorthEast => { | ||
self.x += 1; | ||
self.y -= 1; | ||
} | ||
CustomEvent::MouseEast => self.x += 1, | ||
CustomEvent::MouseSouthEast => { | ||
self.x += 1; | ||
self.y += 1; | ||
} | ||
CustomEvent::MouseSouth => self.y += 1, | ||
CustomEvent::MouseSouthWest => { | ||
self.x -= 1; | ||
self.y += 1; | ||
} | ||
CustomEvent::MouseWest => self.x -= 1, | ||
CustomEvent::MouseNorthWest => { | ||
self.x -= 1; | ||
self.y -= 1; | ||
} | ||
CustomEvent::MouseLeftClick => self.buttons |= 1, | ||
CustomEvent::MouseRightClick => self.buttons |= 2, | ||
CustomEvent::MouseMiddleClick => self.buttons |= 4, | ||
CustomEvent::MouseScrollUp => self.buttons |= 8, | ||
CustomEvent::MouseScrollDown => self.buttons |= 16, | ||
CustomEvent::MouseUp => self.up = is_pressed, | ||
CustomEvent::MouseDown => self.down = is_pressed, | ||
CustomEvent::MouseRight => self.right = is_pressed, | ||
CustomEvent::MouseLeft => self.left = is_pressed, | ||
CustomEvent::MouseLeftClick => self.left_click = is_pressed, | ||
CustomEvent::MouseRightClick => self.right_click = is_pressed, | ||
CustomEvent::MouseMiddleClick => self.middle_click = is_pressed, | ||
CustomEvent::MouseScrollUp => self.wheel_up = is_pressed, | ||
CustomEvent::MouseScrollDown => self.wheel_down = is_pressed, | ||
} | ||
} | ||
} | ||
|
||
/// Compute the state of the mouse | ||
pub fn tick(&mut self) {} | ||
|
||
/// Generate a HID report for the mouse | ||
pub fn generate_hid_report(&mut self) -> MouseReport { | ||
let mut report = MouseReport::default(); | ||
report.x = self.x; | ||
report.y = self.y; | ||
report.buttons = self.buttons; | ||
self.x = 0; | ||
self.y = 0; | ||
self.buttons = 0; | ||
if self.up { | ||
report.y = self.rate; | ||
} else if self.down { | ||
report.y = -self.rate; | ||
} | ||
if self.left { | ||
report.x = -self.rate; | ||
} else if self.right { | ||
report.x = self.rate; | ||
} | ||
if self.left_click { | ||
report.buttons |= 1; | ||
} | ||
if self.right_click { | ||
report.buttons |= 2; | ||
} | ||
if self.middle_click { | ||
report.buttons |= 4; | ||
} | ||
if self.wheel_up { | ||
report.wheel = 1; | ||
} else if self.wheel_down { | ||
report.wheel = -1; | ||
} | ||
report | ||
} | ||
} |