Skip to content

Commit

Permalink
mouse: keep a state
Browse files Browse the repository at this point in the history
  • Loading branch information
borisfaure committed Apr 24, 2024
1 parent eff6913 commit 0c8ae6e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 76 deletions.
26 changes: 9 additions & 17 deletions src/keymap_borisfaure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,21 +305,13 @@ const T_9: Action<CustomEvent> = seq(&[Press(LCtrl), Tap(A), Release(LCtrl), Tap
const T_0: Action<CustomEvent> = seq(&[Press(LCtrl), Tap(A), Release(LCtrl), Tap(Kb0)].as_slice());

/// Mouse move up
const MN: Action<CustomEvent> = Action::Custom(MouseNorth);
/// Mouse move up and right
const MNE: Action<CustomEvent> = Action::Custom(MouseNorthEast);
/// Mouse move right
const ME: Action<CustomEvent> = Action::Custom(MouseEast);
/// Mouse move down and right
const MSE: Action<CustomEvent> = Action::Custom(MouseSouthEast);
const MU: Action<CustomEvent> = Action::Custom(MouseUp);
/// Mouse move down
const MS: Action<CustomEvent> = Action::Custom(MouseSouth);
/// Mouse move down and left
const MSW: Action<CustomEvent> = Action::Custom(MouseSouthWest);
const MD: Action<CustomEvent> = Action::Custom(MouseDown);
/// Mouse move left
const MW: Action<CustomEvent> = Action::Custom(MouseWest);
/// Mouse move up and left
const MNW: Action<CustomEvent> = Action::Custom(MouseNorthWest);
const ML: Action<CustomEvent> = Action::Custom(MouseLeft);
/// Mouse move right
const MR: Action<CustomEvent> = Action::Custom(MouseRight);
/// Mouse left click
const MLC: Action<CustomEvent> = Action::Custom(MouseLeftClick);
/// Mouse right click
Expand Down Expand Up @@ -354,10 +346,10 @@ pub static LAYERS: keyberon::layout::Layers<10, 4, 9, CustomEvent> = keyberon::l
[ 0 1 2 3 - * F5 F6 F7 F8 ],
[ , 7 8 9 + + F9 F10 F11 F12 ],
[ t {VUNNUM} {UNNUM} {HT_1_SP} Tab Enter {HT_2_BS} n t t ],
} { /* 4: MISC and MOUSE */
[ Pause {GAME} {COLEMAN_DH} {QWERTY} n {MSU} {MNW} {MN} {MNE} n ],
[ n VolDown Mute VolUp n n {MW} {MLC} {ME} n ],
[ n MediaPreviousSong MediaPlayPause MediaNextSong n {MSD} {MSW} {MS} {MSE} n ],
} { /* 4: MISC and Mouse */
[ Pause {GAME} {COLEMAN_DH} {QWERTY} n {MSU} n n n n ],
[ n VolDown Mute VolUp n {ML} {MD} {MU} {MR} n ],
[ n MediaPreviousSong MediaPlayPause MediaNextSong n {MSD} n n n n ],
[ t t n n n {MLC} {MMC} {MRC} t t ],
} { /* 5: TMUX */
[ {T_6} {T_7} {T_8} {T_9} {T_0} {T_1} {T_2} {T_3} {T_4} {T_5} ],
Expand Down
22 changes: 8 additions & 14 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,13 @@ pub static LAYOUT_CHANNEL: Channel<CriticalSectionRawMutex, Event, NB_EVENTS> =
/// Custom events for the layout, mostly mouse events
pub enum CustomEvent {
/// Mouse move up
MouseNorth,
/// Mouse move up and right
MouseNorthEast,
/// Mouse move right
MouseEast,
/// Mouse move down and right
MouseSouthEast,
MouseUp,
/// Mouse move Right
MouseRight,
/// Mouse move down
MouseSouth,
/// Mouse move down and left
MouseSouthWest,
MouseDown,
/// Mouse move left
MouseWest,
/// Mouse move up and left
MouseNorthWest,
MouseLeft,
/// Mouse left click
MouseLeftClick,
/// Mouse right click
Expand Down Expand Up @@ -103,9 +95,11 @@ pub async fn layout_handler() {
HID_KB_CHANNEL.send(kb_report).await;
old_kb_report = kb_report;
}
mouse.new_event(custom_event);
mouse.process_event(custom_event);
mouse.tick();
let mouse_report = mouse.generate_hid_report();
if mouse_report != old_mouse_report {
defmt::info!("Mouse Report: {:?}", defmt::Debug2Format(&mouse_report));
HID_MOUSE_CHANNEL.send(mouse_report).await;
old_mouse_report = mouse_report;
}
Expand Down
118 changes: 73 additions & 45 deletions src/mouse.rs
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
}
}

0 comments on commit 0c8ae6e

Please sign in to comment.