Laptop lid script

Forum: Programming and Scripting
Topic: Laptop lid script
started by: Zucca

Posted by Zucca on April 11 2007,20:59
You need to replace /proc/acpi/button/lid/C162/state to fit your own laptop. But it's somewhere in the /proc/acpi directory.

Code Sample


while true
 sleep 1s
 if grep open /proc/acpi/button/lid/C162/state >> /dev/null && [ "$LIDSTATE" == "closed" ]; then
   # Run commands to set you as 'Online'.
 elif grep closed /proc/acpi/button/lid/C162/state >> /dev/null; then
   # Run commands to set you as 'away'.

You may get the point...
This is really nice script to command your IRC client to set you as away when you close the lid. Also for ICQ and M$N.
But personally I'd put a hd spindown command there (as last command when closing lid). ;) With few secs of delay...

Basically it checks every second if the state of your lid has changed and if it has then it performs commands you have entered.

Maybe 'source /etc/' and '/etc/source' or something like that would be nicer. Then you wouldn't need to edit script itself anymore...

Posted by Juanito on April 12 2007,03:26
Alternatively, you could load acpid to detect the lid state for your machine automatically and run the script of your choice
Posted by mikshaw on April 12 2007,13:45
As usual, I can't help but pick at little optimization opportunities.
Is it necessary to check the "state" file _AND_ set a state variable explicitly?
To reduce redundancy and filesize, you can check the state file once and determine your action from that.
If you have only two possible options, "elif" is unnecessary

This is untested, since i don't have/use acpid or a laptop =o)
The LIDSTATE especially may not work, since i have no idea what the state file looks like.
Code Sample

while true
sleep 1s
LIDSTATE=`cat /proc/acpi/button/lid/C162/state`
if  [ "$LIDSTATE" == "closed" ]; then
  # Run commands to set you as 'away'.
  # Run commands to set you as 'Online'.

Also seems to me that one second is a pretty high frequency. Raising that number would be easier on your processor. It's just an opinion, though.

Posted by Zucca on April 12 2007,20:41
This is why:
Code Sample
$ cat /proc/acpi/button/lid/C162/state
state:      open

And I need to set the variable because that script would then perform commands each second when lid is closed/open. I need to set that variable to tell script that it has already performed those tasks on open/close and won't "reperform" those commands anymore until state has changed.

Every second... Hm. It could be like every 10 seconds. That's true.

Posted by mikshaw on April 12 2007,21:01
I see...hadn't thought about rerunning the same commands for no reason (this thread was the first thinking I did today =o))...good point.

It still seems to me that grepping the state file more than once in a loop may be unnecessary, but it's a lot better compared to what I was thinking this morning.

Posted by mikshaw on April 13 2007,02:32
After coming back to this thread, it looks like I'm being critical of your code for no reason.  Please be assured that it's merely for my own curiosity. To me the enjoyment from tweaking a script is a close second to actually creating something.
That's about all what I wanted to say there.

Posted by Zucca on April 13 2007,06:18
That's okay. I like when people give me tips to my works.
You should see what I've done with PHP.
First I learned to code file listing script with few nice features. After that I decided to code whole site engine with "plugin" support.
Now when I look it I see only heaps of code that needs optimizing a lot. :p
I've been thinkin to rewrite all the code. ;P If I only had time...

Also as a Gentoo user i try to optimize everything. ;)

Powered by Ikonboard 3.1.2a
Ikonboard © 2001 Jarvis Entertainment Group, Inc.