2024-01-25 15:44:30 -08:00
|
|
|
use cosmic::cctk::{
|
|
|
|
|
self,
|
|
|
|
|
cosmic_protocols::screencopy::v1::client::{
|
|
|
|
|
zcosmic_screencopy_manager_v1, zcosmic_screencopy_session_v1,
|
|
|
|
|
},
|
|
|
|
|
screencopy::{
|
|
|
|
|
BufferInfo, ScreencopyHandler, ScreencopySessionData, ScreencopySessionDataExt,
|
|
|
|
|
ScreencopyState,
|
|
|
|
|
},
|
2023-11-08 15:05:45 -08:00
|
|
|
wayland_client::{Connection, QueueHandle, WEnum},
|
2023-02-09 16:04:36 -08:00
|
|
|
};
|
2024-01-25 15:44:30 -08:00
|
|
|
use std::sync::{Arc, Weak};
|
|
|
|
|
|
|
|
|
|
use super::{AppData, Buffer, Capture, CaptureImage, CaptureSource, Event};
|
|
|
|
|
|
|
|
|
|
pub struct ScreencopySession {
|
|
|
|
|
buffer: Option<Buffer>,
|
|
|
|
|
session: zcosmic_screencopy_session_v1::ZcosmicScreencopySessionV1,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ScreencopySession {
|
|
|
|
|
pub fn new(
|
|
|
|
|
capture: &Arc<Capture>,
|
|
|
|
|
manager: &zcosmic_screencopy_manager_v1::ZcosmicScreencopyManagerV1,
|
|
|
|
|
qh: &QueueHandle<AppData>,
|
|
|
|
|
) -> Self {
|
|
|
|
|
let udata = SessionData {
|
|
|
|
|
session_data: Default::default(),
|
|
|
|
|
capture: Arc::downgrade(capture),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let session = match &capture.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,
|
|
|
|
|
),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Self {
|
|
|
|
|
buffer: None,
|
|
|
|
|
session,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn attach_buffer_and_commit(&self, capture: &Capture, conn: &Connection) {
|
|
|
|
|
let Some(buffer) = self.buffer.as_ref() else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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 capture.first_frame() {
|
|
|
|
|
self.session
|
|
|
|
|
.commit(zcosmic_screencopy_session_v1::Options::empty());
|
|
|
|
|
capture.unset_first_frame();
|
|
|
|
|
} else {
|
|
|
|
|
self.session
|
|
|
|
|
.commit(zcosmic_screencopy_session_v1::Options::OnDamage);
|
|
|
|
|
}
|
|
|
|
|
conn.flush().unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for ScreencopySession {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
self.session.destroy();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct SessionData {
|
|
|
|
|
session_data: ScreencopySessionData,
|
|
|
|
|
// Weak reference so session can be destroyed when all strong references
|
|
|
|
|
// are dropped.
|
|
|
|
|
pub capture: Weak<Capture>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ScreencopySessionDataExt for SessionData {
|
|
|
|
|
fn screencopy_session_data(&self) -> &ScreencopySessionData {
|
|
|
|
|
&self.session_data
|
2024-01-24 16:07:00 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 16:04:36 -08:00
|
|
|
impl ScreencopyHandler for AppData {
|
|
|
|
|
fn screencopy_state(&mut self) -> &mut ScreencopyState {
|
|
|
|
|
&mut self.screencopy_state
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn init_done(
|
|
|
|
|
&mut self,
|
|
|
|
|
conn: &Connection,
|
2023-11-08 15:05:45 -08:00
|
|
|
_qh: &QueueHandle<Self>,
|
2023-02-09 16:04:36 -08:00
|
|
|
session: &zcosmic_screencopy_session_v1::ZcosmicScreencopySessionV1,
|
|
|
|
|
buffer_infos: &[BufferInfo],
|
|
|
|
|
) {
|
2024-01-24 13:58:12 -08:00
|
|
|
let Some(capture) = Capture::for_session(session) else {
|
2023-02-09 16:04:36 -08:00
|
|
|
return;
|
2024-01-24 13:58:12 -08:00
|
|
|
};
|
2024-01-25 15:44:30 -08:00
|
|
|
let mut session = capture.session.lock().unwrap();
|
|
|
|
|
let Some(session) = session.as_mut() else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
2023-02-09 16:04:36 -08:00
|
|
|
|
|
|
|
|
// Create new buffer if none, or different format
|
2024-01-25 15:44:30 -08:00
|
|
|
if !session
|
|
|
|
|
.buffer
|
2023-02-09 16:04:36 -08:00
|
|
|
.as_ref()
|
2023-11-08 15:05:45 -08:00
|
|
|
.map_or(false, |x| buffer_infos.contains(&x.buffer_info))
|
2023-02-09 16:04:36 -08:00
|
|
|
{
|
2024-01-25 15:44:30 -08:00
|
|
|
session.buffer = Some(self.create_buffer(buffer_infos));
|
2023-02-09 16:04:36 -08:00
|
|
|
}
|
|
|
|
|
|
2024-01-25 15:44:30 -08:00
|
|
|
session.attach_buffer_and_commit(&capture, conn);
|
2023-02-09 16:04:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ready(
|
|
|
|
|
&mut self,
|
2024-01-24 15:29:03 -08:00
|
|
|
conn: &Connection,
|
2023-02-09 16:04:36 -08:00
|
|
|
_qh: &QueueHandle<Self>,
|
|
|
|
|
session: &zcosmic_screencopy_session_v1::ZcosmicScreencopySessionV1,
|
|
|
|
|
) {
|
2024-01-24 13:58:12 -08:00
|
|
|
let Some(capture) = Capture::for_session(session) else {
|
2023-02-09 16:04:36 -08:00
|
|
|
return;
|
2024-01-24 13:58:12 -08:00
|
|
|
};
|
2024-01-24 15:29:03 -08:00
|
|
|
if !capture.running() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-01-25 15:44:30 -08:00
|
|
|
let mut session = capture.session.lock().unwrap();
|
|
|
|
|
let Some(session) = session.as_mut() else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
2023-02-09 16:04:36 -08:00
|
|
|
|
2024-01-25 15:44:30 -08:00
|
|
|
if session.buffer.is_none() {
|
2023-12-12 11:59:32 -08:00
|
|
|
eprintln!("Error: No capture buffer?");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-01-25 15:44:30 -08:00
|
|
|
let img = unsafe { session.buffer.as_mut().unwrap().to_image() };
|
2023-11-16 19:38:42 -08:00
|
|
|
let image = CaptureImage { img };
|
2023-03-22 10:03:18 -07:00
|
|
|
match &capture.source {
|
|
|
|
|
CaptureSource::Toplevel(toplevel) => {
|
|
|
|
|
self.send_event(Event::ToplevelCapture(toplevel.clone(), image))
|
|
|
|
|
}
|
|
|
|
|
CaptureSource::Workspace(workspace, output) => {
|
2023-11-08 13:59:53 -08:00
|
|
|
self.send_event(Event::WorkspaceCapture(
|
|
|
|
|
workspace.clone(),
|
|
|
|
|
output.clone(),
|
|
|
|
|
image,
|
|
|
|
|
));
|
2023-02-09 16:04:36 -08:00
|
|
|
}
|
|
|
|
|
};
|
2024-01-24 15:29:03 -08:00
|
|
|
|
2023-02-09 16:04:36 -08:00
|
|
|
// Capture again on damage
|
2024-01-25 15:44:30 -08:00
|
|
|
session.attach_buffer_and_commit(&capture, conn);
|
2023-02-09 16:04:36 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn failed(
|
|
|
|
|
&mut self,
|
|
|
|
|
_conn: &Connection,
|
|
|
|
|
_qh: &QueueHandle<Self>,
|
|
|
|
|
session: &zcosmic_screencopy_session_v1::ZcosmicScreencopySessionV1,
|
|
|
|
|
_reason: WEnum<zcosmic_screencopy_session_v1::FailureReason>,
|
|
|
|
|
) {
|
|
|
|
|
// TODO
|
|
|
|
|
println!("Failed");
|
2023-12-06 15:35:25 -08:00
|
|
|
if let Some(capture) = Capture::for_session(session) {
|
2024-01-24 15:29:03 -08:00
|
|
|
capture.stop();
|
2023-12-06 15:35:25 -08:00
|
|
|
}
|
2023-02-09 16:04:36 -08:00
|
|
|
}
|
|
|
|
|
}
|
2024-01-25 15:44:30 -08:00
|
|
|
|
|
|
|
|
cctk::delegate_screencopy!(AppData, session: [SessionData]);
|