volumectl/volumectl

279 lines
8.8 KiB
Plaintext
Raw Normal View History

#!/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"
2020-10-28 16:05:06 +01:00
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
2021-03-03 20:34:07 +01:00
NOTIFICATION_ENABLED=1
2021-03-04 02:03:32 +01:00
NOTIFICATION_TIMEOUT=3000
2021-03-04 02:13:10 +01:00
CONFIG_PATH="$XDG_CONFIG_HOME/volumectl/config.ini"
VERBOSE=0
NORMAL_DELTA=5
2021-02-28 09:08:40 +01:00
BIG_DELTA=10
2021-03-03 20:34:07 +01:00
VOLUME_MAX=100
VOLUME_MIN=5
## Args
OPTIONAL_FLAGS=("--notification-timeout" "-v" "--verbose" "-c" "--config" "-n" "--notifcation" "-N" "--no-notification" "-h" "--help")
2021-02-28 09:08:40 +01:00
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%)\""
2021-03-03 20:34:07 +01:00
[[ $NOTIFICATION_ENABLED -eq 1 ]] && sh -c "$cmd"
verbose && ( [[ $NOTIFICATION_ENABLED -eq 1 ]] && echo "Notified with \`$cmd\`" || echo "Didn't notify" )
2021-02-28 09:08:40 +01:00
}
volume_up_by () {
local cmd="$VOLUME_CONTROLLER ${ADDITIONAL_FLAGS[@]} $INCREASE_FLAG $1"
2021-02-28 09:08:40 +01:00
verbose && echo "Increasing volume by $1, with \`$cmd\`"
2021-02-28 09:08:40 +01:00
verbose && echo "Volume before command: $(get_volume)"
2021-03-04 02:03:32 +01:00
sh -c "$cmd"
2021-02-28 09:08:40 +01:00
verbose && echo "Volume after command: $(get_volume)"
notify
}
volume_down_by () {
local cmd="$VOLUME_CONTROLLER ${ADDITIONAL_FLAGS[@]} $DECREASE_FLAG $1"
2021-02-28 09:08:40 +01:00
verbose && echo "Decreasing volume by $1, with \`$cmd\`"
2021-02-28 09:08:40 +01:00
verbose && echo "Volume before command: $(get_volume)"
sh -c "$cmd"
2021-02-28 09:08:40 +01:00
verbose && echo "Volume after command: $(get_volume)"
notify
}
volume_set () {
local cmd="$VOLUME_CONTROLLER ${ADDITIONAL_FLAGS[@]} $SET_VOLUME_FLAG $1"
2021-02-28 09:08:40 +01:00
verbose && echo "Setting the volume to $1, with \`$cmd\`"
2021-02-28 09:08:40 +01:00
verbose && echo "Volume before command: $(get_volume)"
sh -c "$cmd"
2021-02-28 09:08:40 +01:00
verbose && echo "Volume after command: $(get_volume)"
notify
}
volume_big_up () {
local cmd="volume_up_by $BIG_DELTA"
2021-02-28 09:08:40 +01:00
verbose && echo "Increasing the volume alot, with \`$cmd\`"
$cmd
}
volume_big_down () {
local cmd="volume_down_by $BIG_DELTA"
2021-02-28 09:08:40 +01:00
verbose && echo "Decreasing the volume alot, with \`$cmd\`"
$cmd
}
volume_up () {
local cmd="volume_up_by $NORMAL_DELTA"
2021-02-28 09:08:40 +01:00
verbose && echo "Increasing the volume, with \`$cmd\`"
2021-03-04 02:03:32 +01:00
$cmd
}
volume_down () {
local cmd="volume_down_by $NORMAL_DELTA"
2021-02-28 09:08:40 +01:00
verbose && echo "Decreasing the volume, with \`$cmd\`"
$cmd
}
toggle_mute() {
local cmd="$VOLUME_CONTROLLER $TOGGLE_MUTE_FLAG"
2021-02-28 09:08:40 +01:00
verbose && echo "Toggling the mute, with \`$cmd\`"
2021-02-28 09:08:40 +01:00
verbose && echo "Mute state before command: $(get_mute)"
sh -c "$cmd"
2021-02-28 09:08:40 +01:00
verbose && echo "Mute state after command: $(get_mute)"
notify
}
mute () {
2021-02-28 09:08:40 +01:00
local cmd="$VOLUME_CONTROLLER $SET_MUTE_FLAG"
verbose && echo "Setting the mute on, with \`$cmd\`"
2021-02-28 09:08:40 +01:00
verbose && echo "Mute state before command: $(get_mute)"
sh -c "$cmd"
2021-02-28 09:08:40 +01:00
verbose && echo "Mute state after command: $(get_mute)"
notify
}
unmute () {
2021-02-28 09:08:40 +01:00
local cmd="$VOLUME_CONTROLLER $SET_UNMUTE_FLAG"
verbose && echo "Setting the mute off, with \`$cmd\`"
2021-02-28 09:08:40 +01:00
verbose && echo "Mute state before command: $(get_mute)"
sh -c "$cmd"
2021-02-28 09:08:40 +01:00
verbose && echo "Mute state after command: $(get_mute)"
notify
}
2020-10-28 16:05:06 +01:00
get_volume () {
sh -c "$VOLUME_CONTROLLER ${ADDITIONAL_FLAGS[@]} $GET_VOLUME_FLAG"
2020-10-28 16:05:06 +01:00
}
get_mute () {
sh -c "$VOLUME_CONTROLLER ${ADDITIONAL_FLAGS[@]} $GET_MUTE_FLAG"
}
usage() {
cat << EOF
2021-03-03 20:34:07 +01:00
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
2021-03-04 01:37:17 +01:00
--set-min-volume set volume to minimum
2021-03-03 20:34:07 +01:00
--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
2021-02-28 09:08:40 +01:00
verbose && echo "Checking for optional arguments"
case $1 in
-v|--verbose)
VERBOSE=1
2021-03-03 20:34:07 +01:00
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";
2021-03-03 20:34:07 +01:00
verbose && echo "Setting \$CONFIG_PATH to $2 (\$CONFIG_PATH=$CONFIG_PATH)"
shift 2
;;
-n|--notification)
2021-03-03 20:34:07 +01:00
NOTIFICATION_ENABLED=1
verbose && echo "Setting \$NOTIFICATION_ENABLED to true (\$NOTIFICATION_ENABLED=$NOTIFICATION_ENABLED)"
shift
;;
-N|--no-notification)
2021-03-03 20:34:07 +01:00
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
2021-03-03 20:34:07 +01:00
verbose && echo "Looking at the argument '$@'"
case $1 in
-i|--increase)
2021-02-28 09:08:40 +01:00
verbose && echo "Detected -i or --increase, with argument '$2'"
[[ -z "$2" ]] && volume_up || volume_up_by $2
;;
-I|--big-increase)
2021-02-28 09:08:40 +01:00
verbose && echo "Detected -I or --big-increase"
volume_big_up
;;
-d|--decrease)
2021-02-28 09:08:40 +01:00
verbose && echo "Detected -d or --decrease, with argument '$2'"
[[ -z "$2" ]] && volume_down || volume_down_by $2
;;
-D|--big-decrease)
2021-02-28 09:08:40 +01:00
verbose && echo "Detected -D or --big-decrease"
volume_big_down
;;
-s|--set-volume)
2021-02-28 09:08:40 +01:00
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
2020-10-28 16:05:06 +01:00
;;
2021-03-03 20:34:07 +01:00
--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)
2021-02-28 09:08:40 +01:00
verbose && echo "Detected -m or --mute"
mute
;;
-u|--ummute)
2021-02-28 09:08:40 +01:00
verbose && echo "Detected -u or --unmute"
unmute
;;
-t|--toggle-mute)
2021-02-28 09:08:40 +01:00
verbose && echo "Detected -t or --toggle-mute"
toggle_mute
;;
-g|--get-volume)
2021-02-28 09:08:40 +01:00
verbose && echo "Detected -g or --get-volume"
get_volume
;;
-G|--get-mute)
2021-02-28 09:08:40 +01:00
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