From 448c796940ca138e2265fc63c3024c350166fe57 Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Wed, 29 Oct 2025 22:59:11 +0000 Subject: Change volume controls to change the volume of the selected app. As opposed to always changing the volume of the default sink. --- extras/HOME/.local/bin/set-app-volume.sh | 69 ++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100755 extras/HOME/.local/bin/set-app-volume.sh (limited to 'extras') diff --git a/extras/HOME/.local/bin/set-app-volume.sh b/extras/HOME/.local/bin/set-app-volume.sh new file mode 100755 index 0000000..ae19ae2 --- /dev/null +++ b/extras/HOME/.local/bin/set-app-volume.sh @@ -0,0 +1,69 @@ +#!/usr/bin/env bash + +# Best-effort script to control volume of the focused application, +# falling back to the default sink. + +USAGE="Usage: $(basename "$0") --up | --down" + +# Get the focused window ID +FOCUSED_WIN_ID=$(xprop -root _NET_ACTIVE_WINDOW | awk '{print $NF}') + +SINK_INPUT_INDEX="" +if [ -n "$FOCUSED_WIN_ID" ] && [ "$FOCUSED_WIN_ID" != "0x0" ]; 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 } + ') + fi +fi + +ACTION=$1 +CHANGE="5%" + +set_volume() { + local target=$1 + local diff=$2 + if [[ "$target" == *#* ]]; then + echo "Setting volume for Sink Input $target ($diff)" >&2 + pactl set-sink-input-volume "${target#SINK INPUT #}" "$diff" + else + echo "Setting default sink volume ($diff)" >&2 + pactl set-sink-volume "$target" "$diff" + fi +} + +TARGET="@DEFAULT_SINK@" +if [ -n "$SINK_INPUT_INDEX" ]; then + TARGET="SINK INPUT #$SINK_INPUT_INDEX" +fi + +case $ACTION in + --up) + set_volume "$TARGET" "+$CHANGE" + ;; + --down) + set_volume "$TARGET" "-$CHANGE" + ;; + *) + echo "$USAGE" >&2 + exit 1 + ;; +esac -- cgit