From 72b8d6a3de823de1919a9b5969bcc9e645bb8416 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 13 Jan 2021 23:17:09 +0800 Subject: [PATCH] add keyboard input emulation in hosted mode --- Cargo.lock | 1 + services/graphics-server/Cargo.toml | 1 + .../graphics-server/src/backend/minifb.rs | 128 +++++++++++++++++- services/keyboard/src/api.rs | 10 ++ services/keyboard/src/main.rs | 28 +++- services/shell/src/main.rs | 30 ++-- services/trng/src/main.rs | 5 + 7 files changed, 185 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a3adb3eb8..04bd72417 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -295,6 +295,7 @@ name = "graphics-server" version = "0.1.0" dependencies = [ "blitstr", + "keyboard", "log", "log-server", "minifb", diff --git a/services/graphics-server/Cargo.toml b/services/graphics-server/Cargo.toml index e449aa4a6..d307da1d1 100644 --- a/services/graphics-server/Cargo.toml +++ b/services/graphics-server/Cargo.toml @@ -10,6 +10,7 @@ xous = { path = "../../xous-rs" } log-server = { path = "../log-server" } xous-names = { path = "../xous-names" } log = "0.4" +keyboard = { path = "../keyboard" } blitstr = { git = "https://github.com/betrusted-io/blitstr.git", rev = "ea27e611d5ee165d8be894f9ca1c1130156c1931" } # blitstr = { path = "../../../blitstr" } diff --git a/services/graphics-server/src/backend/minifb.rs b/services/graphics-server/src/backend/minifb.rs index 7ea64dbe2..4cb169371 100644 --- a/services/graphics-server/src/backend/minifb.rs +++ b/services/graphics-server/src/backend/minifb.rs @@ -1,4 +1,4 @@ -use minifb::{Key, Window, WindowOptions}; +use minifb::{Key, Window, WindowOptions, KeyRepeat}; const WIDTH: usize = 336; const HEIGHT: usize = 536; @@ -15,6 +15,7 @@ pub struct XousDisplay { native_buffer: Vec, //[u32; WIDTH * HEIGHT], emulated_buffer: [u32; FB_SIZE], window: Window, + kbd_conn: xous::CID, } impl XousDisplay { @@ -43,10 +44,13 @@ impl XousDisplay { .update_with_buffer(&native_buffer, WIDTH, HEIGHT) .unwrap(); + let kbd_conn = xous_names::request_connection_blocking(xous::names::SERVER_NAME_KBD).expect("GFX|hosted: can't connect to KBD for emulation"); + XousDisplay { native_buffer, window, emulated_buffer: [0u32; FB_SIZE], + kbd_conn, } } @@ -73,6 +77,17 @@ impl XousDisplay { if !self.window.is_open() || self.window.is_key_down(Key::Escape) { std::process::exit(0); } + + let pressed = self.window.get_keys_pressed(KeyRepeat::No); + if let Some(keys) = pressed { + for k in keys { + log::info!("GFX|hosted: sending key {:?}", k); + let c = self.decode_key(k); + if c != '\u{0000}' { + xous::send_message(self.kbd_conn, keyboard::api::Opcode::HostModeInjectKey(c).into()).map(|_| ()).unwrap(); + } + } + } } fn emulated_to_native(&mut self) { @@ -92,4 +107,115 @@ impl XousDisplay { } } } + + fn decode_key(&mut self, k: Key) -> char { + let mut shift = false; + if self.window.is_key_pressed(Key::LeftShift, KeyRepeat::Yes) || + self.window.is_key_pressed(Key::RightShift, KeyRepeat::Yes) { + shift = true; + } + let base: char = + if shift == false { + match k { + Key::A => 'a', + Key::B => 'b', + Key::C => 'c', + Key::D => 'd', + Key::E => 'e', + Key::F => 'f', + Key::G => 'g', + Key::H => 'h', + Key::I => 'i', + Key::J => 'j', + Key::K => 'k', + Key::L => 'l', + Key::M => 'm', + Key::N => 'n', + Key::O => 'o', + Key::P => 'p', + Key::Q => 'q', + Key::R => 'r', + Key::S => 's', + Key::T => 't', + Key::U => 'u', + Key::V => 'v', + Key::W => 'w', + Key::X => 'x', + Key::Y => 'y', + Key::Z => 'z', + Key::Key0 => '0', + Key::Key1 => '1', + Key::Key2 => '2', + Key::Key3 => '3', + Key::Key4 => '4', + Key::Key5 => '5', + Key::Key6 => '6', + Key::Key7 => '7', + Key::Key8 => '8', + Key::Key9 => '9', + Key::Left => '←', + Key::Right => '→', + Key::Up => '↑', + Key::Down => '↓', + Key::Home => '∴', + Key::Backspace => 0x8_u8.into(), + Key::Delete => 0x8_u8.into(), + Key::Space => ' ', + Key::Comma => ',', + Key::Period => '.', + _ => '\u{0000}', + } + } else { + match k { + Key::A => 'A', + Key::B => 'B', + Key::C => 'C', + Key::D => 'D', + Key::E => 'E', + Key::F => 'F', + Key::G => 'G', + Key::H => 'H', + Key::I => 'I', + Key::J => 'J', + Key::K => 'K', + Key::L => 'L', + Key::M => 'M', + Key::N => 'N', + Key::O => 'O', + Key::P => 'P', + Key::Q => 'Q', + Key::R => 'R', + Key::S => 'S', + Key::T => 'T', + Key::U => 'U', + Key::V => 'V', + Key::W => 'W', + Key::X => 'X', + Key::Y => 'Y', + Key::Z => 'Z', + Key::Key0 => ')', + Key::Key1 => '!', + Key::Key2 => '@', + Key::Key3 => '#', + Key::Key4 => '$', + Key::Key5 => '%', + Key::Key6 => '^', + Key::Key7 => '&', + Key::Key8 => '*', + Key::Key9 => '(', + Key::Left => '←', + Key::Right => '→', + Key::Up => '↑', + Key::Down => '↓', + Key::Home => '∴', + Key::Backspace => 0x8_u8.into(), + Key::Delete => 0x8_u8.into(), + Key::Space => ' ', + Key::Comma => '<', + Key::Period => '>', + _ => '\u{0000}', + } + }; + base + } } diff --git a/services/keyboard/src/api.rs b/services/keyboard/src/api.rs index 529274336..22dd7e958 100644 --- a/services/keyboard/src/api.rs +++ b/services/keyboard/src/api.rs @@ -134,6 +134,9 @@ pub enum Opcode { /// keyboard events (as sent to listeners) KeyboardEvent([char; 4]), + + /// used by host mode emulation to inject keys + HostModeInjectKey(char), } impl core::convert::TryFrom<& Message> for Opcode { @@ -151,6 +154,8 @@ impl core::convert::TryFrom<& Message> for Opcode { if let Some(a) = core::char::from_u32(m.arg3 as u32) { a } else { '\u{0000}' }, if let Some(a) = core::char::from_u32(m.arg4 as u32) { a } else { '\u{0000}' } ])), + 3 => Ok(Opcode::HostModeInjectKey( + if let Some(a) = core::char::from_u32(m.arg1 as u32) {a} else { '\u{0000}'} )), _ => Err("KBD api: unknown Scalar ID"), }, Message::Borrow(m) => { @@ -199,6 +204,11 @@ impl Into for Opcode { arg3: keys[2] as u32 as usize, arg4: keys[3] as u32 as usize, }), + Opcode::HostModeInjectKey(key) => Message::Scalar(ScalarMessage { + id: 3, + arg1: key as u32 as usize, + arg2: 0, arg3: 0, arg4: 0, + }), _ => panic!("KBD api: Opcode type not handled by Into(), refer to helper method"), } } diff --git a/services/keyboard/src/main.rs b/services/keyboard/src/main.rs index c0a20a964..65de1f574 100644 --- a/services/keyboard/src/main.rs +++ b/services/keyboard/src/main.rs @@ -801,6 +801,7 @@ fn xmain() -> ! { let mut raw_conns: Vec = Vec::new(); info!("KBD: starting main loop"); + let mut injected_keys: Vec = Vec::new(); loop { let maybe_env = xous::try_receive_message(kbd_sid).unwrap(); match maybe_env { @@ -827,6 +828,9 @@ fn xmain() -> ! { }, Opcode::KeyboardEvent(_keys) => { error!("KBD: somehow received an outgoing event code, this shouldn't happen!"); + }, + Opcode::HostModeInjectKey(key) => { + injected_keys.push(key).unwrap(); } } } else { @@ -881,7 +885,29 @@ fn xmain() -> ! { }; // send keys, if any - if kc.len() > 0 { + if kc.len() > 0 || injected_keys.len() > 0 { + // this is used for hosted mode emulation injection of keys + if injected_keys.len() > 0 { + let mut keys: [char; 4] = ['\u{0000}', '\u{0000}', '\u{0000}', '\u{0000}']; + let mut i = 0; + while i < injected_keys.len() && i < 4 { + keys[i] = injected_keys[i]; + i = i + 1; + } + if injected_keys.len() < 4 { + injected_keys.clear(); + } else { + let mut temp: Vec = Vec::new(); + for i in 4..injected_keys.len() { + temp.push(injected_keys[i]).unwrap(); + } + injected_keys = temp; + } + for conn in normal_conns.iter() { + xous::send_message(*conn, api::Opcode::KeyboardEvent(keys).into()).map(|_| ()).expect("KBD: Couldn't send event to listener"); + } + } + let mut keys: [char; 4] = ['\u{0000}', '\u{0000}', '\u{0000}', '\u{0000}']; for i in 0..kc.len() { // info!("KBD: sending key '{}'", kc[i]); diff --git a/services/shell/src/main.rs b/services/shell/src/main.rs index 383abaa48..54ded9bd5 100644 --- a/services/shell/src/main.rs +++ b/services/shell/src/main.rs @@ -11,19 +11,11 @@ use xous::String; use core::convert::TryFrom; -fn move_lfsr(mut lfsr: u32) -> u32 { - lfsr ^= lfsr >> 7; - lfsr ^= lfsr << 9; - lfsr ^= lfsr >> 13; - lfsr -} - pub struct Bounce { vector: Point, radius: u16, bounds: Rectangle, loc: Point, - lfsr: u32, } impl Bounce { @@ -36,7 +28,6 @@ impl Bounce { (bounds.br.x - bounds.tl.x) / 2, (bounds.br.y - bounds.tl.y) / 2, ), - lfsr: 0xace1u32, } } @@ -51,11 +42,6 @@ impl Bounce { } pub fn next_rand(&mut self, trng_conn: xous::CID) -> i16 { - /* - let mut ret = move_lfsr(self.lfsr); - self.lfsr = ret; - ret *= 3; // make the ball move faster - */ let ret = trng::get_u32(trng_conn).expect("SHELL: can't get TRNG") * 3; (ret % 12) as i16 @@ -100,6 +86,8 @@ impl Bounce { } use core::sync::atomic::{AtomicI16, AtomicU16, AtomicU8, Ordering, AtomicU32, AtomicBool}; +use heapless::Vec; +use heapless::consts::U64; // need atomic global constants to pass data between threads // as we do not yet have a "Mutex" in Xous @@ -121,7 +109,18 @@ fn event_thread(_arg: usize) { com::request_battstat_events(xous::names::SERVER_NAME_SHELL, com_conn).expect("SHELL|event_thread: couldn't request events from COM"); info!("SHELL|event_thread: starting COM response handler thread"); + let mut key_queue: Vec = Vec::new(); loop { + if key_queue.len() > 0 { + for i in 0..key_queue.len() { + INCOMING_CHAR.store(key_queue[i] as u32, Ordering::Relaxed); + INCOMING_FRESH.store(true, Ordering::Relaxed); + while INCOMING_FRESH.load(Ordering::Relaxed) { + xous::yield_slice(); + } + } + key_queue.clear(); + } let envelope = xous::syscall::receive_message(shell_server).expect("couldn't get address"); // info!("SHELL|event_thread: got message {:?}", envelope); if let Ok(opcode) = com::api::Opcode::try_from(&envelope.body) { @@ -139,9 +138,8 @@ fn event_thread(_arg: usize) { keyboard::api::Opcode::KeyboardEvent(keys) => { for &k in keys.iter() { if k != '\u{0000}' { + key_queue.push(k).unwrap(); // info!("SHELL:event_thread: got key '{}'", k); - INCOMING_CHAR.store(k as u32, Ordering::Relaxed); - INCOMING_FRESH.store(true, Ordering::Relaxed); } } }, diff --git a/services/trng/src/main.rs b/services/trng/src/main.rs index c7d9b5e15..d8b57d02f 100644 --- a/services/trng/src/main.rs +++ b/services/trng/src/main.rs @@ -155,7 +155,12 @@ fn xmain() -> ! { let trng_sid = xous_names::register_name(xous::names::SERVER_NAME_TRNG).expect("TRNG: can't register server"); info!("TRNG: registered with NS -- {:?}", trng_sid); + #[cfg(target_os = "none")] + let trng = Trng::new(); + + #[cfg(not(target_os = "none"))] let mut trng = Trng::new(); + info!("TRNG: ready to accept requests"); loop {