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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
use std::sync::{Arc, RwLock};
use zellij_tile::data::Event;
use crate::{
os_input_output::ServerOsApi, pty::PtyInstruction, screen::ScreenInstruction,
wasm_vm::PluginInstruction, ServerInstruction, SessionMetaData,
};
use zellij_utils::{
channels::SenderWithContext,
input::{
actions::{Action, Direction},
get_mode_info,
},
ipc::ClientToServerMsg,
};
fn route_action(action: Action, session: &SessionMetaData, os_input: &dyn ServerOsApi) {
match action {
Action::Write(val) => {
session
.senders
.send_to_screen(ScreenInstruction::ClearScroll)
.unwrap();
session
.senders
.send_to_screen(ScreenInstruction::WriteCharacter(val))
.unwrap();
}
Action::SwitchToMode(mode) => {
let palette = os_input.load_palette();
// TODO: use the palette from the client and remove it from the server os api
// this is left here as a stop gap measure until we shift some code around
// to allow for this
session
.senders
.send_to_plugin(PluginInstruction::Update(
None,
Event::ModeUpdate(get_mode_info(mode, palette, session.capabilities)),
))
.unwrap();
session
.senders
.send_to_screen(ScreenInstruction::ChangeMode(get_mode_info(
mode,
palette,
session.capabilities,
)))
.unwrap();
session
.senders
.send_to_screen(ScreenInstruction::Render)
.unwrap();
}
Action::Resize(direction) => {
let screen_instr = match direction {
Direction::Left => ScreenInstruction::ResizeLeft,
Direction::Right => ScreenInstruction::ResizeRight,
Direction::Up => ScreenInstruction::ResizeUp,
Direction::Down => ScreenInstruction::ResizeDown,
};
session.senders.send_to_screen(screen_instr).unwrap();
}
Action::SwitchFocus => {
session
.senders
.send_to_screen(ScreenInstruction::SwitchFocus)
.unwrap();
}
Action::FocusNextPane => {
session
.senders
.send_to_screen(ScreenInstruction::FocusNextPane)
.unwrap();
}
Action::FocusPreviousPane => {
session
.senders
.send_to_screen(ScreenInstruction::FocusPreviousPane)
.unwrap();
}
Action::MoveFocus(direction) => {
let screen_instr = match direction {
Direction::Left => ScreenInstruction::MoveFocusLeft,
Direction::Right => ScreenInstruction::MoveFocusRight,
Direction::Up => ScreenInstruction::MoveFocusUp,
Direction::Down => ScreenInstruction::MoveFocusDown,
};
session.senders.send_to_screen(screen_instr).unwrap();
}
Action::MoveFocusOrTab(direction) => {
let screen_instr = match direction {
Direction::Left => ScreenInstruction::MoveFocusLeftOrPreviousTab,
Direction::Right => ScreenInstruction::MoveFocusRightOrNextTab,
_ => unreachable!(),
};
session.senders.send_to_screen(screen_instr).unwrap();
}
Action::ScrollUp => {
session
.senders
.send_to_screen(ScreenInstruction::ScrollUp)
.unwrap();
}
Action::ScrollDown => {
session
.senders
.send_to_screen(ScreenInstruction::ScrollDown)
.unwrap();
}
Action::PageScrollUp => {
session
.senders
.send_to_screen(ScreenInstruction::PageScrollUp)
.unwrap();
}
Action::PageScrollDown => {
session
.senders
.send_to_screen(ScreenInstruction::PageScrollDown)
.unwrap();
}
Action::ToggleFocusFullscreen => {
session
.senders
.send_to_screen(ScreenInstruction::ToggleActiveTerminalFullscreen)
.unwrap();
}
Action::NewPane(direction) => {
let pty_instr = match direction {
Some(Direction::Left) => PtyInstruction::SpawnTerminalVertically(None),
Some(Direction::Right) => PtyInstruction::SpawnTerminalVertically(None),
Some(Direction::Up) => PtyInstruction::SpawnTerminalHorizontally(None),
Some(Direction::Down) => PtyInstruction::SpawnTerminalHorizontally(None),
// No direction specified - try to put it in the biggest available spot
None => PtyInstruction::SpawnTerminal(None),
};
session.senders.send_to_pty(pty_instr).unwrap();
}
Action::CloseFocus => {
session
.senders
.send_to_screen(ScreenInstruction::CloseFocusedPane)
.unwrap();
}
Action::NewTab => {
session.senders.send_to_pty(PtyInstruction::NewTab).unwrap();
}
Action::GoToNextTab => {
session
.senders
.send_to_screen(ScreenInstruction::SwitchTabNext)
.unwrap();
}
Action::GoToPreviousTab => {
session
.senders
.send_to_screen(ScreenInstruction::SwitchTabPrev)
.unwrap();
}
Action::ToggleActiveSyncTab => {
session
.senders
.send_to_screen(ScreenInstruction::ToggleActiveSyncTab)
.unwrap();
}
Action::CloseTab => {
session
.senders
.send_to_screen(ScreenInstruction::CloseTab)
.unwrap();
}
Action::GoToTab(i) => {
session
.senders
.send_to_screen(ScreenInstruction::GoToTab(i))
.unwrap();
}
Action::TabNameInput(c) => {
session
.senders
.send_to_screen(ScreenInstruction::UpdateTabName(c))
.unwrap();
}
Action::NoOp => {}
Action::Quit => panic!("Received unexpected action"),
}
}
pub(crate) fn route_thread_main(
sessions: Arc<RwLock<Option<SessionMetaData>>>,
mut os_input: Box<dyn ServerOsApi>,
to_server: SenderWithContext<ServerInstruction>,
) {
loop {
let (instruction, err_ctx) = os_input.recv_from_client();
err_ctx.update_thread_ctx();
let rlocked_sessions = sessions.read().unwrap();
match instruction {
ClientToServerMsg::ClientExit => {
to_server.send(instruction.into()).unwrap();
break;
}
ClientToServerMsg::Action(action) => {
if let Some(rlocked_sessions) = rlocked_sessions.as_ref() {
route_action(action, rlocked_sessions, &*os_input);
}
|