diff options
| author | Kunal Mohan <kunalmohan99@gmail.com> | 2021-04-14 01:41:40 +0530 |
|---|---|---|
| committer | Kunal Mohan <kunalmohan99@gmail.com> | 2021-05-04 20:48:15 +0530 |
| commit | fedb588330713b815bdd2380c00f8c0762f4e859 (patch) | |
| tree | 26dc04734b27582c25c91c6573b825e5e1733abc /src/common | |
| parent | fix terminal window resize tests by restricting lifetime of locks (diff) | |
| download | sl0ppij-fedb588330713b815bdd2380c00f8c0762f4e859.tar.gz sl0ppij-fedb588330713b815bdd2380c00f8c0762f4e859.tar.xz sl0ppij-fedb588330713b815bdd2380c00f8c0762f4e859.zip | |
Fix after rebase
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/errors.rs | 2 | ||||
| -rw-r--r-- | src/common/input/handler.rs | 81 | ||||
| -rw-r--r-- | src/common/mod.rs | 13 | ||||
| -rw-r--r-- | src/common/os_input_output.rs | 8 | ||||
| -rw-r--r-- | src/common/pty_bus.rs | 112 | ||||
| -rw-r--r-- | src/common/screen.rs | 2 |
6 files changed, 75 insertions, 143 deletions
diff --git a/src/common/errors.rs b/src/common/errors.rs index 10b2165a..5685f885 100644 --- a/src/common/errors.rs +++ b/src/common/errors.rs @@ -1,7 +1,7 @@ //! Error context system based on a thread-local representation of the call stack, itself based on //! the instructions that are sent between threads. -use super::{ServerInstruction, OPENCALLS}; +use super::{ServerInstruction, ASYNCOPENCALLS, OPENCALLS}; use crate::client::ClientInstruction; use crate::pty_bus::PtyInstruction; use crate::screen::ScreenInstruction; diff --git a/src/common/input/handler.rs b/src/common/input/handler.rs index 947a8d02..2fba8537 100644 --- a/src/common/input/handler.rs +++ b/src/common/input/handler.rs @@ -1,12 +1,14 @@ //! Main input logic. use super::actions::Action; -use super::keybinds::get_default_keybinds; +use super::keybinds::Keybinds; use crate::client::ClientInstruction; +use crate::common::input::config::Config; use crate::common::{SenderWithContext, OPENCALLS}; use crate::errors::ContextType; use crate::os_input_output::ClientOsApi; use crate::pty_bus::PtyInstruction; +use crate::screen::ScreenInstruction; use crate::server::ServerInstruction; use crate::CommandIsExecuting; @@ -19,8 +21,10 @@ struct InputHandler { /// The current input mode mode: InputMode, os_input: Box<dyn ClientOsApi>, + config: Config, command_is_executing: CommandIsExecuting, send_client_instructions: SenderWithContext<ClientInstruction>, + should_exit: bool, } impl InputHandler { @@ -28,6 +32,7 @@ impl InputHandler { fn new( os_input: Box<dyn ClientOsApi>, command_is_executing: CommandIsExecuting, + config: Config, send_client_instructions: SenderWithContext<ClientInstruction>, ) -> Self { InputHandler { @@ -36,6 +41,7 @@ impl InputHandler { config, command_is_executing, send_client_instructions, + should_exit: false, } } @@ -44,34 +50,26 @@ impl InputHandler { fn handle_input(&mut self) { let mut err_ctx = OPENCALLS.with(|ctx| *ctx.borrow()); err_ctx.add_call(ContextType::StdinHandler); - self.send_client_instructions.update(err_ctx); - self.os_input.update_senders(err_ctx); - if let Ok(keybinds) = get_default_keybinds() { - 'input_loop: loop { - //@@@ I think this should actually just iterate over stdin directly - let stdin_buffer = self.os_input.read_from_stdin(); - for key_result in stdin_buffer.events_and_raw() { - match key_result { - Ok((event, raw_bytes)) => match event { - termion::event::Event::Key(key) => { - let key = cast_termion_key(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 - // been revised. Sorry about this (@categorille) - let mut should_break = false; - for action in key_to_actions(&key, raw_bytes, &self.mode, &keybinds) - { - should_break |= self.dispatch_action(action); - } - if should_break { - break 'input_loop; - } - } - termion::event::Event::Mouse(_) - | termion::event::Event::Unsupported(_) => { - // Mouse and unsupported events aren't implemented yet, - // use a NoOp untill then. + let keybinds = self.config.keybinds.clone(); + let alt_left_bracket = vec![27, 91]; + loop { + if self.should_exit { + break; + } + let stdin_buffer = self.os_input.read_from_stdin(); + for key_result in stdin_buffer.events_and_raw() { + match key_result { + Ok((event, raw_bytes)) => match event { + termion::event::Event::Key(key) => { + let key = cast_termion_key(key); + self.handle_key(&key, raw_bytes, &keybinds); + } + termion::event::Event::Unsupported(unsupported_key) => { + // we have to do this because of a bug in termion + // this should be a key event and not an unsupported event + if unsupported_key == alt_left_bracket { + let key = Key::Alt('['); + self.handle_key(&key, raw_bytes, &keybinds); } } termion::event::Event::Mouse(_) => { @@ -139,8 +137,19 @@ impl InputHandler { }; self.os_input.send_to_server(screen_instr); } - Action::SwitchFocus(_) => { - self.os_input.send_to_server(ServerInstruction::MoveFocus); + Action::SwitchFocus => { + self.os_input + .send_to_server(ServerInstruction::ToScreen(ScreenInstruction::SwitchFocus)); + } + Action::FocusNextPane => { + self.os_input.send_to_server(ServerInstruction::ToScreen( + ScreenInstruction::FocusNextPane, + )); + } + Action::FocusPreviousPane => { + self.os_input.send_to_server(ServerInstruction::ToScreen( + ScreenInstruction::FocusPreviousPane, + )); } Action::MoveFocus(direction) => { let screen_instr = match direction { @@ -299,11 +308,17 @@ pub fn get_mode_info(mode: InputMode, palette: Palette) -> ModeInfo { /// its [`InputHandler::handle_input()`] loop. pub fn input_loop( os_input: Box<dyn ClientOsApi>, + config: Config, command_is_executing: CommandIsExecuting, send_client_instructions: SenderWithContext<ClientInstruction>, ) { - let _handler = - InputHandler::new(os_input, command_is_executing, send_client_instructions).handle_input(); + let _handler = InputHandler::new( + os_input, + command_is_executing, + config, + send_client_instructions, + ) + .handle_input(); } pub fn parse_keys(input_bytes: &[u8]) -> Vec<Key> { diff --git a/src/common/mod.rs b/src/common/mod.rs index d9b5e6cf..6061f1a6 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -11,7 +11,8 @@ pub mod wasm_vm; use crate::panes::PaneId; use crate::server::ServerInstruction; -use errors::ErrorContext; +use async_std::task_local; +use errors::{get_current_ctx, ErrorContext}; use std::cell::RefCell; use std::sync::mpsc; @@ -43,8 +44,8 @@ pub struct SenderWithContext<T: Clone> { } impl<T: Clone> SenderWithContext<T> { - pub fn new(err_ctx: ErrorContext, sender: SenderType<T>) -> Self { - Self { err_ctx, sender } + pub fn new(sender: SenderType<T>) -> Self { + Self { sender } } /// Sends an event, along with the current [`ErrorContext`], on this @@ -66,3 +67,9 @@ thread_local!( /// stack in the form of an [`ErrorContext`]. pub static OPENCALLS: RefCell<ErrorContext> = RefCell::default() ); + +task_local! { + /// A key to some task local storage that holds a representation of the task's call + /// stack in the form of an [`ErrorContext`]. + static ASYNCOPENCALLS: RefCell<ErrorContext> = RefCell::default() +} diff --git a/src/common/os_input_output.rs b/src/common/os_input_output.rs index 66957a4a..3d7b8918 100644 --- a/src/common/os_input_output.rs +++ b/src/common/os_input_output.rs @@ -256,7 +256,13 @@ impl ServerOsApi for ServerOsInputOutput { Box::new((*self).clone()) } fn kill(&mut self, pid: RawFd) -> Result<(), nix::Error> { - kill(Pid::from_raw(pid), Some(Signal::SIGINT)).unwrap(); + // TODO: + // Ideally, we should be using SIGINT rather than SIGKILL here, but there are cases in which + // the terminal we're trying to kill hangs on SIGINT and so all the app gets stuck + // that's why we're sending SIGKILL here + // A better solution would be to send SIGINT here and not wait for it, and then have + // a background thread do the waitpid stuff and send SIGKILL if the process is stuck + kill(Pid::from_raw(pid), Some(Signal::SIGKILL)).unwrap(); waitpid(Pid::from_raw(pid), None).unwrap(); Ok(()) } diff --git a/src/common/pty_bus.rs b/src/common/pty_bus.rs index b9bb422b..b9f61d09 100644 --- a/src/common/pty_bus.rs +++ b/src/common/pty_bus.rs @@ -6,16 +6,15 @@ use ::std::os::unix::io::RawFd; use ::std::pin::*; use ::std::sync::mpsc::Receiver; use ::std::time::{Duration, Instant}; -use ::vte; use serde::{Deserialize, Serialize}; use std::path::PathBuf; -use super::{SenderWithContext, OPENCALLS}; +use super::SenderWithContext; use crate::layout::Layout; use crate::os_input_output::ServerOsApi; use crate::utils::logging::debug_to_file; use crate::{ - errors::{ContextType, ErrorContext}, + errors::{get_current_ctx, ContextType, ErrorContext}, panes::PaneId, screen::ScreenInstruction, wasm_vm::PluginInstruction, @@ -67,99 +66,7 @@ impl Stream for ReadFromPid { } } -#[derive(Debug, Clone, Serialize, Deserialize)] -pub enum VteEvent { - // TODO: try not to allocate Vecs - Print(char), - Execute(u8), // byte - Hook(Vec<i64>, Vec<u8>, bool, char), // params, intermediates, ignore, char - Put(u8), // byte - Unhook, - OscDispatch(Vec<Vec<u8>>, bool), // params, bell_terminated - CsiDispatch(Vec<i64>, Vec<u8>, bool, char), // params, intermediates, ignore, char - EscDispatch(Vec<u8>, bool, u8), // intermediates, ignore, byte -} - -struct VteEventSender { - id: RawFd, - send_screen_instructions: SenderWithContext<ScreenInstruction>, -} - -impl VteEventSender { - pub fn new(id: RawFd, send_screen_instructions: SenderWithContext<ScreenInstruction>) -> Self { - VteEventSender { - id, - send_screen_instructions, - } - } -} - -impl vte::Perform for VteEventSender { - fn print(&mut self, c: char) { - self.send_screen_instructions - .send(ScreenInstruction::Pty(self.id, VteEvent::Print(c))) - .unwrap(); - } - fn execute(&mut self, byte: u8) { - self.send_screen_instructions - .send(ScreenInstruction::Pty(self.id, VteEvent::Execute(byte))) - .unwrap(); - } - - fn hook(&mut self, params: &[i64], intermediates: &[u8], ignore: bool, c: char) { - let params = params.iter().copied().collect(); - let intermediates = intermediates.iter().copied().collect(); - self.send_screen_instructions - .send(ScreenInstruction::Pty( - self.id, - VteEvent::Hook(params, intermediates, ignore, c), - )) - .unwrap(); - } - - fn put(&mut self, byte: u8) { - self.send_screen_instructions - .send(ScreenInstruction::Pty(self.id, VteEvent::Put(byte))) - .unwrap(); - } - - fn unhook(&mut self) { - self.send_screen_instructions - .send(ScreenInstruction::Pty(self.id, VteEvent::Unhook)) - .unwrap(); - } - - fn osc_dispatch(&mut self, params: &[&[u8]], bell_terminated: bool) { - let params = params.iter().map(|p| p.to_vec()).collect(); - self.send_screen_instructions - .send(ScreenInstruction::Pty( - self.id, - VteEvent::OscDispatch(params, bell_terminated), - )) - .unwrap(); - } - - fn csi_dispatch(&mut self, params: &[i64], intermediates: &[u8], ignore: bool, c: char) { - let params = params.iter().copied().collect(); - let intermediates = intermediates.iter().copied().collect(); - self.send_screen_instructions - .send(ScreenInstruction::Pty( - self.id, - VteEvent::CsiDispatch(params, intermediates, ignore, c), - )) - .unwrap(); - } - - fn esc_dispatch(&mut self, intermediates: &[u8], ignore: bool, byte: u8) { - let intermediates = intermediates.iter().copied().collect(); - self.send_screen_instructions - .send(ScreenInstruction::Pty( - self.id, - VteEvent::EscDispatch(intermediates, ignore, byte), - )) - .unwrap(); - } -} +pub type VteBytes = Vec<u8>; /// Instructions related to PTYs (pseudoterminals). #[derive(Clone, Debug, Serialize, Deserialize)] @@ -185,18 +92,15 @@ pub struct PtyBus { fn stream_terminal_bytes( pid: RawFd, + send_screen_instructions: SenderWithContext<ScreenInstruction>, os_input: Box<dyn ServerOsApi>, - mut send_screen_instructions: SenderWithContext<ScreenInstruction>, debug: bool, ) -> JoinHandle<()> { - let mut err_ctx = OPENCALLS.with(|ctx| *ctx.borrow()); + let mut err_ctx = get_current_ctx(); task::spawn({ async move { err_ctx.add_call(ContextType::AsyncTask); - send_screen_instructions.update(err_ctx); - let mut vte_parser = vte::Parser::new(); - let mut vte_event_sender = VteEventSender::new(pid, send_screen_instructions.clone()); - let mut terminal_bytes = ReadFromPid::new(&pid, os_input.clone()); + let mut terminal_bytes = ReadFromPid::new(&pid, os_input); let mut last_byte_receive_time: Option<Instant> = None; let mut pending_render = false; @@ -282,8 +186,8 @@ impl PtyBus { self.os_input.spawn_terminal(file_to_open); let task_handle = stream_terminal_bytes( pid_primary, - self.os_input.clone(), self.send_screen_instructions.clone(), + self.os_input.clone(), self.debug_to_file, ); self.task_handles.insert(pid_primary, task_handle); @@ -308,8 +212,8 @@ impl PtyBus { for id in new_pane_pids { let task_handle = stream_terminal_bytes( id, - self.os_input.clone(), self.send_screen_instructions.clone(), + self.os_input.clone(), self.debug_to_file, ); self.task_handles.insert(id, task_handle); diff --git a/src/common/screen.rs b/src/common/screen.rs index 92e1075c..8c83f59e 100644 --- a/src/common/screen.rs +++ b/src/common/screen.rs @@ -10,7 +10,7 @@ use std::sync::mpsc::Receiver; use crate::common::SenderWithContext; use crate::os_input_output::ServerOsApi; use crate::panes::PositionAndSize; -use crate::pty_bus::{PtyInstruction, VteEvent}; +use crate::pty_bus::{PtyInstruction, VteBytes}; use crate::server::ServerInstruction; use crate::tab::Tab; use crate::{errors::ErrorContext, wasm_vm::PluginInstruction}; |
