From 43e9ef390a844e0daf1e1b68616246b55c0bcf01 Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Wed, 24 Jan 2024 16:07:00 -0800 Subject: [PATCH] Move `attach_buffer_and_commit` --- src/wayland/capture.rs | 26 +++++--------------------- src/wayland/screencopy.rs | 25 +++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/wayland/capture.rs b/src/wayland/capture.rs index e85d423..9dc14ff 100644 --- a/src/wayland/capture.rs +++ b/src/wayland/capture.rs @@ -35,7 +35,7 @@ pub struct Capture { pub buffer: Mutex>, pub source: CaptureSource, first_frame: AtomicBool, - session: Mutex>, + pub session: Mutex>, } impl Capture { @@ -64,6 +64,10 @@ impl Capture { self.session.lock().unwrap().is_some() } + pub fn unset_first_frame(&self) { + self.first_frame.store(false, Ordering::SeqCst); + } + pub fn first_frame(&self) -> bool { self.first_frame.load(Ordering::SeqCst) } @@ -108,26 +112,6 @@ impl Capture { } *self.buffer.lock().unwrap() = None; } - - pub fn attach_buffer_and_commit(&self, conn: &Connection) { - let session = self.session.lock().unwrap(); - let buffer = self.buffer.lock().unwrap(); - let (Some(session), Some(buffer)) = (session.as_ref(), buffer.as_ref()) else { - return; - }; - - let node = buffer - .node() - .and_then(|x| x.to_str().map(|x| x.to_string())); - - session.attach_buffer(&buffer.buffer, node, 0); // XXX age? - if self.first_frame() { - session.commit(zcosmic_screencopy_session_v1::Options::empty()); - } else { - session.commit(zcosmic_screencopy_session_v1::Options::OnDamage); - } - conn.flush().unwrap(); - } } impl Drop for Capture { diff --git a/src/wayland/screencopy.rs b/src/wayland/screencopy.rs index 3739df3..ad20378 100644 --- a/src/wayland/screencopy.rs +++ b/src/wayland/screencopy.rs @@ -7,6 +7,27 @@ use cosmic::cctk; use super::{AppData, Capture, CaptureImage, CaptureSource, Event}; +fn attach_buffer_and_commit(capture: &Capture, conn: &Connection) { + let session = capture.session.lock().unwrap(); + let buffer = capture.buffer.lock().unwrap(); + let (Some(session), Some(buffer)) = (session.as_ref(), buffer.as_ref()) else { + return; + }; + + let node = buffer + .node() + .and_then(|x| x.to_str().map(|x| x.to_string())); + + session.attach_buffer(&buffer.buffer, node, 0); // XXX age? + if capture.first_frame() { + session.commit(zcosmic_screencopy_session_v1::Options::empty()); + capture.unset_first_frame(); + } else { + session.commit(zcosmic_screencopy_session_v1::Options::OnDamage); + } + conn.flush().unwrap(); +} + impl ScreencopyHandler for AppData { fn screencopy_state(&mut self) -> &mut ScreencopyState { &mut self.screencopy_state @@ -34,7 +55,7 @@ impl ScreencopyHandler for AppData { drop(buffer); - capture.attach_buffer_and_commit(conn); + attach_buffer_and_commit(&capture, conn); } fn ready( @@ -73,7 +94,7 @@ impl ScreencopyHandler for AppData { drop(buffer); // Capture again on damage - capture.attach_buffer_and_commit(conn); + attach_buffer_and_commit(&capture, conn); } fn failed(