|
| 1 | +//! 低功耗闲置跳过判定。 |
| 2 | +//! |
| 3 | +//! 仅在工作阶段结束时查询一次系统空闲时长,不做持续轮询。 |
| 4 | +
|
| 5 | +use std::time::Duration; |
| 6 | + |
| 7 | +const MAX_ALLOWED_ACTIVE_TIME: Duration = Duration::from_secs(8); |
| 8 | + |
| 9 | +pub fn should_skip_break(cycle_elapsed: Duration) -> bool { |
| 10 | + let Some(idle_duration) = current_idle_duration() else { |
| 11 | + return false; |
| 12 | + }; |
| 13 | + should_skip_break_with_idle(cycle_elapsed, idle_duration) |
| 14 | +} |
| 15 | + |
| 16 | +fn should_skip_break_with_idle(cycle_elapsed: Duration, idle_duration: Duration) -> bool { |
| 17 | + if cycle_elapsed <= MAX_ALLOWED_ACTIVE_TIME { |
| 18 | + return false; |
| 19 | + } |
| 20 | + |
| 21 | + idle_duration + MAX_ALLOWED_ACTIVE_TIME >= cycle_elapsed |
| 22 | +} |
| 23 | + |
| 24 | +#[cfg(target_os = "macos")] |
| 25 | +#[allow(unsafe_code)] |
| 26 | +fn current_idle_duration() -> Option<Duration> { |
| 27 | + use std::time::Duration; |
| 28 | + |
| 29 | + type CGEventSourceStateID = u32; |
| 30 | + type CGEventType = u32; |
| 31 | + |
| 32 | + const K_CG_EVENT_SOURCE_STATE_COMBINED_SESSION_STATE: CGEventSourceStateID = 0; |
| 33 | + const K_CG_ANY_INPUT_EVENT_TYPE: CGEventType = !0; |
| 34 | + |
| 35 | + #[link(name = "ApplicationServices", kind = "framework")] |
| 36 | + unsafe extern "C" { |
| 37 | + fn CGEventSourceSecondsSinceLastEventType( |
| 38 | + source: CGEventSourceStateID, |
| 39 | + event_type: CGEventType, |
| 40 | + ) -> f64; |
| 41 | + } |
| 42 | + |
| 43 | + let seconds = unsafe { |
| 44 | + CGEventSourceSecondsSinceLastEventType( |
| 45 | + K_CG_EVENT_SOURCE_STATE_COMBINED_SESSION_STATE, |
| 46 | + K_CG_ANY_INPUT_EVENT_TYPE, |
| 47 | + ) |
| 48 | + }; |
| 49 | + |
| 50 | + if !seconds.is_finite() || seconds.is_sign_negative() { |
| 51 | + return None; |
| 52 | + } |
| 53 | + |
| 54 | + Some(Duration::from_secs_f64(seconds)) |
| 55 | +} |
| 56 | + |
| 57 | +#[cfg(target_os = "windows")] |
| 58 | +#[allow(unsafe_code)] |
| 59 | +fn current_idle_duration() -> Option<Duration> { |
| 60 | + use windows::Win32::System::SystemInformation::GetTickCount; |
| 61 | + use windows::Win32::UI::Input::KeyboardAndMouse::{GetLastInputInfo, LASTINPUTINFO}; |
| 62 | + |
| 63 | + let mut last_input = LASTINPUTINFO { |
| 64 | + cbSize: u32::try_from(std::mem::size_of::<LASTINPUTINFO>()).ok()?, |
| 65 | + ..Default::default() |
| 66 | + }; |
| 67 | + |
| 68 | + if !unsafe { GetLastInputInfo(&raw mut last_input) }.as_bool() { |
| 69 | + return None; |
| 70 | + } |
| 71 | + |
| 72 | + let now = unsafe { GetTickCount() }; |
| 73 | + let idle_ms = now.wrapping_sub(last_input.dwTime); |
| 74 | + Some(Duration::from_millis(u64::from(idle_ms))) |
| 75 | +} |
| 76 | + |
| 77 | +#[cfg(not(any(target_os = "macos", target_os = "windows")))] |
| 78 | +const fn current_idle_duration() -> Option<Duration> { |
| 79 | + None |
| 80 | +} |
| 81 | + |
| 82 | +#[cfg(test)] |
| 83 | +mod tests { |
| 84 | + use super::*; |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn skips_only_when_idle_almost_covers_cycle() { |
| 88 | + let cycle = Duration::from_secs(30 * 60); |
| 89 | + let almost_all_idle = cycle.checked_sub(Duration::from_secs(5)).unwrap(); |
| 90 | + assert!(should_skip_break_with_idle(cycle, almost_all_idle)); |
| 91 | + assert!(!should_skip_break_with_idle( |
| 92 | + cycle, |
| 93 | + cycle.checked_sub(Duration::from_secs(20)).unwrap() |
| 94 | + )); |
| 95 | + } |
| 96 | +} |
0 commit comments