Visual Studio Code, rebuilt in Rust
Rust
//! shortcut: no powerMonitor yet: active, on AC and no events; port/ledger.md M24 row app/src/power.rs:9
//! (GetLastInputInfo, GetSystemPowerStatus, WM_POWERBROADCAST).
use futures::channel::mpsc::UnboundedSender;
use windows_sys::Win32::Foundation::{CloseHandle, HANDLE, INVALID_HANDLE_VALUE};
use windows_sys::Win32::System::Power::{POWER_REQUEST_TYPE, PowerClearRequest, PowerCreateRequest, PowerRequestDisplayRequired, PowerRequestExecutionRequired, PowerSetRequest};
use windows_sys::Win32::System::SystemServices::POWER_REQUEST_CONTEXT_VERSION;
use windows_sys::Win32::System::Threading::{POWER_REQUEST_CONTEXT_SIMPLE_STRING, REASON_CONTEXT, REASON_CONTEXT_0};
use super::PowerEvent;
pub fn system_idle_time() -> u64 {
0
}
pub(super) fn screen_locked() -> bool {
false
}
/// Electron has a thermal state on macOS only.
pub fn thermal_state() -> &'static str {
"unknown"
}
pub fn on_battery_power() -> bool {
false
}
/// The request handle as an address: BLOCKERS is a static, and a raw pointer is not `Send`.
pub(super) type Assertion = (usize, POWER_REQUEST_TYPE);
/// Chromium's power_save_blocker_win.cc: a power request, display required or execution required.
pub(super) fn create_assertion(blocker_type: &str) -> Option<Assertion> {
let request_type = match blocker_type {
"prevent-display-sleep" => PowerRequestDisplayRequired,
_ => PowerRequestExecutionRequired,
};
let mut reason: Vec<u16> = "Electron".encode_utf16().chain([0]).collect();
let context = REASON_CONTEXT { Version: POWER_REQUEST_CONTEXT_VERSION, Flags: POWER_REQUEST_CONTEXT_SIMPLE_STRING, Reason: REASON_CONTEXT_0 { SimpleReasonString: reason.as_mut_ptr() } };
// SAFETY: `context` and the `reason` buffer it points at are alive for the call.
let request = unsafe { PowerCreateRequest(&context) };
if request == INVALID_HANDLE_VALUE {
return None;
}
// SAFETY: `request` is the valid handle made above.
if unsafe { PowerSetRequest(request, request_type) } == 0 {
// SAFETY: `request` is valid and not used after this.
unsafe { CloseHandle(request) };
return None;
}
Some((request as usize, request_type))
}
pub(super) fn release_assertion((request, request_type): Assertion) {
// SAFETY: `request` is the handle create_assertion returned, closed once here.
unsafe {
PowerClearRequest(request as HANDLE, request_type);
CloseHandle(request as HANDLE);
}
}
pub fn watch(_events: UnboundedSender<PowerEvent>) {}
pub fn watch_accessibility_display(_changed: UnboundedSender<()>) {}