aboutsummaryrefslogtreecommitdiff
path: root/extras/HOME
diff options
context:
space:
mode:
authorJosh Rahm <rahm@google.com>2025-10-29 22:59:11 +0000
committerJosh Rahm <rahm@google.com>2025-10-29 23:02:12 +0000
commit448c796940ca138e2265fc63c3024c350166fe57 (patch)
tree39dedf34c44272da6913cc8b31184ec4b3267b3d /extras/HOME
parent1f28055a60975f76a9631dbe0c7deb9264ea2b52 (diff)
downloadrde-448c796940ca138e2265fc63c3024c350166fe57.tar.gz
rde-448c796940ca138e2265fc63c3024c350166fe57.tar.bz2
rde-448c796940ca138e2265fc63c3024c350166fe57.zip
Change volume controls to change the volume of the selected app.
As opposed to always changing the volume of the default sink.
Diffstat (limited to 'extras/HOME')
-rwxr-xr-xextras/HOME/.local/bin/set-app-volume.sh69
1 files changed, 69 insertions, 0 deletions
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