From ed81b62ec24790fea79877d2e2124d82271ee3df Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 9 Jan 2025 20:00:30 -0800 Subject: [PATCH] apprt/gtk: winproto Rename "protocol" to "winproto". --- src/apprt/gtk/App.zig | 34 ++- src/apprt/gtk/Surface.zig | 15 +- src/apprt/gtk/Window.zig | 38 +++- src/apprt/gtk/key.zig | 6 +- src/apprt/gtk/protocol.zig | 149 ------------- src/apprt/gtk/protocol/wayland.zig | 125 ----------- src/apprt/gtk/winproto.zig | 128 +++++++++++ src/apprt/gtk/winproto/noop.zig | 56 +++++ src/apprt/gtk/winproto/wayland.zig | 211 +++++++++++++++++++ src/apprt/gtk/{protocol => winproto}/x11.zig | 178 +++++++++++----- 10 files changed, 589 insertions(+), 351 deletions(-) delete mode 100644 src/apprt/gtk/protocol.zig delete mode 100644 src/apprt/gtk/protocol/wayland.zig create mode 100644 src/apprt/gtk/winproto.zig create mode 100644 src/apprt/gtk/winproto/noop.zig create mode 100644 src/apprt/gtk/winproto/wayland.zig rename src/apprt/gtk/{protocol => winproto}/x11.zig (65%) diff --git a/src/apprt/gtk/App.zig b/src/apprt/gtk/App.zig index ecbe61bce..b041d29fb 100644 --- a/src/apprt/gtk/App.zig +++ b/src/apprt/gtk/App.zig @@ -36,7 +36,7 @@ const c = @import("c.zig").c; const version = @import("version.zig"); const inspector = @import("inspector.zig"); const key = @import("key.zig"); -const protocol = @import("protocol.zig"); +const winproto = @import("winproto.zig"); const testing = std.testing; const log = std.log.scoped(.gtk); @@ -49,6 +49,9 @@ config: Config, app: *c.GtkApplication, ctx: *c.GMainContext, +/// State and logic for the underlying windowing protocol. +winproto: winproto.App, + /// True if the app was launched with single instance mode. single_instance: bool, @@ -70,8 +73,6 @@ clipboard_confirmation_window: ?*ClipboardConfirmationWindow = null, /// This is set to false when the main loop should exit. running: bool = true, -protocol: protocol.App, - /// The base path of the transient cgroup used to put all surfaces /// into their own cgroup. This is only set if cgroups are enabled /// and initialization was successful. @@ -161,7 +162,12 @@ pub fn init(core_app: *CoreApp, opts: Options) !App { } c.gtk_init(); - const display = c.gdk_display_get_default(); + const display: *c.GdkDisplay = c.gdk_display_get_default() orelse { + // I'm unsure of any scenario where this happens. Because we don't + // want to litter null checks everywhere, we just exit here. + log.warn("gdk display is null, exiting", .{}); + std.posix.exit(1); + }; // If we're using libadwaita, log the version if (adwaita.enabled(&config)) { @@ -359,7 +365,14 @@ pub fn init(core_app: *CoreApp, opts: Options) !App { return error.GtkApplicationRegisterFailed; } - const app_protocol = try protocol.App.init(display, &config, app_id); + // Setup our windowing protocol logic + var winproto_app = try winproto.App.init( + core_app.alloc, + display, + app_id, + &config, + ); + errdefer winproto_app.deinit(core_app.alloc); // This just calls the `activate` signal but its part of the normal startup // routine so we just call it, but only if the config allows it (this allows @@ -385,7 +398,7 @@ pub fn init(core_app: *CoreApp, opts: Options) !App { .config = config, .ctx = ctx, .cursor_none = cursor_none, - .protocol = app_protocol, + .winproto = winproto_app, .single_instance = single_instance, // If we are NOT the primary instance, then we never want to run. // This means that another instance of the GTK app is running and @@ -413,6 +426,8 @@ pub fn terminate(self: *App) void { } self.custom_css_providers.deinit(self.core_app.alloc); + self.winproto.deinit(self.core_app.alloc); + self.config.deinit(); } @@ -837,9 +852,10 @@ fn configChange( new_config: *const Config, ) void { switch (target) { - .surface => |surface| { - if (surface.rt_surface.container.window()) |window| window.syncAppearance(new_config) catch |err| { - log.warn("error syncing appearance changes to window err={}", .{err}); + .surface => |surface| surface: { + const window = surface.rt_surface.container.window() orelse break :surface; + window.updateConfig(new_config) catch |err| { + log.warn("error updating config for window err={}", .{err}); }; }, diff --git a/src/apprt/gtk/Surface.zig b/src/apprt/gtk/Surface.zig index 97b7bb0f4..c16f696b1 100644 --- a/src/apprt/gtk/Surface.zig +++ b/src/apprt/gtk/Surface.zig @@ -824,6 +824,9 @@ pub fn getContentScale(self: *const Surface) !apprt.ContentScale { c.g_object_get_property(@ptrCast(@alignCast(settings)), "gtk-xft-dpi", &value); const gtk_xft_dpi = c.g_value_get_int(&value); + // As noted above gtk-xft-dpi is multiplied by 1024, so we divide by + // 1024, then divide by the default value (96) to derive a scale. Note + // gtk-xft-dpi can be fractional, so we use floating point math here. const xft_dpi: f32 = @as(f32, @floatFromInt(gtk_xft_dpi)) / 1024; break :xft_scale xft_dpi / 96; }; @@ -1380,9 +1383,13 @@ fn gtkResize(area: *c.GtkGLArea, width: c.gint, height: c.gint, ud: ?*anyopaque) return; }; - if (self.container.window()) |window| window.protocol.onResize() catch |err| { - log.warn("failed to notify X11/Wayland integration of resize={}", .{err}); - }; + if (self.container.window()) |window| { + if (window.winproto) |*winproto| { + winproto.resizeEvent() catch |err| { + log.warn("failed to notify window protocol of resize={}", .{err}); + }; + } + } self.resize_overlay.maybeShow(); } @@ -1702,7 +1709,7 @@ pub fn keyEvent( event, physical_key, gtk_mods, - &self.app.protocol, + &self.app.winproto, ); // Get our consumed modifiers diff --git a/src/apprt/gtk/Window.zig b/src/apprt/gtk/Window.zig index fbba22195..d0e678057 100644 --- a/src/apprt/gtk/Window.zig +++ b/src/apprt/gtk/Window.zig @@ -25,7 +25,7 @@ const gtk_key = @import("key.zig"); const Notebook = @import("notebook.zig").Notebook; const HeaderBar = @import("headerbar.zig").HeaderBar; const version = @import("version.zig"); -const protocol = @import("protocol.zig"); +const winproto = @import("winproto.zig"); const log = std.log.scoped(.gtk); @@ -56,7 +56,8 @@ toast_overlay: ?*c.GtkWidget, /// See adwTabOverviewOpen for why we have this. adw_tab_overview_focus_timer: ?c.guint = null, -protocol: protocol.Surface, +/// State and logic for windowing protocol for a window. +winproto: ?winproto.Window, pub fn create(alloc: Allocator, app: *App) !*Window { // Allocate a fixed pointer for our window. We try to minimize @@ -82,7 +83,7 @@ pub fn init(self: *Window, app: *App) !void { .notebook = undefined, .context_menu = undefined, .toast_overlay = undefined, - .protocol = undefined, + .winproto = null, }; // Create the window @@ -384,6 +385,16 @@ pub fn init(self: *Window, app: *App) !void { c.gtk_widget_show(window); } +pub fn updateConfig( + self: *Window, + config: *const configpkg.Config, +) !void { + if (self.winproto) |*v| try v.updateConfigEvent(config); + + // We always resync our appearance whenever the config changes. + try self.syncAppearance(config); +} + /// Updates appearance based on config settings. Will be called once upon window /// realization, and every time the config is reloaded. /// @@ -396,8 +407,10 @@ pub fn syncAppearance(self: *Window, config: *const configpkg.Config) !void { c.gtk_widget_add_css_class(@ptrCast(self.window), "background"); } - // Perform protocol-specific config updates - try self.protocol.onConfigUpdate(config); + // Window protocol specific appearance updates + if (self.winproto) |*v| v.syncAppearance() catch |err| { + log.warn("failed to sync window protocol appearance error={}", .{err}); + }; } /// Sets up the GTK actions for the window scope. Actions are how GTK handles @@ -437,7 +450,7 @@ fn initActions(self: *Window) void { pub fn deinit(self: *Window) void { c.gtk_widget_unparent(@ptrCast(self.context_menu)); - self.protocol.deinit(); + if (self.winproto) |*v| v.deinit(self.app.core_app.alloc); if (self.adw_tab_overview_focus_timer) |timer| { _ = c.g_source_remove(timer); @@ -578,8 +591,19 @@ pub fn sendToast(self: *Window, title: [:0]const u8) void { fn gtkRealize(v: *c.GtkWindow, ud: ?*anyopaque) callconv(.C) bool { const self = userdataSelf(ud.?); - self.protocol.init(v, &self.app.protocol, &self.app.config); + // Initialize our window protocol logic + if (winproto.Window.init( + self.app.core_app.alloc, + &self.app.winproto, + v, + &self.app.config, + )) |winproto_win| { + self.winproto = winproto_win; + } else |err| { + log.warn("failed to initialize window protocol error={}", .{err}); + } + // When we are realized we always setup our appearance self.syncAppearance(&self.app.config) catch |err| { log.err("failed to initialize appearance={}", .{err}); }; diff --git a/src/apprt/gtk/key.zig b/src/apprt/gtk/key.zig index ef460e62c..40c9ca9a4 100644 --- a/src/apprt/gtk/key.zig +++ b/src/apprt/gtk/key.zig @@ -2,7 +2,7 @@ const std = @import("std"); const build_options = @import("build_options"); const input = @import("../../input.zig"); const c = @import("c.zig").c; -const protocol = @import("protocol.zig"); +const winproto = @import("winproto.zig"); /// Returns a GTK accelerator string from a trigger. pub fn accelFromTrigger(buf: []u8, trigger: input.Binding.Trigger) !?[:0]const u8 { @@ -108,11 +108,11 @@ pub fn eventMods( event: *c.GdkEvent, physical_key: input.Key, gtk_mods: c.GdkModifierType, - app_protocol: *protocol.App, + app_winproto: *winproto.App, ) input.Mods { const device = c.gdk_event_get_device(event); - var mods = app_protocol.eventMods(device, gtk_mods); + var mods = app_winproto.eventMods(device, gtk_mods); mods.num_lock = c.gdk_device_get_num_lock_state(device) == 1; switch (physical_key) { diff --git a/src/apprt/gtk/protocol.zig b/src/apprt/gtk/protocol.zig deleted file mode 100644 index c7d7247cf..000000000 --- a/src/apprt/gtk/protocol.zig +++ /dev/null @@ -1,149 +0,0 @@ -const std = @import("std"); -const x11 = @import("protocol/x11.zig"); -const wayland = @import("protocol/wayland.zig"); -const c = @import("c.zig").c; -const build_options = @import("build_options"); -const input = @import("../../input.zig"); -const apprt = @import("../../apprt.zig"); -const Config = @import("../../config.zig").Config; -const adwaita = @import("adwaita.zig"); -const builtin = @import("builtin"); -const key = @import("key.zig"); - -const log = std.log.scoped(.gtk_platform); - -pub const App = struct { - gdk_display: *c.GdkDisplay, - derived_config: DerivedConfig, - - inner: union(enum) { - none, - x11: if (build_options.x11) x11.App else void, - wayland: if (build_options.wayland) wayland.App else void, - }, - - const DerivedConfig = struct { - app_id: [:0]const u8, - x11_program_name: [:0]const u8, - - pub fn init(config: *const Config, app_id: [:0]const u8) DerivedConfig { - return .{ - .app_id = app_id, - .x11_program_name = if (config.@"x11-instance-name") |pn| - pn - else if (builtin.mode == .Debug) - "ghostty-debug" - else - "ghostty", - }; - } - }; - - pub fn init(display: ?*c.GdkDisplay, config: *const Config, app_id: [:0]const u8) !App { - var self: App = .{ - .inner = .none, - .derived_config = DerivedConfig.init(config, app_id), - .gdk_display = display orelse { - // TODO: When does this ever happen...? - std.debug.panic("GDK display is null!", .{}); - }, - }; - - // The X11/Wayland init functions set `self.inner` when successful, - // so we only need to keep trying if `self.inner` stays `.none` - if (self.inner == .none and comptime build_options.wayland) try wayland.App.init(&self); - if (self.inner == .none and comptime build_options.x11) try x11.App.init(&self); - - // Welp, no integration for you - if (self.inner == .none) { - log.warn( - "neither X11 nor Wayland integrations enabled - lots of features would be missing!", - .{}, - ); - } - - return self; - } - - pub fn eventMods(self: *App, device: ?*c.GdkDevice, gtk_mods: c.GdkModifierType) input.Mods { - return switch (self.inner) { - // Add any modifier state events from Xkb if we have them (X11 - // only). Null back from the Xkb call means there was no modifier - // event to read. This likely means that the key event did not - // result in a modifier change and we can safely rely on the GDK - // state. - .x11 => |*x| if (comptime build_options.x11) - x.modifierStateFromNotify() orelse key.translateMods(gtk_mods) - else - unreachable, - - // On Wayland, we have to use the GDK device because the mods sent - // to this event do not have the modifier key applied if it was - // pressed (i.e. left control). - .wayland, .none => key.translateMods(c.gdk_device_get_modifier_state(device)), - }; - } -}; - -pub const Surface = struct { - app: *App, - gtk_window: *c.GtkWindow, - derived_config: DerivedConfig, - - inner: union(enum) { - none, - x11: if (build_options.x11) x11.Surface else void, - wayland: if (build_options.wayland) wayland.Surface else void, - }, - - pub const DerivedConfig = struct { - blur: Config.BackgroundBlur, - adw_enabled: bool, - - pub fn init(config: *const Config) DerivedConfig { - return .{ - .blur = config.@"background-blur-radius", - .adw_enabled = adwaita.enabled(config), - }; - } - }; - - pub fn init(self: *Surface, window: *c.GtkWindow, app: *App, config: *const Config) void { - self.* = .{ - .app = app, - .derived_config = DerivedConfig.init(config), - .gtk_window = window, - .inner = .none, - }; - - switch (app.inner) { - .x11 => if (comptime build_options.x11) x11.Surface.init(self) else unreachable, - .wayland => if (comptime build_options.wayland) wayland.Surface.init(self) else unreachable, - .none => {}, - } - } - - pub fn deinit(self: Surface) void { - switch (self.inner) { - .wayland => |wl| if (comptime build_options.wayland) wl.deinit() else unreachable, - .x11, .none => {}, - } - } - - pub fn onConfigUpdate(self: *Surface, config: *const Config) !void { - self.derived_config = DerivedConfig.init(config); - - switch (self.inner) { - .x11 => |*x| if (comptime build_options.x11) try x.onConfigUpdate() else unreachable, - .wayland => |*wl| if (comptime build_options.wayland) try wl.onConfigUpdate() else unreachable, - .none => {}, - } - } - - pub fn onResize(self: *Surface) !void { - switch (self.inner) { - .x11 => |*x| if (comptime build_options.x11) try x.onResize() else unreachable, - .wayland, .none => {}, - } - } -}; diff --git a/src/apprt/gtk/protocol/wayland.zig b/src/apprt/gtk/protocol/wayland.zig deleted file mode 100644 index 985d7c5a8..000000000 --- a/src/apprt/gtk/protocol/wayland.zig +++ /dev/null @@ -1,125 +0,0 @@ -const std = @import("std"); -const c = @import("../c.zig").c; -const wayland = @import("wayland"); -const protocol = @import("../protocol.zig"); -const Config = @import("../../../config.zig").Config; - -const wl = wayland.client.wl; -const org = wayland.client.org; - -const log = std.log.scoped(.gtk_wayland); - -/// Wayland state that contains application-wide Wayland objects (e.g. wl_display). -pub const App = struct { - display: *wl.Display, - blur_manager: ?*org.KdeKwinBlurManager = null, - - pub fn init(common: *protocol.App) !void { - // Check if we're actually on Wayland - if (c.g_type_check_instance_is_a( - @ptrCast(@alignCast(common.gdk_display)), - c.gdk_wayland_display_get_type(), - ) == 0) - return; - - var self: App = .{ - .display = @ptrCast(c.gdk_wayland_display_get_wl_display(common.gdk_display) orelse return), - }; - - log.debug("wayland platform init={}", .{self}); - - const registry = try self.display.getRegistry(); - - registry.setListener(*App, registryListener, &self); - if (self.display.roundtrip() != .SUCCESS) return error.RoundtripFailed; - - common.inner = .{ .wayland = self }; - } -}; - -/// Wayland state that contains Wayland objects associated with a window (e.g. wl_surface). -pub const Surface = struct { - common: *const protocol.Surface, - app: *App, - surface: *wl.Surface, - - /// A token that, when present, indicates that the window is blurred. - blur_token: ?*org.KdeKwinBlur = null, - - pub fn init(common: *protocol.Surface) void { - const surface = c.gtk_native_get_surface(@ptrCast(common.gtk_window)) orelse return; - - // Check if we're actually on Wayland - if (c.g_type_check_instance_is_a( - @ptrCast(@alignCast(surface)), - c.gdk_wayland_surface_get_type(), - ) == 0) - return; - - const self: Surface = .{ - .common = common, - .app = &common.app.inner.wayland, - .surface = @ptrCast(c.gdk_wayland_surface_get_wl_surface(surface) orelse return), - }; - - common.inner = .{ .wayland = self }; - } - - pub fn deinit(self: Surface) void { - if (self.blur_token) |blur| blur.release(); - } - - pub fn onConfigUpdate(self: *Surface) !void { - try self.updateBlur(); - } - - fn updateBlur(self: *Surface) !void { - const blur = self.common.derived_config.blur; - log.debug("setting blur={}", .{blur}); - - const mgr = self.app.blur_manager orelse { - log.warn("can't set blur: org_kde_kwin_blur_manager protocol unavailable", .{}); - return; - }; - - if (self.blur_token) |tok| { - // Only release token when transitioning from blurred -> not blurred - if (!blur.enabled()) { - mgr.unset(self.surface); - tok.release(); - self.blur_token = null; - } - } else { - // Only acquire token when transitioning from not blurred -> blurred - if (blur.enabled()) { - const tok = try mgr.create(self.surface); - tok.commit(); - self.blur_token = tok; - } - } - } -}; - -fn registryListener(registry: *wl.Registry, event: wl.Registry.Event, state: *App) void { - switch (event) { - .global => |global| { - log.debug("got global interface={s}", .{global.interface}); - if (bindInterface(org.KdeKwinBlurManager, registry, global, 1)) |iface| { - state.blur_manager = iface; - return; - } - }, - .global_remove => {}, - } -} - -fn bindInterface(comptime T: type, registry: *wl.Registry, global: anytype, version: u32) ?*T { - if (std.mem.orderZ(u8, global.interface, T.interface.name) == .eq) { - return registry.bind(global.name, T, version) catch |err| { - log.warn("encountered error={} while binding interface {s}", .{ err, global.interface }); - return null; - }; - } else { - return null; - } -} diff --git a/src/apprt/gtk/winproto.zig b/src/apprt/gtk/winproto.zig new file mode 100644 index 000000000..49d96bb02 --- /dev/null +++ b/src/apprt/gtk/winproto.zig @@ -0,0 +1,128 @@ +const std = @import("std"); +const build_options = @import("build_options"); +const Allocator = std.mem.Allocator; +const c = @import("c.zig").c; +const Config = @import("../../config.zig").Config; +const input = @import("../../input.zig"); +const key = @import("key.zig"); + +pub const noop = @import("winproto/noop.zig"); +pub const x11 = @import("winproto/x11.zig"); +pub const wayland = @import("winproto/wayland.zig"); + +pub const Protocol = enum { + none, + wayland, + x11, +}; + +/// App-state for the underlying windowing protocol. There should be one +/// instance of this struct per application. +pub const App = union(Protocol) { + none: noop.App, + wayland: if (build_options.wayland) wayland.App else noop.App, + x11: if (build_options.x11) x11.App else noop.App, + + pub fn init( + alloc: Allocator, + gdk_display: *c.GdkDisplay, + app_id: [:0]const u8, + config: *const Config, + ) !App { + inline for (@typeInfo(App).Union.fields) |field| { + if (try field.type.init( + alloc, + gdk_display, + app_id, + config, + )) |v| { + return @unionInit(App, field.name, v); + } + } + + return .none; + } + + pub fn deinit(self: *App, alloc: Allocator) void { + switch (self.*) { + inline else => |*v| v.deinit(alloc), + } + } + + pub fn eventMods( + self: *App, + device: ?*c.GdkDevice, + gtk_mods: c.GdkModifierType, + ) input.Mods { + return switch (self.*) { + inline else => |*v| v.eventMods(device, gtk_mods), + } orelse key.translateMods(gtk_mods); + } +}; + +/// Per-Window state for the underlying windowing protocol. +/// +/// In both X and Wayland, the terminology used is "Surface" and this is +/// really "Surface"-specific state. But Ghostty uses the term "Surface" +/// heavily to mean something completely different, so we use "Window" here +/// to better match what it generally maps to in the Ghostty codebase. +pub const Window = union(Protocol) { + none: noop.Window, + wayland: if (build_options.wayland) wayland.Window else noop.Window, + x11: if (build_options.x11) x11.Window else noop.Window, + + pub fn init( + alloc: Allocator, + app: *App, + window: *c.GtkWindow, + config: *const Config, + ) !Window { + return switch (app.*) { + inline else => |*v, tag| { + inline for (@typeInfo(Window).Union.fields) |field| { + if (comptime std.mem.eql( + u8, + field.name, + @tagName(tag), + )) return @unionInit( + Window, + field.name, + try field.type.init( + alloc, + v, + window, + config, + ), + ); + } + }, + }; + } + + pub fn deinit(self: *Window, alloc: Allocator) void { + switch (self.*) { + inline else => |*v| v.deinit(alloc), + } + } + + pub fn resizeEvent(self: *Window) !void { + switch (self.*) { + inline else => |*v| try v.resizeEvent(), + } + } + + pub fn updateConfigEvent( + self: *Window, + config: *const Config, + ) !void { + switch (self.*) { + inline else => |*v| try v.updateConfigEvent(config), + } + } + + pub fn syncAppearance(self: *Window) !void { + switch (self.*) { + inline else => |*v| try v.syncAppearance(), + } + } +}; diff --git a/src/apprt/gtk/winproto/noop.zig b/src/apprt/gtk/winproto/noop.zig new file mode 100644 index 000000000..54c14fe14 --- /dev/null +++ b/src/apprt/gtk/winproto/noop.zig @@ -0,0 +1,56 @@ +const std = @import("std"); +const Allocator = std.mem.Allocator; +const c = @import("../c.zig").c; +const Config = @import("../../../config.zig").Config; +const input = @import("../../../input.zig"); + +const log = std.log.scoped(.winproto_noop); + +pub const App = struct { + pub fn init( + _: Allocator, + _: *c.GdkDisplay, + _: [:0]const u8, + _: *const Config, + ) !?App { + return .{}; + } + + pub fn deinit(self: *App, alloc: Allocator) void { + _ = self; + _ = alloc; + } + + pub fn eventMods( + _: *App, + _: ?*c.GdkDevice, + _: c.GdkModifierType, + ) ?input.Mods { + return null; + } +}; + +pub const Window = struct { + pub fn init( + _: Allocator, + _: *App, + _: *c.GtkWindow, + _: *const Config, + ) !Window { + return .{}; + } + + pub fn deinit(self: Window, alloc: Allocator) void { + _ = self; + _ = alloc; + } + + pub fn updateConfigEvent( + _: *Window, + _: *const Config, + ) !void {} + + pub fn resizeEvent(_: *Window) !void {} + + pub fn syncAppearance(_: *Window) !void {} +}; diff --git a/src/apprt/gtk/winproto/wayland.zig b/src/apprt/gtk/winproto/wayland.zig new file mode 100644 index 000000000..3f7ad0068 --- /dev/null +++ b/src/apprt/gtk/winproto/wayland.zig @@ -0,0 +1,211 @@ +//! Wayland protocol implementation for the Ghostty GTK apprt. +const std = @import("std"); +const wayland = @import("wayland"); +const Allocator = std.mem.Allocator; +const c = @import("../c.zig").c; +const Config = @import("../../../config.zig").Config; +const input = @import("../../../input.zig"); + +const wl = wayland.client.wl; +const org = wayland.client.org; + +const log = std.log.scoped(.winproto_wayland); + +/// Wayland state that contains application-wide Wayland objects (e.g. wl_display). +pub const App = struct { + display: *wl.Display, + context: *Context, + + const Context = struct { + kde_blur_manager: ?*org.KdeKwinBlurManager = null, + }; + + pub fn init( + alloc: Allocator, + gdk_display: *c.GdkDisplay, + app_id: [:0]const u8, + config: *const Config, + ) !?App { + _ = config; + _ = app_id; + + // Check if we're actually on Wayland + if (c.g_type_check_instance_is_a( + @ptrCast(@alignCast(gdk_display)), + c.gdk_wayland_display_get_type(), + ) == 0) return null; + + const display: *wl.Display = @ptrCast(c.gdk_wayland_display_get_wl_display( + gdk_display, + ) orelse return error.NoWaylandDisplay); + + // Create our context for our callbacks so we have a stable pointer. + // Note: at the time of writing this comment, we don't really need + // a stable pointer, but it's too scary that we'd need one in the future + // and not have it and corrupt memory or something so let's just do it. + const context = try alloc.create(Context); + errdefer alloc.destroy(context); + context.* = .{}; + + // Get our display registry so we can get all the available interfaces + // and bind to what we need. + const registry = try display.getRegistry(); + registry.setListener(*Context, registryListener, context); + if (display.roundtrip() != .SUCCESS) return error.RoundtripFailed; + + return .{ + .display = display, + .context = context, + }; + } + + pub fn deinit(self: *App, alloc: Allocator) void { + alloc.destroy(self.context); + } + + pub fn eventMods( + _: *App, + _: ?*c.GdkDevice, + _: c.GdkModifierType, + ) ?input.Mods { + return null; + } + + fn registryListener( + registry: *wl.Registry, + event: wl.Registry.Event, + context: *Context, + ) void { + switch (event) { + // https://wayland.app/protocols/wayland#wl_registry:event:global + .global => |global| global: { + log.debug("wl_registry.global: interface={s}", .{global.interface}); + + if (registryBind( + org.KdeKwinBlurManager, + registry, + global, + 1, + )) |blur_manager| { + context.kde_blur_manager = blur_manager; + break :global; + } + }, + + // We don't handle removal events + .global_remove => {}, + } + } + + fn registryBind( + comptime T: type, + registry: *wl.Registry, + global: anytype, + version: u32, + ) ?*T { + if (std.mem.orderZ( + u8, + global.interface, + T.interface.name, + ) != .eq) return null; + + return registry.bind(global.name, T, version) catch |err| { + log.warn("error binding interface {s} error={}", .{ + global.interface, + err, + }); + return null; + }; + } +}; + +/// Per-window (wl_surface) state for the Wayland protocol. +pub const Window = struct { + config: DerivedConfig, + + /// The Wayland surface for this window. + surface: *wl.Surface, + + /// The context from the app where we can load our Wayland interfaces. + app_context: *App.Context, + + /// A token that, when present, indicates that the window is blurred. + blur_token: ?*org.KdeKwinBlur = null, + + const DerivedConfig = struct { + blur: bool, + + pub fn init(config: *const Config) DerivedConfig { + return .{ + .blur = config.@"background-blur-radius".enabled(), + }; + } + }; + + pub fn init( + alloc: Allocator, + app: *App, + gtk_window: *c.GtkWindow, + config: *const Config, + ) !Window { + _ = alloc; + + const gdk_surface = c.gtk_native_get_surface( + @ptrCast(gtk_window), + ) orelse return error.NotWaylandSurface; + + // This should never fail, because if we're being called at this point + // then we've already asserted that our app state is Wayland. + if (c.g_type_check_instance_is_a( + @ptrCast(@alignCast(gdk_surface)), + c.gdk_wayland_surface_get_type(), + ) == 0) return error.NotWaylandSurface; + + const wl_surface: *wl.Surface = @ptrCast(c.gdk_wayland_surface_get_wl_surface( + gdk_surface, + ) orelse return error.NoWaylandSurface); + + return .{ + .config = DerivedConfig.init(config), + .surface = wl_surface, + .app_context = app.context, + }; + } + + pub fn deinit(self: Window, alloc: Allocator) void { + _ = alloc; + if (self.blur_token) |blur| blur.release(); + } + + pub fn updateConfigEvent(self: *Window, config: *const Config) !void { + self.config = DerivedConfig.init(config); + } + + pub fn resizeEvent(_: *Window) !void {} + + pub fn syncAppearance(self: *Window) !void { + try self.syncBlur(); + } + + /// Update the blur state of the window. + fn syncBlur(self: *Window) !void { + const manager = self.app_context.kde_blur_manager orelse return; + const blur = self.config.blur; + + if (self.blur_token) |tok| { + // Only release token when transitioning from blurred -> not blurred + if (!blur) { + manager.unset(self.surface); + tok.release(); + self.blur_token = null; + } + } else { + // Only acquire token when transitioning from not blurred -> blurred + if (blur) { + const tok = try manager.create(self.surface); + tok.commit(); + self.blur_token = tok; + } + } + } +}; diff --git a/src/apprt/gtk/protocol/x11.zig b/src/apprt/gtk/winproto/x11.zig similarity index 65% rename from src/apprt/gtk/protocol/x11.zig rename to src/apprt/gtk/winproto/x11.zig index 55762f316..d896fc051 100644 --- a/src/apprt/gtk/protocol/x11.zig +++ b/src/apprt/gtk/winproto/x11.zig @@ -1,38 +1,45 @@ -/// Utility functions for X11 handling. +//! X11 window protocol implementation for the Ghostty GTK apprt. const std = @import("std"); +const builtin = @import("builtin"); const build_options = @import("build_options"); +const Allocator = std.mem.Allocator; const c = @import("../c.zig").c; const input = @import("../../../input.zig"); const Config = @import("../../../config.zig").Config; -const protocol = @import("../protocol.zig"); const adwaita = @import("../adwaita.zig"); const log = std.log.scoped(.gtk_x11); pub const App = struct { - common: *protocol.App, display: *c.Display, + base_event_code: c_int, kde_blur_atom: c.Atom, - base_event_code: c_int = 0, + pub fn init( + alloc: Allocator, + gdk_display: *c.GdkDisplay, + app_id: [:0]const u8, + config: *const Config, + ) !?App { + _ = alloc; - /// Initialize an Xkb struct for the given GDK display. If the display isn't - /// backed by X then this will return null. - pub fn init(common: *protocol.App) !void { // If the display isn't X11, then we don't need to do anything. if (c.g_type_check_instance_is_a( - @ptrCast(@alignCast(common.gdk_display)), + @ptrCast(@alignCast(gdk_display)), c.gdk_x11_display_get_type(), - ) == 0) - return; + ) == 0) return null; - var self: App = .{ - .common = common, - .display = c.gdk_x11_display_get_xdisplay(common.gdk_display) orelse return, - .kde_blur_atom = c.gdk_x11_get_xatom_by_name_for_display(common.gdk_display, "_KDE_NET_WM_BLUR_BEHIND_REGION"), - }; + // Get our X11 display + const display: *c.Display = c.gdk_x11_display_get_xdisplay( + gdk_display, + ) orelse return error.NoX11Display; - log.debug("X11 platform init={}", .{self}); + const x11_program_name: [:0]const u8 = if (config.@"x11-instance-name") |pn| + pn + else if (builtin.mode == .Debug) + "ghostty-debug" + else + "ghostty"; // Set the X11 window class property (WM_CLASS) if are are on an X11 // display. @@ -50,22 +57,21 @@ pub const App = struct { // WM_CLASS(STRING) = "ghostty", "com.mitchellh.ghostty" // // Append "-debug" on both when using the debug build. - - c.g_set_prgname(common.derived_config.x11_program_name); - c.gdk_x11_display_set_program_class(common.gdk_display, common.derived_config.app_id); + c.g_set_prgname(x11_program_name); + c.gdk_x11_display_set_program_class(gdk_display, app_id); // XKB log.debug("Xkb.init: initializing Xkb", .{}); - log.debug("Xkb.init: running XkbQueryExtension", .{}); var opcode: c_int = 0; + var base_event_code: c_int = 0; var base_error_code: c_int = 0; var major = c.XkbMajorVersion; var minor = c.XkbMinorVersion; if (c.XkbQueryExtension( - self.display, + display, &opcode, - &self.base_event_code, + &base_event_code, &base_error_code, &major, &minor, @@ -76,7 +82,7 @@ pub const App = struct { log.debug("Xkb.init: running XkbSelectEventDetails", .{}); if (c.XkbSelectEventDetails( - self.display, + display, c.XkbUseCoreKbd, c.XkbStateNotify, c.XkbModifierStateMask, @@ -86,7 +92,19 @@ pub const App = struct { return error.XkbInitializationError; } - common.inner = .{ .x11 = self }; + return .{ + .display = display, + .base_event_code = base_event_code, + .kde_blur_atom = c.gdk_x11_get_xatom_by_name_for_display( + gdk_display, + "_KDE_NET_WM_BLUR_BEHIND_REGION", + ), + }; + } + + pub fn deinit(self: *App, alloc: Allocator) void { + _ = self; + _ = alloc; } /// Checks for an immediate pending XKB state update event, and returns the @@ -99,7 +117,14 @@ pub const App = struct { /// Returns null if there is no event. In this case, the caller should fall /// back to the standard GDK modifier state (this likely means the key /// event did not result in a modifier change). - pub fn modifierStateFromNotify(self: App) ?input.Mods { + pub fn eventMods( + self: App, + device: ?*c.GdkDevice, + gtk_mods: c.GdkModifierType, + ) ?input.Mods { + _ = device; + _ = gtk_mods; + // Shoutout to Mozilla for figuring out a clean way to do this, this is // paraphrased from Firefox/Gecko in widget/gtk/nsGtkKeyUtils.cpp. if (c.XEventsQueued(self.display, c.QueuedAfterReading) == 0) return null; @@ -127,57 +152,94 @@ pub const App = struct { } }; -pub const Surface = struct { - common: *protocol.Surface, +pub const Window = struct { app: *App, + config: DerivedConfig, window: c.Window, - + gtk_window: *c.GtkWindow, blur_region: Region, - pub fn init(common: *protocol.Surface) void { - const surface = c.gtk_native_get_surface(@ptrCast(common.gtk_window)) orelse return; + const DerivedConfig = struct { + blur: bool, + + pub fn init(config: *const Config) DerivedConfig { + return .{ + .blur = config.@"background-blur-radius".enabled(), + }; + } + }; + + pub fn init( + _: Allocator, + app: *App, + gtk_window: *c.GtkWindow, + config: *const Config, + ) !Window { + const surface = c.gtk_native_get_surface( + @ptrCast(gtk_window), + ) orelse return error.NotX11Surface; // Check if we're actually on X11 if (c.g_type_check_instance_is_a( @ptrCast(@alignCast(surface)), c.gdk_x11_surface_get_type(), - ) == 0) - return; + ) == 0) return error.NotX11Surface; - var blur_region: Region = .{}; + const blur_region: Region = blur: { + if ((comptime !adwaita.versionAtLeast(0, 0, 0)) or + !adwaita.enabled(config)) break :blur .{}; - if ((comptime adwaita.versionAtLeast(0, 0, 0)) and common.derived_config.adw_enabled) { // NOTE(pluiedev): CSDs are a f--king mistake. // Please, GNOME, stop this nonsense of making a window ~30% bigger // internally than how they really are just for your shadows and // rounded corners and all that fluff. Please. I beg of you. + var x: f64 = 0; + var y: f64 = 0; + c.gtk_native_get_surface_transform( + @ptrCast(gtk_window), + &x, + &y, + ); - var x: f64, var y: f64 = .{ 0, 0 }; - c.gtk_native_get_surface_transform(@ptrCast(common.gtk_window), &x, &y); - blur_region.x, blur_region.y = .{ @intFromFloat(x), @intFromFloat(y) }; - } + break :blur .{ + .x = @intFromFloat(x), + .y = @intFromFloat(y), + }; + }; - common.inner = .{ .x11 = .{ - .common = common, - .app = &common.app.inner.x11, + return .{ + .app = app, + .config = DerivedConfig.init(config), .window = c.gdk_x11_surface_get_xid(surface), + .gtk_window = gtk_window, .blur_region = blur_region, - } }; + }; } - pub fn onConfigUpdate(self: *Surface) !void { - // Whether background blur is enabled could've changed. Update. - try self.updateBlur(); + pub fn deinit(self: Window, alloc: Allocator) void { + _ = self; + _ = alloc; } - pub fn onResize(self: *Surface) !void { + pub fn updateConfigEvent( + self: *Window, + config: *const Config, + ) !void { + self.config = DerivedConfig.init(config); + } + + pub fn resizeEvent(self: *Window) !void { // The blur region must update with window resizes - self.blur_region.width = c.gtk_widget_get_width(@ptrCast(self.common.gtk_window)); - self.blur_region.height = c.gtk_widget_get_height(@ptrCast(self.common.gtk_window)); - try self.updateBlur(); + self.blur_region.width = c.gtk_widget_get_width(@ptrCast(self.gtk_window)); + self.blur_region.height = c.gtk_widget_get_height(@ptrCast(self.gtk_window)); + try self.syncBlur(); } - fn updateBlur(self: *Surface) !void { + pub fn syncAppearance(self: *Window) !void { + try self.syncBlur(); + } + + fn syncBlur(self: *Window) !void { // FIXME: This doesn't currently factor in rounded corners on Adwaita, // which means that the blur region will grow slightly outside of the // window borders. Unfortunately, actually calculating the rounded @@ -186,10 +248,14 @@ pub const Surface = struct { // and I think it's not really noticable enough to justify the effort. // (Wayland also has this visual artifact anyway...) - const blur = self.common.derived_config.blur; - log.debug("set blur={}, window xid={}, region={}", .{ blur, self.window, self.blur_region }); + const blur = self.config.blur; + log.debug("set blur={}, window xid={}, region={}", .{ + blur, + self.window, + self.blur_region, + }); - if (blur.enabled()) { + if (blur) { _ = c.XChangeProperty( self.app.display, self.window, @@ -210,7 +276,11 @@ pub const Surface = struct { 4, ); } else { - _ = c.XDeleteProperty(self.app.display, self.window, self.app.kde_blur_atom); + _ = c.XDeleteProperty( + self.app.display, + self.window, + self.app.kde_blur_atom, + ); } } };