screencasting: Extract some logic into functions
Browse files at this commitff081acddc91e90ee3518b38526c4b913b8ec96aParent: afe27a11 changed file +75 -61
- src/screencasting/mod.rs +75 -61
src/screencasting/mod.rsmodified+75 -61
| @@ -3,13 +3,17 @@ use std::collections::{HashMap, HashSet}; | |||
| 3 | use std::mem; | 3 | use std::mem; |
| 4 | use std::time::Duration; | 4 | use std::time::Duration; |
| 5 | 5 | ||
| 6 | +use anyhow::Context as _; | ||
| 6 | use calloop::LoopHandle; | 7 | use calloop::LoopHandle; |
| 8 | +use smithay::backend::allocator::format::FormatSet; | ||
| 9 | +use smithay::backend::allocator::gbm::GbmDevice; | ||
| 10 | +use smithay::backend::drm::DrmDeviceFd; | ||
| 7 | use smithay::backend::renderer::element::utils::{Relocate, RelocateRenderElement}; | 11 | use smithay::backend::renderer::element::utils::{Relocate, RelocateRenderElement}; |
| 8 | use smithay::backend::renderer::gles::GlesRenderer; | 12 | use smithay::backend::renderer::gles::GlesRenderer; |
| 9 | use smithay::desktop::Window; | 13 | use smithay::desktop::Window; |
| 10 | use smithay::output::Output; | 14 | use smithay::output::Output; |
| 11 | use smithay::reexports::gbm::Modifier; | 15 | use smithay::reexports::gbm::Modifier; |
| 12 | -use smithay::utils::{Point, Scale, Size}; | 16 | +use smithay::utils::{Physical, Point, Scale, Size}; |
| 13 | 17 | ||
| 14 | use crate::dbus::mutter_screen_cast::{self, ScreenCastToNiri, StreamTargetId}; | 18 | use crate::dbus::mutter_screen_cast::{self, ScreenCastToNiri, StreamTargetId}; |
| 15 | use crate::niri::{CastTarget, Niri, OutputRenderElements, PointerRenderElements, State}; | 19 | use crate::niri::{CastTarget, Niri, OutputRenderElements, PointerRenderElements, State}; |
| @@ -59,6 +63,42 @@ impl Screencasting { | |||
| 59 | } | 63 | } |
| 60 | 64 | ||
| 61 | impl State { | 65 | impl State { |
| 66 | + fn prepare_pw_cast(&mut self) -> anyhow::Result<(GbmDevice<DrmDeviceFd>, FormatSet)> { | ||
| 67 | + let gbm = self | ||
| 68 | + .backend | ||
| 69 | + .gbm_device() | ||
| 70 | + .context("no GBM device available")?; | ||
| 71 | + | ||
| 72 | + // Ensure PipeWire is initialized. | ||
| 73 | + if self.niri.casting.pipewire.is_none() { | ||
| 74 | + let pw = PipeWire::new( | ||
| 75 | + self.niri.event_loop.clone(), | ||
| 76 | + self.niri.casting.pw_to_niri.clone(), | ||
| 77 | + ) | ||
| 78 | + .context("error initializing PipeWire")?; | ||
| 79 | + self.niri.casting.pipewire = Some(pw); | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + let mut render_formats = self | ||
| 83 | + .backend | ||
| 84 | + .with_primary_renderer(|renderer| { | ||
| 85 | + renderer.egl_context().dmabuf_render_formats().clone() | ||
| 86 | + }) | ||
| 87 | + .unwrap_or_default(); | ||
| 88 | + | ||
| 89 | + { | ||
| 90 | + let config = self.niri.config.borrow(); | ||
| 91 | + if config.debug.force_pipewire_invalid_modifier { | ||
| 92 | + render_formats = render_formats | ||
| 93 | + .into_iter() | ||
| 94 | + .filter(|f| f.modifier == Modifier::Invalid) | ||
| 95 | + .collect(); | ||
| 96 | + } | ||
| 97 | + } | ||
| 98 | + | ||
| 99 | + Ok((gbm, render_formats)) | ||
| 100 | + } | ||
| 101 | + | ||
| 62 | pub fn on_pw_msg(&mut self, msg: PwToNiri) { | 102 | pub fn on_pw_msg(&mut self, msg: PwToNiri) { |
| 63 | match msg { | 103 | match msg { |
| 64 | PwToNiri::StopCast { session_id } => self.niri.stop_cast(session_id), | 104 | PwToNiri::StopCast { session_id } => self.niri.stop_cast(session_id), |
| @@ -252,30 +292,6 @@ impl State { | |||
| 252 | 292 | ||
| 253 | debug!(session_id, stream_id, "StartCast"); | 293 | debug!(session_id, stream_id, "StartCast"); |
| 254 | 294 | ||
| 255 | - let Some(gbm) = self.backend.gbm_device() else { | ||
| 256 | - warn!("error starting screencast: no GBM device available"); | ||
| 257 | - self.niri.stop_cast(session_id); | ||
| 258 | - return; | ||
| 259 | - }; | ||
| 260 | - | ||
| 261 | - let pw = if let Some(pw) = &self.niri.casting.pipewire { | ||
| 262 | - pw | ||
| 263 | - } else { | ||
| 264 | - match PipeWire::new( | ||
| 265 | - self.niri.event_loop.clone(), | ||
| 266 | - self.niri.casting.pw_to_niri.clone(), | ||
| 267 | - ) { | ||
| 268 | - Ok(pipewire) => self.niri.casting.pipewire.insert(pipewire), | ||
| 269 | - Err(err) => { | ||
| 270 | - warn!( | ||
| 271 | - "error starting screencast: PipeWire failed to initialize: {err:?}" | ||
| 272 | - ); | ||
| 273 | - self.niri.stop_cast(session_id); | ||
| 274 | - return; | ||
| 275 | - } | ||
| 276 | - } | ||
| 277 | - }; | ||
| 278 | - | ||
| 279 | let mut dynamic_target = false; | 295 | let mut dynamic_target = false; |
| 280 | let (target, size, refresh, alpha) = match target { | 296 | let (target, size, refresh, alpha) = match target { |
| 281 | StreamTargetId::Output { name } => { | 297 | StreamTargetId::Output { name } => { |
| @@ -287,10 +303,7 @@ impl State { | |||
| 287 | return; | 303 | return; |
| 288 | }; | 304 | }; |
| 289 | 305 | ||
| 290 | - let mode = output.current_mode().unwrap(); | 306 | + let (size, refresh) = cast_params_for_output(output); |
| 291 | - let transform = output.current_transform(); | ||
| 292 | - let size = transform.transform_size(mode.size); | ||
| 293 | - let refresh = mode.refresh as u32; | ||
| 294 | (CastTarget::Output(output.downgrade()), size, refresh, false) | 307 | (CastTarget::Output(output.downgrade()), size, refresh, false) |
| 295 | } | 308 | } |
| 296 | StreamTargetId::Window { id } | 309 | StreamTargetId::Window { id } |
| @@ -303,46 +316,24 @@ impl State { | |||
| 303 | (CastTarget::Nothing, Size::from((1, 1)), 1000, true) | 316 | (CastTarget::Nothing, Size::from((1, 1)), 1000, true) |
| 304 | } | 317 | } |
| 305 | StreamTargetId::Window { id } => { | 318 | StreamTargetId::Window { id } => { |
| 306 | - let Some(window) = self.niri.layout.windows().find_map(|(_, mapped)| { | 319 | + let Some((size, refresh)) = self.niri.cast_params_for_window(id) else { |
| 307 | - (mapped.id().get() == id).then_some(&mapped.window) | ||
| 308 | - }) else { | ||
| 309 | warn!("error starting screencast: requested window is missing"); | 320 | warn!("error starting screencast: requested window is missing"); |
| 310 | self.niri.stop_cast(session_id); | 321 | self.niri.stop_cast(session_id); |
| 311 | return; | 322 | return; |
| 312 | }; | 323 | }; |
| 313 | - | 324 | + (CastTarget::Window { id }, size, refresh, true) |
| 314 | - // Use the cached output since it will be present even if the output was | ||
| 315 | - // currently disconnected. | ||
| 316 | - let Some(output) = self.niri.casting.mapped_cast_output.get(window) else { | ||
| 317 | - warn!("error starting screencast: requested window is missing"); | ||
| 318 | - self.niri.stop_cast(session_id); | ||
| 319 | - return; | ||
| 320 | - }; | ||
| 321 | - | ||
| 322 | - let scale = Scale::from(output.current_scale().fractional_scale()); | ||
| 323 | - let bbox = window.bbox_with_popups().to_physical_precise_up(scale); | ||
| 324 | - let refresh = output.current_mode().unwrap().refresh as u32; | ||
| 325 | - | ||
| 326 | - (CastTarget::Window { id }, bbox.size, refresh, true) | ||
| 327 | } | 325 | } |
| 328 | }; | 326 | }; |
| 329 | 327 | ||
| 330 | - let mut render_formats = self | 328 | + let (gbm, render_formats) = match self.prepare_pw_cast() { |
| 331 | - .backend | 329 | + Ok(x) => x, |
| 332 | - .with_primary_renderer(|renderer| { | 330 | + Err(err) => { |
| 333 | - renderer.egl_context().dmabuf_render_formats().clone() | 331 | + warn!("error starting screencast: {err:?}"); |
| 334 | - }) | 332 | + self.niri.stop_cast(session_id); |
| 335 | - .unwrap_or_default(); | 333 | + return; |
| 336 | - | ||
| 337 | - { | ||
| 338 | - let config = self.niri.config.borrow(); | ||
| 339 | - if config.debug.force_pipewire_invalid_modifier { | ||
| 340 | - render_formats = render_formats | ||
| 341 | - .into_iter() | ||
| 342 | - .filter(|f| f.modifier == Modifier::Invalid) | ||
| 343 | - .collect(); | ||
| 344 | } | 334 | } |
| 345 | - } | 335 | + }; |
| 336 | + let pw = self.niri.casting.pipewire.as_ref().unwrap(); | ||
| 346 | 337 | ||
| 347 | let res = pw.start_cast( | 338 | let res = pw.start_cast( |
| 348 | gbm, | 339 | gbm, |
| @@ -662,6 +653,29 @@ impl Niri { | |||
| 662 | .insert_idle(|state| state.set_dynamic_cast_target(CastTarget::Nothing)); | 653 | .insert_idle(|state| state.set_dynamic_cast_target(CastTarget::Nothing)); |
| 663 | } | 654 | } |
| 664 | } | 655 | } |
| 656 | + | ||
| 657 | + fn cast_params_for_window(&self, window_id: u64) -> Option<(Size<i32, Physical>, u32)> { | ||
| 658 | + let (_, mapped) = self | ||
| 659 | + .layout | ||
| 660 | + .windows() | ||
| 661 | + .find(|(_, m)| m.id().get() == window_id)?; | ||
| 662 | + let output = self.casting.mapped_cast_output.get(&mapped.window)?; | ||
| 663 | + let scale = Scale::from(output.current_scale().fractional_scale()); | ||
| 664 | + let bbox = mapped | ||
| 665 | + .window | ||
| 666 | + .bbox_with_popups() | ||
| 667 | + .to_physical_precise_up(scale); | ||
| 668 | + let refresh = output.current_mode().unwrap().refresh as u32; | ||
| 669 | + Some((bbox.size, refresh)) | ||
| 670 | + } | ||
| 671 | +} | ||
| 672 | + | ||
| 673 | +fn cast_params_for_output(output: &Output) -> (Size<i32, Physical>, u32) { | ||
| 674 | + let mode = output.current_mode().unwrap(); | ||
| 675 | + let transform = output.current_transform(); | ||
| 676 | + let size = transform.transform_size(mode.size); | ||
| 677 | + let refresh = mode.refresh as u32; | ||
| 678 | + (size, refresh) | ||
| 665 | } | 679 | } |
| 666 | 680 | ||
| 667 | niri_render_elements! { | 681 | niri_render_elements! { |