sway: limit volume boost via helper script

Fixes: SIG#22
This commit is contained in:
Aleksei Bavshin 2023-03-23 00:15:25 -07:00
commit 967a54323c
No known key found for this signature in database
GPG key ID: 4F071603387A382A
3 changed files with 94 additions and 34 deletions

View file

@ -1,30 +0,0 @@
#!/bin/sh
if ! command -v notify-send >/dev/null || ! command -v pactl > /dev/null; then
exit 0;
fi
# pactl output depends on the current locale
export LANG=C.UTF-8 LC_ALL=C.UTF-8
SINK=${1:-@DEFAULT_SINK@}
VOLUME=$(pactl get-sink-volume "$SINK")
# get first percent value
VOLUME=${VOLUME%%%*}
VOLUME=${VOLUME##* }
TEXT="Volume: ${VOLUME}%"
case $(pactl get-sink-mute "$SINK") in
*yes)
TEXT="Volume: muted"
VOLUME=0
;;
esac
notify-send \
--app-name sway \
--expire-time 800 \
--hint string:x-canonical-private-synchronous:volume \
--hint "int:value:$VOLUME" \
--transient \
"${TEXT}"

89
scripts/sway/volume-helper Executable file
View file

@ -0,0 +1,89 @@
#!/bin/sh
if ! command -v pactl >/dev/null; then
exit 0;
fi
# pactl output depends on the current locale
export LANG=C.UTF-8 LC_ALL=C.UTF-8
DEFAULT_STEP=5
LIMIT=${LIMIT:-100}
SINK="@DEFAULT_SINK@"
clamp() {
if [ "$1" -lt 0 ]; then
echo "0"
elif [ "$1" -gt "$LIMIT" ]; then
echo "$LIMIT"
else
echo "$1"
fi
}
get_sink_volume() { # sink
ret=$(pactl get-sink-volume "$1")
# get first percent value
ret=${ret%%%*}
ret=${ret##* }
echo "$ret"
unset ret
}
CHANGE=0
VOLUME=-1
while true; do
case $1 in
--sink)
SINK=${2:-$SINK}
shift;;
-l|--limit)
LIMIT=$((${2:-$LIMIT}))
shift;;
--set-volume)
VOLUME=$(($2))
shift;;
-i|--increase)
CHANGE=$((${2:-$DEFAULT_STEP}))
shift;;
-d|--decrease)
CHANGE=$((-${2:-$DEFAULT_STEP}))
shift;;
*)
break
;;
esac
shift
done
if [ "$CHANGE" -ne 0 ]; then
VOLUME=$(get_sink_volume "$SINK")
VOLUME=$(( VOLUME + CHANGE ))
pactl set-sink-volume "$SINK" "$(clamp "$VOLUME")%"
elif [ "$VOLUME" -ge 0 ]; then
pactl set-sink-volume "$SINK" "$(clamp "$VOLUME")%"
fi
# Display desktop notification
if ! command -v notify-send >/dev/null; then
exit 0;
fi
VOLUME=$(get_sink_volume "$SINK")
TEXT="Volume: ${VOLUME}%"
case $(pactl get-sink-mute "$SINK") in
*yes)
TEXT="Volume: muted"
VOLUME=0
;;
esac
notify-send \
--app-name sway \
--expire-time 800 \
--hint string:x-canonical-private-synchronous:volume \
--hint "int:value:$VOLUME" \
--transient \
"${TEXT}"

View file

@ -3,18 +3,19 @@
#
# Volume increase/decrease step can be customized by setting the `$volume_step`
# variable to a numeric value before including the file.
# Maximum volume boost level can be set with the `$volume_limit` variable.
#
# Requires: pulseaudio-utils
# Recommends: libnotify
set $volume_notification_cmd /usr/libexec/sway/notify-volume
set $volume_helper_cmd /usr/libexec/sway/volume-helper
# Allow volume controls even if the screen is locked
bindsym --locked {
XF86AudioRaiseVolume exec \
'STEP="$volume_step" && pactl set-sink-volume @DEFAULT_SINK@ +${STEP:-5}% && $volume_notification_cmd'
$volume_helper_cmd --limit "$volume_limit" --increase "$volume_step"
XF86AudioLowerVolume exec \
'STEP="$volume_step" && pactl set-sink-volume @DEFAULT_SINK@ -${STEP:-5}% && $volume_notification_cmd'
XF86AudioMute exec pactl set-sink-mute @DEFAULT_SINK@ toggle && $volume_notification_cmd
$volume_helper_cmd --limit "$volume_limit" --decrease "$volume_step"
XF86AudioMute exec pactl set-sink-mute @DEFAULT_SINK@ toggle && $volume_helper_cmd
XF86AudioMicMute exec pactl set-source-mute @DEFAULT_SOURCE@ toggle
}