tony / oxwm

refactor(monitor): remove last globals Browse files
emzywastaken committed 2026-02-23 parent e58c23b commit 69e7de0f157b5dc4c4ab56aff5b8660e1567e0ac
Showing 4 changed files with 27 additions and 46 deletions. UnifiedSplit
+18 −29 src/monitor.zig
@@ -1,13 +1,14 @@
1 1 const std = @import("std");
2 2 const xlib = @import("x11/xlib.zig");
3 +
3 4 const Client = @import("client.zig").Client;
5 +const WindowManager = @import("wm/wm.zig").WindowManager;
4 6
5 7 pub const Layout = struct {
6 8 symbol: []const u8,
7 9 arrange_fn: ?*const fn (*Monitor) void,
8 10 };
9 11
10 -// TODO: Make clearer, document? refactor?
11 12 pub const Pertag = struct {
12 13 curtag: u32 = 1,
13 14 prevtag: u32 = 1,
@@ -18,7 +19,6 @@ pub const Pertag = struct {
18 19 showbars: [10]bool = [_]bool{true} ** 10,
19 20 };
20 21
21 -// TODO: Make clearer, document? refactor?
22 22 pub const Monitor = struct {
23 23 lt_symbol: [16]u8 = std.mem.zeroes([16]u8),
24 24 mfact: f32 = 0.55,
@@ -52,47 +52,32 @@ pub const Monitor = struct {
52 52 pertag: Pertag = .{},
53 53 };
54 54
55 -// NOTE: `monitors` and `selected_monitor` will soon be removed, they will
56 -// move to `WindowManager` fields in a future refactor step. All new
57 -// code should prefer receiving a `*Monitor` or `?*Monitor` as a parameter
58 -// rather than reaching into these globals directly.
59 -
60 -pub var monitors: ?*Monitor = null;
61 -pub var selected_monitor: ?*Monitor = null;
62 -
63 -var allocator: std.mem.Allocator = undefined;
64 -
65 -/// Must be called once before any other function in this module.
66 -pub fn init(alloc: std.mem.Allocator) void {
67 - allocator = alloc;
68 -}
69 -
70 55 /// Allocates and zero-initialises a new `Monitor`.
71 -pub fn create() ?*Monitor {
56 +pub fn create(allocator: std.mem.Allocator) ?*Monitor {
72 57 const mon = allocator.create(Monitor) catch return null;
73 58 mon.* = Monitor{};
74 59 return mon;
75 60 }
76 61
77 62 /// Frees a monitor previously returned by `create`.
78 -pub fn destroy(mon: *Monitor) void {
63 +pub fn destroy(allocator: std.mem.Allocator, mon: *Monitor) void {
79 64 allocator.destroy(mon);
80 65 }
81 66
82 67 /// Returns the monitor whose bar window or client matches `win`, falling
83 68 /// back to a pointer-position query when `win` is the root window.
84 -///
85 -/// `display` and `root` are passed explicitly rather than cached in module
86 -/// state, this keeps ownership clear and avoids a stale-pointer hazard.
87 -pub fn window_to_monitor(display: *xlib.Display, root: xlib.Window, win: xlib.Window) ?*Monitor {
88 - if (win == root) {
69 +pub fn window_to_monitor(wm: *WindowManager, win: xlib.Window) ?*Monitor {
70 + const monitors = wm.monitors;
71 + const selected_monitor = wm.selected_monitor;
72 +
73 + if (win == wm.display.root) {
89 74 var root_x: c_int = undefined;
90 75 var root_y: c_int = undefined;
91 76 var dummy_win: xlib.Window = undefined;
92 77 var dummy_int: c_int = undefined;
93 78 var dummy_uint: c_uint = undefined;
94 - if (xlib.XQueryPointer(display, root, &dummy_win, &dummy_win, &root_x, &root_y, &dummy_int, &dummy_int, &dummy_uint) != 0) {
95 - return rect_to_monitor(root_x, root_y, 1, 1);
79 + if (xlib.XQueryPointer(wm.display.handle, wm.display.root, &dummy_win, &dummy_win, &root_x, &root_y, &dummy_int, &dummy_int, &dummy_uint) != 0) {
80 + return rect_to_monitor(wm, root_x, root_y, 1, 1);
96 81 }
97 82 }
98 83
@@ -112,7 +97,9 @@ pub fn window_to_monitor(display: *xlib.Display, root: xlib.Window, win: xlib.Wi
112 97
113 98 /// Returns the monitor with the greatest intersection area with the given
114 99 /// rectangle, or `selected_monitor` if no intersection is found.
115 -pub fn rect_to_monitor(x: i32, y: i32, width: i32, height: i32) ?*Monitor {
100 +pub fn rect_to_monitor(wm: *WindowManager, x: i32, y: i32, width: i32, height: i32) ?*Monitor {
101 + const monitors = wm.monitors;
102 + const selected_monitor = wm.selected_monitor;
116 103 var result = selected_monitor;
117 104 var max_area: i32 = 0;
118 105
@@ -130,7 +117,7 @@ pub fn rect_to_monitor(x: i32, y: i32, width: i32, height: i32) ?*Monitor {
130 117 return result;
131 118 }
132 119
133 -/// Returns the next or previous monitor relative to `selected_monitor`.
120 +/// Returns the next or previous monitor relative to `wm.selected_monitor`.
134 121 ///
135 122 /// Positive `direction` moves forward through the linked list (wrapping to
136 123 /// the head); negative moves backward (wrapping to the tail).
@@ -138,7 +125,9 @@ pub fn rect_to_monitor(x: i32, y: i32, width: i32, height: i32) ?*Monitor {
138 125 // TODO:
139 126 // - Change direction to an enum/enum_literal
140 127 // - Rename function
141 -pub fn dir_to_monitor(direction: i32) ?*Monitor {
128 +pub fn dir_to_monitor(wm: *WindowManager, direction: i32) ?*Monitor {
129 + const monitors = wm.monitors;
130 + const selected_monitor = wm.selected_monitor;
142 131 var target: ?*Monitor = null;
143 132
144 133 if (direction > 0) {
+3 −3 src/wm/actions.zig
@@ -403,7 +403,7 @@ pub fn set_layout_index(index: u32, wm: *WindowManager) void {
403 403
404 404 pub fn focusmon(direction: i32, wm: *WindowManager) void {
405 405 const selmon = wm.selected_monitor orelse return;
406 - const target = monitor_mod.dir_to_monitor(direction) orelse return;
406 + const target = monitor_mod.dir_to_monitor(wm, direction) orelse return;
407 407 if (target == selmon) {
408 408 return;
409 409 }
@@ -416,7 +416,7 @@ pub fn focusmon(direction: i32, wm: *WindowManager) void {
416 416 pub fn sendmon(direction: i32, wm: *WindowManager) void {
417 417 const source_monitor = wm.selected_monitor orelse return;
418 418 const client = source_monitor.sel orelse return;
419 - const target = monitor_mod.dir_to_monitor(direction) orelse return;
419 + const target = monitor_mod.dir_to_monitor(wm, direction) orelse return;
420 420
421 421 if (target == source_monitor) {
422 422 return;
@@ -533,7 +533,7 @@ pub fn movemouse(wm: *WindowManager) void {
533 533
534 534 _ = xlib.XUngrabPointer(wm.display.handle, xlib.CurrentTime);
535 535
536 - const new_mon = monitor_mod.rect_to_monitor(client.x, client.y, client.width, client.height);
536 + const new_mon = monitor_mod.rect_to_monitor(wm, client.x, client.y, client.width, client.height);
537 537 if (new_mon != null and new_mon != monitor) {
538 538 client_mod.detach(client);
539 539 client_mod.detach_stack(client);
+3 −3 src/wm/handlers.zig
@@ -193,7 +193,7 @@ fn handle_expose(event: *xlib.XExposeEvent, wm: *WindowManager) void {
193 193 fn handle_button_press(event: *xlib.XButtonEvent, wm: *WindowManager) void {
194 194 std.debug.print("button_press: window=0x{x} subwindow=0x{x}\n", .{ event.window, event.subwindow });
195 195
196 - const clicked_monitor = monitor_mod.window_to_monitor(wm.display.handle, wm.display.root, event.window);
196 + const clicked_monitor = monitor_mod.window_to_monitor(wm, event.window);
197 197 if (clicked_monitor) |monitor| {
198 198 if (monitor != wm.selected_monitor) {
199 199 if (wm.selected_monitor) |selmon| {
@@ -291,7 +291,7 @@ fn handle_enter_notify(event: *xlib.XCrossingEvent, wm: *WindowManager) void {
291 291 }
292 292
293 293 const client = client_mod.window_to_client(wm.monitors, event.window);
294 - const target_mon = if (client) |c| c.monitor else monitor_mod.window_to_monitor(wm.display.handle, wm.display.root, event.window);
294 + const target_mon = if (client) |c| c.monitor else monitor_mod.window_to_monitor(wm, event.window);
295 295 const selmon = wm.selected_monitor;
296 296
297 297 if (target_mon != selmon) {
@@ -321,7 +321,7 @@ fn handle_focus_in(event: *xlib.XFocusChangeEvent, wm: *WindowManager) void {
321 321 fn handle_motion_notify(event: *xlib.XMotionEvent, wm: *WindowManager) void {
322 322 if (event.window != wm.display.root) return;
323 323
324 - const target_mon = monitor_mod.rect_to_monitor(event.x_root, event.y_root, 1, 1);
324 + const target_mon = monitor_mod.rect_to_monitor(wm, event.x_root, event.y_root, 1, 1);
325 325 if (target_mon != wm.last_motion_monitor and wm.last_motion_monitor != null) {
326 326 if (wm.selected_monitor) |selmon| {
327 327 core.unfocus_client(selmon.sel, true, wm);
+3 −11 src/wm/wm.zig
@@ -105,8 +105,6 @@ pub const WindowManager = struct {
105 105 tiling.set_display(display.handle);
106 106 tiling.set_screen_size(display.screen_width(), display.screen_height());
107 107
108 - monitor_mod.init(allocator);
109 -
110 108 var wm = WindowManager{
111 109 .allocator = allocator,
112 110 .display = display,
@@ -148,7 +146,7 @@ pub const WindowManager = struct {
148 146 var mon = self.monitors;
149 147 while (mon) |m| {
150 148 const next = m.next;
151 - monitor_mod.destroy(m);
149 + monitor_mod.destroy(self.allocator, m);
152 150 mon = next;
153 151 }
154 152 self.monitors = null;
@@ -171,7 +169,7 @@ pub const WindowManager = struct {
171 169
172 170 while (index < @as(usize, @intCast(screen_count))) : (index += 1) {
173 171 const screen = screens[index];
174 - const mon = monitor_mod.create() orelse continue;
172 + const mon = monitor_mod.create(self.allocator) orelse continue;
175 173
176 174 mon.num = @intCast(index);
177 175 mon.mon_x = screen.x_org;
@@ -213,7 +211,7 @@ pub const WindowManager = struct {
213 211
214 212 // Fallback: single monitor covering the full screen.
215 213 if (self.monitors == null) {
216 - const mon = monitor_mod.create() orelse return;
214 + const mon = monitor_mod.create(self.allocator) orelse return;
217 215 mon.num = 0;
218 216 mon.mon_x = 0;
219 217 mon.mon_y = 0;
@@ -241,12 +239,6 @@ pub const WindowManager = struct {
241 239 self.monitors = mon;
242 240 self.selected_monitor = mon;
243 241 }
244 -
245 - // Mirror into monitor_mod so legacy code that still reads the
246 - // module-level vars continues to work during the transition.
247 - // TODO(wm-refactor): remove once all callers use self.monitors.
248 - monitor_mod.monitors = self.monitors;
249 - monitor_mod.selected_monitor = self.selected_monitor;
250 242 }
251 243
252 244 fn init_monitor_gaps(self: *WindowManager, mon: *Monitor) void {