1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
use crate::cli::CliArgs;
use crate::command_is_executing::CommandIsExecuting;
use crate::common::{
ChannelWithContext, ClientInstruction, IpcSenderWithContext, SenderType, SenderWithContext,
ServerInstruction,
};
use crate::errors::{ContextType, ErrorContext, PtyContext};
use crate::os_input_output::OsApi;
use crate::panes::PaneId;
use crate::pty_bus::{PtyBus, PtyInstruction};
use crate::screen::ScreenInstruction;
use crate::utils::consts::ZELLIJ_IPC_PIPE;
use ipmpsc::{Receiver as IpcReceiver, SharedRingBuffer};
use std::path::PathBuf;
use std::sync::mpsc::channel;
use std::thread;
pub fn start_server(
os_input: Box<dyn OsApi>,
opts: CliArgs,
command_is_executing: CommandIsExecuting,
) -> thread::JoinHandle<()> {
let (send_pty_instructions, receive_pty_instructions): ChannelWithContext<PtyInstruction> =
channel();
let mut send_pty_instructions = SenderWithContext::new(
ErrorContext::new(),
SenderType::Sender(send_pty_instructions),
);
let server_buffer = SharedRingBuffer::create(ZELLIJ_IPC_PIPE, 8192).unwrap();
// Don't use default layouts in tests, but do everywhere else
#[cfg(not(test))]
let default_layout = Some(PathBuf::from("default"));
#[cfg(test)]
let default_layout = None;
let maybe_layout = opts.layout.or(default_layout);
let send_server_instructions = IpcSenderWithContext::new(server_buffer.clone());
let mut pty_bus = PtyBus::new(
receive_pty_instructions,
os_input.clone(),
send_server_instructions,
opts.debug,
);
let pty_thread = thread::Builder::new()
.name("pty".to_string())
.spawn({
let mut command_is_executing = command_is_executing.clone();
move || loop {
let (event, mut err_ctx) = pty_bus
.receive_pty_instructions
.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_server_instructions
.send(ServerInstruction::ToScreen(ScreenInstruction::NewPane(
PaneId::Terminal(pid),
)))
.unwrap();
}
PtyInstruction::SpawnTerminalVertically(file_to_open) => {
let pid = pty_bus.spawn_terminal(file_to_open);
pty_bus
.send_server_instructions
.send(ServerInstruction::ToScreen(
ScreenInstruction::VerticalSplit(PaneId::Terminal(pid)),
))
.unwrap();
}
PtyInstruction::SpawnTerminalHorizontally(file_to_open) => {
let pid = pty_bus.spawn_terminal(file_to_open);
pty_bus
.send_server_instructions
.send(ServerInstruction::ToScreen(
ScreenInstruction::HorizontalSplit(PaneId::Terminal(pid)),
))
.unwrap();
}
PtyInstruction::NewTab => {
if let Some(layout) = maybe_layout.clone() {
pty_bus.spawn_terminals_for_layout(layout);
} else {
let pid = pty_bus.spawn_terminal(None);
pty_bus
.send_server_instructions
.send(ServerInstruction::ToScreen(ScreenInstruction::NewTab(pid)))
.unwrap();
}
}
PtyInstruction::ClosePane(id) => {
pty_bus.close_pane(id);
command_is_executing.done_closing_pane();
}
PtyInstruction::CloseTab(ids) => {
pty_bus.close_tab(ids);
command_is_executing.done_closing_pane();
}
PtyInstruction::Exit => {
break;
}
}
}
})
.unwrap();
thread::Builder::new()
.name("ipc_server".to_string())
.spawn({
let recv_server_instructions = IpcReceiver::new(server_buffer);
// Fixme: We cannot use uninitialised sender, therefore this Vec.
// We make sure that the first message is `NewClient` so there are no out of bouns panics.
let mut send_client_instructions: Vec<IpcSenderWithContext> = Vec::with_capacity(1);
move || loop {
let (mut err_ctx, instruction): (ErrorContext, ServerInstruction) =
recv_server_instructions.recv().unwrap();
err_ctx.add_call(ContextType::IPCServer);
send_pty_instructions.update(err_ctx);
if send_client_instructions.len() == 1 {
send_client_instructions[0].update(err_ctx);
}
match instruction {
ServerInstruction::OpenFile(file_name) => {
let path = PathBuf::from(file_name);
send_pty_instructions
.send(PtyInstruction::SpawnTerminal(Some(path)))
.unwrap();
}
ServerInstruction::SplitHorizontally => {
send_pty_instructions
.send(PtyInstruction::SpawnTerminalHorizontally(None))
.unwrap();
}
ServerInstruction::SplitVertically => {
send_pty_instructions
.send(PtyInstruction::SpawnTerminalVertically(None))
.unwrap();
}
ServerInstruction::MoveFocus => {
send_client_instructions[0]
.send(ClientInstruction::ToScreen(ScreenInstruction::MoveFocus))
.unwrap();
}
ServerInstruction::NewClient(buffer_path) => {
send_pty_instructions.send(PtyInstruction::NewTab).unwrap();
send_client_instructions.push(IpcSenderWithContext::new(
SharedRingBuffer::open(&buffer_path).unwrap(),
));
}
ServerInstruction::ToPty(instr) => {
send_pty_instructions.send(instr).unwrap();
}
ServerInstruction::ToScreen(instr) => {
send_client_instructions[0]
.send(ClientInstruction::ToScreen(instr))
.unwrap();
}
ServerInstruction::ClosePluginPane(pid) => {
send_client_instructions[0]
.send(ClientInstruction::ClosePluginPane(pid))
.unwrap();
}
ServerInstruction::Exit => {
let _ = send_pty_instructions.send(PtyInstruction::Exit);
let _ = pty_thread.join();
let _ = send_client_instructions[0].send(ClientInstruction::Exit);
break;
}
}
}
})
.unwrap()
}
|