Editing Documentation/Maemo 5 Developer Guide/DBus/DBus Basics

Warning: You are not logged in. Your IP address will be recorded in this page's edit history.

Warning: This page is 54 kilobytes long; some browsers may have problems editing pages approaching or longer than 32kb. Please consider breaking the page into smaller sections.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 32: Line 32:
In addition to the single system bus, a separate session bus for each desktop session can exist. Because all user applications in a Maemo-compatible device run with the same user ID, the device only has one session bus as well.
In addition to the single system bus, a separate session bus for each desktop session can exist. Because all user applications in a Maemo-compatible device run with the same user ID, the device only has one session bus as well.
-
A bus exists in the system in the form of a ''bus daemon'', a process that specializes in passing messages from one process to another. The daemon also forwards notifications to all applications on the bus. At the lowest level, D-Bus only supports point-to-point communication, normally using the local domain sockets (<code>AF_UNIX</code>) between the application and the bus daemon. The point-to-point aspect of D-Bus is, however, abstracted by the bus daemon, which implements the addressing and message passing functionality. This means that applications do not need to care about which specific process receives each method call or notification.
+
A bus exists in the system in the form of a ''bus daemon'', a process that specializes in passing messages from one process to another. The daemon also forwards notifications to all applications on the bus. At the lowest level, D-Bus only supports point-to-point communication, normally using the local domain sockets (AF_UNIX) between the application and the bus daemon. The point-to-point aspect of D-Bus is, however, abstracted by the bus daemon, which implements the addressing and message passing functionality. This means that applications do not need to care about which specific process receives each method call or notification.
According to the above-mentioned details, sending a message using D-Bus always involves the following steps (under normal conditions):
According to the above-mentioned details, sending a message using D-Bus always involves the following steps (under normal conditions):
Line 54: Line 54:
** Examples: <code>org.maemo.Alert</code> and <code>org.freedesktop.Notifications</code>.
** Examples: <code>org.maemo.Alert</code> and <code>org.freedesktop.Notifications</code>.
* Each service can contain multiple different objects, each of which provides a different (or the same) service. In order to separate one object from another, ''object paths'' are used. A personal information manager (PIM) information store, for example, might include separate objects to manage the contact information and synchronization.
* Each service can contain multiple different objects, each of which provides a different (or the same) service. In order to separate one object from another, ''object paths'' are used. A personal information manager (PIM) information store, for example, might include separate objects to manage the contact information and synchronization.
-
** Object paths look like file paths (elements separated with the '<code>/</code>' character).
+
** Object paths look like file paths (elements separated with the '/' character).
** In D-Bus, "lazy binding" can also be made, so that a specific function in the recipient is called on all remote method calls, irrespective of the object paths in the calls. This allows the on-demand targeting of method calls, so that the user can remove a specific object in an address book service (using an object path similar to <code>/org/maemo/AddressBook/Contacts/ShortName</code>). Due to the limitations on characters that can be put into the object path, this is not recommended. A better way is to supply the ShortName as a method call argument instead (as a UTF-8 formatted string).
** In D-Bus, "lazy binding" can also be made, so that a specific function in the recipient is called on all remote method calls, irrespective of the object paths in the calls. This allows the on-demand targeting of method calls, so that the user can remove a specific object in an address book service (using an object path similar to <code>/org/maemo/AddressBook/Contacts/ShortName</code>). Due to the limitations on characters that can be put into the object path, this is not recommended. A better way is to supply the ShortName as a method call argument instead (as a UTF-8 formatted string).
** The object path is usually formed using the same elements as in the well-known name, but replacing the dots with slashes, and appending a specific object name to the end. For example: <code>/org/maemo/Alert/Alerter</code>. This is the common convention, but it also solves a specific problem if a process could re-use an existing D-Bus connection without explicitly knowing about it (using a library that encapsulates D-Bus functionality). In such cases, using short names increases the risk of name-space collisions within that process.
** The object path is usually formed using the same elements as in the well-known name, but replacing the dots with slashes, and appending a specific object name to the end. For example: <code>/org/maemo/Alert/Alerter</code>. This is the common convention, but it also solves a specific problem if a process could re-use an existing D-Bus connection without explicitly knowing about it (using a library that encapsulates D-Bus functionality). In such cases, using short names increases the risk of name-space collisions within that process.
Line 67: Line 67:
Examples of all four components that are used for sending a simple message (a method call) in the [[Documentation/Maemo 5 Final SDK|SDK]] can be found below:
Examples of all four components that are used for sending a simple message (a method call) in the [[Documentation/Maemo 5 Final SDK|SDK]] can be found below:
-
<source lang="c">
+
<tt><span>'''<span><font color="#000080"><nowiki>#define</nowiki></font></span>'''</span> SYSNOTE_NAME  <span><font color="#FF0000">"org.freedesktop.Notifications"</font></span>
-
#define SYSNOTE_NAME  "org.freedesktop.Notifications"
+
<span>'''<span><font color="#000080"><nowiki>#define</nowiki></font></span>'''</span> SYSNOTE_OPATH <span><font color="#FF0000">"/org/freedesktop/Notifications"</font></span>
-
#define SYSNOTE_OPATH "/org/freedesktop/Notifications"
+
<span>'''<span><font color="#000080"><nowiki>#define</nowiki></font></span>'''</span> SYSNOTE_IFACE <span><font color="#FF0000">"org.freedesktop.Notifications"</font></span>
-
#define SYSNOTE_IFACE "org.freedesktop.Notifications"
+
<span>'''<span><font color="#000080"><nowiki>#define</nowiki></font></span>'''</span> SYSNOTE_NOTE  <span><font color="#FF0000">"SystemNoteDialog"</font></span></tt>
-
#define SYSNOTE_NOTE  "SystemNoteDialog"
+
-
</source>
+
When switching to the LibOSSO RPC functions (which encapsulate a lot of the D-Bus machinery), operations are still performed with all of the D-Bus naming components.
When switching to the LibOSSO RPC functions (which encapsulate a lot of the D-Bus machinery), operations are still performed with all of the D-Bus naming components.
Line 128: Line 126:
<pre>
<pre>
-
[sbox-DIABLO_X86: ~] > run-standalone.sh dbus-send --print-reply  \
+
[sbox-DIABLO_X86: ~] > run-standalone.sh dbus-send --print-reply  \
-
--type=method_call --dest=org.freedesktop.Notifications  \
+
  --type=method_call --dest=org.freedesktop.Notifications  \
-
/org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog  \
+
  /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog  \
-
string:'Hello, world!' uint32:0 string:'NAO OK!'
+
  string:'Hello, world!' uint32:0 string:'NAO OK!'
-
method return sender=:1.1 -> dest=:1.15
+
method return sender=:1.1 -> dest=:1.15
-
  uint32 4
+
    uint32 4
