apprt/embedded: reload config support

This commit is contained in:
Mitchell Hashimoto
2023-03-13 22:08:35 -07:00
parent f5c1dfa374
commit 8d3f40fa41
2 changed files with 34 additions and 6 deletions

View File

@ -54,6 +54,7 @@ extension Ghostty {
var runtime_cfg = ghostty_runtime_config_s( var runtime_cfg = ghostty_runtime_config_s(
userdata: Unmanaged.passUnretained(self).toOpaque(), userdata: Unmanaged.passUnretained(self).toOpaque(),
wakeup_cb: { userdata in AppState.wakeup(userdata) }, 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) }, set_title_cb: { userdata, title in AppState.setTitle(userdata, title: title) },
read_clipboard_cb: { userdata in AppState.readClipboard(userdata) }, read_clipboard_cb: { userdata in AppState.readClipboard(userdata) },
write_clipboard_cb: { userdata, str in AppState.writeClipboard(userdata, string: str) }, write_clipboard_cb: { userdata, str in AppState.writeClipboard(userdata, string: str) },
@ -140,6 +141,13 @@ extension Ghostty {
pb.setString(valueStr, forType: .string) pb.setString(valueStr, forType: .string)
} }
static func reloadConfig(_ userdata: UnsafeMutableRawPointer?) -> ghostty_config_t? {
// TODO: implement config reloading in the mac app
let state = Unmanaged<AppState>.fromOpaque(userdata!).takeUnretainedValue()
_ = state
return nil
}
static func wakeup(_ userdata: UnsafeMutableRawPointer?) { static func wakeup(_ userdata: UnsafeMutableRawPointer?) {
let state = Unmanaged<AppState>.fromOpaque(userdata!).takeUnretainedValue() let state = Unmanaged<AppState>.fromOpaque(userdata!).takeUnretainedValue()

View File

@ -13,6 +13,7 @@ const apprt = @import("../apprt.zig");
const input = @import("../input.zig"); const input = @import("../input.zig");
const CoreApp = @import("../App.zig"); const CoreApp = @import("../App.zig");
const CoreSurface = @import("../Surface.zig"); const CoreSurface = @import("../Surface.zig");
const Config = @import("../config.zig").Config;
const log = std.log.scoped(.embedded_window); const log = std.log.scoped(.embedded_window);
@ -35,6 +36,11 @@ pub const App = struct {
/// a full tick of the app loop. /// a full tick of the app loop.
wakeup: *const fn (AppUD) callconv(.C) void, 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. /// Called to set the title of the window.
set_title: *const fn (SurfaceUD, [*]const u8) callconv(.C) void, set_title: *const fn (SurfaceUD, [*]const u8) callconv(.C) void,
@ -57,16 +63,31 @@ pub const App = struct {
}; };
core_app: *CoreApp, core_app: *CoreApp,
config: *const Config,
opts: Options, opts: Options,
pub fn init(core_app: *CoreApp, opts: Options) !App { pub fn init(core_app: *CoreApp, config: *const Config, opts: Options) !App {
return .{ .core_app = core_app, .opts = opts }; return .{
.core_app = core_app,
.config = config,
.opts = opts,
};
} }
pub fn terminate(self: App) void { pub fn terminate(self: App) void {
_ = self; _ = 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 { pub fn wakeup(self: App) void {
self.opts.wakeup(self.opts.userdata); self.opts.wakeup(self.opts.userdata);
} }
@ -143,7 +164,7 @@ pub const Surface = struct {
// ready to use. // ready to use.
try self.core_surface.init( try self.core_surface.init(
app.core_app.alloc, app.core_app.alloc,
app.core_app.config, app.config,
.{ .rt_app = app, .mailbox = &app.core_app.mailbox }, .{ .rt_app = app, .mailbox = &app.core_app.mailbox },
self, self,
); );
@ -338,7 +359,6 @@ pub const Surface = struct {
// C API // C API
pub const CAPI = struct { pub const CAPI = struct {
const global = &@import("../main.zig").state; const global = &@import("../main.zig").state;
const Config = @import("../config.zig").Config;
/// Create a new app. /// Create a new app.
export fn ghostty_app_new( export fn ghostty_app_new(
@ -355,13 +375,13 @@ pub const CAPI = struct {
opts: *const apprt.runtime.App.Options, opts: *const apprt.runtime.App.Options,
config: *const Config, config: *const Config,
) !*App { ) !*App {
var core_app = try CoreApp.create(global.alloc, config); var core_app = try CoreApp.create(global.alloc);
errdefer core_app.destroy(); errdefer core_app.destroy();
// Create our runtime app // Create our runtime app
var app = try global.alloc.create(App); var app = try global.alloc.create(App);
errdefer global.alloc.destroy(app); errdefer global.alloc.destroy(app);
app.* = try App.init(core_app, opts.*); app.* = try App.init(core_app, config, opts.*);
errdefer app.terminate(); errdefer app.terminate();
return app; return app;