Skip to content

Commit

Permalink
refactor(clients): support multiple clients in tab/screen rendering i…
Browse files Browse the repository at this point in the history
…nfra (#770)
  • Loading branch information
imsnif authored Oct 7, 2021
1 parent 24154e4 commit f2401d0
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 151 deletions.
62 changes: 49 additions & 13 deletions zellij-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use crate::{
os_input_output::ServerOsApi,
pty::{pty_thread_main, Pty, PtyInstruction},
screen::{screen_thread_main, ScreenInstruction},
tab::Output,
thread_bus::{Bus, ThreadSenders},
wasm_vm::{wasm_thread_main, PluginInstruction},
};
Expand Down Expand Up @@ -59,7 +60,7 @@ pub(crate) enum ServerInstruction {
ClientId,
Option<PluginsConfig>,
),
Render(Option<String>),
Render(Option<Output>),
UnblockInputThread,
ClientExit(ClientId),
RemoveClient(ClientId),
Expand Down Expand Up @@ -261,7 +262,6 @@ pub fn start_server(mut os_input: Box<dyn ServerOsApi>, socket_path: PathBuf) {
os_input.clone(),
to_server.clone(),
client_attributes,
session_state.clone(),
SessionOptions {
opts,
layout: layout.clone(),
Expand Down Expand Up @@ -289,7 +289,11 @@ pub fn start_server(mut os_input: Box<dyn ServerOsApi>, socket_path: PathBuf) {
.as_ref()
.unwrap()
.senders
.send_to_pty(PtyInstruction::NewTab(default_shell.clone(), tab_layout))
.send_to_pty(PtyInstruction::NewTab(
default_shell.clone(),
tab_layout,
client_id,
))
.unwrap()
};

Expand Down Expand Up @@ -317,6 +321,10 @@ pub fn start_server(mut os_input: Box<dyn ServerOsApi>, socket_path: PathBuf) {
.senders
.send_to_screen(ScreenInstruction::TerminalResize(min_size))
.unwrap();
session_data
.senders
.send_to_screen(ScreenInstruction::AddClient(client_id))
.unwrap();
let default_mode = options.default_mode.unwrap_or_default();
let mode_info =
get_mode_info(default_mode, attrs.palette, session_data.capabilities);
Expand Down Expand Up @@ -353,6 +361,16 @@ pub fn start_server(mut os_input: Box<dyn ServerOsApi>, socket_path: PathBuf) {
.senders
.send_to_screen(ScreenInstruction::TerminalResize(min_size))
.unwrap();
// we only do this inside this if because it means there are still connected
// clients
session_data
.write()
.unwrap()
.as_ref()
.unwrap()
.senders
.send_to_screen(ScreenInstruction::RemoveClient(client_id))
.unwrap();
}
if session_state.read().unwrap().clients.is_empty() {
*session_data.write().unwrap() = None;
Expand All @@ -370,6 +388,16 @@ pub fn start_server(mut os_input: Box<dyn ServerOsApi>, socket_path: PathBuf) {
.senders
.send_to_screen(ScreenInstruction::TerminalResize(min_size))
.unwrap();
// we only do this inside this if because it means there are still connected
// clients
session_data
.write()
.unwrap()
.as_ref()
.unwrap()
.senders
.send_to_screen(ScreenInstruction::RemoveClient(client_id))
.unwrap();
}
}
ServerInstruction::DetachSession(client_id) => {
Expand All @@ -384,6 +412,16 @@ pub fn start_server(mut os_input: Box<dyn ServerOsApi>, socket_path: PathBuf) {
.senders
.send_to_screen(ScreenInstruction::TerminalResize(min_size))
.unwrap();
// we only do this inside this if because it means there are still connected
// clients
session_data
.write()
.unwrap()
.as_ref()
.unwrap()
.senders
.send_to_screen(ScreenInstruction::RemoveClient(client_id))
.unwrap();
}
}
ServerInstruction::Render(mut output) => {
Expand All @@ -392,8 +430,13 @@ pub fn start_server(mut os_input: Box<dyn ServerOsApi>, socket_path: PathBuf) {
// If `Some(_)`- unwrap it and forward it to the clients to render.
// If `None`- Send an exit instruction. This is the case when a user closes the last Tab/Pane.
if let Some(op) = output.as_mut() {
for client_id in client_ids {
os_input.send_to_client(client_id, ServerToClientMsg::Render(op.clone()));
for (client_id, client_render_instruction) in
op.client_render_instructions.iter_mut()
{
os_input.send_to_client(
*client_id,
ServerToClientMsg::Render(client_render_instruction.clone()),
);
}
} else {
for client_id in client_ids {
Expand Down Expand Up @@ -440,7 +483,6 @@ fn init_session(
os_input: Box<dyn ServerOsApi>,
to_server: SenderWithContext<ServerInstruction>,
client_attributes: ClientAttributes,
session_state: Arc<RwLock<SessionState>>,
options: SessionOptions,
) -> SessionMetaData {
let SessionOptions {
Expand Down Expand Up @@ -508,13 +550,7 @@ fn init_session(
let max_panes = opts.max_panes;

move || {
screen_thread_main(
screen_bus,
max_panes,
client_attributes,
config_options,
session_state,
);
screen_thread_main(screen_bus, max_panes, client_attributes, config_options);
}
})
.unwrap();
Expand Down
12 changes: 7 additions & 5 deletions zellij-server/src/pty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
screen::ScreenInstruction,
thread_bus::{Bus, ThreadSenders},
wasm_vm::PluginInstruction,
ServerInstruction,
ClientId, ServerInstruction,
};
use async_std::{
future::timeout as async_timeout,
Expand Down Expand Up @@ -36,7 +36,7 @@ pub(crate) enum PtyInstruction {
SpawnTerminalVertically(Option<TerminalAction>),
SpawnTerminalHorizontally(Option<TerminalAction>),
UpdateActivePane(Option<PaneId>),
NewTab(Option<TerminalAction>, Option<TabLayout>),
NewTab(Option<TerminalAction>, Option<TabLayout>, ClientId),
ClosePane(PaneId),
CloseTab(Vec<PaneId>),
Exit,
Expand Down Expand Up @@ -96,7 +96,7 @@ pub(crate) fn pty_thread_main(mut pty: Pty, layout: LayoutFromYaml) {
PtyInstruction::UpdateActivePane(pane_id) => {
pty.set_active_pane(pane_id);
}
PtyInstruction::NewTab(terminal_action, tab_layout) => {
PtyInstruction::NewTab(terminal_action, tab_layout, client_id) => {
let tab_name = tab_layout.as_ref().and_then(|layout| {
if layout.name.is_empty() {
None
Expand All @@ -109,7 +109,7 @@ pub(crate) fn pty_thread_main(mut pty: Pty, layout: LayoutFromYaml) {
let layout: Layout =
Layout::try_from(merged_layout).unwrap_or_else(|err| panic!("{}", err));

pty.spawn_terminals_for_layout(layout, terminal_action.clone());
pty.spawn_terminals_for_layout(layout, terminal_action.clone(), client_id);

if let Some(tab_name) = tab_name {
// clear current name at first
Expand Down Expand Up @@ -284,6 +284,7 @@ impl Pty {
&mut self,
layout: Layout,
default_shell: Option<TerminalAction>,
client_id: ClientId,
) {
let default_shell = default_shell.unwrap_or_else(|| self.get_default_terminal());
let extracted_run_instructions = layout.extract_run_instructions();
Expand Down Expand Up @@ -313,9 +314,10 @@ impl Pty {
}
self.bus
.senders
.send_to_screen(ScreenInstruction::ApplyLayout(
.send_to_screen(ScreenInstruction::NewTab(
layout,
new_pane_pids.clone(),
client_id,
))
.unwrap();
for id in new_pane_pids {
Expand Down
2 changes: 1 addition & 1 deletion zellij-server/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ fn route_action(
let shell = session.default_shell.clone();
session
.senders
.send_to_pty(PtyInstruction::NewTab(shell, tab_layout))
.send_to_pty(PtyInstruction::NewTab(shell, tab_layout, client_id))
.unwrap();
}
Action::GoToNextTab => {
Expand Down
Loading

0 comments on commit f2401d0

Please sign in to comment.