Skip to content

Commit

Permalink
config: switch to a Surface.Options-based approach
Browse files Browse the repository at this point in the history
This introduces an ArenaAllocator that models the lifetime of the
Surface.Options structure from initial surface creation command to the
actual surface creation. This implementation is a little rough and
relies on an `anyopaque` pointer (because `extern struct`), but it
demonstrates a more apprt-centric approach.

Still only implemented for embedded apprt and macOS. The other apprts
will need their own plumbing to support this approach.

We can also chose to clean up newConfig() and call Config.shallowClone()
directly after this change.
  • Loading branch information
jparise committed Jul 25, 2024
1 parent ec2f8e6 commit 2fb8434
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 48 deletions.
8 changes: 1 addition & 7 deletions include/ghostty.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ typedef void* ghostty_surface_t;
typedef void* ghostty_inspector_t;

// Enums are up top so we can reference them later.
typedef enum {
GHOSTTY_SURFACE_KIND_SPLIT,
GHOSTTY_SURFACE_KIND_TAB,
GHOSTTY_SURFACE_KIND_WINDOW,
} ghostty_surface_kind_e;

typedef enum {
GHOSTTY_PLATFORM_INVALID,
GHOSTTY_PLATFORM_MACOS,
Expand Down Expand Up @@ -401,14 +395,14 @@ typedef union {
} ghostty_platform_u;

typedef struct {
ghostty_surface_kind_e kind;
ghostty_platform_e platform_tag;
ghostty_platform_u platform;
void* userdata;
double scale_factor;
float font_size;
const char* working_directory;
const char* command;
void* _arena;
} ghostty_surface_config_s;

typedef void (*ghostty_runtime_wakeup_cb)(void*);
Expand Down
4 changes: 0 additions & 4 deletions macos/Sources/Ghostty/SurfaceView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,6 @@ extension Ghostty {
/// The configuration for a surface. For any configuration not set, defaults will be chosen from
/// libghostty, usually from the Ghostty configuration.
struct SurfaceConfiguration {
var kind: ghostty_surface_kind_e? = nil;

/// Explicit font size to use in points
var fontSize: Float32? = nil

Expand All @@ -305,7 +303,6 @@ extension Ghostty {
init() {}

init(from config: ghostty_surface_config_s) {
self.kind = config.kind;
self.fontSize = config.font_size
self.workingDirectory = String.init(cString: config.working_directory, encoding: .utf8)
self.command = String.init(cString: config.command, encoding: .utf8)
Expand Down Expand Up @@ -337,7 +334,6 @@ extension Ghostty {
#error("unsupported target")
#endif

if let kind = kind { config.kind = kind }
if let fontSize = fontSize { config.font_size = fontSize }
if let workingDirectory = workingDirectory {
config.working_directory = (workingDirectory as NSString).utf8String
Expand Down
2 changes: 1 addition & 1 deletion src/Surface.zig
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ config: DerivedConfig,
child_exited: bool = false,

/// The kind of surface.
pub const Kind = enum(c_int) {
pub const Kind = enum {
split,
tab,
window,
Expand Down
57 changes: 48 additions & 9 deletions src/apprt/embedded.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const std = @import("std");
const builtin = @import("builtin");
const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const ArenaAllocator = std.heap.ArenaAllocator;
const objc = @import("objc");
const apprt = @import("../apprt.zig");
const font = @import("../font/main.zig");
Expand Down Expand Up @@ -308,9 +309,6 @@ pub const Surface = struct {

/// Surface initialization options.
pub const Options = extern struct {
/// The initial kind of surface.
kind: CoreSurface.Kind = .window,

/// The platform that this surface is being initialized for and
/// the associated platform-specific configuration.
platform_tag: c_int = 0,
Expand All @@ -332,6 +330,17 @@ pub const Surface = struct {
/// the "wait-after-command" option is also automatically set to true,
/// since this is used for scripting.
command: [*:0]const u8 = "",

_arena: ?*anyopaque = null,

pub fn deinit(self: *Options) void {
if (self._arena) |ptr| {
const arena: *ArenaAllocator = @ptrCast(@alignCast(ptr));
const alloc = arena.child_allocator;
arena.deinit();
alloc.destroy(arena);
}
}
};

/// This is the key event sent for ghostty_surface_key.
Expand Down Expand Up @@ -368,7 +377,7 @@ pub const Surface = struct {
errdefer app.core_app.deleteSurface(self);

// Shallow copy the config so that we can modify it.
var config = try apprt.surface.newConfig(app.core_app, app.config, opts.kind);
var config = try apprt.surface.newConfig(app.core_app, app.config);
defer config.deinit();

// If we have a working directory from the options then we set it.
Expand Down Expand Up @@ -476,7 +485,7 @@ pub const Surface = struct {
return;
};

const options = self.newSurfaceOptions(.split);
const options = try self.newSurfaceOptions(.split);
func(self.userdata, direction, options);
}

Expand Down Expand Up @@ -1027,7 +1036,7 @@ pub const Surface = struct {
return;
};

const options = self.newSurfaceOptions(.tab);
const options = try self.newSurfaceOptions(.tab);
func(self.userdata, options);
}

Expand All @@ -1037,7 +1046,7 @@ pub const Surface = struct {
return;
};

const options = self.newSurfaceOptions(.window);
const options = try self.newSurfaceOptions(.window);
func(self.userdata, options);
}

Expand Down Expand Up @@ -1068,15 +1077,44 @@ pub const Surface = struct {
func(self.userdata, width, height);
}

fn newSurfaceOptions(self: *const Surface, kind: CoreSurface.Kind) apprt.Surface.Options {
fn newSurfaceOptions(self: *const Surface, kind: CoreSurface.Kind) !apprt.Surface.Options {
const font_size: f32 = font_size: {
if (!self.app.config.@"window-inherit-font-size") break :font_size 0;
break :font_size self.core_surface.font_size.points;
};

const working_directory: [*:0]const u8, const arena: ?*ArenaAllocator = dir: {
const prev = self.app.core_app.focusedSurface();
if (prev) |p| {
const inherit_pwd: bool = switch (kind) {
.split => self.app.config.@"window-inherit-working-directory".split,
.tab => self.app.config.@"window-inherit-working-directory".tab,
.window => self.app.config.@"window-inherit-working-directory".window,
};
if (inherit_pwd) {
const app_alloc = self.app.core_app.alloc;

var arena = try app_alloc.create(ArenaAllocator);
errdefer app_alloc.destroy(arena);

arena.* = ArenaAllocator.init(app_alloc);
errdefer arena.deinit();

const arena_alloc = arena.allocator();

if (try p.pwd(app_alloc)) |pwd| {
defer app_alloc.free(pwd);
break :dir .{ try arena_alloc.dupeZ(u8, pwd), arena };
}
}
}
break :dir .{ "", null };
};

return .{
.kind = kind,
.font_size = font_size,
.working_directory = working_directory,
._arena = arena,
};
}

Expand Down Expand Up @@ -1503,6 +1541,7 @@ pub const CAPI = struct {
app: *App,
opts: *const apprt.Surface.Options,
) !*Surface {
defer @constCast(opts).deinit();
return try app.newSurface(opts.*);
}

Expand Down
3 changes: 1 addition & 2 deletions src/apprt/glfw.zig
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,7 @@ pub const Surface = struct {
errdefer app.app.deleteSurface(self);

// Get our new surface config
const kind: CoreSurface.Kind = .window; // TODO
var config = try apprt.surface.newConfig(app.app, &app.config, kind);
var config = try apprt.surface.newConfig(app.app, &app.config);
defer config.deinit();

// Initialize our surface now that we have the stable pointer.
Expand Down
3 changes: 1 addition & 2 deletions src/apprt/gtk/Surface.zig
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,7 @@ fn realize(self: *Surface) !void {
errdefer self.app.core_app.deleteSurface(self);

// Get our new surface config
const kind: CoreSurface.Kind = .window; // TODO
var config = try apprt.surface.newConfig(self.app.core_app, &self.app.config, kind);
var config = try apprt.surface.newConfig(self.app.core_app, &self.app.config);
defer config.deinit();
if (!self.parent_surface) {
// A hack, see the "parent_surface" field for more information.
Expand Down
25 changes: 2 additions & 23 deletions src/apprt/surface.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const std = @import("std");
const apprt = @import("../apprt.zig");
const App = @import("../App.zig");
const Surface = @import("../Surface.zig");
Expand Down Expand Up @@ -86,27 +85,7 @@ pub const Mailbox = struct {
/// Returns a new config for a surface for the given app that should be
/// used for any new surfaces. The resulting config should be deinitialized
/// after the surface is initialized.
pub fn newConfig(app: *const App, config: *const Config, kind: Surface.Kind) !Config {
pub fn newConfig(app: *const App, config: *const Config) !Config {
// Create a shallow clone
var copy = config.shallowClone(app.alloc);

// Our allocator is our config's arena
const alloc = copy._arena.?.allocator();

// Get our previously focused surface for some inherited values.
const prev = app.focusedSurface();
if (prev) |p| {
const inherit_pwd: bool = switch (kind) {
.split => config.@"window-inherit-working-directory".split,
.tab => config.@"window-inherit-working-directory".tab,
.window => config.@"window-inherit-working-directory".window,
};
if (inherit_pwd) {
if (try p.pwd(alloc)) |pwd| {
copy.@"working-directory" = pwd;
}
}
}

return copy;
return config.shallowClone(app.alloc);
}

0 comments on commit 2fb8434

Please sign in to comment.