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 "accessible-register.h"
31 #define ATK_BRIDGE_OBJECT_PATH_PREFIX "/org/freedesktop/atspi/accessible"
32 #define ATK_BRIDGE_OBJECT_REFERENCE_TEMPLATE ATK_BRIDGE_OBJECT_PATH_PREFIX "/%d"
33 #define ATK_BRIDGE_PATH_PREFIX_LENGTH 33
36 * This module is responsible for keeping track of all the AtkObjects in
37 * the application, so that they can be accessed remotely and placed in
38 * a client side cache.
40 * To access an AtkObject remotely we need to provide a D-Bus object
41 * path for it. The D-Bus object paths used have a standard prefix
42 * (ATK_BRIDGE_OBJECT_PATH_PREFIX). Appended to this prefix is a string
43 * representation of an integer reference. So to access an AtkObject
44 * remotely we keep a Hashtable that maps the given reference to
45 * the AtkObject pointer. An object in this hash table is said to be 'registered'.
47 * The architecture of AT-SPI dbus is such that AtkObjects are not
48 * remotely reference counted. This means that we need to keep track of
49 * object destruction. When an object is destroyed it must be 'deregistered'
50 * To do this lookup we keep a dbus-id attribute on each AtkObject.
52 * In addition to accessing the objects remotely we must be able to update
53 * the client side cache. This is done using the 'update' signal of the
54 * org.freedesktop.atspi.Tree interface. The update signal should send out
55 * all of the cacheable data for an Accessible object.
62 * While traversing the ATK tree we may modify it unintentionally.
63 * This is either a bug in the Gail implementation or this module.
64 * If a change is caused that recurses, via a signal into this module
67 * Things could also be changed that do not cause signal emission,
68 * but do cause a failure. Not sure what these would be.
70 * The other option is that there are threads that modify the GUI.
71 * This IS A BUG IN THE PROGRAM. But it may happen. If seeing very
72 * odd bugs change this to take the GDK lock. Just to make sure.
75 static GHashTable *ref2ptr = NULL; /* Used for converting a D-Bus path (Reference) to the object pointer */
77 static guint counter = 1;
79 static GStaticRecMutex registration_mutex = G_STATIC_REC_MUTEX_INIT;
81 /*---------------------------------------------------------------------------*/
83 static GStaticMutex recursion_check_guard = G_STATIC_MUTEX_INIT;
84 static gboolean recursion_check = FALSE;
87 recursion_check_and_set ()
90 g_static_mutex_lock (&recursion_check_guard);
91 ret = recursion_check;
92 recursion_check = TRUE;
93 g_static_mutex_unlock (&recursion_check_guard);
98 recursion_check_unset ()
100 g_static_mutex_lock (&recursion_check_guard);
101 recursion_check = FALSE;
102 g_static_mutex_unlock (&recursion_check_guard);
105 /*---------------------------------------------------------------------------*/
108 * Each AtkObject must be asssigned a D-Bus path (Reference)
110 * This function provides an integer reference for a new
114 assign_reference(void)
117 /* Reference of 0 not allowed as used as direct key in hash table */
123 * Returns the reference of the object, or 0 if it is not registered.
126 object_to_ref (AtkObject *accessible)
128 return GPOINTER_TO_INT(g_object_get_data (G_OBJECT (accessible), "dbus-id"));
132 * Converts the Accessible object reference to its D-Bus object path
135 ref_to_path (guint ref)
137 return g_strdup_printf(ATK_BRIDGE_OBJECT_REFERENCE_TEMPLATE, ref);
140 /*---------------------------------------------------------------------------*/
143 * Callback for when a registered AtkObject is destroyed.
145 * Removes the AtkObject from the reference lookup tables, meaning
146 * it is no longer exposed over D-Bus.
149 deregister_accessible (gpointer data, GObject *accessible)
152 g_assert (ATK_IS_OBJECT (accessible));
154 ref = object_to_ref (ATK_OBJECT(accessible));
157 g_hash_table_remove(ref2ptr, GINT_TO_POINTER(ref));
162 * Called to register an AtkObject with AT-SPI and expose it over D-Bus.
165 register_accessible (AtkObject *accessible)
168 g_assert(ATK_IS_OBJECT(accessible));
170 ref = assign_reference();
172 g_hash_table_insert (ref2ptr, GINT_TO_POINTER(ref), accessible);
173 g_object_set_data (G_OBJECT(accessible), "dbus-id", GINT_TO_POINTER(ref));
174 g_object_weak_ref(G_OBJECT(accessible), deregister_accessible, NULL);
177 /*---------------------------------------------------------------------------*/
180 * This function checks that the ref-count of an accessible
183 * There is not currently any remote reference counting
184 * in AT-SPI D-Bus so objects that are remotely owned are not
187 * TODO Add debug wrapper
190 non_owned_accessible (AtkObject *accessible)
192 if ((G_OBJECT (accessible))->ref_count <= 1)
194 g_warning ("AT-SPI: Child referenced that is not owned by its parent");
203 /*---------------------------------------------------------------------------*/
206 has_manages_descendants (AtkObject *accessible)
209 gboolean result = FALSE;
211 /* This is dangerous, refing the state set
212 * seems to do wierd things to the tree & cause recursion
213 * by modifying the tree alot.
215 state = atk_object_ref_state_set (accessible);
216 if (atk_state_set_contains_state (state, ATK_STATE_MANAGES_DESCENDANTS))
218 g_warning ("AT-SPI: Object with 'Manages descendants' states not currently handled by AT-SPI");
221 g_object_unref (state);
227 * Registers a subtree of accessible objects
228 * rooted at the accessible object provided.
230 * The leaf nodes do not have their children
231 * registered. A node is considered a leaf
232 * if it has the state "manages-descendants"
233 * or if it has already been registered.
236 register_subtree (AtkObject *accessible)
238 AtkObject *current, *tmp;
244 current = g_object_ref (accessible);
245 if (has_manages_descendants (current))
247 g_object_unref (current);
251 stack = g_queue_new ();
253 register_accessible (current);
254 g_queue_push_head (stack, GINT_TO_POINTER (0));
257 * The index held on the stack is the next child node
258 * that needs processing at the corresponding level in the tree.
260 while (!g_queue_is_empty (stack))
262 /* Find the next child node that needs processing */
264 i = GPOINTER_TO_INT(g_queue_peek_head (stack));
267 while (i < atk_object_get_n_accessible_children (current) &&
270 tmp = atk_object_ref_accessible_child (current, i);
272 /* TODO Add debug wrapper */
273 non_owned_accessible (tmp);
275 if (object_to_ref (tmp))
277 /* If its already registered, just update */
278 spi_emit_cache_update (tmp, atk_adaptor_app_data->bus);
280 else if (has_manages_descendants (tmp))
282 /* If it has manages descendants, just register and update */
283 register_accessible (tmp);
284 spi_emit_cache_update (tmp, atk_adaptor_app_data->bus);
293 g_object_unref (G_OBJECT (tmp));
301 /* Push onto stack */
303 register_accessible (current);
305 g_queue_peek_head_link (stack)->data = GINT_TO_POINTER (i);
306 g_queue_push_head (stack, GINT_TO_POINTER (0));
311 spi_emit_cache_update (current, atk_adaptor_app_data->bus);
313 current = atk_object_get_parent (current);
314 g_object_unref (G_OBJECT (tmp));
315 g_queue_pop_head (stack);
318 g_queue_free (stack);
321 /*---------------------------------------------------------------------------*/
324 * Called when an already registered object is updated in such a
325 * way that client side cache needs to be updated.
328 update_accessible (AtkObject *accessible)
331 g_assert(ATK_IS_OBJECT(accessible));
333 ref = object_to_ref (accessible);
336 spi_emit_cache_update (accessible, atk_adaptor_app_data->bus);
340 /*---------------------------------------------------------------------------*/
343 atk_dbus_foreach_registered(GHFunc func, gpointer data)
345 g_hash_table_foreach(ref2ptr, func, data);
349 * Used to lookup an AtkObject from its D-Bus path.
352 atk_dbus_path_to_object (const char *path)
359 if (strncmp(path, ATK_BRIDGE_OBJECT_PATH_PREFIX, ATK_BRIDGE_PATH_PREFIX_LENGTH) != 0)
362 path += ATK_BRIDGE_PATH_PREFIX_LENGTH; /* Skip over the prefix */
365 return atk_get_root();
371 data = g_hash_table_lookup (ref2ptr, GINT_TO_POINTER(index));
373 return ATK_OBJECT (data);
379 * Used to lookup a D-Bus path from the AtkObject.
382 atk_dbus_object_to_path (AtkObject *accessible)
386 ref = object_to_ref (accessible);
390 return ref_to_path (ref);
393 /*---------------------------------------------------------------------------*/
396 * Events are not evaluated for non-registered accessibles.
398 * When a property change is made on a registered accessible
399 * the client side cache should be updated.
401 * When a parent is changed the subtree is de-registered
402 * if the parent is not attached to the root accessible.
405 tree_update_listener (GSignalInvocationHint *signal_hint,
406 guint n_param_values,
407 const GValue *param_values,
410 AtkObject *accessible;
411 AtkPropertyValues *values;
412 const gchar *pname = NULL;
414 g_static_rec_mutex_lock (®istration_mutex);
416 /* Ensure that only registered accessibles
417 * have their signals processed.
419 accessible = g_value_get_object (¶m_values[0]);
420 g_assert (ATK_IS_OBJECT (accessible));
422 if (object_to_ref (accessible))
424 /* TODO Add debug wrapper */
425 if (recursion_check_and_set ())
426 g_warning ("AT-SPI: Recursive use of registration module");
428 values = (AtkPropertyValues*) g_value_get_pointer (¶m_values[1]);
429 pname = values[0].property_name;
430 if (strcmp (pname, "accessible-name") == 0 ||
431 strcmp (pname, "accessible-description") == 0)
433 update_accessible (accessible);
435 /* Parent value us updated by child-add signal of parent object */
437 recursion_check_unset ();
440 g_static_rec_mutex_unlock (®istration_mutex);
446 * Events are not evaluated for non registered accessibles.
448 * When the children of a registered accessible are changed
449 * the subtree, rooted at the child is registered.
452 tree_update_children_listener (GSignalInvocationHint *signal_hint,
453 guint n_param_values,
454 const GValue *param_values,
457 AtkObject *accessible;
458 const gchar *detail = NULL;
461 g_static_rec_mutex_lock (®istration_mutex);
463 /* Ensure that only registered accessibles
464 * have their signals processed.
466 accessible = g_value_get_object (¶m_values[0]);
467 g_assert (ATK_IS_OBJECT (accessible));
469 if (object_to_ref (accessible))
471 /* TODO Add debug wrapper */
472 if (recursion_check_and_set ())
473 g_warning ("AT-SPI: Recursive use of registration module");
475 if (signal_hint->detail)
476 detail = g_quark_to_string (signal_hint->detail);
478 if (!strcmp (detail, "add"))
481 int index = g_value_get_uint (param_values + 1);
482 child = g_value_get_pointer (param_values + 2);
484 if (!ATK_IS_OBJECT (child))
486 child = atk_object_ref_accessible_child (accessible, index);
487 /* TODO Add debug wrapper */
488 non_owned_accessible (child);
490 register_subtree (child);
493 recursion_check_unset ();
496 g_static_rec_mutex_unlock (®istration_mutex);
502 * Initializes required global data. The update and removal lists
503 * and the reference lookup tables.
505 * Initializes all of the required D-Bus interfaces.
508 atk_dbus_initialize (AtkObject *root)
511 ref2ptr = g_hash_table_new(g_direct_hash, g_direct_equal);
513 if (g_thread_supported ())
514 g_message ("AT-SPI: Threads enabled");
516 register_subtree (root);
518 atk_add_global_event_listener (tree_update_listener, "Gtk:AtkObject:property-change");
519 atk_add_global_event_listener (tree_update_children_listener, "Gtk:AtkObject:children-changed");
522 /*END------------------------------------------------------------------------*/