2zw
rm openbsd bat, change modules position, remove not needed functions due to removal of openbsd readbat
0294f58f15e3ea74f725926f1d056a3da309f1c5
SM <seb.michalk@gmail.com>
2026-02-20 06:51:41 +0000
README.txt | 2 +- build.zig | 4 ++- src/bar.zig | 84 ++++++++++-------------------------------------------------- src/main.zig | 4 +-- 4 files changed, 19 insertions(+), 75 deletions(-) diff --git a/README.txt b/README.txt index 4c691e5..6787c0d 100644 --- a/README.txt +++ b/README.txt @@ -20,7 +20,7 @@ Features Requirements ------------ In order to build 2zw you need: -- Zig compiler (0.13.0 or newer, tested with 0.14.0) +- Zig compiler (0.13.0 or newer, tested with 0.15.1) - Xlib header files - libX11-dev - libXrandr-dev diff --git a/build.zig b/build.zig index 30e164d..68502af 100644 --- a/build.zig +++ b/build.zig @@ -2,7 +2,7 @@ const std = @import("std"); pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); - const optimize = b.standardOptimizeOption(.{}); + const optimize = b.standardOptimizeOption(.{ .preferred_optimize_mode = .ReleaseSmall }); const exe = b.addExecutable(.{ .name = "2zw", .root_module = b.createModule(.{ @@ -12,6 +12,8 @@ pub fn build(b: *std.Build) void { }), }); + exe.root_module.strip = true; + exe.linkLibC(); exe.linkSystemLibrary("X11"); exe.linkSystemLibrary("Xrandr"); diff --git a/src/bar.zig b/src/bar.zig index 4be70bc..3cabe78 100644 --- a/src/bar.zig +++ b/src/bar.zig @@ -216,7 +216,7 @@ pub const Bar = struct { } pub fn addClock(self: *Bar, interval_ms: u64) !void { - try self.addMod("clock", interval_ms, clkUpd, .right); + try self.addMod("clock", interval_ms, clkUpd, .center); } pub fn addBat(self: *Bar, interval_ms: u64) !void { @@ -224,7 +224,7 @@ pub const Bar = struct { } pub fn addWin(self: *Bar, interval_ms: u64) !void { - try self.addMod("window", interval_ms, winUpd, .center); + try self.addMod("window", interval_ms, winUpd, .left); } pub fn setSel(self: *Bar, win: ?C.Window) void { @@ -452,27 +452,7 @@ fn clkUpd(mod: *Mod, alloc: std.mem.Allocator, now_ms: i64) !void { try mod.set(text); } -fn batUpd(mod: *Mod, alloc: std.mem.Allocator, _: i64) !void { - const capacity = readbat(alloc) catch { - try mod.set("N/A"); - return; - }; - - var buffer: [16]u8 = undefined; - const text = try std.fmt.bufPrint(&buffer, "BAT {d}%", .{capacity}); - const owned = try alloc.dupe(u8, text); - defer alloc.free(owned); - try mod.set(owned); -} - -fn readbat(alloc: std.mem.Allocator) !u8 { - return switch (builtin.target.os.tag) { - .openbsd => readbat_openbsd(alloc), - else => readbat_linux(), - }; -} - -fn readbat_linux() !u8 { +fn readbat() !u8 { const capacity_path = "/sys/class/power_supply/BAT0/capacity"; const file = std.fs.openFileAbsolute(capacity_path, .{}) catch |err| { if (err == error.FileNotFound) return error.FileNotFound; @@ -488,55 +468,17 @@ fn readbat_linux() !u8 { return std.fmt.parseInt(u8, content, 10); } -fn readbat_openbsd(alloc: std.mem.Allocator) !u8 { - var child = std.process.Child.init(&.{ "sysctl", "-n", "hw.sensors.acpibat0.raw0" }, alloc); - child.stdout_behavior = .Pipe; - child.stderr_behavior = .Ignore; - try child.spawn(); - defer reapChild(child.id); - - const stdout_file = child.stdout orelse return error.MissingStdout; - defer stdout_file.close(); - - var buf: [128]u8 = undefined; - const read_bytes = try stdout_file.readAll(&buf); - if (read_bytes == 0) return error.EndOfStream; - - const trimmed = std.mem.trim(u8, buf[0..read_bytes], &std.ascii.whitespace); - - // Happy path: sysctl prints "XX.XX%" or "XX%". - if (std.mem.indexOfScalar(u8, trimmed, '%')) |percent_idx| { - const number_slice = trimmed[0..percent_idx]; - return parsePercent(number_slice); - } - - return error.Invalid; -} - -fn reapChild(pid: std.posix.pid_t) void { - var status: c_int = 0; - while (true) { - const rc = c.waitpid(pid, &status, 0); - if (rc == pid) return; - if (rc == 0) continue; - if (rc == -1) { - switch (std.posix.errno(rc)) { - .INTR => continue, - .CHILD => return, - else => return, - } - } else { - return; - } - } -} +fn batUpd(mod: *Mod, alloc: std.mem.Allocator, _: i64) !void { + const capacity = readbat() catch { + try mod.set("N/A"); + return; + }; -fn parsePercent(text: []const u8) !u8 { - const number_text = std.mem.trim(u8, text, &std.ascii.whitespace); - if (number_text.len == 0) return error.Invalid; - const value = try std.fmt.parseFloat(f32, number_text); - const rounded = std.math.clamp(@as(i32, @intFromFloat(value + 0.5)), 0, 100); - return @as(u8, @intCast(rounded)); + var buffer: [16]u8 = undefined; + const text = try std.fmt.bufPrint(&buffer, "BAT {d}%", .{capacity}); + const owned = try alloc.dupe(u8, text); + defer alloc.free(owned); + try mod.set(owned); } var g_bar: ?*Bar = null; diff --git a/src/main.zig b/src/main.zig index 3b71331..9247771 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1350,7 +1350,7 @@ fn onbtnup(_: *C.XEvent) void { mouse.subwindow = 0; } -var shouldquit = false; +var quit = false; var display: *C.Display = undefined; var root: C.Window = undefined; var wc: C.XWindowChanges = undefined; @@ -1508,7 +1508,7 @@ fn cleanup() void { fn run(allocator: std.mem.Allocator) !void { var event: C.XEvent = undefined; - while (!shouldquit) { + while (!quit) { const pending = C.XPending(display); if (pending == 0) { var sleep_ns: u64 = 50 * NS_PER_MS;