2023-02-09 16:04:36 -08:00
|
|
|
use cctk::{
|
|
|
|
|
cosmic_protocols::{
|
|
|
|
|
screencopy::v1::client::{zcosmic_screencopy_manager_v1, zcosmic_screencopy_session_v1},
|
|
|
|
|
toplevel_info::v1::client::zcosmic_toplevel_handle_v1,
|
|
|
|
|
workspace::v1::client::zcosmic_workspace_handle_v1,
|
|
|
|
|
},
|
|
|
|
|
screencopy::{ScreencopySessionData, ScreencopySessionDataExt},
|
2024-01-24 15:29:03 -08:00
|
|
|
wayland_client::{protocol::wl_output, Connection, Proxy, QueueHandle},
|
2023-02-09 16:04:36 -08:00
|
|
|
};
|
2023-11-21 16:15:02 -05:00
|
|
|
use cosmic::cctk;
|
2023-02-09 16:04:36 -08:00
|
|
|
|
|
|
|
|
use std::sync::{
|
|
|
|
|
atomic::{AtomicBool, Ordering},
|
2024-01-24 13:58:12 -08:00
|
|
|
Arc, Mutex, Weak,
|
2023-02-09 16:04:36 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use super::{AppData, Buffer};
|
|
|
|
|
|
2023-11-16 13:38:37 -08:00
|
|
|
#[derive(Clone, Hash, PartialEq, Eq)]
|
2023-02-09 16:04:36 -08:00
|
|
|
pub enum CaptureSource {
|
|
|
|
|
Toplevel(zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1),
|
|
|
|
|
Workspace(
|
|
|
|
|
zcosmic_workspace_handle_v1::ZcosmicWorkspaceHandleV1,
|
|
|
|
|
wl_output::WlOutput,
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
|
pub struct CaptureFilter {
|
2023-11-08 13:59:53 -08:00
|
|
|
pub workspaces_on_outputs: Vec<wl_output::WlOutput>,
|
2023-02-09 16:04:36 -08:00
|
|
|
pub toplevels_on_workspaces: Vec<zcosmic_workspace_handle_v1::ZcosmicWorkspaceHandleV1>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Capture {
|
|
|
|
|
pub buffer: Mutex<Option<Buffer>>,
|
|
|
|
|
pub source: CaptureSource,
|
2023-02-10 10:12:26 -08:00
|
|
|
first_frame: AtomicBool,
|
2024-01-24 15:59:18 -08:00
|
|
|
session: Mutex<Option<zcosmic_screencopy_session_v1::ZcosmicScreencopySessionV1>>,
|
2023-02-09 16:04:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Capture {
|
2024-01-24 15:29:03 -08:00
|
|
|
pub fn new(
|
|
|
|
|
source: CaptureSource,
|
|
|
|
|
manager: &zcosmic_screencopy_manager_v1::ZcosmicScreencopyManagerV1,
|
|
|
|
|
qh: &QueueHandle<AppData>,
|
|
|
|
|
) -> Arc<Capture> {
|
2024-01-24 15:59:18 -08:00
|
|
|
Arc::new(Capture {
|
|
|
|
|
buffer: Mutex::new(None),
|
|
|
|
|
source,
|
|
|
|
|
first_frame: AtomicBool::new(true),
|
|
|
|
|
session: Mutex::new(None),
|
2024-01-24 15:29:03 -08:00
|
|
|
})
|
2023-02-09 16:04:36 -08:00
|
|
|
}
|
|
|
|
|
|
2024-01-24 15:29:03 -08:00
|
|
|
// Returns `None` if capture is destroyed
|
2024-01-24 13:58:12 -08:00
|
|
|
// (or if `session` wasn't created with `SessionData`)
|
2023-02-09 16:04:36 -08:00
|
|
|
pub fn for_session(
|
|
|
|
|
session: &zcosmic_screencopy_session_v1::ZcosmicScreencopySessionV1,
|
2024-01-24 13:58:12 -08:00
|
|
|
) -> Option<Arc<Self>> {
|
2024-01-24 15:29:03 -08:00
|
|
|
session.data::<SessionData>()?.capture.upgrade()
|
2023-02-09 16:04:36 -08:00
|
|
|
}
|
|
|
|
|
|
2023-02-10 10:12:26 -08:00
|
|
|
pub fn running(&self) -> bool {
|
2024-01-24 15:59:18 -08:00
|
|
|
self.session.lock().unwrap().is_some()
|
2024-01-24 15:29:03 -08:00
|
|
|
}
|
|
|
|
|
|
2023-02-10 10:12:26 -08:00
|
|
|
pub fn first_frame(&self) -> bool {
|
|
|
|
|
self.first_frame.load(Ordering::SeqCst)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-24 15:29:03 -08:00
|
|
|
// Start capturing frames
|
2024-01-24 15:59:18 -08:00
|
|
|
pub fn start(
|
|
|
|
|
self: &Arc<Self>,
|
|
|
|
|
manager: &zcosmic_screencopy_manager_v1::ZcosmicScreencopyManagerV1,
|
|
|
|
|
qh: &QueueHandle<AppData>,
|
|
|
|
|
) {
|
|
|
|
|
let mut session = self.session.lock().unwrap();
|
|
|
|
|
if session.is_none() {
|
|
|
|
|
self.first_frame.store(true, Ordering::SeqCst);
|
|
|
|
|
|
|
|
|
|
let udata = SessionData {
|
|
|
|
|
session_data: Default::default(),
|
|
|
|
|
capture: Arc::downgrade(self),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
*session = Some(match &self.source {
|
|
|
|
|
CaptureSource::Toplevel(toplevel) => manager.capture_toplevel(
|
|
|
|
|
toplevel,
|
|
|
|
|
zcosmic_screencopy_manager_v1::CursorMode::Hidden,
|
|
|
|
|
qh,
|
|
|
|
|
udata,
|
|
|
|
|
),
|
|
|
|
|
CaptureSource::Workspace(workspace, output) => manager.capture_workspace(
|
|
|
|
|
workspace,
|
|
|
|
|
output,
|
|
|
|
|
zcosmic_screencopy_manager_v1::CursorMode::Hidden,
|
|
|
|
|
qh,
|
|
|
|
|
udata,
|
|
|
|
|
),
|
|
|
|
|
});
|
2024-01-24 15:29:03 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stop capturing. Can be started again with `start`
|
|
|
|
|
pub fn stop(&self) {
|
2024-01-24 15:59:18 -08:00
|
|
|
if let Some(session) = self.session.lock().unwrap().take() {
|
|
|
|
|
session.destroy();
|
|
|
|
|
}
|
|
|
|
|
*self.buffer.lock().unwrap() = None;
|
2023-02-09 16:04:36 -08:00
|
|
|
}
|
|
|
|
|
|
2024-01-24 15:29:03 -08:00
|
|
|
pub fn attach_buffer_and_commit(&self, conn: &Connection) {
|
2024-01-24 15:59:18 -08:00
|
|
|
let session = self.session.lock().unwrap();
|
2024-01-24 15:29:03 -08:00
|
|
|
let buffer = self.buffer.lock().unwrap();
|
2024-01-24 15:59:18 -08:00
|
|
|
let (Some(session), Some(buffer)) = (session.as_ref(), buffer.as_ref()) else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
2024-01-24 15:29:03 -08:00
|
|
|
|
|
|
|
|
let node = buffer
|
|
|
|
|
.node()
|
|
|
|
|
.and_then(|x| x.to_str().map(|x| x.to_string()));
|
|
|
|
|
|
2024-01-24 15:59:18 -08:00
|
|
|
session.attach_buffer(&buffer.buffer, node, 0); // XXX age?
|
2024-01-24 15:29:03 -08:00
|
|
|
if self.first_frame() {
|
2024-01-24 15:59:18 -08:00
|
|
|
session.commit(zcosmic_screencopy_session_v1::Options::empty());
|
2024-01-24 15:29:03 -08:00
|
|
|
} else {
|
2024-01-24 15:59:18 -08:00
|
|
|
session.commit(zcosmic_screencopy_session_v1::Options::OnDamage);
|
2023-02-09 16:04:36 -08:00
|
|
|
}
|
2024-01-24 15:29:03 -08:00
|
|
|
conn.flush().unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for Capture {
|
|
|
|
|
fn drop(&mut self) {
|
2024-01-24 15:59:18 -08:00
|
|
|
if let Some(session) = self.session.lock().unwrap().as_ref() {
|
|
|
|
|
session.destroy();
|
|
|
|
|
}
|
2023-02-09 16:04:36 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct SessionData {
|
|
|
|
|
session_data: ScreencopySessionData,
|
2024-01-24 13:58:12 -08:00
|
|
|
capture: Weak<Capture>,
|
2023-02-09 16:04:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ScreencopySessionDataExt for SessionData {
|
|
|
|
|
fn screencopy_session_data(&self) -> &ScreencopySessionData {
|
|
|
|
|
&self.session_data
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cctk::delegate_screencopy!(AppData, session: [SessionData]);
|