From 8d3f40fa41d996a9a2af71da1fe5de82e6a84260 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 13 Mar 2023 22:08:35 -0700 Subject: [PATCH] apprt/embedded: reload config support --- macos/Sources/Ghostty/AppState.swift | 8 +++++++ src/apprt/embedded.zig | 32 ++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/macos/Sources/Ghostty/AppState.swift b/macos/Sources/Ghostty/AppState.swift index 892b41af5..2307aa082 100644 --- a/macos/Sources/Ghostty/AppState.swift +++ b/macos/Sources/Ghostty/AppState.swift @@ -54,6 +54,7 @@ extension Ghostty { var runtime_cfg = ghostty_runtime_config_s( userdata: Unmanaged.passUnretained(self).toOpaque(), wakeup_cb: { userdata in AppState.wakeup(userdata) }, + reload_config_cb: { userdata in AppState.reloadConfig(userdata) }, set_title_cb: { userdata, title in AppState.setTitle(userdata, title: title) }, read_clipboard_cb: { userdata in AppState.readClipboard(userdata) }, write_clipboard_cb: { userdata, str in AppState.writeClipboard(userdata, string: str) }, @@ -140,6 +141,13 @@ extension Ghostty { pb.setString(valueStr, forType: .string) } + static func reloadConfig(_ userdata: UnsafeMutableRawPointer?) -> ghostty_config_t? { + // TODO: implement config reloading in the mac app + let state = Unmanaged.fromOpaque(userdata!).takeUnretainedValue() + _ = state + return nil + } + static func wakeup(_ userdata: UnsafeMutableRawPointer?) { let state = Unmanaged.fromOpaque(userdata!).takeUnretainedValue() diff --git a/src/apprt/embedded.zig b/src/apprt/embedded.zig index b782ba8cf..08fd04a5d 100644 --- a/src/apprt/embedded.zig +++ b/src/apprt/embedded.zig @@ -13,6 +13,7 @@ const apprt = @import("../apprt.zig"); const input = @import("../input.zig"); const CoreApp = @import("../App.zig"); const CoreSurface = @import("../Surface.zig"); +const Config = @import("../config.zig").Config; const log = std.log.scoped(.embedded_window); @@ -35,6 +36,11 @@ pub const App = struct { /// a full tick of the app loop. wakeup: *const fn (AppUD) callconv(.C) void, + /// Reload the configuration and return the new configuration. + /// The old configuration can be freed immediately when this is + /// called. + reload_config: *const fn (AppUD) callconv(.C) ?*const Config, + /// Called to set the title of the window. set_title: *const fn (SurfaceUD, [*]const u8) callconv(.C) void, @@ -57,16 +63,31 @@ pub const App = struct { }; core_app: *CoreApp, + config: *const Config, opts: Options, - pub fn init(core_app: *CoreApp, opts: Options) !App { - return .{ .core_app = core_app, .opts = opts }; + pub fn init(core_app: *CoreApp, config: *const Config, opts: Options) !App { + return .{ + .core_app = core_app, + .config = config, + .opts = opts, + }; } pub fn terminate(self: App) void { _ = self; } + pub fn reloadConfig(self: *App) !?*const Config { + // Reload + if (self.opts.reload_config(self.opts.userdata)) |new| { + self.config = new; + return self.config; + } + + return null; + } + pub fn wakeup(self: App) void { self.opts.wakeup(self.opts.userdata); } @@ -143,7 +164,7 @@ pub const Surface = struct { // ready to use. try self.core_surface.init( app.core_app.alloc, - app.core_app.config, + app.config, .{ .rt_app = app, .mailbox = &app.core_app.mailbox }, self, ); @@ -338,7 +359,6 @@ pub const Surface = struct { // C API pub const CAPI = struct { const global = &@import("../main.zig").state; - const Config = @import("../config.zig").Config; /// Create a new app. export fn ghostty_app_new( @@ -355,13 +375,13 @@ pub const CAPI = struct { opts: *const apprt.runtime.App.Options, config: *const Config, ) !*App { - var core_app = try CoreApp.create(global.alloc, config); + var core_app = try CoreApp.create(global.alloc); errdefer core_app.destroy(); // Create our runtime app var app = try global.alloc.create(App); errdefer global.alloc.destroy(app); - app.* = try App.init(core_app, opts.*); + app.* = try App.init(core_app, config, opts.*); errdefer app.terminate(); return app;