-
Notifications
You must be signed in to change notification settings - Fork 0
/
volume-control
executable file
·130 lines (112 loc) · 2.79 KB
/
volume-control
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
#!/usr/bin/env bash
# ./volume-control
#
# see --help for usage
#
# example:
# ./script --card 1 --toggle
# ./script -t
# ./script -i 5
# ./script -d 5
#
# depends: amixer
# optional depends: dzen2, dbar (for --dzen)
# author: @qbbr
# for --dzen only
readonly DZEN_FONT="hack-10"
readonly DZEN_BG="#b58900"
readonly DZEN_FG="#fdf6e3"
readonly DZEN_WIDTH=272
usage() {
echo "Usage: ${0##*/} [option] [argument]"
echo
echo "Options:"
echo " -c, --card - set card, optional [default=1]"
echo " -t, --toggle - toggle mute on/off"
echo " -i, --increment - increment by argument in percent"
echo " -d, --decrement - decrement by argument in percent"
echo " -m, --max - set volume to max"
echo " --dzen - show dbar in dzen2"
echo " -h, --help - display this help and exit"
exit 0
}
fail() {
echo "$1"
exit 1
}
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case ${key} in
-c|--card)
CARD_ARG="-c $2"
shift # past argument
shift # past value
;;
-t|--toggle)
CMD="toggle"
shift # past argument
;;
-i|--increment)
CMD="${2}%+"
shift
shift
;;
-d|--decrement)
CMD="${2}%-"
shift
shift
;;
-m|--max)
CMD="100%"
shift
;;
--dzen)
SHOW_DZEN2=1
shift
;;
-h|--help)
usage
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameter
if [[ -z "${CMD}" ]] || [[ "${CMD}" == "%+" ]] || [[ "${CMD}" == "%-" ]]; then
fail "nothing to do, see --help"
fi
SCONTROL="Master"
SCONTROL_HEADPHONE="Headphone"
call_amixer() {
amixer ${CARD_ARG} -D pulse $@
}
# use Headphone if it is on and Master is switch off
[[ "${CMD}" != "toggle" ]] \
&& [[ -n "$(echo $(call_amixer get ${SCONTROL}) | grep '\[off\]')" ]] \
&& [[ -n "$(echo $(call_amixer get ${SCONTROL_HEADPHONE}) | grep '\[on\]')" ]] \
&& SCONTROL="${SCONTROL_HEADPHONE}"
output="$(echo $(call_amixer set ${SCONTROL} ${CMD}) | tail -n 1)"
if [[ -z "${SHOW_DZEN2}" ]]; then
exit 0
fi
PIPE_FILE="/tmp/pipe-${0##*/}"
# calculate center
SCREEN_SIZE=$(xdpyinfo | grep dimensions | cut -d ' ' -f 7)
SCREEN_WIDTH=$(echo ${SCREEN_SIZE} | cut -d 'x' -f 1)
SCREEN_HEIGHT=$(echo ${SCREEN_SIZE} | cut -d 'x' -f 2)
((XPOS = SCREEN_WIDTH / 2 - DZEN_WIDTH / 2))
((YPOS = SCREEN_HEIGHT / 2))
if [[ -n "$(echo ${output} | grep '\[off\]')" ]]; then
vol="0"
else
vol="$(echo ${output} | cut -d '[' -f 2 | tr -d '[%]]')"
fi
# prevents multiple dzen instance
if [[ ! -e "${PIPE_FILE}" ]]; then
mkfifo "${PIPE_FILE}"
(dzen2 -l 1 -x "${XPOS}" -y "${YPOS}" -w "${DZEN_WIDTH}" -fn "${DZEN_FONT}" -bg "${DZEN_BG}" -fg "${DZEN_FG}" -e 'onstart=uncollapse' < "${PIPE_FILE}"; rm -f "${PIPE_FILE}") &
fi
(echo "Volume (${SCONTROL})" ; echo "${vol}" | dbar ; sleep 1) > "${PIPE_FILE}"