Merge "test : enhance accessible state information" into tizen
[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       goto out;
166     }
167     /* demarshal */
168     if (p[0] == '=' && p[1] == '>')
169     {
170         DBusMessageIter iter;
171         dbus_message_iter_init (reply, &iter);
172         if (strcmp (p + 2, dbus_message_get_signature (reply)) != 0)
173         {
174             g_warning ("dbind: Call to \"%s\" returned signature %s; expected %s",
175                        method, dbus_message_get_signature (reply), p + 2);
176             if (opt_error)
177                 dbus_set_error (opt_error, DBUS_ERROR_INVALID_ARGS,
178                                 "Call to \"%s\" returned signature %s; expected %s",
179                                 method, dbus_message_get_signature (reply),
180                                 p + 2);
181             goto out;
182         }
183         p = arg_types;
184         dbind_any_demarshal_va (&iter, &p, args_demarshal);
185     }
186
187     success = TRUE;
188 out:
189     if (msg)
190         dbus_message_unref (msg);
191
192     if (reply)
193         dbus_message_unref (reply);
194
195     if (dbus_error_is_set (&real_err))
196         dbus_error_free (&real_err);
197
198     va_end (args_demarshal);
199     return success;
200 }
201
202 /**
203  * dbind_method_call_reentrant:
204  *
205  * @cnx:       A D-Bus Connection used to make the method call.
206  * @bus_name:  The D-Bus bus name of the program where the method call should
207  *             be made.
208  * @path:      The D-Bus object path that should handle the method.
209  * @interface: The D-Bus interface used to scope the method name.
210  * @method:    Method to be invoked.
211  * @opt_error: D-Bus error.
212  * @arg_types: Variable length arguments interleaving D-Bus argument types
213  *             and pointers to argument data.
214  *
215  * Makes a D-Bus method call using the supplied location data, method name and
216  * argument data.This function is re-entrant. It continuously reads from the D-Bus
217  * bus and dispatches messages until a reply has been recieved.
218  **/
219 dbus_bool_t
220 dbind_method_call_reentrant (DBusConnection *cnx,
221                              const char     *bus_name,
222                              const char     *path,
223                              const char     *interface,
224                              const char     *method,
225                              DBusError      *opt_error,
226                              const char     *arg_types,
227                              ...)
228 {
229     dbus_bool_t success = FALSE;
230     va_list args;
231
232     va_start (args, arg_types);
233     success = dbind_method_call_reentrant_va (cnx,
234                                               bus_name,
235                                               path,
236                                               interface,
237                                               method,
238                                               opt_error,
239                                               arg_types,
240                                               args);
241     va_end (args);
242
243     return success;
244 }
245
246 /*---------------------------------------------------------------------------*/
247
248 /* TODO: opt_error is unused; should be removed */
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     const char *p;
262
263     msg = dbus_message_new_signal (path, interface, signal);
264     if (!msg)
265         goto out;
266
267     p = arg_types;
268     dbus_message_iter_init_append (msg, &iter);
269     dbind_any_marshal_va (&iter, &p, args);
270
271     if (!dbus_connection_send (cnx, msg, NULL))
272        goto out;
273
274     success = TRUE;
275 out:
276
277     if (msg)
278         dbus_message_unref (msg);
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------------------------------------------------------------------------*/