From 2f0905b60c4609fb1b7a18f22699f59c9bfdd140 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 11 Sep 2023 15:20:31 -0700 Subject: [PATCH] macos: reload config MUST go through Zig core --- include/ghostty.h | 1 + macos/Sources/Ghostty/AppState.swift | 36 +++++++++++++++------------- src/apprt/embedded.zig | 8 +++++++ 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/include/ghostty.h b/include/ghostty.h index ed66824ed..56b1fe5dd 100644 --- a/include/ghostty.h +++ b/include/ghostty.h @@ -315,6 +315,7 @@ void ghostty_app_free(ghostty_app_t); bool ghostty_app_tick(ghostty_app_t); void *ghostty_app_userdata(ghostty_app_t); void ghostty_app_keyboard_changed(ghostty_app_t); +void ghostty_app_reload_config(ghostty_app_t); ghostty_surface_config_s ghostty_surface_config_new(); diff --git a/macos/Sources/Ghostty/AppState.swift b/macos/Sources/Ghostty/AppState.swift index 20a815ebc..c714a4f40 100644 --- a/macos/Sources/Ghostty/AppState.swift +++ b/macos/Sources/Ghostty/AppState.swift @@ -52,7 +52,7 @@ extension Ghostty { } // Initialize the global configuration. - guard let cfg = Self.reloadConfig() else { + guard let cfg = Self.loadConfig() else { readiness = .error return } @@ -109,7 +109,7 @@ extension Ghostty { } /// Initializes a new configuration and loads all the values. - static func reloadConfig() -> ghostty_config_t? { + static func loadConfig() -> ghostty_config_t? { // Initialize the global configuration. guard let cfg = ghostty_config_new() else { AppDelegate.logger.critical("ghostty_config_new failed") @@ -172,19 +172,8 @@ extension Ghostty { } func reloadConfig() { - guard let newConfig = Self.reloadConfig() else { - AppDelegate.logger.warning("failed to reload configuration") - return - } - - // Assign the new config. This will automatically free the old config. - // It is safe to free the old config from within this function call. - config = newConfig - - // If we have a delegate, notify. - if let delegate = delegate { - delegate.configDidReload(self) - } + guard let app = self.app else { return } + ghostty_app_reload_config(app) } /// Request that the given surface is closed. This will trigger the full normal surface close event @@ -302,9 +291,22 @@ extension Ghostty { } static func reloadConfig(_ userdata: UnsafeMutableRawPointer?) -> ghostty_config_t? { + guard let newConfig = Self.loadConfig() else { + AppDelegate.logger.warning("failed to reload configuration") + return nil + } + + // Assign the new config. This will automatically free the old config. + // It is safe to free the old config from within this function call. let state = Unmanaged.fromOpaque(userdata!).takeUnretainedValue() - state.reloadConfig() - return state.config + state.config = newConfig + + // If we have a delegate, notify. + if let delegate = state.delegate { + delegate.configDidReload(state) + } + + return newConfig } static func wakeup(_ userdata: UnsafeMutableRawPointer?) { diff --git a/src/apprt/embedded.zig b/src/apprt/embedded.zig index 650bee842..e3475cc82 100644 --- a/src/apprt/embedded.zig +++ b/src/apprt/embedded.zig @@ -779,6 +779,14 @@ pub const CAPI = struct { }; } + /// Reload the configuration. + export fn ghostty_app_reload_config(v: *App) void { + _ = v.reloadConfig() catch |err| { + log.err("error reloading config err={}", .{err}); + return; + }; + } + /// Returns initial surface options. export fn ghostty_surface_config_new() apprt.Surface.Options { return .{};