Code, mostly.

Archives:

[ Home | RSS 2.0 | ATOM 1.0 | Comment ]

Sat, 26 Dec 2009

Eco-button for linux

Having received an eco-button I wondered what it would take to get it to do anything in Linux. I was thinking about modifying a simple USB driver or something arcane like that, but it turned out to be much simpler. When you plug the eco-button in to a USB port and press it it generates a stream of XF86Sleep keypress events. It doesn't show up under lsusb which makes me think it might be injecting keyboard input, but anyway, it sends a clear and usable signal.

If your system is setup to sleep when the extended keyboard's sleep key is pressed the eco-button might work out of the box for your. If not, here are three simple steps to make it go.

  1. Have your window-manager bind the XF86Sleep key to run the following python program. Put the text below in a file, called perhaps /home/joeuser/bin/ecobutton, and then tell your window-manager to run it when the XF86Sleep is pressed. You'll need to make the program executeable (chmod +x /home/joeuser/bin/ecobutton). When you're setting up the hotkey, you can press the eco-button itself of course, to send the XF86Sleep keystroke.
#!/usr/bin/python

import os
import time
import fcntl

aFile = open(os.path.expanduser("~/.ecobutton.lck"), 'w')
fd = aFile.fileno()
fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
# program aborts at this point if another already has lock
os.system("sudo /usr/sbin/pm-hibernate")
time.sleep(10)  # stay running to preserve lock
  1. Make sure the pm-utils package, or whatever package provides pm-hibernate, is installed on your system.

  2. Use visudo to add an entry like this at the end of /etc/sudoers (replace joeuser with your username):

    joeuser ALL=(ALL) NOPASSWD: /usr/sbin/pm-hibernate
    

That's it. Because the eco-button generates a stream of XF86Sleep key-presses the python program uses a lockfile to ensure pm-hibernate is only run once. That's probably unnecessary, pm-hibernate may do its own locking anyway.

Of course all of this is just a convoluted way of invoking the hibernate function, and to a degree it's just code for its own sake, making the button work in Linux. On the other hand it really has reduced my energy usage, because it inspired me to test Ubuntu's hibernate function (which wasn't working well last time I looked, 2-3 years ago, but is now fine), and the glowing button thing reminds me to use it.

posted at: 12:21 | path: /code/python | permanent link to this entry

Made with PyBlosxom