summaryrefslogtreecommitdiff
path: root/.local/bin/set-audio-sink
blob: 20ade7ac4b7e53cd2aa06f4a98f1d367253a5da5 (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
#!/usr/bin/env bash
# Copyright 2020  Luke Shumaker

errusage() {
	if (( $# > 0 )); then
		printf '%s: %s\n' "${0##*/}" "$(printf "$@")" >&2
	fi
	printf "Try '%s --help' for more information.\n" "${0##*/}" >&2
	exit 2
}

usage() {
	printf 'Usage: %s [OPTIONS] PULSE_SINK\n' "${0##*/}"
	printf "Adjust default audio sink, and move currently running programs\n"
	printf "to that sink.\n"
	echo
	printf 'OPTIONS:\n'
	printf '  -h, --help      Show this message\n'
	printf '  -n, --dry-run   Print what would be done, without doing it\n'
}

set -euE -o pipefail
args=$(getopt -n "${0##*/}" -o 'hn' -l 'help,dry-run' -- "$@") || errusage
eval "set -- $args"

arg_dry_run=false
while (( $# > 0 )); do
	case "$1" in
		-h|--help)
			usage
			exit 0
			;;
		-n|--dry-run)
			arg_dry_run=true
			shift
			;;
		--)
			shift
			break
			;;
	esac
done
if (( $# != 1 )); then
	errusage
fi

(
	
	new_sink=$1
	printf 'pactl set-default-sink %q\n' "$new_sink"
	while IFS=$'\t' read -r sink_input current_sink client driver sample_spec; do
		printf 'pactl move-sink-input %q %q\n' "$sink_input" "$new_sink"
	done < <(pactl list short sink-inputs)
) | (
	if $arg_dry_run; then
		cat
	else
		bash -v
	fi
)