Skip to main content

hematite/ui/
sleep_inhibitor.rs

1//! Windows sleep inhibitor — prevents the OS from sleeping during active
2//! inference or tool execution. Uses `SetThreadExecutionState` with
3//! `ES_SYSTEM_REQUIRED` so the system stays awake without forcing the display on.
4//!
5//! On non-Windows platforms this is a zero-cost no-op struct.
6//!
7//! Usage: create a `SleepInhibitor` at the start of a model turn; it
8//! automatically restores normal power policy when it drops.
9
10#[cfg(target_os = "windows")]
11mod imp {
12    const ES_CONTINUOUS: u32 = 0x80000000;
13    const ES_SYSTEM_REQUIRED: u32 = 0x00000001;
14
15    #[link(name = "kernel32")]
16    extern "system" {
17        fn SetThreadExecutionState(esFlags: u32) -> u32;
18    }
19
20    pub struct SleepInhibitor;
21
22    impl SleepInhibitor {
23        pub fn acquire() -> Self {
24            unsafe {
25                SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);
26            }
27            Self
28        }
29    }
30
31    impl Drop for SleepInhibitor {
32        fn drop(&mut self) {
33            unsafe {
34                SetThreadExecutionState(ES_CONTINUOUS);
35            }
36        }
37    }
38}
39
40#[cfg(not(target_os = "windows"))]
41mod imp {
42    pub struct SleepInhibitor;
43    impl SleepInhibitor {
44        pub fn acquire() -> Self {
45            Self
46        }
47    }
48}
49
50pub use imp::SleepInhibitor;