From 8dcc41a54d2606bab2d0e950f4b67668b48d9750 Mon Sep 17 00:00:00 2001 From: Ivan Molodetskikh Date: Sun, 22 Dec 2024 11:00:17 +0300 Subject: Initialize PipeWire lazily This helps with: - System setups starting PipeWire late (after niri startup, but before any screencast). - Tests which don't even want to start PipeWire. --- src/niri.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'src/niri.rs') diff --git a/src/niri.rs b/src/niri.rs index 03b4b4e4..d5cb5fdb 100644 --- a/src/niri.rs +++ b/src/niri.rs @@ -1,4 +1,4 @@ -use std::cell::{Cell, OnceCell, RefCell}; +use std::cell::{Cell, LazyCell, OnceCell, RefCell}; use std::collections::{HashMap, HashSet}; use std::ffi::OsString; use std::path::PathBuf; @@ -327,7 +327,7 @@ pub struct Niri { // Casts are dropped before PipeWire to prevent a double-free (yay). pub casts: Vec, - pub pipewire: Option, + pub pipewire: LazyCell, Box Option>>, // Screencast output for each mapped window. #[cfg(feature = "xdp-gnome-screencast")] @@ -1504,13 +1504,15 @@ impl State { let gbm = match self.backend.gbm_device() { Some(gbm) => gbm, None => { - debug!("no GBM device available"); + warn!("error starting screencast: no GBM device available"); + self.niri.stop_cast(session_id); return; } }; - let Some(pw) = &self.niri.pipewire else { - error!("screencasting must be disabled if PipeWire is missing"); + let Some(pw) = &*self.niri.pipewire else { + warn!("error starting screencast: PipeWire failed to initialize"); + self.niri.stop_cast(session_id); return; }; @@ -1882,13 +1884,14 @@ impl Niri { } }; - let pipewire = match PipeWire::new(&event_loop) { + let loop_handle = event_loop.clone(); + let pipewire = LazyCell::new(Box::new(move || match PipeWire::new(&loop_handle) { Ok(pipewire) => Some(pipewire), Err(err) => { warn!("error connecting to PipeWire, screencasting will not work: {err:?}"); None } - }; + }) as _); let display_source = Generic::new(display, Interest::READ, Mode::Level); event_loop -- cgit