aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrooks J Rady <b.j.rady@gmail.com>2020-11-17 21:40:01 +0000
committerBrooks J Rady <b.j.rady@gmail.com>2020-11-17 21:40:01 +0000
commit115bc83b8ebf4bf0cc338106d708ffcc9d20d2b5 (patch)
tree7dd198a6b2c393efca3dd38ef6bb5b77049e29c0
downloadsl0ppij-115bc83b8ebf4bf0cc338106d708ffcc9d20d2b5.tar.gz
sl0ppij-115bc83b8ebf4bf0cc338106d708ffcc9d20d2b5.tar.xz
sl0ppij-115bc83b8ebf4bf0cc338106d708ffcc9d20d2b5.zip
Initial Commit
-rw-r--r--.gitignore2
-rw-r--r--Cargo.toml9
-rw-r--r--src/keys.rs37
-rw-r--r--src/lib.rs43
-rw-r--r--src/shim.rs18
5 files changed, 109 insertions, 0 deletions
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 <b.j.rady@gmail.com>"]
+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();
+}