From b85ef01bdf949ad5d7638ed588cba29c2d37dd05 Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Thu, 30 Jan 2025 15:53:50 -0800 Subject: [PATCH] WIP use update screencopy abstraction --- Cargo.lock | 20 ++--- Cargo.toml | 6 +- cosmic-app-list/src/wayland_handler.rs | 75 +++++++---------- cosmic-applet-minimize/src/wayland_handler.rs | 80 +++++++------------ 4 files changed, 71 insertions(+), 110 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dbac25a3..ac36dfaa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1362,7 +1362,7 @@ dependencies = [ [[package]] name = "cosmic-client-toolkit" version = "0.1.0" -source = "git+https://github.com/pop-os/cosmic-protocols//?rev=d218c76#d218c76b58c7a3b20dd5e7943f93fc306a1b81b8" +source = "git+https://github.com/pop-os/cosmic-protocols//?branch=screencopy-improve#b6e1404fba78e81c04269ce10b8fec63241ed223" dependencies = [ "cosmic-protocols", "libc", @@ -1501,7 +1501,7 @@ dependencies = [ [[package]] name = "cosmic-protocols" version = "0.1.0" -source = "git+https://github.com/pop-os/cosmic-protocols//?rev=d218c76#d218c76b58c7a3b20dd5e7943f93fc306a1b81b8" +source = "git+https://github.com/pop-os/cosmic-protocols//?branch=screencopy-improve#b6e1404fba78e81c04269ce10b8fec63241ed223" dependencies = [ "bitflags 2.7.0", "wayland-backend", @@ -2098,7 +2098,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" dependencies = [ "libc", - "windows-sys 0.59.0", + "windows-sys 0.52.0", ] [[package]] @@ -4506,7 +4506,7 @@ version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af1844ef2428cc3e1cb900be36181049ef3d3193c63e43026cfe202983b27a56" dependencies = [ - "proc-macro-crate 3.2.0", + "proc-macro-crate 1.3.1", "proc-macro2", "quote", "syn 2.0.96", @@ -5574,7 +5574,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.4.15", - "windows-sys 0.59.0", + "windows-sys 0.52.0", ] [[package]] @@ -6099,7 +6099,7 @@ dependencies = [ "getrandom", "once_cell", "rustix 0.38.43", - "windows-sys 0.59.0", + "windows-sys 0.52.0", ] [[package]] @@ -6907,7 +6907,7 @@ dependencies = [ "js-sys", "log", "naga", - "parking_lot 0.12.3", + "parking_lot 0.11.2", "profiling", "raw-window-handle", "smallvec", @@ -6935,7 +6935,7 @@ dependencies = [ "log", "naga", "once_cell", - "parking_lot 0.12.3", + "parking_lot 0.11.2", "profiling", "raw-window-handle", "rustc-hash 1.1.0", @@ -6976,7 +6976,7 @@ dependencies = [ "ndk-sys 0.5.0+25.2.9519653", "objc", "once_cell", - "parking_lot 0.12.3", + "parking_lot 0.11.2", "profiling", "range-alloc", "raw-window-handle", @@ -7029,7 +7029,7 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" dependencies = [ - "windows-sys 0.59.0", + "windows-sys 0.48.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index e1d85d66..666cd921 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/cosmic-app-list/src/wayland_handler.rs b/cosmic-app-list/src/wayland_handler.rs index e943cbba..9c5069bf 100644 --- a/cosmic-app-list/src/wayland_handler.rs +++ b/cosmic-app-list/src/wayland_handler.rs @@ -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::{ @@ -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, @@ -285,7 +282,7 @@ impl ToplevelInfoHandler for AppData { #[derive(Default)] struct SessionInner { formats: Option, - res: Option>>, + res: Option>>, } // TODO: dmabuf? need to handle modifier negotation @@ -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::()?.session) } @@ -365,8 +360,7 @@ struct CaptureData { qh: QueueHandle, conn: Connection, wl_shm: WlShm, - screencopy_manager: zcosmic_screencopy_manager_v2::ZcosmicScreencopyManagerV2, - toplevel_source_manager: ZcosmicToplevelImageSourceManagerV1, + capturer: Capturer, } impl CaptureData { @@ -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 @@ -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(); @@ -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; @@ -559,7 +548,7 @@ impl ScreencopyHandler for AppData { &mut self, _conn: &Connection, _qh: &QueueHandle, - session: &zcosmic_screencopy_session_v2::ZcosmicScreencopySessionV2, + session: &CaptureSession, formats: &Formats, ) { Session::for_session(session).unwrap().update(|data| { @@ -571,38 +560,30 @@ impl ScreencopyHandler for AppData { &mut self, _conn: &Connection, _qh: &QueueHandle, - screencopy_frame: &zcosmic_screencopy_frame_v2::ZcosmicScreencopyFrameV2, + screencopy_frame: &CaptureFrame, _frame: Frame, ) { let session = &screencopy_frame.data::().unwrap().session; Session::for_session(session).unwrap().update(|data| { data.res = Some(Ok(())); }); - session.destroy(); } fn failed( &mut self, _conn: &Connection, _qh: &QueueHandle, - screencopy_frame: &zcosmic_screencopy_frame_v2::ZcosmicScreencopyFrameV2, - reason: WEnum, + screencopy_frame: &CaptureFrame, + reason: WEnum, ) { // TODO send message to thread let session = &screencopy_frame.data::().unwrap().session; Session::for_session(session).unwrap().update(|data| { data.res = Some(Err(reason)); }); - session.destroy(); } - fn stopped( - &mut self, - _conn: &Connection, - _qh: &QueueHandle, - _session: &zcosmic_screencopy_session_v2::ZcosmicScreencopySessionV2, - ) { - } + fn stopped(&mut self, _conn: &Connection, _qh: &QueueHandle, _session: &CaptureSession) {} } pub(crate) fn wayland_handler( diff --git a/cosmic-applet-minimize/src/wayland_handler.rs b/cosmic-applet-minimize/src/wayland_handler.rs index d1b39f0d..6960018c 100644 --- a/cosmic-applet-minimize/src/wayland_handler.rs +++ b/cosmic-applet-minimize/src/wayland_handler.rs @@ -26,17 +26,12 @@ use cosmic::{ cctk::{ self, cosmic_protocols::{ - self, - 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::ZcosmicToplevelHandleV1, + self, toplevel_info::v1::client::zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1, }, screencopy::{ - capture, Formats, Frame, ScreencopyFrameData, ScreencopyFrameDataExt, - ScreencopyHandler, ScreencopySessionData, ScreencopySessionDataExt, ScreencopyState, + CaptureFrame, CaptureOptions, CaptureSession, CaptureSource, Capturer, FailureReason, + Formats, Frame, ScreencopyFrameData, ScreencopyFrameDataExt, ScreencopyHandler, + ScreencopySessionData, ScreencopySessionDataExt, ScreencopyState, }, sctk::shm::{Shm, ShmHandler}, wayland_client::{ @@ -45,7 +40,7 @@ use cosmic::{ wl_shm::{self, WlShm}, wl_shm_pool, }, - Dispatch, Proxy, + Dispatch, }, }, iced_futures::futures, @@ -61,7 +56,7 @@ use wayland_client::{globals::registry_queue_init, Connection, QueueHandle}; #[derive(Default)] struct SessionInner { formats: Option, - res: Option>>, + res: Option>>, } // TODO: dmabuf? need to handle modifier negotation @@ -79,13 +74,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::()?.session) } @@ -130,8 +123,7 @@ struct CaptureData { qh: QueueHandle, conn: Connection, wl_shm: WlShm, - screencopy_manager: zcosmic_screencopy_manager_v2::ZcosmicScreencopyManagerV2, - toplevel_source_manager: ZcosmicToplevelImageSourceManagerV1, + capturer: Capturer, } impl CaptureData { @@ -149,18 +141,18 @@ 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(), - }, - ); + 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 @@ -204,14 +196,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(); @@ -306,12 +297,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; @@ -509,7 +495,7 @@ impl ScreencopyHandler for AppData { &mut self, _conn: &Connection, _qh: &QueueHandle, - session: &zcosmic_screencopy_session_v2::ZcosmicScreencopySessionV2, + session: &CaptureSession, formats: &Formats, ) { Session::for_session(session).unwrap().update(|data| { @@ -521,38 +507,30 @@ impl ScreencopyHandler for AppData { &mut self, _conn: &Connection, _qh: &QueueHandle, - screencopy_frame: &zcosmic_screencopy_frame_v2::ZcosmicScreencopyFrameV2, + screencopy_frame: &CaptureFrame, _frame: Frame, ) { let session = &screencopy_frame.data::().unwrap().session; Session::for_session(session).unwrap().update(|data| { data.res = Some(Ok(())); }); - session.destroy(); } fn failed( &mut self, _conn: &Connection, _qh: &QueueHandle, - screencopy_frame: &zcosmic_screencopy_frame_v2::ZcosmicScreencopyFrameV2, - reason: WEnum, + screencopy_frame: &CaptureFrame, + reason: WEnum, ) { // TODO send message to thread let session = &screencopy_frame.data::().unwrap().session; Session::for_session(session).unwrap().update(|data| { data.res = Some(Err(reason)); }); - session.destroy(); } - fn stopped( - &mut self, - _conn: &Connection, - _qh: &QueueHandle, - _session: &zcosmic_screencopy_session_v2::ZcosmicScreencopySessionV2, - ) { - } + fn stopped(&mut self, _conn: &Connection, _qh: &QueueHandle, _session: &CaptureSession) {} } impl Dispatch for AppData {