summaryrefslogtreecommitdiff
path: root/scripts/svol
blob: 79251a228a7092f811c34f47f835813ce4603c76 (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
#!/bin/sh

help() {
cat << EOF
${0}: Wrapper script to change volume

options:
 -i [Vol]	Increase volume by Vol
 -d [Vol]	Decrease volume by Vol
 -s [Vol] 	Set volume to Vol
 -p		Show the current volume
 -t		Toggle between mute and unmute
 -h 		Print this message and exit
EOF
}

err() {
	for line in "${@}"; do
		echo "${line}" >&2
	done
	exit 1
}

run() {
	if ! ${1} > /dev/null 2>&1; then
		err "${2}"
	fi

	[ -n "${3}" ] && echo "${3}"
	exit 0
}

if ! command -v pactl > /dev/null 2>&1; then
	err "${0}: pulseaudio must be installed"
fi

if [ $# != 1 ] && [ $# != 2 ]; then
	err "${0}: Invalid usage" "Try '$0 -h' for help."
fi

while getopts "i:d:s:pth" option; do
	case "${option}" in
	i)
		run "pactl set-sink-volume @DEFAULT_SINK@ +${OPTARG}%" \
		"${0}: Failed to increase volume"
	;;
	d)
		run "pactl set-sink-volume @DEFAULT_SINK@ -${OPTARG}%" \
		"${0}: Failed to decrease volume"
	;;
	s)
		run "pactl set-sink-volume @DEFAULT_SINK@ ${OPTARG}%" \
		"${0}: Failed to set volume"
	;;
	t)
		run "pactl set-sink-mute @DEFAULT_SINK@ toggle" \
		"${0}: Failed to toggle volume"
	;;
	p)
		if ! pactl get-sink-volume @DEFAULT_SINK@ 2>/dev/null; then
			err "Failed to get current volume"
		else
			exit 0
		fi
	;;
	h) help; exit 0 ;;

	*) err "Try '${0} -h' for help" ;;

	esac
done

err "${0}: Invalid usage" "Try '$0 -h' for help."