Andrew Clunis

Trending towards more awesome.

Getting Power Button Events With HAL

I needed to teach the product I’m working on about the ACPI power button, so fired up dbus-monitor –system to see what events I get when the button is pressed. It was easy to find the device in question in d-feet afterwards. Unfortunately, the HAL device objects (/org/freedesktop/Hal/devices/computer_logicaldev_input_N) that emit the events change between machines and even between reboots, so it’s necessary to properly discover it.

This little spike solution prints out messages to stdout whenever certain special ACPI keys on your keyboard are pressed (on my Lenovo ThinkPad T61, it shows the power button, the volume buttons, the Fn-key chords for changing backlight, etc.):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import gobject
import dbus
import dbus.mainloop.glib

def getButtonCondition(event, btn):
    print "Event: %s, arg: %s" % (event, btn)
    mainloop = gobject.MainLoop()

dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()

hal_manager_proxy = bus.get_object('org.freedesktop.Hal',
                                   '/org/freedesktop/Hal/Manager')

hal_manager = dbus.Interface(hal_manager_proxy,
                             dbus_interface='org.freedesktop.Hal.Manager')

button_paths = hal_manager.FindDeviceByCapability('button')

button_proxies = []
buttons = []

for p in button_paths:
    button_proxy = bus.get_object('org.freedesktop.Hal', p)

    button = dbus.Interface(button_proxy, dbus_interface = 'org.freedesktop.Hal.Device')
    button.connect_to_signal("Condition", getButtonCondition)
    button_proxies.append(button_proxy)
    buttons.append(button)

mainloop.run()