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},
|
|
|
|
|
wayland_server::Display,
|
|
|
|
|
},
|
|
|
|
|
wayland::socket::ListeningSocketSource,
|
2021-12-15 18:00:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use anyhow::{Context, Result};
|
2022-11-08 10:32:53 +01:00
|
|
|
use std::{ffi::OsString, os::unix::prelude::AsRawFd, sync::Arc};
|
2023-02-24 17:41:52 +01:00
|
|
|
use tracing::{error, info, warn};
|
2021-12-15 18:00:28 +01: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;
|
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
|
2022-05-12 14:04:21 +02:00
|
|
|
let mut event_loop =
|
|
|
|
|
EventLoop::try_new_high_precision().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(
|
2022-07-04 15:26:26 +02:00
|
|
|
&display.handle(),
|
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
|
2022-07-04 15:26:26 +02:00
|
|
|
backend::init_backend_auto(&display.handle(), &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
|
|
|
|
2022-07-04 16:00:29 +02:00
|
|
|
let mut data = state::Data { display, state };
|
2021-12-15 23:23:49 +01:00
|
|
|
// run the event loop
|
2022-07-04 15:26:26 +02:00
|
|
|
event_loop.run(None, &mut data, |data| {
|
2021-12-15 23:23:49 +01:00
|
|
|
// shall we shut down?
|
2022-07-04 15:26:26 +02:00
|
|
|
if data.state.common.shell.outputs().next().is_none() || data.state.common.should_stop {
|
2023-02-24 17:41:52 +01:00
|
|
|
info!("Shutting down");
|
2022-07-04 15:26:26 +02:00
|
|
|
data.state.common.event_loop_signal.stop();
|
|
|
|
|
data.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
|
2022-09-28 12:01:29 +02:00
|
|
|
data.state.common.shell.refresh();
|
2022-08-31 13:01:23 +02:00
|
|
|
state::Common::refresh_focus(&mut data.state);
|
2022-07-04 15:26:26 +02:00
|
|
|
|
2021-12-15 23:23:49 +01:00
|
|
|
// send out events
|
2022-07-04 15:26:26 +02:00
|
|
|
let _ = data.display.flush_clients();
|
2021-12-15 23:23:49 +01:00
|
|
|
})?;
|
|
|
|
|
|
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);
|
2022-12-05 23:20:32 +01:00
|
|
|
std::mem::drop(data);
|
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(
|
|
|
|
|
event_loop: &mut EventLoop<state::Data>,
|
|
|
|
|
) -> Result<(Display<state::State>, OsString)> {
|
2022-07-04 15:26:26 +02:00
|
|
|
let mut display = Display::new().unwrap();
|
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()
|
|
|
|
|
.insert_source(source, |client_stream, _, data| {
|
2022-07-06 23:31:46 +02:00
|
|
|
if let Err(err) = data.display.handle().insert_client(
|
|
|
|
|
client_stream,
|
|
|
|
|
Arc::new(if cfg!(debug_assertions) {
|
|
|
|
|
data.state.new_privileged_client_state()
|
|
|
|
|
} else {
|
|
|
|
|
data.state.new_client_state()
|
|
|
|
|
}),
|
|
|
|
|
) {
|
2023-02-24 17:41:52 +01: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(
|
2022-09-28 15:18:04 +02:00
|
|
|
Generic::new(
|
|
|
|
|
display.backend().poll_fd().as_raw_fd(),
|
|
|
|
|
Interest::READ,
|
|
|
|
|
Mode::Level,
|
|
|
|
|
),
|
2022-07-04 16:00:29 +02:00
|
|
|
move |_, _, data: &mut state::Data| match data.display.dispatch_clients(&mut data.state)
|
|
|
|
|
{
|
|
|
|
|
Ok(_) => Ok(PostAction::Continue),
|
2023-02-24 17:41:52 +01:00
|
|
|
Err(err) => {
|
|
|
|
|
error!(?err, "I/O error on the Wayland display");
|
2022-07-04 16:00:29 +02:00
|
|
|
data.state.common.should_stop = true;
|
2023-02-24 17:41:52 +01:00
|
|
|
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
|
|
|
|
2022-02-08 17:15:24 +01:00
|
|
|
Ok((display, socket_name))
|
2021-12-15 17:25:15 +01:00
|
|
|
}
|