| 1 | /// Keeps the computer and its display awake while it lives; a call holds one from dial to hang up. |
| 2 | pub struct StayAwake { |
| 3 | #[cfg(windows)] |
| 4 | request: Option<windows::Win32::Foundation::HANDLE>, |
| 5 | #[cfg(not(windows))] |
| 6 | inhibitor: Option<std::process::Child>, |
| 7 | } |
| 8 | |
| 9 | const REASON: &str = "Teams call in progress"; |
| 10 | |
| 11 | #[cfg(windows)] |
| 12 | mod platform { |
| 13 | use windows::Win32::Foundation::{CloseHandle, HANDLE}; |
| 14 | use windows::Win32::System::Power::{POWER_REQUEST_TYPE, PowerClearRequest, PowerCreateRequest, PowerRequestDisplayRequired, PowerRequestSystemRequired, PowerSetRequest}; |
| 15 | use windows::Win32::System::Threading::{POWER_REQUEST_CONTEXT_SIMPLE_STRING, REASON_CONTEXT, REASON_CONTEXT_0}; |
| 16 | use windows_core::PWSTR; |
| 17 | |
| 18 | pub const REQUESTS: [POWER_REQUEST_TYPE; 2] = [PowerRequestSystemRequired, PowerRequestDisplayRequired]; |
| 19 | |
| 20 | pub fn acquire(reason: &str) -> Option<HANDLE> { |
| 21 | let mut wide: Vec<u16> = reason.encode_utf16().chain(Some(0)).collect(); |
| 22 | let context = REASON_CONTEXT { Version: 0, Flags: POWER_REQUEST_CONTEXT_SIMPLE_STRING, Reason: REASON_CONTEXT_0 { SimpleReasonString: PWSTR(wide.as_mut_ptr()) } }; |
| 23 | let request = unsafe { PowerCreateRequest(&context) }.ok()?; |
| 24 | for kind in REQUESTS { |
| 25 | let _ = unsafe { PowerSetRequest(request, kind) }; |
| 26 | } |
| 27 | Some(request) |
| 28 | } |
| 29 | |
| 30 | pub fn release(request: HANDLE) { |
| 31 | for kind in REQUESTS { |
| 32 | let _ = unsafe { PowerClearRequest(request, kind) }; |
| 33 | } |
| 34 | let _ = unsafe { CloseHandle(request) }; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | impl StayAwake { |
| 39 | #[cfg(windows)] |
| 40 | pub fn new() -> Self { |
| 41 | StayAwake { request: platform::acquire(REASON) } |
| 42 | } |
| 43 | |
| 44 | #[cfg(not(windows))] |
| 45 | pub fn new() -> Self { |
| 46 | let inhibitor = std::process::Command::new("systemd-inhibit") |
| 47 | .args(["--what=idle:sleep", "--who=Rusty Teams", &format!("--why={REASON}"), "--mode=block", "sleep", "infinity"]) |
| 48 | .stdin(std::process::Stdio::null()) |
| 49 | .stdout(std::process::Stdio::null()) |
| 50 | .stderr(std::process::Stdio::null()) |
| 51 | .spawn() |
| 52 | .ok(); |
| 53 | StayAwake { inhibitor } |
| 54 | } |
| 55 | |
| 56 | pub fn holding(&self) -> bool { |
| 57 | #[cfg(windows)] |
| 58 | return self.request.is_some(); |
| 59 | #[cfg(not(windows))] |
| 60 | return self.inhibitor.is_some(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | impl Drop for StayAwake { |
| 65 | fn drop(&mut self) { |
| 66 | #[cfg(windows)] |
| 67 | if let Some(request) = self.request.take() { |
| 68 | platform::release(request); |
| 69 | } |
| 70 | #[cfg(not(windows))] |
| 71 | if let Some(mut inhibitor) = self.inhibitor.take() { |
| 72 | let _ = inhibitor.kill(); |
| 73 | let _ = inhibitor.wait(); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // SAFETY: a power request handle is a process-wide kernel object, usable and closable from any thread. |
| 79 | #[cfg(windows)] |
| 80 | unsafe impl Send for StayAwake {} |
| 81 | #[cfg(windows)] |
| 82 | unsafe impl Sync for StayAwake {} |