From 86a01bbcf2a5bcc4a835b85f58776166eca477fb Mon Sep 17 00:00:00 2001 From: Squibid Date: Mon, 20 Oct 2025 22:53:17 -0400 Subject: [PATCH] load a runtime file where the prefix is determined at comptime --- build.zig | 12 +++++++++ runtime/share/mezzaluna/init.lua | 1 + src/lua.zig | 43 ++++++++++++++++++++++++++++++++ src/main.zig | 11 +++----- 4 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 runtime/share/mezzaluna/init.lua create mode 100644 src/lua.zig diff --git a/build.zig b/build.zig index 753997c..d12fdd0 100644 --- a/build.zig +++ b/build.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const builtin = @import("builtin"); const Scanner = @import("wayland").Scanner; @@ -6,6 +7,13 @@ pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); + // TODO: this will probably change based on the install paths, make this a var + // that can be passed at comptime? + const runtime_path_prefix = switch (builtin.mode) { + .Debug => "runtime/", + else => "/usr/share", + }; + // If instead your goal is to create an executable, consider if users might // be interested in also being able to embed the core functionality of your // program in their own executable in order to avoid the overhead involved in @@ -60,6 +68,10 @@ pub fn build(b: *std.Build) void { mez.linkSystemLibrary("xkbcommon"); mez.linkSystemLibrary("pixman-1"); + const options = b.addOptions(); + options.addOption([]const u8, "runtime_path_prefix", runtime_path_prefix); + mez.root_module.addOptions("config", options); + b.installArtifact(mez); const run_step = b.step("run", "Run the app"); diff --git a/runtime/share/mezzaluna/init.lua b/runtime/share/mezzaluna/init.lua new file mode 100644 index 0000000..33802c8 --- /dev/null +++ b/runtime/share/mezzaluna/init.lua @@ -0,0 +1 @@ +print("hello from our runtime file") diff --git a/src/lua.zig b/src/lua.zig new file mode 100644 index 0000000..357874c --- /dev/null +++ b/src/lua.zig @@ -0,0 +1,43 @@ +const Lua = @This(); + +const std = @import("std"); +const config = @import("config"); +const zlua = @import("zlua"); + +const gpa = std.heap.c_allocator; + +state: *zlua.Lua, + +fn loadRuntimeDir(self: *Lua) !void { + const tmppath = try std.fs.path.join(gpa, &[_][]const u8{ + config.runtime_path_prefix, + "share", + "mezzaluna", + "init.lua", + }); + var path_buffer = try gpa.alloc(u8, tmppath.len + 1); + std.mem.copyForwards(u8, path_buffer[0..tmppath.len], tmppath); + path_buffer[tmppath.len] = 0; + const path: [:0]u8 = path_buffer[0..tmppath.len :0]; + + try self.state.doFile(path); +} + +pub fn init(self: *Lua) !void { + self.state = try zlua.Lua.init(gpa); + errdefer self.state.deinit(); + self.state.openLibs(); + + { + self.state.newTable(); + defer _ = self.state.pushString("mez"); + } + + try loadRuntimeDir(self); + + std.log.debug("Loaded lua", .{}); +} + +pub fn deinit(self: *Lua) void { + self.state.deinit(); +} diff --git a/src/main.zig b/src/main.zig index 97c3abf..44fa4a7 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,25 +1,20 @@ const std = @import("std"); const wlr = @import("wlroots"); -const zlua = @import("zlua"); -const Lua = zlua.Lua; - const Server = @import("server.zig"); +const Lua = @import("lua.zig"); const gpa = std.heap.c_allocator; pub var server: Server = undefined; +pub var lua: Lua = undefined; pub fn main() !void { wlr.log.init(.err, null); - var lua = try Lua.init(gpa); - defer lua.deinit(); - lua.openLibs(); - lua.doString("print('Hello from Lua embedded in Zig!')") catch {}; - std.log.info("Starting mezzaluna", .{}); + try lua.init(); try server.init(); defer server.deinit();