8 #include "dbind/dbind.h"
10 static int dbind_timeout = -1;
13 * FIXME: compare types - to ensure they match &
14 * do dynamic padding of structures etc.
17 /*---------------------------------------------------------------------------*/
19 typedef struct _SpiReentrantCallClosure
24 } SpiReentrantCallClosure;
27 set_reply (DBusPendingCall * pending, void *user_data)
29 SpiReentrantCallClosure* closure = (SpiReentrantCallClosure *) user_data;
31 closure->reply = dbus_pending_call_steal_reply (pending);
32 g_main_loop_quit (closure->loop);
36 main_loop_timeout (SpiReentrantCallClosure *closure)
38 g_main_loop_quit (closure->loop);
39 /* Returning TRUE because caller will remove the timer */
44 dbind_send_and_allow_reentry (DBusConnection * bus, DBusMessage * message, DBusError *error)
46 DBusPendingCall *pending;
47 SpiReentrantCallClosure closure;
49 if (strcmp (dbus_message_get_destination (message),
50 dbus_bus_get_unique_name (bus)) != 0)
51 return dbus_connection_send_with_reply_and_block (bus, message, dbind_timeout, error);
53 /* TODO: Figure out why this isn't working */
55 if (!dbus_connection_send_with_reply (bus, message, &pending, dbind_timeout))
57 dbus_pending_call_set_notify (pending, set_reply, (void *) &closure, NULL);
58 closure.loop = g_main_loop_new (NULL, FALSE);
60 dbus_connection_setup_with_g_main(bus, NULL);
64 closure.timeout = g_timeout_add_seconds (2, main_loop_timeout, &closure);
65 g_main_loop_run (closure.loop);
66 g_source_remove (closure.timeout);
71 while (!closure.reply)
73 if (!dbus_connection_read_write_dispatch (bus, dbind_timeout))
78 g_main_loop_unref (closure.loop);
83 dbind_method_call_reentrant_va (DBusConnection *cnx,
86 const char *interface,
89 const char *arg_types,
92 dbus_bool_t success = FALSE;
93 DBusMessage *msg = NULL, *reply = NULL;
95 DBusError *err, real_err;
97 va_list args_demarshal;
99 va_copy (args_demarshal, args);
103 dbus_error_init (&real_err);
107 msg = dbus_message_new_method_call (bus_name, path, interface, method);
112 dbus_message_iter_init_append (msg, &iter);
113 dbind_any_marshal_va (&iter, &p, args);
115 reply = dbind_send_and_allow_reentry (cnx, msg, err);
119 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
121 const char *name = dbus_message_get_error_name (reply);
122 dbus_set_error (err, name, g_strdup (""));
126 if (p[0] == '=' && p[1] == '>')
128 DBusMessageIter iter;
129 dbus_message_iter_init (reply, &iter);
131 dbind_any_demarshal_va (&iter, &p, args_demarshal);
137 dbus_message_unref (msg);
140 dbus_message_unref (reply);
142 if (err == &real_err)
143 dbus_error_free (err);
145 va_end (args_demarshal);
150 * dbind_method_call_reentrant:
152 * @cnx: A D-Bus Connection used to make the method call.
153 * @bus_name: The D-Bus bus name of the program where the method call should
155 * @path: The D-Bus object path that should handle the method.
156 * @interface: The D-Bus interface used to scope the method name.
157 * @method: Method to be invoked.
158 * @opt_error: D-Bus error.
159 * @arg_types: Variable length arguments interleaving D-Bus argument types
160 * and pointers to argument data.
162 * Makes a D-Bus method call using the supplied location data, method name and
163 * argument data.This function is re-entrant. It continuously reads from the D-Bus
164 * bus and dispatches messages until a reply has been recieved.
167 dbind_method_call_reentrant (DBusConnection *cnx,
168 const char *bus_name,
170 const char *interface,
172 DBusError *opt_error,
173 const char *arg_types,
176 dbus_bool_t success = FALSE;
179 va_start (args, arg_types);
180 success = dbind_method_call_reentrant_va (cnx,
193 /*---------------------------------------------------------------------------*/
196 dbind_emit_signal_va (DBusConnection *cnx,
198 const char *interface,
200 DBusError *opt_error,
201 const char *arg_types,
204 dbus_bool_t success = FALSE;
205 DBusMessage *msg = NULL;
206 DBusMessageIter iter;
207 DBusError *err, real_err;
213 dbus_error_init (&real_err);
217 msg = dbus_message_new_signal (path, interface, signal);
222 dbus_message_iter_init_append (msg, &iter);
223 dbind_any_marshal_va (&iter, &p, args);
225 if (!dbus_connection_send (cnx, msg, NULL))
232 dbus_message_unref (msg);
234 if (err == &real_err)
235 dbus_error_free (err);
243 * @cnx: A D-Bus Connection used to make the method call.
244 * @path: The D-Bus object path that this signal is emitted from.
245 * @interface: The D-Bus interface used to scope the method name.
246 * @signal: Name of signal to emit.
247 * @opt_error: D-Bus error.
248 * @arg_types: Variable length arguments interleaving D-Bus argument types
249 * and pointers to argument data.
251 * Emits a D-Bus signal using the supplied signal name and argument data.
254 dbind_emit_signal (DBusConnection *cnx,
256 const char *interface,
258 DBusError *opt_error,
259 const char *arg_types,
262 dbus_bool_t success = FALSE;
265 va_start (args, arg_types);
266 success = dbind_emit_signal_va (cnx, path, interface, signal, opt_error, arg_types, args);
272 dbind_set_timeout (int timeout)
274 dbind_timeout = timeout;
278 /*END------------------------------------------------------------------------*/