Add script_output to get output from a script.

Check for errors, but otherwise get output, using process.wait()
to wait for all output, etc.
This commit is contained in:
Jeremy Whiting 2023-10-03 10:34:15 -06:00
parent 96b42d1338
commit c06da2f376

View file

@ -24,7 +24,7 @@
*/ */
use std::ffi::OsStr; use std::ffi::OsStr;
use subprocess::{ExitStatus::Exited, Popen, PopenConfig, PopenError}; use subprocess::{ExitStatus::Exited, Popen, PopenConfig, PopenError, Redirection};
use zbus_macros::dbus_interface; use zbus_macros::dbus_interface;
pub struct SMManager { pub struct SMManager {
} }
@ -45,6 +45,20 @@ fn run_script(name: &str, argv: &[impl AsRef<OsStr>]) -> bool {
} }
} }
fn script_output(argv: &[impl AsRef<OsStr>]) -> Result<String, PopenError> {
// Run given command and return the output given
let mut process = Popen::create(argv, PopenConfig {
stdout: Redirection::Pipe,
..Default::default()
})?;
let (output, _err) = process.communicate(None)?;
let _exit_status = process.wait()?;
match output {
Some(output_strings) => Ok(output_strings),
None => Err(PopenError::LogicError("Error getting output"))
}
}
#[dbus_interface(name = "com.steampowered.SteamOSManager1")] #[dbus_interface(name = "com.steampowered.SteamOSManager1")]
impl SMManager { impl SMManager {
const API_VERSION: u32 = 1; const API_VERSION: u32 = 1;
@ -79,6 +93,18 @@ impl SMManager {
run_script("check hardware support", &["jupiter-check-support"]) run_script("check hardware support", &["jupiter-check-support"])
} }
async fn read_als_calibration(&self) -> f32 {
// Run script to get calibration value
let result = script_output(&["/usr/bin/steamos-polkit-helpers/jupiter-get-als-gain"]);
let mut value: f32 = -1.0;
match result {
Ok(as_string) => value = as_string.trim().parse().unwrap(),
Err(message) => println!("Unable to run als calibration script : {}", message),
}
value
}
/// A version property. /// A version property.
#[dbus_interface(property)] #[dbus_interface(property)]
async fn version(&self) -> u32 { async fn version(&self) -> u32 {