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)
75 ret = dbus_connection_send_with_reply_and_block (bus, message,
76 dbind_timeout, error);
77 if (g_main_depth () == 0)
78 while (dbus_connection_dispatch (bus) == DBUS_DISPATCH_DATA_REMAINS);
82 closure = g_new0 (SpiReentrantCallClosure, 1);
83 closure->reply = NULL;
84 atspi_dbus_connection_setup_with_g_main(bus, NULL);
85 if (!dbus_connection_send_with_reply (bus, message, &pending, dbind_timeout)
91 dbus_pending_call_set_notify (pending, set_reply, (void *) closure, g_free);
93 closure->reply = NULL;
94 gettimeofday (&tv, NULL);
95 dbus_pending_call_ref (pending);
96 while (!closure->reply)
98 if (!dbus_connection_read_write_dispatch (bus, dbind_timeout))
100 //dbus_pending_call_set_notify (pending, NULL, NULL, NULL);
101 dbus_pending_call_cancel (pending);
102 dbus_pending_call_unref (pending);
105 if (time_elapsed (&tv) > dbind_timeout)
107 //dbus_pending_call_set_notify (pending, NULL, NULL, NULL);
108 dbus_pending_call_cancel (pending);
109 dbus_pending_call_unref (pending);
110 dbus_set_error_const (error, "org.freedesktop.DBus.Error.NoReply",
111 "timeout from dbind");
116 ret = closure->reply;
117 dbus_pending_call_unref (pending);
122 dbind_method_call_reentrant_va (DBusConnection *cnx,
123 const char *bus_name,
125 const char *interface,
127 DBusError *opt_error,
128 const char *arg_types,
131 dbus_bool_t success = FALSE;
132 DBusMessage *msg = NULL, *reply = NULL;
133 DBusMessageIter iter;
134 DBusError *err, real_err;
136 va_list args_demarshal;
138 dbus_error_init (&real_err);
140 va_copy (args_demarshal, args);
147 msg = dbus_message_new_method_call (bus_name, path, interface, method);
152 dbus_message_iter_init_append (msg, &iter);
153 dbind_any_marshal_va (&iter, &p, args);
155 reply = dbind_send_and_allow_reentry (cnx, msg, err);
159 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
161 const char *name = dbus_message_get_error_name (reply);
165 if (p[0] == '=' && p[1] == '>')
167 DBusMessageIter iter;
168 dbus_message_iter_init (reply, &iter);
169 if (strcmp (p + 2, dbus_message_get_signature (reply)) != 0)
171 g_warning ("dbind: Call to \"%s\" returned signature %s; expected %s",
172 method, dbus_message_get_signature (reply), p + 2);
174 dbus_set_error (opt_error, DBUS_ERROR_INVALID_ARGS,
175 "Call to \"%s\" returned signature %s; expected %s",
176 method, dbus_message_get_signature (reply),
181 dbind_any_demarshal_va (&iter, &p, args_demarshal);
187 dbus_message_unref (msg);
190 dbus_message_unref (reply);
192 if (dbus_error_is_set (&real_err))
193 dbus_error_free (&real_err);
195 va_end (args_demarshal);
200 * dbind_method_call_reentrant:
202 * @cnx: A D-Bus Connection used to make the method call.
203 * @bus_name: The D-Bus bus name of the program where the method call should
205 * @path: The D-Bus object path that should handle the method.
206 * @interface: The D-Bus interface used to scope the method name.
207 * @method: Method to be invoked.
208 * @opt_error: D-Bus error.
209 * @arg_types: Variable length arguments interleaving D-Bus argument types
210 * and pointers to argument data.
212 * Makes a D-Bus method call using the supplied location data, method name and
213 * argument data.This function is re-entrant. It continuously reads from the D-Bus
214 * bus and dispatches messages until a reply has been recieved.
217 dbind_method_call_reentrant (DBusConnection *cnx,
218 const char *bus_name,
220 const char *interface,
222 DBusError *opt_error,
223 const char *arg_types,
226 dbus_bool_t success = FALSE;
229 va_start (args, arg_types);
230 success = dbind_method_call_reentrant_va (cnx,
243 /*---------------------------------------------------------------------------*/
246 dbind_emit_signal_va (DBusConnection *cnx,
248 const char *interface,
250 DBusError *opt_error,
251 const char *arg_types,
254 dbus_bool_t success = FALSE;
255 DBusMessage *msg = NULL;
256 DBusMessageIter iter;
257 DBusError *err, real_err;
260 dbus_error_init (&real_err);
268 msg = dbus_message_new_signal (path, interface, signal);
273 dbus_message_iter_init_append (msg, &iter);
274 dbind_any_marshal_va (&iter, &p, args);
276 if (!dbus_connection_send (cnx, msg, NULL))
283 dbus_message_unref (msg);
285 if (dbus_error_is_set (&real_err))
286 dbus_error_free (&real_err);
294 * @cnx: A D-Bus Connection used to make the method call.
295 * @path: The D-Bus object path that this signal is emitted from.
296 * @interface: The D-Bus interface used to scope the method name.
297 * @signal: Name of signal to emit.
298 * @opt_error: D-Bus error.
299 * @arg_types: Variable length arguments interleaving D-Bus argument types
300 * and pointers to argument data.
302 * Emits a D-Bus signal using the supplied signal name and argument data.
305 dbind_emit_signal (DBusConnection *cnx,
307 const char *interface,
309 DBusError *opt_error,
310 const char *arg_types,
313 dbus_bool_t success = FALSE;
316 va_start (args, arg_types);
317 success = dbind_emit_signal_va (cnx, path, interface, signal, opt_error, arg_types, args);
323 dbind_set_timeout (int timeout)
325 dbind_timeout = timeout;
329 /*END------------------------------------------------------------------------*/