1
0

make work better

This commit is contained in:
dtb 2022-06-26 16:34:09 -04:00
parent 59936a7c21
commit 19282627fc

View File

@ -14,13 +14,16 @@ argv0="$0"
# `volume s` must output the current volume in integer form in whatever units
# are being used by `volume a` on the given system.
! command -v stris >/dev/null 2>/dev/null \
&& printf "%b: Missing stris.\n" "$argv0" \
&& exit 1 \
|| true
VOLUME_TMP_FILE="$HOME/.volume-previous"
system_determine(){
if [ "$(uname)" = NetBSD ]; then
printf "NetBSD\n"
elif command -v pactl >/dev/null 2>&1; then
printf "PulseAudio\n"
fi
}
unknown_system(){
printf "%s: Unknown system.\n" "$argv0" 1>&2
exit 1
@ -34,6 +37,7 @@ Functions:
'+' - shortcut to \"relative \$2\"
'-' - shortcut to \"relative -\$2\"
absolute - change sound output to absolute unit
debug - show debugging information
help,'' - print this help output
mute - toggle mute/unmute
relative - change sound output relative to current status
@ -56,29 +60,27 @@ case "$argv1" in
#
# SYSTEM DEPENDENT
#
(a*)
[ -n "$2" ] && [ -z "$3" ] && stris int "$2" || usage
if [ "$(uname)" = "NetBSD" ]; then
a*) [ -n "$2" ] || usage
case "$(system_determine)" in
NetBSD)
# NetBSD 9.2 STABLE 2021-07
audioctl -w play.gain=$2 >/dev/null
elif command -v pactl >/dev/null; then
;;
PulseAudio)
# pactl 15.0 compiled+linked with libpulse 15.0.0
pactl set-sink-volume @DEFAULT_SINK@ $2%
;;
*) unknown_system ;;
esac; exit 0 ;;
else unknown_system; fi
exit 0 ;;
(s*)
[ -z "$2" ] || usage
if [ "$(uname)" = "NetBSD" ]; then
# hacky
s*) [ -z "$2" ] || usage
case "$(system_determine)" in
NetBSD) # hacky
audioctl -a \
| grep "play\.gain" \
| cut -d '=' -f 2
elif command -v pactl >/dev/null; then
;;
PulseAudio)
# really hacky, gets the job done
# gets the volume % of Lchan specifically
pactl get-sink-volume @DEFAULT_SINK@ \
@ -86,32 +88,31 @@ case "$argv1" in
| cut -d '/' -f 2 \
| xargs echo \
| sed s/'%'//
;;
*) unknown_system ;;
esac; exit 0 ;;
else unknown_system; fi
exit 0 ;;
#
# SYSTEM independent
#
(+)
[ -n "$2" ] && [ -z "$3" ] && stris uint "$2" \
&& "$argv0" r "$2" \
|| usage
;;
(-)
[ -n "$2" ] && [ -z "$3" ] && stris uint "$2" \
&& "$argv0" r -"$2" \
|| usage
;;
(m*)
[ -z "$2" ] || usage
+) "$argv0" r "$2" ;;
-) "$argv0" r -"$2" ;;
d*)
printf '$0: %s\nAudio system detected: %s\n' \
"$argv0" \
"$(system_determine)"
exit 0 ;;
m*) [ -z "$2" ] || usage
# restore previous volume if there is one
if [ -e "$VOLUME_TMP_FILE" ]; then
"$argv0" a "$(cat "$VOLUME_TMP_FILE")" \
xargs "$argv0" a <"$VOLUME_TMP_FILE" \
&& rm "$VOLUME_TMP_FILE" \
&& printf "Unmuted.\n" \
&& exit 0 \
|| printf "Error restoring previous volume.\n" \
>/dev/stderr \
1>&2 \
&& exit 1
fi
@ -120,10 +121,10 @@ case "$argv1" in
# dd used rather than shell redirect so it's easy to determine
# whether or not the file write worked
if ! printf "%b" "$("$argv0" s)" \
| dd >"$VOLUME_TMP_FILE" 2>/dev/null
| dd of="$VOLUME_TMP_FILE" 2>/dev/null
then
printf "Error writing to file.\n" >/dev/stderr
false
printf "Error writing to file.\n" 1>&2
exit 1
fi
# and then of course mute
@ -131,16 +132,13 @@ case "$argv1" in
printf "Muted.\n"
exit 0 ;;
(r*)
[ -n "$2" ] && [ -z "$3" ] && stris int "$2" \
|| usage
if ! newval=$(add $("$argv0" s) $2) || ! stris int "$newval"; then
printf "%b: Error finding new value for volume.\n" "$argv0"
exit 1
fi
"$argv0" a $newval
r*)
! newval=$(printf "%s %s + p\n" $("$argv0"s) "$2" | dc) \
&& printf "%b: Error finding new value for volume.\n" "$argv0" \
&& exit 1
"$argv0" a "$newval"
exit $? ;;
(h*) usage ;;
(*) usage ;;
*) usage ;;
esac