Files
cinny-desktop/src-tauri/src/native/power.rs
T

49 lines
2.0 KiB
Rust
Raw Normal View History

//! P5-46 — System power management (call continuity).
//!
//! Prevents the system from sleeping / turning off the display while a voice or
//! video call is active, then releases the request when the call ends. The web
//! client calls `set_call_active(true|false)` from `useTauriCallPower` as the
//! call-embed atom transitions.
//!
//! Windows: `SetThreadExecutionState`. The request is per-thread and persists
//! until cleared, so we run every set/clear on the **main thread** (via
//! `run_on_main_thread`) to guarantee the clear cancels the matching set even
//! though Tauri commands otherwise run on a pool thread.
//!
//! Other platforms are a no-op for now (macOS would use `IOPMAssertionCreate`,
//! Linux `org.freedesktop.ScreenSaver`/`login1` inhibit) — tracked as a future
//! extension; the command stays cross-platform so the web side is unconditional.
use tauri::AppHandle;
#[tauri::command]
pub fn set_call_active(app: AppHandle, active: bool) {
#[cfg(target_os = "windows")]
{
let _ = app.run_on_main_thread(move || {
use windows::Win32::System::Power::{
SetThreadExecutionState, ES_CONTINUOUS, ES_DISPLAY_REQUIRED, ES_SYSTEM_REQUIRED,
};
let flags = if active {
ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED
} else {
// Clearing to ES_CONTINUOUS alone releases the sleep/display
// requirement while leaving no lingering per-thread state.
ES_CONTINUOUS
};
// Safety: FFI call with no pointers; returns the previous state,
// which we don't need.
unsafe {
SetThreadExecutionState(flags);
}
});
}
#[cfg(not(target_os = "windows"))]
{
// No-op on non-Windows platforms (see module docs). Bind the args so the
// signature stays identical cross-platform and no unused warnings fire.
let _ = (&app, active);
}
}