2023-02-09 16:04:36 -08:00
|
|
|
use cctk::{
|
|
|
|
|
cosmic_protocols::screencopy::v1::client::zcosmic_screencopy_session_v1,
|
|
|
|
|
screencopy::{BufferInfo, ScreencopyHandler, ScreencopyState},
|
2023-11-08 15:05:45 -08:00
|
|
|
wayland_client::{Connection, QueueHandle, WEnum},
|
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
|
|
|
|
2023-11-16 19:38:42 -08:00
|
|
|
use super::{AppData, Capture, CaptureImage, CaptureSource, Event};
|
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
|
|
|
session.destroy();
|
|
|
|
|
return;
|
2024-01-24 13:58:12 -08:00
|
|
|
};
|
2023-02-09 16:04:36 -08:00
|
|
|
|
|
|
|
|
let mut buffer = capture.buffer.lock().unwrap();
|
|
|
|
|
// Create new buffer if none, or different format
|
|
|
|
|
if !buffer
|
|
|
|
|
.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
|
|
|
{
|
2023-11-08 15:05:45 -08:00
|
|
|
*buffer = Some(self.create_buffer(buffer_infos));
|
2023-02-09 16:04:36 -08:00
|
|
|
}
|
|
|
|
|
let buffer = buffer.as_ref().unwrap();
|
|
|
|
|
|
2023-11-09 13:45:28 -08:00
|
|
|
let node = buffer
|
|
|
|
|
.node()
|
|
|
|
|
.and_then(|x| x.to_str().map(|x| x.to_string()));
|
|
|
|
|
session.attach_buffer(&buffer.buffer, node, 0); // XXX age?
|
2023-02-10 10:12:26 -08:00
|
|
|
if capture.first_frame() {
|
2023-02-09 16:04:36 -08:00
|
|
|
session.commit(zcosmic_screencopy_session_v1::Options::empty());
|
|
|
|
|
} else {
|
|
|
|
|
session.commit(zcosmic_screencopy_session_v1::Options::OnDamage);
|
|
|
|
|
}
|
|
|
|
|
conn.flush().unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ready(
|
|
|
|
|
&mut self,
|
|
|
|
|
_conn: &Connection,
|
|
|
|
|
_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
|
|
|
session.destroy();
|
|
|
|
|
return;
|
2024-01-24 13:58:12 -08:00
|
|
|
};
|
2023-02-09 16:04:36 -08:00
|
|
|
|
|
|
|
|
let mut buffer = capture.buffer.lock().unwrap();
|
2023-12-12 11:59:32 -08:00
|
|
|
if buffer.is_none() {
|
|
|
|
|
eprintln!("Error: No capture buffer?");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-11-16 19:38:42 -08:00
|
|
|
let img = unsafe { buffer.as_mut().unwrap().to_image() };
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
session.destroy();
|
|
|
|
|
|
|
|
|
|
// Capture again on damage
|
|
|
|
|
capture.capture(&self.screencopy_state.screencopy_manager, &self.qh);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
capture.cancel();
|
|
|
|
|
}
|
2023-02-09 16:04:36 -08:00
|
|
|
session.destroy();
|
|
|
|
|
}
|
|
|
|
|
}
|