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"
31 * Need to add concurrency support.
34 #define ATK_BRIDGE_OBJECT_PATH_PREFIX "/org/freedesktop/atspi/accessible"
35 #define ATK_BRIDGE_OBJECT_REFERENCE_TEMPLATE ATK_BRIDGE_OBJECT_PATH_PREFIX "/%d"
36 #define ATK_BRIDGE_PATH_PREFIX_LENGTH 33
39 * This module is responsible for keeping track of all the AtkObjects in
40 * the application, so that they can be accessed remotely and placed in
41 * a client side cache.
43 * To access an AtkObject remotely we need to provide a D-Bus object
44 * path for it. The D-Bus object paths used have a standard prefix
45 * (ATK_BRIDGE_OBJECT_PATH_PREFIX). Appended to this prefix is a string
46 * representation of an integer reference. So to access an AtkObject
47 * remotely we keep a Hashtable that maps the given reference to
48 * the AtkObject pointer. An object in this hash table is said to be 'registered'.
50 * The architecture of AT-SPI dbus is such that AtkObjects are not
51 * remotely reference counted. This means that we need to keep track of
52 * object destruction. When an object is destroyed it must be 'deregistered'
53 * To do this lookup we keep a dbus-id attribute on each AtkObject.
55 * In addition to accessing the objects remotely we must be able to update
56 * the client side cache. This is done using the 'update' signal of the
57 * org.freedesktop.atspi.Tree interface. The update signal should send out
58 * all of the cacheable data for each AtkObject that has changed since the
59 * last update. It should also provide a list of objects that have been
60 * removed since the last update.
62 * To enbale this two hash tables are kept. One keeps a list of AtkObjects
63 * that have been updated. The other a list of objects that have been
64 * removed since the last 'update' signal. The reason they are
65 * stored as hash tables is to ensure that if an AtkObject is removed
66 * and then added between update signals then the object is easy to delete
67 * from the 'update' list without doing a costly lookup.
69 * The mappings will be called 'reference lookup tables'
70 * The hashtable containing AtkObjects that need updating in the client side
71 * cache will be called the 'update list'
72 * The hashtable containing the AtkObjects that need removing from the client
73 * side cache will be called the 'removal list'
76 GHashTable *ref2ptr = NULL; /* Used for converting a D-Bus path (Reference) to the object pointer */
78 GHashTable *update_list = NULL; /* Stores the objects that need a client side cache update */
79 GHashTable *remove_list = NULL; /* Stores the objects that need to be removed from the client side cache */
81 static guint counter = 1;
83 /* Amazingly the ATK event callbacks dont have a user
84 * data parameter. Instead, with great sadness, we use
85 * some global data. Data is declared and initialized
88 extern SpiAppData *atk_adaptor_app_data;
90 /*---------------------------------------------------------------------------*/
93 * Each AtkObject must be asssigned a D-Bus path (Reference)
95 * This function provides an integer reference for a new
99 assign_reference(void)
102 /* Reference of 0 not allowed as used as direct key in hash table */
107 /*---------------------------------------------------------------------------*/
110 * Called when a registered AtkObject is deleted.
112 * Removes the AtkObject from the reference lookup tables.
113 * Adds the reference of the object to the removal list.
116 deregister_accessible(gpointer data, GObject *accessible)
120 g_assert(ATK_IS_OBJECT(accessible));
123 ref = GPOINTER_TO_INT(g_object_get_data (accessible, "dbus-id"));
125 /* Remove from update list */
126 g_hash_table_remove(update_list, GINT_TO_POINTER(ref));
130 g_hash_table_remove(ref2ptr, GINT_TO_POINTER(ref));
131 /* Add to removal list */
134 * Pyatspi client side exceptions have occured indicating
135 * that an object has been removed twice.
136 * This should not be possible and needs investigation.
138 g_hash_table_insert(remove_list, GINT_TO_POINTER(ref), NULL);
141 atk_tree_cache_needs_update(atk_adaptor_app_data->bus);
144 /*---------------------------------------------------------------------------*/
147 * Registers a new AtkObject.
149 * Adds the AtkObject to the reference lookup tables.
150 * Adds the AtkObject to the update list.
153 register_accessible (AtkObject *accessible)
157 g_assert(ATK_IS_OBJECT(accessible));
159 reference = assign_reference();
161 g_hash_table_insert (ref2ptr, GINT_TO_POINTER(reference), accessible);
162 g_object_set_data (G_OBJECT(accessible), "dbus-id", GINT_TO_POINTER(reference));
163 g_object_weak_ref(G_OBJECT(accessible), deregister_accessible, NULL);
165 /* Add to update list */
166 g_hash_table_insert (update_list, GINT_TO_POINTER(reference), accessible);
168 atk_tree_cache_needs_update(atk_adaptor_app_data->bus);
173 /*---------------------------------------------------------------------------*/
175 /* TODO Turn these into an iterator API - Think about the locking */
178 atk_dbus_foreach_registered(GHFunc func, gpointer data)
180 g_hash_table_foreach(ref2ptr, func, data);
183 /*---------------------------------------------------------------------------*/
186 atk_dbus_foreach_update_list(GHFunc func, gpointer data)
188 g_hash_table_foreach(update_list, func, data);
189 g_hash_table_remove_all(update_list);
192 /*---------------------------------------------------------------------------*/
195 atk_dbus_foreach_remove_list(GHFunc func, gpointer data)
197 g_hash_table_foreach(remove_list, func, data);
198 g_hash_table_remove_all(remove_list);
201 /*---------------------------------------------------------------------------*/
204 * Called on an AtkObject when it has changed in such a way
205 * that the client side cache of the object will need updating.
208 atk_dbus_notify_change(AtkObject *accessible)
211 g_assert(ATK_IS_OBJECT(accessible));
213 if (!g_object_get_data (G_OBJECT (accessible), "dbus-id"))
215 register_accessible(accessible);
219 ref = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (accessible), "dbus-id"));
220 g_hash_table_insert (update_list, GINT_TO_POINTER (ref), accessible);
221 atk_tree_cache_needs_update(atk_adaptor_app_data->bus);
225 /*---------------------------------------------------------------------------*/
228 * Used to lookup an AtkObject from its D-Bus path.
231 atk_dbus_get_object (const char *path)
238 if (strncmp(path, ATK_BRIDGE_OBJECT_PATH_PREFIX, ATK_BRIDGE_PATH_PREFIX_LENGTH) != 0)
241 path += ATK_BRIDGE_PATH_PREFIX_LENGTH; /* Skip over the prefix */
244 return atk_get_root();
250 data = g_hash_table_lookup (ref2ptr, GINT_TO_POINTER(index));
252 return ATK_OBJECT (data);
257 /*---------------------------------------------------------------------------*/
260 atk_dbus_get_path_from_ref(guint ref)
262 return g_strdup_printf(ATK_BRIDGE_OBJECT_REFERENCE_TEMPLATE, ref);
265 /*---------------------------------------------------------------------------*/
268 * Used to lookup a D-Bus path from the AtkObject.
270 * Objects without a path are registered and provided with one.
273 atk_dbus_get_path (AtkObject *accessible)
277 g_assert (accessible);
279 index = GPOINTER_TO_INT(g_object_get_data (G_OBJECT (accessible), "dbus-id"));
281 index = register_accessible(accessible);
283 return g_strdup_printf(ATK_BRIDGE_OBJECT_REFERENCE_TEMPLATE, index);
286 /*---------------------------------------------------------------------------*/
289 * Used to recursively register accessibles.
291 * When children are added to an accessible we need to
292 * iterate over the new subtree provided to register new accessibles.
295 atk_dbus_register_subtree(AtkObject *accessible)
301 ref = GPOINTER_TO_INT(g_object_get_data (G_OBJECT (accessible), "dbus-id"));
303 ref = register_accessible(accessible);
305 n_children = atk_object_get_n_accessible_children(accessible);
306 for (i=0; i < n_children; i++)
308 child = atk_object_ref_accessible_child(accessible, i);
309 atk_dbus_register_subtree(child);
314 /*---------------------------------------------------------------------------*/
317 * Marshals a single object into a D-Bus message.
319 * Unrefs the AtkObject if unref is true.
322 spi_dbus_return_object (DBusMessage *message, AtkObject *obj, gboolean unref)
327 path = atk_dbus_get_path (obj);
330 g_object_unref (obj);
332 reply = dbus_message_new_method_return (message);
335 dbus_message_append_args (reply, DBUS_TYPE_OBJECT_PATH, path,
341 /*---------------------------------------------------------------------------*/
344 * Marshals a variant containing the object reference into the message iter
347 * Unrefs the object if unref is true.
350 spi_dbus_return_v_object (DBusMessageIter *iter, AtkObject *obj, int unref)
354 path = atk_dbus_get_path (obj);
357 g_object_unref (obj);
359 return droute_return_v_object (iter, path);
362 /*---------------------------------------------------------------------------*/
365 * Initializes required global data. The update and removal lists
366 * and the reference lookup tables.
368 * Initializes all of the required D-Bus interfaces.
371 atk_dbus_initialize (AtkObject *root)
374 ref2ptr = g_hash_table_new(g_direct_hash, g_direct_equal);
376 update_list = g_hash_table_new(g_direct_hash, g_direct_equal);
378 remove_list = g_hash_table_new(g_direct_hash, g_direct_equal);
380 /* Get the root accessible and add */
381 atk_dbus_register_subtree(root);
384 /*END------------------------------------------------------------------------*/