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