2009-04-23 Mark Doffman <mark.doffman@codethink.co.uk>
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / accessible-register.c
index af9f752..c6c7328 100644 (file)
@@ -77,7 +77,7 @@
 
 static GHashTable *ref2ptr = NULL; /* Used for converting a D-Bus path (Reference) to the object pointer */
 
-static guint counter = 1;
+static guint reference_counter = 0;
 
 static GStaticRecMutex registration_mutex = G_STATIC_REC_MUTEX_INIT;
 
@@ -116,10 +116,11 @@ recursion_check_unset ()
 static guint
 assign_reference(void)
 {
-  counter++;
+  reference_counter++;
   /* Reference of 0 not allowed as used as direct key in hash table */
-  if (counter == 0)
-    counter++;
+  if (reference_counter == 0)
+    reference_counter++;
+  return reference_counter;
 }
 
 /*
@@ -152,7 +153,7 @@ static void
 deregister_accessible (gpointer data, GObject *accessible)
 {
   guint ref;
-  g_assert (ATK_IS_OBJECT (accessible));
+  g_return_if_fail (ATK_IS_OBJECT (accessible));
 
   ref = object_to_ref (ATK_OBJECT(accessible));
   if (ref != 0)
@@ -168,7 +169,7 @@ static void
 register_accessible (AtkObject *accessible)
 {
   guint ref;
-  g_assert(ATK_IS_OBJECT(accessible));
+  g_return_if_fail (ATK_IS_OBJECT(accessible));
 
   ref = assign_reference();
 
@@ -230,6 +231,29 @@ has_manages_descendants (AtkObject *accessible)
    return result;
 }
 
+static void
+append_children (AtkObject *accessible, GQueue *traversal)
+{
+  AtkObject *current;
+  guint i;
+
+  for (i =0; i < atk_object_get_n_accessible_children (accessible); i++)
+    {
+      current = atk_object_ref_accessible_child (accessible, i);
+#ifdef SPI_ATK_DEBUG
+      non_owned_accessible (current);
+#endif
+      if (!has_manages_descendants (current))
+        {
+          g_queue_push_tail (traversal, current);
+        }
+      else
+        {
+          g_object_unref (G_OBJECT (current));
+        }
+    }
+}
+
 /*
  * Registers a subtree of accessible objects
  * rooted at the accessible object provided.
@@ -242,88 +266,43 @@ has_manages_descendants (AtkObject *accessible)
 void
 register_subtree (AtkObject *accessible)
 {
-  AtkObject *current, *tmp;
-  GQueue    *stack;
-  guint      i;
-  gboolean   recurse;
+  AtkObject *current;
+  GQueue    *traversal;
+  GQueue    *emit_update;
 
+  g_return_if_fail (ATK_IS_OBJECT (accessible));
 
-  current = g_object_ref (accessible);
-  if (has_manages_descendants (current))
-    {
-      g_object_unref (current);
-      return;
-    }
+  traversal = g_queue_new ();
+  emit_update = g_queue_new ();
 
-  stack = g_queue_new ();
+  g_object_ref (accessible);
+  g_queue_push_tail (traversal, accessible);
 
-  register_accessible (current);
-  g_queue_push_head (stack, GINT_TO_POINTER (0));
-
-  /*
-   * The index held on the stack is the next child node
-   * that needs processing at the corresponding level in the tree.
-   */
-  while (!g_queue_is_empty (stack))
+  while (!g_queue_is_empty (traversal))
     {
-      /* Find the next child node that needs processing */
-
-      i = GPOINTER_TO_INT(g_queue_peek_head (stack));
-      recurse = FALSE;
-
-      while (i < atk_object_get_n_accessible_children (current) &&
-             recurse == FALSE)
+      current = g_queue_pop_head (traversal);
+      g_queue_push_tail (emit_update, current);
+      if (!object_to_ref (current))
         {
-          tmp = atk_object_ref_accessible_child (current, i);
-
+          register_accessible (current);
 #ifdef SPI_ATK_DEBUG
-          non_owned_accessible (tmp);
+          g_debug ("REG  - %s - %d - %s", atk_object_get_name     (current),
+                                          atk_object_get_role     (current),
+                                          atk_dbus_object_to_path (current));
 #endif
-
-          if (object_to_ref (tmp))
-            {
-              /* If its already registered, just update */
-              spi_emit_cache_update (tmp, atk_adaptor_app_data->bus);
-            }
-          else if (has_manages_descendants (tmp))
-            {
-              /* If it has manages descendants, just register and update */
-              register_accessible (tmp);
-              spi_emit_cache_update (tmp, atk_adaptor_app_data->bus);
-            }
-          else
-            {
-              recurse = TRUE;
-            }
-
-          if (!recurse)
-            {
-              g_object_unref (G_OBJECT (tmp));
-            }
-
-          i++;
+          append_children (current, traversal);
         }
+    }
 
