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.
26 #include "accessible-cache.h"
27 #include "accessible-register.h"
30 SpiCache *spi_global_cache = NULL;
33 child_added_listener (GSignalInvocationHint * signal_hint,
35 const GValue * param_values, gpointer data);
38 toplevel_added_listener (AtkObject * accessible,
39 guint index, AtkObject * child);
42 remove_object (GObject * source, GObject * gobj, gpointer data);
45 add_object (SpiCache * cache, GObject * gobj);
48 add_subtree (SpiCache *cache, AtkObject * accessible);
51 add_pending_items (gpointer data);
53 /*---------------------------------------------------------------------------*/
56 spi_cache_finalize (GObject * object);
59 spi_cache_dispose (GObject * object);
61 /*---------------------------------------------------------------------------*/
69 static guint cache_signals[LAST_SIGNAL] = { 0 };
71 /*---------------------------------------------------------------------------*/
73 G_DEFINE_TYPE (SpiCache, spi_cache, G_TYPE_OBJECT)
75 static void spi_cache_class_init (SpiCacheClass * klass)
77 GObjectClass *object_class = (GObjectClass *) klass;
79 spi_cache_parent_class = g_type_class_ref (G_TYPE_OBJECT);
81 object_class->finalize = spi_cache_finalize;
82 object_class->dispose = spi_cache_dispose;
84 cache_signals [OBJECT_ADDED] = \
85 g_signal_new ("object-added",
91 g_cclosure_marshal_VOID__OBJECT,
96 cache_signals [OBJECT_REMOVED] = \
97 g_signal_new ("object-removed",
103 g_cclosure_marshal_VOID__OBJECT,
110 spi_cache_init (SpiCache * cache)
112 cache->objects = g_hash_table_new (g_direct_hash, g_direct_equal);
113 cache->add_traversal = g_queue_new ();
116 if (g_thread_supported ())
117 g_message ("AT-SPI: Threads enabled");
119 g_debug ("AT-SPI: Initial Atk tree regisration");
122 g_signal_connect (spi_global_register,
123 "object-deregistered",
124 (GCallback) remove_object, cache);
126 add_subtree (cache, spi_global_app_data->root);
128 cache->child_added_listener = atk_add_global_event_listener (child_added_listener,
129 "Gtk:AtkObject:children-changed");
131 g_signal_connect (G_OBJECT (spi_global_app_data->root),
132 "children-changed::add",
133 (GCallback) toplevel_added_listener, NULL);
137 spi_cache_finalize (GObject * object)
139 SpiCache *cache = SPI_CACHE (object);
141 while (!g_queue_is_empty (cache->add_traversal))
142 g_object_unref (G_OBJECT (g_queue_pop_head (cache->add_traversal)));
143 g_queue_free (cache->add_traversal);
144 g_hash_table_unref (cache->objects);
146 g_signal_handlers_disconnect_by_func (spi_global_register,
147 (GCallback) remove_object, cache);
149 g_signal_handlers_disconnect_by_func (G_OBJECT (spi_global_app_data->root),
150 (GCallback) toplevel_added_listener, NULL);
152 atk_remove_global_event_listener (cache->child_added_listener);
154 G_OBJECT_CLASS (spi_cache_parent_class)->finalize (object);
158 spi_cache_dispose (GObject * object)
160 SpiCache *cache = SPI_CACHE (object);
162 G_OBJECT_CLASS (spi_cache_parent_class)->dispose (object);
165 /*---------------------------------------------------------------------------*/
168 remove_object (GObject * source, GObject * gobj, gpointer data)
170 SpiCache *cache = SPI_CACHE (data);
172 if (spi_cache_in (cache, gobj))
175 g_debug ("CACHE REM - %s - %d - %s\n", atk_object_get_name (ATK_OBJECT (gobj)),
176 atk_object_get_role (ATK_OBJECT (gobj)),
177 spi_register_object_to_path (spi_global_register, gobj));
179 g_signal_emit (cache, cache_signals [OBJECT_REMOVED], 0, gobj);
180 g_hash_table_remove (cache->objects, gobj);
184 g_queue_remove (cache->add_traversal, gobj);
185 g_object_unref (gobj);
190 add_object (SpiCache * cache, GObject * gobj)
192 g_return_if_fail (G_IS_OBJECT (gobj));
194 g_hash_table_insert (cache->objects, gobj, NULL);
197 g_debug ("CACHE ADD - %s - %d - %s\n", atk_object_get_name (ATK_OBJECT (gobj)),
198 atk_object_get_role (ATK_OBJECT (gobj)),
199 spi_register_object_to_path (spi_global_register, gobj));
202 g_signal_emit (cache, cache_signals [OBJECT_ADDED], 0, gobj);
205 /*---------------------------------------------------------------------------*/
207 static GStaticRecMutex cache_mutex = G_STATIC_REC_MUTEX_INIT;
208 static GStaticMutex recursion_check_guard = G_STATIC_MUTEX_INIT;
210 static gboolean recursion_check = FALSE;
213 recursion_check_and_set ()
216 g_static_mutex_lock (&recursion_check_guard);
217 ret = recursion_check;
218 recursion_check = TRUE;
219 g_static_mutex_unlock (&recursion_check_guard);
224 recursion_check_unset ()
226 g_static_mutex_lock (&recursion_check_guard);
227 recursion_check = FALSE;
228 g_static_mutex_unlock (&recursion_check_guard);
231 /*---------------------------------------------------------------------------*/
234 append_children (AtkObject * accessible, GQueue * traversal)
238 gint count = atk_object_get_n_accessible_children (accessible);
242 for (i = 0; i < count; i++)
244 current = atk_object_ref_accessible_child (accessible, i);
247 g_queue_push_tail (traversal, current);
253 * Adds a subtree of accessible objects
254 * to the cache at the accessible object provided.
256 * The leaf nodes do not have their children
257 * registered. A node is considered a leaf
258 * if it has the state "manages-descendants"
259 * or if it has already been registered.
262 add_subtree (SpiCache *cache, AtkObject * accessible)
264 g_return_if_fail (ATK_IS_OBJECT (accessible));
266 g_object_ref (accessible);
267 g_queue_push_tail (cache->add_traversal, accessible);
268 add_pending_items (cache);
272 add_pending_items (gpointer data)
274 SpiCache *cache = SPI_CACHE (data);
278 to_add = g_queue_new ();
280 while (!g_queue_is_empty (cache->add_traversal))
284 /* cache->add_traversal holds a ref to current */
285 current = g_queue_pop_head (cache->add_traversal);
286 set = atk_object_ref_state_set (current);
288 if (set && !atk_state_set_contains_state (set, ATK_STATE_TRANSIENT))
290 /* transfer the ref into to_add */
291 g_queue_push_tail (to_add, current);
292 if (!spi_cache_in (cache, G_OBJECT (current)) &&
293 !atk_state_set_contains_state (set, ATK_STATE_MANAGES_DESCENDANTS) &&
294 !atk_state_set_contains_state (set, ATK_STATE_DEFUNCT))
296 append_children (current, cache->add_traversal);
301 /* drop the ref for the removed object */
302 g_object_unref (current);
306 g_object_unref (set);
309 while (!g_queue_is_empty (to_add))
311 current = g_queue_pop_head (to_add);
313 /* Make sure object is registerd so we are notified if it goes away */
314 g_free (spi_register_object_to_path (spi_global_register,
315 G_OBJECT (current)));
317 add_object (cache, G_OBJECT(current));
318 g_object_unref (G_OBJECT (current));
321 g_queue_free (to_add);
322 cache->add_pending_idle = 0;
326 /*---------------------------------------------------------------------------*/
329 child_added_listener (GSignalInvocationHint * signal_hint,
330 guint n_param_values,
331 const GValue * param_values, gpointer data)
333 SpiCache *cache = spi_global_cache;
334 AtkObject *accessible;
336 const gchar *detail = NULL;
338 g_static_rec_mutex_lock (&cache_mutex);
341 * Ensure that only accessibles already in the cache
342 * have their signals processed.
344 accessible = ATK_OBJECT (g_value_get_object (¶m_values[0]));
345 g_return_val_if_fail (ATK_IS_OBJECT (accessible), TRUE);
347 if (spi_cache_in (cache, G_OBJECT(accessible)))
350 if (recursion_check_and_set ())
351 g_warning ("AT-SPI: Recursive use of cache module");
353 g_debug ("AT-SPI: Tree update listener");
355 if (signal_hint->detail)
356 detail = g_quark_to_string (signal_hint->detail);
358 if (detail && !strncmp (detail, "add", 3))
361 int index = g_value_get_uint (param_values + 1);
362 child = g_value_get_pointer (param_values + 2);
365 g_static_rec_mutex_unlock (&cache_mutex);
369 g_object_ref (child);
370 g_queue_push_tail (cache->add_traversal, child);
372 if (cache->add_pending_idle == 0)
373 cache->add_pending_idle = g_idle_add (add_pending_items, cache);
376 recursion_check_unset ();
380 g_static_rec_mutex_unlock (&cache_mutex);
385 /*---------------------------------------------------------------------------*/
388 toplevel_added_listener (AtkObject * accessible,
389 guint index, AtkObject * child)
391 SpiCache *cache = spi_global_cache;
393 g_static_rec_mutex_lock (&cache_mutex);
395 g_return_if_fail (ATK_IS_OBJECT (accessible));
397 if (spi_cache_in (cache, G_OBJECT(accessible)))
400 if (recursion_check_and_set ())
401 g_warning ("AT-SPI: Recursive use of registration module");
403 g_debug ("AT-SPI: Toplevel added listener");
405 if (!ATK_IS_OBJECT (child))
407 child = atk_object_ref_accessible_child (accessible, index);
410 g_object_ref (child);
412 g_queue_push_tail (cache->add_traversal, child);
414 if (cache->add_pending_idle == 0)
415 cache->add_pending_idle = g_idle_add (add_pending_items, cache);
417 recursion_check_unset ();
421 g_static_rec_mutex_unlock (&cache_mutex);
424 /*---------------------------------------------------------------------------*/
427 spi_cache_foreach (SpiCache * cache, GHFunc func, gpointer data)
429 g_hash_table_foreach (cache->objects, func, data);
433 spi_cache_in (SpiCache * cache, GObject * object)
438 if (g_hash_table_lookup_extended (cache->objects,
449 spi_cache_print_info (GObject * obj)
451 char * path = spi_register_object_to_path (spi_global_register, obj);
453 if (spi_cache_in (spi_global_cache, obj))
454 g_printf ("%s IC\n", path);
456 g_printf ("%s NC\n", path);
463 /*END------------------------------------------------------------------------*/