zellij/zellij-utils/src/ipc.rs
Brooks Rady 76a5bc8a05
feat(ui): overhauled resize and layout systems
* refactor(panes): move to parametric pane sizes

* Fixed the simpler errors by casting to usize

* The least I can do is pass the formatting check...

* Move to stable toolchain

* Well, it compiles?

* And now it doesn't! ;)

* Baseline functionality with the new Dimension type

* Working POC for percent-based resizing

* REVERT THIS COMMIT – DELETES TESTS

* Perfected the discrete resize algorithm

* Fixed fixed-size panes

* Basic bidirectional resize

* feat(resize): finalised parametric resize algorithm

* Reduce the logging level a bit

* Fixed nested layouts using percents

* Bug squishing for implicit sizing

* Here is a funky (read: rubbish) rounding approach

* And now it's gone again!

* Improve discretisation algorithm to fix rounding errors

* Fix the last layout bug (maybe?)

* Mixed explicit and implied percents work now

* Let's pretend that didn't happen...

* Make things a bit less crashy

* Crash slightly more for now (to find bugs)

* Manaually splitting of panes works now

* Start moving to percent-based resizes

* Everything but fullscreen seems to be working

* Fix compilatation errors

* Culled a massive amount of border code

* Why not pause to please rustfmt?

* Turns out I was still missing a few tests...

* Bringing back even more tests!

* Fix tests and pane boarders

* Fix the resize system without gaps

* Fix content offset

* Fixed a bug with pane closing

* Add a hack to fix setting of the viewport

* Fix toggling between shared borders and frames

* fix(tests): make e2e properly use PaneGeom

* style(fmt): make rustfmt happy

* Revert unintentional rounding of borders

* Purge some old borderless stuff

* Fix busted tab-bar shrinking

* Update E2E tests

* Finish implementing fullscreen!

* Don't crash anymore?

* Fix (almost) all tests

* Fix a lack of tab-stops

* All tests passing

* I really can't be bothered to debug a CI issue

* Tie up loose ends

* Knock out some lingering FIXMEs

* Continue to clean things up

* Change some naming and address FIXMEs

* Cull more code + FIXMEs

* Refactor of the resize system + polish

* Only draw frames when absolutely necessary

* Fix the tab-bar crash

* Fix rendering of boarders on reattach

* Fix resizing at small pane sizes

* Deduplicate code in the layout system

* Update tab-bar WASM

* Fixed the pinching of panes during resize

* Unexpose needlessly public type

* Add back a lost test

* Re-add tab tests and get them to compile

* All tabs need layouts

* Start fixing tests + bug in main

* Stabilize the resize algorithm rounding

* All tests from main are now passing

* Cull more dead code
2021-08-28 17:46:24 +01:00

168 lines
5.2 KiB
Rust

