| 1 |
#!/bin/bash |
| 2 |
# bash completion for bluez-alsa project applications |
| 3 |
|
| 4 |
# helper function gets available profiles |
| 5 |
# @param $1 the bluealsa executable name |
| 6 |
_bluealsa_profiles() { |
| 7 |
"$1" --help | while read -r line; do |
| 8 |
[[ "$line" = "Available BT profiles:" ]] && start=yes && continue |
| 9 |
[[ "$start" ]] || continue |
| 10 |
[[ "$line" ]] || break |
| 11 |
read -ra words <<< "$line" |
| 12 |
echo "${words[1]}" |
| 13 |
done |
| 14 |
} |
| 15 |
|
| 16 |
# helper function gets available codecs |
| 17 |
# @param $1 the bluealsa executable name |
| 18 |
_bluealsa_codecs() { |
| 19 |
"$1" --help | while read -r line; do |
| 20 |
[[ "$line" = "Available BT audio codecs:" ]] && start=yes && continue |
| 21 |
[[ "$start" ]] || continue |
| 22 |
[[ "$line" ]] || break |
| 23 |
line=${line,,} |
| 24 |
IFS=", " read -ra words <<< "${line,,}" |
| 25 |
echo "${words[@]:1}" |
| 26 |
done |
| 27 |
} |
| 28 |
|
| 29 |
# helper function gets available enum values |
| 30 |
# @param $1 the bluealsa executable name |
| 31 |
# @param $2 the bluealsa option to inspect |
| 32 |
_bluealsa_enum_values() { |
| 33 |
"$1" "${2}=" 2>&1 | while read -r line; do |
| 34 |
[[ $line =~ \{([^}]*)\} ]] || continue |
| 35 |
echo "${BASH_REMATCH[1]//,/}" |
| 36 |
done |
| 37 |
} |
| 38 |
|
| 39 |
# helper function gets dbus services |
| 40 |
# note that this function does not detect if default service (no suffix) is running. |
| 41 |
_bluealsa_list_dbus_suffices() { |
| 42 |
dbus-send --system --dest=org.freedesktop.DBus --type=method_call \ |
| 43 |
--print-reply /org/freedesktop/DBus \ |
| 44 |
org.freedesktop.DBus.ListNames 2>/dev/null | \ |
| 45 |
while read -r line; do |
| 46 |
[[ $line =~ org\.bluealsa\.([^'"']+) ]] || continue |
| 47 |
echo "${BASH_REMATCH[1]}" |
| 48 |
done |
| 49 |
} |
| 50 |
|
| 51 |
# helper function gets codecs for given pcm |
| 52 |
# before calling this function, make sure that the bautil_args was properly |
| 53 |
# initialized with the _bluealsa_util_init function |
| 54 |
# @param $1 pcm path |
| 55 |
_bluealsa_pcm_codecs() { |
| 56 |
"${bautil_args[@]}" codec "$1" | while read -r line; do |
| 57 |
[[ $line =~ ^Available\ codecs:\ ([^[]+$) ]] || continue |
| 58 |
echo "${BASH_REMATCH[1]}" |
| 59 |
done |
| 60 |
} |
| 61 |
|
| 62 |
# helper function completes supported bluealsa-cli monitor properties |
| 63 |
_bluealsa_cli_properties() { |
| 64 |
local properties=( codec running softvolume volume ) |
| 65 |
if [[ "$cur" == *,* ]]; then |
| 66 |
local realcur prefix chosen remaining |
| 67 |
realcur="${cur##*,}" |
| 68 |
prefix="${cur%,*}" |
| 69 |
IFS="," read -ra chosen <<< "${prefix,,}" |
| 70 |
readarray -t remaining < <(printf '%s\n' "${properties[@]}" "${chosen[@]}" | sort | uniq -u) |
| 71 |
if [[ ${#remaining[@]} -gt 0 ]]; then |
| 72 |
readarray -t COMPREPLY < <(compgen -W "${remaining[*]}" -- "$realcur") |
| 73 |
if [[ ${#COMPREPLY[@]} -eq 1 ]] ; then |
| 74 |
COMPREPLY[0]="$prefix,${COMPREPLY[0]}" |
| 75 |
fi |
| 76 |
if [[ ${#remaining[@]} -gt 0 && "$cur" == "${COMPREPLY[0]}" ]] ; then |
| 77 |
COMPREPLY=( "${COMPREPLY[0]}," ) |
| 78 |
fi |
| 79 |
if [[ ${#remaining[@]} -gt 1 ]]; then |
| 80 |
compopt -o nospace |
| 81 |
fi |
| 82 |
fi |
| 83 |
else |
| 84 |
readarray -t COMPREPLY < <(compgen -W "${properties[*]}" -- "$cur") |
| 85 |
if [[ ${#COMPREPLY[@]} -eq 1 && "$cur" == "${COMPREPLY[0]}" ]]; then |
| 86 |
COMPREPLY=("${COMPREPLY[0]},") |
| 87 |
fi |
| 88 |
compopt -o nospace |
| 89 |
fi |
| 90 |
} |
| 91 |
|
| 92 |
# helper function gets ALSA pcms |
| 93 |
# @param $1 is the current word to match |
| 94 |
_bluealsa_aplay_pcms() { |
| 95 |
eval local cur="$1" |
| 96 |
cur="${cur/%\\/\\\\}" |
| 97 |
while read -r; do |
| 98 |
[[ "$REPLY" == " "* ]] && continue |
| 99 |
[[ "$REPLY" == "$cur"* ]] || continue |
| 100 |
echo "${REPLY// /\\ }" |
| 101 |
done < <(aplay -L 2>/dev/null) |
| 102 |
} |
| 103 |
|
| 104 |
# helper function gets rfcomm dbus paths |
| 105 |
# @param $1 the full bluealsa service name ( org.bluealsa* ) |
| 106 |
_bluealsa_rfcomm_paths() { |
| 107 |
busctl --list tree "$1" 2>/dev/null | while read -r line; do |
| 108 |
[[ "$line" = /org/bluealsa/hci[0-9]/dev*/rfcomm ]] || continue |
| 109 |
echo "${line/\/rfcomm/}" |
| 110 |
done |
| 111 |
} |
| 112 |
|
| 113 |
# helper function gets options from command line |
| 114 |
# @param $1 the executable name |
| 115 |
# puts exec and dbus arg ( --dbus=aaa ) into array variable bautil_args |
| 116 |
# puts service name ( org.bluealsa.aaa ) into variable service |
| 117 |
# puts offset of first non-option argument into variable nonopt_offset |
| 118 |
# @return 0 if no errors found, 1 if dbus check failed |
| 119 |
_bluealsa_util_init() { |
| 120 |
local valopts="-B --dbus" |
| 121 |
bautil_args=( "$1" ) |
| 122 |
service="org.bluealsa" |
| 123 |
nonopt_offset=0 |
| 124 |
local i |
| 125 |
for (( i=1; i <= COMP_CWORD; i++ )); do |
| 126 |
case "${COMP_WORDS[i]}" in |
| 127 |
--dbus) |
| 128 |
if (( i == COMP_CWORD )) ; then |
| 129 |
break |
| 130 |
elif (( i == COMP_CWORD - 1 )) ; then |
| 131 |
[[ "${COMP_WORDS[i+1]}" = = ]] && break |
| 132 |
bautil_args+=( "--dbus=${COMP_WORDS[i+1]}" ) |
| 133 |
service="org.bluealsa.${COMP_WORDS[i+1]}" |
| 134 |
break |
| 135 |
else |
| 136 |
[[ "${COMP_WORDS[i+1]}" = = ]] && (( i++ )) |
| 137 |
if [[ "${COMP_WORDS[i+1]}" ]] ; then |
| 138 |
(( i++ )) |
| 139 |
bautil_args+=( "--dbus=${COMP_WORDS[i]}" ) |
| 140 |
service="org.bluealsa.${COMP_WORDS[i]}" |
| 141 |
continue |
| 142 |
fi |
| 143 |
fi |
| 144 |
;; |
| 145 |
-B) |
| 146 |
if (( i == COMP_CWORD )) ; then |
| 147 |
break |
| 148 |
elif (( i == COMP_CWORD - 1 )) ; then |
| 149 |
bautil_args+=( "--dbus=${COMP_WORDS[i+1]}" ) |
| 150 |
service="org.bluealsa.${COMP_WORDS[i+1]}" |
| 151 |
break |
| 152 |
else |
| 153 |
if [[ "${COMP_WORDS[i+1]}" ]] ; then |
| 154 |
(( i++ )) |
| 155 |
bautil_args+=( "--dbus=${COMP_WORDS[i]}" ) |
| 156 |
service="org.bluealsa.${COMP_WORDS[i]}" |
| 157 |
continue |
| 158 |
fi |
| 159 |
fi |
| 160 |
;; |
| 161 |
-*|=) |
| 162 |
continue |
| 163 |
;; |
| 164 |
esac |
| 165 |
|
| 166 |
[[ "${COMP_WORDS[i-1]}" == = ]] && (( i < COMP_CWORD )) && continue |
| 167 |
[[ "$valopts " == *"${COMP_WORDS[i-1]} "* ]] && continue |
| 168 |
|
| 169 |
nonopt_offset=$i |
| 170 |
break |
| 171 |
|
| 172 |
done |
| 173 |
|
| 174 |
return 0 |
| 175 |
} |
| 176 |
|
| 177 |
# helper function completes options |
| 178 |
_bluealsa_complete_options() { |
| 179 |
readarray -t COMPREPLY < <(compgen -W "$(_parse_help "$1")" -- "$cur") |
| 180 |
[[ ${COMPREPLY[0]} == *= ]] && compopt -o nospace |
| 181 |
} |
| 182 |
|
| 183 |
# completion function for bluealsa |
| 184 |
# complete available devices and profiles in addition to options |
| 185 |
_bluealsa() { |
| 186 |
|
| 187 |
local cur prev words cword split |
| 188 |
_init_completion -s || return |
| 189 |
|
| 190 |
local prefix list |
| 191 |
|
| 192 |
case "$prev" in |
| 193 |
--device|-i) |
| 194 |
readarray -t list < <(ls -I '*:*' /sys/class/bluetooth) |
| 195 |
readarray -t COMPREPLY < <(compgen -W "${list[*]}" -- "$cur") |
| 196 |
return |
| 197 |
;; |
| 198 |
--profile|-p) |
| 199 |
[[ $cur =~ ^[+-].* ]] && prefix=${cur:0:1} && cur=${cur:1} |
| 200 |
readarray -t list < <(_bluealsa_profiles "$1") |
| 201 |
readarray -t COMPREPLY < <(compgen -P "$prefix" -W "${list[*]}" -- "$cur") |
| 202 |
return |
| 203 |
;; |
| 204 |
--codec|-c) |
| 205 |
[[ $cur =~ ^[+-].* ]] && prefix=${cur:0:1} && cur=${cur:1} |
| 206 |
readarray -t list < <(_bluealsa_codecs "$1") |
| 207 |
readarray -t COMPREPLY < <(compgen -P "$prefix" -W "${list[*]}" -- "$cur") |
| 208 |
return |
| 209 |
;; |
| 210 |
--loglevel|--sbc-quality|--aac-latm-version|--mp3-algorithm|--mp3-vbr-quality|--ldac-quality) |
| 211 |
readarray -t list < <(_bluealsa_enum_values "$1" "$prev") |
| 212 |
readarray -t COMPREPLY < <(compgen -W "${list[*]}" -- "$cur") |
| 213 |
return |
| 214 |
;; |
| 215 |
--xapl-resp-name) |
| 216 |
readarray -t COMPREPLY < <(compgen -W "BlueALSA iPhone" -- "$cur") |
| 217 |
return |
| 218 |
;; |
| 219 |
--*) |
| 220 |
[[ ${COMP_WORDS[COMP_CWORD]} = = ]] && return |
| 221 |
;; |
| 222 |
esac |
| 223 |
|
| 224 |
_bluealsa_complete_options "$1" |
| 225 |
} |
| 226 |
|
| 227 |
# completion function for bluealsa-aplay |
| 228 |
# completes available dbus suffices and ALSA pcms in addition to options |
| 229 |
# - does not complete MAC addresses |
| 230 |
# requires aplay to list ALSA pcms |
| 231 |
_bluealsa_aplay() { |
| 232 |
|
| 233 |
local cur prev words cword split |
| 234 |
_init_completion -s -n : || return |
| 235 |
|
| 236 |
local list |
| 237 |
|
| 238 |
case "$prev" in |
| 239 |
--dbus|-B) |
| 240 |
_have dbus-send || return |
| 241 |
readarray -t list < <(_bluealsa_list_dbus_suffices) |
| 242 |
readarray -t COMPREPLY < <(compgen -W "${list[*]}" -- "$cur") |
| 243 |
return |
| 244 |
;; |
| 245 |
--pcm|-D) |
| 246 |
_have aplay || return |
| 247 |
|
| 248 |
# do not attempt completion on words containing ' or " |
| 249 |
[[ "$cur" =~ [\'\"] ]] && return |
| 250 |
|
| 251 |
readarray -t COMPREPLY < <(_bluealsa_aplay_pcms "$cur") |
| 252 |
|
| 253 |
# ALSA pcm names can contain '=' and ':', both of which cause |
| 254 |
# problems for bash completion if it considers them to be word |
| 255 |
# terminators. So we adjust the list of candidate matches to allow |
| 256 |
# for this. |
| 257 |
if [[ "$cur" == *=* && "$COMP_WORDBREAKS" == *=* ]]; then |
| 258 |
# Remove equal-word prefix from COMPREPLY items |
| 259 |
local equal_prefix=${cur%"${cur##*=}"} |
| 260 |
local i=${#COMPREPLY[*]} |
| 261 |
while [[ $((--i)) -ge 0 ]]; do |
| 262 |
COMPREPLY[i]=${COMPREPLY[$i]#"$equal_prefix"} |
| 263 |
done |
| 264 |
fi |
| 265 |
__ltrim_colon_completions "$cur" |
| 266 |
return |
| 267 |
;; |
| 268 |
--loglevel|--volume) |
| 269 |
readarray -t list < <(_bluealsa_enum_values "$1" "$prev") |
| 270 |
readarray -t COMPREPLY < <(compgen -W "${list[*]}" -- "$cur") |
| 271 |
return |
| 272 |
;; |
| 273 |
--*) |
| 274 |
[[ ${COMP_WORDS[COMP_CWORD]} = = ]] && return |
| 275 |
;; |
| 276 |
esac |
| 277 |
|
| 278 |
_bluealsa_complete_options "$1" |
| 279 |
} |
| 280 |
|
| 281 |
# completion function for bluealsa-cli |
| 282 |
# complete available dbus suffices, command names and pcm paths in addition to options |
| 283 |
_bluealsa_cli() { |
| 284 |
|
| 285 |
local cur prev words cword split |
| 286 |
_init_completion -s || return |
| 287 |
|
| 288 |
local bautil_args service |
| 289 |
local -i nonopt_offset |
| 290 |
_bluealsa_util_init "$1" || return |
| 291 |
|
| 292 |
# the command names supported by this version of bluealsa-cli |
| 293 |
local simple_commands="list-pcms list-services monitor status" |
| 294 |
local path_commands="codec delay-adjustment info mute open soft-volume volume" |
| 295 |
|
| 296 |
# options that may appear before or after the command |
| 297 |
local global_shortopts="-h" |
| 298 |
local global_longopts="--help" |
| 299 |
|
| 300 |
# options that may appear only before the command |
| 301 |
local base_shortopts="-B -V -q -v" |
| 302 |
local base_longopts="--dbus= --version --quiet --verbose" |
| 303 |
|
| 304 |
local command path list |
| 305 |
|
| 306 |
# process pre-command options |
| 307 |
case "$nonopt_offset" in |
| 308 |
0) |
| 309 |
case "$prev" in |
| 310 |
--dbus|-B) |
| 311 |
_have dbus-send || return |
| 312 |
readarray -t list < <(_bluealsa_list_dbus_suffices) |
| 313 |
readarray -t COMPREPLY < <(compgen -W "${list[*]}" -- "$cur") |
| 314 |
return |
| 315 |
;; |
| 316 |
esac |
| 317 |
case "$cur" in |
| 318 |
-|--*) |
| 319 |
readarray -t COMPREPLY < <(compgen -W "$global_longopts $base_longopts" -- "$cur") |
| 320 |
[[ "${COMPREPLY[0]}" == *= ]] && compopt -o nospace |
| 321 |
;; |
| 322 |
-?) |
| 323 |
readarray -t COMPREPLY < <(compgen -W "$global_shortopts $base_shortopts" -- "$cur") |
| 324 |
;; |
| 325 |
esac |
| 326 |
return |
| 327 |
;; |
| 328 |
"$COMP_CWORD") |
| 329 |
# list available commands |
| 330 |
readarray -t COMPREPLY < <(compgen -W "$simple_commands $path_commands" -- "$cur") |
| 331 |
return |
| 332 |
;; |
| 333 |
esac |
| 334 |
|
| 335 |
# check for valid command |
| 336 |
command="${COMP_WORDS[nonopt_offset]}" |
| 337 |
[[ "$simple_commands $path_commands" =~ $command ]] || return |
| 338 |
|
| 339 |
# process command-specific options |
| 340 |
case "$command" in |
| 341 |
monitor) |
| 342 |
case "$prev" in |
| 343 |
--properties) |
| 344 |
_bluealsa_cli_properties |
| 345 |
return |
| 346 |
;; |
| 347 |
esac |
| 348 |
global_longopts+=" --properties" |
| 349 |
global_shortopts+=" -p" |
| 350 |
case "$cur" in |
| 351 |
-p) |
| 352 |
COMPREPLY=( "--properties" ) |
| 353 |
compopt -o nospace |
| 354 |
return |
| 355 |
;; |
| 356 |
-p?*) |
| 357 |
COMPREPLY=( "--properties=${cur:2}" ) |
| 358 |
compopt -o nospace |
| 359 |
return |
| 360 |
;; |
| 361 |
--properties) |
| 362 |
COMPREPLY=( "--properties=" ) |
| 363 |
compopt -o nospace |
| 364 |
return |
| 365 |
;; |
| 366 |
-) |
| 367 |
COMPREPLY=( "--" ) |
| 368 |
compopt -o nospace |
| 369 |
return |
| 370 |
;; |
| 371 |
esac |
| 372 |
;; |
| 373 |
esac |
| 374 |
|
| 375 |
# find path argument |
| 376 |
for (( i=(nonopt_offset + 1); i < COMP_CWORD; i++ )); do |
| 377 |
[[ "${COMP_WORDS[i]}" == -* ]] && continue |
| 378 |
path="${COMP_WORDS[i]}" |
| 379 |
path_offset=i |
| 380 |
break |
| 381 |
done |
| 382 |
|
| 383 |
# process global options and path |
| 384 |
if [[ -z "$path" ]] ; then |
| 385 |
case "$cur" in |
| 386 |
-|--*) |
| 387 |
readarray -t COMPREPLY < <(compgen -W "$global_longopts" -- "$cur") |
| 388 |
[[ "${COMPREPLY[0]}" == --properties* ]] && compopt -o nospace |
| 389 |
return |
| 390 |
;; |
| 391 |
-?) |
| 392 |
readarray -t COMPREPLY < <(compgen -W "$global_shortopts" -- "$cur") |
| 393 |
return |
| 394 |
;; |
| 395 |
esac |
| 396 |
if [[ "$path_commands" =~ $command ]]; then |
| 397 |
readarray -t list < <("${bautil_args[@]}" list-pcms 2>/dev/null) |
| 398 |
readarray -t COMPREPLY < <(compgen -W "${list[*]}" -- "$cur") |
| 399 |
return |
| 400 |
fi |
| 401 |
fi |
| 402 |
|
| 403 |
# process command positional arguments |
| 404 |
case "$command" in |
| 405 |
codec) |
| 406 |
if (( COMP_CWORD == path_offset + 1 )) ; then |
| 407 |
readarray -t list < <(_bluealsa_pcm_codecs "$path") |
| 408 |
readarray -t COMPREPLY < <(compgen -W "${list[*]}" -- "$cur") |
| 409 |
fi |
| 410 |
;; |
| 411 |
mute) |
| 412 |
if (( COMP_CWORD < path_offset + 3 )) ; then |
| 413 |
if [[ "$cur" == "" ]] ; then |
| 414 |
COMPREPLY=( true false ) |
| 415 |
else |
| 416 |
readarray -t COMPREPLY < <(compgen -W "on yes true 1 off no false 0" -- "${cur,,}") |
| 417 |
fi |
| 418 |
fi |
| 419 |
;; |
| 420 |
soft-volume) |
| 421 |
if (( COMP_CWORD < path_offset + 2 )) ; then |
| 422 |
if [[ "$cur" == "" ]] ; then |
| 423 |
COMPREPLY=( true false ) |
| 424 |
else |
| 425 |
readarray -t COMPREPLY < <(compgen -W "on yes true 1 off no false 0" -- "${cur,,}") |
| 426 |
fi |
| 427 |
fi |
| 428 |
;; |
| 429 |
monitor) |
| 430 |
case "$prev" in |
| 431 |
--properties) |
| 432 |
_bluealsa_cli_properties |
| 433 |
;; |
| 434 |
-p) |
| 435 |
return |
| 436 |
;; |
| 437 |
esac |
| 438 |
;; |
| 439 |
esac |
| 440 |
} |
| 441 |
|
| 442 |
# completion function for bluealsa-rfcomm |
| 443 |
# complete available dbus suffices and device paths in addition to options |
| 444 |
# requires busctl (part of elogind or systemd) to list device paths |
| 445 |
_bluealsa_rfcomm() { |
| 446 |
|
| 447 |
local cur prev words cword split |
| 448 |
_init_completion -s || return |
| 449 |
|
| 450 |
local bautil_args service |
| 451 |
local -i nonopt_offset |
| 452 |
_bluealsa_util_init "$1" || return |
| 453 |
|
| 454 |
local list |
| 455 |
|
| 456 |
# check for dbus service suffix first |
| 457 |
case "$prev" in |
| 458 |
--dbus|-B) |
| 459 |
_have dbus-send || return |
| 460 |
readarray -t list < <(_bluealsa_list_dbus_suffices) |
| 461 |
readarray -t COMPREPLY < <(compgen -W "${list[*]}" -- "$cur") |
| 462 |
return |
| 463 |
;; |
| 464 |
--*) |
| 465 |
[[ ${COMP_WORDS[COMP_CWORD]} = = ]] && return |
| 466 |
;; |
| 467 |
esac |
| 468 |
|
| 469 |
if (( nonopt_offset == COMP_CWORD )) ; then |
| 470 |
_have busctl || return |
| 471 |
readarray -t list < <(_bluealsa_rfcomm_paths "$service") |
| 472 |
readarray -t COMPREPLY < <(compgen -W "${list[*]}" -- "$cur") |
| 473 |
return |
| 474 |
fi |
| 475 |
|
| 476 |
# do not list options if path was found |
| 477 |
(( nonopt_offset > 0 )) && return |
| 478 |
|
| 479 |
_bluealsa_complete_options "$1" |
| 480 |
} |
| 481 |
|
| 482 |
# completion function for a2dpconf and hcitop |
| 483 |
# complete only the options |
| 484 |
_bluealsa_others() { |
| 485 |
# shellcheck disable=SC2034 |
| 486 |
local cur prev words cword split |
| 487 |
_init_completion -s || return |
| 488 |
case "$prev" in |
| 489 |
--*) |
| 490 |
[[ ${COMP_WORDS[COMP_CWORD]} = = ]] && return |
| 491 |
;; |
| 492 |
esac |
| 493 |
_bluealsa_complete_options "$1" |
| 494 |
} |
| 495 |
|
| 496 |
complete -F _bluealsa bluealsa |
| 497 |
complete -F _bluealsa_aplay bluealsa-aplay |
| 498 |
complete -F _bluealsa_cli bluealsa-cli |
| 499 |
complete -F _bluealsa_rfcomm bluealsa-rfcomm |
| 500 |
complete -F _bluealsa_others hcitop |
| 501 |
complete -F _bluealsa_others a2dpconf |