tony / oxwm

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