cosmic-comp/src/main.rs

129 lines
3.8 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},
wayland_server::Display,
},
wayland::socket::ListeningSocketSource,
};
use anyhow::{Context, Result};
use std::{ffi::OsString, os::unix::prelude::AsRawFd, sync::Arc};
use tracing::{error, info, warn};
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;
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
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(
&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
backend::init_backend_auto(&display.handle(), &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
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
event_loop.run(None, &mut data, |data| {
2021-12-15 23:23:49 +01:00
// shall we shut down?
if data.state.common.shell.outputs().next().is_none() || data.state.common.should_stop {
info!("Shutting down");
data.state.common.event_loop_signal.stop();
data.state.common.event_loop_signal.wakeup();
2021-12-15 23:23:49 +01:00
return;
}
// 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);
2021-12-15 23:23:49 +01:00
// send out events
let _ = data.display.flush_clients();
2021-12-15 23:23:49 +01:00
})?;
// drop eventloop & state before logger
2022-03-16 19:47:39 +01:00
std::mem::drop(event_loop);
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)> {
let mut display = Display::new().unwrap();
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()
.insert_source(source, |client_stream, _, data| {
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()
}),
) {
warn!(?err, "Error adding wayland client");
};
})
.with_context(|| "Failed to init the wayland socket source.")?;
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),
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;
Err(err)
}
},
)
2021-12-15 23:23:49 +01:00
.with_context(|| "Failed to init the wayland event source.")?;
2022-02-08 17:15:24 +01:00
Ok((display, socket_name))
2021-12-15 17:25:15 +01:00
}