commit 115bc83b8ebf4bf0cc338106d708ffcc9d20d2b5 Author: Brooks J Rady Date: Tue Nov 17 21:40:01 2020 +0000 Initial Commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..44709884 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target/ +Cargo.lock \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 00000000..90fc26cf --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "mosaic-tile" +version = "0.1.0" +authors = ["Brooks J Rady "] +edition = "2018" + +[dependencies] +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" \ No newline at end of file diff --git a/src/keys.rs b/src/keys.rs new file mode 100644 index 00000000..ed44554a --- /dev/null +++ b/src/keys.rs @@ -0,0 +1,37 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash, Serialize, Deserialize)] +pub struct KeyEvent { + pub code: KeyCode, + pub modifiers: KeyModifiers, +} + +#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash, Serialize, Deserialize)] +pub struct KeyModifiers { + pub bits: u8, + //pub shift: bool, + //pub ctrl: bool, + //pub alt: bool, +} + +#[derive(Debug, PartialOrd, PartialEq, Eq, Clone, Copy, Hash, Serialize, Deserialize)] +pub enum KeyCode { + Backspace, + Enter, + Left, + Right, + Up, + Down, + Home, + End, + PageUp, + PageDown, + Tab, + BackTab, + Delete, + Insert, + F(u8), + Char(char), + Null, + Esc, +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 00000000..536504ff --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,43 @@ +mod keys; +mod shim; + +pub use keys::*; +pub use shim::*; + +pub trait MosaicPlugin { + fn init(&mut self); + fn draw(&mut self, rows: usize, cols: usize); + fn handle_key(&mut self, key: KeyEvent); +} + +#[macro_export] +macro_rules! register_plugin { + ($t:ty) => { + use mosaic_plugin::*; + + use std::cell::RefCell; + thread_local! { + static STATE: RefCell<$t> = RefCell::new(Default::default()); + } + + fn main() { + STATE.with(|state| { + state.borrow_mut().init(); + }); + } + + #[no_mangle] + pub fn draw(rows: i32, cols: i32) { + STATE.with(|state| { + state.borrow_mut().draw(rows as usize, cols as usize); + }); + } + + #[no_mangle] + pub fn handle_key() { + STATE.with(|state| { + state.borrow_mut().handle_key(get_key()); + }); + } + }; +} diff --git a/src/shim.rs b/src/shim.rs new file mode 100644 index 00000000..850d1668 --- /dev/null +++ b/src/shim.rs @@ -0,0 +1,18 @@ +use crate::keys::KeyEvent; +use std::{io, path::Path}; + +pub fn get_key() -> KeyEvent { + let mut json = String::new(); + io::stdin().read_line(&mut json).unwrap(); + serde_json::from_str(&json).unwrap() +} + +pub fn open_file(path: &Path) { + println!("{}", path.to_string_lossy()); + unsafe { host_open_file() }; +} + +#[link(wasm_import_module = "mosaic")] +extern "C" { + fn host_open_file(); +}