tony / oxwm

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