* Initial mouse support * enable mouse support (termion MouseTerminal) * handle scroll up and down events * Allow enabling/disabling of mouse reporting Store the mouse terminal on OsInputOutput * WIP: switch pane focus with mouse * testing to get mouse character selection * wip mouse selection * wip: mouse selection - initial handling of mouse events for text selection within a pane - wide characters currently problematic - selection is not highlighted * highlight currently selected text * improve get currently selected text from TerminalPane * copy selection to clipboard (wayland only for now) * Add missing set_should_render on selection update * Improve text selection - Strip whitespace from end of lines - Insert newlines when selection spans multiple lines * Simplify selection logic - Remove Range struct - Selection is not an Option anymore, it's empty when start == end * copy selection to clipboard with OSC-52 sequence * Improve text selection with multiple panes - Constrain mouse hold and release events to currently active pane - Fix calculation of columns with side by side panes * fix for PositionAndSize changes * Fix mouse selection with full screen pane. - Transform mouse event positions to relative when passing to pane. * Move selection to grid, rework highlighting. - Mark selected lines for update in grid output buffer, for now in the simplest way possible, but can be made more efficient by calculating changed lines only. - Clear selection on pane resize. - Re-add logic to forward mouse hold and release events only to currently active pane. * Tidy up selection - add method to get selection line range - add method to sort the current selection * Grid: move current selection up/down when scrolling - Make the selection work with lines in lines_above and lines_below - Todo: update selection end when scrolling with mouse button being held - Todo: figure out how to move selection when new characters are added. * Grid: move selection when new lines are added * Keep track of selection being active - Handle the selection growing/shrinking when scrolling with mouse button held down but not yet released. * Improve selection end with multiple panes * Tidying up - remove logging statements, unused code * Add some unit tests for selection, move position to zellij-utils * Change Position::new to take i32 for line * Grid: add unit tests for copy from selection * add basic integration test for mouse pane focus * Add basic integration test for mouse scroll * Use color from palette for selection rendering * Improve performance of selection render - Try to minimize lines to update on selection start/update/end * fixes for updated start method * fix lines not in viewport being marked for rendering - the previous optimization to grid selection rendering was always adding the lines at the extremes of previous selection, causing problems when scrolling up or down - make sure to only add lines in viewport * Disable mouse mode on exit * use saturating_sub for usize subtractions * copy selection to clipboard on mouse release * disable mouse on exit after error * remove left-over comments * remove copy keybinding * add default impl for selection methods in Pane - remove the useless methods from Impl Pane in PluginPane * move line diff between selections to selection * add option to disable mouse mode * Allow scrolling with mouse while selecting. * move action repeater to os_input_output, change timeout to 10ms - change repeater to take an action instead of a position with hardcoded action * replace mouse mode integration tests with e2e tests * cleanup * cleanup * check if mouse mode is disabled from merged options * fix missing changes in tests, cleanup
276 lines
9.4 KiB
Rust
276 lines
9.4 KiB
Rust
use zellij_utils::input::actions::Action;
|
|
use zellij_utils::{interprocess, libc, nix, signal_hook, termion, zellij_tile};
|
|
|
|
use interprocess::local_socket::LocalSocketStream;
|
|
use mio::{unix::SourceFd, Events, Interest, Poll, Token};
|
|
use nix::pty::Winsize;
|
|
use nix::sys::termios;
|
|
use signal_hook::{consts::signal::*, iterator::Signals};
|
|
use std::io::prelude::*;
|
|
use std::os::unix::io::RawFd;
|
|
use std::path::Path;
|
|
use std::sync::{Arc, Mutex};
|
|
use std::{io, time};
|
|
use zellij_tile::data::Palette;
|
|
use zellij_utils::{
|
|
errors::ErrorContext,
|
|
ipc::{ClientToServerMsg, IpcReceiverWithContext, IpcSenderWithContext, ServerToClientMsg},
|
|
pane_size::PositionAndSize,
|
|
shared::default_palette,
|
|
};
|
|
|
|
fn into_raw_mode(pid: RawFd) {
|
|
let mut tio = termios::tcgetattr(pid).expect("could not get terminal attribute");
|
|
termios::cfmakeraw(&mut tio);
|
|
match termios::tcsetattr(pid, termios::SetArg::TCSANOW, &tio) {
|
|
Ok(_) => {}
|
|
Err(e) => panic!("error {:?}", e),
|
|
};
|
|
}
|
|
|
|
fn unset_raw_mode(pid: RawFd, orig_termios: termios::Termios) {
|
|
match termios::tcsetattr(pid, termios::SetArg::TCSANOW, &orig_termios) {
|
|
Ok(_) => {}
|
|
Err(e) => panic!("error {:?}", e),
|
|
};
|
|
}
|
|
|
|
pub(crate) fn get_terminal_size_using_fd(fd: RawFd) -> PositionAndSize {
|
|
// TODO: do this with the nix ioctl
|
|
use libc::ioctl;
|
|
use libc::TIOCGWINSZ;
|
|
|
|
let mut winsize = Winsize {
|
|
ws_row: 0,
|
|
ws_col: 0,
|
|
ws_xpixel: 0,
|
|
ws_ypixel: 0,
|
|
};
|
|
|
|
// TIOCGWINSZ is an u32, but the second argument to ioctl is u64 on
|
|
// some platforms. When checked on Linux, clippy will complain about
|
|
// useless conversion.
|
|
#[allow(clippy::useless_conversion)]
|
|
unsafe {
|
|
ioctl(fd, TIOCGWINSZ.into(), &mut winsize)
|
|
};
|
|
PositionAndSize::from(winsize)
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct ClientOsInputOutput {
|
|
orig_termios: Arc<Mutex<termios::Termios>>,
|
|
send_instructions_to_server: Arc<Mutex<Option<IpcSenderWithContext<ClientToServerMsg>>>>,
|
|
receive_instructions_from_server: Arc<Mutex<Option<IpcReceiverWithContext<ServerToClientMsg>>>>,
|
|
mouse_term: Arc<Mutex<Option<termion::input::MouseTerminal<std::io::Stdout>>>>,
|
|
}
|
|
|
|
/// The `ClientOsApi` trait represents an abstract interface to the features of an operating system that
|
|
/// Zellij client requires.
|
|
pub trait ClientOsApi: Send + Sync {
|
|
/// Returns the size of the terminal associated to file descriptor `fd`.
|
|
fn get_terminal_size_using_fd(&self, fd: RawFd) -> PositionAndSize;
|
|
/// Set the terminal associated to file descriptor `fd` to
|
|
/// [raw mode](https://en.wikipedia.org/wiki/Terminal_mode).
|
|
fn set_raw_mode(&mut self, fd: RawFd);
|
|
/// Set the terminal associated to file descriptor `fd` to
|
|
/// [cooked mode](https://en.wikipedia.org/wiki/Terminal_mode).
|
|
fn unset_raw_mode(&self, fd: RawFd);
|
|
/// Returns the writer that allows writing to standard output.
|
|
fn get_stdout_writer(&self) -> Box<dyn io::Write>;
|
|
/// Returns the raw contents of standard input.
|
|
fn read_from_stdin(&self) -> Vec<u8>;
|
|
/// Returns a [`Box`] pointer to this [`ClientOsApi`] struct.
|
|
fn box_clone(&self) -> Box<dyn ClientOsApi>;
|
|
/// Sends a message to the server.
|
|
fn send_to_server(&self, msg: ClientToServerMsg);
|
|
/// Receives a message on client-side IPC channel
|
|
// This should be called from the client-side router thread only.
|
|
fn recv_from_server(&self) -> (ServerToClientMsg, ErrorContext);
|
|
fn handle_signals(&self, sigwinch_cb: Box<dyn Fn()>, quit_cb: Box<dyn Fn()>);
|
|
/// Establish a connection with the server socket.
|
|
fn connect_to_server(&self, path: &Path);
|
|
fn load_palette(&self) -> Palette;
|
|
fn enable_mouse(&self);
|
|
fn disable_mouse(&self);
|
|
// Repeatedly send action, until stdin is readable again
|
|
fn start_action_repeater(&mut self, action: Action);
|
|
}
|
|
|
|
impl ClientOsApi for ClientOsInputOutput {
|
|
fn get_terminal_size_using_fd(&self, fd: RawFd) -> PositionAndSize {
|
|
get_terminal_size_using_fd(fd)
|
|
}
|
|
fn set_raw_mode(&mut self, fd: RawFd) {
|
|
into_raw_mode(fd);
|
|
}
|
|
fn unset_raw_mode(&self, fd: RawFd) {
|
|
let orig_termios = self.orig_termios.lock().unwrap();
|
|
unset_raw_mode(fd, orig_termios.clone());
|
|
}
|
|
fn box_clone(&self) -> Box<dyn ClientOsApi> {
|
|
Box::new((*self).clone())
|
|
}
|
|
fn read_from_stdin(&self) -> Vec<u8> {
|
|
let stdin = std::io::stdin();
|
|
let mut stdin = stdin.lock();
|
|
let buffer = stdin.fill_buf().unwrap();
|
|
let length = buffer.len();
|
|
let read_bytes = Vec::from(buffer);
|
|
stdin.consume(length);
|
|
read_bytes
|
|
}
|
|
fn get_stdout_writer(&self) -> Box<dyn io::Write> {
|
|
let stdout = ::std::io::stdout();
|
|
Box::new(stdout)
|
|
}
|
|
fn send_to_server(&self, msg: ClientToServerMsg) {
|
|
self.send_instructions_to_server
|
|
.lock()
|
|
.unwrap()
|
|
.as_mut()
|
|
.unwrap()
|
|
.send(msg);
|
|
}
|
|
fn recv_from_server(&self) -> (ServerToClientMsg, ErrorContext) {
|
|
self.receive_instructions_from_server
|
|
.lock()
|
|
.unwrap()
|
|
.as_mut()
|
|
.unwrap()
|
|
.recv()
|
|
}
|
|
fn handle_signals(&self, sigwinch_cb: Box<dyn Fn()>, quit_cb: Box<dyn Fn()>) {
|
|
let mut signals = Signals::new(&[SIGWINCH, SIGTERM, SIGINT, SIGQUIT, SIGHUP]).unwrap();
|
|
for signal in signals.forever() {
|
|
match signal {
|
|
SIGWINCH => {
|
|
sigwinch_cb();
|
|
}
|
|
SIGTERM | SIGINT | SIGQUIT | SIGHUP => {
|
|
quit_cb();
|
|
break;
|
|
}
|
|
_ => unreachable!(),
|
|
}
|
|
}
|
|
}
|
|
fn connect_to_server(&self, path: &Path) {
|
|
let socket;
|
|
loop {
|
|
match LocalSocketStream::connect(path) {
|
|
Ok(sock) => {
|
|
socket = sock;
|
|
break;
|
|
}
|
|
Err(_) => {
|
|
std::thread::sleep(std::time::Duration::from_millis(50));
|
|
}
|
|
}
|
|
}
|
|
let sender = IpcSenderWithContext::new(socket);
|
|
let receiver = sender.get_receiver();
|
|
*self.send_instructions_to_server.lock().unwrap() = Some(sender);
|
|
*self.receive_instructions_from_server.lock().unwrap() = Some(receiver);
|
|
}
|
|
fn load_palette(&self) -> Palette {
|
|
// this was removed because termbg doesn't release stdin in certain scenarios (we know of
|
|
// windows terminal and FreeBSD): https://github.com/zellij-org/zellij/issues/538
|
|
//
|
|
// let palette = default_palette();
|
|
// let timeout = std::time::Duration::from_millis(100);
|
|
// if let Ok(rgb) = termbg::rgb(timeout) {
|
|
// palette.bg = PaletteColor::Rgb((rgb.r as u8, rgb.g as u8, rgb.b as u8));
|
|
// // TODO: also dynamically get all other colors from the user's terminal
|
|
// // this should be done in the same method (OSC ]11), but there might be other
|
|
// // considerations here, hence using the library
|
|
// };
|
|
default_palette()
|
|
}
|
|
fn enable_mouse(&self) {
|
|
let mut mouse_term = self.mouse_term.lock().unwrap();
|
|
if mouse_term.is_none() {
|
|
*mouse_term = Some(termion::input::MouseTerminal::from(std::io::stdout()));
|
|
}
|
|
}
|
|
|
|
fn disable_mouse(&self) {
|
|
let mut mouse_term = self.mouse_term.lock().unwrap();
|
|
if mouse_term.is_some() {
|
|
*mouse_term = None;
|
|
}
|
|
}
|
|
|
|
fn start_action_repeater(&mut self, action: Action) {
|
|
let mut poller = StdinPoller::default();
|
|
|
|
loop {
|
|
let ready = poller.ready();
|
|
if ready {
|
|
break;
|
|
}
|
|
self.send_to_server(ClientToServerMsg::Action(action.clone()));
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Clone for Box<dyn ClientOsApi> {
|
|
fn clone(&self) -> Box<dyn ClientOsApi> {
|
|
self.box_clone()
|
|
}
|
|
}
|
|
|
|
pub fn get_client_os_input() -> Result<ClientOsInputOutput, nix::Error> {
|
|
let current_termios = termios::tcgetattr(0)?;
|
|
let orig_termios = Arc::new(Mutex::new(current_termios));
|
|
let mouse_term = Arc::new(Mutex::new(None));
|
|
Ok(ClientOsInputOutput {
|
|
orig_termios,
|
|
send_instructions_to_server: Arc::new(Mutex::new(None)),
|
|
receive_instructions_from_server: Arc::new(Mutex::new(None)),
|
|
mouse_term,
|
|
})
|
|
}
|
|
|
|
pub const DEFAULT_STDIN_POLL_TIMEOUT_MS: u64 = 10;
|
|
|
|
struct StdinPoller {
|
|
poll: Poll,
|
|
events: Events,
|
|
timeout: time::Duration,
|
|
}
|
|
|
|
impl StdinPoller {
|
|
// use mio poll to check if stdin is readable without blocking
|
|
fn ready(&mut self) -> bool {
|
|
self.poll
|
|
.poll(&mut self.events, Some(self.timeout))
|
|
.expect("could not poll stdin for readiness");
|
|
for event in &self.events {
|
|
if event.token() == Token(0) && event.is_readable() {
|
|
return true;
|
|
}
|
|
}
|
|
false
|
|
}
|
|
}
|
|
|
|
impl Default for StdinPoller {
|
|
fn default() -> Self {
|
|
let stdin = 0;
|
|
let mut stdin_fd = SourceFd(&stdin);
|
|
let events = Events::with_capacity(128);
|
|
let poll = Poll::new().unwrap();
|
|
poll.registry()
|
|
.register(&mut stdin_fd, Token(0), Interest::READABLE)
|
|
.expect("could not create stdin poll");
|
|
|
|
let timeout = time::Duration::from_millis(DEFAULT_STDIN_POLL_TIMEOUT_MS);
|
|
|
|
Self {
|
|
poll,
|
|
events,
|
|
timeout,
|
|
}
|
|
}
|
|
}
|