Rewrite home to not rely on gamescope running

This commit is contained in:
NGnius (Graham) 2022-11-27 21:52:18 -05:00
parent fb3f2bde0c
commit 35775afd84
3 changed files with 29 additions and 18 deletions

2
Cargo.lock generated
View file

@ -1037,7 +1037,7 @@ dependencies = [
[[package]] [[package]]
name = "usdpl-back" name = "usdpl-back"
version = "0.7.1" version = "0.7.2"
dependencies = [ dependencies = [
"async-recursion", "async-recursion",
"async-trait", "async-trait",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "usdpl-back" name = "usdpl-back"
version = "0.7.1" version = "0.7.2"
edition = "2021" edition = "2021"
license = "GPL-3.0-only" license = "GPL-3.0-only"
repository = "https://github.com/NGnius/usdpl-rs" repository = "https://github.com/NGnius/usdpl-rs"

View file

@ -6,21 +6,32 @@ use std::process::Command;
/// The home directory of the user currently running the Steam Deck UI (specifically: running gamescope). /// The home directory of the user currently running the Steam Deck UI (specifically: running gamescope).
pub fn home() -> Option<PathBuf> { pub fn home() -> Option<PathBuf> {
let pid_out = Command::new("pidof") let who_out = Command::new("who")
.args(["gamescope"])
.output().ok()?; .output().ok()?;
let pid_out_str = String::from_utf8_lossy(pid_out.stdout.as_slice()); let who_str = String::from_utf8_lossy(who_out.stdout.as_slice());
//println!("pidof gamescope: {}", pid_out_str); for login in who_str.split("\n") {
let pid_str = pid_out_str.split(" ").next()?.trim(); let username = login
let uid: String = super::files::read_single(format!("/proc/{}/loginuid", pid_str.trim())).ok()?; .split(" ")
//println!("uid: {}", uid); .next()?
//let pid: u32 = pid_str.parse().ok()?; .trim();
let user_info = Command::new("bash") let path = Path::new("/home").join(username);
.args(["-c", &format!("id {}", uid)]) if path.is_dir() {
.output().ok()?; return Some(path);
let user_out_str = String::from_utf8_lossy(user_info.stdout.as_slice()); }
//println!("loginuid: {}", user_out_str); }
let user_str = user_out_str.split(")").next()?; None
let user = &user_str[user_str.find("(")?+1..]; }
Some(Path::new("/home").join(user))
#[cfg(test)]
mod test {
use super::*;
#[test]
fn home_test() {
let home_opt = home();
assert!(home_opt.is_some(), "Expected valid home to be detected");
let real_home = home_opt.unwrap();
assert!(real_home.exists(), "Received invalid home dir");
println!("Home dir detected as {}", real_home.display());
}
} }