cosmic-workspaces/src/wayland/mod.rs

329 lines
9.9 KiB
Rust
Raw Normal View History

2022-12-30 14:07:39 -08:00
// Workspaces Info, Toplevel Info
// Capture
// - subscribe to all workspaces, to start with? All that are associated with an output should be
// shown on one.
// * Need output name to compare?
2022-12-30 15:21:05 -08:00
// TODO: Way to activate workspace, toplevel? Close? Move?
2022-12-30 14:07:39 -08:00
use cctk::{
cosmic_protocols::{
toplevel_info::v1::client::zcosmic_toplevel_handle_v1,
2023-01-04 15:17:57 -08:00
toplevel_management::v1::client::zcosmic_toplevel_manager_v1,
2023-01-04 14:41:44 -08:00
workspace::v1::client::{zcosmic_workspace_handle_v1, zcosmic_workspace_manager_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,
event_loop::WaylandSource,
2022-12-30 14:07:39 -08:00
output::{OutputHandler, OutputState},
registry::{ProvidesRegistryState, RegistryState},
2023-01-04 15:41:19 -08:00
seat::{SeatHandler, SeatState},
2023-02-09 14:10:31 -08:00
shm::{ShmHandler, ShmState},
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::{
backend::ObjectId,
globals::registry_queue_init,
2023-02-09 16:04:36 -08:00
protocol::{wl_output, wl_seat},
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-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},
widget::image,
};
2023-01-17 12:36:05 -08:00
use futures_channel::mpsc;
2023-02-09 16:04:36 -08:00
use std::{cell::RefCell, collections::HashMap, 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};
mod screencopy;
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 {
CmdSender(calloop::channel::Sender<Cmd>),
2023-01-04 15:17:57 -08:00
Connection(Connection),
ToplevelManager(zcosmic_toplevel_manager_v1::ZcosmicToplevelManagerV1),
WorkspaceManager(zcosmic_workspace_manager_v1::ZcosmicWorkspaceManagerV1),
2022-12-30 14:29:42 -08:00
// XXX Output name rather than `WlOutput`
Workspaces(Vec<(String, cctk::workspace::Workspace)>),
2022-12-30 14:07:39 -08:00
WorkspaceCapture(
zcosmic_workspace_handle_v1::ZcosmicWorkspaceHandleV1,
image::Handle,
),
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,
image::Handle,
),
2023-01-04 15:41:19 -08:00
Seats(Vec<wl_seat::WlSeat>),
2022-12-30 14:07:39 -08:00
}
pub fn subscription() -> iced::Subscription<Event> {
iced::subscription::run("wayland-sub", async { start() }.flatten_stream())
}
#[derive(Debug)]
pub enum Cmd {
CaptureFilter(CaptureFilter),
}
2023-01-04 14:41:44 -08:00
pub struct AppData {
2022-12-30 14:07:39 -08:00
qh: QueueHandle<Self>,
output_state: OutputState,
registry_state: RegistryState,
toplevel_info_state: ToplevelInfoState,
workspace_state: WorkspaceState,
screencopy_state: ScreencopyState,
2023-01-04 15:41:19 -08:00
seat_state: SeatState,
2022-12-30 14:07:39 -08:00
shm_state: ShmState,
2023-01-04 15:17:57 -08:00
toplevel_manager_state: ToplevelManagerState,
2022-12-30 14:07:39 -08:00
sender: mpsc::Sender<Event>,
2022-12-30 14:29:42 -08:00
output_names: HashMap<ObjectId, Option<String>>,
2023-01-04 15:41:19 -08:00
seats: Vec<wl_seat::WlSeat>,
capture_filter: CaptureFilter,
captures: RefCell<HashMap<CaptureSource, Arc<Capture>>>,
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));
}
// 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();
}
}
}
fn matches_capture_filter(&self, source: &CaptureSource) -> bool {
match source {
CaptureSource::Toplevel(toplevel) => {
let info = self.toplevel_info_state.info(toplevel).unwrap();
if let Some(workspace) = &info.workspace {
self.capture_filter
.toplevels_on_workspaces
.contains(workspace)
} else {
false
}
}
CaptureSource::Workspace(_, output) => {
if let Some(name) = &self.output_state.info(&output).unwrap().name {
self.capture_filter.workspaces_on_outputs.contains(name)
} else {
false
}
}
}
}
fn invalidate_capture_filter(&self) {
for (source, capture) in self.captures.borrow_mut().iter_mut() {
let matches = self.matches_capture_filter(source);
let running = capture.running();
if running && !matches {
capture.cancel();
} else if !running & matches {
capture.capture(&self.screencopy_state.screencopy_manager, &self.qh);
}
}
}
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 = Arc::new(Capture::new(source));
if matches {
capture.capture(&self.screencopy_state.screencopy_manager, &self.qh);
}
capture
});
}
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) {
capture.cancel();
}
}
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-01-04 15:41:19 -08:00
sctk::registry_handlers!(OutputState, 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);
self.send_event(Event::Seats(self.seats.clone()));
}
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);
}
self.send_event(Event::Seats(self.seats.clone()));
}
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 {
fn shm_state(&mut self) -> &mut ShmState {
&mut self.shm_state
}
}
// TODO: don't need this if we use same connection with same IDs? Or?
impl OutputHandler for AppData {
fn output_state(&mut self) -> &mut OutputState {
&mut self.output_state
}
fn new_output(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
2022-12-30 14:29:42 -08:00
output: wl_output::WlOutput,
2022-12-30 14:07:39 -08:00
) {
2022-12-30 14:29:42 -08:00
let name = self.output_state.info(&output).unwrap().name;
self.output_names.insert(output.id(), name);
2022-12-30 14:07:39 -08:00
}
fn update_output(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
_output: wl_output::WlOutput,
) {
}
fn output_destroyed(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
2022-12-30 14:29:42 -08:00
output: wl_output::WlOutput,
2022-12-30 14:07:39 -08:00
) {
2022-12-30 14:29:42 -08:00
self.output_names.remove(&output.id());
2022-12-30 14:07:39 -08:00
}
}
fn start() -> mpsc::Receiver<Event> {
let (sender, receiver) = mpsc::channel(20);
// TODO share connection? Can't use same `WlOutput` with seperate connection
let conn = Connection::connect_to_env().unwrap();
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();
let registry_state = RegistryState::new(&globals);
let mut app_data = AppData {
qh: qh.clone(),
output_state: OutputState::new(&globals, &qh),
workspace_state: WorkspaceState::new(&registry_state, &qh), // Create before toplevel info state
toplevel_info_state: ToplevelInfoState::new(&registry_state, &qh),
2023-01-04 15:17:57 -08:00
toplevel_manager_state: ToplevelManagerState::new(&registry_state, &qh),
2022-12-30 14:07:39 -08:00
screencopy_state: ScreencopyState::new(&globals, &qh),
registry_state,
2023-01-04 15:41:19 -08:00
seat_state: SeatState::new(&globals, &qh),
2022-12-30 14:07:39 -08:00
shm_state: ShmState::bind(&globals, &qh).unwrap(),
sender,
2022-12-30 14:29:42 -08:00
output_names: HashMap::new(),
2023-01-04 15:41:19 -08:00
seats: Vec::new(),
capture_filter: CaptureFilter::default(),
captures: RefCell::new(HashMap::new()),
2022-12-30 14:07:39 -08:00
};
2023-01-04 15:17:57 -08:00
app_data.send_event(Event::Connection(conn));
2023-01-05 18:24:35 -08:00
app_data.send_event(Event::Seats(app_data.seat_state.seats().collect()));
2023-01-04 15:17:57 -08:00
app_data.send_event(Event::ToplevelManager(
app_data.toplevel_manager_state.manager.clone(),
));
2023-01-04 14:41:44 -08:00
if let Ok(manager) = app_data.workspace_state.workspace_manager().get() {
2023-01-04 15:17:57 -08:00
app_data.send_event(Event::WorkspaceManager(manager.clone()));
2023-01-04 14:41:44 -08:00
}
// XXX also monitor cmd sender? Use calloop?
thread::spawn(move || {
//event_queue.blocking_dispatch(&mut app_data).unwrap();
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(event_queue)
.unwrap()
.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();
loop {
event_loop.dispatch(None, &mut app_data).unwrap();
}
2022-12-30 14:07:39 -08:00
});
receiver
}
sctk::delegate_output!(AppData);
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);