Add ext-foreign-toplevel-image-capture-source
Browse files at this commita6b02b197cd5c1f4da17257cfff0ef14816c7029Parent: 9d8f4f85 changed files +355 -11
- src/handlers/image_copy_capture.rs +75 -11
- src/niri.rs +169 -0
- src/protocols/foreign_toplevel.rs +11 -0
- src/protocols/mod.rs +1 -0
- src/protocols/toplevel_capture_source.rs +99 -0
src/handlers/image_copy_capture.rsmodified+75 -11
| @@ -31,6 +31,7 @@ use wayland_backend::server::Credentials; | |||
| 31 | use crate::cursor::{RenderCursor, XCursor}; | 31 | use crate::cursor::{RenderCursor, XCursor}; |
| 32 | use crate::niri::{Niri, State}; | 32 | use crate::niri::{Niri, State}; |
| 33 | use crate::utils::{get_credentials_for_client, CastSessionId, CastStreamId}; | 33 | use crate::utils::{get_credentials_for_client, CastSessionId, CastStreamId}; |
| 34 | +use crate::window::mapped::MappedId; | ||
| 34 | 35 | ||
| 35 | /// Output capture session. | 36 | /// Output capture session. |
| 36 | pub struct ImageCopySession { | 37 | pub struct ImageCopySession { |
| @@ -85,6 +86,27 @@ pub fn source_output(source: &ImageCaptureSource) -> Option<Output> { | |||
| 85 | source.user_data().get::<WeakOutput>()?.upgrade() | 86 | source.user_data().get::<WeakOutput>()?.upgrade() |
| 86 | } | 87 | } |
| 87 | 88 | ||
| 89 | +/// Window captured by a toplevel source. | ||
| 90 | +pub struct ToplevelSource(pub MappedId); | ||
| 91 | + | ||
| 92 | +/// Window captured by a toplevel source, if it is one. | ||
| 93 | +pub fn source_window(source: &ImageCaptureSource) -> Option<MappedId> { | ||
| 94 | + source.user_data().get::<ToplevelSource>().map(|s| s.0) | ||
| 95 | +} | ||
| 96 | + | ||
| 97 | +/// Output and size of a window capture, or `None` if the window is gone or has no size. | ||
| 98 | +pub fn window_capture_target(niri: &Niri, id: MappedId) -> Option<(Output, Size<i32, Physical>)> { | ||
| 99 | + let (monitor, mapped) = niri.layout.windows().find(|(_, win)| win.id() == id)?; | ||
| 100 | + let output = monitor?.output().clone(); | ||
| 101 | + let scale = Scale::from(output.current_scale().fractional_scale()); | ||
| 102 | + let size = mapped | ||
| 103 | + .window | ||
| 104 | + .bbox_with_popups() | ||
| 105 | + .to_physical_precise_up(scale) | ||
| 106 | + .size; | ||
| 107 | + (!size.is_empty()).then_some((output, size)) | ||
| 108 | +} | ||
| 109 | + | ||
| 88 | /// Buffer constraints for capturing an output. | 110 | /// Buffer constraints for capturing an output. |
| 89 | /// | 111 | /// |
| 90 | /// `render_node` is the primary renderer's DRM render node (see | 112 | /// `render_node` is the primary renderer's DRM render node (see |
| @@ -101,7 +123,24 @@ pub fn output_capture_constraints( | |||
| 101 | ) -> Option<BufferConstraints> { | 123 | ) -> Option<BufferConstraints> { |
| 102 | let mode = output.current_mode()?; | 124 | let mode = output.current_mode()?; |
| 103 | let size = Size::<i32, BufferCoords>::from((mode.size.w, mode.size.h)); | 125 | let size = Size::<i32, BufferCoords>::from((mode.size.w, mode.size.h)); |
| 126 | + Some(capture_constraints_for_size( | ||
| 127 | + renderer, | ||
| 128 | + render_node, | ||
| 129 | + size, | ||
| 130 | + false, | ||
| 131 | + )) | ||
| 132 | +} | ||
| 104 | 133 | ||
| 134 | +/// Buffer constraints for capturing a buffer of `size`. `alpha` selects Argb8888 as the | ||
| 135 | +/// preferred format instead of Xrgb8888, for window captures. | ||
| 136 | +/// | ||
| 137 | +/// See `output_capture_constraints()` for `render_node`. | ||
| 138 | +pub fn capture_constraints_for_size( | ||
| 139 | + renderer: &GlesRenderer, | ||
| 140 | + render_node: Option<DrmNode>, | ||
| 141 | + size: Size<i32, BufferCoords>, | ||
| 142 | + alpha: bool, | ||
| 143 | +) -> BufferConstraints { | ||
| 105 | let dma = (|| { | 144 | let dma = (|| { |
| 106 | let node = render_node?; | 145 | let node = render_node?; |
| 107 | let egl = renderer.egl_context(); | 146 | let egl = renderer.egl_context(); |
| @@ -121,21 +160,32 @@ pub fn output_capture_constraints( | |||
| 121 | } | 160 | } |
| 122 | 161 | ||
| 123 | // Put Xrgb8888 and Argb8888 first since some clients always take the | 162 | // Put Xrgb8888 and Argb8888 first since some clients always take the |
| 124 | - // first advertised format (e.g. wl-mirror, grim). | 163 | + // first advertised format (e.g. wl-mirror, grim). The preferred one of |
| 125 | - formats.sort_by_key(|(code, _)| match code { | 164 | + // the two goes first. |
| 126 | - Fourcc::Xrgb8888 => 0, | 165 | + let (first, second) = if alpha { |
| 127 | - Fourcc::Argb8888 => 1, | 166 | + (Fourcc::Argb8888, Fourcc::Xrgb8888) |
| 128 | - _ => 2, | 167 | + } else { |
| 168 | + (Fourcc::Xrgb8888, Fourcc::Argb8888) | ||
| 169 | + }; | ||
| 170 | + formats.sort_by_key(|(code, _)| { | ||
| 171 | + if *code == first { | ||
| 172 | + 0 | ||
| 173 | + } else if *code == second { | ||
| 174 | + 1 | ||
| 175 | + } else { | ||
| 176 | + 2 | ||
| 177 | + } | ||
| 129 | }); | 178 | }); |
| 130 | 179 | ||
| 131 | Some(DmabufConstraints { node, formats }) | 180 | Some(DmabufConstraints { node, formats }) |
| 132 | })(); | 181 | })(); |
| 133 | 182 | ||
| 134 | - Some(BufferConstraints { | 183 | + let shm = if alpha { |
| 135 | - size, | 184 | + vec![wl_shm::Format::Argb8888] |
| 136 | - shm: vec![wl_shm::Format::Xrgb8888], | 185 | + } else { |
| 137 | - dma, | 186 | + vec![wl_shm::Format::Xrgb8888] |
| 138 | - }) | 187 | + }; |
| 188 | + BufferConstraints { size, shm, dma } | ||
| 139 | } | 189 | } |
| 140 | 190 | ||
| 141 | /// Buffer constraints for capturing the cursor of an output. Argb8888 since it has alpha. | 191 | /// Buffer constraints for capturing the cursor of an output. Argb8888 since it has alpha. |
| @@ -296,6 +346,15 @@ impl ImageCopyCaptureHandler for State { | |||
| 296 | } | 346 | } |
| 297 | 347 | ||
| 298 | fn capture_constraints(&mut self, source: &ImageCaptureSource) -> Option<BufferConstraints> { | 348 | fn capture_constraints(&mut self, source: &ImageCaptureSource) -> Option<BufferConstraints> { |
| 349 | + if let Some(id) = source_window(source) { | ||
| 350 | + let (_, size) = window_capture_target(&self.niri, id)?; | ||
| 351 | + let size = Size::<i32, BufferCoords>::from((size.w, size.h)); | ||
| 352 | + let render_node = self.backend.primary_render_node(); | ||
| 353 | + return self.backend.with_primary_renderer(|renderer| { | ||
| 354 | + capture_constraints_for_size(renderer, render_node, size, true) | ||
| 355 | + }); | ||
| 356 | + } | ||
| 357 | + | ||
| 299 | let output = source_output(source)?; | 358 | let output = source_output(source)?; |
| 300 | if !self.niri.output_state.contains_key(&output) { | 359 | if !self.niri.output_state.contains_key(&output) { |
| 301 | return None; | 360 | return None; |
| @@ -380,7 +439,12 @@ impl ImageCopyCaptureHandler for State { | |||
| 380 | s.pending_frame = Some(frame); | 439 | s.pending_frame = Some(frame); |
| 381 | 440 | ||
| 382 | // The frame is captured on the next redraw with damage. | 441 | // The frame is captured on the next redraw with damage. |
| 383 | - if let Some(output) = source_output(&session.source()) { | 442 | + if let Some(id) = source_window(&session.source()) { |
| 443 | + // The window may be gone already (the global lingers). | ||
| 444 | + if let Some((output, _)) = window_capture_target(&self.niri, id) { | ||
| 445 | + self.niri.queue_redraw(&output); | ||
| 446 | + } | ||
| 447 | + } else if let Some(output) = source_output(&session.source()) { | ||
| 384 | // The output may be gone already (the global lingers). | 448 | // The output may be gone already (the global lingers). |
| 385 | if self.niri.output_exists(&output) { | 449 | if self.niri.output_exists(&output) { |
| 386 | self.niri.queue_redraw(&output); | 450 | self.niri.queue_redraw(&output); |
src/niri.rsmodified+169 -0
| @@ -163,6 +163,7 @@ use crate::protocols::gamma_control::GammaControlManagerState; | |||
| 163 | use crate::protocols::mutter_x11_interop::MutterX11InteropManagerState; | 163 | use crate::protocols::mutter_x11_interop::MutterX11InteropManagerState; |
| 164 | use crate::protocols::output_management::OutputManagementManagerState; | 164 | use crate::protocols::output_management::OutputManagementManagerState; |
| 165 | use crate::protocols::screencopy::{Screencopy, ScreencopyBuffer, ScreencopyManagerState}; | 165 | use crate::protocols::screencopy::{Screencopy, ScreencopyBuffer, ScreencopyManagerState}; |
| 166 | +use crate::protocols::toplevel_capture_source::ToplevelCaptureSourceManagerState; | ||
| 166 | use crate::render_helpers::blur::BlurOptions; | 167 | use crate::render_helpers::blur::BlurOptions; |
| 167 | use crate::render_helpers::debug::push_opaque_regions; | 168 | use crate::render_helpers::debug::push_opaque_regions; |
| 168 | use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement; | 169 | use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement; |
| @@ -922,6 +923,39 @@ impl State { | |||
| 922 | 923 | ||
| 923 | let mut sessions = mem::take(&mut self.niri.image_copy_sessions); | 924 | let mut sessions = mem::take(&mut self.niri.image_copy_sessions); |
| 924 | sessions.retain_mut(|s| { | 925 | sessions.retain_mut(|s| { |
| 926 | + if let Some(id) = image_copy_capture_impl::source_window(&s.session.source()) { | ||
| 927 | + let Some((_, size)) = | ||
| 928 | + image_copy_capture_impl::window_capture_target(&self.niri, id) | ||
| 929 | + else { | ||
| 930 | + return false; | ||
| 931 | + }; | ||
| 932 | + let size = Size::<i32, BufferCoords>::from((size.w, size.h)); | ||
| 933 | + if s.session.current_constraints().map(|c| c.size) == Some(size) { | ||
| 934 | + return true; | ||
| 935 | + } | ||
| 936 | + | ||
| 937 | + let render_node = self.backend.primary_render_node(); | ||
| 938 | + let constraints = self.backend.with_primary_renderer(|renderer| { | ||
| 939 | + image_copy_capture_impl::capture_constraints_for_size( | ||
| 940 | + renderer, | ||
| 941 | + render_node, | ||
| 942 | + size, | ||
| 943 | + true, | ||
| 944 | + ) | ||
| 945 | + }); | ||
| 946 | + let Some(constraints) = constraints else { | ||
| 947 | + return false; | ||
| 948 | + }; | ||
| 949 | + | ||
| 950 | + // Same as below, fail the frame before sending the new constraints. | ||
| 951 | + if let Some(frame) = s.pending_frame.take() { | ||
| 952 | + frame.fail(CaptureFailureReason::BufferConstraints); | ||
| 953 | + } | ||
| 954 | + s.session.update_constraints(constraints); | ||
| 955 | + | ||
| 956 | + return true; | ||
| 957 | + } | ||
| 958 | + | ||
| 925 | let Some(output) = live_output(&self.niri, &s.session.source()) else { | 959 | let Some(output) = live_output(&self.niri, &s.session.source()) else { |
| 926 | return false; | 960 | return false; |
| 927 | }; | 961 | }; |
| @@ -2618,6 +2652,7 @@ impl Niri { | |||
| 2618 | &display_handle, | 2652 | &display_handle, |
| 2619 | client_is_unrestricted, | 2653 | client_is_unrestricted, |
| 2620 | ); | 2654 | ); |
| 2655 | + ToplevelCaptureSourceManagerState::new(&display_handle, client_is_unrestricted); | ||
| 2621 | let image_copy_capture_state = ImageCopyCaptureState::new_with_filter::<State, _>( | 2656 | let image_copy_capture_state = ImageCopyCaptureState::new_with_filter::<State, _>( |
| 2622 | &display_handle, | 2657 | &display_handle, |
| 2623 | client_is_unrestricted, | 2658 | client_is_unrestricted, |
| @@ -5013,6 +5048,7 @@ impl Niri { | |||
| 5013 | 5048 | ||
| 5014 | self.render_for_screencopy_with_damage(renderer, output); | 5049 | self.render_for_screencopy_with_damage(renderer, output); |
| 5015 | self.render_for_image_copy_capture(renderer, output, target_presentation_time); | 5050 | self.render_for_image_copy_capture(renderer, output, target_presentation_time); |
| 5051 | + self.render_windows_for_image_copy_capture(renderer, output, target_presentation_time); | ||
| 5016 | self.render_for_image_copy_cursor_capture(renderer, output, target_presentation_time); | 5052 | self.render_for_image_copy_cursor_capture(renderer, output, target_presentation_time); |
| 5017 | }); | 5053 | }); |
| 5018 | } | 5054 | } |
| @@ -5853,6 +5889,139 @@ impl Niri { | |||
| 5853 | self.image_copy_sessions = sessions; | 5889 | self.image_copy_sessions = sessions; |
| 5854 | } | 5890 | } |
| 5855 | 5891 | ||
| 5892 | + /// Renders window toplevel sessions on `output`, the output the window is on. | ||
| 5893 | + pub fn render_windows_for_image_copy_capture( | ||
| 5894 | + &mut self, | ||
| 5895 | + renderer: &mut GlesRenderer, | ||
| 5896 | + output: &Output, | ||
| 5897 | + target_presentation_time: Duration, | ||
| 5898 | + ) { | ||
| 5899 | + let _span = tracy_client::span!("Niri::render_windows_for_image_copy_capture"); | ||
| 5900 | + | ||
| 5901 | + let scale: Scale<f64> = output.current_scale().fractional_scale().into(); | ||
| 5902 | + let transform = Transform::Normal; | ||
| 5903 | + | ||
| 5904 | + let mut sessions = mem::take(&mut self.image_copy_sessions); | ||
| 5905 | + for s in &mut sessions { | ||
| 5906 | + // Cheapest check first. | ||
| 5907 | + if s.pending_frame.is_none() { | ||
| 5908 | + continue; | ||
| 5909 | + } | ||
| 5910 | + | ||
| 5911 | + let Some(id) = image_copy_capture_impl::source_window(&s.session.source()) else { | ||
| 5912 | + continue; | ||
| 5913 | + }; | ||
| 5914 | + let Some(mapped) = self | ||
| 5915 | + .layout | ||
| 5916 | + .windows_for_output(output) | ||
| 5917 | + .find(|m| m.id() == id) | ||
| 5918 | + else { | ||
| 5919 | + // The window is on another output. | ||
| 5920 | + continue; | ||
| 5921 | + }; | ||
| 5922 | + | ||
| 5923 | + let size = mapped | ||
| 5924 | + .window | ||
| 5925 | + .bbox_with_popups() | ||
| 5926 | + .to_physical_precise_up(scale) | ||
| 5927 | + .size; | ||
| 5928 | + if size.is_empty() { | ||
| 5929 | + continue; | ||
| 5930 | + } | ||
| 5931 | + | ||
| 5932 | + // Recreate the damage tracker if the window size changed. | ||
| 5933 | + let OutputModeSource::Static { | ||
| 5934 | + size: last_size, | ||
| 5935 | + scale: last_scale, | ||
| 5936 | + transform: last_transform, | ||
| 5937 | + } = s.damage_tracker.mode().clone() | ||
| 5938 | + else { | ||
| 5939 | + unreachable!("damage tracker must have static mode"); | ||
| 5940 | + }; | ||
| 5941 | + if size != last_size || scale != last_scale || transform != last_transform { | ||
| 5942 | + s.damage_tracker = OutputDamageTracker::new(size, scale, transform); | ||
| 5943 | + } | ||
| 5944 | + | ||
| 5945 | + // Window captures don't include the cursor. | ||
| 5946 | + let mut elements = Vec::new(); | ||
| 5947 | + mapped.render_for_screen_cast(renderer, scale, &mut |elem| elements.push(elem)); | ||
| 5948 | + | ||
| 5949 | + let (damage, states) = s.damage_tracker.damage_output(1, &elements).unwrap(); | ||
| 5950 | + let Some(damage) = damage else { | ||
| 5951 | + // No damage, capture the frame later. | ||
| 5952 | + continue; | ||
| 5953 | + }; | ||
| 5954 | + | ||
| 5955 | + // Convert from Physical coordinates back to Buffer coordinates. | ||
| 5956 | + let damage: Vec<Rectangle<i32, BufferCoords>> = damage | ||
| 5957 | + .iter() | ||
| 5958 | + .map(|dmg| { | ||
| 5959 | + dmg.to_logical(1) | ||
| 5960 | + .to_buffer(1, transform.invert(), &size.to_logical(1)) | ||
| 5961 | + }) | ||
| 5962 | + .collect(); | ||
| 5963 | + | ||
| 5964 | + let frame = s.pending_frame.take().unwrap(); | ||
| 5965 | + let buffer = frame.buffer(); | ||
| 5966 | + let buffer_size = Size::<i32, BufferCoords>::from((size.w, size.h)); | ||
| 5967 | + let capture_buffer = image_copy_capture_impl::capture_buffer( | ||
| 5968 | + &buffer, | ||
| 5969 | + buffer_size, | ||
| 5970 | + wl_shm::Format::Argb8888, | ||
| 5971 | + ); | ||
| 5972 | + let Some(capture_buffer) = capture_buffer else { | ||
| 5973 | + frame.fail(CaptureFailureReason::BufferConstraints); | ||
| 5974 | + // Report full damage next time. | ||
| 5975 | + s.damage_tracker = OutputDamageTracker::new(size, scale, transform); | ||
| 5976 | + continue; | ||
| 5977 | + }; | ||
| 5978 | + | ||
| 5979 | + let res = match capture_buffer { | ||
| 5980 | + CaptureBuffer::Dma(dmabuf) => { | ||
| 5981 | + render_to_dmabuf(renderer, &mut s.damage_tracker, dmabuf, &elements, states) | ||
| 5982 | + .map(Some) | ||
| 5983 | + } | ||
| 5984 | + CaptureBuffer::Shm => render_to_shm( | ||
| 5985 | + renderer, | ||
| 5986 | + &mut s.damage_tracker, | ||
| 5987 | + &buffer, | ||
| 5988 | + wl_shm::Format::Argb8888, | ||
| 5989 | + &elements, | ||
| 5990 | + states, | ||
| 5991 | + ) | ||
| 5992 | + .map(|()| None), | ||
| 5993 | + }; | ||
| 5994 | + | ||
| 5995 | + match res { | ||
| 5996 | + Ok(sync) => { | ||
| 5997 | + image_copy_capture_impl::frame_success_after_sync( | ||
| 5998 | + frame, | ||
| 5999 | + transform, | ||
| 6000 | + damage, | ||
| 6001 | + target_presentation_time, | ||
| 6002 | + sync, | ||
| 6003 | + &self.event_loop, | ||
| 6004 | + ); | ||
| 6005 | + } | ||
| 6006 | + Err(err) => { | ||
| 6007 | + warn!("error rendering window for image copy capture: {err:?}"); | ||
| 6008 | + frame.fail(CaptureFailureReason::Unknown); | ||
| 6009 | + // Report full damage next time. | ||
| 6010 | + s.damage_tracker = OutputDamageTracker::new(size, scale, transform); | ||
| 6011 | + } | ||
| 6012 | + } | ||
| 6013 | + } | ||
| 6014 | + | ||
| 6015 | + // This shouldn't be possible since sessions are only added from the | ||
| 6016 | + // ImageCopyCaptureHandler callbacks, which run in dispatch_clients() | ||
| 6017 | + // (i.e., not in here), but append just in case it ever changes... | ||
| 6018 | + if !self.image_copy_sessions.is_empty() { | ||
| 6019 | + error!("session added while rendering image-copy-capture frame"); | ||
| 6020 | + sessions.append(&mut self.image_copy_sessions); | ||
| 6021 | + } | ||
| 6022 | + self.image_copy_sessions = sessions; | ||
| 6023 | + } | ||
| 6024 | + | ||
| 5856 | pub fn render_for_image_copy_cursor_capture( | 6025 | pub fn render_for_image_copy_cursor_capture( |
| 5857 | &mut self, | 6026 | &mut self, |
| 5858 | renderer: &mut GlesRenderer, | 6027 | renderer: &mut GlesRenderer, |
src/protocols/foreign_toplevel.rsmodified+11 -0
| @@ -88,6 +88,17 @@ impl ForeignToplevelManagerState { | |||
| 88 | toplevels: HashMap::new(), | 88 | toplevels: HashMap::new(), |
| 89 | } | 89 | } |
| 90 | } | 90 | } |
| 91 | + | ||
| 92 | + /// Window that an ext-foreign-toplevel-list handle refers to, if it's still alive. | ||
| 93 | + pub fn mapped_id_for_ext_handle( | ||
| 94 | + &self, | ||
| 95 | + handle: &ExtForeignToplevelHandleV1, | ||
| 96 | + ) -> Option<MappedId> { | ||
| 97 | + self.toplevels | ||
| 98 | + .values() | ||
| 99 | + .find(|data| data.ext_list_instances.contains(handle)) | ||
| 100 | + .map(|data| data.identifier) | ||
| 101 | + } | ||
| 91 | } | 102 | } |
| 92 | 103 | ||
| 93 | pub fn refresh(state: &mut State) { | 104 | pub fn refresh(state: &mut State) { |
src/protocols/mod.rsmodified+1 -0
| @@ -4,6 +4,7 @@ pub mod gamma_control; | |||
| 4 | pub mod mutter_x11_interop; | 4 | pub mod mutter_x11_interop; |
| 5 | pub mod output_management; | 5 | pub mod output_management; |
| 6 | pub mod screencopy; | 6 | pub mod screencopy; |
| 7 | +pub mod toplevel_capture_source; | ||
| 7 | 8 | ||
| 8 | pub mod raw; | 9 | pub mod raw; |
| 9 | 10 | ||
src/protocols/toplevel_capture_source.rsadded+99 -0
| @@ -0,0 +1,99 @@ | |||
| 1 | +use std::sync::Arc; | ||
| 2 | + | ||
| 3 | +use smithay::reexports::wayland_protocols::ext::image_capture_source::v1::server::ext_foreign_toplevel_image_capture_source_manager_v1::{ | ||
| 4 | + self, ExtForeignToplevelImageCaptureSourceManagerV1, | ||
| 5 | +}; | ||
| 6 | +use smithay::reexports::wayland_server::{Client, DataInit, DisplayHandle, New}; | ||
| 7 | +use smithay::wayland::image_capture_source::{ImageCaptureSource, ImageCaptureSourceData}; | ||
| 8 | +use smithay::wayland::{Dispatch2, GlobalDispatch2}; | ||
| 9 | + | ||
| 10 | +use crate::handlers::image_copy_capture::ToplevelSource; | ||
| 11 | +use crate::niri::State; | ||
| 12 | +use crate::protocols::EmptyData; | ||
| 13 | + | ||
| 14 | +const VERSION: u32 = 1; | ||
| 15 | + | ||
| 16 | +pub struct ToplevelCaptureSourceManagerState; | ||
| 17 | + | ||
| 18 | +#[derive(Clone)] | ||
| 19 | +pub struct ToplevelCaptureSourceGlobalData { | ||
| 20 | + filter: Arc<dyn for<'c> Fn(&'c Client) -> bool + Send + Sync>, | ||
| 21 | +} | ||
| 22 | + | ||
| 23 | +impl ToplevelCaptureSourceManagerState { | ||
| 24 | + pub fn new<F>(display: &DisplayHandle, filter: F) -> Self | ||
| 25 | + where | ||
| 26 | + F: for<'c> Fn(&'c Client) -> bool + Send + Sync + 'static, | ||
| 27 | + { | ||
| 28 | + let global_data = ToplevelCaptureSourceGlobalData { | ||
| 29 | + filter: Arc::new(filter), | ||
| 30 | + }; | ||
| 31 | + display.create_global::<State, ExtForeignToplevelImageCaptureSourceManagerV1, _>( | ||
| 32 | + VERSION, | ||
| 33 | + global_data, | ||
| 34 | + ); | ||
| 35 | + Self | ||
| 36 | + } | ||
| 37 | +} | ||
| 38 | + | ||
| 39 | +impl GlobalDispatch2<ExtForeignToplevelImageCaptureSourceManagerV1, State> | ||
| 40 | + for ToplevelCaptureSourceGlobalData | ||
| 41 | +{ | ||
| 42 | + fn bind( | ||
| 43 | + &self, | ||
| 44 | + _state: &mut State, | ||
| 45 | + _handle: &DisplayHandle, | ||
| 46 | + _client: &Client, | ||
| 47 | + resource: New<ExtForeignToplevelImageCaptureSourceManagerV1>, | ||
| 48 | + data_init: &mut DataInit<'_, State>, | ||
| 49 | + ) { | ||
| 50 | + data_init.init(resource, EmptyData); | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + fn can_view(&self, client: &Client) -> bool { | ||
| 54 | + (self.filter)(client) | ||
| 55 | + } | ||
| 56 | +} | ||
| 57 | + | ||
| 58 | +impl Dispatch2<ExtForeignToplevelImageCaptureSourceManagerV1, State> for EmptyData { | ||
| 59 | + fn request( | ||
| 60 | + &self, | ||
| 61 | + state: &mut State, | ||
| 62 | + _client: &Client, | ||
| 63 | + _resource: &ExtForeignToplevelImageCaptureSourceManagerV1, | ||
| 64 | + request: ext_foreign_toplevel_image_capture_source_manager_v1::Request, | ||
| 65 | + _dhandle: &DisplayHandle, | ||
| 66 | + data_init: &mut DataInit<'_, State>, | ||
| 67 | + ) { | ||
| 68 | + match request { | ||
| 69 | + ext_foreign_toplevel_image_capture_source_manager_v1::Request::CreateSource { | ||
| 70 | + source, | ||
| 71 | + toplevel_handle, | ||
| 72 | + } => { | ||
| 73 | + let capture_source = ImageCaptureSource::new(); | ||
| 74 | + | ||
| 75 | + let source_resource = data_init.init( | ||
| 76 | + source, | ||
| 77 | + ImageCaptureSourceData { | ||
| 78 | + source: capture_source.clone(), | ||
| 79 | + }, | ||
| 80 | + ); | ||
| 81 | + capture_source.add_instance(&source_resource); | ||
| 82 | + | ||
| 83 | + // Handles of closed windows give a source without a target; capture | ||
| 84 | + // sessions on it are stopped right away. | ||
| 85 | + if let Some(id) = state | ||
| 86 | + .niri | ||
| 87 | + .foreign_toplevel_state | ||
| 88 | + .mapped_id_for_ext_handle(&toplevel_handle) | ||
| 89 | + { | ||
| 90 | + capture_source | ||
| 91 | + .user_data() | ||
| 92 | + .insert_if_missing(|| ToplevelSource(id)); | ||
| 93 | + } | ||
| 94 | + } | ||
| 95 | + ext_foreign_toplevel_image_capture_source_manager_v1::Request::Destroy => {} | ||
| 96 | + _ => unreachable!(), | ||
| 97 | + } | ||
| 98 | + } | ||
| 99 | +} | ||