Skip to content

Commit

Permalink
WIP use update screencopy abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
ids1024 committed Feb 4, 2025
1 parent 726c828 commit b85ef01
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 110 deletions.
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,7 @@ ignored = ["libcosmic"]
[patch."https://github.com/smithay/client-toolkit.git"]
sctk = { package = "smithay-client-toolkit", version = "=0.19.2" }
[patch.'https://github.com/pop-os/cosmic-protocols']
cosmic-protocols = { git = "https://github.com/pop-os/cosmic-protocols//", rev = "d218c76" }
cosmic-client-toolkit = { git = "https://github.com/pop-os/cosmic-protocols//", rev = "d218c76" }
#cosmic-protocols = { git = "https://github.com/pop-os/cosmic-protocols//", rev = "d218c76" }
#cosmic-client-toolkit = { git = "https://github.com/pop-os/cosmic-protocols//", rev = "d218c76" }
cosmic-client-toolkit = { git = "https://github.com/pop-os/cosmic-protocols//", branch = "screencopy-improve" }
cosmic-protocols = { git = "https://github.com/pop-os/cosmic-protocols//", branch = "screencopy-improve" }
75 changes: 28 additions & 47 deletions cosmic-app-list/src/wayland_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use std::{

use cctk::{
screencopy::{
capture, Formats, Frame, ScreencopyFrameData, ScreencopyFrameDataExt, ScreencopyHandler,
CaptureFrame, CaptureOptions, CaptureSession, CaptureSource, Capturer, FailureReason,
Formats, Frame, ScreencopyFrameData, ScreencopyFrameDataExt, ScreencopyHandler,
ScreencopySessionData, ScreencopySessionDataExt, ScreencopyState,
},
sctk::{
Expand All @@ -36,15 +37,11 @@ use cctk::{
wl_shm_pool,
wl_surface::WlSurface,
},
Connection, Dispatch, Proxy, QueueHandle, WEnum,
Connection, Dispatch, QueueHandle, WEnum,
},
workspace::{WorkspaceHandler, WorkspaceState},
};
use cosmic_protocols::{
image_source::v1::client::zcosmic_toplevel_image_source_manager_v1::ZcosmicToplevelImageSourceManagerV1,
screencopy::v2::client::{
zcosmic_screencopy_frame_v2, zcosmic_screencopy_manager_v2, zcosmic_screencopy_session_v2,
},
toplevel_info::v1::client::zcosmic_toplevel_handle_v1::{self, ZcosmicToplevelHandleV1},
toplevel_management::v1::client::zcosmic_toplevel_manager_v1,
workspace::v1::client::zcosmic_workspace_handle_v1::State as WorkspaceUpdateState,
Expand Down Expand Up @@ -285,7 +282,7 @@ impl ToplevelInfoHandler for AppData {
#[derive(Default)]
struct SessionInner {
formats: Option<Formats>,
res: Option<Result<(), WEnum<zcosmic_screencopy_frame_v2::FailureReason>>>,
res: Option<Result<(), WEnum<FailureReason>>>,
}

// TODO: dmabuf? need to handle modifier negotation
Expand All @@ -303,13 +300,11 @@ struct SessionData {

struct FrameData {
frame_data: ScreencopyFrameData,
session: zcosmic_screencopy_session_v2::ZcosmicScreencopySessionV2,
session: CaptureSession,
}

impl Session {
pub fn for_session(
session: &zcosmic_screencopy_session_v2::ZcosmicScreencopySessionV2,
) -> Option<&Self> {
pub fn for_session(session: &CaptureSession) -> Option<&Self> {
Some(&session.data::<SessionData>()?.session)
}

Expand Down Expand Up @@ -365,8 +360,7 @@ struct CaptureData {
qh: QueueHandle<AppData>,
conn: Connection,
wl_shm: WlShm,
screencopy_manager: zcosmic_screencopy_manager_v2::ZcosmicScreencopyManagerV2,
toplevel_source_manager: ZcosmicToplevelImageSourceManagerV1,
capturer: Capturer,
}

impl CaptureData {
Expand All @@ -384,18 +378,19 @@ impl CaptureData {
let overlay_cursor = if overlay_cursor { 1 } else { 0 };

let session = Arc::new(Session::default());
let image_source = self
.toplevel_source_manager
.create_source(&source, &self.qh, ());
let screencopy_session = self.screencopy_manager.create_session(
&image_source,
zcosmic_screencopy_manager_v2::Options::empty(),
&self.qh,
SessionData {
session: session.clone(),
session_data: Default::default(),
},
);
// Unwrap assumes compositor supports this capture type
let capture_session = self
.capturer
.create_session(
&CaptureSource::CosmicToplevel(source),
CaptureOptions::empty(),
&self.qh,
SessionData {
session: session.clone(),
session_data: Default::default(),
},
)
.unwrap();
self.conn.flush().unwrap();

let formats = session
Expand Down Expand Up @@ -439,14 +434,13 @@ impl CaptureData {
(),
);

capture(
&screencopy_session,
capture_session.capture(
&buffer,
&[],
&self.qh,
FrameData {
frame_data: Default::default(),
session: screencopy_session.clone(),
session: capture_session.clone(),
},
);
self.conn.flush().unwrap();
Expand Down Expand Up @@ -491,12 +485,7 @@ impl AppData {
qh: self.queue_handle.clone(),
conn: self.conn.clone(),
wl_shm: self.shm_state.wl_shm().clone(),
screencopy_manager: self.screencopy_state.screencopy_manager.clone(),
toplevel_source_manager: self
.screencopy_state
.toplevel_source_manager
.clone()
.unwrap(),
capturer: self.screencopy_state.capturer().clone(),
};
std::thread::spawn(move || {
use std::ffi::CStr;
Expand Down Expand Up @@ -559,7 +548,7 @@ impl ScreencopyHandler for AppData {
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
session: &zcosmic_screencopy_session_v2::ZcosmicScreencopySessionV2,
session: &CaptureSession,
formats: &Formats,
) {
Session::for_session(session).unwrap().update(|data| {
Expand All @@ -571,38 +560,30 @@ impl ScreencopyHandler for AppData {
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
screencopy_frame: &zcosmic_screencopy_frame_v2::ZcosmicScreencopyFrameV2,
screencopy_frame: &CaptureFrame,
_frame: Frame,
) {
let session = &screencopy_frame.data::<FrameData>().unwrap().session;
Session::for_session(session).unwrap().update(|data| {
data.res = Some(Ok(()));
});
session.destroy();
}

fn failed(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
screencopy_frame: &zcosmic_screencopy_frame_v2::ZcosmicScreencopyFrameV2,
reason: WEnum<zcosmic_screencopy_frame_v2::FailureReason>,
screencopy_frame: &CaptureFrame,
reason: WEnum<FailureReason>,
) {
// TODO send message to thread
let session = &screencopy_frame.data::<FrameData>().unwrap().session;
Session::for_session(session).unwrap().update(|data| {
data.res = Some(Err(reason));
});
session.destroy();
}

fn stopped(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
_session: &zcosmic_screencopy_session_v2::ZcosmicScreencopySessionV2,
) {
}
fn stopped(&mut self, _conn: &Connection, _qh: &QueueHandle<Self>, _session: &CaptureSession) {}
}

pub(crate) fn wayland_handler(
Expand Down
Loading

0 comments on commit b85ef01

Please sign in to comment.