Jump to content


Closing the lid with external monitor


11 replies to this topic

#1 Mue

    New member

  • Members
  • 3 posts

Posted 02 August 2008 - 09:01 PM

Here's my lidbtn.sh. I hope some finds it useful. Any comments, suggestions or criticism are welcome.
This is what it does:
If I'm running on batteries, it shuts down.
If I'm plugged in, but with no external monitor, it suspends.
If I'm plugged in with an external monitor, it switches off the internal screen, and up the resolution on the external monitor.
If I open the lid and I'm using the external monitor it switched on the internal monitor, and lowers the resolution of the external monitor to match the internal.

#!/bin/sh
LID_STATE=`cat /proc/acpi/button/lid/LID/state | awk '{print $2 }'`
AC0_STATE=`cat /proc/acpi/ac_adapter/AC0/state | awk '{print $2 }'`
VGA_STATE=`xrandr --prop -display :0.0 | grep "VGA connected [0-9]" | wc -l`
LVDS_STATE=`xrandr --prop -display :0.0 | grep "LVDS connected [0-9]" | wc -l`

if [ $LID_STATE = "closed" ]; then
    if [ $AC0_STATE = "on-line"  ]; then
        if [ $VGA_STATE = "1" ]; then
            if [ $LVDS_STATE = "1" ]; then
                xrandr -display :0.0 --output LVDS --off 
                xrandr -display :0.0 --output VGA --mode 1024x768 
            fi
        else 
            /etc/acpi/suspend2ram.sh 
        fi
    else
        /sbin/fastshutdown.sh
    fi
else
    if [ $VGA_STATE = "1" ]; then
        xrandr -display :0.0 --output LVDS --mode 1024x600 
        xrandr -display :0.0 --output VGA --mode 1024x600 
    fi
fi
exit 0

Edited by Mue, 02 August 2008 - 09:02 PM.


#2 albkwan

    ExtrEmE User

  • Members
  • 1,440 posts
  • LocationHong Kong

Posted 03 August 2008 - 02:25 AM

Good work!

I am looking for a way to Shut down on lid close normally, but while I am listening to music with my eeepc, I would like it to Do Nothing On Closing The Lid, just Shutting off screen. Any idea how this can be done?
EeePC 4G white,
Default Xandros (Easy Mode + icewm start menu) on 1st SSD/2nd 16GB SSD added/SD/USB/
http://eeepc.fire.prohosting.com/
http://eeepc-albkwan.blogspot.com/

#3 Mue

    New member

  • Members
  • 3 posts

Posted 04 August 2008 - 07:28 PM

Evening Albkwan,

The first step is detecting if your music playing app is running.
You can use ps to list all the running processes:
ps -A
You can then use grep to search the output from this to see if you music app is listed. If you are using amarok (the default music manager thingy) you can use:
ps -A | grep amarokapp
If amarokapp is running you'll get a line of text back telling you the process id and name.
Then to make this more accessible inside the script, I would pipe it to wc the word counter and ask it to count the number of lines.
ps -A | grep amarokapp | wc -l
Typing this into a terminal window should display the number of copies of amarokapp processes that are running.

