From 28a9d5bca77a977af98287dd2cc12cf2f856a774 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 6 Jun 2022 03:56:05 +0800 Subject: [PATCH 1/2] add hooks so unsafe C-FFI calls can connect to the keyboard - Opcodes are mapped to static IDs - connection ID is now visible to someone who already owns the keyboard object. This can then be shared with the FFI interface to do calls to the keyboard server. --- services/keyboard/src/api.rs | 23 ++++++++++++----------- services/keyboard/src/lib.rs | 2 ++ 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/services/keyboard/src/api.rs b/services/keyboard/src/api.rs index 012e6eb64..9c1c278e1 100644 --- a/services/keyboard/src/api.rs +++ b/services/keyboard/src/api.rs @@ -47,38 +47,39 @@ impl Into for KeyMap { } } +// Opcodes are pinned down to allow for unsafe FFI extraction of key hits #[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] pub(crate) enum Opcode { /// set which keyboard mapping is present - SelectKeyMap, //(KeyMap), - GetKeyMap, + SelectKeyMap = 0, //(KeyMap), + GetKeyMap = 1, /// request for ScanCodes - RegisterListener, + RegisterListener = 2, /// request for raw keyups/downs - RegisterRawListener, + RegisterRawListener = 3, /// set repeat delay, rate; both in ms - SetRepeat, //(u32, u32), + SetRepeat = 4, //(u32, u32), /// set chording interval (how long to wait for all keydowns to happen before interpreting as a chord), in ms (for braille keyboards) - SetChordInterval, //(u32), + SetChordInterval = 5, //(u32), /// used by host mode emulation and debug UART to inject keys - InjectKey, //(char), + InjectKey = 6, //(char), /// used by the interrupt handler to transfer results to the main loop - HandlerTrigger, + HandlerTrigger = 7, /// used to turn keyboard vibrate on and off - Vibe, + Vibe = 8, /// a blocking key listener - blocks until a key is hit - BlockingKeyListener, + BlockingKeyListener = 9, /// Suspend/resume callback - SuspendResume, + SuspendResume = 10, } // this structure is used to register a keyboard listener. Currently, we only accept diff --git a/services/keyboard/src/lib.rs b/services/keyboard/src/lib.rs index 18b915a4e..5f0046bf8 100644 --- a/services/keyboard/src/lib.rs +++ b/services/keyboard/src/lib.rs @@ -92,6 +92,8 @@ impl Keyboard { Ok(_) | Err(_) => panic!("internal error: Incorrect return type") } } + /// Reveal the connection ID for use with unsafe FFI calls + pub fn conn(&self) -> xous::CID { self.conn } #[cfg(not(any(target_os = "none", target_os = "xous")))] pub fn hostmode_inject_key(&self, c: char) { From 3e84e0e597274e0e109b51d1c96ef507a82e2ad1 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 6 Jun 2022 04:33:10 +0800 Subject: [PATCH 2/2] add some example code to FFI-test for implementing a getchar-like facility --- Cargo.lock | 1 + services/ffi-test/Cargo.toml | 1 + services/ffi-test/src/bindings.rs | 54 +++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 34126a0e7..04841cf94 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1157,6 +1157,7 @@ name = "ffi-test" version = "0.1.0" dependencies = [ "ffi-sys", + "keyboard", "log", "log-server", "num-derive", diff --git a/services/ffi-test/Cargo.toml b/services/ffi-test/Cargo.toml index 4695c0589..5f6a7e332 100644 --- a/services/ffi-test/Cargo.toml +++ b/services/ffi-test/Cargo.toml @@ -16,6 +16,7 @@ num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} susres = {path = "../susres"} ffi-sys = {path = "sys"} +keyboard = {path = "../keyboard"} [target.'cfg(not(any(windows,unix)))'.dependencies] utralib = { path = "../../utralib"} diff --git a/services/ffi-test/src/bindings.rs b/services/ffi-test/src/bindings.rs index 062ca59ed..7bfb0183a 100644 --- a/services/ffi-test/src/bindings.rs +++ b/services/ffi-test/src/bindings.rs @@ -16,6 +16,60 @@ pub type c_float = f32; pub type c_double = f64; pub type c_void = core::ffi::c_void; +use xous::{send_message, Message}; +static mut KBD: Option = None; +fn get_keys_blocking() -> Vec { + if unsafe{KBD.is_none()} { + let xns = xous_names::XousNames::new().unwrap(); + let kbd = keyboard::Keyboard::new(&xns).unwrap(); + unsafe{KBD = Some(kbd)}; + } + match send_message(unsafe{KBD.as_ref().unwrap().conn()}, + Message::new_blocking_scalar( + 9, // BlockingKeyboardListener + 0, 0, 0, 0 + ) + ) { + Ok(xous::Result::Scalar2(k1, k2)) => { + let mut ret = Vec::::new(); + if let Some(c) = core::char::from_u32(k1 as u32) { + ret.push(c) + } + if let Some(c) = core::char::from_u32(k2 as u32) { + ret.push(c) + } + ret + } + Ok(_) | Err(_) => panic!("internal error: Incorrect return type") + } +} + + +#[export_name = "rust_getchar"] +pub unsafe extern "C" fn rust_getchar() -> c_char { + log::info!("rust_getchar called"); + let mut chr = 0 as c_char; + let kbhit = get_keys_blocking(); + for k in kbhit { + log::info!("got key character: {}", k); + chr = k as i8; + return chr; + //break; + } + log::info!("libc_getchar cleanup FIXME"); + //kbd.drop(); ????? + return chr; +} + +/* +/// Call this from your Rust init routine, before using get_keys_blocking() +pub fn init_kbd() { + let xns = xous_names::XousNames::new().unwrap(); + let kbd = keyboard::Keyboard::new(&xns).unwrap(); + KBD_CONN.store(kbd.conn(), Ordering::SeqCst); + unsafe{KBD = Some(kbd)}; +}*/ + extern "C" { pub fn add_one(a: i32) -> i32; }