aboutsummaryrefslogtreecommitdiff
path: root/.config/waybar/scripts/volumecontrol.sh
blob: 3359046d3a4fc9aac4ba21763ec859881230689f (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/sh 

# You can call this script like this:
# volumecontrol up
# volumecontrol down
# volumecontrol mute

get_volume() {
    # pactl get-sink-volume @DEFAULT_SINK@ | grep '%' | cut -d '%' -f 1 | cut -d ' ' -f 6
    echo "scale=1; $(mixer vol | grep 'vol.volume=' | cut -d '=' -f 2 | cut -d ':' -f 1)*100" | bc | cut -d '.' -f 1
}

is_mute() {
    # pactl get-sink-mute @DEFAULT_SINK@ | grep 'Mute: yes' >> /dev/null
    mixer vol | grep 'vol.mute=on' >> /dev/null
}

send_notification() {
	volume=$(get_volume)
    # Make the bar with the special character ─ (it's not dash -)
    # https://en.wikipedia.org/wiki/Box-drawing_character
	if [ "$volume" = "100" ]; then
		icon=""
	elif [ "$volume" -ge "89" ] && [ "$volume" -le "100" ]; then
		icon=""
	elif [ "$volume" -ge "79" ] && [ "$volume" -le "90" ]; then
		icon=""
	elif [ "$volume" -ge "69" ] && [ "$volume" -le "80" ]; then
		icon=""
	elif [ "$volume" -ge "59" ] && [ "$volume" -le "70" ]; then
		icon=""
	elif [ "$volume" -ge "49" ] && [ "$volume" -le "60" ]; then
		icon=""
	elif [ "$volume" -ge "39" ] && [ "$volume" -le "50" ]; then
		icon=""
	elif [ "$volume" -ge "29" ] && [ "$volume" -le "40" ]; then
		icon=""
	elif [ "$volume" -ge "19" ] && [ "$volume" -le "30" ]; then
		icon=""
	elif [ "$volume" -ge "9" ] && [ "$volume" -le "20" ]; then
		icon=""
	elif [ "$volume" -gt "0" ] && [ "$volume" -le "10" ]; then
		icon=""
	elif [ "$volume" -eq "0" ]; then
		icon=""
		class="muted"
	fi
bar=$(seq -s "─" $(($volume/5)) | sed 's/[0-9]//g')
# Send the notification
fyi --hint=string:x-canonical-private-synchronous:volumecontrol -u normal "$icon  $bar  $volume"
}

case $1 in
    up)
	# pactl set-sink-mute @DEFAULT_SINK@ 0
	mixer vol.mute=off
	# Up the volume (+ 2%)
	# pactl set-sink-volume @DEFAULT_SINK@ +2%
	mixer vol.volume=+2%
	mixer vol > ~/.cache/audio-volume
	send_notification
	# canberra-gtk-play -i audio-volume-change
	;;
    down)
	# pactl set-sink-mute @DEFAULT_SINK@ 0
	mixer vol.mute=off
	# pactl set-sink-volume @DEFAULT_SINK@ -2%
	mixer vol.volume=-2%
	mixer vol > ~/.cache/audio-volume
	send_notification
	# canberra-gtk-play -i audio-volume-change
	;;
    mute)
    	# Toggle mute
	# pactl set-sink-mute @DEFAULT_SINK@ toggle
	mixer vol.mute=toggle
	mixer vol > ~/.cache/audio-volume
	if is_mute ; then
    icon="󰝟"
    class="muted"
    text="<span color='#202020' bgcolor='#ff00aa' > $icon </span> $volume"
    fyi --hint=string:x-canonical-private-synchronous:volumecontrol -u normal "$icon  Audio Muted"
	else
	   send_notification
	   # canberra-gtk-play -i audio-volume-change
	fi
	;;
    *)
	volume="$(get_volume)"

	if [ "$volume" = "100" ]; then
		icon=""
	elif [ "$volume" -ge "89" ] && [ "$volume" -le "100" ]; then
		icon=""
	elif [ "$volume" -ge "79" ] && [ "$volume" -le "90" ]; then
		icon=""
	elif [ "$volume" -ge "69" ] && [ "$volume" -le "80" ]; then
		icon=""
	elif [ "$volume" -ge "59" ] && [ "$volume" -le "70" ]; then
		icon=""
	elif [ "$volume" -ge "49" ] && [ "$volume" -le "60" ]; then
		icon=""
	elif [ "$volume" -ge "39" ] && [ "$volume" -le "50" ]; then
		icon=""
	elif [ "$volume" -ge "29" ] && [ "$volume" -le "40" ]; then
		icon=""
	elif [ "$volume" -ge "19" ] && [ "$volume" -le "30" ]; then
		icon=""
	elif [ "$volume" -ge "9" ] && [ "$volume" -le "20" ]; then
		icon=""
	elif [ "$volume" -gt "0" ] && [ "$volume" -le "10" ]; then
		icon=""
	elif [ "$volume" -eq "0" ]; then
		icon="󰝟"
    class="muted"
	fi


if is_mute || [ "$class" = 'muted' ]; then
		icon="󰝟"
    class="muted"
    text="<span color='#202020' bgcolor='#cc241d' > $icon  </span> $volume"
  else
    text="<span color='#202020' bgcolor='#d65d0e' > $icon  </span> $volume"
fi

alt=$icon
echo "{\"text\": \"$text"\", \"alt\": \"$alt"\", \"tooltip\":\"$tooltip\", \"class\": \"$class\", \"percentage\":"$volume"}"
  ;;
esac