summaryrefslogtreecommitdiff
path: root/.local/bin/audio_cyclesink
blob: 52b266192f8e94c3f914a10da85201eabd257f1f (plain) (blame)
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
#!/bin/sh

# cycle between wireplumber(pipewire) audio sinks

parse_sinks()
{
	isaudio=
	isadsink=

	while read -r line
	do
		case "$line" in
		Audio)
			isaudio=true
			;;
		├─\ Sinks:)

			[ "$isaudio" = true ] &&
				isadsink=true
			;;
		├─*:)
			[ "$isadsink" = true ] &&
				break
			;;
		*)
			# skip if not inside audio sink
			[ "$isadsink" != true ] &&
				continue

			# skip "blank" lines
			[ -z "${line#│}" ] &&
				continue

			# clean up input
			line="${line#│  }"
			line="${line%%.*}"

			sinks="${sinks}${line}:"

			;;
		esac
	done

	printf "%s" "${sinks%:}"
	unset isaudio isadsink
}

cycle_sink()
{
	read -r sinks
	default_sink=
	next_sink=

	IFS=:
	for sink in $sinks
	do
		case "$sink" in
		\**)
			default_sink="${sinks#*   }"
			;;
		*)
			if [ -n "$default_sink" ]
			then
				next_sink="${sink#    }"
				break
			fi
			;;
		esac
	done

	# if default sink is last one use first sink
	: "${next_sink:="${sinks%%:*}"}"

	wpctl set-default "$next_sink"
}

wpctl status | parse_sinks | cycle_sink