Skip to main content

blyte_cli/
keep_awake.rs

1//! Keeps the machine awake while a dispatch run's child process is alive.
2
3use std::process::{Command, Stdio};
4
5#[cfg(target_os = "macos")]
6pub fn command(pid: u32) -> Option<Command> {
7    let mut cmd = Command::new("/usr/bin/caffeinate");
8    cmd.arg("-i").arg("-w").arg(pid.to_string());
9    Some(cmd)
10}
11
12#[cfg(not(target_os = "macos"))]
13pub fn command(_pid: u32) -> Option<Command> {
14    None
15}
16
17pub fn hold(pid: u32) -> Result<(), String> {
18    if std::env::var("BLYTE_NO_KEEP_AWAKE").as_deref() == Ok("1") {
19        return Ok(());
20    }
21    let Some(mut cmd) = command(pid) else {
22        return Ok(());
23    };
24    cmd.stdin(Stdio::null())
25        .stdout(Stdio::null())
26        .stderr(Stdio::null());
27    cmd.spawn()
28        .map(|_| ())
29        .map_err(|e| format!("could not start /usr/bin/caffeinate: {e}"))
30}