tony / oxwm

Branch: c95e5bb
Switch branches/tags
master
MaliCopter Attach Methods but no hot reload c95e5bb 2026-09-06
789 lines · 26.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 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 if (config_mod.AttachMethods.fromString(self.config.attach_method)) |value| {
198 mon.att_m = @intFromEnum(value);
199 }
200
201 for (0..10) |i| {
202 for (0..layouts.len) |j| {
203 mon.pertag.ltidxs[i][j] = mon.lt[j];
204 }
205 mon.pertag.sellts[i] = mon.sel_lt;
206 }
207
208 for (self.config.tag_layouts, 0..) |maybe_layout, i| {
209 if (maybe_layout) |layout_name| {
210 if (config_mod.Layouts.fromString(layout_name)) |value| {
211 mon.pertag.sellts[i + 1] = @intFromEnum(value);
212 }
213 }
214 }
215
216 self.initMonitorGaps(mon);
217 }
218
219 fn setupMonitors(self: *WindowManager) void {
220 if (xlib.XineramaIsActive(self.display.handle) != 0) {
221 var screen_count: c_int = 0;
222 const screens = xlib.XineramaQueryScreens(self.display.handle, &screen_count);
223 defer if (screens != null) {
224 _ = xlib.XFree(@ptrCast(screens));
225 };
226
227 if (screen_count > 0 and screens != null) {
228 var prev_monitor: ?*Monitor = null;
229 for (0..@as(usize, @intCast(screen_count))) |index| {
230 const screen = screens[index];
231 const mon = monitor_mod.create(self.allocator) orelse continue;
232 self.initMonitor(mon, index, screen.x_org, screen.y_org, screen.width, screen.height);
233
234 if (prev_monitor) |prev| {
235 prev.next = mon;
236 } else {
237 self.monitors = mon;
238 self.selected_monitor = mon;
239 }
240 prev_monitor = mon;
241 }
242 }
243 }
244
245 if (self.monitors == null) {
246 const mon = monitor_mod.create(self.allocator) orelse return;
247 self.initMonitor(mon, 0, 0, 0, self.display.screenWidth(), self.display.screenHeight());
248 self.monitors = mon;
249 self.selected_monitor = mon;
250 }
251 }
252
253 pub fn updateGeom(self: *WindowManager) bool {
254 var dirty = false;
255
256 if (xlib.XineramaIsActive(self.display.handle) != 0) {
257 var n: usize = 0;
258 var m = self.monitors;
259 while (m) |mon| : (m = mon.next) n += 1;
260
261 var nn: c_int = 0;
262 const info = xlib.XineramaQueryScreens(self.display.handle, &nn);
263 if (info == null) return false;
264 defer _ = xlib.XFree(@ptrCast(info));
265
266 const new_count: usize = @intCast(nn);
267
268 for (0..new_count -| n) |_| {
269 var last = self.monitors;
270 while (last) |mon| {
271 if (mon.next == null) break;
272 last = mon.next;
273 }
274 const new_mon = monitor_mod.create(self.allocator) orelse continue;
275 new_mon.lt[0] = &tiling.layout;
276 new_mon.lt[1] = &monocle.layout;
277 new_mon.lt[2] = &floating.layout;
278 new_mon.lt[3] = &scrolling.layout;
279 new_mon.lt[4] = &grid.layout;
280 new_mon.lt[5] = &dwindle.layout;
281 for (0..10) |i| {
282 new_mon.pertag.ltidxs[i][0] = new_mon.lt[0];
283 new_mon.pertag.ltidxs[i][1] = new_mon.lt[1];
284 new_mon.pertag.ltidxs[i][2] = new_mon.lt[2];
285 new_mon.pertag.ltidxs[i][3] = new_mon.lt[3];
286 new_mon.pertag.ltidxs[i][4] = new_mon.lt[4];
287 new_mon.pertag.ltidxs[i][5] = new_mon.lt[5];
288 }
289 if (last) |l| {
290 l.next = new_mon;
291 } else {
292 self.monitors = new_mon;
293 }
294 }
295
296 var i: usize = 0;
297 m = self.monitors;
298 while (m != null and i < new_count) : (i += 1) {
299 const mon = m.?;
300 const screen = info[i];
301 if (i >= n or
302 screen.x_org != mon.mon_x or screen.y_org != mon.mon_y or
303 screen.width != mon.mon_w or screen.height != mon.mon_h)
304 {
305 dirty = true;
306 mon.num = @intCast(i);
307 mon.mon_x = screen.x_org;
308 mon.mon_y = screen.y_org;
309 mon.mon_w = screen.width;
310 mon.mon_h = screen.height;
311 mon.win_x = screen.x_org;
312 mon.win_y = screen.y_org;
313 mon.win_w = screen.width;
314 mon.win_h = screen.height;
315 self.initMonitorGaps(mon);
316 }
317 m = mon.next;
318 }
319
320 for (0..n -| @max(new_count, 1)) |_| {
321 var last: ?*Monitor = null;
322 var iter = self.monitors;
323 while (iter) |mon| {
324 if (mon.next == null) {
325 last = mon;
326 break;
327 }
328 iter = mon.next;
329 }
330 const removed = last orelse continue;
331
332 dirty = true;
333
334 var prev: ?*Monitor = null;
335 iter = self.monitors;
336 while (iter) |mon| {
337 if (mon == removed) {
338 if (prev) |p| {
339 p.next = removed.next;
340 } else {
341 self.monitors = removed.next;
342 }
343 break;
344 }
345 prev = mon;
346 iter = mon.next;
347 }
348
349 if (self.selected_monitor == removed) {
350 self.selected_monitor = self.monitors;
351 }
352
353 if (self.monitors) |target| {
354 while (removed.clients) |c| {
355 removed.clients = c.next;
356 client_mod.detachStack(c);
357 c.monitor = target;
358 c.tags = target.tagset[target.sel_tags];
359 client_mod.attachAside(c);
360 client_mod.attachStack(c);
361 }
362 }
363
364 monitor_mod.destroy(self.allocator, removed);
365 }
366
367 tiling.setScreenSize(info[0].width, info[0].height);
368 } else {
369 if (self.monitors == null) {
370 const mon = monitor_mod.create(self.allocator) orelse return false;
371 mon.lt[0] = &tiling.layout;
372 mon.lt[1] = &monocle.layout;
373 mon.lt[2] = &floating.layout;
374 mon.lt[3] = &scrolling.layout;
375 mon.lt[4] = &grid.layout;
376 mon.lt[5] = &dwindle.layout;
377 self.monitors = mon;
378 self.selected_monitor = mon;
379 }
380
381 const sw = self.display.screenWidth();
382 const sh = self.display.screenHeight();
383
384 if (self.monitors) |mon| {
385 if (mon.mon_w != sw or mon.mon_h != sh) {
386 dirty = true;
387 mon.mon_x = 0;
388 mon.mon_y = 0;
389 mon.mon_w = sw;
390 mon.mon_h = sh;
391 mon.win_x = 0;
392 mon.win_y = 0;
393 mon.win_w = sw;
394 mon.win_h = sh;
395 self.initMonitorGaps(mon);
396 }
397 }
398
399 tiling.setScreenSize(sw, sh);
400 }
401
402 if (dirty) {
403 self.selected_monitor = self.monitors;
404 self.selected_monitor = monitor_mod.windowToMonitor(self, self.display.root);
405 }
406
407 return dirty;
408 }
409
410 pub fn rebuildBars(self: *WindowManager) void {
411 bar_mod.destroyBars(self.bars, self.display.handle);
412 self.bars = null;
413 self.setupBars();
414 self.rebuildBarBlocks();
415 }
416
417 fn initMonitorGaps(self: *WindowManager, mon: *Monitor) void {
418 const cfg = &self.config;
419 const any_gap_nonzero = cfg.gap_inner_h != 0 or cfg.gap_inner_v != 0 or
420 cfg.gap_outer_h != 0 or cfg.gap_outer_v != 0;
421
422 mon.smartgaps_enabled = cfg.smartgaps_enabled;
423
424 if (cfg.gaps_enabled and any_gap_nonzero) {
425 mon.gap_inner_h = cfg.gap_inner_h;
426 mon.gap_inner_v = cfg.gap_inner_v;
427 mon.gap_outer_h = cfg.gap_outer_h;
428 mon.gap_outer_v = cfg.gap_outer_v;
429 } else {
430 mon.gap_inner_h = 0;
431 mon.gap_inner_v = 0;
432 mon.gap_outer_h = 0;
433 mon.gap_outer_v = 0;
434 }
435 }
436
437 pub fn setupBars(self: *WindowManager) void {
438 var current_monitor = self.monitors;
439 var last_bar: ?*Bar = null;
440 var is_first_bar = true;
441 const want_systray = configHasSystray(self.config);
442
443 while (current_monitor) |monitor| {
444 const bar = Bar.create(
445 self.allocator,
446 self.display.handle,
447 self.display.screen,
448 monitor,
449 self.config,
450 is_first_bar and want_systray,
451 ) orelse {
452 current_monitor = monitor.next;
453 continue;
454 };
455 is_first_bar = false;
456
457 if (tiling.bar_height == 0) {
458 tiling.setBarHeight(bar.height);
459 }
460
461 self.populateBarBlocks(bar);
462
463 if (last_bar) |prev| {
464 prev.next = bar;
465 } else {
466 self.bars = bar;
467 }
468 last_bar = bar;
469
470 std.debug.print("bar created for monitor {d}\n", .{monitor.num});
471 current_monitor = monitor.next;
472 }
473 }
474
475 pub fn populateBarBlocks(self: *WindowManager, bar: *Bar) void {
476 if (self.config.blocks.items.len > 0) {
477 for (self.config.blocks.items) |cfg_block| {
478 if (cfg_block.block_type == .systray) {
479 bar.setSystrayConfig(cfg_block.underline, cfg_block.color);
480 } else {
481 bar.addBlock(configBlockToBarBlock(cfg_block));
482 }
483 }
484 } else {
485 bar.addBlock(blocks_mod.Block.initRam("", 5, 0x7aa2f7, true));
486 bar.addBlock(blocks_mod.Block.initStatic(" | ", 0x666666, false));
487 bar.addBlock(blocks_mod.Block.initDatetime("", "%H:%M", 1, 0x0db9d7, true));
488 }
489 }
490
491 fn setupOverlay(self: *WindowManager) void {
492 self.overlay = overlay_mod.KeybindOverlay.init(
493 self.display.handle,
494 self.display.screen,
495 self.display.root,
496 self.config.font,
497 self.allocator,
498 );
499 }
500
501 // Wrap free functions in `bar_mod` with `self.bars` passed in.
502
503 pub fn invalidateBars(self: *WindowManager) void {
504 bar_mod.invalidateBars(self.bars);
505 }
506
507 pub fn windowToBar(self: *WindowManager, win: xlib.Window) ?*Bar {
508 return bar_mod.windowToBar(self.bars, win);
509 }
510
511 pub fn getSystray(self: *WindowManager) ?*bar_mod.Systray {
512 return bar_mod.getSystray(self.bars);
513 }
514
515 /// Refreshes the cached numlock modifier bitmask from the X server's
516 /// current modifier map. Called before any key or button grab so that
517 /// grabs cover all numlock combinations.
518 pub fn updateNumlockMask(self: *WindowManager) void {
519 self.numlock_mask = 0;
520 const modmap = xlib.XGetModifierMapping(self.display.handle);
521 if (modmap == null) return;
522 defer _ = xlib.XFreeModifiermap(modmap);
523
524 const numlock_keycode = xlib.XKeysymToKeycode(self.display.handle, xlib.XK_Num_Lock);
525
526 var modifier_index: usize = 0;
527 while (modifier_index < 8) : (modifier_index += 1) {
528 var key_index: usize = 0;
529 while (key_index < @as(usize, @intCast(modmap.*.max_keypermod))) : (key_index += 1) {
530 const keycode = modmap.*.modifiermap[modifier_index * @as(usize, @intCast(modmap.*.max_keypermod)) + key_index];
531 if (keycode == numlock_keycode) {
532 self.numlock_mask = @as(c_uint, 1) << @intCast(modifier_index);
533 }
534 }
535 }
536 }
537
538 /// Grabs all configured keybinds and mouse buttons from the X server.
539 /// Replaces any existing grabs. Call after config load or reload.
540 pub fn grabKeybinds(self: *WindowManager) void {
541 self.updateNumlockMask();
542 const modifiers = [_]c_uint{ 0, xlib.LockMask, self.numlock_mask, self.numlock_mask | xlib.LockMask };
543
544 _ = xlib.XUngrabKey(self.display.handle, xlib.AnyKey, xlib.AnyModifier, self.display.root);
545
546 for (self.config.keybinds.items) |keybind| {
547 if (keybind.key_count == 0) continue;
548 const first_key = keybind.keys[0];
549 const keycode = xlib.XKeysymToKeycode(self.display.handle, @intCast(first_key.keysym));
550 if (keycode != 0) {
551 for (modifiers) |modifier| {
552 _ = xlib.XGrabKey(
553 self.display.handle,
554 keycode,
555 first_key.mod_mask | modifier,
556 self.display.root,
557 xlib.True,
558 xlib.GrabModeAsync,
559 xlib.GrabModeAsync,
560 );
561 }
562 }
563 }
564
565 for (self.config.buttons.items) |button| {
566 if (button.click == .client_win) {
567 for (modifiers) |modifier| {
568 _ = xlib.XGrabButton(
569 self.display.handle,
570 @intCast(button.button),
571 button.mod_mask | modifier,
572 self.display.root,
573 xlib.True,
574 xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask,
575 xlib.GrabModeAsync,
576 xlib.GrabModeAsync,
577 xlib.None,
578 xlib.None,
579 );
580 }
581 }
582 }
583
584 std.debug.print("grabbed {d} keybinds from config\n", .{self.config.keybinds.items.len});
585 }
586
587 pub fn ungrabKeybinds(self: *WindowManager) void {
588 _ = xlib.XUngrabKey(self.display.handle, xlib.AnyKey, xlib.AnyModifier, self.display.root);
589 }
590
591 /// Walk the existing window tree and call `manageFn` for each window
592 /// that should be managed. Non-transient windows are processed first
593 /// so transient (dialog) windows can be stacked on top.
594 pub fn scanExistingWindows(self: *WindowManager, manageFn: fn (xlib.Window, *xlib.XWindowAttributes, *WindowManager) void) void {
595 var root_return: xlib.Window = undefined;
596 var parent_return: xlib.Window = undefined;
597 var children: [*c]xlib.Window = undefined;
598 var num_children: c_uint = undefined;
599
600 if (xlib.XQueryTree(self.display.handle, self.display.root, &root_return, &parent_return, &children, &num_children) == 0) {
601 return;
602 }
603
604 var index: c_uint = 0;
605 while (index < num_children) : (index += 1) {
606 var window_attrs: xlib.XWindowAttributes = undefined;
607 if (xlib.XGetWindowAttributes(self.display.handle, children[index], &window_attrs) == 0) {
608 continue;
609 }
610 if (window_attrs.override_redirect != 0) continue;
611
612 var trans: xlib.Window = 0;
613 if (xlib.XGetTransientForHint(self.display.handle, children[index], &trans) != 0) continue;
614
615 if (window_attrs.map_state == IsViewable or self.getState(children[index]) == IconicState) {
616 manageFn(children[index], &window_attrs, self);
617 }
618 }
619
620 index = 0;
621 while (index < num_children) : (index += 1) {
622 var window_attrs: xlib.XWindowAttributes = undefined;
623 if (xlib.XGetWindowAttributes(self.display.handle, children[index], &window_attrs) == 0) {
624 continue;
625 }
626 var trans: xlib.Window = 0;
627 if (xlib.XGetTransientForHint(self.display.handle, children[index], &trans) != 0) {
628 if (window_attrs.map_state == IsViewable or self.getState(children[index]) == IconicState) {
629 manageFn(children[index], &window_attrs, self);
630 }
631 }
632 }
633
634 if (children != null) _ = xlib.XFree(@ptrCast(children));
635 }
636
637 /// Reads the ICCCM WM_STATE property for a window.
638 /// Returns WithdrawnState if the property is absent or malformed.
639 pub fn getState(self: *WindowManager, window: xlib.Window) c_long {
640 var actual_type: xlib.Atom = 0;
641 var actual_format: c_int = 0;
642 var num_items: c_ulong = 0;
643 var bytes_after: c_ulong = 0;
644 var prop: [*c]u8 = null;
645
646 const result = xlib.XGetWindowProperty(
647 self.display.handle,
648 window,
649 self.atoms.wm_state,
650 0,
651 2,
652 xlib.False,
653 self.atoms.wm_state,
654 &actual_type,
655 &actual_format,
656 &num_items,
657 &bytes_after,
658 &prop,
659 );
660
661 if (result != 0 or actual_type != self.atoms.wm_state or num_items < 1) {
662 if (prop != null) _ = xlib.XFree(prop);
663 return WithdrawnState;
664 }
665
666 const state: c_long = @as(*c_long, @ptrCast(@alignCast(prop))).*;
667 _ = xlib.XFree(prop);
668 return state;
669 }
670
671 /// Run the event loop until `self.running` is set to false.
672 ///
673 /// `eventFn` dispatches a single XEvent
674 /// `tickFn` is called once per loop iteration to advance animations
675 pub fn run(
676 self: *WindowManager,
677 eventFn: fn (*xlib.XEvent, *WindowManager) void,
678 tickFn: fn (*WindowManager) void,
679 ) void {
680 var fds = [_]std.posix.pollfd{
681 .{ .fd = self.x11_fd, .events = std.posix.POLL.IN, .revents = 0 },
682 };
683
684 _ = xlib.XSync(self.display.handle, xlib.False);
685
686 while (self.running) {
687 while (xlib.XPending(self.display.handle) > 0) {
688 var event = self.display.nextEvent();
689 eventFn(&event, self);
690 }
691
692 tickFn(self);
693
694 var current_bar = self.bars;
695 while (current_bar) |bar| {
696 bar.updateBlocks(self.io, self.allocator);
697 bar.draw(self.display.handle, self.config);
698 current_bar = bar.next;
699 }
700
701 const poll_timeout: i32 = if (self.scroll_animation.isActive()) 16 else 1000;
702 _ = std.posix.poll(&fds, poll_timeout) catch 0;
703 }
704 }
705
706 /// Reloads the configuration
707 ///
708 /// `loadFn` repopulates `self.config`
709 /// keys/buttons are unbound before reload and rebound after
710 pub fn reloadConfig(
711 self: *WindowManager,
712 loadFn: fn (*WindowManager) void,
713 ) void {
714 std.debug.print("reloading config...\n", .{});
715
716 self.ungrabKeybinds();
717
718 self.config.keybinds.clearRetainingCapacity();
719 self.config.buttons.clearRetainingCapacity();
720 self.config.rules.clearRetainingCapacity();
721 self.config.blocks.clearRetainingCapacity();
722
723 loadFn(self);
724
725 bar_mod.destroyBars(self.bars, self.display.handle);
726 self.bars = null;
727 self.setupBars();
728 self.rebuildBarBlocks();
729
730 self.grabKeybinds();
731 }
732
733 fn rebuildBarBlocks(self: *WindowManager) void {
734 var current_bar = self.bars;
735 while (current_bar) |bar| {
736 bar.clearBlocks();
737 self.populateBarBlocks(bar);
738 current_bar = bar.next;
739 }
740 }
741 };
742
743 fn configHasSystray(config: Config) bool {
744 for (config.blocks.items) |block| {
745 if (block.block_type == .systray) return true;
746 }
747 return false;
748 }
749
750 /// Converts a config block description into a live status bar block.
751 pub fn configBlockToBarBlock(cfg: config_mod.Block) blocks_mod.Block {
752 var block = switch (cfg.block_type) {
753 .static => blocks_mod.Block.initStatic(cfg.format, cfg.color, cfg.underline),
754 .datetime => blocks_mod.Block.initDatetime(
755 cfg.format,
756 cfg.datetime_format orelse "%H:%M",
757 cfg.interval,
758 cfg.color,
759 cfg.underline,
760 ),
761 .ram => blocks_mod.Block.initRam(cfg.format, cfg.interval, cfg.color, cfg.underline),
762 .shell => blocks_mod.Block.initShell(
763 cfg.format,
764 cfg.command orelse "",
765 cfg.interval,
766 cfg.color,
767 cfg.underline,
768 ),
769 .battery => blocks_mod.Block.initBattery(
770 cfg.format_charging orelse "",
771 cfg.format_discharging orelse "",
772 cfg.format_full orelse "",
773 cfg.battery_name orelse "BAT0",
774 cfg.interval,
775 cfg.color,
776 cfg.underline,
777 ),
778 .cpu_temp => blocks_mod.Block.initCpuTemp(
779 cfg.format,
780 cfg.thermal_zone orelse "thermal_zone0",
781 cfg.interval,
782 cfg.color,
783 cfg.underline,
784 ),
785 .systray => blocks_mod.Block.initStatic("", 0, false),
786 };
787 block.click = cfg.click;
788 return block;
789 }