From 8656a9eb812ea2f30dee8d3f607bd41b8d6c9f33 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Mon, 13 Nov 2017 14:52:13 +0100 Subject: [PATCH 1/5] Minify notify-send invocations notify-send allows to set a parameter multiple times, but take the last one precedence. Therefore we can dedup most of the stuff by setting the default values (and let it override by special call). --- common/pulseaudio-ctl.in | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/common/pulseaudio-ctl.in b/common/pulseaudio-ctl.in index 487d357..3f0fc27 100644 --- a/common/pulseaudio-ctl.in +++ b/common/pulseaudio-ctl.in @@ -109,10 +109,10 @@ checkconfig() { case "${BARCHART,,}" in y|yes|true|t|on|1|enabled|enable|use) - USEB=1 + USEB="" ;; *) - USEB=0 + unset USEB ;; esac @@ -181,6 +181,15 @@ kde_osd_mic() { dbus-send --session --dest="org.freedesktop.Notifications" --type="method_call" "/org/kde/osdService" "org.kde.osdService.mediaPlayerVolumeChanged" "int32:$1" "string:" "string:audio-input-microphone" } +notify(){ + notify-send \ + --app-name=pulseaudio-ctl \ + --expire-time=1000 \ + --icon=multimedia-volume-control \ + --hint=int:transient:1 \ + "$@" +} + checkconfig setup # checks if exist a valid $2 @@ -201,9 +210,11 @@ case "$1" in refreshcurvol refreshbarvolperc if [[ $USEN -eq 1 ]]; then - [[ $USEB -eq 1 ]] && - notify-send -a pulseaudio-ctl -t 1000 -i multimedia-volume-control --hint=int:transient:1 --hint=int:value:$UPPER_THRESHOLD_AWARE_VOL --hint=string:synchronous:volume "Volume up $PERC %" "" || - notify-send -a pulseaudio-ctl -t 1000 --hint=int:transient:1 "Volume up $PERC%" "Current is $CURVOL %" --icon=multimedia-volume-control + notify \ + "Volume up $PERC %" \ + ${USEB-"Current is $CURVOL %"} \ + ${USEB+"--hint=int:value:$UPPER_THRESHOLD_AWARE_VOL"} \ + ${USEB+"--hint=string:synchronous:volume"} fi [[ $USEK -eq 1 ]] && kde_osd $CURVOL @@ -219,9 +230,11 @@ case "$1" in refreshcurvol refreshbarvolperc if [[ $USEN -eq 1 ]]; then - [[ $USEB -eq 1 ]] && - notify-send -a pulseaudio-ctl -t 1000 -i multimedia-volume-control --hint=int:transient:1 --hint=int:value:$UPPER_THRESHOLD_AWARE_VOL --hint=string:synchronous:volume "Volume down $PERC %" "" || - notify-send -a pulseaudio-ctl -t 1000 --hint=int:transient:1 "Volume down $PERC%" "Current is $CURVOL %" --icon=multimedia-volume-control + notify \ + "Volume down $PERC %" \ + ${USEB-"Current is $CURVOL %"} \ + ${USEB+"--hint=int:value:$UPPER_THRESHOLD_AWARE_VOL"} \ + ${USEB+"--hint=string:synchronous:volume"} fi [[ $USEK -eq 1 ]] && kde_osd $CURVOL @@ -235,7 +248,7 @@ case "$1" in pactl set-sink-mute "$SINK" "$NEW_MUTE" MUTED=$(pacmd list-sinks|grep -A 15 '* index'|awk '/muted:/{ print $2 }') [[ $USEN -eq 1 ]] && - notify-send -a pulseaudio-ctl -t 1000 --hint=int:transient:1 "Mute toggle" "Muted: $MUTED" --icon=audio-volume-muted + notify --icon=audio-volume-muted "Mute toggle" "Muted: $MUTED" if [[ $USEK -eq 1 ]]; then if [[ $MUTED == "yes" ]]; then kde_osd 0 @@ -254,7 +267,7 @@ case "$1" in pactl set-source-mute "$SOURCE" "$NEW_MUTE" SOURCE_MUTED=$(pacmd list-sources|grep -A 15 '* index'|awk '/muted:/{ print $2 }') [[ $USEN -eq 1 ]] && - notify-send -a pulseaudio-ctl -t 1000 --hint=int:transient:1 "Mute toggle" "Muted: $SOURCE_MUTED" --icon=audio-volume-muted + notify --icon=audio-volume-muted "Mute toggle" "Muted: $SOURCE_MUTED" if [[ $USEK -eq 1 ]]; then if [[ $SOURCE_MUTED == "yes" ]]; then kde_osd_mic 0 @@ -274,7 +287,7 @@ case "$1" in esac refreshcurvol [[ $USEN -eq 1 ]] && - notify-send -a pulseaudio-ctl -t 1000 --hint=int:transient:1 "Volume set" "Level: $CURVOL" --icon=multimedia-volume-control + notify "Volume set" "Level: $CURVOL" [[ $USEK -eq 1 ]] && kde_osd $CURVOL ;; @@ -289,7 +302,7 @@ case "$1" in esac refreshcurvol [[ $USEN -eq 1 ]] && - notify-send -a pulseaudio-ctl -t 1000 --hint=int:transient:1 "Atmost set" "Level: $CURVOL" --icon=multimedia-volume-control + notify "Atmost set" "Level: $CURVOL" [[ $USEK -eq 1 ]] && kde_osd $CURVOL ;; @@ -305,7 +318,7 @@ case "$1" in *) # send to notify-send if enabled [[ $USEN -eq 1 ]] && - notify-send -a pulseaudio-ctl -t 8000 --hint=int:transient:1 "Pulseaudio Settings" "Volume level : $CURVOL %\nIs sink muted : $MUTED\nIs source muted : $SOURCE_MUTED\nDetected sink : $SINK\nDetected source : $SOURCE\nPulse version : $PAVERSION" --icon=multimedia-volume-control + notify -t 8000 "Pulseaudio Settings" "Volume level : $CURVOL %\nIs sink muted : $MUTED\nIs source muted : $SOURCE_MUTED\nDetected sink : $SINK\nDetected source : $SOURCE\nPulse version : $PAVERSION" # add pretty colors for mute status for CLI only [[ "$MUTED" = "yes" ]] && MUTED="${NRM}${RED}$MUTED${NRM}" || MUTED="${NRM}${GRN}$MUTED${NRM}" From 9d0ca384588bc7e1eed1d923633bc7f6c9c3d12e Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Mon, 13 Nov 2017 15:24:31 +0100 Subject: [PATCH 2/5] Arrange kde notification calls vertically --- common/pulseaudio-ctl.in | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/common/pulseaudio-ctl.in b/common/pulseaudio-ctl.in index 3f0fc27..72a66c5 100644 --- a/common/pulseaudio-ctl.in +++ b/common/pulseaudio-ctl.in @@ -174,11 +174,25 @@ setup() { } kde_osd() { - dbus-send --session --dest="org.freedesktop.Notifications" --type="method_call" "/org/kde/osdService" "org.kde.osdService.volumeChanged" "int32:$1" + dbus-send \ + --session \ + --dest="org.freedesktop.Notifications" \ + --type="method_call" \ + "/org/kde/osdService" \ + "org.kde.osdService.volumeChanged" \ + "int32:$1" } kde_osd_mic() { - dbus-send --session --dest="org.freedesktop.Notifications" --type="method_call" "/org/kde/osdService" "org.kde.osdService.mediaPlayerVolumeChanged" "int32:$1" "string:" "string:audio-input-microphone" + dbus-send \ + --session \ + --dest="org.freedesktop.Notifications" \ + --type="method_call" \ + "/org/kde/osdService" \ + "org.kde.osdService.mediaPlayerVolumeChanged" \ + "int32:$1" \ + "string:" \ + "string:audio-input-microphone" } notify(){ From cd7c92a1435ced72b9c6b32e756e9d0508bb5dde Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Mon, 13 Nov 2017 15:41:24 +0100 Subject: [PATCH 3/5] Quote all variable parameters For rationale see the shellcheck wiki: https://github.com/koalaman/shellcheck/wiki/SC2086 --- common/pulseaudio-ctl.in | 44 ++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/common/pulseaudio-ctl.in b/common/pulseaudio-ctl.in index 72a66c5..f9bc937 100644 --- a/common/pulseaudio-ctl.in +++ b/common/pulseaudio-ctl.in @@ -53,7 +53,7 @@ fi # stop if the above failed to set a value for PCV if [[ -z $PCV ]]; then - echo -e ${BLD}"Cannot determine which version of pactl is installed. ${RED}Aborting."${NRM} + echo -e "${BLD}Cannot determine which version of pactl is installed. ${RED}Aborting.${NRM}" exit 1 fi @@ -67,17 +67,17 @@ is_integer() { makeconfig() { if [[ ! -f "$SKEL" ]]; then - echo -e "${RED}$SKEL is missing. Reinstall this package to continue."${NRM} + echo -e "${RED}$SKEL is missing. Reinstall this package to continue.${NRM}" exit 1 fi if [[ ! -f "$CONFIG" ]]; then - echo -e ${BLD}'------------------------------------------------------------'${NRM} - echo -e ${BLD}' No config file found so creating a fresh one in:'${NRM} - echo -e ${BLD}${BLU}" $CONFIG"${NRM} + echo -e "${BLD}------------------------------------------------------------${NRM}" + echo -e "${BLD} No config file found so creating a fresh one in:${NRM}" + echo -e "${BLD}${BLU} $CONFIG${NRM}" echo - echo -e ${BLD}" Edit this file if desired."${NRM} - echo -e ${BLD}'------------------------------------------------------------'${NRM} + echo -e "${BLD} Edit this file if desired.${NRM}" + echo -e "${BLD}------------------------------------------------------------${NRM}" install -Dm644 "$SKEL" "$CONFIG" fi } @@ -166,7 +166,7 @@ setup() { VARS_TO_CHECK=([SINK]="default sink" [SOURCE]="default source" [CURVOL]="current volume") for v in "${!VARS_TO_CHECK[@]}"; do if [[ -n "${!v}" ]]; then - [[ $(is_integer "${!v}") == '1' ]] || echo -e "${RED}Cannot determine ${VARS_TO_CHECK[$v]}."${NRM} + [[ $(is_integer "${!v}") == '1' ]] || echo -e "${RED}Cannot determine ${VARS_TO_CHECK[$v]}.${NRM}" else return 0 fi @@ -231,15 +231,15 @@ case "$1" in ${USEB+"--hint=string:synchronous:volume"} fi [[ $USEK -eq 1 ]] && - kde_osd $CURVOL + kde_osd "$CURVOL" ;; D|d|[D,d]own|[D,d]o) # lowers volume by $PERC % [[ "$PERC" -gt "$CURVOL" ]] && PERC="$CURVOL" [[ "$CURVOL" -le 0 ]] && exit 0 || case "$PCV" in - 0|1) pactl set-sink-volume "$SINK" -- -$PERC% ;; - 2) pactl set-sink-volume "$SINK" -$PERC% ;; + 0|1) pactl set-sink-volume "$SINK" -- "-$PERC%" ;; + 2) pactl set-sink-volume "$SINK" "-$PERC%" ;; esac refreshcurvol refreshbarvolperc @@ -251,7 +251,7 @@ case "$1" in ${USEB+"--hint=string:synchronous:volume"} fi [[ $USEK -eq 1 ]] && - kde_osd $CURVOL + kde_osd "$CURVOL" ;; M|m|[M,m]u|[M,m]ute) # mutes/unmutes the volume entirely @@ -268,7 +268,7 @@ case "$1" in kde_osd 0 else refreshcurvol - kde_osd $CURVOL + kde_osd "$CURVOL" fi fi ;; @@ -287,7 +287,7 @@ case "$1" in kde_osd_mic 0 else refreshsrcvol - kde_osd_mic $SRCVOL + kde_osd_mic "$SRCVOL" fi fi ;; @@ -296,14 +296,14 @@ case "$1" in [[ "$NEWVOL" -gt $UPPER_THRESHOLD ]] && exit 0 || [[ "$NEWVOL" -le 0 ]] && exit 0 || case "$PCV" in - 0|1) pactl set-sink-volume "$SINK" -- $NEWVOL% ;; - 2) pactl set-sink-volume "$SINK" $NEWVOL% ;; + 0|1) pactl set-sink-volume "$SINK" -- "$NEWVOL%" ;; + 2) pactl set-sink-volume "$SINK" "$NEWVOL%" ;; esac refreshcurvol [[ $USEN -eq 1 ]] && notify "Volume set" "Level: $CURVOL" [[ $USEK -eq 1 ]] && - kde_osd $CURVOL + kde_osd "$CURVOL" ;; atmost) NEWVOL="${2%%%}" @@ -311,23 +311,23 @@ case "$1" in [[ "$NEWVOL" -ge $UPPER_THRESHOLD ]] && exit 0 || [[ "$NEWVOL" -le 0 ]] && exit 0 || case "$PCV" in - 0|1) pactl set-sink-volume "$SINK" -- $NEWVOL% ;; - 2) pactl set-sink-volume "$SINK" $NEWVOL% ;; + 0|1) pactl set-sink-volume "$SINK" -- "$NEWVOL%" ;; + 2) pactl set-sink-volume "$SINK" "$NEWVOL%" ;; esac refreshcurvol [[ $USEN -eq 1 ]] && notify "Atmost set" "Level: $CURVOL" [[ $USEK -eq 1 ]] && - kde_osd $CURVOL + kde_osd "$CURVOL" ;; C|c|[C,c]urrent) # useful only for scripting - echo $CURVOL% + echo "$CURVOL%" ;; [F,f]s|[F,f]ull-[S,s]tatus) # useful for scripting. # returns current volume and sink and source mute state - echo $CURVOL $MUTED $SOURCE_MUTED + echo "$CURVOL" "$MUTED" "$SOURCE_MUTED" ;; *) # send to notify-send if enabled From 700dc823be81d97c89cf055cc2c4f05dbb8d9009 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Mon, 13 Nov 2017 16:00:27 +0100 Subject: [PATCH 4/5] Replace similar set volume calls with single function --- common/pulseaudio-ctl.in | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/common/pulseaudio-ctl.in b/common/pulseaudio-ctl.in index f9bc937..7526a21 100644 --- a/common/pulseaudio-ctl.in +++ b/common/pulseaudio-ctl.in @@ -204,6 +204,15 @@ notify(){ "$@" } +pactl_volume() { + local sink=${1?No sink given} vol=${2?No volume given} + + case "$PCV" in + 0|1) pactl set-sink-volume "$sink" -- "$vol%" ;; + 2) pactl set-sink-volume "$sink" "$vol%" ;; + esac +} + checkconfig setup # checks if exist a valid $2 @@ -216,10 +225,7 @@ case "$1" in # in cases where external apps have pushed it above [[ "$(( $PERC + $CURVOL ))" -gt "$UPPER_THRESHOLD" ]] && PERC="$(( $UPPER_THRESHOLD - $CURVOL ))" [[ "$CURVOL" -ge $UPPER_THRESHOLD ]] && pactl set-sink-volume "$SINK" $UPPER_THRESHOLD% || - case "$PCV" in - 0|1) pactl set-sink-volume "$SINK" -- +$PERC% ;; - 2) pactl set-sink-volume "$SINK" +$PERC% ;; - esac + pactl_volume "$SINK" "+${PERC}" refreshcurvol refreshbarvolperc @@ -237,10 +243,7 @@ case "$1" in # lowers volume by $PERC % [[ "$PERC" -gt "$CURVOL" ]] && PERC="$CURVOL" [[ "$CURVOL" -le 0 ]] && exit 0 || - case "$PCV" in - 0|1) pactl set-sink-volume "$SINK" -- "-$PERC%" ;; - 2) pactl set-sink-volume "$SINK" "-$PERC%" ;; - esac + pactl_volume "$SINK" "-${PERC}" refreshcurvol refreshbarvolperc if [[ $USEN -eq 1 ]]; then @@ -295,10 +298,7 @@ case "$1" in NEWVOL="${2%%%}" [[ "$NEWVOL" -gt $UPPER_THRESHOLD ]] && exit 0 || [[ "$NEWVOL" -le 0 ]] && exit 0 || - case "$PCV" in - 0|1) pactl set-sink-volume "$SINK" -- "$NEWVOL%" ;; - 2) pactl set-sink-volume "$SINK" "$NEWVOL%" ;; - esac + pactl_volume "$SINK" "$NEWVOL" refreshcurvol [[ $USEN -eq 1 ]] && notify "Volume set" "Level: $CURVOL" @@ -310,10 +310,7 @@ case "$1" in [[ "$CURVOL" -le "$NEWVOL" ]] && exit 0 || [[ "$NEWVOL" -ge $UPPER_THRESHOLD ]] && exit 0 || [[ "$NEWVOL" -le 0 ]] && exit 0 || - case "$PCV" in - 0|1) pactl set-sink-volume "$SINK" -- "$NEWVOL%" ;; - 2) pactl set-sink-volume "$SINK" "$NEWVOL%" ;; - esac + pactl_volume "$SINK" "$NEWVOL" refreshcurvol [[ $USEN -eq 1 ]] && notify "Atmost set" "Level: $CURVOL" From 7cc9a036773a02a6d43eb4b39eeb6ab6225dc854 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Mon, 13 Nov 2017 16:08:00 +0100 Subject: [PATCH 5/5] Improve if/else constructs written with && || See shellcheck's wiki: https://github.com/koalaman/shellcheck/wiki/SC2015 --- common/pulseaudio-ctl.in | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/common/pulseaudio-ctl.in b/common/pulseaudio-ctl.in index 7526a21..0f9d691 100644 --- a/common/pulseaudio-ctl.in +++ b/common/pulseaudio-ctl.in @@ -224,8 +224,11 @@ case "$1" in # raise volume by $PERC % or set it to the upper threshold # in cases where external apps have pushed it above [[ "$(( $PERC + $CURVOL ))" -gt "$UPPER_THRESHOLD" ]] && PERC="$(( $UPPER_THRESHOLD - $CURVOL ))" - [[ "$CURVOL" -ge $UPPER_THRESHOLD ]] && pactl set-sink-volume "$SINK" $UPPER_THRESHOLD% || + if [[ "$CURVOL" -ge $UPPER_THRESHOLD ]]; then + pactl set-sink-volume "$SINK" $UPPER_THRESHOLD% + else pactl_volume "$SINK" "+${PERC}" + fi refreshcurvol refreshbarvolperc @@ -242,8 +245,8 @@ case "$1" in D|d|[D,d]own|[D,d]o) # lowers volume by $PERC % [[ "$PERC" -gt "$CURVOL" ]] && PERC="$CURVOL" - [[ "$CURVOL" -le 0 ]] && exit 0 || - pactl_volume "$SINK" "-${PERC}" + [[ "$CURVOL" -le 0 ]] && exit 0 + pactl_volume "$SINK" "-${PERC}" refreshcurvol refreshbarvolperc if [[ $USEN -eq 1 ]]; then @@ -296,9 +299,9 @@ case "$1" in ;; set) NEWVOL="${2%%%}" - [[ "$NEWVOL" -gt $UPPER_THRESHOLD ]] && exit 0 || - [[ "$NEWVOL" -le 0 ]] && exit 0 || - pactl_volume "$SINK" "$NEWVOL" + [[ "$NEWVOL" -gt $UPPER_THRESHOLD ]] && exit 0 + [[ "$NEWVOL" -le 0 ]] && exit 0 + pactl_volume "$SINK" "$NEWVOL" refreshcurvol [[ $USEN -eq 1 ]] && notify "Volume set" "Level: $CURVOL" @@ -307,10 +310,10 @@ case "$1" in ;; atmost) NEWVOL="${2%%%}" - [[ "$CURVOL" -le "$NEWVOL" ]] && exit 0 || - [[ "$NEWVOL" -ge $UPPER_THRESHOLD ]] && exit 0 || - [[ "$NEWVOL" -le 0 ]] && exit 0 || - pactl_volume "$SINK" "$NEWVOL" + [[ "$CURVOL" -le "$NEWVOL" ]] && exit 0 + [[ "$NEWVOL" -ge $UPPER_THRESHOLD ]] && exit 0 + [[ "$NEWVOL" -le 0 ]] && exit 0 + pactl_volume "$SINK" "$NEWVOL" refreshcurvol [[ $USEN -eq 1 ]] && notify "Atmost set" "Level: $CURVOL"