2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2008 Novell, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
28 #include "accessible.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 /* Amazingly the ATK event callbacks dont have a user
65 * data parameter. Instead, with great sadness, we use
66 * some global data. Data is declared and initialized
70 GHashTable *ref2ptr = NULL; /* Used for converting a D-Bus path (Reference) to the object pointer */
72 static guint counter = 1;
74 extern SpiAppData *atk_adaptor_app_data;
76 /*---------------------------------------------------------------------------*/
79 * Each AtkObject must be asssigned a D-Bus path (Reference)
81 * This function provides an integer reference for a new
85 assign_reference(void)
88 /* Reference of 0 not allowed as used as direct key in hash table */
93 /*---------------------------------------------------------------------------*/
96 atk_dbus_foreach_registered(GHFunc func, gpointer data)
98 g_hash_table_foreach(ref2ptr, func, data);
101 /*---------------------------------------------------------------------------*/
104 * Called when a registered AtkObject is deleted.
105 * Removes the AtkObject from the reference lookup tables.
106 * Sets the client side cache to be updated.
109 deregister_accessible(gpointer data, GObject *accessible)
114 g_assert(ATK_IS_OBJECT(accessible));
117 ref = atk_dbus_object_to_ref (ATK_OBJECT(accessible));
121 g_hash_table_remove(ref2ptr, GINT_TO_POINTER(ref));
124 * Pyatspi client side exceptions have occured indicating
125 * that an object has been removed twice.
126 * This should not be possible and needs investigation.
128 spi_emit_cache_removal (ref, atk_adaptor_app_data->bus);
132 /*---------------------------------------------------------------------------*/
135 * This function registers the object so that it is exported
136 * over D-Bus and schedules an update to client side cache.
139 export (GList **uplist, AtkObject *accessible)
144 g_assert(ATK_IS_OBJECT(accessible));
146 ref = assign_reference();
148 g_hash_table_insert (ref2ptr, GINT_TO_POINTER(ref), accessible);
149 g_object_set_data (G_OBJECT(accessible), "dbus-id", GINT_TO_POINTER(ref));
150 g_object_weak_ref(G_OBJECT(accessible), deregister_accessible, NULL);
152 *uplist = g_list_prepend (*uplist, accessible);
158 * This does a depth first traversal of a subtree of AtkObject
159 * and exports them as Accessible objects if they are not exported
163 export_subtree (AtkObject *accessible)
165 AtkObject *current, *tmp;
167 GList *uplist = NULL;
171 stack = g_queue_new ();
173 current = g_object_ref (accessible);
174 ref = export (&uplist, current);
175 g_queue_push_head (stack, GINT_TO_POINTER (0));
178 * The index held on the stack is the next child node
179 * that needs processing at the corresponding level in the tree.
181 while (!g_queue_is_empty (stack))
183 /* This while loop finds the next node that needs processing,
186 i = GPOINTER_TO_INT(g_queue_peek_head (stack));
188 while (i < atk_object_get_n_accessible_children (current) &&
191 tmp = atk_object_ref_accessible_child (current, i);
192 if (!atk_dbus_object_to_ref (tmp))
199 g_object_unref (G_OBJECT (tmp));
204 /* Still children to process */
206 export (&uplist, current);
207 /* Update parent nodes next child index */
208 g_queue_peek_head_link (stack)->data = GINT_TO_POINTER (i+1);
209 /* Push a new child index for the current node */
210 g_queue_push_head (stack, GINT_TO_POINTER (0));
214 /* No more children, move to parent */
216 current = atk_object_get_parent (current);
217 g_object_unref (G_OBJECT (tmp));
218 g_queue_pop_head (stack);
221 spi_emit_cache_update (uplist, atk_adaptor_app_data->bus);
222 g_list_free (uplist);
226 /*---------------------------------------------------------------------------*/
228 /* Called to register an AtkObject with AT-SPI and expose it over D-Bus. */
230 atk_dbus_register_accessible (AtkObject *accessible)
233 g_assert(ATK_IS_OBJECT(accessible));
235 ref = atk_dbus_object_to_ref (accessible);
237 return export_subtree (accessible);
242 /* Called when an already registered object is updated in such a
243 * way that client side cache needs to be updated.
246 atk_dbus_update_accessible (AtkObject *accessible)
249 g_assert(ATK_IS_OBJECT(accessible));
251 ref = atk_dbus_object_to_ref (accessible);
254 spi_emit_cache_update (accessible, atk_adaptor_app_data->bus);
259 /*---------------------------------------------------------------------------*/
262 * Returns the reference of the object, or 0 if it is not exported over D-Bus.
265 atk_dbus_object_to_ref (AtkObject *accessible)
267 return GPOINTER_TO_INT(g_object_get_data (G_OBJECT (accessible), "dbus-id"));
271 * Converts the Accessible object reference to its D-Bus object path
274 atk_dbus_ref_to_path (guint ref)
276 return g_strdup_printf(ATK_BRIDGE_OBJECT_REFERENCE_TEMPLATE, ref);
280 * Used to lookup an AtkObject from its D-Bus path.
283 atk_dbus_path_to_object (const char *path)
290 if (strncmp(path, ATK_BRIDGE_OBJECT_PATH_PREFIX, ATK_BRIDGE_PATH_PREFIX_LENGTH) != 0)
293 path += ATK_BRIDGE_PATH_PREFIX_LENGTH; /* Skip over the prefix */
296 return atk_get_root();
302 data = g_hash_table_lookup (ref2ptr, GINT_TO_POINTER(index));
304 return ATK_OBJECT (data);
311 * Used to lookup a D-Bus path from the AtkObject.
314 atk_dbus_object_to_path (AtkObject *accessible)
317 g_assert(ATK_IS_OBJECT(accessible));
319 ref = atk_dbus_object_to_ref (accessible);
323 return atk_dbus_ref_to_path (ref);
326 /*---------------------------------------------------------------------------*/
329 * Marshals a single object into a D-Bus message.
331 * Unrefs the AtkObject if unref is true.
334 spi_dbus_return_object (DBusMessage *message, AtkObject *obj, gboolean unref)
339 path = atk_dbus_object_to_path (obj);
342 g_object_unref (obj);
344 reply = dbus_message_new_method_return (message);
347 dbus_message_append_args (reply, DBUS_TYPE_OBJECT_PATH, path,
353 /*---------------------------------------------------------------------------*/
356 * Marshals a variant containing the object reference into the message iter
359 * Unrefs the object if unref is true.
362 spi_dbus_return_v_object (DBusMessageIter *iter, AtkObject *obj, int unref)
366 path = atk_dbus_object_to_path (obj);
369 g_object_unref (obj);
371 return droute_return_v_object (iter, path);
374 /*---------------------------------------------------------------------------*/
377 * Initializes required global data. The update and removal lists
378 * and the reference lookup tables.
380 * Initializes all of the required D-Bus interfaces.
383 atk_dbus_initialize (AtkObject *root)
386 ref2ptr = g_hash_table_new(g_direct_hash, g_direct_equal);
388 /* Get the root accessible and add */
389 atk_dbus_register_accessible (root);
392 /*END------------------------------------------------------------------------*/