diff --git a/src/backend/udev.rs b/src/backend/udev.rs index 2b5f26b..564efb6 100644 --- a/src/backend/udev.rs +++ b/src/backend/udev.rs @@ -236,8 +236,11 @@ impl BackendData for Udev { } } -pub fn run_udev(no_config: bool, config_dir: Option) -> anyhow::Result<()> { - let mut event_loop = EventLoop::try_new()?; +pub fn setup_udev( + no_config: bool, + config_dir: Option, +) -> anyhow::Result<(State, EventLoop<'static, State>)> { + let event_loop = EventLoop::try_new()?; let display = Display::new()?; // Initialize session @@ -515,21 +518,7 @@ pub fn run_udev(no_config: bool, config_dir: Option) -> anyhow::Result< tracing::error!("Failed to start XWayland: {err}"); } - event_loop.run( - 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(()) + Ok((state, event_loop)) } // TODO: document desperately diff --git a/src/backend/winit.rs b/src/backend/winit.rs index 8ece997..33b6092 100644 --- a/src/backend/winit.rs +++ b/src/backend/winit.rs @@ -62,8 +62,11 @@ impl Backend { } /// Start Pinnacle as a window in a graphical environment. -pub fn run_winit(no_config: bool, config_dir: Option) -> anyhow::Result<()> { - let mut event_loop: EventLoop = EventLoop::try_new()?; +pub fn setup_winit( + no_config: bool, + config_dir: Option, +) -> anyhow::Result<(State, EventLoop<'static, State>)> { + let event_loop: EventLoop = EventLoop::try_new()?; let display: Display = Display::new()?; let display_handle = display.handle(); @@ -237,21 +240,7 @@ pub fn run_winit(no_config: bool, config_dir: Option) -> anyhow::Result anyhow::bail!("Failed to insert winit events into event loop: {err}"); } - event_loop.run( - 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(()) + Ok((state, event_loop)) } impl State { diff --git a/src/cli.rs b/src/cli.rs index 54d9d7b..4173574 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -14,7 +14,7 @@ pub enum Backend { /// The main CLI struct. #[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 { /// Start Pinnacle with the config at this directory #[arg(short, long, value_name("DIR"), value_hint(ValueHint::DirPath))] @@ -50,6 +50,7 @@ pub struct Cli { } impl Cli { + // pub fn parse_and_prompt() -> Option { let mut cli = Cli::parse(); diff --git a/src/main.rs b/src/main.rs index 7b0eb36..2ba2d6c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -97,48 +97,60 @@ async fn main() -> anyhow::Result<()> { 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, _) => { if in_graphical_env { 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 { 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) => { if !in_graphical_env { if force { 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 { 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 really want to, additionally pass in the `--force` flag."); + return Ok(()); } } else { 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) => { if in_graphical_env { if force { 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 { warn!("WAYLAND_DISPLAY and/or DISPLAY are set."); 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!("If you really want to, additionally pass in the `--force` flag."); + return Ok(()); } } else { 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(()) }