cosmic-workspaces/src/wayland.rs

613 lines
18 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::{
screencopy::v1::client::{zcosmic_screencopy_manager_v1, zcosmic_screencopy_session_v1},
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-01-23 15:30:47 -08:00
screencopy::{
BufferInfo, ScreencopyHandler, ScreencopySessionData, ScreencopySessionDataExt,
ScreencopyState,
},
2022-12-30 14:07:39 -08:00
sctk::{
self,
event_loop::WaylandSource,
globals::ProvidesBoundGlobal,
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},
2022-12-30 14:07:39 -08:00
shm::{raw::RawPool, ShmHandler, ShmState},
},
2022-12-30 15:21:05 -08:00
toplevel_info::{ToplevelInfo, ToplevelInfoHandler, ToplevelInfoState},
2023-01-04 15:17:57 -08:00
toplevel_management::{ToplevelManagerHandler, ToplevelManagerState},
2022-12-30 14:07:39 -08:00
wayland_client::{
backend::ObjectId,
globals::registry_queue_init,
2023-01-04 15:41:19 -08:00
protocol::{wl_buffer, wl_output, wl_seat, wl_shm},
2022-12-30 14:07:39 -08:00
Connection, Dispatch, Proxy, QueueHandle, WEnum,
},
workspace::{WorkspaceHandler, WorkspaceState},
};
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;
use std::{
cell::RefCell,
collections::HashMap,
sync::{
atomic::{AtomicBool, Ordering},
Arc, Mutex,
},
thread,
};
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<(Option<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-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(Clone, PartialEq, Eq)]
2022-12-30 14:07:39 -08:00
enum CaptureSource {
2022-12-30 15:21:05 -08:00
Toplevel(zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1),
Workspace(
zcosmic_workspace_handle_v1::ZcosmicWorkspaceHandleV1,
wl_output::WlOutput,
),
2022-12-30 14:07:39 -08:00
}
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 Buffer {
pool: RawPool,
buffer: wl_buffer::WlBuffer,
buffer_info: BufferInfo,
}
impl Buffer {
fn new(
buffer_info: BufferInfo,
shm: &impl ProvidesBoundGlobal<wl_shm::WlShm, 1>,
qh: &QueueHandle<AppData>,
) -> Self {
// Assume format is already known to be valid
let mut pool =
RawPool::new((buffer_info.stride * buffer_info.height) as usize, shm).unwrap();
let format = wl_shm::Format::try_from(buffer_info.format).unwrap();
let buffer = pool.create_buffer(
0,
buffer_info.width as i32,
buffer_info.height as i32,
buffer_info.stride as i32,
format,
(),
qh,
);
Self {
pool,
buffer,
buffer_info,
}
}
}
impl Drop for Buffer {
fn drop(&mut self) {
self.buffer.destroy();
}
}
struct Capture {
buffer: Mutex<Option<Buffer>>,
2022-12-30 14:07:39 -08:00
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,
);
}
}
}
2022-12-30 14:07:39 -08:00
}
struct SessionData {
session_data: ScreencopySessionData,
capture: Arc<Capture>,
}
impl ScreencopySessionDataExt for SessionData {
2023-01-23 15:30:47 -08:00
fn screencopy_session_data(&self) -> &ScreencopySessionData {
&self.session_data
}
}
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 invalidate_capture_filter(&mut self) {
// XXX drain filter
// TODO cancel captures if needed, enable capture
}
fn add_capture_source(&self, source: CaptureSource) {
let capture = Arc::new(Capture::new(source.clone()));
capture.capture(&self.screencopy_state.screencopy_manager, &self.qh);
self.captures.borrow_mut().insert(source, capture);
}
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
}
}
// 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,
) {
2022-12-30 15:21:05 -08:00
let info = self.toplevel_info_state.info(&toplevel).unwrap();
2022-12-30 15:35:36 -08:00
self.send_event(Event::NewToplevel(toplevel.clone(), info.clone()));
2022-12-30 15:21:05 -08:00
self.add_capture_source(CaptureSource::Toplevel(toplevel.clone()));
2022-12-30 14:07:39 -08:00
}
fn update_toplevel(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
toplevel: &zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1,
) {
2022-12-30 15:21:05 -08:00
// TODO
2022-12-30 14:07:39 -08:00
}
fn toplevel_closed(
&mut self,
_conn: &Connection,
_qh: &QueueHandle<Self>,
toplevel: &zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1,
) {
2023-01-18 11:01:47 -08:00
self.send_event(Event::CloseToplevel(toplevel.clone()));
2022-12-30 14:07:39 -08:00
}
}
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() {
2022-12-30 14:29:42 -08:00
let output_name = self.output_names.get(&output.id()).unwrap().clone();
workspaces.push((output_name, workspace.clone()));
2022-12-30 14:07:39 -08:00
self.add_capture_source(CaptureSource::Workspace(
workspace.handle.clone(),
output.clone(),
));
2022-12-30 14:07:39 -08:00
}
}
}
2022-12-30 15:21:05 -08:00
self.send_event(Event::Workspaces(workspaces));
2022-12-30 14:07:39 -08:00
}
}
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;
}
2022-12-30 14:07:39 -08:00
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();
let buf_len = buffer_info.stride * buffer_info.height;
2023-01-26 15:44:05 -08:00
// XXX fix in compositor
if buffer_info.width == 0 || buffer_info.height == 0 || buffer_info.stride == 0 {
2023-01-26 15:44:05 -08:00
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();
2022-12-30 14:07:39 -08:00
session.attach_buffer(&buffer.buffer, None, 0); // XXX age?
if capture.first_frame.load(Ordering::SeqCst) {
2022-12-30 14:07:39 -08:00
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 mut buffer = buffer.as_mut().unwrap();
2023-01-17 12:36:05 -08:00
// XXX is this at all a performance issue?
let image = image::Handle::from_pixels(
buffer.buffer_info.width,
buffer.buffer_info.height,
buffer.pool.mmap().to_vec(),
);
let event = match &capture.source {
2022-12-30 15:21:05 -08:00
CaptureSource::Toplevel(toplevel) => Event::ToplevelCapture(toplevel.clone(), image),
CaptureSource::Workspace(workspace, _) => {
2022-12-30 15:21:05 -08:00
Event::WorkspaceCapture(workspace.clone(), image)
2022-12-30 14:07:39 -08:00
}
2022-12-30 15:21:05 -08:00
};
self.send_event(event);
2023-01-26 15:44:05 -08:00
session.destroy();
// Capture again on damage
capture.first_frame.store(false, Ordering::SeqCst);
capture.capture(&self.screencopy_state.screencopy_manager, &self.qh);
2022-12-30 14:07:39 -08:00
}
fn failed(
&mut self,
conn: &Connection,
qh: &QueueHandle<Self>,
session: &zcosmic_screencopy_session_v1::ZcosmicScreencopySessionV1,
reason: WEnum<zcosmic_screencopy_session_v1::FailureReason>,
) {
// TODO
2022-12-30 15:21:05 -08:00
println!("Failed");
2023-01-26 15:44:05 -08:00
session.destroy();
2022-12-30 14:07:39 -08:00
}
}
2023-01-04 15:17:57 -08:00
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>,
>,
) {
}
}
2022-12-30 14:07:39 -08:00
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!(),
}
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();
let (globals, mut event_queue) = registry_queue_init(&conn).unwrap();
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);
cctk::delegate_toplevel_info!(AppData);
2023-01-04 15:17:57 -08:00
cctk::delegate_toplevel_manager!(AppData);
2022-12-30 14:07:39 -08:00
cctk::delegate_workspace!(AppData);
cctk::delegate_screencopy!(AppData, session: [SessionData]);