Reapplied r44918 (as far as it applies to fat-handler):
[AROS.git] / rom / dbus / test / dbus-dcop.c
bloba5703c15548ebef5228c75078c1e4901699e99ab
1 /*
2 * Based on examples from "Connect desktop apps using D-BUS -- Helping
3 * applications talk to one another", developerWorks 27 Jul 2004,
4 */
7 #define DBUS_API_SUBJECT_TO_CHANGE
8 #include <dbus/dbus.h>
9 #include <stdio.h>
11 #ifdef __AMIGA__
12 #include <proto/dbus.h>
13 #include <proto/dos.h>
14 #include <proto/exec.h>
16 #include "dbus-amiga.h"
18 struct Library* DBUSBase;
19 #else
20 # include <dbus/dbus-glib.h>
21 # include <dbus/dbus-glib-lowlevel.h>
22 # define RETURN_OK 0
23 # define RETURN_WARN 5
24 # define RETURN_ERROR 10
25 # define RETURN_FAIL 20
26 #endif
28 static int send_command(DBusConnection *bus, int num_args, const char** args);
30 int main(int argc, const char** argv) {
31 int rc = RETURN_OK;
32 DBusError error;
33 DBusConnection* bus;
35 #ifdef __AMIGA__
36 DBUSBase = OpenLibrary("dbus.library", 1);
38 if (DBUSBase == NULL) {
39 fprintf (stderr, "Unable to open dbus.library version 1\n");
40 return RETURN_FAIL;
42 #endif
44 /* Get a connection to the session bus */
45 dbus_error_init (&error);
46 bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
48 if (bus) {
49 #ifdef __AMIGA__
50 /* Set up this connection to work as an Amiga thread */
51 if (dbus_a_setup_connection(bus)) {
52 rc = send_command(bus, argc - 1, argv + 1);
53 dbus_a_cleanup_connection(bus);
55 else {
56 fprintf(stderr, "Failed to set up the Amiga connection\n");
57 rc = RETURN_FAIL;
59 #else
60 /* Set up this connection to work in a GLib event loop */
61 dbus_connection_setup_with_g_main (bus, NULL);
62 rc = send_command(bus, argc - 1, argv + 1);
63 #endif
65 else {
66 fprintf(stderr, "Failed to connect to the D-BUS daemon: %s\n", error.message);
67 rc = RETURN_FAIL;
70 dbus_error_free(&error);
72 #ifdef __AMIGA__
73 CloseLibrary(DBUSBase);
74 #endif
76 return rc;
79 static int send_command(DBusConnection *bus, int num_args, const char** args)
81 int rc = RETURN_OK;
82 int i;
83 DBusMessage* message = NULL;
84 DBusMessage* reply = NULL;
85 DBusError error;
87 dbus_error_init (&error);
89 /* Create a new message "Exec" on the "org.aros.dbus.DCOP" interface,
90 * from the object "/org/aros/dbus/dbus_dcop". */
91 message = dbus_message_new_method_call ("org.aros.dbus.DCOP",
92 "/org/aros/dbus/dbus_dcop",
93 "org.aros.dbus.DCOP",
94 "Exec");
96 if (message != NULL) {
97 /* Append the argumens as strings to the message */
98 for (i = 0; i < num_args; ++i) {
99 dbus_message_append_args (message,
100 DBUS_TYPE_STRING, args[i],
101 DBUS_TYPE_INVALID);
104 /* Send the message */
105 reply = dbus_connection_send_with_reply_and_block (bus, message, -1, &error);
107 /* Free the message now we have finished with it */
108 dbus_message_unref (message);
110 if (reply != NULL) {
111 char* res_str = NULL;
113 if (dbus_message_get_args(reply, &error,
114 DBUS_TYPE_STRING, &res_str,
115 DBUS_TYPE_INVALID)) {
116 if (res_str != NULL) {
117 /* Print out the result */
118 printf("%s", res_str);
120 dbus_free(res_str);
121 rc = RETURN_OK;
123 else {
124 rc = RETURN_WARN;
128 /* Free the reply now we have finished with it */
129 dbus_message_unref (reply);
131 else {
132 fprintf(stderr, "Failed to get a reply from the D-BUS/DCOP bridge: %s\n", error.message);
133 rc = RETURN_ERROR;
136 else {
137 fprintf(stderr, "Failed to send message to the D-BUS/DCOP bridge: %s\n", error.message);
138 rc = RETURN_ERROR;
142 dbus_error_free(&error);
143 return rc;