2003-09-30 03:34:00 +00:00
|
|
|
<?xml version="1.0" standalone="no"?>
|
|
|
|
|
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
|
|
|
|
|
"http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd"
|
|
|
|
|
[
|
|
|
|
|
]>
|
|
|
|
|
|
|
|
|
|
<article id="index">
|
|
|
|
|
<articleinfo>
|
|
|
|
|
<title>D-BUS Tutorial</title>
|
2005-01-18 20:42:15 +00:00
|
|
|
<releaseinfo>Version 0.3</releaseinfo>
|
|
|
|
|
<date>18 January 2005</date>
|
2003-09-30 03:34:00 +00:00
|
|
|
<authorgroup>
|
|
|
|
|
<author>
|
|
|
|
|
<firstname>Havoc</firstname>
|
|
|
|
|
<surname>Pennington</surname>
|
|
|
|
|
<affiliation>
|
|
|
|
|
<orgname>Red Hat, Inc.</orgname>
|
|
|
|
|
<address>
|
|
|
|
|
<email>hp@pobox.com</email>
|
|
|
|
|
</address>
|
|
|
|
|
</affiliation>
|
|
|
|
|
</author>
|
|
|
|
|
</authorgroup>
|
|
|
|
|
</articleinfo>
|
|
|
|
|
|
2003-10-02 22:34:17 +00:00
|
|
|
<sect1 id="whatis">
|
|
|
|
|
<title>What is D-BUS?</title>
|
2003-09-30 03:34:00 +00:00
|
|
|
<para>
|
2003-10-02 22:34:17 +00:00
|
|
|
D-BUS is a system for <firstterm>interprocess communication</firstterm>
|
|
|
|
|
(IPC). Architecturally, it has several layers:
|
|
|
|
|
|
2003-09-30 03:34:00 +00:00
|
|
|
<itemizedlist>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>
|
2003-10-02 22:55:06 +00:00
|
|
|
A library, <firstterm>libdbus</firstterm>, that allows two
|
|
|
|
|
applications to connect to each other and exchange messages.
|
2003-10-02 22:34:17 +00:00
|
|
|
</para>
|
|
|
|
|
</listitem>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>
|
2003-10-02 22:55:06 +00:00
|
|
|
A <firstterm>message bus daemon</firstterm> executable, built on
|
|
|
|
|
libdbus, that multiple applications can connect to. The daemon can
|
|
|
|
|
route messages from one application to zero or more other
|
|
|
|
|
applications.
|
2003-09-30 03:34:00 +00:00
|
|
|
</para>
|
|
|
|
|
</listitem>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>
|
2003-10-02 22:55:06 +00:00
|
|
|
<firstterm>Wrapper libraries</firstterm> 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,
|
2004-08-10 02:18:37 +00:00
|
|
|
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.
|
2003-09-30 03:34:00 +00:00
|
|
|
</para>
|
|
|
|
|
</listitem>
|
|
|
|
|
</itemizedlist>
|
|
|
|
|
</para>
|
2003-10-02 22:34:17 +00:00
|
|
|
|
2004-08-10 04:18:31 +00:00
|
|
|
<para>
|
|
|
|
|
If you just want to use D-BUS and don't care how it works, jump directly
|
|
|
|
|
to <xref linkend="concepts"/>.
|
|
|
|
|
Otherwise, read on.
|
|
|
|
|
</para>
|
|
|
|
|
|
2003-10-02 22:34:17 +00:00
|
|
|
<para>
|
|
|
|
|
libdbus only supports one-to-one connections, just like a raw network
|
|
|
|
|
socket. However, rather than sending byte streams over the connection, you
|
|
|
|
|
send <firstterm>messages</firstterm>. 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.
|
|
|
|
|
</para>
|
|
|
|
|
|
2003-09-30 03:34:00 +00:00
|
|
|
<para>
|
2003-10-02 22:49:11 +00:00
|
|
|
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.
|
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
<para>
|
|
|
|
|
The bus daemon has multiple instances on a typical computer. The
|
2003-10-02 22:34:17 +00:00
|
|
|
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.
|
2003-09-30 03:34:00 +00:00
|
|
|
</para>
|
2003-10-02 22:34:17 +00:00
|
|
|
|
2003-09-30 03:34:00 +00:00
|
|
|
<para>
|
2003-10-02 22:34:17 +00:00
|
|
|
The systemwide and per-user daemons are separate. Normal within-session
|
|
|
|
|
IPC does not involve the systemwide message bus process and vice versa.
|
2003-09-30 03:34:00 +00:00
|
|
|
</para>
|
2003-10-02 22:34:17 +00:00
|
|
|
|
|
|
|
|
<sect2 id="uses">
|
|
|
|
|
<title>D-BUS applications</title>
|
|
|
|
|
<para>
|
|
|
|
|
There are many, many technologies in the world that have "Inter-process
|
|
|
|
|
communication" or "networking" in their stated purpose: <ulink
|
|
|
|
|
url="http://www.mbus.org/">MBUS</ulink>, <ulink
|
|
|
|
|
url="http://www.omg.org">CORBA</ulink>, <ulink
|
|
|
|
|
url="http://www.xmlrpc.com">XML-RPC</ulink>, <ulink
|
|
|
|
|
url="http://www.w3.org/TR/SOAP/">SOAP</ulink>, and probably hundreds
|
|
|
|
|
more. Each of these is tailored for particular kinds of application.
|
|
|
|
|
D-BUS is designed for two specific cases:
|
|
|
|
|
<itemizedlist>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>
|
|
|
|
|
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).
|
|
|
|
|
</para>
|
|
|
|
|
</listitem>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>
|
|
|
|
|
Communication between the desktop session and the operating system,
|
|
|
|
|
where the operating system would typically include the kernel
|
|
|
|
|
and any system daemons or processes.
|
|
|
|
|
</para>
|
|
|
|
|
</listitem>
|
|
|
|
|
</itemizedlist>
|
|
|
|
|
</para>
|
|
|
|
|
<para>
|
|
|
|
|
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.
|
|
|
|
|
</para>
|
|
|
|
|
<para>
|
|
|
|
|
The problem solved by the systemwide or communication-with-the-OS case
|
|
|
|
|
is explained well by the following text from the Linux Hotplug project:
|
|
|
|
|
<blockquote>
|
|
|
|
|
<para>
|
|
|
|
|
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.
|
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
<para>
|
|
|
|
|
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.
|
|
|
|
|
</para>
|
|
|
|
|
</blockquote>
|
|
|
|
|
</para>
|
|
|
|
|
<para>
|
|
|
|
|
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:
|
|
|
|
|
<itemizedlist>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>
|
|
|
|
|
Binary protocol designed to be used asynchronously
|
|
|
|
|
(similar in spirit to the X Window System protocol).
|
|
|
|
|
</para>
|
|
|
|
|
</listitem>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>
|
|
|
|
|
Stateful, reliable connections held open over time.
|
|
|
|
|
</para>
|
|
|
|
|
</listitem>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>
|
|
|
|
|
The message bus is a daemon, not a "swarm" or
|
|
|
|
|
distributed architecture.
|
|
|
|
|
</para>
|
|
|
|
|
</listitem>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>
|
|
|
|
|
Many implementation and deployment issues are specified rather
|
|
|
|
|
than left ambiguous.
|
|
|
|
|
</para>
|
|
|
|
|
</listitem>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>
|
|
|
|
|
Semantics are similar to the existing DCOP system, allowing
|
|
|
|
|
KDE to adopt it more easily.
|
|
|
|
|
</para>
|
|
|
|
|
</listitem>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>
|
|
|
|
|
Security features to support the systemwide mode of the
|
|
|
|
|
message bus.
|
|
|
|
|
</para>
|
|
|
|
|
</listitem>
|
|
|
|
|
</itemizedlist>
|
|
|
|
|
</para>
|
|
|
|
|
</sect2>
|
2003-09-30 03:34:00 +00:00
|
|
|
</sect1>
|
2003-10-02 22:34:17 +00:00
|
|
|
<sect1 id="concepts">
|
|
|
|
|
<title>Concepts</title>
|
|
|
|
|
<para>
|
|
|
|
|
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.
|
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
<sect2 id="objects">
|
|
|
|
|
<title>Objects and Object Paths</title>
|
|
|
|
|
<para>
|
|
|
|
|
Each application using D-BUS contains <firstterm>objects</firstterm>,
|
|
|
|
|
which generally map to GObject, QObject, C++ objects, or Python objects
|
|
|
|
|
(but need not). An object is an <emphasis>instance</emphasis> 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.
|
|
|
|
|
</para>
|
|
|
|
|
<para>
|
|
|
|
|
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 <firstterm>pointer</firstterm> or
|
|
|
|
|
<firstterm>reference</firstterm>. 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.
|
|
|
|
|
</para>
|
|
|
|
|
<para>
|
|
|
|
|
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 <literal>/org/kde/kspread/sheets/3/cells/4/5</literal>.
|
|
|
|
|
Human-readable paths are nice, but you are free to create an
|
|
|
|
|
object named <literal>/com/mycompany/c5yo817y0c1y1c5b</literal>
|
|
|
|
|
if it makes sense for your application.
|
|
|
|
|
</para>
|
|
|
|
|
<para>
|
|
|
|
|
Namespacing object paths is smart, by starting them with the components
|
|
|
|
|
of a domain name you own (e.g. <literal>/org/kde</literal>). This
|
|
|
|
|
keeps different code modules in the same process from stepping
|
|
|
|
|
on one another's toes.
|
|
|
|
|
</para>
|
|
|
|
|
</sect2>
|
|
|
|
|
|
|
|
|
|
<sect2 id="interfaces">
|
|
|
|
|
<title>Interfaces</title>
|
|
|
|
|
<para>
|
|
|
|
|
Each object supports one or more <firstterm>interfaces</firstterm>.
|
|
|
|
|
Think of an interface as a named group of methods and signals,
|
2004-08-10 04:18:31 +00:00
|
|
|
just as it is in GLib or Qt or Java. Interfaces define the
|
2003-10-02 22:34:17 +00:00
|
|
|
<emphasis>type</emphasis> of an object instance.
|
|
|
|
|
</para>
|
|
|
|
|
</sect2>
|
|
|
|
|
|
|
|
|
|
<sect2 id="messages">
|
|
|
|
|
<title>Message Types</title>
|
|
|
|
|
<para>
|
|
|
|
|
Messages are not all the same; in particular, D-BUS has
|
|
|
|
|
4 built-in message types:
|
|
|
|
|
<itemizedlist>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>
|
|
|
|
|
Method call messages ask to invoke a method
|
|
|
|
|
on an object.
|
|
|
|
|
</para>
|
|
|
|
|
</listitem>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>
|
|
|
|
|
Method return messages return the results
|
|
|
|
|
of invoking a method.
|
|
|
|
|
</para>
|
|
|
|
|
</listitem>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>
|
|
|
|
|
Error messages return an exception caused by
|
|
|
|
|
invoking a method.
|
|
|
|
|
</para>
|
|
|
|
|
</listitem>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para>
|
|
|
|
|
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.
|
|
|
|
|
</para>
|
|
|
|
|
</listitem>
|
|
|
|
|
</itemizedlist>
|
|
|
|
|
</para>
|
|
|
|
|
<para>
|
|
|
|
|
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.
|
|
|
|
|
</para>
|
|
|
|
|
</sect2>
|
|
|
|
|
|
2005-01-18 20:42:15 +00:00
|
|
|
<sect2 id="bus-names">
|
|
|
|
|
<title>Bus Names</title>
|
2003-10-02 22:34:17 +00:00
|
|
|
|
|
|
|
|
<para>
|
|
|
|
|
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.
|
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
<para>
|
2005-01-18 20:42:15 +00:00
|
|
|
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.
|
2003-10-02 22:34:17 +00:00
|
|
|
These names are used to specify the origin and destination
|
|
|
|
|
of messages passing through the message bus. When a name is mapped
|
2005-01-18 20:42:15 +00:00
|
|
|
to a particular application's connection, that application is said to
|
|
|
|
|
<firstterm>own</firstterm> that name.
|
2003-10-02 22:34:17 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
<para>
|
|
|
|
|
On connecting to the bus daemon, each application immediately owns a
|
2005-01-18 20:42:15 +00:00
|
|
|
special name called the <firstterm>unique connection name</firstterm>.
|
|
|
|
|
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
|
|
|
|
|
<literal>:34-907</literal>. The numbers after the colon have
|
|
|
|
|
no meaning other than their uniqueness.
|
2003-10-02 22:34:17 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
<para>
|
|
|
|
|
Applications may ask to own additional <firstterm>well-known
|
2005-01-18 20:42:15 +00:00
|
|
|
names</firstterm>. For example, you could write a specification to
|
|
|
|
|
define a name called <literal>com.mycompany.TextEditor</literal>.
|
|
|
|
|
Your definition could specify that to own this name, an application
|
2003-10-02 22:34:17 +00:00
|
|
|
should have an object at the path
|
|
|
|
|
<literal>/com/mycompany/TextFileManager</literal> supporting the
|
|
|
|
|
interface <literal>org.freedesktop.FileHandler</literal>.
|
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
<para>
|
2005-01-18 20:42:15 +00:00
|
|
|
Applications could then send messages to this bus name,
|
2003-10-02 22:34:17 +00:00
|
|
|
object, and interface to execute method calls.
|
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
<para>
|
2005-01-18 20:42:15 +00:00
|
|
|
You could think of the unique names as IP addresses, and the
|
|
|
|
|
well-known names as domain names. So
|
2003-10-02 22:49:11 +00:00
|
|
|
<literal>com.mycompany.TextEditor</literal> might map to something like
|
|
|
|
|
<literal>:34-907</literal> just as <literal>mycompany.com</literal> maps
|
|
|
|
|
to something like <literal>192.168.0.5</literal>.
|
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
<para>
|
2005-01-18 20:42:15 +00:00
|
|
|
Names have a second important use, other than routing messages. They
|
2003-10-02 22:34:17 +00:00
|
|
|
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
|
2005-01-18 20:42:15 +00:00
|
|
|
remaining applications that the application's names have lost their
|
2003-10-02 22:34:17 +00:00
|
|
|
owner. By tracking these notifications, your application can reliably
|
|
|
|
|
monitor the lifetime of other applications.
|
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
</sect2>
|
|
|
|
|
|
|
|
|
|
<sect2 id="addresses">
|
|
|
|
|
<title>Addresses</title>
|
|
|
|
|
|
|
|
|
|
<para>
|
|
|
|
|
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.
|
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
<para>
|
|
|
|
|
A D-BUS <firstterm>address</firstterm> specifies where a server will
|
|
|
|
|
listen, and where a client will connect. For example, the address
|
|
|
|
|
<literal>unix:path=/tmp/abcdef</literal> specifies that the server will
|
|
|
|
|
listen on a UNIX domain socket at the path
|
|
|
|
|
<literal>/tmp/abcdef</literal> 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.
|
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
<para>
|
|
|
|
|
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).
|
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
<para>
|
|
|
|
|
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.
|
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
</sect2>
|
|
|
|
|
|
|
|
|
|
<sect2 id="bigpicture">
|
|
|
|
|
<title>Big Conceptual Picture</title>
|
|
|
|
|
|
|
|
|
|
<para>
|
|
|
|
|
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:
|
|
|
|
|
<programlisting>
|
2005-01-21 03:44:10 +00:00
|
|
|
Address -> [Bus Name] -> Path -> Interface -> Method
|
2003-10-02 22:34:17 +00:00
|
|
|
</programlisting>
|
2005-01-18 20:42:15 +00:00
|
|
|
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
|
2003-10-02 22:34:17 +00:00
|
|
|
when using the bus daemon. If you have a direct connection to another
|
2005-01-18 20:42:15 +00:00
|
|
|
application, bus names aren't used; there's no bus daemon.
|
2003-10-02 22:34:17 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
<para>
|
|
|
|
|
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.
|
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
</sect2>
|
|
|
|
|
|
|
|
|
|
</sect1>
|
|
|
|
|
|
|
|
|
|
<sect1 id="glib-client">
|
|
|
|
|
<title>GLib API: Using Remote Objects</title>
|
2004-08-10 04:18:31 +00:00
|
|
|
|
2003-10-02 22:34:17 +00:00
|
|
|
<para>
|
|
|
|
|
|
2004-08-10 04:18:31 +00:00
|
|
|
The GLib binding is defined in the header file
|
|
|
|
|
<dbus/dbus-glib.h>. The API is very small, in sharp contrast to the
|
|
|
|
|
low-level <dbus/dbus.h>.
|
|
|
|
|
|
2003-10-02 22:34:17 +00:00
|
|
|
</para>
|
2004-08-10 04:18:31 +00:00
|
|
|
|
|
|
|
|
<para>
|
|
|
|
|
The GLib bindings are incomplete, see the TODO file and comments in the
|
|
|
|
|
source code.
|
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
<para>
|
|
|
|
|
Here is a D-BUS program using the GLib bindings.
|
|
|
|
|
<programlisting>
|
|
|
|
|
int
|
|
|
|
|
main (int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
DBusGConnection *connection;
|
|
|
|
|
GError *error;
|
|
|
|
|
DBusGProxy *proxy;
|
|
|
|
|
DBusGPendingCall *call;
|
2005-01-18 20:42:15 +00:00
|
|
|
char **name_list;
|
|
|
|
|
int name_list_len;
|
2004-08-10 04:18:31 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
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",
|
2005-01-21 03:44:10 +00:00
|
|
|
error->message);
|
2004-08-10 04:18:31 +00:00
|
|
|
g_error_free (error);
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
2005-01-18 20:42:15 +00:00
|
|
|
/* Create a proxy object for the "bus driver" (name "org.freedesktop.DBus") */
|
2004-08-10 04:18:31 +00:00
|
|
|
|
2005-01-18 20:42:15 +00:00
|
|
|
proxy = dbus_g_proxy_new_for_name (connection,
|
|
|
|
|
DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
|
|
|
|
|
DBUS_PATH_ORG_FREEDESKTOP_DBUS,
|
|
|
|
|
DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS);
|
2004-08-10 04:18:31 +00:00
|
|
|
|
2005-01-18 20:42:15 +00:00
|
|
|
/* Call ListNames method */
|
2004-08-10 04:18:31 +00:00
|
|
|
|
2005-01-18 20:42:15 +00:00
|
|
|
call = dbus_g_proxy_begin_call (proxy, "ListNames", DBUS_TYPE_INVALID);
|
2004-08-10 04:18:31 +00:00
|
|
|
|
|
|
|
|
error = NULL;
|
|
|
|
|
if (!dbus_g_proxy_end_call (proxy, call, &error,
|
|
|
|
|
DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
|
2005-01-18 20:42:15 +00:00
|
|
|
&name_list, &name_list_len,
|
2004-08-10 04:18:31 +00:00
|
|
|
DBUS_TYPE_INVALID))
|
|
|
|
|
{
|
2005-01-18 20:42:15 +00:00
|
|
|
g_printerr ("Failed to complete ListNames call: %s\n",
|
2005-01-21 03:44:10 +00:00
|
|
|
error->message);
|
2004-08-10 04:18:31 +00:00
|
|
|
g_error_free (error);
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Print the results */
|
|
|
|
|
|
2005-01-18 20:42:15 +00:00
|
|
|
g_print ("Names on the message bus:\n");
|
2004-08-10 04:18:31 +00:00
|
|
|
i = 0;
|
2005-01-18 20:42:15 +00:00
|
|
|
while (i < name_list_len)
|
2004-08-10 04:18:31 +00:00
|
|
|
{
|
2005-01-18 20:42:15 +00:00
|
|
|
g_assert (name_list[i] != NULL);
|
|
|
|
|
g_print (" %s\n", name_list[i]);
|
2004-08-10 04:18:31 +00:00
|
|
|
++i;
|
|
|
|
|
}
|
2005-01-18 20:42:15 +00:00
|
|
|
g_assert (name_list[i] == NULL);
|
2004-08-10 04:18:31 +00:00
|
|
|
|
2005-01-18 20:42:15 +00:00
|
|
|
g_strfreev (name_list);
|
2004-08-10 04:18:31 +00:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
</programlisting>
|
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
<para>
|
|
|
|
|
|
|
|
|
|
DBusGProxy represents a remote object. dbus_g_proxy_begin_call() sends
|
|
|
|
|
a method call to the remote object, and dbus_g_proxy_end_call() retrieves
|
|
|
|
|
any return values or exceptions resulting from the method call.
|
|
|
|
|
There are also DBusGProxy functions to connect and disconnect signals,
|
|
|
|
|
not shown in the code example.
|
|
|
|
|
|
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
<para>
|
|
|
|
|
|
|
|
|
|
dbus_g_bus_get() assumes that the application will use GMainLoop. The
|
|
|
|
|
created connection will be associated with the main loop such that
|
|
|
|
|
messages will be sent and received when the main loop runs. However, in
|
|
|
|
|
the above code example the main loop never runs; D-BUS will not run the
|
|
|
|
|
loop implicitly. Instead, dbus_g_proxy_end_call() will block until the
|
|
|
|
|
method call has been sent and the reply received. A more complex GUI
|
|
|
|
|
application might run the main loop while waiting for the method call
|
|
|
|
|
reply. (DBusGPendingCall is currently missing the "notify me when the
|
|
|
|
|
call is complete" functionality found in DBusPendingCall, but it should be
|
|
|
|
|
added.)
|
|
|
|
|
|
|
|
|
|
</para>
|
|
|
|
|
|
|
|
|
|
<para>
|
|
|
|
|
|
|
|
|
|
Future plans (see doc/TODO) are to use G_TYPE_STRING in place of
|
|
|
|
|
DBUS_TYPE_STRING and so forth. In fact the above code is slightly
|
|
|
|
|
incorrect at the moment, since it uses g_strfreev() to free a string array
|
|
|
|
|
that was not allocated with g_malloc(). dbus_free_string_array() should
|
|
|
|
|
really be used. However, once the GLib bindings are complete the returned
|
|
|
|
|
data from dbus_g_proxy_end_call() will be allocated with g_malloc().
|
|
|
|
|
|
|
|
|
|
</para>
|
|
|
|
|
|
2003-10-02 22:34:17 +00:00
|
|
|
</sect1>
|
|
|
|
|
|
|
|
|
|
<sect1 id="glib-server">
|
|
|
|
|
<title>GLib API: Implementing Objects</title>
|
2004-08-10 04:18:31 +00:00
|
|
|
|
2003-10-02 22:34:17 +00:00
|
|
|
<para>
|
|
|
|
|
|
2004-08-10 04:18:31 +00:00
|
|
|
The GLib binding is defined in the header file
|
|
|
|
|
<dbus/dbus-glib.h>. To implement an object, it's also necessary
|
|
|
|
|
to use the dbus-glib-tool command line tool.
|
|
|
|
|
|
2003-10-02 22:34:17 +00:00
|
|
|
</para>
|
2004-08-10 04:18:31 +00:00
|
|
|
|
|
|
|
|
<para>
|
|
|
|
|
The GLib bindings are incomplete. Implementing an object is not yet
|
|
|
|
|
possible, see the TODO file and comments in the source code for details
|
|
|
|
|
on what work needs doing.
|
|
|
|
|
</para>
|
|
|
|
|
|
2003-10-02 22:34:17 +00:00
|
|
|
</sect1>
|
|
|
|
|
|
|
|
|
|
<sect1 id="qt-client">
|
|
|
|
|
<title>Qt API: Using Remote Objects</title>
|
|
|
|
|
<para>
|
|
|
|
|
|
2004-08-10 04:18:31 +00:00
|
|
|
The Qt bindings are not yet documented.
|
|
|
|
|
|
2003-10-02 22:34:17 +00:00
|
|
|
</para>
|
|
|
|
|
</sect1>
|
|
|
|
|
|
|
|
|
|
<sect1 id="qt-server">
|
|
|
|
|
<title>Qt API: Implementing Objects</title>
|
|
|
|
|
<para>
|
2004-08-10 04:18:31 +00:00
|
|
|
The Qt bindings are not yet documented.
|
2003-10-02 22:34:17 +00:00
|
|
|
</para>
|
|
|
|
|
</sect1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<sect1 id="python-client">
|
|
|
|
|
<title>Python API: Using Remote Objects</title>
|
|
|
|
|
<para>
|
2004-08-10 04:18:31 +00:00
|
|
|
The Python bindings are not yet documented, but the
|
|
|
|
|
bindings themselves are in good shape.
|
2003-10-02 22:34:17 +00:00
|
|
|
</para>
|
|
|
|
|
</sect1>
|
|
|
|
|
|
|
|
|
|
<sect1 id="python-server">
|
|
|
|
|
<title>Python API: Implementing Objects</title>
|
|
|
|
|
<para>
|
2004-08-10 04:18:31 +00:00
|
|
|
The Python bindings are not yet documented, but the
|
|
|
|
|
bindings themselves are in good shape.
|
2003-10-02 22:34:17 +00:00
|
|
|
</para>
|
|
|
|
|
</sect1>
|
|
|
|
|
|
2003-09-30 03:34:00 +00:00
|
|
|
</article>
|