diff --git a/src/apprt/gtk/Surface.zig b/src/apprt/gtk/Surface.zig index dce2517ab..ecf59a647 100644 --- a/src/apprt/gtk/Surface.zig +++ b/src/apprt/gtk/Surface.zig @@ -857,12 +857,9 @@ pub fn setInitialWindowSize(self: *const Surface, width: u32, height: u32) !void @intCast(height), ); - // TODO: move this check somewhere else so we can reuse it // If the current WM doesn't support _NET_WM_STATE, we shouldn't try to set it. const gdk_surface = c.gtk_native_get_surface(@ptrCast(window.window)); - if (c.g_type_check_instance_is_a(@ptrCast(@alignCast(gdk_surface)), c.gdk_x11_surface_get_type()) == 0) return; // X11 only, sorry Wayland - - if (c.XInternAtom(c.gdk_x11_display_get_xdisplay(c.gdk_surface_get_display(gdk_surface)), "_NET_WM_STATE", 1) == 0) { + if (!x11.should_use_net_wm_state(gdk_surface)) { log.warn("current WM does not support _NET_WM_STATE", .{}); return; } diff --git a/src/apprt/gtk/Window.zig b/src/apprt/gtk/Window.zig index e18e85c0e..cf56029ae 100644 --- a/src/apprt/gtk/Window.zig +++ b/src/apprt/gtk/Window.zig @@ -26,6 +26,7 @@ const Notebook = @import("notebook.zig").Notebook; const HeaderBar = @import("headerbar.zig").HeaderBar; const version = @import("version.zig"); const wayland = @import("wayland.zig"); +const x11 = @import("x11.zig"); const log = std.log.scoped(.gtk); @@ -623,16 +624,13 @@ fn gtkWindowNotifyMaximized( _: ?*anyopaque, ) callconv(.C) void { const gdk_surface = c.gtk_native_get_surface(@ptrCast(window)); - if (c.g_type_check_instance_is_a(@ptrCast(@alignCast(gdk_surface)), c.gdk_x11_surface_get_type()) == 0) return; // X11 only, sorry Wayland - - const xdisplay = c.gdk_x11_display_get_xdisplay(c.gdk_surface_get_display(gdk_surface)) orelse return; - - // If the WM doesn't support _NET_WM_STATE, no need to continue. - if (c.XInternAtom(xdisplay, "_NET_WM_STATE", 1) == 0) { + if (!x11.should_use_net_wm_state(gdk_surface)) { log.warn("current WM does not support _NET_WM_STATE", .{}); return; } + const xdisplay = c.gdk_x11_display_get_xdisplay(c.gdk_surface_get_display(gdk_surface)) orelse return; + // https://tronche.com/gui/x/xlib/events/client-communication/client-message.html#XClientMessageEvent var client_message_event: c.XClientMessageEvent = std.mem.zeroes(c.XClientMessageEvent); client_message_event.type = c.ClientMessage; diff --git a/src/apprt/gtk/x11.zig b/src/apprt/gtk/x11.zig index 21ff87b34..67b7b00ab 100644 --- a/src/apprt/gtk/x11.zig +++ b/src/apprt/gtk/x11.zig @@ -22,6 +22,17 @@ pub fn is_current_display_server() bool { return is_display(display); } +/// Returns true if we have a valid X11 surface and the current WM supports _NET_WM_STATE. +pub fn should_use_net_wm_state(surface: ?*c.GdkSurface) bool { + if (comptime !build_options.x11) return false; + if (c.g_type_check_instance_is_a( + @ptrCast(@alignCast(surface)), + c.gdk_x11_surface_get_type(), + ) == 0) return false; + + return c.XInternAtom(c.gdk_x11_display_get_xdisplay(c.gdk_surface_get_display(surface)), "_NET_WM_STATE", 1) != 0; +} + pub const Xkb = struct { base_event_code: c_int,