| 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::{ |
| 15 | POWER_REQUEST_TYPE, PowerClearRequest, PowerCreateRequest, PowerRequestDisplayRequired, |
| 16 | PowerRequestSystemRequired, PowerSetRequest, |
| 17 | }; |
| 18 | use windows::Win32::System::Threading::{ |
| 19 | POWER_REQUEST_CONTEXT_SIMPLE_STRING, REASON_CONTEXT, REASON_CONTEXT_0, |
| 20 | }; |
| 21 | use windows_core::PWSTR; |
| 22 | |
| 23 | pub const REQUESTS: [POWER_REQUEST_TYPE; 2] = |
| 24 | [PowerRequestSystemRequired, PowerRequestDisplayRequired]; |
| 25 | |
| 26 | pub fn acquire(reason: &str) -> Option<HANDLE> { |
| 27 | let mut wide: Vec<u16> = reason.encode_utf16().chain(Some(0)).collect(); |
| 28 | let context = REASON_CONTEXT { |
| 29 | Version: 0, |
| 30 | Flags: POWER_REQUEST_CONTEXT_SIMPLE_STRING, |
| 31 | Reason: REASON_CONTEXT_0 { |
| 32 | SimpleReasonString: PWSTR(wide.as_mut_ptr()), |
| 33 | }, |
| 34 | }; |
| 35 | let request = unsafe { PowerCreateRequest(&context) }.ok()?; |
| 36 | for kind in REQUESTS { |
| 37 | let _ = unsafe { PowerSetRequest(request, kind) }; |
| 38 | } |
| 39 | Some(request) |
| 40 | } |
| 41 | |
| 42 | pub fn release(request: HANDLE) { |
| 43 | for kind in REQUESTS { |
| 44 | let _ = unsafe { PowerClearRequest(request, kind) }; |
| 45 | } |
| 46 | let _ = unsafe { CloseHandle(request) }; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | impl StayAwake { |
| 51 | #[cfg(windows)] |
| 52 | pub fn new() -> Self { |
| 53 | StayAwake { |
| 54 | request: platform::acquire(REASON), |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | #[cfg(not(windows))] |
| 59 | pub fn new() -> Self { |
| 60 | // `cat` ends on EOF when this process dies, so a crash cannot leave the inhibitor behind. |
| 61 | let inhibitor = std::process::Command::new("systemd-inhibit") |
| 62 | .args([ |
| 63 | "--what=idle:sleep", |
| 64 | "--who=Rusty Teams", |
| 65 | &format!("--why={REASON}"), |
| 66 | "--mode=block", |
| 67 | "cat", |
| 68 | ]) |
| 69 | .stdin(std::process::Stdio::piped()) |
| 70 | .stdout(std::process::Stdio::null()) |
| 71 | .stderr(std::process::Stdio::null()) |
| 72 | .spawn() |
| 73 | .ok(); |
| 74 | StayAwake { inhibitor } |
| 75 | } |
| 76 | |
| 77 | pub fn holding(&self) -> bool { |
| 78 | #[cfg(windows)] |
| 79 | return self.request.is_some(); |
| 80 | #[cfg(not(windows))] |
| 81 | return self.inhibitor.is_some(); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | impl Drop for StayAwake { |
| 86 | fn drop(&mut self) { |
| 87 | #[cfg(windows)] |
| 88 | if let Some(request) = self.request.take() { |
| 89 | platform::release(request); |
| 90 | } |
| 91 | #[cfg(not(windows))] |
| 92 | if let Some(mut inhibitor) = self.inhibitor.take() { |
| 93 | let _ = inhibitor.kill(); |
| 94 | let _ = inhibitor.wait(); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // SAFETY: a power request handle is a process-wide kernel object, usable and closable from any thread. |
| 100 | #[cfg(windows)] |
| 101 | unsafe impl Send for StayAwake {} |
| 102 | #[cfg(windows)] |
| 103 | unsafe impl Sync for StayAwake {} |