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))
270 g_object_unref (state);
275 * Called to register an AtkObject with AT-SPI and expose it over D-Bus.
278 register_accessible (AtkObject *accessible, AtkObject *container)
280 g_return_if_fail (ATK_IS_OBJECT(accessible));
282 register_gobject (G_OBJECT (accessible), G_OBJECT (container));
286 register_hyperlink (AtkHyperlink *hyperlink, AtkObject *container)
289 g_return_if_fail (ATK_IS_HYPERLINK (hyperlink));
290 g_return_if_fail (container);
292 ref = assign_reference();
294 g_hash_table_insert (ref2ptr, GINT_TO_POINTER(ref), hyperlink);
295 g_object_set_data (G_OBJECT(hyperlink), "dbus-id", GINT_TO_POINTER(ref));
296 g_object_ref (G_OBJECT (hyperlink));
298 GHashTable *subrefs = (GHashTable *) g_object_get_data (G_OBJECT (container), "dbus-subrefs-hyperlink");
301 subrefs = g_hash_table_new(g_direct_hash, g_direct_equal);
302 g_object_set_data (G_OBJECT (container), "dbus-subrefs-hyperlink", GINT_TO_POINTER(ref));
304 g_hash_table_insert (subrefs, GINT_TO_POINTER(ref), hyperlink);
307 /*---------------------------------------------------------------------------*/
311 * This function checks that the ref-count of an accessible
314 * There is not currently any remote reference counting
315 * in AT-SPI D-Bus so objects that are remotely owned are not
318 * TODO Add debug wrapper
321 non_owned_accessible (AtkObject *accessible)
323 if ((G_OBJECT (accessible))->ref_count <= 1)
325 g_warning ("AT-SPI: Child referenced that is not owned by its parent");
333 #endif /* SPI_ATK_DEBUG */
335 /*---------------------------------------------------------------------------*/
337 /* TRUE if we should not keep this object / tell the AT about it
338 * Currently true if TRANSIENT and not SHOWING
341 object_is_moot (AtkObject *accessible)
344 gboolean result = FALSE;
346 /* This is dangerous, refing the state set
347 * seems to do wierd things to the tree & cause recursion
348 * by modifying the tree alot.
350 state = atk_object_ref_state_set (accessible);
351 if ( atk_state_set_contains_state (state, ATK_STATE_TRANSIENT) &&
352 !atk_state_set_contains_state (state, ATK_STATE_SHOWING))
356 g_object_unref (state);
362 append_children (AtkObject *accessible, GQueue *traversal)
366 gint count = atk_object_get_n_accessible_children (accessible);
368 if (count < 0) count = 0;
369 for (i =0; i < count; i++)
371 current = atk_object_ref_accessible_child (accessible, i);
375 non_owned_accessible (current);
377 g_queue_push_tail (traversal, current);
383 * Registers a subtree of accessible objects
384 * rooted at the accessible object provided.
386 * The leaf nodes do not have their children
387 * registered. A node is considered a leaf
388 * if it has the state "manages-descendants"
389 * or if it has already been registered.
392 register_subtree (AtkObject *accessible)
398 g_return_if_fail (ATK_IS_OBJECT (accessible));
400 traversal = g_queue_new ();
401 emit_update = g_queue_new ();
403 g_object_ref (accessible);
404 g_queue_push_tail (traversal, accessible);
406 while (!g_queue_is_empty (traversal))
408 current = g_queue_pop_head (traversal);
409 g_queue_push_tail (emit_update, current);
410 if (!object_to_ref (current))
412 register_accessible (current, NULL);
414 g_debug ("REG - %s - %d - %s", atk_object_get_name (current),
415 atk_object_get_role (current),
416 atk_dbus_object_to_path (current));
418 append_children (current, traversal);
422 while (!g_queue_is_empty (emit_update))
424 current = g_queue_pop_head (emit_update);
425 spi_emit_cache_update (current, atk_adaptor_app_data->bus);
426 g_object_unref (G_OBJECT (current));
429 g_queue_free (traversal);
430 g_queue_free (emit_update);
433 /*---------------------------------------------------------------------------*/
436 * Called when an already registered object is updated in such a
437 * way that client side cache needs to be updated.
440 update_accessible (AtkObject *accessible)
443 g_return_if_fail (ATK_IS_OBJECT(accessible));
445 ref = object_to_ref (accessible);
448 spi_emit_cache_update (accessible, atk_adaptor_app_data->bus);
452 /*---------------------------------------------------------------------------*/
455 atk_dbus_foreach_registered(GHFunc func, gpointer data)
457 g_hash_table_foreach(ref2ptr, func, data);
461 * Used to lookup an AtkObject from its D-Bus path.
464 atk_dbus_path_to_gobject (const char *path)
469 g_return_val_if_fail (path, NULL);
471 if (strncmp(path, SPI_ATK_OBJECT_PATH_PREFIX, SPI_ATK_PATH_PREFIX_LENGTH) != 0)
474 path += SPI_ATK_PATH_PREFIX_LENGTH; /* Skip over the prefix */
476 if (!g_strcmp0 (SPI_ATK_OBJECT_PATH_DESKTOP, path))
477 return G_OBJECT (atk_get_root());
483 data = g_hash_table_lookup (ref2ptr, GINT_TO_POINTER(index));
486 GObject *gobj = G_OBJECT (data);
487 g_object_set_data (gobj, "last-ref-time", (gpointer) time (NULL));
495 atk_dbus_path_to_object (const char *path)
497 return ATK_OBJECT (atk_dbus_path_to_gobject (path));
501 * TODO WARNING HACK This function is dangerous.
502 * It should only be called before sending an event on an
503 * object that has not already been registered.
506 atk_dbus_object_attempt_registration (AtkObject *accessible)
510 ref = object_to_ref (accessible);
513 /* See if the object is attached to the main tree */
514 AtkObject *current, *prev = NULL;
517 /* This should iterate until it hits a NULL or registered parent */
519 current = atk_object_get_parent (accessible);
521 cref = object_to_ref (current);
522 while (current && !cref)
525 current = atk_object_get_parent (current);
527 cref = object_to_ref (current);
530 /* A registered parent, with non-registered child, has been found */
533 register_subtree (prev);
536 /* The object SHOULD be registered now. If it isn't - I give up */
537 ref = object_to_ref (accessible);
540 return atk_dbus_ref_to_path (ref);
545 g_debug ("AT-SPI: Could not register a non-attached accessible object");
552 return atk_dbus_ref_to_path (ref);
558 * Used to lookup a D-Bus path from the AtkObject.
561 atk_dbus_gobject_to_path_internal (GObject *gobj, gboolean do_register, GObject *container)
565 ref = gobject_to_ref (gobj);
566 if (!ref && do_register)
568 register_gobject (gobj, container);
569 ref = gobject_to_ref (gobj);
575 return atk_dbus_ref_to_path (ref);
579 atk_dbus_object_to_path (AtkObject *accessible, gboolean do_register)
581 AtkObject *container = (accessible && do_register? atk_object_get_parent (accessible): NULL);
582 return atk_dbus_gobject_to_path_internal (G_OBJECT (accessible), do_register, G_OBJECT (container));
586 atk_dbus_sub_object_to_path (GObject *gobj, GObject *container)
588 return atk_dbus_gobject_to_path_internal (gobj, TRUE, container);
592 atk_dbus_hyperlink_to_path (AtkHyperlink *hyperlink, AtkObject *container)
596 ref = gobject_to_ref (G_OBJECT (hyperlink));
597 if (!ref && container)
599 register_hyperlink (hyperlink, container);
600 ref = hyperlink_to_ref (hyperlink);
606 return atk_dbus_ref_to_path (ref);
610 atk_dbus_desktop_object_path ()
612 return g_strdup (SPI_ATK_OBJECT_PATH_PREFIX SPI_ATK_OBJECT_PATH_DESKTOP);
615 /*---------------------------------------------------------------------------*/
617 typedef gboolean (*TreeUpdateAction) (GSignalInvocationHint *signal_hint,
618 guint n_param_values,
619 const GValue *param_values,
621 AtkObject *accessible);
624 * Events are not evaluated for non-registered accessibles.
626 * When a property change is made on a registered accessible
627 * the client side cache should be updated.
629 * When a parent is changed the subtree is de-registered
630 * if the parent is not attached to the root accessible.
632 /* TODO Turn this function into a macro? */
634 tree_update_wrapper (GSignalInvocationHint *signal_hint,
635 guint n_param_values,
636 const GValue *param_values,
638 TreeUpdateAction action)
640 AtkObject *accessible;
642 g_static_rec_mutex_lock (®istration_mutex);
644 /* Ensure that only registered accessibles
645 * have their signals processed.
647 accessible = ATK_OBJECT(g_value_get_object (¶m_values[0]));
648 g_return_val_if_fail (ATK_IS_OBJECT (accessible), TRUE);
650 if (object_to_ref (accessible))
653 if (recursion_check_and_set ())
654 g_warning ("AT-SPI: Recursive use of registration module");
656 g_debug ("AT-SPI: Tree update listener");
658 action (signal_hint, n_param_values, param_values, data, accessible);
660 recursion_check_unset ();
663 g_static_rec_mutex_unlock (®istration_mutex);
669 maybe_expire_lease (gpointer key, gpointer obj_data, gpointer iter)
671 time_t secs = time (NULL) - (time_t)obj_data;
675 deregister_sub_accessible (key, obj_data, iter);
680 expire_old_leases_in (gpointer key, gpointer obj_data, gpointer iter)
682 g_hash_table_foreach_remove ((GHashTable *)obj_data, maybe_expire_lease, NULL);
688 g_hash_table_foreach (objects_with_subrefs, expire_old_leases_in, NULL);
692 tree_update_state_action (GSignalInvocationHint *signal_hint,
693 guint n_param_values,
694 const GValue *param_values,
696 AtkObject *accessible)
701 if (n_param_values < 3)
703 g_warning ("at-spi: Not enough params in state-changed signal");
707 name = g_value_get_string (param_values + 1);
708 state = g_value_get_boolean (param_values + 2);
709 if (!strcmp (name, "visible"))
711 AtkStateSet *set = atk_object_ref_state_set (accessible);
712 if (atk_state_set_contains_state (set, ATK_STATE_TRANSIENT))
716 g_hash_table_insert (leased_refs, accessible, (gpointer) time (NULL));
718 /* todo: Set to a high number: 5 for dbg. */
719 if (leased_refs_count > 5)
720 expire_old_leases ();
724 g_hash_table_remove (leased_refs, accessible);
728 g_object_unref (set);
731 update_accessible (accessible);
736 tree_update_state_listener (GSignalInvocationHint *signal_hint,
737 guint n_param_values,
738 const GValue *param_values,
741 tree_update_wrapper (signal_hint, n_param_values, param_values, data, tree_update_state_action);
746 tree_update_property_action (GSignalInvocationHint *signal_hint,
747 guint n_param_values,
748 const GValue *param_values,
750 AtkObject *accessible)
752 AtkPropertyValues *values;
753 const gchar *pname = NULL;
755 values = (AtkPropertyValues*) g_value_get_pointer (¶m_values[1]);
756 pname = values[0].property_name;
757 if (strcmp (pname, "accessible-name") == 0 ||
758 strcmp (pname, "accessible-description") == 0 ||
759 strcmp (pname, "accessible-parent") == 0 ||
760 strcmp (pname, "accessible-role") == 0)
762 update_accessible (accessible);
764 /* Parent value us updated by child-add signal of parent object */
769 tree_update_property_listener (GSignalInvocationHint *signal_hint,
770 guint n_param_values,
771 const GValue *param_values,
774 tree_update_wrapper (signal_hint, n_param_values, param_values, data, tree_update_property_action);
779 tree_update_children_action (GSignalInvocationHint *signal_hint,
780 guint n_param_values,
781 const GValue *param_values,
783 AtkObject *accessible)
785 const gchar *detail = NULL;
788 if (signal_hint->detail)
789 detail = g_quark_to_string (signal_hint->detail);
791 if (!strcmp (detail, "add"))
794 int index = g_value_get_uint (param_values + 1);
795 child = g_value_get_pointer (param_values + 2);
797 if (!ATK_IS_OBJECT (child))
799 child = atk_object_ref_accessible_child (accessible, index);
801 non_owned_accessible (child);
804 register_subtree (child);
805 update_accessible (accessible);
807 else if (!strcmp (detail, "remove"))
809 update_accessible (accessible);
815 tree_update_children_listener (GSignalInvocationHint *signal_hint,
816 guint n_param_values,
817 const GValue *param_values,
820 tree_update_wrapper (signal_hint, n_param_values, param_values, data, tree_update_children_action);
824 /*---------------------------------------------------------------------------*/
827 spi_atk_register_toplevel_added (AtkObject *accessible,
831 g_static_rec_mutex_lock (®istration_mutex);
833 g_return_if_fail (ATK_IS_OBJECT (accessible));
835 if (object_to_ref (accessible))
838 if (recursion_check_and_set ())
839 g_warning ("AT-SPI: Recursive use of registration module");
841 g_debug ("AT-SPI: Toplevel added listener");
843 if (!ATK_IS_OBJECT (child))
845 child = atk_object_ref_accessible_child (accessible, index);
847 non_owned_accessible (child);
850 register_subtree (child);
851 update_accessible (accessible);
853 recursion_check_unset ();
856 g_static_rec_mutex_unlock (®istration_mutex);
860 spi_atk_register_toplevel_removed (AtkObject *accessible,
864 g_static_rec_mutex_lock (®istration_mutex);
866 g_return_if_fail (ATK_IS_OBJECT (accessible));
868 if (object_to_ref (accessible))
871 if (recursion_check_and_set ())
872 g_warning ("AT-SPI: Recursive use of registration module");
874 g_debug ("AT-SPI: Toplevel removed listener");
876 update_accessible (accessible);
877 recursion_check_unset ();
880 g_static_rec_mutex_unlock (®istration_mutex);
884 * Initializes required global data. The update and removal lists
885 * and the reference lookup tables.
887 * Initializes all of the required D-Bus interfaces.
890 atk_dbus_initialize (AtkObject *root)
893 ref2ptr = g_hash_table_new(g_direct_hash, g_direct_equal);
895 if (!objects_with_subrefs)
896 objects_with_subrefs = g_hash_table_new(g_direct_hash, g_direct_equal);
899 leased_refs = g_hash_table_new(g_direct_hash, g_direct_equal);
902 if (g_thread_supported ())
903 g_message ("AT-SPI: Threads enabled");
905 g_debug ("AT-SPI: Initial Atk tree regisration");
908 register_subtree (root);
910 atk_add_global_event_listener (tree_update_property_listener, "Gtk:AtkObject:property-change");
911 atk_add_global_event_listener (tree_update_children_listener, "Gtk:AtkObject:children-changed");
912 atk_add_global_event_listener (tree_update_state_listener, "Gtk:AtkObject:state-change");
914 g_signal_connect (root,
915 "children-changed::add",
916 (GCallback) spi_atk_register_toplevel_added,
918 g_signal_connect (root,
919 "children-changed::remove",
920 (GCallback) spi_atk_register_toplevel_removed,
924 /*END------------------------------------------------------------------------*/