Update 2010-05-02: See this comment.
I read an article in the Dec 2009 issue of Linux Magazine, one of several Linux-focused magazines we get at the office. I’d like to link directly to it, but it the magazine’s own website doesn’t offer the article or even a reliable permanent link to the issue number. Hint: hey guys, sort that out.
The article was about configuring ACPI hotkeys to support your specific laptop. IE, the buttons for “sleep”, “brightness up”, etc. For most laptops this already works on Ubuntu. On my Dell Vostro 1500, every button except for “sleep” worked right after install. This is Linux, so there is always some way to fix that.
Unfortunately, ACPI cannot even detect my keypress. The instructions in the article suggest starting acpid with -d for debug mode, which will print any keypresses that reach it. Pressing Fn+F1, the sleep button, prints nothing. So we’re going to use KDE’s hotkey support instead. Gnome users can use a similar method; only the menu instructions differ.
First, create a script somewhere your user can run. I have $HOME/bin in my $PATH, so I created a file there:
chmod 755 ~/bin/sleep-kde-screen
Then I edited it to contain the following:
qdbus org.freedesktop.ScreenSaver /ScreenSaver Lock
This calls DBUS to lock the screensaver, then asks power-manager to suspend. This way you will be prompted for your password when you resume from suspend. If you prefer to hibernate, change “pm-suspend” to “pm-hibernate”.
Second, you need to tell sudo to allow your user to run this /usr/bin/pm-suspend without prompting for a password. Run
sudo visudo to edit /etc/sudoers, and add this line at the bottom:
%admin ALL=NOPASSWD: /usr/sbin/pm-suspend
Finally, add this script to your K menu with a hotkey. Right-click on the K menu and select “Menu Editor”. Add a new item to the “System” menu, give it a name (I like “Sleep!” *), and tell it to call sleep-kde-screen (wherever you’ve put it). On the Advanced tab, select “Current shortcut key” and press the sleep button. Now save and close the menu editor.
* I imagine saying “Sleeeeeep!” like a hypnotist bad guy from an old movie on MST3K.
Press the sleep button, and you’ll suspend!