2 * Copyright 2008-2011 Novell, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
27 #include "dbind/dbind.h"
28 #include "atspi/atspi-gmain.h"
30 static int dbind_timeout = -1;
33 * FIXME: compare types - to ensure they match &
34 * do dynamic padding of structures etc.
37 /*---------------------------------------------------------------------------*/
39 typedef struct _SpiReentrantCallClosure
42 } SpiReentrantCallClosure;
45 set_reply (DBusPendingCall * pending, void *user_data)
47 SpiReentrantCallClosure* closure = (SpiReentrantCallClosure *) user_data;
49 closure->reply = dbus_pending_call_steal_reply (pending);
50 dbus_pending_call_unref (pending);
54 time_elapsed (struct timeval *origin)
58 gettimeofday (&tv, NULL);
59 return (tv.tv_sec - origin->tv_sec) * 1000 + (tv.tv_usec - origin->tv_usec) / 1000;
63 dbind_send_and_allow_reentry (DBusConnection * bus, DBusMessage * message, DBusError *error)
65 DBusPendingCall *pending;
66 SpiReentrantCallClosure *closure;
67 const char *unique_name = dbus_bus_get_unique_name (bus);
68 const char *destination = dbus_message_get_destination (message);
72 if (unique_name && destination &&
73 strcmp (destination, unique_name) != 0)
74 return dbus_connection_send_with_reply_and_block (bus, message, dbind_timeout, error);
76 closure = g_new0 (SpiReentrantCallClosure, 1);
77 closure->reply = NULL;
78 atspi_dbus_connection_setup_with_g_main(bus, NULL);
79 if (!dbus_connection_send_with_reply (bus, message, &pending, dbind_timeout))
83 dbus_pending_call_set_notify (pending, set_reply, (void *) closure, g_free);
85 closure->reply = NULL;
86 gettimeofday (&tv, NULL);
87 dbus_pending_call_ref (pending);
88 while (!closure->reply)
90 if (!dbus_connection_read_write_dispatch (bus, dbind_timeout))
92 dbus_pending_call_unref (pending);
95 if (time_elapsed (&tv) > dbind_timeout)
97 dbus_pending_call_unref (pending);
98 dbus_set_error_const (error, "org.freedesktop.DBus.Error.NoReply",
99 "timeout from dbind");
104 ret = closure->reply;
105 dbus_pending_call_unref (pending);
110 dbind_method_call_reentrant_va (DBusConnection *cnx,
111 const char *bus_name,
113 const char *interface,
115 DBusError *opt_error,
116 const char *arg_types,
119 dbus_bool_t success = FALSE;
120 DBusMessage *msg = NULL, *reply = NULL;
121 DBusMessageIter iter;
122 DBusError *err, real_err;
124 va_list args_demarshal;
126 va_copy (args_demarshal, args);
130 dbus_error_init (&real_err);
134 msg = dbus_message_new_method_call (bus_name, path, interface, method);
139 dbus_message_iter_init_append (msg, &iter);
140 dbind_any_marshal_va (&iter, &p, args);
142 reply = dbind_send_and_allow_reentry (cnx, msg, err);
146 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
148 const char *name = dbus_message_get_error_name (reply);
152 if (p[0] == '=' && p[1] == '>')
154 DBusMessageIter iter;
155 dbus_message_iter_init (reply, &iter);
157 dbind_any_demarshal_va (&iter, &p, args_demarshal);
163 dbus_message_unref (msg);
166 dbus_message_unref (reply);
168 if (err == &real_err)
169 dbus_error_free (err);
171 va_end (args_demarshal);
176 * dbind_method_call_reentrant:
178 * @cnx: A D-Bus Connection used to make the method call.
179 * @bus_name: The D-Bus bus name of the program where the method call should
181 * @path: The D-Bus object path that should handle the method.
182 * @interface: The D-Bus interface used to scope the method name.
183 * @method: Method to be invoked.
184 * @opt_error: D-Bus error.
185 * @arg_types: Variable length arguments interleaving D-Bus argument types
186 * and pointers to argument data.
188 * Makes a D-Bus method call using the supplied location data, method name and
189 * argument data.This function is re-entrant. It continuously reads from the D-Bus
190 * bus and dispatches messages until a reply has been recieved.
193 dbind_method_call_reentrant (DBusConnection *cnx,
194 const char *bus_name,
196 const char *interface,
198 DBusError *opt_error,
199 const char *arg_types,
202 dbus_bool_t success = FALSE;
205 va_start (args, arg_types);
206 success = dbind_method_call_reentrant_va (cnx,
219 /*---------------------------------------------------------------------------*/
222 dbind_emit_signal_va (DBusConnection *cnx,
224 const char *interface,
226 DBusError *opt_error,
227 const char *arg_types,
230 dbus_bool_t success = FALSE;
231 DBusMessage *msg = NULL;
232 DBusMessageIter iter;
233 DBusError *err, real_err;
239 dbus_error_init (&real_err);
243 msg = dbus_message_new_signal (path, interface, signal);
248 dbus_message_iter_init_append (msg, &iter);
249 dbind_any_marshal_va (&iter, &p, args);
251 if (!dbus_connection_send (cnx, msg, NULL))
258 dbus_message_unref (msg);
260 if (err == &real_err)
261 dbus_error_free (err);
269 * @cnx: A D-Bus Connection used to make the method call.
270 * @path: The D-Bus object path that this signal is emitted from.
271 * @interface: The D-Bus interface used to scope the method name.
272 * @signal: Name of signal to emit.
273 * @opt_error: D-Bus error.
274 * @arg_types: Variable length arguments interleaving D-Bus argument types
275 * and pointers to argument data.
277 * Emits a D-Bus signal using the supplied signal name and argument data.
280 dbind_emit_signal (DBusConnection *cnx,
282 const char *interface,
284 DBusError *opt_error,
285 const char *arg_types,
288 dbus_bool_t success = FALSE;
291 va_start (args, arg_types);
292 success = dbind_emit_signal_va (cnx, path, interface, signal, opt_error, arg_types, args);
298 dbind_set_timeout (int timeout)
300 dbind_timeout = timeout;
304 /*END------------------------------------------------------------------------*/