tony / oxwm

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