From 8289755b8745c43651cfbeaa319074e9c114eff3 Mon Sep 17 00:00:00 2001 From: Beau Hastings Date: Tue, 13 Oct 2020 15:14:38 +0800 Subject: [PATCH] feat: listen mode for PulseAudio (-L) Subscribes to and listens for changes on the provided, or default, PulseAudio sink. When notifications (-n) are enabled, they will be triggered for these events. When the status bar (-t) and signal (-s) are set, the status bar will be signaled to update. Signed-off-by: Beau Hastings --- volume | 76 ++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 58 insertions(+), 18 deletions(-) diff --git a/volume b/volume index 7c2f503..5998a98 100755 --- a/volume +++ b/volume @@ -23,6 +23,18 @@ get_default_sink_name() { pacmd stat | awk -F": " '/^Default sink name: /{print $2}' } +# Get the index of a sink name +# +# Arguments +# Sink name (string) Symbolic name of sink. +get_sink_index() { + local sink="$1" + + pacmd list-sinks | + awk -W posix '/^[ \t*]+index: /{idx = $3} + /^[ \t]+name: / {insink = $2 == "<'$sink'>"; if (insink) { print idx }; exit}' +} + # Get the volume as a percentage. get_volume() { if $opt_use_amixer; then @@ -530,6 +542,25 @@ volume_color() { fi } +# Listens for PulseAudio events +listen() { + local index=$(get_sink_index $sink) + + while read -r event; do + if $opt_notification; then + if is_muted; then + notify_muted + else + notify_volume + fi + fi + + if [ -n "$signal" ] && [ -n "$statusline" ]; then + update_statusline "$signal" "$statusline" + fi + done < <(pactl subscribe | stdbuf -oL grep -e "Event 'change' on sink #${index}") +} + # Display program usage. usage() { echo "Usage: $0 [options] @@ -542,6 +573,7 @@ Options: -e expiration time of notifications, in milliseconds -i increase volume -l use fullcolor instead of symbolic icons + -L listen for changes to a PulseAudio sink (pulseaudio only) -m toggle mute -M specify mixer (ex: Headphone), default Master -n show notifications @@ -607,6 +639,7 @@ COLOR_XOSD_OUTLINE=${COLOR_XOSD_OUTLINE:-"#222222"} opt_decrease_volume=false opt_increase_volume=false +opt_listen=false opt_mute_volume=false opt_notification=false opt_set_volume=false @@ -627,7 +660,7 @@ symbolic_icon_suffix="" output_mode="" notification_method="libnotify" -while getopts ":ac:d:e:hi:lmM:nN:o:ps:S:t:u:v:x:X:y" o; do +while getopts ":ac:d:e:hi:lLmM:nN:o:ps:S:t:u:v:x:X:y" o; do case "$o" in a) opt_use_amixer=true @@ -649,6 +682,9 @@ while getopts ":ac:d:e:hi:lmM:nN:o:ps:S:t:u:v:x:X:y" o; do l) opt_use_fullcolor_icons=true ;; + L) + opt_listen=true + ;; m) opt_mute_volume=true ;; @@ -739,25 +775,29 @@ if [ -n "${symbolic_icon_suffix}" ]; then fi # The options below this line must be last -if ${opt_notification}; then - if is_muted; then - notify_muted - else - notify_volume +if ${opt_listen}; then + listen +else + if ${opt_notification}; then + if is_muted; then + notify_muted + else + notify_volume + fi fi -fi -if [ -n "${signal}" ]; then - if [ -z "${statusline}" ]; then - usage - fi - update_statusline "${signal}" "${statusline}" -else - if [ -n "${statusline}" ]; then - usage + if [ -n "${signal}" ]; then + if [ -z "${statusline}" ]; then + usage + fi + update_statusline "${signal}" "${statusline}" + else + if [ -n "${statusline}" ]; then + usage + fi fi -fi -if [ -n "${output_mode}" ]; then - output_volume + if [ -n "${output_mode}" ]; then + output_volume + fi fi