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