Split up wayland code into more files

This commit is contained in:
Ian Douglas Scott 2023-02-09 16:04:36 -08:00
parent a5b78a4300
commit b29f1149a4
6 changed files with 363 additions and 304 deletions

View file

@ -3,11 +3,13 @@ use cctk::{
sctk::{globals::ProvidesBoundGlobal, shm::raw::RawPool},
wayland_client::{
protocol::{wl_buffer, wl_shm},
QueueHandle,
Connection, Dispatch, QueueHandle,
},
};
use cosmic::iced::widget::image;
use super::AppData;
pub struct Buffer {
pub pool: RawPool,
pub buffer: wl_buffer::WlBuffer,
@ -18,7 +20,7 @@ impl Buffer {
pub fn new(
buffer_info: BufferInfo,
shm: &impl ProvidesBoundGlobal<wl_shm::WlShm, 1>,
qh: &QueueHandle<super::AppData>,
qh: &QueueHandle<AppData>,
) -> Self {
// Assume format is already known to be valid
let mut pool =
@ -57,3 +59,19 @@ impl Drop for Buffer {
self.buffer.destroy();
}
}
impl Dispatch<wl_buffer::WlBuffer, ()> for AppData {
fn event(
_app_data: &mut Self,
_buffer: &wl_buffer::WlBuffer,
event: wl_buffer::Event,
_: &(),
_: &Connection,
_qh: &QueueHandle<Self>,
) {
match event {
wl_buffer::Event::Release => {}
_ => unreachable!(),
}
}
}

116
src/wayland/capture.rs Normal file
View file

@ -0,0 +1,116 @@
use cctk::{
cosmic_protocols::{
screencopy::v1::client::{zcosmic_screencopy_manager_v1, zcosmic_screencopy_session_v1},
toplevel_info::v1::client::zcosmic_toplevel_handle_v1,
workspace::v1::client::zcosmic_workspace_handle_v1,
},
screencopy::{ScreencopySessionData, ScreencopySessionDataExt},
wayland_client::{protocol::wl_output, Proxy, QueueHandle},
};
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc, Mutex,
};
use super::{AppData, Buffer};
#[derive(Clone, PartialEq, Eq)]
pub enum CaptureSource {
Toplevel(zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1),
Workspace(
zcosmic_workspace_handle_v1::ZcosmicWorkspaceHandleV1,
wl_output::WlOutput,
),
}
#[allow(clippy::derive_hash_xor_eq)]
impl std::hash::Hash for CaptureSource {
fn hash<H>(&self, state: &mut H)
where
H: std::hash::Hasher,
{
match self {
Self::Toplevel(handle) => handle.id(),
Self::Workspace(handle, _output) => handle.id(),
}
.hash(state)
}
}
#[derive(Clone, Debug, Default)]
pub struct CaptureFilter {
pub workspaces_on_outputs: Vec<wl_output::WlOutput>,
pub toplevels_on_workspaces: Vec<zcosmic_workspace_handle_v1::ZcosmicWorkspaceHandleV1>,
}
pub struct Capture {
pub buffer: Mutex<Option<Buffer>>,
pub source: CaptureSource,
pub first_frame: AtomicBool,
pub cancelled: AtomicBool,
}
impl Capture {
pub fn new(source: CaptureSource) -> Capture {
Capture {
buffer: Mutex::new(None),
source,
first_frame: AtomicBool::new(true),
cancelled: AtomicBool::new(false),
}
}
pub fn for_session(
session: &zcosmic_screencopy_session_v1::ZcosmicScreencopySessionV1,
) -> Option<&Arc<Self>> {
Some(&session.data::<SessionData>()?.capture)
}
pub fn cancel(&self) {
self.cancelled.store(true, Ordering::SeqCst);
}
pub fn capture(
self: &Arc<Self>,
manager: &zcosmic_screencopy_manager_v1::ZcosmicScreencopyManagerV1,
qh: &QueueHandle<AppData>,
) {
let udata = SessionData {
session_data: Default::default(),
capture: self.clone(),
};
match &self.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,
);
}
}
}
}
struct SessionData {
session_data: ScreencopySessionData,
capture: Arc<Capture>,
}
impl ScreencopySessionDataExt for SessionData {
fn screencopy_session_data(&self) -> &ScreencopySessionData {
&self.session_data
}
}
cctk::delegate_screencopy!(AppData, session: [SessionData]);

