diff options
| -rwxr-xr-x | extras/HOME/.local/bin/set-app-volume.sh | 43 |
1 files changed, 22 insertions, 21 deletions
diff --git a/extras/HOME/.local/bin/set-app-volume.sh b/extras/HOME/.local/bin/set-app-volume.sh index ae19ae2..8080cca 100755 --- a/extras/HOME/.local/bin/set-app-volume.sh +++ b/extras/HOME/.local/bin/set-app-volume.sh @@ -1,37 +1,37 @@ #!/usr/bin/env bash +set -e + # Best-effort script to control volume of the focused application, # falling back to the default sink. -USAGE="Usage: $(basename "$0") --up | --down" +USAGE="Usage: $(basename "$0") WINDOW_ID [--up | --down]" # Get the focused window ID -FOCUSED_WIN_ID=$(xprop -root _NET_ACTIVE_WINDOW | awk '{print $NF}') +FOCUSED_WIN_ID="$1" + +shift; SINK_INPUT_INDEX="" -if [ -n "$FOCUSED_WIN_ID" ] && [ "$FOCUSED_WIN_ID" != "0x0" ]; then +if [ -n "$FOCUSED_WIN_ID" ] && [ "$FOCUSED_WIN_ID" != "0" ]; then # Get the PID of the focused window PID=$(xprop -id "$FOCUSED_WIN_ID" _NET_WM_PID | awk '{print $NF}') if [ -n "$PID" ]; then # Find PulseAudio sink input index for this PID - SINK_INPUT_INDEX=$(pactl list sink-inputs | awk -v pid=""$PID"" ' - BEGIN { RS=""; FS=" -"; index=-1 } - /application.process.id = / { - in_block=0 - for (i=1; i<=NF; i++) { - if ($i ~ /Sink Input #/) { - current_index = substr($i, 14) - } - if ($i ~ "application.process.id = " pid) { - index = current_index - exit - } - } - } - END { if (index != -1) print index } - ') + SINK_INPUT_INDEX=$(pactl list sink-inputs | perl -ne ' + BEGIN { + our $sink_id = -1; + our $maybe_sink_id = -1; + } + if (/Sink Input #(\d+)/) { + $maybe_sink_id = $1; + } + if (/application\.process\.id = "'"$PID"'"/) { + $sink_id = $maybe_sink_id; + } + END { print "$sink_id\n"; } +') fi fi @@ -51,7 +51,7 @@ set_volume() { } TARGET="@DEFAULT_SINK@" -if [ -n "$SINK_INPUT_INDEX" ]; then +if [ -n "$SINK_INPUT_INDEX" -a "$SINK_INPUT_INDEX" -ne -1 ]; then TARGET="SINK INPUT #$SINK_INPUT_INDEX" fi @@ -67,3 +67,4 @@ case $ACTION in exit 1 ;; esac + |