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 each AtkObject that has changed since the
60 * last update. It should also provide a list of objects that have been
61 * removed since the last update.
64 GHashTable *ref2ptr = NULL; /* Used for converting a D-Bus path (Reference) to the object pointer */
66 static guint counter = 1;
68 /*---------------------------------------------------------------------------*/
71 * Each AtkObject must be asssigned a D-Bus path (Reference)
73 * This function provides an integer reference for a new
77 assign_reference(void)
80 /* Reference of 0 not allowed as used as direct key in hash table */
85 /*---------------------------------------------------------------------------*/
88 atk_dbus_foreach_registered(GHFunc func, gpointer data)
90 g_hash_table_foreach(ref2ptr, func, data);
93 /*---------------------------------------------------------------------------*/
96 * Called when a registered AtkObject is deleted.
97 * Removes the AtkObject from the reference lookup tables.
98 * Sets the client side cache to be updated.
101 deregister_accessible(gpointer data, GObject *accessible)
106 g_assert(ATK_IS_OBJECT(accessible));
109 ref = atk_dbus_object_to_ref (ATK_OBJECT(accessible));
113 g_hash_table_remove(ref2ptr, GINT_TO_POINTER(ref));
116 * Pyatspi client side exceptions have occured indicating
117 * that an object has been removed twice.
118 * This should not be possible and needs investigation.
120 spi_emit_cache_removal (ref, atk_adaptor_app_data->bus);
124 /*---------------------------------------------------------------------------*/
127 * This function registers the object so that it is exported
128 * over D-Bus and schedules an update to client side cache.
131 export (GList **uplist, AtkObject *accessible)
136 g_assert(ATK_IS_OBJECT(accessible));
138 ref = assign_reference();
140 g_hash_table_insert (ref2ptr, GINT_TO_POINTER(ref), accessible);
141 g_object_set_data (G_OBJECT(accessible), "dbus-id", GINT_TO_POINTER(ref));
142 g_object_weak_ref(G_OBJECT(accessible), deregister_accessible, NULL);
144 *uplist = g_list_prepend (*uplist, accessible);
150 * This does a depth first traversal of a subtree of AtkObject
151 * and exports them as Accessible objects if they are not exported
155 export_subtree (AtkObject *accessible)
157 AtkObject *current, *tmp;
159 GList *uplist = NULL;
163 stack = g_queue_new ();
165 current = g_object_ref (accessible);
166 ref = export (&uplist, current);
167 g_queue_push_head (stack, GINT_TO_POINTER (0));
170 * The index held on the stack is the next child node
171 * that needs processing at the corresponding level in the tree.
173 while (!g_queue_is_empty (stack))
175 /* This while loop finds the next node that needs processing,
178 i = GPOINTER_TO_INT(g_queue_peek_head (stack));
180 while (i < atk_object_get_n_accessible_children (current) &&
183 tmp = atk_object_ref_accessible_child (current, i);
184 if (!atk_dbus_object_to_ref (tmp))
191 g_object_unref (G_OBJECT (tmp));
196 /* Still children to process */
198 export (&uplist, current);
199 /* Update parent nodes next child index */
200 g_queue_peek_head_link (stack)->data = GINT_TO_POINTER (i+1);
201 /* Push a new child index for the current node */
202 g_queue_push_head (stack, GINT_TO_POINTER (0));
206 /* No more children, move to parent */
208 current = atk_object_get_parent (current);
209 g_object_unref (G_OBJECT (tmp));
210 g_queue_pop_head (stack);
213 spi_emit_cache_update (uplist, atk_adaptor_app_data->bus);
214 g_list_free (uplist);
218 /*---------------------------------------------------------------------------*/
220 /* Called to register an AtkObject with AT-SPI and expose it over D-Bus. */
222 atk_dbus_register_accessible (AtkObject *accessible)
225 g_assert(ATK_IS_OBJECT(accessible));
227 ref = atk_dbus_object_to_ref (accessible);
229 return export_subtree (accessible);
234 /* Called when an already registered object is updated in such a
235 * way that client side cache needs to be updated.
238 atk_dbus_update_accessible (AtkObject *accessible)
241 g_assert(ATK_IS_OBJECT(accessible));
243 ref = atk_dbus_object_to_ref (accessible);
246 spi_emit_cache_update (accessible, atk_adaptor_app_data->bus);
251 /*---------------------------------------------------------------------------*/
254 * Returns the reference of the object, or 0 if it is not exported over D-Bus.
257 atk_dbus_object_to_ref (AtkObject *accessible)
259 return GPOINTER_TO_INT(g_object_get_data (G_OBJECT (accessible), "dbus-id"));
263 * Converts the Accessible object reference to its D-Bus object path
266 atk_dbus_ref_to_path (guint ref)
268 return g_strdup_printf(ATK_BRIDGE_OBJECT_REFERENCE_TEMPLATE, ref);
272 * Used to lookup an AtkObject from its D-Bus path.
275 atk_dbus_path_to_object (const char *path)
282 if (strncmp(path, ATK_BRIDGE_OBJECT_PATH_PREFIX, ATK_BRIDGE_PATH_PREFIX_LENGTH) != 0)
285 path += ATK_BRIDGE_PATH_PREFIX_LENGTH; /* Skip over the prefix */
288 return atk_get_root();
294 data = g_hash_table_lookup (ref2ptr, GINT_TO_POINTER(index));
296 return ATK_OBJECT (data);
303 * Used to lookup a D-Bus path from the AtkObject.
306 atk_dbus_object_to_path (AtkObject *accessible)
309 g_assert(ATK_IS_OBJECT(accessible));
311 ref = atk_dbus_object_to_ref (accessible);
315 return atk_dbus_ref_to_path (ref);
318 /*---------------------------------------------------------------------------*/
321 * Initializes required global data. The update and removal lists
322 * and the reference lookup tables.
324 * Initializes all of the required D-Bus interfaces.
327 atk_dbus_initialize (AtkObject *root)
330 ref2ptr = g_hash_table_new(g_direct_hash, g_direct_equal);
332 /* Get the root accessible and add */
333 atk_dbus_register_accessible (root);
336 /*END------------------------------------------------------------------------*/