Add some missing prototypes
[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
71   if (unique_name && destination &&
72       strcmp (destination, unique_name) != 0)
73     return dbus_connection_send_with_reply_and_block (bus, message, dbind_timeout, error);
74
75   closure.reply = NULL;
76   atspi_dbus_connection_setup_with_g_main(bus, NULL);
77   if (!dbus_connection_send_with_reply (bus, message, &pending, dbind_timeout))
78       return NULL;
79   if (!pending)
80     return NULL;
81   dbus_pending_call_set_notify (pending, set_reply, (void *) &closure, NULL);
82
83   closure.reply = NULL;
84   gettimeofday (&tv, NULL);
85   while (!closure.reply)
86     {
87       if (!dbus_connection_read_write_dispatch (bus, dbind_timeout))
88         return NULL;
89 if (time_elapsed (&tv) > dbind_timeout)
90         return NULL;
91     }
92   
93   return closure.reply;
94 }
95
96 dbus_bool_t
97 dbind_method_call_reentrant_va (DBusConnection *cnx,
98                                 const char     *bus_name,
99                                 const char     *path,
100                                 const char     *interface,
101                                 const char     *method,
102                                 DBusError      *opt_error,
103                                 const char     *arg_types,
104                                 va_list         args)
105 {
106     dbus_bool_t success = FALSE;
107     DBusMessage *msg = NULL, *reply = NULL;
108     DBusMessageIter iter;
109     DBusError *err, real_err;
110     const char *p;
111   va_list args_demarshal;
112
113   va_copy (args_demarshal, args);
114     if (opt_error)
115         err = opt_error;
116     else {
117         dbus_error_init (&real_err);
118         err = &real_err;
119     }
120
121     msg = dbus_message_new_method_call (bus_name, path, interface, method);
122     if (!msg)
123         goto out;
124
125     p = arg_types;
126     dbus_message_iter_init_append (msg, &iter);
127     dbind_any_marshal_va (&iter, &p, args);
128
129     reply = dbind_send_and_allow_reentry (cnx, msg, err);
130     if (!reply)
131         goto out;
132
133     if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
134     {
135       const char *name = dbus_message_get_error_name (reply);
136       goto out;
137     }
138     /* demarshal */
139     if (p[0] == '=' && p[1] == '>')
140     {
141         DBusMessageIter iter;
142         dbus_message_iter_init (reply, &iter);
143         p = arg_types;
144         dbind_any_demarshal_va (&iter, &p, args_demarshal);
145     }
146
147     success = TRUE;
148 out:
149     if (msg)
150         dbus_message_unref (msg);
151
152     if (reply)
153         dbus_message_unref (reply);
154
155     if (err == &real_err)
156         dbus_error_free (err);
157
158     va_end (args_demarshal);
159     return success;
160 }
161
162 /**
163  * dbind_method_call_reentrant:
164  *
165  * @cnx:       A D-Bus Connection used to make the method call.
166  * @bus_name:  The D-Bus bus name of the program where the method call should
167  *             be made.
168  * @path:      The D-Bus object path that should handle the method.
169  * @interface: The D-Bus interface used to scope the method name.
170  * @method:    Method to be invoked.
171  * @opt_error: D-Bus error.
172  * @arg_types: Variable length arguments interleaving D-Bus argument types
173  *             and pointers to argument data.
174  *
175  * Makes a D-Bus method call using the supplied location data, method name and
176  * argument data.This function is re-entrant. It continuously reads from the D-Bus
177  * bus and dispatches messages until a reply has been recieved.
178  **/
179 dbus_bool_t
180 dbind_method_call_reentrant (DBusConnection *cnx,
181                              const char     *bus_name,
182                              const char     *path,
183                              const char     *interface,
184                              const char     *method,
185                              DBusError      *opt_error,
186                              const char     *arg_types,
187                              ...)
188 {
189     dbus_bool_t success = FALSE;
190     va_list args;
191
192     va_start (args, arg_types);
193     success = dbind_method_call_reentrant_va (cnx,
194                                               bus_name,
195                                               path,
196                                               interface,
197                                               method,
198                                               opt_error,
199                                               arg_types,
200                                               args);
201     va_end (args);
202
203     return success;
204 }
205
206 /*---------------------------------------------------------------------------*/
207
208 dbus_bool_t
209 dbind_emit_signal_va (DBusConnection *cnx,
210                       const char     *path,
211                       const char     *interface,
212                       const char     *signal,
213                       DBusError      *opt_error,
214                       const char     *arg_types,
215                       va_list         args)
216 {
217     dbus_bool_t success = FALSE;
218     DBusMessage *msg = NULL;
219     DBusMessageIter iter;
220     DBusError *err, real_err;
221     const char *p;
222
223     if (opt_error)
224         err = opt_error;
225     else {
226         dbus_error_init (&real_err);
227         err = &real_err;
228     }
229
230     msg = dbus_message_new_signal (path, interface, signal);
231     if (!msg)
232         goto out;
233
234     p = arg_types;
235     dbus_message_iter_init_append (msg, &iter);
236     dbind_any_marshal_va (&iter, &p, args);
237
238     if (!dbus_connection_send (cnx, msg, NULL))
239        goto out;
240
241     success = TRUE;
242 out:
243
244     if (msg)
245         dbus_message_unref (msg);
246
247     if (err == &real_err)
248         dbus_error_free (err);
249
250     return success;
251 }
252
253 /**
254  * dbind_emit_signal:
255  *
256  * @cnx:       A D-Bus Connection used to make the method call.
257  * @path:      The D-Bus object path that this signal is emitted from.
258  * @interface: The D-Bus interface used to scope the method name.
259  * @signal:    Name of signal to emit.
260  * @opt_error: D-Bus error.
261  * @arg_types: Variable length arguments interleaving D-Bus argument types
262  *             and pointers to argument data.
263  *
264  * Emits a D-Bus signal  using the supplied signal name and argument data.
265  **/
266 dbus_bool_t
267 dbind_emit_signal (DBusConnection *cnx,
268                    const char     *path,
269                    const char     *interface,
270                    const char     *signal,
271                    DBusError      *opt_error,
272                    const char     *arg_types,
273                    ...)
274 {
275     dbus_bool_t success = FALSE;
276     va_list args;
277
278     va_start (args, arg_types);
279     success = dbind_emit_signal_va (cnx, path, interface, signal, opt_error, arg_types, args);
280     va_end (args);
281
282     return success;
283 }
284 void
285 dbind_set_timeout (int timeout)
286 {
287   dbind_timeout = timeout;
288 }
289
290
291 /*END------------------------------------------------------------------------*/