Skip to main content

system_idle_time/
lib.rs

1/*! # system-idle-time
2Cross-platform Rust library for getting the last user input on the system.
3
4## Supported Platforms
5
6- Windows
7- Linux (X11 and Wayland)
8- macOS
9
10## Example
11
12```rust
13use system_idle_time::get_idle_time;
14
15match get_idle_time() {
16    Ok(idle_time) => println!("Idle time: {} ms", idle_time.as_millis()),
17    Err(e) => eprintln!("Error getting idle time: {}", e),
18}
19```
20*/
21
22#[cfg(target_os = "linux")]
23mod linux;
24#[cfg(target_os = "windows")]
25mod win;
26#[cfg(target_os = "macos")]
27mod macos;
28
29#[cfg(target_os = "linux")]
30use linux::get_idle_time as plat_idle_time;
31#[cfg(target_os = "windows")]
32use win::get_idle_time as plat_idle_time;
33#[cfg(target_os = "macos")]
34use macos::get_idle_time as plat_idle_time;
35
36/// Get system idle time as a `Duration`.
37#[cfg(any(target_os = "linux", target_os = "windows", target_os = "macos"))]
38pub fn get_idle_time() -> Result<std::time::Duration, Box<dyn std::error::Error>> {
39  plat_idle_time()
40}
41
42#[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos")))]
43pub fn get_idle_time() -> Result<std::time::Duration, Box<dyn std::error::Error>> {
44  Err("Unsupported platform".into())
45}