Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various internal improvements #51

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 81 additions & 54 deletions common/pulseaudio-ctl.in
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
}
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -166,19 +166,51 @@ 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
done
}

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(){
notify-send \
--app-name=pulseaudio-ctl \
--expire-time=1000 \
--icon=multimedia-volume-control \
--hint=int:transient:1 \
"$@"
}

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
Expand All @@ -192,39 +224,40 @@ 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% ||
case "$PCV" in
0|1) pactl set-sink-volume "$SINK" -- +$PERC% ;;
2) pactl set-sink-volume "$SINK" +$PERC% ;;
esac
if [[ "$CURVOL" -ge $UPPER_THRESHOLD ]]; then
pactl set-sink-volume "$SINK" $UPPER_THRESHOLD%
else
pactl_volume "$SINK" "+${PERC}"
fi

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
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% ;;
esac
[[ "$CURVOL" -le 0 ]] && exit 0
pactl_volume "$SINK" "-${PERC}"
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
kde_osd "$CURVOL"
;;
M|m|[M,m]u|[M,m]ute)
# mutes/unmutes the volume entirely
Expand All @@ -235,13 +268,13 @@ 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
else
refreshcurvol
kde_osd $CURVOL
kde_osd "$CURVOL"
fi
fi
;;
Expand All @@ -254,58 +287,52 @@ 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
else
refreshsrcvol
kde_osd_mic $SRCVOL
kde_osd_mic "$SRCVOL"
fi
fi
;;
set)
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
[[ "$NEWVOL" -gt $UPPER_THRESHOLD ]] && exit 0
[[ "$NEWVOL" -le 0 ]] && exit 0
pactl_volume "$SINK" "$NEWVOL"
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
kde_osd "$CURVOL"
;;
atmost)
NEWVOL="${2%%%}"
[[ "$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
[[ "$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-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
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
[[ $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}"
Expand Down