graphics-server: buffer keypresses

Buffer keypresses so that keys are not lost. This improves
responsiveness, although there are still kernel plumbing issues.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2021-03-13 17:59:48 +08:00
parent b742dcade2
commit e1890b1717
2 changed files with 169 additions and 131 deletions

View file

@ -13,14 +13,14 @@ keyboard = {path = "../keyboard"}
log = "0.4" log = "0.4"
log-server = {path = "../log-server"} log-server = {path = "../log-server"}
xous = {path = "../../xous-rs"} xous = {path = "../../xous-rs"}
rkyv = { version = "0.3.1", default_features = false } rkyv = { version = "0.3.1", default_features = false, features = ["strict"] }
xous-names = {path = "../xous-names"} xous-names = {path = "../xous-names"}
[target.'cfg(not(any(windows,unix)))'.dependencies] [target.'cfg(not(any(windows,unix)))'.dependencies]
utralib = {path = "../../utralib"} utralib = {path = "../../utralib"}
[target.'cfg(any(windows,unix))'.dependencies] [target.'cfg(any(windows,unix))'.dependencies]
minifb = {version = "0.19.2", git = "https://github.com/xobs/rust_minifb.git", rev = "bbbfc07c059919fae700fabe771346a001226dbc"} minifb = {version = "0.19.2", git = "https://github.com/xobs/rust_minifb.git", rev = "ea25c2e4843abd130d6794b2045812907a24eab2"}
[features] [features]
debugprint = [] debugprint = []

View file

@ -1,5 +1,5 @@
use minifb::{Key, Window, WindowOptions, KeyRepeat};
use crate::api::Point; use crate::api::Point;
use minifb::{Key, Window, WindowOptions};
const WIDTH: usize = 336; const WIDTH: usize = 336;
const HEIGHT: usize = 536; const HEIGHT: usize = 536;
@ -16,7 +16,12 @@ pub struct XousDisplay {
native_buffer: Vec<u32>, //[u32; WIDTH * HEIGHT], native_buffer: Vec<u32>, //[u32; WIDTH * HEIGHT],
emulated_buffer: [u32; FB_SIZE], emulated_buffer: [u32; FB_SIZE],
window: Window, window: Window,
}
struct XousKeyboardHandler {
kbd_conn: xous::CID, kbd_conn: xous::CID,
left_shift: bool,
right_shift: bool,
} }
impl XousDisplay { impl XousDisplay {
@ -45,17 +50,25 @@ impl XousDisplay {
.update_with_buffer(&native_buffer, WIDTH, HEIGHT) .update_with_buffer(&native_buffer, WIDTH, HEIGHT)
.unwrap(); .unwrap();
let kbd_conn = xous_names::request_connection_blocking(xous::names::SERVER_NAME_KBD).expect("GFX|hosted: can't connect to KBD for emulation"); let kbd_conn = xous_names::request_connection_blocking(xous::names::SERVER_NAME_KBD)
.expect("GFX|hosted: can't connect to KBD for emulation");
let keyboard_handler = Box::new(XousKeyboardHandler {
kbd_conn: kbd_conn,
left_shift: false,
right_shift: false,
});
window.set_input_callback(keyboard_handler);
XousDisplay { XousDisplay {
native_buffer, native_buffer,
window, window,
emulated_buffer: [0u32; FB_SIZE], emulated_buffer: [0u32; FB_SIZE],
kbd_conn,
} }
} }
pub fn screen_size(&self) -> Point { Point::new(WIDTH as i16, HEIGHT as i16) } pub fn screen_size(&self) -> Point {
Point::new(WIDTH as i16, HEIGHT as i16)
}
pub fn blit_screen(&mut self, bmp: [u32; FB_SIZE]) { pub fn blit_screen(&mut self, bmp: [u32; FB_SIZE]) {
for (dest, src) in self.emulated_buffer.iter_mut().zip(bmp.iter()) { for (dest, src) in self.emulated_buffer.iter_mut().zip(bmp.iter()) {
@ -80,17 +93,16 @@ impl XousDisplay {
if !self.window.is_open() || self.window.is_key_down(Key::Escape) { if !self.window.is_open() || self.window.is_key_down(Key::Escape) {
std::process::exit(0); std::process::exit(0);
} }
// let pressed = self.window.get_keys_pressed(KeyRepeat::No);
let pressed = self.window.get_keys_pressed(KeyRepeat::No); // if let Some(keys) = pressed {
if let Some(keys) = pressed { // for k in keys {
for k in keys { // log::info!("GFX|hosted: sending key {:?}", k);
log::info!("GFX|hosted: sending key {:?}", k); // let c = self.decode_key(k);
let c = self.decode_key(k); // if c != '\u{0000}' {
if c != '\u{0000}' { // xous::send_message(self.kbd_conn, keyboard::api::Opcode::HostModeInjectKey(c).into()).map(|_| ()).unwrap();
xous::send_message(self.kbd_conn, keyboard::api::Opcode::HostModeInjectKey(c).into()).map(|_| ()).unwrap(); // }
} // }
} // }
}
} }
fn emulated_to_native(&mut self) { fn emulated_to_native(&mut self) {
@ -110,15 +122,12 @@ 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 { impl XousKeyboardHandler {
fn decode_key(&mut self, k: Key) -> char {
let shift = self.left_shift || self.right_shift;
let base: char = if shift == false {
match k { match k {
Key::A => 'a', Key::A => 'a',
Key::B => 'b', Key::B => 'b',
@ -229,3 +238,32 @@ impl XousDisplay {
base base
} }
} }
impl minifb::InputCallback for XousKeyboardHandler {
fn add_char(&mut self, _uni_char: u32) {}
fn set_key_state(&mut self, key: minifb::Key, state: bool) {
if key == Key::LeftShift {
self.left_shift = state;
return;
}
if key == Key::RightShift {
self.right_shift = state;
return;
}
if !state {
return;
}
log::info!("GFX|hosted: sending key {:?}", key);
let c = self.decode_key(key);
if c != '\u{0000}' {
xous::send_message(
self.kbd_conn,
keyboard::api::Opcode::HostModeInjectKey(c).into(),
)
.map(|_| ())
.unwrap();
}
}
}