2021-12-15 17:25:15 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
|
|
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};
|
2024-02-06 09:25:48 -07:00
|
|
|
use std::{env, ffi::OsString, process, sync::Arc};
|
2023-02-24 17:41:52 +01:00
|
|
|
use tracing::{error, info, warn};
|
2021-12-15 18:00:28 +01:00
|
|
|
|
2024-01-17 11:34:19 +00:00
|
|
|
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;
|
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
|
|
|
|
2021-12-15 18:00:28 +01:00
|
|
|
fn main() -> Result<()> {
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
// 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)?;
|
2022-07-11 22:34:34 +02:00
|
|
|
// potentially tell systemd we are setup now
|
2023-02-27 16:24:01 -08:00
|
|
|
#[cfg(feature = "systemd")]
|
2022-07-11 22:34:34 +02:00
|
|
|
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
|
|
|
|
2024-02-06 09:25:48 -07: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
|
|
|
|
|
};
|
|
|
|
|
|
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?
|
2023-09-29 21:33:16 +02:00
|
|
|
if state.common.shell.outputs().next().is_none() || 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
|
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);
|
2022-07-04 15:26:26 +02: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
|
|
|
|
|
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
|
|
|
})?;
|
|
|
|
|
|
2024-02-06 09:25:48 -07:00
|
|
|
// kill kiosk child if loop exited
|
|
|
|
|
if let Some(mut child) = child_opt {
|
|
|
|
|
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(())
|
|
|
|
|
}
|
|
|
|
|
|
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-01-17 11:34:19 +00:00
|
|
|
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(
|
2022-07-06 23:31:46 +02:00
|
|
|
client_stream,
|
|
|
|
|
Arc::new(if cfg!(debug_assertions) {
|
2023-09-29 21:33:16 +02:00
|
|
|
state.new_privileged_client_state()
|
2024-01-17 11:34:19 +00:00
|
|
|
} else if let Some(node) = node {
|
|
|
|
|
state.new_client_state_with_node(node)
|
2022-07-06 23:31:46 +02:00
|
|
|
} else {
|
2023-09-29 21:33:16 +02:00
|
|
|
state.new_client_state()
|
2022-07-06 23:31:46 +02:00
|
|
|
}),
|
|
|
|
|
) {
|
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
|
|
|
}
|