Dynamically allocate reentrant call closures
[platform/upstream/at-spi2-core.git] / dbind / dbind.c
1 /*
2  * Copyright 2008-2011 Novell, Inc.
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <sys/time.h>
23 #include <string.h>
24 #include <glib.h>
25
26 #include "config.h"
27 #include "dbind/dbind.h"
28 #include "atspi/atspi.h"
29
30 static int dbind_timeout = -1;
31
32 /*
33  * FIXME: compare types - to ensure they match &
34  *        do dynamic padding of structures etc.
35  */
36
37 /*---------------------------------------------------------------------------*/
38
39 typedef struct _SpiReentrantCallClosure 
40 {
41   DBusMessage *reply;
42 } SpiReentrantCallClosure;
43
44 static void
45 set_reply (DBusPendingCall * pending, void *user_data)
46 {
47   SpiReentrantCallClosure* closure = (SpiReentrantCallClosure *) user_data; 
48
49   closure->reply = dbus_pending_call_steal_reply (pending);
50   dbus_pending_call_unref (pending);
51 }
52
53 static gint
54 time_elapsed (struct timeval *origin)
55 {
56   struct timeval tv;
57
58   gettimeofday (&tv, NULL);
59   return (tv.tv_sec - origin->tv_sec) * 1000 + (tv.tv_usec - origin->tv_usec) / 1000;
60 }
61
62 DBusMessage *
63 dbind_send_and_allow_reentry (DBusConnection * bus, DBusMessage * message, DBusError *error)
64 {
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);
69   struct timeval tv;
70   DBusMessage *ret;
71
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);
75
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))
80       return NULL;
81   if (!pending)
82     return NULL;
83   dbus_pending_call_set_notify (pending, set_reply, (void *) closure, g_free);
84
85   closure->reply = NULL;
86   gettimeofday (&tv, NULL);
87   dbus_pending_call_ref (pending);
88   while (!closure->reply)
89     {
90       if (!dbus_connection_read_write_dispatch (bus, dbind_timeout) ||
91           time_elapsed (&tv) > dbind_timeout)
92         {
93           dbus_pending_call_unref (pending);
94           return NULL;
95         }
96     }
97   
98   ret = closure->reply;
99   dbus_pending_call_unref (pending);
100   return ret;
101 }
102
103 dbus_bool_t
104 dbind_method_call_reentrant_va (DBusConnection *cnx,
105                                 const char     *bus_name,
106                                 const char     *path,
107                                 const char     *interface,
108                                 const char     *method,
109                                 DBusError      *opt_error,
110                                 const char     *arg_types,
111                                 va_list         args)
112 {
113     dbus_bool_t success = FALSE;
114     DBusMessage *msg = NULL, *reply = NULL;
115     DBusMessageIter iter;
116     DBusError *err, real_err;
117     const char *p;
118   va_list args_demarshal;
119
120   va_copy (args_demarshal, args);
121     if (opt_error)
122         err = opt_error;
123     else {
124         dbus_error_init (&real_err);
125         err = &real_err;
126     }
127
128     msg = dbus_message_new_method_call (bus_name, path, interface, method);
129     if (!msg)
130         goto out;
131
132     p = arg_types;
133     dbus_message_iter_init_append (msg, &iter);
134     dbind_any_marshal_va (&iter, &p, args);
135
136     reply = dbind_send_and_allow_reentry (cnx, msg, err);
137     if (!reply)
138         goto out;
139
140     if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
141     {
142       const char *name = dbus_message_get_error_name (reply);
143       goto out;
144     }
145     /* demarshal */
146     if (p[0] == '=' && p[1] == '>')
147     {
148         DBusMessageIter iter;
149         dbus_message_iter_init (reply, &iter);
150         p = arg_types;
151         dbind_any_demarshal_va (&iter, &p, args_demarshal);
152     }
153
154     success = TRUE;
155 out:
156     if (msg)
157         dbus_message_unref (msg);
158
159     if (reply)
160         dbus_message_unref (reply);
161
162     if (err == &real_err)
163         dbus_error_free (err);
164
165     va_end (args_demarshal);
166     return success;
167 }
168
169 /**
170  * dbind_method_call_reentrant:
171  *
172  * @cnx:       A D-Bus Connection used to make the method call.
173  * @bus_name:  The D-Bus bus name of the program where the method call should
174  *             be made.
175  * @path:      The D-Bus object path that should handle the method.
176  * @interface: The D-Bus interface used to scope the method name.
177  * @method:    Method to be invoked.
178  * @opt_error: D-Bus error.
179  * @arg_types: Variable length arguments interleaving D-Bus argument types
180  *             and pointers to argument data.
181  *
182  * Makes a D-Bus method call using the supplied location data, method name and
183  * argument data.This function is re-entrant. It continuously reads from the D-Bus
184  * bus and dispatches messages until a reply has been recieved.
185  **/
186 dbus_bool_t
187 dbind_method_call_reentrant (DBusConnection *cnx,
188                              const char     *bus_name,
189                              const char     *path,
190                              const char     *interface,
191                              const char     *method,
192                              DBusError      *opt_error,
193                              const char     *arg_types,
194                              ...)
195 {
196     dbus_bool_t success = FALSE;
197     va_list args;
198
199     va_start (args, arg_types);
200     success = dbind_method_call_reentrant_va (cnx,
201                                               bus_name,
202                                               path,
203                                               interface,
204                                               method,
205                                               opt_error,
206                                               arg_types,
207                                               args);
208     va_end (args);
209
210     return success;
211 }
212
213 /*---------------------------------------------------------------------------*/
214
215 dbus_bool_t
216 dbind_emit_signal_va (DBusConnection *cnx,
217                       const char     *path,
218                       const char     *interface,
219                       const char     *signal,
220                       DBusError      *opt_error,
221                       const char     *arg_types,
222                       va_list         args)
223 {
224     dbus_bool_t success = FALSE;
225     DBusMessage *msg = NULL;
226     DBusMessageIter iter;
227     DBusError *err, real_err;
228     const char *p;
229
230     if (opt_error)
231         err = opt_error;
232     else {
233         dbus_error_init (&real_err);
234         err = &real_err;
235     }
236
237     msg = dbus_message_new_signal (path, interface, signal);
238     if (!msg)
239         goto out;
240
241     p = arg_types;
242     dbus_message_iter_init_append (msg, &iter);
243     dbind_any_marshal_va (&iter, &p, args);
244
245     if (!dbus_connection_send (cnx, msg, NULL))
246        goto out;
247
248     success = TRUE;
249 out:
250
251     if (msg)
252         dbus_message_unref (msg);
253
254     if (err == &real_err)
255         dbus_error_free (err);
256
257     return success;
258 }
259
260 /**
261  * dbind_emit_signal:
262  *
263  * @cnx:       A D-Bus Connection used to make the method call.
264  * @path:      The D-Bus object path that this signal is emitted from.
265  * @interface: The D-Bus interface used to scope the method name.
266  * @signal:    Name of signal to emit.
267  * @opt_error: D-Bus error.
268  * @arg_types: Variable length arguments interleaving D-Bus argument types
269  *             and pointers to argument data.
270  *
271  * Emits a D-Bus signal  using the supplied signal name and argument data.
272  **/
273 dbus_bool_t
274 dbind_emit_signal (DBusConnection *cnx,
275                    const char     *path,
276                    const char     *interface,
277                    const char     *signal,
278                    DBusError      *opt_error,
279                    const char     *arg_types,
280                    ...)
281 {
282     dbus_bool_t success = FALSE;
283     va_list args;
284
285     va_start (args, arg_types);
286     success = dbind_emit_signal_va (cnx, path, interface, signal, opt_error, arg_types, args);
287     va_end (args);
288
289     return success;
290 }
291 void
292 dbind_set_timeout (int timeout)
293 {
294   dbind_timeout = timeout;
295 }
296
297
298 /*END------------------------------------------------------------------------*/