Skip to content

Commit

Permalink
apprt/gtk: winproto
Browse files Browse the repository at this point in the history
Rename "protocol" to "winproto".
  • Loading branch information
mitchellh committed Jan 10, 2025
1 parent 405a897 commit ed81b62
Show file tree
Hide file tree
Showing 10 changed files with 589 additions and 351 deletions.
34 changes: 25 additions & 9 deletions src/apprt/gtk/App.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,

Expand All @@ -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.
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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});
};
},

Expand Down
15 changes: 11 additions & 4 deletions src/apprt/gtk/Surface.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -1702,7 +1709,7 @@ pub fn keyEvent(
event,
physical_key,
gtk_mods,
&self.app.protocol,
&self.app.winproto,
);

// Get our consumed modifiers
Expand Down
38 changes: 31 additions & 7 deletions src/apprt/gtk/Window.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
///
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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});
};
Expand Down
6 changes: 3 additions & 3 deletions src/apprt/gtk/key.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
149 changes: 0 additions & 149 deletions src/apprt/gtk/protocol.zig

This file was deleted.

Loading

0 comments on commit ed81b62

Please sign in to comment.