Implemented logic to look for running audio from pulseaudio instances of all active users
**/*.rs.bk
*~
#*#
+.idea
\ No newline at end of file
"regex",
"rust-ini",
"time",
+ "users",
]
[[package]]
checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
[[package]]
+name = "log"
+version = "0.4.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
+dependencies = [
+ "cfg-if",
+]
+
+[[package]]
name = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
]
[[package]]
+name = "users"
+version = "0.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "24cc0f6d6f267b73e5a2cadf007ba8f9bc39c6a6f9666f8cf25ea809a153b032"
+dependencies = [
+ "libc",
+ "log",
+]
+
+[[package]]
name = "wasi"
version = "0.10.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
clap = "3.2.23"
time = "0.1.45"
nix = "0.26.1"
+users = "0.11.0"
extern crate time;
+extern crate users;
+use users::{get_user_by_name};
+
use std::io::Write;
use std::process::Stdio;
use std::process::Command;
/// Determine whether audio is actively playing on any Pulseaudio interface.
fn exist_audio_pulseaudio() -> ExistResult {
- let mut count = 0;
- let mut cat_output = Command::new("pacmd")
- .arg("list-sink-inputs")
+ let users_output = Command::new("users")
.stderr(Stdio::null())
- .stdout(Stdio::piped()).spawn()?;
- let _ = cat_output.wait()?;
- let stdout = cat_output.stdout
- .ok_or(CircadianError("pacmd failed".to_string()))?;
- let output = Command::new("grep")
- .arg("state: RUNNING") // Does not includes CORKED == Paused audio
- .stdin(stdout)
- .output()?;
- let output_str = String::from_utf8(output.stdout)?;
- let lines: Vec<&str> = output_str.split("\n")
+ .output();
+ let users_stdout = users_output
+ .map_err(| _ | CircadianError("users failed".to_string()));
+ let users_output_str = String::from_utf8(users_stdout?.stdout)?;
+ let active_users: Vec<&str> = users_output_str.split(" ")
.filter(|l| l.len() > 0)
.collect();
- count += lines.len();
+
+ let mut count = 0;
+ for active_user in active_users {
+ match get_user_by_name(&active_user.trim()) {
+ Some(x) => {
+ let active_user_id = x.uid();
+ let mut pacmd_output = Command::new("pacmd").uid(active_user_id)
+ .env("XDG_RUNTIME_DIR", format!("/run/user/{}", active_user_id))
+ .arg("list-sink-inputs")
+ .stderr(Stdio::null())
+ .stdout(Stdio::piped()).spawn()?;
+ let _ = pacmd_output.wait()?;
+ let stdout = pacmd_output.stdout
+ .ok_or(CircadianError("pacmd failed".to_string()))?;
+ let output = Command::new("grep")
+ .arg("state: RUNNING") // Does not includes CORKED == Paused audio
+ .stdin(stdout)
+ .output()?;
+ let output_str = String::from_utf8(output.stdout)?;
+ let lines: Vec<&str> = output_str.split("\n")
+ .filter(|l| l.len() > 0)
+ .collect();
+ count += lines.len();
+ },
+ None => continue,
+ }
+
+ }
Ok(count > 0)
}