cosmic-comp/src/main.rs

89 lines
2.7 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};
use std::sync::atomic::Ordering;
2021-12-15 23:23:49 +01:00
pub mod backend;
2021-12-22 20:14:09 +01:00
pub mod input;
2021-12-17 17:53:01 +01:00
pub mod shell;
pub mod state;
2022-02-05 00:40:17 +01:00
mod logger;
2021-12-15 23:23:49 +01:00
pub mod utils;
#[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
let display = init_wayland_display(&mut event_loop)?;
// init state
2022-02-05 00:40:17 +01:00
let mut state = state::State::new(display, event_loop.handle(), log);
2021-12-15 23:23:49 +01:00
// init backend
backend::init_backend_auto(&mut event_loop, &mut state)?;
// run the event loop
let signal = event_loop.get_signal();
event_loop.run(None, &mut state, |state| {
// shall we shut down?
2022-01-11 17:00:04 +01:00
if state.common.spaces.outputs().next().is_none() || state.common.should_stop {
2021-12-15 23:23:49 +01:00
slog_scope::info!("Shutting down");
signal.stop();
signal.wakeup();
return;
}
// do we need to trigger another render
if state.common.dirty_flag.swap(false, Ordering::SeqCst) {
for output in state.common.spaces.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
state.common.spaces.refresh();
2021-12-15 23:23:49 +01:00
})?;
Ok(())
}
fn init_wayland_display(event_loop: &mut EventLoop<state::State>) -> Result<Display> {
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.")?;
2021-12-15 23:23:49 +01:00
Ok(display)
2021-12-15 17:25:15 +01:00
}