|
| 1 | +// SPDX-License-Identifier: GPL-3.0-only |
| 2 | + |
| 3 | +use smithay::{ |
| 4 | + reexports::{ |
| 5 | + calloop::{generic::Generic, EventLoop, Interest, Mode, PostAction}, |
| 6 | + wayland_server::{Display, DisplayHandle}, |
| 7 | + }, |
| 8 | + wayland::socket::ListeningSocketSource, |
| 9 | +}; |
| 10 | + |
| 11 | +use anyhow::{Context, Result}; |
| 12 | +use state::State; |
| 13 | +use std::{env, ffi::OsString, os::unix::process::CommandExt, process, sync::Arc}; |
| 14 | +use tracing::{error, info, warn}; |
| 15 | +use wayland::protocols::overlap_notify::OverlapNotifyState; |
| 16 | + |
| 17 | +use crate::wayland::handlers::compositor::client_compositor_state; |
| 18 | + |
| 19 | +pub mod backend; |
| 20 | +pub mod config; |
| 21 | +pub mod dbus; |
| 22 | +#[cfg(feature = "debug")] |
| 23 | +pub mod debug; |
| 24 | +pub mod input; |
| 25 | +mod logger; |
| 26 | +pub mod session; |
| 27 | +pub mod shell; |
| 28 | +pub mod state; |
| 29 | +#[cfg(feature = "systemd")] |
| 30 | +pub mod systemd; |
| 31 | +pub mod theme; |
| 32 | +pub mod utils; |
| 33 | +pub mod wayland; |
| 34 | +pub mod xwayland; |
| 35 | + |
| 36 | +#[cfg(feature = "profile-with-tracy")] |
| 37 | +#[global_allocator] |
| 38 | +static GLOBAL: profiling::tracy_client::ProfiledAllocator<std::alloc::System> = |
| 39 | + profiling::tracy_client::ProfiledAllocator::new(std::alloc::System, 10); |
| 40 | + |
| 41 | +// called by the Xwayland source, either after starting or failing |
| 42 | +impl State { |
| 43 | + fn notify_ready(&mut self) { |
| 44 | + // TODO: Don't notify again, but potentially import updated env-variables |
| 45 | + // into systemd and the session? |
| 46 | + self.ready.call_once(|| { |
| 47 | + // potentially tell systemd we are setup now |
| 48 | + if let state::BackendData::Kms(_) = &self.backend { |
| 49 | + #[cfg(feature = "systemd")] |
| 50 | + systemd::ready(&self.common); |
| 51 | + #[cfg(not(feature = "systemd"))] |
| 52 | + if let Err(err) = dbus::ready(&self.common) { |
| 53 | + error!(?err, "Failed to update the D-Bus activation environment"); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // potentially tell the session we are setup now |
| 58 | + if let Err(err) = |
| 59 | + session::setup_socket(self.common.event_loop_handle.clone(), &self.common) |
| 60 | + { |
| 61 | + warn!(?err, "Failed to setup cosmic-session communication"); |
| 62 | + } |
| 63 | + |
| 64 | + let mut args = env::args().skip(1); |
| 65 | + self.common.kiosk_child = if let Some(exec) = args.next() { |
| 66 | + // Run command in kiosk mode |
| 67 | + let mut command = process::Command::new(&exec); |
| 68 | + command.args(args); |
| 69 | + command.envs( |
| 70 | + session::get_env(&self.common).expect("WAYLAND_DISPLAY should be valid UTF-8"), |
| 71 | + ); |
| 72 | + unsafe { command.pre_exec(|| Ok(utils::rlimit::restore_nofile_limit())) }; |
| 73 | + |
| 74 | + info!("Running {:?}", exec); |
| 75 | + command |
| 76 | + .spawn() |
| 77 | + .map_err(|err| { |
| 78 | + // TODO: replace with `inspect_err` once stable |
| 79 | + error!(?err, "Error running kiosk child."); |
| 80 | + err |
| 81 | + }) |
| 82 | + .ok() |
| 83 | + } else { |
| 84 | + None |
| 85 | + }; |
| 86 | + }); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +fn run() -> Result<()> { |
| 91 | + // setup logger |
| 92 | + logger::init_logger()?; |
| 93 | + info!("Cosmic starting up!"); |
| 94 | + |
| 95 | + #[cfg(feature = "profile-with-tracy")] |
| 96 | + profiling::tracy_client::Client::start(); |
| 97 | + profiling::register_thread!("Main Thread"); |
| 98 | + |
| 99 | + utils::rlimit::increase_nofile_limit(); |
| 100 | + |
| 101 | + // init event loop |
| 102 | + let mut event_loop = EventLoop::try_new().with_context(|| "Failed to initialize event loop")?; |
| 103 | + // init wayland |
| 104 | + let (display, socket) = init_wayland_display(&mut event_loop)?; |
| 105 | + // init state |
| 106 | + let mut state = state::State::new( |
| 107 | + &display, |
| 108 | + socket, |
| 109 | + event_loop.handle(), |
| 110 | + event_loop.get_signal(), |
| 111 | + ); |
| 112 | + // init backend |
| 113 | + backend::init_backend_auto(&display, &mut event_loop, &mut state)?; |
| 114 | + |
| 115 | + if let Err(err) = theme::watch_theme(event_loop.handle()) { |
| 116 | + warn!(?err, "Failed to watch theme"); |
| 117 | + } |
| 118 | + |
| 119 | + // run the event loop |
| 120 | + event_loop.run(None, &mut state, |state| { |
| 121 | + // shall we shut down? |
| 122 | + if state.common.should_stop { |
| 123 | + info!("Shutting down"); |
| 124 | + state.common.event_loop_signal.stop(); |
| 125 | + state.common.event_loop_signal.wakeup(); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + // trigger routines |
| 130 | + let clients = state.common.shell.write().unwrap().update_animations(); |
| 131 | + { |
| 132 | + let dh = state.common.display_handle.clone(); |
| 133 | + for client in clients.values() { |
| 134 | + client_compositor_state(&client).blocker_cleared(state, &dh); |
| 135 | + } |
| 136 | + } |
| 137 | + state.common.refresh(); |
| 138 | + state::Common::refresh_focus(state); |
| 139 | + OverlapNotifyState::refresh(state); |
| 140 | + state.common.update_x11_stacking_order(); |
| 141 | + |
| 142 | + { |
| 143 | + let shell = state.common.shell.read().unwrap(); |
| 144 | + if shell.animations_going() { |
| 145 | + for output in shell.outputs().cloned().collect::<Vec<_>>().into_iter() { |
| 146 | + state.backend.schedule_render(&output); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + // send out events |
| 152 | + let _ = state.common.display_handle.flush_clients(); |
| 153 | + |
| 154 | + // check if kiosk child is running |
| 155 | + if let Some(child) = state.common.kiosk_child.as_mut() { |
| 156 | + match child.try_wait() { |
| 157 | + // Kiosk child exited with status |
| 158 | + Ok(Some(exit_status)) => { |
| 159 | + info!("Command exited with status {:?}", exit_status); |
| 160 | + match exit_status.code() { |
| 161 | + // Exiting with the same status as the kiosk child |
| 162 | + Some(code) => process::exit(code), |
| 163 | + // The kiosk child exited with signal, exiting with error |
| 164 | + None => process::exit(1), |
| 165 | + } |
| 166 | + } |
| 167 | + // Command still running |
| 168 | + Ok(None) => {} |
| 169 | + // Kiosk child disappeared, exiting with error |
| 170 | + Err(err) => { |
| 171 | + warn!(?err, "Failed to wait for command"); |
| 172 | + process::exit(1); |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + })?; |
| 177 | + |
| 178 | + // kill kiosk child if loop exited |
| 179 | + if let Some(mut child) = state.common.kiosk_child.take() { |
| 180 | + let _ = child.kill(); |
| 181 | + } |
| 182 | + |
| 183 | + // drop eventloop & state before logger |
| 184 | + std::mem::drop(event_loop); |
| 185 | + std::mem::drop(state); |
| 186 | + |
| 187 | + Ok(()) |
| 188 | +} |
| 189 | + |
| 190 | +fn init_wayland_display( |
| 191 | + event_loop: &mut EventLoop<state::State>, |
| 192 | +) -> Result<(DisplayHandle, OsString)> { |
| 193 | + let display = Display::new().unwrap(); |
| 194 | + let handle = display.handle(); |
| 195 | + |
| 196 | + let source = ListeningSocketSource::new_auto().unwrap(); |
| 197 | + let socket_name = source.socket_name().to_os_string(); |
| 198 | + info!("Listening on {:?}", socket_name); |
| 199 | + |
| 200 | + event_loop |
| 201 | + .handle() |
| 202 | + .insert_source(source, |client_stream, _, state| { |
| 203 | + let client_state = state.new_client_state(); |
| 204 | + if let Err(err) = state |
| 205 | + .common |
| 206 | + .display_handle |
| 207 | + .insert_client(client_stream, Arc::new(client_state)) |
| 208 | + { |
| 209 | + warn!(?err, "Error adding wayland client") |
| 210 | + }; |
| 211 | + }) |
| 212 | + .with_context(|| "Failed to init the wayland socket source.")?; |
| 213 | + event_loop |
| 214 | + .handle() |
| 215 | + .insert_source( |
| 216 | + Generic::new(display, Interest::READ, Mode::Level), |
| 217 | + move |_, display, state| { |
| 218 | + // SAFETY: We don't drop the display |
| 219 | + match unsafe { display.get_mut().dispatch_clients(state) } { |
| 220 | + Ok(_) => Ok(PostAction::Continue), |
| 221 | + Err(err) => { |
| 222 | + error!(?err, "I/O error on the Wayland display"); |
| 223 | + state.common.should_stop = true; |
| 224 | + Err(err) |
| 225 | + } |
| 226 | + } |
| 227 | + }, |
| 228 | + ) |
| 229 | + .with_context(|| "Failed to init the wayland event source.")?; |
| 230 | + |
| 231 | + Ok((handle, socket_name)) |
| 232 | +} |
0 commit comments