How to read dbus-monitor output?
Solution 1:
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.
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
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 name ItemPropertyUpdated
which is defined in the interface org.ayatana.dbusmenu
(like org.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 its enabled
property to true.
Another example on signals:
signal sender=:1.1602 -> dest=(null destination) serial=20408 path=/im/pidgin/purple/PurpleObject; interface=im.pidgin.purple.PurpleInterface; member=SendingChatMsg int32 47893 string "test" uint32 1 signal sender=:1.1602 -> dest=(null destination) serial=20409 path=/im/pidgin/purple/PurpleObject; interface=im.pidgin.purple.PurpleInterface; member=IrcSendingText int32 64170 string "PRIVMSG #chat :test
I sent a text message "test" using Pidgin to an IRC channel, and /im/pidgin/purple/PurpleObject
emitted two signals under the im.pidgin.purple.PurpleInterface
interface: first a general SendingChatMsg
, then a more specific IrcSendingText
.
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.
import dbus, dbus.proxies
#-- connect to the session bus (as opposed to the system bus)
session = dbus.SessionBus()
#-- create proxy object of D-Bus object
obj_proxy = dbus.proxies.ProxyObject(conn=session,
bus_name="org.freedesktop.Notifications", #-- name of the service we are retrieving object from
object_path="/org/freedesktop/Notifications") #-- the object path
#-- create proxy object of the D-Bus object wrapped into specific interface
intf_proxy = dbus.proxies.Interface(obj_proxy, "org.freedesktop.Notifications")
#-- lastly, create proxy object of the D-Bus method
method_proxy = intf_proxy.get_dbus_method("Notify")
#-- ... and call the method
method_proxy("test from python",
dbus.UInt32(0),
"bluetooth", #-- icon name
"Notification summary",
"Here goes notification body",
[], {},
5) #-- timeout
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. :)
Solution 2:
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:
import gtk
import dbus
from dbus.mainloop.glib import DBusGMainLoop
def filter_cb(bus, message):
# the NameAcquired message comes through before match string gets applied
if message.get_member() != "Notify":
return
args = message.get_args_list()
# args are
# (app_name, notification_id, icon, summary, body, actions, hints, timeout)
print("Notification from app '%s'" % args[0])
print("Summary: %s" % args[3])
print("Body: %s", args[4])
DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
bus.add_match_string(
"type='method_call',interface='org.freedesktop.Notifications',member='Notify'")
bus.add_message_filter(filter_cb)
gtk.main()
Hope this helps someone, as it seems that there isn't many simple python examples related to monitoring the dbus messages.