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 * HACK - Needs permanent soltion.
188 * TODO Add debug wrapper
191 non_owned_accessible (AtkObject *accessible)
193 if ((G_OBJECT (accessible))->ref_count <= 1)
195 g_critical ("AT-SPI: Child referenced that is not owned by its parent");
204 /*---------------------------------------------------------------------------*/
207 has_manages_descendants (AtkObject *accessible)
210 gboolean result = FALSE;
212 /* This is dangerous, refing the state set
213 * seems to do wierd things to the tree & cause recursion
214 * by modifying the tree alot.
216 state = atk_object_ref_state_set (accessible);
217 if (atk_state_set_contains_state (state, ATK_STATE_MANAGES_DESCENDANTS))
219 g_message ("AT-SPI: Object with 'Manages descendants' states not currently handled by AT-SPI");
222 g_object_unref (state);
228 * Registers a subtree of accessible objects
229 * rooted at the accessible object provided.
231 * The leaf nodes do not have their children
232 * registered. A node is considered a leaf
233 * if it has the state "manages-descendants"
234 * or if it has already been registered.
237 register_subtree (AtkObject *accessible)
239 AtkObject *current, *tmp;
245 current = g_object_ref (accessible);
246 if (has_manages_descendants (current))
248 g_object_unref (current);
252 stack = g_queue_new ();
254 register_accessible (current);
255 g_queue_push_head (stack, GINT_TO_POINTER (0));
258 * The index held on the stack is the next child node
259 * that needs processing at the corresponding level in the tree.
261 while (!g_queue_is_empty (stack))
263 /* Find the next child node that needs processing */
265 i = GPOINTER_TO_INT(g_queue_peek_head (stack));
268 while (i < atk_object_get_n_accessible_children (current) &&
271 tmp = atk_object_ref_accessible_child (current, i);
273 /* TODO Add debug wrapper */
274 if (non_owned_accessible (tmp))
280 if (object_to_ref (tmp))
282 /* If its already registered, just update */
283 spi_emit_cache_update (tmp, atk_adaptor_app_data->bus);
285 else if (has_manages_descendants (tmp))
287 /* If it has manages descendants, just register and update */
288 register_accessible (tmp);
289 spi_emit_cache_update (tmp, atk_adaptor_app_data->bus);
299 g_object_unref (G_OBJECT (tmp));
305 /* Push onto stack */
307 register_accessible (current);
309 g_queue_peek_head_link (stack)->data = GINT_TO_POINTER (i+1);
310 g_queue_push_head (stack, GINT_TO_POINTER (0));
315 spi_emit_cache_update (current, atk_adaptor_app_data->bus);
317 current = atk_object_get_parent (current);
318 g_object_unref (G_OBJECT (tmp));
319 g_queue_pop_head (stack);
322 g_queue_free (stack);
325 /*---------------------------------------------------------------------------*/
328 * Called when an already registered object is updated in such a
329 * way that client side cache needs to be updated.
332 update_accessible (AtkObject *accessible)
335 g_assert(ATK_IS_OBJECT(accessible));
337 ref = object_to_ref (accessible);
340 spi_emit_cache_update (accessible, atk_adaptor_app_data->bus);
344 /*---------------------------------------------------------------------------*/
347 atk_dbus_foreach_registered(GHFunc func, gpointer data)
349 g_hash_table_foreach(ref2ptr, func, data);
353 * Used to lookup an AtkObject from its D-Bus path.
356 atk_dbus_path_to_object (const char *path)
363 if (strncmp(path, ATK_BRIDGE_OBJECT_PATH_PREFIX, ATK_BRIDGE_PATH_PREFIX_LENGTH) != 0)
366 path += ATK_BRIDGE_PATH_PREFIX_LENGTH; /* Skip over the prefix */
369 return atk_get_root();
375 data = g_hash_table_lookup (ref2ptr, GINT_TO_POINTER(index));
377 return ATK_OBJECT (data);
383 * Used to lookup a D-Bus path from the AtkObject.
386 atk_dbus_object_to_path (AtkObject *accessible)
390 ref = object_to_ref (accessible);
394 return ref_to_path (ref);
397 /*---------------------------------------------------------------------------*/
400 * Events are not evaluated for non-registered accessibles.
402 * When a property change is made on a registered accessible
403 * the client side cache should be updated.
405 * When a parent is changed the subtree is de-registered
406 * if the parent is not attached to the root accessible.
409 tree_update_listener (GSignalInvocationHint *signal_hint,
410 guint n_param_values,
411 const GValue *param_values,
414 AtkObject *accessible;
415 AtkPropertyValues *values;
416 const gchar *pname = NULL;
418 g_static_rec_mutex_lock (®istration_mutex);
420 /* Ensure that only registered accessibles
421 * have their signals processed.
423 accessible = g_value_get_object (¶m_values[0]);
424 if (object_to_ref (accessible))
426 /* TODO Add debug wrapper */
427 if (recursion_check_and_set ())
428 g_critical ("AT-SPI: Recursive use of registration module");
430 if (!ATK_IS_OBJECT (accessible))
431 g_critical ("AT-SPI: Object data updated when not a valid AtkObject");
433 values = (AtkPropertyValues*) g_value_get_pointer (¶m_values[1]);
434 pname = values[0].property_name;
435 if (strcmp (pname, "accessible-name") == 0 ||
436 strcmp (pname, "accessible-description"))
438 update_accessible (accessible);
440 /* Parent updates not used */
441 /* Parent value us updated buy child-add signal of parent object */
443 else if (strcmp (pname, "accessible-parent"))
445 update_accessible (accessible);
448 recursion_check_unset ();
451 g_static_rec_mutex_unlock (®istration_mutex);
457 * Events are not evaluated for non registered accessibles.
459 * When the children of a registered accessible are changed
460 * the subtree, rooted at the child is registered.
463 tree_update_children_listener (GSignalInvocationHint *signal_hint,
464 guint n_param_values,
465 const GValue *param_values,
468 AtkObject *accessible;
469 const gchar *detail = NULL;
472 g_static_rec_mutex_lock (®istration_mutex);
474 /* Ensure that only registered accessibles
475 * have their signals processed.
477 accessible = g_value_get_object (¶m_values[0]);
478 if (object_to_ref (accessible))
480 /* TODO Add debug wrapper */
481 if (recursion_check_and_set ())
482 g_warning ("AT-SPI: Recursive use of registration module");
484 if (!ATK_IS_OBJECT (accessible))
485 g_critical ("AT-SPI: Object children updated when not a valid AtkObject");
487 if (signal_hint->detail)
488 detail = g_quark_to_string (signal_hint->detail);
489 if (!strcmp (detail, "add"))
492 int index = g_value_get_uint (param_values + 1);
493 child = g_value_get_pointer (param_values + 2);
495 if (!ATK_IS_OBJECT (child))
497 child = atk_object_ref_accessible_child (accessible, index);
498 /* TODO Add debug wrapper */
499 if (!non_owned_accessible (child))
501 register_subtree (child);
506 register_subtree (child);
511 recursion_check_unset ();
514 g_static_rec_mutex_unlock (®istration_mutex);
520 * Initializes required global data. The update and removal lists
521 * and the reference lookup tables.
523 * Initializes all of the required D-Bus interfaces.
526 atk_dbus_initialize (AtkObject *root)
529 ref2ptr = g_hash_table_new(g_direct_hash, g_direct_equal);
531 if (g_thread_supported ())
532 g_message ("AT-SPI: Threads enabled");
534 register_subtree (root);
536 atk_add_global_event_listener (tree_update_listener, "Gtk:AtkObject:property-change");
537 atk_add_global_event_listener (tree_update_children_listener, "Gtk:AtkObject:children-changed");
540 /*END------------------------------------------------------------------------*/