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 g_print("dbg: init cache\n");
113 cache->objects = g_hash_table_new (g_direct_hash, g_direct_equal);
114 cache->add_traversal = g_queue_new ();
117 if (g_thread_supported ())
118 g_message ("AT-SPI: Threads enabled");
120 g_debug ("AT-SPI: Initial Atk tree regisration");
123 g_signal_connect (spi_global_register,
124 "object-deregistered",
125 (GCallback) remove_object, cache);
127 add_subtree (cache, spi_global_app_data->root);
129 cache->child_added_listener = atk_add_global_event_listener (child_added_listener,
130 "Gtk:AtkObject:children-changed");
132 g_signal_connect (G_OBJECT (spi_global_app_data->root),
133 "children-changed::add",
134 (GCallback) toplevel_added_listener, NULL);
138 spi_cache_finalize (GObject * object)
140 SpiCache *cache = SPI_CACHE (object);
142 while (!g_queue_is_empty (cache->add_traversal))
143 g_object_unref (G_OBJECT (g_queue_pop_head (cache->add_traversal)));
144 g_queue_free (cache->add_traversal);
145 g_hash_table_unref (cache->objects);
147 g_signal_handlers_disconnect_by_func (spi_global_register,
148 (GCallback) remove_object, cache);
150 g_signal_handlers_disconnect_by_func (G_OBJECT (spi_global_app_data->root),
151 (GCallback) toplevel_added_listener, NULL);
153 atk_remove_global_event_listener (cache->child_added_listener);
155 G_OBJECT_CLASS (spi_cache_parent_class)->finalize (object);
159 spi_cache_dispose (GObject * object)
161 SpiCache *cache = SPI_CACHE (object);
163 G_OBJECT_CLASS (spi_cache_parent_class)->dispose (object);
166 /*---------------------------------------------------------------------------*/
169 remove_object (GObject * source, GObject * gobj, gpointer data)
171 SpiCache *cache = SPI_CACHE (data);
173 if (spi_cache_in (cache, gobj))
176 g_debug ("CACHE REM - %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));
180 g_signal_emit (cache, cache_signals [OBJECT_REMOVED], 0, gobj);
181 g_hash_table_remove (cache->objects, gobj);
184 g_queue_remove (cache->add_traversal, gobj);
188 add_object (SpiCache * cache, GObject * gobj)
190 g_return_if_fail (G_IS_OBJECT (gobj));
192 g_hash_table_insert (cache->objects, gobj, NULL);
195 g_debug ("CACHE ADD - %s - %d - %s\n", atk_object_get_name (ATK_OBJECT (gobj)),
196 atk_object_get_role (ATK_OBJECT (gobj)),
197 spi_register_object_to_path (spi_global_register, gobj));
200 g_signal_emit (cache, cache_signals [OBJECT_ADDED], 0, gobj);
203 /*---------------------------------------------------------------------------*/
205 static GStaticRecMutex cache_mutex = G_STATIC_REC_MUTEX_INIT;
206 static GStaticMutex recursion_check_guard = G_STATIC_MUTEX_INIT;
208 static gboolean recursion_check = FALSE;
211 recursion_check_and_set ()
214 g_static_mutex_lock (&recursion_check_guard);
215 ret = recursion_check;
216 recursion_check = TRUE;
217 g_static_mutex_unlock (&recursion_check_guard);
222 recursion_check_unset ()
224 g_static_mutex_lock (&recursion_check_guard);
225 recursion_check = FALSE;
226 g_static_mutex_unlock (&recursion_check_guard);
229 /*---------------------------------------------------------------------------*/
232 append_children (AtkObject * accessible, GQueue * traversal)
236 gint count = atk_object_get_n_accessible_children (accessible);
240 for (i = 0; i < count; i++)
242 current = atk_object_ref_accessible_child (accessible, i);
245 g_queue_push_tail (traversal, current);
251 * Adds a subtree of accessible objects
252 * to the cache at the accessible object provided.
254 * The leaf nodes do not have their children
255 * registered. A node is considered a leaf
256 * if it has the state "manages-descendants"
257 * or if it has already been registered.
260 add_subtree (SpiCache *cache, AtkObject * accessible)
262 g_return_if_fail (ATK_IS_OBJECT (accessible));
264 g_object_ref (accessible);
265 g_queue_push_tail (cache->add_traversal, accessible);
266 add_pending_items (cache);
270 add_pending_items (gpointer data)
272 SpiCache *cache = SPI_CACHE (data);
276 to_add = g_queue_new ();
278 while (!g_queue_is_empty (cache->add_traversal))
282 current = g_queue_pop_head (cache->add_traversal);
283 set = atk_object_ref_state_set (current);
285 if (set && !atk_state_set_contains_state (set, ATK_STATE_TRANSIENT))
287 g_queue_push_tail (to_add, current);
288 if (!spi_cache_in (cache, G_OBJECT (current)) &&
289 !atk_state_set_contains_state (set, ATK_STATE_MANAGES_DESCENDANTS) &&
290 !atk_state_set_contains_state (set, ATK_STATE_DEFUNCT))
292 append_children (current, cache->add_traversal);
297 g_object_unref (set);
300 while (!g_queue_is_empty (to_add))
302 current = g_queue_pop_head (to_add);
304 /* Make sure object is registerd so we are notified if it goes away */
305 g_free (spi_register_object_to_path (spi_global_register,
306 G_OBJECT (current)));
308 add_object (cache, G_OBJECT(current));
309 g_object_unref (G_OBJECT (current));
312 g_queue_free (to_add);
313 cache->add_pending_idle = 0;
317 /*---------------------------------------------------------------------------*/
320 child_added_listener (GSignalInvocationHint * signal_hint,
321 guint n_param_values,
322 const GValue * param_values, gpointer data)
324 SpiCache *cache = spi_global_cache;
325 AtkObject *accessible;
327 const gchar *detail = NULL;
329 g_static_rec_mutex_lock (&cache_mutex);
332 * Ensure that only accessibles already in the cache
333 * have their signals processed.
335 accessible = ATK_OBJECT (g_value_get_object (¶m_values[0]));
336 g_return_val_if_fail (ATK_IS_OBJECT (accessible), TRUE);
338 if (spi_cache_in (cache, G_OBJECT(accessible)))
341 if (recursion_check_and_set ())
342 g_warning ("AT-SPI: Recursive use of cache module");
344 g_debug ("AT-SPI: Tree update listener");
346 if (signal_hint->detail)
347 detail = g_quark_to_string (signal_hint->detail);
349 if (detail && !strncmp (detail, "add", 3))
352 int index = g_value_get_uint (param_values + 1);
353 child = g_value_get_pointer (param_values + 2);
356 g_static_rec_mutex_unlock (&cache_mutex);
360 g_object_ref (child);
361 g_queue_push_tail (cache->add_traversal, child);
363 if (cache->add_pending_idle == 0)
364 cache->add_pending_idle = g_idle_add (add_pending_items, cache);
367 recursion_check_unset ();
371 g_static_rec_mutex_unlock (&cache_mutex);
376 /*---------------------------------------------------------------------------*/
379 toplevel_added_listener (AtkObject * accessible,
380 guint index, AtkObject * child)
382 SpiCache *cache = spi_global_cache;
384 g_static_rec_mutex_lock (&cache_mutex);
386 g_return_if_fail (ATK_IS_OBJECT (accessible));
388 if (spi_cache_in (cache, G_OBJECT(accessible)))
391 if (recursion_check_and_set ())
392 g_warning ("AT-SPI: Recursive use of registration module");
394 g_debug ("AT-SPI: Toplevel added listener");
396 if (!ATK_IS_OBJECT (child))
398 child = atk_object_ref_accessible_child (accessible, index);
401 g_object_ref (child);
403 g_queue_push_tail (cache->add_traversal, child);
405 if (cache->add_pending_idle == 0)
406 cache->add_pending_idle = g_idle_add (add_pending_items, cache);
408 recursion_check_unset ();
412 g_static_rec_mutex_unlock (&cache_mutex);
415 /*---------------------------------------------------------------------------*/
418 spi_cache_foreach (SpiCache * cache, GHFunc func, gpointer data)
420 g_hash_table_foreach (cache->objects, func, data);
424 spi_cache_in (SpiCache * cache, GObject * object)
429 if (g_hash_table_lookup_extended (cache->objects,
440 spi_cache_print_info (GObject * obj)
442 char * path = spi_register_object_to_path (spi_global_register, obj);
444 if (spi_cache_in (spi_global_cache, obj))
445 g_printf ("%s IC\n", path);
447 g_printf ("%s NC\n", path);
454 /*END------------------------------------------------------------------------*/