separate system-independent parts of volume(1)
This commit is contained in:
parent
01fee33d0f
commit
daca2efe30
99
bin/volume
99
bin/volume
@ -1,10 +1,9 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
set -e
|
||||||
|
|
||||||
argv0="$0"
|
argv0="$0"
|
||||||
|
|
||||||
# ADDING A NEW SOUND SERVER/CLIENT/WHATEVERIDK
|
# ADDING A NEW SOUND SERVER/CLIENT/WHATEVERIDK
|
||||||
# `volume a` and `volume s` are the system dependent functions.
|
|
||||||
|
|
||||||
# `volume a $2` must set the volume to $2, an integer value. This should be a
|
# `volume a $2` must set the volume to $2, an integer value. This should be a
|
||||||
# percentage, 100% being the loudest possible volume that can be output by
|
# percentage, 100% being the loudest possible volume that can be output by
|
||||||
# hardware without quality loss. Percentages above 100% will be allowable in
|
# hardware without quality loss. Percentages above 100% will be allowable in
|
||||||
@ -16,22 +15,18 @@ argv0="$0"
|
|||||||
|
|
||||||
VOLUME_TMP_FILE="$HOME/.volume-previous"
|
VOLUME_TMP_FILE="$HOME/.volume-previous"
|
||||||
|
|
||||||
system_determine(){
|
|
||||||
if [ "$(uname)" = NetBSD ]; then
|
if [ "$(uname)" = NetBSD ]; then
|
||||||
printf "NetBSD\n"
|
VOLUME_SYSTEM=netbsd
|
||||||
elif command -v pactl >/dev/null 2>&1; then
|
elif command -v pactl >/dev/null 2>&1; then
|
||||||
printf "PulseAudio\n"
|
VOLUME_SYSTEM=pulseaudio
|
||||||
fi
|
else
|
||||||
}
|
printf "%s: Unknown system.\n" "$0"
|
||||||
|
|
||||||
unknown_system(){
|
|
||||||
printf "%s: Unknown system.\n" "$argv0" 1>&2
|
|
||||||
exit 1
|
exit 1
|
||||||
}
|
fi
|
||||||
|
|
||||||
usage(){
|
usage(){
|
||||||
printf "\
|
printf "\
|
||||||
Usage: %b {function} (operand)\n" "$argv0"
|
Usage: %b {function} (operand)\n" "$0"
|
||||||
printf "\
|
printf "\
|
||||||
Functions:
|
Functions:
|
||||||
'+' - shortcut to \"relative \$2\"
|
'+' - shortcut to \"relative \$2\"
|
||||||
@ -51,63 +46,32 @@ works the same as \"%b h\".
|
|||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# $1 becomes 'help' if previously empty
|
case "$1" in
|
||||||
[ -n "$1" ] \
|
|
||||||
&& argv1="$1" \
|
|
||||||
|| argv1=help
|
|
||||||
|
|
||||||
case "$argv1" in
|
+) "$0" r "$2" ;;
|
||||||
#
|
-) "$0" r -"$2" ;;
|
||||||
# SYSTEM DEPENDENT
|
|
||||||
#
|
|
||||||
a*) [ -n "$2" ] || usage
|
|
||||||
case "$(system_determine)" in
|
|
||||||
NetBSD)
|
|
||||||
# NetBSD 9.2 STABLE 2021-07
|
|
||||||
audioctl -w play.gain=$2 >/dev/null
|
|
||||||
;;
|
|
||||||
PulseAudio)
|
|
||||||
# pactl 15.0 compiled+linked with libpulse 15.0.0
|
|
||||||
pactl set-sink-volume @DEFAULT_SINK@ $2%
|
|
||||||
;;
|
|
||||||
*) unknown_system ;;
|
|
||||||
esac; exit 0 ;;
|
|
||||||
|
|
||||||
s*) [ -z "$2" ] || usage
|
a*)
|
||||||
case "$(system_determine)" in
|
nonzero $2 && ! nonzero $3 && str isdigit "$2" \
|
||||||
NetBSD) # hacky
|
|| usage
|
||||||
audioctl -a \
|
"$0".$VOLUME_SYSTEM "$@"
|
||||||
| grep "play\.gain" \
|
|
||||||
| cut -d '=' -f 2
|
|
||||||
;;
|
;;
|
||||||
PulseAudio)
|
s*)
|
||||||
# really hacky, gets the job done
|
! nonzero $2 \
|
||||||
# gets the volume % of Lchan specifically
|
|| usage
|
||||||
pactl get-sink-volume @DEFAULT_SINK@ \
|
"$0".$VOLUME_SYSTEM s
|
||||||
| sed q \
|
|
||||||
| cut -d '/' -f 2 \
|
|
||||||
| xargs echo \
|
|
||||||
| sed s/'%'//
|
|
||||||
;;
|
;;
|
||||||
*) unknown_system ;;
|
|
||||||
esac; exit 0 ;;
|
|
||||||
|
|
||||||
#
|
|
||||||
# SYSTEM independent
|
|
||||||
#
|
|
||||||
+) "$argv0" r "$2" ;;
|
|
||||||
-) "$argv0" r -"$2" ;;
|
|
||||||
|
|
||||||
d*)
|
d*)
|
||||||
printf '$0: %s\nAudio system detected: %s\n' \
|
printf '$0: %s\nAudio system detected: %s\n' \
|
||||||
"$argv0" \
|
"$0" \
|
||||||
"$(system_determine)"
|
$VOLUME_SYSTEM
|
||||||
exit 0 ;;
|
;;
|
||||||
|
|
||||||
m*) [ -z "$2" ] || usage
|
m*) [ -z "$2" ] || usage
|
||||||
# restore previous volume if there is one
|
# restore previous volume if there is one
|
||||||
if [ -e "$VOLUME_TMP_FILE" ]; then
|
if [ -e "$VOLUME_TMP_FILE" ]; then
|
||||||
xargs "$argv0" a <"$VOLUME_TMP_FILE" \
|
xargs "$0" a <"$VOLUME_TMP_FILE" \
|
||||||
&& rm "$VOLUME_TMP_FILE" \
|
&& rm "$VOLUME_TMP_FILE" \
|
||||||
&& printf "Unmuted.\n" \
|
&& printf "Unmuted.\n" \
|
||||||
&& exit 0 \
|
&& exit 0 \
|
||||||
@ -120,7 +84,7 @@ m*) [ -z "$2" ] || usage
|
|||||||
|
|
||||||
# dd used rather than shell redirect so it's easy to determine
|
# dd used rather than shell redirect so it's easy to determine
|
||||||
# whether or not the file write worked
|
# whether or not the file write worked
|
||||||
if ! printf "%b" "$("$argv0" s)" \
|
if ! printf "%b" "$("$0" s)" \
|
||||||
| dd of="$VOLUME_TMP_FILE" 2>/dev/null
|
| dd of="$VOLUME_TMP_FILE" 2>/dev/null
|
||||||
then
|
then
|
||||||
printf "Error writing to file.\n" 1>&2
|
printf "Error writing to file.\n" 1>&2
|
||||||
@ -128,18 +92,21 @@ m*) [ -z "$2" ] || usage
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# and then of course mute
|
# and then of course mute
|
||||||
"$argv0" a 0
|
"$0" a 0
|
||||||
printf "Muted.\n"
|
printf "Muted.\n"
|
||||||
|
|
||||||
exit 0 ;;
|
exit 0 ;;
|
||||||
|
|
||||||
r*)
|
r*)
|
||||||
set -x
|
nonzero $2 && ! nonzero $3 \
|
||||||
argv2="$(printf "%s\n" "$2" | tr - _)"
|
&& str isdigit "$(printf "%s\n" "$2" | sed 's/^\-//')" \
|
||||||
! newval=$(printf "%s %s + p\n" $("$argv0" s) "$argv2" | dc) \
|
|| usage
|
||||||
&& printf "%b: Error finding new value for volume.\n" "$argv0" \
|
|
||||||
&& exit 1
|
"$0" a \
|
||||||
"$argv0" a "$newval"
|
"$(printf "%s %s + p\n" \
|
||||||
|
$("$0" s) \
|
||||||
|
"$(printf "%s\n" $2 | tr - _)" \
|
||||||
|
| dc)"
|
||||||
exit $? ;;
|
exit $? ;;
|
||||||
|
|
||||||
*) usage ;;
|
*) usage ;;
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
int main(int argc, char *argv[]){
|
int main(int argc, char *argv[]){
|
||||||
return argc > 1;
|
return !(argc > 1);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user