View file

@ -8,15 +8,11 @@
use cctk::{
cosmic_protocols::{
screencopy::v1::client::{zcosmic_screencopy_manager_v1, zcosmic_screencopy_session_v1},
toplevel_info::v1::client::zcosmic_toplevel_handle_v1,
toplevel_management::v1::client::zcosmic_toplevel_manager_v1,
workspace::v1::client::{zcosmic_workspace_handle_v1, zcosmic_workspace_manager_v1},
},
screencopy::{
BufferInfo, ScreencopyHandler, ScreencopySessionData, ScreencopySessionDataExt,
ScreencopyState,
},
screencopy::ScreencopyState,
sctk::{
self,
event_loop::WaylandSource,
@ -25,15 +21,15 @@ use cctk::{
seat::{SeatHandler, SeatState},
shm::{ShmHandler, ShmState},
},
toplevel_info::{ToplevelInfo, ToplevelInfoHandler, ToplevelInfoState},
toplevel_management::{ToplevelManagerHandler, ToplevelManagerState},
toplevel_info::{ToplevelInfo, ToplevelInfoState},
toplevel_management::ToplevelManagerState,
wayland_client::{
backend::ObjectId,
globals::registry_queue_init,
protocol::{wl_buffer, wl_output, wl_seat, wl_shm},
Connection, Dispatch, Proxy, QueueHandle, WEnum,
protocol::{wl_output, wl_seat},
Connection, Proxy, QueueHandle,
},
workspace::{WorkspaceHandler, WorkspaceState},
workspace::WorkspaceState,
};
use cosmic::iced::{
self,
@ -41,18 +37,17 @@ use cosmic::iced::{
widget::image,
};
use futures_channel::mpsc;
use std::{
cell::RefCell,
collections::HashMap,
sync::{
atomic::{AtomicBool, Ordering},
Arc, Mutex,
},
thread,
};
use std::{cell::RefCell, collections::HashMap, sync::Arc, thread};
mod buffer;
use buffer::Buffer;
mod capture;
use capture::{Capture, CaptureSource};
mod screencopy;
mod toplevel;
mod workspace;
pub use capture::CaptureFilter;
// TODO define subscription for a particular output/workspace/toplevel (but we want to rate limit?)
@ -84,103 +79,11 @@ pub fn subscription() -> iced::Subscription<Event> {
iced::subscription::run("wayland-sub", async { start() }.flatten_stream())
}
#[derive(Clone, PartialEq, Eq)]
enum CaptureSource {
Toplevel(zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1),
Workspace(
zcosmic_workspace_handle_v1::ZcosmicWorkspaceHandleV1,
wl_output::WlOutput,
),
}
#[allow(clippy::derive_hash_xor_eq)]
impl std::hash::Hash for CaptureSource {
fn hash<H>(&self, state: &mut H)
where
H: std::hash::Hasher,
{
match self {
Self::Toplevel(handle) => handle.id(),
Self::Workspace(handle, _output) => handle.id(),
}
.hash(state)
}
}
#[derive(Clone, Debug, Default)]
pub struct CaptureFilter {
pub workspaces_on_outputs: Vec<wl_output::WlOutput>,
pub toplevels_on_workspaces: Vec<zcosmic_workspace_handle_v1::ZcosmicWorkspaceHandleV1>,
}
#[derive(Debug)]
pub enum Cmd {
CaptureFilter(CaptureFilter),
}
struct Capture {
buffer: Mutex<Option<Buffer>>,
source: CaptureSource,
first_frame: AtomicBool,
cancelled: AtomicBool,
}
impl Capture {
fn new(source: CaptureSource) -> Capture {
Capture {
buffer: Mutex::new(None),
source,
first_frame: AtomicBool::new(true),
cancelled: AtomicBool::new(false),
}
}
fn cancel(&self) {
self.cancelled.store(true, Ordering::SeqCst);
}
fn capture(
self: &Arc<Self>,
manager: &zcosmic_screencopy_manager_v1::ZcosmicScreencopyManagerV1,
qh: &QueueHandle<AppData>,
) {
let udata = SessionData {
session_data: Default::default(),
capture: self.clone(),
};
match &self.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,
);
}
}
}
}
struct SessionData {
session_data: ScreencopySessionData,
capture: Arc<Capture>,
}
impl ScreencopySessionDataExt for SessionData {
fn screencopy_session_data(&self) -> &ScreencopySessionData {
&self.session_data
}
}
pub struct AppData {
qh: QueueHandle<Self>,
output_state: OutputState,
@ -214,6 +117,7 @@ impl AppData {
}
fn invalidate_capture_filter(&mut self) {
//for i in self.captures
// XXX drain filter
// TODO cancel captures if needed, enable capture
}
@ -223,6 +127,12 @@ impl AppData {
capture.capture(&self.screencopy_state.screencopy_manager, &self.qh);
self.captures.borrow_mut().insert(source, capture);
}
fn remove_capture_source(&self, source: CaptureSource) {
if let Some(capture) = self.captures.borrow_mut().remove(&source) {
capture.cancel();
}
}
}
impl ProvidesRegistryState for AppData {
@ -308,193 +218,6 @@ impl OutputHandler for AppData {
}
}
// TODO any indication when we have all toplevels?
impl ToplevelInfoHandler for AppData {
fn toplevel_info_state(&mut self) -> &mut ToplevelInfoState {
&mut self.toplevel_info_state
}
fn new_toplevel(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
toplevel: &zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1,
) {
let info = self.toplevel_info_state.info(toplevel).unwrap();
self.send_event(Event::NewToplevel(toplevel.clone(), info.clone()));
self.add_capture_source(CaptureSource::Toplevel(toplevel.clone()));
}
fn update_toplevel(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
_toplevel: &zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1,
) {
// TODO
}
fn toplevel_closed(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
toplevel: &zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1,
) {
self.send_event(Event::CloseToplevel(toplevel.clone()));
}
}
impl WorkspaceHandler for AppData {
fn workspace_state(&mut self) -> &mut WorkspaceState {
&mut self.workspace_state
}
fn done(&mut self) {
let mut workspaces = Vec::new();
for group in self.workspace_state.workspace_groups() {
for workspace in &group.workspaces {
if let Some(output) = group.output.as_ref() {
let output_name = self.output_names.get(&output.id()).unwrap().clone();
workspaces.push((output_name, workspace.clone()));
self.add_capture_source(CaptureSource::Workspace(
workspace.handle.clone(),
output.clone(),
));
}
}
}
self.send_event(Event::Workspaces(workspaces));
}
}
impl ScreencopyHandler for AppData {
fn screencopy_state(&mut self) -> &mut ScreencopyState {
&mut self.screencopy_state
}
fn init_done(
&mut self,
conn: &Connection,
qh: &QueueHandle<Self>,
session: &zcosmic_screencopy_session_v1::ZcosmicScreencopySessionV1,
buffer_infos: &[BufferInfo],
) {
let capture = &session.data::<SessionData>().unwrap().capture;
if capture.cancelled.load(Ordering::SeqCst) {
session.destroy();
return;
}
let buffer_info = buffer_infos
.iter()
.find(|x| {
x.type_ == WEnum::Value(zcosmic_screencopy_session_v1::BufferType::WlShm)
&& x.format == wl_shm::Format::Abgr8888.into()
})
.unwrap();
// XXX fix in compositor
if buffer_info.width == 0 || buffer_info.height == 0 || buffer_info.stride == 0 {
session.destroy();
return;
}
let mut buffer = capture.buffer.lock().unwrap();
// Create new buffer if none, or different format
if !buffer
.as_ref()
.map_or(false, |x| &x.buffer_info == buffer_info)
{
*buffer = Some(Buffer::new(buffer_info.clone(), &self.shm_state, qh));
}
let buffer = buffer.as_ref().unwrap();
session.attach_buffer(&buffer.buffer, None, 0); // XXX age?
if capture.first_frame.load(Ordering::SeqCst) {
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 = &session.data::<SessionData>().unwrap().capture;
if capture.cancelled.load(Ordering::SeqCst) {
session.destroy();
return;
}
let mut buffer = capture.buffer.lock().unwrap();
let image = unsafe { buffer.as_mut().unwrap().to_image() };
let event = match &capture.source {
CaptureSource::Toplevel(toplevel) => Event::ToplevelCapture(toplevel.clone(), image),
CaptureSource::Workspace(workspace, _) => {
Event::WorkspaceCapture(workspace.clone(), image)
}
};
self.send_event(event);
session.destroy();
// Capture again on damage
capture.first_frame.store(false, Ordering::SeqCst);
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");
session.destroy();
}
}
impl ToplevelManagerHandler for AppData {
fn toplevel_manager_state(&mut self) -> &mut ToplevelManagerState {
&mut self.toplevel_manager_state
}
fn capabilities(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
_capabilities: Vec<
WEnum<zcosmic_toplevel_manager_v1::ZcosmicToplelevelManagementCapabilitiesV1>,
>,
) {
}
}
impl Dispatch<wl_buffer::WlBuffer, ()> for AppData {
fn event(
_app_data: &mut Self,
_buffer: &wl_buffer::WlBuffer,
event: wl_buffer::Event,
_: &(),
_: &Connection,
_qh: &QueueHandle<Self>,
) {
match event {
wl_buffer::Event::Release => {}
_ => unreachable!(),
}
}
}
fn start() -> mpsc::Receiver<Event> {
let (sender, receiver) = mpsc::channel(20);
@ -563,7 +286,3 @@ sctk::delegate_output!(AppData);
sctk::delegate_registry!(AppData);
sctk::delegate_seat!(AppData);
sctk::delegate_shm!(AppData);
cctk::delegate_toplevel_info!(AppData);
cctk::delegate_toplevel_manager!(AppData);
cctk::delegate_workspace!(AppData);
cctk::delegate_screencopy!(AppData, session: [SessionData]);

101
src/wayland/screencopy.rs Normal file
View file

@ -0,0 +1,101 @@
use cctk::{
cosmic_protocols::screencopy::v1::client::zcosmic_screencopy_session_v1,
screencopy::{BufferInfo, ScreencopyHandler, ScreencopyState},
wayland_client::{protocol::wl_shm, Connection, QueueHandle, WEnum},
};
use std::sync::atomic::Ordering;
use super::{AppData, Buffer, Capture, CaptureSource, Event};
impl ScreencopyHandler for AppData {
fn screencopy_state(&mut self) -> &mut ScreencopyState {
&mut self.screencopy_state
}
fn init_done(
&mut self,
conn: &Connection,
qh: &QueueHandle<Self>,
session: &zcosmic_screencopy_session_v1::ZcosmicScreencopySessionV1,
buffer_infos: &[BufferInfo],
) {
let capture = Capture::for_session(session).unwrap();
if capture.cancelled.load(Ordering::SeqCst) {
session.destroy();
return;
}
let buffer_info = buffer_infos
.iter()
.find(|x| {
x.type_ == WEnum::Value(zcosmic_screencopy_session_v1::BufferType::WlShm)
&& x.format == wl_shm::Format::Abgr8888.into()
})
.unwrap();
// XXX fix in compositor
if buffer_info.width == 0 || buffer_info.height == 0 || buffer_info.stride == 0 {
session.destroy();
return;
}
let mut buffer = capture.buffer.lock().unwrap();
// Create new buffer if none, or different format
if !buffer
.as_ref()
.map_or(false, |x| &x.buffer_info == buffer_info)
{
*buffer = Some(Buffer::new(buffer_info.clone(), &self.shm_state, qh));
}
let buffer = buffer.as_ref().unwrap();
session.attach_buffer(&buffer.buffer, None, 0); // XXX age?
if capture.first_frame.load(Ordering::SeqCst) {
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();
if capture.cancelled.load(Ordering::SeqCst) {
session.destroy();
return;
}
let mut buffer = capture.buffer.lock().unwrap();
let image = unsafe { buffer.as_mut().unwrap().to_image() };
let event = match &capture.source {
CaptureSource::Toplevel(toplevel) => Event::ToplevelCapture(toplevel.clone(), image),
CaptureSource::Workspace(workspace, _) => {
Event::WorkspaceCapture(workspace.clone(), image)
}
};
self.send_event(event);
session.destroy();
// Capture again on damage
capture.first_frame.store(false, Ordering::SeqCst);
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");
session.destroy();
}
}

69
src/wayland/toplevel.rs Normal file
View file

@ -0,0 +1,69 @@
use cctk::{
cosmic_protocols::{
toplevel_info::v1::client::zcosmic_toplevel_handle_v1,
toplevel_management::v1::client::zcosmic_toplevel_manager_v1,
},
toplevel_info::{ToplevelInfoHandler, ToplevelInfoState},
toplevel_management::{ToplevelManagerHandler, ToplevelManagerState},
wayland_client::{Connection, QueueHandle, WEnum},
};
use super::{AppData, CaptureSource, Event};
// TODO any indication when we have all toplevels?
impl ToplevelInfoHandler for AppData {
fn toplevel_info_state(&mut self) -> &mut ToplevelInfoState {
&mut self.toplevel_info_state
}
fn new_toplevel(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
toplevel: &zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1,
) {
let info = self.toplevel_info_state.info(toplevel).unwrap();
self.send_event(Event::NewToplevel(toplevel.clone(), info.clone()));
self.add_capture_source(CaptureSource::Toplevel(toplevel.clone()));
}
fn update_toplevel(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
_toplevel: &zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1,
) {
// TODO
}
fn toplevel_closed(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
toplevel: &zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1,
) {
self.send_event(Event::CloseToplevel(toplevel.clone()));
self.remove_capture_source(CaptureSource::Toplevel(toplevel.clone()));
}
}
impl ToplevelManagerHandler for AppData {
fn toplevel_manager_state(&mut self) -> &mut ToplevelManagerState {
&mut self.toplevel_manager_state
}
fn capabilities(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
_capabilities: Vec<
WEnum<zcosmic_toplevel_manager_v1::ZcosmicToplelevelManagementCapabilitiesV1>,
>,
) {
}
}
cctk::delegate_toplevel_info!(AppData);
cctk::delegate_toplevel_manager!(AppData);

36
src/wayland/workspace.rs Normal file
View file

@ -0,0 +1,36 @@
use cctk::{
wayland_client::Proxy,
workspace::{WorkspaceHandler, WorkspaceState},
};
use super::{AppData, CaptureSource, Event};
impl WorkspaceHandler for AppData {
fn workspace_state(&mut self) -> &mut WorkspaceState {
&mut self.workspace_state
}
fn done(&mut self) {
let mut workspaces = Vec::new();
// XXX remove capture source for removed workspaces
for group in self.workspace_state.workspace_groups() {
for workspace in &group.workspaces {
if let Some(output) = group.output.as_ref() {
let output_name = self.output_names.get(&output.id()).unwrap().clone();
workspaces.push((output_name, workspace.clone()));
self.add_capture_source(CaptureSource::Workspace(
workspace.handle.clone(),
output.clone(),
));
}
}
}
self.send_event(Event::Workspaces(workspaces));
}
}
cctk::delegate_workspace!(AppData);