-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathmain.rs
More file actions
322 lines (296 loc) · 12.1 KB
/
main.rs
File metadata and controls
322 lines (296 loc) · 12.1 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
mod config_watching;
mod iced;
mod minimize;
mod notifications;
mod space;
mod space_container;
mod workspaces_dbus;
mod xdg_shell_wrapper;
use crate::xdg_shell_wrapper::{
client_state::ClientState, run, server_state::ServerState, shared_state::GlobalState,
};
use anyhow::Result;
use calloop::channel::Sender;
use cctk::{
wayland_client::protocol::wl_output::WlOutput,
wayland_protocols::ext::foreign_toplevel_list::v1::client::ext_foreign_toplevel_handle_v1,
};
use config_watching::{watch_config, watch_cosmic_theme};
use cosmic_panel_config::CosmicPanelConfig;
use launch_pad::{ProcessKey, ProcessManager};
use minimize::MinimizeApplet;
use notifications::notifications_conn;
use smithay::reexports::{calloop, wayland_server::backend::ClientId};
use std::{
collections::HashMap,
mem,
os::fd::{AsRawFd, OwnedFd},
time::Duration,
};
use tokio::{runtime, sync::mpsc};
use tracing::{error, info, warn};
use tracing_subscriber::{EnvFilter, fmt, prelude::*};
#[derive(Debug)]
pub enum PanelCalloopMsg {
ClientSocketPair(ClientId),
RestartSpace(CosmicPanelConfig, WlOutput),
MinimizeRect { output: String, applet_info: MinimizeApplet },
UpdateToplevel(ext_foreign_toplevel_handle_v1::ExtForeignToplevelHandleV1),
}
/// Access glibc malloc tunables.
#[cfg(target_env = "gnu")]
mod malloc {
use std::os::raw::c_int;
const M_MMAP_THRESHOLD: c_int = -3;
unsafe extern "C" {
fn malloc_trim(pad: usize);
fn mallopt(param: c_int, value: c_int) -> c_int;
}
/// Prevents glibc from hoarding memory via memory fragmentation.
pub fn limit_mmap_threshold() {
unsafe {
mallopt(M_MMAP_THRESHOLD, 65536);
}
}
/// Asks glibc to trim malloc arenas.
pub fn trim() {
unsafe {
malloc_trim(0);
}
}
}
fn main() -> Result<()> {
// Prevents glibc from hoarding memory via memory fragmentation.
#[cfg(target_env = "gnu")]
malloc::limit_mmap_threshold();
let fmt_layer = fmt::layer().with_target(false);
let filter_layer = EnvFilter::try_from_default_env().unwrap_or_else(|_| {
if cfg!(debug_assertions) {
EnvFilter::new(format!("warn,{}=debug", env!("CARGO_CRATE_NAME")))
} else {
EnvFilter::new("warn")
}
});
if let Ok(journal_layer) = tracing_journald::layer() {
tracing_subscriber::registry().with(journal_layer).with(filter_layer).init();
} else {
tracing_subscriber::registry().with(fmt_layer).with(filter_layer).init();
}
log_panics::init();
let arg = std::env::args().nth(1);
let usage = "USAGE: cosmic-panel";
let config = match arg.as_ref().map(|s| &s[..]) {
Some(arg) if arg == "--help" || arg == "-h" => {
println!("{}", usage);
std::process::exit(1);
},
None => match cosmic_panel_config::CosmicPanelContainerConfig::load() {
Ok(c) => c,
Err((errors, c)) => {
for e in errors {
error!("Panel Entry Error: {:?}", e);
}
let _ = c.write_entries();
c
},
},
_ => {
println!("{}", usage);
std::process::exit(1);
},
};
let (applet_tx, mut applet_rx) = mpsc::channel(200);
let (calloop_tx, calloop_rx): (Sender<PanelCalloopMsg>, _) = calloop::channel::channel();
let event_loop = calloop::EventLoop::try_new()?;
let mut space = space_container::SpaceContainer::new(
config,
applet_tx.clone(),
calloop_tx.clone(),
event_loop.handle(),
);
let handle = event_loop.handle();
match watch_config(&space.config, handle) {
Ok(watchers) => {
info!("Watching panel config successful");
space.watchers = watchers;
},
Err(e) => warn!("Failed to watch config: {:?}", e),
};
match watch_cosmic_theme(event_loop.handle()) {
Ok(w) => mem::forget(w),
Err(e) => error!("Error while watching cosmic theme: {:?}", e),
};
event_loop
.handle()
.insert_source(
calloop::timer::Timer::from_duration(Duration::from_secs(1)),
|_, _, state: &mut GlobalState| {
tracing::trace!("Updating hidden applets");
state.space.update_hidden_applet_frame();
calloop::timer::TimeoutAction::ToDuration(Duration::from_secs(1))
},
)
.expect("failed to insert hidden applet frame timer");
#[cfg(target_env = "gnu")]
event_loop
.handle()
.insert_source(
calloop::timer::Timer::from_duration(Duration::from_secs(600)),
|_, _, _: &mut GlobalState| {
tracing::trace!("Releasing free memory from the heap.");
malloc::trim();
calloop::timer::TimeoutAction::ToDuration(Duration::from_secs(600))
},
)
.expect("failed to insert malloc trim timer");
event_loop
.handle()
.insert_source(calloop_rx, move |e, _, state: &mut GlobalState| {
tracing::trace!("Panel Event: {e:?}");
match e {
calloop::channel::Event::Msg(e) => match e {
PanelCalloopMsg::ClientSocketPair(client_id) => {
state.space.cleanup_client(client_id);
},
PanelCalloopMsg::RestartSpace(config, o) => {
state.space.update_space(
config,
&state.client_state.compositor_state,
state.client_state.fractional_scaling_manager.as_ref(),
state.client_state.viewporter_state.as_ref(),
&mut state.client_state.layer_state,
&state.client_state.queue_handle,
Some(o),
state.client_state.overlap_notify.clone(),
);
},
PanelCalloopMsg::UpdateToplevel(toplevel) => {
minimize::update_toplevel(state, toplevel)
},
PanelCalloopMsg::MinimizeRect { output, applet_info } => {
minimize::set_rectangles(state, output, applet_info)
},
},
calloop::channel::Event::Closed => {},
};
})
.expect("failed to insert dbus event source");
std::thread::spawn(move || -> anyhow::Result<()> {
let rt = runtime::Builder::new_current_thread().enable_all().build()?;
let mut process_ids: HashMap<String, Vec<ProcessKey>> = HashMap::new();
rt.block_on(async move {
let process_manager = ProcessManager::new().await;
let _ = process_manager
.set_restart_mode(launch_pad::RestartMode::ExponentialBackoff(
Duration::from_millis(2),
))
.await;
let _ = process_manager.set_max_restarts(999999).await;
let mut notifications_proxy =
match tokio::time::timeout(Duration::from_secs(5), notifications_conn()).await {
Ok(Ok(p)) => Some(p),
_ => {
error!("Failed to connect to the notifications daemon.");
None
},
};
while let Some(msg) = applet_rx.recv().await {
tracing::trace!("Applet Message: {msg:?}");
match msg {
space::AppletMsg::NewProcess(id, process) => {
if let Ok(key) = process_manager.start(process).await {
let entry = process_ids.entry(id).or_default();
entry.push(key);
}
},
space::AppletMsg::NewNotificationsProcess(
id,
mut process,
mut env,
mut fds,
) => {
let Some(proxy) = notifications_proxy.as_mut() else {
notifications_proxy = match tokio::time::timeout(
Duration::from_secs(1),
notifications_conn(),
)
.await
{
Ok(Ok(p)) => Some(p),
_ => {
error!("Failed to connect to the notifications daemon",);
None
},
};
warn!("Can't start notifications applet without a connection");
continue;
};
info!("Getting fd for notifications applet");
let notif_fd = match tokio::time::timeout(
Duration::from_secs(1),
proxy.get_fd(),
)
.await
{
Ok(Ok(fd)) => fd,
Ok(Err(err)) => {
error!("Failed to get fd for the notifications applet {}", err);
continue;
},
Err(err) => {
error!("Failed to get fd for the notifications applet {}", err);
continue;
},
};
let notif_fd = OwnedFd::from(notif_fd);
env.push((
"COSMIC_NOTIFICATIONS".to_string(),
notif_fd.as_raw_fd().to_string(),
));
fds.push(notif_fd);
process = process.with_fds(move || fds);
process = process.with_env(env);
info!("Starting notifications applet");
if let Ok(key) = process_manager.start(process).await {
let entry = process_ids.entry(id).or_default();
entry.push(key);
}
},
space::AppletMsg::ClientSocketPair(client_id) => {
let _ = calloop_tx.send(PanelCalloopMsg::ClientSocketPair(client_id));
},
space::AppletMsg::Cleanup(id) => {
for id in process_ids.remove(&id).unwrap_or_default() {
let _ = process_manager.stop_process(id).await;
}
},
space::AppletMsg::NeedNewNotificationFd(sender) => {
let Some(proxy) = notifications_proxy.as_mut() else {
warn!("Can't start notifications applet without a connection");
continue;
};
let fd = match proxy.get_fd().await {
Ok(fd) => fd,
Err(err) => {
error!("Failed to get fd for the notifications applet {}", err);
continue;
},
};
let fd = OwnedFd::from(fd);
_ = sender.send(fd);
},
};
}
});
Ok(())
});
let server_display = smithay::reexports::wayland_server::Display::new().unwrap();
let s_dh = server_display.handle();
let mut server_state = ServerState::new(s_dh.clone());
let mut client_state = ClientState::new(event_loop.handle(), &mut space, &mut server_state)?;
client_state.init_workspace_state();
client_state.init_toplevel_info_state();
client_state.init_toplevel_manager_state();
run(space, client_state, server_state, event_loop, server_display)?;
Ok(())
}