dotfiles
This commit is contained in:
Executable
+158
@@ -0,0 +1,158 @@
|
||||
#!/bin/sh
|
||||
|
||||
argv0="$0"
|
||||
|
||||
# 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
|
||||
# percentage, 100% being the loudest possible volume that can be output by
|
||||
# hardware without quality loss. Percentages above 100% will be allowable in
|
||||
# software if using percentage units. If percentage units are implausible to
|
||||
# implement, system units are okay (e.g. 0-$arbitrary scale versus 0-100).
|
||||
|
||||
# `volume s` must output the current volume in integer form in whatever units
|
||||
# are being used by `volume a` on the given system.
|
||||
|
||||
VOLUME_TMP_FILE="$HOME/.volume-previous"
|
||||
|
||||
if [ "$(uname)" = "NetBSD" ]; then
|
||||
backend="NetBSD"
|
||||
elif command -v pactl >/dev/null; then
|
||||
backend="pulseaudio"
|
||||
fi
|
||||
|
||||
unknown_system(){
|
||||
printf "%s: Unknown system.\n" "$argv0" 1>&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
usage(){
|
||||
printf "\
|
||||
Usage: %b {function} (operand)\n" "$argv0"
|
||||
printf "\
|
||||
Functions:
|
||||
'+' - shortcut to \"relative \$2\"
|
||||
'-' - shortcut to \"relative -\$2\"
|
||||
absolute - change sound output to absolute unit
|
||||
help,'' - print this help output
|
||||
mute - toggle mute/unmute
|
||||
relative - change sound output relative to current status
|
||||
status - print current sound output in system unit
|
||||
"
|
||||
printf "\
|
||||
The options are matched with [char]*. So \"%b help\"
|
||||
works the same as \"%b h\".
|
||||
|
||||
" "$argv0" "$argv0"
|
||||
printf "\
|
||||
The backend detected is %b.
|
||||
" "$backend"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# $1 becomes 'help' if previously empty
|
||||
[ -n "$1" ] \
|
||||
&& argv1="$1" \
|
||||
|| argv1=help
|
||||
|
||||
case "$argv1" in
|
||||
#
|
||||
# SYSTEM DEPENDENT
|
||||
#
|
||||
(a*)
|
||||
[ -n "$2" ] && [ -z "$3" ] || usage
|
||||
|
||||
case "$backend" 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
|
||||
|
||||
case "$backend" in
|
||||
NetBSD)
|
||||
# hacky
|
||||
audioctl -a \
|
||||
| grep "play\.gain" \
|
||||
| cut -d '=' -f 2
|
||||
;;
|
||||
|
||||
pulseaudio)
|
||||
# really hacky, gets the job done
|
||||
# gets the volume % of Lchan specifically
|
||||
pactl get-sink-volume @DEFAULT_SINK@ \
|
||||
| sed q \
|
||||
| cut -d '/' -f 2 \
|
||||
| xargs echo \
|
||||
| sed s/'%'//
|
||||
;;
|
||||
|
||||
*) unknown_system ;;
|
||||
esac
|
||||
exit 0 ;;
|
||||
#
|
||||
# SYSTEM independent
|
||||
#
|
||||
(+)
|
||||
[ -n "$2" ] && [ -z "$3" ] \
|
||||
&& "$argv0" r "$2" \
|
||||
|| usage
|
||||
;;
|
||||
(-)
|
||||
[ -n "$2" ] && [ -z "$3" ] \
|
||||
&& "$argv0" r -"$2" \
|
||||
|| usage
|
||||
;;
|
||||
(m*)
|
||||
[ -z "$2" ] || usage
|
||||
# restore previous volume if there is one
|
||||
if [ -e "$VOLUME_TMP_FILE" ]; then
|
||||
"$argv0" a "$(cat "$VOLUME_TMP_FILE")" \
|
||||
&& rm "$VOLUME_TMP_FILE" \
|
||||
&& printf "Unmuted.\n" \
|
||||
&& exit 0 \
|
||||
|| printf "Error restoring previous volume.\n" \
|
||||
>/dev/stderr \
|
||||
&& exit 1
|
||||
fi
|
||||
|
||||
# otherwise, make new file with previous volume
|
||||
|
||||
# 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
|
||||
then
|
||||
printf "Error writing to file.\n" >/dev/stderr
|
||||
false
|
||||
fi
|
||||
|
||||
# and then of course mute
|
||||
"$argv0" a 0
|
||||
printf "Muted.\n"
|
||||
|
||||
exit 0 ;;
|
||||
(r*)
|
||||
[ -n "$2" ] && [ -z "$3" ] \
|
||||
|| usage
|
||||
|
||||
if ! newval="$(printf "%s %s + p\n" "$("$argv0" s)" "$(printf "%s\n" "$2" | sed 's/^-/_/')" | dc)"; then
|
||||
printf "%b: Error finding new value for volume.\n" "$argv0"
|
||||
exit 1
|
||||
fi
|
||||
"$argv0" a "$newval"
|
||||
exit $? ;;
|
||||
(h*) usage ;;
|
||||
(*) usage ;;
|
||||
esac
|
||||
Reference in New Issue
Block a user