From fb27181ec4c3043e6581852f87588400b06c97ed Mon Sep 17 00:00:00 2001 From: Ryan Brue Date: Sat, 21 Sep 2024 19:48:32 -0500 Subject: [PATCH] remove unused cosmic_comp module Signed-off-by: Ryan Brue --- src/app_tray/compositor/cosmic.rs | 2 + src/app_tray/compositor/cosmic_comp.rs | 682 ------------------------- 2 files changed, 2 insertions(+), 682 deletions(-) delete mode 100644 src/app_tray/compositor/cosmic_comp.rs diff --git a/src/app_tray/compositor/cosmic.rs b/src/app_tray/compositor/cosmic.rs index ad14554..6242a3c 100644 --- a/src/app_tray/compositor/cosmic.rs +++ b/src/app_tray/compositor/cosmic.rs @@ -77,6 +77,7 @@ impl Dispatch for AppDa _conn: &Connection, _qhandle: &QueueHandle, ) { + println!("TOPLEVEL HANDLE EVENT! {:?}", event); state.handle_toplevel_handle_event( ToplevelHandle::Zcosmic(toplevel.clone()), ToplevelHandleEvent::from(event), @@ -93,6 +94,7 @@ impl Dispatch for AppData { _conn: &Connection, _qhandle: &QueueHandle, ) { + println!("Toplevel manager event! {:?}", event); state.handle_toplevel_manager_event(ToplevelManagerEvent::from(event)); } diff --git a/src/app_tray/compositor/cosmic_comp.rs b/src/app_tray/compositor/cosmic_comp.rs deleted file mode 100644 index 39ddf34..0000000 --- a/src/app_tray/compositor/cosmic_comp.rs +++ /dev/null @@ -1,682 +0,0 @@ -use std::{ - collections::HashMap, - os::{ - fd::{FromRawFd, RawFd}, - unix::net::UnixStream, - }, -}; - -use cctk::{ - sctk::{ - activation::{ActivationHandler, ActivationState, RequestData, RequestDataExt}, - output::{OutputHandler, OutputInfo, OutputState}, - reexports::{ - calloop::{ - channel::{self, Channel, Sender}, - EventLoop, - }, - calloop_wayland_source::WaylandSource, - }, - registry::{ProvidesRegistryState, RegistryState}, - seat::{SeatHandler, SeatState}, - }, - toplevel_info::{ToplevelInfo, ToplevelInfoHandler, ToplevelInfoState}, - toplevel_management::{ToplevelManagerHandler, ToplevelManagerState}, - wayland_client::{ - globals::registry_queue_init, protocol::wl_output::WlOutput, Connection, QueueHandle, WEnum, - }, - workspace::{WorkspaceHandler, WorkspaceState}, -}; -use cosmic_protocols::{ - toplevel_info::v1::client::zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1, - workspace::v1::client::zcosmic_workspace_handle_v1::State as WorkspaceUpdateState, - workspace::v1::client::zcosmic_workspace_handle_v1::ZcosmicWorkspaceHandleV1, -}; -use iced::{ - futures::{ - self, - channel::mpsc::{UnboundedReceiver, UnboundedSender}, - lock::Mutex, - SinkExt, StreamExt, - }, - Subscription, Task, -}; -use once_cell::sync::Lazy; - -use crate::app_tray::AppTrayMessage; - -struct WaylandData { - _conn: Connection, - queue_handle: QueueHandle, - output_state: OutputState, - workspace_state: WorkspaceState, - toplevel_info_state: ToplevelInfoState, - toplevel_manager_state: ToplevelManagerState, - activation_state: Option, - registry_state: RegistryState, - seat_state: SeatState, - tx: UnboundedSender, - exit: bool, -} - -impl OutputHandler for WaylandData { - fn output_state(&mut self) -> &mut OutputState { - &mut self.output_state - } - - fn new_output( - &mut self, - _conn: &Connection, - _qh: &QueueHandle, - output: cctk::wayland_client::protocol::wl_output::WlOutput, - ) { - if let Some(info) = self.output_state.info(&output) { - let _ = self - .tx - .unbounded_send(CosmicIncoming::Output(OutputUpdate::Add( - output.clone(), - info.clone(), - ))); - } - } - - fn update_output( - &mut self, - _conn: &Connection, - _qh: &QueueHandle, - output: cctk::wayland_client::protocol::wl_output::WlOutput, - ) { - if let Some(info) = self.output_state.info(&output) { - let _ = self - .tx - .unbounded_send(CosmicIncoming::Output(OutputUpdate::Update( - output.clone(), - info.clone(), - ))); - } - } - - fn output_destroyed( - &mut self, - _conn: &Connection, - _qh: &QueueHandle, - output: cctk::wayland_client::protocol::wl_output::WlOutput, - ) { - let _ = self - .tx - .unbounded_send(CosmicIncoming::Output(OutputUpdate::Remove(output.clone()))); - } -} - -impl WorkspaceHandler for WaylandData { - fn workspace_state(&mut self) -> &mut WorkspaceState { - &mut self.workspace_state - } - - fn done(&mut self) { - let active_workspaces = self - .workspace_state - .workspace_groups() - .iter() - .filter_map(|x| { - x.workspaces.iter().find(|w| { - w.state - .contains(&WEnum::Value(WorkspaceUpdateState::Active)) - }) - }) - .map(|workspace| workspace.handle.clone()) - .collect::>(); - let _ = self - .tx - .unbounded_send(CosmicIncoming::Workspace(active_workspaces.clone())); - } -} - -impl ProvidesRegistryState for WaylandData { - fn registry(&mut self) -> &mut cctk::sctk::registry::RegistryState { - &mut self.registry_state - } - - cctk::sctk::registry_handlers!(); -} - -struct ExecRequestData { - data: RequestData, - exec: String, - gpu_idx: Option, -} - -impl RequestDataExt for ExecRequestData { - fn app_id(&self) -> Option<&str> { - self.data.app_id() - } - - fn seat_and_serial(&self) -> Option<(&cctk::wayland_client::protocol::wl_seat::WlSeat, u32)> { - self.data.seat_and_serial() - } - - fn surface(&self) -> Option<&cctk::wayland_client::protocol::wl_surface::WlSurface> { - self.data.surface() - } -} - -impl ActivationHandler for WaylandData { - type RequestData = ExecRequestData; - - fn new_token(&mut self, token: String, data: &Self::RequestData) { - let _ = self.tx.unbounded_send(CosmicIncoming::ActivationToken { - _token: Some(token), - _app_id: data.app_id().map(|x| x.to_owned()), - _exec: data.exec.clone(), - _gpu_idx: data.gpu_idx, - }); - } -} - -impl SeatHandler for WaylandData { - fn seat_state(&mut self) -> &mut cctk::sctk::seat::SeatState { - &mut self.seat_state - } - - fn new_seat( - &mut self, - _conn: &Connection, - _qh: &QueueHandle, - _seat: cctk::wayland_client::protocol::wl_seat::WlSeat, - ) { - // Intentionally empty for now - } - - fn new_capability( - &mut self, - _conn: &Connection, - _qh: &QueueHandle, - _seat: cctk::wayland_client::protocol::wl_seat::WlSeat, - _capability: cctk::sctk::seat::Capability, - ) { - // Intentionally empty for now - } - - fn remove_capability( - &mut self, - _conn: &Connection, - _qh: &QueueHandle, - _seat: cctk::wayland_client::protocol::wl_seat::WlSeat, - _capability: cctk::sctk::seat::Capability, - ) { - // Intentionally empty for now - } - - fn remove_seat( - &mut self, - _conn: &Connection, - _qh: &QueueHandle, - _seat: cctk::wayland_client::protocol::wl_seat::WlSeat, - ) { - // Intentionally empty for now - } -} - -impl ToplevelManagerHandler for WaylandData { - fn toplevel_manager_state(&mut self) -> &mut ToplevelManagerState { - &mut self.toplevel_manager_state - } - - fn capabilities( - &mut self, - _conn: &Connection, - _qh: &QueueHandle, - _capabilities: Vec< - cctk::wayland_client::WEnum, - >, - ) { - // Intentionally empty for now - } -} - -impl ToplevelInfoHandler for WaylandData { - fn toplevel_info_state(&mut self) -> &mut ToplevelInfoState { - &mut self.toplevel_info_state - } - - fn new_toplevel( - &mut self, - _conn: &Connection, - _qh: &QueueHandle, - toplevel: &cosmic_protocols::toplevel_info::v1::client::zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1, - ) { - if let Some(info) = self.toplevel_info_state.info(toplevel) { - let _ = self - .tx - .unbounded_send(CosmicIncoming::Toplevel(ToplevelUpdate::Add( - toplevel.clone(), - info.clone(), - ))); - } else { - panic!("Not sure how this would happen. cosmic-client-toolkit did something wrong."); - } - } - - fn update_toplevel( - &mut self, - _conn: &Connection, - _qh: &QueueHandle, - toplevel: &cosmic_protocols::toplevel_info::v1::client::zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1, - ) { - if let Some(info) = self.toplevel_info_state.info(toplevel) { - let _ = self - .tx - .unbounded_send(CosmicIncoming::Toplevel(ToplevelUpdate::Update( - toplevel.clone(), - info.clone(), - ))); - } - } - - fn toplevel_closed( - &mut self, - _conn: &Connection, - _qh: &QueueHandle, - toplevel: &cosmic_protocols::toplevel_info::v1::client::zcosmic_toplevel_handle_v1::ZcosmicToplevelHandleV1, - ) { - let _ = self - .tx - .unbounded_send(CosmicIncoming::Toplevel(ToplevelUpdate::Remove( - toplevel.clone(), - ))); - } -} - -fn wayland_handler(tx: UnboundedSender, rx: Channel) { - let socket = std::env::var("X_PRIVILEGED_WAYLAND_SOCKET") - .ok() - .and_then(|fd| { - fd.parse::() - .ok() - .map(|fd| unsafe { UnixStream::from_raw_fd(fd) }) - }); - - let conn = if let Some(socket) = socket { - Connection::from_socket(socket).unwrap() - } else { - Connection::connect_to_env().unwrap() - }; - - let (globals, event_queue) = registry_queue_init(&conn).unwrap(); - - let mut event_loop = EventLoop::::try_new().unwrap(); - let qh = event_queue.handle(); - let wayland_source = WaylandSource::new(conn.clone(), event_queue); - let handle = event_loop.handle(); - wayland_source - .insert(handle.clone()) - .expect("Failed to insert wayland source."); - if handle - .insert_source(rx, |event, _, state| match event { - channel::Event::Msg(req) => match req { - WaylandRequest::Toplevel(req) => match req { - ToplevelRequest::Activate(handle) => { - if let Some(seat) = state.seat_state.seats().next() { - let manager = &state.toplevel_manager_state.manager; - manager.activate(&handle, &seat); - } - } - ToplevelRequest::Minimize(handle) => { - let manager = &state.toplevel_manager_state.manager; - manager.set_minimized(&handle); - } - ToplevelRequest::Quit(handle) => { - let manager = &state.toplevel_manager_state.manager; - manager.close(&handle); - } - }, - WaylandRequest::TokenRequest { - app_id, - exec, - gpu_idx, - } => { - if let Some(activation_state) = state.activation_state.as_ref() { - let seat_and_serial = state.seat_state.seats().next().map(|seat| (seat, 0)); - activation_state.request_token_with_data( - &state.queue_handle, - ExecRequestData { - data: RequestData { - app_id: Some(app_id), - seat_and_serial, - surface: None, - }, - exec, - gpu_idx, - }, - ); - } else { - let _ = state.tx.unbounded_send(CosmicIncoming::ActivationToken { - _token: None, - _app_id: Some(app_id), - _exec: exec, - _gpu_idx: gpu_idx, - }); - } - } - }, - channel::Event::Closed => { - state.exit = true; - } - }) - .is_err() - { - return; - } - let registry_state = RegistryState::new(&globals); - - let mut app_data = WaylandData { - exit: false, - tx, - _conn: conn, - queue_handle: qh.clone(), - output_state: OutputState::new(&globals, &qh), - workspace_state: WorkspaceState::new(®istry_state, &qh), - toplevel_info_state: ToplevelInfoState::new(®istry_state, &qh), - toplevel_manager_state: ToplevelManagerState::new(®istry_state, &qh), - registry_state, - seat_state: SeatState::new(&globals, &qh), - activation_state: ActivationState::bind::(&globals, &qh).ok(), - }; - - loop { - if app_data.exit { - log::debug!("Exiting COSMIC wayland loop..."); - break; - } - event_loop.dispatch(None, &mut app_data).unwrap(); - } -} - -#[derive(Clone, Debug)] -pub enum CosmicIncoming { - Init(channel::Sender), - Finished, - Toplevel(ToplevelUpdate), - Workspace(Vec), - Output(OutputUpdate), - ActivationToken { - _token: Option, - _app_id: Option, - _exec: String, - _gpu_idx: Option, - }, -} - -#[derive(Clone, Debug)] -pub enum CosmicOutgoing { - Exec(String, String), - Toggle(ZcosmicToplevelHandleV1), - #[allow(unused)] - Activate(ZcosmicToplevelHandleV1), -} - -#[derive(Clone, Debug)] -pub enum ToplevelUpdate { - Add(ZcosmicToplevelHandleV1, ToplevelInfo), - Update(ZcosmicToplevelHandleV1, ToplevelInfo), - Remove(ZcosmicToplevelHandleV1), -} - -#[derive(Clone, Debug)] -pub enum OutputUpdate { - Add(WlOutput, OutputInfo), - Update(WlOutput, OutputInfo), - Remove(WlOutput), -} - -#[derive(Clone, Debug)] -pub enum WaylandRequest { - Toplevel(ToplevelRequest), - TokenRequest { - app_id: String, - exec: String, - gpu_idx: Option, - }, -} - -#[derive(Debug, Clone)] -pub enum ToplevelRequest { - Activate(ZcosmicToplevelHandleV1), - Minimize(ZcosmicToplevelHandleV1), - #[allow(unused)] - Quit(ZcosmicToplevelHandleV1), -} - -cctk::sctk::delegate_seat!(WaylandData); -cctk::sctk::delegate_registry!(WaylandData); -cctk::delegate_toplevel_info!(WaylandData); -cctk::delegate_workspace!(WaylandData); -cctk::delegate_toplevel_manager!(WaylandData); - -cctk::sctk::delegate_activation!(WaylandData, ExecRequestData); - -cctk::sctk::delegate_output!(WaylandData); - -// Wayland Subscription - -pub enum State { - Waiting, - Finished, -} - -pub static WAYLAND_RX: Lazy>>> = - Lazy::new(|| Mutex::new(None)); - -async fn start_listening( - state: State, - output: &mut futures::channel::mpsc::Sender, -) -> State { - match state { - State::Waiting => { - let mut guard = WAYLAND_RX.lock().await; - let rx = { - if guard.is_none() { - let (calloop_tx, calloop_rx) = channel::channel(); - let (toplevel_tx, toplevel_rx) = iced::futures::channel::mpsc::unbounded(); - let _ = std::thread::spawn(move || { - wayland_handler(toplevel_tx, calloop_rx); - }); - *guard = Some(toplevel_rx); - _ = output.send(CosmicIncoming::Init(calloop_tx)).await; - } - guard.as_mut().unwrap() - }; - match rx.next().await { - Some(u) => { - _ = output.send(u).await; - State::Waiting - } - None => { - _ = output.send(CosmicIncoming::Finished).await; - State::Finished - } - } - } - State::Finished => iced::futures::future::pending().await, - } -} - -#[derive(Debug, Clone)] -pub struct CosmicCompBackend { - wayland_sender: Option>, - active_workspaces: Vec, - active_toplevels: HashMap>, - output_list: HashMap, - _current_output: String, // TODO: Get current output -} - -impl CosmicCompBackend { - pub fn new() -> Self { - Self { - wayland_sender: None, - active_workspaces: Vec::new(), - active_toplevels: HashMap::new(), - output_list: HashMap::new(), - _current_output: "".to_string(), - } - } - - pub fn wayland_subscription(&self) -> Subscription { - Subscription::run(|| { - iced::stream::channel(50, move |mut output| async move { - let mut state = State::Waiting; - - loop { - state = start_listening(state, &mut output).await; - } - }) - }) - } - - pub fn handle_incoming(&mut self, incoming: CosmicIncoming) -> Option> { - match incoming { - CosmicIncoming::Init(wayland_sender) => { - self.wayland_sender.replace(wayland_sender); - None - } - CosmicIncoming::Finished => None, - CosmicIncoming::Toplevel(toplevel_update) => match toplevel_update { - ToplevelUpdate::Add(handle, info) => { - let app_id = info.app_id.clone(); - if self.active_toplevels.contains_key(&app_id) { - self.active_toplevels - .get_mut(&info.app_id) - .unwrap() - .insert(handle, info); - } else { - self.active_toplevels - .insert(app_id.clone(), HashMap::from([(handle, info.clone())])); - } - None - } - ToplevelUpdate::Update(handle, info) => { - // TODO probably want to make sure it is removed - if info.app_id.is_empty() { - return Some(Task::none()); - } else if !self.active_toplevels.contains_key(&info.app_id) { - return Some(Task::none()); - } - - for (t_handle, t_info) in self - .active_toplevels - .get_mut(&info.app_id) - .unwrap() - .iter_mut() - { - if &handle == t_handle { - *t_info = info; - break; - } - } - - None - } - ToplevelUpdate::Remove(handle) => { - let mut target_app_id: Option = None; - for (app_id, app_info) in self.active_toplevels.iter_mut() { - if app_info.contains_key(&handle.clone()) { - app_info.remove(&handle); - if app_info.is_empty() { - target_app_id = Some(app_id.clone()); - } - break; - } - } - if let Some(app_id) = target_app_id { - self.active_toplevels.remove(&app_id); - } - None - } - }, - CosmicIncoming::Workspace(workspaces) => { - self.active_workspaces = workspaces; - None - } - CosmicIncoming::Output(output_update) => match output_update { - OutputUpdate::Add(output, info) => { - self.output_list.insert(output, info); - None - } - OutputUpdate::Update(output, info) => { - self.output_list.insert(output, info); - None - } - OutputUpdate::Remove(output) => { - self.output_list.remove(&output); - None - } - }, - _ => None, - } - } - - pub fn handle_outgoing(&mut self, outgoing: CosmicOutgoing) -> Option> { - match outgoing { - CosmicOutgoing::Exec(app_id, exec) => { - if let Some(tx) = self.wayland_sender.as_ref() { - let _ = tx.send(WaylandRequest::TokenRequest { - app_id, - exec, - gpu_idx: None, - }); - } - None - } - CosmicOutgoing::Toggle(window) => { - if let Some(tx) = self.wayland_sender.as_ref() { - let _ = tx.send(WaylandRequest::Toplevel( - if self.active_window().is_some() { - ToplevelRequest::Minimize(window) - } else { - ToplevelRequest::Activate(window) - }, - )); - } - // if let Some(p) = self.popup.take() { - // return destroy_popup(p.id); - // } - None - } - CosmicOutgoing::Activate(window) => { - if let Some(tx) = self.wayland_sender.as_ref() { - let _ = tx.send(WaylandRequest::Toplevel(ToplevelRequest::Activate(window))); - } - // if let Some(p) = self.popup.take() { - // return destroy_popup(p.id); - // } - None - } - } - } - - pub fn active_window(&self) -> Option { - if self.active_workspaces.is_empty() { - return None; - } - let mut focused_toplevels: Vec = Vec::new(); - let active_workspaces = self.active_workspaces.clone(); - for (_, app_group) in self.active_toplevels.iter() { - for (t_handle, t_info) in app_group.iter() { - if t_info.state.contains(&cosmic_protocols::toplevel_info::v1::client::zcosmic_toplevel_handle_v1::State::Activated) - && active_workspaces - .iter() - .any(|workspace| t_info.workspace.contains(workspace)) - && t_info.output.iter().any(|x| { - self.output_list.get(x).is_some_and(|_val| { - true // TODO: Output stuff - // val.name.as_ref().is_some_and(|n| *n == self.current_output) - }) - }) - { - focused_toplevels.push(t_handle.clone()); - } - } - } - focused_toplevels.first().map(|f| f.clone()) - } -}