-      if (recurse)
-        {
-          /* Push onto stack */
-          current = tmp;
-          register_accessible (current);
-
-          g_queue_peek_head_link (stack)->data = GINT_TO_POINTER (i);
-          g_queue_push_head (stack, GINT_TO_POINTER (0));
-        }
-      else
-        {
-          /* Pop from stack */
-          spi_emit_cache_update (current, atk_adaptor_app_data->bus);
-          tmp = current;
-          current = atk_object_get_parent (current);
-          g_object_unref (G_OBJECT (tmp));
-          g_queue_pop_head (stack);
-        }
+  while (!g_queue_is_empty (emit_update))
+    {
+      current = g_queue_pop_head (emit_update);
+      spi_emit_cache_update (current, atk_adaptor_app_data->bus);
+      g_object_unref (G_OBJECT (current));
     }
-    g_queue_free (stack);
+
+  g_queue_free (traversal);
+  g_queue_free (emit_update);
 }
 
 /*---------------------------------------------------------------------------*/
@@ -336,7 +315,7 @@ static void
 update_accessible (AtkObject *accessible)
 {
   guint  ref = 0;
-  g_assert(ATK_IS_OBJECT(accessible));
+  g_return_if_fail (ATK_IS_OBJECT(accessible));
 
   ref = object_to_ref (accessible);
   if (ref)
@@ -362,7 +341,7 @@ atk_dbus_path_to_object (const char *path)
   guint index;
   void *data;
 
-  g_assert (path);
+  g_return_val_if_fail (path, NULL);
 
   if (strncmp(path, SPI_ATK_OBJECT_PATH_PREFIX, SPI_ATK_PATH_PREFIX_LENGTH) != 0)
     return NULL;
@@ -406,6 +385,12 @@ atk_dbus_desktop_object_path ()
 
 /*---------------------------------------------------------------------------*/
 
+typedef gboolean (*TreeUpdateAction) (GSignalInvocationHint *signal_hint,
+                                      guint                  n_param_values,
+                                      const GValue          *param_values,
+                                      gpointer               data,
+                                      AtkObject             *accessible);
+
 /*
  * Events are not evaluated for non-registered accessibles.
  *
@@ -415,78 +400,100 @@ atk_dbus_desktop_object_path ()
  * When a parent is changed the subtree is de-registered
  * if the parent is not attached to the root accessible.
  */
+/* TODO Turn this function into a macro? */
 static gboolean
-tree_update_listener (GSignalInvocationHint *signal_hint,
-                      guint                  n_param_values,
-                      const GValue          *param_values,
-                      gpointer               data)
+tree_update_wrapper (GSignalInvocationHint *signal_hint,
+                     guint                  n_param_values,
+                     const GValue          *param_values,
+                     gpointer               data,
+                     TreeUpdateAction       action)
 {
   AtkObject *accessible;
-  AtkPropertyValues *values;
-  const gchar *pname = NULL;
 
   g_static_rec_mutex_lock (&registration_mutex);
 
   /* Ensure that only registered accessibles
    * have their signals processed.
    */
-  accessible = g_value_get_object (&param_values[0]);
-  g_assert (ATK_IS_OBJECT (accessible));
+  accessible = ATK_OBJECT(g_value_get_object (&param_values[0]));
+  g_return_val_if_fail (ATK_IS_OBJECT (accessible), TRUE);
 
   if (object_to_ref (accessible))
     {
 #ifdef SPI_ATK_DEBUG
       if (recursion_check_and_set ())
           g_warning ("AT-SPI: Recursive use of registration module");
+
+      g_debug ("AT-SPI: Tree update listener");
 #endif
+      action (signal_hint, n_param_values, param_values, data, accessible);
+
+      recursion_check_unset ();
+    }
+
+  g_static_rec_mutex_unlock (&registration_mutex);
+
+  return TRUE;
+}
+
+static gboolean
+tree_update_state_action (GSignalInvocationHint *signal_hint,
+                          guint                  n_param_values,
+                          const GValue          *param_values,
+                          gpointer               data,
+                          AtkObject             *accessible)
+{
+      update_accessible (accessible);
+}
+
+static gboolean
+tree_update_state_listener (GSignalInvocationHint *signal_hint,
+                            guint                  n_param_values,
+                            const GValue          *param_values,
+                            gpointer               data)
+{
+      tree_update_wrapper (signal_hint, n_param_values, param_values, data, tree_update_state_action);
+}
+
+static gboolean
+tree_update_property_action (GSignalInvocationHint *signal_hint,
+                             guint                  n_param_values,
+                             const GValue          *param_values,
+                             gpointer               data,
+                             AtkObject             *accessible)
+{
+      AtkPropertyValues *values;
+      const gchar *pname = NULL;
 
       values = (AtkPropertyValues*) g_value_get_pointer (&param_values[1]);
       pname = values[0].property_name;
       if (strcmp (pname, "accessible-name") == 0 ||
-          strcmp (pname, "accessible-description") == 0)
+          strcmp (pname, "accessible-description") == 0 ||
+          strcmp (pname, "accessible-parent") == 0)
         {
           update_accessible (accessible);
         }
       /* Parent value us updated by child-add signal of parent object */
-
-      recursion_check_unset ();
-    }
-
-  g_static_rec_mutex_unlock (&registration_mutex);
-
-  return TRUE;
 }
 
