From f12dbece1838fabbe1fe91358b8fb467953a02b4 Mon Sep 17 00:00:00 2001 From: Deven Blake Date: Tue, 23 Nov 2021 04:15:00 +0000 Subject: [PATCH] Add pactl support --- dotfiles-old/bin/volume | 104 +++++++++++++++++++++++++++------------- 1 file changed, 70 insertions(+), 34 deletions(-) diff --git a/dotfiles-old/bin/volume b/dotfiles-old/bin/volume index c521349..e7837dd 100755 --- a/dotfiles-old/bin/volume +++ b/dotfiles-old/bin/volume @@ -1,18 +1,32 @@ #!/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" \ +# 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. + +! command -v stris >/dev/null 2>/dev/null \ + && printf "%b: Missing stris.\n" "$argv0" \ && exit 1 \ || true VOLUME_TMP_FILE="$HOME/.volume-previous" -usage() { +unknown_system(){ + printf "%s: Unknown system.\n" "$argv0" 1>&2 + exit 1 +} + +usage(){ printf "\ Usage: %b [function] (argument)\n" "$argv0" printf "\ @@ -36,28 +50,57 @@ works the same as \"%b h\". || argv1=s case "$argv1" in -(+) # shortcut +# +# SYSTEM DEPENDENT +# +(a*) + [ -n "$2" ] && [ -z "$3" ] && stris int "$2" || usage + + if [ "$(uname)" = "NetBSD" ]; then + # NetBSD 9.2 STABLE 2021-07 + audioctl -w play.gain=$2 >/dev/null + + elif command -v pactl >/dev/null; then + # pactl 15.0 compiled+linked with libpulse 15.0.0 + pactl set-sink-volume @DEFAULT_SINK@ $2% + + else unknown_system; fi + exit 0 ;; +(s*) + [ -z "$2" ] || usage + + if [ "$(uname)" = "NetBSD" ]; then + # hacky + audioctl -a \ + | grep "play\.gain" \ + | cut -d '=' -f 2 + + elif command -v pactl >/dev/null; then + # 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/'%'// + + else unknown_system; fi + exit 0 ;; +# +# SYSTEM independent +# +(+) [ -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) - # 9.2 STABLE 2021-07 - audioctl -w play.gain=$2 >/dev/null - ;; - (*) unknown_system - esac - exit 0 ;; (h*) usage ;; -(m*) # system independent +(m*) [ -z "$2" ] || usage # restore previous volume if there is one if [ -e "$VOLUME_TMP_FILE" ]; then @@ -74,9 +117,9 @@ 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 of="$VOLUME_TMP_FILE" \ - 2>/dev/null - then + if ! printf "%b" "$("$argv0" s)" \ + | dd >"$VOLUME_TMP_FILE" 2>/dev/null + then printf "Error writing to file.\n" >/dev/stderr false fi @@ -86,22 +129,15 @@ case "$argv1" in printf "Muted.\n" exit 0 ;; -(r*) # system independent - [ -n "$2" ] && [ -z "$3" ] && stris int "$2" || usage +(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" + printf "%b: Error finding new value for volume.\n" "$argv0" 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 - esac - exit 0 ;; (*) usage esac