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