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:58:34 -08:00
|
|
|
use std::{
|
|
|
|
|
mem,
|
|
|
|
|
sync::{Arc, Weak},
|
|
|
|
|
};
|
2024-01-25 15:44:30 -08:00
|
|
|
|
|
|
|
|
use super::{AppData, Buffer, Capture, CaptureImage, CaptureSource, Event};
|
|
|
|
|
|
|
|
|
|
pub struct ScreencopySession {
|
2024-01-25 15:58:34 -08:00
|
|
|
buffers: Option<(Buffer, Buffer)>,
|
2024-01-25 15:44:30 -08:00
|
|
|
session: zcosmic_screencopy_session_v1::ZcosmicScreencopySessionV1,
|
2024-01-25 15:48:40 -08:00
|
|
|
first_frame: bool,
|
2024-01-25 15:44:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
2024-01-25 15:58:34 -08:00
|
|
|
buffers: None,
|
2024-01-25 15:44:30 -08:00
|
|
|
session,
|
2024-01-25 15:48:40 -08:00
|
|
|
first_frame: true,
|
2024-01-25 15:44:30 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-25 15:48:40 -08:00
|
|
|
fn attach_buffer_and_commit(&mut self, capture: &Capture, conn: &Connection) {
|
2024-01-25 15:58:34 -08:00
|
|
|
let Some((front, back)) = self.buffers.as_mut() else {
|
2024-01-25 15:44:30 -08:00
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
2024-01-25 15:58:34 -08:00
|
|
|
let node = front.node().and_then(|x| x.to_str().map(|x| x.to_string()));
|
2024-01-25 15:44:30 -08:00
|
|
|
|
2024-01-25 15:58:34 -08:00
|
|
|
self.session.attach_buffer(&back.buffer, node, 0); // XXX age?
|
2024-01-25 15:48:40 -08:00
|
|
|
if self.first_frame {
|
2024-01-25 15:44:30 -08:00
|
|
|
self.session
|
|
|
|
|
.commit(zcosmic_screencopy_session_v1::Options::empty());
|
2024-01-25 15:48:40 -08:00
|
|
|
self.first_frame = false;
|
2024-01-25 15:44:30 -08:00
|
|
|
} 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:58:34 -08:00
|
|
|
if !session.buffers.as_ref().map_or(false, |(front, _)| {
|
|
|
|
|
buffer_infos.contains(&front.buffer_info)
|
|
|
|
|
}) {
|
|
|
|
|
session.buffers = Some((
|
|
|
|
|
self.create_buffer(buffer_infos),
|
|
|
|
|
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-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:58:34 -08:00
|
|
|
if session.buffers.is_none() {
|
|
|
|
|
eprintln!("Error: No capture buffers?");
|
2023-12-12 11:59:32 -08:00
|
|
|
return;
|
|
|
|
|
}
|
2024-01-25 15:58:34 -08:00
|
|
|
|
|
|
|
|
let (front, back) = session.buffers.as_mut().unwrap();
|
|
|
|
|
mem::swap(front, back);
|
|
|
|
|
|
|
|
|
|
let img = unsafe { front.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]);
|