2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2009, 2010 Codethink Ltd.
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.
25 #include "accessible-cache.h"
26 #include "accessible-register.h"
29 SpiCache *spi_global_cache = NULL;
32 child_added_listener (GSignalInvocationHint * signal_hint,
34 const GValue * param_values, gpointer data);
37 toplevel_added_listener (AtkObject * accessible,
38 guint index, AtkObject * child);
41 remove_object (GObject * source, GObject * gobj, gpointer data);
44 add_object (SpiCache * cache, GObject * gobj);
47 add_subtree (SpiCache *cache, AtkObject * accessible);
49 /*---------------------------------------------------------------------------*/
52 spi_cache_finalize (GObject * object);
55 spi_cache_dispose (GObject * object);
57 /*---------------------------------------------------------------------------*/
65 static guint cache_signals[LAST_SIGNAL] = { 0 };
67 /*---------------------------------------------------------------------------*/
69 G_DEFINE_TYPE (SpiCache, spi_cache, G_TYPE_OBJECT)
71 static void spi_cache_class_init (SpiCacheClass * klass)
73 GObjectClass *object_class = (GObjectClass *) klass;
75 spi_cache_parent_class = g_type_class_ref (G_TYPE_OBJECT);
77 object_class->finalize = spi_cache_finalize;
78 object_class->dispose = spi_cache_dispose;
80 cache_signals [OBJECT_ADDED] = \
81 g_signal_new ("object-added",
87 g_cclosure_marshal_VOID__OBJECT,
92 cache_signals [OBJECT_REMOVED] = \
93 g_signal_new ("object-removed",
99 g_cclosure_marshal_VOID__OBJECT,
106 spi_cache_init (SpiCache * cache)
108 cache->objects = g_hash_table_new (g_direct_hash, g_direct_equal);
111 if (g_thread_supported ())
112 g_message ("AT-SPI: Threads enabled");
114 g_debug ("AT-SPI: Initial Atk tree regisration");
117 g_signal_connect (spi_global_register,
118 "object-deregistered",
119 (GCallback) remove_object, cache);
121 add_subtree (cache, spi_global_app_data->root);
123 atk_add_global_event_listener (child_added_listener,
124 "Gtk:AtkObject:children-changed");
126 g_signal_connect (G_OBJECT (spi_global_app_data->root),
127 "children-changed::add",
128 (GCallback) toplevel_added_listener, NULL);
132 spi_cache_finalize (GObject * object)
134 SpiCache *cache = SPI_CACHE (object);
136 g_free (cache->objects);
138 G_OBJECT_CLASS (spi_cache_parent_class)->finalize (object);
142 spi_cache_dispose (GObject * object)
144 SpiCache *cache = SPI_CACHE (object);
146 G_OBJECT_CLASS (spi_cache_parent_class)->dispose (object);
149 /*---------------------------------------------------------------------------*/
152 remove_object (GObject * source, GObject * gobj, gpointer data)
154 SpiCache *cache = SPI_CACHE (data);
156 if (spi_cache_in (cache, gobj))
158 g_signal_emit (cache, cache_signals [OBJECT_REMOVED], 0, gobj);
159 g_hash_table_remove (cache->objects, gobj);
164 add_object (SpiCache * cache, GObject * gobj)
166 g_return_if_fail (G_IS_OBJECT (gobj));
168 g_hash_table_insert (cache->objects, gobj, NULL);
171 g_debug ("CACHE - %s - %d - %s", atk_object_get_name (ATK_OBJECT (gobj)),
172 atk_object_get_role (ATK_OBJECT (gobj)),
173 spi_register_object_to_path (spi_global_register, gobj));
176 g_signal_emit (cache, cache_signals [OBJECT_ADDED], 0, gobj);
179 /*---------------------------------------------------------------------------*/
181 static GStaticRecMutex cache_mutex = G_STATIC_REC_MUTEX_INIT;
182 static GStaticMutex recursion_check_guard = G_STATIC_MUTEX_INIT;
184 static gboolean recursion_check = FALSE;
187 recursion_check_and_set ()
190 g_static_mutex_lock (&recursion_check_guard);
191 ret = recursion_check;
192 recursion_check = TRUE;
193 g_static_mutex_unlock (&recursion_check_guard);
198 recursion_check_unset ()
200 g_static_mutex_lock (&recursion_check_guard);
201 recursion_check = FALSE;
202 g_static_mutex_unlock (&recursion_check_guard);
205 /*---------------------------------------------------------------------------*/
208 append_children (AtkObject * accessible, GQueue * traversal)
212 gint count = atk_object_get_n_accessible_children (accessible);
216 for (i = 0; i < count; i++)
218 current = atk_object_ref_accessible_child (accessible, i);
221 g_queue_push_tail (traversal, current);
227 * Adds a subtree of accessible objects
228 * to the cache at the accessible object provided.
230 * The leaf nodes do not have their children
231 * registered. A node is considered a leaf
232 * if it has the state "manages-descendants"
233 * or if it has already been registered.
236 add_subtree (SpiCache *cache, AtkObject * accessible)
242 g_return_if_fail (ATK_IS_OBJECT (accessible));
244 traversal = g_queue_new ();
245 to_add = g_queue_new ();
247 g_object_ref (accessible);
248 g_queue_push_tail (traversal, accessible);
250 while (!g_queue_is_empty (traversal))
254 current = g_queue_pop_head (traversal);
255 set = atk_object_ref_state_set (current);
257 if (!atk_state_set_contains_state (set, ATK_STATE_TRANSIENT))
259 g_queue_push_tail (to_add, current);
260 if (!spi_cache_in (cache, G_OBJECT (current)) &&
261 !atk_state_set_contains_state (set, ATK_STATE_MANAGES_DESCENDANTS))
263 append_children (current, traversal);
267 g_object_unref (set);
270 while (!g_queue_is_empty (to_add))
272 current = g_queue_pop_head (to_add);
273 add_object (cache, G_OBJECT(current));
274 g_object_unref (G_OBJECT (current));
277 g_queue_free (traversal);
278 g_queue_free (to_add);
281 /*---------------------------------------------------------------------------*/
284 child_added_listener (GSignalInvocationHint * signal_hint,
285 guint n_param_values,
286 const GValue * param_values, gpointer data)
288 SpiCache *cache = spi_global_cache;
290 AtkObject *accessible;
293 const gchar *detail = NULL;
295 g_static_rec_mutex_lock (&cache_mutex);
298 * Ensure that only accessibles already in the cache
299 * have their signals processed.
301 accessible = ATK_OBJECT (g_value_get_object (¶m_values[0]));
302 g_return_val_if_fail (ATK_IS_OBJECT (accessible), TRUE);
304 if (spi_cache_in (cache, G_OBJECT(accessible)))
307 if (recursion_check_and_set ())
308 g_warning ("AT-SPI: Recursive use of cache module");
310 g_debug ("AT-SPI: Tree update listener");
312 if (signal_hint->detail)
313 detail = g_quark_to_string (signal_hint->detail);
315 if (!g_strcmp0 (detail, "add"))
318 int index = g_value_get_uint (param_values + 1);
319 child = g_value_get_pointer (param_values + 2);
321 if (!ATK_IS_OBJECT (child))
323 child = atk_object_ref_accessible_child (accessible, index);
325 add_subtree (cache, child);
328 recursion_check_unset ();
332 g_static_rec_mutex_unlock (&cache_mutex);
337 /*---------------------------------------------------------------------------*/
340 toplevel_added_listener (AtkObject * accessible,
341 guint index, AtkObject * child)
343 SpiCache *cache = spi_global_cache;
345 g_static_rec_mutex_lock (&cache_mutex);
347 g_return_if_fail (ATK_IS_OBJECT (accessible));
349 if (spi_cache_in (cache, G_OBJECT(accessible)))
352 if (recursion_check_and_set ())
353 g_warning ("AT-SPI: Recursive use of registration module");
355 g_debug ("AT-SPI: Toplevel added listener");
357 if (!ATK_IS_OBJECT (child))
359 child = atk_object_ref_accessible_child (accessible, index);
361 add_subtree (cache, child);
363 recursion_check_unset ();
367 g_static_rec_mutex_unlock (&cache_mutex);
370 /*---------------------------------------------------------------------------*/
373 spi_cache_foreach (SpiCache * cache, GHFunc func, gpointer data)
375 g_hash_table_foreach (cache->objects, func, data);
379 spi_cache_in (SpiCache * cache, GObject * object)
381 if (g_hash_table_lookup_extended (cache->objects,
392 spi_cache_print_info (GObject * obj)
394 char * path = spi_register_object_to_path (spi_global_register, obj);
396 if (spi_cache_in (spi_global_cache, obj))
397 g_printf ("%s IC\n", path);
399 g_printf ("%s NC\n", path);
406 /*END------------------------------------------------------------------------*/