summaryrefslogtreecommitdiff
path: root/.local/bin/pa_control
blob: d209374a8da9908921dc68ce442e5a50e2a5c5e4 (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
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
#!/bin/sh

default_sink=
default_sink_nickname=

get_default_sink (){
	default_sink="$(pactl info | grep Sink | awk '{print $3}')"
}

get_default_sink_nickname (){
	sink_list="$(pactl list sinks\
		| grep -e Name -e alsa.card_name\
		| sed -e 's/^\ *//' -e 's/\ *$//')"

	found_default_sink="0"

	while IFS= read line; do
		# Trim line depending on content.
		if [ "$(echo $line | cut -d' ' -f1)" = "Name:" ]; then
			line="$(echo $line | cut -d' ' -f2)"
		else
			line="$(echo $line | cut -d' ' -f3- | sed 's/\"//g')"
		fi


		# If fond default_sink, get and set the nickname, then exit.
		if [ $found_default_sink = "1" ]; then
			default_sink_nickname="$line"
			break;
		fi

		# If current line is the default sink, set a flag.
		if [ $default_sink = "$line" ]; then
			found_default_sink="1"
		fi
	done <<EOF
	$sink_list
EOF
# The above line need not to have a space before EOF.
}

# Similar to get_default_sink_nickname, just shorter.
set_next_sink (){
	sink_list="$(pactl list sinks\
		| grep -e Name\
		| sed -e 's/^\ *//' -e 's/\ *$//'\
		| cut -d' ' -f2)"

	# Some flags because I don't know a better way to do this, lol.
	found_default_sink="0"
	setted_after_finding_default_sink="0"
	first_found_sink="0"

	while IFS= read line; do
		if [ $found_default_sink = "1" ]; then
			pactl set-default-sink $line
			setted_after_finding_default_sink="1"
		fi

		if [ $default_sink = $line ]; then
			found_default_sink="1"
		else
			# In case the first line is not he default sink,
			# store the first found sink in case the default sink,
			# is at the end of the variable (in which case the while loop
			# exits and we no longer know which one is next [next would be the first
			# line ;) ])
			if [ $first_found_sink = "0" ]; then
				first_found_sink="$line"
			fi
		fi
	done <<EOF
	$sink_list
EOF
	# If the next sink wasn't set after finding the default sink, it means
	# the next sink is actually the first sink, so, set it here
	if [ $setted_after_finding_default_sink = "0" ]; then
		pactl set-default-sink $first_found_sink
	fi
}

case "$1" in
	get-sink)
		get_default_sink
		get_default_sink_nickname

		notify-send "Current sink" "$default_sink_nickname"
		;;
	next-sink)
		get_default_sink
		get_default_sink_nickname

		set_next_sink

		get_default_sink
		get_default_sink_nickname

		notify-send "Changed to sink" "$(echo $default_sink_nickname)"
		;;
esac