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