Add a plugin callback for global keypresses

This commit is contained in:
Brooks J Rady 2021-01-10 23:02:41 +00:00
parent 624f842a20
commit 7e308515e5
4 changed files with 35 additions and 30 deletions

View file

@ -458,17 +458,22 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
buf_tx.send(wasi_stdout(&plugin_env.wasi_env)).unwrap(); buf_tx.send(wasi_stdout(&plugin_env.wasi_env)).unwrap();
} }
PluginInstruction::Input(pid, input_bytes) => { PluginInstruction::Input(pid, input_bytes) => {
let (instance, plugin_env) = plugin_map.get(&pid).unwrap(); // FIXME: Set up an event subscription system, and timed callbacks
for (&id, (instance, plugin_env)) in &plugin_map {
let handle_key = let handler = if PaneId::Plugin(id) == pid {
instance.exports.get_function("handle_key").unwrap(); "handle_key"
for key in input_bytes.keys() { } else {
if let Ok(key) = key { "handle_global_key"
wasi_write_string( };
&plugin_env.wasi_env, let handler = instance.exports.get_function(handler).unwrap();
&serde_json::to_string(&key).unwrap(), for key in input_bytes.keys() {
); if let Ok(key) = key {
handle_key.call(&[]).unwrap(); wasi_write_string(
&plugin_env.wasi_env,
&serde_json::to_string(&key).unwrap(),
);
handler.call(&[]).unwrap();
}
} }
} }

View file

@ -473,23 +473,23 @@ impl Tab {
} }
} }
pub fn write_to_active_terminal(&mut self, input_bytes: Vec<u8>) { pub fn write_to_active_terminal(&mut self, input_bytes: Vec<u8>) {
match self.get_active_pane_id() { let pid = self.get_active_pane_id();
Some(PaneId::Terminal(active_terminal_id)) => {
let active_terminal = self.get_active_pane().unwrap(); if let Some(pid) = pid {
let mut adjusted_input = active_terminal.adjust_input_to_terminal(input_bytes); self.send_plugin_instructions
self.os_api .send(PluginInstruction::Input(pid, input_bytes.clone()))
.write_to_tty_stdin(active_terminal_id, &mut adjusted_input) .unwrap();
.expect("failed to write to terminal"); }
self.os_api
.tcdrain(active_terminal_id) if let Some(PaneId::Terminal(active_terminal_id)) = pid {
.expect("failed to drain terminal"); let active_terminal = self.get_active_pane().unwrap();
} let mut adjusted_input = active_terminal.adjust_input_to_terminal(input_bytes);
Some(PaneId::Plugin(pid)) => { self.os_api
self.send_plugin_instructions .write_to_tty_stdin(active_terminal_id, &mut adjusted_input)
.send(PluginInstruction::Input(pid, input_bytes)) .expect("failed to write to terminal");
.unwrap(); self.os_api
} .tcdrain(active_terminal_id)
_ => {} .expect("failed to drain terminal");
} }
} }
pub fn get_active_terminal_cursor_position(&self) -> Option<(usize, usize)> { pub fn get_active_terminal_cursor_position(&self) -> Option<(usize, usize)> {

View file

@ -2,13 +2,13 @@ use std::{path::PathBuf, sync::mpsc::Sender};
use wasmer::{imports, Function, ImportObject, Store, WasmerEnv}; use wasmer::{imports, Function, ImportObject, Store, WasmerEnv};
use wasmer_wasi::WasiEnv; use wasmer_wasi::WasiEnv;
use crate::{pty_bus::PtyInstruction, SenderWithContext}; use crate::{panes::PaneId, pty_bus::PtyInstruction, SenderWithContext};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum PluginInstruction { pub enum PluginInstruction {
Load(Sender<u32>, PathBuf), Load(Sender<u32>, PathBuf),
Draw(Sender<String>, u32, usize, usize), // String buffer, plugin id, rows, cols Draw(Sender<String>, u32, usize, usize), // String buffer, plugin id, rows, cols
Input(u32, Vec<u8>), // plugin id, input bytes Input(PaneId, Vec<u8>), // pane id, input bytes
Unload(u32), Unload(u32),
Quit, Quit,
} }

Binary file not shown.