aboutsummaryrefslogtreecommitdiff
path: root/extras
diff options
context:
space:
mode:
authorJosh Rahm <rahm@google.com>2025-10-30 11:03:47 -0600
committerJosh Rahm <rahm@google.com>2025-10-30 11:03:47 -0600
commit6d13431bc1d26a6489fa99cfbaaaa9d9ed7ff47d (patch)
treec3f98df8148902a8977a6ce1cf689365e7d7ba60 /extras
parent448c796940ca138e2265fc63c3024c350166fe57 (diff)
downloadrde-6d13431bc1d26a6489fa99cfbaaaa9d9ed7ff47d.tar.gz
rde-6d13431bc1d26a6489fa99cfbaaaa9d9ed7ff47d.tar.bz2
rde-6d13431bc1d26a6489fa99cfbaaaa9d9ed7ff47d.zip
Update the set-app-volume.sh
Diffstat (limited to 'extras')
-rwxr-xr-xextras/HOME/.local/bin/set-app-volume.sh43
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
+