2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2008 Novell, Inc.
6 * Copyright 2008, 2009 Codethink Ltd.
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.
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.
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.
29 #include "tree-adaptor.h"
30 #include "accessible-register.h"
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.
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'.
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.
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.
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.
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:
68 * Removing infinite spaces (Child access no longer references child).
69 * Removing lazy creation of accessible objects.
72 #define SPI_ATK_OBJECT_PATH_PREFIX "/org/freedesktop/atspi/accessible"
73 #define SPI_ATK_OBJECT_PATH_DESKTOP "/desktop"
75 #define SPI_ATK_PATH_PREFIX_LENGTH 33
76 #define SPI_ATK_OBJECT_REFERENCE_TEMPLATE SPI_ATK_OBJECT_PATH_PREFIX "/%d"
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;
84 static guint reference_counter = 0;
86 static GStaticRecMutex registration_mutex = G_STATIC_REC_MUTEX_INIT;
88 /*---------------------------------------------------------------------------*/
90 static GStaticMutex recursion_check_guard = G_STATIC_MUTEX_INIT;
91 static gboolean recursion_check = FALSE;
92 static int last_gc_time;
94 static void deregister_sub_accessible (gpointer key, gpointer obj_data, gpointer iter);
96 static void deregister_sub_hyperlink (gpointer key, gpointer obj_data, gpointer iter);
99 recursion_check_and_set ()
102 g_static_mutex_lock (&recursion_check_guard);
103 ret = recursion_check;
104 recursion_check = TRUE;
105 g_static_mutex_unlock (&recursion_check_guard);
110 recursion_check_unset ()
112 g_static_mutex_lock (&recursion_check_guard);
113 recursion_check = FALSE;
114 g_static_mutex_unlock (&recursion_check_guard);
117 /*---------------------------------------------------------------------------*/
120 * Each AtkObject must be asssigned a D-Bus path (Reference)
122 * This function provides an integer reference for a new
126 assign_reference(void)
129 /* Reference of 0 not allowed as used as direct key in hash table */
130 if (reference_counter == 0)
132 /* TODO: If we've wrapped, ensure that two objects don't have the same ref */
133 return reference_counter;
137 * Returns the reference of the object, or 0 if it is not registered.
140 gobject_to_ref (GObject *gobj)
142 return GPOINTER_TO_INT(g_object_get_data (gobj, "dbus-id"));
146 object_to_ref (AtkObject *accessible)
148 return gobject_to_ref (G_OBJECT (accessible));
152 hyperlink_to_ref (AtkHyperlink *link)
154 return gobject_to_ref (G_OBJECT (link));
158 * Converts the Accessible object reference to its D-Bus object path
161 atk_dbus_ref_to_path (guint ref)
163 return g_strdup_printf(SPI_ATK_OBJECT_REFERENCE_TEMPLATE, ref);
166 /*---------------------------------------------------------------------------*/
169 * Callback for when a registered AtkObject is destroyed.
171 * Removes the AtkObject from the reference lookup tables, meaning
172 * it is no longer exposed over D-Bus.
175 deregister_object (gpointer data, GObject *gobj)
178 GHashTable *subrefs_atk;
179 GHashTable *subrefs_hyperlink;
180 g_return_if_fail (ATK_IS_OBJECT (gobj) || ATK_IS_HYPERLINK (gobj));
182 subrefs_atk = (GHashTable *) g_object_get_data (gobj, "dbus-subrefs-atk");
183 subrefs_hyperlink = (GHashTable *) g_object_get_data (gobj, "dbus-subrefs-hyperlink");
187 g_hash_table_foreach (subrefs_atk, deregister_sub_accessible, data);
188 g_hash_table_unref (subrefs_atk);
191 if (subrefs_hyperlink)
193 g_hash_table_foreach (subrefs_hyperlink, deregister_sub_hyperlink, data);
194 g_hash_table_unref (subrefs_hyperlink);
197 if (subrefs_atk || subrefs_hyperlink)
198 g_hash_table_remove (objects_with_subrefs, gobj);
200 if (ATK_IS_OBJECT (gobj))
202 ref = object_to_ref (ATK_OBJECT (gobj));
205 spi_emit_cache_removal (ref, atk_adaptor_app_data->bus);
206 g_hash_table_remove(ref2ptr, GINT_TO_POINTER(ref));
212 deregister_sub_accessible (gpointer key, gpointer obj_data, gpointer iter)
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);
221 deregister_sub_hyperlink (gpointer key, gpointer obj_data, gpointer iter)
224 GObject *ghyperlink = G_OBJECT (obj_data);
226 g_return_if_fail (ATK_IS_HYPERLINK (ghyperlink));
228 ref = gobject_to_ref (ghyperlink);
231 g_hash_table_remove(ref2ptr, GINT_TO_POINTER(ref));
233 g_object_unref (ghyperlink);
237 register_gobject (GObject *gobj, GObject *container)
240 g_return_if_fail (G_IS_OBJECT(gobj));
242 ref = assign_reference();
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);
250 GHashTable *subrefs = (GHashTable *) g_object_get_data (G_OBJECT (container), "dbus-subrefs-atk");
253 subrefs = g_hash_table_new(g_direct_hash, g_direct_equal);
254 g_object_set_data (G_OBJECT (container), "dbus-subrefs-atk", subrefs);
256 g_hash_table_insert (subrefs, GINT_TO_POINTER(ref), gobj);
257 g_hash_table_insert (objects_with_subrefs, gobj, subrefs);
260 if (ATK_IS_HYPERLINK (gobj))
262 else if (ATK_IS_OBJECT (gobj))
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))
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);
272 g_object_unref (state);
277 * Called to register an AtkObject with AT-SPI and expose it over D-Bus.
280 register_accessible (AtkObject *accessible, AtkObject *container)
282 g_return_if_fail (ATK_IS_OBJECT(accessible));
284 register_gobject (G_OBJECT (accessible), G_OBJECT (container));
288 register_hyperlink (AtkHyperlink *hyperlink, AtkObject *container)
291 g_return_if_fail (ATK_IS_HYPERLINK (hyperlink));
292 g_return_if_fail (container);
294 ref = assign_reference();
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));
300 GHashTable *subrefs = (GHashTable *) g_object_get_data (G_OBJECT (container), "dbus-subrefs-hyperlink");
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));
306 g_hash_table_insert (subrefs, GINT_TO_POINTER(ref), hyperlink);
309 /*---------------------------------------------------------------------------*/
313 * This function checks that the ref-count of an accessible
316 * There is not currently any remote reference counting
317 * in AT-SPI D-Bus so objects that are remotely owned are not
320 * TODO Add debug wrapper
323 non_owned_accessible (AtkObject *accessible)
325 if ((G_OBJECT (accessible))->ref_count <= 1)
327 g_warning ("AT-SPI: Child referenced that is not owned by its parent");
335 #endif /* SPI_ATK_DEBUG */
337 /*---------------------------------------------------------------------------*/
339 /* TRUE if we should not keep this object / tell the AT about it
340 * Currently true if TRANSIENT and not SHOWING
343 object_is_moot (AtkObject *accessible)
346 gboolean result = FALSE;
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.
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))
358 g_object_unref (state);
364 append_children (AtkObject *accessible, GQueue *traversal)
368 gint count = atk_object_get_n_accessible_children (accessible);
370 if (count < 0) count = 0;
371 for (i =0; i < count; i++)
373 current = atk_object_ref_accessible_child (accessible, i);
377 non_owned_accessible (current);
379 g_queue_push_tail (traversal, current);
385 * Registers a subtree of accessible objects
386 * rooted at the accessible object provided.
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.
394 register_subtree (AtkObject *accessible)
400 g_return_if_fail (ATK_IS_OBJECT (accessible));
402 traversal = g_queue_new ();
403 emit_update = g_queue_new ();
405 g_object_ref (accessible);
406 g_queue_push_tail (traversal, accessible);
408 while (!g_queue_is_empty (traversal))
410 current = g_queue_pop_head (traversal);
411 g_queue_push_tail (emit_update, current);
412 if (!object_to_ref (current))
414 register_accessible (current, NULL);
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));
420 append_children (current, traversal);
424 while (!g_queue_is_empty (emit_update))
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));
431 g_queue_free (traversal);
432 g_queue_free (emit_update);
435 /*---------------------------------------------------------------------------*/
438 * Called when an already registered object is updated in such a
439 * way that client side cache needs to be updated.
442 update_accessible (AtkObject *accessible)
445 g_return_if_fail (ATK_IS_OBJECT(accessible));
447 ref = object_to_ref (accessible);
450 spi_emit_cache_update (accessible, atk_adaptor_app_data->bus);
454 /*---------------------------------------------------------------------------*/
457 atk_dbus_foreach_registered(GHFunc func, gpointer data)
459 g_hash_table_foreach(ref2ptr, func, data);
463 * Used to lookup an AtkObject from its D-Bus path.
466 atk_dbus_path_to_gobject (const char *path)
471 g_return_val_if_fail (path, NULL);
473 if (strncmp(path, SPI_ATK_OBJECT_PATH_PREFIX, SPI_ATK_PATH_PREFIX_LENGTH) != 0)
476 path += SPI_ATK_PATH_PREFIX_LENGTH; /* Skip over the prefix */
478 if (!g_strcmp0 (SPI_ATK_OBJECT_PATH_DESKTOP, path))
479 return G_OBJECT (atk_get_root());
485 data = g_hash_table_lookup (ref2ptr, GINT_TO_POINTER(index));
488 GObject *gobj = G_OBJECT (data);
489 g_object_set_data (gobj, "last-ref-time", (gpointer) time (NULL));
497 atk_dbus_path_to_object (const char *path)
499 return ATK_OBJECT (atk_dbus_path_to_gobject (path));
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.
508 atk_dbus_object_attempt_registration (AtkObject *accessible)
512 ref = object_to_ref (accessible);
515 /* See if the object is attached to the main tree */
516 AtkObject *current, *prev = NULL;
519 /* This should iterate until it hits a NULL or registered parent */
521 current = atk_object_get_parent (accessible);
523 cref = object_to_ref (current);
524 while (current && !cref)
527 current = atk_object_get_parent (current);
529 cref = object_to_ref (current);
532 /* A registered parent, with non-registered child, has been found */
535 register_subtree (prev);
538 /* The object SHOULD be registered now. If it isn't - I give up */
539 ref = object_to_ref (accessible);
542 return atk_dbus_ref_to_path (ref);
547 g_debug ("AT-SPI: Could not register a non-attached accessible object");
554 return atk_dbus_ref_to_path (ref);
560 * Used to lookup a D-Bus path from the AtkObject.
563 atk_dbus_gobject_to_path_internal (GObject *gobj, gboolean do_register, GObject *container)
567 ref = gobject_to_ref (gobj);
568 if (!ref && do_register)
570 register_gobject (gobj, container);
571 ref = gobject_to_ref (gobj);
577 return atk_dbus_ref_to_path (ref);
581 atk_dbus_object_to_path (AtkObject *accessible, gboolean do_register)
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));
588 atk_dbus_sub_object_to_path (GObject *gobj, GObject *container)
590 return atk_dbus_gobject_to_path_internal (gobj, TRUE, container);
594 atk_dbus_hyperlink_to_path (AtkHyperlink *hyperlink, AtkObject *container)
598 ref = gobject_to_ref (G_OBJECT (hyperlink));
599 if (!ref && container)
601 register_hyperlink (hyperlink, container);
602 ref = hyperlink_to_ref (hyperlink);
608 return atk_dbus_ref_to_path (ref);
612 atk_dbus_desktop_object_path ()
614 return g_strdup (SPI_ATK_OBJECT_PATH_PREFIX SPI_ATK_OBJECT_PATH_DESKTOP);
617 /*---------------------------------------------------------------------------*/
619 typedef gboolean (*TreeUpdateAction) (GSignalInvocationHint *signal_hint,
620 guint n_param_values,
621 const GValue *param_values,
623 AtkObject *accessible);
626 * Events are not evaluated for non-registered accessibles.
628 * When a property change is made on a registered accessible
629 * the client side cache should be updated.
631 * When a parent is changed the subtree is de-registered
632 * if the parent is not attached to the root accessible.
634 /* TODO Turn this function into a macro? */
636 tree_update_wrapper (GSignalInvocationHint *signal_hint,
637 guint n_param_values,
638 const GValue *param_values,
640 TreeUpdateAction action)
642 AtkObject *accessible;
644 g_static_rec_mutex_lock (®istration_mutex);
646 /* Ensure that only registered accessibles
647 * have their signals processed.
649 accessible = ATK_OBJECT(g_value_get_object (¶m_values[0]));
650 g_return_val_if_fail (ATK_IS_OBJECT (accessible), TRUE);
652 if (object_to_ref (accessible))
655 if (recursion_check_and_set ())
656 g_warning ("AT-SPI: Recursive use of registration module");
658 g_debug ("AT-SPI: Tree update listener");
660 action (signal_hint, n_param_values, param_values, data, accessible);
662 recursion_check_unset ();
665 g_static_rec_mutex_unlock (®istration_mutex);
671 maybe_expire_lease (gpointer key, gpointer obj_data, gpointer iter)
673 time_t secs = time (NULL) - (time_t)obj_data;
677 deregister_sub_accessible (key, obj_data, iter);
682 expire_old_leases_in (gpointer key, gpointer obj_data, gpointer iter)
684 g_hash_table_foreach_remove ((GHashTable *)obj_data, maybe_expire_lease, NULL);
690 g_hash_table_foreach (objects_with_subrefs, expire_old_leases_in, NULL);
694 tree_update_state_action (GSignalInvocationHint *signal_hint,
695 guint n_param_values,
696 const GValue *param_values,
698 AtkObject *accessible)
703 if (n_param_values < 3)
705 g_warning ("at-spi: Not enough params in state-changed signal");
709 name = g_value_get_string (param_values + 1);
710 state = g_value_get_boolean (param_values + 2);
711 if (!strcmp (name, "visible"))
713 AtkStateSet *set = atk_object_ref_state_set (accessible);
714 if (atk_state_set_contains_state (set, ATK_STATE_TRANSIENT))
718 g_hash_table_insert (leased_refs, accessible, (gpointer) time (NULL));
720 /* todo: Set to a high number: 5 for dbg. */
721 if (leased_refs_count > 5)
722 expire_old_leases ();
726 g_hash_table_remove (leased_refs, accessible);
730 g_object_unref (set);
733 update_accessible (accessible);
738 tree_update_state_listener (GSignalInvocationHint *signal_hint,
739 guint n_param_values,
740 const GValue *param_values,
743 tree_update_wrapper (signal_hint, n_param_values, param_values, data, tree_update_state_action);
748 tree_update_property_action (GSignalInvocationHint *signal_hint,
749 guint n_param_values,
750 const GValue *param_values,
752 AtkObject *accessible)
754 AtkPropertyValues *values;
755 const gchar *pname = NULL;
757 values = (AtkPropertyValues*) g_value_get_pointer (¶m_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)
764 update_accessible (accessible);
766 /* Parent value us updated by child-add signal of parent object */
771 tree_update_property_listener (GSignalInvocationHint *signal_hint,
772 guint n_param_values,
773 const GValue *param_values,
776 tree_update_wrapper (signal_hint, n_param_values, param_values, data, tree_update_property_action);
781 tree_update_children_action (GSignalInvocationHint *signal_hint,
782 guint n_param_values,
783 const GValue *param_values,
785 AtkObject *accessible)
787 const gchar *detail = NULL;
790 if (signal_hint->detail)
791 detail = g_quark_to_string (signal_hint->detail);
793 if (!strcmp (detail, "add"))
796 int index = g_value_get_uint (param_values + 1);
797 child = g_value_get_pointer (param_values + 2);
799 if (!ATK_IS_OBJECT (child))
801 child = atk_object_ref_accessible_child (accessible, index);
803 non_owned_accessible (child);
806 register_subtree (child);
807 update_accessible (accessible);
809 else if (!strcmp (detail, "remove"))
811 update_accessible (accessible);
817 tree_update_children_listener (GSignalInvocationHint *signal_hint,
818 guint n_param_values,
819 const GValue *param_values,
822 tree_update_wrapper (signal_hint, n_param_values, param_values, data, tree_update_children_action);
826 /*---------------------------------------------------------------------------*/
829 spi_atk_register_toplevel_added (AtkObject *accessible,
833 g_static_rec_mutex_lock (®istration_mutex);
835 g_return_if_fail (ATK_IS_OBJECT (accessible));
837 if (object_to_ref (accessible))
840 if (recursion_check_and_set ())
841 g_warning ("AT-SPI: Recursive use of registration module");
843 g_debug ("AT-SPI: Toplevel added listener");
845 if (!ATK_IS_OBJECT (child))
847 child = atk_object_ref_accessible_child (accessible, index);
849 non_owned_accessible (child);
852 register_subtree (child);
853 update_accessible (accessible);
855 recursion_check_unset ();
858 g_static_rec_mutex_unlock (®istration_mutex);
862 spi_atk_register_toplevel_removed (AtkObject *accessible,
866 g_static_rec_mutex_lock (®istration_mutex);
868 g_return_if_fail (ATK_IS_OBJECT (accessible));
870 if (object_to_ref (accessible))
873 if (recursion_check_and_set ())
874 g_warning ("AT-SPI: Recursive use of registration module");
876 g_debug ("AT-SPI: Toplevel removed listener");
878 update_accessible (accessible);
879 recursion_check_unset ();
882 g_static_rec_mutex_unlock (®istration_mutex);
886 * Initializes required global data. The update and removal lists
887 * and the reference lookup tables.
889 * Initializes all of the required D-Bus interfaces.
892 atk_dbus_initialize (AtkObject *root)
895 ref2ptr = g_hash_table_new(g_direct_hash, g_direct_equal);
897 if (!objects_with_subrefs)
898 objects_with_subrefs = g_hash_table_new(g_direct_hash, g_direct_equal);
901 leased_refs = g_hash_table_new(g_direct_hash, g_direct_equal);
904 if (g_thread_supported ())
905 g_message ("AT-SPI: Threads enabled");
907 g_debug ("AT-SPI: Initial Atk tree regisration");
910 register_subtree (root);
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");
916 g_signal_connect (root,
917 "children-changed::add",
918 (GCallback) spi_atk_register_toplevel_added,
920 g_signal_connect (root,
921 "children-changed::remove",
922 (GCallback) spi_atk_register_toplevel_removed,
926 /*END------------------------------------------------------------------------*/