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-08 15:05:45 -08:00
|
|
|
use super::{AppData, Capture, 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],
|
|
|
|
|
) {
|
|
|
|
|
let capture = Capture::for_session(session).unwrap();
|
2023-02-10 10:12:26 -08:00
|
|
|
if !capture.running() {
|
2023-02-09 16:04:36 -08:00
|
|
|
session.destroy();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
session.attach_buffer(&buffer.buffer, None, 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,
|
|
|
|
|
) {
|
|
|
|
|
let capture = Capture::for_session(session).unwrap();
|
2023-02-10 10:12:26 -08:00
|
|
|
if !capture.running() {
|
2023-02-09 16:04:36 -08:00
|
|
|
session.destroy();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut buffer = capture.buffer.lock().unwrap();
|
|
|
|
|
let image = unsafe { buffer.as_mut().unwrap().to_image() };
|
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-02-10 13:41:08 -08:00
|
|
|
let capture = Capture::for_session(session).unwrap();
|
|
|
|
|
capture.cancel();
|
2023-02-09 16:04:36 -08:00
|
|
|
session.destroy();
|
|
|
|
|
}
|
|
|
|
|
}
|