#!/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() { 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 "${@}"