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