2022-12-15 14:35:31 -05:00
|
|
|
use crate::toplevel_subscription::{ToplevelRequest, ToplevelUpdate};
|
2022-12-12 19:48:31 -05:00
|
|
|
use cctk::{
|
2022-12-15 14:35:31 -05:00
|
|
|
sctk::{
|
|
|
|
|
self,
|
2023-03-01 10:06:14 -08:00
|
|
|
reexports::client::{protocol::wl_seat::WlSeat, WaylandSource},
|
2022-12-15 14:35:31 -05:00
|
|
|
seat::{SeatHandler, SeatState},
|
|
|
|
|
},
|
2022-12-12 19:48:31 -05:00
|
|
|
toplevel_info::{ToplevelInfoHandler, ToplevelInfoState},
|
2022-12-13 19:58:00 -05:00
|
|
|
toplevel_management::{ToplevelManagerHandler, ToplevelManagerState},
|
2022-12-16 09:52:18 -05:00
|
|
|
wayland_client::{self, WEnum},
|
|
|
|
|
};
|
|
|
|
|
use cosmic_protocols::{
|
|
|
|
|
toplevel_info::v1::client::zcosmic_toplevel_handle_v1,
|
|
|
|
|
toplevel_management::v1::client::zcosmic_toplevel_manager_v1,
|
2022-12-12 19:48:31 -05:00
|
|
|
};
|
|
|
|
|
use futures::channel::mpsc::UnboundedSender;
|
|
|
|
|
use sctk::registry::{ProvidesRegistryState, RegistryState};
|
|
|
|
|
use wayland_client::{globals::registry_queue_init, Connection, QueueHandle};
|
|
|
|
|
|
|
|
|
|
struct AppData {
|
|
|
|
|
exit: bool,
|
|
|
|
|
tx: UnboundedSender<ToplevelUpdate>,
|
|
|
|
|
registry_state: RegistryState,
|
|
|
|
|
toplevel_info_state: ToplevelInfoState,
|
2022-12-13 19:58:00 -05:00
|
|
|
toplevel_manager_state: ToplevelManagerState,
|
|
|
|
|
seat_state: SeatState,
|
2022-12-12 19:48:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ProvidesRegistryState for AppData {
|
|
|
|
|
fn registry(&mut self) -> &mut RegistryState {
|
|
|
|
|
&mut self.registry_state
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sctk::registry_handlers!();
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-13 19:58:00 -05:00
|
|
|
impl SeatHandler for AppData {
|
|
|
|
|
fn seat_state(&mut self) -> &mut sctk::seat::SeatState {
|
|
|
|
|
&mut self.seat_state
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 14:35:31 -05:00
|
|
|
fn new_seat(&mut self, _: &Connection, _: &QueueHandle<Self>, _: WlSeat) {}
|
2022-12-13 19:58:00 -05:00
|
|
|
|
|
|
|
|
fn new_capability(
|
|
|
|
|
&mut self,
|
2022-12-15 14:35:31 -05:00
|
|
|
_: &Connection,
|
|
|
|
|
_: &QueueHandle<Self>,
|
|
|
|
|
_: WlSeat,
|
|
|
|
|
_: sctk::seat::Capability,
|
|
|
|
|
) {
|
|
|
|
|
}
|
2022-12-13 19:58:00 -05:00
|
|
|
|
|
|
|
|
fn remove_capability(
|
|
|
|
|
&mut self,
|
2022-12-15 14:35:31 -05:00
|
|
|
_: &Connection,
|
|
|
|
|
_: &QueueHandle<Self>,
|
|
|
|
|
_: WlSeat,
|
|
|
|
|
_: sctk::seat::Capability,
|
|
|
|
|
) {
|
|
|
|
|
}
|
2022-12-13 19:58:00 -05:00
|
|
|
|
2022-12-15 14:35:31 -05:00
|
|
|
fn remove_seat(&mut self, _: &Connection, _: &QueueHandle<Self>, _: WlSeat) {}
|
2022-12-13 19:58:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ToplevelManagerHandler for AppData {
|
|
|
|
|
fn toplevel_manager_state(&mut self) -> &mut cctk::toplevel_management::ToplevelManagerState {
|
|
|
|
|
&mut self.toplevel_manager_state
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-16 09:52:18 -05:00
|
|
|
fn capabilities(
|
|
|
|
|
&mut self,
|
|
|
|
|
_: &Connection,
|
|
|
|
|
_: &QueueHandle<Self>,
|
|
|
|
|
_: Vec<WEnum<zcosmic_toplevel_manager_v1::ZcosmicToplelevelManagementCapabilitiesV1>>,
|
|
|
|
|
) {
|
2022-12-13 19:58:00 -05:00
|
|
|
// TODO capabilities could affect the options in the applet
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-12 19:48:31 -05:00
|
|
|
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,
|
|
|
|
|
) {
|
|
|
|
|
if let Some(info) = self.toplevel_info_state.info(toplevel) {
|
|
|
|
|
let _ = self
|
|
|
|
|
.tx
|
|
|
|
|
.unbounded_send(ToplevelUpdate::AddToplevel(toplevel.clone(), info.clone()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn update_toplevel(
|
|
|
|
|
&mut self,
|
|
|
|
|
_conn: &Connection,
|
|
|
|
|
_qh: &QueueHandle<Self>,
|
|
|
|
|
toplevel: &zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1,
|
|
|
|
|
) {
|
|
|
|
|
if let Some(info) = self.toplevel_info_state.info(toplevel) {
|
|
|
|
|
let _ = self.tx.unbounded_send(ToplevelUpdate::UpdateToplevel(
|
|
|
|
|
toplevel.clone(),
|
|
|
|
|
info.clone(),
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn toplevel_closed(
|
|
|
|
|
&mut self,
|
|
|
|
|
_conn: &Connection,
|
|
|
|
|
_qh: &QueueHandle<Self>,
|
|
|
|
|
toplevel: &zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1,
|
|
|
|
|
) {
|
|
|
|
|
let _ = self
|
|
|
|
|
.tx
|
|
|
|
|
.unbounded_send(ToplevelUpdate::RemoveToplevel(toplevel.clone()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn toplevel_handler(
|
|
|
|
|
tx: UnboundedSender<ToplevelUpdate>,
|
|
|
|
|
rx: calloop::channel::Channel<ToplevelRequest>,
|
|
|
|
|
) {
|
|
|
|
|
let conn = Connection::connect_to_env().unwrap();
|
|
|
|
|
let (globals, event_queue) = registry_queue_init(&conn).unwrap();
|
|
|
|
|
let mut event_loop = calloop::EventLoop::<AppData>::try_new().unwrap();
|
|
|
|
|
let qh = event_queue.handle();
|
|
|
|
|
let wayland_source = WaylandSource::new(event_queue).unwrap();
|
|
|
|
|
let handle = event_loop.handle();
|
|
|
|
|
|
|
|
|
|
if handle
|
|
|
|
|
.insert_source(wayland_source, |_, q, state| q.dispatch_pending(state))
|
|
|
|
|
.is_err()
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if handle
|
|
|
|
|
.insert_source(rx, |event, _, state| match event {
|
|
|
|
|
calloop::channel::Event::Msg(req) => match req {
|
2022-12-13 19:58:00 -05:00
|
|
|
ToplevelRequest::Activate(handle, seat) => {
|
|
|
|
|
let manager = &state.toplevel_manager_state.manager;
|
|
|
|
|
manager.activate(&handle, &seat);
|
2022-12-15 14:35:31 -05:00
|
|
|
}
|
|
|
|
|
ToplevelRequest::Quit(handle) => {
|
|
|
|
|
let manager = &state.toplevel_manager_state.manager;
|
|
|
|
|
manager.close(&handle);
|
|
|
|
|
}
|
2022-12-12 19:48:31 -05:00
|
|
|
ToplevelRequest::Exit => {
|
|
|
|
|
state.exit = true;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
calloop::channel::Event::Closed => {
|
|
|
|
|
state.exit = true;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.is_err()
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let registry_state = RegistryState::new(&globals);
|
|
|
|
|
let mut app_data = AppData {
|
|
|
|
|
exit: false,
|
|
|
|
|
tx,
|
2022-12-13 19:58:00 -05:00
|
|
|
seat_state: SeatState::new(&globals, &qh),
|
2022-12-12 19:48:31 -05:00
|
|
|
toplevel_info_state: ToplevelInfoState::new(®istry_state, &qh),
|
2022-12-13 19:58:00 -05:00
|
|
|
toplevel_manager_state: ToplevelManagerState::new(®istry_state, &qh),
|
2022-12-12 19:48:31 -05:00
|
|
|
registry_state,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
if app_data.exit {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
event_loop.dispatch(None, &mut app_data).unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-13 19:58:00 -05:00
|
|
|
sctk::delegate_seat!(AppData);
|
2022-12-12 19:48:31 -05:00
|
|
|
sctk::delegate_registry!(AppData);
|
|
|
|
|
cctk::delegate_toplevel_info!(AppData);
|
2022-12-13 19:58:00 -05:00
|
|
|
cctk::delegate_toplevel_manager!(AppData);
|