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"
32 * This module is responsible for keeping track of all the AtkObjects in
33 * the application, so that they can be accessed remotely and placed in
34 * a client side cache.
36 * To access an AtkObject remotely we need to provide a D-Bus object
37 * path for it. The D-Bus object paths used have a standard prefix
38 * (SPI_ATK_OBJECT_PATH_PREFIX). Appended to this prefix is a string
39 * representation of an integer reference. So to access an AtkObject
40 * remotely we keep a Hashtable that maps the given reference to
41 * the AtkObject pointer. An object in this hash table is said to be 'registered'.
43 * The architecture of AT-SPI dbus is such that AtkObjects are not
44 * remotely reference counted. This means that we need to keep track of
45 * object destruction. When an object is destroyed it must be 'deregistered'
46 * To do this lookup we keep a dbus-id attribute on each AtkObject.
48 * In addition to accessing the objects remotely we must be able to update
49 * the client side cache. This is done using the 'update' signal of the
50 * org.freedesktop.atspi.Tree interface. The update signal should send out
51 * all of the cacheable data for an Accessible object.
58 * This code seems very brittle.
59 * I would prefer changes to be made to
60 * gail and the ATK interface so that all Accessible
61 * objects are registered with an exporting module.
63 * This is the same system as Qt has with the QAccessibleBridge
64 * and QAccessibleBridgePlugin. It entails some rather
65 * large structural changes to ATK though:
67 * Removing infinite spaces (Child access no longer references child).
68 * Removing lazy creation of accessible objects.
71 #define SPI_ATK_OBJECT_PATH_PREFIX "/org/freedesktop/atspi/accessible"
72 #define SPI_ATK_OBJECT_PATH_DESKTOP "/root"
74 #define SPI_ATK_PATH_PREFIX_LENGTH 33
75 #define SPI_ATK_OBJECT_REFERENCE_TEMPLATE SPI_ATK_OBJECT_PATH_PREFIX "/%d"
78 static GHashTable *ref2ptr = NULL; /* Used for converting a D-Bus path (Reference) to the object pointer */
80 static guint reference_counter = 0;
82 static GStaticRecMutex registration_mutex = G_STATIC_REC_MUTEX_INIT;
84 /*---------------------------------------------------------------------------*/
86 static GStaticMutex recursion_check_guard = G_STATIC_MUTEX_INIT;
87 static gboolean recursion_check = FALSE;
90 recursion_check_and_set ()
93 g_static_mutex_lock (&recursion_check_guard);
94 ret = recursion_check;
95 recursion_check = TRUE;
96 g_static_mutex_unlock (&recursion_check_guard);
101 recursion_check_unset ()
103 g_static_mutex_lock (&recursion_check_guard);
104 recursion_check = FALSE;
105 g_static_mutex_unlock (&recursion_check_guard);
108 /*---------------------------------------------------------------------------*/
111 * Each AtkObject must be asssigned a D-Bus path (Reference)
113 * This function provides an integer reference for a new
117 assign_reference(void)
120 /* Reference of 0 not allowed as used as direct key in hash table */
121 if (reference_counter == 0)
123 return reference_counter;
127 * Returns the reference of the object, or 0 if it is not registered.
130 object_to_ref (AtkObject *accessible)
132 return GPOINTER_TO_INT(g_object_get_data (G_OBJECT (accessible), "dbus-id"));
136 * Converts the Accessible object reference to its D-Bus object path
139 ref_to_path (guint ref)
141 return g_strdup_printf(SPI_ATK_OBJECT_REFERENCE_TEMPLATE, ref);
144 /*---------------------------------------------------------------------------*/
147 * Callback for when a registered AtkObject is destroyed.
149 * Removes the AtkObject from the reference lookup tables, meaning
150 * it is no longer exposed over D-Bus.
153 deregister_accessible (gpointer data, GObject *accessible)
156 g_return_if_fail (ATK_IS_OBJECT (accessible));
158 ref = object_to_ref (ATK_OBJECT(accessible));
161 g_hash_table_remove(ref2ptr, GINT_TO_POINTER(ref));
166 * Called to register an AtkObject with AT-SPI and expose it over D-Bus.
169 register_accessible (AtkObject *accessible)
172 g_return_if_fail (ATK_IS_OBJECT(accessible));
174 ref = assign_reference();
176 g_hash_table_insert (ref2ptr, GINT_TO_POINTER(ref), accessible);
177 g_object_set_data (G_OBJECT(accessible), "dbus-id", GINT_TO_POINTER(ref));
178 g_object_weak_ref(G_OBJECT(accessible), deregister_accessible, NULL);
181 /*---------------------------------------------------------------------------*/
185 * This function checks that the ref-count of an accessible
188 * There is not currently any remote reference counting
189 * in AT-SPI D-Bus so objects that are remotely owned are not
192 * TODO Add debug wrapper
195 non_owned_accessible (AtkObject *accessible)
197 if ((G_OBJECT (accessible))->ref_count <= 1)
199 g_warning ("AT-SPI: Child referenced that is not owned by its parent");
207 #endif /* SPI_ATK_DEBUG */
209 /*---------------------------------------------------------------------------*/
212 has_manages_descendants (AtkObject *accessible)
215 gboolean result = FALSE;
217 /* This is dangerous, refing the state set
218 * seems to do wierd things to the tree & cause recursion
219 * by modifying the tree alot.
221 state = atk_object_ref_state_set (accessible);
222 if (atk_state_set_contains_state (state, ATK_STATE_MANAGES_DESCENDANTS))
225 g_warning ("AT-SPI: Object with 'Manages descendants' states not currently handled by AT-SPI");
229 g_object_unref (state);
235 append_children (AtkObject *accessible, GQueue *traversal)
240 for (i =0; i < atk_object_get_n_accessible_children (accessible); i++)
242 current = atk_object_ref_accessible_child (accessible, i);
246 non_owned_accessible (current);
248 if (!has_manages_descendants (current))
249 g_queue_push_tail (traversal, current);
251 g_object_unref (G_OBJECT (current));
257 * Registers a subtree of accessible objects
258 * rooted at the accessible object provided.
260 * The leaf nodes do not have their children
261 * registered. A node is considered a leaf
262 * if it has the state "manages-descendants"
263 * or if it has already been registered.
266 register_subtree (AtkObject *accessible)
272 g_return_if_fail (ATK_IS_OBJECT (accessible));
274 traversal = g_queue_new ();
275 emit_update = g_queue_new ();
277 g_object_ref (accessible);
278 g_queue_push_tail (traversal, accessible);
280 while (!g_queue_is_empty (traversal))
282 current = g_queue_pop_head (traversal);
283 g_queue_push_tail (emit_update, current);
284 if (!object_to_ref (current))
286 register_accessible (current);
288 g_debug ("REG - %s - %d - %s", atk_object_get_name (current),
289 atk_object_get_role (current),
290 atk_dbus_object_to_path (current));
292 append_children (current, traversal);
296 while (!g_queue_is_empty (emit_update))
298 current = g_queue_pop_head (emit_update);
299 spi_emit_cache_update (current, atk_adaptor_app_data->bus);
300 g_object_unref (G_OBJECT (current));
303 g_queue_free (traversal);
304 g_queue_free (emit_update);
307 /*---------------------------------------------------------------------------*/
310 * Called when an already registered object is updated in such a
311 * way that client side cache needs to be updated.
314 update_accessible (AtkObject *accessible)
317 g_return_if_fail (ATK_IS_OBJECT(accessible));
319 ref = object_to_ref (accessible);
322 spi_emit_cache_update (accessible, atk_adaptor_app_data->bus);
326 /*---------------------------------------------------------------------------*/
329 atk_dbus_foreach_registered(GHFunc func, gpointer data)
331 g_hash_table_foreach(ref2ptr, func, data);
335 * Used to lookup an AtkObject from its D-Bus path.
338 atk_dbus_path_to_object (const char *path)
343 g_return_val_if_fail (path, NULL);
345 if (strncmp(path, SPI_ATK_OBJECT_PATH_PREFIX, SPI_ATK_PATH_PREFIX_LENGTH) != 0)
348 path += SPI_ATK_PATH_PREFIX_LENGTH; /* Skip over the prefix */
350 if (!g_strcmp0 (SPI_ATK_OBJECT_PATH_DESKTOP, path))
351 return atk_get_root();
357 data = g_hash_table_lookup (ref2ptr, GINT_TO_POINTER(index));
359 return ATK_OBJECT (data);
365 * TODO WARNING HACK This function is dangerous.
366 * It should only be called before sending an event on an
367 * object that has not already been registered.
370 atk_dbus_object_attempt_registration (AtkObject *accessible)
374 ref = object_to_ref (accessible);
377 /* See if the object is attached to the main tree */
378 AtkObject *current, *prev = NULL;
381 /* This should iterate until it hits a NULL or registered parent */
383 current = atk_object_get_parent (accessible);
385 cref = object_to_ref (current);
386 while (current && !cref)
389 current = atk_object_get_parent (current);
391 cref = object_to_ref (current);
394 /* A registered parent, with non-registered child, has been found */
397 register_subtree (prev);
400 /* The object SHOULD be registered now. If it isn't - I give up */
401 ref = object_to_ref (accessible);
404 return ref_to_path (ref);
409 g_debug ("AT-SPI: Could not register a non-attached accessible object");
416 return ref_to_path (ref);
422 * Used to lookup a D-Bus path from the AtkObject.
425 atk_dbus_object_to_path (AtkObject *accessible)
429 ref = object_to_ref (accessible);
433 return ref_to_path (ref);
437 atk_dbus_desktop_object_path ()
439 return g_strdup (SPI_ATK_OBJECT_PATH_PREFIX SPI_ATK_OBJECT_PATH_DESKTOP);
442 /*---------------------------------------------------------------------------*/
444 typedef gboolean (*TreeUpdateAction) (GSignalInvocationHint *signal_hint,
445 guint n_param_values,
446 const GValue *param_values,
448 AtkObject *accessible);
451 * Events are not evaluated for non-registered accessibles.
453 * When a property change is made on a registered accessible
454 * the client side cache should be updated.
456 * When a parent is changed the subtree is de-registered
457 * if the parent is not attached to the root accessible.
459 /* TODO Turn this function into a macro? */
461 tree_update_wrapper (GSignalInvocationHint *signal_hint,
462 guint n_param_values,
463 const GValue *param_values,
465 TreeUpdateAction action)
467 AtkObject *accessible;
469 g_static_rec_mutex_lock (®istration_mutex);
471 /* Ensure that only registered accessibles
472 * have their signals processed.
474 accessible = ATK_OBJECT(g_value_get_object (¶m_values[0]));
475 g_return_val_if_fail (ATK_IS_OBJECT (accessible), TRUE);
477 if (object_to_ref (accessible))
480 if (recursion_check_and_set ())
481 g_warning ("AT-SPI: Recursive use of registration module");
483 g_debug ("AT-SPI: Tree update listener");
485 action (signal_hint, n_param_values, param_values, data, accessible);
487 recursion_check_unset ();
490 g_static_rec_mutex_unlock (®istration_mutex);
496 tree_update_state_action (GSignalInvocationHint *signal_hint,
497 guint n_param_values,
498 const GValue *param_values,
500 AtkObject *accessible)
502 update_accessible (accessible);
506 tree_update_state_listener (GSignalInvocationHint *signal_hint,
507 guint n_param_values,
508 const GValue *param_values,
511 tree_update_wrapper (signal_hint, n_param_values, param_values, data, tree_update_state_action);
515 tree_update_property_action (GSignalInvocationHint *signal_hint,
516 guint n_param_values,
517 const GValue *param_values,
519 AtkObject *accessible)
521 AtkPropertyValues *values;
522 const gchar *pname = NULL;
524 values = (AtkPropertyValues*) g_value_get_pointer (¶m_values[1]);
525 pname = values[0].property_name;
526 if (strcmp (pname, "accessible-name") == 0 ||
527 strcmp (pname, "accessible-description") == 0 ||
528 strcmp (pname, "accessible-parent") == 0)
530 update_accessible (accessible);
532 /* Parent value us updated by child-add signal of parent object */
536 tree_update_property_listener (GSignalInvocationHint *signal_hint,
537 guint n_param_values,
538 const GValue *param_values,
541 tree_update_wrapper (signal_hint, n_param_values, param_values, data, tree_update_property_action);
545 tree_update_children_action (GSignalInvocationHint *signal_hint,
546 guint n_param_values,
547 const GValue *param_values,
549 AtkObject *accessible)
551 const gchar *detail = NULL;
554 if (signal_hint->detail)
555 detail = g_quark_to_string (signal_hint->detail);
557 if (!strcmp (detail, "add"))
560 int index = g_value_get_uint (param_values + 1);
561 child = g_value_get_pointer (param_values + 2);
563 if (!ATK_IS_OBJECT (child))
565 child = atk_object_ref_accessible_child (accessible, index);
567 non_owned_accessible (child);
570 register_subtree (child);
571 update_accessible (accessible);
576 tree_update_children_listener (GSignalInvocationHint *signal_hint,
577 guint n_param_values,
578 const GValue *param_values,
581 tree_update_wrapper (signal_hint, n_param_values, param_values, data, tree_update_children_action);
584 /*---------------------------------------------------------------------------*/
587 spi_atk_register_toplevel_added (AtkObject *accessible,
591 g_static_rec_mutex_lock (®istration_mutex);
593 g_return_if_fail (ATK_IS_OBJECT (accessible));
595 if (object_to_ref (accessible))
598 if (recursion_check_and_set ())
599 g_warning ("AT-SPI: Recursive use of registration module");
601 g_debug ("AT-SPI: Toplevel added listener");
603 if (!ATK_IS_OBJECT (child))
605 child = atk_object_ref_accessible_child (accessible, index);
607 non_owned_accessible (child);
610 register_subtree (child);
611 update_accessible (accessible);
613 recursion_check_unset ();
616 g_static_rec_mutex_unlock (®istration_mutex);
620 spi_atk_register_toplevel_removed (AtkObject *accessible,
624 g_static_rec_mutex_lock (®istration_mutex);
626 g_return_if_fail (ATK_IS_OBJECT (accessible));
628 if (object_to_ref (accessible))
631 if (recursion_check_and_set ())
632 g_warning ("AT-SPI: Recursive use of registration module");
634 g_debug ("AT-SPI: Toplevel removed listener");
636 update_accessible (accessible);
637 recursion_check_unset ();
640 g_static_rec_mutex_unlock (®istration_mutex);
644 * Initializes required global data. The update and removal lists
645 * and the reference lookup tables.
647 * Initializes all of the required D-Bus interfaces.
650 atk_dbus_initialize (AtkObject *root)
653 ref2ptr = g_hash_table_new(g_direct_hash, g_direct_equal);
656 if (g_thread_supported ())
657 g_message ("AT-SPI: Threads enabled");
659 g_debug ("AT-SPI: Initial Atk tree regisration");
662 register_subtree (root);
664 atk_add_global_event_listener (tree_update_property_listener, "Gtk:AtkObject:property-change");
665 atk_add_global_event_listener (tree_update_children_listener, "Gtk:AtkObject:children-changed");
666 atk_add_global_event_listener (tree_update_state_listener, "Gtk:AtkObject:state-change");
668 g_signal_connect (root,
669 "children-changed::add",
670 (GCallback) spi_atk_register_toplevel_added,
672 g_signal_connect (root,
673 "children-changed::remove",
674 (GCallback) spi_atk_register_toplevel_removed,
678 /*END------------------------------------------------------------------------*/