I'm playing around with dbus-monitor to try and understand how dbus is working in Ubuntu environment. I have several questions in this regard:
Could you please let me know how to read the following properly? I understand the big idea, but not the details.
signal sender=:1.1948 -> dest=(null destination) serial=1829990 path=/org/ayatana/menu/DA00003; interface=org.ayatana.dbusmenu; member=ItemPropertyUpdated int32 23 string "enabled" variant boolean true method call sender=:1.6 -> dest=org.freedesktop.Notifications serial=1399 path=/org/freedesktop/Notifications; interface=org.freedesktop.Notifications; member=GetCapabilities
I get that the first one is a signal whereas the second is a method. Does destination mean there can be a specific receiver/slot for a signal? What's a member? And are the items of the list following the signal the arguments passed in the signal? What are sender and serials?
I noticed something about the relationship between volume-control and notifications. From what I read from the dbus-monitor output
method call sender=:1.6 -> dest=org.freedesktop.Notifications serial=1400 path=/org/freedesktop/Notifications; interface=org.freedesktop.Notifications; member=Notify string "gnome-settings-daemon" uint32 0 string "notification-audio-volume-medium" string " " string "" array [ ] array [ dict entry( string "value" variant int32 38 ) dict entry( string "x-canonical-private-synchronous" variant string "volume" ) ] int32 -1
It seems that the notification is triggered by its method. I just don't really understand why it works this way. In my view it would make more sense if there was a signal emitted "notification-audio-volume-medium" while the notification would listen for this signal and react accordingly. If the sending / receiving would be public rather than private, wouldn't it allow for more flexibility and efficiency? For instance if there was a public signal for "notification-audio-volume-medium" then several applications could listen for this signal (which would allow for competing notification applications to come to existence) and developers would just have to be concerned with sending signals, while picking up and handling a signal would be the notifying application's business (or any other program that needs those signals).
I'm just new to Dbus and want to learn more as I am working with Dbus on Python, mainly to develop some applets. I've seen the dbus-python tutorial and it teaches how to listen to all signals (by specifying neither interface nor path etc.) But how to track methods when they are called, like dbus-monitor does?
If you have the patience to teach how that works, you're welcome.
D-Bus introduction
D-Bus provides means for communicating between services. Services can be anonymous (identified solely by bus address, like :1.6), and services can aquire well-known names, like
org.freedesktop.Notifications
ororg.freedesktop.NetworkManager
. The sender and destination which you can see in the logs are services. "Null destination" means broadcast: delivery to all services.A service can export one or several objects to the bus. Objects are given object paths, like
/org/freedesktop/NetworkManager/ActiveConnection/1
or/org/ayatana/menu/DA00003
. Object paths use slash as separator, like filesystem paths.Each object can support one or several interfaces. An interface is nothing more than a set of methods and signals, coloquially known as members (very similar to OOP interface). Methods and signals have fixed signatures. Members are always namespaced within well-known interface names.
Once published, well-known names never change.
Any service can connect to another service's signals and asynchronously call its methods. Any service can emit signals.
Signals
Now to your specific questions.
Yes, you're right, this is a signal. It's broadcasted by service
:1.1948
, and "self" object is/org/ayatana/menu/DA00003
. The signal has nameItemPropertyUpdated
which is defined in the interfaceorg.ayatana.dbusmenu
(likeorg.ayatana.dbusmenu::ItemPropertyUpdated
in C++). The serial, I guess, is a kind of unique identifier of the event on the bus.Then we see the signal arguments. According to the interface documentation, the first int32 argument is an item's id, second string is its property name, and the third variant is the property value. So, the
/org/ayatana/menu/DA00003
object is notifying us that the item id #23 changed itsenabled
property to true.Another example on signals:
I sent a text message "test" using Pidgin to an IRC channel, and
/im/pidgin/purple/PurpleObject
emitted two signals under theim.pidgin.purple.PurpleInterface
interface: first a generalSendingChatMsg
, then a more specificIrcSendingText
.Methods
Now methods. Methods are a way to ask D-Bus objects to do something, or to perform some query and return data. They are quite similar to classic OOP methods, except that D-Bus methods are called asynchronously.
Let's call a D-Bus method programmatically.
Note the arguments, especially the icon name. In your example
"notification-audio-volume-medium"
was the icon of medium-powered volume speaker.Custom services
It is absolutely possible to run your own D-Bus services, export your own D-Bus objects and define your own D-Bus interfaces with your own methods and signals. This all can be done in Python pretty easily once you grasp the overall concept and read the
dbus
module documentation.:)
I was also looking for solution to collect the desktop notifications through dbus with a python script. This question was the closest I got with googling, but writing a replacement for notify-osd seemed like an overkill :)
Looking at the recent-notifications applet sources I got some hints how to monitor the dbus messages and here is the python implementation I came up with:
Hope this helps someone, as it seems that there isn't many simple python examples related to monitoring the dbus messages.