summary history branches tags files
commit:0e58c1b1864e007ee2d9163f6b0fefc6887df12f
author:quantum-byte
committer:Trevor Bentley
date:Mon Feb 13 06:41:30 2023 +0100
parents:dabe4bed73eaed28dda7463e766cfcc0c80c008c
Implemented logic to look for running audio from pulseaudio instances of all active users
diff --git a/.gitignore b/.gitignore
line changes: +1/-0
index 18f44ef..b0db002
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,4 @@
 **/*.rs.bk
 *~
 #*#
+.idea 
\ No newline at end of file

diff --git a/Cargo.lock b/Cargo.lock
line changes: +20/-0
index 5126118..b2ada51
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -50,6 +50,7 @@ dependencies = [
  "regex",
  "rust-ini",
  "time",
+ "users",
 ]
 
 [[package]]
@@ -114,6 +115,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 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"
@@ -216,6 +226,16 @@ dependencies = [
 ]
 
 [[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"

diff --git a/Cargo.toml b/Cargo.toml
line changes: +1/-0
index 1cf3344..a6f9754
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -43,3 +43,4 @@ rust-ini = "0.13.0"
 clap = "3.2.23"
 time = "0.1.45"
 nix = "0.26.1"
+users = "0.11.0"

diff --git a/src/main.rs b/src/main.rs
line changes: +37/-14
index 16edda8..665155d
--- a/src/main.rs
+++ b/src/main.rs
@@ -36,6 +36,9 @@ use nix::sys::signal;
 
 extern crate time;
 
+extern crate users;
+use users::{get_user_by_name};
+
 use std::io::Write;
 use std::process::Stdio;
 use std::process::Command;
@@ -531,23 +534,43 @@ fn exist_audio_alsa() -> ExistResult {
 
 /// 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)
 }