scripts/waybar/waybar-volume.sh

58 lines
1.8 KiB
Bash
Raw Normal View History

#!/bin/bash
LOCK=/tmp/volume-lock
if [ -e $LOCK ]; then
exit
fi
touch $LOCK
SINK=$(pactl get-default-sink)
if [ -z $1 ]; then
ACTION="get-sink-volume $SINK"
elif [ $1 == 'up' ]; then
ACTION="set-sink-volume $SINK +1%"
elif [ $1 == 'down' ]; then
ACTION="set-sink-volume $SINK -1%"
else
echo "Invalid argument $1"
fi
if [ -e /tmp/volume-notification ]; then
NOTIFY_ID=$(cat /tmp/volume-notification)
if [ -z /tmp/volume-notification ]; then
rm /tmp/volume-notification
fi
fi
VOLUME=$(pactl get-sink-volume $SINK | grep Volume | sed -r 's/.* (1?[0-9]?[0-9])% \/.*/\1/')
if [[ $ACTION != "set-sink-volume $SINK +1%" ]] && [[ $VOLUME -eq 0 ]]; then
echo $VOLUME > ${HOME}/.local/state/last_volume
elif [[ $ACTION != "set-sink-volume $SINK -1%" ]] && [[ $VOLUME -eq 150 ]]; then
echo $VOLUME > ${HOME}/.local/state/last_volume
elif [[ $ACTION != "get-sink-volume $SINK" ]]; then
pactl $ACTION
VOLUME=$(pactl get-sink-volume $SINK | grep Volume | sed -r 's/.* (1?[0-9]?[0-9])% \/.*/\1/')
fi
if [[ $VOLUME -eq 0 ]]; then
ICON='-off'
elif [[ $VOLUME -le 34 ]]; then
ICON='-low'
elif [[ $VOLUME -le 67 ]]; then
ICON='-medium'
else
ICON='-high'
fi
if [[ -z $NOTIFY_ID ]]; then
NOTIFY_ID=$(notify-send --category=volume --urgency=low --hint=int:value:$VOLUME --icon=${HOME}/.icons/Gruvbox/48x48@2x/devices/notification-audio-volume${ICON}.svg -p -t 1000 Volume ${VOLUME}%)
else
NOTIFY_ID=$(notify-send --category=volume --urgency=low --hint=int:value:$VOLUME --icon=${HOME}/.icons/Gruvbox/48x48@2x/status/notification-audio-volume${ICON}.svg -p -r $NOTIFY_ID -t 1000 Volume ${VOLUME}%)
fi
VOLUME=$(pactl get-sink-volume $SINK | grep Volume | sed -r 's/.* (1?[0-9]?[0-9])% \/.*/\1/')
echo $NOTIFY_ID > /tmp/volume-notification
echo $VOLUME > ${HOME}/.local/state/last_volume
rm /tmp/volume-lock