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 */
81 static guint reference_counter = 0;
83 static GStaticRecMutex registration_mutex = G_STATIC_REC_MUTEX_INIT;
85 /*---------------------------------------------------------------------------*/
87 static GStaticMutex recursion_check_guard = G_STATIC_MUTEX_INIT;
88 static gboolean recursion_check = FALSE;
91 recursion_check_and_set ()
94 g_static_mutex_lock (&recursion_check_guard);
95 ret = recursion_check;
96 recursion_check = TRUE;
97 g_static_mutex_unlock (&recursion_check_guard);
102 recursion_check_unset ()
104 g_static_mutex_lock (&recursion_check_guard);
105 recursion_check = FALSE;
106 g_static_mutex_unlock (&recursion_check_guard);
109 /*---------------------------------------------------------------------------*/
112 * Each AtkObject must be asssigned a D-Bus path (Reference)
114 * This function provides an integer reference for a new
118 assign_reference(void)
121 /* Reference of 0 not allowed as used as direct key in hash table */
122 if (reference_counter == 0)
124 return reference_counter;
128 * Returns the reference of the object, or 0 if it is not registered.
131 object_to_ref (AtkObject *accessible)
133 return GPOINTER_TO_INT(g_object_get_data (G_OBJECT (accessible), "dbus-id"));
137 * Converts the Accessible object reference to its D-Bus object path
140 atk_dbus_ref_to_path (guint ref)
142 return g_strdup_printf(SPI_ATK_OBJECT_REFERENCE_TEMPLATE, ref);
145 /*---------------------------------------------------------------------------*/
148 * Callback for when a registered AtkObject is destroyed.
150 * Removes the AtkObject from the reference lookup tables, meaning
151 * it is no longer exposed over D-Bus.
154 deregister_accessible (gpointer data, GObject *accessible)
157 g_return_if_fail (ATK_IS_OBJECT (accessible));
159 ref = object_to_ref (ATK_OBJECT(accessible));
162 spi_emit_cache_removal (ref, atk_adaptor_app_data->bus);
163 g_hash_table_remove(ref2ptr, GINT_TO_POINTER(ref));
168 * Called to register an AtkObject with AT-SPI and expose it over D-Bus.
171 register_accessible (AtkObject *accessible)
174 g_return_if_fail (ATK_IS_OBJECT(accessible));
176 ref = assign_reference();
178 g_hash_table_insert (ref2ptr, GINT_TO_POINTER(ref), accessible);
179 g_object_set_data (G_OBJECT(accessible), "dbus-id", GINT_TO_POINTER(ref));
180 g_object_weak_ref(G_OBJECT(accessible), deregister_accessible, NULL);
183 /*---------------------------------------------------------------------------*/
187 * This function checks that the ref-count of an accessible
190 * There is not currently any remote reference counting
191 * in AT-SPI D-Bus so objects that are remotely owned are not
194 * TODO Add debug wrapper
197 non_owned_accessible (AtkObject *accessible)
199 if ((G_OBJECT (accessible))->ref_count <= 1)
201 g_warning ("AT-SPI: Child referenced that is not owned by its parent");
209 #endif /* SPI_ATK_DEBUG */
211 /*---------------------------------------------------------------------------*/
214 has_manages_descendants (AtkObject *accessible)
217 gboolean result = FALSE;
219 /* This is dangerous, refing the state set
220 * seems to do wierd things to the tree & cause recursion
221 * by modifying the tree alot.
223 state = atk_object_ref_state_set (accessible);
224 if (atk_state_set_contains_state (state, ATK_STATE_MANAGES_DESCENDANTS))
227 g_warning ("AT-SPI: Object with 'Manages descendants' states not currently handled by AT-SPI");
231 g_object_unref (state);
237 append_children (AtkObject *accessible, GQueue *traversal)
241 gint count = atk_object_get_n_accessible_children (accessible);
243 if (count < 0) count = 0;
244 for (i =0; i < count; i++)
246 current = atk_object_ref_accessible_child (accessible, i);
250 non_owned_accessible (current);
252 if (!has_manages_descendants (current))
253 g_queue_push_tail (traversal, current);
255 g_object_unref (G_OBJECT (current));
261 * Registers a subtree of accessible objects
262 * rooted at the accessible object provided.
264 * The leaf nodes do not have their children
265 * registered. A node is considered a leaf
266 * if it has the state "manages-descendants"
267 * or if it has already been registered.
270 register_subtree (AtkObject *accessible)
276 g_return_if_fail (ATK_IS_OBJECT (accessible));
278 traversal = g_queue_new ();
279 emit_update = g_queue_new ();
281 g_object_ref (accessible);
282 g_queue_push_tail (traversal, accessible);
284 while (!g_queue_is_empty (traversal))
286 current = g_queue_pop_head (traversal);
287 g_queue_push_tail (emit_update, current);
288 if (!object_to_ref (current))
290 register_accessible (current);
292 g_debug ("REG - %s - %d - %s", atk_object_get_name (current),
293 atk_object_get_role (current),
294 atk_dbus_object_to_path (current));
296 append_children (current, traversal);
300 while (!g_queue_is_empty (emit_update))
302 current = g_queue_pop_head (emit_update);
303 spi_emit_cache_update (current, atk_adaptor_app_data->bus);
304 g_object_unref (G_OBJECT (current));
307 g_queue_free (traversal);
308 g_queue_free (emit_update);
311 /*---------------------------------------------------------------------------*/
314 * Called when an already registered object is updated in such a
315 * way that client side cache needs to be updated.
318 update_accessible (AtkObject *accessible)
321 g_return_if_fail (ATK_IS_OBJECT(accessible));
323 ref = object_to_ref (accessible);
326 spi_emit_cache_update (accessible, atk_adaptor_app_data->bus);
330 /*---------------------------------------------------------------------------*/
333 atk_dbus_foreach_registered(GHFunc func, gpointer data)
335 g_hash_table_foreach(ref2ptr, func, data);
339 * Used to lookup an AtkObject from its D-Bus path.
342 atk_dbus_path_to_object (const char *path)
347 g_return_val_if_fail (path, NULL);
349 if (strncmp(path, SPI_ATK_OBJECT_PATH_PREFIX, SPI_ATK_PATH_PREFIX_LENGTH) != 0)
352 path += SPI_ATK_PATH_PREFIX_LENGTH; /* Skip over the prefix */
354 if (!g_strcmp0 (SPI_ATK_OBJECT_PATH_DESKTOP, path))
355 return atk_get_root();
361 data = g_hash_table_lookup (ref2ptr, GINT_TO_POINTER(index));
363 return ATK_OBJECT (data);
369 * TODO WARNING HACK This function is dangerous.
370 * It should only be called before sending an event on an
371 * object that has not already been registered.
374 atk_dbus_object_attempt_registration (AtkObject *accessible)
378 ref = object_to_ref (accessible);
381 /* See if the object is attached to the main tree */
382 AtkObject *current, *prev = NULL;
385 /* This should iterate until it hits a NULL or registered parent */
387 current = atk_object_get_parent (accessible);
389 cref = object_to_ref (current);
390 while (current && !cref)
393 current = atk_object_get_parent (current);
395 cref = object_to_ref (current);
398 /* A registered parent, with non-registered child, has been found */
401 register_subtree (prev);
404 /* The object SHOULD be registered now. If it isn't - I give up */
405 ref = object_to_ref (accessible);
408 return atk_dbus_ref_to_path (ref);
413 g_debug ("AT-SPI: Could not register a non-attached accessible object");
420 return atk_dbus_ref_to_path (ref);
426 * Used to lookup a D-Bus path from the AtkObject.
429 atk_dbus_object_to_path (AtkObject *accessible)
433 ref = object_to_ref (accessible);
437 return atk_dbus_ref_to_path (ref);
441 atk_dbus_desktop_object_path ()
443 return g_strdup (SPI_ATK_OBJECT_PATH_PREFIX SPI_ATK_OBJECT_PATH_DESKTOP);
446 /*---------------------------------------------------------------------------*/
448 typedef gboolean (*TreeUpdateAction) (GSignalInvocationHint *signal_hint,
449 guint n_param_values,
450 const GValue *param_values,
452 AtkObject *accessible);
455 * Events are not evaluated for non-registered accessibles.
457 * When a property change is made on a registered accessible
458 * the client side cache should be updated.
460 * When a parent is changed the subtree is de-registered
461 * if the parent is not attached to the root accessible.
463 /* TODO Turn this function into a macro? */
465 tree_update_wrapper (GSignalInvocationHint *signal_hint,
466 guint n_param_values,
467 const GValue *param_values,
469 TreeUpdateAction action)
471 AtkObject *accessible;
473 g_static_rec_mutex_lock (®istration_mutex);
475 /* Ensure that only registered accessibles
476 * have their signals processed.
478 accessible = ATK_OBJECT(g_value_get_object (¶m_values[0]));
479 g_return_val_if_fail (ATK_IS_OBJECT (accessible), TRUE);
481 if (object_to_ref (accessible))
484 if (recursion_check_and_set ())
485 g_warning ("AT-SPI: Recursive use of registration module");
487 g_debug ("AT-SPI: Tree update listener");
489 action (signal_hint, n_param_values, param_values, data, accessible);
491 recursion_check_unset ();
494 g_static_rec_mutex_unlock (®istration_mutex);
500 tree_update_state_action (GSignalInvocationHint *signal_hint,
501 guint n_param_values,
502 const GValue *param_values,
504 AtkObject *accessible)
506 update_accessible (accessible);
511 tree_update_state_listener (GSignalInvocationHint *signal_hint,
512 guint n_param_values,
513 const GValue *param_values,
516 tree_update_wrapper (signal_hint, n_param_values, param_values, data, tree_update_state_action);
521 tree_update_property_action (GSignalInvocationHint *signal_hint,
522 guint n_param_values,
523 const GValue *param_values,
525 AtkObject *accessible)
527 AtkPropertyValues *values;
528 const gchar *pname = NULL;
530 values = (AtkPropertyValues*) g_value_get_pointer (¶m_values[1]);
531 pname = values[0].property_name;
532 if (strcmp (pname, "accessible-name") == 0 ||
533 strcmp (pname, "accessible-description") == 0 ||
534 strcmp (pname, "accessible-parent") == 0)
536 update_accessible (accessible);
538 /* Parent value us updated by child-add signal of parent object */
543 tree_update_property_listener (GSignalInvocationHint *signal_hint,
544 guint n_param_values,
545 const GValue *param_values,
548 tree_update_wrapper (signal_hint, n_param_values, param_values, data, tree_update_property_action);
553 tree_update_children_action (GSignalInvocationHint *signal_hint,
554 guint n_param_values,
555 const GValue *param_values,
557 AtkObject *accessible)
559 const gchar *detail = NULL;
562 if (has_manages_descendants (accessible)) return TRUE;
563 if (signal_hint->detail)
564 detail = g_quark_to_string (signal_hint->detail);
566 if (!strcmp (detail, "add"))
569 int index = g_value_get_uint (param_values + 1);
570 child = g_value_get_pointer (param_values + 2);
572 if (!ATK_IS_OBJECT (child))
574 child = atk_object_ref_accessible_child (accessible, index);
576 non_owned_accessible (child);
579 register_subtree (child);
580 update_accessible (accessible);
586 tree_update_children_listener (GSignalInvocationHint *signal_hint,
587 guint n_param_values,
588 const GValue *param_values,
591 tree_update_wrapper (signal_hint, n_param_values, param_values, data, tree_update_children_action);
595 /*---------------------------------------------------------------------------*/
598 spi_atk_register_toplevel_added (AtkObject *accessible,
602 g_static_rec_mutex_lock (®istration_mutex);
604 g_return_if_fail (ATK_IS_OBJECT (accessible));
606 if (object_to_ref (accessible))
609 if (recursion_check_and_set ())
610 g_warning ("AT-SPI: Recursive use of registration module");
612 g_debug ("AT-SPI: Toplevel added listener");
614 if (!ATK_IS_OBJECT (child))
616 child = atk_object_ref_accessible_child (accessible, index);
618 non_owned_accessible (child);
621 register_subtree (child);
622 update_accessible (accessible);
624 recursion_check_unset ();
627 g_static_rec_mutex_unlock (®istration_mutex);
631 spi_atk_register_toplevel_removed (AtkObject *accessible,
635 g_static_rec_mutex_lock (®istration_mutex);
637 g_return_if_fail (ATK_IS_OBJECT (accessible));
639 if (object_to_ref (accessible))
642 if (recursion_check_and_set ())
643 g_warning ("AT-SPI: Recursive use of registration module");
645 g_debug ("AT-SPI: Toplevel removed listener");
647 update_accessible (accessible);
648 recursion_check_unset ();
651 g_static_rec_mutex_unlock (®istration_mutex);
655 * Initializes required global data. The update and removal lists
656 * and the reference lookup tables.
658 * Initializes all of the required D-Bus interfaces.
661 atk_dbus_initialize (AtkObject *root)
664 ref2ptr = g_hash_table_new(g_direct_hash, g_direct_equal);
667 if (g_thread_supported ())
668 g_message ("AT-SPI: Threads enabled");
670 g_debug ("AT-SPI: Initial Atk tree regisration");
673 register_subtree (root);
675 atk_add_global_event_listener (tree_update_property_listener, "Gtk:AtkObject:property-change");
676 atk_add_global_event_listener (tree_update_children_listener, "Gtk:AtkObject:children-changed");
677 atk_add_global_event_listener (tree_update_state_listener, "Gtk:AtkObject:state-change");
679 g_signal_connect (root,
680 "children-changed::add",
681 (GCallback) spi_atk_register_toplevel_added,
683 g_signal_connect (root,
684 "children-changed::remove",
685 (GCallback) spi_atk_register_toplevel_removed,
689 /*END------------------------------------------------------------------------*/