wayland: Fix window close (#10702)

Partially fixes https://github.com/zed-industries/zed/issues/10483 (X11
still has this issue)

Also adds some missing destroy() calls for some objects.
Thanks @phisch!

Release Notes:

- N/A
This commit is contained in:
apricotbucket28
2024-04-18 16:02:05 -07:00
committed by GitHub
parent 95827d4c49
commit d4922eb10b
2 changed files with 61 additions and 20 deletions
@@ -118,8 +118,8 @@ pub(crate) struct WaylandClientState {
loop_handle: LoopHandle<'static, WaylandClientStatePtr>, loop_handle: LoopHandle<'static, WaylandClientStatePtr>,
cursor_icon_name: String, cursor_icon_name: String,
cursor: Cursor, cursor: Cursor,
clipboard: Clipboard, clipboard: Option<Clipboard>,
primary: Primary, primary: Option<Primary>,
event_loop: Option<EventLoop<'static, WaylandClientStatePtr>>, event_loop: Option<EventLoop<'static, WaylandClientStatePtr>>,
common: LinuxCommon, common: LinuxCommon,
} }
@@ -163,6 +163,12 @@ impl WaylandClientStatePtr {
state.mouse_focused_window = Some(window); state.mouse_focused_window = Some(window);
} }
} }
if state.windows.is_empty() {
// Drop the clipboard to prevent a seg fault after we've closed all Wayland connections.
state.clipboard = None;
state.primary = None;
state.common.signal.stop();
}
} }
} }
@@ -278,8 +284,8 @@ impl WaylandClient {
cursor_icon_name: "arrow".to_string(), cursor_icon_name: "arrow".to_string(),
enter_token: None, enter_token: None,
cursor, cursor,
clipboard, clipboard: Some(clipboard),
primary, primary: Some(primary),
event_loop: Some(event_loop), event_loop: Some(event_loop),
})); }));
@@ -378,17 +384,29 @@ impl LinuxClient for WaylandClient {
} }
fn write_to_primary(&self, item: crate::ClipboardItem) { fn write_to_primary(&self, item: crate::ClipboardItem) {
self.0.borrow_mut().primary.set_contents(item.text); self.0
.borrow_mut()
.primary
.as_mut()
.unwrap()
.set_contents(item.text);
} }
fn write_to_clipboard(&self, item: crate::ClipboardItem) { fn write_to_clipboard(&self, item: crate::ClipboardItem) {
self.0.borrow_mut().clipboard.set_contents(item.text); self.0
.borrow_mut()
.clipboard
.as_mut()
.unwrap()
.set_contents(item.text);
} }
fn read_from_primary(&self) -> Option<crate::ClipboardItem> { fn read_from_primary(&self) -> Option<crate::ClipboardItem> {
self.0 self.0
.borrow_mut() .borrow_mut()
.primary .primary
.as_mut()
.unwrap()
.get_contents() .get_contents()
.ok() .ok()
.map(|s| crate::ClipboardItem { .map(|s| crate::ClipboardItem {
@@ -401,6 +419,8 @@ impl LinuxClient for WaylandClient {
self.0 self.0
.borrow_mut() .borrow_mut()
.clipboard .clipboard
.as_mut()
.unwrap()
.get_contents() .get_contents()
.ok() .ok()
.map(|s| crate::ClipboardItem { .map(|s| crate::ClipboardItem {
@@ -68,6 +68,7 @@ unsafe impl HasRawDisplayHandle for RawWindow {
pub struct WaylandWindowState { pub struct WaylandWindowState {
xdg_surface: xdg_surface::XdgSurface, xdg_surface: xdg_surface::XdgSurface,
pub surface: wl_surface::WlSurface, pub surface: wl_surface::WlSurface,
decoration: Option<zxdg_toplevel_decoration_v1::ZxdgToplevelDecorationV1>,
toplevel: xdg_toplevel::XdgToplevel, toplevel: xdg_toplevel::XdgToplevel,
viewport: Option<wp_viewport::WpViewport>, viewport: Option<wp_viewport::WpViewport>,
outputs: HashSet<ObjectId>, outputs: HashSet<ObjectId>,
@@ -90,11 +91,13 @@ pub struct WaylandWindowStatePtr {
} }
impl WaylandWindowState { impl WaylandWindowState {
#[allow(clippy::too_many_arguments)]
pub(crate) fn new( pub(crate) fn new(
surface: wl_surface::WlSurface, surface: wl_surface::WlSurface,
xdg_surface: xdg_surface::XdgSurface, xdg_surface: xdg_surface::XdgSurface,
viewport: Option<wp_viewport::WpViewport>,
toplevel: xdg_toplevel::XdgToplevel, toplevel: xdg_toplevel::XdgToplevel,
decoration: Option<zxdg_toplevel_decoration_v1::ZxdgToplevelDecorationV1>,
viewport: Option<wp_viewport::WpViewport>,
client: WaylandClientStatePtr, client: WaylandClientStatePtr,
globals: Globals, globals: Globals,
options: WindowParams, options: WindowParams,
@@ -132,6 +135,7 @@ impl WaylandWindowState {
Self { Self {
xdg_surface, xdg_surface,
surface, surface,
decoration,
toplevel, toplevel,
viewport, viewport,
globals, globals,
@@ -158,16 +162,27 @@ impl Drop for WaylandWindow {
let mut state = self.0.state.borrow_mut(); let mut state = self.0.state.borrow_mut();
let surface_id = state.surface.id(); let surface_id = state.surface.id();
let client = state.client.clone(); let client = state.client.clone();
state.renderer.destroy(); state.renderer.destroy();
if let Some(decoration) = &state.decoration {
decoration.destroy();
}
state.toplevel.destroy(); state.toplevel.destroy();
if let Some(viewport) = &state.viewport {
viewport.destroy();
}
state.xdg_surface.destroy(); state.xdg_surface.destroy();
state.surface.destroy(); state.surface.destroy();
let state_ptr = self.0.clone(); let state_ptr = self.0.clone();
state.globals.executor.spawn(async move { state
state_ptr.close(); .globals
client.drop_window(&surface_id) .executor
}); .spawn(async move {
state_ptr.close();
client.drop_window(&surface_id)
})
.detach();
drop(state); drop(state);
} }
} }
@@ -197,13 +212,18 @@ impl WaylandWindow {
} }
// Attempt to set up window decorations based on the requested configuration // Attempt to set up window decorations based on the requested configuration
if let Some(decoration_manager) = globals.decoration_manager.as_ref() { let decoration = globals
let decoration = .decoration_manager
decoration_manager.get_toplevel_decoration(&toplevel, &globals.qh, surface.id()); .as_ref()
.map(|decoration_manager| {
// Request client side decorations if possible let decoration = decoration_manager.get_toplevel_decoration(
decoration.set_mode(zxdg_toplevel_decoration_v1::Mode::ClientSide); &toplevel,
} &globals.qh,
surface.id(),
);
decoration.set_mode(zxdg_toplevel_decoration_v1::Mode::ClientSide);
decoration
});
let viewport = globals let viewport = globals
.viewporter .viewporter
@@ -216,8 +236,9 @@ impl WaylandWindow {
state: Rc::new(RefCell::new(WaylandWindowState::new( state: Rc::new(RefCell::new(WaylandWindowState::new(
surface.clone(), surface.clone(),
xdg_surface, xdg_surface,
viewport,
toplevel, toplevel,
decoration,
viewport,
client, client,
globals, globals,
params, params,
@@ -319,7 +340,7 @@ impl WaylandWindowStatePtr {
} }
result result
} else { } else {
false true
} }
} }
_ => false, _ => false,