From b742dcade2e1db1ab8898307eda0044de027c3cc Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Fri, 12 Mar 2021 09:18:30 +0800 Subject: [PATCH 1/7] xous: make macros optional These are not used when building libstd. Signed-off-by: Sean Cross --- xous-rs/Cargo.toml | 4 ++-- xous-rs/src/lib.rs | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/xous-rs/Cargo.toml b/xous-rs/Cargo.toml index 6aab704d7..c7c5c67bf 100644 --- a/xous-rs/Cargo.toml +++ b/xous-rs/Cargo.toml @@ -8,7 +8,7 @@ description = "System call interface for Xous" [dependencies] bitflags = {version = "1", optional = true } -xous-macros = { path = "../macros", version = "0.1.0" } +xous-macros = { path = "../macros", version = "0.1.0", optional = true } log = { version = "0.4", optional = true } rkyv = { version = "0.3.1", default_features = false, optional = true } core = { version = '1.0.0', optional = true, package = 'rustc-std-workspace-core' } @@ -33,7 +33,7 @@ logging = ["log"] # This is being built as part of libstd rustc-dep-of-std = ['core', 'compiler_builtins/rustc-dep-of-std'] -default = ["bit-flags", "archive"] +default = ["bit-flags", "archive", "xous-macros"] [target.'cfg(any(windows,unix))'.dependencies] lazy_static = "1.4" diff --git a/xous-rs/src/lib.rs b/xous-rs/src/lib.rs index 805591e22..c5dc3b55b 100644 --- a/xous-rs/src/lib.rs +++ b/xous-rs/src/lib.rs @@ -4,8 +4,9 @@ #[macro_use] extern crate bitflags; +#[cfg(not(feature = "rustc-dep-of-std"))] extern crate xous_macros as macros; - +#[cfg(not(feature = "rustc-dep-of-std"))] pub use macros::xous_main; pub mod arch; From e1890b1717c639f74f273e339629604fd05cab70 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sat, 13 Mar 2021 17:59:48 +0800 Subject: [PATCH 2/7] 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 --- services/graphics-server/Cargo.toml | 4 +- .../graphics-server/src/backend/minifb.rs | 296 ++++++++++-------- 2 files changed, 169 insertions(+), 131 deletions(-) diff --git a/services/graphics-server/Cargo.toml b/services/graphics-server/Cargo.toml index c8032065e..2f693e4c2 100644 --- a/services/graphics-server/Cargo.toml +++ b/services/graphics-server/Cargo.toml @@ -13,14 +13,14 @@ keyboard = {path = "../keyboard"} log = "0.4" log-server = {path = "../log-server"} 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"} [target.'cfg(not(any(windows,unix)))'.dependencies] utralib = {path = "../../utralib"} [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] debugprint = [] diff --git a/services/graphics-server/src/backend/minifb.rs b/services/graphics-server/src/backend/minifb.rs index 56dd42b84..34e13c76e 100644 --- a/services/graphics-server/src/backend/minifb.rs +++ b/services/graphics-server/src/backend/minifb.rs @@ -1,5 +1,5 @@ -use minifb::{Key, Window, WindowOptions, KeyRepeat}; use crate::api::Point; +use minifb::{Key, Window, WindowOptions}; const WIDTH: usize = 336; const HEIGHT: usize = 536; @@ -16,7 +16,12 @@ pub struct XousDisplay { native_buffer: Vec, //[u32; WIDTH * HEIGHT], emulated_buffer: [u32; FB_SIZE], window: Window, +} + +struct XousKeyboardHandler { kbd_conn: xous::CID, + left_shift: bool, + right_shift: bool, } impl XousDisplay { @@ -45,17 +50,25 @@ 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"); + 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 { native_buffer, window, 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]) { 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) { 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(); - } - } - } + // 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) { @@ -110,122 +122,148 @@ impl XousDisplay { } } } +} +impl XousKeyboardHandler { 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::Enter => 0xd_u8.into(), - Key::Space => ' ', - Key::Comma => ',', - Key::Period => '.', - Key::F1 => 0x11_u8.into(), - Key::F2 => 0x12_u8.into(), - Key::F3 => 0x13_u8.into(), - Key::F4 => 0x14_u8.into(), - Key::F5 => '😃', - Key::F6 => '福', - _ => '\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}', - } - }; + let shift = self.left_shift || self.right_shift; + 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::Enter => 0xd_u8.into(), + Key::Space => ' ', + Key::Comma => ',', + Key::Period => '.', + Key::F1 => 0x11_u8.into(), + Key::F2 => 0x12_u8.into(), + Key::F3 => 0x13_u8.into(), + Key::F4 => 0x14_u8.into(), + Key::F5 => '😃', + Key::F6 => '福', + _ => '\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 } } + +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(); + } + } +} From f960c1cb4a7bd3affefbcb69fd1f02fdd4c7a610 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sat, 13 Mar 2021 18:02:21 +0800 Subject: [PATCH 3/7] cargo: sync latest git tags We've updated `minifb`, which updates the lockfile. Signed-off-by: Sean Cross --- Cargo.lock | 41 +++++++++++++++++------------------------ Cargo.toml | 1 - 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ed21811f2..dc822fa6b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -704,8 +704,8 @@ dependencies = [ [[package]] name = "minifb" -version = "0.19.2" -source = "git+https://github.com/xobs/rust_minifb.git?rev=bbbfc07c059919fae700fabe771346a001226dbc#bbbfc07c059919fae700fabe771346a001226dbc" +version = "0.19.3" +source = "git+https://github.com/xobs/rust_minifb.git?rev=ea25c2e4843abd130d6794b2045812907a24eab2#ea25c2e4843abd130d6794b2045812907a24eab2" dependencies = [ "cc", "orbclient", @@ -732,15 +732,14 @@ dependencies = [ [[package]] name = "nix" -version = "0.17.0" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50e4785f2c3b7589a0d0c1dd60285e1188adac4006e8abd6dd578e1567027363" +checksum = "fa9b4819da1bc61c0ea48b63b7bc8604064dd43013e7cc325df098d49cd7c18a" dependencies = [ "bitflags", "cc", - "cfg-if 0.1.10", + "cfg-if 1.0.0", "libc", - "void", ] [[package]] @@ -1360,12 +1359,6 @@ version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" -[[package]] -name = "void" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" - [[package]] name = "wasi" version = "0.10.2+wasi-snapshot-preview1" @@ -1374,9 +1367,9 @@ checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" [[package]] name = "wayland-client" -version = "0.27.0" +version = "0.28.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab702fefbcd6d6f67fb5816e3a89a3b5a42a94290abbc015311c9a30d1068ae4" +checksum = "06ca44d86554b85cf449f1557edc6cc7da935cc748c8e4bf1c507cbd43bae02c" dependencies = [ "bitflags", "downcast-rs", @@ -1389,9 +1382,9 @@ dependencies = [ [[package]] name = "wayland-commons" -version = "0.27.0" +version = "0.28.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e972e9336ad5a9dd861b4e21ff35ad71d3e5c6b4803d65c39913612f851b95f1" +checksum = "8bd75ae380325dbcff2707f0cd9869827ea1d2d6d534cff076858d3f0460fd5a" dependencies = [ "nix", "once_cell", @@ -1401,9 +1394,9 @@ dependencies = [ [[package]] name = "wayland-cursor" -version = "0.27.0" +version = "0.28.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "539f346e1a3f706f38c8ccbe1196001e2fb1c9b3e6b605c27d665db2f5b60d41" +checksum = "b37e5455ec72f5de555ec39b5c3704036ac07c2ecd50d0bffe02d5fe2d4e65ab" dependencies = [ "nix", "wayland-client", @@ -1412,9 +1405,9 @@ dependencies = [ [[package]] name = "wayland-protocols" -version = "0.27.0" +version = "0.28.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3d6fc54b17b98b5083bc21ae3a30e6d75cb4b01647360e4c3a04648bcf8781d" +checksum = "95df3317872bcf9eec096c864b69aa4769a1d5d6291a5b513f8ba0af0efbd52c" dependencies = [ "bitflags", "wayland-client", @@ -1424,9 +1417,9 @@ dependencies = [ [[package]] name = "wayland-scanner" -version = "0.27.0" +version = "0.28.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "030f56009d932bd9400bb472764fea8109be1b0fc482d9cd75496c943ac30328" +checksum = "389d680d7bd67512dc9c37f39560224327038deb0f0e8d33f870900441b68720" dependencies = [ "proc-macro2 1.0.24", "quote 1.0.8", @@ -1435,9 +1428,9 @@ dependencies = [ [[package]] name = "wayland-sys" -version = "0.27.0" +version = "0.28.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8bdeffbbb474477dfa2acb45ac7479e5fe8f741c64ab032c5d11b94d07edc269" +checksum = "2907bd297eef464a95ba9349ea771611771aa285b932526c633dc94d5400a8e2" dependencies = [ "pkg-config", ] diff --git a/Cargo.toml b/Cargo.toml index 9831205f2..73b1e61b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -69,4 +69,3 @@ exclude = [ codegen-units = 1 # 1 better optimizations debug = true # symbols are nice and they don't increase the size on Flash lto = "fat" - From 9c8b5627110d1238d0ea406afd5b466d420448bd Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sat, 13 Mar 2021 18:05:11 +0800 Subject: [PATCH 4/7] hosted: set tcp_nodelay This might have a performance impact, but it's unclear. Signed-off-by: Sean Cross --- kernel/src/arch/hosted.rs | 1 + xous-rs/src/arch/hosted.rs | 10 ++++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/kernel/src/arch/hosted.rs b/kernel/src/arch/hosted.rs index aca878114..e5ace70ca 100644 --- a/kernel/src/arch/hosted.rs +++ b/kernel/src/arch/hosted.rs @@ -312,6 +312,7 @@ fn listen_thread( // Read the challenge access key from the client let mut access_key = [0u8; 16]; conn.read_exact(&mut access_key).unwrap(); + conn.set_nodelay(true).unwrap(); // Spawn a new process. This process will start out in the "Allocated" state. chn.send(ThreadMessage::NewConnection( diff --git a/xous-rs/src/arch/hosted.rs b/xous-rs/src/arch/hosted.rs index 1bc7cfcaa..cf16139dc 100644 --- a/xous-rs/src/arch/hosted.rs +++ b/xous-rs/src/arch/hosted.rs @@ -397,6 +397,7 @@ fn xous_connect_impl( Ok(mut conn) => { conn.write_all(&key.0).unwrap(); // Send key to authenticate us as PID 1 conn.flush().unwrap(); + conn.set_nodelay(true).unwrap(); let mut pid = [0u8]; conn.read_exact(&mut pid).unwrap(); PROCESS_ID.with(|process_id| *process_id.borrow_mut() = PID::new(pid[0]).unwrap()); @@ -456,7 +457,7 @@ pub fn _xous_syscall( a6, a7, &call, - &mut xsc_asmut.send.lock().unwrap(), + xsc_asmut ); _xous_syscall_result(ret, *tid.borrow(), xsc_asmut); if *ret != Result::WouldBlock { @@ -656,7 +657,7 @@ fn _xous_syscall_to( a6: usize, a7: usize, call: &crate::SysCall, - xsc: &mut TcpStream, + xsc: &mut ServerConnection, ) { // println!( // "Making Syscall: {:?}", @@ -677,9 +678,10 @@ fn _xous_syscall_to( pkt.extend_from_slice(data); } - if let Err(e) = xsc.write_all(&pkt) { + let mut stream = xsc.send.lock().unwrap(); + if let Err(e) = stream.write_all(&pkt) { eprintln!("Server shut down: {}", e); std::process::exit(0); } - xsc.flush().unwrap(); + // stream.flush().unwrap(); } From 14c9d62046a1b3b49e657308c64c4c1cf0ca7140 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sat, 13 Mar 2021 18:05:33 +0800 Subject: [PATCH 5/7] gam: make sure opcodes are only available in-crate This prevents them from accidentally being used outside of the crate. Signed-off-by: Sean Cross --- services/gam/src/api.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/gam/src/api.rs b/services/gam/src/api.rs index 9ee51d21c..5f6d73619 100644 --- a/services/gam/src/api.rs +++ b/services/gam/src/api.rs @@ -30,9 +30,10 @@ pub struct ContentCanvasRequest { pub servername: xous::String<256>, } +#[repr(C)] #[derive(Debug, rkyv::Archive, rkyv::Unarchive, Copy, Clone)] // #[archive(derive(Copy, Clone))] -pub enum Opcode { +pub(crate) enum Opcode { // clears a canvas with a given GID ClearCanvas(Gid), From ff8a839ac0c4cc5edf828b2b9833e9b542f2ddb2 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sat, 13 Mar 2021 18:06:23 +0800 Subject: [PATCH 6/7] xous: string: implement as_str() for ArchivedString Signed-off-by: Sean Cross --- xous-rs/src/string.rs | 99 ++++++++++++++++++++++++++++--------------- 1 file changed, 64 insertions(+), 35 deletions(-) diff --git a/xous-rs/src/string.rs b/xous-rs/src/string.rs index 10dec993e..f9881d8c5 100644 --- a/xous-rs/src/string.rs +++ b/xous-rs/src/string.rs @@ -1,9 +1,9 @@ -use crate::{CID, Error, MemoryMessage, Result}; +use crate::{Error, MemoryMessage, Result, CID}; -use rkyv::Write; -use rkyv::Unarchive; -use rkyv::archived_value; use core::pin::Pin; +use rkyv::archived_value; +use rkyv::Unarchive; +use rkyv::Write; #[derive(Copy, Clone)] pub struct String { @@ -19,6 +19,24 @@ impl String { } } + pub fn from_str(src: &str) -> String { + let mut s = Self::new(); + // Copy the string into our backing store. + for (&src_byte, dest_byte) in src.as_bytes().iter().zip(&mut s.bytes) { + *dest_byte = src_byte; + } + // Set the string length to the length of the passed-in String, + // or the maximum possible length. Which ever is smaller. + s.len = s.bytes.len().min(src.as_bytes().len()) as u32; + + // If the string is not valid, set its length to 0. + if s.as_str().is_err() { + s.len = 0; + } + + s + } + pub fn as_bytes(&self) -> [u8; N] { self.bytes } @@ -37,13 +55,11 @@ impl String { /// Convert a `MemoryMessage` into a `String` pub fn from_message( - message: & mut MemoryMessage, + message: &mut MemoryMessage, ) -> core::result::Result, core::str::Utf8Error> { - let buf = unsafe{ crate::XousBuffer::from_memory_message(message) }; + let buf = unsafe { crate::XousBuffer::from_memory_message(message) }; let bytes = Pin::new(buf.as_ref()); - let value = unsafe { - archived_value::>(&bytes, message.id as usize) - }; + let value = unsafe { archived_value::>(&bytes, message.id as usize) }; let s = value.unarchive(); Ok(s) } @@ -60,9 +76,10 @@ impl String { connection: CID, // id: crate::MessageId, ) -> core::result::Result { - - let mut writer = rkyv::ArchiveBuffer::new(crate::XousBuffer::new( N )); - let pos = writer.archive(self).expect("xous::String -- couldn't archive self"); + let mut writer = rkyv::ArchiveBuffer::new(crate::XousBuffer::new(N)); + let pos = writer + .archive(self) + .expect("xous::String -- couldn't archive self"); let xous_buffer = writer.into_inner(); // note that "id" is actually used as the position into the rkyv buffer @@ -75,8 +92,11 @@ impl String { connection: CID, // id: crate::MessageId, ) -> core::result::Result { - let mut writer = rkyv::ArchiveBuffer::new(crate::XousBuffer::new(/*self.bytes.len()*/ 4096)); - let pos = writer.archive(&self).expect("xous::String -- couldn't archive self"); + let mut writer = + rkyv::ArchiveBuffer::new(crate::XousBuffer::new(/*self.bytes.len()*/ 4096)); + let pos = writer + .archive(&self) + .expect("xous::String -- couldn't archive self"); let xous_buffer = writer.into_inner(); xous_buffer.send(connection, pos as u32) @@ -116,14 +136,12 @@ impl String { Some(_thing) => { self.push(c).unwrap(); // always succeeds because we're re-encoding our string maybe_c = next_c; - }, - None => { - return next_c } + None => return next_c, } - }, + } None => { - return None // we should actually never get here because len() == 0 case already covered + return None; // we should actually never get here because len() == 0 case already covered } } } @@ -139,7 +157,7 @@ impl String { } else { Err(Error::OutOfMemory) } - }, + } _ => { let mut bytes: usize = 0; let mut data: [u8; 4] = [0; 4]; @@ -154,7 +172,7 @@ impl String { } else { Err(Error::OutOfMemory) } - }, + } } } @@ -164,7 +182,7 @@ impl String { if let Ok(bytes) = self.push(ch) { bytes_added += bytes; } else { - return Err(Error::OutOfMemory) + return Err(Error::OutOfMemory); } } Ok(bytes_added) @@ -205,48 +223,59 @@ impl core::fmt::Debug for String { } } +#[repr(C)] pub struct ArchivedString { ptr: rkyv::RelPtr, len: u32, } + +impl ArchivedString { + // Provide a `str` view of an `ArchivedString`. + fn as_str(&self) -> &str { + unsafe { + // The as_ptr() function of RelPtr will get a pointer + // to its memory. + let bytes = core::slice::from_raw_parts(self.ptr.as_ptr(), self.len as usize); + core::str::from_utf8_unchecked(bytes) + } + } +} + pub struct StringResolver { bytes_pos: usize, } + +// Turn a stream of bytes into an `ArchivedString`. impl rkyv::Resolve> for StringResolver { type Archived = ArchivedString; fn resolve(self, pos: usize, value: &String) -> Self::Archived { Self::Archived { ptr: unsafe { - rkyv::RelPtr::new( - pos + rkyv::offset_of!(ArchivedString, ptr), - self.bytes_pos) + rkyv::RelPtr::new(pos + rkyv::offset_of!(ArchivedString, ptr), self.bytes_pos) }, len: value.len() as u32, } } } +/// Turn a `String` into an archived object impl rkyv::Archive for String { type Archived = ArchivedString; type Resolver = StringResolver; - fn archive(&self, writer: &mut W) -> core::result::Result { + fn archive( + &self, + writer: &mut W, + ) -> core::result::Result { let bytes_pos = writer.pos(); writer.write(&self.bytes[0..self.len()])?; Ok(Self::Resolver { bytes_pos }) } } + impl rkyv::Unarchive> for ArchivedString { fn unarchive(&self) -> String { - let mut s: String = String::::new(); - unsafe { - let p = self.ptr.as_ptr() as *const u8; - for(i, val) in s.bytes.iter_mut().enumerate() { - *val = p.add(i).read(); - } - }; - s.len = self.len; - s + String::from_str(self.as_str()) } } From 5c1940a019d03eb8e28a555243c4fd0df0255c47 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 13 Mar 2021 18:15:30 +0800 Subject: [PATCH 7/7] change the name of the window --- services/graphics-server/src/backend/minifb.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/graphics-server/src/backend/minifb.rs b/services/graphics-server/src/backend/minifb.rs index 56dd42b84..566d3fa05 100644 --- a/services/graphics-server/src/backend/minifb.rs +++ b/services/graphics-server/src/backend/minifb.rs @@ -22,7 +22,7 @@ pub struct XousDisplay { impl XousDisplay { pub fn new() -> XousDisplay { let mut window = Window::new( - "Betrusted", + "Precursor", WIDTH, HEIGHT, WindowOptions {