</pre>
</pre>
Line 140: Line 138:
Assuming that the above command is run while the application framework is already running, the end result looks like this:
Assuming that the above command is run while the application framework is already running, the end result looks like this:
-
[[Image:dbus-send-SystemNoteDialog.png|frame|center|alt=Screenshot of Note dialog showing text ‘Hello, world!’|System note dialog]]
+
[[Image:dbus-send-SystemNoteDialog.png|Image dbus-send-SystemNoteDialog]]
If the command is repeated multiple times, the notification service is capable of displaying only one dialog at a time. This makes sense because the dialog is modal. Furthermore, the method calls are queued, not lost; the notification service displays all of the requested dialogs. The service also acknowledges the RPC method call without delay (which is not always the obvious thing to do), giving a different return value each time (incrementing by one each time).
If the command is repeated multiple times, the notification service is capable of displaying only one dialog at a time. This makes sense because the dialog is modal. Furthermore, the method calls are queued, not lost; the notification service displays all of the requested dialogs. The service also acknowledges the RPC method call without delay (which is not always the obvious thing to do), giving a different return value each time (incrementing by one each time).
Line 150: Line 148:
The [http://maemo.org/api_refs/5.0/5.0-final/dbus/api/ libdbus API reference] documentation contains a helpful note:
The [http://maemo.org/api_refs/5.0/5.0-final/dbus/api/ libdbus API reference] documentation contains a helpful note:
-
<source lang="c">
+
<tt><span>''<span><font color="#9A1900">/**</font></span>''</span>
-
/**
+
  <span>''<span><font color="#9A1900"> * Uses the low-level libdbus which should not be used directly.</font></span>''</span>
-
  * Uses the low-level libdbus which should not be used directly.
+
  <span>''<span><font color="#9A1900"> * As the D-Bus API reference puts it "If you use this low-level API</font></span>''</span>
-
  * As the D-Bus API reference puts it "If you use this low-level API
+
  <span>''<span><font color="#9A1900"> * directly, you are signing up for some pain".</font></span>''</span>
-
  * directly, you are signing up for some pain".
+
  <span>''<span><font color="#9A1900"> */</font></span>''</span></tt>
-
  */
+
-
</source>
+
At this point, this example ignores the warnings, and uses the library to implement a simple program that replicates the earlier <code>dbus-send</code> example. To do this with the minimum amount of code, the code does not process (or expect) any responses to the method call. However, the code demonstrates the bare minimum function calls that are needed to send messages on the bus.
At this point, this example ignores the warnings, and uses the library to implement a simple program that replicates the earlier <code>dbus-send</code> example. To do this with the minimum amount of code, the code does not process (or expect) any responses to the method call. However, the code demonstrates the bare minimum function calls that are needed to send messages on the bus.
-
The first step is to introduce the necessary header files. [https://vcs.maemo.org/svn/maemoexamples/trunk/libdbus-example/dbus-example.c libdbus-example/dbus-example.c]
+
The first step is to introduce the necessary header files. libdbus-example/dbus-example.c
-
<source lang="c">
+
<tt><span>'''<span><font color="#000080"><nowiki>#include</nowiki></font></span>'''</span> <span><font color="#FF0000">&lt;dbus/dbus.h&gt;</font></span> <span>''<span><font color="#9A1900">/* Pull in all of D-Bus headers. */</font></span>''</span>
-
#include <dbus/dbus.h> /* Pull in all of D-Bus headers. */
+
<span>'''<span><font color="#000080"><nowiki>#include</nowiki></font></span>'''</span> <span><font color="#FF0000">&lt;stdio.h&gt;</font></span>    <span>''<span><font color="#9A1900">/* printf, fprintf, stderr */</font></span>''</span>
-
#include <stdio.h>    /* printf, fprintf, stderr */
+
<span>'''<span><font color="#000080"><nowiki>#include</nowiki></font></span>'''</span> <span><font color="#FF0000">&lt;stdlib.h&gt;</font></span>    <span>''<span><font color="#9A1900">/* EXIT_FAILURE, EXIT_SUCCESS */</font></span>''</span>
-
#include <stdlib.h>    /* EXIT_FAILURE, EXIT_SUCCESS */
+
<span>'''<span><font color="#000080"><nowiki>#include</nowiki></font></span>'''</span> <span><font color="#FF0000">&lt;assert.h&gt;</font></span>    <span>''<span><font color="#9A1900">/* assert */</font></span>''</span>
-
#include <assert.h>    /* assert */
+
<span>''<span><font color="#9A1900">/* Symbolic defines for the D-Bus well-known name, interface, object</font></span>''</span>
-
/* Symbolic defines for the D-Bus well-known name, interface, object
+
<span>''<span><font color="#9A1900">  path and method name that we are going to use. */</font></span>''</span>
-
  path and method name that we are going to use. */
+
<span>'''<span><font color="#000080"><nowiki>#define</nowiki></font></span>'''</span> SYSNOTE_NAME  <span><font color="#FF0000">"org.freedesktop.Notifications"</font></span>
-
#define SYSNOTE_NAME  "org.freedesktop.Notifications"
+
<span>'''<span><font color="#000080"><nowiki>#define</nowiki></font></span>'''</span> SYSNOTE_OPATH <span><font color="#FF0000">"/org/freedesktop/Notifications"</font></span>
-
#define SYSNOTE_OPATH "/org/freedesktop/Notifications"
+
<span>'''<span><font color="#000080"><nowiki>#define</nowiki></font></span>'''</span> SYSNOTE_IFACE <span><font color="#FF0000">"org.freedesktop.Notifications"</font></span>
-
#define SYSNOTE_IFACE "org.freedesktop.Notifications"
+
<span>'''<span><font color="#000080"><nowiki>#define</nowiki></font></span>'''</span> SYSNOTE_NOTE  <span><font color="#FF0000">"SystemNoteDialog"</font></span></tt>
-
#define SYSNOTE_NOTE  "SystemNoteDialog"
+
-
</source>
+
Unlike the rest of the code in this material, the dbus example does not use GLib or other support libraries (other than libdbus). This explains why the example uses printf and other functions that are normally replaced with GLib equivalents.
Unlike the rest of the code in this material, the dbus example does not use GLib or other support libraries (other than libdbus). This explains why the example uses printf and other functions that are normally replaced with GLib equivalents.
-
Connecting to the session bus yields a <code>DBusConnection</code> structure: [https://vcs.maemo.org/svn/maemoexamples/trunk/libdbus-example/dbus-example.c libdbus-example/dbus-example.c]
+
Connecting to the session bus yields a <code>DBusConnection</code> structure: libdbus-example/dbus-example.c
-
<source lang="c">
+
<tt><span>''<span><font color="#9A1900">/**</font></span>''</span>
-
/**
+
  <span>''<span><font color="#9A1900"> * The main program that demonstrates a simple "fire &amp; forget" RPC</font></span>''</span>
-
  * The main program that demonstrates a simple "fire & forget" RPC
+
  <span>''<span><font color="#9A1900"> * method invocation.</font></span>''</span>
-
  * method invocation.
+
  <span>''<span><font color="#9A1900"> */</font></span>''</span>
-
  */
+
<span><font color="#009900">int</font></span> <span>'''<span><font color="#000000">main</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#009900">int</font></span> argc<span><font color="#990000">,</font></span> <span><font color="#009900">char</font></span><span><font color="#990000"><nowiki>**</nowiki></font></span> argv<span><font color="#990000">)</font></span> <span><font color="#FF0000">{</font></span>
-
int main(int argc, char** argv) {
+
-
 
+
  <span>''<span><font color="#9A1900">/* Structure representing the connection to a bus. */</font></span>''</span>
-
  /* Structure representing the connection to a bus. */
+
  DBusConnection<span><font color="#990000"><nowiki>*</nowiki></font></span> bus <span><font color="#990000"><nowiki>=</nowiki></font></span> NULL<span><font color="#990000"><nowiki>;</nowiki></font></span>
-
  DBusConnection* bus = NULL;
+
  <span>''<span><font color="#9A1900">/* The method call message. */</font></span>''</span>
-
  /* The method call message. */
+
  DBusMessage<span><font color="#990000"><nowiki>*</nowiki></font></span> msg <span><font color="#990000"><nowiki>=</nowiki></font></span> NULL<span><font color="#990000"><nowiki>;</nowiki></font></span>
-
  DBusMessage* msg = NULL;
+
-
 
+
  <span>''<span><font color="#9A1900">/* D-Bus reports problems and exceptions using the DBusError</font></span>''</span>
-
  /* D-Bus reports problems and exceptions using the DBusError
+
<span>''<span><font color="#9A1900">    structure. We allocate one in stack (so that we don't need to</font></span>''</span>
-
    structure. We allocate one in stack (so that we don't need to
+
<span>''<span><font color="#9A1900">    free it explicitly. */</font></span>''</span>
-
    free it explicitly. */
+
  DBusError error<span><font color="#990000"><nowiki>;</nowiki></font></span>
-
  DBusError error;
+
-
 
+
  <span>''<span><font color="#9A1900">/* Message to display. */</font></span>''</span>
-
  /* Message to display. */
+
  <span>'''<span><font color="#0000FF">const</font></span>'''</span> <span><font color="#009900">char</font></span><span><font color="#990000"><nowiki>*</nowiki></font></span> dispMsg <span><font color="#990000"><nowiki>=</nowiki></font></span> <span><font color="#FF0000">"Hello World!"</font></span><span><font color="#990000"><nowiki>;</nowiki></font></span>
-
  const char* dispMsg = "Hello World!";
+
  <span>''<span><font color="#9A1900">/* Text to use for the acknowledgement button. "" means default. */</font></span>''</span>
-
  /* Text to use for the acknowledgement button. "" means default. */
+
  <span>'''<span><font color="#0000FF">const</font></span>'''</span> <span><font color="#009900">char</font></span><span><font color="#990000"><nowiki>*</nowiki></font></span> buttonText <span><font color="#990000"><nowiki>=</nowiki></font></span> <span><font color="#FF0000">""</font></span><span><font color="#990000"><nowiki>;</nowiki></font></span>
-
  const char* buttonText = "";
+
  <span>''<span><font color="#9A1900">/* Type of icon to use in the dialog (1 = OSSO_GN_ERROR). We could</font></span>''</span>
-
  /* Type of icon to use in the dialog (1 = OSSO_GN_ERROR). We could
+
<span>''<span><font color="#9A1900">    have just used the symbolic version here as well, but that would</font></span>''</span>
-
    have just used the symbolic version here as well, but that would
+
<span>''<span><font color="#9A1900">    have required pulling the LibOSSO-header files. And this example</font></span>''</span>
-
    have required pulling the LibOSSO-header files. And this example
+
<span>''<span><font color="#9A1900">    must work without LibOSSO, so this is why a number is used. */</font></span>''</span>
-
    must work without LibOSSO, so this is why a number is used. */
+
  <span><font color="#009900">int</font></span> iconType <span><font color="#990000"><nowiki>=</nowiki></font></span> <span><font color="#993399">1</font></span><span><font color="#990000"><nowiki>;</nowiki></font></span>
-
  int iconType = 1;
+
-
 
+
  <span>''<span><font color="#9A1900">/* Clean the error state. */</font></span>''</span>
-
  /* Clean the error state. */
+
  <span>'''<span><font color="#000000">dbus_error_init</font></span>'''</span><span><font color="#990000">(&amp;</font></span>error<span><font color="#990000">);</font></span>
-
  dbus_error_init(&error);
+
-
 
+
  <span>'''<span><font color="#000000">printf</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"Connecting to Session D-Bus</font></span><span><font color="#CC33CC">\n</font></span><span><font color="#FF0000">"</font></span><span><font color="#990000">);</font></span>
-
  printf("Connecting to Session D-Bus\n");
+
  bus <span><font color="#990000"><nowiki>=</nowiki></font></span> <span>'''<span><font color="#000000">dbus_bus_get</font></span>'''</span><span><font color="#990000">(</font></span>DBUS_BUS_SESSION<span><font color="#990000">,</font></span> <span><font color="#990000">&amp;</font></span>error<span><font color="#990000">);</font></span>
-
  bus = dbus_bus_get(DBUS_BUS_SESSION, &error);
+
  <span>'''<span><font color="#000000">terminateOnError</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"Failed to open Session bus</font></span><span><font color="#CC33CC">\n</font></span><span><font color="#FF0000">"</font></span><span><font color="#990000">,</font></span> <span><font color="#990000">&amp;</font></span>error<span><font color="#990000">);</font></span>
-
  terminateOnError("Failed to open Session bus\n", &error);
+
  <span>'''<span><font color="#000000">assert</font></span>'''</span><span><font color="#990000">(</font></span>bus <span><font color="#990000"><nowiki>!=</nowiki></font></span> NULL<span><font color="#990000">);</font></span></tt>
-
  assert(bus != NULL);
+
-
</source>
+
Libdbus attempts to share the existing connection structures when the same process is connecting to the same bus. This is done to avoid the costly connection set-up time. Sharing connections is beneficial when the program is using libraries that would also open their own connections to the same buses.
Libdbus attempts to share the existing connection structures when the same process is connecting to the same bus. This is done to avoid the costly connection set-up time. Sharing connections is beneficial when the program is using libraries that would also open their own connections to the same buses.
-
To communicate errors, libdbus uses <code>DBusError</code> structures, whose contents are simple. The dbus_error_init is used for guaranteeing that the error structure contains a non-error state before connecting to the bus. If there is an error, it is handled in <code>terminateOnError</code>: [https://vcs.maemo.org/svn/maemoexamples/trunk/libdbus-example/dbus-example.c libdbus-example/dbus-example.c]
+
To communicate errors, libdbus uses <code>DBusError</code> structures, whose contents are simple. The dbus_error_init is used for guaranteeing that the error structure contains a non-error state before connecting to the bus. If there is an error, it is handled in <code>terminateOnError</code>: libdbus-example/dbus-example.c
-
<source lang="c">
+
<tt><span>''<span><font color="#9A1900">/**</font></span>''</span>
-
/**
+
  <span>''<span><font color="#9A1900"> * Utility to terminate if given DBusError is set.</font></span>''</span>
-
  * Utility to terminate if given DBusError is set.
+
  <span>''<span><font color="#9A1900"> * Prints out the message and error before terminating.</font></span>''</span>
-
  * Prints out the message and error before terminating.
+
  <span>''<span><font color="#9A1900"> *</font></span>''</span>
-
  *
+
  <span>''<span><font color="#9A1900"> * If error is not set, does nothing.</font></span>''</span>
-
  * If error is not set, does nothing.
+
  <span>''<span><font color="#9A1900"> *</font></span>''</span>
-
  *
+
  <span>''<span><font color="#9A1900"> * NOTE: In real applications you should spend a moment or two</font></span>''</span>
-
  * NOTE: In real applications you should spend a moment or two
+
  <span>''<span><font color="#9A1900"> *      thinking about the exit-paths from your application and</font></span>''</span>
-
  *      thinking about the exit-paths from your application and
+
  <span>''<span><font color="#9A1900"> *      whether you need to close/unreference all resources that you</font></span>''</span>
-
  *      whether you need to close/unreference all resources that you
+
  <span>''<span><font color="#9A1900"> *      have allocated. In this program, we rely on the kernel to do</font></span>''</span>
-
  *      have allocated. In this program, we rely on the kernel to do
+
  <span>''<span><font color="#9A1900"> *      all necessary cleanup (closing sockets, releasing memory),</font></span>''</span>
-
  *      all necessary cleanup (closing sockets, releasing memory),
+
  <span>''<span><font color="#9A1900"> *      but in real life you need to be more careful.</font></span>''</span>
-
  *      but in real life you need to be more careful.
+
  <span>''<span><font color="#9A1900"> *</font></span>''</span>
-
  *
+
  <span>''<span><font color="#9A1900"> *      One possible solution model to this is implemented in</font></span>''</span>
-
  *      One possible solution model to this is implemented in
+
  <span>''<span><font color="#9A1900"> *      "flashlight", a simple program that is presented later.</font></span>''</span>
-
  *      "flashlight", a simple program that is presented later.
+
  <span>''<span><font color="#9A1900"> */</font></span>''</span>
-
  */
+
<span>'''<span><font color="#0000FF">static</font></span>'''</span> <span><font color="#009900">void</font></span> <span>'''<span><font color="#000000">terminateOnError</font></span>'''</span><span><font color="#990000">(</font></span><span>'''<span><font color="#0000FF">const</font></span>'''</span> <span><font color="#009900">char</font></span><span><font color="#990000"><nowiki>*</nowiki></font></span> msg<span><font color="#990000">,</font></span>
-
static void terminateOnError(const char* msg,
+
                              <span>'''<span><font color="#0000FF">const</font></span>'''</span> DBusError<span><font color="#990000"><nowiki>*</nowiki></font></span> error<span><font color="#990000">)</font></span> <span><font color="#FF0000">{</font></span>
-
                            const DBusError* error) {
+
 +
  <span>'''<span><font color="#000000">assert</font></span>'''</span><span><font color="#990000">(</font></span>msg <span><font color="#990000"><nowiki>!=</nowiki></font></span> NULL<span><font color="#990000">);</font></span>
 +
  <span>'''<span><font color="#000000">assert</font></span>'''</span><span><font color="#990000">(</font></span>error <span><font color="#990000"><nowiki>!=</nowiki></font></span> NULL<span><font color="#990000">);</font></span>
 +
 +
  <span>'''<span><font color="#0000FF">if</font></span>'''</span> <span><font color="#990000">(</font></span><span>'''<span><font color="#000000">dbus_error_is_set</font></span>'''</span><span><font color="#990000">(</font></span>error<span><font color="#990000">))</font></span> <span><font color="#FF0000">{</font></span>
 +
    <span>'''<span><font color="#000000">fprintf</font></span>'''</span><span><font color="#990000">(</font></span>stderr<span><font color="#990000">,</font></span> msg<span><font color="#990000">);</font></span>
 +
    <span>'''<span><font color="#000000">fprintf</font></span>'''</span><span><font color="#990000">(</font></span>stderr<span><font color="#990000">,</font></span> <span><font color="#FF0000">"DBusError.name: %s</font></span><span><font color="#CC33CC">\n</font></span><span><font color="#FF0000">"</font></span><span><font color="#990000">,</font></span> error<span><font color="#990000">-&gt;</font></span>name<span><font color="#990000">);</font></span>
 +
    <span>'''<span><font color="#000000">fprintf</font></span>'''</span><span><font color="#990000">(</font></span>stderr<span><font color="#990000">,</font></span> <span><font color="#FF0000">"DBusError.message: %s</font></span><span><font color="#CC33CC">\n</font></span><span><font color="#FF0000">"</font></span><span><font color="#990000">,</font></span> error<span><font color="#990000">-&gt;</font></span>message<span><font color="#990000">);</font></span>
 +
    <span>''<span><font color="#9A1900">/* If the program does not exit because of the error, freeing the</font></span>''</span>
 +
<span>''<span><font color="#9A1900">      DBusError needs to be done (with dbus_error_free(error)).</font></span>''</span>
 +
<span>''<span><font color="#9A1900">      NOTE:</font></span>''</span>
 +
<span>''<span><font color="#9A1900">        dbus_error_free(error) would only free the error if it was</font></span>''</span>
 +
<span>''<span><font color="#9A1900">        set, so it is safe to use even when you are unsure. */</font></span>''</span>
 +
    <span>'''<span><font color="#000000">exit</font></span>'''</span><span><font color="#990000">(</font></span>EXIT_FAILURE<span><font color="#990000">);</font></span>
 +
  <span><font color="#FF0000">}</font></span>
 +
<span><font color="#FF0000">}</font></span></tt>
-
  assert(msg != NULL);
+
libdbus also contains some utility functions so that everything does not have to be coded manually. One such utility is <code>dbus_bus_name_has_owner</code>, that checks whether there is at least some process that owns the given well-known name at that moment: libdbus-example/dbus-example.c
-
  assert(error != NULL);
+
-
  if (dbus_error_is_set(error)) {
+
<tt>  <span>''<span><font color="#9A1900">/* Normally one would just do the RPC call immediately without</font></span>''</span>
-
    fprintf(stderr, msg);
+
<span>''<span><font color="#9A1900">    checking for name existence first. However, sometimes it is useful</font></span>''</span>
-
    fprintf(stderr, "DBusError.name: %s\n", error->name);
+
<span>''<span><font color="#9A1900">    to check whether a specific name even exists on a platform on</font></span>''</span>
-
    fprintf(stderr, "DBusError.message: %s\n", error->message);
+
<span>''<span><font color="#9A1900">    which you are planning to use D-Bus.</font></span>''</span>
-
    /* If the program does not exit because of the error, freeing the
+
-
      DBusError needs to be done (with dbus_error_free(error)).
+
<span>''<span><font color="#9A1900">    In our case it acts as a reminder to run this program using the</font></span>''</span>
-
      NOTE:
+
<span>''<span><font color="#9A1900">    run-standalone.sh script when running in the SDK.</font></span>''</span>
-
        dbus_error_free(error) would only free the error if it was
+
-
        set, so it is safe to use even when you are unsure. */
+
<span>''<span><font color="#9A1900">    The existence check is not necessary if the recipient is</font></span>''</span>
-
     exit(EXIT_FAILURE);
+
<span>''<span><font color="#9A1900">    startable/activateable by D-Bus. In that case, if the recipient</font></span>''</span>
-
  }
+
<span>''<span><font color="#9A1900">    is not already running, the D-Bus daemon starts the</font></span>''</span>
-
}
+
<span>''<span><font color="#9A1900">    recipient (a process that has been registered for that</font></span>''</span>
-
</source>
+
<span>''<span><font color="#9A1900">    well-known name) and then passes the message to it. This</font></span>''</span>
 +
<span>''<span><font color="#9A1900">    automatic starting mechanism avoids the race condition</font></span>''</span>
 +
<span>''<span><font color="#9A1900">    discussed below and also makes sure that only one instance of</font></span>''</span>
 +
<span>''<span><font color="#9A1900">    the service is running at any given time. */</font></span>''</span>
 +
  <span>'''<span><font color="#000000">printf</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"Checking whether the target name exists ("</font></span>
 +
          SYSNOTE_NAME <span><font color="#FF0000">")</font></span><span><font color="#CC33CC">\n</font></span><span><font color="#FF0000">"</font></span><span><font color="#990000">);</font></span>
 +
  <span>'''<span><font color="#0000FF">if</font></span>'''</span> <span><font color="#990000">(!</font></span><span>'''<span><font color="#000000">dbus_bus_name_has_owner</font></span>'''</span><span><font color="#990000">(</font></span>bus<span><font color="#990000">,</font></span> SYSNOTE_NAME<span><font color="#990000">,</font></span> <span><font color="#990000">&amp;</font></span>error<span><font color="#990000">))</font></span> <span><font color="#FF0000">{</font></span>
 +
    <span>'''<span><font color="#000000">fprintf</font></span>'''</span><span><font color="#990000">(</font></span>stderr<span><font color="#990000">,</font></span> <span><font color="#FF0000">"Name has no owner on the bus!</font></span><span><font color="#CC33CC">\n</font></span><span><font color="#FF0000">"</font></span><span><font color="#990000">);</font></span>
 +
    <span>'''<span><font color="#0000FF">return</font></span>'''</span> EXIT_FAILURE<span><font color="#990000"><nowiki>;</nowiki></font></span>
 +
  <span><font color="#FF0000">}</font></span>
 +
  <span>'''<span><font color="#000000">terminateOnError</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"Failed to check for name ownership</font></span><span><font color="#CC33CC">\n</font></span><span><font color="#FF0000">"</font></span><span><font color="#990000">,</font></span> <span><font color="#990000">&amp;</font></span>error<span><font color="#990000">);</font></span>
 +
  <span>''<span><font color="#9A1900">/* Someone on the Session bus owns the name. So we can proceed in</font></span>''</span>
 +
<span>''<span><font color="#9A1900">    relative safety. There is a chance of a race. If the name owner</font></span>''</span>
 +
<span>''<span><font color="#9A1900">    decides to drop out from the bus just after we check that it is</font></span>''</span>
 +
<span>''<span><font color="#9A1900">     owned, our RPC call (below) fails anyway. */</font></span>''</span>
 +
</tt>
-
libdbus also contains some utility functions so that everything does not have to be coded manually. One such utility is <code>dbus_bus_name_has_owner</code>, that checks whether there is at least some process that owns the given well-known name at that moment: [https://vcs.maemo.org/svn/maemoexamples/trunk/libdbus-example/dbus-example.c libdbus-example/dbus-example.c]
+
Creating a method call using libdbus is slightly more tedious than using the higher-level interfaces. The process is separated into two steps: creating a message structure, and appending the arguments to the message: libdbus-example/dbus-example.c
-
<source lang="c">
+
<tt>  <span>''<span><font color="#9A1900">/* Construct a DBusMessage that represents a method call.</font></span>''</span>
-
  /* Normally one would just do the RPC call immediately without
+
<span>''<span><font color="#9A1900">     Parameters are added later. The internal type of the message</font></span>''</span>
-
    checking for name existence first. However, sometimes it is useful
+
<span>''<span><font color="#9A1900">    is DBUS_MESSAGE_TYPE_METHOD_CALL. */</font></span>''</span>
-
    to check whether a specific name even exists on a platform on
+
  <span>'''<span><font color="#000000">printf</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"Creating a message object</font></span><span><font color="#CC33CC">\n</font></span><span><font color="#FF0000">"</font></span><span><font color="#990000">);</font></span>
-
    which you are planning to use D-Bus.
+
  msg <span><font color="#990000"><nowiki>=</nowiki></font></span> <span>'''<span><font color="#000000">dbus_message_new_method_call</font></span>'''</span><span><font color="#990000">(</font></span>SYSNOTE_NAME<span><font color="#990000">,</font></span> <span>''<span><font color="#9A1900">/* destination */</font></span>''</span>
-
 
+
                                      SYSNOTE_OPATH<span><font color="#990000">,</font></span> <span>''<span><font color="#9A1900">/* obj. path */</font></span>''</span>
-
    In our case it acts as a reminder to run this program using the
+
                                      SYSNOTE_IFACE<span><font color="#990000">,</font></span> <span>''<span><font color="#9A1900">/* interface */</font></span>''</span>
-
    run-standalone.sh script when running in the SDK.
+
                                      SYSNOTE_NOTE<span><font color="#990000">);</font></span> <span>''<span><font color="#9A1900">/* method str */</font></span>''</span>
-
 
+
  <span>'''<span><font color="#0000FF">if</font></span>'''</span> <span><font color="#990000">(</font></span>msg <span><font color="#990000"><nowiki>==</nowiki></font></span> NULL<span><font color="#990000">)</font></span> <span><font color="#FF0000">{</font></span>
-
    The existence check is not necessary if the recipient is
+
    <span>'''<span><font color="#000000">fprintf</font></span>'''</span><span><font color="#990000">(</font></span>stderr<span><font color="#990000">,</font></span> <span><font color="#FF0000">"Ran out of memory when creating a message</font></span><span><font color="#CC33CC">\n</font></span><span><font color="#FF0000">"</font></span><span><font color="#990000">);</font></span>
-
    startable/activateable by D-Bus. In that case, if the recipient
+
    <span>'''<span><font color="#000000">exit</font></span>'''</span><span><font color="#990000">(</font></span>EXIT_FAILURE<span><font color="#990000">);</font></span>
-
    is not already running, the D-Bus daemon starts the
+
  <span><font color="#FF0000">}</font></span>
-
    recipient (a process that has been registered for that
+
-
    well-known name) and then passes the message to it. This
+
  <span>''<span><font color="#9A1900">/*... Listing cut for brevity ...*/</font></span>''</span>
-
    automatic starting mechanism avoids the race condition
+
-
    discussed below and also makes sure that only one instance of
+
  <span>''<span><font color="#9A1900">/* Add the arguments to the message. For the Note dialog, we need</font></span>''</span>
-
    the service is running at any given time. */
+
<span>''<span><font color="#9A1900">    three arguments:</font></span>''</span>
-
  printf("Checking whether the target name exists ("
+
<span>''<span><font color="#9A1900">      arg0: (STRING) "message to display, in UTF-8"</font></span>''</span>
-
        SYSNOTE_NAME ")\n");
+
<span>''<span><font color="#9A1900">      arg1: (UINT32) type of dialog to display. We use 1.</font></span>''</span>
-
  if (!dbus_bus_name_has_owner(bus, SYSNOTE_NAME, &error)) {
+
<span>''<span><font color="#9A1900">                      (libosso.h/OSSO_GN_ERROR).</font></span>''</span>
-
    fprintf(stderr, "Name has no owner on the bus!\n");
+
<span>''<span><font color="#9A1900">      arg2: (STRING) "text to use for the ack button". "" means</font></span>''</span>
-
    return EXIT_FAILURE;
+
<span>''<span><font color="#9A1900">                      default text (OK in our case).</font></span>''</span>
-
  }
+
-
  terminateOnError("Failed to check for name ownership\n", &error);
+
<span>''<span><font color="#9A1900">    When listing the arguments, the type needs to be specified first</font></span>''</span>
-
  /* Someone on the Session bus owns the name. So we can proceed in
+
<span>''<span><font color="#9A1900">    (by using the libdbus constants) and then a pointer to the</font></span>''</span>
-
    relative safety. There is a chance of a race. If the name owner
+
<span>''<span><font color="#9A1900">    argument content needs to be given.</font></span>''</span>
-
    decides to drop out from the bus just after we check that it is
+
-
    owned, our RPC call (below) fails anyway. */
+
<span>''<span><font color="#9A1900">    NOTE: It is always a pointer to the argument value, not the value</font></span>''</span>
-
</source>
+
<span>''<span><font color="#9A1900">          itself!</font></span>''</span>
-
 
+
-
Creating a method call using libdbus is slightly more tedious than using the higher-level interfaces. The process is separated into two steps: creating a message structure, and appending the arguments to the message: [https://vcs.maemo.org/svn/maemoexamples/trunk/libdbus-example/dbus-example.c libdbus-example/dbus-example.c]
+
<span>''<span><font color="#9A1900">    We terminate the list with DBUS_TYPE_INVALID. */</font></span>''</span>
-
 
+
  <span>'''<span><font color="#000000">printf</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"Appending arguments to the message</font></span><span><font color="#CC33CC">\n</font></span><span><font color="#FF0000">"</font></span><span><font color="#990000">);</font></span>
-
<source lang="c">
+
  <span>'''<span><font color="#0000FF">if</font></span>'''</span> <span><font color="#990000">(!</font></span><span>'''<span><font color="#000000">dbus_message_append_args</font></span>'''</span><span><font color="#990000">(</font></span>msg<span><font color="#990000">,</font></span>
-
  /* Construct a DBusMessage that represents a method call.
+
                                DBUS_TYPE_STRING<span><font color="#990000">,</font></span> <span><font color="#990000">&amp;</font></span>dispMsg<span><font color="#990000">,</font></span>
-
    Parameters are added later. The internal type of the message
+
                                DBUS_TYPE_UINT32<span><font color="#990000">,</font></span> <span><font color="#990000">&amp;</font></span>iconType<span><font color="#990000">,</font></span>
-
    is DBUS_MESSAGE_TYPE_METHOD_CALL. */
+
                                DBUS_TYPE_STRING<span><font color="#990000">,</font></span> <span><font color="#990000">&amp;</font></span>buttonText<span><font color="#990000">,</font></span>
-
  printf("Creating a message object\n");
+
                                DBUS_TYPE_INVALID<span><font color="#990000">))</font></span> <span><font color="#FF0000">{</font></span>
-
  msg = dbus_message_new_method_call(SYSNOTE_NAME, /* destination */
+
    <span>'''<span><font color="#000000">fprintf</font></span>'''</span><span><font color="#990000">(</font></span>stderr<span><font color="#990000">,</font></span> <span><font color="#FF0000">"Ran out of memory while constructing args</font></span><span><font color="#CC33CC">\n</font></span><span><font color="#FF0000">"</font></span><span><font color="#990000">);</font></span>
-
                                    SYSNOTE_OPATH,  /* obj. path */
+
    <span>'''<span><font color="#000000">exit</font></span>'''</span><span><font color="#990000">(</font></span>EXIT_FAILURE<span><font color="#990000">);</font></span>
-
                                    SYSNOTE_IFACE,  /* interface */
+
  <span><font color="#FF0000">}</font></span></tt>
-
                                    SYSNOTE_NOTE); /* method str */
+
-
  if (msg == NULL) {
+
-
    fprintf(stderr, "Ran out of memory when creating a message\n");
+
-
    exit(EXIT_FAILURE);
+
-
  }
+
-
 
+
-
  /*... Listing cut for brevity ...*/
+
-
 
+
-
  /* Add the arguments to the message. For the Note dialog, we need
+
-
    three arguments:
+
-
      arg0: (STRING) "message to display, in UTF-8"
+
-
      arg1: (UINT32) type of dialog to display. We use 1.
+
-
                      (libosso.h/OSSO_GN_ERROR).
+
-
      arg2: (STRING) "text to use for the ack button". "" means
+
-
                      default text (OK in our case).
+
-
 
+
-
    When listing the arguments, the type needs to be specified first
+
-
    (by using the libdbus constants) and then a pointer to the
+
-
    argument content needs to be given.
+
-
 
+
-
    NOTE: It is always a pointer to the argument value, not the value
+
-
          itself!
+
-
 
+
-
    We terminate the list with DBUS_TYPE_INVALID. */
+
-
  printf("Appending arguments to the message\n");
+
-
  if (!dbus_message_append_args(msg,
+
-
                                DBUS_TYPE_STRING, &dispMsg,
+
-
                                DBUS_TYPE_UINT32, &iconType,
+
-
                                DBUS_TYPE_STRING, &buttonText,
+
-
                                DBUS_TYPE_INVALID)) {
+
-
    fprintf(stderr, "Ran out of memory while constructing args\n");
+
-
    exit(EXIT_FAILURE);
+
-
  }
+
-
</source>
+
When arguments are appended to the message, their content is copied, and possibly converted into a format that is sent over the connection to the daemon. This process is called marshaling, and is a common feature to most RPC systems. The method call requires two parameters (as before), the first being the text to be displayed, and the second being the style of the icon to be used. Parameters passed to libdbus are always passed by address. This is different from the higher level libraries.
When arguments are appended to the message, their content is copied, and possibly converted into a format that is sent over the connection to the daemon. This process is called marshaling, and is a common feature to most RPC systems. The method call requires two parameters (as before), the first being the text to be displayed, and the second being the style of the icon to be used. Parameters passed to libdbus are always passed by address. This is different from the higher level libraries.
-
The arguments are encoded, so that their type code is followed by the pointer where the marshaling functions can find the content. The argument list is terminated with <code>DBUS_TYPE_INVALID</code>, so that the function knows where the argument list ends (since the function prototype ends with an ellipsis, ...). [https://vcs.maemo.org/svn/maemoexamples/trunk/libdbus-example/dbus-example.c libdbus-example/dbus-example.c]
+
The arguments are encoded, so that their type code is followed by the pointer where the marshaling functions can find the content. The argument list is terminated with <code>DBUS_TYPE_INVALID</code>, so that the function knows where the argument list ends (since the function prototype ends with an ellipsis, ...). libdbus-example/dbus-example.c
-
<source lang="c">
+
<tt>  <span>''<span><font color="#9A1900">/* Set the "no-reply-wanted" flag into the message. This also means</font></span>''</span>
-
  /* Set the "no-reply-wanted" flag into the message. This also means
+
<span>''<span><font color="#9A1900">    that we cannot reliably know whether the message was delivered or</font></span>''</span>
-
    that we cannot reliably know whether the message was delivered or
+
<span>''<span><font color="#9A1900">    not, but because we do not have reply message handling here, it</font></span>''</span>
-
    not, but because we do not have reply message handling here, it
+
<span>''<span><font color="#9A1900">    does not matter. The "no-reply" is a potential flag for the remote</font></span>''</span>
-
    does not matter. The "no-reply" is a potential flag for the remote
+
<span>''<span><font color="#9A1900">    end so that they know that they do not need to respond to us.</font></span>''</span>
-
    end so that they know that they do not need to respond to us.
+
<span>''<span><font color="#9A1900">    If the no-reply flag is set, the D-Bus daemon makes sure that the</font></span>''</span>
-
    If the no-reply flag is set, the D-Bus daemon makes sure that the
+
<span>''<span><font color="#9A1900">    possible reply is discarded and not sent to us. */</font></span>''</span>
-
    possible reply is discarded and not sent to us. */
+
  <span>'''<span><font color="#000000">dbus_message_set_no_reply</font></span>'''</span><span><font color="#990000">(</font></span>msg<span><font color="#990000">,</font></span> TRUE<span><font color="#990000">);</font></span></tt>
-
  dbus_message_set_no_reply(msg, TRUE);
+
-
</source>
+
Setting the no-reply-flag effectively tells the bus daemon that even if there is a reply coming back for this RPC method, it is not wanted. In this case, the daemon does not send a reply.
Setting the no-reply-flag effectively tells the bus daemon that even if there is a reply coming back for this RPC method, it is not wanted. In this case, the daemon does not send a reply.
-
Once the message is fully constructed, it can be added to the sending queue of the program. Messages are not sent immediately by libdbus. Normally this allows the message queue to accumulate to more than one message, and all of the messages are sent at once to the daemon. This in turn cuts down the number of the necessary context switches. In this case, this message is the only message that the program ever sends, so the send queue is instructed to be flushed immediately, which in turn instructs the library to send all messages to the daemon without a delay: [https://vcs.maemo.org/svn/maemoexamples/trunk/libdbus-example/dbus-example.c libdbus-example/dbus-example.c]
+
Once the message is fully constructed, it can be added to the sending queue of the program. Messages are not sent immediately by libdbus. Normally this allows the message queue to accumulate to more than one message, and all of the messages are sent at once to the daemon. This in turn cuts down the number of the necessary context switches. In this case, this message is the only message that the program ever sends, so the send queue is instructed to be flushed immediately, which in turn instructs the library to send all messages to the daemon without a delay: libdbus-example/dbus-example.c
-
 
+
-
<source lang="c">
+
-
  printf("Adding message to client's send-queue\n");
+
-
  /* We could also get a serial number (dbus_uint32_t) for the message
+
-
    so that we could correlate responses to sent messages later. In
+
-
    our case there is not going to be a response anyway, so we do not care about
+
-
    the serial, so we pass a NULL as the last parameter. */
+
-
  if (!dbus_connection_send(bus, msg, NULL)) {
+
-
    fprintf(stderr, "Ran out of memory while queueing message\n");
+
-
    exit(EXIT_FAILURE);
+
-
  }
+
-
 
+
-
  printf("Waiting for send-queue to be sent out\n");
+
-
  dbus_connection_flush(bus);
+
-
 
+
-
  printf("Queue is now empty\n");
+
-
</source>
+
-
 
+
-
After the message is sent, the reserved resources must be freed. Here, the first one to be freed is the message, and then the connection structure. [https://vcs.maemo.org/svn/maemoexamples/trunk/libdbus-example/dbus-example.c libdbus-example/dbus-example.c]
+
-
 
+
-
<source lang="c">
+
-
  printf("Cleaning up\n");
+
-
 
+
-
  /* Free up the allocated message. Most D-Bus objects have internal
+
-
    reference count and sharing possibility, so _unref() functions
+
-
    are quite common. */
+
-
  dbus_message_unref(msg);
+
-
  msg = NULL;
+
-
  /* Free-up the connection. libdbus attempts to share existing
+
<tt>  <span>'''<span><font color="#000000">printf</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"Adding message to client's send-queue</font></span><span><font color="#CC33CC">\n</font></span><span><font color="#FF0000">"</font></span><span><font color="#990000">);</font></span>
-
    connections for the same client, so instead of closing down a
+
  <span>''<span><font color="#9A1900">/* We could also get a serial number (dbus_uint32_t) for the message</font></span>''</span>
-
    connection object, it is unreferenced. The D-Bus library
+
<span>''<span><font color="#9A1900">    so that we could correlate responses to sent messages later. In</font></span>''</span>
-
    keeps an internal reference to each shared connection, to
+
<span>''<span><font color="#9A1900">    our case there is not going to be a response anyway, so we do not care about</font></span>''</span>
-
     prevent accidental closing of shared connections before the
+
<span>''<span><font color="#9A1900">    the serial, so we pass a NULL as the last parameter. */</font></span>''</span>
-
     library is finalized. */
+
  <span>'''<span><font color="#0000FF">if</font></span>'''</span> <span><font color="#990000">(!</font></span><span>'''<span><font color="#000000">dbus_connection_send</font></span>'''</span><span><font color="#990000">(</font></span>bus<span><font color="#990000">,</font></span> msg<span><font color="#990000">,</font></span> NULL<span><font color="#990000">))</font></span> <span><font color="#FF0000">{</font></span>
-
  dbus_connection_unref(bus);
+
     <span>'''<span><font color="#000000">fprintf</font></span>'''</span><span><font color="#990000">(</font></span>stderr<span><font color="#990000">,</font></span> <span><font color="#FF0000">"Ran out of memory while queueing message</font></span><span><font color="#CC33CC">\n</font></span><span><font color="#FF0000">"</font></span><span><font color="#990000">);</font></span>
-
  bus = NULL;
+
     <span>'''<span><font color="#000000">exit</font></span>'''</span><span><font color="#990000">(</font></span>EXIT_FAILURE<span><font color="#990000">);</font></span>
 +
  <span><font color="#FF0000">}</font></span>
 +
 +
  <span>'''<span><font color="#000000">printf</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"Waiting for send-queue to be sent out</font></span><span><font color="#CC33CC">\n</font></span><span><font color="#FF0000">"</font></span><span><font color="#990000">);</font></span>
 +
  <span>'''<span><font color="#000000">dbus_connection_flush</font></span>'''</span><span><font color="#990000">(</font></span>bus<span><font color="#990000">);</font></span>
 +
 +
  <span>'''<span><font color="#000000">printf</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"Queue is now empty</font></span><span><font color="#CC33CC">\n</font></span><span><font color="#FF0000">"</font></span><span><font color="#990000">);</font></span></tt>
-
  printf("Quitting (success)\n");
+
After the message is sent, the reserved resources must be freed. Here, the first one to be freed is the message, and then the connection structure. libdbus-example/dbus-example.c
-
  return EXIT_SUCCESS;
+
<tt>  <span>'''<span><font color="#000000">printf</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"Cleaning up</font></span><span><font color="#CC33CC">\n</font></span><span><font color="#FF0000">"</font></span><span><font color="#990000">);</font></span>
-
}
+
-
</source>
+
  <span>''<span><font color="#9A1900">/* Free up the allocated message. Most D-Bus objects have internal</font></span>''</span>
 +
<span>''<span><font color="#9A1900">    reference count and sharing possibility, so _unref() functions</font></span>''</span>
 +
<span>''<span><font color="#9A1900">    are quite common. */</font></span>''</span>
 +
  <span>'''<span><font color="#000000">dbus_message_unref</font></span>'''</span><span><font color="#990000">(</font></span>msg<span><font color="#990000">);</font></span>
 +
  msg <span><font color="#990000"><nowiki>=</nowiki></font></span> NULL<span><font color="#990000"><nowiki>;</nowiki></font></span>
 +
 +
  <span>''<span><font color="#9A1900">/* Free-up the connection. libdbus attempts to share existing</font></span>''</span>
 +
<span>''<span><font color="#9A1900">    connections for the same client, so instead of closing down a</font></span>''</span>
 +
<span>''<span><font color="#9A1900">    connection object, it is unreferenced. The D-Bus library</font></span>''</span>
 +
<span>''<span><font color="#9A1900">    keeps an internal reference to each shared connection, to</font></span>''</span>
 +
<span>''<span><font color="#9A1900">    prevent accidental closing of shared connections before the</font></span>''</span>
 +
<span>''<span><font color="#9A1900">    library is finalized. */</font></span>''</span>
 +
  <span>'''<span><font color="#000000">dbus_connection_unref</font></span>'''</span><span><font color="#990000">(</font></span>bus<span><font color="#990000">);</font></span>
 +
  bus <span><font color="#990000"><nowiki>=</nowiki></font></span> NULL<span><font color="#990000"><nowiki>;</nowiki></font></span>
 +
 +
  <span>'''<span><font color="#000000">printf</font></span>'''</span><span><font color="#990000">(</font></span><span><font color="#FF0000">"Quitting (success)</font></span><span><font color="#CC33CC">\n</font></span><span><font color="#FF0000">"</font></span><span><font color="#990000">);</font></span>
 +
 +
  <span>'''<span><font color="#0000FF">return</font></span>'''</span> EXIT_SUCCESS<span><font color="#990000"><nowiki>;</nowiki></font></span>
 +
<span><font color="#FF0000">}</font></span></tt>
After building the program, attempt to run it:
After building the program, attempt to run it:
Line 426: Line 407:
The error message (about <code>/dev/dsp</code>) printed to the same terminal where AF was started is normal behavior (in SDK). Displaying the Note dialog normally also causes an "Alert" sound to be played. The sound system has not been setup in the SDK, so the notification component complains about failing to open the sound device.
The error message (about <code>/dev/dsp</code>) printed to the same terminal where AF was started is normal behavior (in SDK). Displaying the Note dialog normally also causes an "Alert" sound to be played. The sound system has not been setup in the SDK, so the notification component complains about failing to open the sound device.
-
[[Image:libdbus-example.png|frame|center|alt=Screenshot of message ‘Hello World!’|The friendly error message, using low-level D-Bus]]
+
[[Image:libdbus-example.png|Image libdbus-example]]The friendly error message, using low-level D-Bus
-
To get libdbus integrated into makefiles, use pkg-config. One possible solution is presented below (see section [[Documentation/Maemo 5 Developer Guide/GNU Build System#GNU Make and Makefiles|GNU Make and Makefiles]] in chapter [[Documentation/Maemo 5 Developer Guide/GNU Build System|GNU Build System]], if necessary): [https://vcs.maemo.org/svn/maemoexamples/trunk/libdbus-example/Makefile libdbus-example/Makefile]
+
To get libdbus integrated into makefiles, use pkg-config. One possible solution is presented below (see section [[Documentation/Maemo 5 Developer Guide/GNU Build System#GNU Make and Makefiles|GNU Make and Makefiles]] in chapter [[Documentation/Maemo 5 Developer Guide/GNU Build System|GNU Build System]], if necessary): libdbus-example/Makefile
-
<source lang="make">
+
<tt><span>''<span><font color="#9A1900"><nowiki># Define a list of pkg-config packages we want to use</nowiki></font></span>''</span>
-
# Define a list of pkg-config packages we want to use
+
pkg_packages <span><font color="#990000"><nowiki>:=</nowiki></font></span> dbus-glib-<span><font color="#993399">1</font></span>
-
pkg_packages := dbus-glib-1
+
PKG_CFLAGS  <span><font color="#990000"><nowiki>:=</nowiki></font></span> <span><font color="#009900">$(</font></span>shell pkg-config -cflags <span><font color="#009900">$(pkg_packages))</font></span>
-
PKG_CFLAGS  := $(shell pkg-config -cflags $(pkg_packages))
+
PKG_LDFLAGS <span><font color="#990000"><nowiki>:=</nowiki></font></span> <span><font color="#009900">$(</font></span>shell pkg-config -libs <span><font color="#009900">$(pkg_packages))</font></span>
-
PKG_LDFLAGS := $(shell pkg-config -libs $(pkg_packages))
+
<span>''<span><font color="#9A1900"><nowiki># Additional flags for the compiler:</nowiki></font></span>''</span>
-
# Additional flags for the compiler:
+
<span>''<span><font color="#9A1900"><nowiki>#    -g : Add debugging symbols</nowiki></font></span>''</span>
-
#    -g : Add debugging symbols
+
<span>''<span><font color="#9A1900"><nowiki># -Wall : Enable most gcc warnings</nowiki></font></span>''</span>
-
# -Wall : Enable most gcc warnings
+
ADD_CFLAGS <span><font color="#990000"><nowiki>:=</nowiki></font></span> -g -Wall
-
ADD_CFLAGS := -g -Wall
+
<span>''<span><font color="#9A1900"><nowiki># Combine user supplied, additional, and pkg-config flags</nowiki></font></span>''</span>
-
# Combine user supplied, additional, and pkg-config flags
+
CFLAGS  <span><font color="#990000"><nowiki>:=</nowiki></font></span> <span><font color="#009900">$(PKG_CFLAGS)</font></span> <span><font color="#009900">$(ADD_CFLAGS)</font></span> <span><font color="#009900">$(CFLAGS)</font></span>
-
CFLAGS  := $(PKG_CFLAGS) $(ADD_CFLAGS) $(CFLAGS)
+
LDFLAGS <span><font color="#990000"><nowiki>:=</nowiki></font></span> <span><font color="#009900">$(PKG_LDFLAGS)</font></span> <span><font color="#009900">$(LDFLAGS)</font></span></tt>
-
LDFLAGS := $(PKG_LDFLAGS) $(LDFLAGS)
+
-
</source>
+
The above example shows one possibility to integrate user-supplied variables into makefiles, so that they are still passed along the toolchain. This allows the user to execute make with custom flags, overriding those that are introduced using other means. For example: "CFLAGS='-g0' make" results in the -g0 being interpreted after the -g that is in the Makefile, and this leads to debugging symbols being disabled. Environmental variables can be taken into account in exactly the same way.
The above example shows one possibility to integrate user-supplied variables into makefiles, so that they are still passed along the toolchain. This allows the user to execute make with custom flags, overriding those that are introduced using other means. For example: "CFLAGS='-g0' make" results in the -g0 being interpreted after the -g that is in the Makefile, and this leads to debugging symbols being disabled. Environmental variables can be taken into account in exactly the same way.

Learn more about Contributing to the wiki.


Please note that all contributions to maemo.org wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see maemo.org wiki:Copyrights for details). Do not submit copyrighted work without permission!


Cancel | Editing help (opens in new window)