So far so good, now to use this inside the lidbtn.sh script. Adding a line near the beginning of the script to execute the above command and store the output in a variable. Something like:
MUSIC_APP_STATE=`ps -A | grep amarokapp | wc -l`
Make sure you use back ticks ` instead of apostrophes '

Then further down in the lidbtn.sh, before shutting down, check that the music player is NOT running with something like:
if [ $MUSIC_APP_STATE == "0" ]; then
I hope that gets you some if not all of the way there. The main problem with this approach is it only checks if amarok is running, not if it is actually playing music. When you close the amarok window, it doesn't quit, it just minimizes to the task bar, so you'll have to quit it before closing the lid will shutdown you eee.

Mue.

Edited by Mue, 04 August 2008 - 07:32 PM.


#4 albkwan

    ExtrEmE User

  • Members
  • 1,440 posts
  • LocationHong Kong

Posted 05 August 2008 - 08:19 AM

Hi, Mue,

Thanks for the advice. I have run some test and this is what I have worked out:
#!/bin/sh
LID_STATE=`cat /proc/acpi/button/lid/LID/state | awk '{print $2 }'`
#AC0_STATE=`cat /proc/acpi/ac_adapter/AC0/state | awk '{print $2 }'`
VGA_STATE=`xrandr --prop -display :0.0 | grep "VGA connected [0-9]" | wc -l`
#LVDS_STATE=`xrandr --prop -display :0.0 | grep "LVDS connected [0-9]" | wc -l`
MUSIC_APP_STATE=`ps -A | grep amarokapp  | wc -l`

if [ $LID_STATE = "closed" ] ; then
#    /etc/acpi/suspend2ram.sh
    if [ $VGA_STATE = "1" ]; then
            xrandr -display :0.0 --output LVDS --off
    else
        if [ $MUSIC_APP_STATE == "1" ]; then
            if [ $LID_STATE = "closed" ] ; then
                su user -c "DISPLAY=:0 xset dpms force suspend"
            elif [ $LID_STATE = "open" ] ; then
                su user -c "DISPLAY=:0 xset dpms force on"
            fi
        else
            /sbin/fastshutdown.sh
        fi
    fi
fi
exit 0
Now my eeepc will normally shut down on lid close. But when the display is set to external monitor, it will just switch off the LCD. And when I am playing music from Amarok, it will just blank screen (any mouse movement will resume).

Thanks again

albkwan
EeePC 4G white,
Default Xandros (Easy Mode + icewm start menu) on 1st SSD/2nd 16GB SSD added/SD/USB/
http://eeepc.fire.prohosting.com/
http://eeepc-albkwan.blogspot.com/

#5 simd

    New member

  • Members
  • 7 posts

Posted 05 August 2008 - 08:28 AM

Fantastic work - very useful - thanks! I had no idea this was possible...

#6 albkwan

    ExtrEmE User

  • Members
  • 1,440 posts
  • LocationHong Kong

Posted 05 August 2008 - 09:22 AM

A little enhancement: This time, the script will detect any of the following music players running, and will do the same trick: amarok, audacious, vlc, smplayer, mpg321, xmms
#!/bin/sh
LID_STATE=`cat /proc/acpi/button/lid/LID/state | awk '{print $2 }'`
#AC0_STATE=`cat /proc/acpi/ac_adapter/AC0/state | awk '{print $2 }'`
VGA_STATE=`xrandr --prop -display :0.0 | grep "VGA connected [0-9]" | wc -l`
#LVDS_STATE=`xrandr --prop -display :0.0 | grep "LVDS connected [0-9]" | wc -l`
MUSIC_APP_STATE_1=`ps -A | grep amarokapp  | wc -l`
MUSIC_APP_STATE_2=`ps -A | grep audacious  | wc -l`
MUSIC_APP_STATE_3=`ps -A | grep wxvlc  | wc -l`
MUSIC_APP_STATE_4=`ps -A | grep mplayer  | wc -l`
MUSIC_APP_STATE_5=`ps -A | grep mpg321  | wc -l`
MUSIC_APP_STATE_6=`ps -A | grep xmms  | wc -l`
MUSIC_APP_STATE=${MUSIC_APP_STATE_1}${MUSIC_APP_STATE_2}${MUSIC_APP_STATE_3}${MUSIC_APP_STATE_4}${MUSIC_APP_STATE_5}${MUSIC_APP_STATE_6}

if [ $LID_STATE = "closed" ] ; then
#    /etc/acpi/suspend2ram.sh
    if [ $VGA_STATE = "1" ]; then
            xrandr -display :0.0 --output LVDS --off
    else
        if [ $MUSIC_APP_STATE != "000000" ]; then
            if [ $LID_STATE = "closed" ] ; then
                su user -c "DISPLAY=:0 xset dpms force suspend"
            elif [ $LID_STATE = "open" ] ; then
                su user -c "DISPLAY=:0 xset dpms force on"
            fi
        else
            /sbin/fastshutdown.sh
        fi
    fi
fi
exit 0

EeePC 4G white,
Default Xandros (Easy Mode + icewm start menu) on 1st SSD/2nd 16GB SSD added/SD/USB/
http://eeepc.fire.prohosting.com/
http://eeepc-albkwan.blogspot.com/

#7 MamiyaOtaru

    Senior Member

  • Members
  • 123 posts

Posted 06 August 2008 - 04:04 AM

Awesome script. It does sensible things depending on what's plugged in.

The only thing I'd worry about is running with the lid closed. A fair amount of heat bleeds out the keyboard, and that heat would now be fatiguing the LCD (even though it's off).

But if you've been doing it and it's been fine, it's worth trying out!

*EDIT* it would NOW be fatiguing the LCD, instead of it would NOT be fatiguing the LCD. FFS that totally flopped the meaning :(

Edited by MamiyaOtaru, 17 October 2008 - 10:24 AM.


#8 Mue

    New member

  • Members
  • 3 posts

Posted 06 August 2008 - 10:55 PM

Hey Albkwan, excellent work.

One little thing you could do to simplify your script. You can use the 'let' to do arithmetic on variables. So you could change your code that combines the different music states to something like:
let MUSIC_APP_STATE=${MUSIC_APP_STATE_1}+${MUSIC_APP_STATE_2}+${MUSIC_APP_STATE_3}+${MUSIC_APP_STATE_4}+${MUSIC_APP_STATE_5}+${MUSIC_APP_STATE_6}
Then ${MUSIC_APP_STATE} will be equal to 0 if nothing is running, or say 3 if xmms, mpg321 and amarokapp are all running.


Hey MamiyaOtaru.

I'm currently running my eee on the AC adaptor with the battery removed, the lid closed and an external monitor. It currently much cooler than when I run it open from the battery with no external monitor.

Mue

#9 albkwan

    ExtrEmE User

  • Members
  • 1,440 posts
  • LocationHong Kong

Posted 10 August 2008 - 10:52 AM

I have added these scripts to the wiki: Howto: Shut down on lid close
EeePC 4G white,
Default Xandros (Easy Mode + icewm start menu) on 1st SSD/2nd 16GB SSD added/SD/USB/
http://eeepc.fire.prohosting.com/
http://eeepc-albkwan.blogspot.com/

#10 SunrayDJ

    New member

  • Members
  • 3 posts

Posted 08 September 2008 - 02:32 PM

Hey guys I'm sorta new to this. Would be the first code I've made.
I have attempted to combine a few features of the above scripts and wish to achieve the following:

1. When I am playing music, running on batteries, closing the lid would just suspend the screen and come back when I open the lid.
2. When I am at home, I always have it plugged into the mains, if I have yet to plug the vga cable, it would shutdown upon closing
to charge the batteries.
3. When I am at home and have plugged in the vga cable, closing would turn off laptop display and switch to external monitor at 1280x1024.
When i open i want the laptop dispaly to come back.

I think that covers it.

Here's the script I created. Please provide feedback. I don't think it works. I tried it once and my laptop froze.

#!/bin/sh
LID_STATE=`cat /proc/acpi/button/lid/LID/state | awk '{print $2 }'`
PWR_STATE=`cat /proc/acpi/ac_adapter/AC0/state | awk '{print $2 }'`
VGA_STATE=`xrandr --prop -display :0.0 | grep "VGA connected [0-9]" | wc -l`
MUSIC_APP_STATE=`ps -A | egrep "amarokapp|audacious|wxvlc|mplayer|mpg321|xmms" | wc -l`

if [ $LID_STATE = "closed" ] ; then
if [ $PWR_STATE = "on-line" ] ; then
if [ $VGA_STATE = "1" ] ; then
xrandr -display :0.0 --output LVDS --off
xrandr -display :0.0 --output VGA --mode 1280x1024
else
/sbin/fastshutdown.sh
fi
else
if [ $MUSIC_APP_STATE = "0" ] ; then
/etc/acpi/suspend2ram.sh
else
DISPLAY=:0 xset dpms force suspend
fi
else
if [ $VGA_STATE = "1" ] ; then
xrandr -display :0.0 --output LVDS --mode 800x480
xrandr -display :0.0 --output VGA --mode 1280x1024
else
DISPLAY=:0 xset dpms force on
fi
fi
exit 0

#11 albkwan

    ExtrEmE User

  • Members
  • 1,440 posts
  • LocationHong Kong

Posted 08 September 2008 - 03:16 PM

Quote

Here's the script I created. Please provide feedback. I don't think it works. I tried it once and my laptop froze.

#!/bin/sh
LID_STATE=`cat /proc/acpi/button/lid/LID/state | awk '{print $2 }'`
PWR_STATE=`cat /proc/acpi/ac_adapter/AC0/state | awk '{print $2 }'`
VGA_STATE=`xrandr --prop -display :0.0 | grep "VGA connected [0-9]" | wc -l`
MUSIC_APP_STATE=`ps -A | egrep "amarokapp|audacious|wxvlc|mplayer|mpg321|xmms" | wc -l`

if [ $LID_STATE = "closed" ] ; then
if [ $PWR_STATE = "on-line" ] ; then
if [ $VGA_STATE = "1" ] ; then
xrandr -display :0.0 --output LVDS --off
xrandr -display :0.0 --output VGA --mode 1280x1024
else
/sbin/fastshutdown.sh
fi
else
if [ $MUSIC_APP_STATE = "0" ] ; then
/etc/acpi/suspend2ram.sh
else
DISPLAY=:0 xset dpms force suspend
fi
*** fi ***
else
if [ $VGA_STATE = "1" ] ; then
xrandr -display :0.0 --output LVDS --mode 800x480
xrandr -display :0.0 --output VGA --mode 1280x1024
else
DISPLAY=:0 xset dpms force on
fi
fi
exit 0
There is a "fi" missing in this script.

Also, depending on the Eee model you have, you may not be able to reach a display size of 1280x1024 + 800x480 because I think the max size of the virtual display (at least for my 701) is 1680x1680.
EeePC 4G white,
Default Xandros (Easy Mode + icewm start menu) on 1st SSD/2nd 16GB SSD added/SD/USB/
http://eeepc.fire.prohosting.com/
http://eeepc-albkwan.blogspot.com/

#12 SunrayDJ

    New member

  • Members
  • 3 posts

Posted 09 September 2008 - 12:11 PM

Oh ok. When i do that I jus get the left hand side top of the external monitor on the laptop screen, which is fine. Thanks for that.

I have a 701 as well.

I just tried the script. Works ok when on batts and playing music.

But i plugged in the vga cable and ac and then closed lid...it switched screen to ext. monitor but a whole bunch of apps crashed then it restart itself.

Should I only boot up with the vga cable IN, not plug it is while it is running?

If so, any ideas on a workaround apart from plugging it in pre-boot?

Thanks again

Edited by SunrayDJ, 09 September 2008 - 12:44 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users