gtk(wayland): prevent gtk4-layer-shell crash on old versions (#7712)

Supersedes #7154

In gtk4-layer-shell versions < 1.0.4, the app could crash upon opening a
quick terminal window on certain compositors that implement the
`xdg_wm_dialog_v1` protocol. The exact reason is a bit complicated, but
is nicely summarized in the upstream issue (wmww/gtk4-layer-shell#50).

The circumstances that could cause this crash to occur should gradually
diminish as distros update to newer gtk4-layer-shell versions, but this
is known to crash on Fedora 41 and Hyprland, which could be a sizable
chunk of our userbase given that this would also occur on GNOME/Mutter
and KDE/KWin. The diff should be minimal enough that this can be removed
or reverted once this band-aid fix is no longer necessary.
This commit is contained in:
Leah Amelia Chen
2025-06-28 17:09:24 +02:00
committed by GitHub
2 changed files with 69 additions and 21 deletions

View File

@@ -1,3 +1,5 @@
const std = @import("std");
const c = @cImport({
@cInclude("gtk4-layer-shell.h");
});
@@ -31,6 +33,14 @@ pub fn getProtocolVersion() c_uint {
return c.gtk_layer_get_protocol_version();
}
pub fn getLibraryVersion() std.SemanticVersion {
return .{
.major = c.gtk_layer_get_major_version(),
.minor = c.gtk_layer_get_minor_version(),
.patch = c.gtk_layer_get_micro_version(),
};
}
pub fn initForWindow(window: *gtk.Window) void {
c.gtk_layer_init_for_window(@ptrCast(window));
}

View File

@@ -37,6 +37,19 @@ pub const App = struct {
default_deco_mode: ?org.KdeKwinServerDecorationManager.Mode = null,
xdg_activation: ?*xdg.ActivationV1 = null,
/// Whether the xdg_wm_dialog_v1 protocol is present.
///
/// If it is present, gtk4-layer-shell < 1.0.4 may crash when the user
/// creates a quick terminal, and we need to ensure this fails
/// gracefully if this situation occurs.
///
/// FIXME: This is a temporary workaround - we should remove this when
/// all of our supported distros drop support for affected old
/// gtk4-layer-shell versions.
///
/// See https://github.com/wmww/gtk4-layer-shell/issues/50
xdg_wm_dialog_present: bool = false,
};
pub fn init(
@@ -95,11 +108,21 @@ pub const App = struct {
return null;
}
pub fn supportsQuickTerminal(_: App) bool {
pub fn supportsQuickTerminal(self: App) bool {
if (!layer_shell.isSupported()) {
log.warn("your compositor does not support the wlr-layer-shell protocol; disabling quick terminal", .{});
return false;
}
if (self.context.xdg_wm_dialog_present and layer_shell.getLibraryVersion().order(.{
.major = 1,
.minor = 0,
.patch = 4,
}) == .lt) {
log.warn("the version of gtk4-layer-shell installed on your system is too old (must be 1.0.4 or newer); disabling quick terminal", .{});
return false;
}
return true;
}
@@ -111,26 +134,38 @@ pub const App = struct {
layer_shell.setNamespace(window, "ghostty-quick-terminal");
}
fn getInterfaceType(comptime field: std.builtin.Type.StructField) ?type {
// Globals should be optional pointers
const T = switch (@typeInfo(field.type)) {
.optional => |o| switch (@typeInfo(o.child)) {
.pointer => |v| v.child,
else => return null,
},
else => return null,
};
// Only process Wayland interfaces
if (!@hasDecl(T, "interface")) return null;
return T;
}
fn registryListener(
registry: *wl.Registry,
event: wl.Registry.Event,
context: *Context,
) void {
inline for (@typeInfo(Context).@"struct".fields) |field| {
// Globals should be optional pointers
const T = switch (@typeInfo(field.type)) {
.optional => |o| switch (@typeInfo(o.child)) {
.pointer => |v| v.child,
else => continue,
},
else => continue,
};
const ctx_fields = @typeInfo(Context).@"struct".fields;
// Only process Wayland interfaces
if (!@hasDecl(T, "interface")) continue;
switch (event) {
.global => |v| global: {
// We don't actually do anything with this other than checking
// for its existence, so we process this separately.
if (std.mem.orderZ(u8, v.interface, "xdg_wm_dialog_v1") == .eq)
context.xdg_wm_dialog_present = true;
inline for (ctx_fields) |field| {
const T = getInterfaceType(field) orelse continue;
switch (event) {
.global => |v| global: {
if (std.mem.orderZ(
u8,
v.interface,
@@ -148,19 +183,22 @@ pub const App = struct {
);
return;
};
},
}
},
// This should be a rare occurrence, but in case a global
// is suddenly no longer available, we destroy and unset it
// as the protocol mandates.
.global_remove => |v| remove: {
// This should be a rare occurrence, but in case a global
// is suddenly no longer available, we destroy and unset it
// as the protocol mandates.
.global_remove => |v| remove: {
inline for (ctx_fields) |field| {
if (getInterfaceType(field) == null) continue;
const global = @field(context, field.name) orelse break :remove;
if (global.getId() == v.name) {
global.destroy();
@field(context, field.name) = null;
}
},
}
}
},
}
}