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

src/server/util.rs (905B)

 1 // SPDX-FileCopyrightText: © 2022 The River Developers
 2 // SPDX-License-Identifier: GPL-3.0-only
 3 
 4 /// CLOCK_MONOTONIC in nanoseconds — the presentation clock's time base
 5 /// (`wlr_output_event_present.when` is on the same clock).
 6 pub fn timestamp_ns() -> u64 {
 7     let ts = timestamp();
 8     ts.tv_sec as u64 * 1_000_000_000 + ts.tv_nsec as u64
 9 }
10 
11 pub fn timestamp() -> libc::timespec {
12     let mut ts = libc::timespec { tv_sec: 0, tv_nsec: 0 };
13     unsafe {
14         if libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts) < 0 {
15             panic!("CLOCK_MONOTONIC not supported");
16         }
17     }
18     ts
19 }
20 
21 pub fn msec_timestamp() -> u32 {
22     let now = timestamp();
23     // 2^32-1 milliseconds is ~50 days.
24     // Wrap it using wrapping arithmetic.
25     let secs_ms = (now.tv_sec as u64).wrapping_mul(1000);
26     let nsecs_ms = (now.tv_nsec as u64) / 1_000_000;
27     secs_ms.wrapping_add(nsecs_ms) as u32
28 }