D-BUS Tutorial
Version 0.4.1
15 July 2005
Havoc
Pennington
Red Hat, Inc.
hp@pobox.com
David
Wheeler
John
Palmieri
Red Hat, Inc.
johnp@redhat.com
Colin
Walters
Red Hat, Inc.
walters@redhat.com
What is D-BUS?
D-BUS is a system for interprocess communication
(IPC). Architecturally, it has several layers:
A library, libdbus, that allows two
applications to connect to each other and exchange messages.
A message bus daemon executable, built on
libdbus, that multiple applications can connect to. The daemon can
route messages from one application to zero or more other
applications.
Wrapper libraries based on particular
application frameworks. For example, libdbus-glib and
libdbus-qt. There are also bindings to languages such as
Python. These wrapper libraries are the API most people should use,
as they simplify the details of D-BUS programming. libdbus is
intended to be a low-level backend for the higher level bindings.
Much of the libdbus API is only useful for binding implementation.
If you just want to use D-BUS and don't care how it works, jump directly
to .
Otherwise, read on.
libdbus only supports one-to-one connections, just like a raw network
socket. However, rather than sending byte streams over the connection, you
send messages. Messages have a header identifying
the kind of message, and a body containing a data payload. libdbus also
abstracts the exact transport used (sockets vs. whatever else), and
handles details such as authentication.
The message bus daemon forms the hub of a wheel. Each spoke of the wheel
is a one-to-one connection to an application using libdbus. An
application sends a message to the bus daemon over its spoke, and the bus
daemon forwards the message to other connected applications as
appropriate. Think of the daemon as a router.
The bus daemon has multiple instances on a typical computer. The
first instance is a machine-global singleton, that is, a system daemon
similar to sendmail or Apache. This instance has heavy security
restrictions on what messages it will accept, and is used for systemwide
communication. The other instances are created one per user login session.
These instances allow applications in the user's session to communicate
with one another.
The systemwide and per-user daemons are separate. Normal within-session
IPC does not involve the systemwide message bus process and vice versa.
D-BUS applications
There are many, many technologies in the world that have "Inter-process
communication" or "networking" in their stated purpose: CORBA, DCE, DCOM, DCOP, XML-RPC, SOAP, MBUS, Internet Communications Engine (ICE),
and probably hundreds more.
Each of these is tailored for particular kinds of application.
D-BUS is designed for two specific cases:
Communication between desktop applications in the same desktop
session; to allow integration of the desktop session as a whole,
and address issues of process lifecycle (when do desktop components
start and stop running).
Communication between the desktop session and the operating system,
where the operating system would typically include the kernel
and any system daemons or processes.
For the within-desktop-session use case, the GNOME and KDE desktops
have significant previous experience with different IPC solutions
such as CORBA and DCOP. D-BUS is built on that experience and
carefully tailored to meet the needs of these desktop projects
in particular. D-BUS may or may not be appropriate for other
applications; the FAQ has some comparisons to other IPC systems.
The problem solved by the systemwide or communication-with-the-OS case
is explained well by the following text from the Linux Hotplug project:
A gap in current Linux support is that policies with any sort of
dynamic "interact with user" component aren't currently
supported. For example, that's often needed the first time a network
adapter or printer is connected, and to determine appropriate places
to mount disk drives. It would seem that such actions could be
supported for any case where a responsible human can be identified:
single user workstations, or any system which is remotely
administered.
This is a classic "remote sysadmin" problem, where in this case
hotplugging needs to deliver an event from one security domain
(operating system kernel, in this case) to another (desktop for
logged-in user, or remote sysadmin). Any effective response must go
the other way: the remote domain taking some action that lets the
kernel expose the desired device capabilities. (The action can often
be taken asynchronously, for example letting new hardware be idle
until a meeting finishes.) At this writing, Linux doesn't have
widely adopted solutions to such problems. However, the new D-Bus
work may begin to solve that problem.
D-BUS may happen to be useful for purposes other than the one it was
designed for. Its general properties that distinguish it from
other forms of IPC are:
Binary protocol designed to be used asynchronously
(similar in spirit to the X Window System protocol).
Stateful, reliable connections held open over time.
The message bus is a daemon, not a "swarm" or
distributed architecture.
Many implementation and deployment issues are specified rather
than left ambiguous.
Semantics are similar to the existing DCOP system, allowing
KDE to adopt it more easily.
Security features to support the systemwide mode of the
message bus.
Concepts
Some basic concepts apply no matter what application framework you're
using to write a D-BUS application. The exact code you write will be
different for GLib vs. Qt vs. Python applications, however.
Here is a diagram (png svg) that may help you visualize the concepts
that follow.
Objects and Object Paths
Each application using D-BUS contains objects,
which generally map to GObject, QObject, C++ objects, or Python objects
(but need not). An object is an instance rather
than a type. When messages are received over a D-BUS connection, they
are sent to a specific object, not to the application as a whole.
To allow messages to specify their destination object, there has to be a
way to refer to an object. In your favorite programming language, this
is normally called a pointer or
reference. However, these references are
implemented as memory addresses relative to the address space of your
application, and thus can't be passed from one application to another.
To solve this, D-BUS introduces a name for each object. The name
looks like a filesystem path, for example an object could be
named /org/kde/kspread/sheets/3/cells/4/5.
Human-readable paths are nice, but you are free to create an
object named /com/mycompany/c5yo817y0c1y1c5b
if it makes sense for your application.
Namespacing object paths is smart, by starting them with the components
of a domain name you own (e.g. /org/kde). This
keeps different code modules in the same process from stepping
on one another's toes.
Interfaces
Each object supports one or more interfaces.
Think of an interface as a named group of methods and signals,
just as it is in GLib or Qt or Java. Interfaces define the
type of an object instance.
Message Types
Messages are not all the same; in particular, D-BUS has
4 built-in message types:
Method call messages ask to invoke a method
on an object.
Method return messages return the results
of invoking a method.
Error messages return an exception caused by
invoking a method.
Signal messages are notifications that a given signal
has been emitted (that an event has occurred).
You could also think of these as "event" messages.
A method call maps very simply to messages, then: you send a method call
message, and receive either a method return message or an error message
in reply.
Bus Names
Object paths, interfaces, and messages exist on the level of
libdbus and the D-BUS protocol; they are used even in the
1-to-1 case with no message bus involved.
Bus names, on the other hand, are a property of the message bus daemon.
The bus maintains a mapping from names to message bus connections.
These names are used to specify the origin and destination
of messages passing through the message bus. When a name is mapped
to a particular application's connection, that application is said to
own that name.
On connecting to the bus daemon, each application immediately owns a
special name called the unique connection name.
A unique name begins with a ':' (colon) character; no other names are
allowed to begin with that character. Unique names are special because
they are created dynamically, and are never re-used during the lifetime
of the same bus daemon. You know that a given unique name will have the
same owner at all times. An example of a unique name might be
:34-907. The numbers after the colon have
no meaning other than their uniqueness.
Applications may ask to own additional well-known
names. For example, you could write a specification to
define a name called com.mycompany.TextEditor.
Your definition could specify that to own this name, an application
should have an object at the path
/com/mycompany/TextFileManager supporting the
interface org.freedesktop.FileHandler.
Applications could then send messages to this bus name,
object, and interface to execute method calls.
You could think of the unique names as IP addresses, and the
well-known names as domain names. So
com.mycompany.TextEditor might map to something like
:34-907 just as mycompany.com maps
to something like 192.168.0.5.
Names have a second important use, other than routing messages. They
are used to track lifecycle. When an application exits (or crashes), its
connection to the message bus will be closed by the operating system
kernel. The message bus then sends out notification messages telling
remaining applications that the application's names have lost their
owner. By tracking these notifications, your application can reliably
monitor the lifetime of other applications.
Addresses
Applications using D-BUS are either servers or clients. A server
listens for incoming connections; a client connects to a server. Once
the connection is established, it is a symmetric flow of messages; the
client-server distinction only matters when setting up the
connection.
A D-BUS address specifies where a server will
listen, and where a client will connect. For example, the address
unix:path=/tmp/abcdef specifies that the server will
listen on a UNIX domain socket at the path
/tmp/abcdef and the client will connect to that
socket. An address can also specify TCP/IP sockets, or any other
transport defined in future iterations of the D-BUS specification.
When using D-BUS with a message bus, the bus daemon is a server
and all other applications are clients of the bus daemon.
libdbus automatically discovers the address of the per-session bus
daemon by reading an environment variable. It discovers the
systemwide bus daemon by checking a well-known UNIX domain socket path
(though you can override this address with an environment variable).
If you're using D-BUS without a bus daemon, it's up to you to
define which application will be the server and which will be
the client, and specify a mechanism for them to agree on
the server's address.
Big Conceptual Picture
Pulling all these concepts together, to specify a particular
method call on a particular object instance, a number of
nested components have to be named:
Address -> [Bus Name] -> Path -> Interface -> Method
The bus name is in brackets to indicate that it's optional -- you only
provide a name to route the method call to the right application
when using the bus daemon. If you have a direct connection to another
application, bus names aren't used; there's no bus daemon.
The interface is also optional, primarily for historical
reasons; DCOP does not require specifying the interface,
instead simply forbidding duplicate method names
on the same object instance. D-BUS will thus let you
omit the interface, but if your method name is ambiguous
it is undefined which method will be invoked.
GLib API: Using Remote Objects
The GLib binding is defined in the header file
<dbus/dbus-glib.h>.
D-BUS - GLib type mappings
The heart of the GLib bindings for D-BUS is the mapping it
provides between D-BUS "type signatures" and GLib types
(GType). The D-BUS type system is composed of
a number of "basic" types, along with several "container" types.
Basic type mappings
Below is a list of the basic types, along with their associated
mapping to a GType.
D-BUS basic type
GType
Free function
Notes
BYTE
G_TYPE_UCHAR
BOOLEAN
G_TYPE_BOOLEAN
INT16
G_TYPE_INT
Will be changed to a G_TYPE_INT16 once GLib has it
UINT16
G_TYPE_UINT
Will be changed to a G_TYPE_UINT16 once GLib has it
INT32
G_TYPE_INT
Will be changed to a G_TYPE_INT32 once GLib has it
UINT32
G_TYPE_UINT
Will be changed to a G_TYPE_UINT32 once GLib has it
INT64
G_TYPE_GINT64
UINT64
G_TYPE_GUINT64
DOUBLE
G_TYPE_DOUBLE
STRING
G_TYPE_STRING
g_free
OBJECT_PATH
DBUS_TYPE_G_PROXY
g_object_unref
The returned proxy does not have an interface set; use dbus_g_proxy_set_interface to invoke methods
As you can see, the basic mapping is fairly straightforward.
Container type mappings
The D-BUS type system also has a number of "container"
types, such as DBUS_TYPE_ARRAY and
DBUS_TYPE_STRUCT. The D-BUS type system
is fully recursive, so one can for example have an array of
array of strings (i.e. type signature
aas).
However, not all of these types are in common use; for
example, at the time of this writing the author knows of no
one using DBUS_TYPE_STRUCT, or a
DBUS_TYPE_ARRAY containing any non-basic
type. The approach the GLib bindings take is pragmatic; try
to map the most common types in the most obvious way, and
let using less common and more complex types be less
"natural".
First, D-BUS type signatures which have an "obvious"
corresponding builtin GLib type are mapped using that type:
D-BUS type signature
Description
GType
C typedef
Free function
Notes
as
Array of strings
G_TYPE_STRV
char **
g_strfreev
v
Generic value container
G_TYPE_VALUE
GValue *
g_value_unset
The calling conventions for values expect that method callers have allocated return values; see below.
The next most common recursive type signatures are arrays of
basic values. The most obvious mapping for arrays of basic
types is a GArray. Now, GLib does not
provide a builtin GType for
GArray. However, we actually need more than
that - we need a "parameterized" type which includes the
contained type. Why we need this we will see below.
The approach taken is to create these types in the D-BUS GLib
bindings; however, there is nothing D-BUS specific about them.
In the future, we hope to include such "fundamental" types in GLib
itself.
D-BUS type signature
Description
GType
C typedef
Free function
Notes
ay
Array of bytes
DBUS_TYPE_G_BYTE_ARRAY
GArray *
g_array_free
au
Array of uint
DBUS_TYPE_G_UINT_ARRAY
GArray *
g_array_free
ai
Array of int
DBUS_TYPE_G_INT_ARRAY
GArray *
g_array_free
ax
Array of int64
DBUS_TYPE_G_INT64_ARRAY
GArray *
g_array_free
at
Array of uint64
DBUS_TYPE_G_UINT64_ARRAY
GArray *
g_array_free
ad
Array of double
DBUS_TYPE_G_DOUBLE_ARRAY
GArray *
g_array_free
ab
Array of boolean
DBUS_TYPE_G_BOOLEAN_ARRAY
GArray *
g_array_free
D-BUS also includes a special type DBUS_TYPE_DICT_ENTRY which
is only valid in arrays. It's intended to be mapped to a "dictionary"
type by bindings. The obvious GLib mapping here is GHashTable. Again,
however, there is no builtin GType for a GHashTable.
Moreover, just like for arrays, we need a parameterized type so that
the bindings can communiate which types are contained in the hash table.
At present, only strings are supported. Work is in progress to
include more types.
D-BUS type signature
Description
GType
C typedef
Free function
Notes
a{ss}
Dictionary mapping strings to strings
DBUS_TYPE_G_STRING_STRING_HASHTABLE
GHashTable *
g_hash_table_destroy
Arbitrarily recursive type mappings
Finally, it is possible users will want to write or invoke D-BUS
methods which have arbitrarily complex type signatures not
directly supported by these bindings. For this case, we have a
DBusGValue which acts as a kind of special
variant value which may be iterated over manually. The
GType associated is
DBUS_TYPE_G_VALUE.
TODO insert usage of DBUS_TYPE_G_VALUE here.
A sample program
Here is a D-BUS program using the GLib bindings.
int
main (int argc, char **argv)
{
DBusGConnection *connection;
GError *error;
DBusGProxy *proxy;
char **name_list;
char **name_list_ptr;
g_type_init ();
error = NULL;
connection = dbus_g_bus_get (DBUS_BUS_SESSION,
&error);
if (connection == NULL)
{
g_printerr ("Failed to open connection to bus: %s\n",
error->message);
g_error_free (error);
exit (1);
}
/* Create a proxy object for the "bus driver" (name "org.freedesktop.DBus") */
proxy = dbus_g_proxy_new_for_name (connection,
DBUS_SERVICE_DBUS,
DBUS_PATH_DBUS,
DBUS_INTERFACE_DBUS);
/* Call ListNames method, wait for reply */
error = NULL;
if (!dbus_g_proxy_call (proxy, "ListNames", &error, G_TYPE_INVALID,
G_TYPE_STRV, &name_list, G_TYPE_INVALID))
{
/* Just do demonstrate remote exceptions versus regular GError */
if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION)
g_printerr ("Caught remote method exception %s: %s",
dbus_g_error_get_name (error),
error->message);
else
g_printerr ("Error: %s\n", error->message);
g_error_free (error);
exit (1);
}
/* Print the results */
g_print ("Names on the message bus:\n");
for (name_list_ptr = name_list; *name_list_ptr; name_list_ptr++)
{
g_print (" %s\n", *name_list_ptr);
}
g_strfreev (name_list);
g_object_unref (proxy);
return 0;
}
Program initalization
A connection to the bus is acquired using
dbus_g_bus_get. Next, a proxy
is created for the object "/org/freedesktop/DBus" with
interface org.freedesktop.DBus
on the service org.freedesktop.DBus.
This is a proxy for the message bus itself.
Understanding method invocation
You have a number of choices for method invocation. First, as
used above, dbus_g_proxy_call sends a
method call to the remote object, and blocks until reply is
recieved. The outgoing arguments are specified in the varargs
array, terminated with G_TYPE_INVALID.
Next, pointers to return values are specified, followed again
by G_TYPE_INVALID.
To invoke a method asynchronously, use
dbus_g_proxy_begin_call. This returns a
DBusGPendingCall object; you may then set a
notification function using
dbus_g_pending_call_set_notify.
Connecting to object signals
You may connect to signals using
dbus_g_proxy_add_signal and
dbus_g_proxy_connect_signal. You must
invoke dbus_g_proxy_add_signal to specify
the signature of your signal handlers; you may then invoke
dbus_g_proxy_connect_signal multiple times.
Note that it will often be the case that there is no builtin
marshaller for the type signature of a remote signal. In that
case, you must generate a marshaller yourself by using
glib-genmarshal, and then register
it using dbus_g_object_register_marshaller.
Error handling and remote exceptions
All of the GLib binding methods such as
dbus_g_proxy_end_call return a
GError. This GError can
represent two different things:
An internal D-BUS error, such as an out-of-memory
condition, an I/O error, or a network timeout. Errors
generated by the D-BUS library itself have the domain
DBUS_GERROR, and a corresponding code
such as DBUS_GERROR_NO_MEMORY. It will
not be typical for applications to handle these errors
specifically.
A remote D-BUS exception, thrown by the peer, bus, or
service. D-BUS remote exceptions have both a textual
"name" and a "message". The GLib bindings store this
information in the GError, but some
special rules apply.
The set error will have the domain
DBUS_GERROR as above, and will also
have the code
DBUS_GERROR_REMOTE_EXCEPTION. In order
to access the remote exception name, you must use a
special accessor, such as
dbus_g_error_has_name or
dbus_g_error_get_name. The remote
exception detailed message is accessible via the regular
GError message member.
More examples of method invocation
Sending an integer and string, receiving an array of bytes
GArray *arr;
error = NULL;
if (!dbus_g_proxy_call (proxy, "Foobar", &error,
G_TYPE_INT, 42, G_TYPE_STRING, "hello",
G_TYPE_INVALID,
DBUS_TYPE_G_UCHAR_ARRAY, &arr, G_TYPE_INVALID))
{
/* Handle error */
}
g_assert (arr != NULL);
printf ("got back %u values", arr->len);
Sending a GHashTable
GHashTable *hash = g_hash_table_new (g_str_hash, g_str_equal);
guint32 ret;
g_hash_table_insert (hash, "foo", "bar");
g_hash_table_insert (hash, "baz", "whee");
error = NULL;
if (!dbus_g_proxy_call (proxy, "HashSize", &error,
DBUS_TYPE_G_STRING_STRING_HASH, hash, G_TYPE_INVALID,
G_TYPE_UINT, &ret, G_TYPE_INVALID))
{
/* Handle error */
}
g_assert (ret == 2);
g_hash_table_destroy (hash);
Receiving a boolean and a string
gboolean boolret;
char *strret;
error = NULL;
if (!dbus_g_proxy_call (proxy, "GetStuff", &error,
G_TYPE_INVALID,
G_TYPE_BOOLEAN, &boolret,
G_TYPE_STRING, &strret,
G_TYPE_INVALID))
{
/* Handle error */
}
printf ("%s %s", boolret ? "TRUE" : "FALSE", strret);
g_free (strret);
Sending two arrays of strings
/* NULL terminate */
char *strs_static[] = {"foo", "bar", "baz", NULL};
/* Take pointer to array; cannot pass array directly */
char **strs_static_p = strs_static;
char **strs_dynamic;
strs_dynamic = g_new (char *, 4);
strs_dynamic[0] = g_strdup ("hello");
strs_dynamic[1] = g_strdup ("world");
strs_dynamic[2] = g_strdup ("!");
/* NULL terminate */
strs_dynamic[3] = NULL;
error = NULL;
if (!dbus_g_proxy_call (proxy, "TwoStrArrays", &error,
G_TYPE_STRV, strs_static_p,
G_TYPE_STRV, strs_dynamic,
G_TYPE_INVALID,
G_TYPE_INVALID))
{
/* Handle error */
}
g_strfreev (strs_dynamic);
Sending a boolean, receiving an array of strings
char **strs;
char **strs_p;
gboolean blah;
error = NULL;
blah = TRUE;
if (!dbus_g_proxy_call (proxy, "GetStrs", &error,
G_TYPE_BOOLEAN, blah,
G_TYPE_INVALID,
G_TYPE_STRV, &strs,
G_TYPE_INVALID))
{
/* Handle error */
}
for (strs_p = strs; *strs_p; strs_p++)
printf ("got string: \"%s\"", *strs_p);
g_strfreev (strs);
Sending a variant
GValue val = {0, };
g_value_init (&val, G_TYPE_STRING);
g_value_set_string (&val, "hello world");
error = NULL;
if (!dbus_g_proxy_call (proxy, "SendVariant", &error,
G_TYPE_VALUE, &val, G_TYPE_INVALID,
G_TYPE_INVALID))
{
/* Handle error */
}
g_assert (ret == 2);
g_value_unset (&val);
Receiving a variant
GValue val = {0, };
error = NULL;
if (!dbus_g_proxy_call (proxy, "GetVariant", &error, G_TYPE_INVALID,
G_TYPE_VALUE, &val, G_TYPE_INVALID))
{
/* Handle error */
}
if (G_VALUE_TYPE (&val) == G_TYPE_STRING)
printf ("%s\n", g_value_get_string (&val));
else if (G_VALUE_TYPE (&val) == G_TYPE_INT)
printf ("%d\n", g_value_get_int (&val));
else
...
g_value_unset (&val);
GLib API: Implementing Objects
At the moment, to expose a GObject via D-BUS, you must
write XML by hand which describes the methods exported
by the object. In the future, this manual step will
be obviated by the upcoming GLib introspection support.
Here is a sample XML file which describes an object that exposes
one method, named ManyArgs.
<?xml version="1.0" encoding="UTF-8" ?>
<node name="/com/example/MyObject">
<interface name="com.example.MyObject">
<annotation name="org.freedesktop.DBus.GLib.CSymbol" value="my_object"/>
<method name="ManyArgs">
<!-- This is optional, and in this case is redunundant -->
<annotation name="org.freedesktop.DBus.GLib.CSymbol" value="my_object_many_args"/>
<arg type="u" name="x" direction="in" />
<arg type="s" name="str" direction="in" />
<arg type="d" name="trouble" direction="in" />
<arg type="d" name="d_ret" direction="out" />
<arg type="s" name="str_ret" direction="out" />
</method>
</interface>
</node>
This XML is in the same format as the D-BUS introspection XML
format. Except we must include an "annotation" which give the C
symbols corresponding to the object implementation prefix
(my_object). In addition, if particular
methods symbol names deviate from C convention
(i.e. ManyArgs ->
many_args), you may specify an annotation
giving the C symbol.
Once you have written this XML, run dbus-binding-tool --mode=glib-server FILENAME > HEADER_NAME. to
generate a header file. For example: dbus-binding-tool --mode=glib-server my-objet.xml > my-object-glue.h.
Next, include the generated header in your program, and invoke
dbus_g_object_class_install_info, passing the
object class and "object info" included in the header. For
example:
dbus_g_object_type_install_info (COM_FOO_TYPE_MY_OBJECT, &com_foo_my_object_info);
This should be done exactly once per object class.
To actually implement the method, just define a C function named e.g.
my_object_many_args in the same file as the info
header is included. At the moment, it is required that this function
conform to the following rules:
The function must return a value of type gboolean;
TRUE on success, and FALSE
otherwise.
The first parameter is a pointer to an instance of the object.
Following the object instance pointer are the method
input values.
Following the input values are pointers to return values.
The final parameter must be a GError **.
If the function returns FALSE for an
error, the error parameter must be initalized with
g_set_error.
Finally, you can export an object using dbus_g_connection_register_g_object. For example:
dbus_g_connection_register_g_object (connection,
"/com/foo/MyObject",
obj);
Python API: Using Remote Objects
The Python bindings provide a simple to use interface for talking over D-BUS.
Where possible much of the inner-workings of D-BUS are hidden behind what looks
like normal Python objects.
D-BUS - Python type mappings
While python itself is a largely untyped language D-BUS provides a simple type system
for talking with other languages which may be strongly typed. Python for the most part
tries automatically map python objects to types on the bus. It is none the less good to
know what the type mappings are so one can better utilize services over the bus.
Basic type mappings
Below is a list of the basic types, along with their associated
mapping to a Python object.
D-BUS basic type
Python wrapper
Notes
BYTE
dbus.Byte
BOOLEAN
dbus.Boolean
Any variable assigned a True or False boolean value will automatically be converted into a BOOLEAN over the bus
INT16
dbus.Int16
UINT16
dbus.UInt16
INT32
dbus.Int32
This is the default mapping for Python integers
UINT32
dbus.UInt32
INT64
dbus.Int64
UINT64
dbus.UInt64
DOUBLE
dbus.Double
Any variable assigned a floating point number will automatically be converted into a DOUBLE over the bus
STRING
dbus.String
Any variable assigned a quoted string will automatically be converted into a STRING over the bus
OBJECT_PATH
dbus.ObjectPath
Container type mappings
The D-BUS type system also has a number of "container"
types, such as DBUS_TYPE_ARRAY and
DBUS_TYPE_STRUCT. The D-BUS type system
is fully recursive, so one can for example have an array of
array of strings (i.e. type signature
aas).
D-BUS container types have native corresponding built-in Python types
so it is easy to use them.
D-BUS type
Python type
Python wrapper
Notes
ARRAY
Python lists
dbus.Array
Python lists, denoted by square brackets [], are converted into arrays and visa versa.
The one restriction is that when sending a Python list each element of the list must be of the same
type. This is because D-BUS arrays can contain only one element type. Use Python tuples for mixed types.
When using the wrapper you may also specify a type or signature of the elements contained in the Array.
This is manditory when passing an empty Array to a method on the bus because Python can not guess at the
contents of an empty array. For example if a method is expecting an Array of int32's and you need to pass
it an empty Array you would do it as such:
emptyint32array = dbus.Array([], type=dbus.Int32)
or
emptyint32array = dbus.Array([], signature="i")
Note that dbus.Array derives from list so it acts just like a python list.
STRUCT
Python tuple
dbus.Struct
Python tuples, denoted by parentheses (,), are converted into structs and visa versa.
Tuples can have mixed types.
DICTIONARY
Python dictionary
dbus.Dictionary
D-BUS doesn't have an explicit dictionary type. Instead it uses LISTS of DICT_ENTRIES to
represent a dictionary. A DICT_ENTRY is simply a two element struct containing a key/value pair.
Python dictionaries are automatically converted to a LIST of DICT_ENTRIES and visa versa.
Since dictonaries are described as lists of dict_entries we also need the signature in order
to pass empty dictionaries. The wrapper provides a way of specifying this through the key_type/value_type
type parameters or the signature parameters. To send an empty Dictionary where the key is a string
and the value is a string you would do it as such:
emptystringstringdict = dbus.Dictionary({}, key_type=dbus.String, value_type=dbus.Value)
or
emptystringstringdict = dbus.Dictionary({}, signature="ss")
Note that dbus.Dictionary derives from dict so it acts just like a python dictionary.
VARIANT
any type
dbus.Variant
A variant is a container for any type. Python exports its methods to accept only variants
since we are an untyped language and can demarshal into any Python type.
To send a variant you must first wrap it in adbus.Variant. If no type or signiture is
given to the variant the marshaler will get the type from the contents.
Invoking Methods
Here is a D-BUS program using the Python bindings to get a listing of all names on the session bus.
import dbus
bus = dbus.SessionBus()
proxy_obj = bus.bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
dbus_iface = dbus.Interface(proxy_obj, 'org.freedesktop.DBus')
print dbus_iface.ListNames()
Notice I get an interface on the proxy object and use that to make the call. While the specifications
state that you do not need to specify an interface if the call is unambiguous (i.e. only one method implements
that name) due to a bug on the bus that drops messages which don't have an interface field you need to specify
interfaces at this time. In any event it is always good practice to specify the interface of the method you
wish to call to avoid any side effects should a method of the same name be implemented on another interface.
You can specify the interface for a single call using the dbus_interface keyword.
proxy_obj.ListNames(dbus_interface = 'org.freedesktop.DBus')
This is all fine and good if all you want to do is call methods on the bus and then exit. In order to
do more complex things such as use a GUI or make asynchronous calls you will need a mainloop. You would use
asynchronous calls because in GUI applications it is very bad to block for any long period of time. This cause
the GUI to seem to freeze. Since replies to D-BUS messages can take an indeterminate amount of time using async
calls allows you to return control to the GUI while you wait for the reply. This is exceedingly easy to do in
Python. Here is an example using the GLib/GTK+ mainloop.
import gobject
import dbus
if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
import dbus.glib
def print_list_names_reply(list):
print str(list)
def print_error(e):
print str(e)
bus = dbus.SessionBus()
proxy_obj = bus.bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
dbus_iface = dbus.Interface(proxy_obj, 'org.freedesktop.DBus')
dbus_iface.ListNames(reply_handler=print_list_names_reply, error_handler=print_error)
mainloop = gobject.MainLoop()
mainloop.run()
In the above listing you will notice the reply_handler and error_handler keywords. These tell the method that
it should be called async and to call print_list_names_reply or print_error depending if you get a reply or an error.
The signature for replys depends on the number of arguments being sent back. Error handlers always take one parameter
which is the error object returned.
You will also notice that I check the version of the dbus bindings before importing dbus.glib. In older versions
glib was the only available mainloop. As of version 0.41.0 we split out the glib dependency to allow for other mainloops
to be implemented. Notice also the python binding version does not match up with the D-BUS version. Once we reach 1.0
this should change with Python changes simply tracking the D-BUS changes.
While the glib mainloop is the only mainloop currently implemented, integrating other mainloops should
be very easy to do. There are plans for creating a a generic mainloop to be the default for non gui programs.
Listening for Signals
Signals are emitted by objects on the bus to notify listening programs that an event has occurred. There are a couple of ways
to register a signal handler on the bus. One way is to attach to an already created proxy using the connect_to_signal method
which takes a signal name and handler as arguments. Let us look at an example of connecting to the HAL service to receive
signals when devices are added and removed and when devices register a capability. This example assumes you have HAL already running.
import gobject
import dbus
if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
import dbus.glib
def device_added_callback(udi):
print 'Device with udi %s was added' % (udi)
def device_removed_callback(udi):
print 'Device with udi %s was added' % (udi)
def device_capability_callback(udi, capability):
print 'Device with udi %s added capability %s' % (udi, capability)
bus = dbus.SystemBus()
hal_manager_obj = bus.get_object('org.freedesktop.Hal',
'/org/freedesktop/Hal/Manager')
hal_manager = dbus.Interface(hal_manager_obj,
'org.freedesktop.Hal.Manager')
hal_manager.connect_to_signal('DeviceAdded', device_added_callback)
hal_manager.connect_to_signal('DeviceRemoved', device_removed_callback)
hal_manager.connect_to_signal('NewCapability', device_capability_callback)
mainloop = gobject.MainLoop()
mainloop.run()
The drawback of using this method is that the service that you are connecting to has to be around when you register
your signal handler. While HAL is guaranteed to be around on systems that use it this is not always the case for every
service on the bus. Say our program started up before HAL, we could connect to the signal by adding a signal receiver
directly to the bus.
bus.add_signal_receiver(device_added_callback,
'DeviceAdded',
'org.freedesktop.Hal.Manager',
'org.freedesktop.Hal',
'/org/freedesktop/Hal/Manager')
bus.add_signal_receiver(device_removed_callback,
'DeviceRemoved',
'org.freedesktop.Hal.Manager',
'org.freedesktop.Hal',
'/org/freedesktop/Hal/Manager')
bus.add_signal_receiver(device_capability_callback,
'DeviceAdded',
'org.freedesktop.Hal.Manager',
'org.freedesktop.Hal',
'/org/freedesktop/Hal/Manager')
All this can be done without creating the proxy object if one wanted to but in most cases you would want to have
a reference to the object so once a signal was received operations could be executed on the object.
Signal matching on arguments
Starting with D-Bus 0.36 and the (0, 43, 0) version of the python
bindings you can now add a match on arguments being sent in a signal.
This is useful for instance for only getting NameOwnerChanged
signals for your service. Lets say we create a name on the bus called
'org.foo.MyName' we could also add a match to just get
NameOwnerChanges for that name as such:
bus.add_signal_receiver(myname_changed,
'NameOwnerChanged',
'org.freedesktop.DBus',
'org.freedesktop.DBus',
'/org/freedesktop/DBus',
arg0='org.foo.MyName')
It is as simple as that. To match the second arg you would use arg1=,
the third arg2=, etc.
Cost of Creating a Proxy Object
Note that creating proxy objects can have an associated processing cost. When introspection is implemented
a proxy may wait for introspection data before processing any requests. It is generally good practice to
create proxies once and reuse the proxy when calling into the object. Constantly creating the same proxy
over and over again can become a bottleneck for your program.
TODO: example of getting information about devices from HAL
Python API: Implementing Objects
Implementing object on the bus is just as easy as invoking methods or listening for signals on the bus.
Version Alert
The Python D-BUS bindings require version 2.4 or greater of Python when creating D-BUS objects.
Inheriting From dbus.service.Object
In order to export a Python object over the bus one must first get a bus name and then create
a Python object that inherits from dbus.service.Object. The following is the start of an example
HelloWorld object that we want to export over the session bus.
import gobject
import dbus
import dbus.service
if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
import dbus.glib
class HelloWorldObject(dbus.service.Object):
def __init__(self, bus_name, object_path='/org/freedesktop/HelloWorldObject'):
dbus.service.Object.__init__(self, bus_name, object_path)
session_bus = dbus.SessionBus()
bus_name = dbus.service.BusName('org.freedesktop.HelloWorld', bus=session_bus)
object = HelloWorldObject(bus_name)
mainloop = gobject.MainLoop()
mainloop.run()
Here we got the session bus, then created a BusName object which requests a name on the bus.
We pass that bus name to the HelloWorldObject object which inherits from dbus.service.Object.
We now have an object on the bus but it is pretty useless.
Exporting Methods Over The Bus
Let's make this object do something and export a method over the bus.
import gobject
import dbus
import dbus.service
if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
import dbus.glib
class HelloWorldObject(dbus.service.Object):
def __init__(self, bus_name, object_path='/org/freedesktop/HelloWorldObject'):
dbus.service.Object.__init__(self, bus_name, object_path)
@dbus.service.method('org.freedesktop.HelloWorldIFace')
def hello(self):
return 'Hello from the HelloWorldObject'
session_bus = dbus.SessionBus()
bus_name = dbus.service.BusName('org.freedesktop.HelloWorld', bus=session_bus)
object = HelloWorldObject(bus_name)
mainloop = gobject.MainLoop()
mainloop.run()
Python Decorators
Notice the @ symbol on the line before the hello method. This is a new directive introduced in
Python 2.4. It is called a decorator and it "decorates" methods. All you have to know is that
it provides metadata that can then be used to alter the behavior of the method being decorated.
In this case we are telling the bindings that the hello method should be exported as a D-BUS method
over the bus.
As you can see we exported the hello method as part of the org.freedesktop.HelloWorldIFace interface.
It takes no arguments and returns a string to the calling program. Let's create a proxy and invoke this
method.
import dbus
bus = dbus.SessionBus()
proxy_obj = bus.bus.get_object('org.freedesktop.HelloWorld', '/org/freedesktop/HelloWorldObject')
iface = dbus.Interface(proxy_obj, 'org.freedesktop.HelloWorldIFace')
print iface.hello()
When invoking methods exported over the bus the bindings automatically know how many parameters
the method exports. You can even make a method that exports an arbitrary number of parameters.
Also, whatever you return will automatically be transfered as a reply over the bus. Some examples.
@dbus.service.method('org.freedesktop.HelloWorldIFace')
def one_arg(self, first_arg):
return 'I got arg %s' % first_arg
@dbus.service.method('org.freedesktop.HelloWorldIFace')
def two_args(self, first_arg, second_arg):
return ('I got 2 args', first_arg, second_arg)
@dbus.service.method('org.freedesktop.HelloWorldIFace')
def return_list(self):
return [1, 2, 3, 4, 5, 6]
@dbus.service.method('org.freedesktop.HelloWorldIFace')
def return_dict(self):
return {one: '1ne', two: '2wo', three: '3ree'}
Emitting Signals
Setting up signals to emit is just as easy as exporting methods. It uses the same syntax as methods.
import gobject
import dbus
import dbus.service
if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
import dbus.glib
class HelloWorldObject(dbus.service.Object):
def __init__(self, bus_name, object_path='/org/freedesktop/HelloWorldObject'):
dbus.service.Object.__init__(self, bus_name, object_path)
@dbus.service.method('org.freedesktop.HelloWorldIFace')
def hello(self):
return 'Hello from the HelloWorldObject'
@dbus.service.signal('org.freedesktop.HelloWorldIFace')
def hello_signal(self, message):
pass
session_bus = dbus.SessionBus()
bus_name = dbus.service.BusName('org.freedesktop.HelloWorld', bus=session_bus)
object = HelloWorldObject(bus_name)
object.hello_signal('I sent a hello signal')
mainloop = gobject.MainLoop()
mainloop.run()
Adding a @dbus.service.signal decorator to a method turns it into a signal emitter. You can put code
in this method to do things like keep track of how many times you call the emitter or to print out debug
messages but for the most part a pass noop will do. Whenever you call the emitter a signal will be emitted
with the parameters you passed in as arguments. In the above example we send the message 'I sent a hello signal'
with the signal.
Inheriting from HelloWorldObject
One of the cool things you can do in Python is inherit from another D-BUS object. We use this trick in
the bindings to provide a default implementation for the org.freedesktop.DBus.Introspectable interface.
Let's inherit from the HelloWorldObject example above and overide the hello method to say goodbye.
class HelloWorldGoodbyeObject(HelloWorldObject):
def __init__(self, bus_name, object_path='/org/freedesktop/HelloWorldGoodbyeObject'):
HelloWorldObject.__init__(self, bus_name, object_path)
@dbus.service.method('org.freedesktop.HelloWorldGoodbyeIFace')
def hello(self):
return 'Goodbye'
goodbye_object = HelloWorldGoodbyeObject(bus_name)
Let's now call both methods with a little help from interfaces.
import dbus
bus = dbus.SessionBus()
proxy_obj = bus.bus.get_object('org.freedesktop.HelloWorld', '/org/freedesktop/HelloWorldGoodbyeObject')
print proxy_obj.hello(dbus_interface='org.freedesktop.HelloWorldIFace')
print proxy_obj.hello(dbus_interface='org.freedesktop.HelloWorldGoodbyeIFace')
This should print out 'Hello from the HelloWorldObject' followed by a 'Goodbye'.
Conclusion
As you can see, using D-BUS from Python is an extremely easy proposition. Hopefully
the tutorial has been helpful in getting you started. If you need anymore help please
feel free to post on the mailing list.
The Python bindings are still in a state of flux and there may be API changes in the future.
This tutorial will be updated if such changes occur.
Qt API: Using Remote Objects
The Qt bindings are not yet documented.
Qt API: Implementing Objects
The Qt bindings are not yet documented.