BlueGit

ict rusty-teams

Files

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

awake.rs

awake.rs 3.6 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 // Die with the call process on Linux so a SIGSEGV does not leave a sleep infinity orphan.
61 let mut command = std::process::Command::new("systemd-inhibit");
62 command
63 .args([
64 "--what=idle:sleep",
65 "--who=Rusty Teams",
66 &format!("--why={REASON}"),
67 "--mode=block",
68 "sleep",
69 "infinity",
70 ])
71 .stdin(std::process::Stdio::null())
72 .stdout(std::process::Stdio::null())
73 .stderr(std::process::Stdio::null());
74 #[cfg(target_os = "linux")]
75 {
76 use std::os::unix::process::CommandExt;
77 // SAFETY: pre_exec runs in the child before exec; PR_SET_PDEATHSIG is process-local.
78 unsafe {
79 command.pre_exec(|| {
80 libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGTERM);
81 Ok(())
82 });
83 }
84 }
85 let inhibitor = command.spawn().ok();
86 StayAwake { inhibitor }
87 }
88
89 pub fn holding(&self) -> bool {
90 #[cfg(windows)]
91 return self.request.is_some();
92 #[cfg(not(windows))]
93 return self.inhibitor.is_some();
94 }
95}
96
97impl Drop for StayAwake {
98 fn drop(&mut self) {
99 #[cfg(windows)]
100 if let Some(request) = self.request.take() {
101 platform::release(request);
102 }
103 #[cfg(not(windows))]
104 if let Some(mut inhibitor) = self.inhibitor.take() {
105 let _ = inhibitor.kill();
106 let _ = inhibitor.wait();
107 }
108 }
109}
110
111// SAFETY: a power request handle is a process-wide kernel object, usable and closable from any thread.
112#[cfg(windows)]
113unsafe impl Send for StayAwake {}
114#[cfg(windows)]
115unsafe impl Sync for StayAwake {}