128d2eb47545b90c144ab4fd8626c1c53c249e8e
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / accessible-cache.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2009, 2010 Codethink Ltd.
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #include <atk/atk.h>
24 #include <string.h>
25
26 #include "accessible-cache.h"
27 #include "accessible-register.h"
28 #include "bridge.h"
29
30 SpiCache *spi_global_cache = NULL;
31
32 static gboolean
33 child_added_listener (GSignalInvocationHint * signal_hint,
34                       guint n_param_values,
35                       const GValue * param_values, gpointer data);
36
37 static void
38 toplevel_added_listener (AtkObject * accessible,
39                          guint index, AtkObject * child);
40
41 static void
42 remove_object (GObject * source, GObject * gobj, gpointer data);
43
44 static void
45 add_object (SpiCache * cache, GObject * gobj);
46
47 static void
48 add_subtree (SpiCache *cache, AtkObject * accessible);
49
50 static gboolean
51 add_pending_items (gpointer data);
52
53 /*---------------------------------------------------------------------------*/
54
55 static void
56 spi_cache_finalize (GObject * object);
57
58 /*---------------------------------------------------------------------------*/
59
60 enum
61 {
62   OBJECT_ADDED,
63   OBJECT_REMOVED,
64   LAST_SIGNAL
65 };
66 static guint cache_signals[LAST_SIGNAL] = { 0 };
67
68 /*---------------------------------------------------------------------------*/
69
70 G_DEFINE_TYPE (SpiCache, spi_cache, G_TYPE_OBJECT)
71
72 static void spi_cache_class_init (SpiCacheClass * klass)
73 {
74   GObjectClass *object_class = (GObjectClass *) klass;
75
76   spi_cache_parent_class = g_type_class_ref (G_TYPE_OBJECT);
77
78   object_class->finalize = spi_cache_finalize;
79
80   cache_signals [OBJECT_ADDED] = \
81       g_signal_new ("object-added",
82                     SPI_CACHE_TYPE,
83                     G_SIGNAL_ACTION,
84                     0,
85                     NULL,
86                     NULL,
87                     g_cclosure_marshal_VOID__OBJECT,
88                     G_TYPE_NONE,
89                     1,
90                     G_TYPE_OBJECT);
91
92   cache_signals [OBJECT_REMOVED] = \
93       g_signal_new ("object-removed",
94                     SPI_CACHE_TYPE,
95                     G_SIGNAL_ACTION,
96                     0,
97                     NULL,
98                     NULL,
99                     g_cclosure_marshal_VOID__OBJECT,
100                     G_TYPE_NONE,
101                     1,
102                     G_TYPE_OBJECT);
103 }
104
105 static void
106 spi_cache_init (SpiCache * cache)
107 {
108   cache->objects = g_hash_table_new (g_direct_hash, g_direct_equal);
109   cache->add_traversal = g_queue_new ();
110
111 #ifdef SPI_ATK_DEBUG
112   if (g_thread_supported ())
113     g_message ("AT-SPI: Threads enabled");
114
115   g_debug ("AT-SPI: Initial Atk tree regisration");
116 #endif
117
118   g_signal_connect (spi_global_register,
119                     "object-deregistered",
120                     (GCallback) remove_object, cache);
121
122   add_subtree (cache, spi_global_app_data->root);
123
124   cache->child_added_listener = atk_add_global_event_listener (child_added_listener,
125                                                                "Gtk:AtkObject:children-changed"); 
126
127   g_signal_connect (G_OBJECT (spi_global_app_data->root),
128                     "children-changed::add",
129                     (GCallback) toplevel_added_listener, NULL);
130 }
131
132 static void
133 spi_cache_finalize (GObject * object)
134 {
135   SpiCache *cache = SPI_CACHE (object);
136
137   while (!g_queue_is_empty (cache->add_traversal))
138     g_object_unref (G_OBJECT (g_queue_pop_head (cache->add_traversal)));
139   g_queue_free (cache->add_traversal);
140   g_hash_table_unref (cache->objects);
141
142   g_signal_handlers_disconnect_by_func (spi_global_register,
143                                         (GCallback) remove_object, cache);
144
145   g_signal_handlers_disconnect_by_func (G_OBJECT (spi_global_app_data->root),
146                                         (GCallback) toplevel_added_listener, NULL);
147
148   atk_remove_global_event_listener (cache->child_added_listener);
149
150   G_OBJECT_CLASS (spi_cache_parent_class)->finalize (object);
151 }
152
153 /*---------------------------------------------------------------------------*/
154
155 static void
156 remove_object (GObject * source, GObject * gobj, gpointer data)
157 {
158   SpiCache *cache = SPI_CACHE (data);
159   
160   if (spi_cache_in (cache, gobj))
161     {
162 #ifdef SPI_ATK_DEBUG
163   g_debug ("CACHE REM - %s - %d - %s\n", atk_object_get_name (ATK_OBJECT (gobj)),
164             atk_object_get_role (ATK_OBJECT (gobj)),
165             spi_register_object_to_path (spi_global_register, gobj));
166 #endif
167       g_signal_emit (cache, cache_signals [OBJECT_REMOVED], 0, gobj);
168       g_hash_table_remove (cache->objects, gobj);
169     }
170   else if (g_queue_remove (cache->add_traversal, gobj))
171     {
172       g_object_unref (gobj);
173     }
174 }
175
176 static void
177 add_object (SpiCache * cache, GObject * gobj)
178 {
179   g_return_if_fail (G_IS_OBJECT (gobj));
180
181   g_hash_table_insert (cache->objects, gobj, NULL);
182
183 #ifdef SPI_ATK_DEBUG
184   g_debug ("CACHE ADD - %s - %d - %s\n", atk_object_get_name (ATK_OBJECT (gobj)),
185             atk_object_get_role (ATK_OBJECT (gobj)),
186             spi_register_object_to_path (spi_global_register, gobj));
187 #endif
188
189   g_signal_emit (cache, cache_signals [OBJECT_ADDED], 0, gobj);
190 }
191
192 /*---------------------------------------------------------------------------*/
193
194 static GRecMutex cache_mutex;
195
196 #ifdef SPI_ATK_DEBUG
197 static GStaticMutex recursion_check_guard = G_STATIC_MUTEX_INIT;
198 static gboolean recursion_check = FALSE;
199
200 static gboolean
201 recursion_check_and_set ()
202 {
203   gboolean ret;
204   g_static_mutex_lock (&recursion_check_guard);
205   ret = recursion_check;
206   recursion_check = TRUE;
207   g_static_mutex_unlock (&recursion_check_guard);
208   return ret;
209 }
210
211 static void
212 recursion_check_unset ()
213 {
214   g_static_mutex_lock (&recursion_check_guard);
215   recursion_check = FALSE;
216   g_static_mutex_unlock (&recursion_check_guard);
217 }
218 #endif /* SPI_ATK_DEBUG */
219
220 /*---------------------------------------------------------------------------*/
221
222 static void
223 append_children (AtkObject * accessible, GQueue * traversal)
224 {
225   AtkObject *current;
226   guint i;
227   gint count = atk_object_get_n_accessible_children (accessible);
228
229   if (count < 0)
230     count = 0;
231   for (i = 0; i < count; i++)
232     {
233       current = atk_object_ref_accessible_child (accessible, i);
234       if (current)
235         {
236           g_queue_push_tail (traversal, current);
237         }
238     }
239 }
240
241 /*
242  * Adds a subtree of accessible objects
243  * to the cache at the accessible object provided.
244  *
245  * The leaf nodes do not have their children
246  * registered. A node is considered a leaf
247  * if it has the state "manages-descendants"
248  * or if it has already been registered.
249  */
250 static void
251 add_subtree (SpiCache *cache, AtkObject * accessible)
252 {
253   g_return_if_fail (ATK_IS_OBJECT (accessible));
254
255   g_object_ref (accessible);
256   g_queue_push_tail (cache->add_traversal, accessible);
257   add_pending_items (cache);
258 }
259
260 static gboolean
261 add_pending_items (gpointer data)
262 {
263   SpiCache *cache = SPI_CACHE (data);
264   AtkObject *current;
265   GQueue *to_add;
266
267   to_add = g_queue_new ();
268
269   while (!g_queue_is_empty (cache->add_traversal))
270     {
271       AtkStateSet *set;
272
273       /* cache->add_traversal holds a ref to current */
274       current = g_queue_pop_head (cache->add_traversal);
275       set = atk_object_ref_state_set (current);
276
277       if (set && !atk_state_set_contains_state (set, ATK_STATE_TRANSIENT))
278         {
279           /* transfer the ref into to_add */
280           g_queue_push_tail (to_add, current);
281           if (!spi_cache_in (cache, G_OBJECT (current)) &&
282               !atk_state_set_contains_state  (set, ATK_STATE_MANAGES_DESCENDANTS) &&
283               !atk_state_set_contains_state  (set, ATK_STATE_DEFUNCT))
284             {
285               append_children (current, cache->add_traversal);
286             }
287         }
288       else
289         {
290           /* drop the ref for the removed object */
291           g_object_unref (current);
292         }
293
294       if (set)
295         g_object_unref (set);
296     }
297
298   while (!g_queue_is_empty (to_add))
299     {
300       current = g_queue_pop_head (to_add);
301
302       /* Make sure object is registerd so we are notified if it goes away */
303       g_free (spi_register_object_to_path (spi_global_register,
304               G_OBJECT (current)));
305
306       add_object (cache, G_OBJECT(current));
307       g_object_unref (G_OBJECT (current));
308     }
309
310   g_queue_free (to_add);
311   cache->add_pending_idle = 0;
312   return FALSE;
313 }
314
315 /*---------------------------------------------------------------------------*/
316
317 static gboolean
318 child_added_listener (GSignalInvocationHint * signal_hint,
319                       guint n_param_values,
320                       const GValue * param_values, gpointer data)
321 {
322   SpiCache *cache = spi_global_cache;
323   AtkObject *accessible;
324
325   const gchar *detail = NULL;
326
327   g_rec_mutex_lock (&cache_mutex);
328
329   /* 
330    * Ensure that only accessibles already in the cache
331    * have their signals processed.
332    */
333   accessible = ATK_OBJECT (g_value_get_object (&param_values[0]));
334   g_return_val_if_fail (ATK_IS_OBJECT (accessible), TRUE);
335
336   if (spi_cache_in (cache, G_OBJECT(accessible)))
337     {
338 #ifdef SPI_ATK_DEBUG
339       if (recursion_check_and_set ())
340         g_warning ("AT-SPI: Recursive use of cache module");
341
342       g_debug ("AT-SPI: Tree update listener");
343 #endif
344       if (signal_hint->detail)
345         detail = g_quark_to_string (signal_hint->detail);
346
347       if (detail && !strncmp (detail, "add", 3))
348         {
349           gpointer child;
350           int index = g_value_get_uint (param_values + 1);
351           child = g_value_get_pointer (param_values + 2);
352           if (!child)
353             {
354               g_rec_mutex_unlock (&cache_mutex);
355               return;
356             }
357
358           g_object_ref (child);
359           g_queue_push_tail (cache->add_traversal, child);
360
361           if (cache->add_pending_idle == 0)
362             cache->add_pending_idle = g_idle_add (add_pending_items, cache);
363         }
364 #ifdef SPI_ATK_DEBUG
365       recursion_check_unset ();
366 #endif
367     }
368
369   g_rec_mutex_unlock (&cache_mutex);
370
371   return TRUE;
372 }
373
374 /*---------------------------------------------------------------------------*/
375
376 static void
377 toplevel_added_listener (AtkObject * accessible,
378                          guint index, AtkObject * child)
379 {
380   SpiCache *cache = spi_global_cache;
381
382   g_rec_mutex_lock (&cache_mutex);
383
384   g_return_if_fail (ATK_IS_OBJECT (accessible));
385
386   if (spi_cache_in (cache, G_OBJECT(accessible)))
387     {
388 #ifdef SPI_ATK_DEBUG
389       if (recursion_check_and_set ())
390         g_warning ("AT-SPI: Recursive use of registration module");
391
392       g_debug ("AT-SPI: Toplevel added listener");
393 #endif
394       if (!ATK_IS_OBJECT (child))
395         {
396           child = atk_object_ref_accessible_child (accessible, index);
397         }
398       else
399         g_object_ref (child);
400
401       g_queue_push_tail (cache->add_traversal, child);
402
403       if (cache->add_pending_idle == 0)
404         cache->add_pending_idle = g_idle_add (add_pending_items, cache);
405 #ifdef SPI_ATK_DEBUG
406       recursion_check_unset ();
407 #endif
408     }
409
410   g_rec_mutex_unlock (&cache_mutex);
411 }
412
413 /*---------------------------------------------------------------------------*/
414
415 void
416 spi_cache_foreach (SpiCache * cache, GHFunc func, gpointer data)
417 {
418   g_hash_table_foreach (cache->objects, func, data);
419 }
420
421 gboolean
422 spi_cache_in (SpiCache * cache, GObject * object)
423 {
424   if (!cache)
425     return FALSE;
426
427   if (g_hash_table_lookup_extended (cache->objects,
428                                     object,
429                                     NULL,
430                                     NULL))
431     return TRUE;
432   else
433     return FALSE;
434 }
435
436 #ifdef SPI_ATK_DEBUG
437 void
438 spi_cache_print_info (GObject * obj)
439 {
440   char * path = spi_register_object_to_path (spi_global_register, obj);
441  
442   if (spi_cache_in (spi_global_cache, obj))
443       g_printf ("%s IC\n", path);
444   else
445       g_printf ("%s NC\n", path);
446
447   if (path)
448       g_free (path);
449 }
450 #endif
451
452 /*END------------------------------------------------------------------------*/