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)
168 if (p[0] == '=' && p[1] == '>')
170 DBusMessageIter iter;
171 dbus_message_iter_init (reply, &iter);
172 if (strcmp (p + 2, dbus_message_get_signature (reply)) != 0)
174 g_warning ("dbind: Call to \"%s\" returned signature %s; expected %s",
175 method, dbus_message_get_signature (reply), p + 2);
177 dbus_set_error (opt_error, DBUS_ERROR_INVALID_ARGS,
178 "Call to \"%s\" returned signature %s; expected %s",
179 method, dbus_message_get_signature (reply),
184 dbind_any_demarshal_va (&iter, &p, args_demarshal);
190 dbus_message_unref (msg);
193 dbus_message_unref (reply);
195 if (dbus_error_is_set (&real_err))
196 dbus_error_free (&real_err);
198 va_end (args_demarshal);
203 * dbind_method_call_reentrant:
205 * @cnx: A D-Bus Connection used to make the method call.
206 * @bus_name: The D-Bus bus name of the program where the method call should
208 * @path: The D-Bus object path that should handle the method.
209 * @interface: The D-Bus interface used to scope the method name.
210 * @method: Method to be invoked.
211 * @opt_error: D-Bus error.
212 * @arg_types: Variable length arguments interleaving D-Bus argument types
213 * and pointers to argument data.
215 * Makes a D-Bus method call using the supplied location data, method name and
216 * argument data.This function is re-entrant. It continuously reads from the D-Bus
217 * bus and dispatches messages until a reply has been recieved.
220 dbind_method_call_reentrant (DBusConnection *cnx,
221 const char *bus_name,
223 const char *interface,
225 DBusError *opt_error,
226 const char *arg_types,
229 dbus_bool_t success = FALSE;
232 va_start (args, arg_types);
233 success = dbind_method_call_reentrant_va (cnx,
246 /*---------------------------------------------------------------------------*/
248 /* TODO: opt_error is unused; should be removed */
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;
263 msg = dbus_message_new_signal (path, interface, signal);
268 dbus_message_iter_init_append (msg, &iter);
269 dbind_any_marshal_va (&iter, &p, args);
271 if (!dbus_connection_send (cnx, msg, NULL))
278 dbus_message_unref (msg);
286 * @cnx: A D-Bus Connection used to make the method call.
287 * @path: The D-Bus object path that this signal is emitted from.
288 * @interface: The D-Bus interface used to scope the method name.
289 * @signal: Name of signal to emit.
290 * @opt_error: D-Bus error.
291 * @arg_types: Variable length arguments interleaving D-Bus argument types
292 * and pointers to argument data.
294 * Emits a D-Bus signal using the supplied signal name and argument data.
297 dbind_emit_signal (DBusConnection *cnx,
299 const char *interface,
301 DBusError *opt_error,
302 const char *arg_types,
305 dbus_bool_t success = FALSE;
308 va_start (args, arg_types);
309 success = dbind_emit_signal_va (cnx, path, interface, signal, opt_error, arg_types, args);
315 dbind_set_timeout (int timeout)
317 dbind_timeout = timeout;
321 /*END------------------------------------------------------------------------*/