core: App looks up resources dir on startup

This commit is contained in:
Mitchell Hashimoto
2023-08-08 09:21:52 -07:00
parent 4e9dcf6b62
commit bd7cc4b71d
2 changed files with 20 additions and 1 deletions

View File

@ -16,6 +16,7 @@ const Config = @import("config.zig").Config;
const BlockingQueue = @import("./blocking_queue.zig").BlockingQueue; const BlockingQueue = @import("./blocking_queue.zig").BlockingQueue;
const renderer = @import("renderer.zig"); const renderer = @import("renderer.zig");
const font = @import("font/main.zig"); const font = @import("font/main.zig");
const internal_os = @import("os/main.zig");
const macos = @import("macos"); const macos = @import("macos");
const objc = @import("objc"); const objc = @import("objc");
@ -40,6 +41,10 @@ mailbox: Mailbox.Queue,
/// Set to true once we're quitting. This never goes false again. /// Set to true once we're quitting. This never goes false again.
quit: bool, quit: bool,
/// The app resources directory, equivalent to zig-out/share when we build
/// from source. This is null if we can't detect it.
resources_dir: ?[]const u8 = null,
/// Initialize the main app instance. This creates the main window, sets /// Initialize the main app instance. This creates the main window, sets
/// up the renderer state, compiles the shaders, etc. This is the primary /// up the renderer state, compiles the shaders, etc. This is the primary
/// "startup" logic. /// "startup" logic.
@ -48,11 +53,21 @@ pub fn create(
) !*App { ) !*App {
var app = try alloc.create(App); var app = try alloc.create(App);
errdefer alloc.destroy(app); errdefer alloc.destroy(app);
// Find our resources directory once for the app so every launch
// hereafter can use this cached value.
var resources_buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
const resources_dir = if (try internal_os.resourcesDir(&resources_buf)) |dir|
try alloc.dupe(u8, dir)
else
null;
app.* = .{ app.* = .{
.alloc = alloc, .alloc = alloc,
.surfaces = .{}, .surfaces = .{},
.mailbox = .{}, .mailbox = .{},
.quit = false, .quit = false,
.resources_dir = resources_dir,
}; };
errdefer app.surfaces.deinit(alloc); errdefer app.surfaces.deinit(alloc);
@ -64,6 +79,7 @@ pub fn destroy(self: *App) void {
for (self.surfaces.items) |surface| surface.deinit(); for (self.surfaces.items) |surface| surface.deinit();
self.surfaces.deinit(self.alloc); self.surfaces.deinit(self.alloc);
if (self.resources_dir) |dir| self.alloc.free(dir);
self.alloc.destroy(self); self.alloc.destroy(self);
} }

View File

@ -8,6 +8,9 @@ const Allocator = std.mem.Allocator;
/// the output is not ALWAYS written to the buffer and may refer to /// the output is not ALWAYS written to the buffer and may refer to
/// static memory. /// static memory.
/// ///
/// This is highly Ghostty-specific and can likely be generalized at
/// some point but we can cross that bridge if we ever need to.
///
/// This returns error.OutOfMemory is buffer is not big enough. /// This returns error.OutOfMemory is buffer is not big enough.
pub fn resourcesDir(buf: []u8) !?[]const u8 { pub fn resourcesDir(buf: []u8) !?[]const u8 {
// If we have an environment variable set, we always use that. // If we have an environment variable set, we always use that.
@ -60,7 +63,7 @@ pub fn maybeDir(
sub: []const u8, sub: []const u8,
suffix: []const u8, suffix: []const u8,
) !?[]const u8 { ) !?[]const u8 {
const path = try std.fmt.bufPrint(&buf, "{s}/{s}/{s}", .{ base, sub, suffix }); const path = try std.fmt.bufPrint(buf, "{s}/{s}/{s}", .{ base, sub, suffix });
if (std.fs.accessAbsolute(path, .{})) { if (std.fs.accessAbsolute(path, .{})) {
const len = path.len - suffix.len - 1; const len = path.len - suffix.len - 1;