Share wayland connection with iced-sctk instead of creating new one

This commit is contained in:
Ian Douglas Scott 2023-11-08 13:31:13 -08:00
parent c34857b34c
commit 96cd334b81
2 changed files with 60 additions and 49 deletions

View file

@ -10,7 +10,7 @@ use cctk::{
toplevel_info::ToplevelInfo, toplevel_info::ToplevelInfo,
wayland_client::{ wayland_client::{
protocol::{wl_data_device_manager::DndAction, wl_output, wl_seat}, protocol::{wl_data_device_manager::DndAction, wl_output, wl_seat},
Connection, WEnum, Connection, Proxy, WEnum,
}, },
}; };
use cosmic::{ use cosmic::{
@ -279,50 +279,65 @@ impl Application for App {
fn update(&mut self, message: Msg) -> Command<Msg> { fn update(&mut self, message: Msg) -> Command<Msg> {
match message { match message {
Msg::WaylandEvent(evt) => match evt { Msg::WaylandEvent(evt) => match evt {
WaylandEvent::Output(evt, output) => match evt { WaylandEvent::Output(evt, output) => {
OutputEvent::Created(Some(info)) => { // TODO: Less hacky way to get connection from iced-sctk
if let (Some((width, height)), Some(name)) = (info.logical_size, info.name) if self.conn.is_none() {
{ if let Some(backend) = output.backend().upgrade() {
self.outputs.push(Output { self.conn = Some(Connection::from_backend(backend));
handle: output.clone(), }
name: name.clone(), }
width,
height, 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 { 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) => { Msg::Wayland(evt) => {
match evt { match evt {
wayland::Event::Connection(conn) => {
self.conn = Some(conn);
}
wayland::Event::CmdSender(sender) => { wayland::Event::CmdSender(sender) => {
self.wayland_cmd_sender = Some(sender); self.wayland_cmd_sender = Some(sender);
} }
@ -486,11 +501,11 @@ impl Application for App {
None None
} }
}); });
iced::Subscription::batch(vec![ let mut subscriptions = vec![events, toggle_dbus::subscription().map(Msg::DBus)];
events, if let Some(conn) = self.conn.clone() {
toggle_dbus::subscription().map(Msg::DBus), subscriptions.push(wayland::subscription(conn).map(Msg::Wayland));
wayland::subscription().map(Msg::Wayland), }
]) iced::Subscription::batch(subscriptions)
} }
fn view(&self, id: SurfaceId) -> cosmic::Element<Msg> { fn view(&self, id: SurfaceId) -> cosmic::Element<Msg> {

View file

@ -59,7 +59,6 @@ pub use capture::CaptureFilter;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Event { pub enum Event {
CmdSender(calloop::channel::Sender<Cmd>), CmdSender(calloop::channel::Sender<Cmd>),
Connection(Connection),
ToplevelManager(zcosmic_toplevel_manager_v1::ZcosmicToplevelManagerV1), ToplevelManager(zcosmic_toplevel_manager_v1::ZcosmicToplevelManagerV1),
WorkspaceManager(zcosmic_workspace_manager_v1::ZcosmicWorkspaceManagerV1), WorkspaceManager(zcosmic_workspace_manager_v1::ZcosmicWorkspaceManagerV1),
// XXX Output name rather than `WlOutput` // XXX Output name rather than `WlOutput`
@ -87,8 +86,8 @@ pub enum Event {
Seats(Vec<wl_seat::WlSeat>), Seats(Vec<wl_seat::WlSeat>),
} }
pub fn subscription() -> iced::Subscription<Event> { pub fn subscription(conn: Connection) -> iced::Subscription<Event> {
iced::subscription::run_with_id("wayland-sub", async { start() }.flatten_stream()) iced::subscription::run_with_id("wayland-sub", async { start(conn) }.flatten_stream())
} }
#[derive(Debug)] #[derive(Debug)]
@ -264,11 +263,9 @@ impl OutputHandler for AppData {
} }
} }
fn start() -> mpsc::Receiver<Event> { fn start(conn: Connection) -> mpsc::Receiver<Event> {
let (sender, receiver) = mpsc::channel(20); 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 (globals, event_queue) = registry_queue_init(&conn).unwrap();
let qh = event_queue.handle(); let qh = event_queue.handle();
@ -290,7 +287,6 @@ fn start() -> mpsc::Receiver<Event> {
captures: RefCell::new(HashMap::new()), 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::Seats(app_data.seat_state.seats().collect()));
app_data.send_event(Event::ToplevelManager( app_data.send_event(Event::ToplevelManager(
app_data.toplevel_manager_state.manager.clone(), app_data.toplevel_manager_state.manager.clone(),