2009-08-28 Mike Gorse <mgorse@novell.com>
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / accessible-register.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2008 Novell, Inc.
6  * Copyright 2008, 2009 Codethink Ltd.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "bridge.h"
29 #include "tree-adaptor.h"
30 #include "accessible-register.h"
31
32 /*
33  * This module is responsible for keeping track of all the AtkObjects in
34  * the application, so that they can be accessed remotely and placed in
35  * a client side cache.
36  *
37  * To access an AtkObject remotely we need to provide a D-Bus object 
38  * path for it. The D-Bus object paths used have a standard prefix
39  * (SPI_ATK_OBJECT_PATH_PREFIX). Appended to this prefix is a string
40  * representation of an integer reference. So to access an AtkObject 
41  * remotely we keep a Hashtable that maps the given reference to 
42  * the AtkObject pointer. An object in this hash table is said to be 'registered'.
43  *
44  * The architecture of AT-SPI dbus is such that AtkObjects are not
45  * remotely reference counted. This means that we need to keep track of
46  * object destruction. When an object is destroyed it must be 'deregistered'
47  * To do this lookup we keep a dbus-id attribute on each AtkObject.
48  *
49  * In addition to accessing the objects remotely we must be able to update
50  * the client side cache. This is done using the 'update' signal of the 
51  * org.freedesktop.atspi.Tree interface. The update signal should send out
52  * all of the cacheable data for an Accessible object.
53  *
54  */
55
56 /*
57  * FIXME
58  *
59  * This code seems very brittle.
60  * I would prefer changes to be made to
61  * gail and the ATK interface so that all Accessible
62  * objects are registered with an exporting module.
63  *
64  * This is the same system as Qt has with the QAccessibleBridge
65  * and QAccessibleBridgePlugin. It entails some rather
66  * large structural changes to ATK though:
67  *
68  * Removing infinite spaces (Child access no longer references child).
69  * Removing lazy creation of accessible objects.
70  */
71
72 #define SPI_ATK_OBJECT_PATH_PREFIX  "/org/freedesktop/atspi/accessible"
73 #define SPI_ATK_OBJECT_PATH_DESKTOP "/root"
74
75 #define SPI_ATK_PATH_PREFIX_LENGTH 33
76 #define SPI_ATK_OBJECT_REFERENCE_TEMPLATE SPI_ATK_OBJECT_PATH_PREFIX "/%d"
77
78
79 static GHashTable *ref2ptr = NULL; /* Used for converting a D-Bus path (Reference) to the object pointer */
80
81 static guint reference_counter = 0;
82
83 static GStaticRecMutex registration_mutex = G_STATIC_REC_MUTEX_INIT;
84
85 /*---------------------------------------------------------------------------*/
86
87 static GStaticMutex   recursion_check_guard = G_STATIC_MUTEX_INIT;
88 static gboolean       recursion_check = FALSE;
89
90 static gboolean
91 recursion_check_and_set ()
92 {
93   gboolean ret;
94   g_static_mutex_lock   (&recursion_check_guard);
95   ret = recursion_check;
96   recursion_check = TRUE;
97   g_static_mutex_unlock (&recursion_check_guard);
98   return ret;
99 }
100
101 static void
102 recursion_check_unset ()
103 {
104   g_static_mutex_lock   (&recursion_check_guard);
105   recursion_check = FALSE;
106   g_static_mutex_unlock (&recursion_check_guard);
107 }
108
109 /*---------------------------------------------------------------------------*/
110
111 /*
112  * Each AtkObject must be asssigned a D-Bus path (Reference)
113  *
114  * This function provides an integer reference for a new
115  * AtkObject.
116  */
117 static guint
118 assign_reference(void)
119 {
120   reference_counter++;
121   /* Reference of 0 not allowed as used as direct key in hash table */
122   if (reference_counter == 0)
123     reference_counter++;
124   return reference_counter;
125 }
126
127 /*
128  * Returns the reference of the object, or 0 if it is not registered.
129  */
130 static guint
131 gobject_to_ref (GObject *gobj)
132 {
133   return GPOINTER_TO_INT(g_object_get_data (gobj, "dbus-id"));
134 }
135
136 static guint
137 object_to_ref (AtkObject *accessible)
138 {
139   return gobject_to_ref (G_OBJECT (accessible));
140 }
141
142 static guint
143 hyperlink_to_ref (AtkHyperlink *accessible)
144 {
145   return GPOINTER_TO_INT(g_object_get_data (G_OBJECT (accessible), "dbus-id"));
146 }
147
148 /*
149  * Converts the Accessible object reference to its D-Bus object path
150  */
151 gchar *
152 atk_dbus_ref_to_path (guint ref)
153 {
154   return g_strdup_printf(SPI_ATK_OBJECT_REFERENCE_TEMPLATE, ref);
155 }
156
157 /*---------------------------------------------------------------------------*/
158
159 static void
160 deregister_sub_accessible (gpointer key, gpointer obj_data, gpointer iter);
161
162 static void
163 deregister_sub_hyperlink (gpointer key, gpointer obj_data, gpointer iter);
164
165 /*
166  * Callback for when a registered AtkObject is destroyed.
167  *
168  * Removes the AtkObject from the reference lookup tables, meaning
169  * it is no longer exposed over D-Bus.
170  */
171 static void
172 deregister_object (gpointer data, GObject *gobj)
173 {
174   guint ref;
175   GHashTable *subrefs_atk;
176   GHashTable *subrefs_hyperlink;
177   g_return_if_fail (ATK_IS_OBJECT (gobj) || ATK_IS_HYPERLINK (gobj));
178
179   subrefs_atk = (GHashTable *) g_object_get_data (gobj, "dbus-subrefs-atk");
180   if (subrefs_atk)
181     g_hash_table_foreach (subrefs_atk, deregister_sub_accessible, data);
182   
183   subrefs_hyperlink = (GHashTable *) g_object_get_data (gobj, "dbus-subrefs-hyperlink");
184   if (subrefs_hyperlink)
185     g_hash_table_foreach (subrefs_hyperlink, deregister_sub_hyperlink, data);
186   
187   if (ATK_IS_OBJECT (gobj))
188   {
189     ref = object_to_ref (ATK_OBJECT (gobj));
190     if (ref != 0)
191       {
192         spi_emit_cache_removal (ref, atk_adaptor_app_data->bus);
193         g_hash_table_remove(ref2ptr, GINT_TO_POINTER(ref));
194       }
195     }
196   }
197
198 static void
199 deregister_sub_accessible (gpointer key, gpointer obj_data, gpointer iter)
200 {
201   GObject *obj = G_OBJECT (obj_data);
202   deregister_object (NULL, obj);
203   g_object_unref (obj);
204 }
205
206 static void
207 deregister_sub_hyperlink (gpointer key, gpointer obj_data, gpointer iter)
208 {
209   guint ref;
210   GObject *ghyperlink = G_OBJECT (obj_data);
211
212   g_return_if_fail (ATK_IS_HYPERLINK (ghyperlink));
213
214   ref = gobject_to_ref (ghyperlink);
215   if (ref != 0)
216     {
217       g_hash_table_remove(ref2ptr, GINT_TO_POINTER(ref));
218     }
219   g_object_unref (ghyperlink);
220 }
221
222 static void
223 register_gobject (GObject *gobj, GObject *container)
224 {
225   guint ref;
226   g_return_if_fail (G_IS_OBJECT(gobj));
227
228   ref = assign_reference();
229
230   g_hash_table_insert (ref2ptr, GINT_TO_POINTER(ref), gobj);
231   g_object_set_data (G_OBJECT(gobj), "dbus-id", GINT_TO_POINTER(ref));
232   g_object_weak_ref(G_OBJECT(gobj), deregister_object, NULL);
233
234   if (container)
235   {
236     GHashTable *subrefs = (GHashTable *) g_object_get_data (G_OBJECT (container), "dbus-subrefs-atk");
237     if (!subrefs)
238     {
239       subrefs = g_hash_table_new(g_direct_hash, g_direct_equal);
240       g_object_set_data (G_OBJECT (container), "dbus-subrefs-atk", subrefs);
241     }
242     g_hash_table_insert (subrefs, GINT_TO_POINTER(ref), gobj);
243   }
244
245   if (ATK_IS_HYPERLINK (gobj))
246     g_object_ref (gobj);
247   else if (ATK_IS_OBJECT (gobj))
248   {
249     AtkObject *accessible = ATK_OBJECT (gobj);
250     AtkStateSet *state = atk_object_ref_state_set (accessible);
251     if (atk_state_set_contains_state (state, ATK_STATE_TRANSIENT) &&
252         atk_state_set_contains_state (state, ATK_STATE_SHOWING))
253     {
254       g_object_ref (gobj);
255     }
256     g_object_unref (state);
257   }
258 }
259
260 /*
261  * Called to register an AtkObject with AT-SPI and expose it over D-Bus.
262  */
263 static void
264 register_accessible (AtkObject *accessible, AtkObject *container)
265 {
266   g_return_if_fail (ATK_IS_OBJECT(accessible));
267
268   register_gobject (G_OBJECT (accessible), G_OBJECT (container));
269 }
270
271 static void
272 register_hyperlink (AtkHyperlink *hyperlink, AtkObject *container)
273 {
274   guint ref;
275   g_return_if_fail (ATK_IS_HYPERLINK (hyperlink));
276   g_return_if_fail (container);
277
278   ref = assign_reference();
279
280   g_hash_table_insert (ref2ptr, GINT_TO_POINTER(ref), hyperlink);
281   g_object_set_data (G_OBJECT(hyperlink), "dbus-id", GINT_TO_POINTER(ref));
282   g_object_ref (G_OBJECT (hyperlink));
283
284   GHashTable *subrefs = (GHashTable *) g_object_get_data (G_OBJECT (container), "dbus-subrefs-hyperlink");
285   if (!subrefs)
286   {
287     subrefs = g_hash_table_new(g_direct_hash, g_direct_equal);
288     g_object_set_data (G_OBJECT (container), "dbus-subrefs-hyperlink", GINT_TO_POINTER(ref));
289   }
290   g_hash_table_insert (subrefs, GINT_TO_POINTER(ref), hyperlink);
291 }
292
293 /*---------------------------------------------------------------------------*/
294
295 #ifdef SPI_ATK_DEBUG
296 /*
297  * This function checks that the ref-count of an accessible
298  * is greater than 1.
299  *
300  * There is not currently any remote reference counting
301  * in AT-SPI D-Bus so objects that are remotely owned are not
302  * allowed.
303  *
304  * TODO Add debug wrapper
305  */
306 static gboolean
307 non_owned_accessible (AtkObject *accessible)
308 {
309    if ((G_OBJECT (accessible))->ref_count <= 1)
310      {
311        g_warning ("AT-SPI: Child referenced that is not owned by its parent");
312        return TRUE;
313      }
314    else
315      {
316        return FALSE;
317      }
318 }
319 #endif /* SPI_ATK_DEBUG */
320
321 /*---------------------------------------------------------------------------*/
322
323 /* TRUE if we should not keep this object / tell the AT about it
324  * Currently true if TRANSIENT and not SHOWING
325  */
326 static gboolean
327 object_is_moot (AtkObject *accessible)
328 {
329    AtkStateSet *state;
330    gboolean result = FALSE;
331
332    /* This is dangerous, refing the state set
333     * seems to do wierd things to the tree & cause recursion
334     * by modifying the tree alot.
335     */
336    state = atk_object_ref_state_set (accessible);
337    if ( atk_state_set_contains_state (state, ATK_STATE_TRANSIENT) &&
338        !atk_state_set_contains_state (state, ATK_STATE_SHOWING))
339      {
340        result = TRUE;
341      }
342    g_object_unref (state);
343
344    return result;
345 }
346
347 static void
348 append_children (AtkObject *accessible, GQueue *traversal)
349 {
350   AtkObject *current;
351   guint i;
352   gint count = atk_object_get_n_accessible_children (accessible);
353
354   if (count < 0) count = 0;
355   for (i =0; i < count; i++)
356     {
357       current = atk_object_ref_accessible_child (accessible, i);
358       if (current)
359         {
360 #ifdef SPI_ATK_DEBUG
361           non_owned_accessible (current);
362 #endif
363           if (!object_is_moot (current))
364               g_queue_push_tail (traversal, current);
365           else
366               g_object_unref (G_OBJECT (current));
367         }
368     }
369 }
370
371 /*
372  * Registers a subtree of accessible objects
373  * rooted at the accessible object provided.
374  *
375  * The leaf nodes do not have their children
376  * registered. A node is considered a leaf
377  * if it has the state "manages-descendants"
378  * or if it has already been registered.
379  */
380 void
381 register_subtree (AtkObject *accessible)
382 {
383   AtkObject *current;
384   GQueue    *traversal;
385   GQueue    *emit_update;
386
387   g_return_if_fail (ATK_IS_OBJECT (accessible));
388
389   traversal = g_queue_new ();
390   emit_update = g_queue_new ();
391
392   g_object_ref (accessible);
393   g_queue_push_tail (traversal, accessible);
394
395   while (!g_queue_is_empty (traversal))
396     {
397       current = g_queue_pop_head (traversal);
398       g_queue_push_tail (emit_update, current);
399       if (!object_to_ref (current))
400         {
401           register_accessible (current, NULL);
402 #ifdef SPI_ATK_DEBUG
403           g_debug ("REG  - %s - %d - %s", atk_object_get_name     (current),
404                                           atk_object_get_role     (current),
405                                           atk_dbus_object_to_path (current));
406 #endif
407           append_children (current, traversal);
408         }
409     }
410
411   while (!g_queue_is_empty (emit_update))
412     {
413       current = g_queue_pop_head (emit_update);
414       spi_emit_cache_update (current, atk_adaptor_app_data->bus);
415       g_object_unref (G_OBJECT (current));
416     }
417
418   g_queue_free (traversal);
419   g_queue_free (emit_update);
420 }
421
422 /*---------------------------------------------------------------------------*/
423
424 /*
425  * Called when an already registered object is updated in such a
426  * way that client side cache needs to be updated.
427  */
428 static void
429 update_accessible (AtkObject *accessible)
430 {
431   guint  ref = 0;
432   g_return_if_fail (ATK_IS_OBJECT(accessible));
433
434   ref = object_to_ref (accessible);
435   if (ref)
436     {
437       spi_emit_cache_update (accessible, atk_adaptor_app_data->bus);
438     }
439 }
440
441 /*---------------------------------------------------------------------------*/
442
443 void
444 atk_dbus_foreach_registered(GHFunc func, gpointer data)
445 {
446   g_hash_table_foreach(ref2ptr, func, data);
447 }
448
449 /*
450  * Used to lookup an AtkObject from its D-Bus path.
451  */
452 GObject *
453 atk_dbus_path_to_gobject (const char *path)
454 {
455   guint index;
456   void *data;
457
458   g_return_val_if_fail (path, NULL);
459
460   if (strncmp(path, SPI_ATK_OBJECT_PATH_PREFIX, SPI_ATK_PATH_PREFIX_LENGTH) != 0)
461     return NULL;
462
463   path += SPI_ATK_PATH_PREFIX_LENGTH; /* Skip over the prefix */
464
465   if (!g_strcmp0 (SPI_ATK_OBJECT_PATH_DESKTOP, path))
466      return G_OBJECT (atk_get_root());
467   if (path[0] != '/')
468      return NULL;
469
470   path++;
471   index = atoi (path);
472   data = g_hash_table_lookup (ref2ptr, GINT_TO_POINTER(index));
473   if (data)
474     return G_OBJECT (data);
475   else
476     return NULL;
477 }
478
479 AtkObject *
480 atk_dbus_path_to_object (const char *path)
481 {
482   return ATK_OBJECT (atk_dbus_path_to_gobject (path));
483 }
484
485 /*
486  * TODO WARNING HACK This function is dangerous.
487  * It should only be called before sending an event on an
488  * object that has not already been registered.
489  */
490 gchar *
491 atk_dbus_object_attempt_registration (AtkObject *accessible)
492 {
493   guint ref;
494
495   ref = object_to_ref (accessible);
496   if (!ref)
497     {
498       /* See if the object is attached to the main tree */
499       AtkObject *current, *prev = NULL;
500       guint cref = 0;
501
502       /* This should iterate until it hits a NULL or registered parent */
503       prev = accessible;
504       current = atk_object_get_parent (accessible);
505       if (current)
506           cref = object_to_ref (current);
507       while (current && !cref)
508         {
509           prev = current;
510           current = atk_object_get_parent (current);
511           if (current)
512               cref = object_to_ref (current);
513         }
514
515       /* A registered parent, with non-registered child, has been found */
516       if (current)
517         {
518           register_subtree (prev);
519         }
520
521       /* The object SHOULD be registered now. If it isn't - I give up */
522       ref = object_to_ref (accessible);
523       if (ref)
524         {
525           return atk_dbus_ref_to_path (ref);
526         }
527       else
528         {
529 #ifdef SPI_ATK_DEBUG
530           g_debug ("AT-SPI: Could not register a non-attached accessible object");
531 #endif
532           return NULL;
533         }
534     }
535   else
536     {
537       return atk_dbus_ref_to_path (ref);
538     }
539 }
540
541
542 /*
543  * Used to lookup a D-Bus path from the AtkObject.
544  */
545 gchar *
546 atk_dbus_gobject_to_path_internal (GObject *gobj, gboolean do_register, GObject *container)
547 {
548   guint ref;
549
550   ref = gobject_to_ref (gobj);
551   if (!ref && do_register)
552   {
553     register_gobject (gobj, container);
554     ref = gobject_to_ref (gobj);
555   }
556
557   if (!ref)
558       return NULL;
559   else
560       return atk_dbus_ref_to_path (ref);
561 }
562
563 gchar *
564 atk_dbus_object_to_path (AtkObject *accessible, gboolean do_register)
565 {
566   AtkObject *container = (accessible && do_register? atk_object_get_parent (accessible): NULL);
567   return atk_dbus_gobject_to_path_internal (G_OBJECT (accessible), do_register, G_OBJECT (container));
568 }
569
570 gchar *
571 atk_dbus_sub_object_to_path (GObject *gobj, GObject *container)
572 {
573   return atk_dbus_gobject_to_path_internal (gobj, TRUE, container);
574 }
575
576 gchar *
577 atk_dbus_hyperlink_to_path (AtkHyperlink *hyperlink, AtkObject *container)
578 {
579   guint ref;
580
581   ref = gobject_to_ref (G_OBJECT (hyperlink));
582   if (!ref && container)
583   {
584     register_hyperlink (hyperlink, container);
585     ref = hyperlink_to_ref (hyperlink);
586   }
587
588   if (!ref)
589       return NULL;
590   else
591       return atk_dbus_ref_to_path (ref);
592 }
593
594 gchar *
595 atk_dbus_desktop_object_path ()
596 {
597   return g_strdup (SPI_ATK_OBJECT_PATH_PREFIX SPI_ATK_OBJECT_PATH_DESKTOP);
598 }
599
600 /*---------------------------------------------------------------------------*/
601
602 typedef gboolean (*TreeUpdateAction) (GSignalInvocationHint *signal_hint,
603                                       guint                  n_param_values,
604                                       const GValue          *param_values,
605                                       gpointer               data,
606                                       AtkObject             *accessible);
607
608 /*
609  * Events are not evaluated for non-registered accessibles.
610  *
611  * When a property change is made on a registered accessible
612  * the client side cache should be updated.
613  *
614  * When a parent is changed the subtree is de-registered
615  * if the parent is not attached to the root accessible.
616  */
617 /* TODO Turn this function into a macro? */
618 static gboolean
619 tree_update_wrapper (GSignalInvocationHint *signal_hint,
620                      guint                  n_param_values,
621                      const GValue          *param_values,
622                      gpointer               data,
623                      TreeUpdateAction       action)
624 {
625   AtkObject *accessible;
626
627   g_static_rec_mutex_lock (&registration_mutex);
628
629   /* Ensure that only registered accessibles
630    * have their signals processed.
631    */
632   accessible = ATK_OBJECT(g_value_get_object (&param_values[0]));
633   g_return_val_if_fail (ATK_IS_OBJECT (accessible), TRUE);
634
635   if (object_to_ref (accessible))
636     {
637 #ifdef SPI_ATK_DEBUG
638       if (recursion_check_and_set ())
639           g_warning ("AT-SPI: Recursive use of registration module");
640
641       g_debug ("AT-SPI: Tree update listener");
642 #endif
643       action (signal_hint, n_param_values, param_values, data, accessible);
644
645       recursion_check_unset ();
646     }
647
648   g_static_rec_mutex_unlock (&registration_mutex);
649
650   return TRUE;
651 }
652
653 static gboolean
654 tree_update_state_action (GSignalInvocationHint *signal_hint,
655                           guint                  n_param_values,
656                           const GValue          *param_values,
657                           gpointer               data,
658                           AtkObject             *accessible)
659 {
660   const gchar *name;
661   gboolean state;
662
663   if (n_param_values < 3)
664   {
665     g_warning ("at-spi: Not enough params in state-changed signal");
666                                 return TRUE;
667   }
668
669   name = g_value_get_string (param_values + 1);
670   state = g_value_get_boolean (param_values + 2);
671   if (!strcmp (name, "visible") && state == 0)
672   {
673     if (object_is_moot (accessible))
674     {
675       int ref_count = G_OBJECT(accessible)->ref_count;
676       g_object_unref (accessible);
677       /* If the ref count was >1, then someone else is still holding a ref,
678          but our ref is gone, so remove from the cache */
679       if (ref_count > 1)
680         deregister_object (NULL, G_OBJECT (accessible));
681       return TRUE;
682     }
683   }
684
685       update_accessible (accessible);
686   return TRUE;
687 }
688
689 static gboolean
690 tree_update_state_listener (GSignalInvocationHint *signal_hint,
691                             guint                  n_param_values,
692                             const GValue          *param_values,
693                             gpointer               data)
694 {
695       tree_update_wrapper (signal_hint, n_param_values, param_values, data, tree_update_state_action);
696   return TRUE;
697 }
698
699 static gboolean
700 tree_update_property_action (GSignalInvocationHint *signal_hint,
701                              guint                  n_param_values,
702                              const GValue          *param_values,
703                              gpointer               data,
704                              AtkObject             *accessible)
705 {
706       AtkPropertyValues *values;
707       const gchar *pname = NULL;
708
709       values = (AtkPropertyValues*) g_value_get_pointer (&param_values[1]);
710       pname = values[0].property_name;
711       if (strcmp (pname, "accessible-name") == 0 ||
712           strcmp (pname, "accessible-description") == 0 ||
713           strcmp (pname, "accessible-parent") == 0)
714         {
715           update_accessible (accessible);
716         }
717       /* Parent value us updated by child-add signal of parent object */
718       return TRUE;
719 }
720
721 static gboolean
722 tree_update_property_listener (GSignalInvocationHint *signal_hint,
723                                guint                  n_param_values,
724                                const GValue          *param_values,
725                                gpointer               data)
726 {
727   tree_update_wrapper (signal_hint, n_param_values, param_values, data, tree_update_property_action);
728   return TRUE;
729 }
730
731 static gboolean
732 tree_update_children_action (GSignalInvocationHint *signal_hint,
733                              guint                  n_param_values,
734                              const GValue          *param_values,
735                              gpointer               data,
736                              AtkObject             *accessible)
737 {
738       const gchar *detail = NULL;
739       AtkObject *child;
740
741       if (signal_hint->detail)
742           detail = g_quark_to_string (signal_hint->detail);
743
744       if (!strcmp (detail, "add"))
745         {
746           gpointer child;
747           int index = g_value_get_uint (param_values + 1);
748           child = g_value_get_pointer (param_values + 2);
749
750           if (!ATK_IS_OBJECT (child))
751             {
752               child = atk_object_ref_accessible_child (accessible, index);
753 #ifdef SPI_ATK_DEBUG
754               non_owned_accessible (child);
755 #endif
756             }
757           register_subtree (child);
758           update_accessible (accessible);
759         }
760       return TRUE;
761 }
762
763 static gboolean
764 tree_update_children_listener (GSignalInvocationHint *signal_hint,
765                                guint                  n_param_values,
766                                const GValue          *param_values,
767                                gpointer               data)
768 {
769       tree_update_wrapper (signal_hint, n_param_values, param_values, data, tree_update_children_action);
770       return TRUE;
771 }
772
773 /*---------------------------------------------------------------------------*/
774
775 static void
776 spi_atk_register_toplevel_added (AtkObject *accessible,
777                                  guint     index,
778                                  AtkObject *child)
779 {
780   g_static_rec_mutex_lock (&registration_mutex);
781
782   g_return_if_fail (ATK_IS_OBJECT (accessible));
783
784   if (object_to_ref (accessible))
785     {
786 #ifdef SPI_ATK_DEBUG
787       if (recursion_check_and_set ())
788           g_warning ("AT-SPI: Recursive use of registration module");
789
790       g_debug ("AT-SPI: Toplevel added listener");
791 #endif
792       if (!ATK_IS_OBJECT (child))
793         {
794           child = atk_object_ref_accessible_child (accessible, index);
795 #ifdef SPI_ATK_DEBUG
796           non_owned_accessible (child);
797 #endif
798         }
799       register_subtree (child);
800       update_accessible (accessible);
801
802       recursion_check_unset ();
803     }
804
805   g_static_rec_mutex_unlock (&registration_mutex);
806 }
807
808 static void
809 spi_atk_register_toplevel_removed (AtkObject *accessible,
810                                    guint     index,
811                                    AtkObject *child)
812 {
813   g_static_rec_mutex_lock (&registration_mutex);
814
815   g_return_if_fail (ATK_IS_OBJECT (accessible));
816
817   if (object_to_ref (accessible))
818     {
819 #ifdef SPI_ATK_DEBUG
820       if (recursion_check_and_set ())
821           g_warning ("AT-SPI: Recursive use of registration module");
822
823       g_debug ("AT-SPI: Toplevel removed listener");
824 #endif
825       update_accessible (accessible);
826       recursion_check_unset ();
827     }
828
829   g_static_rec_mutex_unlock (&registration_mutex);
830 }
831
832 /*
833  * Initializes required global data. The update and removal lists
834  * and the reference lookup tables.
835  *
836  * Initializes all of the required D-Bus interfaces.
837  */
838 void
839 atk_dbus_initialize (AtkObject *root)
840 {
841   if (!ref2ptr)
842     ref2ptr = g_hash_table_new(g_direct_hash, g_direct_equal);
843
844 #ifdef SPI_ATK_DEBUG
845   if (g_thread_supported ())
846       g_message ("AT-SPI: Threads enabled");
847
848   g_debug ("AT-SPI: Initial Atk tree regisration");
849 #endif
850
851   register_subtree (root);
852
853   atk_add_global_event_listener (tree_update_property_listener, "Gtk:AtkObject:property-change");
854   atk_add_global_event_listener (tree_update_children_listener, "Gtk:AtkObject:children-changed");
855   atk_add_global_event_listener (tree_update_state_listener, "Gtk:AtkObject:state-change");
856
857   g_signal_connect (root,
858                     "children-changed::add",
859                     (GCallback) spi_atk_register_toplevel_added,
860                     NULL);
861   g_signal_connect (root,
862                     "children-changed::remove",
863                     (GCallback) spi_atk_register_toplevel_removed,
864                     NULL);
865 }
866
867 /*END------------------------------------------------------------------------*/