1 <?xml version="1.0" standalone="no"?>
2 <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd"
9 <title>D-BUS Tutorial</title>
10 <releaseinfo>Version 0.3</releaseinfo>
11 <date>18 January 2005</date>
14 <firstname>Havoc</firstname>
15 <surname>Pennington</surname>
17 <orgname>Red Hat, Inc.</orgname>
19 <email>hp@pobox.com</email>
24 <firstname>David</firstname>
25 <surname>Wheeler</surname>
31 <title>What is D-BUS?</title>
33 D-BUS is a system for <firstterm>interprocess communication</firstterm>
34 (IPC). Architecturally, it has several layers:
39 A library, <firstterm>libdbus</firstterm>, that allows two
40 applications to connect to each other and exchange messages.
45 A <firstterm>message bus daemon</firstterm> executable, built on
46 libdbus, that multiple applications can connect to. The daemon can
47 route messages from one application to zero or more other
53 <firstterm>Wrapper libraries</firstterm> based on particular
54 application frameworks. For example, libdbus-glib and
55 libdbus-qt. There are also bindings to languages such as
56 Python. These wrapper libraries are the API most people should use,
57 as they simplify the details of D-BUS programming. libdbus is
58 intended to be a low-level backend for the higher level bindings.
59 Much of the libdbus API is only useful for binding implementation.
66 If you just want to use D-BUS and don't care how it works, jump directly
67 to <xref linkend="concepts"/>.
72 libdbus only supports one-to-one connections, just like a raw network
73 socket. However, rather than sending byte streams over the connection, you
74 send <firstterm>messages</firstterm>. Messages have a header identifying
75 the kind of message, and a body containing a data payload. libdbus also
76 abstracts the exact transport used (sockets vs. whatever else), and
77 handles details such as authentication.
81 The message bus daemon forms the hub of a wheel. Each spoke of the wheel
82 is a one-to-one connection to an application using libdbus. An
83 application sends a message to the bus daemon over its spoke, and the bus
84 daemon forwards the message to other connected applications as
85 appropriate. Think of the daemon as a router.
89 The bus daemon has multiple instances on a typical computer. The
90 first instance is a machine-global singleton, that is, a system daemon
91 similar to sendmail or Apache. This instance has heavy security
92 restrictions on what messages it will accept, and is used for systemwide
93 communication. The other instances are created one per user login session.
94 These instances allow applications in the user's session to communicate
99 The systemwide and per-user daemons are separate. Normal within-session
100 IPC does not involve the systemwide message bus process and vice versa.
104 <title>D-BUS applications</title>
106 There are many, many technologies in the world that have "Inter-process
107 communication" or "networking" in their stated purpose: <ulink
108 url="http://www.omg.org">CORBA</ulink>, <ulink
109 url="http://www.opengroup.org/dce/">DCE</ulink>, <ulink
110 url="http://www.microsoft.com/com/">DCOM</ulink>, <ulink
111 url="http://developer.kde.org/documentation/library/kdeqt/dcop.html">DCOP</ulink>, <ulink
112 url="http://www.xmlrpc.com">XML-RPC</ulink>, <ulink
113 url="http://www.w3.org/TR/SOAP/">SOAP</ulink>, <ulink
114 url="http://www.mbus.org/">MBUS</ulink>, <ulink
115 url="http://www.zeroc.com/ice.html">Internet Communications Engine (ICE)</ulink>,
116 and probably hundreds more.
117 Each of these is tailored for particular kinds of application.
118 D-BUS is designed for two specific cases:
122 Communication between desktop applications in the same desktop
123 session; to allow integration of the desktop session as a whole,
124 and address issues of process lifecycle (when do desktop components
125 start and stop running).
130 Communication between the desktop session and the operating system,
131 where the operating system would typically include the kernel
132 and any system daemons or processes.
138 For the within-desktop-session use case, the GNOME and KDE desktops
139 have significant previous experience with different IPC solutions
140 such as CORBA and DCOP. D-BUS is built on that experience and
141 carefully tailored to meet the needs of these desktop projects
142 in particular. D-BUS may or may not be appropriate for other
143 applications; the FAQ has some comparisons to other IPC systems.
146 The problem solved by the systemwide or communication-with-the-OS case
147 is explained well by the following text from the Linux Hotplug project:
150 A gap in current Linux support is that policies with any sort of
151 dynamic "interact with user" component aren't currently
152 supported. For example, that's often needed the first time a network
153 adapter or printer is connected, and to determine appropriate places
154 to mount disk drives. It would seem that such actions could be
155 supported for any case where a responsible human can be identified:
156 single user workstations, or any system which is remotely
161 This is a classic "remote sysadmin" problem, where in this case
162 hotplugging needs to deliver an event from one security domain
163 (operating system kernel, in this case) to another (desktop for
164 logged-in user, or remote sysadmin). Any effective response must go
165 the other way: the remote domain taking some action that lets the
166 kernel expose the desired device capabilities. (The action can often
167 be taken asynchronously, for example letting new hardware be idle
168 until a meeting finishes.) At this writing, Linux doesn't have
169 widely adopted solutions to such problems. However, the new D-Bus
170 work may begin to solve that problem.
175 D-BUS may happen to be useful for purposes other than the one it was
176 designed for. Its general properties that distinguish it from
177 other forms of IPC are:
181 Binary protocol designed to be used asynchronously
182 (similar in spirit to the X Window System protocol).
187 Stateful, reliable connections held open over time.
192 The message bus is a daemon, not a "swarm" or
193 distributed architecture.
198 Many implementation and deployment issues are specified rather
204 Semantics are similar to the existing DCOP system, allowing
205 KDE to adopt it more easily.
210 Security features to support the systemwide mode of the
218 <sect1 id="concepts">
219 <title>Concepts</title>
221 Some basic concepts apply no matter what application framework you're
222 using to write a D-BUS application. The exact code you write will be
223 different for GLib vs. Qt vs. Python applications, however.
227 Here is a diagram (<ulink url="diagram.png">png</ulink> <ulink
228 url="diagram.svg">svg</ulink>) that may help you visualize the concepts
233 <title>Objects and Object Paths</title>
235 Each application using D-BUS contains <firstterm>objects</firstterm>,
236 which generally map to GObject, QObject, C++ objects, or Python objects
237 (but need not). An object is an <emphasis>instance</emphasis> rather
238 than a type. When messages are received over a D-BUS connection, they
239 are sent to a specific object, not to the application as a whole.
242 To allow messages to specify their destination object, there has to be a
243 way to refer to an object. In your favorite programming language, this
244 is normally called a <firstterm>pointer</firstterm> or
245 <firstterm>reference</firstterm>. However, these references are
246 implemented as memory addresses relative to the address space of your
247 application, and thus can't be passed from one application to another.
250 To solve this, D-BUS introduces a name for each object. The name
251 looks like a filesystem path, for example an object could be
252 named <literal>/org/kde/kspread/sheets/3/cells/4/5</literal>.
253 Human-readable paths are nice, but you are free to create an
254 object named <literal>/com/mycompany/c5yo817y0c1y1c5b</literal>
255 if it makes sense for your application.
258 Namespacing object paths is smart, by starting them with the components
259 of a domain name you own (e.g. <literal>/org/kde</literal>). This
260 keeps different code modules in the same process from stepping
261 on one another's toes.
265 <sect2 id="interfaces">
266 <title>Interfaces</title>
268 Each object supports one or more <firstterm>interfaces</firstterm>.
269 Think of an interface as a named group of methods and signals,
270 just as it is in GLib or Qt or Java. Interfaces define the
271 <emphasis>type</emphasis> of an object instance.
275 <sect2 id="messages">
276 <title>Message Types</title>
278 Messages are not all the same; in particular, D-BUS has
279 4 built-in message types:
283 Method call messages ask to invoke a method
289 Method return messages return the results
290 of invoking a method.
295 Error messages return an exception caused by
301 Signal messages are notifications that a given signal
302 has been emitted (that an event has occurred).
303 You could also think of these as "event" messages.
309 A method call maps very simply to messages, then: you send a method call
310 message, and receive either a method return message or an error message
315 <sect2 id="bus-names">
316 <title>Bus Names</title>
319 Object paths, interfaces, and messages exist on the level of
320 libdbus and the D-BUS protocol; they are used even in the
321 1-to-1 case with no message bus involved.
325 Bus names, on the other hand, are a property of the message bus daemon.
326 The bus maintains a mapping from names to message bus connections.
327 These names are used to specify the origin and destination
328 of messages passing through the message bus. When a name is mapped
329 to a particular application's connection, that application is said to
330 <firstterm>own</firstterm> that name.
334 On connecting to the bus daemon, each application immediately owns a
335 special name called the <firstterm>unique connection name</firstterm>.
336 A unique name begins with a ':' (colon) character; no other names are
337 allowed to begin with that character. Unique names are special because
338 they are created dynamically, and are never re-used during the lifetime
339 of the same bus daemon. You know that a given unique name will have the
340 same owner at all times. An example of a unique name might be
341 <literal>:34-907</literal>. The numbers after the colon have
342 no meaning other than their uniqueness.
346 Applications may ask to own additional <firstterm>well-known
347 names</firstterm>. For example, you could write a specification to
348 define a name called <literal>com.mycompany.TextEditor</literal>.
349 Your definition could specify that to own this name, an application
350 should have an object at the path
351 <literal>/com/mycompany/TextFileManager</literal> supporting the
352 interface <literal>org.freedesktop.FileHandler</literal>.
356 Applications could then send messages to this bus name,
357 object, and interface to execute method calls.
361 You could think of the unique names as IP addresses, and the
362 well-known names as domain names. So
363 <literal>com.mycompany.TextEditor</literal> might map to something like
364 <literal>:34-907</literal> just as <literal>mycompany.com</literal> maps
365 to something like <literal>192.168.0.5</literal>.
369 Names have a second important use, other than routing messages. They
370 are used to track lifecycle. When an application exits (or crashes), its
371 connection to the message bus will be closed by the operating system
372 kernel. The message bus then sends out notification messages telling
373 remaining applications that the application's names have lost their
374 owner. By tracking these notifications, your application can reliably
375 monitor the lifetime of other applications.
380 <sect2 id="addresses">
381 <title>Addresses</title>
384 Applications using D-BUS are either servers or clients. A server
385 listens for incoming connections; a client connects to a server. Once
386 the connection is established, it is a symmetric flow of messages; the
387 client-server distinction only matters when setting up the
392 A D-BUS <firstterm>address</firstterm> specifies where a server will
393 listen, and where a client will connect. For example, the address
394 <literal>unix:path=/tmp/abcdef</literal> specifies that the server will
395 listen on a UNIX domain socket at the path
396 <literal>/tmp/abcdef</literal> and the client will connect to that
397 socket. An address can also specify TCP/IP sockets, or any other
398 transport defined in future iterations of the D-BUS specification.
402 When using D-BUS with a message bus, the bus daemon is a server
403 and all other applications are clients of the bus daemon.
404 libdbus automatically discovers the address of the per-session bus
405 daemon by reading an environment variable. It discovers the
406 systemwide bus daemon by checking a well-known UNIX domain socket path
407 (though you can override this address with an environment variable).
411 If you're using D-BUS without a bus daemon, it's up to you to
412 define which application will be the server and which will be
413 the client, and specify a mechanism for them to agree on
414 the server's address.
419 <sect2 id="bigpicture">
420 <title>Big Conceptual Picture</title>
423 Pulling all these concepts together, to specify a particular
424 method call on a particular object instance, a number of
425 nested components have to be named:
427 Address -> [Bus Name] -> Path -> Interface -> Method
429 The bus name is in brackets to indicate that it's optional -- you only
430 provide a name to route the method call to the right application
431 when using the bus daemon. If you have a direct connection to another
432 application, bus names aren't used; there's no bus daemon.
436 The interface is also optional, primarily for historical
437 reasons; DCOP does not require specifying the interface,
438 instead simply forbidding duplicate method names
439 on the same object instance. D-BUS will thus let you
440 omit the interface, but if your method name is ambiguous
441 it is undefined which method will be invoked.
448 <sect1 id="glib-client">
449 <title>GLib API: Using Remote Objects</title>
452 The GLib binding is defined in the header file
453 <dbus/dbus-glib.h>.
456 <sect2 id="glib-typemappings">
457 <title>D-BUS - GLib type mappings</title>
459 The heart of the GLib bindings for D-BUS is the mapping it
460 provides between D-BUS "type signatures" and GLib types
461 (<literal>GType</literal>). The D-BUS type system is composed of
462 a number of "basic" types, along with several "container" types.
464 <sect3 id="glib-basic-typemappings">
465 <title>Basic type mappings</title>
467 Below is a list of the basic types, along with their associated
468 mapping to a <literal>GType</literal>.
473 <entry>D-BUS basic type</entry>
475 <entry>Free function</entry>
481 <entry><literal>BYTE</literal></entry>
482 <entry><literal>G_TYPE_UCHAR</literal></entry>
486 <entry><literal>BOOLEAN</literal></entry>
487 <entry><literal>G_TYPE_BOOLEAN</literal></entry>
491 <entry><literal>INT16</literal></entry>
492 <entry><literal>G_TYPE_INT</literal></entry>
494 <entry>Will be changed to a G_TYPE_INT16 once GLib has it</entry>
496 <entry><literal>UINT16</literal></entry>
497 <entry><literal>G_TYPE_UINT</literal></entry>
499 <entry>Will be changed to a G_TYPE_UINT16 once GLib has it</entry>
501 <entry><literal>INT32</literal></entry>
502 <entry><literal>G_TYPE_INT</literal></entry>
504 <entry>Will be changed to a G_TYPE_INT32 once GLib has it</entry>
506 <entry><literal>UINT32</literal></entry>
507 <entry><literal>G_TYPE_UINT</literal></entry>
509 <entry>Will be changed to a G_TYPE_UINT32 once GLib has it</entry>
511 <entry><literal>INT64</literal></entry>
512 <entry><literal>G_TYPE_GINT64</literal></entry>
516 <entry><literal>UINT64</literal></entry>
517 <entry><literal>G_TYPE_GUINT64</literal></entry>
521 <entry><literal>DOUBLE</literal></entry>
522 <entry><literal>G_TYPE_DOUBLE</literal></entry>
526 <entry><literal>STRING</literal></entry>
527 <entry><literal>G_TYPE_STRING</literal></entry>
528 <entry>g_free</entry>
531 <entry><literal>OBJECT_PATH</literal></entry>
532 <entry><literal>DBUS_TYPE_G_PROXY</literal></entry>
533 <entry>g_object_unref</entry>
534 <entry>The returned proxy does not have an interface set; use <literal>dbus_g_proxy_set_interface</literal> to invoke methods</entry>
539 As you can see, the basic mapping is fairly straightforward.
542 <sect3 id="glib-container-typemappings">
543 <title>Container type mappings</title>
545 The D-BUS type system also has a number of "container"
546 types, such as <literal>DBUS_TYPE_ARRAY</literal> and
547 <literal>DBUS_TYPE_STRUCT</literal>. The D-BUS type system
548 is fully recursive, so one can for example have an array of
549 array of strings (i.e. type signature
550 <literal>aas</literal>).
553 However, not all of these types are in common use; for
554 example, at the time of this writing the author knows of no
555 one using <literal>DBUS_TYPE_STRUCT</literal>, or a
556 <literal>DBUS_TYPE_ARRAY</literal> containing any non-basic
557 type. The approach the GLib bindings take is pragmatic; try
558 to map the most common types in the most obvious way, and
559 let using less common and more complex types be less
563 First, D-BUS type signatures which have an "obvious"
564 corresponding builtin GLib type are mapped using that type:
569 <entry>D-BUS type signature</entry>
570 <entry>Description</entry>
572 <entry>C typedef</entry>
573 <entry>Free function</entry>
579 <entry><literal>as</literal></entry>
580 <entry>Array of strings</entry>
581 <entry><literal>G_TYPE_STRV</literal></entry>
582 <entry><literal>char **</literal></entry>
583 <entry>g_strfreev</entry>
586 <entry><literal>v</literal></entry>
587 <entry>Generic value container</entry>
588 <entry><literal>G_TYPE_VALUE</literal></entry>
589 <entry><literal>GValue *</literal></entry>
590 <entry>g_value_unset</entry>
591 <entry>The calling conventions for values expect that method callers have allocated return values; see below.</entry>
598 The next most common recursive type signatures are arrays of
599 basic values. The most obvious mapping for arrays of basic
600 types is a <literal>GArray</literal>. Now, GLib does not
601 provide a builtin <literal>GType</literal> for
602 <literal>GArray</literal>. However, we actually need more than
603 that - we need a "parameterized" type which includes the
604 contained type. Why we need this we will see below.
607 The approach taken is to create these types in the D-BUS GLib
608 bindings; however, there is nothing D-BUS specific about them.
609 In the future, we hope to include such "fundamental" types in GLib
615 <entry>D-BUS type signature</entry>
616 <entry>Description</entry>
618 <entry>C typedef</entry>
619 <entry>Free function</entry>
625 <entry><literal>ay</literal></entry>
626 <entry>Array of bytes</entry>
627 <entry><literal>DBUS_TYPE_G_BYTE_ARRAY</literal></entry>
628 <entry><literal>GArray *</literal></entry>
629 <entry>g_array_free</entry>
633 <entry><literal>au</literal></entry>
634 <entry>Array of uint</entry>
635 <entry><literal>DBUS_TYPE_G_UINT_ARRAY</literal></entry>
636 <entry><literal>GArray *</literal></entry>
637 <entry>g_array_free</entry>
641 <entry><literal>ai</literal></entry>
642 <entry>Array of int</entry>
643 <entry><literal>DBUS_TYPE_G_INT_ARRAY</literal></entry>
644 <entry><literal>GArray *</literal></entry>
645 <entry>g_array_free</entry>
649 <entry><literal>ax</literal></entry>
650 <entry>Array of int64</entry>
651 <entry><literal>DBUS_TYPE_G_INT64_ARRAY</literal></entry>
652 <entry><literal>GArray *</literal></entry>
653 <entry>g_array_free</entry>
657 <entry><literal>at</literal></entry>
658 <entry>Array of uint64</entry>
659 <entry><literal>DBUS_TYPE_G_UINT64_ARRAY</literal></entry>
660 <entry><literal>GArray *</literal></entry>
661 <entry>g_array_free</entry>
665 <entry><literal>ad</literal></entry>
666 <entry>Array of double</entry>
667 <entry><literal>DBUS_TYPE_G_DOUBLE_ARRAY</literal></entry>
668 <entry><literal>GArray *</literal></entry>
669 <entry>g_array_free</entry>
673 <entry><literal>ab</literal></entry>
674 <entry>Array of boolean</entry>
675 <entry><literal>DBUS_TYPE_G_BOOLEAN_ARRAY</literal></entry>
676 <entry><literal>GArray *</literal></entry>
677 <entry>g_array_free</entry>
685 D-BUS also includes a special type DBUS_TYPE_DICT_ENTRY which
686 is only valid in arrays. It's intended to be mapped to a "dictionary"
687 type by bindings. The obvious GLib mapping here is GHashTable. Again,
688 however, there is no builtin <literal>GType</literal> for a GHashTable.
689 Moreover, just like for arrays, we need a parameterized type so that
690 the bindings can communiate which types are contained in the hash table.
693 At present, only strings are supported. Work is in progress to
699 <entry>D-BUS type signature</entry>
700 <entry>Description</entry>
702 <entry>C typedef</entry>
703 <entry>Free function</entry>
709 <entry><literal>a{ss}</literal></entry>
710 <entry>Dictionary mapping strings to strings</entry>
711 <entry><literal>DBUS_TYPE_G_STRING_STRING_HASHTABLE</literal></entry>
712 <entry><literal>GHashTable *</literal></entry>
713 <entry>g_hash_table_destroy</entry>
721 <sect3 id="glib-generic-typemappings">
722 <title>Arbitrarily recursive type mappings</title>
724 Finally, it is possible users will want to write or invoke D-BUS
725 methods which have arbitrarily complex type signatures not
726 directly supported by these bindings. For this case, we have a
727 <literal>DBusGValue</literal> which acts as a kind of special
728 variant value which may be iterated over manually. The
729 <literal>GType</literal> associated is
730 <literal>DBUS_TYPE_G_VALUE</literal>.
733 TODO insert usage of <literal>DBUS_TYPE_G_VALUE</literal> here.
737 <sect2 id="sample-program-1">
738 <title>A sample program</title>
739 <para>Here is a D-BUS program using the GLib bindings.
742 main (int argc, char **argv)
744 DBusGConnection *connection;
748 char **name_list_ptr;
753 connection = dbus_g_bus_get (DBUS_BUS_SESSION,
755 if (connection == NULL)
757 g_printerr ("Failed to open connection to bus: %s\n",
759 g_error_free (error);
763 /* Create a proxy object for the "bus driver" (name "org.freedesktop.DBus") */
765 proxy = dbus_g_proxy_new_for_name (connection,
766 DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
767 DBUS_PATH_ORG_FREEDESKTOP_DBUS,
768 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS);
770 /* Call ListNames method, wait for reply */
772 if (!dbus_g_proxy_invoke (proxy, "ListNames", &error, G_TYPE_INVALID,
773 G_TYPE_STRV, &name_list, G_TYPE_INVALID))
775 g_printerr ("Failed to complete ListNames call: %s\n",
777 g_error_free (error);
781 /* Print the results */
783 g_print ("Names on the message bus:\n");
785 for (name_list_ptr = name_list; *name_list_ptr; name_list_ptr++)
787 g_print (" %s\n", *name_list_ptr);
789 g_strfreev (name_list);
791 g_object_unref (proxy);
798 <sect2 id="glib-program-setup">
799 <title>Program initalization</title>
801 A connection to the bus is acquired using
802 <literal>dbus_g_bus_get</literal>. Next, a proxy
803 is created for the object "/org/freedesktop/DBus" with
804 interface <literal>org.freedesktop.DBus</literal>
805 on the service <literal>org.freedesktop.DBus</literal>.
806 This is a proxy for the message bus itself.
809 <sect2 id="glib-method-invocation">
810 <title>Understanding method invocation</title>
812 You have a number of choices for method invocation. First, as
813 used above, <literal>dbus_g_proxy_invoke</literal> sends a
814 method call to the remote object, and blocks until reply is
815 recieved. The outgoing arguments are specified in the varargs
816 array, terminated with <literal>G_TYPE_INVALID</literal>.
817 Next, pointers to return values are specified, followed again
818 by <literal>G_TYPE_INVALID</literal>.
821 To invoke a method asynchronously, use
822 <literal>dbus_g_proxy_begin_call</literal>. This returns a
823 <literal>DBusGPendingCall</literal> object; you may then set a
824 notification function using
825 <literal>dbus_g_pending_call_set_notify</literal>.
828 <sect2 id="glib-signal-connection">
829 <title>Connecting to object signals</title>
831 You may connect to signals using
832 <literal>dbus_g_proxy_add_signal</literal> and
833 <literal>dbus_g_proxy_connect_signal</literal>. At the
834 moment, <literal>dbus_g_proxy_add_signal</literal> requires
835 the D-BUS types of the remote object; this will likely be
839 <sect2 id="glib-more-examples">
840 <title>More examples of method invocation</title>
841 <sect3 id="glib-sending-stuff">
842 <title>Sending an integer and string, receiving an array of bytes</title>
848 if (!dbus_g_proxy_invoke (proxy, "Foobar", &error,
849 G_TYPE_INT, 42, G_TYPE_STRING, "hello",
851 DBUS_TYPE_G_UCHAR_ARRAY, &arr, G_TYPE_INVALID))
853 g_printerr ("Failed to complete Foobar: %s\n",
855 g_error_free (error);
858 g_assert (arr != NULL);
859 printf ("got back %u values", arr->len);
863 <sect3 id="glib-sending-hash">
864 <title>Sending a GHashTable</title>
867 GHashTable *hash = g_hash_table_new (g_str_hash, g_str_equal);
870 g_hash_table_insert (hash, "foo", "bar");
871 g_hash_table_insert (hash, "baz", "whee");
874 if (!dbus_g_proxy_invoke (proxy, "HashSize", &error,
875 DBUS_TYPE_G_STRING_STRING_HASH, hash, G_TYPE_INVALID,
876 G_TYPE_UINT, &ret, G_TYPE_INVALID))
878 g_printerr ("Failed to complete HashSize: %s\n",
880 g_error_free (error);
884 g_hash_table_destroy (hash);
888 <sect3 id="glib-receiving-bool-int">
889 <title>Receiving a boolean and a string</title>
896 if (!dbus_g_proxy_invoke (proxy, "GetStuff", &error,
898 G_TYPE_BOOLEAN, &boolret,
899 G_TYPE_STRING, &strret,
902 g_printerr ("Failed to complete GetStuff: %s\n",
904 g_error_free (error);
907 printf ("%s %s", boolret ? "TRUE" : "FALSE", strret);
912 <sect3 id="glib-sending-str-arrays">
913 <title>Sending two arrays of strings</title>
917 char *strs_static[] = {"foo", "bar", "baz", NULL};
918 /* Take pointer to array; cannot pass array directly */
919 char **strs_static_p = strs_static;
922 strs_dynamic = g_new (char *, 4);
923 strs_dynamic[0] = g_strdup ("hello");
924 strs_dynamic[1] = g_strdup ("world");
925 strs_dynamic[2] = g_strdup ("!");
927 strs_dynamic[3] = NULL;
930 if (!dbus_g_proxy_invoke (proxy, "TwoStrArrays", &error,
931 G_TYPE_STRV, strs_static_p,
932 G_TYPE_STRV, strs_dynamic,
936 g_printerr ("Failed to complete TwoStrArrays: %s\n",
938 g_error_free (error);
941 g_strfreev (strs_dynamic);
945 <sect3 id="glib-getting-str-array">
946 <title>Sending a boolean, receiving an array of strings</title>
955 if (!dbus_g_proxy_invoke (proxy, "GetStrs", &error,
956 G_TYPE_BOOLEAN, blah,
958 G_TYPE_STRV, &strs,
961 g_printerr ("Failed to complete GetStrs: %s\n",
963 g_error_free (error);
966 for (strs_p = strs; *strs_p; strs_p++)
967 printf ("got string: \"%s\"", *strs_p);
972 <sect3 id="glib-sending-variant">
973 <title>Sending a variant</title>
978 g_value_init (&val, G_TYPE_STRING);
979 g_value_set_string (&val, "hello world");
982 if (!dbus_g_proxy_invoke (proxy, "SendVariant", &error,
983 G_TYPE_VALUE, &val, G_TYPE_INVALID,
986 g_printerr ("Failed to complete SendVariant: %s\n",
988 g_error_free (error);
992 g_value_unset (&val);
996 <sect3 id="glib-receiving-variant">
997 <title>Receiving a variant</title>
1003 if (!dbus_g_proxy_invoke (proxy, "GetVariant", &error, G_TYPE_INVALID,
1004 G_TYPE_VALUE, &val, G_TYPE_INVALID))
1006 g_printerr ("Failed to complete GetVariant: %s\n",
1008 g_error_free (error);
1011 if (G_VALUE_TYPE (&val) == G_TYPE_STRING)
1012 printf ("%s\n", g_value_get_string (&val));
1013 else if (G_VALUE_TYPE (&val) == G_TYPE_INT)
1014 printf ("%d\n", g_value_get_int (&val));
1017 g_value_unset (&val);
1024 <sect1 id="glib-server">
1025 <title>GLib API: Implementing Objects</title>
1027 At the moment, to expose a GObject via D-BUS, you must
1028 write XML by hand which describes the methods exported
1029 by the object. In the future, this manual step will
1030 be obviated by the upcoming GLib introspection support.
1033 Here is a sample XML file which describes an object that exposes
1034 one method, named <literal>ManyArgs</literal>.
1036 <?xml version="1.0" encoding="UTF-8" ?>
1038 <node name="/com/example/MyObject">
1040 <interface name="com.example.MyObject">
1041 <annotation name="org.freedesktop.DBus.GLib.CSymbol" value="my_object"/>
1042 <method name="ManyArgs">
1043 <!-- This is optional, and in this case is redunundant -->
1044 <annotation name="org.freedesktop.DBus.GLib.CSymbol" value="my_object_many_args"/>
1045 <arg type="u" name="x" direction="in" />
1046 <arg type="s" name="str" direction="in" />
1047 <arg type="d" name="trouble" direction="in" />
1048 <arg type="d" name="d_ret" direction="out" />
1049 <arg type="s" name="str_ret" direction="out" />
1056 This XML is in the same format as the D-BUS introspection XML
1057 format. Except we must include an "annotation" which give the C
1058 symbols corresponding to the object implementation prefix
1059 (<literal>my_object</literal>). In addition, if particular
1060 methods symbol names deviate from C convention
1061 (i.e. <literal>ManyArgs</literal> ->
1062 <literal>many_args</literal>), you may specify an annotation
1063 giving the C symbol.
1066 Once you have written this XML, run <literal>dbus-binding-tool --mode=glib-server <replaceable>FILENAME</replaceable> > <replaceable>HEADER_NAME</replaceable>.</literal> to
1067 generate a header file. For example: <command>dbus-binding-tool --mode=glib-server my-objet.xml > my-object-glue.h</command>.
1070 Next, include the generated header in your program, and invoke
1071 <literal>dbus_g_object_class_install_info</literal>, passing the
1072 object class and "object info" included in the header. For
1075 dbus_g_object_type_install_info (COM_FOO_TYPE_MY_OBJECT, &com_foo_my_object_info);
1077 This should be done exactly once per object class.
1080 To actually implement the method, just define a C function named e.g.
1081 <literal>my_object_many_args</literal> in the same file as the info
1082 header is included. At the moment, it is required that this function
1083 conform to the following rules:
1087 The function must return a value of type <literal>gboolean</literal>;
1088 <literal>TRUE</literal> on success, and <literal>FALSE</literal>
1094 The first parameter is a pointer to an instance of the object.
1099 Following the object instance pointer are the method
1105 Following the input values are pointers to return values.
1110 The final parameter must be a <literal>GError **</literal>.
1111 If the function returns <literal>FALSE</literal> for an
1112 error, the error parameter must be initalized with
1113 <literal>g_set_error</literal>.
1119 Finally, you can export an object using <literal>dbus_g_connection_register_g_object</literal>. For example:
1121 dbus_g_connection_register_g_object (connection,
1122 "/com/foo/MyObject",
1128 <sect1 id="qt-client">
1129 <title>Qt API: Using Remote Objects</title>
1132 The Qt bindings are not yet documented.
1137 <sect1 id="qt-server">
1138 <title>Qt API: Implementing Objects</title>
1140 The Qt bindings are not yet documented.
1145 <sect1 id="python-client">
1146 <title>Python API: Using Remote Objects</title>
1148 The Python bindings are not yet documented, but the
1149 bindings themselves are in good shape.
1153 <sect1 id="python-server">
1154 <title>Python API: Implementing Objects</title>
1156 The Python bindings are not yet documented, but the
1157 bindings themselves are in good shape.