summaryrefslogtreecommitdiff
path: root/.local/bin/pa_control
blob: 1109064853d00ba08beaaa953650bd34614ad48b (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

# This script is just used to get the current PA sink,
# and to change to the 'next' sink. Using only 'pactl'.


get_default_sink (){
	echo "$(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/\ *$//')"

	default_sink="$(get_default_sink)"

	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
			echo "$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)"

	default_sink="$(get_default_sink)"
	default_sink_nickname="$(get_default_sink_nickname)"

	# 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
		# For some reason, when expanding, the first entry of 'sink_list' contains white spaces...
		line="$(echo $line | sed -e 's/\ //')"

		if [ "$found_default_sink" = "1" ]; then
			pactl set-default-sink $line
			setted_after_finding_default_sink="1"
			break;
		fi

		if [ "$default_sink" = "$line" ]; then
			found_default_sink="1"
		fi

		# 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
	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)
		notify-send -u low "Current sink" "$(get_default_sink_nickname)"
		;;
	next-sink)
		set_next_sink
		notify-send -u low "Changed sink" "$(get_default_sink_nickname)"
		;;
esac