2 * Based on examples from "Connect desktop apps using D-BUS -- Helping
3 * applications talk to one another", developerWorks 27 Jul 2004,
7 #define DBUS_API_SUBJECT_TO_CHANGE
12 #include <proto/dbus.h>
13 #include <proto/dos.h>
14 #include <proto/exec.h>
16 #include "dbus-amiga.h"
18 struct Library
* DBUSBase
;
20 # include <dbus/dbus-glib.h>
21 # include <dbus/dbus-glib-lowlevel.h>
23 # define RETURN_WARN 5
24 # define RETURN_ERROR 10
25 # define RETURN_FAIL 20
28 static int send_command(DBusConnection
*bus
, int num_args
, const char** args
);
30 int main(int argc
, const char** argv
) {
36 DBUSBase
= OpenLibrary("dbus.library", 1);
38 if (DBUSBase
== NULL
) {
39 fprintf (stderr
, "Unable to open dbus.library version 1\n");
44 /* Get a connection to the session bus */
45 dbus_error_init (&error
);
46 bus
= dbus_bus_get (DBUS_BUS_SESSION
, &error
);
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
);
56 fprintf(stderr
, "Failed to set up the Amiga connection\n");
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);
66 fprintf(stderr
, "Failed to connect to the D-BUS daemon: %s\n", error
.message
);
70 dbus_error_free(&error
);
73 CloseLibrary(DBUSBase
);
79 static int send_command(DBusConnection
*bus
, int num_args
, const char** args
)
83 DBusMessage
* message
= NULL
;
84 DBusMessage
* reply
= NULL
;
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",
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
],
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
);
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
);
128 /* Free the reply now we have finished with it */
129 dbus_message_unref (reply
);
132 fprintf(stderr
, "Failed to get a reply from the D-BUS/DCOP bridge: %s\n", error
.message
);
137 fprintf(stderr
, "Failed to send message to the D-BUS/DCOP bridge: %s\n", error
.message
);
142 dbus_error_free(&error
);