DBus – How to Get a Proxy for a Non-Well-Known DBus Object

application-developmentdbuslibdbusmenupython

I'm endeavouring to use dbus to react to a particular signal (user clicks on an appindicator menu on the unity top panel). From using dbus-monitor, I've found the relevant signal that I'd like to react to:

sender=:1.96 -> dest=:1.232 serial=2091 path=/org/ayatana/NotificationItem/myapp/Menu; interface=com.canonical.dbusmenu; member=AboutToShowGroup
array [
   int32 9
]

Then my plan was to do the following with python:

dbus_proxy = bus.get_object ("BUS.NAME", "PATH")
interface = dbus.Interface (proxy, "INTERFACE")
interface.connect_to_signal ("SIGNAL", my_function)

def my_function:
    # react 

In order to understand dbus, I've been using the following two articles:
the dbus-python tutorial and
How to read dbus-monitor output?

However, I'm a bit confused as to what values I should be using for bus name, path, interface and signal in my code. I've tried poking around in qdbus in order to work out the correct values, but I can't seem to find what I'm looking for. I think I've been thrown off because get_object requires a "well-known" name, but all I've got is the numerical unique identifiers for sender, dest.

Any nudge in the right direction would be greatly appreciated.

Best Answer

The add_signal_receiver function can be used without specifying a bus name. This can be useful for capturing signals when the bus name, interface or specific signal name is unknown.

session_bus = dbus.SessionBus()

session_bus.add_signal_receiver(self.do_something,
                                signal_name=None,
                                dbus_interface=None,
                                bus_name=None, 
                                path=None)

The above code calls the do_something method every time a signal is received on the session bus.

In my original question, I stated I wanted to respond to 'AboutToShowGroup'. However, after closer inspection of the 'com.canonical.dbusmenu' interface, I realised it is in fact a method and not a signal. I have since chosen another signal to react to.

Related Question