user_idle_time/
lib.rs

1//! Get the idle time of a user.
2//! The time returned is the time since the last user input event.
3//!
4//! Example:
5//! ```
6//! use user_idle_time::get_idle_time;
7//! let idle = get_idle_time().unwrap();
8//! let idle_seconds = idle.as_secs();
9//! ```
10
11pub type Error = anyhow::Error;
12pub type Result<T> = anyhow::Result<T>;
13
14#[cfg(all(target_os = "linux", feature = "x11"))]
15#[path = "x11_impl.rs"]
16mod idle;
17
18#[cfg(all(target_os = "linux", not(feature = "x11")))]
19#[path = "dbus_impl.rs"]
20mod idle;
21
22#[cfg(target_os = "windows")]
23#[path = "windows_impl.rs"]
24mod idle;
25
26// #[cfg(target_os = "macos")]
27// #[path = "macos_impl.rs"]
28// mod idle;
29
30pub use idle::get_idle_time;
31
32#[expect(clippy::unwrap_used, reason = "unit tests")]
33#[cfg(test)]
34mod test {
35    use std::{thread::sleep, time::Duration};
36
37    use super::get_idle_time;
38
39    const DURATION: Duration = Duration::from_secs(10);
40
41    #[test]
42    // If this test fails, you probably moved your mouse or something while the test was running.
43    fn main() {
44        let idle_before = get_idle_time().unwrap();
45        sleep(DURATION);
46        let idle_after = get_idle_time().unwrap();
47        assert!(idle_after >= idle_before + DURATION,);
48    }
49}