cosmic-comp/src/main.rs

98 lines
3 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,
};
use anyhow::{Context, Result};
2022-03-16 20:01:34 +01:00
use std::{ffi::OsString, sync::atomic::Ordering};
2021-12-15 23:23:49 +01:00
pub mod backend;
2022-03-28 23:45:30 +02:00
pub mod config;
2021-12-22 20:14:09 +01:00
pub mod input;
2022-03-16 20:01:34 +01:00
mod logger;
2021-12-17 17:53:01 +01:00
pub mod shell;
pub mod state;
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;
#[cfg(feature = "debug")]
pub mod debug;
fn main() -> Result<()> {
// setup logger
2022-02-05 00:40:17 +01:00
let log = logger::init_logger()?;
2021-12-15 23:23:49 +01:00
slog_scope::info!("Cosmic starting up!");
// init event loop
let mut event_loop = EventLoop::try_new().with_context(|| "Failed to initialize event loop")?;
// 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-04-27 13:25:17 +02:00
let mut state = state::State::new(display, socket, event_loop.handle(), event_loop.get_signal(), log);
2021-12-15 23:23:49 +01:00
// init backend
backend::init_backend_auto(&mut event_loop, &mut state)?;
2022-04-27 20:08:05 +02:00
// potentially tell systemd we are setup now
systemd::ready(&state);
2021-12-15 23:23:49 +01:00
// run the event loop
event_loop.run(None, &mut state, |state| {
// shall we shut down?
2022-03-24 20:32:31 +01:00
if state.common.shell.outputs().next().is_none() || state.common.should_stop {
2021-12-15 23:23:49 +01:00
slog_scope::info!("Shutting down");
2022-04-27 13:25:17 +02:00
state.common.event_loop_signal.stop();
state.common.event_loop_signal.wakeup();
2021-12-15 23:23:49 +01:00
return;
}
// do we need to trigger another render
if state.common.dirty_flag.swap(false, Ordering::SeqCst) {
2022-03-24 20:32:31 +01:00
for output in state.common.shell.outputs() {
state.backend.schedule_render(output)
}
}
2021-12-15 23:23:49 +01:00
// send out events
2022-01-11 17:00:04 +01:00
let display = state.common.display.clone();
2021-12-15 23:23:49 +01:00
display.borrow_mut().flush_clients(state);
// trigger routines
2022-03-24 20:32:31 +01:00
state.common.shell.refresh();
2022-03-30 22:00:44 +02:00
state.common.refresh_focus();
2021-12-15 23:23:49 +01:00
})?;
2022-03-16 19:47:39 +01:00
let _log = state.destroy();
// drop eventloop before logger
std::mem::drop(event_loop);
2021-12-15 23:23:49 +01:00
Ok(())
}
2022-02-08 17:15:24 +01:00
fn init_wayland_display(event_loop: &mut EventLoop<state::State>) -> Result<(Display, OsString)> {
let mut display = Display::new();
let socket_name = display.add_socket_auto()?;
2021-12-15 23:23:49 +01:00
slog_scope::info!("Listening on {:?}", socket_name);
event_loop
.handle()
.insert_source(
Generic::from_fd(display.get_poll_fd(), Interest::READ, Mode::Level),
move |_, _, state: &mut state::State| {
2022-01-11 17:00:04 +01:00
let display = state.common.display.clone();
let mut display = display.borrow_mut();
match display.dispatch(std::time::Duration::from_millis(0), state) {
Ok(_) => Ok(PostAction::Continue),
Err(e) => {
slog_scope::error!("I/O error on the Wayland display: {}", e);
2022-01-11 17:00:04 +01:00
state.common.should_stop = true;
Err(e)
}
}
},
)
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
}