//! IPC stuff for starting to split things into a client and server model.
use crate::{
cli::CliArgs,
errors::{get_current_ctx, ErrorContext},
input::{actions::Action, layout::LayoutFromYaml, options::Options},
pane_size::Size,
};
use interprocess::local_socket::LocalSocketStream;
use nix::unistd::dup;
use serde::{Deserialize, Serialize};
use std::{
fmt::{Display, Error, Formatter},
io::{self, Write},
marker::PhantomData,
os::unix::io::{AsRawFd, FromRawFd},
};
use zellij_tile::data::Palette;
type SessionId = u64;
#[derive(PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct Session {
// Unique ID for this session
id: SessionId,
// Identifier for the underlying IPC primitive (socket, pipe)
conn_name: String,
// User configured alias for the session
alias: String,
}
// How do we want to connect to a session?
#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ClientType {
Reader,
Writer,
}
#[derive(Default, Serialize, Deserialize, Debug, Clone, Copy)]
pub struct ClientAttributes {
pub size: Size,
pub palette: Palette,
}
// Types of messages sent from the client to the server
#[allow(clippy::large_enum_variant)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum ClientToServerMsg {
/*// List which sessions are available
ListSessions,
// Create a new session
CreateSession,
// Attach to a running session
AttachToSession(SessionId, ClientType),
// Force detach
DetachSession(SessionId),
// Disconnect from the session we're connected to
DisconnectFromSession,*/
TerminalResize(Size),
NewClient(ClientAttributes, Box<CliArgs>, Box<Options>, LayoutFromYaml),
AttachClient(ClientAttributes, bool, Options),
Action(Action),
ClientExited,
}
// Types of messages sent from the server to the client
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum ServerToClientMsg {
/*// Info about a particular session
SessionInfo(Session),
// A list of sessions
SessionList(HashSet<Session>),*/
Render(String),
UnblockInputThread,
Exit(ExitReason),
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum ExitReason {
Normal,
ForceDetached,
CannotAttach,
Error(String),
}
impl Display for ExitReason {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
match self {
Self::Normal => write!(f, "Bye from Zellij!"),
Self::ForceDetached => write!(
f,
"Session was detach from this client (possibly because another client connected)"
),
Self::CannotAttach => write!(
f,
"Session attached to another client. Use --force flag to force connect."
),
Self::Error(e) => write!(f, "Error occured in server:\n{}", e),
}
}
}
/// Sends messages on a stream socket, along with an [`ErrorContext`].
pub struct IpcSenderWithContext<T: Serialize> {
sender: io::BufWriter<LocalSocketStream>,
_phantom: PhantomData<T>,
}
impl<T: Serialize> IpcSenderWithContext<T> {
/// Returns a sender to the given [LocalSocketStream](interprocess::local_socket::LocalSocketStream).
pub fn new(sender: LocalSocketStream) -> Self {
Self {
sender: io::BufWriter::new(sender),
_phantom: PhantomData,
}
}
/// Sends an event, along with the current [`ErrorContext`], on this [`IpcSenderWithContext`]'s socket.
pub fn send(&mut self, msg: T) {
let err_ctx = get_current_ctx();
bincode::serialize_into(&mut self.sender, &(msg, err_ctx)).unwrap();
self.sender.flush().unwrap();
}
/// Returns an [`IpcReceiverWithContext`] with the same socket as this sender.
pub fn get_receiver<F>(&self) -> IpcReceiverWithContext<F>
where
F: for<'de> Deserialize<'de> + Serialize,
{
let sock_fd = self.sender.get_ref().as_raw_fd();
let dup_sock = dup(sock_fd).unwrap();
let socket = unsafe { LocalSocketStream::from_raw_fd(dup_sock) };
IpcReceiverWithContext::new(socket)
}
}
/// Receives messages on a stream socket, along with an [`ErrorContext`].
pub struct IpcReceiverWithContext<T> {
receiver: io::BufReader<LocalSocketStream>,
_phantom: PhantomData<T>,
}
impl<T> IpcReceiverWithContext<T>
where
T: for<'de> Deserialize<'de> + Serialize,
{
/// Returns a receiver to the given [LocalSocketStream](interprocess::local_socket::LocalSocketStream).
pub fn new(receiver: LocalSocketStream) -> Self {
Self {
receiver: io::BufReader::new(receiver),
_phantom: PhantomData,
}
}
/// Receives an event, along with the current [`ErrorContext`], on this [`IpcReceiverWithContext`]'s socket.
pub fn recv(&mut self) -> (T, ErrorContext) {
bincode::deserialize_from(&mut self.receiver).unwrap()
}
/// Returns an [`IpcSenderWithContext`] with the same socket as this receiver.
pub fn get_sender<F: Serialize>(&self) -> IpcSenderWithContext<F> {
let sock_fd = self.receiver.get_ref().as_raw_fd();
let dup_sock = dup(sock_fd).unwrap();
let socket = unsafe { LocalSocketStream::from_raw_fd(dup_sock) };
IpcSenderWithContext::new(socket)
}
}