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