fix(x11): keep the shared display open until the last capture is dropped

The X11 engine has one process-wide display connection, but every
ScreenCapture closed it on drop. During guest assistance the device
init's screen-size probe created and dropped a capture after the main
one, closing the display under it: the stream stopped with
"X11 display wake failed" and the viewer got no video. A capture
replaced after a resize had the same hazard. Count live X11 captures
and close only on the last drop.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
VallierDevandClaude Opus 5.5 committed 2026-10-17 14:19:49 +08:00
1 parent 80ac4448d6
commit da49e71b9d
1 file changed
+8 -1
+8 -1
View File
@@ -108,6 +108,7 @@ impl ScreenCapture {
if display_ptr.is_null() { return Err("Capture display name unavailable".into()); }
let display = unsafe { std::ffi::CStr::from_ptr(display_ptr) }.to_string_lossy().into_owned();
log::info!("Selected capture display: {}", display);
X11_USERS.fetch_add(1, std::sync::atomic::Ordering::AcqRel);
Ok(Self {
backend,
init_error:None,
@@ -150,9 +151,15 @@ impl ScreenCapture {
}
}
/// The X11 engine keeps one process-wide display connection shared by every ScreenCapture.
/// Only the last one may close it: a short-lived capture (device init's screen size during guest
/// assistance, or the replaced capture after a resize) used to close it under the live stream.
static X11_USERS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
impl Drop for ScreenCapture {
fn drop(&mut self) {
if self.init_error.is_none() && self.backend==crate::desktop_backend::DesktopBackend::X11 { unsafe { x11_close() }; }
if self.init_error.is_none() && self.backend==crate::desktop_backend::DesktopBackend::X11
&& X11_USERS.fetch_sub(1, std::sync::atomic::Ordering::AcqRel) == 1 { unsafe { x11_close() }; }
}
}