Add a plugin callback for global keypresses
This commit is contained in:
parent
624f842a20
commit
7e308515e5
4 changed files with 35 additions and 30 deletions
15
src/main.rs
15
src/main.rs
|
|
@ -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"
|
||||||
|
} else {
|
||||||
|
"handle_global_key"
|
||||||
|
};
|
||||||
|
let handler = instance.exports.get_function(handler).unwrap();
|
||||||
for key in input_bytes.keys() {
|
for key in input_bytes.keys() {
|
||||||
if let Ok(key) = key {
|
if let Ok(key) = key {
|
||||||
wasi_write_string(
|
wasi_write_string(
|
||||||
&plugin_env.wasi_env,
|
&plugin_env.wasi_env,
|
||||||
&serde_json::to_string(&key).unwrap(),
|
&serde_json::to_string(&key).unwrap(),
|
||||||
);
|
);
|
||||||
handle_key.call(&[]).unwrap();
|
handler.call(&[]).unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
18
src/tab.rs
18
src/tab.rs
|
|
@ -473,8 +473,15 @@ 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)) => {
|
|
||||||
|
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 active_terminal = self.get_active_pane().unwrap();
|
||||||
let mut adjusted_input = active_terminal.adjust_input_to_terminal(input_bytes);
|
let mut adjusted_input = active_terminal.adjust_input_to_terminal(input_bytes);
|
||||||
self.os_api
|
self.os_api
|
||||||
|
|
@ -484,13 +491,6 @@ impl Tab {
|
||||||
.tcdrain(active_terminal_id)
|
.tcdrain(active_terminal_id)
|
||||||
.expect("failed to drain terminal");
|
.expect("failed to drain terminal");
|
||||||
}
|
}
|
||||||
Some(PaneId::Plugin(pid)) => {
|
|
||||||
self.send_plugin_instructions
|
|
||||||
.send(PluginInstruction::Input(pid, input_bytes))
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
pub fn get_active_terminal_cursor_position(&self) -> Option<(usize, usize)> {
|
pub fn get_active_terminal_cursor_position(&self) -> Option<(usize, usize)> {
|
||||||
// (x, y)
|
// (x, y)
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
BIN
strider.wasm
BIN
strider.wasm
Binary file not shown.
Loading…
Add table
Reference in a new issue