commit 44000a941d686e165897d73a17849c4a4c8f460e
parent f2b1666ae757dec589237d9846334fbcbbcf1c60
Author: Tomas Nemec <owl@gtms.dev>
Date: Wed, 19 Jun 2024 06:40:36 +0200
feat(clock): refresh time every second
Diffstat:
M | src/Loop.zig | | | 67 | ++++++++++++++++++++++++++++++++++++++++++++++--------------------- |
1 file changed, 46 insertions(+), 21 deletions(-)
diff --git a/src/Loop.zig b/src/Loop.zig
@@ -8,6 +8,7 @@ const utils = @import("utils.zig");
const Loop = @This();
const state = &@import("root").state;
+const render = @import("render.zig");
sfd: posix.fd_t,
@@ -45,43 +46,67 @@ 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;
+
+ log.info("loop", .{});
while (true) {
const ret = wayland.display.dispatchPending();
_ = wayland.display.flush();
if (ret == .SUCCESS) break;
}
- _ = posix.poll(&fds, -1) catch |err| {
+ const poll_result = posix.poll(&fds, timeout_ms) catch |err| {
log.err("poll failed: {s}", .{@errorName(err)});
return;
};
- for (fds) |fd| {
- if (fd.revents & posix.POLL.HUP != 0 or fd.revents & posix.POLL.ERR != 0) {
+ if (poll_result > 0) {
+ for (fds) |fd| {
+ if (fd.revents & posix.POLL.HUP != 0 or fd.revents & posix.POLL.ERR != 0) {
+ return;
+ }
+ }
+
+ // signals
+ if (fds[0].revents & posix.POLL.IN != 0) {
return;
}
- }
- // signals
- if (fds[0].revents & posix.POLL.IN != 0) {
- return;
- }
+ // wayland
+ if (fds[1].revents & posix.POLL.IN != 0) {
+ const errno = wayland.display.dispatch();
+ if (errno != .SUCCESS) return;
+ }
+ if (fds[1].revents & posix.POLL.OUT != 0) {
+ const errno = wayland.display.flush();
+ if (errno != .SUCCESS) return;
+ }
- // wayland
- if (fds[1].revents & posix.POLL.IN != 0) {
- const errno = wayland.display.dispatch();
- if (errno != .SUCCESS) return;
- }
- if (fds[1].revents & posix.POLL.OUT != 0) {
- const errno = wayland.display.flush();
- if (errno != .SUCCESS) return;
+ // modules
+ if (modules.pulse) |*mod| if (fds[2].revents & posix.POLL.IN != 0) {
+ log.info("pulse", .{});
+ mod.refresh() catch return;
+ };
}
- // modules
- if (modules.pulse) |*mod| if (fds[2].revents & posix.POLL.IN != 0) {
- log.info("pulse", .{});
- mod.refresh() catch return;
- };
+ current_time = std.time.milliTimestamp();
+ if (current_time - last_tick_time >= 1000) {
+ last_tick_time = current_time;
+
+ 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();
+ }
+ }
+ }
+ }
}
}