Add a mock backend for testing (including on other compositors)
Should help determine which issues are cosmic-comp bugs.
This commit is contained in:
parent
170e102275
commit
c75a48535a
13 changed files with 397 additions and 184 deletions
216
src/backend/mock.rs
Normal file
216
src/backend/mock.rs
Normal file
|
|
@ -0,0 +1,216 @@
|
|||
// Copyright 2024 System76 <info@system76.com>
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
use cosmic::{
|
||||
cctk::{
|
||||
cosmic_protocols::{
|
||||
toplevel_info::v1::client::zcosmic_toplevel_handle_v1,
|
||||
workspace::v1::client::zcosmic_workspace_handle_v1,
|
||||
},
|
||||
wayland_client::{
|
||||
protocol::{wl_output, wl_shm},
|
||||
Connection, WEnum,
|
||||
},
|
||||
},
|
||||
iced::{
|
||||
self,
|
||||
futures::{executor::block_on, FutureExt, SinkExt},
|
||||
},
|
||||
iced_sctk::subsurface_widget::{Shmbuf, SubsurfaceBuffer},
|
||||
};
|
||||
|
||||
use futures_channel::mpsc;
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
fs,
|
||||
io::Write,
|
||||
sync::{
|
||||
atomic::{AtomicUsize, Ordering},
|
||||
Arc,
|
||||
},
|
||||
thread,
|
||||
};
|
||||
|
||||
use super::{CaptureImage, Cmd, Event};
|
||||
use crate::utils;
|
||||
|
||||
#[derive(Eq, PartialEq, Clone, Debug, Hash)]
|
||||
struct MockObjectId(usize);
|
||||
|
||||
fn create_solid_capture_image(r: u8, g: u8, b: u8) -> CaptureImage {
|
||||
let mut file = fs::File::from(utils::create_memfile().unwrap());
|
||||
|
||||
for i in 0..512 * 512 {
|
||||
file.write(&[r, g, b, 255]).unwrap();
|
||||
}
|
||||
|
||||
CaptureImage {
|
||||
width: 512,
|
||||
height: 512,
|
||||
wl_buffer: SubsurfaceBuffer::new(Arc::new(
|
||||
Shmbuf {
|
||||
fd: file.into(),
|
||||
offset: 0,
|
||||
width: 512,
|
||||
height: 512,
|
||||
stride: 512 * 4,
|
||||
format: wl_shm::Format::Abgr8888,
|
||||
}
|
||||
.into(),
|
||||
))
|
||||
.0,
|
||||
}
|
||||
}
|
||||
|
||||
impl MockObjectId {
|
||||
fn new() -> Self {
|
||||
static NEXT_MOCK_ID: AtomicUsize = AtomicUsize::new(0);
|
||||
Self(NEXT_MOCK_ID.fetch_add(1, Ordering::SeqCst))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, Clone, Debug, Hash)]
|
||||
pub struct ZcosmicWorkspaceHandleV1(MockObjectId);
|
||||
|
||||
#[derive(Eq, PartialEq, Clone, Debug, Hash)]
|
||||
pub struct ZcosmicToplevelHandleV1(MockObjectId);
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct CaptureFilter {
|
||||
pub workspaces_on_outputs: Vec<wl_output::WlOutput>,
|
||||
pub toplevels_on_workspaces: Vec<ZcosmicWorkspaceHandleV1>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct ToplevelInfo {
|
||||
pub title: String,
|
||||
pub app_id: String,
|
||||
pub state: HashSet<zcosmic_toplevel_handle_v1::State>,
|
||||
pub output: HashSet<wl_output::WlOutput>,
|
||||
pub workspace: HashSet<ZcosmicWorkspaceHandleV1>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Workspace {
|
||||
pub handle: ZcosmicWorkspaceHandleV1,
|
||||
pub name: String,
|
||||
// pub coordinates: Vec<u32>,
|
||||
pub state: Vec<WEnum<zcosmic_workspace_handle_v1::State>>,
|
||||
// pub capabilities: Vec<WEnum<zcosmic_workspace_handle_v1::ZcosmicWorkspaceCapabilitiesV1>>,
|
||||
// pub tiling: Option<WEnum<zcosmic_workspace_handle_v1::TilingState>>,
|
||||
}
|
||||
|
||||
pub fn subscription(conn: Connection) -> iced::Subscription<Event> {
|
||||
iced::subscription::run_with_id("wayland-mock-sub", async { start(conn) }.flatten_stream())
|
||||
}
|
||||
|
||||
struct AppData {
|
||||
sender: mpsc::Sender<Event>,
|
||||
outputs: Vec<wl_output::WlOutput>,
|
||||
workspaces: Vec<(HashSet<wl_output::WlOutput>, Workspace)>,
|
||||
}
|
||||
|
||||
impl AppData {
|
||||
fn send_event(&mut self, event: Event) {
|
||||
let _ = block_on(self.sender.send(event));
|
||||
}
|
||||
|
||||
fn add_output(&mut self, output: &wl_output::WlOutput) {
|
||||
// Add four workspaces for each output
|
||||
let mut new_workspaces = Vec::new();
|
||||
for i in 0..=4 {
|
||||
let workspace_handle = ZcosmicWorkspaceHandleV1(MockObjectId::new());
|
||||
let workspace = Workspace {
|
||||
handle: workspace_handle.clone(),
|
||||
name: format!("Workspace {i}"),
|
||||
state: if i == 0 {
|
||||
vec![WEnum::Value(zcosmic_workspace_handle_v1::State::Active)]
|
||||
} else {
|
||||
Vec::new()
|
||||
},
|
||||
};
|
||||
// Add three toplevels for each workspace
|
||||
for j in 0..=3 {
|
||||
let toplevel_handle = ZcosmicToplevelHandleV1(MockObjectId::new());
|
||||
let toplevel_info = ToplevelInfo {
|
||||
title: format!("App {}", j),
|
||||
app_id: "com.example.app".to_string(),
|
||||
state: if i == 0 {
|
||||
HashSet::from([zcosmic_toplevel_handle_v1::State::Activated])
|
||||
} else {
|
||||
HashSet::new()
|
||||
},
|
||||
output: HashSet::from([output.clone()]),
|
||||
workspace: HashSet::from([workspace_handle.clone()]),
|
||||
};
|
||||
self.send_event(Event::NewToplevel(toplevel_handle.clone(), toplevel_info));
|
||||
self.send_event(Event::ToplevelCapture(
|
||||
toplevel_handle,
|
||||
create_solid_capture_image(255, 0, 0),
|
||||
));
|
||||
}
|
||||
self.workspaces
|
||||
.push((HashSet::from([output.clone()]), workspace));
|
||||
new_workspaces.push(workspace_handle);
|
||||
}
|
||||
self.send_event(Event::Workspaces(self.workspaces.clone()));
|
||||
for workspace_handle in new_workspaces {
|
||||
self.send_event(Event::WorkspaceCapture(
|
||||
workspace_handle,
|
||||
output.clone(),
|
||||
create_solid_capture_image(0, 255, 0),
|
||||
));
|
||||
}
|
||||
self.outputs.push(output.clone());
|
||||
}
|
||||
|
||||
fn handle_cmd(&mut self, cmd: Cmd) {
|
||||
match cmd {
|
||||
Cmd::CaptureFilter(filter) => {
|
||||
for output in &filter.workspaces_on_outputs {
|
||||
if !self.outputs.contains(output) {
|
||||
self.add_output(output);
|
||||
}
|
||||
}
|
||||
}
|
||||
Cmd::ActivateToplevel(toplevel_handle) => {
|
||||
println!("Activate {:?}", toplevel_handle);
|
||||
}
|
||||
Cmd::CloseToplevel(toplevel_handle) => {
|
||||
println!("Close {:?}", toplevel_handle);
|
||||
}
|
||||
Cmd::MoveToplevelToWorkspace(toplevel_handle, workspace_handle, output) => {}
|
||||
Cmd::ActivateWorkspace(workspace_handle) => {
|
||||
println!("Activate {:?}", workspace_handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn start(conn: Connection) -> mpsc::Receiver<Event> {
|
||||
let (sender, receiver) = mpsc::channel(20);
|
||||
thread::spawn(move || {
|
||||
let mut event_loop = calloop::EventLoop::try_new().unwrap();
|
||||
let (cmd_sender, cmd_channel) = calloop::channel::channel();
|
||||
event_loop
|
||||
.handle()
|
||||
.insert_source(cmd_channel, |cmd, (), app_data: &mut AppData| match cmd {
|
||||
calloop::channel::Event::Msg(cmd) => app_data.handle_cmd(cmd),
|
||||
calloop::channel::Event::Closed => {}
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let mut app_data = AppData {
|
||||
sender,
|
||||
outputs: Vec::new(),
|
||||
workspaces: Vec::new(),
|
||||
};
|
||||
app_data.send_event(Event::CmdSender(cmd_sender));
|
||||
loop {
|
||||
event_loop.dispatch(None, &mut app_data).unwrap();
|
||||
}
|
||||
});
|
||||
receiver
|
||||
}
|
||||
|
||||
// TODO WorkspaceCapture, ToplevelCapture, NewToplevel, Workspaces
|
||||
75
src/backend/mod.rs
Normal file
75
src/backend/mod.rs
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
// Copyright 2023 System76 <info@system76.com>
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
//! The backend of getting workspace/toplevel information and previews, and
|
||||
//! sending commands to change them.
|
||||
//!
|
||||
//! There are two backends: one that uses cosmic-comp protocols, and a mock
|
||||
//! backend for testing without any special protocols.
|
||||
|
||||
use cosmic::{
|
||||
cctk::wayland_client::protocol::wl_output, iced_sctk::subsurface_widget::SubsurfaceBuffer,
|
||||
};
|
||||
use std::collections::HashSet;
|
||||
|
||||
// Wayland backend using cosmic-comp specific protocols
|
||||
#[cfg(not(feature = "mock-backend"))]
|
||||
mod wayland;
|
||||
#[cfg(not(feature = "mock-backend"))]
|
||||
pub use cosmic::cctk::{
|
||||
cosmic_protocols::{
|
||||
toplevel_info::v1::client::zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1,
|
||||
workspace::v1::client::zcosmic_workspace_handle_v1::ZcosmicWorkspaceHandleV1,
|
||||
},
|
||||
toplevel_info::ToplevelInfo,
|
||||
workspace::Workspace,
|
||||
};
|
||||
#[cfg(not(feature = "mock-backend"))]
|
||||
pub use wayland::subscription;
|
||||
|
||||
// Mock backend
|
||||
#[cfg(feature = "mock-backend")]
|
||||
mod mock;
|
||||
#[cfg(feature = "mock-backend")]
|
||||
pub use mock::{
|
||||
subscription, ToplevelInfo, Workspace, ZcosmicToplevelHandleV1, ZcosmicWorkspaceHandleV1,
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct CaptureFilter {
|
||||
pub workspaces_on_outputs: Vec<wl_output::WlOutput>,
|
||||
pub toplevels_on_workspaces: Vec<ZcosmicWorkspaceHandleV1>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct CaptureImage {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub wl_buffer: SubsurfaceBuffer,
|
||||
#[cfg(feature = "no-subsurfaces")]
|
||||
pub image: cosmic::widget::image::Handle,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Event {
|
||||
CmdSender(calloop::channel::Sender<Cmd>),
|
||||
Workspaces(Vec<(HashSet<wl_output::WlOutput>, Workspace)>),
|
||||
WorkspaceCapture(ZcosmicWorkspaceHandleV1, wl_output::WlOutput, CaptureImage),
|
||||
NewToplevel(ZcosmicToplevelHandleV1, ToplevelInfo),
|
||||
UpdateToplevel(ZcosmicToplevelHandleV1, ToplevelInfo),
|
||||
CloseToplevel(ZcosmicToplevelHandleV1),
|
||||
ToplevelCapture(ZcosmicToplevelHandleV1, CaptureImage),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Cmd {
|
||||
CaptureFilter(CaptureFilter),
|
||||
ActivateToplevel(ZcosmicToplevelHandleV1),
|
||||
CloseToplevel(ZcosmicToplevelHandleV1),
|
||||
MoveToplevelToWorkspace(
|
||||
ZcosmicToplevelHandleV1,
|
||||
ZcosmicWorkspaceHandleV1,
|
||||
wl_output::WlOutput,
|
||||
),
|
||||
ActivateWorkspace(ZcosmicWorkspaceHandleV1),
|
||||
}
|
||||
239
src/backend/wayland/buffer.rs
Normal file
239
src/backend/wayland/buffer.rs
Normal file
|
|
@ -0,0 +1,239 @@
|
|||
use cctk::{
|
||||
screencopy::Formats,
|
||||
wayland_client::{
|
||||
protocol::{wl_buffer, wl_shm, wl_shm_pool},
|
||||
Connection, Dispatch, QueueHandle,
|
||||
},
|
||||
};
|
||||
use cosmic::cctk;
|
||||
use cosmic::iced_sctk::subsurface_widget::{BufferSource, Dmabuf, Plane, Shmbuf};
|
||||
use rustix::{io::Errno, shm::ShmOFlags};
|
||||
use std::{
|
||||
os::fd::{AsFd, OwnedFd},
|
||||
path::{Path, PathBuf},
|
||||
sync::Arc,
|
||||
time::{SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
use wayland_protocols::wp::linux_dmabuf::zv1::client::zwp_linux_buffer_params_v1;
|
||||
|
||||
use super::AppData;
|
||||
use crate::utils;
|
||||
|
||||
pub struct Buffer {
|
||||
pub backing: Arc<BufferSource>,
|
||||
pub buffer: wl_buffer::WlBuffer,
|
||||
node: Option<PathBuf>,
|
||||
pub size: (u32, u32),
|
||||
#[cfg(feature = "no-subsurfaces")]
|
||||
pub mmap: memmap2::Mmap,
|
||||
}
|
||||
|
||||
impl AppData {
|
||||
fn create_shm_buffer(&self, format: u32, (width, height): (u32, u32)) -> Buffer {
|
||||
let fd = utils::create_memfile().unwrap(); // XXX?
|
||||
rustix::fs::ftruncate(&fd, width as u64 * height as u64 * 4).unwrap();
|
||||
|
||||
let pool = self.shm_state.wl_shm().create_pool(
|
||||
fd.as_fd(),
|
||||
width as i32 * height as i32 * 4,
|
||||
&self.qh,
|
||||
(),
|
||||
);
|
||||
|
||||
let format = wl_shm::Format::try_from(format).unwrap();
|
||||
let buffer = pool.create_buffer(
|
||||
0,
|
||||
width as i32,
|
||||
height as i32,
|
||||
width as i32 * 4,
|
||||
format,
|
||||
&self.qh,
|
||||
(),
|
||||
);
|
||||
|
||||
pool.destroy();
|
||||
|
||||
#[cfg(feature = "no-subsurfaces")]
|
||||
let mmap = unsafe { memmap2::Mmap::map(&fd).unwrap() };
|
||||
|
||||
Buffer {
|
||||
backing: Arc::new(
|
||||
Shmbuf {
|
||||
fd,
|
||||
offset: 0,
|
||||
width: width as i32,
|
||||
height: height as i32,
|
||||
stride: width as i32 * 4,
|
||||
format,
|
||||
}
|
||||
.into(),
|
||||
),
|
||||
buffer,
|
||||
#[cfg(feature = "no-subsurfaces")]
|
||||
mmap,
|
||||
node: None,
|
||||
size: (width, height),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "force-shm-screencopy"))]
|
||||
fn create_gbm_buffer(
|
||||
&self,
|
||||
format: u32,
|
||||
(width, height): (u32, u32),
|
||||
needs_linear: bool,
|
||||
) -> anyhow::Result<Option<Buffer>> {
|
||||
let (Some((node, gbm)), Some(feedback)) =
|
||||
(self.gbm.as_ref(), self.dmabuf_feedback.as_ref())
|
||||
else {
|
||||
return Ok(None);
|
||||
};
|
||||
let formats = feedback.format_table();
|
||||
|
||||
let modifiers = feedback
|
||||
.tranches()
|
||||
.iter()
|
||||
.flat_map(|x| &x.formats)
|
||||
.filter_map(|x| formats.get(*x as usize))
|
||||
.filter(|x| {
|
||||
x.format == format
|
||||
&& (!needs_linear || x.modifier == u64::from(gbm::Modifier::Linear))
|
||||
})
|
||||
.filter_map(|x| gbm::Modifier::try_from(x.modifier).ok())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
if modifiers.is_empty() {
|
||||
return Ok(None);
|
||||
};
|
||||
let gbm_format = gbm::Format::try_from(format)?;
|
||||
//dbg!(format, modifiers);
|
||||
let bo = if !modifiers.iter().all(|x| *x == gbm::Modifier::Invalid) {
|
||||
gbm.create_buffer_object_with_modifiers::<()>(
|
||||
width,
|
||||
height,
|
||||
gbm_format,
|
||||
modifiers.iter().copied(),
|
||||
)?
|
||||
} else {
|
||||
// TODO make sure this isn't used across different GPUs
|
||||
gbm.create_buffer_object::<()>(
|
||||
width,
|
||||
height,
|
||||
gbm_format,
|
||||
gbm::BufferObjectFlags::empty(),
|
||||
)?
|
||||
};
|
||||
|
||||
let mut planes = Vec::new();
|
||||
|
||||
let params = self.dmabuf_state.create_params(&self.qh)?;
|
||||
let modifier = bo.modifier()?;
|
||||
for i in 0..bo.plane_count()? as i32 {
|
||||
let plane_fd = bo.fd_for_plane(i)?;
|
||||
let plane_offset = bo.offset(i)?;
|
||||
let plane_stride = bo.stride_for_plane(i)?;
|
||||
params.add(
|
||||
plane_fd.as_fd(),
|
||||
i as u32,
|
||||
plane_offset,
|
||||
plane_stride,
|
||||
modifier.into(),
|
||||
);
|
||||
planes.push(Plane {
|
||||
fd: plane_fd,
|
||||
plane_idx: i as u32,
|
||||
offset: plane_offset,
|
||||
stride: plane_stride,
|
||||
});
|
||||
}
|
||||
let buffer = params
|
||||
.create_immed(
|
||||
width as i32,
|
||||
height as i32,
|
||||
format,
|
||||
zwp_linux_buffer_params_v1::Flags::empty(),
|
||||
&self.qh,
|
||||
)
|
||||
.0;
|
||||
|
||||
Ok(Some(Buffer {
|
||||
backing: Arc::new(
|
||||
Dmabuf {
|
||||
width: width as i32,
|
||||
height: height as i32,
|
||||
planes,
|
||||
format,
|
||||
modifier: modifier.into(),
|
||||
}
|
||||
.into(),
|
||||
),
|
||||
buffer,
|
||||
node: Some(node.clone()),
|
||||
size: (width, height),
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn create_buffer(&self, formats: &Formats) -> Buffer {
|
||||
// XXX Handle other formats?
|
||||
let format = u32::from(wl_shm::Format::Abgr8888);
|
||||
|
||||
#[cfg(not(feature = "force-shm-screencopy"))]
|
||||
if let Some((_, _modifiers)) = formats.dmabuf_formats.iter().find(|(f, _)| *f == format) {
|
||||
// TODO Restrict modifiers
|
||||
match self.create_gbm_buffer(format, formats.buffer_size, false) {
|
||||
Ok(Some(buffer)) => {
|
||||
return buffer;
|
||||
}
|
||||
Ok(None) => {}
|
||||
Err(err) => log::error!("Failed to create gbm buffer: {}", err),
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to shm buffer
|
||||
// Assume format is already known to be valid
|
||||
assert!(formats.shm_formats.contains(&format));
|
||||
self.create_shm_buffer(format, formats.buffer_size)
|
||||
}
|
||||
}
|
||||
|
||||
impl Buffer {
|
||||
// Use this when dmabuf/screencopy has a way to specify node
|
||||
#[allow(dead_code)]
|
||||
pub fn node(&self) -> Option<&Path> {
|
||||
self.node.as_deref()
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Buffer {
|
||||
fn drop(&mut self) {
|
||||
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!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Dispatch<wl_shm_pool::WlShmPool, ()> for AppData {
|
||||
fn event(
|
||||
_app_data: &mut Self,
|
||||
_shm: &wl_shm_pool::WlShmPool,
|
||||
_event: wl_shm_pool::Event,
|
||||
_: &(),
|
||||
_: &Connection,
|
||||
_qh: &QueueHandle<Self>,
|
||||
) {
|
||||
}
|
||||
}
|
||||
58
src/backend/wayland/capture.rs
Normal file
58
src/backend/wayland/capture.rs
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
use cctk::{
|
||||
cosmic_protocols::{
|
||||
screencopy::v2::client::zcosmic_screencopy_session_v2,
|
||||
toplevel_info::v1::client::zcosmic_toplevel_handle_v1,
|
||||
workspace::v1::client::zcosmic_workspace_handle_v1,
|
||||
},
|
||||
screencopy::ScreencopyState,
|
||||
wayland_client::{protocol::wl_output, Proxy, QueueHandle},
|
||||
};
|
||||
use cosmic::cctk;
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use super::{AppData, ScreencopySession, SessionData};
|
||||
|
||||
#[derive(Clone, Hash, PartialEq, Eq)]
|
||||
pub enum CaptureSource {
|
||||
Toplevel(zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1),
|
||||
Workspace(
|
||||
zcosmic_workspace_handle_v1::ZcosmicWorkspaceHandleV1,
|
||||
wl_output::WlOutput,
|
||||
),
|
||||
}
|
||||
|
||||
pub struct Capture {
|
||||
pub source: CaptureSource,
|
||||
pub session: Mutex<Option<ScreencopySession>>,
|
||||
}
|
||||
|
||||
impl Capture {
|
||||
pub fn new(source: CaptureSource) -> Arc<Capture> {
|
||||
Arc::new(Capture {
|
||||
source,
|
||||
session: Mutex::new(None),
|
||||
})
|
||||
}
|
||||
|
||||
// Returns `None` if capture is destroyed
|
||||
// (or if `session` wasn't created with `SessionData`)
|
||||
pub fn for_session(
|
||||
session: &zcosmic_screencopy_session_v2::ZcosmicScreencopySessionV2,
|
||||
) -> Option<Arc<Self>> {
|
||||
session.data::<SessionData>()?.capture.upgrade()
|
||||
}
|
||||
|
||||
// Start capturing frames
|
||||
pub fn start(self: &Arc<Self>, screencopy_state: &ScreencopyState, qh: &QueueHandle<AppData>) {
|
||||
let mut session = self.session.lock().unwrap();
|
||||
if session.is_none() {
|
||||
*session = Some(ScreencopySession::new(self, screencopy_state, qh));
|
||||
}
|
||||
}
|
||||
|
||||
// Stop capturing. Can be started again with `start`
|
||||
pub fn stop(&self) {
|
||||
self.session.lock().unwrap().take();
|
||||
}
|
||||
}
|
||||
82
src/backend/wayland/dmabuf.rs
Normal file
82
src/backend/wayland/dmabuf.rs
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
use cctk::{
|
||||
sctk::{
|
||||
self,
|
||||
dmabuf::{DmabufFeedback, DmabufHandler, DmabufState},
|
||||
},
|
||||
wayland_client::{protocol::wl_buffer, Connection, QueueHandle},
|
||||
};
|
||||
use cosmic::cctk;
|
||||
|
||||
use std::{fs, io, os::unix::fs::MetadataExt, path::PathBuf};
|
||||
|
||||
use wayland_protocols::wp::linux_dmabuf::zv1::client::{
|
||||
zwp_linux_buffer_params_v1::ZwpLinuxBufferParamsV1,
|
||||
zwp_linux_dmabuf_feedback_v1::ZwpLinuxDmabufFeedbackV1,
|
||||
};
|
||||
|
||||
use super::AppData;
|
||||
|
||||
impl DmabufHandler for AppData {
|
||||
fn dmabuf_state(&mut self) -> &mut DmabufState {
|
||||
&mut self.dmabuf_state
|
||||
}
|
||||
fn dmabuf_feedback(
|
||||
&mut self,
|
||||
_conn: &Connection,
|
||||
_qh: &QueueHandle<Self>,
|
||||
_proxy: &ZwpLinuxDmabufFeedbackV1,
|
||||
feedback: DmabufFeedback,
|
||||
) {
|
||||
if self.gbm.is_none() {
|
||||
#[allow(clippy::unnecessary_cast)]
|
||||
match find_gbm_device(feedback.main_device() as u64) {
|
||||
Ok(Some(gbm)) => {
|
||||
self.gbm = Some(gbm);
|
||||
}
|
||||
Ok(None) => {
|
||||
log::error!("Gbm main device '{}' not found", feedback.main_device());
|
||||
}
|
||||
Err(err) => {
|
||||
log::error!("Failed to open gbm main device: {}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
self.dmabuf_feedback = Some(feedback);
|
||||
}
|
||||
fn created(
|
||||
&mut self,
|
||||
_conn: &Connection,
|
||||
_qh: &QueueHandle<Self>,
|
||||
_params: &ZwpLinuxBufferParamsV1,
|
||||
_buffer: wl_buffer::WlBuffer,
|
||||
) {
|
||||
}
|
||||
fn failed(
|
||||
&mut self,
|
||||
_conn: &Connection,
|
||||
_qh: &QueueHandle<Self>,
|
||||
_params: &ZwpLinuxBufferParamsV1,
|
||||
) {
|
||||
}
|
||||
fn released(
|
||||
&mut self,
|
||||
_conn: &Connection,
|
||||
_qh: &QueueHandle<Self>,
|
||||
_buffer: &wl_buffer::WlBuffer,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
fn find_gbm_device(dev: u64) -> io::Result<Option<(PathBuf, gbm::Device<fs::File>)>> {
|
||||
for i in std::fs::read_dir("/dev/dri")? {
|
||||
let i = i?;
|
||||
if i.metadata()?.rdev() == dev {
|
||||
let file = fs::File::options().read(true).write(true).open(i.path())?;
|
||||
log::info!("Opened gbm main device '{}'", i.path().display());
|
||||
return Ok(Some((i.path(), gbm::Device::new(file)?)));
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
sctk::delegate_dmabuf!(AppData);
|
||||
264
src/backend/wayland/mod.rs
Normal file
264
src/backend/wayland/mod.rs
Normal file
|
|
@ -0,0 +1,264 @@
|
|||
// A thread handles screencopy, and other wayland protocols, returning information as a
|
||||
// subscription.
|
||||
|
||||
use calloop_wayland_source::WaylandSource;
|
||||
use cctk::{
|
||||
screencopy::ScreencopyState,
|
||||
sctk::{
|
||||
self,
|
||||
dmabuf::{DmabufFeedback, DmabufState},
|
||||
registry::{ProvidesRegistryState, RegistryState},
|
||||
seat::{SeatHandler, SeatState},
|
||||
shm::{Shm, ShmHandler},
|
||||
},
|
||||
toplevel_info::ToplevelInfoState,
|
||||
toplevel_management::ToplevelManagerState,
|
||||
wayland_client::{
|
||||
globals::registry_queue_init, protocol::wl_seat, Connection, Proxy, QueueHandle,
|
||||
},
|
||||
workspace::WorkspaceState,
|
||||
};
|
||||
use cosmic::cctk;
|
||||
use cosmic::iced::{
|
||||
self,
|
||||
futures::{executor::block_on, FutureExt, SinkExt},
|
||||
};
|
||||
use futures_channel::mpsc;
|
||||
use std::{cell::RefCell, collections::HashMap, fs, path::PathBuf, sync::Arc, thread};
|
||||
|
||||
mod buffer;
|
||||
use buffer::Buffer;
|
||||
mod capture;
|
||||
use capture::{Capture, CaptureSource};
|
||||
mod dmabuf;
|
||||
mod screencopy;
|
||||
use screencopy::{ScreencopySession, SessionData};
|
||||
mod toplevel;
|
||||
mod workspace;
|
||||
|
||||
use super::{CaptureFilter, CaptureImage, Cmd, Event};
|
||||
|
||||
pub fn subscription(conn: Connection) -> iced::Subscription<Event> {
|
||||
iced::subscription::run_with_id("wayland-sub", async { start(conn) }.flatten_stream())
|
||||
}
|
||||
|
||||
pub struct AppData {
|
||||
qh: QueueHandle<Self>,
|
||||
dmabuf_state: DmabufState,
|
||||
registry_state: RegistryState,
|
||||
toplevel_info_state: ToplevelInfoState,
|
||||
workspace_state: WorkspaceState,
|
||||
screencopy_state: ScreencopyState,
|
||||
seat_state: SeatState,
|
||||
shm_state: Shm,
|
||||
toplevel_manager_state: ToplevelManagerState,
|
||||
sender: mpsc::Sender<Event>,
|
||||
seats: Vec<wl_seat::WlSeat>,
|
||||
capture_filter: CaptureFilter,
|
||||
captures: RefCell<HashMap<CaptureSource, Arc<Capture>>>,
|
||||
dmabuf_feedback: Option<DmabufFeedback>,
|
||||
gbm: Option<(PathBuf, gbm::Device<fs::File>)>,
|
||||
scheduler: calloop::futures::Scheduler<()>,
|
||||
}
|
||||
|
||||
impl AppData {
|
||||
fn send_event(&mut self, event: Event) {
|
||||
let _ = block_on(self.sender.send(event));
|
||||
}
|
||||
|
||||
// Handle message from main thread
|
||||
fn handle_cmd(&mut self, cmd: Cmd) {
|
||||
match cmd {
|
||||
Cmd::CaptureFilter(filter) => {
|
||||
self.capture_filter = filter;
|
||||
self.invalidate_capture_filter();
|
||||
}
|
||||
Cmd::ActivateToplevel(toplevel_handle) => {
|
||||
if !self.seats.is_empty() {
|
||||
for seat in &self.seats {
|
||||
self.toplevel_manager_state
|
||||
.manager
|
||||
.activate(&toplevel_handle, seat);
|
||||
}
|
||||
}
|
||||
}
|
||||
Cmd::CloseToplevel(toplevel_handle) => {
|
||||
self.toplevel_manager_state.manager.close(&toplevel_handle);
|
||||
}
|
||||
Cmd::MoveToplevelToWorkspace(toplevel_handle, workspace_handle, output) => {
|
||||
if self.toplevel_manager_state.manager.version() >= 2 {
|
||||
self.toplevel_manager_state.manager.move_to_workspace(
|
||||
&toplevel_handle,
|
||||
&workspace_handle,
|
||||
&output,
|
||||
);
|
||||
}
|
||||
}
|
||||
Cmd::ActivateWorkspace(workspace_handle) => {
|
||||
if let Ok(workspace_manager) = self.workspace_state.workspace_manager().get() {
|
||||
workspace_handle.activate();
|
||||
workspace_manager.commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn matches_capture_filter(&self, source: &CaptureSource) -> bool {
|
||||
match source {
|
||||
CaptureSource::Toplevel(toplevel) => {
|
||||
let info = self.toplevel_info_state.info(toplevel).unwrap();
|
||||
info.workspace.iter().any(|workspace| {
|
||||
self.capture_filter
|
||||
.toplevels_on_workspaces
|
||||
.contains(workspace)
|
||||
})
|
||||
}
|
||||
CaptureSource::Workspace(_, output) => {
|
||||
self.capture_filter.workspaces_on_outputs.contains(output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn invalidate_capture_filter(&self) {
|
||||
for (source, capture) in self.captures.borrow_mut().iter_mut() {
|
||||
let matches = self.matches_capture_filter(source);
|
||||
if matches {
|
||||
capture.start(&self.screencopy_state, &self.qh);
|
||||
} else {
|
||||
capture.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn add_capture_source(&self, source: CaptureSource) {
|
||||
self.captures
|
||||
.borrow_mut()
|
||||
.entry(source.clone())
|
||||
.or_insert_with(|| {
|
||||
let matches = self.matches_capture_filter(&source);
|
||||
let capture = Capture::new(source);
|
||||
if matches {
|
||||
capture.start(&self.screencopy_state, &self.qh);
|
||||
}
|
||||
capture
|
||||
});
|
||||
}
|
||||
|
||||
fn remove_capture_source(&self, source: CaptureSource) {
|
||||
if let Some(capture) = self.captures.borrow_mut().remove(&source) {
|
||||
capture.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ProvidesRegistryState for AppData {
|
||||
fn registry(&mut self) -> &mut RegistryState {
|
||||
&mut self.registry_state
|
||||
}
|
||||
|
||||
sctk::registry_handlers!(SeatState);
|
||||
}
|
||||
|
||||
impl SeatHandler for AppData {
|
||||
fn seat_state(&mut self) -> &mut SeatState {
|
||||
&mut self.seat_state
|
||||
}
|
||||
|
||||
fn new_seat(&mut self, _: &Connection, _: &QueueHandle<Self>, seat: wl_seat::WlSeat) {
|
||||
self.seats.push(seat);
|
||||
}
|
||||
|
||||
fn remove_seat(&mut self, _: &Connection, _: &QueueHandle<Self>, seat: wl_seat::WlSeat) {
|
||||
if let Some(idx) = self.seats.iter().position(|i| i == &seat) {
|
||||
self.seats.remove(idx);
|
||||
}
|
||||
}
|
||||
|
||||
fn new_capability(
|
||||
&mut self,
|
||||
_: &Connection,
|
||||
_: &QueueHandle<Self>,
|
||||
_: wl_seat::WlSeat,
|
||||
_: sctk::seat::Capability,
|
||||
) {
|
||||
}
|
||||
fn remove_capability(
|
||||
&mut self,
|
||||
_: &Connection,
|
||||
_: &QueueHandle<Self>,
|
||||
_: wl_seat::WlSeat,
|
||||
_: sctk::seat::Capability,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
impl ShmHandler for AppData {
|
||||
fn shm_state(&mut self) -> &mut Shm {
|
||||
&mut self.shm_state
|
||||
}
|
||||
}
|
||||
|
||||
fn start(conn: Connection) -> mpsc::Receiver<Event> {
|
||||
let (sender, receiver) = mpsc::channel(20);
|
||||
|
||||
let (globals, event_queue) = registry_queue_init(&conn).unwrap();
|
||||
let qh = event_queue.handle();
|
||||
|
||||
let dmabuf_state = DmabufState::new(&globals, &qh);
|
||||
dmabuf_state.get_default_feedback(&qh).unwrap();
|
||||
|
||||
thread::spawn(move || {
|
||||
let (executor, scheduler) = calloop::futures::executor().unwrap();
|
||||
|
||||
let registry_state = RegistryState::new(&globals);
|
||||
let mut app_data = AppData {
|
||||
qh: qh.clone(),
|
||||
dmabuf_state,
|
||||
workspace_state: WorkspaceState::new(®istry_state, &qh), // Create before toplevel info state
|
||||
toplevel_info_state: ToplevelInfoState::new(®istry_state, &qh),
|
||||
toplevel_manager_state: ToplevelManagerState::new(®istry_state, &qh),
|
||||
screencopy_state: ScreencopyState::new(&globals, &qh),
|
||||
registry_state,
|
||||
seat_state: SeatState::new(&globals, &qh),
|
||||
shm_state: Shm::bind(&globals, &qh).unwrap(),
|
||||
sender,
|
||||
seats: Vec::new(),
|
||||
capture_filter: CaptureFilter::default(),
|
||||
captures: RefCell::new(HashMap::new()),
|
||||
dmabuf_feedback: None,
|
||||
gbm: None,
|
||||
scheduler,
|
||||
};
|
||||
|
||||
let (cmd_sender, cmd_channel) = calloop::channel::channel();
|
||||
app_data.send_event(Event::CmdSender(cmd_sender));
|
||||
|
||||
let mut event_loop = calloop::EventLoop::try_new().unwrap();
|
||||
WaylandSource::new(conn, event_queue)
|
||||
.insert(event_loop.handle())
|
||||
.unwrap();
|
||||
event_loop
|
||||
.handle()
|
||||
.insert_source(cmd_channel, |event, _, app_data| {
|
||||
if let calloop::channel::Event::Msg(msg) = event {
|
||||
app_data.handle_cmd(msg)
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
event_loop
|
||||
.handle()
|
||||
.insert_source(executor, |(), _, _| {})
|
||||
.unwrap();
|
||||
|
||||
loop {
|
||||
event_loop.dispatch(None, &mut app_data).unwrap();
|
||||
}
|
||||
});
|
||||
|
||||
receiver
|
||||
}
|
||||
|
||||
// Don't bind outputs; use `WlOutput` instances from iced-sctk
|
||||
sctk::delegate_registry!(AppData);
|
||||
sctk::delegate_seat!(AppData);
|
||||
sctk::delegate_shm!(AppData);
|
||||
252
src/backend/wayland/screencopy.rs
Normal file
252
src/backend/wayland/screencopy.rs
Normal file
|
|
@ -0,0 +1,252 @@
|
|||
use cosmic::cctk::{
|
||||
self,
|
||||
cosmic_protocols::screencopy::v2::client::{
|
||||
zcosmic_screencopy_frame_v2, zcosmic_screencopy_manager_v2, zcosmic_screencopy_session_v2,
|
||||
},
|
||||
screencopy::{
|
||||
capture, Formats, Frame, ScreencopyFrameData, ScreencopyFrameDataExt, ScreencopyHandler,
|
||||
ScreencopySessionData, ScreencopySessionDataExt, ScreencopyState,
|
||||
},
|
||||
wayland_client::{Connection, Proxy, QueueHandle, WEnum},
|
||||
};
|
||||
use cosmic::iced_sctk::subsurface_widget::{SubsurfaceBuffer, SubsurfaceBufferRelease};
|
||||
use std::{
|
||||
array,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
use super::{AppData, Buffer, Capture, CaptureImage, CaptureSource, Event};
|
||||
|
||||
pub struct ScreencopySession {
|
||||
// swapchain buffers
|
||||
buffers: Option<[Buffer; 2]>,
|
||||
session: zcosmic_screencopy_session_v2::ZcosmicScreencopySessionV2,
|
||||
// Future signaled when buffer is signaled.
|
||||
// if triple buffer is used, will need more than one.
|
||||
release: Option<SubsurfaceBufferRelease>,
|
||||
}
|
||||
|
||||
impl ScreencopySession {
|
||||
pub fn new(
|
||||
capture: &Arc<Capture>,
|
||||
screencopy_state: &ScreencopyState,
|
||||
qh: &QueueHandle<AppData>,
|
||||
) -> Self {
|
||||
let image_source = match &capture.source {
|
||||
CaptureSource::Toplevel(toplevel) => screencopy_state
|
||||
.toplevel_source_manager
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.create_source(toplevel, qh, ()),
|
||||
CaptureSource::Workspace(workspace, _output) => screencopy_state
|
||||
.workspace_source_manager
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.create_source(workspace, qh, ()),
|
||||
};
|
||||
|
||||
let udata = SessionData {
|
||||
session_data: Default::default(),
|
||||
capture: Arc::downgrade(capture),
|
||||
};
|
||||
|
||||
let session = screencopy_state.screencopy_manager.create_session(
|
||||
&image_source,
|
||||
zcosmic_screencopy_manager_v2::Options::empty(),
|
||||
qh,
|
||||
udata,
|
||||
);
|
||||
|
||||
Self {
|
||||
buffers: None,
|
||||
session,
|
||||
release: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn attach_buffer_and_commit(
|
||||
&mut self,
|
||||
_capture: &Capture,
|
||||
conn: &Connection,
|
||||
qh: &QueueHandle<AppData>,
|
||||
) {
|
||||
let Some(back) = self.buffers.as_ref().map(|x| &x[1]) else {
|
||||
return;
|
||||
};
|
||||
|
||||
// TODO
|
||||
// let node = back.node().and_then(|x| x.to_str().map(|x| x.to_string()));
|
||||
|
||||
capture(
|
||||
&self.session,
|
||||
&back.buffer,
|
||||
&[],
|
||||
qh,
|
||||
FrameData {
|
||||
frame_data: Default::default(),
|
||||
session: self.session.clone(),
|
||||
},
|
||||
);
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
struct FrameData {
|
||||
frame_data: ScreencopyFrameData,
|
||||
session: zcosmic_screencopy_session_v2::ZcosmicScreencopySessionV2,
|
||||
}
|
||||
|
||||
impl ScreencopyFrameDataExt for FrameData {
|
||||
fn screencopy_frame_data(&self) -> &ScreencopyFrameData {
|
||||
&self.frame_data
|
||||
}
|
||||
}
|
||||
|
||||
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_v2::ZcosmicScreencopySessionV2,
|
||||
formats: &Formats,
|
||||
) {
|
||||
let Some(capture) = Capture::for_session(session) else {
|
||||
return;
|
||||
};
|
||||
let mut session = capture.session.lock().unwrap();
|
||||
let Some(session) = session.as_mut() else {
|
||||
return;
|
||||
};
|
||||
|
||||
// Create new buffer if none
|
||||
// XXX What if formats have changed?
|
||||
if session.buffers.is_none() {
|
||||
session.buffers = Some(array::from_fn(|_| self.create_buffer(formats)));
|
||||
}
|
||||
|
||||
session.attach_buffer_and_commit(&capture, conn, &self.qh);
|
||||
}
|
||||
|
||||
fn ready(
|
||||
&mut self,
|
||||
conn: &Connection,
|
||||
qh: &QueueHandle<Self>,
|
||||
screencopy_frame: &zcosmic_screencopy_frame_v2::ZcosmicScreencopyFrameV2,
|
||||
_frame: Frame,
|
||||
) {
|
||||
let session = &screencopy_frame.data::<FrameData>().unwrap().session;
|
||||
let Some(capture) = Capture::for_session(session) else {
|
||||
return;
|
||||
};
|
||||
let mut session = capture.session.lock().unwrap();
|
||||
let Some(session) = session.as_mut() else {
|
||||
return;
|
||||
};
|
||||
|
||||
if session.buffers.is_none() {
|
||||
log::error!("No capture buffers?");
|
||||
return;
|
||||
}
|
||||
|
||||
// swap buffers
|
||||
session.buffers.as_mut().unwrap().rotate_left(1);
|
||||
|
||||
// Capture again on damage
|
||||
let capture_clone = capture.clone();
|
||||
let conn = conn.clone();
|
||||
let release = session.release.take();
|
||||
let qh = qh.clone();
|
||||
self.scheduler
|
||||
.schedule(async move {
|
||||
if let Some(release) = release {
|
||||
// Wait for buffer to be released by server
|
||||
release.await;
|
||||
}
|
||||
let mut session = capture_clone.session.lock().unwrap();
|
||||
let Some(session) = session.as_mut() else {
|
||||
return;
|
||||
};
|
||||
session.attach_buffer_and_commit(&capture_clone, &conn, &qh);
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let front = session.buffers.as_mut().unwrap().first_mut().unwrap();
|
||||
let (buffer, release) = SubsurfaceBuffer::new(front.backing.clone());
|
||||
session.release = Some(release);
|
||||
let image = CaptureImage {
|
||||
wl_buffer: buffer,
|
||||
width: front.size.0,
|
||||
height: front.size.1,
|
||||
#[cfg(feature = "no-subsurfaces")]
|
||||
image: cosmic::widget::image::Handle::from_pixels(
|
||||
front.size.0,
|
||||
front.size.1,
|
||||
front.mmap.to_vec(),
|
||||
),
|
||||
};
|
||||
match &capture.source {
|
||||
CaptureSource::Toplevel(toplevel) => {
|
||||
self.send_event(Event::ToplevelCapture(toplevel.clone(), image))
|
||||
}
|
||||
CaptureSource::Workspace(workspace, output) => {
|
||||
self.send_event(Event::WorkspaceCapture(
|
||||
workspace.clone(),
|
||||
output.clone(),
|
||||
image,
|
||||
));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn failed(
|
||||
&mut self,
|
||||
_conn: &Connection,
|
||||
_qh: &QueueHandle<Self>,
|
||||
screencopy_frame: &zcosmic_screencopy_frame_v2::ZcosmicScreencopyFrameV2,
|
||||
reason: WEnum<zcosmic_screencopy_frame_v2::FailureReason>,
|
||||
) {
|
||||
// TODO
|
||||
log::error!("Screencopy failed: {:?}", reason);
|
||||
let session = &screencopy_frame.data::<FrameData>().unwrap().session;
|
||||
if let Some(capture) = Capture::for_session(session) {
|
||||
capture.stop();
|
||||
}
|
||||
}
|
||||
|
||||
fn stopped(
|
||||
&mut self,
|
||||
_conn: &Connection,
|
||||
_qh: &QueueHandle<Self>,
|
||||
session: &zcosmic_screencopy_session_v2::ZcosmicScreencopySessionV2,
|
||||
) {
|
||||
// TODO
|
||||
if let Some(capture) = Capture::for_session(session) {
|
||||
capture.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cctk::delegate_screencopy!(AppData, session: [SessionData], frame: [FrameData]);
|
||||
71
src/backend/wayland/toplevel.rs
Normal file
71
src/backend/wayland/toplevel.rs
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
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 cosmic::cctk;
|
||||
|
||||
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,
|
||||
) {
|
||||
let info = self.toplevel_info_state.info(toplevel).unwrap();
|
||||
self.send_event(Event::UpdateToplevel(toplevel.clone(), info.clone()));
|
||||
}
|
||||
|
||||
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);
|
||||
34
src/backend/wayland/workspace.rs
Normal file
34
src/backend/wayland/workspace.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
use cctk::workspace::{WorkspaceHandler, WorkspaceState};
|
||||
use cosmic::cctk;
|
||||
|
||||
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
|
||||
// Handle move to another output
|
||||
|
||||
for group in self.workspace_state.workspace_groups() {
|
||||
for workspace in &group.workspaces {
|
||||
workspaces.push((group.outputs.iter().cloned().collect(), workspace.clone()));
|
||||
|
||||
for output in &group.outputs {
|
||||
self.add_capture_source(CaptureSource::Workspace(
|
||||
workspace.handle.clone(),
|
||||
output.clone(),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.send_event(Event::Workspaces(workspaces));
|
||||
}
|
||||
}
|
||||
|
||||
cctk::delegate_workspace!(AppData);
|
||||
Loading…
Add table
Add a link
Reference in a new issue