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))
159 g_debug ("CACHE REM - %s - %d - %s\n", atk_object_get_name (ATK_OBJECT (gobj)),
160 atk_object_get_role (ATK_OBJECT (gobj)),
161 spi_register_object_to_path (spi_global_register, gobj));
163 g_signal_emit (cache, cache_signals [OBJECT_REMOVED], 0, gobj);
164 g_hash_table_remove (cache->objects, gobj);
169 add_object (SpiCache * cache, GObject * gobj)
171 g_return_if_fail (G_IS_OBJECT (gobj));
173 g_hash_table_insert (cache->objects, gobj, NULL);
176 g_debug ("CACHE ADD - %s - %d - %s\n", atk_object_get_name (ATK_OBJECT (gobj)),
177 atk_object_get_role (ATK_OBJECT (gobj)),
178 spi_register_object_to_path (spi_global_register, gobj));
181 g_signal_emit (cache, cache_signals [OBJECT_ADDED], 0, gobj);
184 /*---------------------------------------------------------------------------*/
186 static GStaticRecMutex cache_mutex = G_STATIC_REC_MUTEX_INIT;
187 static GStaticMutex recursion_check_guard = G_STATIC_MUTEX_INIT;
189 static gboolean recursion_check = FALSE;
192 recursion_check_and_set ()
195 g_static_mutex_lock (&recursion_check_guard);
196 ret = recursion_check;
197 recursion_check = TRUE;
198 g_static_mutex_unlock (&recursion_check_guard);
203 recursion_check_unset ()
205 g_static_mutex_lock (&recursion_check_guard);
206 recursion_check = FALSE;
207 g_static_mutex_unlock (&recursion_check_guard);
210 /*---------------------------------------------------------------------------*/
213 append_children (AtkObject * accessible, GQueue * traversal)
217 gint count = atk_object_get_n_accessible_children (accessible);
221 for (i = 0; i < count; i++)
223 current = atk_object_ref_accessible_child (accessible, i);
226 g_queue_push_tail (traversal, current);
232 * Adds a subtree of accessible objects
233 * to the cache at the accessible object provided.
235 * The leaf nodes do not have their children
236 * registered. A node is considered a leaf
237 * if it has the state "manages-descendants"
238 * or if it has already been registered.
241 add_subtree (SpiCache *cache, AtkObject * accessible)
247 g_return_if_fail (ATK_IS_OBJECT (accessible));
249 traversal = g_queue_new ();
250 to_add = g_queue_new ();
252 g_object_ref (accessible);
253 g_queue_push_tail (traversal, accessible);
255 while (!g_queue_is_empty (traversal))
259 current = g_queue_pop_head (traversal);
260 set = atk_object_ref_state_set (current);
262 if (!atk_state_set_contains_state (set, ATK_STATE_TRANSIENT))
264 g_queue_push_tail (to_add, current);
265 if (!spi_cache_in (cache, G_OBJECT (current)) &&
266 !atk_state_set_contains_state (set, ATK_STATE_MANAGES_DESCENDANTS))
268 append_children (current, traversal);
272 g_object_unref (set);
275 while (!g_queue_is_empty (to_add))
277 current = g_queue_pop_head (to_add);
278 add_object (cache, G_OBJECT(current));
279 g_object_unref (G_OBJECT (current));
282 g_queue_free (traversal);
283 g_queue_free (to_add);
286 /*---------------------------------------------------------------------------*/
289 child_added_listener (GSignalInvocationHint * signal_hint,
290 guint n_param_values,
291 const GValue * param_values, gpointer data)
293 SpiCache *cache = spi_global_cache;
295 AtkObject *accessible;
298 const gchar *detail = NULL;
300 g_static_rec_mutex_lock (&cache_mutex);
303 * Ensure that only accessibles already in the cache
304 * have their signals processed.
306 accessible = ATK_OBJECT (g_value_get_object (¶m_values[0]));
307 g_return_val_if_fail (ATK_IS_OBJECT (accessible), TRUE);
309 if (spi_cache_in (cache, G_OBJECT(accessible)))
312 if (recursion_check_and_set ())
313 g_warning ("AT-SPI: Recursive use of cache module");
315 g_debug ("AT-SPI: Tree update listener");
317 if (signal_hint->detail)
318 detail = g_quark_to_string (signal_hint->detail);
320 if (!g_strcmp0 (detail, "add"))
323 int index = g_value_get_uint (param_values + 1);
324 child = g_value_get_pointer (param_values + 2);
326 if (!ATK_IS_OBJECT (child))
328 child = atk_object_ref_accessible_child (accessible, index);
330 add_subtree (cache, child);
333 recursion_check_unset ();
337 g_static_rec_mutex_unlock (&cache_mutex);
342 /*---------------------------------------------------------------------------*/
345 toplevel_added_listener (AtkObject * accessible,
346 guint index, AtkObject * child)
348 SpiCache *cache = spi_global_cache;
350 g_static_rec_mutex_lock (&cache_mutex);
352 g_return_if_fail (ATK_IS_OBJECT (accessible));
354 if (spi_cache_in (cache, G_OBJECT(accessible)))
357 if (recursion_check_and_set ())
358 g_warning ("AT-SPI: Recursive use of registration module");
360 g_debug ("AT-SPI: Toplevel added listener");
362 if (!ATK_IS_OBJECT (child))
364 child = atk_object_ref_accessible_child (accessible, index);
366 add_subtree (cache, child);
368 recursion_check_unset ();
372 g_static_rec_mutex_unlock (&cache_mutex);
375 /*---------------------------------------------------------------------------*/
378 spi_cache_foreach (SpiCache * cache, GHFunc func, gpointer data)
380 g_hash_table_foreach (cache->objects, func, data);
384 spi_cache_in (SpiCache * cache, GObject * object)
386 if (g_hash_table_lookup_extended (cache->objects,
397 spi_cache_print_info (GObject * obj)
399 char * path = spi_register_object_to_path (spi_global_register, obj);
401 if (spi_cache_in (spi_global_cache, obj))
402 g_printf ("%s IC\n", path);
404 g_printf ("%s NC\n", path);
411 /*END------------------------------------------------------------------------*/