Fork me on GitHub

Suspend on lid close in Debian Squeeze

I recently decided that Gnome is not the best desktop for my little EeePC netbook with a little 10.5" screen. So I'm playing around with a window manager that mainly just maximizes everything. I've tried Ratpoison, I've tried wmii, and now I'm trying Awesome.

I have a lot of customizations to do, but one thing that was missing was a way to suspend the netbook when the laptop lid is closed. I could manually run acpitool -s in a shell, or pm-suspend, but it's best handled by DBus, as intended.

Now, a simple way to have DBus do the work is using dbus-send, like so:

dbus-send --print-reply \
          --system \
          --dest=org.freedesktop.UPower \
          /org/freedesktop/UPower \

The hard part is subscribing to the lid close event, so I'm not polling all the time, exactly what DBus was written to prevent. I had a Python script for this, but the API was changed in Squeeze to use the UPower daemon and API.

I had to do some poking around to figure out how to update it, but I just got it working, so I thought I'd share.


import dbus, gobject, sys
from dbus.mainloop.glib import DBusGMainLoop

pow_prop_iface = None
pow_iface = None

def handle_lidclose(*args):
    closed = pow_prop_iface.Get('',
    if closed:
        print "lid is closed, suspending"
        print "lid is open"

def main():
    global pow_prop_iface, pow_iface


    bus = dbus.SystemBus()

    power_proxy = bus.get_object('org.freedesktop.UPower',

    pow_prop_iface = dbus.Interface(power_proxy,
    pow_iface = dbus.Interface(power_proxy,

    print "Registering a signal receiver for upower events..."


    loop = gobject.MainLoop()

if __name__ == '__main__':

Now I just run it in the background from my .xsession script at X11 login, and it's sitting there waiting for any change in UPower status. Works like a charm.

Comments !