alsasound.initd 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/openrc-run
  2. # Copyright 1999-2019 Gentoo Authors
  3. # Distributed under the terms of the GNU General Public License v2
  4. alsastatedir=/var/lib/alsa
  5. alsascrdir=/etc/alsa.d
  6. alsahomedir=/run/alsasound
  7. extra_commands="save restore"
  8. depend() {
  9. need localmount
  10. after bootmisc modules isapnp coldplug hotplug
  11. }
  12. restore() {
  13. ebegin "Restoring Mixer Levels"
  14. checkpath -q -d -m 0700 -o root:root ${alsahomedir} || return 1
  15. if [ ! -r "${alsastatedir}/asound.state" ] ; then
  16. ewarn "No mixer config in ${alsastatedir}/asound.state, you have to unmute your card!"
  17. eend 0
  18. return 0
  19. fi
  20. local cards="$(sed -n -e 's/^ *\([[:digit:]]*\) .*/\1/p' /proc/asound/cards)"
  21. local CARDNUM
  22. for cardnum in ${cards}; do
  23. [ -e /dev/snd/controlC${cardnum} ] || sleep 2
  24. [ -e /dev/snd/controlC${cardnum} ] || sleep 2
  25. [ -e /dev/snd/controlC${cardnum} ] || sleep 2
  26. [ -e /dev/snd/controlC${cardnum} ] || sleep 2
  27. alsactl -E HOME="${alsahomedir}" -I -f "${alsastatedir}/asound.state" restore ${cardnum} \
  28. || ewarn "Errors while restoring defaults, ignoring"
  29. done
  30. for ossfile in "${alsastatedir}"/oss/card*_pcm* ; do
  31. [ -e "${ossfile}" ] || continue
  32. # We use cat because I'm not sure if cp works properly on /proc
  33. local procfile=${ossfile##${alsastatedir}/oss}
  34. procfile="$(echo "${procfile}" | sed -e 's,_,/,g')"
  35. if [ -e /proc/asound/"${procfile}"/oss ] ; then
  36. cat "${ossfile}" > /proc/asound/"${procfile}"/oss
  37. fi
  38. done
  39. eend 0
  40. }
  41. save() {
  42. ebegin "Storing ALSA Mixer Levels"
  43. checkpath -q -d -m 0700 -o root:root ${alsahomedir} || return 1
  44. mkdir -p "${alsastatedir}"
  45. if ! alsactl -E HOME="${alsahomedir}" -f "${alsastatedir}/asound.state" store; then
  46. eerror "Error saving levels."
  47. eend 1
  48. return 1
  49. fi
  50. for ossfile in /proc/asound/card*/pcm*/oss; do
  51. [ -e "${ossfile}" ] || continue
  52. local device=${ossfile##/proc/asound/} ; device=${device%%/oss}
  53. device="$(echo "${device}" | sed -e 's,/,_,g')"
  54. mkdir -p "${alsastatedir}/oss/"
  55. cp "${ossfile}" "${alsastatedir}/oss/${device}"
  56. done
  57. eend 0
  58. }
  59. start() {
  60. if [ "${RESTORE_ON_START}" = "yes" ]; then
  61. restore
  62. fi
  63. return 0
  64. }
  65. stop() {
  66. if [ "${SAVE_ON_STOP}" = "yes" ]; then
  67. save
  68. fi
  69. return 0
  70. }