forge
Explore
Log in

Don't you dare go Hollow

ipc: Add PID to screencopy Casts

Browse files at this commit

e546b339a3f9f56dda44f77ceb256bec1d34f283Ivan MolodetskikhParent: b39edf4
niri-ipc/src/lib.rsmodified+4 -0
@@ -1505,6 +1505,10 @@ pub struct Cast {
1505 /// This can be `false` for example when switching away to a different scene in OBS, which1505 /// This can be `false` for example when switching away to a different scene in OBS, which
1506 /// pauses the stream.1506 /// pauses the stream.
1507 pub is_active: bool,1507 pub is_active: bool,
1508+ /// Process ID of the screencast consumer, if known.
1509+ ///
1510+ /// Currently, only wlr-screencopy screencasts can have a pid.
1511+ pub pid: Option<i32>,
1508 }1512 }
1509 1513
1510 /// Kind of screencast.1514 /// Kind of screencast.
src/ipc/client.rsmodified+4 -0
@@ -764,6 +764,10 @@ fn print_cast(cast: &Cast) {
764 if cast.is_dynamic_target {764 if cast.is_dynamic_target {
765 println!(" Dynamic cast target");765 println!(" Dynamic cast target");
766 }766 }
767+
768+ if let Some(pid) = cast.pid {
769+ println!(" PID: {pid}");
770+ }
767 }771 }
768 772
769 fn fmt_rounded(x: f64) -> String {773 fn fmt_rounded(x: f64) -> String {
src/ipc/server.rsmodified+3 -0
@@ -829,6 +829,7 @@ impl State {
829 target: niri_ipc::CastTarget::Nothing {},829 target: niri_ipc::CastTarget::Nothing {},
830 is_dynamic_target: true,830 is_dynamic_target: true,
831 is_active: false,831 is_active: false,
832+ pid: None,
832 };833 };
833 events.push(Event::CastStartedOrChanged { cast });834 events.push(Event::CastStartedOrChanged { cast });
834 }835 }
@@ -850,6 +851,7 @@ impl State {
850 target: cast.target.make_ipc(),851 target: cast.target.make_ipc(),
851 is_dynamic_target: cast.dynamic_target,852 is_dynamic_target: cast.dynamic_target,
852 is_active: cast.is_active(),853 is_active: cast.is_active(),
854+ pid: None,
853 };855 };
854 events.push(Event::CastStartedOrChanged { cast });856 events.push(Event::CastStartedOrChanged { cast });
855 }857 }
@@ -883,6 +885,7 @@ impl State {
883 },885 },
884 is_dynamic_target: false,886 is_dynamic_target: false,
885 is_active: true,887 is_active: true,
888+ pid: queue.credentials().map(|creds| creds.pid),
886 };889 };
887 events.push(Event::CastStartedOrChanged { cast });890 events.push(Event::CastStartedOrChanged { cast });
888 }891 }
src/protocols/screencopy.rsmodified+15 -11
@@ -20,10 +20,11 @@ use smithay::reexports::wayland_server::{
20 };20 };
21 use smithay::utils::{Physical, Point, Rectangle, Size, Transform};21 use smithay::utils::{Physical, Point, Rectangle, Size, Transform};
22 use smithay::wayland::{dmabuf, shm};22 use smithay::wayland::{dmabuf, shm};
23+use wayland_backend::server::Credentials;
23 use zwlr_screencopy_frame_v1::{Flags, ZwlrScreencopyFrameV1};24 use zwlr_screencopy_frame_v1::{Flags, ZwlrScreencopyFrameV1};
24 use zwlr_screencopy_manager_v1::ZwlrScreencopyManagerV1;25 use zwlr_screencopy_manager_v1::ZwlrScreencopyManagerV1;
25 26
26-use crate::utils::{get_monotonic_time, CastSessionId, CastStreamId};27+use crate::utils::{get_credentials_for_client, get_monotonic_time, CastSessionId, CastStreamId};
27 28
28 const VERSION: u32 = 3;29 const VERSION: u32 = 3;
29 30
@@ -35,6 +36,8 @@ const VERSION: u32 = 3;
35 const CAST_TIMEOUT: Duration = Duration::from_secs(10);36 const CAST_TIMEOUT: Duration = Duration::from_secs(10);
36 37
37 pub struct ScreencopyQueue {38 pub struct ScreencopyQueue {
39+ /// Credentials of this wlr-screencopy client, if known.
40+ credentials: Option<Credentials>,
38 damage_tracker: OutputDamageTracker,41 damage_tracker: OutputDamageTracker,
39 /// Frames waiting for the client to call copy or destroy.42 /// Frames waiting for the client to call copy or destroy.
40 pending_frames: HashSet<ZwlrScreencopyFrameV1>,43 pending_frames: HashSet<ZwlrScreencopyFrameV1>,
@@ -83,19 +86,14 @@ impl ScreencopyCast {
83 }86 }
84 }87 }
85 88
86-impl Default for ScreencopyQueue {
87- fn default() -> Self {
88- Self::new()
89- }
90-}
91-
92 impl ScreencopyQueue {89 impl ScreencopyQueue {
93- pub fn new() -> Self {90+ pub fn new(credentials: Option<Credentials>) -> Self {
94 Self {91 Self {
95 damage_tracker: OutputDamageTracker::new((0, 0), 1.0, Transform::Normal),92 damage_tracker: OutputDamageTracker::new((0, 0), 1.0, Transform::Normal),
96 pending_frames: HashSet::new(),93 pending_frames: HashSet::new(),
97 screencopies: Vec::new(),94 screencopies: Vec::new(),
98 cast: None,95 cast: None,
96+ credentials,
99 }97 }
100 }98 }
101 99
@@ -108,6 +106,10 @@ impl ScreencopyQueue {
108 self.cast.as_ref()106 self.cast.as_ref()
109 }107 }
110 108
109+ pub fn credentials(&self) -> Option<Credentials> {
110+ self.credentials
111+ }
112+
111 pub fn split(&mut self) -> (&mut OutputDamageTracker, Option<&Screencopy>) {113 pub fn split(&mut self) -> (&mut OutputDamageTracker, Option<&Screencopy>) {
112 let ScreencopyQueue {114 let ScreencopyQueue {
113 damage_tracker,115 damage_tracker,
@@ -287,8 +289,8 @@ where
287 {289 {
288 fn bind(290 fn bind(
289 state: &mut D,291 state: &mut D,
290- _display: &DisplayHandle,292+ dh: &DisplayHandle,
291- _client: &Client,293+ client: &Client,
292 manager: New<ZwlrScreencopyManagerV1>,294 manager: New<ZwlrScreencopyManagerV1>,
293 _manager_state: &ScreencopyManagerGlobalData,295 _manager_state: &ScreencopyManagerGlobalData,
294 data_init: &mut DataInit<'_, D>,296 data_init: &mut DataInit<'_, D>,
@@ -296,7 +298,9 @@ where
296 let manager = data_init.init(manager, ());298 let manager = data_init.init(manager, ());
297 299
298 let state = state.screencopy_state();300 let state = state.screencopy_state();
299- state.queues.insert(manager.clone(), ScreencopyQueue::new());301+ let credentials = get_credentials_for_client(dh, client);
302+ let queue = ScreencopyQueue::new(credentials);
303+ state.queues.insert(manager.clone(), queue);
300 }304 }
301 305
302 fn can_view(client: Client, global_data: &ScreencopyManagerGlobalData) -> bool {306 fn can_view(client: Client, global_data: &ScreencopyManagerGlobalData) -> bool {
src/utils/mod.rsmodified+6 -2
@@ -21,7 +21,7 @@ use smithay::reexports::rustix::time::{clock_gettime, ClockId};
21 use smithay::reexports::wayland_protocols::xdg::decoration::zv1::server::zxdg_toplevel_decoration_v1;21 use smithay::reexports::wayland_protocols::xdg::decoration::zv1::server::zxdg_toplevel_decoration_v1;
22 use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel;22 use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel;
23 use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;23 use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;
24-use smithay::reexports::wayland_server::{DisplayHandle, Resource as _};24+use smithay::reexports::wayland_server::{Client, DisplayHandle, Resource as _};
25 use smithay::utils::{Coordinate, Logical, Point, Rectangle, Size, Transform};25 use smithay::utils::{Coordinate, Logical, Point, Rectangle, Size, Transform};
26 use smithay::wayland::compositor::{send_surface_state, with_states, SurfaceData};26 use smithay::wayland::compositor::{send_surface_state, with_states, SurfaceData};
27 use smithay::wayland::fractional_scale::with_fractional_scale;27 use smithay::wayland::fractional_scale::with_fractional_scale;
@@ -457,12 +457,16 @@ pub fn get_credentials_for_surface(surface: &WlSurface) -> Option<Credentials> {
457 let dh = DisplayHandle::from(handle);457 let dh = DisplayHandle::from(handle);
458 458
459 let client = dh.get_client(surface.id()).ok()?;459 let client = dh.get_client(surface.id()).ok()?;
460+ get_credentials_for_client(&dh, &client)
461+}
462+
463+pub fn get_credentials_for_client(dh: &DisplayHandle, client: &Client) -> Option<Credentials> {
460 let data = client.get_data::<ClientState>().unwrap();464 let data = client.get_data::<ClientState>().unwrap();
461 if data.credentials_unknown {465 if data.credentials_unknown {
462 return None;466 return None;
463 }467 }
464 468
465- client.get_credentials(&dh).ok()469+ client.get_credentials(dh).ok()
466 }470 }
467 471
468 pub fn ensure_min_max_size(mut x: i32, min_size: i32, max_size: i32) -> i32 {472 pub fn ensure_min_max_size(mut x: i32, min_size: i32, max_size: i32) -> i32 {