git.lucas.co / cce-compositor
Wayland compositor (wlroots)
git clone https://git.lucas.co/cce-compositor.git

commit18b6d1a5f36f6e0f33e6cb16caaa46ff5c85e400
parent620d15b4ae
authorIsaac Freund <[email protected]>
date2024-12-30 09:21
river: wrap monotonic time > 2^32-1 milliseconds

(cherry picked from commit 6abcc68a198d0246077aaaed8af08db224f16775)

 river/Cursor.zig | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/river/Cursor.zig b/river/Cursor.zig
index 8e542b1..e7b897c 100644
--- a/river/Cursor.zig
+++ b/river/Cursor.zig
@@ -705,10 +705,14 @@ pub fn updateState(cursor: *Cursor) void {
     switch (cursor.mode) {
         .passthrough, .drag => {
             cursor.updateHovered();
-
-            const now = posix.clock_gettime(posix.CLOCK.MONOTONIC) catch @panic("CLOCK_MONOTONIC not supported");
-            const msec: u32 = @intCast(now.sec * std.time.ms_per_s +
-                @divTrunc(now.nsec, std.time.ns_per_ms));
+            const now = posix.clock_gettime(.MONOTONIC) catch @panic("CLOCK_MONOTONIC not supported");
+            // 2^32-1 milliseconds is ~50 days, which is a realistic uptime.
+            // This means that we must wrap if the monotonic time is greater than
+            // 2^32-1 milliseconds and hope that clients don't get too confused.
+            const msec: u32 = @intCast(@rem(
+                now.sec *% std.time.ms_per_s +% @divTrunc(now.nsec, std.time.ns_per_ms),
+                math.maxInt(u32),
+            ));
             cursor.passthrough(msec);
         },
         .ignore, .down, .op => {},