-
Notifications
You must be signed in to change notification settings - Fork 0
/
alsaconf.in
261 lines (226 loc) · 7.91 KB
/
alsaconf.in
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# 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 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.
#
# For more information, please refer to <https://unlicense.org>
#
# revision @version@
alsaconf_profile=a2dp
alsaconf_stream=duplex
alsaconf_transport_filter=a2dp
alsaconf_mode_filter=source,sink
alsaconf_srv=""
alsaconf_conffile=""
alsaconf_use_default=false
declare -A alsaconf_pcms
declare -A alsaconf_ctls
declare -A alsaconf_Input_defaults
declare -A alsaconf_Output_defaults
declare -a alsaconf_alsaver
alsaconf_shortopts="d::"
alsaconf_longopts="default::"
alsaconf_print_help() {
cat <<-EOF
-d,--default make bluealsa PCM the ALSA default when connected
- optional arg: [a2dp|sco][,[capture|playback]]
- default is a2dp, both capture and playback
EOF
}
alsaconf_parse_options() {
while (( $# > 0 )) ; do
case "$1" in
-d|--default)
alsaconf_use_default="a2dp,capture,playback"
shift
[[ -n "$1" ]] && alsaconf_use_default="$1"
shift
;;
*)
shift
;;
esac
done
}
alsaconf_parse_default() {
local IFS=','
local temp=( $alsaconf_use_default )
local word capture playback a2dp sco
for word in "${temp[@]}"; do
case "$word" in
'capture') capture=1 ;;
'playback') playback=1 ;;
'a2dp') a2dp=1 ;;
'sco') sco=1 ;;
*)
echo "Invalid --default argument '$word'" >&2
exit 1
;;
esac
done
[[ "$capture" = 1 ]] && { alsaconf_mode_filter=source ; alsaconf_stream=capture ; }
[[ "$playback" = 1 ]] && { alsaconf_mode_filter=sink ; alsaconf_stream=playback ; }
[[ "$capture" = 1 && "$playback" = 1 ]] && { alsaconf_mode_filter=sinksource; alsaconf_stream=duplex ; }
[[ "$a2dp" = 1 ]] && alsaconf_profile=a2dp && alsaconf_transport_filter=a2dp
[[ "$sco" = 1 ]] && alsaconf_profile=sco && alsaconf_transport_filter=sco
if [[ "$a2dp" = 1 && "$sco" = 1 ]] ; then
echo "Default cannot be both a2dp and sco" >&2
exit 1;
fi
}
# get alsa installed version
alsaconf_get_alsaver() {
local alsa_ver_str=$(aplay --version)
alsa_ver_str=${alsa_ver_str% by*}
alsa_ver_str=${alsa_ver_str#*version }
local IFS=.
alsaconf_alsaver=( $alsa_ver_str )
}
# delete all stale entries from alsa config file
alsaconf_init_config() {
cat > "$alsaconf_conffile" <<-EOF
# DO NOT EDIT - automatically managed by bluealsa-monitor service
bluealsa.default.capture.pcm {
type asym
capture.pcm "pcm.bluealsa:PROFILE=$alsaconf_profile$alsaconf_srv"
playback.pcm {
@func refer
name "bluealsa.default.playback.fallback"
default sysdefault
}
hint.show on
hint.description "Capture: Bluetooth default, Playback: sysdefault"
}
bluealsa.default.playback.pcm {
type asym
capture.pcm {
@func refer
name "bluealsa.default.capture.fallback"
default sysdefault
}
playback.pcm "pcm.bluealsa:PROFILE=$alsaconf_profile$alsaconf_srv"
hint.show on
hint.description "Capture: sysdefault, Playback: Bluetooth default"
}
bluealsa.default.duplex.pcm {
type empty
slave.pcm "pcm.bluealsa:PROFILE=$alsaconf_profile$alsaconf_srv"
hint.show on
hint.description "Bluetooth default"
}
namehint.ctl.bluealsa "bluealsa|${alsaconf_DESC}BlueALSA global control device"
EOF
}
alsaconf_init() {
alsaconf_running=
alsaconf_conffile="$(bluealsa_rundir)/asoundrc"
[[ "$bluealsa_dbus_suffix" ]] && alsaconf_srv=",SRV=org.bluealsa.$bluealsa_dbus_suffix"
[[ "$alsaconf_use_default" = false ]] || alsaconf_parse_default
# For libasound version < 1.2.3.2, we need to include the tag "DESC" in a namehint
alsaconf_DESC="DESC"
alsaconf_get_alsaver
if [[ "${alsaconf_alsaver[0]}" -ge 1 && "${alsaconf_alsaver[1]}" -ge 2 && "${alsaconf_alsaver[2]}" -ge 3 ]] ; then
if [[ "${alsaconf_alsaver[2]}" -gt 3 || "${alsaconf_alsaver[3]}" -ge 2 ]] ; then
alsaconf_DESC=""
fi
fi
alsaconf_init_config
}
# create namehints for pcm and ctl, write them to the config
# also adds the pcm to global defaults arrays if appropriate
# reads data from the global bluealsa_new_pcm array
alsaconf_add_namehint() {
local alias=${bluealsa_new_pcm[alias]}
local io=Output
[[ "${bluealsa_new_pcm[mode]}" = source ]] && io=Input
local transport="${bluealsa_new_pcm[profile],,}"
[[ "$transport" = a2dp ]] || transport=sco
local codec
[[ ${bluealsa_new_pcm[codec]} = "<null>" ]] || codec=" (${bluealsa_new_pcm[codec]})"
alsaconf_pcms["${bluealsa_new_pcm[id]}"]="${bluealsa_new_pcm[addr]//:/}$transport$io"
alsaconf_ctls["${bluealsa_new_pcm[id]}"]="${bluealsa_new_pcm[addr]//:/}"
cat >> "$alsaconf_conffile" <<-EOF
namehint.pcm.bluealsa${alsaconf_pcms["${bluealsa_new_pcm[id]}"]} "bluealsa:DEV=${bluealsa_new_pcm[addr]},PROFILE=${transport}${alsaconf_srv}|${alsaconf_DESC}${alias}, ${bluealsa_new_pcm[profile]}${codec}|IOID${io}"
EOF
grep -q "^namehint.ctl.bluealsa${alsaconf_ctls["${bluealsa_new_pcm[id]}"]}" "$alsaconf_conffile" || cat >> "$alsaconf_conffile" <<-EOF
namehint.ctl.bluealsa${alsaconf_ctls["${bluealsa_new_pcm[id]}"]} "bluealsa:DEV=${bluealsa_new_pcm[addr]}$alsaconf_srv|${alsaconf_DESC}${alias} control device"
EOF
if [[ "$alsaconf_transport_filter" =~ $transport && "$alsaconf_mode_filter" =~ ${bluealsa_new_pcm[mode]} ]]
then
if [[ ${io} = Input ]] ; then
alsaconf_Input_defaults["${bluealsa_new_pcm[id]}"]="${bluealsa_new_pcm[id]}"
else
alsaconf_Output_defaults["${bluealsa_new_pcm[id]}"]="${bluealsa_new_pcm[id]}"
fi
fi
}
# remove namehint from the config
# also removes pcm from the global defaults
# $1 the id of the pcm to be removed
alsaconf_remove_namehint() {
sed -i '/^namehint.pcm.bluealsa'${alsaconf_pcms["$1"]}'/,+1d' "$alsaconf_conffile"
[[ -n "${alsaconf_ctls["$1"]}" ]] && sed -i '/^namehint.ctl.bluealsa'${alsaconf_ctls["$1"]}'/,+1d' "$alsaconf_conffile"
if [[ alsaconf_pcms["$1"] == *Input ]] ; then
unset alsaconf_Input_defaults["$1"]
else
unset alsaconf_Output_defaults["$1"]
fi
unset alsaconf_pcms["$1"]
unset alsaconf_ctls["$1"]
}
# update the config pcm.!default entry if necessary
alsaconf_set_default() {
[[ "$alsaconf_use_default" = false ]] && return
local stream
[[ "${#alsaconf_Input_defaults[@]}" -gt 0 ]] && stream=capture
[[ "${#alsaconf_Output_defaults[@]}" -gt 0 ]] && {
if [[ -z "$stream" ]] ; then
stream=playback
else
stream=duplex
fi
}
sed -i '/^pcm.!default/d' "$alsaconf_conffile" 2>/dev/null
[[ -n "$stream" ]] && echo 'pcm.!default "bluealsa.default.'"$stream"'.pcm"' >> "$alsaconf_conffile"
}
alsaconf_start() {
alsaconf_running=true
alsaconf_set_default
}
alsaconf_pcm_added() {
alsaconf_add_namehint
[[ $alsaconf_running ]] && {
alsaconf_set_default
}
}
alsaconf_pcm_removed() {
alsaconf_remove_namehint "$1"
alsaconf_set_default
}
alsaconf_service_stopped() {
for item in "${!alsaconf_pcms[@]}" ; do
alsaconf_remove_namehint "$item"
done
sed -i '/^pcm.!default/d' "$alsaconf_conffile" 2>/dev/null
[[ "$alsaconf_use_default" = false ]] || sed -i '/^pcm.!default/d' "$alsaconf_conffile" 2>/dev/null
}
alsaconf_finished() {
alsaconf_service_stopped
}