-/*
- * Events are not evaluated for non registered accessibles.
- *
- * When the children of a registered accessible are changed
- * the subtree, rooted at the child is registered.
- */
 static gboolean
-tree_update_children_listener (GSignalInvocationHint *signal_hint,
+tree_update_property_listener (GSignalInvocationHint *signal_hint,
                                guint                  n_param_values,
                                const GValue          *param_values,
                                gpointer               data)
 {
-  AtkObject *accessible;
-  const gchar *detail = NULL;
-  AtkObject *child;
-
-  g_static_rec_mutex_lock (&registration_mutex);
-
-  /* Ensure that only registered accessibles
-   * have their signals processed.
-   */
-  accessible = g_value_get_object (&param_values[0]);
-  g_assert (ATK_IS_OBJECT (accessible));
+      tree_update_wrapper (signal_hint, n_param_values, param_values, data, tree_update_property_action);
+}
 
-  if (object_to_ref (accessible))
-    {
-#ifdef SPI_ATK_DEBUG
-      if (recursion_check_and_set ())
-          g_warning ("AT-SPI: Recursive use of registration module");
-#endif
+static gboolean
+tree_update_children_action (GSignalInvocationHint *signal_hint,
+                             guint                  n_param_values,
+                             const GValue          *param_values,
+                             gpointer               data,
+                             AtkObject             *accessible)
+{
+      const gchar *detail = NULL;
+      AtkObject *child;
 
       if (signal_hint->detail)
           detail = g_quark_to_string (signal_hint->detail);
@@ -505,14 +512,76 @@ tree_update_children_listener (GSignalInvocationHint *signal_hint,
 #endif
             }
           register_subtree (child);
+          update_accessible (accessible);
+        }
+}
+
+static gboolean
+tree_update_children_listener (GSignalInvocationHint *signal_hint,
+                               guint                  n_param_values,
+                               const GValue          *param_values,
+                               gpointer               data)
+{
+      tree_update_wrapper (signal_hint, n_param_values, param_values, data, tree_update_children_action);
+}
+
+/*---------------------------------------------------------------------------*/
+
+static void
+spi_atk_register_toplevel_added (AtkObject *accessible,
+                                 guint     index,
+                                 AtkObject *child)
+{
+  g_static_rec_mutex_lock (&registration_mutex);
+
+  g_return_if_fail (ATK_IS_OBJECT (accessible));
+
+  if (object_to_ref (accessible))
+    {
+#ifdef SPI_ATK_DEBUG
+      if (recursion_check_and_set ())
+          g_warning ("AT-SPI: Recursive use of registration module");
+
+      g_debug ("AT-SPI: Toplevel added listener");
+#endif
+      if (!ATK_IS_OBJECT (child))
+        {
+          child = atk_object_ref_accessible_child (accessible, index);
+#ifdef SPI_ATK_DEBUG
+          non_owned_accessible (child);
+#endif
         }
+      register_subtree (child);
+      update_accessible (accessible);
 
       recursion_check_unset ();
     }
 
   g_static_rec_mutex_unlock (&registration_mutex);
+}
 
-  return TRUE;
+static void
+spi_atk_register_toplevel_removed (AtkObject *accessible,
+                                   guint     index,
+                                   AtkObject *child)
+{
+  g_static_rec_mutex_lock (&registration_mutex);
+
+  g_return_if_fail (ATK_IS_OBJECT (accessible));
+
+  if (object_to_ref (accessible))
+    {
+#ifdef SPI_ATK_DEBUG
+      if (recursion_check_and_set ())
+          g_warning ("AT-SPI: Recursive use of registration module");
+
+      g_debug ("AT-SPI: Toplevel removed listener");
+#endif
+      update_accessible (accessible);
+      recursion_check_unset ();
+    }
+
+  g_static_rec_mutex_unlock (&registration_mutex);
 }
 
 /*
@@ -530,13 +599,24 @@ atk_dbus_initialize (AtkObject *root)
 #ifdef SPI_ATK_DEBUG
   if (g_thread_supported ())
       g_message ("AT-SPI: Threads enabled");
+
+  g_debug ("AT-SPI: Initial Atk tree regisration");
 #endif
 
   register_subtree (root);
 
-  atk_add_global_event_listener (tree_update_listener, "Gtk:AtkObject:property-change");
+  atk_add_global_event_listener (tree_update_property_listener, "Gtk:AtkObject:property-change");
   atk_add_global_event_listener (tree_update_children_listener, "Gtk:AtkObject:children-changed");
+  atk_add_global_event_listener (tree_update_state_listener, "Gtk:AtkObject:state-change");
+
+  g_signal_connect (root,
+                    "children-changed::add",
+                    (GCallback) spi_atk_register_toplevel_added,
+                    NULL);
+  g_signal_connect (root,
+                    "children-changed::remove",
+                    (GCallback) spi_atk_register_toplevel_removed,
+                    NULL);
 }
 
 /*END------------------------------------------------------------------------*/
-