tony / oxwm

Merge branch 'tonybanters:master' into home-manager-module Browse files
Pascal committed 2026-02-18 parents d34ab88 e254661 commit 9c4b2126e8d7cfd5fb9a79c52fc94dcb243e32e9
Showing 3 changed files with 77 additions and 17 deletions. UnifiedSplit
+3 −0 src/config/config.zig
@@ -109,6 +109,7 @@ pub const Color_Scheme = struct {
109 109
110 110 pub const Config = struct {
111 111 allocator: std.mem.Allocator,
112 + string_arena: std.heap.ArenaAllocator,
112 113
113 114 terminal: []const u8 = "st",
114 115 font: []const u8 = "monospace:size=10",
@@ -149,10 +150,12 @@ pub const Config = struct {
149 150 pub fn init(allocator: std.mem.Allocator) Config {
150 151 return Config{
151 152 .allocator = allocator,
153 + .string_arena = std.heap.ArenaAllocator.init(allocator),
152 154 };
153 155 }
154 156
155 157 pub fn deinit(self: *Config) void {
158 + self.string_arena.deinit();
156 159 self.keybinds.deinit(self.allocator);
157 160 self.rules.deinit(self.allocator);
158 161 self.blocks.deinit(self.allocator);
+21 −13 src/config/lua.zig
@@ -743,7 +743,7 @@ fn lua_rule_add(state: ?*c.lua_State) callconv(.c) c_int {
743 743 fn lua_bar_set_font(state: ?*c.lua_State) callconv(.c) c_int {
744 744 const cfg = config orelse return 0;
745 745 const s = state orelse return 0;
746 - if (get_string_arg(s, 1)) |font| {
746 + if (dupe_lua_string(s, 1)) |font| {
747 747 cfg.font = font;
748 748 }
749 749 return 0;
@@ -784,7 +784,7 @@ fn parse_block_config(state: *c.lua_State, idx: c_int) ?Block {
784 784 c.lua_settop(state, -2);
785 785
786 786 _ = c.lua_getfield(state, idx, "format");
787 - const format = get_lua_string(state, -1) orelse "";
787 + const format = dupe_lua_string(state, -1) orelse "";
788 788 c.lua_settop(state, -2);
789 789
790 790 _ = c.lua_getfield(state, idx, "interval");
@@ -812,17 +812,17 @@ fn parse_block_config(state: *c.lua_State, idx: c_int) ?Block {
812 812 } else if (std.mem.eql(u8, block_type_str, "DateTime")) {
813 813 block.block_type = .datetime;
814 814 _ = c.lua_getfield(state, idx, "__arg");
815 - block.datetime_format = get_lua_string(state, -1);
815 + block.datetime_format = dupe_lua_string(state, -1);
816 816 c.lua_settop(state, -2);
817 817 } else if (std.mem.eql(u8, block_type_str, "Shell")) {
818 818 block.block_type = .shell;
819 819 _ = c.lua_getfield(state, idx, "__arg");
820 - block.command = get_lua_string(state, -1);
820 + block.command = dupe_lua_string(state, -1);
821 821 c.lua_settop(state, -2);
822 822 } else if (std.mem.eql(u8, block_type_str, "Static")) {
823 823 block.block_type = .static;
824 824 _ = c.lua_getfield(state, idx, "__arg");
825 - if (get_lua_string(state, -1)) |text| {
825 + if (dupe_lua_string(state, -1)) |text| {
826 826 block.format = text;
827 827 }
828 828 c.lua_settop(state, -2);
@@ -831,19 +831,19 @@ fn parse_block_config(state: *c.lua_State, idx: c_int) ?Block {
831 831 _ = c.lua_getfield(state, idx, "__arg");
832 832 if (c.lua_type(state, -1) == c.LUA_TTABLE) {
833 833 _ = c.lua_getfield(state, -1, "charging");
834 - block.format_charging = get_lua_string(state, -1);
834 + block.format_charging = dupe_lua_string(state, -1);
835 835 c.lua_settop(state, -2);
836 836
837 837 _ = c.lua_getfield(state, -1, "discharging");
838 - block.format_discharging = get_lua_string(state, -1);
838 + block.format_discharging = dupe_lua_string(state, -1);
839 839 c.lua_settop(state, -2);
840 840
841 841 _ = c.lua_getfield(state, -1, "full");
842 - block.format_full = get_lua_string(state, -1);
842 + block.format_full = dupe_lua_string(state, -1);
843 843 c.lua_settop(state, -2);
844 844
845 845 _ = c.lua_getfield(state, -1, "battery_name");
846 - block.battery_name = get_lua_string(state, -1);
846 + block.battery_name = dupe_lua_string(state, -1);
847 847 c.lua_settop(state, -2);
848 848 }
849 849 c.lua_settop(state, -2);
@@ -988,7 +988,7 @@ fn create_block_table(state: *c.lua_State, block_type: [*:0]const u8, arg: ?[]co
988 988 fn lua_set_terminal(state: ?*c.lua_State) callconv(.c) c_int {
989 989 const cfg = config orelse return 0;
990 990 const s = state orelse return 0;
991 - if (get_string_arg(s, 1)) |term| {
991 + if (dupe_lua_string(s, 1)) |term| {
992 992 cfg.terminal = term;
993 993 }
994 994 return 0;
@@ -1025,7 +1025,7 @@ fn lua_set_tags(state: ?*c.lua_State) callconv(.c) c_int {
1025 1025 var i: usize = 0;
1026 1026 while (i < len and i < 9) : (i += 1) {
1027 1027 _ = c.lua_rawgeti(s, 1, @intCast(i + 1));
1028 - if (get_lua_string(s, -1)) |tag_str| {
1028 + if (dupe_lua_string(s, -1)) |tag_str| {
1029 1029 cfg.tags[i] = tag_str;
1030 1030 }
1031 1031 c.lua_settop(s, -2);
@@ -1037,7 +1037,7 @@ fn lua_set_tags(state: ?*c.lua_State) callconv(.c) c_int {
1037 1037 fn lua_autostart(state: ?*c.lua_State) callconv(.c) c_int {
1038 1038 const cfg = config orelse return 0;
1039 1039 const s = state orelse return 0;
1040 - if (get_string_arg(s, 1)) |cmd| {
1040 + if (dupe_lua_string(s, 1)) |cmd| {
1041 1041 cfg.add_autostart(cmd) catch return 0;
1042 1042 }
1043 1043 return 0;
@@ -1054,7 +1054,7 @@ fn lua_set_layout_symbol(state: ?*c.lua_State) callconv(.c) c_int {
1054 1054 const cfg = config orelse return 0;
1055 1055 const s = state orelse return 0;
1056 1056 const name = get_string_arg(s, 1) orelse return 0;
1057 - const symbol = get_string_arg(s, 2) orelse return 0;
1057 + const symbol = dupe_lua_string(s, 2) orelse return 0;
1058 1058
1059 1059 const layout_map = .{
1060 1060 .{ "tiling", &cfg.layout_tile_symbol },
@@ -1158,6 +1158,14 @@ fn get_lua_string(state: *c.lua_State, idx: c_int) ?[]const u8 {
1158 1158 return std.mem.span(cstr);
1159 1159 }
1160 1160
1161 +fn dupe_lua_string(state: *c.lua_State, idx: c_int) ?[]const u8 {
1162 + const cfg = config orelse return null;
1163 + const lua_str = get_lua_string(state, idx) orelse return null;
1164 + const arena_allocator = cfg.string_arena.allocator();
1165 + const duped = arena_allocator.dupe(u8, lua_str) catch return null;
1166 + return duped;
1167 +}
1168 +
1161 1169 fn parse_color(state: *c.lua_State, idx: c_int) u32 {
1162 1170 const lua_type = c.lua_type(state, idx);
1163 1171 if (lua_type == c.LUA_TNUMBER) {
+53 −4 src/main.zig
@@ -1,5 +1,5 @@
1 1 const std = @import("std");
2 -const VERSION = "v0.11.0";
2 +const VERSION = "v0.11.2";
3 3 const display_mod = @import("x11/display.zig");
4 4 const events = @import("x11/events.zig");
5 5 const xlib = @import("x11/xlib.zig");
@@ -88,6 +88,7 @@ fn print_help() void {
88 88 \\OPTIONS:
89 89 \\ --init Create default config in ~/.config/oxwm/config.lua
90 90 \\ --config <PATH> Use custom config file
91 + \\ --validate Validate config file without starting window manager
91 92 \\ --version Print version information
92 93 \\ --help Print this help message
93 94 \\
@@ -132,12 +133,40 @@ fn init_config(allocator: std.mem.Allocator) void {
132 133 std.debug.print("No compilation needed - changes take effect immediately!\n", .{});
133 134 }
134 135
136 +fn validate_config(allocator: std.mem.Allocator, config_path: []const u8) !void {
137 + config = config_mod.Config.init(allocator);
138 + defer config.deinit();
139 +
140 + if (!lua.init(&config)) {
141 + std.debug.print("error: failed to initialize lua\n", .{});
142 + std.process.exit(1);
143 + }
144 + defer lua.deinit();
145 +
146 + _ = std.fs.cwd().statFile(config_path) catch |err| {
147 + std.debug.print("error: config file not found: {s}\n", .{config_path});
148 + std.debug.print(" {}\n", .{err});
149 + std.process.exit(1);
150 + };
151 +
152 + if (lua.load_file(config_path)) {
153 + std.debug.print("✓ config valid: {s}\n", .{config_path});
154 + std.process.exit(0);
155 + } else {
156 + std.debug.print("✗ config validation failed\n", .{});
157 + std.process.exit(1);
158 + }
159 +}
160 +
135 161 pub fn main() !void {
136 162 const allocator = gpa.allocator();
137 163 defer _ = gpa.deinit();
138 164
139 - var config_path: []const u8 = try get_config_path(allocator);
140 - defer allocator.free(config_path);
165 + const default_config_path = try get_config_path(allocator);
166 + defer allocator.free(default_config_path);
167 +
168 + var config_path: []const u8 = default_config_path;
169 + var validate_mode: bool = false;
141 170 var args = std.process.args();
142 171 _ = args.skip();
143 172 while (args.next()) |arg| {
@@ -147,14 +176,21 @@ pub fn main() !void {
147 176 print_help();
148 177 return;
149 178 } else if (std.mem.eql(u8, arg, "-v") or std.mem.eql(u8, arg, "--version")) {
150 - std.debug.print(VERSION, .{});
179 + std.debug.print("{s}\n", .{VERSION});
151 180 return;
152 181 } else if (std.mem.eql(u8, arg, "--init")) {
153 182 init_config(allocator);
154 183 return;
184 + } else if (std.mem.eql(u8, arg, "--validate")) {
185 + validate_mode = true;
155 186 }
156 187 }
157 188
189 + if (validate_mode) {
190 + try validate_config(allocator, config_path);
191 + return;
192 + }
193 +
158 194 std.debug.print("oxwm starting\n", .{});
159 195
160 196 config = config_mod.Config.init(allocator);
@@ -218,6 +254,19 @@ pub fn main() !void {
218 254 std.debug.print("entering event loop\n", .{});
219 255 run_event_loop(&display);
220 256
257 + bar_mod.destroy_bars(allocator, display.handle);
258 +
259 + var mon = monitor_mod.monitors;
260 + while (mon) |m| {
261 + const next = m.next;
262 + monitor_mod.destroy(m);
263 + mon = next;
264 + }
265 +
266 + if (keybind_overlay) |overlay| {
267 + overlay.deinit(allocator);
268 + }
269 +
221 270 lua.deinit();
222 271 std.debug.print("oxwm exiting\n", .{});
223 272 }