tony / oxwm

Branch: 61c6a94
Switch branches/tags
master
tonybanters Added dwindle layout, accept fib, fibonacci as keywords from config, added helper to lsp file, and comment to default config. 1435e0c 2026-05-11
776 lines · 26.5 KB Raw Blame History
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 atoms_mod = @import("../x11/atoms.zig");
7 const chord_mod = @import("../keyboard/chord.zig");
8 const monitor_mod = @import("../monitor.zig");
9 const client_mod = @import("../client.zig");
10 const bar_mod = @import("../bar/bar.zig");
11 const blocks_mod = @import("../bar/blocks/blocks.zig");
12 const config_mod = @import("../config/config.zig");
13 const overlay_mod = @import("../overlay.zig");
14 const animations = @import("../animations.zig");
15 const tiling = @import("../layouts/tiling.zig");
16 const monocle = @import("../layouts/monocle.zig");
17 const floating = @import("../layouts/floating.zig");
18 const scrolling = @import("../layouts/scrolling.zig");
19 const grid = @import("../layouts/grid.zig");
20 const dwindle = @import("../layouts/dwindle.zig");
21
22 pub const core = @import("core.zig");
23 pub const actions = @import("actions.zig");
24 pub const handlers = @import("handlers.zig");
25
26 const Display = display_mod.Display;
27 const Atoms = atoms_mod.Atoms;
28 const ChordState = chord_mod.ChordState;
29 const Monitor = monitor_mod.Monitor;
30 const Bar = bar_mod.Bar;
31 const Config = config_mod.Config;
32
33 // Standard ICCCM WM_STATE values used by get_state and set_client_state.
34 pub const NormalState: c_long = 1;
35 pub const WithdrawnState: c_long = 0;
36 pub const IconicState: c_long = 3;
37 pub const IsViewable: c_int = 2;
38
39 pub const Cursors = struct {
40 normal: xlib.Cursor,
41 resize: xlib.Cursor,
42 move: xlib.Cursor,
43
44 pub fn init(display: *Display) Cursors {
45 return .{
46 .normal = xlib.XCreateFontCursor(display.handle, xlib.XC_left_ptr),
47 .resize = xlib.XCreateFontCursor(display.handle, xlib.XC_sizing),
48 .move = xlib.XCreateFontCursor(display.handle, xlib.XC_fleur),
49 };
50 }
51 };
52
53 pub const WindowManager = struct {
54 allocator: mem.Allocator,
55
56 /// The connection to the X server.
57 display: Display,
58 x11_fd: c_int,
59 /// Invisible 1×1 window used to satisfy the _NET_SUPPORTING_WM_CHECK
60 /// EWMH convention.
61 wm_check_window: xlib.Window,
62
63 atoms: Atoms,
64 cursors: Cursors,
65 /// Cached numlock modifier mask, refreshed before any key/button grab.
66 numlock_mask: c_uint,
67
68 config: Config,
69 /// Path to the config file that was loaded.
70 /// Null if using default config.
71 config_path: ?[]const u8,
72
73 /// Head of the linked list of all managed monitors.
74 monitors: ?*Monitor,
75 /// The monitor that currently has input focus.
76 selected_monitor: ?*Monitor,
77
78 /// Head of the linked list of status bars (one per monitor).
79 bars: ?*Bar,
80
81 chord: ChordState,
82
83 overlay: ?*overlay_mod.KeybindOverlay,
84
85 scroll_animation: animations.ScrollAnimation,
86 animation_config: animations.AnimationConfig,
87
88 running: bool,
89 next_spawn_floating: bool = false,
90 next_spawn_bypass_rules: bool = false,
91 last_motion_monitor: ?*Monitor,
92
93 /// Initialises the window manager
94 ///
95 /// Returns an error if the display cannot be opened or another WM is
96 /// already running.
97 pub fn init(allocator: mem.Allocator, config: Config, config_path: ?[]const u8) !WindowManager {
98 var display = try Display.open();
99 errdefer display.close();
100
101 try display.becomeWindowManager();
102
103 const x11_fd = xlib.XConnectionNumber(display.handle);
104
105 const atoms_result = Atoms.init(display.handle, display.root);
106 const cursors = Cursors.init(&display);
107 _ = xlib.XDefineCursor(display.handle, display.root, cursors.normal);
108
109 tiling.setDisplay(display.handle);
110 tiling.setScreenSize(display.screenWidth(), display.screenHeight());
111
112 var wm = WindowManager{
113 .allocator = allocator,
114 .display = display,
115 .x11_fd = x11_fd,
116 .wm_check_window = atoms_result.check_window,
117 .atoms = atoms_result.atoms,
118 .cursors = cursors,
119 .numlock_mask = 0,
120 .config = config,
121 .config_path = config_path,
122 .monitors = null,
123 .selected_monitor = null,
124 .bars = null,
125 .chord = .{},
126 .overlay = null,
127 .scroll_animation = .{},
128 .animation_config = .{ .duration_ms = 150, .easing = .ease_out },
129 .running = true,
130 .last_motion_monitor = null,
131 };
132
133 wm.setupMonitors();
134 wm.setupBars();
135 wm.setupOverlay();
136
137 return wm;
138 }
139
140 /// Release all allocated memory owned by the WM.
141 pub fn deinit(self: *WindowManager) void {
142 bar_mod.destroyBars(self.bars, self.display.handle);
143 self.bars = null;
144
145 if (self.overlay) |o| {
146 o.deinit(self.allocator);
147 self.overlay = null;
148 }
149
150 var mon = self.monitors;
151 while (mon) |m| {
152 const next = m.next;
153 monitor_mod.destroy(self.allocator, m);
154 mon = next;
155 }
156 self.monitors = null;
157 self.selected_monitor = null;
158
159 _ = xlib.XDestroyWindow(self.display.handle, self.wm_check_window);
160
161 self.display.close();
162 self.config.deinit();
163 }
164
165 const layouts = [_]*const monitor_mod.Layout{
166 &tiling.layout,
167 &monocle.layout,
168 &floating.layout,
169 &scrolling.layout,
170 &grid.layout,
171 &dwindle.layout,
172 };
173
174 fn initMonitor(self: *WindowManager, mon: *Monitor, num: usize, x: i16, y: i16, w: c_int, h: c_int) void {
175 mon.num = @intCast(num);
176 mon.mon_x = x;
177 mon.mon_y = y;
178 mon.mon_w = w;
179 mon.mon_h = h;
180 mon.win_x = x;
181 mon.win_y = y;
182 mon.win_w = w;
183 mon.win_h = h;
184
185 for (&mon.lt, layouts) |*slot, layout| {
186 slot.* = layout;
187 }
188
189 if (config_mod.Layouts.fromString(self.config.layout)) |value| {
190 mon.sel_lt = @intFromEnum(value);
191 }
192
193 for (0..10) |i| {
194 for (0..layouts.len) |j| {
195 mon.pertag.ltidxs[i][j] = mon.lt[j];
196 }
197 mon.pertag.sellts[i] = mon.sel_lt;
198 }
199
200 for (self.config.tag_layouts, 0..) |maybe_layout, i| {
201 if (maybe_layout) |layout_name| {
202 if (config_mod.Layouts.fromString(layout_name)) |value| {
203 mon.pertag.sellts[i + 1] = @intFromEnum(value);
204 }
205 }
206 }
207
208 self.initMonitorGaps(mon);
209 }
210
211 fn setupMonitors(self: *WindowManager) void {
212 if (xlib.XineramaIsActive(self.display.handle) != 0) {
213 var screen_count: c_int = 0;
214 const screens = xlib.XineramaQueryScreens(self.display.handle, &screen_count);
215 defer if (screens != null) {
216 _ = xlib.XFree(@ptrCast(screens));
217 };
218
219 if (screen_count > 0 and screens != null) {
220 var prev_monitor: ?*Monitor = null;
221 for (0..@as(usize, @intCast(screen_count))) |index| {
222 const screen = screens[index];
223 const mon = monitor_mod.create(self.allocator) orelse continue;
224 self.initMonitor(mon, index, screen.x_org, screen.y_org, screen.width, screen.height);
225
226 if (prev_monitor) |prev| {
227 prev.next = mon;
228 } else {
229 self.monitors = mon;
230 self.selected_monitor = mon;
231 }
232 prev_monitor = mon;
233 }
234 }
235 }
236
237 if (self.monitors == null) {
238 const mon = monitor_mod.create(self.allocator) orelse return;
239 self.initMonitor(mon, 0, 0, 0, self.display.screenWidth(), self.display.screenHeight());
240 self.monitors = mon;
241 self.selected_monitor = mon;
242 }
243 }
244
245 pub fn updateGeom(self: *WindowManager) bool {
246 var dirty = false;
247
248 if (xlib.XineramaIsActive(self.display.handle) != 0) {
249 var n: usize = 0;
250 var m = self.monitors;
251 while (m) |mon| : (m = mon.next) n += 1;
252
253 var nn: c_int = 0;
254 const info = xlib.XineramaQueryScreens(self.display.handle, &nn);
255 if (info == null) return false;
256 defer _ = xlib.XFree(@ptrCast(info));
257
258 const new_count: usize = @intCast(nn);
259
260 for (n..new_count) |_| {
261 var last = self.monitors;
262 while (last) |mon| {
263 if (mon.next == null) break;
264 last = mon.next;
265 }
266 const new_mon = monitor_mod.create(self.allocator) orelse continue;
267 new_mon.lt[0] = &tiling.layout;
268 new_mon.lt[1] = &monocle.layout;
269 new_mon.lt[2] = &floating.layout;
270 new_mon.lt[3] = &scrolling.layout;
271 new_mon.lt[4] = &grid.layout;
272 new_mon.lt[5] = &dwindle.layout;
273 for (0..10) |i| {
274 new_mon.pertag.ltidxs[i][0] = new_mon.lt[0];
275 new_mon.pertag.ltidxs[i][1] = new_mon.lt[1];
276 new_mon.pertag.ltidxs[i][2] = new_mon.lt[2];
277 new_mon.pertag.ltidxs[i][3] = new_mon.lt[3];
278 new_mon.pertag.ltidxs[i][4] = new_mon.lt[4];
279 new_mon.pertag.ltidxs[i][5] = new_mon.lt[5];
280 }
281 if (last) |l| {
282 l.next = new_mon;
283 } else {
284 self.monitors = new_mon;
285 }
286 }
287
288 var i: usize = 0;
289 m = self.monitors;
290 while (m != null and i < new_count) : (i += 1) {
291 const mon = m.?;
292 const screen = info[i];
293 if (i >= n or
294 screen.x_org != mon.mon_x or screen.y_org != mon.mon_y or
295 screen.width != mon.mon_w or screen.height != mon.mon_h)
296 {
297 dirty = true;
298 mon.num = @intCast(i);
299 mon.mon_x = screen.x_org;
300 mon.mon_y = screen.y_org;
301 mon.mon_w = screen.width;
302 mon.mon_h = screen.height;
303 mon.win_x = screen.x_org;
304 mon.win_y = screen.y_org;
305 mon.win_w = screen.width;
306 mon.win_h = screen.height;
307 self.initMonitorGaps(mon);
308 }
309 m = mon.next;
310 }
311
312 for (new_count..n) |_| {
313 var last: ?*Monitor = null;
314 var iter = self.monitors;
315 while (iter) |mon| {
316 if (mon.next == null) {
317 last = mon;
318 break;
319 }
320 iter = mon.next;
321 }
322 const removed = last orelse continue;
323
324 while (removed.clients) |c| {
325 dirty = true;
326 removed.clients = c.next;
327 client_mod.detachStack(c);
328 c.monitor = self.monitors;
329 client_mod.attachAside(c);
330 client_mod.attachStack(c);
331 }
332
333 if (self.selected_monitor == removed) {
334 self.selected_monitor = self.monitors;
335 }
336
337 var prev: ?*Monitor = null;
338 iter = self.monitors;
339 while (iter) |mon| {
340 if (mon == removed) {
341 if (prev) |p| {
342 p.next = removed.next;
343 } else {
344 self.monitors = removed.next;
345 }
346 break;
347 }
348 prev = mon;
349 iter = mon.next;
350 }
351 monitor_mod.destroy(self.allocator, removed);
352 }
353
354 tiling.setScreenSize(info[0].width, info[0].height);
355 } else {
356 if (self.monitors == null) {
357 const mon = monitor_mod.create(self.allocator) orelse return false;
358 mon.lt[0] = &tiling.layout;
359 mon.lt[1] = &monocle.layout;
360 mon.lt[2] = &floating.layout;
361 mon.lt[3] = &scrolling.layout;
362 mon.lt[4] = &grid.layout;
363 mon.lt[5] = &dwindle.layout;
364 self.monitors = mon;
365 self.selected_monitor = mon;
366 }
367
368 const sw = self.display.screenWidth();
369 const sh = self.display.screenHeight();
370
371 if (self.monitors) |mon| {
372 if (mon.mon_w != sw or mon.mon_h != sh) {
373 dirty = true;
374 mon.mon_x = 0;
375 mon.mon_y = 0;
376 mon.mon_w = sw;
377 mon.mon_h = sh;
378 mon.win_x = 0;
379 mon.win_y = 0;
380 mon.win_w = sw;
381 mon.win_h = sh;
382 self.initMonitorGaps(mon);
383 }
384 }
385
386 tiling.setScreenSize(sw, sh);
387 }
388
389 if (dirty) {
390 self.selected_monitor = self.monitors;
391 self.selected_monitor = monitor_mod.windowToMonitor(self, self.display.root);
392 }
393
394 return dirty;
395 }
396
397 pub fn rebuildBars(self: *WindowManager) void {
398 bar_mod.destroyBars(self.bars, self.display.handle);
399 self.bars = null;
400 self.setupBars();
401 self.rebuildBarBlocks();
402 }
403
404 fn initMonitorGaps(self: *WindowManager, mon: *Monitor) void {
405 const cfg = &self.config;
406 const any_gap_nonzero = cfg.gap_inner_h != 0 or cfg.gap_inner_v != 0 or
407 cfg.gap_outer_h != 0 or cfg.gap_outer_v != 0;
408
409 mon.smartgaps_enabled = cfg.smartgaps_enabled;
410
411 if (cfg.gaps_enabled and any_gap_nonzero) {
412 mon.gap_inner_h = cfg.gap_inner_h;
413 mon.gap_inner_v = cfg.gap_inner_v;
414 mon.gap_outer_h = cfg.gap_outer_h;
415 mon.gap_outer_v = cfg.gap_outer_v;
416 } else {
417 mon.gap_inner_h = 0;
418 mon.gap_inner_v = 0;
419 mon.gap_outer_h = 0;
420 mon.gap_outer_v = 0;
421 }
422 }
423
424 pub fn setupBars(self: *WindowManager) void {
425 var current_monitor = self.monitors;
426 var last_bar: ?*Bar = null;
427 var is_first_bar = true;
428 const want_systray = configHasSystray(self.config);
429
430 while (current_monitor) |monitor| {
431 const bar = Bar.create(
432 self.allocator,
433 self.display.handle,
434 self.display.screen,
435 monitor,
436 self.config,
437 is_first_bar and want_systray,
438 ) orelse {
439 current_monitor = monitor.next;
440 continue;
441 };
442 is_first_bar = false;
443
444 if (tiling.bar_height == 0) {
445 tiling.setBarHeight(bar.height);
446 }
447
448 self.populateBarBlocks(bar);
449
450 if (last_bar) |prev| {
451 prev.next = bar;
452 } else {
453 self.bars = bar;
454 }
455 last_bar = bar;
456
457 std.debug.print("bar created for monitor {d}\n", .{monitor.num});
458 current_monitor = monitor.next;
459 }
460 }
461
462 pub fn populateBarBlocks(self: *WindowManager, bar: *Bar) void {
463 if (self.config.blocks.items.len > 0) {
464 for (self.config.blocks.items) |cfg_block| {
465 if (cfg_block.block_type == .systray) {
466 bar.setSystrayConfig(cfg_block.underline, cfg_block.color);
467 } else {
468 bar.addBlock(configBlockToBarBlock(cfg_block));
469 }
470 }
471 } else {
472 bar.addBlock(blocks_mod.Block.initRam("", 5, 0x7aa2f7, true));
473 bar.addBlock(blocks_mod.Block.initStatic(" | ", 0x666666, false));
474 bar.addBlock(blocks_mod.Block.initDatetime("", "%H:%M", 1, 0x0db9d7, true));
475 }
476 }
477
478 fn setupOverlay(self: *WindowManager) void {
479 self.overlay = overlay_mod.KeybindOverlay.init(
480 self.display.handle,
481 self.display.screen,
482 self.display.root,
483 self.config.font,
484 self.allocator,
485 );
486 }
487
488 // Wrap free functions in `bar_mod` with `self.bars` passed in.
489
490 pub fn invalidateBars(self: *WindowManager) void {
491 bar_mod.invalidateBars(self.bars);
492 }
493
494 pub fn windowToBar(self: *WindowManager, win: xlib.Window) ?*Bar {
495 return bar_mod.windowToBar(self.bars, win);
496 }
497
498 pub fn getSystray(self: *WindowManager) ?*bar_mod.Systray {
499 return bar_mod.getSystray(self.bars);
500 }
501
502 /// Refreshes the cached numlock modifier bitmask from the X server's
503 /// current modifier map. Called before any key or button grab so that
504 /// grabs cover all numlock combinations.
505 pub fn updateNumlockMask(self: *WindowManager) void {
506 self.numlock_mask = 0;
507 const modmap = xlib.XGetModifierMapping(self.display.handle);
508 if (modmap == null) return;
509 defer _ = xlib.XFreeModifiermap(modmap);
510
511 const numlock_keycode = xlib.XKeysymToKeycode(self.display.handle, xlib.XK_Num_Lock);
512
513 var modifier_index: usize = 0;
514 while (modifier_index < 8) : (modifier_index += 1) {
515 var key_index: usize = 0;
516 while (key_index < @as(usize, @intCast(modmap.*.max_keypermod))) : (key_index += 1) {
517 const keycode = modmap.*.modifiermap[modifier_index * @as(usize, @intCast(modmap.*.max_keypermod)) + key_index];
518 if (keycode == numlock_keycode) {
519 self.numlock_mask = @as(c_uint, 1) << @intCast(modifier_index);
520 }
521 }
522 }
523 }
524
525 /// Grabs all configured keybinds and mouse buttons from the X server.
526 /// Replaces any existing grabs. Call after config load or reload.
527 pub fn grabKeybinds(self: *WindowManager) void {
528 self.updateNumlockMask();
529 const modifiers = [_]c_uint{ 0, xlib.LockMask, self.numlock_mask, self.numlock_mask | xlib.LockMask };
530
531 _ = xlib.XUngrabKey(self.display.handle, xlib.AnyKey, xlib.AnyModifier, self.display.root);
532
533 for (self.config.keybinds.items) |keybind| {
534 if (keybind.key_count == 0) continue;
535 const first_key = keybind.keys[0];
536 const keycode = xlib.XKeysymToKeycode(self.display.handle, @intCast(first_key.keysym));
537 if (keycode != 0) {
538 for (modifiers) |modifier| {
539 _ = xlib.XGrabKey(
540 self.display.handle,
541 keycode,
542 first_key.mod_mask | modifier,
543 self.display.root,
544 xlib.True,
545 xlib.GrabModeAsync,
546 xlib.GrabModeAsync,
547 );
548 }
549 }
550 }
551
552 for (self.config.buttons.items) |button| {
553 if (button.click == .client_win) {
554 for (modifiers) |modifier| {
555 _ = xlib.XGrabButton(
556 self.display.handle,
557 @intCast(button.button),
558 button.mod_mask | modifier,
559 self.display.root,
560 xlib.True,
561 xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask,
562 xlib.GrabModeAsync,
563 xlib.GrabModeAsync,
564 xlib.None,
565 xlib.None,
566 );
567 }
568 }
569 }
570
571 std.debug.print("grabbed {d} keybinds from config\n", .{self.config.keybinds.items.len});
572 }
573
574 pub fn ungrabKeybinds(self: *WindowManager) void {
575 _ = xlib.XUngrabKey(self.display.handle, xlib.AnyKey, xlib.AnyModifier, self.display.root);
576 }
577
578 /// Walk the existing window tree and call `manageFn` for each window
579 /// that should be managed. Non-transient windows are processed first
580 /// so transient (dialog) windows can be stacked on top.
581 pub fn scanExistingWindows(self: *WindowManager, manageFn: fn (xlib.Window, *xlib.XWindowAttributes, *WindowManager) void) void {
582 var root_return: xlib.Window = undefined;
583 var parent_return: xlib.Window = undefined;
584 var children: [*c]xlib.Window = undefined;
585 var num_children: c_uint = undefined;
586
587 if (xlib.XQueryTree(self.display.handle, self.display.root, &root_return, &parent_return, &children, &num_children) == 0) {
588 return;
589 }
590
591 var index: c_uint = 0;
592 while (index < num_children) : (index += 1) {
593 var window_attrs: xlib.XWindowAttributes = undefined;
594 if (xlib.XGetWindowAttributes(self.display.handle, children[index], &window_attrs) == 0) {
595 continue;
596 }
597 if (window_attrs.override_redirect != 0) continue;
598
599 var trans: xlib.Window = 0;
600 if (xlib.XGetTransientForHint(self.display.handle, children[index], &trans) != 0) continue;
601
602 if (window_attrs.map_state == IsViewable or self.getState(children[index]) == IconicState) {
603 manageFn(children[index], &window_attrs, self);
604 }
605 }
606
607 index = 0;
608 while (index < num_children) : (index += 1) {
609 var window_attrs: xlib.XWindowAttributes = undefined;
610 if (xlib.XGetWindowAttributes(self.display.handle, children[index], &window_attrs) == 0) {
611 continue;
612 }
613 var trans: xlib.Window = 0;
614 if (xlib.XGetTransientForHint(self.display.handle, children[index], &trans) != 0) {
615 if (window_attrs.map_state == IsViewable or self.getState(children[index]) == IconicState) {
616 manageFn(children[index], &window_attrs, self);
617 }
618 }
619 }
620
621 if (children != null) _ = xlib.XFree(@ptrCast(children));
622 }
623
624 /// Reads the ICCCM WM_STATE property for a window.
625 /// Returns WithdrawnState if the property is absent or malformed.
626 pub fn getState(self: *WindowManager, window: xlib.Window) c_long {
627 var actual_type: xlib.Atom = 0;
628 var actual_format: c_int = 0;
629 var num_items: c_ulong = 0;
630 var bytes_after: c_ulong = 0;
631 var prop: [*c]u8 = null;
632
633 const result = xlib.XGetWindowProperty(
634 self.display.handle,
635 window,
636 self.atoms.wm_state,
637 0,
638 2,
639 xlib.False,
640 self.atoms.wm_state,
641 &actual_type,
642 &actual_format,
643 &num_items,
644 &bytes_after,
645 &prop,
646 );
647
648 if (result != 0 or actual_type != self.atoms.wm_state or num_items < 1) {
649 if (prop != null) _ = xlib.XFree(prop);
650 return WithdrawnState;
651 }
652
653 const state: c_long = @as(*c_long, @ptrCast(@alignCast(prop))).*;
654 _ = xlib.XFree(prop);
655 return state;
656 }
657
658 /// Run the event loop until `self.running` is set to false.
659 ///
660 /// `eventFn` dispatches a single XEvent
661 /// `tickFn` is called once per loop iteration to advance animations
662 pub fn run(
663 self: *WindowManager,
664 eventFn: fn (*xlib.XEvent, *WindowManager) void,
665 tickFn: fn (*WindowManager) void,
666 ) void {
667 var fds = [_]std.posix.pollfd{
668 .{ .fd = self.x11_fd, .events = std.posix.POLL.IN, .revents = 0 },
669 };
670
671 _ = xlib.XSync(self.display.handle, xlib.False);
672
673 while (self.running) {
674 while (xlib.XPending(self.display.handle) > 0) {
675 var event = self.display.nextEvent();
676 eventFn(&event, self);
677 }
678
679 tickFn(self);
680
681 var current_bar = self.bars;
682 while (current_bar) |bar| {
683 bar.updateBlocks();
684 bar.draw(self.display.handle, self.config);
685 current_bar = bar.next;
686 }
687
688 const poll_timeout: i32 = if (self.scroll_animation.isActive()) 16 else 1000;
689 _ = std.posix.poll(&fds, poll_timeout) catch 0;
690 }
691 }
692
693 /// Reloads the configuration
694 ///
695 /// `loadFn` repopulates `self.config`
696 /// keys/buttons are unbound before reload and rebound after
697 pub fn reloadConfig(
698 self: *WindowManager,
699 loadFn: fn (*WindowManager) void,
700 ) void {
701 std.debug.print("reloading config...\n", .{});
702
703 self.ungrabKeybinds();
704
705 self.config.keybinds.clearRetainingCapacity();
706 self.config.buttons.clearRetainingCapacity();
707 self.config.rules.clearRetainingCapacity();
708 self.config.blocks.clearRetainingCapacity();
709
710 loadFn(self);
711
712 bar_mod.destroyBars(self.bars, self.display.handle);
713 self.bars = null;
714 self.setupBars();
715 self.rebuildBarBlocks();
716
717 self.grabKeybinds();
718 }
719
720 fn rebuildBarBlocks(self: *WindowManager) void {
721 var current_bar = self.bars;
722 while (current_bar) |bar| {
723 bar.clearBlocks();
724 self.populateBarBlocks(bar);
725 current_bar = bar.next;
726 }
727 }
728 };
729
730 fn configHasSystray(config: Config) bool {
731 for (config.blocks.items) |block| {
732 if (block.block_type == .systray) return true;
733 }
734 return false;
735 }
736
737 /// Converts a config block description into a live status bar block.
738 pub fn configBlockToBarBlock(cfg: config_mod.Block) blocks_mod.Block {
739 var block = switch (cfg.block_type) {
740 .static => blocks_mod.Block.initStatic(cfg.format, cfg.color, cfg.underline),
741 .datetime => blocks_mod.Block.initDatetime(
742 cfg.format,
743 cfg.datetime_format orelse "%H:%M",
744 cfg.interval,
745 cfg.color,
746 cfg.underline,
747 ),
748 .ram => blocks_mod.Block.initRam(cfg.format, cfg.interval, cfg.color, cfg.underline),
749 .shell => blocks_mod.Block.initShell(
750 cfg.format,
751 cfg.command orelse "",
752 cfg.interval,
753 cfg.color,
754 cfg.underline,
755 ),
756 .battery => blocks_mod.Block.initBattery(
757 cfg.format_charging orelse "",
758 cfg.format_discharging orelse "",
759 cfg.format_full orelse "",
760 cfg.battery_name orelse "BAT0",
761 cfg.interval,
762 cfg.color,
763 cfg.underline,
764 ),
765 .cpu_temp => blocks_mod.Block.initCpuTemp(
766 cfg.format,
767 cfg.thermal_zone orelse "thermal_zone0",
768 cfg.interval,
769 cfg.color,
770 cfg.underline,
771 ),
772 .systray => blocks_mod.Block.initStatic("", 0, false),
773 };
774 block.click = cfg.click;
775 return block;
776 }