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