From 466fbf26b395368e53e21a01232badff4a4125cb Mon Sep 17 00:00:00 2001 From: Wanjohi Date: Sun, 25 Oct 2026 14:02:24 +0300 Subject: [PATCH] wip(nescope): bind each X11 window's capture source --- apps/nescope/src/state.rs | 2 ++ apps/nescope/src/windows.rs | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/apps/nescope/src/state.rs b/apps/nescope/src/state.rs index 86fe3bb3..d1ac727e 100644 --- a/apps/nescope/src/state.rs +++ b/apps/nescope/src/state.rs @@ -888,6 +888,7 @@ impl NescopeState { let Some(key) = WinKey::of(window) else { return; }; + let native = window.x11_surface().map(|x11| x11.window_id()); let (pid, title, app_id) = if let Some(x11) = window.x11_surface() { // The Wayland client of an Xwayland window is Xwayland itself; // the game says who it is in _NET_WM_PID. @@ -906,6 +907,7 @@ impl NescopeState { }; let new = crate::windows::NewWindow { pid, + native, title, app_id, size: (clamp16(self.width), clamp16(self.height)), diff --git a/apps/nescope/src/windows.rs b/apps/nescope/src/windows.rs index acf20b8b..577b3b24 100644 --- a/apps/nescope/src/windows.rs +++ b/apps/nescope/src/windows.rs @@ -18,6 +18,8 @@ pub struct Tracked { pub key: K, pub id: WindowId, pub pid: Option, + /// The X11 window id, for a window Xwayland draws. + native: Option, /// False while an X11 window is unmapped: the hub has been told it closed, /// and a map again opens it under the same id. mapped: bool, @@ -48,6 +50,8 @@ impl Default for Registry { /// What the compositor knows about a window when it maps. pub struct NewWindow { pub pid: Option, + /// The X11 window id, for a window Xwayland draws. + pub native: Option, pub title: String, pub app_id: String, pub size: (u16, u16), @@ -70,6 +74,7 @@ impl Registry { } w.mapped = true; w.pid = new.pid.or(w.pid); + w.native = new.native.or(w.native); w.title = new.title; w.app_id = new.app_id; tracing::info!(window = w.id, "window opened again"); @@ -88,6 +93,7 @@ impl Registry { key, id, pid: new.pid, + native: new.native, mapped: true, launch_id, title: new.title, @@ -214,9 +220,21 @@ fn announce(w: &Tracked, size: (u16, u16)) -> Vec> { encode_window_pid(&mut m, w.id, pid); out.push(m); } + // A capture layer names frames by the X11 window they were drawn for when + // it knows it, marked by WINDOW_SOURCE_BIT, so a process drawing several + // windows streams each as its own. + if let Some(native) = w.native { + let mut m = Vec::new(); + encode_window_pid(&mut m, w.id, WINDOW_SOURCE_BIT | native); + out.push(m); + } out } +/// Marks a capture source as an X11 window rather than a process. Process ids +/// stay far below it. +pub const WINDOW_SOURCE_BIT: u32 = 0x8000_0000; + /// Read a process's `NESTRI_LAUNCH_ID`, which the launch put in its /// environment and its children inherit. pub fn launch_of_process(pid: u32) -> Option { @@ -304,6 +322,7 @@ mod tests { fn new(pid: Option, title: &str) -> NewWindow { NewWindow { pid, + native: None, title: title.into(), app_id: "app".into(), size: (1920, 1080),