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"
31 #define ATK_BRIDGE_OBJECT_PATH_PREFIX "/org/freedesktop/atspi/accessible"
32 #define ATK_BRIDGE_OBJECT_REFERENCE_TEMPLATE ATK_BRIDGE_OBJECT_PATH_PREFIX "/%d"
33 #define ATK_BRIDGE_PATH_PREFIX_LENGTH 33
36 * This module is responsible for keeping track of all the AtkObjects in
37 * the application, so that they can be accessed remotely and placed in
38 * a client side cache.
40 * To access an AtkObject remotely we need to provide a D-Bus object
41 * path for it. The D-Bus object paths used have a standard prefix
42 * (ATK_BRIDGE_OBJECT_PATH_PREFIX). Appended to this prefix is a string
43 * representation of an integer reference. So to access an AtkObject
44 * remotely we keep a Hashtable that maps the given reference to
45 * the AtkObject pointer. An object in this hash table is said to be 'registered'.
47 * The architecture of AT-SPI dbus is such that AtkObjects are not
48 * remotely reference counted. This means that we need to keep track of
49 * object destruction. When an object is destroyed it must be 'deregistered'
50 * To do this lookup we keep a dbus-id attribute on each AtkObject.
52 * In addition to accessing the objects remotely we must be able to update
53 * the client side cache. This is done using the 'update' signal of the
54 * org.freedesktop.atspi.Tree interface. The update signal should send out
55 * all of the cacheable data for an Accessible object.
58 GHashTable *ref2ptr = NULL; /* Used for converting a D-Bus path (Reference) to the object pointer */
60 static guint counter = 1;
62 /*---------------------------------------------------------------------------*/
65 * Each AtkObject must be asssigned a D-Bus path (Reference)
67 * This function provides an integer reference for a new
71 assign_reference(void)
74 /* Reference of 0 not allowed as used as direct key in hash table */
80 * Returns the reference of the object, or 0 if it is not registered.
83 object_to_ref (AtkObject *accessible)
85 return GPOINTER_TO_INT(g_object_get_data (G_OBJECT (accessible), "dbus-id"));
89 * Converts the Accessible object reference to its D-Bus object path
92 ref_to_path (guint ref)
94 return g_strdup_printf(ATK_BRIDGE_OBJECT_REFERENCE_TEMPLATE, ref);
97 /*---------------------------------------------------------------------------*/
100 * Called when a registered AtkObject is deleted.
101 * Removes the AtkObject from the reference lookup tables.
104 deregister_accessible(gpointer data, GObject *accessible)
109 g_assert(ATK_IS_OBJECT(accessible));
111 ref = atk_dbus_object_to_ref (ATK_OBJECT(accessible));
115 g_hash_table_remove(ref2ptr, GINT_TO_POINTER(ref));
120 * Called to register an AtkObject with AT-SPI and expose it over D-Bus.
123 register_accessible (AtkObject *accessible)
126 g_assert(ATK_IS_OBJECT(accessible));
128 ref = assign_reference();
130 g_hash_table_insert (ref2ptr, GINT_TO_POINTER(ref), accessible);
131 g_object_set_data (G_OBJECT(accessible), "dbus-id", GINT_TO_POINTER(ref));
132 g_object_weak_ref(G_OBJECT(accessible), deregister_accessible, NULL);
135 /*---------------------------------------------------------------------------*/
137 typedef void (*ActionFunc) (AtkObject *);
139 /* Return true if action should be performed */
140 typedef gboolean (*FilterFunc) (AtkObject *);
143 * This function performs a depth first traversal of a tree of AtkObjects.
145 * It uses a FilterFunc to determine if a node needs to have an action
148 * Nodes that are filtered out become leaves and no recursion is performed on
151 * Nodes where an action was performed are returned in a GList.
154 traverse_atk_tree (AtkObject *accessible,
159 AtkObject *current, *tmp;
161 GList *uplist = NULL;
165 if (!filter (accessible))
168 stack = g_queue_new ();
169 current = g_object_ref (accessible);
171 *visited = g_list_prepend (*visited, current);
172 g_queue_push_head (stack, GINT_TO_POINTER (0));
175 * The index held on the stack is the next child node
176 * that needs processing at the corresponding level in the tree.
178 while (!g_queue_is_empty (stack))
180 /* Find the next child node that needs processing */
181 i = GPOINTER_TO_INT(g_queue_peek_head (stack));
183 while (i < atk_object_get_n_accessible_children (current) &&
186 tmp = atk_object_ref_accessible_child (current, i);
187 /* If filter function */
195 g_object_unref (G_OBJECT (tmp));
201 /* Push onto stack */
204 *visited = g_list_prepend (*visited, current);
205 g_queue_peek_head_link (stack)->data = GINT_TO_POINTER (i+1);
206 g_queue_push_head (stack, GINT_TO_POINTER (0));
212 current = atk_object_get_parent (current);
213 g_object_unref (G_OBJECT (tmp));
214 g_queue_pop_head (stack);
221 /*---------------------------------------------------------------------------*/
224 * Called when an already registered object is updated in such a
225 * way that client side cache needs to be updated.
228 update_accessible (AtkObject *accessible)
231 g_assert(ATK_IS_OBJECT(accessible));
233 ref = atk_dbus_object_to_ref (accessible);
236 spi_emit_cache_update (accessible, atk_adaptor_app_data->bus);
240 /*---------------------------------------------------------------------------*/
243 atk_dbus_foreach_registered(GHFunc func, gpointer data)
245 g_hash_table_foreach(ref2ptr, func, data);
249 * Used to lookup an AtkObject from its D-Bus path.
252 atk_dbus_path_to_object (const char *path)
259 if (strncmp(path, ATK_BRIDGE_OBJECT_PATH_PREFIX, ATK_BRIDGE_PATH_PREFIX_LENGTH) != 0)
262 path += ATK_BRIDGE_PATH_PREFIX_LENGTH; /* Skip over the prefix */
265 return atk_get_root();
271 data = g_hash_table_lookup (ref2ptr, GINT_TO_POINTER(index));
273 return ATK_OBJECT (data);
279 * Used to lookup a D-Bus path from the AtkObject.
282 atk_dbus_object_to_path (AtkObject *accessible)
285 g_assert(ATK_IS_OBJECT(accessible));
287 ref = atk_dbus_object_to_ref (accessible);
291 return atk_dbus_ref_to_path (ref);
294 /*---------------------------------------------------------------------------*/
297 * Events are not evaluated for non-registered accessibles.
299 * When a property change is made on a registered accessible
300 * the client side cache should be updated.
302 * When a parent is changed the subtree is de-registered
303 * if the parent is not attached to the root accessible.
306 tree_update_listener (GSignalInvocationHint *signal_hint,
307 guint n_param_values,
308 const GValue *param_values,
311 AtkObject *accessible;
312 AtkPropertyValues *values;
313 const gchar *pname = NULL;
315 accessible = g_value_get_object (¶m_values[0]);
316 values = (AtkPropertyValues*) g_value_get_pointer (¶m_values[1]);
318 pname = values[0].property_name;
320 if (!atk_dbus_object_to_ref (accessible))
323 if (strcmp (pname, "accessible-name") == 0 ||
324 strcmp (pname, "accessible-description"))
326 atk_dbus_update_accessible (accessible);
328 else if (strcmp (pname, "accessible-parent"))
332 ref = atk_dbus_object_to_ref;
339 * Events are not evaluated for non registered accessibles.
341 * When the children of a registered accessible are changed
342 * the subtree, rooted at the child is registered.
345 tree_update_children_listener (GSignalInvocationHint *signal_hint,
346 guint n_param_values,
347 const GValue *param_values,
350 AtkObject *accessible;
351 const gchar *detail = NULL;
353 gboolean child_needs_unref = FALSE;
355 if (signal_hint->detail)
356 detail = g_quark_to_string (signal_hint->detail);
358 accessible = g_value_get_object (¶m_values[0]);
359 if (!strcmp (detail, "add"))
362 int index = g_value_get_uint (param_values + 1);
363 child = g_value_get_pointer (param_values + 2);
365 if (ATK_IS_OBJECT (child))
366 g_object_ref (child);
368 child = atk_object_ref_accessible_child (accessible, index);
370 atk_dbus_register_subtree (child);
371 g_object_unref (child);
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 atk_dbus_register_accessible (root);
390 atk_add_global_event_listener (tree_update_listener, "Gtk:AtkObject:property-change");
391 atk_add_global_event_listener (tree_update_children_listener, "Gtk:AtkObject:children-changed");
394 /*END------------------------------------------------------------------------*/