cosmic-comp/src/main.rs

191 lines
6 KiB
Rust
Raw Normal View History

2021-12-15 17:25:15 +01:00
// SPDX-License-Identifier: GPL-3.0-only
use smithay::{
reexports::{
calloop::{generic::Generic, EventLoop, Interest, Mode, PostAction},
2023-09-29 21:33:16 +02:00
wayland_server::{Display, DisplayHandle},
},
wayland::socket::ListeningSocketSource,
};
use anyhow::{Context, Result};
use std::{env, ffi::OsString, process, sync::Arc};
use tracing::{error, info, warn};
use crate::{
state::BackendData, utils::prelude::SeatExt,
wayland::handlers::compositor::client_compositor_state,
};
2023-07-05 23:48:10 +02:00
2021-12-15 23:23:49 +01:00
pub mod backend;
2022-03-28 23:45:30 +02:00
pub mod config;
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;
pub mod state;
#[cfg(feature = "systemd")]
2022-04-27 20:08:05 +02:00
pub mod systemd;
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;
fn main() -> Result<()> {
// setup logger
logger::init_logger()?;
info!("Cosmic starting up!");
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)?;
// potentially tell systemd we are setup now
#[cfg(feature = "systemd")]
if let state::BackendData::Kms(_) = &state.backend {
systemd::ready(&state);
}
2022-07-06 23:30:50 +02:00
// potentially tell the session we are setup now
session::setup_socket(event_loop.handle(), &state)?;
2021-12-15 23:23:49 +01:00
let mut args = env::args().skip(1);
let mut child_opt = 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(&state)?);
info!("Running {:?}", exec);
Some(command.spawn()?)
} else {
None
};
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?
2023-09-29 21:33:16 +02:00
if state.common.shell.outputs().next().is_none() || state.common.should_stop {
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;
}
// trigger routines
2023-09-29 21:33:16 +02:00
let clients = state.common.shell.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
}
}
2023-09-29 21:33:16 +02:00
state.common.shell.refresh();
state::Common::refresh_focus(state);
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();
// check if kiosk child is running
if let Some(ref mut child) = child_opt {
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
})?;
// kill kiosk child if loop exited
if let Some(mut child) = child_opt {
let _ = child.kill();
}
// 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(())
}
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
let source = ListeningSocketSource::new_auto().unwrap();
let socket_name = source.socket_name().to_os_string();
info!("Listening on {:?}", socket_name);
2022-07-04 16:00:29 +02:00
event_loop
.handle()
2023-09-29 21:33:16 +02:00
.insert_source(source, |client_stream, _, state| {
let node = match &state.backend {
BackendData::Kms(kms_state) if kms_state.auto_assign => kms_state
.target_node_for_output(&state.common.last_active_seat().active_output()),
_ => None,
};
2023-09-29 21:33:16 +02:00
if let Err(err) = state.common.display_handle.insert_client(
client_stream,
Arc::new(if cfg!(debug_assertions) {
2023-09-29 21:33:16 +02:00
state.new_privileged_client_state()
} else if let Some(node) = node {
state.new_client_state_with_node(node)
} else {
2023-09-29 21:33:16 +02:00
state.new_client_state()
}),
) {
warn!(?err, "Error adding wayland client")
};
})
.with_context(|| "Failed to init the wayland socket source.")?;
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 23:23:49 +01:00
.with_context(|| "Failed to init the wayland event source.")?;
2023-09-29 21:33:16 +02:00
Ok((handle, socket_name))
2021-12-15 17:25:15 +01:00
}