2020-10-28 15:49:32 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2021-02-01 16:05:35 +01:00
|
|
|
## Default controller values
|
2020-10-28 15:49:32 +01:00
|
|
|
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"
|
2020-10-28 15:49:32 +01:00
|
|
|
TOGGLE_MUTE_FLAG="--toggle-mute"
|
2021-02-01 16:05:35 +01:00
|
|
|
GET_VOLUME_FLAG="--get-volume"
|
|
|
|
GET_MUTE_FLAG="--get-mute"
|
2020-10-28 15:49:32 +01:00
|
|
|
ADDITIONAL_FLAGS=("--allow-boost")
|
|
|
|
|
2021-02-01 16:05:35 +01:00
|
|
|
## 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 01:36:53 +01:00
|
|
|
|
2021-03-04 02:13:10 +01:00
|
|
|
CONFIG_PATH="$XDG_CONFIG_HOME/volumectl/config.ini"
|
2021-02-01 16:05:35 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2021-02-01 16:05:35 +01:00
|
|
|
|
|
|
|
## Args
|
2021-03-04 18:24:22 +01:00
|
|
|
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
|
|
|
|
|
2021-03-04 01:36:53 +01:00
|
|
|
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
|
|
|
}
|
2020-10-28 15:49:32 +01:00
|
|
|
|
|
|
|
volume_up_by () {
|
2021-02-01 16:05:35 +01:00
|
|
|
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-01 16:05:35 +01:00
|
|
|
|
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
|
2020-10-28 15:49:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
volume_down_by () {
|
2021-02-28 07:35:23 +01:00
|
|
|
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-01 16:05:35 +01:00
|
|
|
|
2021-02-28 09:08:40 +01:00
|
|
|
verbose && echo "Volume before command: $(get_volume)"
|
2021-03-04 01:36:53 +01:00
|
|
|
sh -c "$cmd"
|
2021-02-28 09:08:40 +01:00
|
|
|
verbose && echo "Volume after command: $(get_volume)"
|
|
|
|
notify
|
2020-10-28 15:49:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
volume_set () {
|
2021-02-28 07:35:23 +01:00
|
|
|
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-01 16:05:35 +01:00
|
|
|
|
2021-02-28 09:08:40 +01:00
|
|
|
verbose && echo "Volume before command: $(get_volume)"
|
2021-03-04 01:36:53 +01:00
|
|
|
sh -c "$cmd"
|
2021-02-28 09:08:40 +01:00
|
|
|
verbose && echo "Volume after command: $(get_volume)"
|
|
|
|
notify
|
2020-10-28 15:49:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
volume_big_up () {
|
2021-02-28 07:35:23 +01:00
|
|
|
local cmd="volume_up_by $BIG_DELTA"
|
2021-02-28 09:08:40 +01:00
|
|
|
verbose && echo "Increasing the volume alot, with \`$cmd\`"
|
2021-02-01 16:05:35 +01:00
|
|
|
$cmd
|
2020-10-28 15:49:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
volume_big_down () {
|
2021-02-28 07:35:23 +01:00
|
|
|
local cmd="volume_down_by $BIG_DELTA"
|
2021-02-28 09:08:40 +01:00
|
|
|
verbose && echo "Decreasing the volume alot, with \`$cmd\`"
|
2021-02-01 16:05:35 +01:00
|
|
|
$cmd
|
2020-10-28 15:49:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
volume_up () {
|
2021-02-28 07:35:23 +01:00
|
|
|
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
|
2020-10-28 15:49:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
volume_down () {
|
2021-02-28 07:35:23 +01:00
|
|
|
local cmd="volume_down_by $NORMAL_DELTA"
|
2021-02-28 09:08:40 +01:00
|
|
|
verbose && echo "Decreasing the volume, with \`$cmd\`"
|
2021-02-01 16:05:35 +01:00
|
|
|
$cmd
|
2020-10-28 15:49:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
toggle_mute() {
|
2021-02-28 07:35:23 +01:00
|
|
|
local cmd="$VOLUME_CONTROLLER $TOGGLE_MUTE_FLAG"
|
2021-02-28 09:08:40 +01:00
|
|
|
verbose && echo "Toggling the mute, with \`$cmd\`"
|
2021-02-01 16:05:35 +01:00
|
|
|
|
2021-02-28 09:08:40 +01:00
|
|
|
verbose && echo "Mute state before command: $(get_mute)"
|
2021-03-04 01:36:53 +01:00
|
|
|
sh -c "$cmd"
|
2021-02-28 09:08:40 +01:00
|
|
|
verbose && echo "Mute state after command: $(get_mute)"
|
|
|
|
notify
|
2021-02-01 16:05:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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-01 16:05:35 +01:00
|
|
|
|
2021-02-28 09:08:40 +01:00
|
|
|
verbose && echo "Mute state before command: $(get_mute)"
|
2021-03-04 01:36:53 +01:00
|
|
|
sh -c "$cmd"
|
2021-02-28 09:08:40 +01:00
|
|
|
verbose && echo "Mute state after command: $(get_mute)"
|
|
|
|
notify
|
2020-10-28 15:49:32 +01:00
|
|
|
}
|
|
|
|
|
2021-02-01 16:05:35 +01:00
|
|
|
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-01 16:05:35 +01:00
|
|
|
|
2021-02-28 09:08:40 +01:00
|
|
|
verbose && echo "Mute state before command: $(get_mute)"
|
2021-03-04 01:36:53 +01:00
|
|
|
sh -c "$cmd"
|
2021-02-28 09:08:40 +01:00
|
|
|
verbose && echo "Mute state after command: $(get_mute)"
|
|
|
|
notify
|
2021-02-01 16:05:35 +01:00
|
|
|
}
|
2020-10-28 16:05:06 +01:00
|
|
|
|
2021-02-01 16:05:35 +01:00
|
|
|
get_volume () {
|
2021-03-04 01:36:53 +01:00
|
|
|
sh -c "$VOLUME_CONTROLLER ${ADDITIONAL_FLAGS[@]} $GET_VOLUME_FLAG"
|
2020-10-28 16:05:06 +01:00
|
|
|
}
|
|
|
|
|
2021-02-01 16:05:35 +01:00
|
|
|
get_mute () {
|
2021-03-04 01:36:53 +01:00
|
|
|
sh -c "$VOLUME_CONTROLLER ${ADDITIONAL_FLAGS[@]} $GET_MUTE_FLAG"
|
2020-10-28 15:49:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2020-10-28 15:49:32 +01:00
|
|
|
|
2021-02-01 16:05:35 +01:00
|
|
|
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
|
2021-02-01 16:05:35 +01:00
|
|
|
-m, --mute mute
|
|
|
|
-u, --unmute unmute
|
|
|
|
-t, --toggle toggle mute
|
|
|
|
-g, --get-volume get volume
|
|
|
|
-G, --get-mute get mute
|
|
|
|
|
|
|
|
Optional arguments:
|
2021-03-04 18:24:22 +01:00
|
|
|
--notification-timeout arg forces the notification timeout, in milliseconds (overrides the configuation setting,
|
2021-03-04 01:36:53 +01:00
|
|
|
the default value is $NOTIFICATION_TIMEOUT)
|
2020-10-28 15:49:32 +01:00
|
|
|
-n, --notification forces the notification to appear (overrides the configuration setting)
|
|
|
|
-N, --no-notification forces the notification NOT to appear (overrides the configuration setting)
|
2021-02-01 16:05:35 +01:00
|
|
|
-v, --verbose print each step the script passes through
|
2021-03-04 01:36:53 +01:00
|
|
|
-h, -?, --help show this message
|
2020-10-28 15:49:32 +01:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then #if '__name__' == '__main__'
|
|
|
|
# check if the notification is being forced or not
|
2021-02-01 16:05:35 +01:00
|
|
|
|
|
|
|
while [[ " ${OPTIONAL_FLAGS[@]} " =~ " $1 " ]]; do
|
2021-02-28 09:08:40 +01:00
|
|
|
verbose && echo "Checking for optional arguments"
|
2021-02-01 16:05:35 +01:00
|
|
|
|
|
|
|
case $1 in
|
|
|
|
-v|--verbose)
|
|
|
|
VERBOSE=1
|
2021-03-03 20:34:07 +01:00
|
|
|
verbose && echo "Setting \$VERBOSE to true (\$VERBOSE=$VERBOSE)"
|
2021-02-01 16:05:35 +01:00
|
|
|
shift
|
|
|
|
;;
|
2021-03-04 18:24:22 +01:00
|
|
|
--notification-timeout)
|
2021-03-04 01:36:53 +01:00
|
|
|
NOTIFICATION_TIMEOUT="$2";
|
|
|
|
verbose && echo "Setting \$NOTIFICATION_TIMEOUT to $2 (\$NOTIFICATION_TIMEOUT=$NOTIFICATION_TIMEOUT)"
|
|
|
|
shift 2
|
|
|
|
;;
|
2021-02-01 16:05:35 +01:00
|
|
|
-c|--config)
|
|
|
|
CONFIG_PATH="$2";
|
2021-03-03 20:34:07 +01:00
|
|
|
verbose && echo "Setting \$CONFIG_PATH to $2 (\$CONFIG_PATH=$CONFIG_PATH)"
|
2021-02-01 16:05:35 +01:00
|
|
|
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)"
|
2021-02-01 16:05:35 +01:00
|
|
|
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)"
|
2021-02-01 16:05:35 +01:00
|
|
|
shift
|
|
|
|
;;
|
2021-03-04 01:36:53 +01:00
|
|
|
-h|-\?|--help)
|
|
|
|
verbose && echo "Detected -h, -? or --help"
|
|
|
|
usage
|
|
|
|
shift
|
|
|
|
;;
|
2021-02-01 16:05:35 +01:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2021-03-03 20:34:07 +01:00
|
|
|
verbose && echo "Looking at the argument '$@'"
|
2020-10-28 15:49:32 +01:00
|
|
|
case $1 in
|
|
|
|
-i|--increase)
|
2021-02-28 09:08:40 +01:00
|
|
|
verbose && echo "Detected -i or --increase, with argument '$2'"
|
2020-10-28 15:49:32 +01:00
|
|
|
[[ -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"
|
2020-10-28 15:49:32 +01:00
|
|
|
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
|
2020-10-28 15:49:32 +01:00
|
|
|
;;
|
|
|
|
-D|--big-decrease)
|
2021-02-28 09:08:40 +01:00
|
|
|
verbose && echo "Detected -D or --big-decrease"
|
2020-10-28 15:49:32 +01:00
|
|
|
volume_big_down
|
|
|
|
;;
|
|
|
|
-s|--set-volume)
|
2021-02-28 09:08:40 +01:00
|
|
|
verbose && echo "Detected -s or --set-volume, with argument '$2'"
|
2021-02-01 16:05:35 +01:00
|
|
|
[[ -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
|
|
|
|
;;
|
2020-10-28 15:49:32 +01:00
|
|
|
-m|--set-mute)
|
2021-02-28 09:08:40 +01:00
|
|
|
verbose && echo "Detected -m or --mute"
|
2021-02-01 16:05:35 +01:00
|
|
|
mute
|
|
|
|
;;
|
|
|
|
-u|--ummute)
|
2021-02-28 09:08:40 +01:00
|
|
|
verbose && echo "Detected -u or --unmute"
|
2021-02-01 16:05:35 +01:00
|
|
|
unmute
|
2020-10-28 15:49:32 +01:00
|
|
|
;;
|
|
|
|
-t|--toggle-mute)
|
2021-02-28 09:08:40 +01:00
|
|
|
verbose && echo "Detected -t or --toggle-mute"
|
2020-10-28 15:49:32 +01:00
|
|
|
toggle_mute
|
|
|
|
;;
|
2021-02-01 16:05:35 +01:00
|
|
|
-g|--get-volume)
|
2021-02-28 09:08:40 +01:00
|
|
|
verbose && echo "Detected -g or --get-volume"
|
2021-02-01 16:05:35 +01:00
|
|
|
get_volume
|
|
|
|
;;
|
|
|
|
-G|--get-mute)
|
2021-02-28 09:08:40 +01:00
|
|
|
verbose && echo "Detected -G or --get-mute"
|
2021-02-01 16:05:35 +01:00
|
|
|
get_mute
|
|
|
|
;;
|
2021-03-04 01:36:53 +01:00
|
|
|
"")
|
2020-10-28 15:49:32 +01:00
|
|
|
usage
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Unrecognized '$1' flag or argument"
|
|
|
|
echo "Try 'volumectl -h' for more information"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
2021-02-28 07:35:23 +01:00
|
|
|
|