BlueGit

ict rusty-teams

Files

rusty-teams / crates / calling / src / awake.rs

awake.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
awake.rs 3.1 KiB Download Raw
1/// Keeps the computer and its display awake while it lives; a call holds one from dial to hang up.
2pub struct StayAwake {
3 #[cfg(windows)]
4 request: Option<windows::Win32::Foundation::HANDLE>,
5 #[cfg(not(windows))]
6 inhibitor: Option<std::process::Child>,
7}
8
9const REASON: &str = "Teams call in progress";
10
11#[cfg(windows)]
12mod 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
50impl 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 let inhibitor = std::process::Command::new("systemd-inhibit")
61 .args([
62 "--what=idle:sleep",
63 "--who=Rusty Teams",
64 &format!("--why={REASON}"),
65 "--mode=block",
66 "sleep",
67 "infinity",
68 ])
69 .stdin(std::process::Stdio::null())
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
85impl 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)]
101unsafe impl Send for StayAwake {}
102#[cfg(windows)]
103unsafe impl Sync for StayAwake {}