9 #include "dbind/dbind.h"
11 static int dbind_timeout = -1;
14 * FIXME: compare types - to ensure they match &
15 * do dynamic padding of structures etc.
18 /*---------------------------------------------------------------------------*/
20 typedef struct _SpiReentrantCallClosure
23 } SpiReentrantCallClosure;
26 set_reply (DBusPendingCall * pending, void *user_data)
28 SpiReentrantCallClosure* closure = (SpiReentrantCallClosure *) user_data;
30 closure->reply = dbus_pending_call_steal_reply (pending);
31 dbus_pending_call_unref (pending);
35 time_elapsed (struct timeval *origin)
39 gettimeofday (&tv, NULL);
40 return (tv.tv_sec - origin->tv_sec) * 1000 + (tv.tv_usec - origin->tv_usec) / 1000;
44 dbind_send_and_allow_reentry (DBusConnection * bus, DBusMessage * message, DBusError *error)
46 DBusPendingCall *pending;
47 SpiReentrantCallClosure closure;
48 const char *unique_name = dbus_bus_get_unique_name (bus);
49 const char *destination = dbus_message_get_destination (message);
52 if (unique_name && destination &&
53 strcmp (destination, unique_name) != 0)
54 return dbus_connection_send_with_reply_and_block (bus, message, dbind_timeout, error);
57 dbus_connection_setup_with_g_main(bus, NULL);
58 if (!dbus_connection_send_with_reply (bus, message, &pending, dbind_timeout))
62 dbus_pending_call_set_notify (pending, set_reply, (void *) &closure, NULL);
65 gettimeofday (&tv, NULL);
66 while (!closure.reply)
68 if (!dbus_connection_read_write_dispatch (bus, dbind_timeout))
70 if (time_elapsed (&tv) > dbind_timeout)
78 dbind_method_call_reentrant_va (DBusConnection *cnx,
81 const char *interface,
84 const char *arg_types,
87 dbus_bool_t success = FALSE;
88 DBusMessage *msg = NULL, *reply = NULL;
90 DBusError *err, real_err;
92 va_list args_demarshal;
94 va_copy (args_demarshal, args);
98 dbus_error_init (&real_err);
102 msg = dbus_message_new_method_call (bus_name, path, interface, method);
107 dbus_message_iter_init_append (msg, &iter);
108 dbind_any_marshal_va (&iter, &p, args);
110 reply = dbind_send_and_allow_reentry (cnx, msg, err);
114 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
116 const char *name = dbus_message_get_error_name (reply);
120 if (p[0] == '=' && p[1] == '>')
122 DBusMessageIter iter;
123 dbus_message_iter_init (reply, &iter);
125 dbind_any_demarshal_va (&iter, &p, args_demarshal);
131 dbus_message_unref (msg);
134 dbus_message_unref (reply);
136 if (err == &real_err)
137 dbus_error_free (err);
139 va_end (args_demarshal);
144 * dbind_method_call_reentrant:
146 * @cnx: A D-Bus Connection used to make the method call.
147 * @bus_name: The D-Bus bus name of the program where the method call should
149 * @path: The D-Bus object path that should handle the method.
150 * @interface: The D-Bus interface used to scope the method name.
151 * @method: Method to be invoked.
152 * @opt_error: D-Bus error.
153 * @arg_types: Variable length arguments interleaving D-Bus argument types
154 * and pointers to argument data.
156 * Makes a D-Bus method call using the supplied location data, method name and
157 * argument data.This function is re-entrant. It continuously reads from the D-Bus
158 * bus and dispatches messages until a reply has been recieved.
161 dbind_method_call_reentrant (DBusConnection *cnx,
162 const char *bus_name,
164 const char *interface,
166 DBusError *opt_error,
167 const char *arg_types,
170 dbus_bool_t success = FALSE;
173 va_start (args, arg_types);
174 success = dbind_method_call_reentrant_va (cnx,
187 /*---------------------------------------------------------------------------*/
190 dbind_emit_signal_va (DBusConnection *cnx,
192 const char *interface,
194 DBusError *opt_error,
195 const char *arg_types,
198 dbus_bool_t success = FALSE;
199 DBusMessage *msg = NULL;
200 DBusMessageIter iter;
201 DBusError *err, real_err;
207 dbus_error_init (&real_err);
211 msg = dbus_message_new_signal (path, interface, signal);
216 dbus_message_iter_init_append (msg, &iter);
217 dbind_any_marshal_va (&iter, &p, args);
219 if (!dbus_connection_send (cnx, msg, NULL))
226 dbus_message_unref (msg);
228 if (err == &real_err)
229 dbus_error_free (err);
237 * @cnx: A D-Bus Connection used to make the method call.
238 * @path: The D-Bus object path that this signal is emitted from.
239 * @interface: The D-Bus interface used to scope the method name.
240 * @signal: Name of signal to emit.
241 * @opt_error: D-Bus error.
242 * @arg_types: Variable length arguments interleaving D-Bus argument types
243 * and pointers to argument data.
245 * Emits a D-Bus signal using the supplied signal name and argument data.
248 dbind_emit_signal (DBusConnection *cnx,
250 const char *interface,
252 DBusError *opt_error,
253 const char *arg_types,
256 dbus_bool_t success = FALSE;
259 va_start (args, arg_types);
260 success = dbind_emit_signal_va (cnx, path, interface, signal, opt_error, arg_types, args);
266 dbind_set_timeout (int timeout)
268 dbind_timeout = timeout;
272 /*END------------------------------------------------------------------------*/