Imported version 2.7.91
[platform/core/uifw/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     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         {
92           dbus_pending_call_unref (pending);
93           return NULL;
94         }
95       if (time_elapsed (&tv) > dbind_timeout)
96         {
97           dbus_pending_call_unref (pending);
98           dbus_set_error_const (error, "org.freedesktop.DBus.Error.NoReply",
99                                 "timeout from dbind");
100           return NULL;
101         }
102     }
103   
104   ret = closure->reply;
105   dbus_pending_call_unref (pending);
106   return ret;
107 }
108
109 dbus_bool_t
110 dbind_method_call_reentrant_va (DBusConnection *cnx,
111                                 const char     *bus_name,
112                                 const char     *path,
113                                 const char     *interface,
114                                 const char     *method,
115                                 DBusError      *opt_error,
116                                 const char     *arg_types,
117                                 va_list         args)
118 {
119     dbus_bool_t success = FALSE;
120     DBusMessage *msg = NULL, *reply = NULL;
121     DBusMessageIter iter;
122     DBusError *err, real_err;
123     const char *p;
124   va_list args_demarshal;
125
126   dbus_error_init (&real_err);
127
128   va_copy (args_demarshal, args);
129     if (opt_error)
130         err = opt_error;
131     else {
132         err = &real_err;
133     }
134
135     (void)err;
136
137     msg = dbus_message_new_method_call (bus_name, path, interface, method);
138     if (!msg)
139         goto out;
140
141     p = arg_types;
142     dbus_message_iter_init_append (msg, &iter);
143     dbind_any_marshal_va (&iter, &p, args);
144
145     reply = dbind_send_and_allow_reentry (cnx, msg, err);
146     if (!reply)
147         goto out;
148
149     if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
150     {
151       dbus_message_get_error_name (reply);
152       goto out;
153     }
154     /* demarshal */
155     if (p[0] == '=' && p[1] == '>')
156     {
157         DBusMessageIter iter;
158         dbus_message_iter_init (reply, &iter);
159         if (strcmp (p + 2, dbus_message_get_signature (reply)) != 0)
160         {
161             g_warning ("dbind: Call to \"%s\" returned signature %s; expected %s",
162                        method, dbus_message_get_signature (reply), p + 2);
163             if (opt_error)
164                 dbus_set_error (opt_error, DBUS_ERROR_INVALID_ARGS,
165                                 "Call to \"%s\" returned signature %s; expected %s",
166                                 method, dbus_message_get_signature (reply),
167                                 p + 2);
168             goto out;
169         }
170         p = arg_types;
171         dbind_any_demarshal_va (&iter, &p, args_demarshal);
172     }
173
174     success = TRUE;
175 out:
176     if (msg)
177         dbus_message_unref (msg);
178
179     if (reply)
180         dbus_message_unref (reply);
181
182     if (dbus_error_is_set (&real_err))
183         dbus_error_free (&real_err);
184
185     va_end (args_demarshal);
186     return success;
187 }
188
189 /**
190  * dbind_method_call_reentrant:
191  *
192  * @cnx:       A D-Bus Connection used to make the method call.
193  * @bus_name:  The D-Bus bus name of the program where the method call should
194  *             be made.
195  * @path:      The D-Bus object path that should handle the method.
196  * @interface: The D-Bus interface used to scope the method name.
197  * @method:    Method to be invoked.
198  * @opt_error: D-Bus error.
199  * @arg_types: Variable length arguments interleaving D-Bus argument types
200  *             and pointers to argument data.
201  *
202  * Makes a D-Bus method call using the supplied location data, method name and
203  * argument data.This function is re-entrant. It continuously reads from the D-Bus
204  * bus and dispatches messages until a reply has been recieved.
205  **/
206 dbus_bool_t
207 dbind_method_call_reentrant (DBusConnection *cnx,
208                              const char     *bus_name,
209                              const char     *path,
210                              const char     *interface,
211                              const char     *method,
212                              DBusError      *opt_error,
213                              const char     *arg_types,
214                              ...)
215 {
216     dbus_bool_t success = FALSE;
217     va_list args;
218
219     va_start (args, arg_types);
220     success = dbind_method_call_reentrant_va (cnx,
221                                               bus_name,
222                                               path,
223                                               interface,
224                                               method,
225                                               opt_error,
226                                               arg_types,
227                                               args);
228     va_end (args);
229
230     return success;
231 }
232
233 /*---------------------------------------------------------------------------*/
234
235 dbus_bool_t
236 dbind_emit_signal_va (DBusConnection *cnx,
237                       const char     *path,
238                       const char     *interface,
239                       const char     *signal,
240                       DBusError      *opt_error,
241                       const char     *arg_types,
242                       va_list         args)
243 {
244     dbus_bool_t success = FALSE;
245     DBusMessage *msg = NULL;
246     DBusMessageIter iter;
247     DBusError *err, real_err;
248     const char *p;
249
250     dbus_error_init (&real_err);
251
252     if (opt_error)
253         err = opt_error;
254     else {
255         err = &real_err;
256     }
257
258     (void)err;
259
260     msg = dbus_message_new_signal (path, interface, signal);
261     if (!msg)
262         goto out;
263
264     p = arg_types;
265     dbus_message_iter_init_append (msg, &iter);
266     dbind_any_marshal_va (&iter, &p, args);
267
268     if (!dbus_connection_send (cnx, msg, NULL))
269        goto out;
270
271     success = TRUE;
272 out:
273
274     if (msg)
275         dbus_message_unref (msg);
276
277     if (dbus_error_is_set (&real_err))
278         dbus_error_free (&real_err);
279
280     return success;
281 }
282
283 /**
284  * dbind_emit_signal:
285  *
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.
293  *
294  * Emits a D-Bus signal  using the supplied signal name and argument data.
295  **/
296 dbus_bool_t
297 dbind_emit_signal (DBusConnection *cnx,
298                    const char     *path,
299                    const char     *interface,
300                    const char     *signal,
301                    DBusError      *opt_error,
302                    const char     *arg_types,
303                    ...)
304 {
305     dbus_bool_t success = FALSE;
306     va_list args;
307
308     va_start (args, arg_types);
309     success = dbind_emit_signal_va (cnx, path, interface, signal, opt_error, arg_types, args);
310     va_end (args);
311
312     return success;
313 }
314 void
315 dbind_set_timeout (int timeout)
316 {
317   dbind_timeout = timeout;
318 }
319
320
321 /*END------------------------------------------------------------------------*/