[Pm-utils] code available to solve sound-after-suspend problems

Mark Stosberg mark at summersault.com
Wed Mar 14 20:33:38 PDT 2007


Olivier Blin wrote:
> Mark Stosberg <mark at summersault.com> writes:
> 
>>> But maybe just because nobody even bothered to file a bug against the kernel.
>>> It might also very well be that it is just not documented well enough. I
>>> figure that you would not google for "pm-utils custom hook" if you had a
>>> problem with sound after suspend. I will try to get this better documented
>>> (and google-indexed :-), but this is where you can also help.
>> I assume you mean that this would be helpful if someone had published a
>> pm-utils hook script with this workaround, which no one was yet
>> (although it is trivial to do given the code Mandriva has already
>> published for this).
> 
> Since some of our users require such workarounds, I'll port our sound
> script to pm-utils (as I did with other scripts).
> 
> If you want to have a look at our scripts, they are available here:
> http://svn.mandriva.com/cgi-bin/viewvc.cgi/packages/cooker/pm-utils/current/SOURCES/
> 
> We will try to push our relevant changes upstream, but we are a bit in
> a rush right now...

Olivier,

Based on those examples, attached is what I believe to be working
translation of the Mandriva sound script to pm-utils. It includes the
fix for Exaile and other script-based music players it reported earlier.

However, I decided to remove the explicit support to save and recover an
xmms playlist (It's so old...) but that I could easily be added back
from the original Mandriva scripts.

Mandriva and the pm-utils project are welcome to consider any
contributions I made in the process under the GPL.

  Mark
-------------- next part --------------
#!/bin/bash

# RESTORE_SOUND=yes

# Forcefully kill and restart applications using the sound card.
# This possibility is available for sound drivers that don't properly handle
# suspending. 

. /usr/share/pm-utils/functions

SOUND_RUN=/var/run/pm-soundprogs

stop_sound() {
    rm -rf $SOUND_RUN
    if [ -c /dev/sndstat ] ; then
        for n in `cat /dev/sndstat 2>/dev/null|grep Open\ by|awk {'print $3'}|uniq`; do
            SOUNDPID=`echo $n|awk -F / {'print $1'}`
            SOUNDPROG=`ps -p $SOUNDPID -o args=`
            SOUNDUSER=`ls -ld /proc/$SOUNDPID |awk {'print $3'}`
            SOUNDDISPLAY=`perl -e 's/\000/\n/g;' -p /proc/$SOUNDPID/environ|grep ^DISPLAY=|cut -d= -f2-`

            kill $SOUNDPID
            ps "$SOUNDPID" &>/dev/null && kill -9 $SOUNDPID
            logger \
            "pm-utils: Program $SOUNDPROG ($SOUNDPID) of user $SOUNDUSER on display $SOUNDDISPLAY terminated."	    
        done
    fi

    for n in `lsof -n|grep /dev|egrep ' (14|116),'|sed -e 's/ \+/|/g'|cut '-d|' -f1-3`; do
        SOUNDPID=`echo $n |cut '-d|' -f2`
        SOUNDPROG=`ps -p $SOUNDPID -o args=`
        SOUNDUSER=`echo $n |cut '-d|' -f3`
        if [[ -f $SOUND_RUN ]] && grep -q "$SOUNDPROG.*$SOUNDUSER.*" $SOUND_RUN;then
            continue;
        fi
        SOUNDDISPLAY=`perl -e 's/\000/\n/g;' -p /proc/$SOUNDPID/environ|grep ^DISPLAY=|cut -d= -f2-`

        kill $SOUNDPID
        ps "$SOUNDPID" &>/dev/null && kill -9 $SOUNDPID

        echo "$SOUNDPROG|$SOUNDPID|$SOUNDUSER|$SOUNDDISPLAY" >>$SOUND_RUN
        logger \
        "ACPI: Program $SOUNDPROG ($SOUNDPID) of user $SOUNDUSER on display $SOUNDDISPLAY terminated."

    done
    # We unload the modules later to give the driver some time
    # to spin down.

}

start_sound() {
    if [[ -f $SOUND_RUN ]] ; then
        # at least we can restart the programs using sound on
        # the right display
        for n in `cat $SOUND_RUN`; do
            SOUNDPROG=`echo $n|cut '-d|' -f1`
            SOUNDPID=`echo $n|cut '-d|' -f2`
            SOUNDUSER=`echo $n|cut '-d|' -f3`
            SOUNDDISPLAY=`echo $n|cut '-d|' -f4`

            su --shell="/bin/bash" - "$SOUNDUSER" -c \
                "source /etc/profile;[ -f ~/.bashrc ] && \
                    source ~/.bashrc;[ -f ~/.bash_profile ] && \
                    source ~/.bash_profile;export DISPLAY="$SOUNDDISPLAY";$SOUNDPROG &"
            logger "pm-utils: Program $SOUNDPROG started as user $SOUNDUSER on display $SOUNDDISPLAY."
        done
        rm -f $SOUND_RUN 
    fi
}

case "$1" in
    hibernate|suspend)
        if [[ "$RESTORE_SOUND" == "yes" ]] ; then
            stop_sound
        fi
        ;;
    thaw|resume)
        if [[ "$RESTORE_SOUND" == "yes" ]] ; then
            start_sound
        fi
        ;;
    *)
        ;;
esac

exit $?

###


More information about the Pm-utils mailing list