Don't leak errors
[platform/upstream/at-spi2-core.git] / atspi / atspi-misc.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001, 2002 Sun Microsystems Inc.,
6  * Copyright 2001, 2002 Ximian, Inc.
7  * Copyright 2010, 2011 Novell, Inc.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 /*
26  *
27  * Basic SPI initialization and event loop function prototypes
28  *
29  */
30
31 #include "atspi-private.h"
32 #include "X11/Xlib.h"
33 #include "atspi-gmain.h"
34 #include <stdio.h>
35 #include <string.h>
36
37 static void handle_get_items (DBusPendingCall *pending, void *user_data);
38
39 static DBusConnection *bus = NULL;
40 static GHashTable *live_refs = NULL;
41 static gint method_call_timeout = 800;
42 static gint app_startup_time = 15000;
43
44 GMainLoop *atspi_main_loop;
45 gboolean atspi_no_cache;
46
47 const char *atspi_path_dec = ATSPI_DBUS_PATH_DEC;
48 const char *atspi_path_registry = ATSPI_DBUS_PATH_REGISTRY;
49 const char *atspi_path_root = ATSPI_DBUS_PATH_ROOT;
50 const char *atspi_bus_registry = ATSPI_DBUS_NAME_REGISTRY;
51 const char *atspi_interface_accessible = ATSPI_DBUS_INTERFACE_ACCESSIBLE;
52 const char *atspi_interface_action = ATSPI_DBUS_INTERFACE_ACTION;
53 const char *atspi_interface_application = ATSPI_DBUS_INTERFACE_APPLICATION;
54 const char *atspi_interface_collection = ATSPI_DBUS_INTERFACE_COLLECTION;
55 const char *atspi_interface_component = ATSPI_DBUS_INTERFACE_COMPONENT;
56 const char *atspi_interface_dec = ATSPI_DBUS_INTERFACE_DEC;
57 const char *atspi_interface_device_event_listener = ATSPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER;
58 const char *atspi_interface_document = ATSPI_DBUS_INTERFACE_DOCUMENT;
59 const char *atspi_interface_editable_text = ATSPI_DBUS_INTERFACE_EDITABLE_TEXT;
60 const char *atspi_interface_event_object = ATSPI_DBUS_INTERFACE_EVENT_OBJECT;
61 const char *atspi_interface_hyperlink = ATSPI_DBUS_INTERFACE_HYPERLINK;
62 const char *atspi_interface_hypertext = ATSPI_DBUS_INTERFACE_HYPERTEXT;
63 const char *atspi_interface_image = ATSPI_DBUS_INTERFACE_IMAGE;
64 const char *atspi_interface_registry = ATSPI_DBUS_INTERFACE_REGISTRY;
65 const char *atspi_interface_selection = ATSPI_DBUS_INTERFACE_SELECTION;
66 const char *atspi_interface_table = ATSPI_DBUS_INTERFACE_TABLE;
67 const char *atspi_interface_text = ATSPI_DBUS_INTERFACE_TEXT;
68 const char *atspi_interface_cache = ATSPI_DBUS_INTERFACE_CACHE;
69 const char *atspi_interface_value = ATSPI_DBUS_INTERFACE_VALUE;
70
71 static const char *interfaces[] =
72 {
73   ATSPI_DBUS_INTERFACE_ACCESSIBLE,
74   ATSPI_DBUS_INTERFACE_ACTION,
75   ATSPI_DBUS_INTERFACE_APPLICATION,
76   ATSPI_DBUS_INTERFACE_COLLECTION,
77   ATSPI_DBUS_INTERFACE_COMPONENT,
78   ATSPI_DBUS_INTERFACE_DOCUMENT,
79   ATSPI_DBUS_INTERFACE_EDITABLE_TEXT,
80   ATSPI_DBUS_INTERFACE_HYPERLINK,
81   ATSPI_DBUS_INTERFACE_HYPERTEXT,
82   ATSPI_DBUS_INTERFACE_IMAGE,
83   "org.a11y.atspi.LoginHelper",
84   ATSPI_DBUS_INTERFACE_SELECTION,
85   ATSPI_DBUS_INTERFACE_TABLE,
86   ATSPI_DBUS_INTERFACE_TEXT,
87   ATSPI_DBUS_INTERFACE_VALUE,
88   NULL
89 };
90
91 gint
92 _atspi_get_iface_num (const char *iface)
93 {
94   /* TODO: Use a binary search or hash to improve performance */
95   int i;
96
97   for (i = 0; interfaces[i]; i++)
98   {
99     if (!strcmp(iface, interfaces[i])) return i;
100   }
101   return -1;
102 }
103
104 static GHashTable *
105 get_live_refs (void)
106 {
107   if (!live_refs) 
108     {
109       live_refs = g_hash_table_new (g_direct_hash, g_direct_equal);
110     }
111   return live_refs;
112 }
113
114 /* TODO: Add an application parameter */
115 DBusConnection *
116 _atspi_bus ()
117 {
118   if (!bus)
119     atspi_init ();
120   if (!bus)
121     g_error ("AT-SPI: COuldn't connect to accessibility bus. Is at-spi-bus-launcher running?");
122   return bus;
123 }
124
125 #define APP_IS_REGISTRY(app) (!strcmp (app->bus_name, atspi_bus_registry))
126
127 static void
128 cleanup ()
129 {
130   GHashTable *refs;
131
132   refs = live_refs;
133   live_refs = NULL;
134   if (refs)
135     {
136       g_hash_table_destroy (refs);
137     }
138 }
139
140 static gboolean atspi_inited = FALSE;
141
142 static GHashTable *app_hash = NULL;
143
144 static void
145 handle_get_bus_address (DBusPendingCall *pending, void *user_data)
146 {
147   AtspiApplication *app = user_data;
148   DBusMessage *reply = dbus_pending_call_steal_reply (pending);
149   DBusMessage *message;
150   const char *address;
151   DBusPendingCall *new_pending;
152
153   if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
154   {
155     if (dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &address,
156                                DBUS_TYPE_INVALID))
157     {
158       DBusError error;
159       dbus_error_init (&error);
160       DBusConnection *bus = dbus_connection_open (address, &error);
161       if (bus)
162       {
163         if (app->bus)
164           dbus_connection_unref (app->bus);
165         app->bus = bus;
166       }
167       else
168       {
169         g_warning ("Unable to open bus connection: %s", error.message);
170         dbus_error_free (&error);
171       }
172     }
173   }
174   dbus_message_unref (reply);
175   dbus_pending_call_unref (pending);
176
177   if (!app->bus)
178     return; /* application has gone away / been disposed */
179
180   message = dbus_message_new_method_call (app->bus_name,
181                                           "/org/a11y/atspi/cache",
182                                           atspi_interface_cache, "GetItems");
183
184    dbus_connection_send_with_reply (app->bus, message, &new_pending, 2000);
185   dbus_pending_call_set_notify (new_pending, handle_get_items, app, NULL);
186   dbus_message_unref (message);
187 }
188
189 static AtspiApplication *
190 get_application (const char *bus_name)
191 {
192   AtspiApplication *app = NULL;
193   char *bus_name_dup;
194   DBusMessage *message;
195   DBusPendingCall *pending = NULL;
196
197   if (!app_hash)
198   {
199     app_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_object_unref);
200     if (!app_hash) return NULL;
201   }
202   app = g_hash_table_lookup (app_hash, bus_name);
203   if (app) return app;
204   bus_name_dup = g_strdup (bus_name);
205   if (!bus_name_dup) return NULL;
206   // TODO: change below to something that will send state-change:defunct notification if necessary */
207   app = _atspi_application_new (bus_name);
208   if (!app) return NULL;
209   app->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
210   app->bus = dbus_connection_ref (_atspi_bus ());
211   gettimeofday (&app->time_added, NULL);
212   app->cache = ATSPI_CACHE_UNDEFINED;
213   g_hash_table_insert (app_hash, bus_name_dup, app);
214   message = dbus_message_new_method_call (bus_name, atspi_path_root,
215                                           atspi_interface_application, "GetApplicationBusAddress");
216
217    dbus_connection_send_with_reply (app->bus, message, &pending, 2000);
218   dbus_pending_call_set_notify (pending, handle_get_bus_address, app, NULL);
219   dbus_message_unref (message);
220   return app;
221 }
222
223 static AtspiAccessible *
224 ref_accessible (const char *app_name, const char *path)
225 {
226   AtspiApplication *app;
227   AtspiAccessible *a;
228
229   if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
230     return NULL;
231
232   app = get_application (app_name);
233
234   if (!strcmp (path, "/org/a11y/atspi/accessible/root"))
235   {
236     if (!app->root)
237     {
238       app->root = _atspi_accessible_new (app, atspi_path_root);
239       app->root->accessible_parent = atspi_get_desktop (0);
240     }
241     return g_object_ref (app->root);
242   }
243
244   a = g_hash_table_lookup (app->hash, path);
245   if (a)
246   {
247     return g_object_ref (a);
248   }
249   a = _atspi_accessible_new (app, path);
250   if (!a)
251     return NULL;
252   g_hash_table_insert (app->hash, g_strdup (a->parent.path), a);
253   g_object_ref (a);     /* for the hash */
254   return a;
255 }
256
257 static AtspiHyperlink *
258 ref_hyperlink (const char *app_name, const char *path)
259 {
260   AtspiApplication *app = get_application (app_name);
261   AtspiHyperlink *hyperlink;
262
263   if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
264     return NULL;
265
266   hyperlink = g_hash_table_lookup (app->hash, path);
267   if (hyperlink)
268   {
269     return g_object_ref (hyperlink);
270   }
271   hyperlink = _atspi_hyperlink_new (app, path);
272   g_hash_table_insert (app->hash, g_strdup (hyperlink->parent.path), hyperlink);
273   /* TODO: This should be a weak ref */
274   g_object_ref (hyperlink);     /* for the hash */
275   return hyperlink;
276 }
277
278 typedef struct
279 {
280   char *path;
281   char *parent;
282   GArray *children;
283   GArray *interfaces;
284   char *name;
285   dbus_uint32_t role;
286   char *description;
287   GArray *state_bitflags;
288 } CACHE_ADDITION;
289
290 static DBusHandlerResult
291 handle_remove_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
292 {
293   const char *sender = dbus_message_get_sender (message);
294   AtspiApplication *app;
295   const char *path;
296   DBusMessageIter iter, iter_struct;
297   const char *signature = dbus_message_get_signature (message);
298   AtspiAccessible *a;
299
300   if (strcmp (signature, "(so)") != 0)
301   {
302     g_warning ("AT-SPI: Unknown signature %s for RemoveAccessible", signature);
303     return DBUS_HANDLER_RESULT_HANDLED;
304   }
305
306   dbus_message_iter_init (message, &iter);
307   dbus_message_iter_recurse (&iter, &iter_struct);
308   dbus_message_iter_get_basic (&iter_struct, &sender);
309   dbus_message_iter_next (&iter_struct);
310   dbus_message_iter_get_basic (&iter_struct, &path);
311   app = get_application (sender);
312   a = ref_accessible (sender, path);
313   if (!a)
314     return DBUS_HANDLER_RESULT_HANDLED;
315   g_object_run_dispose (G_OBJECT (a));
316   g_hash_table_remove (app->hash, a->parent.path);
317   g_object_unref (a);   /* unref our own ref */
318   return DBUS_HANDLER_RESULT_HANDLED;
319 }
320
321 static gboolean
322 add_app_to_desktop (AtspiAccessible *a, const char *bus_name)
323 {
324   AtspiAccessible *obj = ref_accessible (bus_name, atspi_path_root);
325   if (obj)
326   {
327     a->children = g_list_append (a->children, obj);
328     return TRUE;
329   }
330   else
331   {
332     g_warning ("AT-SPI: Error calling getRoot for %s", bus_name);
333   }
334   return FALSE;
335 }
336
337 static void
338 send_children_changed (AtspiAccessible *parent, AtspiAccessible *child, gboolean add)
339 {
340   AtspiEvent e;
341
342   memset (&e, 0, sizeof (e));
343   e.type = (add? "object:children-changed:add": "object:children-changed:remove");
344   e.source = parent;
345   e.detail1 = g_list_index (parent->children, child);
346   e.detail2 = 0;
347   _atspi_send_event (&e);
348 }
349
350 static void
351 unref_object_and_descendants (AtspiAccessible *obj)
352 {
353   GList *l;
354
355   for (l = obj->children; l; l = l->next)
356   {
357     unref_object_and_descendants (l->data);
358   }
359   g_object_unref (obj);
360 }
361
362 static gboolean
363 remove_app_from_desktop (AtspiAccessible *a, const char *bus_name)
364 {
365   GList *l;
366   AtspiAccessible *child;
367
368   for (l = a->children; l; l = l->next)
369   {
370     child = l->data;
371     if (!strcmp (bus_name, child->parent.app->bus_name)) break;
372   }
373   if (!l)
374   {
375     return FALSE;
376   }
377   send_children_changed (a, child, FALSE);
378   a->children = g_list_remove (a->children, child);
379   unref_object_and_descendants (child);
380   return TRUE;
381 }
382
383 static AtspiAccessible *desktop;
384
385 void
386 get_reference_from_iter (DBusMessageIter *iter, const char **app_name, const char **path)
387 {
388   DBusMessageIter iter_struct;
389
390   dbus_message_iter_recurse (iter, &iter_struct);
391   dbus_message_iter_get_basic (&iter_struct, app_name);
392   dbus_message_iter_next (&iter_struct);
393   dbus_message_iter_get_basic (&iter_struct, path);
394   dbus_message_iter_next (iter);
395 }
396
397 static void
398 add_accessible_from_iter (DBusMessageIter *iter)
399 {
400   DBusMessageIter iter_struct, iter_array;
401   const char *app_name, *path;
402   AtspiAccessible *accessible;
403   const char *name, *description;
404   dbus_uint32_t role;
405
406   dbus_message_iter_recurse (iter, &iter_struct);
407
408   /* get accessible */
409   get_reference_from_iter (&iter_struct, &app_name, &path);
410   accessible = ref_accessible (app_name, path);
411   if (!accessible)
412     return;
413
414   /* Get application: TODO */
415   dbus_message_iter_next (&iter_struct);
416
417   /* get parent */
418   get_reference_from_iter (&iter_struct, &app_name, &path);
419   if (accessible->accessible_parent)
420     g_object_unref (accessible->accessible_parent);
421   accessible->accessible_parent = ref_accessible (app_name, path);
422
423   /* Get children */
424   while (accessible->children)
425   {
426     g_object_unref (accessible->children->data);
427     accessible->children = g_list_remove (accessible->children, accessible->children->data);
428   }
429   dbus_message_iter_recurse (&iter_struct, &iter_array);
430   while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
431   {
432     AtspiAccessible *child;
433     get_reference_from_iter (&iter_array, &app_name, &path);
434     child = ref_accessible (app_name, path);
435     accessible->children = g_list_append (accessible->children, child);
436   }
437
438   /* interfaces */
439   dbus_message_iter_next (&iter_struct);
440   _atspi_dbus_set_interfaces (accessible, &iter_struct);
441   dbus_message_iter_next (&iter_struct);
442
443   /* name */
444   if (accessible->name)
445     g_free (accessible->name);
446   dbus_message_iter_get_basic (&iter_struct, &name);
447   accessible->name = g_strdup (name);
448   dbus_message_iter_next (&iter_struct);
449
450   /* role */
451   dbus_message_iter_get_basic (&iter_struct, &role);
452   accessible->role = role;
453   dbus_message_iter_next (&iter_struct);
454
455   /* description */
456   if (accessible->description)
457     g_free (accessible->description);
458   dbus_message_iter_get_basic (&iter_struct, &description);
459   accessible->description = g_strdup (description);
460   dbus_message_iter_next (&iter_struct);
461
462   _atspi_dbus_set_state (accessible, &iter_struct);
463   dbus_message_iter_next (&iter_struct);
464
465   _atspi_accessible_add_cache (accessible, ATSPI_CACHE_NAME | ATSPI_CACHE_ROLE |
466                                ATSPI_CACHE_PARENT | ATSPI_CACHE_DESCRIPTION);
467   if (!atspi_state_set_contains (accessible->states,
468                                        ATSPI_STATE_MANAGES_DESCENDANTS))
469     _atspi_accessible_add_cache (accessible, ATSPI_CACHE_CHILDREN);
470
471   /* This is a bit of a hack since the cache holds a ref, so we don't need
472    * the one provided for us anymore */
473   g_object_unref (accessible);
474 }
475
476 static void
477 handle_get_items (DBusPendingCall *pending, void *user_data)
478 {
479   DBusMessage *reply = dbus_pending_call_steal_reply (pending);
480   DBusMessageIter iter, iter_array;
481
482   if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
483   {
484     const char *sender = dbus_message_get_sender (reply);
485     const char *error = NULL;
486     dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &error,
487                            DBUS_TYPE_INVALID);
488     g_warning ("AT-SPI: Error in GetItems, sender=%s, error=%s", sender, error);
489     dbus_message_unref (reply);
490     dbus_pending_call_unref (pending);
491     return;
492   }
493
494   dbus_message_iter_init (reply, &iter);
495   dbus_message_iter_recurse (&iter, &iter_array);
496   while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
497   {
498     add_accessible_from_iter (&iter_array);
499     dbus_message_iter_next (&iter_array);
500   }
501   dbus_message_unref (reply);
502   dbus_pending_call_unref (pending);
503 }
504
505 /* TODO: Do we stil need this function? */
506 static AtspiAccessible *
507 ref_accessible_desktop (AtspiApplication *app)
508 {
509   GError *error;
510   DBusMessage *message, *reply;
511   DBusMessageIter iter, iter_array;
512   gchar *bus_name_dup;
513
514   if (desktop)
515   {
516     g_object_ref (desktop);
517     return desktop;
518   }
519   desktop = _atspi_accessible_new (app, atspi_path_root);
520   if (!desktop)
521   {
522     return NULL;
523   }
524   g_hash_table_insert (app->hash, desktop->parent.path, desktop);
525   g_object_ref (desktop);       /* for the hash */
526   desktop->name = g_strdup ("main");
527   message = dbus_message_new_method_call (atspi_bus_registry,
528         atspi_path_root,
529         atspi_interface_accessible,
530         "GetChildren");
531   if (!message)
532     return NULL;
533   error = NULL;
534   reply = _atspi_dbus_send_with_reply_and_block (message, &error);
535   if (!reply || strcmp (dbus_message_get_signature (reply), "a(so)") != 0)
536   {
537     if (error != NULL)
538     {
539       g_warning ("Couldn't get application list: %s", error->message);
540       g_clear_error (&error);
541     }
542     if (reply)
543       dbus_message_unref (reply);
544     return NULL;
545   }
546   dbus_message_iter_init (reply, &iter);
547   dbus_message_iter_recurse (&iter, &iter_array);
548   while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
549   {
550     const char *app_name, *path;
551     get_reference_from_iter (&iter_array, &app_name, &path);
552     add_app_to_desktop (desktop, app_name);
553   }
554   dbus_message_unref (reply);
555
556   /* Record the alternate name as an alias for org.a11y.atspi.Registry */
557   bus_name_dup = g_strdup (dbus_message_get_sender (reply));
558   if (bus_name_dup)
559     g_hash_table_insert (app_hash, bus_name_dup, app);
560
561   return desktop;
562 }
563
564 AtspiAccessible *
565 _atspi_ref_accessible (const char *app, const char *path)
566 {
567   AtspiApplication *a = get_application (app);
568   if (!a) return NULL;
569   if ( APP_IS_REGISTRY(a))
570   {
571     return a->root = ref_accessible_desktop (a);
572   }
573   return ref_accessible (app, path);
574 }
575
576 AtspiAccessible *
577 _atspi_dbus_return_accessible_from_message (DBusMessage *message)
578 {
579   DBusMessageIter iter;
580   AtspiAccessible *retval = NULL;
581   const char *signature;
582
583   if (!message)
584     return NULL;
585
586   signature = dbus_message_get_signature (message);
587   if (!strcmp (signature, "(so)"))
588   {
589     dbus_message_iter_init (message, &iter);
590     retval =  _atspi_dbus_return_accessible_from_iter (&iter);
591   }
592   else
593   {
594     g_warning ("AT-SPI: Called _atspi_dbus_return_accessible_from_message with strange signature %s", signature);
595   }
596   dbus_message_unref (message);
597   return retval;
598 }
599
600 AtspiAccessible *
601 _atspi_dbus_return_accessible_from_iter (DBusMessageIter *iter)
602 {
603   const char *app_name, *path;
604
605   get_reference_from_iter (iter, &app_name, &path);
606   return ref_accessible (app_name, path);
607 }
608
609 AtspiHyperlink *
610 _atspi_dbus_return_hyperlink_from_message (DBusMessage *message)
611 {
612   DBusMessageIter iter;
613   AtspiHyperlink *retval = NULL;
614   const char *signature;
615    
616   if (!message)
617     return NULL;
618
619   signature = dbus_message_get_signature (message);
620   if (!strcmp (signature, "(so)"))
621   {
622     dbus_message_iter_init (message, &iter);
623     retval =  _atspi_dbus_return_hyperlink_from_iter (&iter);
624   }
625   else
626   {
627     g_warning ("AT-SPI: Called _atspi_dbus_return_hyperlink_from_message with strange signature %s", signature);
628   }
629   dbus_message_unref (message);
630   return retval;
631 }
632
633 AtspiHyperlink *
634 _atspi_dbus_return_hyperlink_from_iter (DBusMessageIter *iter)
635 {
636   const char *app_name, *path;
637
638   get_reference_from_iter (iter, &app_name, &path);
639   return ref_hyperlink (app_name, path);
640 }
641
642 const char *cache_signal_type = "((so)(so)(so)a(so)assusau)";
643
644 static DBusHandlerResult
645 handle_add_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
646 {
647   DBusMessageIter iter;
648   const char *sender = dbus_message_get_sender (message);
649
650   if (strcmp (dbus_message_get_signature (message), cache_signal_type) != 0)
651   {
652     g_warning ("AT-SPI: AddAccessible with unknown signature %s\n",
653                dbus_message_get_signature (message));
654     return DBUS_HANDLER_RESULT_HANDLED;
655   }
656
657   dbus_message_iter_init (message, &iter);
658   add_accessible_from_iter (&iter);
659   return DBUS_HANDLER_RESULT_HANDLED;
660 }
661
662 typedef struct
663 {
664   DBusConnection *bus;
665   DBusMessage *message;
666   void *data;
667 } BusDataClosure;
668
669 static guint process_deferred_messages_id = -1;
670
671 static void
672 process_deferred_message (BusDataClosure *closure)
673 {
674   int type = dbus_message_get_type (closure->message);
675   const char *interface = dbus_message_get_interface (closure->message);
676
677   if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
678       !strncmp (interface, "org.a11y.atspi.Event.", 21))
679   {
680     _atspi_dbus_handle_event (closure->bus, closure->message, closure->data);
681   }
682   if (dbus_message_is_method_call (closure->message, atspi_interface_device_event_listener, "NotifyEvent"))
683   {
684     _atspi_dbus_handle_DeviceEvent (closure->bus,
685                                    closure->message, closure->data);
686   }
687   if (dbus_message_is_signal (closure->message, atspi_interface_cache, "AddAccessible"))
688   {
689     handle_add_accessible (closure->bus, closure->message, closure->data);
690   }
691   if (dbus_message_is_signal (closure->message, atspi_interface_cache, "RemoveAccessible"))
692   {
693     handle_remove_accessible (closure->bus, closure->message, closure->data);
694   }
695 }
696
697 static GQueue *deferred_messages = NULL;
698
699 gboolean
700 _atspi_process_deferred_messages (gpointer data)
701 {
702   static int in_process_deferred_messages = 0;
703   BusDataClosure *closure;
704
705   if (in_process_deferred_messages)
706     return TRUE;
707   in_process_deferred_messages = 1;
708   while (closure = g_queue_pop_head (deferred_messages))
709   {
710     process_deferred_message (closure);
711     dbus_message_unref (closure->message);
712     dbus_connection_unref (closure->bus);
713     g_free (closure);
714   }
715   /* If data is NULL, assume that we were called from GLib */
716   if (!data)
717     process_deferred_messages_id = -1;
718   in_process_deferred_messages = 0;
719   return FALSE;
720 }
721
722 static DBusHandlerResult
723 defer_message (DBusConnection *connection, DBusMessage *message, void *user_data)
724 {
725   BusDataClosure *closure = g_new (BusDataClosure, 1);
726
727   closure->bus = dbus_connection_ref (bus);
728   closure->message = dbus_message_ref (message);
729   closure->data = user_data;
730
731   g_queue_push_tail (deferred_messages, closure);
732
733   if (process_deferred_messages_id == -1)
734     process_deferred_messages_id = g_idle_add (_atspi_process_deferred_messages, NULL);
735   return DBUS_HANDLER_RESULT_HANDLED;
736 }
737
738 static DBusHandlerResult
739 atspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
740 {
741   int type = dbus_message_get_type (message);
742   const char *interface = dbus_message_get_interface (message);
743
744   if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
745       !strncmp (interface, "org.a11y.atspi.Event.", 21))
746   {
747     return defer_message (bus, message, data);
748   }
749   if (dbus_message_is_method_call (message, atspi_interface_device_event_listener, "NotifyEvent"))
750   {
751     return defer_message (bus, message, data);
752   }
753   if (dbus_message_is_signal (message, atspi_interface_cache, "AddAccessible"))
754   {
755     return defer_message (bus, message, data);
756   }
757   if (dbus_message_is_signal (message, atspi_interface_cache, "RemoveAccessible"))
758   {
759     return defer_message (bus, message, data);
760   }
761   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
762 }
763
764 static const char *signal_interfaces[] =
765 {
766   "org.a11y.atspi.Event.Object",
767   "org.a11y.atspi.Event.Window",
768   "org.a11y.atspi.Event.Mouse",
769   "org.a11y.atspi.Event.Terminal",
770   "org.a11y.atspi.Event.Document",
771   "org.a11y.atspi.Event.Focus",
772   NULL
773 };
774
775 /*
776  * Returns a 'canonicalized' value for DISPLAY,
777  * with the screen number stripped off if present.
778  *
779  * TODO: Avoid having duplicate functions for this here and in at-spi2-atk
780  */
781 static const gchar *
782 spi_display_name (void)
783 {
784   static const char *canonical_display_name = NULL;
785   if (!canonical_display_name)
786     {
787       const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
788       if (!display_env)
789         {
790           display_env = g_getenv ("DISPLAY");
791           if (!display_env || !display_env[0])
792             canonical_display_name = ":0";
793           else
794             {
795               gchar *display_p, *screen_p;
796               canonical_display_name = g_strdup (display_env);
797               display_p = g_utf8_strrchr (canonical_display_name, -1, ':');
798               screen_p = g_utf8_strrchr (canonical_display_name, -1, '.');
799               if (screen_p && display_p && (screen_p > display_p))
800                 {
801                   *screen_p = '\0';
802                 }
803             }
804         }
805       else
806         {
807           canonical_display_name = display_env;
808         }
809     }
810   return canonical_display_name;
811 }
812
813 /**
814  * atspi_init:
815  *
816  * Connects to the accessibility registry and initializes the SPI.
817  *
818  * Returns: 0 on success, otherwise an integer error code.  
819  **/
820 int
821 atspi_init (void)
822 {
823   char *match;
824   const gchar *no_cache;
825
826   if (atspi_inited)
827     {
828       return 1;
829     }
830
831   atspi_inited = TRUE;
832
833   g_type_init ();
834
835   get_live_refs();
836
837   bus = atspi_get_a11y_bus ();
838   if (!bus)
839     return 2;
840   dbus_bus_register (bus, NULL);
841   atspi_dbus_connection_setup_with_g_main(bus, g_main_context_default());
842   dbus_connection_add_filter (bus, atspi_dbus_filter, NULL, NULL);
843   match = g_strdup_printf ("type='signal',interface='%s',member='AddAccessible'", atspi_interface_cache);
844   dbus_bus_add_match (bus, match, NULL);
845   g_free (match);
846   match = g_strdup_printf ("type='signal',interface='%s',member='RemoveAccessible'", atspi_interface_cache);
847   dbus_bus_add_match (bus, match, NULL);
848   g_free (match);
849   match = g_strdup_printf ("type='signal',interface='%s',member='ChildrenChanged'", atspi_interface_event_object);
850   dbus_bus_add_match (bus, match, NULL);
851   g_free (match);
852   match = g_strdup_printf ("type='signal',interface='%s',member='PropertyChange'", atspi_interface_event_object);
853   dbus_bus_add_match (bus, match, NULL);
854   g_free (match);
855   match = g_strdup_printf ("type='signal',interface='%s',member='StateChanged'", atspi_interface_event_object);
856   dbus_bus_add_match (bus, match, NULL);
857   g_free (match);
858
859   no_cache = g_getenv ("ATSPI_NO_CACHE");
860   if (no_cache && g_strcmp0 (no_cache, "0") != 0)
861     atspi_no_cache = TRUE;
862
863   deferred_messages = g_queue_new ();
864
865   return 0;
866 }
867
868 /**
869  * atspi_event_main:
870  *
871  * Starts/enters the main event loop for the AT-SPI services.
872  *
873  * NOTE: This method does not return control; it is exited via a call to
874  * #atspi_event_quit from within an event handler.
875  *
876  **/
877 void
878 atspi_event_main (void)
879 {
880   atspi_main_loop = g_main_loop_new (NULL, FALSE);
881   g_main_loop_run (atspi_main_loop);
882   atspi_main_loop = NULL;
883 }
884
885 /**
886  * atspi_event_quit:
887  *
888  * Quits the last main event loop for the AT-SPI services,
889  * See: #atspi_event_main
890  **/
891 void
892 atspi_event_quit (void)
893 {
894   g_main_loop_quit (atspi_main_loop);
895 }
896
897 /**
898  * atspi_exit:
899  *
900  * Disconnects from #AtspiRegistry instances and releases 
901  * any floating resources. Call only once at exit.
902  *
903  * Returns: 0 if there were no leaks, otherwise other integer values.
904  **/
905 int
906 atspi_exit (void)
907 {
908   int leaked;
909
910   if (!atspi_inited)
911     {
912       return 0;
913     }
914
915   atspi_inited = FALSE;
916
917   if (live_refs)
918     {
919       leaked = g_hash_table_size (live_refs);
920     }
921   else
922     {
923       leaked = 0;
924     }
925
926   cleanup ();
927
928   return leaked;
929 }
930
931 static GSList *hung_processes;
932
933 static void
934 remove_hung_process (DBusPendingCall *pending, void *data)
935 {
936   gchar *bus_name = data;
937
938   hung_processes = g_slist_remove (hung_processes, data);
939   g_free (data);
940   dbus_pending_call_unref (pending);
941 }
942
943 static void
944 check_for_hang (DBusMessage *message, DBusError *error, DBusConnection *bus, const char *bus_name)
945 {
946   if (!message && error->name &&
947       !strcmp (error->name, "org.freedesktop.DBus.Error.NoReply"))
948   {
949     GSList *l;
950     DBusMessage *message;
951     gchar *bus_name_dup;
952     DBusPendingCall *pending = NULL;
953     for (l = hung_processes; l; l = l->next)
954       if (!strcmp (l->data, bus_name))
955         return;
956     message = dbus_message_new_method_call (bus_name, "/",
957                                             "org.freedesktop.DBus.Peer",
958                                             "Ping");
959     if (!message)
960       return;
961     dbus_connection_send_with_reply (bus, message, &pending, -1);
962     dbus_message_unref (message);
963     if (!pending)
964       return;
965     bus_name_dup = g_strdup (bus_name);
966     hung_processes = g_slist_append (hung_processes, bus_name_dup);
967     dbus_pending_call_set_notify (pending, remove_hung_process, bus_name_dup, NULL);
968   }
969 }
970
971 static gboolean
972 connection_is_hung (const char *bus_name)
973 {
974   GSList *l;
975
976   for (l = hung_processes; l; l = l->next)
977     if (!strcmp (l->data, bus_name))
978       return TRUE;
979   return FALSE;
980 }
981
982 static gboolean
983 check_app (AtspiApplication *app, GError **error)
984 {
985   if (!app || !app->bus)
986   {
987     g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
988                           _("The application no longer exists"));
989     return FALSE;
990   }
991
992   if (atspi_main_loop && connection_is_hung (app->bus_name))
993   {
994       g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC,
995                            "The process appears to be hung.");
996     return FALSE;
997   }
998
999   return TRUE;
1000 }
1001
1002 static void
1003 set_timeout (AtspiApplication *app)
1004 {
1005   struct timeval tv;
1006   int diff;
1007
1008   if (app && app_startup_time > 0)
1009   {
1010     gettimeofday (&tv, NULL);
1011     diff = (tv.tv_sec - app->time_added.tv_sec) * 1000 + (tv.tv_usec - app->time_added.tv_usec) / 1000;
1012     dbind_set_timeout (MAX(method_call_timeout, app_startup_time - diff));
1013   }
1014   else
1015     dbind_set_timeout (method_call_timeout);
1016 }
1017
1018 dbus_bool_t
1019 _atspi_dbus_call (gpointer obj, const char *interface, const char *method, GError **error, const char *type, ...)
1020 {
1021   va_list args;
1022   dbus_bool_t retval;
1023   DBusError err;
1024   AtspiObject *aobj = ATSPI_OBJECT (obj);
1025
1026   if (!check_app (aobj->app, error))
1027     return FALSE;
1028
1029   va_start (args, type);
1030   dbus_error_init (&err);
1031   set_timeout (aobj->app);
1032   retval = dbind_method_call_reentrant_va (aobj->app->bus, aobj->app->bus_name,
1033                                            aobj->path, interface, method, &err,
1034                                            type, args);
1035   va_end (args);
1036   check_for_hang (NULL, &err, aobj->app->bus, aobj->app->bus_name);
1037   _atspi_process_deferred_messages ((gpointer)TRUE);
1038   if (dbus_error_is_set (&err))
1039   {
1040     g_set_error(error, ATSPI_ERROR, ATSPI_ERROR_IPC, "%s", err.message);
1041     dbus_error_free (&err);
1042   }
1043   return retval;
1044 }
1045
1046 DBusMessage *
1047 _atspi_dbus_call_partial (gpointer obj,
1048                           const char *interface,
1049                           const char *method,
1050                           GError **error,
1051                           const char *type, ...)
1052 {
1053   va_list args;
1054
1055   va_start (args, type);
1056   return _atspi_dbus_call_partial_va (obj, interface, method, error, type, args);
1057 }
1058
1059
1060 DBusMessage *
1061 _atspi_dbus_call_partial_va (gpointer obj,
1062                           const char *interface,
1063                           const char *method,
1064                           GError **error,
1065                           const char *type,
1066                           va_list args)
1067 {
1068   AtspiObject *aobj = ATSPI_OBJECT (obj);
1069   DBusError err;
1070     DBusMessage *msg = NULL, *reply = NULL;
1071     DBusMessageIter iter;
1072     const char *p;
1073
1074   dbus_error_init (&err);
1075
1076   if (!check_app (aobj->app, error))
1077     goto out;
1078
1079   msg = dbus_message_new_method_call (aobj->app->bus_name, aobj->path, interface, method);
1080   if (!msg)
1081     goto out;
1082
1083   p = type;
1084   dbus_message_iter_init_append (msg, &iter);
1085   dbind_any_marshal_va (&iter, &p, args);
1086
1087   set_timeout (aobj->app);
1088   reply = dbind_send_and_allow_reentry (aobj->app->bus, msg, &err);
1089   check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1090 out:
1091   va_end (args);
1092   if (msg)
1093     dbus_message_unref (msg);
1094   _atspi_process_deferred_messages ((gpointer)TRUE);
1095   if (dbus_error_is_set (&err))
1096   {
1097     /* TODO: Set gerror */
1098     dbus_error_free (&err);
1099   }
1100   return reply;
1101 }
1102
1103 dbus_bool_t
1104 _atspi_dbus_get_property (gpointer obj, const char *interface, const char *name, GError **error, const char *type, void *data)
1105 {
1106   DBusMessage *message, *reply;
1107   DBusMessageIter iter, iter_variant;
1108   DBusError err;
1109   dbus_bool_t retval = FALSE;
1110   AtspiObject *aobj = ATSPI_OBJECT (obj);
1111   char expected_type = (type [0] == '(' ? 'r' : type [0]);
1112
1113   if (!aobj)
1114     return FALSE;
1115
1116   if (!check_app (aobj->app, error))
1117     return FALSE;
1118
1119   message = dbus_message_new_method_call (aobj->app->bus_name,
1120                                           aobj->path,
1121                                           "org.freedesktop.DBus.Properties",
1122                                           "Get");
1123   if (!message)
1124   {
1125     // TODO: throw exception
1126     return FALSE;
1127   }
1128   dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1129   dbus_error_init (&err);
1130   set_timeout (aobj->app);
1131   reply = dbind_send_and_allow_reentry (aobj->app->bus, message, &err);
1132   check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1133   dbus_message_unref (message);
1134   _atspi_process_deferred_messages ((gpointer)TRUE);
1135   if (!reply)
1136   {
1137     // TODO: throw exception
1138     goto done;
1139   }
1140
1141   if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
1142   {
1143     const char *err_str = NULL;
1144     dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &err_str, DBUS_TYPE_INVALID);
1145     if (err_str)
1146       g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err_str);
1147     goto done;
1148   }
1149
1150   dbus_message_iter_init (reply, &iter);
1151   if (dbus_message_iter_get_arg_type (&iter) != 'v')
1152   {
1153     g_warning ("AT-SPI: expected a variant when fetching %s from interface %s; got %s\n", name, interface, dbus_message_get_signature (reply));
1154     goto done;
1155   }
1156   dbus_message_iter_recurse (&iter, &iter_variant);
1157   if (dbus_message_iter_get_arg_type (&iter_variant) != expected_type)
1158   {
1159     g_warning ("atspi_dbus_get_property: Wrong type: expected %s, got %c\n", type, dbus_message_iter_get_arg_type (&iter_variant));
1160     goto done;
1161   }
1162   if (!strcmp (type, "(so)"))
1163   {
1164     *((AtspiAccessible **)data) = _atspi_dbus_return_accessible_from_iter (&iter_variant);
1165   }
1166   else
1167   {
1168     dbus_message_iter_get_basic (&iter_variant, data);
1169     if (type [0] == 's')
1170       *(char **)data = g_strdup (*(char **)data);
1171   }
1172   retval = TRUE;
1173 done:
1174   dbus_error_free (&err);
1175   if (reply)
1176     dbus_message_unref (reply);
1177   return retval;
1178 }
1179
1180 DBusMessage *
1181 _atspi_dbus_send_with_reply_and_block (DBusMessage *message, GError **error)
1182 {
1183   DBusMessage *reply;
1184   DBusError err;
1185   AtspiApplication *app;
1186   DBusConnection *bus;
1187
1188   app = get_application (dbus_message_get_destination (message));
1189
1190   if (app && !app->bus)
1191     return NULL;        /* will fail anyway; app has been disposed */
1192
1193   bus = (app ? app->bus : _atspi_bus());
1194   dbus_error_init (&err);
1195   set_timeout (app);
1196   reply = dbind_send_and_allow_reentry (bus, message, &err);
1197   _atspi_process_deferred_messages ((gpointer)TRUE);
1198   dbus_message_unref (message);
1199   if (dbus_error_is_set (&err))
1200   {
1201     if (error)
1202       g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err.message);
1203     dbus_error_free (&err);
1204   }
1205   return reply;
1206 }
1207
1208 GHashTable *
1209 _atspi_dbus_return_hash_from_message (DBusMessage *message)
1210 {
1211   DBusMessageIter iter;
1212   GHashTable *ret;
1213
1214   if (!message)
1215     return NULL;
1216
1217   _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1218
1219   dbus_message_iter_init (message, &iter);
1220   ret = _atspi_dbus_hash_from_iter (&iter);
1221   dbus_message_unref (message);
1222   return ret;
1223 }
1224
1225 GHashTable *
1226 _atspi_dbus_hash_from_iter (DBusMessageIter *iter)
1227 {
1228   GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
1229                                             (GDestroyNotify) g_free,
1230                                             (GDestroyNotify) g_free);
1231   DBusMessageIter iter_array, iter_dict;
1232
1233   dbus_message_iter_recurse (iter, &iter_array);
1234   while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1235   {
1236     const char *name, *value;
1237     dbus_message_iter_recurse (&iter_array, &iter_dict);
1238     dbus_message_iter_get_basic (&iter_dict, &name);
1239     dbus_message_iter_next (&iter_dict);
1240     dbus_message_iter_get_basic (&iter_dict, &value);
1241     g_hash_table_insert (hash, g_strdup (name), g_strdup (value));
1242     dbus_message_iter_next (&iter_array);
1243   }
1244   return hash;
1245 }
1246
1247 GArray *
1248 _atspi_dbus_return_attribute_array_from_message (DBusMessage *message)
1249 {
1250   DBusMessageIter iter;
1251   GArray *ret;
1252
1253   if (!message)
1254     return NULL;
1255
1256   _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1257
1258   dbus_message_iter_init (message, &iter);
1259
1260   ret = _atspi_dbus_attribute_array_from_iter (&iter);
1261   dbus_message_unref (message);
1262   return ret;
1263 }
1264
1265 GArray *
1266 _atspi_dbus_attribute_array_from_iter (DBusMessageIter *iter)
1267 {
1268   DBusMessageIter iter_array, iter_dict;
1269   GArray *array = g_array_new (TRUE, TRUE, sizeof (gchar *));
1270
1271   dbus_message_iter_recurse (iter, &iter_array);
1272   while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1273   {
1274     const char *name, *value;
1275     gchar *str;
1276     dbus_message_iter_recurse (&iter_array, &iter_dict);
1277     dbus_message_iter_get_basic (&iter_dict, &name);
1278     dbus_message_iter_next (&iter_dict);
1279     dbus_message_iter_get_basic (&iter_dict, &value);
1280     str = g_strdup_printf ("%s:%s", name, value);
1281     array = g_array_append_val (array, str);
1282     dbus_message_iter_next (&iter_array);;
1283   }
1284   return array;
1285 }
1286
1287 void
1288 _atspi_dbus_set_interfaces (AtspiAccessible *accessible, DBusMessageIter *iter)
1289 {
1290   DBusMessageIter iter_array;
1291
1292   accessible->interfaces = 0;
1293   dbus_message_iter_recurse (iter, &iter_array);
1294   while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1295   {
1296     const char *iface;
1297     gint n;
1298     dbus_message_iter_get_basic (&iter_array, &iface);
1299     if (!strcmp (iface, "org.freedesktop.DBus.Introspectable")) continue;
1300     n = _atspi_get_iface_num (iface);
1301     if (n == -1)
1302     {
1303       g_warning ("AT-SPI: Unknown interface %s", iface);
1304     }
1305     else
1306       accessible->interfaces |= (1 << n);
1307     dbus_message_iter_next (&iter_array);
1308   }
1309   _atspi_accessible_add_cache (accessible, ATSPI_CACHE_INTERFACES);
1310 }
1311
1312 void
1313 _atspi_dbus_set_state (AtspiAccessible *accessible, DBusMessageIter *iter)
1314 {
1315   DBusMessageIter iter_array;
1316   gint count;
1317   dbus_uint32_t *states;
1318
1319   dbus_message_iter_recurse (iter, &iter_array);
1320   dbus_message_iter_get_fixed_array (&iter_array, &states, &count);
1321   if (count != 2)
1322   {
1323     g_warning ("AT-SPI: expected 2 values in states array; got %d\n", count);
1324     if (!accessible->states)
1325       accessible->states = _atspi_state_set_new_internal (accessible, 0);
1326   }
1327   else
1328   {
1329     guint64 val = ((guint64)states [1]) << 32;
1330     val += states [0];
1331     if (!accessible->states)
1332       accessible->states = _atspi_state_set_new_internal (accessible, val);
1333     else
1334       accessible->states->states = val;
1335   }
1336   _atspi_accessible_add_cache (accessible, ATSPI_CACHE_STATES);
1337 }
1338
1339 GQuark
1340 _atspi_error_quark (void)
1341 {
1342   return g_quark_from_static_string ("atspi_error");
1343 }
1344
1345 /*
1346  * Gets the IOR from the XDisplay.
1347  */
1348 static char *
1349 get_accessibility_bus_address_x11 (void)
1350 {
1351   Atom AT_SPI_BUS;
1352   Atom actual_type;
1353   Display *bridge_display;
1354   int actual_format;
1355   char *data;
1356   unsigned char *data_x11 = NULL;
1357   unsigned long nitems;
1358   unsigned long leftover;
1359
1360   bridge_display = XOpenDisplay (spi_display_name ());
1361   if (!bridge_display)
1362     {
1363       g_warning ("Could not open X display");
1364       return NULL;
1365     }
1366       
1367   AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
1368   XGetWindowProperty (bridge_display,
1369                       XDefaultRootWindow (bridge_display),
1370                       AT_SPI_BUS, 0L,
1371                       (long) BUFSIZ, False,
1372                       (Atom) 31, &actual_type, &actual_format,
1373                       &nitems, &leftover, &data_x11);
1374   XCloseDisplay (bridge_display);
1375
1376   data = g_strdup (data_x11);
1377   XFree (data_x11);
1378   return data;
1379 }
1380
1381 static char *
1382 get_accessibility_bus_address_dbus (void)
1383 {
1384   DBusConnection *session_bus = NULL;
1385   DBusMessage *message;
1386   DBusMessage *reply;
1387   DBusError error;
1388   char *address = NULL;
1389
1390   session_bus = dbus_bus_get (DBUS_BUS_SESSION, NULL);
1391   if (!session_bus)
1392     return NULL;
1393
1394   message = dbus_message_new_method_call ("org.a11y.Bus",
1395                                           "/org/a11y/bus",
1396                                           "org.a11y.Bus",
1397                                           "GetAddress");
1398
1399   dbus_error_init (&error);
1400   reply = dbus_connection_send_with_reply_and_block (session_bus,
1401                                                      message,
1402                                                      -1,
1403                                                      &error);
1404   dbus_message_unref (message);
1405
1406   if (!reply)
1407   {
1408     g_warning ("Error retrieving accessibility bus address: %s: %s",
1409                error.name, error.message);
1410     dbus_error_free (&error);
1411     return NULL;
1412   }
1413   
1414   {
1415     const char *tmp_address;
1416     if (!dbus_message_get_args (reply,
1417                                 NULL,
1418                                 DBUS_TYPE_STRING,
1419                                 &tmp_address,
1420                                 DBUS_TYPE_INVALID))
1421       {
1422         dbus_message_unref (reply);
1423         return NULL;
1424       }
1425     address = g_strdup (tmp_address);
1426     dbus_message_unref (reply);
1427   }
1428   
1429   return address;
1430 }
1431
1432 DBusConnection *
1433 atspi_get_a11y_bus (void)
1434 {
1435   DBusConnection *bus = NULL;
1436   DBusError error;
1437   char *address;
1438
1439   address = get_accessibility_bus_address_x11 ();
1440   if (!address)
1441     address = get_accessibility_bus_address_dbus ();
1442   if (!address)
1443     return NULL;
1444
1445   dbus_error_init (&error);
1446   bus = dbus_connection_open (address, &error);
1447   g_free (address);
1448
1449   if (!bus)
1450     {
1451       g_warning ("Couldn't connect to accessibility bus: %s", error.message);
1452       dbus_error_free (&error);
1453       return NULL;
1454     }
1455   else
1456     {
1457       if (!dbus_bus_register (bus, &error))
1458         {
1459           g_warning ("Couldn't register with accessibility bus: %s", error.message);
1460           dbus_error_free (&error);
1461           return NULL;
1462         }
1463     }
1464   
1465   return bus;
1466 }
1467
1468 /**
1469  *  Set the timeout used for method calls. If this is not set explicitly,
1470  *  a default of 0.8 ms is used.
1471  *  Note that at-spi2-registryd currently uses a timeout of 3 seconds when
1472  *  sending a keyboard event notification. This means that, if an AT makes
1473  *  a call in response to the keyboard notification and the application
1474  *  being called does not respond before the timeout is reached,
1475  *  at-spi2-registryd will time out on the keyboard event notification and
1476  *  pass the key onto the application (ie, reply to indicate that the key
1477  *  was not consumed), so this may make it undesirable to set a timeout
1478  *  larger than 3 seconds.
1479  *
1480  *  @val: The timeout value, in milliseconds, or -1 to disable the timeout.
1481  *  @startup_time: The amount of time, in milliseconds, to allow to pass
1482  *  before enforcing timeouts on an application. Can be used to prevent
1483  *  timeout exceptions if an application is likely to block for an extended
1484  *  period of time on initialization. -1 can be passed to disable this
1485  *  behavior.
1486  *
1487  * By default, the normal timeout is set to 800 ms, and the application startup
1488  * timeout is set to 15 seconds.
1489  */
1490 void
1491 atspi_set_timeout (gint val, gint startup_time)
1492 {
1493   method_call_timeout = val;
1494   app_startup_time = startup_time;
1495 }