forge
Explore
Log in

Don't you dare go Hollow

Extract cast session/stream ID counters to global scope

Browse files at this commit

570ea119bae8e8cd3fa9677b634c173b8f3db8cfIvan MolodetskikhParent: df4614e
Add CastSessionId and CastStreamId newtypes. This lifts the atomic
counters from the D-Bus mutter_screen_cast module to a shared location,
preparing for adding screencopy cast tracking which will need the same
ID types.
src/dbus/mutter_screen_cast.rsmodified+17 -19
@@ -1,6 +1,6 @@
11 use std::collections::HashMap;
22 use std::mem;
3-use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
3+use std::sync::atomic::{AtomicBool, Ordering};
44 use std::sync::{Arc, Mutex};
55
66 use serde::Deserialize;
@@ -11,6 +11,7 @@ use zbus::{fdo, interface, ObjectServer};
1111
1212 use super::Start;
1313 use crate::backend::IpcOutputMap;
14+use crate::utils::{CastSessionId, CastStreamId};
1415
1516 #[derive(Clone)]
1617 pub struct ScreenCast {
@@ -22,7 +23,7 @@ pub struct ScreenCast {
2223
2324 #[derive(Clone)]
2425 pub struct Session {
25- id: usize,
26+ id: CastSessionId,
2627 ipc_outputs: Arc<Mutex<IpcOutputMap>>,
2728 to_niri: calloop::channel::Sender<ScreenCastToNiri>,
2829 #[allow(clippy::type_complexity)]
@@ -58,12 +59,10 @@ struct RecordWindowProperties {
5859 _is_recording: Option<bool>,
5960 }
6061
61-static STREAM_ID: AtomicUsize = AtomicUsize::new(0);
62-
6362 #[derive(Clone)]
6463 pub struct Stream {
65- id: usize,
66- session_id: usize,
64+ id: CastStreamId,
65+ session_id: CastSessionId,
6766 target: StreamTarget,
6867 cursor_mode: CursorMode,
6968 was_started: Arc<AtomicBool>,
@@ -94,14 +93,14 @@ struct StreamParameters {
9493
9594 pub enum ScreenCastToNiri {
9695 StartCast {
97- session_id: usize,
98- stream_id: usize,
96+ session_id: CastSessionId,
97+ stream_id: CastStreamId,
9998 target: StreamTargetId,
10099 cursor_mode: CursorMode,
101100 signal_ctx: SignalEmitter<'static>,
102101 },
103102 StopCast {
104- session_id: usize,
103+ session_id: CastSessionId,
105104 },
106105 }
107106
@@ -118,9 +117,8 @@ impl ScreenCast {
118117 ));
119118 }
120119
121- static NUMBER: AtomicUsize = AtomicUsize::new(0);
122- let session_id = NUMBER.fetch_add(1, Ordering::SeqCst);
123- let path = format!("/org/gnome/Mutter/ScreenCast/Session/u{session_id}");
120+ let session_id = CastSessionId::next();
121+ let path = format!("/org/gnome/Mutter/ScreenCast/Session/u{}", session_id.get());
124122 let path = OwnedObjectPath::try_from(path).unwrap();
125123
126124 let session = Session::new(session_id, self.ipc_outputs.clone(), self.to_niri.clone());
@@ -207,8 +205,8 @@ impl Session {
207205 return Err(fdo::Error::Failed("monitor is disabled".to_owned()));
208206 }
209207
210- let stream_id = STREAM_ID.fetch_add(1, Ordering::SeqCst);
211- let path = format!("/org/gnome/Mutter/ScreenCast/Stream/u{stream_id}");
208+ let stream_id = CastStreamId::next();
209+ let path = format!("/org/gnome/Mutter/ScreenCast/Stream/u{}", stream_id.get());
212210 let path = OwnedObjectPath::try_from(path).unwrap();
213211
214212 let cursor_mode = properties.cursor_mode.unwrap_or_default();
@@ -244,8 +242,8 @@ impl Session {
244242 ) -> fdo::Result<OwnedObjectPath> {
245243 debug!(?properties, "record_window");
246244
247- let stream_id = STREAM_ID.fetch_add(1, Ordering::SeqCst);
248- let path = format!("/org/gnome/Mutter/ScreenCast/Stream/u{stream_id}");
245+ let stream_id = CastStreamId::next();
246+ let path = format!("/org/gnome/Mutter/ScreenCast/Stream/u{}", stream_id.get());
249247 let path = OwnedObjectPath::try_from(path).unwrap();
250248
251249 let cursor_mode = properties.cursor_mode.unwrap_or_default();
@@ -337,7 +335,7 @@ impl Start for ScreenCast {
337335
338336 impl Session {
339337 pub fn new(
340- id: usize,
338+ id: CastSessionId,
341339 ipc_outputs: Arc<Mutex<IpcOutputMap>>,
342340 to_niri: calloop::channel::Sender<ScreenCastToNiri>,
343341 ) -> Self {
@@ -361,8 +359,8 @@ impl Drop for Session {
361359
362360 impl Stream {
363361 fn new(
364- id: usize,
365- session_id: usize,
362+ id: CastStreamId,
363+ session_id: CastSessionId,
366364 target: StreamTarget,
367365 cursor_mode: CursorMode,
368366 to_niri: calloop::channel::Sender<ScreenCastToNiri>,
src/screencasting/mod.rsmodified+8 -8
@@ -20,7 +20,7 @@ use crate::dbus::mutter_screen_cast::{self, CursorMode, ScreenCastToNiri, Stream
2020 use crate::niri::{CastTarget, Niri, OutputRenderElements, PointerRenderElements, State};
2121 use crate::niri_render_elements;
2222 use crate::render_helpers::RenderTarget;
23-use crate::utils::get_monotonic_time;
23+use crate::utils::{get_monotonic_time, CastSessionId, CastStreamId};
2424 use crate::window::mapped::{MappedId, WindowCastRenderElements};
2525
2626 mod pw_utils;
@@ -46,8 +46,8 @@ pub struct Screencasting {
4646
4747 /// A screencast request that hasn't been started yet.
4848 pub struct PendingCast {
49- pub session_id: usize,
50- pub stream_id: usize,
49+ pub session_id: CastSessionId,
50+ pub stream_id: CastStreamId,
5151 pub cursor_mode: CursorMode,
5252 pub signal_ctx: SignalEmitter<'static>,
5353 }
@@ -137,7 +137,7 @@ impl State {
137137 }
138138 }
139139
140- fn redraw_cast(&mut self, stream_id: usize) {
140+ fn redraw_cast(&mut self, stream_id: CastStreamId) {
141141 let _span = tracy_client::span!("State::redraw_cast");
142142
143143 let casts = &mut self.niri.casting.casts;
@@ -391,7 +391,7 @@ impl State {
391391 signal_ctx,
392392 } => {
393393 let _span = tracy_client::span!("StartCast");
394- let _span = debug_span!("StartCast", session_id, stream_id).entered();
394+ let _span = debug_span!("StartCast", %session_id, %stream_id).entered();
395395
396396 let (target, size, refresh, alpha) = match target {
397397 StreamTargetId::Output { name } => {
@@ -694,9 +694,9 @@ impl Niri {
694694 }
695695 }
696696
697- fn stop_cast(&mut self, session_id: usize) {
697+ fn stop_cast(&mut self, session_id: CastSessionId) {
698698 let _span = tracy_client::span!("Niri::stop_cast");
699- let _span = debug_span!("stop_cast", session_id).entered();
699+ let _span = debug_span!("stop_cast", %session_id).entered();
700700
701701 self.casting
702702 .pending_dynamic_casts
@@ -716,7 +716,7 @@ impl Niri {
716716
717717 let dbus = &self.dbus.as_ref().unwrap();
718718 let server = dbus.conn_screen_cast.as_ref().unwrap().object_server();
719- let path = format!("/org/gnome/Mutter/ScreenCast/Session/u{session_id}");
719+ let path = format!("/org/gnome/Mutter/ScreenCast/Session/u{}", session_id.get());
720720 if let Ok(iface) = server.interface::<_, mutter_screen_cast::Session>(path) {
721721 let _span = tracy_client::span!("invoking Session::stop");
722722
src/screencasting/pw_utils.rsmodified+15 -15
@@ -55,7 +55,7 @@ use crate::render_helpers::{
5555 clear_dmabuf, encompassing_geo, render_and_download, render_to_dmabuf,
5656 };
5757 use crate::screencasting::CastRenderElement;
58-use crate::utils::get_monotonic_time;
58+use crate::utils::{get_monotonic_time, CastSessionId, CastStreamId};
5959
6060 // Give a 0.1 ms allowance for presentation time errors.
6161 const CAST_DELAY_ALLOWANCE: Duration = Duration::from_micros(100);
@@ -79,15 +79,15 @@ pub struct PipeWire {
7979 }
8080
8181 pub enum PwToNiri {
82- StopCast { session_id: usize },
83- Redraw { stream_id: usize },
82+ StopCast { session_id: CastSessionId },
83+ Redraw { stream_id: CastStreamId },
8484 FatalError,
8585 }
8686
8787 pub struct Cast {
8888 event_loop: LoopHandle<'static, State>,
89- pub session_id: usize,
90- pub stream_id: usize,
89+ pub session_id: CastSessionId,
90+ pub stream_id: CastStreamId,
9191 // Listener is dropped before Stream to prevent a use-after-free.
9292 _listener: StreamListener<()>,
9393 pub stream: StreamRc,
@@ -269,8 +269,8 @@ impl PipeWire {
269269 &self,
270270 gbm: GbmDevice<DrmDeviceFd>,
271271 formats: FormatSet,
272- session_id: usize,
273- stream_id: usize,
272+ session_id: CastSessionId,
273+ stream_id: CastStreamId,
274274 target: CastTarget,
275275 size: Size<i32, Physical>,
276276 refresh: u32,
@@ -283,13 +283,13 @@ impl PipeWire {
283283 let to_niri_ = self.to_niri.clone();
284284 let stop_cast = move || {
285285 if let Err(err) = to_niri_.send(PwToNiri::StopCast { session_id }) {
286- warn!(session_id, "error sending StopCast to niri: {err:?}");
286+ warn!(%session_id, "error sending StopCast to niri: {err:?}");
287287 }
288288 };
289289 let to_niri_ = self.to_niri.clone();
290290 let redraw = move || {
291291 if let Err(err) = to_niri_.send(PwToNiri::Redraw { stream_id }) {
292- warn!(stream_id, "error sending Redraw to niri: {err:?}");
292+ warn!(%stream_id, "error sending Redraw to niri: {err:?}");
293293 }
294294 };
295295 let redraw_ = redraw.clone();
@@ -328,7 +328,7 @@ impl PipeWire {
328328 let inner = inner.clone();
329329 let stop_cast = stop_cast.clone();
330330 move |stream, (), old, new| {
331- let _span = debug_span!("state_changed", stream_id).entered();
331+ let _span = debug_span!("state_changed", %stream_id).entered();
332332 debug!("{old:?} -> {new:?}");
333333 let mut inner = inner.borrow_mut();
334334
@@ -378,7 +378,7 @@ impl PipeWire {
378378 let formats = formats.clone();
379379 move |stream, (), id, pod| {
380380 let id = ParamType::from_raw(id);
381- trace!(stream_id, ?id, "param_changed");
381+ trace!(%stream_id, ?id, "param_changed");
382382 let mut inner = inner.borrow_mut();
383383 let inner = &mut *inner;
384384
@@ -386,7 +386,7 @@ impl PipeWire {
386386 return;
387387 }
388388
389- let _span = debug_span!("param_changed", stream_id).entered();
389+ let _span = debug_span!("param_changed", %stream_id).entered();
390390
391391 let Some(pod) = pod else { return };
392392
@@ -678,7 +678,7 @@ impl PipeWire {
678678 let inner = inner.clone();
679679 let stop_cast = stop_cast.clone();
680680 move |stream, (), buffer| {
681- let _span = debug_span!("add_buffer", stream_id).entered();
681+ let _span = debug_span!("add_buffer", %stream_id).entered();
682682 let mut inner = inner.borrow_mut();
683683
684684 let (size, alpha, modifier) = if let CastState::Ready {
@@ -758,7 +758,7 @@ impl PipeWire {
758758 .remove_buffer({
759759 let inner = inner.clone();
760760 move |_stream, (), buffer| {
761- trace!(stream_id, "remove_buffer");
761+ trace!(%stream_id, "remove_buffer");
762762 let mut inner = inner.borrow_mut();
763763
764764 inner
@@ -779,7 +779,7 @@ impl PipeWire {
779779 .unwrap();
780780
781781 trace!(
782- stream_id,
782+ %stream_id,
783783 "starting pw stream with size={pending_size:?}, refresh={refresh:?}"
784784 );
785785
src/utils/mod.rsmodified+46 -1
@@ -1,12 +1,13 @@
11 use std::cmp::{max, min};
2-use std::f64;
32 use std::ffi::{CString, OsStr};
3+use std::fmt::Display;
44 use std::io::Write;
55 use std::os::unix::prelude::OsStrExt;
66 use std::path::{Path, PathBuf};
77 use std::ptr::null_mut;
88 use std::sync::atomic::AtomicBool;
99 use std::time::Duration;
10+use std::{f64, fmt};
1011
1112 use anyhow::{ensure, Context};
1213 use bitflags::bitflags;
@@ -44,6 +45,50 @@ pub mod xwayland;
4445
4546 pub static IS_SYSTEMD_SERVICE: AtomicBool = AtomicBool::new(false);
4647
48+use id::IdCounter;
49+
50+/// Unique ID for a screencast session.
51+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
52+pub struct CastSessionId(u64);
53+
54+impl CastSessionId {
55+ pub fn next() -> Self {
56+ static COUNTER: IdCounter = IdCounter::new();
57+ Self(COUNTER.next())
58+ }
59+
60+ pub fn get(self) -> u64 {
61+ self.0
62+ }
63+}
64+
65+impl Display for CastSessionId {
66+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67+ write!(f, "{}", self.0)
68+ }
69+}
70+
71+/// Unique ID for a screencast stream.
72+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
73+pub struct CastStreamId(u64);
74+
75+impl CastStreamId {
76+ pub fn next() -> Self {
77+ static COUNTER: IdCounter = IdCounter::new();
78+ Self(COUNTER.next())
79+ }
80+
81+ pub fn get(self) -> u64 {
82+ self.0
83+ }
84+}
85+
86+impl Display for CastStreamId {
87+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88+ write!(f, "{}", self.0)
89+ }
90+}
91+
4792 bitflags! {
4893 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
4994 pub struct ResizeEdge: u32 {