diff options
Diffstat (limited to 'lib/panel')
-rwxr-xr-x | lib/panel/volmon | 100 |
1 files changed, 97 insertions, 3 deletions
diff --git a/lib/panel/volmon b/lib/panel/volmon index 0b1b87e..50f6344 100755 --- a/lib/panel/volmon +++ b/lib/panel/volmon @@ -1,10 +1,104 @@ #!/bin/sh +# +# volmon -- Panel applet to monitor ALSA mixer volume +# +# Copyright (C) 2018 Patrick "P. J." McDermott +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +set -eu + +# Default values, overridden in ~/.config/panel/volmonrc +scontrol='Master,0' +db_format_on='%3.0f' +db_format_off='off' +output='dB Vol:\n${Front_Left} ${Front_Right}\n${Side_Left} ${Side_Right}\n' +output="${output}"'${Rear_Left} ${Rear_Right}\n' + +volmon() +{ + local pb_bre= + local channel_var= + local db_vol= + local on_off= + local vol_str= + + pb_bre='^ \([^:][^:]*\): Playback [0-9][0-9]* \[[0-9][0-9]*%\]' + pb_bre="${pb_bre}"' \[\(-*[0-9][0-9]*.[0-9][0-9]\)dB\] \[\(o[nf]*\)\]$' + + while :; do + if ! read -r channel_var; then + break + fi + if ! read -r db_vol on_off; then + break + fi + case "${on_off}" in + 'on') + vol_str="$(printf "${db_format_on}" ${db_vol})" + ;; + 'off') + vol_str="$(printf "${db_format_off}" ${db_vol})" + ;; + esac + eval "${channel_var}=\"\${vol_str}\"" + done <<-EOF + $(amixer get "${scontrol}" | sed -n " + /${pb_bre}/{ # Playback channel lines. + h; # Save original line. + s/${pb_bre}/\1/; # Extract channel name. + s/[^A-Za-z0-9]/_/g; # Make safe for var name. + p; # And print it. + g; # Restore original line. + s/${pb_bre}/\2 \3/; # Extract volume and mute. + p; # And print it. + };") + EOF + + eval "printf \"${output}\"" + + return 0 +} + +usage() +{ + printf 'Usage: %s\n' "${0}" + + return 0 +} main() { - vol="$(amixer get Master,0 | \ - sed -n 's/^.*Playback [0-9]* \[\([0-9]*\)%\].*$/\1/p')" - printf '🔉%3d%%\n' ${vol} + if [ ${#} -ne 0 ]; then + usage 1>&2 + return 1 + fi + + # POSIX on the dot utility: + # "If no readable file is found, a non-interactive shell shall abort" + # So to survive an ENOENT or other error, we need this eval/cat command. + eval "$(cat ~/.config/panel/volmonrc 2>/dev/null || :)" + + volmon + + return 0 } main "${@}" |