summaryrefslogtreecommitdiffstats
path: root/lib/panel/volmon
blob: 50f63447d8f5e73ffc7e282ef240e7addb932238 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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()
{
	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 "${@}"