tony / oxwm

refactor: get rid of `config` and `config_path_global` globals in main module Browse files
`initialize_default_config()` now takes `*config_mod.Config`
emzywastaken committed 2026-02-21 parent 2cbdc67 commit 5acaef31838a86fc11df2478fdad3a143aaf9f42
Showing 1 changed file with 66 additions and 69 deletions. UnifiedSplit
+66 −69 src/main.zig
@@ -25,11 +25,6 @@ const IconicState: c_long = 3;
25 25 const IsViewable: c_int = 2;
26 26 const snap_distance: i32 = 32;
27 27
28 -/// Config built from Lua, passed into `WindowManager.init`.
29 -var config: config_mod.Config = undefined;
30 -/// Path to the loaded config file.
31 -var config_path_global: ?[]const u8 = null;
32 -
33 28 /// File descriptor of the X11 connection, used in spawn_child_setup.
34 29 /// Set from `wm.x11_fd` after init.
35 30 var x11_fd: c_int = -1;
@@ -109,7 +104,7 @@ fn init_config(allocator: std.mem.Allocator) void {
109 104 }
110 105
111 106 fn validate_config(allocator: std.mem.Allocator, config_path: []const u8) !void {
112 - config = config_mod.Config.init(allocator);
107 + var config = config_mod.Config.init(allocator);
113 108 defer config.deinit();
114 109
115 110 if (!lua.init(&config)) {
@@ -168,7 +163,8 @@ pub fn main() !void {
168 163
169 164 std.debug.print("oxwm starting\n", .{});
170 165
171 - config = config_mod.Config.init(allocator);
166 + var config = config_mod.Config.init(allocator);
167 + var config_path_global: ?[]const u8 = null;
172 168
173 169 if (lua.init(&config)) {
174 170 const loaded = if (std.fs.cwd().statFile(config_path)) |_|
@@ -183,11 +179,11 @@ pub fn main() !void {
183 179 std.debug.print("loaded config from {s}\n", .{config_path});
184 180 } else {
185 181 std.debug.print("no config found, using defaults\n", .{});
186 - initialize_default_config();
182 + initialize_default_config(&config);
187 183 }
188 184 } else {
189 185 std.debug.print("failed to init lua, using defaults\n", .{});
190 - initialize_default_config();
186 + initialize_default_config(&config);
191 187 }
192 188
193 189 var wm = WindowManager.init(allocator, config, config_path_global) catch |err| {
@@ -196,8 +192,8 @@ pub fn main() !void {
196 192 };
197 193 defer wm.deinit();
198 194
199 - // Propagate values that transitional code in this file still reads
200 - // from module-level vars. These will be removed soon.
195 + // x11_fd is read by spawn_child_setup() which runs in a forked child
196 + // and cannot access wm directly.
201 197 x11_fd = wm.x11_fd;
202 198
203 199 std.debug.print("display opened: screen={d} root=0x{x}\n", .{ wm.display.screen, wm.display.root });
@@ -207,7 +203,7 @@ pub fn main() !void {
207 203 grab_keybinds(&wm.display, &wm);
208 204 scan_existing_windows(&wm.display, &wm);
209 205
210 - try run_autostart_commands(allocator, config.autostart.items);
206 + try run_autostart_commands(allocator, wm.config.autostart.items);
211 207 std.debug.print("entering event loop\n", .{});
212 208 run_event_loop(&wm);
213 209
@@ -234,45 +230,45 @@ fn make_keybind_str(mod: u32, key: u64, action: config_mod.Action, str_arg: []co
234 230 return kb;
235 231 }
236 232
237 -fn initialize_default_config() void {
233 +fn initialize_default_config(cfg: *config_mod.Config) void {
238 234 const mod_key: u32 = 1 << 6;
239 235 const shift_key: u32 = 1 << 0;
240 236 const control_key: u32 = 1 << 2;
241 237
242 - config.add_keybind(make_keybind(mod_key, 0xff0d, .spawn_terminal)) catch {};
243 - config.add_keybind(make_keybind_str(mod_key, 'd', .spawn, "rofi -show drun")) catch {};
244 - config.add_keybind(make_keybind_str(mod_key, 's', .spawn, "maim -s | xclip -selection clipboard -t image/png")) catch {};
245 - config.add_keybind(make_keybind(mod_key, 'q', .kill_client)) catch {};
246 - config.add_keybind(make_keybind(mod_key | shift_key, 'q', .quit)) catch {};
247 - config.add_keybind(make_keybind(mod_key | shift_key, 'r', .reload_config)) catch {};
248 - config.add_keybind(make_keybind(mod_key, 'j', .focus_next)) catch {};
249 - config.add_keybind(make_keybind(mod_key, 'k', .focus_prev)) catch {};
250 - config.add_keybind(make_keybind(mod_key | shift_key, 'j', .move_next)) catch {};
251 - config.add_keybind(make_keybind(mod_key | shift_key, 'k', .move_prev)) catch {};
252 - config.add_keybind(make_keybind_int(mod_key, 'h', .resize_master, -50)) catch {};
253 - config.add_keybind(make_keybind_int(mod_key, 'l', .resize_master, 50)) catch {};
254 - config.add_keybind(make_keybind(mod_key, 'i', .inc_master)) catch {};
255 - config.add_keybind(make_keybind(mod_key, 'p', .dec_master)) catch {};
256 - config.add_keybind(make_keybind(mod_key, 'a', .toggle_gaps)) catch {};
257 - config.add_keybind(make_keybind(mod_key, 'f', .toggle_fullscreen)) catch {};
258 - config.add_keybind(make_keybind(mod_key, 0x0020, .toggle_floating)) catch {};
259 - config.add_keybind(make_keybind(mod_key, 'n', .cycle_layout)) catch {};
260 - config.add_keybind(make_keybind_int(mod_key, 0x002c, .focus_monitor, -1)) catch {};
261 - config.add_keybind(make_keybind_int(mod_key, 0x002e, .focus_monitor, 1)) catch {};
262 - config.add_keybind(make_keybind_int(mod_key | shift_key, 0x002c, .send_to_monitor, -1)) catch {};
263 - config.add_keybind(make_keybind_int(mod_key | shift_key, 0x002e, .send_to_monitor, 1)) catch {};
238 + cfg.add_keybind(make_keybind(mod_key, 0xff0d, .spawn_terminal)) catch {};
239 + cfg.add_keybind(make_keybind_str(mod_key, 'd', .spawn, "rofi -show drun")) catch {};
240 + cfg.add_keybind(make_keybind_str(mod_key, 's', .spawn, "maim -s | xclip -selection clipboard -t image/png")) catch {};
241 + cfg.add_keybind(make_keybind(mod_key, 'q', .kill_client)) catch {};
242 + cfg.add_keybind(make_keybind(mod_key | shift_key, 'q', .quit)) catch {};
243 + cfg.add_keybind(make_keybind(mod_key | shift_key, 'r', .reload_config)) catch {};
244 + cfg.add_keybind(make_keybind(mod_key, 'j', .focus_next)) catch {};
245 + cfg.add_keybind(make_keybind(mod_key, 'k', .focus_prev)) catch {};
246 + cfg.add_keybind(make_keybind(mod_key | shift_key, 'j', .move_next)) catch {};
247 + cfg.add_keybind(make_keybind(mod_key | shift_key, 'k', .move_prev)) catch {};
248 + cfg.add_keybind(make_keybind_int(mod_key, 'h', .resize_master, -50)) catch {};
249 + cfg.add_keybind(make_keybind_int(mod_key, 'l', .resize_master, 50)) catch {};
250 + cfg.add_keybind(make_keybind(mod_key, 'i', .inc_master)) catch {};
251 + cfg.add_keybind(make_keybind(mod_key, 'p', .dec_master)) catch {};
252 + cfg.add_keybind(make_keybind(mod_key, 'a', .toggle_gaps)) catch {};
253 + cfg.add_keybind(make_keybind(mod_key, 'f', .toggle_fullscreen)) catch {};
254 + cfg.add_keybind(make_keybind(mod_key, 0x0020, .toggle_floating)) catch {};
255 + cfg.add_keybind(make_keybind(mod_key, 'n', .cycle_layout)) catch {};
256 + cfg.add_keybind(make_keybind_int(mod_key, 0x002c, .focus_monitor, -1)) catch {};
257 + cfg.add_keybind(make_keybind_int(mod_key, 0x002e, .focus_monitor, 1)) catch {};
258 + cfg.add_keybind(make_keybind_int(mod_key | shift_key, 0x002c, .send_to_monitor, -1)) catch {};
259 + cfg.add_keybind(make_keybind_int(mod_key | shift_key, 0x002e, .send_to_monitor, 1)) catch {};
264 260
265 261 var tag_index: i32 = 0;
266 262 while (tag_index < 9) : (tag_index += 1) {
267 263 const keysym: u64 = @as(u64, '1') + @as(u64, @intCast(tag_index));
268 - config.add_keybind(make_keybind_int(mod_key, keysym, .view_tag, tag_index)) catch {};
269 - config.add_keybind(make_keybind_int(mod_key | shift_key, keysym, .move_to_tag, tag_index)) catch {};
270 - config.add_keybind(make_keybind_int(mod_key | control_key, keysym, .toggle_view_tag, tag_index)) catch {};
271 - config.add_keybind(make_keybind_int(mod_key | control_key | shift_key, keysym, .toggle_tag, tag_index)) catch {};
264 + cfg.add_keybind(make_keybind_int(mod_key, keysym, .view_tag, tag_index)) catch {};
265 + cfg.add_keybind(make_keybind_int(mod_key | shift_key, keysym, .move_to_tag, tag_index)) catch {};
266 + cfg.add_keybind(make_keybind_int(mod_key | control_key, keysym, .toggle_view_tag, tag_index)) catch {};
267 + cfg.add_keybind(make_keybind_int(mod_key | control_key | shift_key, keysym, .toggle_tag, tag_index)) catch {};
272 268 }
273 269
274 - config.add_button(.{ .click = .client_win, .mod_mask = mod_key, .button = 1, .action = .move_mouse }) catch {};
275 - config.add_button(.{ .click = .client_win, .mod_mask = mod_key, .button = 3, .action = .resize_mouse }) catch {};
270 + cfg.add_button(.{ .click = .client_win, .mod_mask = mod_key, .button = 1, .action = .move_mouse }) catch {};
271 + cfg.add_button(.{ .click = .client_win, .mod_mask = mod_key, .button = 3, .action = .resize_mouse }) catch {};
276 272 }
277 273
278 274 fn grab_keybinds(display: *Display, wm: *WindowManager) void {
@@ -281,7 +277,7 @@ fn grab_keybinds(display: *Display, wm: *WindowManager) void {
281 277
282 278 _ = xlib.XUngrabKey(display.handle, xlib.AnyKey, xlib.AnyModifier, display.root);
283 279
284 - for (config.keybinds.items) |keybind| {
280 + for (wm.config.keybinds.items) |keybind| {
285 281 if (keybind.key_count == 0) continue;
286 282 const first_key = keybind.keys[0];
287 283 const keycode = xlib.XKeysymToKeycode(display.handle, @intCast(first_key.keysym));
@@ -300,7 +296,7 @@ fn grab_keybinds(display: *Display, wm: *WindowManager) void {
300 296 }
301 297 }
302 298
303 - for (config.buttons.items) |button| {
299 + for (wm.config.buttons.items) |button| {
304 300 if (button.click == .client_win) {
305 301 for (modifiers) |modifier| {
306 302 _ = xlib.XGrabButton(
@@ -319,7 +315,7 @@ fn grab_keybinds(display: *Display, wm: *WindowManager) void {
319 315 }
320 316 }
321 317
322 - std.debug.print("grabbed {d} keybinds from config\n", .{config.keybinds.items.len});
318 + std.debug.print("grabbed {d} keybinds from config\n", .{wm.config.keybinds.items.len});
323 319 }
324 320
325 321 fn get_state(display: *Display, window: xlib.Window, wm: *WindowManager) c_long {
@@ -484,7 +480,7 @@ fn manage(display: *Display, win: xlib.Window, window_attrs: *xlib.XWindowAttrib
484 480 client.old_width = window_attrs.width;
485 481 client.old_height = window_attrs.height;
486 482 client.old_border_width = window_attrs.border_width;
487 - client.border_width = config.border_width;
483 + client.border_width = wm.config.border_width;
488 484
489 485 update_title(display, client, wm);
490 486
@@ -512,7 +508,7 @@ fn manage(display: *Display, win: xlib.Window, window_attrs: *xlib.XWindowAttrib
512 508 client.y = @max(client.y, monitor.win_y);
513 509
514 510 _ = xlib.XSetWindowBorderWidth(display.handle, win, @intCast(client.border_width));
515 - _ = xlib.XSetWindowBorder(display.handle, win, config.border_unfocused);
511 + _ = xlib.XSetWindowBorder(display.handle, win, wm.config.border_unfocused);
516 512 tiling.send_configure(client);
517 513
518 514 update_window_type(display, client, wm);
@@ -627,7 +623,7 @@ fn handle_key_press(display: *Display, event: *xlib.XKeyEvent, wm: *WindowManage
627 623
628 624 _ = wm.chord.push(.{ .mod_mask = clean_state, .keysym = keysym });
629 625
630 - for (config.keybinds.items) |keybind| {
626 + for (wm.config.keybinds.items) |keybind| {
631 627 if (keybind.key_count == 0) continue;
632 628
633 629 if (keybind.key_count == wm.chord.index) {
@@ -649,7 +645,7 @@ fn handle_key_press(display: *Display, event: *xlib.XKeyEvent, wm: *WindowManage
649 645 }
650 646
651 647 var has_partial_match = false;
652 - for (config.keybinds.items) |keybind| {
648 + for (wm.config.keybinds.items) |keybind| {
653 649 if (keybind.key_count > wm.chord.index) {
654 650 var matches = true;
655 651 for (0..wm.chord.index) |i| {
@@ -676,7 +672,7 @@ fn handle_key_press(display: *Display, event: *xlib.XKeyEvent, wm: *WindowManage
676 672
677 673 fn execute_action(display: *Display, action: config_mod.Action, int_arg: i32, str_arg: ?[]const u8, wm: *WindowManager) void {
678 674 switch (action) {
679 - .spawn_terminal => spawn_terminal(),
675 + .spawn_terminal => spawn_terminal(wm),
680 676 .spawn => {
681 677 if (str_arg) |cmd| spawn_command(cmd);
682 678 },
@@ -741,28 +737,28 @@ fn reload_config(display: *Display, wm: *WindowManager) void {
741 737
742 738 ungrab_keybinds(display);
743 739
744 - config.keybinds.clearRetainingCapacity();
745 - config.buttons.clearRetainingCapacity();
746 - config.rules.clearRetainingCapacity();
747 - config.blocks.clearRetainingCapacity();
740 + wm.config.keybinds.clearRetainingCapacity();
741 + wm.config.buttons.clearRetainingCapacity();
742 + wm.config.rules.clearRetainingCapacity();
743 + wm.config.blocks.clearRetainingCapacity();
748 744
749 745 lua.deinit();
750 - _ = lua.init(&config);
746 + _ = lua.init(&wm.config);
751 747
752 - const loaded = if (config_path_global) |path|
748 + const loaded = if (wm.config_path) |path|
753 749 lua.load_file(path)
754 750 else
755 751 lua.load_config();
756 752
757 753 if (loaded) {
758 - if (config_path_global) |path| {
754 + if (wm.config_path) |path| {
759 755 std.debug.print("reloaded config from {s}\n", .{path});
760 756 } else {
761 757 std.debug.print("reloaded config from ~/.config/oxwm/config.lua\n", .{});
762 758 }
763 759 } else {
764 760 std.debug.print("reload failed, restoring defaults\n", .{});
765 - initialize_default_config();
761 + initialize_default_config(&wm.config);
766 762 }
767 763
768 764 bar_mod.destroy_bars(wm.bars, gpa.allocator(), display.handle);
@@ -814,12 +810,13 @@ fn spawn_command(cmd: []const u8) void {
814 810 }
815 811 }
816 812
817 -fn spawn_terminal() void {
813 +// TODO: take in the terminal cmd directly.
814 +fn spawn_terminal(wm: *WindowManager) void {
818 815 const pid = std.posix.fork() catch return;
819 816 if (pid == 0) {
820 817 spawn_child_setup();
821 818 var term_buf: [256]u8 = undefined;
822 - const terminal = config.terminal;
819 + const terminal = wm.config.terminal;
823 820 if (terminal.len >= term_buf.len) {
824 821 std.posix.exit(1);
825 822 }
@@ -940,10 +937,10 @@ fn toggle_client_tag(display: *Display, tag_mask: u32, wm: *WindowManager) void
940 937 fn toggle_gaps(wm: *WindowManager) void {
941 938 const monitor = monitor_mod.selected_monitor orelse return;
942 939 if (monitor.gap_inner_h == 0) {
943 - monitor.gap_inner_h = config.gap_inner_h;
944 - monitor.gap_inner_v = config.gap_inner_v;
945 - monitor.gap_outer_h = config.gap_outer_h;
946 - monitor.gap_outer_v = config.gap_outer_v;
940 + monitor.gap_inner_h = wm.config.gap_inner_h;
941 + monitor.gap_inner_v = wm.config.gap_inner_v;
942 + monitor.gap_outer_h = wm.config.gap_outer_h;
943 + monitor.gap_outer_v = wm.config.gap_outer_v;
947 944 } else {
948 945 monitor.gap_inner_h = 0;
949 946 monitor.gap_inner_v = 0;
@@ -1432,7 +1429,7 @@ fn movemouse(display: *Display, wm: *WindowManager) void {
1432 1429 arrange(monitor, wm);
1433 1430 }
1434 1431
1435 - if (config.auto_tile and !was_floating) {
1432 + if (wm.config.auto_tile and !was_floating) {
1436 1433 const drop_monitor = client.monitor orelse return;
1437 1434 const center_x = client.x + @divTrunc(client.width, 2);
1438 1435 const center_y = client.y + @divTrunc(client.height, 2);
@@ -1572,7 +1569,7 @@ fn handle_button_press(display: *Display, event: *xlib.XButtonEvent, wm: *Window
1572 1569 }
1573 1570
1574 1571 const clean_state = clean_mask(event.state, wm);
1575 - for (config.buttons.items) |button| {
1572 + for (wm.config.buttons.items) |button| {
1576 1573 if (button.click != .client_win) continue;
1577 1574 const button_clean_mask = clean_mask(button.mod_mask, wm);
1578 1575 if (clean_state == button_clean_mask and event.button == button.button) {
@@ -1770,7 +1767,7 @@ fn handle_property_notify(display: *Display, event: *xlib.XPropertyEvent, wm: *W
1770 1767 fn unfocus_client(display: *Display, client: ?*Client, reset_input_focus: bool, wm: *WindowManager) void {
1771 1768 const unfocus_target = client orelse return;
1772 1769 grabbuttons(display, unfocus_target, false, wm);
1773 - _ = xlib.XSetWindowBorder(display.handle, unfocus_target.window, config.border_unfocused);
1770 + _ = xlib.XSetWindowBorder(display.handle, unfocus_target.window, wm.config.border_unfocused);
1774 1771 if (reset_input_focus) {
1775 1772 _ = xlib.XSetInputFocus(display.handle, display.root, xlib.RevertToPointerRoot, xlib.CurrentTime);
1776 1773 _ = xlib.XDeleteProperty(display.handle, display.root, wm.atoms.net_active_window);
@@ -1821,7 +1818,7 @@ fn grabbuttons(display: *Display, client: *Client, focused: bool, wm: *WindowMan
1821 1818 xlib.None,
1822 1819 );
1823 1820 }
1824 - for (config.buttons.items) |button| {
1821 + for (wm.config.buttons.items) |button| {
1825 1822 if (button.click == .client_win) {
1826 1823 for (modifiers) |modifier| {
1827 1824 _ = xlib.XGrabButton(
@@ -1867,7 +1864,7 @@ fn focus(display: *Display, target_client: ?*Client, wm: *WindowManager) void {
1867 1864 client_mod.detach_stack(client);
1868 1865 client_mod.attach_stack(client);
1869 1866 grabbuttons(display, client, true, wm);
1870 - _ = xlib.XSetWindowBorder(display.handle, client.window, config.border_focused);
1867 + _ = xlib.XSetWindowBorder(display.handle, client.window, wm.config.border_focused);
1871 1868 if (!client.never_focus) {
1872 1869 _ = xlib.XSetInputFocus(display.handle, client.window, xlib.RevertToPointerRoot, xlib.CurrentTime);
1873 1870 _ = xlib.XChangeProperty(display.handle, display.root, wm.atoms.net_active_window, xlib.XA_WINDOW, 32, xlib.PropModeReplace, @ptrCast(&client.window), 1);
@@ -2151,7 +2148,7 @@ fn apply_rules(display: *Display, client: *Client, wm: *WindowManager) void {
2151 2148 client.tags = 0;
2152 2149 var rule_focus = false;
2153 2150
2154 - for (config.rules.items) |rule| {
2151 + for (wm.config.rules.items) |rule| {
2155 2152 const class_matches = if (rule.class) |rc| std.mem.indexOf(u8, class_str, rc) != null else true;
2156 2153 const instance_matches = if (rule.instance) |ri| std.mem.indexOf(u8, instance_str, ri) != null else true;
2157 2154 const title_matches = if (rule.title) |rt| std.mem.indexOf(u8, std.mem.sliceTo(&client.name, 0), rt) != null else true;