tony / oxwm

refactor: create `WindowManager` type to hold wm state, cleanup bar and monitor module Browse files
- moved almost all global state into new `WindowManager` struct
- clean up `bar` module and remove reliance on global state
- add doc commnets for some functions in `bar` module
- document and clean up `monitor` module, removing some global state

some temporary global state still exists but will be removed in the
coming commits
emzywastaken committed 2026-02-21 parent 15a76df commit 82fd9ea645555288b5ec9f2911e9b3025b54a1ed
Showing 4 changed files with 616 additions and 496 deletions. UnifiedSplit
+80 −113 src/bar/bar.zig
@@ -1,7 +1,6 @@
1 1 const std = @import("std");
2 2 const xlib = @import("../x11/xlib.zig");
3 3 const monitor_mod = @import("../monitor.zig");
4 -const client_mod = @import("../client.zig");
5 4 const blocks_mod = @import("blocks/blocks.zig");
6 5 const config_mod = @import("../config/config.zig");
7 6 const ColorScheme = config_mod.ColorScheme;
@@ -9,22 +8,12 @@ const ColorScheme = config_mod.ColorScheme;
9 8 const Monitor = monitor_mod.Monitor;
10 9 const Block = blocks_mod.Block;
11 10
12 -fn get_layout_symbol(layout_index: u32, config: ?config_mod.Config) []const u8 {
13 - if (config) |conf| {
14 - return switch (layout_index) {
15 - 0 => conf.layout_tile_symbol,
16 - 1 => conf.layout_monocle_symbol,
17 - 2 => conf.layout_floating_symbol,
18 - 3 => "[S]",
19 - 4 => "[#]",
20 - else => "[?]",
21 - };
22 - }
11 +fn get_layout_symbol(layout_index: u32, config: config_mod.Config) []const u8 {
23 12 return switch (layout_index) {
24 - 0 => "[]=",
25 - 1 => "[M]",
26 - 2 => "><>",
27 - 3 => "[S]",
13 + 0 => config.layout_tile_symbol,
14 + 1 => config.layout_monocle_symbol,
15 + 2 => config.layout_floating_symbol,
16 + 3 => config.layout_scrolling_symbol,
28 17 4 => "[#]",
29 18 else => "[?]",
30 19 };
@@ -53,6 +42,8 @@ pub const Bar = struct {
53 42 needs_redraw: bool,
54 43 next: ?*Bar,
55 44
45 + /// Creates a bar window for `monitor` using the given config.
46 + /// Returns null on allocation failure or if the font cannot be loaded.
56 47 pub fn create(
57 48 allocator: std.mem.Allocator,
58 49 display: *xlib.Display,
@@ -67,7 +58,10 @@ pub const Bar = struct {
67 58 const depth = xlib.XDefaultDepth(display, screen);
68 59 const root = xlib.XRootWindow(display, screen);
69 60
70 - const font_name_z = allocator.dupeZ(u8, config.font) catch return null;
61 + const font_name_z = allocator.dupeZ(u8, config.font) catch {
62 + allocator.destroy(bar);
63 + return null;
64 + };
71 65 defer allocator.free(font_name_z);
72 66
73 67 const font = xlib.XftFontOpenName(display, screen, font_name_z);
@@ -91,7 +85,6 @@ pub const Bar = struct {
91 85 0x1a1b26,
92 86 );
93 87
94 - _ = xlib.c.XSetWindowAttributes{};
95 88 var attributes: xlib.c.XSetWindowAttributes = undefined;
96 89 attributes.override_redirect = xlib.True;
97 90 attributes.event_mask = xlib.c.ExposureMask | xlib.c.ButtonPressMask;
@@ -106,7 +99,6 @@ pub const Bar = struct {
106 99 );
107 100
108 101 const graphics_context = xlib.XCreateGC(display, pixmap, 0, null);
109 -
110 102 const xft_draw = xlib.XftDrawCreate(display, pixmap, visual, colormap);
111 103
112 104 _ = xlib.XMapWindow(display, window);
@@ -127,7 +119,7 @@ pub const Bar = struct {
127 119 .scheme_urgent = config.scheme_urgent,
128 120 .hide_vacant_tags = config.hide_vacant_tags,
129 121 .allocator = allocator,
130 - .blocks = .{},
122 + .blocks = .empty,
131 123 .needs_redraw = true,
132 124 .next = null,
133 125 };
@@ -139,13 +131,12 @@ pub const Bar = struct {
139 131 return bar;
140 132 }
141 133
134 + /// Destroys the bar's X resources and frees the allocation.
135 + // TODO: Remove `allocator` param as its already stored in `Bar`.
142 136 pub fn destroy(self: *Bar, allocator: std.mem.Allocator, display: *xlib.Display) void {
143 - if (self.xft_draw) |xft_draw| {
144 - xlib.XftDrawDestroy(xft_draw);
145 - }
146 - if (self.font) |font| {
147 - xlib.XftFontClose(display, font);
148 - }
137 + if (self.xft_draw) |xft_draw| xlib.XftDrawDestroy(xft_draw);
138 + if (self.font) |font| xlib.XftFontClose(display, font);
139 +
149 140 _ = xlib.XFreeGC(display, self.graphics_context);
150 141 _ = xlib.XFreePixmap(display, self.pixmap);
151 142 _ = xlib.c.XDestroyWindow(display, self.window);
@@ -157,11 +148,16 @@ pub const Bar = struct {
157 148 self.blocks.append(self.allocator, block) catch {};
158 149 }
159 150
151 + pub fn clear_blocks(self: *Bar) void {
152 + self.blocks.clearRetainingCapacity();
153 + }
154 +
160 155 pub fn invalidate(self: *Bar) void {
161 156 self.needs_redraw = true;
162 157 }
163 158
164 - pub fn draw(self: *Bar, display: *xlib.Display, tags: []const []const u8, config: ?config_mod.Config) void {
159 + /// Redraws the bar if marked dirty. Tags are taken from `config.tags`
160 + pub fn draw(self: *Bar, display: *xlib.Display, config: config_mod.Config) void {
165 161 if (!self.needs_redraw) return;
166 162
167 163 self.fill_rect(display, 0, 0, self.width, self.height, self.scheme_normal.background);
@@ -171,14 +167,19 @@ pub const Bar = struct {
171 167 const monitor = self.monitor;
172 168 const current_tags = monitor.tagset[monitor.sel_tags];
173 169
174 - for (tags, 0..) |tag, index| {
170 + for (config.tags, 0..) |tag, index| {
175 171 const tag_mask: u32 = @as(u32, 1) << @intCast(index);
176 172 const is_selected = (current_tags & tag_mask) != 0;
177 173 const is_occupied = has_clients_on_tag(monitor, tag_mask);
178 174
179 175 if (self.hide_vacant_tags and !is_occupied and !is_selected) continue;
180 176
181 - const scheme = if (is_selected) self.scheme_selected else if (is_occupied) self.scheme_occupied else self.scheme_normal;
177 + const scheme = if (is_selected)
178 + self.scheme_selected
179 + else if (is_occupied)
180 + self.scheme_occupied
181 + else
182 + self.scheme_normal;
182 183
183 184 const tag_text_width = self.text_width(display, tag);
184 185 const tag_width = tag_text_width + padding * 2;
@@ -189,7 +190,6 @@ pub const Bar = struct {
189 190
190 191 const text_y = @divTrunc(self.height + self.font_height, 2) - 4;
191 192 self.draw_text(display, x_position + padding, text_y, tag, scheme.foreground);
192 -
193 193 x_position += tag_width;
194 194 }
195 195
@@ -220,48 +220,15 @@ pub const Bar = struct {
220 220 self.needs_redraw = false;
221 221 }
222 222
223 - fn fill_rect(self: *Bar, display: *xlib.Display, x: i32, y: i32, width: i32, height: i32, color: c_ulong) void {
224 - _ = xlib.XSetForeground(display, self.graphics_context, color);
225 - _ = xlib.XFillRectangle(display, self.pixmap, self.graphics_context, x, y, @intCast(width), @intCast(height));
226 - }
227 -
228 - fn draw_text(self: *Bar, display: *xlib.Display, x: i32, y: i32, text: []const u8, color: c_ulong) void {
229 - if (self.xft_draw == null or self.font == null) return;
230 -
231 - var xft_color: xlib.XftColor = undefined;
232 - var render_color: xlib.XRenderColor = undefined;
233 - render_color.red = @intCast((color >> 16 & 0xff) * 257);
234 - render_color.green = @intCast((color >> 8 & 0xff) * 257);
235 - render_color.blue = @intCast((color & 0xff) * 257);
236 - render_color.alpha = 0xffff;
237 -
238 - const visual = xlib.XDefaultVisual(display, 0);
239 - const colormap = xlib.XDefaultColormap(display, 0);
240 -
241 - _ = xlib.XftColorAllocValue(display, visual, colormap, &render_color, &xft_color);
242 -
243 - xlib.XftDrawStringUtf8(self.xft_draw, &xft_color, self.font, x, y, text.ptr, @intCast(text.len));
244 -
245 - xlib.XftColorFree(display, visual, colormap, &xft_color);
246 - }
247 -
248 - fn text_width(self: *Bar, display: *xlib.Display, text: []const u8) i32 {
249 - if (self.font == null) return 0;
250 -
251 - var extents: xlib.XGlyphInfo = undefined;
252 - xlib.XftTextExtentsUtf8(display, self.font, text.ptr, @intCast(text.len), &extents);
253 - return extents.xOff;
254 - }
255 -
256 - pub fn handle_click(self: *Bar, click_x: i32, tags: []const []const u8) ?usize {
223 + /// Returns the index of the tag the user clicked on, or null if the
224 + /// click was outside the tag area.
225 + pub fn handle_click(self: *Bar, display: *xlib.Display, click_x: i32, config: config_mod.Config) ?usize {
257 226 var x_position: i32 = 0;
258 227 const padding: i32 = 8;
259 - const display = xlib.c.XOpenDisplay(null) orelse return null;
260 - defer _ = xlib.XCloseDisplay(display);
261 -
262 228 const monitor = self.monitor;
263 229 const current_tags = monitor.tagset[monitor.sel_tags];
264 - for (tags, 0..) |tag, index| {
230 +
231 + for (config.tags, 0..) |tag, index| {
265 232 const tag_mask = @as(u32, 1) << @intCast(index);
266 233 const is_selected = (current_tags & tag_mask) != 0;
267 234 const is_occupied = has_clients_on_tag(monitor, tag_mask);
@@ -279,59 +246,51 @@ pub const Bar = struct {
279 246 return null;
280 247 }
281 248
249 + /// Updates all blocks and marks the bar dirty if any block changed.
282 250 pub fn update_blocks(self: *Bar) void {
283 251 var changed = false;
284 252 for (self.blocks.items) |*block| {
285 - if (block.update()) {
286 - changed = true;
287 - }
288 - }
289 - if (changed) {
290 - self.needs_redraw = true;
253 + if (block.update()) changed = true;
291 254 }
255 + if (changed) self.needs_redraw = true;
292 256 }
293 257
294 - pub fn clear_blocks(self: *Bar) void {
295 - self.blocks.clearRetainingCapacity();
258 + fn fill_rect(self: *Bar, display: *xlib.Display, x: i32, y: i32, width: i32, height: i32, color: c_ulong) void {
259 + _ = xlib.XSetForeground(display, self.graphics_context, color);
260 + _ = xlib.XFillRectangle(display, self.pixmap, self.graphics_context, x, y, @intCast(width), @intCast(height));
296 261 }
297 -};
298 262
299 -fn has_clients_on_tag(monitor: *Monitor, tag_mask: u32) bool {
300 - var current = monitor.clients;
301 - while (current) |client| {
302 - if ((client.tags & tag_mask) != 0) {
303 - return true;
304 - }
305 - current = client.next;
306 - }
307 - return false;
308 -}
263 + fn draw_text(self: *Bar, display: *xlib.Display, x: i32, y: i32, text: []const u8, color: c_ulong) void {
264 + if (self.xft_draw == null or self.font == null) return;
309 265
310 -pub var bars: ?*Bar = null;
266 + var xft_color: xlib.XftColor = undefined;
267 + var render_color: xlib.XRenderColor = undefined;
268 + render_color.red = @intCast((color >> 16 & 0xff) * 257);
269 + render_color.green = @intCast((color >> 8 & 0xff) * 257);
270 + render_color.blue = @intCast((color & 0xff) * 257);
271 + render_color.alpha = 0xffff;
311 272
312 -pub fn create_bars(allocator: std.mem.Allocator, display: *xlib.Display, screen: c_int) void {
313 - var current_monitor = monitor_mod.monitors;
314 - while (current_monitor) |monitor| {
315 - const bar = Bar.create(allocator, display, screen, monitor, "monospace:size=10");
316 - if (bar) |created_bar| {
317 - bars = created_bar;
318 - }
319 - current_monitor = monitor.next;
273 + const visual = xlib.XDefaultVisual(display, 0);
274 + const colormap = xlib.XDefaultColormap(display, 0);
275 +
276 + _ = xlib.XftColorAllocValue(display, visual, colormap, &render_color, &xft_color);
277 + xlib.XftDrawStringUtf8(self.xft_draw, &xft_color, self.font, x, y, text.ptr, @intCast(text.len));
278 + xlib.XftColorFree(display, visual, colormap, &xft_color);
320 279 }
321 -}
322 280
323 -pub fn draw_bars(display: *xlib.Display, tags: []const []const u8) void {
324 - var current_monitor = monitor_mod.monitors;
325 - while (current_monitor) |monitor| {
326 - _ = monitor;
327 - if (bars) |bar| {
328 - bar.draw(display, tags, null);
329 - }
330 - current_monitor = if (current_monitor) |m| m.next else null;
281 + fn text_width(self: *Bar, display: *xlib.Display, text: []const u8) i32 {
282 + if (self.font == null) return 0;
283 +
284 + var extents: xlib.XGlyphInfo = undefined;
285 + xlib.XftTextExtentsUtf8(display, self.font, text.ptr, @intCast(text.len), &extents);
286 + return extents.xOff;
331 287 }
332 -}
288 +};
289 +
290 +// Bar list helpers >.<
333 291
334 -pub fn invalidate_bars() void {
292 +/// Marks all bars in the list as needing a redraw.
293 +pub fn invalidate_bars(bars: ?*Bar) void {
335 294 var current = bars;
336 295 while (current) |bar| {
337 296 bar.invalidate();
@@ -339,23 +298,31 @@ pub fn invalidate_bars() void {
339 298 }
340 299 }
341 300
342 -pub fn destroy_bars(allocator: std.mem.Allocator, display: *xlib.Display) void {
301 +/// Destroys all bars in the list and frees their resources.
302 +pub fn destroy_bars(bars: ?*Bar, allocator: std.mem.Allocator, display: *xlib.Display) void {
343 303 var current = bars;
344 304 while (current) |bar| {
345 305 const next = bar.next;
346 306 bar.destroy(allocator, display);
347 307 current = next;
348 308 }
349 - bars = null;
350 309 }
351 310
352 -pub fn window_to_bar(win: xlib.Window) ?*Bar {
311 +/// Returns the bar whose window matches `win`, or null.
312 +pub fn window_to_bar(bars: ?*Bar, win: xlib.Window) ?*Bar {
353 313 var current = bars;
354 314 while (current) |bar| {
355 - if (bar.window == win) {
356 - return bar;
357 - }
315 + if (bar.window == win) return bar;
358 316 current = bar.next;
359 317 }
360 318 return null;
361 319 }
320 +
321 +fn has_clients_on_tag(monitor: *Monitor, tag_mask: u32) bool {
322 + var current = monitor.clients;
323 + while (current) |client| {
324 + if ((client.tags & tag_mask) != 0) return true;
325 + current = client.next;
326 + }
327 + return false;
328 +}
+136 −366 src/main.zig Large diff collapsed, click to expand
@@ -1,20 +1,14 @@
1 1 const std = @import("std");
2 2 const VERSION = "v0.11.2";
3 -const Atoms = @import("x11/atoms.zig").Atoms;
4 -const kchord = @import("keyboard/chord.zig");
3 +const wm_mod = @import("wm.zig");
5 4 const display_mod = @import("x11/display.zig");
6 5 const events = @import("x11/events.zig");
7 6 const xlib = @import("x11/xlib.zig");
8 7 const client_mod = @import("client.zig");
9 8 const monitor_mod = @import("monitor.zig");
10 9 const tiling = @import("layouts/tiling.zig");
11 -const monocle = @import("layouts/monocle.zig");
12 -const floating = @import("layouts/floating.zig");
13 10 const scrolling = @import("layouts/scrolling.zig");
14 -const grid = @import("layouts/grid.zig");
15 -const animations = @import("animations.zig");
16 11 const bar_mod = @import("bar/bar.zig");
17 -const blocks_mod = @import("bar/blocks/blocks.zig");
18 12 const config_mod = @import("config/config.zig");
19 13 const lua = @import("config/lua.zig");
20 14 const overlay_mod = @import("overlay.zig");
@@ -22,50 +16,47 @@ const overlay_mod = @import("overlay.zig");
22 16 const Display = display_mod.Display;
23 17 const Client = client_mod.Client;
24 18 const Monitor = monitor_mod.Monitor;
19 +const WindowManager = wm_mod.WindowManager;
25 20
26 -var running: bool = true;
27 21 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
28 22
29 -var atoms: Atoms = undefined;
30 -var wm_check_window: xlib.Window = undefined;
31 -
32 -const Cursors = struct {
33 - normal: xlib.Cursor,
34 - resize: xlib.Cursor,
35 - move: xlib.Cursor,
36 -
37 - fn init(display: *Display) Cursors {
38 - return .{
39 - .normal = xlib.XCreateFontCursor(display.handle, xlib.XC_left_ptr),
40 - .resize = xlib.XCreateFontCursor(display.handle, xlib.XC_sizing),
41 - .move = xlib.XCreateFontCursor(display.handle, xlib.XC_fleur),
42 - };
43 - }
44 -};
45 -
46 -var cursors: Cursors = undefined;
47 -
48 23 const NormalState: c_long = 1;
49 24 const WithdrawnState: c_long = 0;
50 25 const IconicState: c_long = 3;
51 26 const IsViewable: c_int = 2;
52 27 const snap_distance: i32 = 32;
53 28
54 -var numlock_mask: c_uint = 0;
29 +// Remaining module-level state
55 30
31 +/// Config built from Lua, passed into WindowManager.init.
56 32 var config: config_mod.Config = undefined;
33 +/// Path to the loaded config file, kept for hot-reload.
57 34 var config_path_global: ?[]const u8 = null;
58 35
59 -/// Interim pointer to the X11 display, used only by `arrange` until the
60 -/// WindowManager struct refactor moves display ownership there properly.
36 +/// TODO(wm-refactor): move into WindowManager, waiting on handle_key_press
37 +/// and execute_action being converted to methods.
38 +var chord = @import("keyboard/chord.zig").ChordState{};
39 +
40 +/// TODO(wm-refactor): remove once all callers are WindowManager methods.
41 +/// Aliases wm.atoms so existing functions that take display but not wm
42 +/// can still reach atom values without requiring a full signature change.
43 +var atoms: @import("x11/atoms.zig").Atoms = undefined;
44 +
45 +/// TODO(wm-refactor): move into WindowManager, waiting on arrange() becoming
46 +/// a method so it can access self.display directly.
61 47 var wm_display: ?*Display = null;
62 48
63 -var scroll_animation: animations.Scroll_Animation = .{};
64 -var animation_config: animations.Animation_Config = .{ .duration_ms = 150, .easing = .ease_out };
49 +/// TODO(wm-refactor): remove once all callers are WindowManager methods.
50 +/// Used only by functions that need to invalidate bars but don't yet receive
51 +/// wm as a parameter.
52 +var wm_ptr: ?*WindowManager = null;
65 53
66 -var chord = kchord.ChordState{};
54 +/// TODO(wm-refactor): move into WindowManager.
55 +var numlock_mask: c_uint = 0;
67 56
68 -var keybind_overlay: ?*overlay_mod.Keybind_Overlay = null;
57 +/// File descriptor of the X11 connection, used in spawn_child_setup.
58 +/// Set from wm.x11_fd after init.
59 +var x11_fd: c_int = -1;
69 60
70 61 fn print_help() void {
71 62 std.debug.print(
@@ -98,6 +89,7 @@ fn get_config_path(allocator: std.mem.Allocator) ![]u8 {
98 89 const home = std.posix.getenv("HOME") orelse return error.CouldNotGetHomeDir;
99 90 break :blk try std.fs.path.join(allocator, &.{ home, ".config" });
100 91 };
92 + // TODO: wtf is this shit
101 93 defer if (std.posix.getenv("XDG_CONFIG_HOME") == null) allocator.free(config_home);
102 94
103 95 const config_path = try std.fs.path.join(allocator, &.{ config_home, "oxwm", "config.lua" });
@@ -201,7 +193,6 @@ pub fn main() !void {
201 193 std.debug.print("oxwm starting\n", .{});
202 194
203 195 config = config_mod.Config.init(allocator);
204 - defer config.deinit();
205 196
206 197 if (lua.init(&config)) {
207 198 const loaded = if (std.fs.cwd().statFile(config_path)) |_|
@@ -223,229 +214,34 @@ pub fn main() !void {
223 214 initialize_default_config();
224 215 }
225 216
226 - var display = Display.open() catch |err| {
227 - std.debug.print("failed to open display: {}\n", .{err});
217 + var wm = WindowManager.init(allocator, config, config_path_global) catch |err| {
218 + std.debug.print("failed to start window manager: {}\n", .{err});
228 219 return;
229 220 };
230 - defer display.close();
231 -
232 - x11_fd = xlib.XConnectionNumber(display.handle);
233 - wm_display = &display;
221 + defer wm.deinit();
234 222
235 - std.debug.print("display opened: screen={d} root=0x{x}\n", .{ display.screen, display.root });
236 - std.debug.print("screen size: {d}x{d}\n", .{ display.screen_width(), display.screen_height() });
237 -
238 - display.become_window_manager() catch |err| {
239 - std.debug.print("failed to become window manager: {}\n", .{err});
240 - return;
241 - };
223 + // Propagate values that transitional code in this file still reads
224 + // from module-level vars. These will be removed soon.
225 + x11_fd = wm.x11_fd;
226 + wm_display = &wm.display;
227 + wm_ptr = &wm;
228 + atoms = wm.atoms;
242 229
230 + std.debug.print("display opened: screen={d} root=0x{x}\n", .{ wm.display.screen, wm.display.root });
243 231 std.debug.print("successfully became window manager\n", .{});
244 -
245 - const atoms_result = Atoms.init(display.handle, display.root);
246 - atoms = atoms_result.atoms;
247 - wm_check_window = atoms_result.check_window;
248 232 std.debug.print("atoms initialized with EWMH support\n", .{});
249 233
250 - cursors = Cursors.init(&display);
251 - _ = xlib.XDefineCursor(display.handle, display.root, cursors.normal);
252 - monitor_mod.init(allocator);
253 - tiling.set_display(display.handle);
254 - tiling.set_screen_size(display.screen_width(), display.screen_height());
255 -
256 - setup_monitors(&display);
257 - setup_bars(allocator, &display);
258 - setup_overlay(allocator, &display);
259 - grab_keybinds(&display);
260 - scan_existing_windows(&display);
234 + grab_keybinds(&wm.display);
235 + scan_existing_windows(&wm.display);
261 236
262 237 try run_autostart_commands(allocator, config.autostart.items);
263 238 std.debug.print("entering event loop\n", .{});
264 - run_event_loop(&display);
265 -
266 - bar_mod.destroy_bars(allocator, display.handle);
267 -
268 - var mon = monitor_mod.monitors;
269 - while (mon) |m| {
270 - const next = m.next;
271 - monitor_mod.destroy(m);
272 - mon = next;
273 - }
274 -
275 - if (keybind_overlay) |overlay| {
276 - overlay.deinit(allocator);
277 - }
239 + run_event_loop(&wm);
278 240
279 241 lua.deinit();
280 242 std.debug.print("oxwm exiting\n", .{});
281 243 }
282 244
283 -fn setup_bars(allocator: std.mem.Allocator, display: *Display) void {
284 - var current_monitor = monitor_mod.monitors;
285 - var last_bar: ?*bar_mod.Bar = null;
286 -
287 - while (current_monitor) |monitor| {
288 - const bar = bar_mod.Bar.create(allocator, display.handle, display.screen, monitor, config);
289 - if (bar) |created_bar| {
290 - if (tiling.bar_height == 0) {
291 - tiling.set_bar_height(created_bar.height);
292 - }
293 -
294 - if (config.blocks.items.len > 0) {
295 - for (config.blocks.items) |cfg_block| {
296 - const block = config_block_to_bar_block(cfg_block);
297 - created_bar.add_block(block);
298 - }
299 - } else {
300 - created_bar.add_block(blocks_mod.Block.init_ram("", 5, 0x7aa2f7, true));
301 - created_bar.add_block(blocks_mod.Block.init_static(" | ", 0x666666, false));
302 - created_bar.add_block(blocks_mod.Block.init_datetime("", "%H:%M", 1, 0x0db9d7, true));
303 - }
304 -
305 - if (last_bar) |prev| {
306 - prev.next = created_bar;
307 - } else {
308 - bar_mod.bars = created_bar;
309 - }
310 - last_bar = created_bar;
311 - std.debug.print("bar created for monitor {d}\n", .{monitor.num});
312 - }
313 - current_monitor = monitor.next;
314 - }
315 -}
316 -
317 -fn setup_overlay(allocator: std.mem.Allocator, display: *Display) void {
318 - keybind_overlay = overlay_mod.Keybind_Overlay.init(display.handle, display.screen, display.root, config.font, allocator);
319 -}
320 -
321 -fn config_block_to_bar_block(cfg: config_mod.Block) blocks_mod.Block {
322 - return switch (cfg.block_type) {
323 - .static => blocks_mod.Block.init_static(cfg.format, cfg.color, cfg.underline),
324 - .datetime => blocks_mod.Block.init_datetime(cfg.format, cfg.datetime_format orelse "%H:%M", cfg.interval, cfg.color, cfg.underline),
325 - .ram => blocks_mod.Block.init_ram(cfg.format, cfg.interval, cfg.color, cfg.underline),
326 - .shell => blocks_mod.Block.init_shell(cfg.format, cfg.command orelse "", cfg.interval, cfg.color, cfg.underline),
327 - .battery => blocks_mod.Block.init_battery(
328 - cfg.format_charging orelse "",
329 - cfg.format_discharging orelse "",
330 - cfg.format_full orelse "",
331 - cfg.battery_name orelse "BAT0",
332 - cfg.interval,
333 - cfg.color,
334 - cfg.underline,
335 - ),
336 - .cpu_temp => blocks_mod.Block.init_cpu_temp(
337 - cfg.format,
338 - cfg.thermal_zone orelse "thermal_zone0",
339 - cfg.interval,
340 - cfg.color,
341 - cfg.underline,
342 - ),
343 - };
344 -}
345 -
346 -fn setup_monitors(display: *Display) void {
347 - std.debug.print("checking xinerama...\n", .{});
348 - if (xlib.XineramaIsActive(display.handle) != 0) {
349 - std.debug.print("xinerama is active!\n", .{});
350 - var screen_count: c_int = 0;
351 - const screens = xlib.XineramaQueryScreens(display.handle, &screen_count);
352 -
353 - if (screen_count > 0 and screens != null) {
354 - var prev_monitor: ?*Monitor = null;
355 - var index: usize = 0;
356 -
357 - while (index < @as(usize, @intCast(screen_count))) : (index += 1) {
358 - const screen = screens[index];
359 - const mon = monitor_mod.create() orelse continue;
360 -
361 - mon.num = @intCast(index);
362 - mon.mon_x = screen.x_org;
363 - mon.mon_y = screen.y_org;
364 - mon.mon_w = screen.width;
365 - mon.mon_h = screen.height;
366 - mon.win_x = screen.x_org;
367 - mon.win_y = screen.y_org;
368 - mon.win_w = screen.width;
369 - mon.win_h = screen.height;
370 - mon.lt[0] = &tiling.layout;
371 - mon.lt[1] = &monocle.layout;
372 - mon.lt[2] = &floating.layout;
373 - mon.lt[3] = &scrolling.layout;
374 - mon.lt[4] = &grid.layout;
375 - for (0..10) |i| {
376 - mon.pertag.ltidxs[i][0] = mon.lt[0];
377 - mon.pertag.ltidxs[i][1] = mon.lt[1];
378 - mon.pertag.ltidxs[i][2] = mon.lt[2];
379 - mon.pertag.ltidxs[i][3] = mon.lt[3];
380 - mon.pertag.ltidxs[i][4] = mon.lt[4];
381 - }
382 -
383 - init_monitor_gaps(mon);
384 -
385 - if (prev_monitor) |prev| {
386 - prev.next = mon;
387 - } else {
388 - monitor_mod.monitors = mon;
389 - }
390 - prev_monitor = mon;
391 -
392 - std.debug.print("monitor {d}: {d}x{d} at ({d},{d})\n", .{ index, mon.mon_w, mon.mon_h, mon.mon_x, mon.mon_y });
393 - }
394 -
395 - monitor_mod.selected_monitor = monitor_mod.monitors;
396 - _ = xlib.XFree(@ptrCast(screens));
397 - return;
398 - }
399 - } else {
400 - std.debug.print("xinerama not active, using single monitor\n", .{});
401 - }
402 -
403 - const mon = monitor_mod.create() orelse return;
404 - mon.mon_x = 0;
405 - mon.mon_y = 0;
406 - mon.mon_w = display.screen_width();
407 - mon.mon_h = display.screen_height();
408 - mon.win_x = 0;
409 - mon.win_y = 0;
410 - mon.win_w = display.screen_width();
411 - mon.win_h = display.screen_height();
412 - mon.lt[0] = &tiling.layout;
413 - mon.lt[1] = &monocle.layout;
414 - mon.lt[2] = &floating.layout;
415 - mon.lt[3] = &scrolling.layout;
416 - mon.lt[4] = &grid.layout;
417 - for (0..10) |i| {
418 - mon.pertag.ltidxs[i][0] = mon.lt[0];
419 - mon.pertag.ltidxs[i][1] = mon.lt[1];
420 - mon.pertag.ltidxs[i][2] = mon.lt[2];
421 - mon.pertag.ltidxs[i][3] = mon.lt[3];
422 - mon.pertag.ltidxs[i][4] = mon.lt[4];
423 - }
424 -
425 - init_monitor_gaps(mon);
426 -
427 - monitor_mod.monitors = mon;
428 - monitor_mod.selected_monitor = mon;
429 - std.debug.print("monitor created: {d}x{d}\n", .{ mon.mon_w, mon.mon_h });
430 -}
431 -
432 -fn init_monitor_gaps(mon: *Monitor) void {
433 - const any_gap_nonzero = config.gap_inner_h != 0 or config.gap_inner_v != 0 or
434 - config.gap_outer_h != 0 or config.gap_outer_v != 0;
435 -
436 - if (config.gaps_enabled and any_gap_nonzero) {
437 - mon.gap_inner_h = config.gap_inner_h;
438 - mon.gap_inner_v = config.gap_inner_v;
439 - mon.gap_outer_h = config.gap_outer_h;
440 - mon.gap_outer_v = config.gap_outer_v;
441 - } else {
442 - mon.gap_inner_h = 0;
443 - mon.gap_inner_v = 0;
444 - mon.gap_outer_h = 0;
445 - mon.gap_outer_v = 0;
446 - }
447 -}
448 -
449 245 fn make_keybind(mod: u32, key: u64, action: config_mod.Action) config_mod.Keybind {
450 246 var kb: config_mod.Keybind = .{ .action = action };
451 247 kb.keys[0] = .{ .mod_mask = mod, .keysym = key };
@@ -634,35 +430,34 @@ fn scan_existing_windows(display: *Display) void {
634 430 }
635 431 }
636 432
637 -fn run_event_loop(display: *Display) void {
638 - const fd = xlib.XConnectionNumber(display.handle);
433 +fn run_event_loop(wm: *WindowManager) void {
639 434 var fds = [_]std.posix.pollfd{
640 - .{ .fd = fd, .events = std.posix.POLL.IN, .revents = 0 },
435 + .{ .fd = wm.x11_fd, .events = std.posix.POLL.IN, .revents = 0 },
641 436 };
642 437
643 - _ = xlib.XSync(display.handle, xlib.False);
438 + _ = xlib.XSync(wm.display.handle, xlib.False);
644 439
645 - while (running) {
646 - while (xlib.XPending(display.handle) > 0) {
647 - var event = display.next_event();
648 - handle_event(display, &event);
440 + while (wm.running) {
441 + while (xlib.XPending(wm.display.handle) > 0) {
442 + var event = wm.display.next_event();
443 + handle_event(&wm.display, &event, wm);
649 444 }
650 445
651 - tick_animations();
446 + tick_animations(wm);
652 447
653 - var current_bar = bar_mod.bars;
448 + var current_bar = wm.bars;
654 449 while (current_bar) |bar| {
655 450 bar.update_blocks();
656 - bar.draw(display.handle, &config.tags, config);
451 + bar.draw(wm.display.handle, wm.config);
657 452 current_bar = bar.next;
658 453 }
659 454
660 - const poll_timeout: i32 = if (scroll_animation.is_active()) 16 else 1000;
455 + const poll_timeout: i32 = if (wm.scroll_animation.is_active()) 16 else 1000;
661 456 _ = std.posix.poll(&fds, poll_timeout) catch 0;
662 457 }
663 458 }
664 459
665 -fn handle_event(display: *Display, event: *xlib.XEvent) void {
460 +fn handle_event(display: *Display, event: *xlib.XEvent, wm: *WindowManager) void {
666 461 const event_type = events.get_event_type(event);
667 462
668 463 if (event_type == .button_press) {
@@ -672,15 +467,15 @@ fn handle_event(display: *Display, event: *xlib.XEvent) void {
672 467 switch (event_type) {
673 468 .map_request => handle_map_request(display, &event.xmaprequest),
674 469 .configure_request => handle_configure_request(display, &event.xconfigurerequest),
675 - .key_press => handle_key_press(display, &event.xkey),
470 + .key_press => handle_key_press(display, &event.xkey, wm),
676 471 .destroy_notify => handle_destroy_notify(display, &event.xdestroywindow),
677 472 .unmap_notify => handle_unmap_notify(display, &event.xunmap),
678 - .enter_notify => handle_enter_notify(display, &event.xcrossing),
473 + .enter_notify => handle_enter_notify(display, &event.xcrossing, wm),
679 474 .focus_in => handle_focus_in(display, &event.xfocus),
680 - .motion_notify => handle_motion_notify(display, &event.xmotion),
475 + .motion_notify => handle_motion_notify(display, &event.xmotion, wm),
681 476 .client_message => handle_client_message(display, &event.xclient),
682 - .button_press => handle_button_press(display, &event.xbutton),
683 - .expose => handle_expose(display, &event.xexpose),
477 + .button_press => handle_button_press(display, &event.xbutton, wm),
478 + .expose => handle_expose(display, &event.xexpose, wm),
684 479 .property_notify => handle_property_notify(display, &event.xproperty),
685 480 else => {},
686 481 }
@@ -843,42 +638,37 @@ fn handle_configure_request(display: *Display, event: *xlib.XConfigureRequestEve
843 638 _ = xlib.XSync(display.handle, xlib.False);
844 639 }
845 640
846 -fn reset_chord_state(display_handle: *xlib.Display) void {
847 - chord.reset(display_handle);
848 -}
849 -
850 -fn handle_key_press(display: *Display, event: *xlib.XKeyEvent) void {
641 +fn handle_key_press(display: *Display, event: *xlib.XKeyEvent, wm: *WindowManager) void {
851 642 const keysym = xlib.XKeycodeToKeysym(display.handle, @intCast(event.keycode), 0);
852 643
853 - if (keybind_overlay) |overlay| {
854 - if (overlay.handle_key(keysym)) {
855 - return;
856 - }
644 + if (wm.overlay) |overlay| {
645 + if (overlay.handle_key(keysym)) return;
857 646 }
858 647
859 648 const clean_state = event.state & ~@as(c_uint, xlib.LockMask | xlib.Mod2Mask);
860 - const current_time = std.time.milliTimestamp();
861 649
862 - if (chord.index > 0 and (current_time - chord.last_timestamp) > kchord.timeout_ms) {
863 - reset_chord_state(display.handle);
650 + if (wm.chord.is_timed_out()) {
651 + wm.chord.reset(display.handle);
864 652 }
865 653
866 - _ = chord.push(.{ .mod_mask = clean_state, .keysym = keysym });
654 + _ = wm.chord.push(.{ .mod_mask = clean_state, .keysym = keysym });
867 655
868 656 for (config.keybinds.items) |keybind| {
869 657 if (keybind.key_count == 0) continue;
870 658
871 - if (keybind.key_count == chord.index) {
659 + if (keybind.key_count == wm.chord.index) {
872 660 var matches = true;
873 661 for (0..keybind.key_count) |i| {
874 - if (chord.keys[i].keysym != keybind.keys[i].keysym or chord.keys[i].mod_mask != keybind.keys[i].mod_mask) {
662 + if (wm.chord.keys[i].keysym != keybind.keys[i].keysym or
663 + wm.chord.keys[i].mod_mask != keybind.keys[i].mod_mask)
664 + {
875 665 matches = false;
876 666 break;
877 667 }
878 668 }
879 669 if (matches) {
880 - execute_action(display, keybind.action, keybind.int_arg, keybind.str_arg);
881 - reset_chord_state(display.handle);
670 + execute_action(display, keybind.action, keybind.int_arg, keybind.str_arg, wm);
671 + wm.chord.reset(display.handle);
882 672 return;
883 673 }
884 674 }
@@ -886,10 +676,12 @@ fn handle_key_press(display: *Display, event: *xlib.XKeyEvent) void {
886 676
887 677 var has_partial_match = false;
888 678 for (config.keybinds.items) |keybind| {
889 - if (keybind.key_count > chord.index) {
679 + if (keybind.key_count > wm.chord.index) {
890 680 var matches = true;
891 - for (0..chord.index) |i| {
892 - if (chord.keys[i].keysym != keybind.keys[i].keysym or chord.keys[i].mod_mask != keybind.keys[i].mod_mask) {
681 + for (0..wm.chord.index) |i| {
682 + if (wm.chord.keys[i].keysym != keybind.keys[i].keysym or
683 + wm.chord.keys[i].mod_mask != keybind.keys[i].mod_mask)
684 + {
893 685 matches = false;
894 686 break;
895 687 }
@@ -902,32 +694,30 @@ fn handle_key_press(display: *Display, event: *xlib.XKeyEvent) void {
902 694 }
903 695
904 696 if (has_partial_match) {
905 - chord.grab_keyboard(display.handle, display.root);
906 - } else if (!has_partial_match) {
907 - reset_chord_state(display.handle);
697 + wm.chord.grab_keyboard(display.handle, display.root);
698 + } else {
699 + wm.chord.reset(display.handle);
908 700 }
909 701 }
910 702
911 -fn execute_action(display: *Display, action: config_mod.Action, int_arg: i32, str_arg: ?[]const u8) void {
703 +fn execute_action(display: *Display, action: config_mod.Action, int_arg: i32, str_arg: ?[]const u8, wm: *WindowManager) void {
912 704 switch (action) {
913 705 .spawn_terminal => spawn_terminal(),
914 706 .spawn => {
915 - if (str_arg) |cmd| {
916 - spawn_command(cmd);
917 - }
707 + if (str_arg) |cmd| spawn_command(cmd);
918 708 },
919 709 .kill_client => kill_focused(display),
920 710 .quit => {
921 711 std.debug.print("quit keybind pressed\n", .{});
922 - running = false;
712 + wm.running = false;
923 713 },
924 - .reload_config => reload_config(display),
925 - .restart => reload_config(display),
714 + .reload_config => reload_config(display, wm),
715 + .restart => reload_config(display, wm),
926 716 .show_keybinds => {
927 - if (keybind_overlay) |overlay| {
928 - const mon = monitor_mod.selected_monitor orelse monitor_mod.monitors;
717 + if (wm.overlay) |overlay| {
718 + const mon = wm.selected_monitor orelse wm.monitors;
929 719 if (mon) |m| {
930 - overlay.toggle(m.mon_x, m.mon_y, m.mon_w, m.mon_h, &config);
720 + overlay.toggle(m.mon_x, m.mon_y, m.mon_w, m.mon_h, &wm.config);
931 721 }
932 722 }
933 723 },
@@ -967,16 +757,12 @@ fn execute_action(display: *Display, action: config_mod.Action, int_arg: i32, st
967 757 },
968 758 .focus_monitor => focusmon(display, int_arg),
969 759 .send_to_monitor => sendmon(display, int_arg),
970 - .scroll_left => {
971 - scroll_layout(-1);
972 - },
973 - .scroll_right => {
974 - scroll_layout(1);
975 - },
760 + .scroll_left => scroll_layout(-1, wm),
761 + .scroll_right => scroll_layout(1, wm),
976 762 }
977 763 }
978 764
979 -fn reload_config(display: *Display) void {
765 +fn reload_config(display: *Display, wm: *WindowManager) void {
980 766 std.debug.print("reloading config...\n", .{});
981 767
982 768 ungrab_keybinds(display);
@@ -1005,27 +791,19 @@ fn reload_config(display: *Display) void {
1005 791 initialize_default_config();
1006 792 }
1007 793
1008 - bar_mod.destroy_bars(gpa.allocator(), display.handle);
1009 - setup_bars(gpa.allocator(), display);
1010 - rebuild_bar_blocks();
794 + bar_mod.destroy_bars(wm.bars, gpa.allocator(), display.handle);
795 + wm.bars = null;
796 + wm.setup_bars();
797 + rebuild_bar_blocks(wm);
1011 798
1012 799 grab_keybinds(display);
1013 800 }
1014 801
1015 -fn rebuild_bar_blocks() void {
1016 - var current_bar = bar_mod.bars;
802 +fn rebuild_bar_blocks(wm: *WindowManager) void {
803 + var current_bar = wm.bars;
1017 804 while (current_bar) |bar| {
1018 805 bar.clear_blocks();
1019 - if (config.blocks.items.len > 0) {
1020 - for (config.blocks.items) |cfg_block| {
1021 - const block = config_block_to_bar_block(cfg_block);
1022 - bar.add_block(block);
1023 - }
1024 - } else {
1025 - bar.add_block(blocks_mod.Block.init_ram("", 5, 0x7aa2f7, true));
1026 - bar.add_block(blocks_mod.Block.init_static(" | ", 0x666666, false));
1027 - bar.add_block(blocks_mod.Block.init_datetime("", "%H:%M", 1, 0x0db9d7, true));
1028 - }
806 + wm.populate_bar_blocks(bar);
1029 807 current_bar = bar.next;
1030 808 }
1031 809 }
@@ -1034,10 +812,6 @@ fn ungrab_keybinds(display: *Display) void {
1034 812 _ = xlib.XUngrabKey(display.handle, xlib.AnyKey, xlib.AnyModifier, display.root);
1035 813 }
1036 814
1037 -/// File descriptor for the X11 connection. Set once after the display is
1038 -/// opened and used only in post-fork child setup to close the inherited fd.
1039 -var x11_fd: c_int = -1;
1040 -
1041 815 fn spawn_child_setup() void {
1042 816 _ = std.c.setsid();
1043 817 if (x11_fd >= 0) std.posix.close(@intCast(x11_fd));
@@ -1173,7 +947,7 @@ fn toggle_view(display: *Display, tag_mask: u32) void {
1173 947
1174 948 focus_top_client(display, monitor);
1175 949 arrange(monitor);
1176 - bar_mod.invalidate_bars();
950 + if (wm_ptr) |wm| wm.invalidate_bars();
1177 951 }
1178 952 }
1179 953
@@ -1185,7 +959,7 @@ fn toggle_client_tag(display: *Display, tag_mask: u32) void {
1185 959 client.tags = new_tags;
1186 960 focus_top_client(display, monitor);
1187 961 arrange(monitor);
1188 - bar_mod.invalidate_bars();
962 + if (wm_ptr) |wm| wm.invalidate_bars();
1189 963 }
1190 964 }
1191 965
@@ -1309,7 +1083,7 @@ fn view(display: *Display, tag_mask: u32) void {
1309 1083
1310 1084 focus_top_client(display, monitor);
1311 1085 arrange(monitor);
1312 - bar_mod.invalidate_bars();
1086 + if (wm_ptr) |wm| wm.invalidate_bars();
1313 1087 std.debug.print("view: tag_mask={d}\n", .{monitor.tagset[monitor.sel_tags]});
1314 1088 }
1315 1089
@@ -1365,7 +1139,7 @@ fn tag_client(display: *Display, tag_mask: u32) void {
1365 1139 client.tags = tag_mask;
1366 1140 focus_top_client(display, monitor);
1367 1141 arrange(monitor);
1368 - bar_mod.invalidate_bars();
1142 + if (wm_ptr) |wm| wm.invalidate_bars();
1369 1143 std.debug.print("tag_client: window=0x{x} tag_mask={d}\n", .{ client.window, tag_mask });
1370 1144 }
1371 1145
@@ -1485,7 +1259,7 @@ fn cycle_layout() void {
1485 1259 monitor.scroll_offset = 0;
1486 1260 }
1487 1261 arrange(monitor);
1488 - bar_mod.invalidate_bars();
1262 + if (wm_ptr) |wm| wm.invalidate_bars();
1489 1263 if (monitor.lt[monitor.sel_lt]) |layout| {
1490 1264 std.debug.print("cycle_layout: {s}\n", .{layout.symbol});
1491 1265 }
@@ -1516,7 +1290,7 @@ fn set_layout(layout_name: ?[]const u8) void {
1516 1290 monitor.scroll_offset = 0;
1517 1291 }
1518 1292 arrange(monitor);
1519 - bar_mod.invalidate_bars();
1293 + if (wm_ptr) |wm| wm.invalidate_bars();
1520 1294 if (monitor.lt[monitor.sel_lt]) |layout| {
1521 1295 std.debug.print("set_layout: {s}\n", .{layout.symbol});
1522 1296 }
@@ -1530,7 +1304,7 @@ fn set_layout_index(index: u32) void {
1530 1304 monitor.scroll_offset = 0;
1531 1305 }
1532 1306 arrange(monitor);
1533 - bar_mod.invalidate_bars();
1307 + if (wm_ptr) |wm| wm.invalidate_bars();
1534 1308 if (monitor.lt[monitor.sel_lt]) |layout| {
1535 1309 std.debug.print("set_layout_index: {s}\n", .{layout.symbol});
1536 1310 }
@@ -1591,7 +1365,7 @@ fn snap_y(client: *Client, new_y: i32, monitor: *Monitor) i32 {
1591 1365 return new_y;
1592 1366 }
1593 1367
1594 -fn movemouse(display: *Display) void {
1368 +fn movemouse(display: *Display, wm: *WindowManager) void {
1595 1369 const monitor = monitor_mod.selected_monitor orelse return;
1596 1370 const client = monitor.sel orelse return;
1597 1371
@@ -1622,7 +1396,7 @@ fn movemouse(display: *Display) void {
1622 1396 xlib.GrabModeAsync,
1623 1397 xlib.GrabModeAsync,
1624 1398 xlib.None,
1625 - cursors.move,
1399 + wm.cursors.move,
1626 1400 xlib.CurrentTime,
1627 1401 );
1628 1402
@@ -1698,7 +1472,7 @@ fn movemouse(display: *Display) void {
1698 1472 }
1699 1473 }
1700 1474
1701 -fn resizemouse(display: *Display) void {
1475 +fn resizemouse(display: *Display, wm: *WindowManager) void {
1702 1476 const monitor = monitor_mod.selected_monitor orelse return;
1703 1477 const client = monitor.sel orelse return;
1704 1478
@@ -1720,7 +1494,7 @@ fn resizemouse(display: *Display) void {
1720 1494 xlib.GrabModeAsync,
1721 1495 xlib.GrabModeAsync,
1722 1496 xlib.None,
1723 - cursors.resize,
1497 + wm.cursors.resize,
1724 1498 xlib.CurrentTime,
1725 1499 );
1726 1500
@@ -1771,12 +1545,11 @@ fn resizemouse(display: *Display) void {
1771 1545 arrange(monitor);
1772 1546 }
1773 1547
1774 -fn handle_expose(display: *Display, event: *xlib.XExposeEvent) void {
1548 +fn handle_expose(display: *Display, event: *xlib.XExposeEvent, wm: *WindowManager) void {
1775 1549 if (event.count != 0) return;
1776 -
1777 - if (bar_mod.window_to_bar(event.window)) |bar| {
1550 + if (bar_mod.window_to_bar(wm.bars, event.window)) |bar| {
1778 1551 bar.invalidate();
1779 - bar.draw(display.handle, &config.tags, config);
1552 + bar.draw(display.handle, wm.config);
1780 1553 }
1781 1554 }
1782 1555
@@ -1792,7 +1565,7 @@ fn clean_mask(mask: c_uint) c_uint {
1792 1565 return mask & ~(lock | numlock_mask) & (shift | ctrl | mod1 | mod2 | mod3 | mod4 | mod5);
1793 1566 }
1794 1567
1795 -fn handle_button_press(display: *Display, event: *xlib.XButtonEvent) void {
1568 +fn handle_button_press(display: *Display, event: *xlib.XButtonEvent, wm: *WindowManager) void {
1796 1569 std.debug.print("button_press: window=0x{x} subwindow=0x{x}\n", .{ event.window, event.subwindow });
1797 1570
1798 1571 const clicked_monitor = monitor_mod.window_to_monitor(display.handle, display.root, event.window);
@@ -1806,8 +1579,8 @@ fn handle_button_press(display: *Display, event: *xlib.XButtonEvent) void {
1806 1579 }
1807 1580 }
1808 1581
1809 - if (bar_mod.window_to_bar(event.window)) |bar| {
1810 - const clicked_tag = bar.handle_click(event.x, &config.tags);
1582 + if (bar_mod.window_to_bar(wm.bars, event.window)) |bar| {
1583 + const clicked_tag = bar.handle_click(display.handle, event.x, wm.config);
1811 1584 if (clicked_tag) |tag_index| {
1812 1585 const tag_mask: u32 = @as(u32, 1) << @intCast(tag_index);
1813 1586 view(display, tag_mask);
@@ -1830,8 +1603,8 @@ fn handle_button_press(display: *Display, event: *xlib.XButtonEvent) void {
1830 1603 const button_clean_mask = clean_mask(button.mod_mask);
1831 1604 if (clean_state == button_clean_mask and event.button == button.button) {
1832 1605 switch (button.action) {
1833 - .move_mouse => movemouse(display),
1834 - .resize_mouse => resizemouse(display),
1606 + .move_mouse => movemouse(display, wm),
1607 + .resize_mouse => resizemouse(display, wm),
1835 1608 .toggle_floating => {
1836 1609 if (click_client) |found_client| {
1837 1610 found_client.is_floating = !found_client.is_floating;
@@ -1933,10 +1706,11 @@ fn unmanage(display: *Display, client: *Client) void {
1933 1706
1934 1707 client_mod.destroy(gpa.allocator(), client);
1935 1708 update_client_list(display);
1936 - bar_mod.invalidate_bars();
1709 + if (wm_ptr) |wm| wm.invalidate_bars();
1937 1710 }
1938 1711
1939 -fn handle_enter_notify(display: *Display, event: *xlib.XCrossingEvent) void {
1712 +fn handle_enter_notify(display: *Display, event: *xlib.XCrossingEvent, wm: *WindowManager) void {
1713 + _ = wm;
1940 1714 if ((event.mode != xlib.NotifyNormal or event.detail == xlib.NotifyInferior) and event.window != display.root) {
1941 1715 return;
1942 1716 }
@@ -1977,22 +1751,18 @@ fn set_focus(display: *Display, client: *Client) void {
1977 1751 _ = send_event(display, client, atoms.wm_take_focus);
1978 1752 }
1979 1753
1980 -var last_motion_monitor: ?*Monitor = null;
1981 -
1982 -fn handle_motion_notify(display: *Display, event: *xlib.XMotionEvent) void {
1983 - if (event.window != display.root) {
1984 - return;
1985 - }
1754 +fn handle_motion_notify(display: *Display, event: *xlib.XMotionEvent, wm: *WindowManager) void {
1755 + if (event.window != display.root) return;
1986 1756
1987 1757 const target_mon = monitor_mod.rect_to_monitor(event.x_root, event.y_root, 1, 1);
1988 - if (target_mon != last_motion_monitor and last_motion_monitor != null) {
1758 + if (target_mon != wm.last_motion_monitor and wm.last_motion_monitor != null) {
1989 1759 if (monitor_mod.selected_monitor) |selmon| {
1990 1760 unfocus_client(display, selmon.sel, true);
1991 1761 }
1992 1762 monitor_mod.selected_monitor = target_mon;
1993 1763 focus(display, null);
1994 1764 }
1995 - last_motion_monitor = target_mon;
1765 + wm.last_motion_monitor = target_mon;
1996 1766 }
1997 1767
1998 1768 fn handle_property_notify(display: *Display, event: *xlib.XPropertyEvent) void {
@@ -2016,7 +1786,7 @@ fn handle_property_notify(display: *Display, event: *xlib.XPropertyEvent) void {
2016 1786 client.hints_valid = false;
2017 1787 } else if (event.atom == xlib.XA_WM_HINTS) {
2018 1788 update_wm_hints(display, client);
2019 - bar_mod.invalidate_bars();
1789 + if (wm_ptr) |wm| wm.invalidate_bars();
2020 1790 } else if (event.atom == xlib.XA_WM_NAME or event.atom == atoms.net_wm_name) {
2021 1791 update_title(display, client);
2022 1792 } else if (event.atom == atoms.net_wm_window_type) {
@@ -2140,15 +1910,15 @@ fn focus(display: *Display, target_client: ?*Client) void {
2140 1910
2141 1911 if (focus_client) |client| {
2142 1912 if (is_scrolling_layout(current_selmon)) {
2143 - scroll_to_window(client, true);
1913 + scroll_to_window(client, true, wm_ptr orelse return);
2144 1914 }
2145 1915 }
2146 1916
2147 - bar_mod.invalidate_bars();
1917 + if (wm_ptr) |wm| wm.invalidate_bars();
2148 1918 }
2149 1919
2150 1920 fn restack(display: *Display, monitor: *Monitor) void {
2151 - bar_mod.invalidate_bars();
1921 + if (wm_ptr) |wm| wm.invalidate_bars();
2152 1922 const selected_client = monitor.sel orelse return;
2153 1923
2154 1924 if (selected_client.is_floating or monitor.lt[monitor.sel_lt] == null) {
@@ -2177,7 +1947,7 @@ fn restack(display: *Display, monitor: *Monitor) void {
2177 1947 }
2178 1948
2179 1949 fn arrange(monitor: *Monitor) void {
2180 - // TODO: display will become a WindowManager field; for now
1950 + // TODO(wm-refactor): display will become a WindowManager field; for now
2181 1951 // we use a module-level pointer set once after the display is opened.
2182 1952 if (wm_display) |display| {
2183 1953 showhide(display, monitor);
@@ -2192,11 +1962,11 @@ fn arrange(monitor: *Monitor) void {
2192 1962 }
2193 1963 }
2194 1964
2195 -fn tick_animations() void {
2196 - if (!scroll_animation.is_active()) return;
1965 +fn tick_animations(wm: *WindowManager) void {
1966 + if (!wm.scroll_animation.is_active()) return;
2197 1967
2198 - const monitor = monitor_mod.selected_monitor orelse return;
2199 - if (scroll_animation.update()) |new_offset| {
1968 + const monitor = wm.selected_monitor orelse return;
1969 + if (wm.scroll_animation.update()) |new_offset| {
2200 1970 monitor.scroll_offset = new_offset;
2201 1971 arrange(monitor);
2202 1972 }
@@ -2209,32 +1979,32 @@ fn is_scrolling_layout(monitor: *Monitor) bool {
2209 1979 return false;
2210 1980 }
2211 1981
2212 -fn scroll_layout(direction: i32) void {
2213 - const monitor = monitor_mod.selected_monitor orelse return;
1982 +fn scroll_layout(direction: i32, wm: *WindowManager) void {
1983 + const monitor = wm.selected_monitor orelse return;
2214 1984 if (!is_scrolling_layout(monitor)) return;
2215 1985
2216 1986 const scroll_step = scrolling.get_scroll_step(monitor);
2217 1987 const max_scroll = scrolling.get_max_scroll(monitor);
2218 1988
2219 - const current = if (scroll_animation.is_active())
2220 - scroll_animation.target()
1989 + const current = if (wm.scroll_animation.is_active())
1990 + wm.scroll_animation.target()
2221 1991 else
2222 1992 monitor.scroll_offset;
2223 1993
2224 1994 var target = current + direction * scroll_step;
2225 1995 target = @max(0, @min(target, max_scroll));
2226 1996
2227 - scroll_animation.start(monitor.scroll_offset, target, animation_config);
1997 + wm.scroll_animation.start(monitor.scroll_offset, target, wm.animation_config);
2228 1998 }
2229 1999
2230 -fn scroll_to_window(client: *Client, animate: bool) void {
2000 +fn scroll_to_window(client: *Client, animate: bool, wm: *WindowManager) void {
2231 2001 const monitor = client.monitor orelse return;
2232 2002 if (!is_scrolling_layout(monitor)) return;
2233 2003
2234 2004 const target = scrolling.get_target_scroll_for_window(monitor, client);
2235 2005
2236 2006 if (animate) {
2237 - scroll_animation.start(monitor.scroll_offset, target, animation_config);
2007 + wm.scroll_animation.start(monitor.scroll_offset, target, wm.animation_config);
2238 2008 } else {
2239 2009 monitor.scroll_offset = target;
2240 2010 arrange(monitor);
+34 −17 src/monitor.zig
@@ -7,6 +7,7 @@ pub const Layout = struct {
7 7 arrange_fn: ?*const fn (*Monitor) void,
8 8 };
9 9
10 +// TODO: Make clearer, document? refactor?
10 11 pub const Pertag = struct {
11 12 curtag: u32 = 1,
12 13 prevtag: u32 = 1,
@@ -17,6 +18,7 @@ pub const Pertag = struct {
17 18 showbars: [10]bool = [_]bool{true} ** 10,
18 19 };
19 20
21 +// TODO: Make clearer, document? refactor?
20 22 pub const Monitor = struct {
21 23 lt_symbol: [16]u8 = std.mem.zeroes([16]u8),
22 24 mfact: f32 = 0.55,
@@ -46,58 +48,61 @@ pub const Monitor = struct {
46 48 stack: ?*Client = null,
47 49 next: ?*Monitor = null,
48 50 bar_win: xlib.Window = 0,
49 - lt: [5]?*const Layout = .{ null, null, null, null, null },
50 - pertag: Pertag = Pertag{},
51 + lt: [5]?*const Layout = .{null} ** 5,
52 + pertag: Pertag = .{},
51 53 };
52 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 +
53 60 pub var monitors: ?*Monitor = null;
54 61 pub var selected_monitor: ?*Monitor = null;
55 62
56 63 var allocator: std.mem.Allocator = undefined;
57 64
65 +/// Must be called once before any other function in this module.
58 66 pub fn init(alloc: std.mem.Allocator) void {
59 67 allocator = alloc;
60 68 }
61 69
70 +/// Allocates and zero-initialises a new `Monitor`.
62 71 pub fn create() ?*Monitor {
63 72 const mon = allocator.create(Monitor) catch return null;
64 73 mon.* = Monitor{};
65 74 return mon;
66 75 }
67 76
77 +/// Frees a monitor previously returned by `create`.
68 78 pub fn destroy(mon: *Monitor) void {
69 79 allocator.destroy(mon);
70 80 }
71 81
72 -var root_window: xlib.Window = 0;
73 -var display_handle: ?*xlib.Display = null;
74 -
75 -pub fn set_root_window(root: xlib.Window, display: *xlib.Display) void {
76 - root_window = root;
77 - display_handle = display;
78 -}
79 -
80 -pub fn window_to_monitor(win: xlib.Window) ?*Monitor {
81 - if (win == root_window and display_handle != null) {
82 +/// Returns the monitor whose bar window or client matches `win`, falling
83 +/// 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) {
82 89 var root_x: c_int = undefined;
83 90 var root_y: c_int = undefined;
84 91 var dummy_win: xlib.Window = undefined;
85 92 var dummy_int: c_int = undefined;
86 93 var dummy_uint: c_uint = undefined;
87 - if (xlib.XQueryPointer(display_handle.?, root_window, &dummy_win, &dummy_win, &root_x, &root_y, &dummy_int, &dummy_int, &dummy_uint) != 0) {
94 + if (xlib.XQueryPointer(display, root, &dummy_win, &dummy_win, &root_x, &root_y, &dummy_int, &dummy_int, &dummy_uint) != 0) {
88 95 return rect_to_monitor(root_x, root_y, 1, 1);
89 96 }
90 97 }
91 98
92 99 var current = monitors;
93 100 while (current) |monitor| {
94 - if (monitor.bar_win == win) {
95 - return monitor;
96 - }
101 + if (monitor.bar_win == win) return monitor;
97 102 current = monitor.next;
98 103 }
99 104
100 - const client = @import("client.zig").window_to_client(win);
105 + const client = @import("client.zig").window_to_client(monitors, win);
101 106 if (client) |found_client| {
102 107 return found_client.monitor;
103 108 }
@@ -105,6 +110,8 @@ pub fn window_to_monitor(win: xlib.Window) ?*Monitor {
105 110 return selected_monitor;
106 111 }
107 112
113 +/// Returns the monitor with the greatest intersection area with the given
114 +/// rectangle, or `selected_monitor` if no intersection is found.
108 115 pub fn rect_to_monitor(x: i32, y: i32, width: i32, height: i32) ?*Monitor {
109 116 var result = selected_monitor;
110 117 var max_area: i32 = 0;
@@ -123,6 +130,14 @@ pub fn rect_to_monitor(x: i32, y: i32, width: i32, height: i32) ?*Monitor {
123 130 return result;
124 131 }
125 132
133 +/// Returns the next or previous monitor relative to `selected_monitor`.
134 +///
135 +/// Positive `direction` moves forward through the linked list (wrapping to
136 +/// the head); negative moves backward (wrapping to the tail).
137 +///
138 +// TODO:
139 +// - Change direction to an enum/enum_literal
140 +// - Rename function
126 141 pub fn dir_to_monitor(direction: i32) ?*Monitor {
127 142 var target: ?*Monitor = null;
128 143
@@ -132,6 +147,7 @@ pub fn dir_to_monitor(direction: i32) ?*Monitor {
132 147 target = monitors;
133 148 }
134 149 } else if (selected_monitor == monitors) {
150 + // Already at head, walk to tail.
135 151 var last = monitors;
136 152 while (last) |iter| {
137 153 if (iter.next == null) {
@@ -141,6 +157,7 @@ pub fn dir_to_monitor(direction: i32) ?*Monitor {
141 157 last = iter.next;
142 158 }
143 159 } else {
160 + // Walk until we find the node just before selected_monitor.
144 161 var previous = monitors;
145 162 while (previous) |iter| {
146 163 if (iter.next == selected_monitor) {
+366 −0 src/wm.zig new file mode 100644
@@ -0,0 +1,366 @@
1 +const std = @import("std");
2 +const mem = std.mem;
3 +
4 +const display_mod = @import("x11/display.zig");
5 +const xlib = @import("x11/xlib.zig");
6 +const events = @import("x11/events.zig");
7 +const atoms_mod = @import("x11/atoms.zig");
8 +const chord_mod = @import("keyboard/chord.zig");
9 +const client_mod = @import("client.zig");
10 +const monitor_mod = @import("monitor.zig");
11 +const bar_mod = @import("bar/bar.zig");
12 +const blocks_mod = @import("bar/blocks/blocks.zig");
13 +const config_mod = @import("config/config.zig");
14 +const overlay_mod = @import("overlay.zig");
15 +const animations = @import("animations.zig");
16 +const tiling = @import("layouts/tiling.zig");
17 +const monocle = @import("layouts/monocle.zig");
18 +const floating = @import("layouts/floating.zig");
19 +const scrolling = @import("layouts/scrolling.zig");
20 +const grid = @import("layouts/grid.zig");
21 +
22 +const Display = display_mod.Display;
23 +const Atoms = atoms_mod.Atoms;
24 +const ChordState = chord_mod.ChordState;
25 +const Client = client_mod.Client;
26 +const Monitor = monitor_mod.Monitor;
27 +const Bar = bar_mod.Bar;
28 +const Config = config_mod.Config;
29 +
30 +pub const Cursors = struct {
31 + normal: xlib.Cursor,
32 + resize: xlib.Cursor,
33 + move: xlib.Cursor,
34 +
35 + pub fn init(display: *Display) Cursors {
36 + return .{
37 + .normal = xlib.XCreateFontCursor(display.handle, xlib.XC_left_ptr),
38 + .resize = xlib.XCreateFontCursor(display.handle, xlib.XC_sizing),
39 + .move = xlib.XCreateFontCursor(display.handle, xlib.XC_fleur),
40 + };
41 + }
42 +};
43 +
44 +pub const WindowManager = struct {
45 + allocator: mem.Allocator,
46 +
47 + /// The connection to the X server.
48 + display: Display,
49 + x11_fd: c_int,
50 + /// Invisible 1×1 window used to satisfy the _NET_SUPPORTING_WM_CHECK
51 + /// EWMH convention.
52 + wm_check_window: xlib.Window,
53 +
54 + atoms: Atoms,
55 + cursors: Cursors,
56 + numlock_mask: c_uint,
57 +
58 + config: Config,
59 + /// Path to the config file that was loaded.
60 + /// Null if using default config.
61 + config_path: ?[]const u8,
62 +
63 + /// Head of the linked list of all managed monitors.
64 + monitors: ?*Monitor,
65 + /// The monitor that currently has input focus.
66 + selected_monitor: ?*Monitor,
67 +
68 + /// Head of the linked list of status bars (one per monitor).
69 + bars: ?*Bar,
70 +
71 + chord: ChordState,
72 +
73 + overlay: ?*overlay_mod.Keybind_Overlay,
74 +
75 + scroll_animation: animations.Scroll_Animation,
76 + animation_config: animations.Animation_Config,
77 +
78 + running: bool,
79 + last_motion_monitor: ?*Monitor,
80 +
81 + /// Initialises the window manager
82 + ///
83 + /// Returns an error if the display cannot be opened or another WM is
84 + /// already running.
85 + pub fn init(allocator: mem.Allocator, config: Config, config_path: ?[]const u8) !WindowManager {
86 + var display = try Display.open();
87 + errdefer display.close();
88 +
89 + try display.become_window_manager();
90 +
91 + const x11_fd = xlib.XConnectionNumber(display.handle);
92 +
93 + const atoms_result = Atoms.init(display.handle, display.root);
94 + const cursors = Cursors.init(&display);
95 + _ = xlib.XDefineCursor(display.handle, display.root, cursors.normal);
96 +
97 + tiling.set_display(display.handle);
98 + tiling.set_screen_size(display.screen_width(), display.screen_height());
99 +
100 + monitor_mod.init(allocator);
101 + // client_mod.init(allocator);
102 +
103 + var wm = WindowManager{
104 + .allocator = allocator,
105 + .display = display,
106 + .x11_fd = x11_fd,
107 + .wm_check_window = atoms_result.check_window,
108 + .atoms = atoms_result.atoms,
109 + .cursors = cursors,
110 + .numlock_mask = 0,
111 + .config = config,
112 + .config_path = config_path,
113 + .monitors = null,
114 + .selected_monitor = null,
115 + .bars = null,
116 + .chord = .{},
117 + .overlay = null,
118 + .scroll_animation = .{},
119 + .animation_config = .{ .duration_ms = 150, .easing = .ease_out },
120 + .running = true,
121 + .last_motion_monitor = null,
122 + };
123 +
124 + wm.setup_monitors();
125 + wm.setup_bars();
126 + wm.setup_overlay();
127 +
128 + return wm;
129 + }
130 +
131 + /// Release all allocated memory owned by the WM.
132 + pub fn deinit(self: *WindowManager) void {
133 + bar_mod.destroy_bars(self.bars, self.allocator, self.display.handle);
134 + self.bars = null;
135 +
136 + if (self.overlay) |o| {
137 + o.deinit(self.allocator);
138 + self.overlay = null;
139 + }
140 +
141 + var mon = self.monitors;
142 + while (mon) |m| {
143 + const next = m.next;
144 + monitor_mod.destroy(m);
145 + mon = next;
146 + }
147 + self.monitors = null;
148 + self.selected_monitor = null;
149 +
150 + _ = xlib.XDestroyWindow(self.display.handle, self.wm_check_window);
151 +
152 + self.display.close();
153 + self.config.deinit();
154 + }
155 +
156 + fn setup_monitors(self: *WindowManager) void {
157 + if (xlib.XineramaIsActive(self.display.handle) != 0) {
158 + var screen_count: c_int = 0;
159 + const screens = xlib.XineramaQueryScreens(self.display.handle, &screen_count);
160 +
161 + if (screen_count > 0 and screens != null) {
162 + var prev_monitor: ?*Monitor = null;
163 + var index: usize = 0;
164 +
165 + while (index < @as(usize, @intCast(screen_count))) : (index += 1) {
166 + const screen = screens[index];
167 + const mon = monitor_mod.create() orelse continue;
168 +
169 + mon.num = @intCast(index);
170 + mon.mon_x = screen.x_org;
171 + mon.mon_y = screen.y_org;
172 + mon.mon_w = screen.width;
173 + mon.mon_h = screen.height;
174 + mon.win_x = screen.x_org;
175 + mon.win_y = screen.y_org;
176 + mon.win_w = screen.width;
177 + mon.win_h = screen.height;
178 + mon.lt[0] = &tiling.layout;
179 + mon.lt[1] = &monocle.layout;
180 + mon.lt[2] = &floating.layout;
181 + mon.lt[3] = &scrolling.layout;
182 + mon.lt[4] = &grid.layout;
183 +
184 + for (0..10) |i| {
185 + mon.pertag.ltidxs[i][0] = mon.lt[0];
186 + mon.pertag.ltidxs[i][1] = mon.lt[1];
187 + mon.pertag.ltidxs[i][2] = mon.lt[2];
188 + mon.pertag.ltidxs[i][3] = mon.lt[3];
189 + mon.pertag.ltidxs[i][4] = mon.lt[4];
190 + }
191 +
192 + self.init_monitor_gaps(mon);
193 +
194 + if (prev_monitor) |prev| {
195 + prev.next = mon;
196 + } else {
197 + self.monitors = mon;
198 + self.selected_monitor = mon;
199 + }
200 + prev_monitor = mon;
201 + }
202 +
203 + _ = xlib.XFree(@ptrCast(screens));
204 + }
205 + }
206 +
207 + // Fallback: single monitor covering the full screen.
208 + if (self.monitors == null) {
209 + const mon = monitor_mod.create() orelse return;
210 + mon.num = 0;
211 + mon.mon_x = 0;
212 + mon.mon_y = 0;
213 + mon.mon_w = self.display.screen_width();
214 + mon.mon_h = self.display.screen_height();
215 + mon.win_x = 0;
216 + mon.win_y = 0;
217 + mon.win_w = mon.mon_w;
218 + mon.win_h = mon.mon_h;
219 + mon.lt[0] = &tiling.layout;
220 + mon.lt[1] = &monocle.layout;
221 + mon.lt[2] = &floating.layout;
222 + mon.lt[3] = &scrolling.layout;
223 + mon.lt[4] = &grid.layout;
224 +
225 + for (0..10) |i| {
226 + mon.pertag.ltidxs[i][0] = mon.lt[0];
227 + mon.pertag.ltidxs[i][1] = mon.lt[1];
228 + mon.pertag.ltidxs[i][2] = mon.lt[2];
229 + mon.pertag.ltidxs[i][3] = mon.lt[3];
230 + mon.pertag.ltidxs[i][4] = mon.lt[4];
231 + }
232 +
233 + self.init_monitor_gaps(mon);
234 + self.monitors = mon;
235 + self.selected_monitor = mon;
236 + }
237 +
238 + // Mirror into monitor_mod so legacy code that still reads the
239 + // module-level vars continues to work during the transition.
240 + // TODO(wm-refactor): remove once all callers use self.monitors.
241 + monitor_mod.monitors = self.monitors;
242 + monitor_mod.selected_monitor = self.selected_monitor;
243 + }
244 +
245 + fn init_monitor_gaps(self: *WindowManager, mon: *Monitor) void {
246 + const cfg = &self.config;
247 + const any_gap_nonzero = cfg.gap_inner_h != 0 or cfg.gap_inner_v != 0 or
248 + cfg.gap_outer_h != 0 or cfg.gap_outer_v != 0;
249 +
250 + if (cfg.gaps_enabled and any_gap_nonzero) {
251 + mon.gap_inner_h = cfg.gap_inner_h;
252 + mon.gap_inner_v = cfg.gap_inner_v;
253 + mon.gap_outer_h = cfg.gap_outer_h;
254 + mon.gap_outer_v = cfg.gap_outer_v;
255 + } else {
256 + mon.gap_inner_h = 0;
257 + mon.gap_inner_v = 0;
258 + mon.gap_outer_h = 0;
259 + mon.gap_outer_v = 0;
260 + }
261 + }
262 +
263 + pub fn setup_bars(self: *WindowManager) void {
264 + var current_monitor = self.monitors;
265 + var last_bar: ?*Bar = null;
266 +
267 + while (current_monitor) |monitor| {
268 + const bar = Bar.create(
269 + self.allocator,
270 + self.display.handle,
271 + self.display.screen,
272 + monitor,
273 + self.config,
274 + ) orelse {
275 + current_monitor = monitor.next;
276 + continue;
277 + };
278 +
279 + if (tiling.bar_height == 0) {
280 + tiling.set_bar_height(bar.height);
281 + }
282 +
283 + self.populate_bar_blocks(bar);
284 +
285 + if (last_bar) |prev| {
286 + prev.next = bar;
287 + } else {
288 + self.bars = bar;
289 + }
290 + last_bar = bar;
291 +
292 + std.debug.print("bar created for monitor {d}\n", .{monitor.num});
293 + current_monitor = monitor.next;
294 + }
295 + }
296 +
297 + pub fn populate_bar_blocks(self: *WindowManager, bar: *Bar) void {
298 + if (self.config.blocks.items.len > 0) {
299 + for (self.config.blocks.items) |cfg_block| {
300 + bar.add_block(config_block_to_bar_block(cfg_block));
301 + }
302 + } else {
303 + bar.add_block(blocks_mod.Block.init_ram("", 5, 0x7aa2f7, true));
304 + bar.add_block(blocks_mod.Block.init_static(" | ", 0x666666, false));
305 + bar.add_block(blocks_mod.Block.init_datetime("", "%H:%M", 1, 0x0db9d7, true));
306 + }
307 + }
308 +
309 + fn setup_overlay(self: *WindowManager) void {
310 + self.overlay = overlay_mod.Keybind_Overlay.init(
311 + self.display.handle,
312 + self.display.screen,
313 + self.display.root,
314 + self.config.font,
315 + self.allocator,
316 + );
317 + }
318 +
319 + // Wrap free functions in `bar_mod` with `self.bars` passed in.
320 +
321 + pub fn invalidate_bars(self: *WindowManager) void {
322 + bar_mod.invalidate_bars(self.bars);
323 + }
324 +
325 + pub fn window_to_bar(self: *WindowManager, win: xlib.Window) ?*Bar {
326 + return bar_mod.window_to_bar(self.bars, win);
327 + }
328 +};
329 +
330 +/// Converts a config block description into a live status bar block.
331 +pub fn config_block_to_bar_block(cfg: config_mod.Block) blocks_mod.Block {
332 + return switch (cfg.block_type) {
333 + .static => blocks_mod.Block.init_static(cfg.format, cfg.color, cfg.underline),
334 + .datetime => blocks_mod.Block.init_datetime(
335 + cfg.format,
336 + cfg.datetime_format orelse "%H:%M",
337 + cfg.interval,
338 + cfg.color,
339 + cfg.underline,
340 + ),
341 + .ram => blocks_mod.Block.init_ram(cfg.format, cfg.interval, cfg.color, cfg.underline),
342 + .shell => blocks_mod.Block.init_shell(
343 + cfg.format,
344 + cfg.command orelse "",
345 + cfg.interval,
346 + cfg.color,
347 + cfg.underline,
348 + ),
349 + .battery => blocks_mod.Block.init_battery(
350 + cfg.format_charging orelse "",
351 + cfg.format_discharging orelse "",
352 + cfg.format_full orelse "",
353 + cfg.battery_name orelse "BAT0",
354 + cfg.interval,
355 + cfg.color,
356 + cfg.underline,
357 + ),
358 + .cpu_temp => blocks_mod.Block.init_cpu_temp(
359 + cfg.format,
360 + cfg.thermal_zone orelse "thermal_zone0",
361 + cfg.interval,
362 + cfg.color,
363 + cfg.underline,
364 + ),
365 + };
366 +}