mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
add keyboard remapping that also tracks screen flipping
This commit is contained in:
parent
1ce29e80fa
commit
70543731d1
2 changed files with 81 additions and 23 deletions
|
|
@ -91,6 +91,10 @@ pub enum KeyboardOpcode {
|
||||||
|
|
||||||
/// a blocking key listener - blocks until a key is hit
|
/// a blocking key listener - blocks until a key is hit
|
||||||
BlockingKeyListener = 9,
|
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.
|
// this structure is used to register a keyboard listener.
|
||||||
|
|
@ -201,6 +205,20 @@ impl Keyboard {
|
||||||
.unwrap();
|
.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
|
/// Reveal the connection ID for use with unsafe FFI calls
|
||||||
pub fn conn(&self) -> xous::CID { self.conn }
|
pub fn conn(&self) -> xous::CID { self.conn }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -62,10 +62,18 @@ struct KeyTracker {
|
||||||
pub rate_ms: usize,
|
pub rate_ms: usize,
|
||||||
pub delay_ms: usize,
|
pub delay_ms: usize,
|
||||||
pub keys: Vec<KeypressTimestamp>,
|
pub keys: Vec<KeypressTimestamp>,
|
||||||
|
pub orientation_flipped: bool,
|
||||||
}
|
}
|
||||||
#[cfg(feature = "board-baosec")]
|
#[cfg(feature = "board-baosec")]
|
||||||
impl KeyTracker {
|
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) {
|
pub fn register_key_down(&mut self, key: KeyPress, ts: u64) {
|
||||||
if let Some(entry) = self.keys.iter_mut().find(|e| e.kp == key) {
|
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
|
/// Returns any keys that should have repeat key-press events generated
|
||||||
pub fn get_repeats(&mut self, now: u64) -> Vec<char> {
|
pub fn get_repeats(&mut self, now: u64) -> Vec<char> {
|
||||||
let mut kps = Vec::new();
|
let mut kps = Vec::new();
|
||||||
for key in self.keys.iter_mut() {
|
// use index-based iteration to avoid borrow checker issue on map_keypress() call
|
||||||
if now >= key.next_repeat_time {
|
for i in 0..self.keys.len() {
|
||||||
kps.push(map_keypress(key.kp));
|
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
|
// 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
|
kps
|
||||||
|
|
@ -126,23 +136,47 @@ impl KeyTracker {
|
||||||
pub fn keys_pressed(&self) -> usize { self.keys.len() }
|
pub fn keys_pressed(&self) -> usize { self.keys.len() }
|
||||||
|
|
||||||
pub fn clear_keys(&mut self) { self.keys.clear(); }
|
pub fn clear_keys(&mut self) { self.keys.clear(); }
|
||||||
}
|
|
||||||
#[cfg(feature = "board-baosec")]
|
pub fn map_keypress(&self, kp: KeyPress) -> char {
|
||||||
fn map_keypress(kp: KeyPress) -> char {
|
match kp {
|
||||||
match kp {
|
KeyPress::Down => {
|
||||||
KeyPress::Down => '↓',
|
if self.orientation_flipped {
|
||||||
KeyPress::Up => '↑',
|
'↑'
|
||||||
KeyPress::Left => '←',
|
} else {
|
||||||
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
|
KeyPress::Up => {
|
||||||
// by mapping "fire" to the center key, we get a UI-specific action key without invoking
|
if self.orientation_flipped {
|
||||||
// shell commands in the background unintentionally.
|
'↓'
|
||||||
KeyPress::Center => '🔥',
|
} else {
|
||||||
#[cfg(feature = "accel-kbd")]
|
'↑'
|
||||||
KeyPress::Accel => '⏯',
|
}
|
||||||
_ => '\u{0000}',
|
}
|
||||||
|
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);
|
log::debug!("{:?}", key_down);
|
||||||
if key_down != KeyPress::Invalid && key_down != KeyPress::None {
|
if key_down != KeyPress::Invalid && key_down != KeyPress::None {
|
||||||
key_tracker.register_key_down(key_down, now);
|
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
|
// 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);
|
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 => {
|
None => {
|
||||||
log::error!("couldn't convert KeyboardOpcode");
|
log::error!("couldn't convert KeyboardOpcode");
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue