Send UpdateAccessible for transient objects
[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 "/desktop"
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 static GHashTable *objects_with_subrefs = NULL;
81 static GHashTable *leased_refs = NULL;
82 static int leased_refs_count;
83
84 static guint reference_counter = 0;
85
86 static GStaticRecMutex registration_mutex = G_STATIC_REC_MUTEX_INIT;
87
88 /*---------------------------------------------------------------------------*/
89
90 static GStaticMutex   recursion_check_guard = G_STATIC_MUTEX_INIT;
91 static gboolean       recursion_check = FALSE;
92 static int last_gc_time;
93
94 static void deregister_sub_accessible (gpointer key, gpointer obj_data, gpointer iter);
95
96 static void deregister_sub_hyperlink (gpointer key, gpointer obj_data, gpointer iter);
97
98 static gboolean
99 recursion_check_and_set ()
100 {
101   gboolean ret;
102   g_static_mutex_lock   (&recursion_check_guard);
103   ret = recursion_check;
104   recursion_check = TRUE;
105   g_static_mutex_unlock (&recursion_check_guard);
106   return ret;
107 }
108
109 static void
110 recursion_check_unset ()
111 {
112   g_static_mutex_lock   (&recursion_check_guard);
113   recursion_check = FALSE;
114   g_static_mutex_unlock (&recursion_check_guard);
115 }
116
117 /*---------------------------------------------------------------------------*/
118
119 /*
120  * Each AtkObject must be asssigned a D-Bus path (Reference)
121  *
122  * This function provides an integer reference for a new
123  * AtkObject.
124  */
125 static guint
126 assign_reference(void)
127 {
128   reference_counter++;
129   /* Reference of 0 not allowed as used as direct key in hash table */
130   if (reference_counter == 0)
131     reference_counter++;
132   /* TODO: If we've wrapped, ensure that two objects don't have the same ref */
133   return reference_counter;
134 }
135
136 /*
137  * Returns the reference of the object, or 0 if it is not registered.
138  */
139 static guint
140 gobject_to_ref (GObject *gobj)
141 {
142   return GPOINTER_TO_INT(g_object_get_data (gobj, "dbus-id"));
143 }
144
145 static guint
146 object_to_ref (AtkObject *accessible)
147 {
148   return gobject_to_ref (G_OBJECT (accessible));
149 }
150
151 static guint
152 hyperlink_to_ref (AtkHyperlink *link)
153 {
154   return gobject_to_ref (G_OBJECT (link));
155 }
156
157 /*
158  * Converts the Accessible object reference to its D-Bus object path
159  */
160 gchar *
161 atk_dbus_ref_to_path (guint ref)
162 {
163   return g_strdup_printf(SPI_ATK_OBJECT_REFERENCE_TEMPLATE, ref);
164 }
165
166 /*---------------------------------------------------------------------------*/
167
168 /*
169  * Callback for when a registered AtkObject is destroyed.
170  *
171  * Removes the AtkObject from the reference lookup tables, meaning
172  * it is no longer exposed over D-Bus.
173  */
174 static void
175 deregister_object (gpointer data, GObject *gobj)
176 {
177   guint ref;
178   GHashTable *subrefs_atk;
179   GHashTable *subrefs_hyperlink;
180   g_return_if_fail (ATK_IS_OBJECT (gobj) || ATK_IS_HYPERLINK (gobj));
181
182   subrefs_atk = (GHashTable *) g_object_get_data (gobj, "dbus-subrefs-atk");
183   subrefs_hyperlink = (GHashTable *) g_object_get_data (gobj, "dbus-subrefs-hyperlink");
184
185   if (subrefs_atk)
186   {
187     g_hash_table_foreach (subrefs_atk, deregister_sub_accessible, data);
188     g_hash_table_unref (subrefs_atk);
189   }
190   
191   if (subrefs_hyperlink)
192   {
193     g_hash_table_foreach (subrefs_hyperlink, deregister_sub_hyperlink, data);
194     g_hash_table_unref (subrefs_hyperlink);
195   }
196   
197   if (subrefs_atk || subrefs_hyperlink)
198     g_hash_table_remove (objects_with_subrefs, gobj);
199
200   if (ATK_IS_OBJECT (gobj))
201   {
202     ref = object_to_ref (ATK_OBJECT (gobj));
203     if (ref != 0)
204       {
205         spi_emit_cache_removal (ref, atk_adaptor_app_data->bus);
206         g_hash_table_remove(ref2ptr, GINT_TO_POINTER(ref));
207       }
208     }
209   }
210
211 static void
212 deregister_sub_accessible (gpointer key, gpointer obj_data, gpointer iter)
213 {
214   GObject *obj = G_OBJECT (obj_data);
215   deregister_object (NULL, obj);
216   g_hash_table_remove (leased_refs, obj);
217   g_object_unref (obj);
218 }
219
220 static void
221 deregister_sub_hyperlink (gpointer key, gpointer obj_data, gpointer iter)
222 {
223   guint ref;
224   GObject *ghyperlink = G_OBJECT (obj_data);
225
226   g_return_if_fail (ATK_IS_HYPERLINK (ghyperlink));
227
228   ref = gobject_to_ref (ghyperlink);
229   if (ref != 0)
230     {
231       g_hash_table_remove(ref2ptr, GINT_TO_POINTER(ref));
232     }
233   g_object_unref (ghyperlink);
234 }
235
236 static void
237 register_gobject (GObject *gobj, GObject *container)
238 {
239   guint ref;
240   g_return_if_fail (G_IS_OBJECT(gobj));
241
242   ref = assign_reference();
243
244   g_hash_table_insert (ref2ptr, GINT_TO_POINTER(ref), gobj);
245   g_object_set_data (G_OBJECT(gobj), "dbus-id", GINT_TO_POINTER(ref));
246   g_object_weak_ref(G_OBJECT(gobj), deregister_object, NULL);
247
248   if (container)
249   {
250     GHashTable *subrefs = (GHashTable *) g_object_get_data (G_OBJECT (container), "dbus-subrefs-atk");
251     if (!subrefs)
252     {
253       subrefs = g_hash_table_new(g_direct_hash, g_direct_equal);
254       g_object_set_data (G_OBJECT (container), "dbus-subrefs-atk", subrefs);
255     }
256     g_hash_table_insert (subrefs, GINT_TO_POINTER(ref), gobj);
257     g_hash_table_insert (objects_with_subrefs, gobj, subrefs);
258   }
259
260   if (ATK_IS_HYPERLINK (gobj))
261     g_object_ref (gobj);
262   else if (ATK_IS_OBJECT (gobj))
263   {
264     AtkObject *accessible = ATK_OBJECT (gobj);
265     AtkStateSet *state = atk_object_ref_state_set (accessible);
266     if (atk_state_set_contains_state (state, ATK_STATE_TRANSIENT))
267     {
268       g_object_ref (gobj);
269       /* We should only get here as the result of a query other than GetTree */
270       spi_emit_cache_update (accessible, atk_adaptor_app_data->bus);
271     }
272     g_object_unref (state);
273   }
274 }
275
276 /*
277  * Called to register an AtkObject with AT-SPI and expose it over D-Bus.
278  */
279 static void
280 register_accessible (AtkObject *accessible, AtkObject *container)
281 {
282   g_return_if_fail (ATK_IS_OBJECT(accessible));
283
284   register_gobject (G_OBJECT (accessible), G_OBJECT (container));
285 }
286
287 static void
288 register_hyperlink (AtkHyperlink *hyperlink, AtkObject *container)
289 {
290   guint ref;
291   g_return_if_fail (ATK_IS_HYPERLINK (hyperlink));
292   g_return_if_fail (container);
293
294   ref = assign_reference();
295
296   g_hash_table_insert (ref2ptr, GINT_TO_POINTER(ref), hyperlink);
297   g_object_set_data (G_OBJECT(hyperlink), "dbus-id", GINT_TO_POINTER(ref));
298   g_object_ref (G_OBJECT (hyperlink));
299
300   GHashTable *subrefs = (GHashTable *) g_object_get_data (G_OBJECT (container), "dbus-subrefs-hyperlink");
301   if (!subrefs)
302   {
303     subrefs = g_hash_table_new(g_direct_hash, g_direct_equal);
304     g_object_set_data (G_OBJECT (container), "dbus-subrefs-hyperlink", GINT_TO_POINTER(ref));
305   }
306   g_hash_table_insert (subrefs, GINT_TO_POINTER(ref), hyperlink);
307 }
308
309 /*---------------------------------------------------------------------------*/
310
311 #ifdef SPI_ATK_DEBUG
312 /*
313  * This function checks that the ref-count of an accessible
314  * is greater than 1.
315  *
316  * There is not currently any remote reference counting
317  * in AT-SPI D-Bus so objects that are remotely owned are not
318  * allowed.
319  *
320  * TODO Add debug wrapper
321  */
322 static gboolean
323 non_owned_accessible (AtkObject *accessible)
324 {
325    if ((G_OBJECT (accessible))->ref_count <= 1)
326      {
327        g_warning ("AT-SPI: Child referenced that is not owned by its parent");
328        return TRUE;
329      }
330    else
331      {
332        return FALSE;
333      }
334 }
335 #endif /* SPI_ATK_DEBUG */
336
337 /*---------------------------------------------------------------------------*/
338
339 /* TRUE if we should not keep this object / tell the AT about it
340  * Currently true if TRANSIENT and not SHOWING
341  */
342 static gboolean
343 object_is_moot (AtkObject *accessible)
344 {
345    AtkStateSet *state;
346    gboolean result = FALSE;
347
348    /* This is dangerous, refing the state set
349     * seems to do wierd things to the tree & cause recursion
350     * by modifying the tree alot.
351     */
352    state = atk_object_ref_state_set (accessible);
353    if ( atk_state_set_contains_state (state, ATK_STATE_TRANSIENT) &&
354        !atk_state_set_contains_state (state, ATK_STATE_SHOWING))
355      {
356        result = TRUE;
357      }
358    g_object_unref (state);
359
360    return result;
361 }
362
363 static void
364 append_children (AtkObject *accessible, GQueue *traversal)
365 {
366   AtkObject *current;
367   guint i;
368   gint count = atk_object_get_n_accessible_children (accessible);
369
370   if (count < 0) count = 0;
371   for (i =0; i < count; i++)
372     {
373       current = atk_object_ref_accessible_child (accessible, i);
374       if (current)
375         {
376 #ifdef SPI_ATK_DEBUG
377           non_owned_accessible (current);
378 #endif
379           g_queue_push_tail (traversal, current);
380         }
381     }
382 }
383
384 /*
385  * Registers a subtree of accessible objects
386  * rooted at the accessible object provided.
387  *
388  * The leaf nodes do not have their children
389  * registered. A node is considered a leaf
390  * if it has the state "manages-descendants"
391  * or if it has already been registered.
392  */
393 void
394 register_subtree (AtkObject *accessible)
395 {
396   AtkObject *current;
397   GQueue    *traversal;
398   GQueue    *emit_update;
399
400   g_return_if_fail (ATK_IS_OBJECT (accessible));
401
402   traversal = g_queue_new ();
403   emit_update = g_queue_new ();
404
405   g_object_ref (accessible);
406   g_queue_push_tail (traversal, accessible);
407
408   while (!g_queue_is_empty (traversal))
409     {
410       current = g_queue_pop_head (traversal);
411       g_queue_push_tail (emit_update, current);
412       if (!object_to_ref (current))
413         {
414           register_accessible (current, NULL);
415 #ifdef SPI_ATK_DEBUG
416           g_debug ("REG  - %s - %d - %s", atk_object_get_name     (current),
417                                           atk_object_get_role     (current),
418                                           atk_dbus_object_to_path (current));
419 #endif
420           append_children (current, traversal);
421         }
422     }
423
424   while (!g_queue_is_empty (emit_update))
425     {
426       current = g_queue_pop_head (emit_update);
427       spi_emit_cache_update (current, atk_adaptor_app_data->bus);
428       g_object_unref (G_OBJECT (current));
429     }
430
431   g_queue_free (traversal);
432   g_queue_free (emit_update);
433 }
434
435 /*---------------------------------------------------------------------------*/
436
437 /*
438  * Called when an already registered object is updated in such a
439  * way that client side cache needs to be updated.
440  */
441 static void
442 update_accessible (AtkObject *accessible)
443 {
444   guint  ref = 0;
445   g_return_if_fail (ATK_IS_OBJECT(accessible));
446
447   ref = object_to_ref (accessible);
448   if (ref)
449     {
450       spi_emit_cache_update (accessible, atk_adaptor_app_data->bus);
451     }
452 }
453
454 /*---------------------------------------------------------------------------*/
455
456 void
457 atk_dbus_foreach_registered(GHFunc func, gpointer data)
458 {
459   g_hash_table_foreach(ref2ptr, func, data);
460 }
461
462 /*
463  * Used to lookup an AtkObject from its D-Bus path.
464  */
465 GObject *
466 atk_dbus_path_to_gobject (const char *path)
467 {
468   guint index;
469   void *data;
470
471   g_return_val_if_fail (path, NULL);
472
473   if (strncmp(path, SPI_ATK_OBJECT_PATH_PREFIX, SPI_ATK_PATH_PREFIX_LENGTH) != 0)
474     return NULL;
475
476   path += SPI_ATK_PATH_PREFIX_LENGTH; /* Skip over the prefix */
477
478   if (!g_strcmp0 (SPI_ATK_OBJECT_PATH_DESKTOP, path))
479      return G_OBJECT (atk_get_root());
480   if (path[0] != '/')
481      return NULL;
482
483   path++;
484   index = atoi (path);
485   data = g_hash_table_lookup (ref2ptr, GINT_TO_POINTER(index));
486   if (data)
487   {
488     GObject *gobj = G_OBJECT (data);
489     g_object_set_data (gobj, "last-ref-time", (gpointer) time (NULL));
490     return gobj;
491   }
492   else
493     return NULL;
494 }
495
496 AtkObject *
497 atk_dbus_path_to_object (const char *path)
498 {
499   return ATK_OBJECT (atk_dbus_path_to_gobject (path));
500 }
501
502 /*
503  * TODO WARNING HACK This function is dangerous.
504  * It should only be called before sending an event on an
505  * object that has not already been registered.
506  */
507 gchar *
508 atk_dbus_object_attempt_registration (AtkObject *accessible)
509 {
510   guint ref;
511
512   ref = object_to_ref (accessible);
513   if (!ref)
514     {
515       /* See if the object is attached to the main tree */
516       AtkObject *current, *prev = NULL;
517       guint cref = 0;
518
519       /* This should iterate until it hits a NULL or registered parent */
520       prev = accessible;
521       current = atk_object_get_parent (accessible);
522       if (current)
523           cref = object_to_ref (current);
524       while (current && !cref)
525         {
526           prev = current;
527           current = atk_object_get_parent (current);
528           if (current)
529               cref = object_to_ref (current);
530         }
531
532       /* A registered parent, with non-registered child, has been found */
533       if (current)
534         {
535           register_subtree (prev);
536         }
537
538       /* The object SHOULD be registered now. If it isn't - I give up */
539       ref = object_to_ref (accessible);
540       if (ref)
541         {
542           return atk_dbus_ref_to_path (ref);
543         }
544       else
545         {
546 #ifdef SPI_ATK_DEBUG
547           g_debug ("AT-SPI: Could not register a non-attached accessible object");
548 #endif
549           return NULL;
550         }
551     }
552   else
553     {
554       return atk_dbus_ref_to_path (ref);
555     }
556 }
557
558
559 /*
560  * Used to lookup a D-Bus path from the AtkObject.
561  */
562 static gchar *
563 atk_dbus_gobject_to_path_internal (GObject *gobj, gboolean do_register, GObject *container)
564 {
565   guint ref;
566
567   ref = gobject_to_ref (gobj);
568   if (!ref && do_register)
569   {
570     register_gobject (gobj, container);
571     ref = gobject_to_ref (gobj);
572   }
573
574   if (!ref)
575       return NULL;
576   else
577       return atk_dbus_ref_to_path (ref);
578 }
579
580 gchar *
581 atk_dbus_object_to_path (AtkObject *accessible, gboolean do_register)
582 {
583   AtkObject *container = (accessible && do_register? atk_object_get_parent (accessible): NULL);
584   return atk_dbus_gobject_to_path_internal (G_OBJECT (accessible), do_register, G_OBJECT (container));
585 }
586
587 gchar *
588 atk_dbus_sub_object_to_path (GObject *gobj, GObject *container)
589 {
590   return atk_dbus_gobject_to_path_internal (gobj, TRUE, container);
591 }
592
593 gchar *
594 atk_dbus_hyperlink_to_path (AtkHyperlink *hyperlink, AtkObject *container)
595 {
596   guint ref;
597
598   ref = gobject_to_ref (G_OBJECT (hyperlink));
599   if (!ref && container)
600   {
601     register_hyperlink (hyperlink, container);
602     ref = hyperlink_to_ref (hyperlink);
603   }
604
605   if (!ref)
606       return NULL;
607   else
608       return atk_dbus_ref_to_path (ref);
609 }
610
611 gchar *
612 atk_dbus_desktop_object_path ()
613 {
614   return g_strdup (SPI_ATK_OBJECT_PATH_PREFIX SPI_ATK_OBJECT_PATH_DESKTOP);
615 }
616
617 /*---------------------------------------------------------------------------*/
618
619 typedef gboolean (*TreeUpdateAction) (GSignalInvocationHint *signal_hint,
620                                       guint                  n_param_values,
621                                       const GValue          *param_values,
622                                       gpointer               data,
623                                       AtkObject             *accessible);
624
625 /*
626  * Events are not evaluated for non-registered accessibles.
627  *
628  * When a property change is made on a registered accessible
629  * the client side cache should be updated.
630  *
631  * When a parent is changed the subtree is de-registered
632  * if the parent is not attached to the root accessible.
633  */
634 /* TODO Turn this function into a macro? */
635 static gboolean
636 tree_update_wrapper (GSignalInvocationHint *signal_hint,
637                      guint                  n_param_values,
638                      const GValue          *param_values,
639                      gpointer               data,
640                      TreeUpdateAction       action)
641 {
642   AtkObject *accessible;
643
644   g_static_rec_mutex_lock (&registration_mutex);
645
646   /* Ensure that only registered accessibles
647    * have their signals processed.
648    */
649   accessible = ATK_OBJECT(g_value_get_object (&param_values[0]));
650   g_return_val_if_fail (ATK_IS_OBJECT (accessible), TRUE);
651
652   if (object_to_ref (accessible))
653     {
654 #ifdef SPI_ATK_DEBUG
655       if (recursion_check_and_set ())
656           g_warning ("AT-SPI: Recursive use of registration module");
657
658       g_debug ("AT-SPI: Tree update listener");
659 #endif
660       action (signal_hint, n_param_values, param_values, data, accessible);
661
662       recursion_check_unset ();
663     }
664
665   g_static_rec_mutex_unlock (&registration_mutex);
666
667   return TRUE;
668 }
669
670 static gboolean
671 maybe_expire_lease (gpointer key, gpointer obj_data, gpointer iter)
672 {
673   time_t secs = time (NULL) - (time_t)obj_data;
674
675   if (secs < 30)
676     return FALSE;
677   deregister_sub_accessible (key, obj_data, iter);
678   return TRUE;
679 }
680
681 static void
682 expire_old_leases_in (gpointer key, gpointer obj_data, gpointer iter)
683 {
684   g_hash_table_foreach_remove ((GHashTable *)obj_data, maybe_expire_lease, NULL);
685 }
686
687 static void
688 expire_old_leases ()
689 {
690   g_hash_table_foreach (objects_with_subrefs, expire_old_leases_in, NULL);
691 }
692
693 static gboolean
694 tree_update_state_action (GSignalInvocationHint *signal_hint,
695                           guint                  n_param_values,
696                           const GValue          *param_values,
697                           gpointer               data,
698                           AtkObject             *accessible)
699 {
700   const gchar *name;
701   gboolean state;
702
703   if (n_param_values < 3)
704   {
705     g_warning ("at-spi: Not enough params in state-changed signal");
706                                 return TRUE;
707   }
708
709   name = g_value_get_string (param_values + 1);
710   state = g_value_get_boolean (param_values + 2);
711   if (!strcmp (name, "visible"))
712   {
713     AtkStateSet *set = atk_object_ref_state_set (accessible);
714     if (atk_state_set_contains_state (set, ATK_STATE_TRANSIENT))
715     {
716       if (state == 0)
717       {
718         g_hash_table_insert (leased_refs, accessible, (gpointer) time (NULL));
719         leased_refs_count++;
720         /* todo: Set to a high number: 5 for dbg. */
721         if (leased_refs_count > 5)
722           expire_old_leases ();
723       }
724       else
725       {
726         g_hash_table_remove (leased_refs, accessible);
727         leased_refs_count--;
728       }
729     }
730     g_object_unref (set);
731   }
732
733       update_accessible (accessible);
734   return TRUE;
735 }
736
737 static gboolean
738 tree_update_state_listener (GSignalInvocationHint *signal_hint,
739                             guint                  n_param_values,
740                             const GValue          *param_values,
741                             gpointer               data)
742 {
743       tree_update_wrapper (signal_hint, n_param_values, param_values, data, tree_update_state_action);
744   return TRUE;
745 }
746
747 static gboolean
748 tree_update_property_action (GSignalInvocationHint *signal_hint,
749                              guint                  n_param_values,
750                              const GValue          *param_values,
751                              gpointer               data,
752                              AtkObject             *accessible)
753 {
754       AtkPropertyValues *values;
755       const gchar *pname = NULL;
756
757       values = (AtkPropertyValues*) g_value_get_pointer (&param_values[1]);
758       pname = values[0].property_name;
759       if (strcmp (pname, "accessible-name") == 0 ||
760           strcmp (pname, "accessible-description") == 0 ||
761           strcmp (pname, "accessible-parent") == 0 ||
762           strcmp (pname, "accessible-role") == 0)
763         {
764           update_accessible (accessible);
765         }
766       /* Parent value us updated by child-add signal of parent object */
767       return TRUE;
768 }
769
770 static gboolean
771 tree_update_property_listener (GSignalInvocationHint *signal_hint,
772                                guint                  n_param_values,
773                                const GValue          *param_values,
774                                gpointer               data)
775 {
776   tree_update_wrapper (signal_hint, n_param_values, param_values, data, tree_update_property_action);
777   return TRUE;
778 }
779
780 static gboolean
781 tree_update_children_action (GSignalInvocationHint *signal_hint,
782                              guint                  n_param_values,
783                              const GValue          *param_values,
784                              gpointer               data,
785                              AtkObject             *accessible)
786 {
787       const gchar *detail = NULL;
788       AtkObject *child;
789
790       if (signal_hint->detail)
791           detail = g_quark_to_string (signal_hint->detail);
792
793       if (!strcmp (detail, "add"))
794         {
795           gpointer child;
796           int index = g_value_get_uint (param_values + 1);
797           child = g_value_get_pointer (param_values + 2);
798
799           if (!ATK_IS_OBJECT (child))
800             {
801               child = atk_object_ref_accessible_child (accessible, index);
802 #ifdef SPI_ATK_DEBUG
803               non_owned_accessible (child);
804 #endif
805             }
806           register_subtree (child);
807           update_accessible (accessible);
808         }
809       else if (!strcmp (detail, "remove"))
810       {
811         update_accessible (accessible);
812       }
813       return TRUE;
814 }
815
816 static gboolean
817 tree_update_children_listener (GSignalInvocationHint *signal_hint,
818                                guint                  n_param_values,
819                                const GValue          *param_values,
820                                gpointer               data)
821 {
822       tree_update_wrapper (signal_hint, n_param_values, param_values, data, tree_update_children_action);
823       return TRUE;
824 }
825
826 /*---------------------------------------------------------------------------*/
827
828 static void
829 spi_atk_register_toplevel_added (AtkObject *accessible,
830                                  guint     index,
831                                  AtkObject *child)
832 {
833   g_static_rec_mutex_lock (&registration_mutex);
834
835   g_return_if_fail (ATK_IS_OBJECT (accessible));
836
837   if (object_to_ref (accessible))
838     {
839 #ifdef SPI_ATK_DEBUG
840       if (recursion_check_and_set ())
841           g_warning ("AT-SPI: Recursive use of registration module");
842
843       g_debug ("AT-SPI: Toplevel added listener");
844 #endif
845       if (!ATK_IS_OBJECT (child))
846         {
847           child = atk_object_ref_accessible_child (accessible, index);
848 #ifdef SPI_ATK_DEBUG
849           non_owned_accessible (child);
850 #endif
851         }
852       register_subtree (child);
853       update_accessible (accessible);
854
855       recursion_check_unset ();
856     }
857
858   g_static_rec_mutex_unlock (&registration_mutex);
859 }
860
861 static void
862 spi_atk_register_toplevel_removed (AtkObject *accessible,
863                                    guint     index,
864                                    AtkObject *child)
865 {
866   g_static_rec_mutex_lock (&registration_mutex);
867
868   g_return_if_fail (ATK_IS_OBJECT (accessible));
869
870   if (object_to_ref (accessible))
871     {
872 #ifdef SPI_ATK_DEBUG
873       if (recursion_check_and_set ())
874           g_warning ("AT-SPI: Recursive use of registration module");
875
876       g_debug ("AT-SPI: Toplevel removed listener");
877 #endif
878       update_accessible (accessible);
879       recursion_check_unset ();
880     }
881
882   g_static_rec_mutex_unlock (&registration_mutex);
883 }
884
885 /*
886  * Initializes required global data. The update and removal lists
887  * and the reference lookup tables.
888  *
889  * Initializes all of the required D-Bus interfaces.
890  */
891 void
892 atk_dbus_initialize (AtkObject *root)
893 {
894   if (!ref2ptr)
895     ref2ptr = g_hash_table_new(g_direct_hash, g_direct_equal);
896
897   if (!objects_with_subrefs)
898     objects_with_subrefs = g_hash_table_new(g_direct_hash, g_direct_equal);
899
900   if (!leased_refs)
901     leased_refs = g_hash_table_new(g_direct_hash, g_direct_equal);
902
903 #ifdef SPI_ATK_DEBUG
904   if (g_thread_supported ())
905       g_message ("AT-SPI: Threads enabled");
906
907   g_debug ("AT-SPI: Initial Atk tree regisration");
908 #endif
909
910   register_subtree (root);
911
912   atk_add_global_event_listener (tree_update_property_listener, "Gtk:AtkObject:property-change");
913   atk_add_global_event_listener (tree_update_children_listener, "Gtk:AtkObject:children-changed");
914   atk_add_global_event_listener (tree_update_state_listener, "Gtk:AtkObject:state-change");
915
916   g_signal_connect (root,
917                     "children-changed::add",
918                     (GCallback) spi_atk_register_toplevel_added,
919                     NULL);
920   g_signal_connect (root,
921                     "children-changed::remove",
922                     (GCallback) spi_atk_register_toplevel_removed,
923                     NULL);
924 }
925
926 /*END------------------------------------------------------------------------*/