From 98ad1d955cf8d66cf5548f581a6502cf10f2f852 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 3 Mar 2026 19:53:15 -0800 Subject: [PATCH] use proper type for optional path --- include/ghostty.h | 6 ++++++ macos/Sources/App/macOS/AppDelegate.swift | 4 ++-- macos/Sources/Ghostty/Ghostty.Config.swift | 9 ++++----- macos/Sources/Ghostty/Ghostty.ConfigTypes.swift | 6 ++++++ src/config/path.zig | 14 ++++++++++---- 5 files changed, 28 insertions(+), 11 deletions(-) diff --git a/include/ghostty.h b/include/ghostty.h index 19b6e0fa4..19a200f10 100644 --- a/include/ghostty.h +++ b/include/ghostty.h @@ -463,6 +463,12 @@ typedef struct { // Config types +// config.Path +typedef struct { + const char* path; + bool optional; +} ghostty_config_path_s; + // config.Color typedef struct { uint8_t r; diff --git a/macos/Sources/App/macOS/AppDelegate.swift b/macos/Sources/App/macOS/AppDelegate.swift index ad290c36c..f9e2dc93f 100644 --- a/macos/Sources/App/macOS/AppDelegate.swift +++ b/macos/Sources/App/macOS/AppDelegate.swift @@ -778,8 +778,8 @@ class AppDelegate: NSObject, } if ghostty.config.bellFeatures.contains(.audio) { - if let path = ghostty.config.bellAudioPath, - let sound = NSSound(contentsOfFile: path, byReference: false) { + if let configPath = ghostty.config.bellAudioPath, + let sound = NSSound(contentsOfFile: configPath.path, byReference: false) { sound.volume = ghostty.config.bellAudioVolume sound.play() } diff --git a/macos/Sources/Ghostty/Ghostty.Config.swift b/macos/Sources/Ghostty/Ghostty.Config.swift index 00c1f5307..87ae0511f 100644 --- a/macos/Sources/Ghostty/Ghostty.Config.swift +++ b/macos/Sources/Ghostty/Ghostty.Config.swift @@ -134,14 +134,13 @@ extension Ghostty { return .init(rawValue: v) } - var bellAudioPath: String? { + var bellAudioPath: ConfigPath? { guard let config = self.config else { return nil } - var v: UnsafePointer? + var v = ghostty_config_path_s() let key = "bell-audio-path" guard ghostty_config_get(config, &v, key, UInt(key.lengthOfBytes(using: .utf8))) else { return nil } - guard let ptr = v else { return nil } - let path = String(cString: ptr) - return path.isEmpty ? nil : path + let path = String(cString: v.path) + return path.isEmpty ? nil : ConfigPath(path: path, optional: v.optional) } var bellAudioVolume: Float { diff --git a/macos/Sources/Ghostty/Ghostty.ConfigTypes.swift b/macos/Sources/Ghostty/Ghostty.ConfigTypes.swift index 8c559fad2..90470f38a 100644 --- a/macos/Sources/Ghostty/Ghostty.ConfigTypes.swift +++ b/macos/Sources/Ghostty/Ghostty.ConfigTypes.swift @@ -2,6 +2,12 @@ // can get typed information without depending on all the dependencies of GhosttyKit. extension Ghostty { + /// A configuration path value that may be optional or required. + struct ConfigPath: Sendable { + let path: String + let optional: Bool + } + /// macos-icon enum MacOSIcon: String, Sendable { case official diff --git a/src/config/path.zig b/src/config/path.zig index 7faf5f312..793cf1845 100644 --- a/src/config/path.zig +++ b/src/config/path.zig @@ -32,11 +32,17 @@ pub const Path = union(enum) { return std.meta.eql(self, other); } - /// Returns the path as a C-compatible null-terminated string pointer. - pub fn cval(self: Path) [*:0]const u8 { + /// ghostty_config_path_s + pub const C = extern struct { + path: [*:0]const u8, + optional: bool, + }; + + /// Returns the path as a C-compatible struct. + pub fn cval(self: Path) C { return switch (self) { - .optional => |path| path.ptr, - .required => |path| path.ptr, + .optional => |path| .{ .path = path.ptr, .optional = true }, + .required => |path| .{ .path = path.ptr, .optional = false }, }; }