diff --git a/dotfiles-old/bin/volume b/dotfiles-old/bin/volume index df439a3..c521349 100755 --- a/dotfiles-old/bin/volume +++ b/dotfiles-old/bin/volume @@ -1,18 +1,28 @@ #!/bin/sh +# Has some undocumented features but they're functional (as far as I know) +# and system-independent. just shortcuts for me really. + argv0="$0" +! which stris >/dev/null 2>/dev/null \ + && printf "%b: Missing stris.\n" \ + && exit 1 \ + || true + +VOLUME_TMP_FILE="$HOME/.volume-previous" + usage() { printf "\ -Usage: %b {absolute,help,relative,status} [ARGUMENT...]\n" "$argv0" +Usage: %b [function] (argument)\n" "$argv0" printf "\ -Examples: - %b absolute 0 # mute sound output - %b help # print this help text - %b relative 4 # increase the volume by 4 units - %b relative -4 # decrease the volume by 4 units - %b status # print current volume -" "$argv0" "$argv0" "$argv0" "$argv0" "$argv0" +Functions: + 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\". @@ -21,38 +31,77 @@ works the same as \"%b h\". exit 1 } -[ -n "$1" ] || usage +[ -n "$1" ] \ + && argv1="$1" \ + || argv1=s -case "$1" in +case "$argv1" in +(+) # shortcut + [ -n "$2" ] && [ -z "$3" ] && stris uint "$2" \ + && "$argv0" r "$2" \ + || usage + ;; +(-) # shortcut + [ -n "$2" ] && [ -z "$3" ] && stris uint "$2" \ + && "$argv0" r -"$2" \ + || usage + ;; (a*) + [ -n "$2" ] && [ -z "$3" ] && stris int "$2" || usage case "$(uname)" in (NetBSD) - audioctl -w play.gain=$1 >/dev/null + # 9.2 STABLE 2021-07 + audioctl -w play.gain=$2 >/dev/null ;; - (*) unknown_system + (*) unknown_system esac - ;; -(h*) usage ;; -(r*) - case "$(uname)" in - (NetBSD) - # works. 9.2 STABLE 2021-07 - audioctl -w play.gain=$(( \ - $( \ - audioctl -a \ - | grep "play\.gain" \ - | cut -d '=' -f 2 \ - ) + $2)) >/dev/null - ;; - (*) unknown_system - esac - ;; + exit 0 ;; +(h*) usage ;; +(m*) # system independent + [ -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 of="$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*) # system independent + [ -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" + exit 1 + fi + "$argv0" a $newval + exit $? ;; (s*) + [ -z "$2" ] || usage case "$(uname)" in (NetBSD) audioctl -a | grep "play\.gain" | cut -d '=' -f 2 ;; - (*) unknown_system + (*) unknown_system esac - ;; + exit 0 ;; +(*) usage esac