c api: add ghostty_info to get metadata about the build

This commit is contained in:
Mitchell Hashimoto
2023-09-15 12:32:41 -07:00
parent 0ac1010d5a
commit de0f71c6a1
2 changed files with 42 additions and 0 deletions

View File

@ -284,9 +284,22 @@ typedef struct {
bool physical; bool physical;
} ghostty_input_trigger_s; } ghostty_input_trigger_s;
typedef enum {
GHOSTTY_BUILD_MODE_DEBUG,
GHOSTTY_BUILD_MODE_RELEASE_SAFE,
GHOSTTY_BUILD_MODE_RELEASE_FAST,
GHOSTTY_BUILD_MODE_RELEASE_SMALL,
} ghostty_build_mode_e;
// Fully defined types. This MUST be kept in sync with equivalent Zig // Fully defined types. This MUST be kept in sync with equivalent Zig
// structs. To find the Zig struct, grep for this type name. The documentation // structs. To find the Zig struct, grep for this type name. The documentation
// for all of these types is available in the Zig source. // for all of these types is available in the Zig source.
typedef struct {
ghostty_build_mode_e build_mode;
const char *version;
uintptr_t version_len;
} ghostty_info_s;
typedef struct { typedef struct {
const char *message; const char *message;
} ghostty_error_s; } ghostty_error_s;
@ -338,6 +351,7 @@ typedef struct {
// Published API // Published API
int ghostty_init(void); int ghostty_init(void);
ghostty_info_s ghostty_info(void);
ghostty_config_t ghostty_config_new(); ghostty_config_t ghostty_config_new();
void ghostty_config_free(ghostty_config_t); void ghostty_config_free(ghostty_config_t);

View File

@ -9,6 +9,7 @@
const std = @import("std"); const std = @import("std");
const assert = std.debug.assert; const assert = std.debug.assert;
const builtin = @import("builtin"); const builtin = @import("builtin");
const build_config = @import("build_config.zig");
const main = @import("main.zig"); const main = @import("main.zig");
const apprt = @import("apprt.zig"); const apprt = @import("apprt.zig");
@ -23,6 +24,20 @@ pub const std_options = main.std_options;
pub usingnamespace @import("config.zig").CAPI; pub usingnamespace @import("config.zig").CAPI;
pub usingnamespace apprt.runtime.CAPI; pub usingnamespace apprt.runtime.CAPI;
/// ghostty_info_s
const Info = extern struct {
mode: BuildMode,
version: [*]const u8,
version_len: usize,
const BuildMode = enum(c_int) {
debug,
release_safe,
release_fast,
release_small,
};
};
/// Initialize ghostty global state. It is possible to have more than /// Initialize ghostty global state. It is possible to have more than
/// one global state but it has zero practical benefit. /// one global state but it has zero practical benefit.
export fn ghostty_init() c_int { export fn ghostty_init() c_int {
@ -30,3 +45,16 @@ export fn ghostty_init() c_int {
main.state.init(); main.state.init();
return 0; return 0;
} }
export fn ghostty_info() Info {
return .{
.mode = switch (builtin.mode) {
.Debug => .debug,
.ReleaseSafe => .release_safe,
.ReleaseFast => .release_fast,
.ReleaseSmall => .release_small,
},
.version = build_config.version_string.ptr,
.version_len = build_config.version_string.len,
};
}