From 967a54323c1cbb02f4d4c43418294d29a7bef5b7 Mon Sep 17 00:00:00 2001 From: Aleksei Bavshin Date: Thu, 23 Mar 2023 00:15:25 -0700 Subject: [PATCH] sway: limit volume boost via helper script Fixes: SIG#22 --- scripts/sway/notify-volume | 30 --------- scripts/sway/volume-helper | 89 +++++++++++++++++++++++++++ sway/config.d/60-bindings-volume.conf | 9 +-- 3 files changed, 94 insertions(+), 34 deletions(-) delete mode 100755 scripts/sway/notify-volume create mode 100755 scripts/sway/volume-helper diff --git a/scripts/sway/notify-volume b/scripts/sway/notify-volume deleted file mode 100755 index 3416c2b..0000000 --- a/scripts/sway/notify-volume +++ /dev/null @@ -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}" diff --git a/scripts/sway/volume-helper b/scripts/sway/volume-helper new file mode 100755 index 0000000..ed6989e --- /dev/null +++ b/scripts/sway/volume-helper @@ -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}" diff --git a/sway/config.d/60-bindings-volume.conf b/sway/config.d/60-bindings-volume.conf index 4415304..85575f2 100644 --- a/sway/config.d/60-bindings-volume.conf +++ b/sway/config.d/60-bindings-volume.conf @@ -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 }