Jesús c6a2d62411
Multiples fixes:
* [volume: Use '/usr/bin/env bash' as shebang](cd182fd4f2 (diff-210ab9e731c9c36c2c38db15c28a8d1c))

On some systems (e.g. NixOS, openBSD) Bash isn't located or symlinked at
`/bin/bash`. This change ensures that the correct path is used across
different operating systems.

* [volume: add LABEL support](be3f4ae715 (diff-210ab9e731c9c36c2c38db15c28a8d1c))

* [volume: use injected properties](50b2b6ee53 (diff-210ab9e731c9c36c2c38db15c28a8d1c))

See #132

* [volume: pulse-detection: explicitly try the "pulse" device](777a056cce (diff-210ab9e731c9c36c2c38db15c28a8d1c))

Because it's available on some systems (for which pulse is the right
option), but not the default, so `amixer info` will not give the right
result. When this command succeeds, there is no more need to check the
output.

Additionally removed a wrong test bracket (not neccessary and not
working for all bash versions).

* [volume: don't check for pulse via lsmod but command](67dba4fca9 (diff-210ab9e731c9c36c2c38db15c28a8d1c))

- check if command `pulseaudio` exists
- check if pulse is running
- in the case that pulse is detected, still check if the mixer name
  "pulse" exists

- instruction for manual setting in README

Fixes vivien/i3blocks#236
Fixes vivien/i3blocks#201
Fixes: vivien/i3blocks#281

* [volume: allow device names to include numbers](1b3efc6e44 (diff-210ab9e731c9c36c2c38db15c28a8d1c)):

Fixes: vivien/i3blocks#281
2019-07-02 12:51:01 -05:00

84 lines
3.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Copyright (C) 2014 Julien Bonjean <julien@bonjean.info>
# Copyright (C) 2014 Alexander Keller <github@nycroth.com>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#------------------------------------------------------------------------
# The second parameter overrides the mixer selection
# For PulseAudio users, eventually use "pulse"
# For Jack/Jack2 users, use "jackplug"
# For ALSA users, you may use "default" for your primary card
# or you may use hw:# where # is the number of the card desired
if [[ -z "$MIXER" ]] ; then
MIXER="default"
if command -v pulseaudio >/dev/null 2>&1 && pulseaudio --check ; then
# pulseaudio is running, but not all installations use "pulse"
if amixer -D pulse info >/dev/null 2>&1 ; then
MIXER="pulse"
fi
fi
[ -n "$(lsmod | grep jack)" ] && MIXER="jackplug"
MIXER="${2:-$MIXER}"
fi
# The instance option sets the control to report and configure
# This defaults to the first control of your selected mixer
# For a list of the available, use `amixer -D $Your_Mixer scontrols`
if [[ -z "$SCONTROL" ]] ; then
SCONTROL="${BLOCK_INSTANCE:-$(amixer -D $MIXER scontrols |
sed -n "s/Simple mixer control '\([^']*\)',0/\1/p" |
head -n1
)}"
fi
# The first parameter sets the step to change the volume by (and units to display)
# This may be in in % or dB (eg. 5% or 3dB)
if [[ -z "$STEP" ]] ; then
STEP="${1:-5%}"
fi
#------------------------------------------------------------------------
capability() { # Return "Capture" if the device is a capture device
amixer -D $MIXER get $SCONTROL |
sed -n "s/ Capabilities:.*cvolume.*/Capture/p"
}
volume() {
amixer -D $MIXER get $SCONTROL $(capability)
}
format() {
perl_filter='if (/.*\[(\d+%)\] (\[(-?\d+.\d+dB)\] )?\[(on|off)\]/)'
perl_filter+='{CORE::say $4 eq "off" ? "MUTE" : "'
# If dB was selected, print that instead
perl_filter+=$([[ $STEP = *dB ]] && echo '$3' || echo '$1')
perl_filter+='"; exit}'
output=$(perl -ne "$perl_filter")
echo "$LABEL$output"
}
#------------------------------------------------------------------------
case $BLOCK_BUTTON in
3) amixer -q -D $MIXER sset $SCONTROL $(capability) toggle ;; # right click, mute/unmute
4) amixer -q -D $MIXER sset $SCONTROL $(capability) ${STEP}+ unmute ;; # scroll up, increase
5) amixer -q -D $MIXER sset $SCONTROL $(capability) ${STEP}- unmute ;; # scroll down, decrease
esac
volume | format