Dispatch dbus messages after method calls when no main loop running
[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-gmain.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     {
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);
79       return ret;
80     }
81
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)
86       || !pending)
87     {
88       g_free (closure);
89       return NULL;
90     }
91   dbus_pending_call_set_notify (pending, set_reply, (void *) closure, g_free);
92
93   closure->reply = NULL;
94   gettimeofday (&tv, NULL);
95   dbus_pending_call_ref (pending);
96   while (!closure->reply)
97     {
98       if (!dbus_connection_read_write_dispatch (bus, dbind_timeout))
99         {
100           //dbus_pending_call_set_notify (pending, NULL, NULL, NULL);
101           dbus_pending_call_cancel (pending);
102           dbus_pending_call_unref (pending);
103           return NULL;
104         }
105       if (time_elapsed (&tv) > dbind_timeout)
106         {
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");
112           return NULL;
113         }
114     }
115   
116   ret = closure->reply;
117   dbus_pending_call_unref (pending);
118   return ret;
119 }
120
121 dbus_bool_t
122 dbind_method_call_reentrant_va (DBusConnection *cnx,
123                                 const char     *bus_name,
124                                 const char     *path,
125                                 const char     *interface,
126                                 const char     *method,
127                                 DBusError      *opt_error,
128                                 const char     *arg_types,
129                                 va_list         args)
130 {
131     dbus_bool_t success = FALSE;
132     DBusMessage *msg = NULL, *reply = NULL;
133     DBusMessageIter iter;
134     DBusError *err, real_err;
135     const char *p;
136   va_list args_demarshal;
137
138   dbus_error_init (&real_err);
139
140   va_copy (args_demarshal, args);
141     if (opt_error)
142         err = opt_error;
143     else {
144         err = &real_err;
145     }
146
147     msg = dbus_message_new_method_call (bus_name, path, interface, method);
148     if (!msg)
149         goto out;
150
151     p = arg_types;
152     dbus_message_iter_init_append (msg, &iter);
153     dbind_any_marshal_va (&iter, &p, args);
154
155     reply = dbind_send_and_allow_reentry (cnx, msg, err);
156     if (!reply)
157         goto out;
158
159     if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
160     {
161       const char *name = dbus_message_get_error_name (reply);
162       goto out;
163     }
164     /* demarshal */
165     if (p[0] == '=' && p[1] == '>')
166     {
167         DBusMessageIter iter;
168         dbus_message_iter_init (reply, &iter);
169         if (strcmp (p + 2, dbus_message_get_signature (reply)) != 0)
170         {
171             g_warning ("dbind: Call to \"%s\" returned signature %s; expected %s",
172                        method, dbus_message_get_signature (reply), p + 2);
173             if (opt_error)
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),
177                                 p + 2);
178             goto out;
179         }
180         p = arg_types;
181         dbind_any_demarshal_va (&iter, &p, args_demarshal);
182     }
183
184     success = TRUE;
185 out:
186     if (msg)
187         dbus_message_unref (msg);
188
189     if (reply)
190         dbus_message_unref (reply);
191
192     if (dbus_error_is_set (&real_err))
193         dbus_error_free (&real_err);
194
195     va_end (args_demarshal);
196     return success;
197 }
198
199 /**
200  * dbind_method_call_reentrant:
201  *
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
204  *             be made.
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.
211  *
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.
215  **/
216 dbus_bool_t
217 dbind_method_call_reentrant (DBusConnection *cnx,
218                              const char     *bus_name,
219                              const char     *path,
220                              const char     *interface,
221                              const char     *method,
222                              DBusError      *opt_error,
223                              const char     *arg_types,
224                              ...)
225 {
226     dbus_bool_t success = FALSE;
227     va_list args;
228
229     va_start (args, arg_types);
230     success = dbind_method_call_reentrant_va (cnx,
231                                               bus_name,
232                                               path,
233                                               interface,
234                                               method,
235                                               opt_error,
236                                               arg_types,
237                                               args);
238     va_end (args);
239
240     return success;
241 }
242
243 /*---------------------------------------------------------------------------*/
244
245 dbus_bool_t
246 dbind_emit_signal_va (DBusConnection *cnx,
247                       const char     *path,
248                       const char     *interface,
249                       const char     *signal,
250                       DBusError      *opt_error,
251                       const char     *arg_types,
252                       va_list         args)
253 {
254     dbus_bool_t success = FALSE;
255     DBusMessage *msg = NULL;
256     DBusMessageIter iter;
257     DBusError *err, real_err;
258     const char *p;
259
260     dbus_error_init (&real_err);
261
262     if (opt_error)
263         err = opt_error;
264     else {
265         err = &real_err;
266     }
267
268     msg = dbus_message_new_signal (path, interface, signal);
269     if (!msg)
270         goto out;
271
272     p = arg_types;
273     dbus_message_iter_init_append (msg, &iter);
274     dbind_any_marshal_va (&iter, &p, args);
275
276     if (!dbus_connection_send (cnx, msg, NULL))
277        goto out;
278
279     success = TRUE;
280 out:
281
282     if (msg)
283         dbus_message_unref (msg);
284
285     if (dbus_error_is_set (&real_err))
286         dbus_error_free (&real_err);
287
288     return success;
289 }
290
291 /**
292  * dbind_emit_signal:
293  *
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.
301  *
302  * Emits a D-Bus signal  using the supplied signal name and argument data.
303  **/
304 dbus_bool_t
305 dbind_emit_signal (DBusConnection *cnx,
306                    const char     *path,
307                    const char     *interface,
308                    const char     *signal,
309                    DBusError      *opt_error,
310                    const char     *arg_types,
311                    ...)
312 {
313     dbus_bool_t success = FALSE;
314     va_list args;
315
316     va_start (args, arg_types);
317     success = dbind_emit_signal_va (cnx, path, interface, signal, opt_error, arg_types, args);
318     va_end (args);
319
320     return success;
321 }
322 void
323 dbind_set_timeout (int timeout)
324 {
325   dbind_timeout = timeout;
326 }
327
328
329 /*END------------------------------------------------------------------------*/