[ Home | RSS 2.0 | ATOM 1.0 | Comment ]
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.
#!/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
Make sure the pm-utils package, or whatever package provides pm-hibernate, is installed on your system.
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.