From 96cd334b818a0e3b47d5d5b5d06777e6a194e1b6 Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Wed, 8 Nov 2023 13:31:13 -0800 Subject: [PATCH] Share wayland connection with iced-sctk instead of creating new one --- src/main.rs | 99 ++++++++++++++++++++++++++-------------------- src/wayland/mod.rs | 10 ++--- 2 files changed, 60 insertions(+), 49 deletions(-) diff --git a/src/main.rs b/src/main.rs index d5cedad..ce192d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ use cctk::{ toplevel_info::ToplevelInfo, wayland_client::{ protocol::{wl_data_device_manager::DndAction, wl_output, wl_seat}, - Connection, WEnum, + Connection, Proxy, WEnum, }, }; use cosmic::{ @@ -279,50 +279,65 @@ impl Application for App { fn update(&mut self, message: Msg) -> Command { match message { Msg::WaylandEvent(evt) => match evt { - WaylandEvent::Output(evt, output) => match evt { - OutputEvent::Created(Some(info)) => { - if let (Some((width, height)), Some(name)) = (info.logical_size, info.name) - { - self.outputs.push(Output { - handle: output.clone(), - name: name.clone(), - width, - height, - }); + WaylandEvent::Output(evt, output) => { + // TODO: Less hacky way to get connection from iced-sctk + if self.conn.is_none() { + if let Some(backend) = output.backend().upgrade() { + self.conn = Some(Connection::from_backend(backend)); + } + } + + match evt { + OutputEvent::Created(Some(info)) => { + if let (Some((width, height)), Some(name)) = + (info.logical_size, info.name) + { + self.outputs.push(Output { + handle: output.clone(), + name: name.clone(), + width, + height, + }); + if self.visible { + return self.create_surface( + output.clone(), + name, + width, + height, + ); + } + } + } + OutputEvent::Created(None) => {} // XXX? + OutputEvent::InfoUpdate(info) => { + if let Some(output) = + self.outputs.iter_mut().find(|x| x.handle == output) + { + if let Some((width, height)) = info.logical_size { + output.width = width; + output.height = height; + } + if let Some(name) = info.name { + output.name = name; + } + // XXX re-create surface? + } + } + OutputEvent::Removed => { + if let Some(idx) = self.outputs.iter().position(|x| x.handle == output) + { + self.outputs.remove(idx); + } if self.visible { - return self.create_surface(output.clone(), name, width, height); + return self.destroy_surface(&output); } } } - OutputEvent::Created(None) => {} // XXX? - OutputEvent::InfoUpdate(info) => { - if let Some(output) = self.outputs.iter_mut().find(|x| x.handle == output) { - if let Some((width, height)) = info.logical_size { - output.width = width; - output.height = height; - } - if let Some(name) = info.name { - output.name = name; - } - // XXX re-create surface? - } - } - OutputEvent::Removed => { - if let Some(idx) = self.outputs.iter().position(|x| x.handle == output) { - self.outputs.remove(idx); - } - if self.visible { - return self.destroy_surface(&output); - } - } - }, + } _ => {} }, Msg::Wayland(evt) => { match evt { - wayland::Event::Connection(conn) => { - self.conn = Some(conn); - } wayland::Event::CmdSender(sender) => { self.wayland_cmd_sender = Some(sender); } @@ -486,11 +501,11 @@ impl Application for App { None } }); - iced::Subscription::batch(vec![ - events, - toggle_dbus::subscription().map(Msg::DBus), - wayland::subscription().map(Msg::Wayland), - ]) + let mut subscriptions = vec![events, toggle_dbus::subscription().map(Msg::DBus)]; + if let Some(conn) = self.conn.clone() { + subscriptions.push(wayland::subscription(conn).map(Msg::Wayland)); + } + iced::Subscription::batch(subscriptions) } fn view(&self, id: SurfaceId) -> cosmic::Element { diff --git a/src/wayland/mod.rs b/src/wayland/mod.rs index 6f39298..5f7af31 100644 --- a/src/wayland/mod.rs +++ b/src/wayland/mod.rs @@ -59,7 +59,6 @@ pub use capture::CaptureFilter; #[derive(Clone, Debug)] pub enum Event { CmdSender(calloop::channel::Sender), - Connection(Connection), ToplevelManager(zcosmic_toplevel_manager_v1::ZcosmicToplevelManagerV1), WorkspaceManager(zcosmic_workspace_manager_v1::ZcosmicWorkspaceManagerV1), // XXX Output name rather than `WlOutput` @@ -87,8 +86,8 @@ pub enum Event { Seats(Vec), } -pub fn subscription() -> iced::Subscription { - iced::subscription::run_with_id("wayland-sub", async { start() }.flatten_stream()) +pub fn subscription(conn: Connection) -> iced::Subscription { + iced::subscription::run_with_id("wayland-sub", async { start(conn) }.flatten_stream()) } #[derive(Debug)] @@ -264,11 +263,9 @@ impl OutputHandler for AppData { } } -fn start() -> mpsc::Receiver { +fn start(conn: Connection) -> mpsc::Receiver { 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, event_queue) = registry_queue_init(&conn).unwrap(); let qh = event_queue.handle(); @@ -290,7 +287,6 @@ fn start() -> mpsc::Receiver { captures: RefCell::new(HashMap::new()), }; - app_data.send_event(Event::Connection(conn.clone())); app_data.send_event(Event::Seats(app_data.seat_state.seats().collect())); app_data.send_event(Event::ToplevelManager( app_data.toplevel_manager_state.manager.clone(),