tony / oxwm

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