-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor-funcs.in
290 lines (256 loc) · 7.25 KB
/
monitor-funcs.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# BlueALSA bash helper functions
# 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@
_bluealsa_monitor_shortopts="B:hv"
_bluealsa_monitor_longopts="dbus:,help,version"
for _bluealsa_module in $bluealsa_monitor_modules; do
source @libdir@/$_bluealsa_module || {
echo "No such module: @libdir@/$_bluealsa_module"
exit 1
}
_shortopts="${_bluealsa_module}_shortopts"
_longopts="${_bluealsa_module}_longopts"
_bluealsa_monitor_shortopts="${_bluealsa_monitor_shortopts}${!_shortopts}"
[[ "$_longopts" ]] && _bluealsa_monitor_longopts="${_bluealsa_monitor_longopts},${!_longopts}"
done
ARGS=$(getopt -o "$_bluealsa_monitor_shortopts" --long "${_bluealsa_monitor_longopts}" -n "$0" -- "$@")
[[ $? = 0 ]] || exit 1
eval set -- "$ARGS"
declare -A bluealsa_new_pcm
_bluealsa_monitor_stop=0
# check that bluealsa-cli supports the "--version" option
_bluealsa_cli_version_check() {
bluealsa-cli --verbose list-services &>/dev/null
}
# store a pcm property into the global bluealsa_new_pcm array
# $1 the property as printed by bluealsa-cli
_bluealsa_set_property() {
local temp
case "$1" in
Device*)
bluealsa_new_pcm[device]="${1#Device: }"
;;
Sequence*)
bluealsa_new_pcm[sequence]="${1#Sequence: }"
;;
Transport*)
temp="${1#Transport: }"
case "$temp" in
A2DP*)
bluealsa_new_pcm[profile]=A2DP
;;
HFP*)
bluealsa_new_pcm[profile]=HFP
;;
HSP*)
bluealsa_new_pcm[profile]=HSP
;;
esac
;;
Mode*)
bluealsa_new_pcm[mode]="${1#Mode: }"
;;
Format*)
bluealsa_new_pcm[format]="${1#Format: }"
;;
Channels*)
bluealsa_new_pcm[channels]="${1#Channels: }"
;;
Sampling*)
temp="${1#Sampling: }"
bluealsa_new_pcm[rate]="${temp% Hz}"
;;
"Selected codec"*)
bluealsa_new_pcm[codec]="${1#Selected codec: }"
;;
esac
}
_bluealsa_monitor_call() {
local _func=$1
shift
for _bluealsa_module in $bluealsa_monitor_modules; do
[[ $(type -t ${_bluealsa_module}_$_func) == function ]] && ${_bluealsa_module}_$_func "$@"
done
}
_bluealsa_pcm_added() {
bluealsa_new_pcm[alias]=$(bluealsa_get_alias "${bluealsa_new_pcm[device]}")
[[ "${bluealsa_new_pcm[alias]}" ]] || return
_bluealsa_monitor_call pcm_added
}
# get connected PCMs
# uses global new_pcm array to pass data to bluealsa_new_pcm() function
_bluealsa_add_initial_pcms() {
local addr
local REPLY
while read; do
case "$REPLY" in
/*)
[[ -n "${bluealsa_new_pcm[id]}" ]] && _bluealsa_pcm_added
bluealsa_new_pcm=()
bluealsa_new_pcm[id]=${REPLY}
addr=${REPLY#*dev_}
addr=${addr%%/*}
bluealsa_new_pcm[addr]=${addr//_/:}
;;
*)
_bluealsa_set_property "$REPLY"
;;
esac
done <<< $(bluealsa-cli $_bluealsa_dbus_arg --quiet --verbose list-pcms)
[[ -n "${bluealsa_new_pcm[id]}" ]] && _bluealsa_pcm_added
}
_bluealsa_monitor_finished() {
_bluealsa_monitor_call finished
_bluealsa_monitor_stop=1
echo "MonitorInternal STOP" >&$_bluealsa_monitor_fd
}
_bluealsa_print_help() {
cat <<-EOF
Usage:
$0 [option ... ]
Options:
-B,--dbus= select the bluealsa d-bus suffix
-h,--help print help message and exit
-v,--version print version and exit
EOF
_bluealsa_monitor_call print_help
}
_bluealsa_monitor_parse_options() {
while (( $# > 0 )) ; do
case "$1" in
-B|--dbus)
bluealsa_dbus_suffix="$2"
_bluealsa_dbus_arg="--dbus=$2"
shift 2
;;
-h|--help)
_bluealsa_print_help
exit 0
;;
-v|--version)
echo "@version@"
exit 0
;;
*)
shift
;;
esac
done
}
################################################################################
#
# Public functions
#
################################################################################
# Create an anonymous FIFO
# @param $1 name of variable to store the FIFO fd
bluealsa_create_fifo() {
declare -n descriptor="$1"
local path=$(mktemp -u)
mkfifo $path
exec {descriptor}<>$path
rm $path
}
# print device alias from Bluez to stdout
# $1 device dbus object path
bluealsa_get_alias() {
local alias=$(dbus-send --print-reply=literal --system --dest=org.bluez "$1" \
org.freedesktop.DBus.Properties.Get \
string:"org.bluez.Device1" string:"Alias")
printf "%s" "${alias# variant }"
}
# print runtime directory to stdout
bluealsa_rundir() {
local rundir=$(echo "$XDG_RUNTIME_DIR")
if [[ -z "$rundir" ]] ; then
rundir=~/.local/
if [[ ! -d "$rundir" ]] ; then
rundir=/tmp/
fi
fi
rundir="$rundir/bluealsa-monitor"
mkdir -p "$rundir"
printf "%s" "$rundir"
}
# Generate an internal event
bluealsa_internal_event() {
[[ $# = 1 ]] || return
printf "MonitorInternal %s\n" "$1" >&$_bluealsa_monitor_fd
}
bluealsa_monitor() {
$(_bluealsa_cli_version_check) || {
echo "bluealsa-cli version 3.2.0 or later not found" >&2
return 1
}
_bluealsa_monitor_call init
bluealsa_create_fifo _bluealsa_monitor_fd
local cli_pid
bluealsa-cli $_bluealsa_dbus_arg --quiet --verbose monitor >&$_bluealsa_monitor_fd &
cli_pid=$!
trap "kill $cli_pid; _bluealsa_monitor_finished; exec {_bluealsa_monitor_fd}>&-" INT TERM
_bluealsa_add_initial_pcms
_bluealsa_monitor_call start
unset _bluealsa_monitor_stop
local REPLY addr
while [[ -z "$_bluealsa_monitor_stop" ]] && read -u $_bluealsa_monitor_fd 2>/dev/null
do
case "$REPLY" in
PCMAdded*)
bluealsa_new_pcm=()
bluealsa_new_pcm[id]=${REPLY#PCMAdded }
addr=${REPLY#*dev_}
addr=${addr%%/*}
bluealsa_new_pcm[addr]=${addr//_/:}
;;
"")
_bluealsa_pcm_added
bluealsa_new_pcm=()
;;
PCMRemoved*)
bluealsa_new_pcm=()
_bluealsa_monitor_call pcm_removed "${REPLY#PCMRemoved }"
;;
ServiceRunning*)
bluealsa_new_pcm=()
_bluealsa_monitor_call service_started
;;
ServiceStopped*)
bluealsa_new_pcm=()
_bluealsa_monitor_call service_stopped
;;
MonitorInternal*)
_bluealsa_monitor_call handle_internal_event "${REPLY#MonitorInternal }"
;;
*)
_bluealsa_set_property "$REPLY"
;;
esac
done
}
_bluealsa_monitor_parse_options "$@"
for _bluealsa_module in $bluealsa_monitor_modules; do
[[ $(type -t ${_bluealsa_module}_parse_options) == function ]] && ${_bluealsa_module}_parse_options "$@"
done