stevee

My wayland statusbar
git clone git://gtms.dev/stevee
Log | Files | Refs | Submodules | README | LICENSE

commit 5f903fa1022f5ae7a45cfde9b6d56883a97cda8d
parent a7c0ffdebf456978521b8a94ff738e801b1a8ef6
Author: Tomas Nemec <owl@gtms.dev>
Date:   Thu,  1 May 2025 18:30:53 +0200

update

Diffstat:
Msrc/Loop.zig | 71+++++++++++++++++++++++++++++++----------------------------------------
Msrc/Modules.zig | 21+++++++++++++--------
Msrc/modules/Backlight.zig | 2+-
Msrc/modules/Battery.zig | 6+++---
Msrc/render.zig | 6+++++-
5 files changed, 53 insertions(+), 53 deletions(-)

diff --git a/src/Loop.zig b/src/Loop.zig @@ -4,7 +4,6 @@ const utils = @import("utils.zig"); const Loop = @This(); const state = &@import("root").state; -const render = @import("render.zig"); sfd: std.posix.fd_t, @@ -36,6 +35,11 @@ pub fn run(self: *Loop) !void { .revents = undefined, }, .{ + .fd = if (modules.backlight) |mod| mod.fd else -1, + .events = std.posix.POLL.IN, + .revents = undefined, + }, + .{ .fd = if (modules.battery) |mod| mod.fd else -1, .events = std.posix.POLL.IN, .revents = undefined, @@ -47,13 +51,7 @@ pub fn run(self: *Loop) !void { }, }; - var last_tick_time = std.time.milliTimestamp(); while (true) { - var current_time = std.time.milliTimestamp(); - const elapsed_time = current_time - last_tick_time; - var timeout_ms: i32 = @intCast(1000 - elapsed_time); - if (timeout_ms < 0) timeout_ms = 0; - while (true) { const ret = wayland.display.dispatchPending(); _ = wayland.display.flush(); @@ -69,42 +67,35 @@ pub fn run(self: *Loop) !void { if (fd.revents & std.posix.POLL.HUP != 0 or fd.revents & std.posix.POLL.ERR != 0) { return; } - - // wayland - if (fds[1].revents & std.posix.POLL.IN != 0) { - const errno = wayland.display.dispatch(); - if (errno != .SUCCESS) return; - } - if (fds[1].revents & std.posix.POLL.OUT != 0) { - const errno = wayland.display.flush(); - if (errno != .SUCCESS) return; - } - - // modules - if (modules.pulse) |*mod| if (fds[2].revents & std.posix.POLL.IN != 0) { - std.log.info("pulse", .{}); - mod.refresh() catch return; - }; - - if (modules.battery) |*mod| if (fds[3].revents & std.posix.POLL.IN != 0) { - std.log.info("battery", .{}); - mod.refresh() catch return; - }; } - current_time = std.time.milliTimestamp(); - if (current_time - last_tick_time >= 1000) { - last_tick_time = current_time; + // signals + if (fds[0].revents & std.posix.POLL.IN != 0) { + return; + } - for (wayland.monitors.items) |monitor| { - if (monitor.bar) |bar| { - if (bar.configured) { - render.renderClock(bar) catch continue; - bar.clock.surface.commit(); - bar.background.surface.commit(); - } - } - } + // wayland + if (fds[1].revents & std.posix.POLL.IN != 0) { + const errno = wayland.display.dispatch(); + if (errno != .SUCCESS) return; + } + if (fds[1].revents & std.posix.POLL.OUT != 0) { + const errno = wayland.display.flush(); + if (errno != .SUCCESS) return; } + + // modules + if (modules.backlight) |*mod| if (fds[2].revents & std.posix.POLL.IN != 0) { + std.log.info("backlight", .{}); + mod.refresh() catch return; + }; + if (modules.battery) |*mod| if (fds[3].revents & std.posix.POLL.IN != 0) { + std.log.info("battery", .{}); + mod.refresh() catch return; + }; + if (modules.pulse) |*mod| if (fds[4].revents & std.posix.POLL.IN != 0) { + std.log.info("pulse", .{}); + mod.refresh() catch return; + }; } } diff --git a/src/Modules.zig b/src/Modules.zig @@ -4,15 +4,16 @@ const ArrayList = std.ArrayList; const Modules = @This(); -const Pulse = @import("modules/Pulse.zig"); +const Backlight = @import("modules/Backlight.zig"); const Battery = @import("modules/Battery.zig"); +const Pulse = @import("modules/Pulse.zig"); var state = &@import("root").state; -const Tag = enum { pulse, battery }; +const Tag = enum { backlight, battery, pulse }; +backlight: ?Backlight = null, battery: ?Battery = null, - pulse: ?Pulse = null, order: ArrayList(Tag), @@ -21,19 +22,23 @@ pub fn init() Modules { } pub fn deinit(self: *Modules) void { - if (self.pulse) |*mod| mod.deinit(); + if (self.backlight) |*mod| mod.deinit(); if (self.battery) |*mod| mod.deinit(); + if (self.pulse) |*mod| mod.deinit(); self.order.deinit(); } pub fn register(self: *Modules, name: []const u8) !void { - if (mem.eql(u8, name, "pulse")) { - self.pulse = try Pulse.init(); - try self.pulse.?.start(); - try self.order.append(.pulse); + if (mem.eql(u8, name, "backlight")) { + self.backlight = try Backlight.init(); + try self.order.append(.backlight); } else if (mem.eql(u8, name, "battery")) { self.battery = try Battery.init(); try self.order.append(.battery); + } else if (mem.eql(u8, name, "pulse")) { + self.pulse = try Pulse.init(); + try self.pulse.?.start(); + try self.order.append(.pulse); } else { return error.UnknownModule; } diff --git a/src/modules/Backlight.zig b/src/modules/Backlight.zig @@ -69,7 +69,7 @@ pub fn print(self: *Backlight, writer: anytype) !void { percent /= @as(f64, @floatFromInt(device.max)); const value = @as(u8, @intFromFloat(@round(percent))); - try writer.print("💡 {d}%", .{value}); + try writer.print(" {d}%", .{value}); } fn updateDevices( diff --git a/src/modules/Battery.zig b/src/modules/Battery.zig @@ -63,14 +63,14 @@ pub fn print(self: *Battery, writer: anytype) !void { var icon: []const u8 = "❓"; if (std.mem.eql(u8, device.status, "Discharging")) { - icon = "🔋"; + icon = "󰁹"; } else if (std.mem.eql(u8, device.status, "Charging")) { - icon = "🔌"; + icon = "󰂄"; } else if (std.mem.eql(u8, device.status, "Full")) { icon = "⚡"; } - try std.fmt.format(writer, "{s} {d}%", .{ icon, device.capacity }); + try std.fmt.format(writer, "{s} {d}%", .{ icon, device.capacity }); } pub fn refresh(self: *Battery) !void { diff --git a/src/render.zig b/src/render.zig @@ -10,6 +10,8 @@ const Bar = @import("Bar.zig"); const Tag = @import("Tags.zig").Tag; const utils = @import("utils.zig"); +const Backlight = @import("modules/Backlight.zig"); +const Battery = @import("modules/Battery.zig"); const Pulse = @import("modules/Pulse.zig"); const state = &@import("root").state; @@ -105,10 +107,12 @@ pub fn renderModules(bar: *Bar) !void { for (state.modules.order.items) |tag| { try writer.print(" | ", .{}); switch (tag) { - .pulse => try state.modules.pulse.?.print(writer), + .backlight => try state.modules.backlight.?.print(writer), .battery => try state.modules.battery.?.print(writer), + .pulse => try state.modules.pulse.?.print(writer), } } + try writer.print(" ", .{}); // ut8 encoding const runes = try utils.toUtf8(state.gpa, string.items);