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);
71 static gboolean in_dispatch = FALSE;
73 if (unique_name && destination &&
74 strcmp (destination, unique_name) != 0)
76 ret = dbus_connection_send_with_reply_and_block (bus, message,
77 dbind_timeout, error);
78 if (g_main_depth () == 0 && !in_dispatch)
81 while (dbus_connection_dispatch (bus) == DBUS_DISPATCH_DATA_REMAINS);
87 closure = g_new0 (SpiReentrantCallClosure, 1);
88 closure->reply = NULL;
89 if (!dbus_connection_send_with_reply (bus, message, &pending, dbind_timeout)
95 dbus_pending_call_set_notify (pending, set_reply, (void *) closure, g_free);
97 closure->reply = NULL;
98 gettimeofday (&tv, NULL);
99 dbus_pending_call_ref (pending);
100 while (!closure->reply)
102 if (!dbus_connection_read_write_dispatch (bus, dbind_timeout))
104 //dbus_pending_call_set_notify (pending, NULL, NULL, NULL);
105 dbus_pending_call_cancel (pending);
106 dbus_pending_call_unref (pending);
109 if (time_elapsed (&tv) > dbind_timeout)
111 //dbus_pending_call_set_notify (pending, NULL, NULL, NULL);
112 dbus_pending_call_cancel (pending);
113 dbus_pending_call_unref (pending);
114 dbus_set_error_const (error, "org.freedesktop.DBus.Error.NoReply",
115 "timeout from dbind");
120 ret = closure->reply;
121 dbus_pending_call_unref (pending);
126 dbind_method_call_reentrant_va (DBusConnection *cnx,
127 const char *bus_name,
129 const char *interface,
131 DBusError *opt_error,
132 const char *arg_types,
135 dbus_bool_t success = FALSE;
136 DBusMessage *msg = NULL, *reply = NULL;
137 DBusMessageIter iter;
138 DBusError *err, real_err;
140 va_list args_demarshal;
142 dbus_error_init (&real_err);
144 va_copy (args_demarshal, args);
151 msg = dbus_message_new_method_call (bus_name, path, interface, method);
156 dbus_message_iter_init_append (msg, &iter);
157 dbind_any_marshal_va (&iter, &p, args);
159 reply = dbind_send_and_allow_reentry (cnx, msg, err);
163 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
165 const char *name = dbus_message_get_error_name (reply);
169 if (p[0] == '=' && p[1] == '>')
171 DBusMessageIter iter;
172 dbus_message_iter_init (reply, &iter);
173 if (strcmp (p + 2, dbus_message_get_signature (reply)) != 0)
175 g_warning ("dbind: Call to \"%s\" returned signature %s; expected %s",
176 method, dbus_message_get_signature (reply), p + 2);
178 dbus_set_error (opt_error, DBUS_ERROR_INVALID_ARGS,
179 "Call to \"%s\" returned signature %s; expected %s",
180 method, dbus_message_get_signature (reply),
185 dbind_any_demarshal_va (&iter, &p, args_demarshal);
191 dbus_message_unref (msg);
194 dbus_message_unref (reply);
196 if (dbus_error_is_set (&real_err))
197 dbus_error_free (&real_err);
199 va_end (args_demarshal);
204 * dbind_method_call_reentrant:
206 * @cnx: A D-Bus Connection used to make the method call.
207 * @bus_name: The D-Bus bus name of the program where the method call should
209 * @path: The D-Bus object path that should handle the method.
210 * @interface: The D-Bus interface used to scope the method name.
211 * @method: Method to be invoked.
212 * @opt_error: D-Bus error.
213 * @arg_types: Variable length arguments interleaving D-Bus argument types
214 * and pointers to argument data.
216 * Makes a D-Bus method call using the supplied location data, method name and
217 * argument data.This function is re-entrant. It continuously reads from the D-Bus
218 * bus and dispatches messages until a reply has been recieved.
221 dbind_method_call_reentrant (DBusConnection *cnx,
222 const char *bus_name,
224 const char *interface,
226 DBusError *opt_error,
227 const char *arg_types,
230 dbus_bool_t success = FALSE;
233 va_start (args, arg_types);
234 success = dbind_method_call_reentrant_va (cnx,
247 /*---------------------------------------------------------------------------*/
250 dbind_emit_signal_va (DBusConnection *cnx,
252 const char *interface,
254 DBusError *opt_error,
255 const char *arg_types,
258 dbus_bool_t success = FALSE;
259 DBusMessage *msg = NULL;
260 DBusMessageIter iter;
261 DBusError *err, real_err;
264 dbus_error_init (&real_err);
272 msg = dbus_message_new_signal (path, interface, signal);
277 dbus_message_iter_init_append (msg, &iter);
278 dbind_any_marshal_va (&iter, &p, args);
280 if (!dbus_connection_send (cnx, msg, NULL))
287 dbus_message_unref (msg);
289 if (dbus_error_is_set (&real_err))
290 dbus_error_free (&real_err);
298 * @cnx: A D-Bus Connection used to make the method call.
299 * @path: The D-Bus object path that this signal is emitted from.
300 * @interface: The D-Bus interface used to scope the method name.
301 * @signal: Name of signal to emit.
302 * @opt_error: D-Bus error.
303 * @arg_types: Variable length arguments interleaving D-Bus argument types
304 * and pointers to argument data.
306 * Emits a D-Bus signal using the supplied signal name and argument data.
309 dbind_emit_signal (DBusConnection *cnx,
311 const char *interface,
313 DBusError *opt_error,
314 const char *arg_types,
317 dbus_bool_t success = FALSE;
320 va_start (args, arg_types);
321 success = dbind_emit_signal_va (cnx, path, interface, signal, opt_error, arg_types, args);
327 dbind_set_timeout (int timeout)
329 dbind_timeout = timeout;
333 /*END------------------------------------------------------------------------*/