config_sway/bin/sway_volume

117 lines
2.9 KiB
Text
Raw Normal View History

#!/bin/bash
2021-01-04 18:46:33 +01:00
source $HOME/.config/sway/bin/sway_helpers
ICON_PATH=$HOME/.local/share/icons/Tela-circle-dark/16/actions
2021-02-24 11:30:20 +01:00
NOTIFY_APP="dunstify -a volume -h string:x-dunst-stack-tag:volume"
2021-03-16 12:50:55 +01:00
APP="pavucontrol"
SOUND=$HOME/.config/sway/bin/audio-volume-change.oga
# echo -e '\u25cf'
VOL1="●"
# echo -e '\u25cc'
VOL2="◌"
2024-12-27 11:33:16 +01:00
function _get_default_sink() {
pactl info | grep "Default Sink" | cut -d ' ' -f 3
}
function _get_sink_nick() {
pactl -f json list sinks | jq -c ".[] | select(.name | contains(\"$1\")) | .properties.\"device.nick\"" | sed 's/"//g'
}
function notify() {
2024-12-27 11:33:16 +01:00
SINK=$(_get_default_sink)
2021-05-14 12:00:51 +02:00
SINK_VOLUME=$(pactl list sinks | grep "Name: $SINK" -A 8 | grep -oE '[0-9]*%' | head -n 1 | sed 's/%//')
SOURCE_VOLUME=$(pactl list sources | grep "Name: $SOURCE" -A 8 | grep -oE '[0-9]*%' | head -n 1 | sed 's/%//')
MUTE=$(pactl list sinks | grep "Name: $SINK" -A 8 | grep 'Mute:' | cut -d " " -f 2)
2021-05-14 12:00:51 +02:00
SOURCE_MUTE=$(pactl list sources | grep "Name: $SOURCE" -A 8 | grep 'Mute:' | cut -d " " -f 2)
2024-12-27 11:33:16 +01:00
NICK=$(_get_sink_nick $SINK)
2021-05-14 12:00:51 +02:00
ICON=$ICON_PATH/audio-volume-high.svg
SINK_ICON=""
if [ "$MUTE" = "yes" ]; then
ICON=$ICON_PATH/audio-volume-muted.svg
2021-05-14 12:00:51 +02:00
elif [ $SINK_VOLUME -le 33 ]; then
2021-03-10 13:55:20 +01:00
ICON=$ICON_PATH/audio-volume-low.svg
2021-05-14 12:00:51 +02:00
SINK_ICON=""
elif [ $SINK_VOLUME -le 67 ]; then
2021-03-10 13:55:20 +01:00
ICON=$ICON_PATH/audio-volume-medium.svg
2021-05-14 12:00:51 +02:00
SINK_ICON=""
fi
SOURCE_ICON=""
if [ "$SOURCE_MUTE" = "yes" ]; then
SOURCE_ICON=""
2021-03-10 13:55:20 +01:00
fi
2021-05-14 12:00:51 +02:00
TITLE="${SINK_ICON} ${SINK_VOLUME}% / ${SOURCE_ICON} ${SOURCE_VOLUME}%"
2021-02-18 16:24:25 +01:00
# $NOTIFY_APP -i $ICON -a "Volume" "$TEXT"
2024-12-27 11:33:16 +01:00
$NOTIFY_APP -i $ICON "$TITLE" "$NICK\n$(getProgressString 10 "$VOL1" "$VOL2" $SINK_VOLUME)"
}
function _toggle_sink() {
sink_next=$(pactl list sinks | grep node\.name | grep -oE '"[a-zA-Z0-9_.-]*"' | grep -v $1 | sed 's/"//g' | sort -R)
pactl set-default-sink $sink_next
}
## helpers
2024-12-27 11:33:16 +01:00
function _toggle_app() {
a=$1
if [ $(pgrep $a) ]; then
killall $a
else
$a &> /dev/null &
fi
}
## main
2024-12-27 11:33:16 +01:00
SINK=$(pactl info | grep "Default Sink" | cut -d ' ' -f 3)
SOURCE=$(pactl info | grep "Default Source" | cut -d ' ' -f 3)
if [ -z "$SINK" ]; then
_log VOLUME ERROR args: $*
exit 1
fi
case $1 in
2021-05-14 12:00:51 +02:00
# decrease
d*)
pactl set-sink-volume $SINK -5%
paplay $SOUND &
notify
;;
2021-05-14 12:00:51 +02:00
# increase
i*)
VOLUME=$(pactl list sinks | grep "Name: $SINK" -A 8 | grep -oE '[0-9]*%' | head -n 1 | sed 's/%//')
INC=$((100 - $VOLUME))
if [ $INC -gt 5 ]; then
INC=5
fi
pactl set-sink-volume $SINK +${INC}%
paplay $SOUND &
notify
;;
2021-05-14 12:00:51 +02:00
# mute
m*)
pactl set-sink-mute $SINK toggle
paplay $SOUND &
notify
;;
2021-05-14 12:00:51 +02:00
# source-mute
s*)
pactl set-source-mute $SOURCE toggle
paplay $SOUND &
notify
;;
2024-12-27 11:33:16 +01:00
# toggle between sinks
t*)
_toggle_sink $SINK
notify
;;
2021-05-14 12:00:51 +02:00
# show gui
2024-12-27 11:33:16 +01:00
g*)
_check_app $APP
2024-12-27 11:33:16 +01:00
_toggle_app $APP
;;
*)
echo "invalid command: \"$1\""
echo "arguments: [decrease|increase|mute|gui]"
;;
esac