volumectl/volumectl

279 lines
8.8 KiB
Bash
Executable File

#!/usr/bin/env bash
## Default controller values
VOLUME_CONTROLLER="pamixer"
INCREASE_FLAG="--increase"
DECREASE_FLAG="--decrease"
SET_VOLUME_FLAG="--set-volume"
SET_MUTE_FLAG="--mute"
SET_UNMUTE_FLAG="--unmute"
TOGGLE_MUTE_FLAG="--toggle-mute"
GET_VOLUME_FLAG="--get-volume"
GET_MUTE_FLAG="--get-mute"
ADDITIONAL_FLAGS=("--allow-boost")
## Default values
NOTIFICATION_ENABLED=1
NOTIFICATION_TIMEOUT=3000
CONFIG_PATH="$XDG_CONFIG_HOME/volumectl/config.ini"
VERBOSE=0
NORMAL_DELTA=5
BIG_DELTA=10
VOLUME_MAX=100
VOLUME_MIN=5
## Args
OPTIONAL_FLAGS=("--notification-timeout" "-v" "--verbose" "-c" "--config" "-n" "--notifcation" "-N" "--no-notification" "-h" "--help")
verbose () {
[[ $VERBOSE -eq 1 ]] && return 0 || return 1
}
notify () {
volume=$(get_volume)
muted=$(get_mute)
if [[ $muted = "true" ]]; then
icon="audio-volume-muted"
elif [[ $volume -eq 0 ]]; then
icon="audio-volume-low"
elif [[ $volume -gt 0 ]] && [[ $volume -lt 50 ]]; then
icon="audio-volume-medium"
elif [[ $volume -ge 50 ]]; then
icon="audio-volume-high"
fi
local cmd="dunstify -h int:value:$volume -t $NOTIFICATION_TIMEOUT -h string:x-dunst-stack-tag:volume -i \"$icon\" \"Volume ($volume%)\""
[[ $NOTIFICATION_ENABLED -eq 1 ]] && sh -c "$cmd"
verbose && ( [[ $NOTIFICATION_ENABLED -eq 1 ]] && echo "Notified with \`$cmd\`" || echo "Didn't notify" )
}
volume_up_by () {
local cmd="$VOLUME_CONTROLLER ${ADDITIONAL_FLAGS[@]} $INCREASE_FLAG $1"
verbose && echo "Increasing volume by $1, with \`$cmd\`"
verbose && echo "Volume before command: $(get_volume)"
sh -c "$cmd"
verbose && echo "Volume after command: $(get_volume)"
notify
}
volume_down_by () {
local cmd="$VOLUME_CONTROLLER ${ADDITIONAL_FLAGS[@]} $DECREASE_FLAG $1"
verbose && echo "Decreasing volume by $1, with \`$cmd\`"
verbose && echo "Volume before command: $(get_volume)"
sh -c "$cmd"
verbose && echo "Volume after command: $(get_volume)"
notify
}
volume_set () {
local cmd="$VOLUME_CONTROLLER ${ADDITIONAL_FLAGS[@]} $SET_VOLUME_FLAG $1"
verbose && echo "Setting the volume to $1, with \`$cmd\`"
verbose && echo "Volume before command: $(get_volume)"
sh -c "$cmd"
verbose && echo "Volume after command: $(get_volume)"
notify
}
volume_big_up () {
local cmd="volume_up_by $BIG_DELTA"
verbose && echo "Increasing the volume alot, with \`$cmd\`"
$cmd
}
volume_big_down () {
local cmd="volume_down_by $BIG_DELTA"
verbose && echo "Decreasing the volume alot, with \`$cmd\`"
$cmd
}
volume_up () {
local cmd="volume_up_by $NORMAL_DELTA"
verbose && echo "Increasing the volume, with \`$cmd\`"
$cmd
}
volume_down () {
local cmd="volume_down_by $NORMAL_DELTA"
verbose && echo "Decreasing the volume, with \`$cmd\`"
$cmd
}
toggle_mute() {
local cmd="$VOLUME_CONTROLLER $TOGGLE_MUTE_FLAG"
verbose && echo "Toggling the mute, with \`$cmd\`"
verbose && echo "Mute state before command: $(get_mute)"
sh -c "$cmd"
verbose && echo "Mute state after command: $(get_mute)"
notify
}
mute () {
local cmd="$VOLUME_CONTROLLER $SET_MUTE_FLAG"
verbose && echo "Setting the mute on, with \`$cmd\`"
verbose && echo "Mute state before command: $(get_mute)"
sh -c "$cmd"
verbose && echo "Mute state after command: $(get_mute)"
notify
}
unmute () {
local cmd="$VOLUME_CONTROLLER $SET_UNMUTE_FLAG"
verbose && echo "Setting the mute off, with \`$cmd\`"
verbose && echo "Mute state before command: $(get_mute)"
sh -c "$cmd"
verbose && echo "Mute state after command: $(get_mute)"
notify
}
get_volume () {
sh -c "$VOLUME_CONTROLLER ${ADDITIONAL_FLAGS[@]} $GET_VOLUME_FLAG"
}
get_mute () {
sh -c "$VOLUME_CONTROLLER ${ADDITIONAL_FLAGS[@]} $GET_MUTE_FLAG"
}
usage() {
cat << EOF
volumectl - A configurable bash script that makes your life easier in controlling volume and notifying it
at the same time.
Usage: volumectl [-v|--verbose] [-c|--config path] [-n|-N] -option [arg]
Options:
-i [arg], --increase [arg] increase the volume by the "normal" amount or by the [arg] amount
-d [arg], --decrease [arg] decrease the volume by the "normal" amount or by the [arg] amount
-I, --big-increase increase the volume by the "big" amount
-D, --big-decrease decrease the volume by the "big" amount
-s arg, --set arg set volume to arg
--set-min-volume set volume to minimum
--set-max-volume set volume to maximum
-m, --mute mute
-u, --unmute unmute
-t, --toggle toggle mute
-g, --get-volume get volume
-G, --get-mute get mute
Optional arguments:
--notification-timeout arg forces the notification timeout, in milliseconds (overrides the configuation setting,
the default value is $NOTIFICATION_TIMEOUT)
-n, --notification forces the notification to appear (overrides the configuration setting)
-N, --no-notification forces the notification NOT to appear (overrides the configuration setting)
-v, --verbose print each step the script passes through
-h, -?, --help show this message
EOF
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then #if '__name__' == '__main__'
# check if the notification is being forced or not
while [[ " ${OPTIONAL_FLAGS[@]} " =~ " $1 " ]]; do
verbose && echo "Checking for optional arguments"
case $1 in
-v|--verbose)
VERBOSE=1
verbose && echo "Setting \$VERBOSE to true (\$VERBOSE=$VERBOSE)"
shift
;;
--notification-timeout)
NOTIFICATION_TIMEOUT="$2";
verbose && echo "Setting \$NOTIFICATION_TIMEOUT to $2 (\$NOTIFICATION_TIMEOUT=$NOTIFICATION_TIMEOUT)"
shift 2
;;
-c|--config)
CONFIG_PATH="$2";
verbose && echo "Setting \$CONFIG_PATH to $2 (\$CONFIG_PATH=$CONFIG_PATH)"
shift 2
;;
-n|--notification)
NOTIFICATION_ENABLED=1
verbose && echo "Setting \$NOTIFICATION_ENABLED to true (\$NOTIFICATION_ENABLED=$NOTIFICATION_ENABLED)"
shift
;;
-N|--no-notification)
NOTIFICATION_ENABLED=0;
verbose && echo "Setting \$NOTIFICATION_ENABLED to false (\$NOTIFICATION_ENABLED=$NOTIFICATION_ENABLED)"
shift
;;
-h|-\?|--help)
verbose && echo "Detected -h, -? or --help"
usage
shift
;;
esac
done
verbose && echo "Looking at the argument '$@'"
case $1 in
-i|--increase)
verbose && echo "Detected -i or --increase, with argument '$2'"
[[ -z "$2" ]] && volume_up || volume_up_by $2
;;
-I|--big-increase)
verbose && echo "Detected -I or --big-increase"
volume_big_up
;;
-d|--decrease)
verbose && echo "Detected -d or --decrease, with argument '$2'"
[[ -z "$2" ]] && volume_down || volume_down_by $2
;;
-D|--big-decrease)
verbose && echo "Detected -D or --big-decrease"
volume_big_down
;;
-s|--set-volume)
verbose && echo "Detected -s or --set-volume, with argument '$2'"
[[ -z "$2" ]] && echo "Error, missing value to set volume" 1>&2 && exit 1 || volume_set $2
;;
--set-min-volume)
verbose && echo "Detected --set-min-volume"
volume_set $VOLUME_MIN
;;
--set-max-volume)
verbose && echo "Detected --set-min-volume"
volume_set $VOLUME_MAX
;;
-m|--set-mute)
verbose && echo "Detected -m or --mute"
mute
;;
-u|--ummute)
verbose && echo "Detected -u or --unmute"
unmute
;;
-t|--toggle-mute)
verbose && echo "Detected -t or --toggle-mute"
toggle_mute
;;
-g|--get-volume)
verbose && echo "Detected -g or --get-volume"
get_volume
;;
-G|--get-mute)
verbose && echo "Detected -G or --get-mute"
get_mute
;;
"")
usage
;;
*)
echo "Unrecognized '$1' flag or argument"
echo "Try 'volumectl -h' for more information"
;;
esac
fi