From 73360c9c1e9fe307f6eba7cf9df2a50deefe26ee Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Fri, 13 Aug 2021 16:07:57 +0200 Subject: [PATCH] i3status: pulseaudio is now refreshed on event --- i3status/Cargo.toml | 2 +- i3status/src/workers.rs | 30 ++++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/i3status/Cargo.toml b/i3status/Cargo.toml index 89b0517..b05c90c 100644 --- a/i3status/Cargo.toml +++ b/i3status/Cargo.toml @@ -19,4 +19,4 @@ features = ["derive"] [dependencies.tokio] version = "1.9.0" -features = ["macros", "rt-multi-thread", "time", "sync", "process"] +features = ["macros", "rt-multi-thread", "time", "sync", "process", "io-util"] diff --git a/i3status/src/workers.rs b/i3status/src/workers.rs index 971af44..695b71c 100644 --- a/i3status/src/workers.rs +++ b/i3status/src/workers.rs @@ -4,6 +4,11 @@ use heim::{ memory::os::linux::MemoryExt, units::{frequency::megahertz, information::megabyte, ratio}, }; +use std::process::Stdio; +use tokio::{ + io::{AsyncBufReadExt, BufReader}, + process::Child, +}; use std::{sync::Arc, time::Duration}; use tokio::{process::Command, sync::RwLock}; @@ -40,7 +45,22 @@ pub(crate) async fn time(bar: Arc>) { } pub(crate) async fn pulseaudio_vol(bar: Arc>) { - let mut int = tokio::time::interval(Duration::from_secs(2)); + let child = Command::new("pactl") + .arg("subscribe") + .stdout(Stdio::piped()) + .spawn(); + dbg!(&child); + let stdout = match child { + Ok(Child { + stdout: Some(stdout), + .. + }) => stdout, + _ => { + bar.write().await.vol = String::from("failed to spawn pactl :("); + return; + }, + }; + let mut stdout = BufReader::new(stdout); loop { let res = Command::new("pactl") @@ -85,7 +105,13 @@ pub(crate) async fn pulseaudio_vol(bar: Arc>) { } bar.write().await.vol = msg; - int.tick().await; + loop { + let mut buf = String::new(); + let _ = stdout.read_line(&mut buf).await; + if buf.contains("change") { + break; + } + } } }