From 70543731d1ccd0293e6880e829f33d5ad20db224 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 7 Apr 2026 00:37:15 +0800 Subject: [PATCH] add keyboard remapping that also tracks screen flipping --- libs/bao1x-api/src/keyboard.rs | 18 ++++ .../bao1x-hal-service/src/servers/keyboard.rs | 86 ++++++++++++++----- 2 files changed, 81 insertions(+), 23 deletions(-) diff --git a/libs/bao1x-api/src/keyboard.rs b/libs/bao1x-api/src/keyboard.rs index b83b69495..bfaeacf35 100644 --- a/libs/bao1x-api/src/keyboard.rs +++ b/libs/bao1x-api/src/keyboard.rs @@ -91,6 +91,10 @@ pub enum KeyboardOpcode { /// a blocking key listener - blocks until a key is hit BlockingKeyListener = 9, + + /// change orientation, and thus key mapping of directional keys. Behavior + /// depends on the hardware type selected. + SetOrientation = 128, } // this structure is used to register a keyboard listener. @@ -201,6 +205,20 @@ impl Keyboard { .unwrap(); } + pub fn flip_orientation(&self, flipped: bool) { + send_message( + self.conn, + Message::new_scalar( + KeyboardOpcode::SetOrientation.to_usize().unwrap(), + if flipped { 1 } else { 0 }, + 0, + 0, + 0, + ), + ) + .unwrap(); + } + /// Reveal the connection ID for use with unsafe FFI calls pub fn conn(&self) -> xous::CID { self.conn } } diff --git a/services/bao1x-hal-service/src/servers/keyboard.rs b/services/bao1x-hal-service/src/servers/keyboard.rs index 7d1a2158b..03b963c23 100644 --- a/services/bao1x-hal-service/src/servers/keyboard.rs +++ b/services/bao1x-hal-service/src/servers/keyboard.rs @@ -62,10 +62,18 @@ struct KeyTracker { pub rate_ms: usize, pub delay_ms: usize, pub keys: Vec, + pub orientation_flipped: bool, } #[cfg(feature = "board-baosec")] impl KeyTracker { - pub fn new() -> Self { KeyTracker { rate_ms: KEYUP_DELAY_MS as usize, delay_ms: 1000, keys: Vec::new() } } + pub fn new() -> Self { + KeyTracker { + rate_ms: KEYUP_DELAY_MS as usize, + delay_ms: 1000, + keys: Vec::new(), + orientation_flipped: false, + } + } pub fn register_key_down(&mut self, key: KeyPress, ts: u64) { if let Some(entry) = self.keys.iter_mut().find(|e| e.kp == key) { @@ -113,11 +121,13 @@ impl KeyTracker { /// Returns any keys that should have repeat key-press events generated pub fn get_repeats(&mut self, now: u64) -> Vec { let mut kps = Vec::new(); - for key in self.keys.iter_mut() { - if now >= key.next_repeat_time { - kps.push(map_keypress(key.kp)); + // use index-based iteration to avoid borrow checker issue on map_keypress() call + for i in 0..self.keys.len() { + if now >= self.keys[i].next_repeat_time { + let kp = self.keys[i].kp; + kps.push(self.map_keypress(kp)); // rebasing off of now prevents keys from "lagging on" in case of UI delay - key.next_repeat_time = now + self.rate_ms as u64; + self.keys[i].next_repeat_time = now + self.rate_ms as u64; } } kps @@ -126,23 +136,47 @@ impl KeyTracker { pub fn keys_pressed(&self) -> usize { self.keys.len() } pub fn clear_keys(&mut self) { self.keys.clear(); } -} -#[cfg(feature = "board-baosec")] -fn map_keypress(kp: KeyPress) -> char { - match kp { - KeyPress::Down => '↓', - KeyPress::Up => '↑', - KeyPress::Left => '←', - KeyPress::Right => '→', - KeyPress::Select => '∴', - // "Fire" is used as the mapping for the center instead of carriage return ('\r' (0xd)) - // because carriage return is reserved for the shell to indicate the end of line. Thus - // by mapping "fire" to the center key, we get a UI-specific action key without invoking - // shell commands in the background unintentionally. - KeyPress::Center => '🔥', - #[cfg(feature = "accel-kbd")] - KeyPress::Accel => '⏯', - _ => '\u{0000}', + + pub fn map_keypress(&self, kp: KeyPress) -> char { + match kp { + KeyPress::Down => { + if self.orientation_flipped { + '↑' + } else { + '↓' + } + } + KeyPress::Up => { + if self.orientation_flipped { + '↓' + } else { + '↑' + } + } + KeyPress::Left => { + if self.orientation_flipped { + '→' + } else { + '←' + } + } + KeyPress::Right => { + if self.orientation_flipped { + '←' + } else { + '→' + } + } + KeyPress::Select => '∴', + // "Fire" is used as the mapping for the center instead of carriage return ('\r' (0xd)) + // because carriage return is reserved for the shell to indicate the end of line. Thus + // by mapping "fire" to the center key, we get a UI-specific action key without invoking + // shell commands in the background unintentionally. + KeyPress::Center => '🔥', + #[cfg(feature = "accel-kbd")] + KeyPress::Accel => '⏯', + _ => '\u{0000}', + } } } @@ -475,7 +509,7 @@ fn keyboard_service() { log::debug!("{:?}", key_down); if key_down != KeyPress::Invalid && key_down != KeyPress::None { key_tracker.register_key_down(key_down, now); - kc.push(map_keypress(key_down)) + kc.push(key_tracker.map_keypress(key_down)) } } // the keys_pressed() check is necessary because the interrupt will fire *before* a key @@ -542,6 +576,12 @@ fn keyboard_service() { log::warn!("Unhandled interrupt: {:x}", pending); } }), + Some(KeyboardOpcode::SetOrientation) => msg_scalar_unpack!(msg, _flipped, _, _, _, { + #[cfg(feature = "board-baosec")] + { + key_tracker.orientation_flipped = _flipped != 0; + } + }), None => { log::error!("couldn't convert KeyboardOpcode"); break;