niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·b0eb8ad·Ivan Molodetskikh·3 months ago·2857 commits
| 1 | use std::collections::hash_map::Entry; |
| 2 | use std::collections::{HashMap, HashSet}; |
| 3 | use std::mem; |
| 4 | use std::time::Duration; |
| 5 | |
| 6 | use anyhow::Context as _; |
| 7 | use calloop::LoopHandle; |
| 8 | use smithay::backend::allocator::format::FormatSet; |
| 9 | use smithay::backend::allocator::gbm::GbmDevice; |
| 10 | use smithay::backend::drm::DrmDeviceFd; |
| 11 | use smithay::backend::renderer::element::utils::{Relocate, RelocateRenderElement}; |
| 12 | use smithay::backend::renderer::gles::GlesRenderer; |
| 13 | use smithay::desktop::Window; |
| 14 | use smithay::output::Output; |
| 15 | use smithay::reexports::gbm::Modifier; |
| 16 | use smithay::utils::{Physical, Point, Scale, Size}; |
| 17 | use zbus::object_server::SignalEmitter; |
| 18 | |
| 19 | use crate::dbus::mutter_screen_cast::{self, CursorMode, ScreenCastToNiri, StreamTargetId}; |
| 20 | use crate::niri::{CastTarget, Niri, OutputRenderElements, PointerRenderElements, State}; |
| 21 | use crate::niri_render_elements; |
| 22 | use crate::render_helpers::{RenderCtx, RenderTarget}; |
| 23 | use crate::utils::{get_monotonic_time, CastSessionId, CastStreamId}; |
| 24 | use crate::window::mapped::{MappedId, WindowCastRenderElements}; |
| 25 | |
| 26 | mod pw_utils; |
| 27 | use pw_utils::{Cast, CastSizeChange, CursorData, PipeWire, PwToNiri}; |
| 28 | |
| 29 | pub struct Screencasting { |
| 30 | pub casts: Vec<Cast>, |
| 31 | |
| 32 | /// Dynamic-target casts waiting for their first target to start. |
| 33 | pub pending_dynamic_casts: Vec<PendingCast>, |
| 34 | |
| 35 | pub pw_to_niri: calloop::channel::Sender<PwToNiri>, |
| 36 | |
| 37 | /// Screencast output for each mapped window. |
| 38 | pub mapped_cast_output: HashMap<Window, Output>, |
| 39 | |
| 40 | /// Window ID for the "dynamic cast" special window for the xdp-gnome picker. |
| 41 | pub dynamic_cast_id_for_portal: MappedId, |
| 42 | |
| 43 | // Drop PipeWire last, and specifically after casts, to prevent a double-free (yay). |
| 44 | pub pipewire: Option<PipeWire>, |
| 45 | } |
| 46 | |
| 47 | /// A screencast request that hasn't been started yet. |
| 48 | pub struct PendingCast { |
| 49 | pub session_id: CastSessionId, |
| 50 | pub stream_id: CastStreamId, |
| 51 | pub cursor_mode: CursorMode, |
| 52 | pub signal_ctx: SignalEmitter<'static>, |
| 53 | } |
| 54 | |
| 55 | impl Screencasting { |
| 56 | pub fn new(event_loop: &LoopHandle<'static, State>) -> Self { |
| 57 | let pw_to_niri = { |
| 58 | let (pw_to_niri, from_pipewire) = calloop::channel::channel(); |
| 59 | event_loop |
| 60 | .insert_source(from_pipewire, move |event, _, state| match event { |
| 61 | calloop::channel::Event::Msg(msg) => state.on_pw_msg(msg), |
| 62 | calloop::channel::Event::Closed => (), |
| 63 | }) |
| 64 | .unwrap(); |
| 65 | pw_to_niri |
| 66 | }; |
| 67 | |
| 68 | Self { |
| 69 | casts: vec![], |
| 70 | pending_dynamic_casts: vec![], |
| 71 | pw_to_niri, |
| 72 | mapped_cast_output: HashMap::new(), |
| 73 | dynamic_cast_id_for_portal: MappedId::next(), |
| 74 | pipewire: None, |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | impl State { |
| 80 | fn prepare_pw_cast(&mut self) -> anyhow::Result<(GbmDevice<DrmDeviceFd>, FormatSet)> { |
| 81 | let gbm = self |
| 82 | .backend |
| 83 | .gbm_device() |
| 84 | .context("no GBM device available")?; |
| 85 | |
| 86 | // Ensure PipeWire is initialized. |
| 87 | if self.niri.casting.pipewire.is_none() { |
| 88 | let pw = PipeWire::new( |
| 89 | self.niri.event_loop.clone(), |
| 90 | self.niri.casting.pw_to_niri.clone(), |
| 91 | ) |
| 92 | .context("error initializing PipeWire")?; |
| 93 | self.niri.casting.pipewire = Some(pw); |
| 94 | } |
| 95 | |
| 96 | let mut render_formats = self |
| 97 | .backend |
| 98 | .with_primary_renderer(|renderer| { |
| 99 | renderer.egl_context().dmabuf_render_formats().clone() |
| 100 | }) |
| 101 | .unwrap_or_default(); |
| 102 | |
| 103 | { |
| 104 | let config = self.niri.config.borrow(); |
| 105 | if config.debug.force_pipewire_invalid_modifier { |
| 106 | render_formats = render_formats |
| 107 | .into_iter() |
| 108 | .filter(|f| f.modifier == Modifier::Invalid) |
| 109 | .collect(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | Ok((gbm, render_formats)) |
| 114 | } |
| 115 | |
| 116 | pub fn on_pw_msg(&mut self, msg: PwToNiri) { |
| 117 | match msg { |
| 118 | PwToNiri::StopCast { session_id } => self.niri.stop_cast(session_id), |
| 119 | PwToNiri::Redraw { stream_id } => self.redraw_cast(stream_id), |
| 120 | PwToNiri::FatalError => { |
| 121 | warn!("stopping PipeWire due to fatal error"); |
| 122 | let casting = &mut self.niri.casting; |
| 123 | if let Some(pw) = casting.pipewire.take() { |
| 124 | let mut ids = HashSet::new(); |
| 125 | for cast in &casting.pending_dynamic_casts { |
| 126 | ids.insert(cast.session_id); |
| 127 | } |
| 128 | for cast in &casting.casts { |
| 129 | ids.insert(cast.session_id); |
| 130 | } |
| 131 | for id in ids { |
| 132 | self.niri.stop_cast(id); |
| 133 | } |
| 134 | self.niri.event_loop.remove(pw.token); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | fn redraw_cast(&mut self, stream_id: CastStreamId) { |
| 141 | let _span = tracy_client::span!("State::redraw_cast"); |
| 142 | |
| 143 | let casts = &mut self.niri.casting.casts; |
| 144 | let Some(idx) = casts.iter().position(|cast| cast.stream_id == stream_id) else { |
| 145 | warn!("cast to redraw is missing"); |
| 146 | return; |
| 147 | }; |
| 148 | let cast = &mut casts[idx]; |
| 149 | |
| 150 | let id = match &cast.target { |
| 151 | CastTarget::Nothing => { |
| 152 | self.backend.with_primary_renderer(|renderer| { |
| 153 | if cast.dequeue_buffer_and_clear(renderer) { |
| 154 | cast.last_frame_time = get_monotonic_time(); |
| 155 | } |
| 156 | }); |
| 157 | return; |
| 158 | } |
| 159 | CastTarget::Output { output, .. } => { |
| 160 | if let Some(output) = output.upgrade() { |
| 161 | self.niri.queue_redraw(&output); |
| 162 | } |
| 163 | return; |
| 164 | } |
| 165 | CastTarget::Window { id } => *id, |
| 166 | }; |
| 167 | |
| 168 | // Lack of partial borrowing strikes again... |
| 169 | let mut casts = mem::take(&mut self.niri.casting.casts); |
| 170 | let cast = &mut casts[idx]; |
| 171 | let mut stop = false; |
| 172 | // Use a loop {} so we can break instead of early-return. |
| 173 | #[allow(clippy::never_loop)] |
| 174 | loop { |
| 175 | let mut windows = self.niri.layout.windows(); |
| 176 | let Some((_, mapped)) = windows.find(|(_, mapped)| mapped.id().get() == id) else { |
| 177 | break; |
| 178 | }; |
| 179 | |
| 180 | // Use the cached output since it will be present even if the output was |
| 181 | // currently disconnected. |
| 182 | let Some(output) = self.niri.casting.mapped_cast_output.get(&mapped.window) else { |
| 183 | break; |
| 184 | }; |
| 185 | |
| 186 | let scale = Scale::from(output.current_scale().fractional_scale()); |
| 187 | let bbox = mapped |
| 188 | .window |
| 189 | .bbox_with_popups() |
| 190 | .to_physical_precise_up(scale); |
| 191 | |
| 192 | match cast.ensure_size(bbox.size) { |
| 193 | Ok(CastSizeChange::Ready) => (), |
| 194 | Ok(CastSizeChange::Pending) => break, |
| 195 | Err(err) => { |
| 196 | warn!("error updating stream size, stopping screencast: {err:?}"); |
| 197 | stop = true; |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | self.backend.with_primary_renderer(|renderer| { |
| 203 | let mut elements = Vec::new(); |
| 204 | let mut pointer_location = Point::default(); |
| 205 | |
| 206 | if self.niri.pointer_visibility.is_visible() { |
| 207 | if let Some((pointer_pos, win_pos)) = |
| 208 | self.niri.pointer_pos_for_window_cast(mapped) |
| 209 | { |
| 210 | // Pointer location must be relative to the screencast buffer. |
| 211 | // - win_pos is the position of the main window surface in output-local |
| 212 | // coordinates |
| 213 | // - bbox.loc moves us relative to the screencast buffer |
| 214 | let buf_pos = win_pos + bbox.loc.to_f64().to_logical(scale); |
| 215 | let output_pos = |
| 216 | self.niri.global_space.output_geometry(output).unwrap().loc; |
| 217 | pointer_location = pointer_pos - output_pos.to_f64() - buf_pos; |
| 218 | |
| 219 | let pos = buf_pos.to_physical_precise_round(scale).upscale(-1); |
| 220 | self.niri.render_pointer(renderer, output, &mut |elem| { |
| 221 | let elem = |
| 222 | RelocateRenderElement::from_element(elem, pos, Relocate::Relative); |
| 223 | elements.push(CastRenderElement::from(elem)); |
| 224 | }); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | let main_start = elements.len(); |
| 229 | mapped.render_for_screen_cast(renderer, scale, &mut |elem| { |
| 230 | elements.push(CastRenderElement::from(elem)) |
| 231 | }); |
| 232 | |
| 233 | let cursor_data = |
| 234 | CursorData::compute(&elements, main_start, pointer_location, scale); |
| 235 | |
| 236 | if cast.dequeue_buffer_and_render( |
| 237 | renderer, |
| 238 | &elements, |
| 239 | &cursor_data, |
| 240 | bbox.size, |
| 241 | scale, |
| 242 | ) { |
| 243 | cast.last_frame_time = get_monotonic_time(); |
| 244 | } |
| 245 | }); |
| 246 | |
| 247 | break; |
| 248 | } |
| 249 | let session_id = cast.session_id; |
| 250 | self.niri.casting.casts = casts; |
| 251 | |
| 252 | if stop { |
| 253 | self.niri.stop_cast(session_id); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | pub fn set_dynamic_cast_target(&mut self, target: CastTarget) { |
| 258 | let _span = tracy_client::span!("State::set_dynamic_cast_target"); |
| 259 | |
| 260 | let mut refresh = None; |
| 261 | match &target { |
| 262 | // Leave refresh as is when clearing. Chances are, the next refresh will match it, |
| 263 | // then we'll avoid reconfiguring. |
| 264 | CastTarget::Nothing => (), |
| 265 | CastTarget::Output { output, .. } => { |
| 266 | if let Some(output) = output.upgrade() { |
| 267 | refresh = Some(output.current_mode().unwrap().refresh as u32); |
| 268 | } |
| 269 | } |
| 270 | CastTarget::Window { id } => { |
| 271 | let mut windows = self.niri.layout.windows(); |
| 272 | if let Some((_, mapped)) = windows.find(|(_, mapped)| mapped.id().get() == *id) { |
| 273 | if let Some(output) = self.niri.casting.mapped_cast_output.get(&mapped.window) { |
| 274 | refresh = Some(output.current_mode().unwrap().refresh as u32); |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | let mut to_redraw = Vec::new(); |
| 281 | let mut to_stop = Vec::new(); |
| 282 | for cast in &mut self.niri.casting.casts { |
| 283 | if !cast.dynamic_target { |
| 284 | continue; |
| 285 | } |
| 286 | |
| 287 | if let Some(refresh) = refresh { |
| 288 | if let Err(err) = cast.set_refresh(refresh) { |
| 289 | warn!("error changing cast FPS: {err:?}"); |
| 290 | to_stop.push(cast.session_id); |
| 291 | continue; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | cast.target = target.clone(); |
| 296 | to_redraw.push(cast.stream_id); |
| 297 | } |
| 298 | |
| 299 | for id in to_redraw { |
| 300 | self.redraw_cast(id); |
| 301 | } |
| 302 | |
| 303 | // Start any pending dynamic casts if we have a real target. |
| 304 | if !matches!(target, CastTarget::Nothing) { |
| 305 | self.start_pending_dynamic_casts(&target); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | fn start_pending_dynamic_casts(&mut self, target: &CastTarget) { |
| 310 | let pending = &self.niri.casting.pending_dynamic_casts; |
| 311 | if pending.is_empty() { |
| 312 | return; |
| 313 | } |
| 314 | debug!("starting {} pending dynamic cast(s)", pending.len()); |
| 315 | |
| 316 | let _span = tracy_client::span!("State::start_pending_dynamic_casts"); |
| 317 | |
| 318 | // We don't stop dynamic casts on missing output/window. |
| 319 | let (size, refresh) = match target { |
| 320 | CastTarget::Nothing => panic!("dynamic cast starting target must not be Nothing"), |
| 321 | CastTarget::Output { output, .. } => { |
| 322 | let Some(output) = output.upgrade() else { |
| 323 | return; |
| 324 | }; |
| 325 | cast_params_for_output(&output) |
| 326 | } |
| 327 | CastTarget::Window { id } => { |
| 328 | let Some((size, refresh)) = self.niri.cast_params_for_window(*id) else { |
| 329 | return; |
| 330 | }; |
| 331 | (size, refresh) |
| 332 | } |
| 333 | }; |
| 334 | |
| 335 | let (gbm, render_formats) = match self.prepare_pw_cast() { |
| 336 | Ok(x) => x, |
| 337 | Err(err) => { |
| 338 | warn!("error starting pending screencasts: {err:?}"); |
| 339 | let mut ids = HashSet::new(); |
| 340 | for pending in self.niri.casting.pending_dynamic_casts.drain(..) { |
| 341 | ids.insert(pending.session_id); |
| 342 | } |
| 343 | for id in ids { |
| 344 | self.niri.stop_cast(id); |
| 345 | } |
| 346 | return; |
| 347 | } |
| 348 | }; |
| 349 | let pw = self.niri.casting.pipewire.as_ref().unwrap(); |
| 350 | |
| 351 | // Alpha is always true since the dynamic target can change between window & output. |
| 352 | let alpha = true; |
| 353 | |
| 354 | // Start each pending cast. |
| 355 | let mut to_stop = HashSet::new(); |
| 356 | for pending in self.niri.casting.pending_dynamic_casts.drain(..) { |
| 357 | let res = pw.start_cast( |
| 358 | gbm.clone(), |
| 359 | render_formats.clone(), |
| 360 | pending.session_id, |
| 361 | pending.stream_id, |
| 362 | target.clone(), |
| 363 | size, |
| 364 | refresh, |
| 365 | alpha, |
| 366 | pending.cursor_mode, |
| 367 | pending.signal_ctx, |
| 368 | ); |
| 369 | match res { |
| 370 | Ok(mut cast) => { |
| 371 | cast.dynamic_target = true; |
| 372 | self.niri.casting.casts.push(cast); |
| 373 | } |
| 374 | Err(err) => { |
| 375 | warn!("error starting pending screencast: {err:?}"); |
| 376 | to_stop.insert(pending.session_id); |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | for session_id in to_stop { |
| 382 | self.niri.stop_cast(session_id); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | pub fn on_screen_cast_msg(&mut self, msg: ScreenCastToNiri) { |
| 387 | match msg { |
| 388 | ScreenCastToNiri::StartCast { |
| 389 | session_id, |
| 390 | stream_id, |
| 391 | target, |
| 392 | cursor_mode, |
| 393 | signal_ctx, |
| 394 | } => { |
| 395 | let _span = tracy_client::span!("StartCast"); |
| 396 | let _span = debug_span!("StartCast", %session_id, %stream_id).entered(); |
| 397 | |
| 398 | let (target, size, refresh, alpha) = match target { |
| 399 | StreamTargetId::Output { name } => { |
| 400 | let global_space = &self.niri.global_space; |
| 401 | let output = global_space.outputs().find(|out| out.name() == name); |
| 402 | let Some(output) = output else { |
| 403 | warn!("error starting screencast: requested output is missing"); |
| 404 | self.niri.stop_cast(session_id); |
| 405 | return; |
| 406 | }; |
| 407 | |
| 408 | let (size, refresh) = cast_params_for_output(output); |
| 409 | (CastTarget::output(output), size, refresh, false) |
| 410 | } |
| 411 | StreamTargetId::Window { id } |
| 412 | if id == self.niri.casting.dynamic_cast_id_for_portal.get() => |
| 413 | { |
| 414 | debug!("delaying dynamic cast until target is set"); |
| 415 | self.niri.casting.pending_dynamic_casts.push(PendingCast { |
| 416 | session_id, |
| 417 | stream_id, |
| 418 | cursor_mode, |
| 419 | signal_ctx, |
| 420 | }); |
| 421 | return; |
| 422 | } |
| 423 | StreamTargetId::Window { id } => { |
| 424 | let Some((size, refresh)) = self.niri.cast_params_for_window(id) else { |
| 425 | warn!("error starting screencast: requested window is missing"); |
| 426 | self.niri.stop_cast(session_id); |
| 427 | return; |
| 428 | }; |
| 429 | (CastTarget::Window { id }, size, refresh, true) |
| 430 | } |
| 431 | }; |
| 432 | |
| 433 | let (gbm, render_formats) = match self.prepare_pw_cast() { |
| 434 | Ok(x) => x, |
| 435 | Err(err) => { |
| 436 | warn!("error starting screencast: {err:?}"); |
| 437 | self.niri.stop_cast(session_id); |
| 438 | return; |
| 439 | } |
| 440 | }; |
| 441 | let pw = self.niri.casting.pipewire.as_ref().unwrap(); |
| 442 | |
| 443 | let res = pw.start_cast( |
| 444 | gbm, |
| 445 | render_formats, |
| 446 | session_id, |
| 447 | stream_id, |
| 448 | target, |
| 449 | size, |
| 450 | refresh, |
| 451 | alpha, |
| 452 | cursor_mode, |
| 453 | signal_ctx, |
| 454 | ); |
| 455 | match res { |
| 456 | Ok(cast) => { |
| 457 | self.niri.casting.casts.push(cast); |
| 458 | } |
| 459 | Err(err) => { |
| 460 | warn!("error starting screencast: {err:?}"); |
| 461 | self.niri.stop_cast(session_id); |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | ScreenCastToNiri::StopCast { session_id } => self.niri.stop_cast(session_id), |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | impl Niri { |
| 471 | pub fn refresh_mapped_cast_window_rules(&mut self) { |
| 472 | // O(N^2) but should be fine since there aren't many casts usually. |
| 473 | self.layout.with_windows_mut(|mapped, _| { |
| 474 | let id = mapped.id().get(); |
| 475 | // Find regardless of cast.is_active. |
| 476 | let value = self |
| 477 | .casting |
| 478 | .casts |
| 479 | .iter() |
| 480 | .any(|cast| cast.target == (CastTarget::Window { id })); |
| 481 | mapped.set_is_window_cast_target(value); |
| 482 | }); |
| 483 | } |
| 484 | |
| 485 | pub fn refresh_mapped_cast_outputs(&mut self) { |
| 486 | let mut seen = HashSet::new(); |
| 487 | let mut output_changed = vec![]; |
| 488 | |
| 489 | self.layout.with_windows(|mapped, output, _, _| { |
| 490 | seen.insert(mapped.window.clone()); |
| 491 | |
| 492 | let Some(output) = output else { |
| 493 | return; |
| 494 | }; |
| 495 | |
| 496 | match self.casting.mapped_cast_output.entry(mapped.window.clone()) { |
| 497 | Entry::Occupied(mut entry) => { |
| 498 | if entry.get() != output { |
| 499 | entry.insert(output.clone()); |
| 500 | output_changed.push((mapped.id(), output.clone())); |
| 501 | } |
| 502 | } |
| 503 | Entry::Vacant(entry) => { |
| 504 | entry.insert(output.clone()); |
| 505 | } |
| 506 | } |
| 507 | }); |
| 508 | |
| 509 | self.casting |
| 510 | .mapped_cast_output |
| 511 | .retain(|win, _| seen.contains(win)); |
| 512 | |
| 513 | let mut to_stop = vec![]; |
| 514 | for (id, out) in output_changed { |
| 515 | let refresh = out.current_mode().unwrap().refresh as u32; |
| 516 | let target = CastTarget::Window { id: id.get() }; |
| 517 | for cast in self |
| 518 | .casting |
| 519 | .casts |
| 520 | .iter_mut() |
| 521 | .filter(|cast| cast.target == target) |
| 522 | { |
| 523 | if let Err(err) = cast.set_refresh(refresh) { |
| 524 | warn!("error changing cast FPS: {err:?}"); |
| 525 | to_stop.push(cast.session_id); |
| 526 | }; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | for session_id in to_stop { |
| 531 | self.stop_cast(session_id); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | pub fn render_for_screen_cast( |
| 536 | &mut self, |
| 537 | renderer: &mut GlesRenderer, |
| 538 | output: &Output, |
| 539 | target_presentation_time: Duration, |
| 540 | ) { |
| 541 | let _span = tracy_client::span!("Niri::render_for_screen_cast"); |
| 542 | |
| 543 | let weak = output.downgrade(); |
| 544 | let size = output.current_mode().unwrap().size; |
| 545 | let transform = output.current_transform(); |
| 546 | let size = transform.transform_size(size); |
| 547 | |
| 548 | let scale = Scale::from(output.current_scale().fractional_scale()); |
| 549 | |
| 550 | let mut elements = Vec::new(); |
| 551 | let mut cursor_data = None; |
| 552 | |
| 553 | let mut casts_to_stop = vec![]; |
| 554 | |
| 555 | let mut casts = mem::take(&mut self.casting.casts); |
| 556 | for cast in &mut casts { |
| 557 | if !cast.is_active() { |
| 558 | continue; |
| 559 | } |
| 560 | |
| 561 | if !cast.target.matches_output(&weak) { |
| 562 | continue; |
| 563 | } |
| 564 | |
| 565 | match cast.ensure_size(size) { |
| 566 | Ok(CastSizeChange::Ready) => (), |
| 567 | Ok(CastSizeChange::Pending) => continue, |
| 568 | Err(err) => { |
| 569 | warn!("error updating stream size, stopping screencast: {err:?}"); |
| 570 | casts_to_stop.push(cast.session_id); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | if cast.check_time_and_schedule(output, target_presentation_time) { |
| 575 | continue; |
| 576 | } |
| 577 | |
| 578 | if cursor_data.is_none() { |
| 579 | let mut pointer_pos = Point::default(); |
| 580 | if self.pointer_visibility.is_visible() { |
| 581 | let output_geo = self.global_space.output_geometry(output).unwrap().to_f64(); |
| 582 | let pointer_loc = self |
| 583 | .tablet_cursor_location |
| 584 | .unwrap_or_else(|| self.seat.get_pointer().unwrap().current_location()); |
| 585 | // Only render when the pointer is within the output. Otherwise, it will |
| 586 | // happily appear anywhere outside the output video source in OBS. |
| 587 | if output_geo.contains(pointer_loc) { |
| 588 | pointer_pos = pointer_loc - output_geo.loc; |
| 589 | self.render_pointer(renderer, output, &mut |elem| { |
| 590 | elements.push(elem.into()) |
| 591 | }); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | let main_start = elements.len(); |
| 596 | let ctx = RenderCtx { |
| 597 | renderer, |
| 598 | target: RenderTarget::Screencast, |
| 599 | xray: None, |
| 600 | }; |
| 601 | self.render(ctx, output, false, &mut |elem| elements.push(elem.into())); |
| 602 | |
| 603 | cursor_data = Some(CursorData::compute( |
| 604 | &elements, |
| 605 | main_start, |
| 606 | pointer_pos, |
| 607 | scale, |
| 608 | )); |
| 609 | } |
| 610 | let cursor_data = cursor_data.as_ref().unwrap(); |
| 611 | |
| 612 | if cast.dequeue_buffer_and_render(renderer, &elements, cursor_data, size, scale) { |
| 613 | cast.last_frame_time = target_presentation_time; |
| 614 | } |
| 615 | } |
| 616 | self.casting.casts = casts; |
| 617 | |
| 618 | for id in casts_to_stop { |
| 619 | self.stop_cast(id); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | pub fn render_windows_for_screen_cast( |
| 624 | &mut self, |
| 625 | renderer: &mut GlesRenderer, |
| 626 | output: &Output, |
| 627 | target_presentation_time: Duration, |
| 628 | ) { |
| 629 | let _span = tracy_client::span!("Niri::render_windows_for_screen_cast"); |
| 630 | |
| 631 | let scale = Scale::from(output.current_scale().fractional_scale()); |
| 632 | |
| 633 | let mut casts_to_stop = vec![]; |
| 634 | |
| 635 | let mut casts = mem::take(&mut self.casting.casts); |
| 636 | for cast in &mut casts { |
| 637 | if !cast.is_active() { |
| 638 | continue; |
| 639 | } |
| 640 | |
| 641 | let CastTarget::Window { id } = cast.target else { |
| 642 | continue; |
| 643 | }; |
| 644 | |
| 645 | let mut windows = self.layout.windows_for_output(output); |
| 646 | let Some(mapped) = windows.find(|win| win.id().get() == id) else { |
| 647 | continue; |
| 648 | }; |
| 649 | |
| 650 | let bbox = mapped |
| 651 | .window |
| 652 | .bbox_with_popups() |
| 653 | .to_physical_precise_up(scale); |
| 654 | |
| 655 | match cast.ensure_size(bbox.size) { |
| 656 | Ok(CastSizeChange::Ready) => (), |
| 657 | Ok(CastSizeChange::Pending) => continue, |
| 658 | Err(err) => { |
| 659 | warn!("error updating stream size, stopping screencast: {err:?}"); |
| 660 | casts_to_stop.push(cast.session_id); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | if cast.check_time_and_schedule(output, target_presentation_time) { |
| 665 | continue; |
| 666 | } |
| 667 | |
| 668 | let mut elements = Vec::new(); |
| 669 | let mut pointer_location = Point::default(); |
| 670 | |
| 671 | if self.pointer_visibility.is_visible() { |
| 672 | if let Some((pointer_pos, win_pos)) = self.pointer_pos_for_window_cast(mapped) { |
| 673 | // Pointer location must be relative to the screencast buffer. |
| 674 | // - win_pos is the position of the main window surface in output-local |
| 675 | // coordinates |
| 676 | // - bbox.loc moves us relative to the screencast buffer |
| 677 | let buf_pos = win_pos + bbox.loc.to_f64().to_logical(scale); |
| 678 | let output_pos = self.global_space.output_geometry(output).unwrap().loc; |
| 679 | pointer_location = pointer_pos - output_pos.to_f64() - buf_pos; |
| 680 | |
| 681 | let pos = buf_pos.to_physical_precise_round(scale).upscale(-1); |
| 682 | self.render_pointer(renderer, output, &mut |elem| { |
| 683 | let elem = |
| 684 | RelocateRenderElement::from_element(elem, pos, Relocate::Relative); |
| 685 | elements.push(CastRenderElement::from(elem)); |
| 686 | }); |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | let main_start = elements.len(); |
| 691 | mapped.render_for_screen_cast(renderer, scale, &mut |elem| { |
| 692 | elements.push(CastRenderElement::from(elem)) |
| 693 | }); |
| 694 | |
| 695 | let cursor_data = CursorData::compute(&elements, main_start, pointer_location, scale); |
| 696 | |
| 697 | if cast.dequeue_buffer_and_render(renderer, &elements, &cursor_data, bbox.size, scale) { |
| 698 | cast.last_frame_time = target_presentation_time; |
| 699 | } |
| 700 | } |
| 701 | self.casting.casts = casts; |
| 702 | |
| 703 | for id in casts_to_stop { |
| 704 | self.stop_cast(id); |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | pub fn stop_cast(&mut self, session_id: CastSessionId) { |
| 709 | let _span = tracy_client::span!("Niri::stop_cast"); |
| 710 | let _span = debug_span!("stop_cast", %session_id).entered(); |
| 711 | |
| 712 | self.casting |
| 713 | .pending_dynamic_casts |
| 714 | .retain(|p| p.session_id != session_id); |
| 715 | |
| 716 | for i in (0..self.casting.casts.len()).rev() { |
| 717 | let cast = &self.casting.casts[i]; |
| 718 | if cast.session_id != session_id { |
| 719 | continue; |
| 720 | } |
| 721 | |
| 722 | let cast = self.casting.casts.swap_remove(i); |
| 723 | if let Err(err) = cast.stream.disconnect() { |
| 724 | warn!("error disconnecting stream: {err:?}"); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | let dbus = &self.dbus.as_ref().unwrap(); |
| 729 | let server = dbus.conn_screen_cast.as_ref().unwrap().object_server(); |
| 730 | let path = format!("/org/gnome/Mutter/ScreenCast/Session/u{}", session_id.get()); |
| 731 | if let Ok(iface) = server.interface::<_, mutter_screen_cast::Session>(path) { |
| 732 | let _span = tracy_client::span!("invoking Session::stop"); |
| 733 | |
| 734 | async_io::block_on(async move { |
| 735 | iface |
| 736 | .get() |
| 737 | .stop(server.inner(), iface.signal_emitter().clone()) |
| 738 | .await |
| 739 | }); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | pub fn stop_casts_for_target(&mut self, target: CastTarget) { |
| 744 | let _span = tracy_client::span!("Niri::stop_casts_for_target"); |
| 745 | |
| 746 | // This is O(N^2) but it shouldn't be a problem I think. |
| 747 | let mut saw_dynamic = false; |
| 748 | let mut ids = Vec::new(); |
| 749 | for cast in &self.casting.casts { |
| 750 | if cast.target != target { |
| 751 | continue; |
| 752 | } |
| 753 | |
| 754 | if cast.dynamic_target { |
| 755 | saw_dynamic = true; |
| 756 | continue; |
| 757 | } |
| 758 | |
| 759 | ids.push(cast.session_id); |
| 760 | } |
| 761 | |
| 762 | for id in ids { |
| 763 | self.stop_cast(id); |
| 764 | } |
| 765 | |
| 766 | // We don't stop dynamic casts, instead we switch them to Nothing. |
| 767 | if saw_dynamic { |
| 768 | self.event_loop |
| 769 | .insert_idle(|state| state.set_dynamic_cast_target(CastTarget::Nothing)); |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | fn cast_params_for_window(&self, window_id: u64) -> Option<(Size<i32, Physical>, u32)> { |
| 774 | let (_, mapped) = self |
| 775 | .layout |
| 776 | .windows() |
| 777 | .find(|(_, m)| m.id().get() == window_id)?; |
| 778 | let output = self.casting.mapped_cast_output.get(&mapped.window)?; |
| 779 | let scale = Scale::from(output.current_scale().fractional_scale()); |
| 780 | let bbox = mapped |
| 781 | .window |
| 782 | .bbox_with_popups() |
| 783 | .to_physical_precise_up(scale); |
| 784 | let refresh = output.current_mode().unwrap().refresh as u32; |
| 785 | Some((bbox.size, refresh)) |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | fn cast_params_for_output(output: &Output) -> (Size<i32, Physical>, u32) { |
| 790 | let mode = output.current_mode().unwrap(); |
| 791 | let transform = output.current_transform(); |
| 792 | let size = transform.transform_size(mode.size); |
| 793 | let refresh = mode.refresh as u32; |
| 794 | (size, refresh) |
| 795 | } |
| 796 | |
| 797 | niri_render_elements! { |
| 798 | CastRenderElement<R> => { |
| 799 | Output = OutputRenderElements<R>, |
| 800 | Window = WindowCastRenderElements<R>, |
| 801 | Pointer = PointerRenderElements<R>, |
| 802 | RelocatedPointer = RelocateRenderElement<PointerRenderElements<R>>, |
| 803 | } |
| 804 | } |