2021-12-15 17:25:15 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
|
|
2025-03-05 19:37:12 +01:00
|
|
|
use calloop::timer::{TimeoutAction, Timer};
|
2022-07-04 15:26:26 +02:00
|
|
|
use smithay::{
|
|
|
|
|
reexports::{
|
|
|
|
|
calloop::{generic::Generic, EventLoop, Interest, Mode, PostAction},
|
2023-09-29 21:33:16 +02:00
|
|
|
wayland_server::{Display, DisplayHandle},
|
2022-07-04 15:26:26 +02:00
|
|
|
},
|
|
|
|
|
wayland::socket::ListeningSocketSource,
|
2021-12-15 18:00:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use anyhow::{Context, Result};
|
2025-03-05 19:37:12 +01:00
|
|
|
use state::{LastRefresh, State};
|
|
|
|
|
use std::{
|
|
|
|
|
env,
|
|
|
|
|
ffi::OsString,
|
|
|
|
|
os::unix::process::CommandExt,
|
|
|
|
|
process,
|
|
|
|
|
sync::Arc,
|
|
|
|
|
time::{Duration, Instant},
|
|
|
|
|
};
|
2023-02-24 17:41:52 +01:00
|
|
|
use tracing::{error, info, warn};
|
2024-11-22 19:10:58 -05:00
|
|
|
use wayland::protocols::overlap_notify::OverlapNotifyState;
|
2021-12-15 18:00:28 +01:00
|
|
|
|
2024-06-07 20:04:39 +02:00
|
|
|
use crate::wayland::handlers::compositor::client_compositor_state;
|
2023-07-05 23:48:10 +02:00
|
|
|
|
2025-03-25 16:44:55 -04:00
|
|
|
use clap_lex::RawArgs;
|
|
|
|
|
|
|
|
|
|
use std::error::Error;
|
|
|
|
|
|
2021-12-15 23:23:49 +01:00
|
|
|
pub mod backend;
|
2022-03-28 23:45:30 +02:00
|
|
|
pub mod config;
|
2024-02-07 13:58:44 +01:00
|
|
|
pub mod dbus;
|
2023-01-18 20:23:41 +01:00
|
|
|
#[cfg(feature = "debug")]
|
|
|
|
|
pub mod debug;
|
2021-12-22 20:14:09 +01:00
|
|
|
pub mod input;
|
2022-03-16 20:01:34 +01:00
|
|
|
mod logger;
|
2022-07-06 23:30:50 +02:00
|
|
|
pub mod session;
|
2021-12-17 17:53:01 +01:00
|
|
|
pub mod shell;
|
2021-12-15 18:00:28 +01:00
|
|
|
pub mod state;
|
2023-02-27 16:24:01 -08:00
|
|
|
#[cfg(feature = "systemd")]
|
2022-04-27 20:08:05 +02:00
|
|
|
pub mod systemd;
|
2023-10-10 13:55:34 -04:00
|
|
|
pub mod theme;
|
2021-12-15 23:23:49 +01:00
|
|
|
pub mod utils;
|
2022-03-16 20:19:12 +01:00
|
|
|
pub mod wayland;
|
2023-01-18 20:23:41 +01:00
|
|
|
pub mod xwayland;
|
2022-01-13 00:33:02 +01:00
|
|
|
|
2023-10-07 19:15:44 -07:00
|
|
|
#[cfg(feature = "profile-with-tracy")]
|
|
|
|
|
#[global_allocator]
|
|
|
|
|
static GLOBAL: profiling::tracy_client::ProfiledAllocator<std::alloc::System> =
|
|
|
|
|
profiling::tracy_client::ProfiledAllocator::new(std::alloc::System, 10);
|
|
|
|
|
|
2024-04-24 09:34:46 -07:00
|
|
|
// called by the Xwayland source, either after starting or failing
|
|
|
|
|
impl State {
|
|
|
|
|
fn notify_ready(&mut self) {
|
|
|
|
|
// TODO: Don't notify again, but potentially import updated env-variables
|
|
|
|
|
// into systemd and the session?
|
|
|
|
|
self.ready.call_once(|| {
|
|
|
|
|
// potentially tell systemd we are setup now
|
|
|
|
|
if let state::BackendData::Kms(_) = &self.backend {
|
2024-12-08 14:40:52 +01:00
|
|
|
#[cfg(feature = "systemd")]
|
2024-04-24 09:34:46 -07:00
|
|
|
systemd::ready(&self.common);
|
2024-12-08 14:40:52 +01:00
|
|
|
#[cfg(not(feature = "systemd"))]
|
|
|
|
|
if let Err(err) = dbus::ready(&self.common) {
|
|
|
|
|
error!(?err, "Failed to update the D-Bus activation environment");
|
|
|
|
|
}
|
2024-04-24 09:34:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// potentially tell the session we are setup now
|
|
|
|
|
if let Err(err) =
|
|
|
|
|
session::setup_socket(self.common.event_loop_handle.clone(), &self.common)
|
|
|
|
|
{
|
|
|
|
|
warn!(?err, "Failed to setup cosmic-session communication");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut args = env::args().skip(1);
|
|
|
|
|
self.common.kiosk_child = if let Some(exec) = args.next() {
|
|
|
|
|
// Run command in kiosk mode
|
|
|
|
|
let mut command = process::Command::new(&exec);
|
|
|
|
|
command.args(args);
|
|
|
|
|
command.envs(
|
|
|
|
|
session::get_env(&self.common).expect("WAYLAND_DISPLAY should be valid UTF-8"),
|
|
|
|
|
);
|
|
|
|
|
unsafe { command.pre_exec(|| Ok(utils::rlimit::restore_nofile_limit())) };
|
|
|
|
|
|
|
|
|
|
info!("Running {:?}", exec);
|
|
|
|
|
command
|
|
|
|
|
.spawn()
|
|
|
|
|
.map_err(|err| {
|
|
|
|
|
// TODO: replace with `inspect_err` once stable
|
|
|
|
|
error!(?err, "Error running kiosk child.");
|
|
|
|
|
err
|
|
|
|
|
})
|
|
|
|
|
.ok()
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-25 16:44:55 -04:00
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
|
|
|
let raw_args = RawArgs::from_args();
|
|
|
|
|
let mut cursor = raw_args.cursor();
|
|
|
|
|
let git_hash = option_env!("GIT_HASH").unwrap_or("unknown");
|
|
|
|
|
|
|
|
|
|
// Parse the arguments
|
|
|
|
|
while let Some(arg) = raw_args.next_os(&mut cursor) {
|
|
|
|
|
match arg.to_str() {
|
|
|
|
|
Some("--help") | Some("-h") => {
|
|
|
|
|
print_help(env!("CARGO_PKG_VERSION"), git_hash);
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
Some("--version") | Some("-V") => {
|
|
|
|
|
println!(
|
|
|
|
|
"cosmic-comp {} (git commit {})",
|
|
|
|
|
env!("CARGO_PKG_VERSION"),
|
|
|
|
|
git_hash
|
|
|
|
|
);
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 18:00:28 +01:00
|
|
|
// setup logger
|
2023-02-24 17:41:52 +01:00
|
|
|
logger::init_logger()?;
|
|
|
|
|
info!("Cosmic starting up!");
|
2021-12-15 23:23:49 +01:00
|
|
|
|
2024-06-07 20:04:39 +02:00
|
|
|
profiling::register_thread!("Main Thread");
|
2025-04-24 18:50:16 +02:00
|
|
|
#[cfg(feature = "profile-with-tracy")]
|
|
|
|
|
tracy_client::Client::start();
|
2024-06-07 20:04:39 +02:00
|
|
|
|
2024-04-03 18:08:43 +02:00
|
|
|
utils::rlimit::increase_nofile_limit();
|
|
|
|
|
|
2021-12-15 23:23:49 +01:00
|
|
|
// init event loop
|
2023-09-29 21:33:16 +02:00
|
|
|
let mut event_loop = EventLoop::try_new().with_context(|| "Failed to initialize event loop")?;
|
2021-12-15 23:23:49 +01:00
|
|
|
// init wayland
|
2022-02-08 17:15:24 +01:00
|
|
|
let (display, socket) = init_wayland_display(&mut event_loop)?;
|
2021-12-15 23:23:49 +01:00
|
|
|
// init state
|
2022-05-03 13:37:51 +02:00
|
|
|
let mut state = state::State::new(
|
2023-09-29 21:33:16 +02:00
|
|
|
&display,
|
2022-05-03 13:37:51 +02:00
|
|
|
socket,
|
|
|
|
|
event_loop.handle(),
|
|
|
|
|
event_loop.get_signal(),
|
|
|
|
|
);
|
2021-12-15 23:23:49 +01:00
|
|
|
// init backend
|
2023-09-29 21:33:16 +02:00
|
|
|
backend::init_backend_auto(&display, &mut event_loop, &mut state)?;
|
2024-02-06 09:25:48 -07:00
|
|
|
|
2023-10-10 13:55:34 -04:00
|
|
|
if let Err(err) = theme::watch_theme(event_loop.handle()) {
|
|
|
|
|
warn!(?err, "Failed to watch theme");
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 23:23:49 +01:00
|
|
|
// run the event loop
|
2023-09-29 21:33:16 +02:00
|
|
|
event_loop.run(None, &mut state, |state| {
|
2021-12-15 23:23:49 +01:00
|
|
|
// shall we shut down?
|
2024-06-26 17:25:02 +02:00
|
|
|
if state.common.should_stop {
|
2023-02-24 17:41:52 +01:00
|
|
|
info!("Shutting down");
|
2023-09-29 21:33:16 +02:00
|
|
|
state.common.event_loop_signal.stop();
|
|
|
|
|
state.common.event_loop_signal.wakeup();
|
2021-12-15 23:23:49 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-04 15:26:26 +02:00
|
|
|
// trigger routines
|
2025-05-20 17:41:27 +02:00
|
|
|
let clients = state.common.shell.write().update_animations();
|
2023-07-05 23:48:10 +02:00
|
|
|
{
|
2023-09-29 21:33:16 +02:00
|
|
|
let dh = state.common.display_handle.clone();
|
2023-07-05 23:48:10 +02:00
|
|
|
for client in clients.values() {
|
2023-09-29 21:33:16 +02:00
|
|
|
client_compositor_state(&client).blocker_cleared(state, &dh);
|
2023-07-05 23:48:10 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-03-05 19:37:12 +01:00
|
|
|
|
|
|
|
|
refresh(state);
|
2022-07-04 15:26:26 +02:00
|
|
|
|
2024-04-10 15:49:08 +02:00
|
|
|
{
|
2025-05-20 17:41:27 +02:00
|
|
|
let shell = state.common.shell.read();
|
2024-04-10 15:49:08 +02:00
|
|
|
if shell.animations_going() {
|
|
|
|
|
for output in shell.outputs().cloned().collect::<Vec<_>>().into_iter() {
|
2024-06-10 20:41:29 +02:00
|
|
|
state.backend.schedule_render(&output);
|
2024-04-10 15:49:08 +02:00
|
|
|
}
|
2024-02-23 17:23:36 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 23:23:49 +01:00
|
|
|
// send out events
|
2023-09-29 21:33:16 +02:00
|
|
|
let _ = state.common.display_handle.flush_clients();
|
2024-02-06 09:25:48 -07:00
|
|
|
|
|
|
|
|
// check if kiosk child is running
|
2024-04-24 09:34:46 -07:00
|
|
|
if let Some(child) = state.common.kiosk_child.as_mut() {
|
2024-02-06 09:25:48 -07:00
|
|
|
match child.try_wait() {
|
|
|
|
|
// Kiosk child exited with status
|
|
|
|
|
Ok(Some(exit_status)) => {
|
|
|
|
|
info!("Command exited with status {:?}", exit_status);
|
|
|
|
|
match exit_status.code() {
|
|
|
|
|
// Exiting with the same status as the kiosk child
|
|
|
|
|
Some(code) => process::exit(code),
|
|
|
|
|
// The kiosk child exited with signal, exiting with error
|
|
|
|
|
None => process::exit(1),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Command still running
|
|
|
|
|
Ok(None) => {}
|
|
|
|
|
// Kiosk child disappeared, exiting with error
|
|
|
|
|
Err(err) => {
|
|
|
|
|
warn!(?err, "Failed to wait for command");
|
|
|
|
|
process::exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-15 23:23:49 +01:00
|
|
|
})?;
|
|
|
|
|
|
2024-02-06 09:25:48 -07:00
|
|
|
// kill kiosk child if loop exited
|
2024-04-24 09:34:46 -07:00
|
|
|
if let Some(mut child) = state.common.kiosk_child.take() {
|
2024-02-06 09:25:48 -07:00
|
|
|
let _ = child.kill();
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-05 23:20:32 +01:00
|
|
|
// drop eventloop & state before logger
|
2022-03-16 19:47:39 +01:00
|
|
|
std::mem::drop(event_loop);
|
2023-09-29 21:33:16 +02:00
|
|
|
std::mem::drop(state);
|
2022-03-16 19:47:39 +01:00
|
|
|
|
2021-12-15 23:23:49 +01:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-25 16:44:55 -04:00
|
|
|
fn print_help(version: &str, git_rev: &str) {
|
|
|
|
|
println!(
|
|
|
|
|
r#"cosmic-comp {version} (git commit {git_rev})
|
|
|
|
|
System76 <info@system76.com>
|
|
|
|
|
|
|
|
|
|
Designed for the COSMIC™ desktop environment, cosmic-comp is a Wayland Compositor.
|
|
|
|
|
|
|
|
|
|
Project home page: https://github.com/pop-os/cosmic-comp
|
|
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
-h, --help Show this message
|
|
|
|
|
-v, --version Show the version of cosmic-comp"#
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-04 16:00:29 +02:00
|
|
|
fn init_wayland_display(
|
2023-09-29 21:33:16 +02:00
|
|
|
event_loop: &mut EventLoop<state::State>,
|
|
|
|
|
) -> Result<(DisplayHandle, OsString)> {
|
|
|
|
|
let display = Display::new().unwrap();
|
|
|
|
|
let handle = display.handle();
|
2021-12-15 23:23:49 +01:00
|
|
|
|
2023-02-24 17:41:52 +01:00
|
|
|
let source = ListeningSocketSource::new_auto().unwrap();
|
2022-07-04 15:26:26 +02:00
|
|
|
let socket_name = source.socket_name().to_os_string();
|
2023-02-24 17:41:52 +01:00
|
|
|
info!("Listening on {:?}", socket_name);
|
2022-07-04 16:00:29 +02:00
|
|
|
|
2022-07-04 15:26:26 +02:00
|
|
|
event_loop
|
|
|
|
|
.handle()
|
2023-09-29 21:33:16 +02:00
|
|
|
.insert_source(source, |client_stream, _, state| {
|
2024-05-07 16:02:12 -07:00
|
|
|
let client_state = state.new_client_state();
|
2024-06-07 20:04:39 +02:00
|
|
|
if let Err(err) = state
|
|
|
|
|
.common
|
|
|
|
|
.display_handle
|
|
|
|
|
.insert_client(client_stream, Arc::new(client_state))
|
|
|
|
|
{
|
2024-01-17 11:34:19 +00:00
|
|
|
warn!(?err, "Error adding wayland client")
|
2022-07-04 15:26:26 +02:00
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
.with_context(|| "Failed to init the wayland socket source.")?;
|
2021-12-15 18:00:28 +01:00
|
|
|
event_loop
|
|
|
|
|
.handle()
|
|
|
|
|
.insert_source(
|
2023-09-29 21:33:16 +02:00
|
|
|
Generic::new(display, Interest::READ, Mode::Level),
|
|
|
|
|
move |_, display, state| {
|
|
|
|
|
// SAFETY: We don't drop the display
|
|
|
|
|
match unsafe { display.get_mut().dispatch_clients(state) } {
|
|
|
|
|
Ok(_) => Ok(PostAction::Continue),
|
|
|
|
|
Err(err) => {
|
|
|
|
|
error!(?err, "I/O error on the Wayland display");
|
|
|
|
|
state.common.should_stop = true;
|
|
|
|
|
Err(err)
|
|
|
|
|
}
|
2021-12-15 18:00:28 +01:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
)
|
2021-12-15 23:23:49 +01:00
|
|
|
.with_context(|| "Failed to init the wayland event source.")?;
|
2021-12-15 18:00:28 +01:00
|
|
|
|
2023-09-29 21:33:16 +02:00
|
|
|
Ok((handle, socket_name))
|
2021-12-15 17:25:15 +01:00
|
|
|
}
|
2025-03-05 19:37:12 +01:00
|
|
|
|
|
|
|
|
fn refresh(state: &mut State) {
|
|
|
|
|
if matches!(state.last_refresh, LastRefresh::Scheduled(_)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if matches!(state.last_refresh, LastRefresh::At(instant) if Instant::now().duration_since(instant) < Duration::from_millis(150))
|
|
|
|
|
{
|
|
|
|
|
if let Ok(token) = state.common.event_loop_handle.insert_source(
|
|
|
|
|
Timer::from_duration(Duration::from_millis(150)),
|
|
|
|
|
|_, _, state| {
|
|
|
|
|
state.last_refresh = LastRefresh::None;
|
|
|
|
|
TimeoutAction::Drop
|
|
|
|
|
},
|
|
|
|
|
) {
|
|
|
|
|
state.last_refresh = LastRefresh::Scheduled(token);
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
warn!("Failed to schedule refresh");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state.common.refresh();
|
|
|
|
|
state::Common::refresh_focus(state);
|
|
|
|
|
OverlapNotifyState::refresh(state);
|
|
|
|
|
state.common.update_x11_stacking_order();
|
|
|
|
|
state.last_refresh = LastRefresh::At(Instant::now());
|
|
|
|
|
}
|