File: init

package info (click to toggle)
alsa-utils 1.0.28-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,280 kB
  • ctags: 1,585
  • sloc: ansic: 21,693; sh: 6,035; xml: 590; makefile: 374; sed: 16
file content (209 lines) | stat: -rwxr-xr-x 5,340 bytes parent folder | download
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/bin/sh
#
# alsa-utils initscript
#
### BEGIN INIT INFO
# Provides:          alsa-utils
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $remote_fs
# Default-Start:     S
# Default-Stop:      0 1 6
# Short-Description: Restore and store ALSA driver settings
# Description:       This script stores and restores mixer levels on
#                    shutdown and bootup.On sysv-rc systems: to
#                    disable storing of mixer levels on shutdown,
#                    remove /etc/rc[06].d/K50alsa-utils.  To disable
#                    restoring of mixer levels on bootup, rename the
#                    "S50alsa-utils" symbolic link in /etc/rcS.d/ to
#                    "K50alsa-utils".
### END INIT INFO

# Don't use set -e; check exit status instead

# Exit silently if package is no longer installed
[ -x /usr/sbin/alsactl ] || exit 0

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MYNAME=/etc/init.d/alsa-utils
ALSACTLHOME=/var/run/alsa

[ -d "$ALSACTLHOME" ] || mkdir -p "$ALSACTLHOME"

. /lib/lsb/init-functions
. /usr/share/alsa/utils.sh

# $1 EXITSTATUS
# [$2 MESSAGE]
log_action_end_msg_and_exit()
{
	log_action_end_msg "$1" ${2:+"$2"}
	exit $1
}

# $1 PROGRAM
executable()
{
	# If which is not available then we must be running before
	# /usr is mounted on a system that has which in /usr/bin/.
	# Conclude that $1 is not executable.
	[ -x /bin/which ] || [ -x /usr/bin/which ] || return 1
	which "$1" >/dev/null 2>&1
}

executable amixer || { echo "${MYNAME}: Error: No amixer program available." >&2 ; exit 1 ; }

# $1 <card ID> | "all"
restore_levels()
{
	[ -f /var/lib/alsa/asound.state ] || return 1
	CARD="$1"
	[ "$1" = all ] && CARD=""
	# Assume that if alsactl prints a message on stderr
	# then it failed somehow.  This works around the fact
	# that alsactl doesn't return nonzero status when it
	# can't restore settings for the card
	if MSG="$(alsactl -E HOME="$ALSACTLHOME" restore $CARD 2>&1 >/dev/null)" && [ ! "$MSG" ] ; then
		return 0
	else
		# Retry with the "force" option.  This restores more levels
		# but it results in much longer error messages.
		alsactl -F restore $CARD >/dev/null 2>&1
		log_action_cont_msg "warning: 'alsactl -E HOME="$ALSACTLHOME" restore${CARD:+ $CARD}' failed with error message '$MSG'"
		return 1
	fi
}

# $1 <card ID> | "all"
store_levels()
{
	CARD="$1"
	[ "$1" = all ] && CARD=""
	if MSG="$(alsactl -E HOME="$ALSACTLHOME" store $CARD 2>&1)" ; then
		sleep 1
		return 0
	else
		log_action_cont_msg "warning: 'alsactl store${CARD:+ $CARD}' failed with error message '$MSG'"
		return 1
	fi
}


# $1 <card ID>
mute_and_zero_levels_on_card()
{
	CARDOPT="-c $1"
	for CTL in \
		Master \
		PCM \
		Synth \
		CD \
		Line \
		Mic \
		"PCM,1" \
		Wave \
		Music \
		AC97 \
		"Master Digital" \
		DAC \
		"DAC,0" \
		"DAC,1" \
		Headphone \
		Speaker \
		Playback
	do
		mute_and_zero_level "$CTL"
	done
#	for CTL in \
#		"Audigy Analog/Digital Output Jack" \
#		"SB Live Analog/Digital Output Jack"
#	do
#		switch_control "$CTL" off
#	done
	return 0
}

# $1 <card ID> | "all"
mute_and_zero_levels()
{
	TTZML_RETURNSTATUS=0
	case "$1" in
	  all)
		for CARD in $(echo_card_indices) ; do
			mute_and_zero_levels_on_card "$CARD" || TTZML_RETURNSTATUS=1
		done
		;;
	  *)
		mute_and_zero_levels_on_card "$1" || TTZML_RETURNSTATUS=1
		;;
	esac
	return $TTZML_RETURNSTATUS
}


# $1 <card ID> | "all"
card_OK()
{
	[ "$1" ] || bugout
	if [ "$1" = all ] ; then
		[ -d /proc/asound ]
		return $?
	else
		[ -d "/proc/asound/card$1" ] || [ -d "/proc/asound/$1" ]
		return $?
	fi
}

# If a card identifier is provided in $2 then regard it as an error
# if that card is not present; otherwise don't regard it as an error.

case "$1" in
  start)
	EXITSTATUS=0
	TARGET_CARD="$2"
	case "$TARGET_CARD" in
	  ""|all) TARGET_CARD=all ; log_action_begin_msg "Setting up ALSA" ;;
	  *) log_action_begin_msg "Setting up ALSA card ${TARGET_CARD}" ;;
	esac
	card_OK "$TARGET_CARD" || log_action_end_msg_and_exit "$( [ ! "$2" ] ; echo $? ; )" "none loaded"
	preinit_levels "$TARGET_CARD" || EXITSTATUS=1
	if ! restore_levels "$TARGET_CARD" ; then
		sanify_levels "$TARGET_CARD" || EXITSTATUS=1
		restore_levels "$TARGET_CARD" >/dev/null 2>&1 || :
	fi
	log_action_end_msg_and_exit "$EXITSTATUS"
	;;
  stop)
	EXITSTATUS=0
	TARGET_CARD="$2"
	case "$TARGET_CARD" in
	  ""|all) TARGET_CARD=all ; log_action_begin_msg "Shutting down ALSA" ;;
	  *) log_action_begin_msg "Shutting down ALSA card ${TARGET_CARD}" ;;
	esac
	card_OK "$TARGET_CARD" || log_action_end_msg_and_exit "$( [ ! "$2" ] ; echo $? ; )" "none loaded"
	store_levels "$TARGET_CARD" || EXITSTATUS=1
	#mute_and_zero_levels "$TARGET_CARD" || EXITSTATUS=1
	log_action_end_msg_and_exit "$EXITSTATUS"
	;;
  restart|force-reload)
	EXITSTATUS=0
	$0 stop || EXITSTATUS=1
	$0 start || EXITSTATUS=1
	exit $EXITSTATUS
	;;
  reset)
	TARGET_CARD="$2"
	case "$TARGET_CARD" in
	  ""|all) TARGET_CARD=all ; log_action_begin_msg "Resetting ALSA" ;;
	  *) log_action_begin_msg "Resetting ALSA card ${TARGET_CARD}" ;;
	esac
	card_OK "$TARGET_CARD" || log_action_end_msg_and_exit "$( [ ! "$2" ] ; echo $? ; )" "none loaded"
	preinit_levels "$TARGET_CARD"
	sanify_levels "$TARGET_CARD"
	log_action_end_msg_and_exit "$?"
	;;
  *)
	echo "Usage: $MYNAME {start [CARD]|stop [CARD]|restart [CARD]|reset [CARD]}" >&2
	exit 3
	;;
esac