pw_utils: Store LoopHandle

This commit is contained in:
Ivan Molodetskikh
2025-08-03 13:29:48 +03:00
parent 8fba696d8e
commit 43577f4d97
2 changed files with 16 additions and 16 deletions
+4 -3
View File
@@ -2029,7 +2029,8 @@ impl State {
let pw = if let Some(pw) = &self.niri.pipewire { let pw = if let Some(pw) = &self.niri.pipewire {
pw pw
} else { } else {
match PipeWire::new(&self.niri.event_loop, self.niri.pw_to_niri.clone()) { match PipeWire::new(self.niri.event_loop.clone(), self.niri.pw_to_niri.clone())
{
Ok(pipewire) => self.niri.pipewire.insert(pipewire), Ok(pipewire) => self.niri.pipewire.insert(pipewire),
Err(err) => { Err(err) => {
warn!( warn!(
@@ -5019,7 +5020,7 @@ impl Niri {
} }
} }
if cast.check_time_and_schedule(&self.event_loop, output, target_presentation_time) { if cast.check_time_and_schedule(output, target_presentation_time) {
continue; continue;
} }
@@ -5085,7 +5086,7 @@ impl Niri {
} }
} }
if cast.check_time_and_schedule(&self.event_loop, output, target_presentation_time) { if cast.check_time_and_schedule(output, target_presentation_time) {
continue; continue;
} }
+12 -13
View File
@@ -57,6 +57,7 @@ pub struct PipeWire {
_context: Context, _context: Context,
pub core: Core, pub core: Core,
pub token: RegistrationToken, pub token: RegistrationToken,
event_loop: LoopHandle<'static, State>,
to_niri: calloop::channel::Sender<PwToNiri>, to_niri: calloop::channel::Sender<PwToNiri>,
} }
@@ -67,6 +68,7 @@ pub enum PwToNiri {
} }
pub struct Cast { pub struct Cast {
event_loop: LoopHandle<'static, State>,
pub session_id: usize, pub session_id: usize,
pub stream_id: usize, pub stream_id: usize,
pub stream: Stream, pub stream: Stream,
@@ -143,7 +145,7 @@ macro_rules! make_params {
impl PipeWire { impl PipeWire {
pub fn new( pub fn new(
event_loop: &LoopHandle<'static, State>, event_loop: LoopHandle<'static, State>,
to_niri: calloop::channel::Sender<PwToNiri>, to_niri: calloop::channel::Sender<PwToNiri>,
) -> anyhow::Result<Self> { ) -> anyhow::Result<Self> {
let main_loop = MainLoop::new(None).context("error creating MainLoop")?; let main_loop = MainLoop::new(None).context("error creating MainLoop")?;
@@ -185,6 +187,7 @@ impl PipeWire {
_context: context, _context: context,
core, core,
token, token,
event_loop,
to_niri, to_niri,
}) })
} }
@@ -666,6 +669,7 @@ impl PipeWire {
.context("error connecting stream")?; .context("error connecting stream")?;
let cast = Cast { let cast = Cast {
event_loop: self.event_loop.clone(),
session_id, session_id,
stream_id, stream_id,
stream, stream,
@@ -784,12 +788,7 @@ impl Cast {
Duration::ZERO Duration::ZERO
} }
fn schedule_redraw( fn schedule_redraw(&mut self, output: Output, target_time: Duration) {
&mut self,
event_loop: &LoopHandle<'static, State>,
output: Output,
target_time: Duration,
) {
if self.scheduled_redraw.is_some() { if self.scheduled_redraw.is_some() {
return; return;
} }
@@ -797,7 +796,8 @@ impl Cast {
let now = get_monotonic_time(); let now = get_monotonic_time();
let duration = target_time.saturating_sub(now); let duration = target_time.saturating_sub(now);
let timer = Timer::from_duration(duration); let timer = Timer::from_duration(duration);
let token = event_loop let token = self
.event_loop
.insert_source(timer, move |_, _, state| { .insert_source(timer, move |_, _, state| {
// Guard against output disconnecting before the timer has a chance to run. // Guard against output disconnecting before the timer has a chance to run.
if state.niri.output_state.contains_key(&output) { if state.niri.output_state.contains_key(&output) {
@@ -810,9 +810,9 @@ impl Cast {
self.scheduled_redraw = Some(token); self.scheduled_redraw = Some(token);
} }
fn remove_scheduled_redraw(&mut self, event_loop: &LoopHandle<'static, State>) { fn remove_scheduled_redraw(&mut self) {
if let Some(token) = self.scheduled_redraw.take() { if let Some(token) = self.scheduled_redraw.take() {
event_loop.remove(token); self.event_loop.remove(token);
} }
} }
@@ -825,17 +825,16 @@ impl Cast {
/// [`Cast::dequeue_buffer_and_render()`]. /// [`Cast::dequeue_buffer_and_render()`].
pub fn check_time_and_schedule( pub fn check_time_and_schedule(
&mut self, &mut self,
event_loop: &LoopHandle<'static, State>,
output: &Output, output: &Output,
target_frame_time: Duration, target_frame_time: Duration,
) -> bool { ) -> bool {
let delay = self.compute_extra_delay(target_frame_time); let delay = self.compute_extra_delay(target_frame_time);
if delay >= CAST_DELAY_ALLOWANCE { if delay >= CAST_DELAY_ALLOWANCE {
trace!("delay >= allowance, scheduling redraw"); trace!("delay >= allowance, scheduling redraw");
self.schedule_redraw(event_loop, output.clone(), target_frame_time + delay); self.schedule_redraw(output.clone(), target_frame_time + delay);
true true
} else { } else {
self.remove_scheduled_redraw(event_loop); self.remove_scheduled_redraw();
false false
} }
} }