Initial Commit

This commit is contained in:
Brooks J Rady 2020-11-17 21:40:01 +00:00
commit 115bc83b8e
5 changed files with 109 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
target/
Cargo.lock

9
Cargo.toml Normal file
View file

@ -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"

37
src/keys.rs Normal file
View file

@ -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,
}

43
src/lib.rs Normal file
View file

@ -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());
});
}
};
}

18
src/shim.rs Normal file
View file

@ -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();
}