forge
Explore
Log in

Don't you dare go Hollow

ipc: Add screencopy cast tracking

Browse files at this commit

e82830c68c8dd2bc7b5ad49e87190167cb87c6f0Ivan MolodetskikhParent: 238caaf
Track wlr-screencopy sessions that use with_damage as screencasts. These
are used by tools like wl-screenrec for continuous recording.
src/ipc/server.rsmodified+64 -34
@@ -798,7 +798,6 @@ impl State {
798 server.send_event(event);798 server.send_event(event);
799 }799 }
800 800
801- #[cfg(feature = "xdp-gnome-screencast")]
802 pub fn ipc_refresh_casts(&mut self) {801 pub fn ipc_refresh_casts(&mut self) {
803 let Some(server) = &self.niri.ipc_server else {802 let Some(server) = &self.niri.ipc_server else {
804 return;803 return;
@@ -812,42 +811,73 @@ impl State {
812 let mut events = Vec::new();811 let mut events = Vec::new();
813 let mut seen = HashSet::new();812 let mut seen = HashSet::new();
814 813
815- // Check pending dynamic casts.814+ // Check PipeWire screencasts.
816- for pending in &self.niri.casting.pending_dynamic_casts {815+ #[cfg(feature = "xdp-gnome-screencast")]
817- let stream_id = pending.stream_id.get();816+ {
818- seen.insert(stream_id);817+ // Check pending dynamic casts.
819-818+ for pending in &self.niri.casting.pending_dynamic_casts {
820- // Pending dynamic casts don't change any properties, so we only need to check if it's819+ let stream_id = pending.stream_id.get();
821- // missing from the state.820+ seen.insert(stream_id);
822- if !state.casts.contains_key(&stream_id) {821+
823- let cast = niri_ipc::Cast {822+ // Pending dynamic casts don't change any properties, so we only need to check if
824- session_id: pending.session_id.get(),823+ // it's missing from the state.
825- stream_id,824+ if !state.casts.contains_key(&stream_id) {
826- target: niri_ipc::CastTarget::Nothing {},825+ let cast = niri_ipc::Cast {
827- is_dynamic_target: true,826+ session_id: pending.session_id.get(),
828- is_active: false,827+ stream_id,
829- };828+ target: niri_ipc::CastTarget::Nothing {},
830- events.push(Event::CastStartedOrChanged { cast });829+ is_dynamic_target: true,
830+ is_active: false,
831+ };
832+ events.push(Event::CastStartedOrChanged { cast });
833+ }
834+ }
835+
836+ // Check active casts.
837+ for cast in &self.niri.casting.casts {
838+ let stream_id = cast.stream_id.get();
839+ seen.insert(stream_id);
840+
841+ if state.casts.get(&stream_id).is_none_or(|existing| {
842+ // Only these properties can change.
843+ existing.is_active != cast.is_active() || !cast.target.matches(&existing.target)
844+ }) {
845+ let cast = niri_ipc::Cast {
846+ session_id: cast.session_id.get(),
847+ stream_id,
848+ target: cast.target.make_ipc(),
849+ is_dynamic_target: cast.dynamic_target,
850+ is_active: cast.is_active(),
851+ };
852+ events.push(Event::CastStartedOrChanged { cast });
853+ }
831 }854 }
832 }855 }
833 856
834- // Check active casts.857+ // Check screencopy casts.
835- for cast in &self.niri.casting.casts {858+ for queue in self.niri.screencopy_state.queues() {
836- let stream_id = cast.stream_id.get();859+ if let Some(cast_info) = queue.cast() {
837- seen.insert(stream_id);860+ let stream_id = cast_info.stream_id.get();
838-861+ seen.insert(stream_id);
839- if state.casts.get(&stream_id).is_none_or(|existing| {862+
840- // Only these properties can change.863+ if state.casts.get(&stream_id).is_none_or(|existing| {
841- existing.is_active != cast.is_active() || !cast.target.matches(&existing.target)864+ // Only this property can change.
842- }) {865+ match &existing.target {
843- let cast = niri_ipc::Cast {866+ niri_ipc::CastTarget::Output { name } => *name != cast_info.output_name,
844- session_id: cast.session_id.get(),867+ _ => true,
845- stream_id,868+ }
846- target: cast.target.make_ipc(),869+ }) {
847- is_dynamic_target: cast.dynamic_target,870+ let cast = niri_ipc::Cast {
848- is_active: cast.is_active(),871+ session_id: cast_info.session_id.get(),
849- };872+ stream_id,
850- events.push(Event::CastStartedOrChanged { cast });873+ target: niri_ipc::CastTarget::Output {
874+ name: cast_info.output_name.clone(),
875+ },
876+ is_dynamic_target: false,
877+ is_active: true,
878+ };
879+ events.push(Event::CastStartedOrChanged { cast });
880+ }
851 }881 }
852 }882 }
853 883
src/niri.rsmodified+0 -1
@@ -816,7 +816,6 @@ impl State {
816 // screencasts.816 // screencasts.
817 #[cfg(feature = "xdp-gnome-screencast")]817 #[cfg(feature = "xdp-gnome-screencast")]
818 self.niri.refresh_mapped_cast_window_rules();818 self.niri.refresh_mapped_cast_window_rules();
819- #[cfg(feature = "xdp-gnome-screencast")]
820 self.ipc_refresh_casts();819 self.ipc_refresh_casts();
821 820
822 self.niri.refresh_window_rules();821 self.niri.refresh_window_rules();
src/protocols/screencopy.rsmodified+66 -3
@@ -9,7 +9,7 @@ use smithay::backend::allocator::dmabuf::Dmabuf;
9 use smithay::backend::allocator::{Buffer, Fourcc};9 use smithay::backend::allocator::{Buffer, Fourcc};
10 use smithay::backend::renderer::damage::OutputDamageTracker;10 use smithay::backend::renderer::damage::OutputDamageTracker;
11 use smithay::backend::renderer::sync::SyncPoint;11 use smithay::backend::renderer::sync::SyncPoint;
12-use smithay::output::Output;12+use smithay::output::{Output, WeakOutput};
13 use smithay::reexports::wayland_protocols_wlr::screencopy::v1::server::{13 use smithay::reexports::wayland_protocols_wlr::screencopy::v1::server::{
14 zwlr_screencopy_frame_v1, zwlr_screencopy_manager_v1,14 zwlr_screencopy_frame_v1, zwlr_screencopy_manager_v1,
15 };15 };
@@ -23,7 +23,7 @@ use smithay::wayland::{dmabuf, shm};
23 use zwlr_screencopy_frame_v1::{Flags, ZwlrScreencopyFrameV1};23 use zwlr_screencopy_frame_v1::{Flags, ZwlrScreencopyFrameV1};
24 use zwlr_screencopy_manager_v1::ZwlrScreencopyManagerV1;24 use zwlr_screencopy_manager_v1::ZwlrScreencopyManagerV1;
25 25
26-use crate::utils::get_monotonic_time;26+use crate::utils::{get_monotonic_time, CastSessionId, CastStreamId};
27 27
28 const VERSION: u32 = 3;28 const VERSION: u32 = 3;
29 29
@@ -33,6 +33,40 @@ pub struct ScreencopyQueue {
33 pending_frames: HashSet<ZwlrScreencopyFrameV1>,33 pending_frames: HashSet<ZwlrScreencopyFrameV1>,
34 /// Queue of screencopies waiting for a corresponding output redraw with damage.34 /// Queue of screencopies waiting for a corresponding output redraw with damage.
35 screencopies: Vec<Screencopy>,35 screencopies: Vec<Screencopy>,
36+ /// Cast tracking, set when the first with_damage request arrives.
37+ cast: Option<ScreencopyCast>,
38+}
39+
40+pub struct ScreencopyCast {
41+ pub session_id: CastSessionId,
42+ pub stream_id: CastStreamId,
43+ /// Output being captured.
44+ ///
45+ /// Generally equal to the front entry in the queue, and persisted here when the queue becomes
46+ /// empty.
47+ pub output: WeakOutput,
48+ /// Cached name of the output.
49+ pub output_name: String,
50+}
51+
52+impl ScreencopyCast {
53+ fn new(output: &Output) -> Self {
54+ Self {
55+ session_id: CastSessionId::next(),
56+ stream_id: CastStreamId::next(),
57+ output: output.downgrade(),
58+ output_name: output.name(),
59+ }
60+ }
61+
62+ fn update_output(&mut self, output: &Output) {
63+ // Only allocate a new name when the output differs.
64+ let weak = output.downgrade();
65+ if self.output != weak {
66+ self.output = weak;
67+ self.output_name = output.name();
68+ }
69+ }
36 }70 }
37 71
38 impl Default for ScreencopyQueue {72 impl Default for ScreencopyQueue {
@@ -47,6 +81,7 @@ impl ScreencopyQueue {
47 damage_tracker: OutputDamageTracker::new((0, 0), 1.0, Transform::Normal),81 damage_tracker: OutputDamageTracker::new((0, 0), 1.0, Transform::Normal),
48 pending_frames: HashSet::new(),82 pending_frames: HashSet::new(),
49 screencopies: Vec::new(),83 screencopies: Vec::new(),
84+ cast: None,
50 }85 }
51 }86 }
52 87
@@ -54,6 +89,11 @@ impl ScreencopyQueue {
54 self.pending_frames.is_empty() && self.screencopies.is_empty()89 self.pending_frames.is_empty() && self.screencopies.is_empty()
55 }90 }
56 91
92+ /// Get the cast tracking info, if this queue is tracking a cast.
93+ pub fn cast(&self) -> Option<&ScreencopyCast> {
94+ self.cast.as_ref()
95+ }
96+
57 pub fn split(&mut self) -> (&mut OutputDamageTracker, Option<&Screencopy>) {97 pub fn split(&mut self) -> (&mut OutputDamageTracker, Option<&Screencopy>) {
58 let ScreencopyQueue {98 let ScreencopyQueue {
59 damage_tracker,99 damage_tracker,
@@ -69,11 +109,30 @@ impl ScreencopyQueue {
69 error!("only screencopy with damage can be pushed in the queue");109 error!("only screencopy with damage can be pushed in the queue");
70 }110 }
71 111
112+ if let Some(cast) = &mut self.cast {
113+ // Update cast output when pushing a new front screencopy.
114+ if self.screencopies.is_empty() {
115+ cast.update_output(screencopy.output());
116+ }
117+ } else {
118+ // First with_damage request, mark this as a screencast.
119+ let output = screencopy.output();
120+ self.cast = Some(ScreencopyCast::new(output));
121+ }
122+
72 self.screencopies.push(screencopy);123 self.screencopies.push(screencopy);
73 }124 }
74 125
75 pub fn pop(&mut self) -> Screencopy {126 pub fn pop(&mut self) -> Screencopy {
76- self.screencopies.remove(0)127+ let rv = self.screencopies.remove(0);
128+
129+ // Update cast output (most of the time we expect this to be the same).
130+ if let Some(first) = self.screencopies.first() {
131+ let cast = self.cast.as_mut().unwrap();
132+ cast.update_output(first.output());
133+ }
134+
135+ rv
77 }136 }
78 }137 }
79 138
@@ -135,6 +194,10 @@ impl ScreencopyManagerState {
135 self.cleanup_queues();194 self.cleanup_queues();
136 }195 }
137 196
197+ pub fn queues(&self) -> impl Iterator<Item = &ScreencopyQueue> {
198+ self.queues.values()
199+ }
200+
138 pub fn with_queues_mut(&mut self, mut f: impl FnMut(&mut ScreencopyQueue)) {201 pub fn with_queues_mut(&mut self, mut f: impl FnMut(&mut ScreencopyQueue)) {
139 for queue in self.queues.values_mut() {202 for queue in self.queues.values_mut() {
140 f(queue);203 f(queue);