aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBrooks J Rady <b.j.rady@gmail.com>2021-03-23 19:52:59 +0000
committerBrooks J Rady <b.j.rady@gmail.com>2021-03-23 19:52:59 +0000
commitac55e590475fb68f29071fc06ded8f6d6bf5db22 (patch)
tree5cd73705a58232f10e9dd0943f2f503517356227 /src
parentMerge changes from main (diff)
downloadsl0ppij-ac55e590475fb68f29071fc06ded8f6d6bf5db22.tar.gz
sl0ppij-ac55e590475fb68f29071fc06ded8f6d6bf5db22.tar.xz
sl0ppij-ac55e590475fb68f29071fc06ded8f6d6bf5db22.zip
Initial implementation of the update callback + upstream termion
Diffstat (limited to 'src')
-rw-r--r--src/common/errors.rs2
-rw-r--r--src/common/input/handler.rs36
-rw-r--r--src/common/input/keybinds.rs1
-rw-r--r--src/common/mod.rs43
-rw-r--r--src/common/wasm_vm.rs3
5 files changed, 81 insertions, 4 deletions
diff --git a/src/common/errors.rs b/src/common/errors.rs
index b5b96342..11d13c7e 100644
--- a/src/common/errors.rs
+++ b/src/common/errors.rs
@@ -278,6 +278,7 @@ use crate::wasm_vm::PluginInstruction;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PluginContext {
Load,
+ Update,
Render,
Input,
GlobalInput,
@@ -290,6 +291,7 @@ impl From<&PluginInstruction> for PluginContext {
fn from(plugin_instruction: &PluginInstruction) -> Self {
match *plugin_instruction {
PluginInstruction::Load(..) => PluginContext::Load,
+ PluginInstruction::Update(_) => PluginContext::Update,
PluginInstruction::Render(..) => PluginContext::Render,
PluginInstruction::Input(..) => PluginContext::Input,
PluginInstruction::GlobalInput(_) => PluginContext::GlobalInput,
diff --git a/src/common/input/handler.rs b/src/common/input/handler.rs
index 29a504f5..90afb64c 100644
--- a/src/common/input/handler.rs
+++ b/src/common/input/handler.rs
@@ -11,7 +11,7 @@ use crate::wasm_vm::{NaughtyEventType, PluginInputType, PluginInstruction};
use crate::CommandIsExecuting;
use termion::input::TermReadEventsAndRaw;
-use zellij_tile::data::{Help, InputMode};
+use zellij_tile::data::{Event, Help, InputMode, Key};
use super::keybinds::key_to_actions;
@@ -61,6 +61,7 @@ impl InputHandler {
'input_loop: loop {
//@@@ I think this should actually just iterate over stdin directly
let stdin_buffer = self.os_input.read_from_stdin();
+ // FIXME: Kill me someday (soon)
drop(
self.send_plugin_instructions
.send(PluginInstruction::GlobalInput(stdin_buffer.clone())),
@@ -69,6 +70,11 @@ impl InputHandler {
match key_result {
Ok((event, raw_bytes)) => match event {
termion::event::Event::Key(key) => {
+ let key = cast_termion_key(key);
+ drop(
+ self.send_plugin_instructions
+ .send(PluginInstruction::Update(Event::KeyPress(key))),
+ );
// FIXME this explicit break is needed because the current test
// framework relies on it to not create dead threads that loop
// and eat up CPUs. Do not remove until the test framework has
@@ -320,3 +326,31 @@ pub fn input_loop(
)
.handle_input();
}
+
+// FIXME: This is an absolutely cursed function that should be destroyed as soon
+// as an alternative that doesn't touch zellij-tile can be developed...
+fn cast_termion_key(event: termion::event::Key) -> Key {
+ match event {
+ termion::event::Key::Backspace => Key::Backspace,
+ termion::event::Key::Left => Key::Left,
+ termion::event::Key::Right => Key::Right,
+ termion::event::Key::Up => Key::Up,
+ termion::event::Key::Down => Key::Down,
+ termion::event::Key::Home => Key::Home,
+ termion::event::Key::End => Key::End,
+ termion::event::Key::PageUp => Key::PageUp,
+ termion::event::Key::PageDown => Key::PageDown,
+ termion::event::Key::BackTab => Key::BackTab,
+ termion::event::Key::Delete => Key::Delete,
+ termion::event::Key::Insert => Key::Insert,
+ termion::event::Key::F(n) => Key::F(n),
+ termion::event::Key::Char(c) => Key::Char(c),
+ termion::event::Key::Alt(c) => Key::Alt(c),
+ termion::event::Key::Ctrl(c) => Key::Ctrl(c),
+ termion::event::Key::Null => Key::Null,
+ termion::event::Key::Esc => Key::Esc,
+ _ => {
+ unimplemented!("Encountered an unknown key!")
+ }
+ }
+}
diff --git a/src/common/input/keybinds.rs b/src/common/input/keybinds.rs
index 183b9a2c..e48d3115 100644
--- a/src/common/input/keybinds.rs
+++ b/src/common/input/keybinds.rs
@@ -5,7 +5,6 @@ use super::actions::{Action, Direction};
use std::collections::HashMap;
use strum::IntoEnumIterator;
-use termion::event::Key;
use zellij_tile::data::*;
type Keybinds = HashMap<InputMode, ModeKeybinds>;
diff --git a/src/common/mod.rs b/src/common/mod.rs
index 675fe828..70edcea8 100644
--- a/src/common/mod.rs
+++ b/src/common/mod.rs
@@ -40,7 +40,7 @@ use wasm_vm::{
};
use wasmer::{ChainableNamedResolver, Instance, Module, Store, Value};
use wasmer_wasi::{Pipe, WasiState};
-use zellij_tile::data::InputMode;
+use zellij_tile::data::{InputMode, Key};
#[derive(Serialize, Deserialize, Debug)]
pub enum ApiCommand {
@@ -456,6 +456,33 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
.collect();
move || loop {
+ // FIXME: This 100% *must* be destroyed before this makes in into main!!!!!!!!!!!
+ fn cast_termion_key(event: termion::event::Key) -> Key {
+ match event {
+ termion::event::Key::Backspace => Key::Backspace,
+ termion::event::Key::Left => Key::Left,
+ termion::event::Key::Right => Key::Right,
+ termion::event::Key::Up => Key::Up,
+ termion::event::Key::Down => Key::Down,
+ termion::event::Key::Home => Key::Home,
+ termion::event::Key::End => Key::End,
+ termion::event::Key::PageUp => Key::PageUp,
+ termion::event::Key::PageDown => Key::PageDown,
+ termion::event::Key::BackTab => Key::BackTab,
+ termion::event::Key::Delete => Key::Delete,
+ termion::event::Key::Insert => Key::Insert,
+ termion::event::Key::F(n) => Key::F(n),
+ termion::event::Key::Char(c) => Key::Char(c),
+ termion::event::Key::Alt(c) => Key::Alt(c),
+ termion::event::Key::Ctrl(c) => Key::Ctrl(c),
+ termion::event::Key::Null => Key::Null,
+ termion::event::Key::Esc => Key::Esc,
+ _ => {
+ unimplemented!("Encountered an unknown key!")
+ }
+ }
+ }
+
let (event, mut err_ctx) = receive_plugin_instructions
.recv()
.expect("failed to receive event on channel");
@@ -522,6 +549,17 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
pid_tx.send(plugin_id).unwrap();
plugin_id += 1;
}
+ PluginInstruction::Update(event) => {
+ for (instance, plugin_env) in plugin_map.values() {
+ let update = instance.exports.get_function("update").unwrap();
+
+ wasi_write_string(
+ &plugin_env.wasi_env,
+ &serde_json::to_string(&event).unwrap(),
+ );
+ update.call(&[]).unwrap();
+ }
+ }
PluginInstruction::Render(buf_tx, pid, rows, cols) => {
let (instance, plugin_env) = plugin_map.get(&pid).unwrap();
@@ -555,6 +593,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
let handle_key =
instance.exports.get_function("handle_key").unwrap();
for key in input_bytes.keys().flatten() {
+ let key = cast_termion_key(key);
wasi_write_string(
&plugin_env.wasi_env,
&serde_json::to_string(&key).unwrap(),
@@ -572,6 +611,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
.get_function(handler_map.get(&event).unwrap())
.unwrap();
for key in input_bytes.keys().flatten() {
+ let key = cast_termion_key(key);
wasi_write_string(
&plugin_env.wasi_env,
&serde_json::to_string(&key).unwrap(),
@@ -589,6 +629,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
let handler =
instance.exports.get_function("handle_global_key").unwrap();
for key in input_bytes.keys().flatten() {
+ let key = cast_termion_key(key);
wasi_write_string(
&plugin_env.wasi_env,
&serde_json::to_string(&key).unwrap(),
diff --git a/src/common/wasm_vm.rs b/src/common/wasm_vm.rs
index 788219a4..2f2a0cf6 100644
--- a/src/common/wasm_vm.rs
+++ b/src/common/wasm_vm.rs
@@ -9,7 +9,7 @@ use std::{
};
use wasmer::{imports, Function, ImportObject, Store, WasmerEnv};
use wasmer_wasi::WasiEnv;
-use zellij_tile::data::{EventType, TabInfo};
+use zellij_tile::data::{Event, EventType, TabInfo};
use super::{
input::handler::get_help, pty_bus::PtyInstruction, screen::ScreenInstruction, AppInstruction,
@@ -30,6 +30,7 @@ pub enum PluginInputType {
#[derive(Clone, Debug)]
pub enum PluginInstruction {
Load(Sender<u32>, PathBuf, Vec<NaughtyEventType>),
+ Update(Event),
Render(Sender<String>, u32, usize, usize), // String buffer, plugin id, rows, cols
Input(PluginInputType, Vec<u8>), // plugin id, input bytes
GlobalInput(Vec<u8>), // input bytes