Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: experimental io #3591

Merged
merged 4 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions lib/wasi-experimental-io-devices/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ 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 }
minifb = { version = "0.24.0", optional = true }
nix = "0.25.0"
ref_thread_local = "0.1"
serde = "1"
Expand All @@ -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 = [
Expand Down
40 changes: 23 additions & 17 deletions lib/wasi-experimental-io-devices/src/link-ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::io::{Read, Seek, SeekFrom, Write};
use tracing::debug;
use wasmer_wasi::types::{wasi::Filesize, *};
use wasmer_wasi::{VirtualFile, WasiFsError, ALL_RIGHTS};
use wasmer_wasi_types::wasi::Fdflags;

use minifb::{Key, KeyRepeat, MouseButton, Scale, Window, WindowOptions};

Expand Down Expand Up @@ -48,7 +49,7 @@ pub(crate) struct FrameBufferState {

pub last_mouse_pos: (u32, u32),
pub inputs: VecDeque<InputEvent>,
pub keys_pressed: BTreeSet<minifb::Key>,
pub keys: Vec<minifb::Key>,
}

impl FrameBufferState {
Expand All @@ -72,7 +73,7 @@ impl FrameBufferState {
window,
last_mouse_pos: (0, 0),
inputs: VecDeque::with_capacity(Self::MAX_INPUTS),
keys_pressed: BTreeSet::new(),
keys: Vec::new(),
}
}

Expand All @@ -95,7 +96,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);
Expand All @@ -115,22 +116,27 @@ impl FrameBufferState {
}

pub fn fill_input_buffer(&mut self) -> Option<()> {
let keys_pressed = self.keys_pressed.iter().cloned().collect::<Vec<Key>>();
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::<Vec<Key>>();
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
Expand Down Expand Up @@ -455,7 +461,7 @@ pub fn initialize(inodes: &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))?
};
Expand All @@ -469,7 +475,7 @@ pub fn initialize(inodes: &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))?;

Expand All @@ -484,7 +490,7 @@ pub fn initialize(inodes: &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))?;

Expand All @@ -499,7 +505,7 @@ pub fn initialize(inodes: &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))?;

Expand All @@ -514,7 +520,7 @@ pub fn initialize(inodes: &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))?;

Expand Down