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
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2024-01-24 15:29:03 -08:00
|
|
|
drop(buffer);
|
|
|
|
|
|
2024-01-24 15:59:18 -08:00
|
|
|
capture.attach_buffer_and_commit(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;
|
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
};
|
2024-01-24 15:29:03 -08:00
|
|
|
|
|
|
|
|
drop(buffer);
|
2023-02-09 16:04:36 -08:00
|
|
|
|
|
|
|
|
// Capture again on damage
|
2024-01-24 15:29:03 -08:00
|
|
|
capture.attach_buffer_and_commit(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
|
|
|
}
|
|
|
|
|
}
|