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 * Need to add concurrency support.
35 #define ATK_BRIDGE_OBJECT_PATH_PREFIX "/org/freedesktop/atspi/accessible"
36 #define ATK_BRIDGE_OBJECT_REFERENCE_TEMPLATE ATK_BRIDGE_OBJECT_PATH_PREFIX "/%d"
37 #define ATK_BRIDGE_PATH_PREFIX_LENGTH 33
40 * This module is responsible for keeping track of all the AtkObjects in
41 * the application, so that they can be accessed remotely and placed in
42 * a client side cache.
44 * To access an AtkObject remotely we need to provide a D-Bus object
45 * path for it. The D-Bus object paths used have a standard prefix
46 * (ATK_BRIDGE_OBJECT_PATH_PREFIX). Appended to this prefix is a string
47 * representation of an integer reference. So to access an AtkObject
48 * remotely we keep a Hashtable that maps the given reference to
49 * the AtkObject pointer. An object in this hash table is said to be 'registered'.
51 * The architecture of AT-SPI dbus is such that AtkObjects are not
52 * remotely reference counted. This means that we need to keep track of
53 * object destruction. When an object is destroyed it must be 'deregistered'
54 * To do this lookup we keep a dbus-id attribute on each AtkObject.
56 * In addition to accessing the objects remotely we must be able to update
57 * the client side cache. This is done using the 'update' signal of the
58 * org.freedesktop.atspi.Tree interface. The update signal should send out
59 * all of the cacheable data for an Accessible object.
62 GHashTable *ref2ptr = NULL; /* Used for converting a D-Bus path (Reference) to the object pointer */
64 static guint counter = 1;
66 /*---------------------------------------------------------------------------*/
69 * Each AtkObject must be asssigned a D-Bus path (Reference)
71 * This function provides an integer reference for a new
75 assign_reference(void)
78 /* Reference of 0 not allowed as used as direct key in hash table */
83 /*---------------------------------------------------------------------------*/
86 atk_dbus_foreach_registered(GHFunc func, gpointer data)
88 g_hash_table_foreach(ref2ptr, func, data);
91 /*---------------------------------------------------------------------------*/
94 * Called when a registered AtkObject is deleted.
95 * Removes the AtkObject from the reference lookup tables.
96 * Sets the client side cache to be updated.
99 deregister_accessible(gpointer data, GObject *accessible)
104 g_assert(ATK_IS_OBJECT(accessible));
107 ref = atk_dbus_object_to_ref (ATK_OBJECT(accessible));
111 g_hash_table_remove(ref2ptr, GINT_TO_POINTER(ref));
114 * Pyatspi client side exceptions have occured indicating
115 * that an object has been removed twice.
116 * This should not be possible and needs investigation.
118 spi_emit_cache_removal (ref, atk_adaptor_app_data->bus);
122 /*---------------------------------------------------------------------------*/
125 * Horrible hack warning.
129 * In an ideal world there would be a signal "Accessible created" that we could
130 * use to register all new AtkObjects with D-Bus. The AtkObjects would be
131 * created at the time of their implementing widget. This is how things
132 * happen in Qt and its damn sensible.
134 * In GTK Gail objects are created 'lazily' when they are accessed. This is
135 * presumably an optimization to reduce memory. I happen to think its a very
136 * very bad one. Anyway, there is no signal, and Gail objects don't get created
137 * automatically for each widget, so how do we register AtkObjects with D-Bus?
139 * Answer, we have one guaranteed AtkObject, the root. We traverse the tree provided
140 * by the root object, registering as we go. When new objects are created we use
141 * the children-changed signal of their parent to find out. As we don't know
142 * if a new object has any children that have not been registered we must traverse
143 * the decendants of every new object to find AtkObjects that have not been registered.
147 * For whatever reason events are generated for objects that have not yet been
148 * registered with D-Bus. This means that when translating an Atk signal to an
149 * AT-SPI one it may be neccessary to register objects first.
151 * The caveat is that when registering an object somewhere in the middle of the
152 * AtkObject tree there is no guarantee that its parent objects have been registered.
153 * So when registering a new object we also need to register its parents back to the
158 * The original solution was completely recursive. So when the reference of an AtkObject
159 * was requested it would be registered there and then. I didn't like the recursive
160 * solution, it was a very very deep stack in some cases.
165 * This function registers the object so that it is exported
166 * over D-Bus and schedules an update to client side cache.
169 export (GList **uplist, AtkObject *accessible)
174 g_assert(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);
182 *uplist = g_list_prepend (*uplist, accessible);
188 * Exports all the dependencies of an AtkObject.
189 * This is the subtree and the ancestors.
191 * Dependencies are the objects that get included in the
192 * cache, and therefore need to be registered before the
193 * update signal is sent.
195 * This does a depth first traversal of a subtree of AtkObject
196 * and exports them as Accessible objects if they are not exported
199 * It exports all ancestors of the object if they are not
203 export_deps (AtkObject *accessible)
205 AtkObject *current, *tmp;
207 GList *uplist = NULL;
212 /* Export subtree including object itself */
213 /*========================================*/
214 ref = atk_dbus_object_to_ref (accessible);
218 stack = g_queue_new ();
220 current = g_object_ref (accessible);
221 ref = export (&uplist, current);
222 g_queue_push_head (stack, GINT_TO_POINTER (0));
225 * The index held on the stack is the next child node
226 * that needs processing at the corresponding level in the tree.
228 while (!g_queue_is_empty (stack))
230 /* This while loop finds the next node that needs processing,
233 i = GPOINTER_TO_INT(g_queue_peek_head (stack));
235 while (i < atk_object_get_n_accessible_children (current) &&
238 tmp = atk_object_ref_accessible_child (current, i);
239 if (!atk_dbus_object_to_ref (tmp))
246 g_object_unref (G_OBJECT (tmp));
251 /* Still children to process */
253 export (&uplist, current);
254 /* Update parent nodes next child index */
255 g_queue_peek_head_link (stack)->data = GINT_TO_POINTER (i+1);
256 /* Push a new child index for the current node */
257 g_queue_push_head (stack, GINT_TO_POINTER (0));
261 /* No more children, move to parent */
263 current = atk_object_get_parent (current);
264 g_object_unref (G_OBJECT (tmp));
265 g_queue_pop_head (stack);
269 /* Export all neccessary ancestors of the object */
270 /*===============================================*/
271 current = atk_object_get_parent (accessible);
272 while (current && !atk_dbus_object_to_ref (current))
274 export (&uplist, current);
277 spi_emit_cache_update (uplist, atk_adaptor_app_data->bus);
278 g_list_free (uplist);
282 /*---------------------------------------------------------------------------*/
284 /* Called to register an AtkObject with AT-SPI and expose it over D-Bus. */
286 atk_dbus_register_accessible (AtkObject *accessible)
289 g_assert(ATK_IS_OBJECT(accessible));
291 return export_deps (accessible);
294 /* Called when an already registered object is updated in such a
295 * way that client side cache needs to be updated.
298 atk_dbus_update_accessible (AtkObject *accessible)
301 GList *uplist = NULL;
302 g_assert(ATK_IS_OBJECT(accessible));
304 ref = atk_dbus_object_to_ref (accessible);
307 uplist = g_list_prepend (uplist, accessible);
308 spi_emit_cache_update (uplist, atk_adaptor_app_data->bus);
309 g_list_free (uplist);
314 /*---------------------------------------------------------------------------*/
317 * Returns the reference of the object, or 0 if it is not exported over D-Bus.
320 atk_dbus_object_to_ref (AtkObject *accessible)
322 return GPOINTER_TO_INT(g_object_get_data (G_OBJECT (accessible), "dbus-id"));
326 * Converts the Accessible object reference to its D-Bus object path
329 atk_dbus_ref_to_path (guint ref)
331 return g_strdup_printf(ATK_BRIDGE_OBJECT_REFERENCE_TEMPLATE, ref);
335 * Used to lookup an AtkObject from its D-Bus path.
338 atk_dbus_path_to_object (const char *path)
345 if (strncmp(path, ATK_BRIDGE_OBJECT_PATH_PREFIX, ATK_BRIDGE_PATH_PREFIX_LENGTH) != 0)
348 path += ATK_BRIDGE_PATH_PREFIX_LENGTH; /* Skip over the prefix */
351 return atk_get_root();
357 data = g_hash_table_lookup (ref2ptr, GINT_TO_POINTER(index));
359 return ATK_OBJECT (data);
366 * Used to lookup a D-Bus path from the AtkObject.
369 atk_dbus_object_to_path (AtkObject *accessible)
372 g_assert(ATK_IS_OBJECT(accessible));
374 ref = atk_dbus_object_to_ref (accessible);
378 return atk_dbus_ref_to_path (ref);
381 /*---------------------------------------------------------------------------*/
384 * Initializes required global data. The update and removal lists
385 * and the reference lookup tables.
387 * Initializes all of the required D-Bus interfaces.
390 atk_dbus_initialize (AtkObject *root)
393 ref2ptr = g_hash_table_new(g_direct_hash, g_direct_equal);
395 /* Get the root accessible and add */
396 atk_dbus_register_accessible (root);
399 /*END------------------------------------------------------------------------*/