aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/vol
blob: 89936897525bfc8b44e2e57c887ed37712a96964 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/sh

# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.

main()
{
	# Handle opts
	while getopts ":h" opt
	do
		case "$opt" in
			h) usage && exit 0 ;;
			*)
				printf "Invalid option passed: %s\n" "$OPTARG" >&2
				;;
		esac
	done

	shift $(( OPTIND - 1 ))

	[ $# -lt 1 ] && usage && exit 1

	DEFAULT_SINK_NAME="$(pactl info | awk -F': ' '/Default Sink/ { print $NF }')"
	DEFAULT_SINK_INDEX="$(pactl list short sinks | awk -v index_number="$DEFAULT_SINK_NAME" '$2==index_number {print $1}')"

	case "$1" in
		inc)
			pactl set-sink-volume @DEFAULT_SINK@ +${2:-5}% > /dev/null
			notify="Increased '$DEFAULT_SINK_NAME' ($DEFAULT_SINK_INDEX) to $(volume_level)%"
			;;
		dec)
			pactl set-sink-volume @DEFAULT_SINK@ -${2:-5}% > /dev/null
			notify="Decreased '$DEFAULT_SINK_NAME' ($DEFAULT_SINK_INDEX) to $(volume_level)%"
			;;
		set)
			pactl set-sink-volume @DEFAULT_SINK@ $2% > /dev/null
			notify="Set '$DEFAULT_SINK_NAME' ($DEFAULT_SINK_INDEX) to $(volume_level)%"
			;;
		on)
			pactl set-sink-mute @DEFAULT_SINK@ 0 > /dev/null
			notify="Unmuted '$DEFAULT_SINK_NAME' ($DEFAULT_SINK_INDEX) at $(volume_level)%"
			;;
		off)
			pactl set-sink-mute @DEFAULT_SINK@ 1 > /dev/null
			notify="Muted '$DEFAULT_SINK_NAME' ($DEFAULT_SINK_INDEX)"
			;;
		toggle)
			case "$(volume_state)" in
				on) exec $0 off ;;
				off) exec $0 on ;;
			esac
			;;
	esac

	printf "%s\n" "$notify"
	notify-send -n 19269 -s -t 2 -i "$(volume_icon)" "Volume" "$notify"
}

volume_level()
{

	pactl list sinks \
		| perl -000ne 'if(/#'"$DEFAULT_SINK_INDEX"'/){/(Volume:.*)/; print "$1\n"}' \
		| sed 's/.*\ \(.*\)%.*/\1/g'
}

volume_state()
{
	pactl list sinks \
		| perl -000ne 'if(/#'"$DEFAULT_SINK_INDEX"'/){/(Mute:.*)/; print "$1\n"}' \
		| awk '$NF == "no" { print "on" } $NF == "yes" { print "off" }'
}

volume_icon()
{
	[ "$(volume_state)" = "off" ] && printf "audio-volume-muted-panel" && return
	[ $(volume_level) -lt  1 ] && printf "audio-volume-low-zero-panel" && return
	[ $(volume_level) -lt 30 ] && printf "audio-volume-low-panel" && return
	[ $(volume_level) -lt 70 ] && printf "audio-volume-medium-panel" && return

	printf "audio-volume-high-panel"
}

usage()
{
	cat <<EOF
Usage:
	${0##*/} -h
	${0##*/} inc [level]
	${0##*/} dec [level]
	${0##*/} set <level>
	${0##*/} on
	${0##*/} off
	${0##*/} toggle

Alter the current volume level or state, and add in a notification.

Subcommands:
	inc     Increase the volume by level, defaults to 5.
	dec     Decrease the volume by level, defaults to 5.
	set     Set the volume to level.
	on      Unmute.
	off     Mute.
	toggle  Toggle between muted and unmuted state.

Options:
	-h  Show this help text and exit.
EOF
}

main "$@"