2024-01-24 15:29:03 -08:00
|
|
|
// A thread handles screencopy, and other wayland protocols, returning information as a
|
|
|
|
|
// subscription.
|
2022-12-30 14:07:39 -08:00
|
|
|
|
2024-03-01 13:07:37 -08:00
|
|
|
use calloop_wayland_source::WaylandSource;
|
2022-12-30 14:07:39 -08:00
|
|
|
use cctk::{
|
|
|
|
|
cosmic_protocols::{
|
|
|
|
|
toplevel_info::v1::client::zcosmic_toplevel_handle_v1,
|
2024-04-23 11:54:05 -07:00
|
|
|
workspace::v1::client::zcosmic_workspace_handle_v1,
|
2022-12-30 14:07:39 -08:00
|
|
|
},
|
2023-02-09 16:04:36 -08:00
|
|
|
screencopy::ScreencopyState,
|
2022-12-30 14:07:39 -08:00
|
|
|
sctk::{
|
|
|
|
|
self,
|
2023-11-08 14:32:33 -08:00
|
|
|
dmabuf::{DmabufFeedback, DmabufState},
|
2022-12-30 14:07:39 -08:00
|
|
|
registry::{ProvidesRegistryState, RegistryState},
|
2023-01-04 15:41:19 -08:00
|
|
|
seat::{SeatHandler, SeatState},
|
2023-03-16 14:08:20 -07:00
|
|
|
shm::{Shm, ShmHandler},
|
2022-12-30 14:07:39 -08:00
|
|
|
},
|
2023-02-09 16:04:36 -08:00
|
|
|
toplevel_info::{ToplevelInfo, ToplevelInfoState},
|
|
|
|
|
toplevel_management::ToplevelManagerState,
|
2022-12-30 14:07:39 -08:00
|
|
|
wayland_client::{
|
|
|
|
|
globals::registry_queue_init,
|
2024-03-05 12:25:20 -08:00
|
|
|
protocol::{wl_output, wl_seat},
|
2024-04-23 11:54:05 -07:00
|
|
|
Connection, Proxy, QueueHandle,
|
2022-12-30 14:07:39 -08:00
|
|
|
},
|
2023-02-09 16:04:36 -08:00
|
|
|
workspace::WorkspaceState,
|
2022-12-30 14:07:39 -08:00
|
|
|
};
|
2023-11-21 16:15:02 -05:00
|
|
|
use cosmic::cctk;
|
2023-01-17 12:36:05 -08:00
|
|
|
use cosmic::iced::{
|
|
|
|
|
self,
|
2022-12-30 14:07:39 -08:00
|
|
|
futures::{executor::block_on, FutureExt, SinkExt},
|
|
|
|
|
};
|
2023-11-08 16:59:29 -08:00
|
|
|
use cosmic::iced_sctk::subsurface_widget::SubsurfaceBuffer;
|
2023-01-17 12:36:05 -08:00
|
|
|
use futures_channel::mpsc;
|
2023-11-03 13:06:43 -07:00
|
|
|
use std::{
|
|
|
|
|
cell::RefCell,
|
|
|
|
|
collections::{HashMap, HashSet},
|
2023-11-08 14:54:42 -08:00
|
|
|
fs,
|
2023-11-09 13:45:28 -08:00
|
|
|
path::PathBuf,
|
2023-11-03 13:06:43 -07:00
|
|
|
sync::Arc,
|
|
|
|
|
thread,
|
|
|
|
|
};
|
2022-12-30 14:07:39 -08:00
|
|
|
|
2023-02-09 14:10:31 -08:00
|
|
|
mod buffer;
|
|
|
|
|
use buffer::Buffer;
|
2023-02-09 16:04:36 -08:00
|
|
|
mod capture;
|
|
|
|
|
use capture::{Capture, CaptureSource};
|
2023-11-08 14:32:33 -08:00
|
|
|
mod dmabuf;
|
2023-02-09 16:04:36 -08:00
|
|
|
mod screencopy;
|
2024-01-25 15:44:30 -08:00
|
|
|
use screencopy::{ScreencopySession, SessionData};
|
2023-02-09 16:04:36 -08:00
|
|
|
mod toplevel;
|
|
|
|
|
mod workspace;
|
|
|
|
|
|
|
|
|
|
pub use capture::CaptureFilter;
|
2023-02-09 14:10:31 -08:00
|
|
|
|
2022-12-30 14:07:39 -08:00
|
|
|
// TODO define subscription for a particular output/workspace/toplevel (but we want to rate limit?)
|
|
|
|
|
|
2022-12-30 15:21:05 -08:00
|
|
|
#[derive(Clone, Debug)]
|
2022-12-30 14:07:39 -08:00
|
|
|
pub enum Event {
|
2023-02-09 14:00:22 -08:00
|
|
|
CmdSender(calloop::channel::Sender<Cmd>),
|
2023-11-08 13:59:53 -08:00
|
|
|
Workspaces(Vec<(HashSet<wl_output::WlOutput>, cctk::workspace::Workspace)>),
|
2022-12-30 14:07:39 -08:00
|
|
|
WorkspaceCapture(
|
|
|
|
|
zcosmic_workspace_handle_v1::ZcosmicWorkspaceHandleV1,
|
2023-11-08 13:59:53 -08:00
|
|
|
wl_output::WlOutput,
|
2023-11-16 19:38:42 -08:00
|
|
|
CaptureImage,
|
2022-12-30 14:07:39 -08:00
|
|
|
),
|
2022-12-30 15:21:05 -08:00
|
|
|
NewToplevel(
|
|
|
|
|
zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1,
|
|
|
|
|
ToplevelInfo,
|
|
|
|
|
),
|
2023-02-10 13:41:08 -08:00
|
|
|
UpdateToplevel(
|
|
|
|
|
zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1,
|
|
|
|
|
ToplevelInfo,
|
|
|
|
|
),
|
2023-01-18 11:01:47 -08:00
|
|
|
CloseToplevel(zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1),
|
2022-12-30 15:21:05 -08:00
|
|
|
ToplevelCapture(
|
|
|
|
|
zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1,
|
2023-11-16 19:38:42 -08:00
|
|
|
CaptureImage,
|
2022-12-30 15:21:05 -08:00
|
|
|
),
|
2022-12-30 14:07:39 -08:00
|
|
|
}
|
|
|
|
|
|
2023-11-16 19:38:42 -08:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub struct CaptureImage {
|
2023-11-08 16:59:29 -08:00
|
|
|
pub width: u32,
|
|
|
|
|
pub height: u32,
|
|
|
|
|
pub wl_buffer: SubsurfaceBuffer,
|
2024-04-01 15:13:51 -07:00
|
|
|
#[cfg(feature = "no-subsurfaces")]
|
|
|
|
|
pub image: cosmic::widget::image::Handle,
|
2023-11-16 19:38:42 -08:00
|
|
|
}
|
|
|
|
|
|
2023-11-08 13:31:13 -08:00
|
|
|
pub fn subscription(conn: Connection) -> iced::Subscription<Event> {
|
|
|
|
|
iced::subscription::run_with_id("wayland-sub", async { start(conn) }.flatten_stream())
|
2022-12-30 14:07:39 -08:00
|
|
|
}
|
|
|
|
|
|
2023-02-09 14:00:22 -08:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub enum Cmd {
|
|
|
|
|
CaptureFilter(CaptureFilter),
|
2024-04-23 11:54:05 -07:00
|
|
|
ActivateToplevel(zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1),
|
|
|
|
|
CloseToplevel(zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1),
|
|
|
|
|
MoveToplevelToWorkspace(
|
|
|
|
|
zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1,
|
|
|
|
|
zcosmic_workspace_handle_v1::ZcosmicWorkspaceHandleV1,
|
|
|
|
|
wl_output::WlOutput,
|
|
|
|
|
),
|
|
|
|
|
ActivateWorkspace(zcosmic_workspace_handle_v1::ZcosmicWorkspaceHandleV1),
|
2023-02-09 14:00:22 -08:00
|
|
|
}
|
|
|
|
|
|
2023-01-04 14:41:44 -08:00
|
|
|
pub struct AppData {
|
2022-12-30 14:07:39 -08:00
|
|
|
qh: QueueHandle<Self>,
|
2023-11-08 14:32:33 -08:00
|
|
|
dmabuf_state: DmabufState,
|
2022-12-30 14:07:39 -08:00
|
|
|
registry_state: RegistryState,
|
|
|
|
|
toplevel_info_state: ToplevelInfoState,
|
|
|
|
|
workspace_state: WorkspaceState,
|
|
|
|
|
screencopy_state: ScreencopyState,
|
2023-01-04 15:41:19 -08:00
|
|
|
seat_state: SeatState,
|
2023-03-16 14:08:20 -07:00
|
|
|
shm_state: Shm,
|
2023-01-04 15:17:57 -08:00
|
|
|
toplevel_manager_state: ToplevelManagerState,
|
2022-12-30 14:07:39 -08:00
|
|
|
sender: mpsc::Sender<Event>,
|
2023-01-04 15:41:19 -08:00
|
|
|
seats: Vec<wl_seat::WlSeat>,
|
2023-02-09 14:00:22 -08:00
|
|
|
capture_filter: CaptureFilter,
|
|
|
|
|
captures: RefCell<HashMap<CaptureSource, Arc<Capture>>>,
|
2023-11-08 14:32:33 -08:00
|
|
|
dmabuf_feedback: Option<DmabufFeedback>,
|
2023-11-09 13:45:28 -08:00
|
|
|
gbm: Option<(PathBuf, gbm::Device<fs::File>)>,
|
2024-01-25 17:06:28 -08:00
|
|
|
scheduler: calloop::futures::Scheduler<()>,
|
2022-12-30 14:07:39 -08:00
|
|
|
}
|
|
|
|
|
|
2022-12-30 15:21:05 -08:00
|
|
|
impl AppData {
|
|
|
|
|
fn send_event(&mut self, event: Event) {
|
|
|
|
|
let _ = block_on(self.sender.send(event));
|
|
|
|
|
}
|
2023-02-09 14:00:22 -08:00
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
}
|
2024-04-23 11:54:05 -07:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-09 14:00:22 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-10 10:12:26 -08:00
|
|
|
fn matches_capture_filter(&self, source: &CaptureSource) -> bool {
|
|
|
|
|
match source {
|
|
|
|
|
CaptureSource::Toplevel(toplevel) => {
|
|
|
|
|
let info = self.toplevel_info_state.info(toplevel).unwrap();
|
2023-11-03 13:06:43 -07:00
|
|
|
info.workspace.iter().any(|workspace| {
|
2023-02-10 10:12:26 -08:00
|
|
|
self.capture_filter
|
|
|
|
|
.toplevels_on_workspaces
|
|
|
|
|
.contains(workspace)
|
2023-11-03 13:06:43 -07:00
|
|
|
})
|
2023-02-10 10:12:26 -08:00
|
|
|
}
|
|
|
|
|
CaptureSource::Workspace(_, output) => {
|
2023-11-08 13:59:53 -08:00
|
|
|
self.capture_filter.workspaces_on_outputs.contains(output)
|
2023-02-10 10:12:26 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn invalidate_capture_filter(&self) {
|
|
|
|
|
for (source, capture) in self.captures.borrow_mut().iter_mut() {
|
|
|
|
|
let matches = self.matches_capture_filter(source);
|
2024-01-24 15:29:03 -08:00
|
|
|
if matches {
|
2024-03-18 17:57:18 -07:00
|
|
|
capture.start(&self.screencopy_state, &self.qh);
|
2024-01-24 15:29:03 -08:00
|
|
|
} else {
|
|
|
|
|
capture.stop();
|
2023-02-10 10:12:26 -08:00
|
|
|
}
|
|
|
|
|
}
|
2023-02-09 14:00:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn add_capture_source(&self, source: CaptureSource) {
|
2023-02-10 10:12:26 -08:00
|
|
|
self.captures
|
|
|
|
|
.borrow_mut()
|
|
|
|
|
.entry(source.clone())
|
|
|
|
|
.or_insert_with(|| {
|
|
|
|
|
let matches = self.matches_capture_filter(&source);
|
2024-01-25 15:44:30 -08:00
|
|
|
let capture = Capture::new(source);
|
2023-02-10 10:12:26 -08:00
|
|
|
if matches {
|
2024-03-18 17:57:18 -07:00
|
|
|
capture.start(&self.screencopy_state, &self.qh);
|
2023-02-10 10:12:26 -08:00
|
|
|
}
|
|
|
|
|
capture
|
|
|
|
|
});
|
2023-02-09 14:00:22 -08:00
|
|
|
}
|
2023-02-09 16:04:36 -08:00
|
|
|
|
|
|
|
|
fn remove_capture_source(&self, source: CaptureSource) {
|
|
|
|
|
if let Some(capture) = self.captures.borrow_mut().remove(&source) {
|
2024-01-24 15:29:03 -08:00
|
|
|
capture.stop();
|
2023-02-09 16:04:36 -08:00
|
|
|
}
|
|
|
|
|
}
|
2022-12-30 15:21:05 -08:00
|
|
|
}
|
|
|
|
|
|
2022-12-30 14:07:39 -08:00
|
|
|
impl ProvidesRegistryState for AppData {
|
|
|
|
|
fn registry(&mut self) -> &mut RegistryState {
|
|
|
|
|
&mut self.registry_state
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-08 13:59:53 -08:00
|
|
|
sctk::registry_handlers!(SeatState);
|
2023-01-04 15:41:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
) {
|
|
|
|
|
}
|
2022-12-30 14:07:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ShmHandler for AppData {
|
2023-03-16 14:08:20 -07:00
|
|
|
fn shm_state(&mut self) -> &mut Shm {
|
2022-12-30 14:07:39 -08:00
|
|
|
&mut self.shm_state
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-08 13:31:13 -08:00
|
|
|
fn start(conn: Connection) -> mpsc::Receiver<Event> {
|
2022-12-30 14:07:39 -08:00
|
|
|
let (sender, receiver) = mpsc::channel(20);
|
|
|
|
|
|
2023-02-09 14:29:34 -08:00
|
|
|
let (globals, event_queue) = registry_queue_init(&conn).unwrap();
|
2022-12-30 14:07:39 -08:00
|
|
|
let qh = event_queue.handle();
|
|
|
|
|
|
2023-11-08 14:32:33 -08:00
|
|
|
let dmabuf_state = DmabufState::new(&globals, &qh);
|
|
|
|
|
dmabuf_state.get_default_feedback(&qh).unwrap();
|
|
|
|
|
|
2023-02-09 14:00:22 -08:00
|
|
|
thread::spawn(move || {
|
2024-01-25 17:06:28 -08:00
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-09 14:00:22 -08:00
|
|
|
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();
|
2023-11-03 13:06:43 -07:00
|
|
|
WaylandSource::new(conn, event_queue)
|
2023-02-09 14:00:22 -08:00
|
|
|
.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();
|
2024-03-05 12:25:20 -08:00
|
|
|
event_loop
|
|
|
|
|
.handle()
|
|
|
|
|
.insert_source(executor, |(), _, _| {})
|
|
|
|
|
.unwrap();
|
2023-02-09 14:00:22 -08:00
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
event_loop.dispatch(None, &mut app_data).unwrap();
|
|
|
|
|
}
|
2022-12-30 14:07:39 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
receiver
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-08 13:59:53 -08:00
|
|
|
// Don't bind outputs; use `WlOutput` instances from iced-sctk
|
2022-12-30 14:07:39 -08:00
|
|
|
sctk::delegate_registry!(AppData);
|
2023-01-04 15:41:19 -08:00
|
|
|
sctk::delegate_seat!(AppData);
|
2022-12-30 14:07:39 -08:00
|
|
|
sctk::delegate_shm!(AppData);
|