Unify event loop running

This commit is contained in:
Ottatop 2024-03-04 15:54:30 -06:00
parent 96d5c9a70f
commit d9116da20d
4 changed files with 34 additions and 43 deletions

View file

@ -236,8 +236,11 @@ impl BackendData for Udev {
} }
} }
pub fn run_udev(no_config: bool, config_dir: Option<PathBuf>) -> anyhow::Result<()> { pub fn setup_udev(
let mut event_loop = EventLoop::try_new()?; no_config: bool,
config_dir: Option<PathBuf>,
) -> anyhow::Result<(State, EventLoop<'static, State>)> {
let event_loop = EventLoop::try_new()?;
let display = Display::new()?; let display = Display::new()?;
// Initialize session // Initialize session
@ -515,21 +518,7 @@ pub fn run_udev(no_config: bool, config_dir: Option<PathBuf>) -> anyhow::Result<
tracing::error!("Failed to start XWayland: {err}"); tracing::error!("Failed to start XWayland: {err}");
} }
event_loop.run( Ok((state, event_loop))
Some(Duration::from_micros(((1.0 / 144.0) * 1000000.0) as u64)),
&mut state,
|state| {
state.fixup_focus();
state.space.refresh();
state.popup_manager.cleanup();
state
.display_handle
.flush_clients()
.expect("failed to flush_clients");
},
)?;
Ok(())
} }
// TODO: document desperately // TODO: document desperately

View file

@ -62,8 +62,11 @@ impl Backend {
} }
/// Start Pinnacle as a window in a graphical environment. /// Start Pinnacle as a window in a graphical environment.
pub fn run_winit(no_config: bool, config_dir: Option<PathBuf>) -> anyhow::Result<()> { pub fn setup_winit(
let mut event_loop: EventLoop<State> = EventLoop::try_new()?; no_config: bool,
config_dir: Option<PathBuf>,
) -> anyhow::Result<(State, EventLoop<'static, State>)> {
let event_loop: EventLoop<State> = EventLoop::try_new()?;
let display: Display<State> = Display::new()?; let display: Display<State> = Display::new()?;
let display_handle = display.handle(); let display_handle = display.handle();
@ -237,21 +240,7 @@ pub fn run_winit(no_config: bool, config_dir: Option<PathBuf>) -> anyhow::Result
anyhow::bail!("Failed to insert winit events into event loop: {err}"); anyhow::bail!("Failed to insert winit events into event loop: {err}");
} }
event_loop.run( Ok((state, event_loop))
Some(Duration::from_micros(((1.0 / 144.0) * 1000000.0) as u64)),
&mut state,
|state| {
state.fixup_focus();
state.space.refresh();
state.popup_manager.cleanup();
state
.display_handle
.flush_clients()
.expect("failed to flush client buffers");
},
)?;
Ok(())
} }
impl State { impl State {

View file

@ -14,7 +14,7 @@ pub enum Backend {
/// The main CLI struct. /// The main CLI struct.
#[derive(clap::Parser, Debug)] #[derive(clap::Parser, Debug)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None, args_conflicts_with_subcommands = true)]
pub struct Cli { pub struct Cli {
/// Start Pinnacle with the config at this directory /// Start Pinnacle with the config at this directory
#[arg(short, long, value_name("DIR"), value_hint(ValueHint::DirPath))] #[arg(short, long, value_name("DIR"), value_hint(ValueHint::DirPath))]
@ -50,6 +50,7 @@ pub struct Cli {
} }
impl Cli { impl Cli {
//
pub fn parse_and_prompt() -> Option<Self> { pub fn parse_and_prompt() -> Option<Self> {
let mut cli = Cli::parse(); let mut cli = Cli::parse();

View file

@ -97,48 +97,60 @@ async fn main() -> anyhow::Result<()> {
warn!("You may see LOTS of file descriptors open under Pinnacle."); warn!("You may see LOTS of file descriptors open under Pinnacle.");
} }
match (cli.backend, cli.force) { let (mut state, mut event_loop) = match (cli.backend, cli.force) {
(None, _) => { (None, _) => {
if in_graphical_env { if in_graphical_env {
info!("Starting winit backend"); info!("Starting winit backend");
crate::backend::winit::run_winit(cli.no_config, cli.config_dir)?; crate::backend::winit::setup_winit(cli.no_config, cli.config_dir)?
} else { } else {
info!("Starting udev backend"); info!("Starting udev backend");
crate::backend::udev::run_udev(cli.no_config, cli.config_dir)?; crate::backend::udev::setup_udev(cli.no_config, cli.config_dir)?
} }
} }
(Some(cli::Backend::Winit), force) => { (Some(cli::Backend::Winit), force) => {
if !in_graphical_env { if !in_graphical_env {
if force { if force {
warn!("Starting winit backend with no detected graphical environment"); warn!("Starting winit backend with no detected graphical environment");
crate::backend::winit::run_winit(cli.no_config, cli.config_dir)?; crate::backend::winit::setup_winit(cli.no_config, cli.config_dir)?
} else { } else {
warn!("Both WAYLAND_DISPLAY and DISPLAY are not set."); warn!("Both WAYLAND_DISPLAY and DISPLAY are not set.");
warn!("If you are trying to run the winit backend in a tty, it won't work."); warn!("If you are trying to run the winit backend in a tty, it won't work.");
warn!("If you really want to, additionally pass in the `--force` flag."); warn!("If you really want to, additionally pass in the `--force` flag.");
return Ok(());
} }
} else { } else {
info!("Starting winit backend"); info!("Starting winit backend");
crate::backend::winit::run_winit(cli.no_config, cli.config_dir)?; crate::backend::winit::setup_winit(cli.no_config, cli.config_dir)?
} }
} }
(Some(cli::Backend::Udev), force) => { (Some(cli::Backend::Udev), force) => {
if in_graphical_env { if in_graphical_env {
if force { if force {
warn!("Starting udev backend with a detected graphical environment"); warn!("Starting udev backend with a detected graphical environment");
crate::backend::udev::run_udev(cli.no_config, cli.config_dir)?; crate::backend::udev::setup_udev(cli.no_config, cli.config_dir)?
} else { } else {
warn!("WAYLAND_DISPLAY and/or DISPLAY are set."); warn!("WAYLAND_DISPLAY and/or DISPLAY are set.");
warn!("If you are trying to run the udev backend in a graphical environment,"); warn!("If you are trying to run the udev backend in a graphical environment,");
warn!("it won't work and may mess some things up."); warn!("it won't work and may mess some things up.");
warn!("If you really want to, additionally pass in the `--force` flag."); warn!("If you really want to, additionally pass in the `--force` flag.");
return Ok(());
} }
} else { } else {
info!("Starting udev backend"); info!("Starting udev backend");
crate::backend::udev::run_udev(cli.no_config, cli.config_dir)?; crate::backend::udev::setup_udev(cli.no_config, cli.config_dir)?
} }
} }
} };
event_loop.run(None, &mut state, |state| {
state.fixup_focus();
state.space.refresh();
state.popup_manager.cleanup();
state
.display_handle
.flush_clients()
.expect("failed to flush client buffers");
})?;
Ok(()) Ok(())
} }