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,
|
|
|
|
|
running: AtomicBool,
|
2024-01-24 15:29:03 -08:00
|
|
|
capturing: AtomicBool,
|
|
|
|
|
session: 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> {
|
|
|
|
|
Arc::new_cyclic(|weak_capture| {
|
|
|
|
|
let udata = SessionData {
|
|
|
|
|
session_data: Default::default(),
|
|
|
|
|
capture: weak_capture.clone(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let session = match &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,
|
|
|
|
|
),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Capture {
|
|
|
|
|
buffer: Mutex::new(None),
|
|
|
|
|
source,
|
|
|
|
|
first_frame: AtomicBool::new(true),
|
|
|
|
|
running: AtomicBool::new(false),
|
|
|
|
|
capturing: AtomicBool::new(false),
|
|
|
|
|
session,
|
|
|
|
|
}
|
|
|
|
|
})
|
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 {
|
|
|
|
|
self.running.load(Ordering::SeqCst)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-24 15:29:03 -08:00
|
|
|
// Buffer is currently attached and commited for capture by server
|
|
|
|
|
pub fn capturing(&self) -> bool {
|
|
|
|
|
self.capturing.load(Ordering::SeqCst)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_capturing(&self, value: bool) {
|
|
|
|
|
if value {
|
|
|
|
|
self.first_frame.store(false, Ordering::SeqCst);
|
|
|
|
|
}
|
|
|
|
|
self.capturing.store(value, Ordering::SeqCst);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
pub fn start(&self, conn: &Connection) {
|
|
|
|
|
let already_running = self.running.swap(true, Ordering::SeqCst);
|
|
|
|
|
let have_buffer = self.buffer.lock().unwrap().is_some();
|
|
|
|
|
if have_buffer && !already_running {
|
|
|
|
|
self.attach_buffer_and_commit(conn);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stop capturing. Can be started again with `start`
|
|
|
|
|
pub fn stop(&self) {
|
2023-02-10 10:12:26 -08:00
|
|
|
self.running.store(false, Ordering::SeqCst);
|
2024-01-24 15:29:03 -08:00
|
|
|
self.first_frame.store(true, Ordering::SeqCst);
|
|
|
|
|
// TODO: Reallocate buffers on re-start
|
|
|
|
|
// *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) {
|
|
|
|
|
let buffer = self.buffer.lock().unwrap();
|
|
|
|
|
let buffer = buffer.as_ref().unwrap();
|
|
|
|
|
|
|
|
|
|
let node = buffer
|
|
|
|
|
.node()
|
|
|
|
|
.and_then(|x| x.to_str().map(|x| x.to_string()));
|
|
|
|
|
|
|
|
|
|
self.session.attach_buffer(&buffer.buffer, node, 0); // XXX age?
|
|
|
|
|
if self.first_frame() {
|
|
|
|
|
self.session
|
|
|
|
|
.commit(zcosmic_screencopy_session_v1::Options::empty());
|
|
|
|
|
} else {
|
|
|
|
|
self.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();
|
|
|
|
|
|
|
|
|
|
self.set_capturing(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for Capture {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
self.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]);
|