add keyboard input emulation in hosted mode

This commit is contained in:
bunnie 2021-01-13 23:17:09 +08:00
parent 6cf140f2da
commit 72b8d6a3de
7 changed files with 185 additions and 18 deletions

1
Cargo.lock generated
View file

@ -295,6 +295,7 @@ name = "graphics-server"
version = "0.1.0"
dependencies = [
"blitstr",
"keyboard",
"log",
"log-server",
"minifb",

View file

@ -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" }

View file

@ -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>, //[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
}
}

View file

@ -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<Message> 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"),
}
}

View file

@ -801,6 +801,7 @@ fn xmain() -> ! {
let mut raw_conns: Vec<xous::CID, U64> = Vec::new();
info!("KBD: starting main loop");
let mut injected_keys: Vec<char, U64> = 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<char, U64> = 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]);

View file

@ -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<char, U64> = 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);
}
}
},

View file

@ -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 {