From 365ec5bb0a591e0b1d13cf37c898fdf8015e9680 Mon Sep 17 00:00:00 2001 From: kotborealis Date: Sat, 18 Feb 2023 12:43:58 +0300 Subject: [PATCH 1/2] fix: io devices --- Cargo.lock | 1 + lib/wasi-experimental-io-devices/Cargo.toml | 3 ++- lib/wasi-experimental-io-devices/src/link-ext.rs | 15 ++++++++------- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7afd69d6824..44bf7dea91a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4861,6 +4861,7 @@ dependencies = [ "tracing", "typetag", "wasmer-wasi", + "wasmer-wasi-types", ] [[package]] diff --git a/lib/wasi-experimental-io-devices/Cargo.toml b/lib/wasi-experimental-io-devices/Cargo.toml index b924ae2288c..c2ea81bfb84 100644 --- a/lib/wasi-experimental-io-devices/Cargo.toml +++ b/lib/wasi-experimental-io-devices/Cargo.toml @@ -15,6 +15,7 @@ maintenance = { status = "experimental" } [dependencies] wasmer-wasi = { version = "=3.2.0-alpha.1", path = "../wasi", default-features=false } +wasmer-wasi-types = { path = "../wasi-types", version = "=3.2.0-alpha.1" } tracing = "0.1" minifb = { version = "0.23", optional = true } nix = "0.25.0" @@ -30,7 +31,7 @@ enable-serde = [ "wasmer-wasi/enable-serde" ] # This feature exists, so that "cargo build --all" doesn't -# accidentally link libxcbcommon and libwayland into the CLI +# accidentally link libxcbcommon and libwayland into the CLI # libraries. When using wasi-experimental-io-devices, users # have to enable this feature manually link_external_libs = [ diff --git a/lib/wasi-experimental-io-devices/src/link-ext.rs b/lib/wasi-experimental-io-devices/src/link-ext.rs index 6ebfc5ad7b5..9fcd20e87b0 100644 --- a/lib/wasi-experimental-io-devices/src/link-ext.rs +++ b/lib/wasi-experimental-io-devices/src/link-ext.rs @@ -10,6 +10,7 @@ use wasmer_wasi::{ WasiInodes, }; use wasmer_wasi::{Fd, VirtualFile, WasiFs, WasiFsError, ALL_RIGHTS, VIRTUAL_ROOT_FD}; +use wasmer_wasi_types::wasi::Fdflags; use minifb::{Key, KeyRepeat, MouseButton, Scale, Window, WindowOptions}; @@ -96,7 +97,7 @@ impl FrameBufferState { return None; } self.x_size = x; - self.y_size = x; + self.y_size = y; self.data_1.resize((x * y) as usize, 0); self.data_2.resize((x * y) as usize, 0); @@ -126,7 +127,7 @@ impl FrameBufferState { self.push_input_event(InputEvent::KeyRelease(key))?; } } - let keys = self.window.get_keys_pressed(KeyRepeat::No)?; + let keys = self.window.get_keys_pressed(KeyRepeat::No); for key in keys { self.keys_pressed.insert(key); self.push_input_event(InputEvent::KeyPress(key))?; @@ -456,7 +457,7 @@ pub fn initialize(inodes: &mut WasiInodes, fs: &mut WasiFs) -> Result<(), String "_wasmer/dev/fb0".to_string(), ALL_RIGHTS, ALL_RIGHTS, - 0, + Fdflags::empty(), ) .map_err(|e| format!("fb: Failed to create dev folder {:?}", e))? }; @@ -470,7 +471,7 @@ pub fn initialize(inodes: &mut WasiInodes, fs: &mut WasiFs) -> Result<(), String "input".to_string(), ALL_RIGHTS, ALL_RIGHTS, - 0, + Fdflags::empty(), ) .map_err(|e| format!("fb: Failed to init framebuffer {:?}", e))?; @@ -485,7 +486,7 @@ pub fn initialize(inodes: &mut WasiInodes, fs: &mut WasiFs) -> Result<(), String "fb".to_string(), ALL_RIGHTS, ALL_RIGHTS, - 0, + Fdflags::empty(), ) .map_err(|e| format!("fb: Failed to init framebuffer {:?}", e))?; @@ -500,7 +501,7 @@ pub fn initialize(inodes: &mut WasiInodes, fs: &mut WasiFs) -> Result<(), String "virtual_size".to_string(), ALL_RIGHTS, ALL_RIGHTS, - 0, + Fdflags::empty(), ) .map_err(|e| format!("fb_resolution: Failed to init framebuffer {:?}", e))?; @@ -515,7 +516,7 @@ pub fn initialize(inodes: &mut WasiInodes, fs: &mut WasiFs) -> Result<(), String "draw".to_string(), ALL_RIGHTS, ALL_RIGHTS, - 0, + Fdflags::empty(), ) .map_err(|e| format!("fb_index_display: Failed to init framebuffer {:?}", e))?; From f03fee187e7eb348ab3867f54e8d8f6a919fb690 Mon Sep 17 00:00:00 2001 From: kotborealis Date: Sat, 18 Feb 2023 17:29:43 +0300 Subject: [PATCH 2/2] fix: fb inputs --- Cargo.lock | 4 +-- lib/wasi-experimental-io-devices/Cargo.toml | 2 +- .../src/link-ext.rs | 27 +++++++++++-------- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 44bf7dea91a..dc9ee994cdd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2144,9 +2144,9 @@ dependencies = [ [[package]] name = "minifb" -version = "0.23.0" +version = "0.24.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9e21c5f89bb820c7878c300c5b944e65de0f1b2a75e0be92ce670b95943740e" +checksum = "c66a1fdd7e946fe33fe9725012e25836bba3655769bee9ee347cce7de3f396df" dependencies = [ "cc", "dlib", diff --git a/lib/wasi-experimental-io-devices/Cargo.toml b/lib/wasi-experimental-io-devices/Cargo.toml index c2ea81bfb84..38e6c05399f 100644 --- a/lib/wasi-experimental-io-devices/Cargo.toml +++ b/lib/wasi-experimental-io-devices/Cargo.toml @@ -17,7 +17,7 @@ maintenance = { status = "experimental" } wasmer-wasi = { version = "=3.2.0-alpha.1", path = "../wasi", default-features=false } wasmer-wasi-types = { path = "../wasi-types", version = "=3.2.0-alpha.1" } tracing = "0.1" -minifb = { version = "0.23", optional = true } +minifb = { version = "0.24.0", optional = true } nix = "0.25.0" ref_thread_local = "0.1" serde = "1" diff --git a/lib/wasi-experimental-io-devices/src/link-ext.rs b/lib/wasi-experimental-io-devices/src/link-ext.rs index 9fcd20e87b0..d4965f2e60e 100644 --- a/lib/wasi-experimental-io-devices/src/link-ext.rs +++ b/lib/wasi-experimental-io-devices/src/link-ext.rs @@ -50,7 +50,7 @@ pub(crate) struct FrameBufferState { pub last_mouse_pos: (u32, u32), pub inputs: VecDeque, - pub keys_pressed: BTreeSet, + pub keys: Vec, } impl FrameBufferState { @@ -74,7 +74,7 @@ impl FrameBufferState { window, last_mouse_pos: (0, 0), inputs: VecDeque::with_capacity(Self::MAX_INPUTS), - keys_pressed: BTreeSet::new(), + keys: Vec::new(), } } @@ -117,22 +117,27 @@ impl FrameBufferState { } pub fn fill_input_buffer(&mut self) -> Option<()> { - let keys_pressed = self.keys_pressed.iter().cloned().collect::>(); if !self.window.is_open() { self.push_input_event(InputEvent::WindowClosed)?; } - for key in keys_pressed { - if self.window.is_key_released(key) { - self.keys_pressed.remove(&key); - self.push_input_event(InputEvent::KeyRelease(key))?; + + let keys = self.keys.iter().cloned().collect::>(); + let new_keys = self.window.get_keys(); + + for key in &keys { + if !new_keys.contains(&key) { + self.push_input_event(InputEvent::KeyRelease(*key))?; } } - let keys = self.window.get_keys_pressed(KeyRepeat::No); - for key in keys { - self.keys_pressed.insert(key); - self.push_input_event(InputEvent::KeyPress(key))?; + + for key in &new_keys { + if !keys.contains(&key) { + self.push_input_event(InputEvent::KeyPress(*key))?; + } } + self.keys = new_keys; + let mouse_position = self.window.get_mouse_pos(minifb::MouseMode::Clamp)?; if mouse_position.0 as u32 != self.last_mouse_pos.0 || mouse_position.1 as u32 != self.last_mouse_pos.1