Results 1 to 10 of 43

Thread: How to make your laptop play sounds on lid open/close :)

Threaded View

  1. #1
    Join Date
    Feb 2007
    Location
    perdita
    Beans
    1,622
    Distro
    Ubuntu

    How to make your laptop play sounds on lid open/close :)

    Main howto:
    Have you ever wanted your laptop to act like a Starcraft tank?
    Well, with Ubuntu it's possible and very easy to do.

    Whenever the lid of your laptop is opened or closed, Ubuntu runs the /etc/acpi/lid.sh script.
    This script determines if the lid is open or closed by looking at the contents of "/proc/acpi/button/lid/LID/state".

    So all you have to do to play opening/closing sounds is add this after the "#!/bin/bash" line:

    Code:
    grep -q closed /proc/acpi/button/lid/LID/state
    if [ $? = 0 ]
    then
            aplay /etc/acpi/closing.wav;
    else
            aplay /etc/acpi/opening.wav;
    fi
    where closing.wav and opening.wav are the sound files I use, placed in /etc/acpi.

    Here's my complete lid.sh file:
    Code:
    #!/bin/bash
    
    grep -q closed /proc/acpi/button/lid/LID/state
    if [ $? = 0 ]
    then
    	aplay /etc/acpi/closing.wav;
    else
    	aplay /etc/acpi/opening.wav;
    fi
    
    . /usr/share/acpi-support/power-funcs
    . /usr/share/acpi-support/policy-funcs
    . /etc/default/acpi-support
    
    [ -x /etc/acpi/local/lid.sh.pre ] && /etc/acpi/local/lid.sh.pre
    
    if [ `CheckPolicy` == 0 ]; then exit; fi
    
    grep -q closed /proc/acpi/button/lid/*/state
    if [ $? = 0 ]
    then
        for x in /tmp/.X11-unix/*; do
    	displaynum=`echo $x | sed s#/tmp/.X11-unix/X##`
    	getXuser;
    	if [ x"$XAUTHORITY" != x"" ]; then
    	    export DISPLAY=":$displaynum"	    
    	    . /usr/share/acpi-support/screenblank
    	fi
        done
    else
        for x in /tmp/.X11-unix/*; do
    	displaynum=`echo $x | sed s#/tmp/.X11-unix/X##`
    	getXuser;
    	if [ x"$XAUTHORITY" != x"" ]; then
    	    export DISPLAY=":$displaynum"
    	    grep -q off-line /proc/acpi/ac_adapter/*/state
    	    if [ $? = 1 ]
    		then
    		if pidof xscreensaver > /dev/null; then 
    		    su $user -c "xscreensaver-command -unthrottle"
    		fi
    	    fi
    	    if [ x$RADEON_LIGHT = xtrue ]; then
    		[ -x /usr/sbin/radeontool ] && radeontool light on
    	    fi
    	    if [ `pidof xscreensaver` ]; then
    		su $user -c "xscreensaver-command -deactivate"
    	    fi
    	    su $user -c "xset dpms force on"
    	fi
        done
    fi
    [ -x /etc/acpi/local/lid.sh.post ] && /etc/acpi/local/lid.sh.post
    If you have Starcraft, you can extract the game sounds using the sound utilities available here: http://www.battle.net/files.shtml
    The sound files for the tank are ttatra00.wav and ttatra01.wav.

    Additional remarks:
    Note 1:
    It is of course possible to run any command on lid open/close events.
    If you have other fun or useful ideas of what a laptop should do on lid events, please post them here.
    (Sleep and hibernate isn't original. )

    I was thinking about a switch into full download mode on lid close, so that your PC automatically starts using bandwidth for P2P for example when you aren't using it. ^^
    I just don't know how to do that by command-line yet.

    Note 2: How to run scripts for laptop lid open/close and dock/undock events:
    Please refer to the howto from lunatico here:
    [HOWTO] Run scripts for laptop lid open/close and dock/undock events

    Note 3: How to run GUIs:
    [edit]
    cf note above which is a better way to do it.
    [/edit]

    I found a way to do it.
    To run a GUI as root:
    Code:
    DISPLAY=:0 && command
    To run a GUI as normal user:
    Code:
    DISPLAY=:0 && sudo -u username command
    And for those interested in running GUI apps from root crontab, here's how:
    To run a GUI from root crontab as root:
    Code:
    XAUTHORITY=/home/username/.Xauthority
    DISPLAY=:0.0
    
    # m h  dom mon dow   command
    * * * * * sudo -u username command
    To run a GUI from root crontab as normal user:
    Code:
    XAUTHORITY=/home/username/.Xauthority
    DISPLAY=:0.0
    
    # m h  dom mon dow   command
    * * * * * command
    Useful links:
    http://www.faqs.org/docs/Linux-mini/Remote-X-Apps.html
    A script to launch another xserver and run a command on it:
    http://ubuntuforums.org/showthread.php?t=699332
    (basically, use "chvt <tty nb>" and then DISPLAY=... && command)

    Note 3: Synthetizing voice
    You can use "espeak" or "festival" for this.
    Code:
    espeak "hello"
    Code:
    echo "hello" | festival --tts
    To read a whole text file:
    Code:
    festival --tts speak.txt
    Note 4: make the console acknowledge each of your commands with an audio sound
    I don't think this deserves its own howto (would need a full .bashrc tutorial), but this might also interest some people: make the console acknowledge each of your commands with an audio sound.

    Again, RTS sounds are the best for this.
    "By your command..."

    Original post: http://ubuntuforums.org/showpost.php...2&postcount=61

    ================================================== ======

    If you want to play sounds in your terminal when running any command (or just pressing enter), do the following:

    Open ~/.bashrc.
    Search for a string looking like "PS1=<some string>".
    Add "\$(playsound)" at the beginning of the string.
    It should then look like this for example:
    Code:
    PS1="\$(playsound)\e[0;1;33;41m[\u@\h:\w]\$ \e[m "
    Then before that (at the beginning of the file), add:
    Code:
    playsound()
    {
    	aplay somefile.wav 1>/dev/null 2>&1 &
    }
    Note the "&" at the end of the aplay command to avoid having to wait for the end of the soundfile playing.
    It also makes spamming possible when pressing enter several times.

    example:"NuNuNuclear lalalaunch detectededed."

    Some documentation:
    http://www.linuxselfhelp.com/howtos/...O-2.html#setps
    http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x279.html
    ================================================== ======
    Last edited by KIAaze; September 19th, 2009 at 06:30 PM. Reason: replaced mplayer with aplay

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •