From bb2369dcb84fe8d66e40d7cc670bafea3173bca1 Mon Sep 17 00:00:00 2001 From: Kyle Sutherland-Cash Date: Sat, 1 May 2021 08:48:58 -0700 Subject: [PATCH] Use Bus type for PTY thread (incomplete) --- src/client/panes/plugin_pane.rs | 2 +- src/client/panes/terminal_pane.rs | 2 +- src/client/tab.rs | 2 +- src/common/errors.rs | 2 +- src/common/input/handler.rs | 2 +- src/common/mod.rs | 68 ++++++++++++++++++------------- src/common/{pty_bus.rs => pty.rs} | 58 +++++++++++++------------- src/common/screen.rs | 8 ++-- src/common/utils/bus.rs | 0 src/common/wasm_vm.rs | 2 +- src/main.rs | 2 +- 11 files changed, 79 insertions(+), 69 deletions(-) rename src/common/{pty_bus.rs => pty.rs} (84%) create mode 100644 src/common/utils/bus.rs diff --git a/src/client/panes/plugin_pane.rs b/src/client/panes/plugin_pane.rs index c3b633ec..05871275 100644 --- a/src/client/panes/plugin_pane.rs +++ b/src/client/panes/plugin_pane.rs @@ -1,4 +1,4 @@ -use crate::{common::SenderWithContext, pty_bus::VteBytes, tab::Pane, wasm_vm::PluginInstruction}; +use crate::{common::SenderWithContext, common::pty::VteBytes, tab::Pane, wasm_vm::PluginInstruction}; use crate::panes::{PaneId, PositionAndSize}; diff --git a/src/client/panes/terminal_pane.rs b/src/client/panes/terminal_pane.rs index a5a957e6..6d03c910 100644 --- a/src/client/panes/terminal_pane.rs +++ b/src/client/panes/terminal_pane.rs @@ -8,7 +8,7 @@ use crate::panes::grid::Grid; use crate::panes::terminal_character::{ CharacterStyles, TerminalCharacter, EMPTY_TERMINAL_CHARACTER, }; -use crate::pty_bus::VteBytes; +use crate::common::pty::VteBytes; #[derive(PartialEq, Eq, Ord, PartialOrd, Hash, Clone, Copy, Debug)] pub enum PaneId { diff --git a/src/client/tab.rs b/src/client/tab.rs index f9bf7b57..50921991 100644 --- a/src/client/tab.rs +++ b/src/client/tab.rs @@ -7,7 +7,7 @@ use crate::common::{input::handler::parse_keys, AppInstruction, SenderWithContex use crate::layout::Layout; use crate::os_input_output::OsApi; use crate::panes::{PaneId, PositionAndSize, TerminalPane}; -use crate::pty_bus::{PtyInstruction, VteBytes}; +use crate::common::pty::{PtyInstruction, VteBytes}; use crate::utils::shared::adjust_to_size; use crate::wasm_vm::PluginInstruction; use crate::{boundaries::Boundaries, panes::PluginPane}; diff --git a/src/common/errors.rs b/src/common/errors.rs index c1c6753f..ab7c57ae 100644 --- a/src/common/errors.rs +++ b/src/common/errors.rs @@ -2,7 +2,7 @@ //! the instructions that are sent between threads. use super::{AppInstruction, ASYNCOPENCALLS, OPENCALLS}; -use crate::pty_bus::PtyInstruction; +use crate::common::pty::PtyInstruction; use crate::screen::ScreenInstruction; use std::fmt::{Display, Error, Formatter}; diff --git a/src/common/input/handler.rs b/src/common/input/handler.rs index 62118722..472a0520 100644 --- a/src/common/input/handler.rs +++ b/src/common/input/handler.rs @@ -6,7 +6,7 @@ use crate::common::input::config::Config; use crate::common::{AppInstruction, SenderWithContext, OPENCALLS}; use crate::errors::ContextType; use crate::os_input_output::OsApi; -use crate::pty_bus::PtyInstruction; +use crate::common::pty::PtyInstruction; use crate::screen::ScreenInstruction; use crate::wasm_vm::PluginInstruction; use crate::CommandIsExecuting; diff --git a/src/common/mod.rs b/src/common/mod.rs index 2b8adf4e..3ae1f570 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -4,7 +4,7 @@ pub mod input; pub mod install; pub mod ipc; pub mod os_input_output; -pub mod pty_bus; +pub mod pty; pub mod screen; pub mod utils; pub mod wasm_vm; @@ -36,7 +36,7 @@ use errors::{ use input::handler::input_loop; use install::populate_data_dir; use os_input_output::OsApi; -use pty_bus::{PtyBus, PtyInstruction}; +use pty::{Pty, PtyInstruction}; use screen::{Screen, ScreenInstruction}; use serde::{Deserialize, Serialize}; use utils::consts::ZELLIJ_IPC_PIPE; @@ -193,11 +193,16 @@ pub fn start(mut os_input: Box, opts: CliArgs) { let send_app_instructions = SenderWithContext::new(SenderType::SyncSender(send_app_instructions)); - let mut pty_bus = PtyBus::new( + let pty_bus = Bus::new( receive_pty_instructions, - send_screen_instructions.clone(), - send_plugin_instructions.clone(), - os_input.clone(), + Some(&send_screen_instructions), + None, + Some(&send_plugin_instructions), + None, + Some(&os_input), + ); + let mut pty = Pty::new( + pty_bus, opts.debug, ); @@ -233,50 +238,59 @@ pub fn start(mut os_input: Box, opts: CliArgs) { let mut command_is_executing = command_is_executing.clone(); send_pty_instructions.send(PtyInstruction::NewTab).unwrap(); move || loop { - let (event, mut err_ctx) = pty_bus - .receive_pty_instructions + let (event, mut err_ctx) = pty + .bus + .receiver .recv() .expect("failed to receive event on channel"); err_ctx.add_call(ContextType::Pty(PtyContext::from(&event))); match event { PtyInstruction::SpawnTerminal(file_to_open) => { - let pid = pty_bus.spawn_terminal(file_to_open); - pty_bus - .send_screen_instructions + let pid = pty.spawn_terminal(file_to_open); + pty.bus + .to_screen + .as_ref() + .unwrap() .send(ScreenInstruction::NewPane(PaneId::Terminal(pid))) .unwrap(); } PtyInstruction::SpawnTerminalVertically(file_to_open) => { - let pid = pty_bus.spawn_terminal(file_to_open); - pty_bus - .send_screen_instructions + let pid = pty.spawn_terminal(file_to_open); + pty.bus + .to_screen + .as_ref() + .unwrap() .send(ScreenInstruction::VerticalSplit(PaneId::Terminal(pid))) .unwrap(); } PtyInstruction::SpawnTerminalHorizontally(file_to_open) => { - let pid = pty_bus.spawn_terminal(file_to_open); - pty_bus - .send_screen_instructions + let pid = pty.spawn_terminal(file_to_open); + pty.bus + .to_screen + .as_ref() + .unwrap() .send(ScreenInstruction::HorizontalSplit(PaneId::Terminal(pid))) .unwrap(); } PtyInstruction::NewTab => { if let Some(layout) = maybe_layout.clone() { - pty_bus.spawn_terminals_for_layout(layout); + pty.spawn_terminals_for_layout(layout); } else { - let pid = pty_bus.spawn_terminal(None); - pty_bus - .send_screen_instructions + let pid = pty.spawn_terminal(None); + pty.bus + .to_screen + .as_ref() + .unwrap() .send(ScreenInstruction::NewTab(pid)) .unwrap(); } } PtyInstruction::ClosePane(id) => { - pty_bus.close_pane(id); + pty.close_pane(id); command_is_executing.done_closing_pane(); } PtyInstruction::CloseTab(ids) => { - pty_bus.close_tab(ids); + pty.close_tab(ids); command_is_executing.done_closing_pane(); } PtyInstruction::Quit => { @@ -302,12 +316,8 @@ pub fn start(mut os_input: Box, opts: CliArgs) { let max_panes = opts.max_panes; move || { - let mut screen = Screen::new( - screen_bus, - &full_screen_ws, - max_panes, - ModeInfo::default(), - ); + let mut screen = + Screen::new(screen_bus, &full_screen_ws, max_panes, ModeInfo::default()); loop { let (event, mut err_ctx) = screen .bus diff --git a/src/common/pty_bus.rs b/src/common/pty.rs similarity index 84% rename from src/common/pty_bus.rs rename to src/common/pty.rs index 2c132228..3a6798f1 100644 --- a/src/common/pty_bus.rs +++ b/src/common/pty.rs @@ -4,7 +4,6 @@ use ::async_std::task::*; use ::std::collections::HashMap; use ::std::os::unix::io::RawFd; use ::std::pin::*; -use ::std::sync::mpsc::Receiver; use ::std::time::{Duration, Instant}; use std::path::PathBuf; @@ -12,7 +11,8 @@ use super::{ScreenInstruction, SenderWithContext}; use crate::os_input_output::OsApi; use crate::utils::logging::debug_to_file; use crate::{ - errors::{get_current_ctx, ContextType, ErrorContext}, + common::Bus, + errors::{get_current_ctx, ContextType}, panes::PaneId, }; use crate::{layout::Layout, wasm_vm::PluginInstruction}; @@ -77,12 +77,9 @@ pub enum PtyInstruction { Quit, } -pub struct PtyBus { - pub send_screen_instructions: SenderWithContext, - pub send_plugin_instructions: SenderWithContext, - pub receive_pty_instructions: Receiver<(PtyInstruction, ErrorContext)>, +pub struct Pty { + pub bus: Bus, pub id_to_child_pid: HashMap, - os_input: Box, debug_to_file: bool, task_handles: HashMap>, } @@ -156,31 +153,29 @@ fn stream_terminal_bytes( }) } -impl PtyBus { +impl Pty { pub fn new( - receive_pty_instructions: Receiver<(PtyInstruction, ErrorContext)>, - send_screen_instructions: SenderWithContext, - send_plugin_instructions: SenderWithContext, - os_input: Box, + bus: Bus, debug_to_file: bool, ) -> Self { - PtyBus { - send_screen_instructions, - send_plugin_instructions, - receive_pty_instructions, - os_input, + Pty { + bus, id_to_child_pid: HashMap::new(), debug_to_file, task_handles: HashMap::new(), } } pub fn spawn_terminal(&mut self, file_to_open: Option) -> RawFd { - let (pid_primary, pid_secondary): (RawFd, RawFd) = - self.os_input.spawn_terminal(file_to_open); + let (pid_primary, pid_secondary): (RawFd, RawFd) = self + .bus + .os_input + .as_mut() + .unwrap() + .spawn_terminal(file_to_open); let task_handle = stream_terminal_bytes( pid_primary, - self.send_screen_instructions.clone(), - self.os_input.clone(), + self.bus.to_screen.as_ref().unwrap().clone(), + self.bus.os_input.as_ref().unwrap().clone(), self.debug_to_file, ); self.task_handles.insert(pid_primary, task_handle); @@ -191,11 +186,15 @@ impl PtyBus { let total_panes = layout.total_terminal_panes(); let mut new_pane_pids = vec![]; for _ in 0..total_panes { - let (pid_primary, pid_secondary): (RawFd, RawFd) = self.os_input.spawn_terminal(None); + let (pid_primary, pid_secondary): (RawFd, RawFd) = + self.bus.os_input.as_mut().unwrap().spawn_terminal(None); self.id_to_child_pid.insert(pid_primary, pid_secondary); new_pane_pids.push(pid_primary); } - self.send_screen_instructions + self.bus + .to_screen + .as_ref() + .unwrap() .send(ScreenInstruction::ApplyLayout(( layout, new_pane_pids.clone(), @@ -204,8 +203,8 @@ impl PtyBus { for id in new_pane_pids { let task_handle = stream_terminal_bytes( id, - self.send_screen_instructions.clone(), - self.os_input.clone(), + self.bus.to_screen.as_ref().unwrap().clone(), + self.bus.os_input.as_ref().unwrap().clone(), self.debug_to_file, ); self.task_handles.insert(id, task_handle); @@ -216,13 +215,16 @@ impl PtyBus { PaneId::Terminal(id) => { let child_pid = self.id_to_child_pid.remove(&id).unwrap(); let handle = self.task_handles.remove(&id).unwrap(); - self.os_input.kill(child_pid).unwrap(); + self.bus.os_input.as_mut().unwrap().kill(child_pid).unwrap(); task::block_on(async { handle.cancel().await; }); } PaneId::Plugin(pid) => drop( - self.send_plugin_instructions + self.bus + .to_plugin + .as_ref() + .unwrap() .send(PluginInstruction::Unload(pid)), ), } @@ -234,7 +236,7 @@ impl PtyBus { } } -impl Drop for PtyBus { +impl Drop for Pty { fn drop(&mut self) { let child_ids: Vec = self.id_to_child_pid.keys().copied().collect(); for id in child_ids { diff --git a/src/common/screen.rs b/src/common/screen.rs index 513368c7..d5df24b0 100644 --- a/src/common/screen.rs +++ b/src/common/screen.rs @@ -3,15 +3,13 @@ use std::collections::BTreeMap; use std::os::unix::io::RawFd; use std::str; -use std::sync::mpsc::Receiver; -use super::{AppInstruction, SenderWithContext}; +use super::AppInstruction; use crate::common::Bus; -use crate::os_input_output::OsApi; use crate::panes::PositionAndSize; -use crate::pty_bus::{PtyInstruction, VteBytes}; +use crate::common::pty::{PtyInstruction, VteBytes}; use crate::tab::Tab; -use crate::{errors::ErrorContext, wasm_vm::PluginInstruction}; +use crate::wasm_vm::PluginInstruction; use crate::{layout::Layout, panes::PaneId}; use zellij_tile::data::{Event, ModeInfo, TabInfo}; diff --git a/src/common/utils/bus.rs b/src/common/utils/bus.rs new file mode 100644 index 00000000..e69de29b diff --git a/src/common/wasm_vm.rs b/src/common/wasm_vm.rs index bd98a85b..f04d4d5f 100644 --- a/src/common/wasm_vm.rs +++ b/src/common/wasm_vm.rs @@ -12,7 +12,7 @@ use wasmer_wasi::WasiEnv; use zellij_tile::data::{Event, EventType, PluginIds}; use super::{ - pty_bus::PtyInstruction, screen::ScreenInstruction, AppInstruction, PaneId, SenderWithContext, + pty::PtyInstruction, screen::ScreenInstruction, AppInstruction, PaneId, SenderWithContext, }; #[derive(Clone, Debug)] diff --git a/src/main.rs b/src/main.rs index 12269ae9..fbe76a84 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ use crate::utils::{ }; use client::{boundaries, layout, panes, tab}; use common::{ - command_is_executing, errors, install, os_input_output, pty_bus, screen, start, utils, wasm_vm, + command_is_executing, errors, install, os_input_output, screen, start, utils, wasm_vm, ApiCommand, }; use std::io::Write;