tony / oxwm

refactor(wm): centralize config reload logic with callback fn Browse files
- Remove old `reload_config` free function in main.zig
emzywastaken committed 2026-02-22 parent a04b4ce commit 43022c012580d511503b671dfcaf13455323e984
Showing 2 changed files with 53 additions and 40 deletions. UnifiedSplit
+25 −39 src/main.zig
@@ -226,6 +226,29 @@ fn make_keybind_str(mod: u32, key: u64, action: config_mod.Action, str_arg: []co
226 return kb; 226 return kb;
227 } 227 }
228 228
229 +/// Config-loading callback for wm.reload_config().
230 +/// Re-initialises Lua and loads the config file (or falls back to defaults).
231 +fn reload_load_config(wm: *WindowManager) void {
232 + lua.deinit();
233 + _ = lua.init(&wm.config);
234 +
235 + const loaded = if (wm.config_path) |path|
236 + lua.load_file(path)
237 + else
238 + lua.load_config();
239 +
240 + if (loaded) {
241 + if (wm.config_path) |path| {
242 + std.debug.print("reloaded config from {s}\n", .{path});
243 + } else {
244 + std.debug.print("reloaded config from ~/.config/oxwm/config.lua\n", .{});
245 + }
246 + } else {
247 + std.debug.print("reload failed, restoring defaults\n", .{});
248 + initialize_default_config(&wm.config);
249 + }
250 +}
251 +
229 fn initialize_default_config(cfg: *config_mod.Config) void { 252 fn initialize_default_config(cfg: *config_mod.Config) void {
230 const mod_key: u32 = 1 << 6; 253 const mod_key: u32 = 1 << 6;
231 const shift_key: u32 = 1 << 0; 254 const shift_key: u32 = 1 << 0;
@@ -521,8 +544,8 @@ fn execute_action(action: config_mod.Action, int_arg: i32, str_arg: ?[]const u8,
521 std.debug.print("quit keybind pressed\n", .{}); 544 std.debug.print("quit keybind pressed\n", .{});
522 wm.running = false; 545 wm.running = false;
523 }, 546 },
524 - .reload_config => reload_config(wm), 547 + .reload_config => wm.reload_config(reload_load_config),
525 - .restart => reload_config(wm), 548 + .restart => wm.reload_config(reload_load_config),
526 .show_keybinds => { 549 .show_keybinds => {
527 if (wm.overlay) |overlay| { 550 if (wm.overlay) |overlay| {
528 const mon = wm.selected_monitor orelse wm.monitors; 551 const mon = wm.selected_monitor orelse wm.monitors;
@@ -572,43 +595,6 @@ fn execute_action(action: config_mod.Action, int_arg: i32, str_arg: ?[]const u8,
572 } 595 }
573 } 596 }
574 597
575 -fn reload_config(wm: *WindowManager) void {
576 - std.debug.print("reloading config...\n", .{});
577 -
578 - wm.ungrab_keybinds();
579 -
580 - wm.config.keybinds.clearRetainingCapacity();
581 - wm.config.buttons.clearRetainingCapacity();
582 - wm.config.rules.clearRetainingCapacity();
583 - wm.config.blocks.clearRetainingCapacity();
584 -
585 - lua.deinit();
586 - _ = lua.init(&wm.config);
587 -
588 - const loaded = if (wm.config_path) |path|
589 - lua.load_file(path)
590 - else
591 - lua.load_config();
592 -
593 - if (loaded) {
594 - if (wm.config_path) |path| {
595 - std.debug.print("reloaded config from {s}\n", .{path});
596 - } else {
597 - std.debug.print("reloaded config from ~/.config/oxwm/config.lua\n", .{});
598 - }
599 - } else {
600 - std.debug.print("reload failed, restoring defaults\n", .{});
601 - initialize_default_config(&wm.config);
602 - }
603 -
604 - bar_mod.destroy_bars(wm.bars, gpa.allocator(), wm.display.handle);
605 - wm.bars = null;
606 - wm.setup_bars();
607 - wm.rebuild_bar_blocks();
608 -
609 - wm.grab_keybinds();
610 -}
611 -
612 fn spawn_child_setup() void { 598 fn spawn_child_setup() void {
613 _ = std.c.setsid(); 599 _ = std.c.setsid();
614 if (x11_fd >= 0) std.posix.close(@intCast(x11_fd)); 600 if (x11_fd >= 0) std.posix.close(@intCast(x11_fd));
+28 −1 src/wm.zig
@@ -523,7 +523,34 @@ pub const WindowManager = struct {
523 } 523 }
524 } 524 }
525 525
526 - pub fn rebuild_bar_blocks(self: *WindowManager) void { 526 + /// Reloads the configuration
527 + ///
528 + /// `load_fn` repopulates `self.config`
529 + /// keys/buttons are unbound before reload and rebound after
530 + pub fn reload_config(
531 + self: *WindowManager,
532 + load_fn: fn (*WindowManager) void,
533 + ) void {
534 + std.debug.print("reloading config...\n", .{});
535 +
536 + self.ungrab_keybinds();
537 +
538 + self.config.keybinds.clearRetainingCapacity();
539 + self.config.buttons.clearRetainingCapacity();
540 + self.config.rules.clearRetainingCapacity();
541 + self.config.blocks.clearRetainingCapacity();
542 +
543 + load_fn(self);
544 +
545 + bar_mod.destroy_bars(self.bars, self.allocator, self.display.handle);
546 + self.bars = null;
547 + self.setup_bars();
548 + self.rebuild_bar_blocks();
549 +
550 + self.grab_keybinds();
551 + }
552 +
553 + fn rebuild_bar_blocks(self: *WindowManager) void {
527 var current_bar = self.bars; 554 var current_bar = self.bars;
528 while (current_bar) |bar| { 555 while (current_bar) |bar| {
529 bar.clear_blocks(); 556 bar.clear_blocks();