tony / oxwm

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