summaryrefslogblamecommitdiff
path: root/.local/bin/audio_cyclesink
blob: 3726f70d609e60117b59c0164d2e6668b8596f4a (plain) (tree)

























































                                                       
                                                  

                          




                                                 




                                                    




                                            
 

                                              


                                       
#!/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="${sink##* }"
			;;
		*)
			[ -z "$default_sink" ] &&
				continue

			next_sink="${sink##* }"
			break
			;;
		esac
	done

	# if default sink is last one use first sink
	if [ -z "$next_sink" ]
	then
		next_sink="${sinks%%:*}"
		next_sink="${next_sink##* }"
	fi

	[ "$next_sink" != "$default_sink" ] &&
		wpctl set-default "$next_sink"
}

wpctl status | parse_sinks | cycle_sink