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"
29 static int dbind_timeout = -1;
32 * FIXME: compare types - to ensure they match &
33 * do dynamic padding of structures etc.
36 /*---------------------------------------------------------------------------*/
38 typedef struct _SpiReentrantCallClosure
41 } SpiReentrantCallClosure;
44 set_reply (DBusPendingCall * pending, void *user_data)
46 SpiReentrantCallClosure* closure = (SpiReentrantCallClosure *) user_data;
48 closure->reply = dbus_pending_call_steal_reply (pending);
49 dbus_pending_call_unref (pending);
53 time_elapsed (struct timeval *origin)
57 gettimeofday (&tv, NULL);
58 return (tv.tv_sec - origin->tv_sec) * 1000 + (tv.tv_usec - origin->tv_usec) / 1000;
62 dbind_send_and_allow_reentry (DBusConnection * bus, DBusMessage * message, DBusError *error)
64 DBusPendingCall *pending;
65 SpiReentrantCallClosure closure;
66 const char *unique_name = dbus_bus_get_unique_name (bus);
67 const char *destination = dbus_message_get_destination (message);
70 if (unique_name && destination &&
71 strcmp (destination, unique_name) != 0)
72 return dbus_connection_send_with_reply_and_block (bus, message, dbind_timeout, error);
75 atspi_dbus_connection_setup_with_g_main(bus, NULL);
76 if (!dbus_connection_send_with_reply (bus, message, &pending, dbind_timeout))
80 dbus_pending_call_set_notify (pending, set_reply, (void *) &closure, NULL);
83 gettimeofday (&tv, NULL);
84 while (!closure.reply)
86 if (!dbus_connection_read_write_dispatch (bus, dbind_timeout))
88 if (time_elapsed (&tv) > dbind_timeout)
96 dbind_method_call_reentrant_va (DBusConnection *cnx,
99 const char *interface,
101 DBusError *opt_error,
102 const char *arg_types,
105 dbus_bool_t success = FALSE;
106 DBusMessage *msg = NULL, *reply = NULL;
107 DBusMessageIter iter;
108 DBusError *err, real_err;
110 va_list args_demarshal;
112 va_copy (args_demarshal, args);
116 dbus_error_init (&real_err);
120 msg = dbus_message_new_method_call (bus_name, path, interface, method);
125 dbus_message_iter_init_append (msg, &iter);
126 dbind_any_marshal_va (&iter, &p, args);
128 reply = dbind_send_and_allow_reentry (cnx, msg, err);
132 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
134 const char *name = dbus_message_get_error_name (reply);
138 if (p[0] == '=' && p[1] == '>')
140 DBusMessageIter iter;
141 dbus_message_iter_init (reply, &iter);
143 dbind_any_demarshal_va (&iter, &p, args_demarshal);
149 dbus_message_unref (msg);
152 dbus_message_unref (reply);
154 if (err == &real_err)
155 dbus_error_free (err);
157 va_end (args_demarshal);
162 * dbind_method_call_reentrant:
164 * @cnx: A D-Bus Connection used to make the method call.
165 * @bus_name: The D-Bus bus name of the program where the method call should
167 * @path: The D-Bus object path that should handle the method.
168 * @interface: The D-Bus interface used to scope the method name.
169 * @method: Method to be invoked.
170 * @opt_error: D-Bus error.
171 * @arg_types: Variable length arguments interleaving D-Bus argument types
172 * and pointers to argument data.
174 * Makes a D-Bus method call using the supplied location data, method name and
175 * argument data.This function is re-entrant. It continuously reads from the D-Bus
176 * bus and dispatches messages until a reply has been recieved.
179 dbind_method_call_reentrant (DBusConnection *cnx,
180 const char *bus_name,
182 const char *interface,
184 DBusError *opt_error,
185 const char *arg_types,
188 dbus_bool_t success = FALSE;
191 va_start (args, arg_types);
192 success = dbind_method_call_reentrant_va (cnx,
205 /*---------------------------------------------------------------------------*/
208 dbind_emit_signal_va (DBusConnection *cnx,
210 const char *interface,
212 DBusError *opt_error,
213 const char *arg_types,
216 dbus_bool_t success = FALSE;
217 DBusMessage *msg = NULL;
218 DBusMessageIter iter;
219 DBusError *err, real_err;
225 dbus_error_init (&real_err);
229 msg = dbus_message_new_signal (path, interface, signal);
234 dbus_message_iter_init_append (msg, &iter);
235 dbind_any_marshal_va (&iter, &p, args);
237 if (!dbus_connection_send (cnx, msg, NULL))
244 dbus_message_unref (msg);
246 if (err == &real_err)
247 dbus_error_free (err);
255 * @cnx: A D-Bus Connection used to make the method call.
256 * @path: The D-Bus object path that this signal is emitted from.
257 * @interface: The D-Bus interface used to scope the method name.
258 * @signal: Name of signal to emit.
259 * @opt_error: D-Bus error.
260 * @arg_types: Variable length arguments interleaving D-Bus argument types
261 * and pointers to argument data.
263 * Emits a D-Bus signal using the supplied signal name and argument data.
266 dbind_emit_signal (DBusConnection *cnx,
268 const char *interface,
270 DBusError *opt_error,
271 const char *arg_types,
274 dbus_bool_t success = FALSE;
277 va_start (args, arg_types);
278 success = dbind_emit_signal_va (cnx, path, interface, signal, opt_error, arg_types, args);
284 dbind_set_timeout (int timeout)
286 dbind_timeout = timeout;
290 /*END------------------------------------------------------------------------*/