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();
}
PluginInstruction::Input(pid, input_bytes) => {
let (instance, plugin_env) = plugin_map.get(&pid).unwrap();
let handle_key =
instance.exports.get_function("handle_key").unwrap();
for key in input_bytes.keys() {
if let Ok(key) = key {
wasi_write_string(
&plugin_env.wasi_env,
&serde_json::to_string(&key).unwrap(),
);
handle_key.call(&[]).unwrap();
// FIXME: Set up an event subscription system, and timed callbacks
for (&id, (instance, plugin_env)) in &plugin_map {
let handler = if PaneId::Plugin(id) == pid {
"handle_key"
} else {
"handle_global_key"
};
let handler = instance.exports.get_function(handler).unwrap();
for key in input_bytes.keys() {
if let Ok(key) = key {
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>) {
match self.get_active_pane_id() {
Some(PaneId::Terminal(active_terminal_id)) => {
let active_terminal = self.get_active_pane().unwrap();
let mut adjusted_input = active_terminal.adjust_input_to_terminal(input_bytes);
self.os_api
.write_to_tty_stdin(active_terminal_id, &mut adjusted_input)
.expect("failed to write to terminal");
self.os_api
.tcdrain(active_terminal_id)
.expect("failed to drain terminal");
}
Some(PaneId::Plugin(pid)) => {
self.send_plugin_instructions
.send(PluginInstruction::Input(pid, input_bytes))
.unwrap();
}
_ => {}
let pid = self.get_active_pane_id();
if let Some(pid) = pid {
self.send_plugin_instructions
.send(PluginInstruction::Input(pid, input_bytes.clone()))
.unwrap();
}
if let Some(PaneId::Terminal(active_terminal_id)) = pid {
let active_terminal = self.get_active_pane().unwrap();
let mut adjusted_input = active_terminal.adjust_input_to_terminal(input_bytes);
self.os_api
.write_to_tty_stdin(active_terminal_id, &mut adjusted_input)
.expect("failed to write to terminal");
self.os_api
.tcdrain(active_terminal_id)
.expect("failed to drain terminal");
}
}
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_wasi::WasiEnv;
use crate::{pty_bus::PtyInstruction, SenderWithContext};
use crate::{panes::PaneId, pty_bus::PtyInstruction, SenderWithContext};
#[derive(Clone, Debug)]
pub enum PluginInstruction {
Load(Sender<u32>, PathBuf),
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),
Quit,
}

Binary file not shown.