4f87b1801acf8c98bc3a4cb927d27397a515c983
[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
25 #include "accessible-cache.h"
26 #include "accessible-register.h"
27 #include "bridge.h"
28
29 SpiCache *spi_global_cache = NULL;
30
31 static gboolean
32 child_added_listener (GSignalInvocationHint * signal_hint,
33                       guint n_param_values,
34                       const GValue * param_values, gpointer data);
35
36 static void
37 toplevel_added_listener (AtkObject * accessible,
38                          guint index, AtkObject * child);
39
40 static void
41 remove_object (gpointer data, GObject * gobj);
42
43 static void
44 add_object (SpiCache * cache, GObject * gobj);
45
46 static void
47 add_subtree (SpiCache *cache, AtkObject * accessible);
48
49 /*---------------------------------------------------------------------------*/
50
51 static void
52 spi_cache_finalize (GObject * object);
53
54 static void
55 spi_cache_dispose (GObject * object);
56
57 /*---------------------------------------------------------------------------*/
58
59 enum
60 {
61   OBJECT_ADDED,
62   OBJECT_REMOVED,
63   LAST_SIGNAL
64 };
65 static guint cache_signals[LAST_SIGNAL] = { 0 };
66
67 /*---------------------------------------------------------------------------*/
68
69 G_DEFINE_TYPE (SpiCache, spi_cache, G_TYPE_OBJECT)
70
71 static void spi_cache_class_init (SpiCacheClass * klass)
72 {
73   GObjectClass *object_class = (GObjectClass *) klass;
74
75   spi_cache_parent_class = g_type_class_ref (G_TYPE_OBJECT);
76
77   object_class->finalize = spi_cache_finalize;
78   object_class->dispose = spi_cache_dispose;
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
110 #ifdef SPI_ATK_DEBUG
111   if (g_thread_supported ())
112     g_message ("AT-SPI: Threads enabled");
113
114   g_debug ("AT-SPI: Initial Atk tree regisration");
115 #endif
116
117   g_signal_connect (spi_global_register,
118                     "object-deregistered",
119                     (GCallback) remove_object, cache);
120
121   add_subtree (cache, spi_global_app_data->root);
122
123   atk_add_global_event_listener (child_added_listener,
124                                  "Gtk:AtkObject:children-changed");
125
126   g_signal_connect (G_OBJECT (spi_global_app_data->root),
127                     "children-changed::add",
128                     (GCallback) toplevel_added_listener, NULL);
129 }
130
131 static void
132 spi_cache_finalize (GObject * object)
133 {
134   SpiCache *cache = SPI_CACHE (object);
135
136   g_free (cache->objects);
137
138   G_OBJECT_CLASS (spi_cache_parent_class)->finalize (object);
139 }
140
141 static void
142 spi_cache_dispose (GObject * object)
143 {
144   SpiCache *cache = SPI_CACHE (object);
145
146   G_OBJECT_CLASS (spi_cache_parent_class)->dispose (object);
147 }
148
149 /*---------------------------------------------------------------------------*/
150
151 static void
152 remove_object (gpointer data, GObject * gobj)
153 {
154   SpiCache *cache = SPI_CACHE (data);
155   
156   if (spi_cache_in (cache, gobj))
157     {
158       g_signal_emit (cache, cache_signals [OBJECT_REMOVED], 0, gobj);
159       g_hash_table_remove (cache->objects, gobj);
160     }
161 }
162
163 static void
164 add_object (SpiCache * cache, GObject * gobj)
165 {
166   g_return_if_fail (G_IS_OBJECT (gobj));
167
168   g_hash_table_insert (cache->objects, gobj, NULL);
169
170   g_signal_emit (cache, cache_signals [OBJECT_ADDED], 0, gobj);
171 }
172
173 /*---------------------------------------------------------------------------*/
174
175 static GStaticRecMutex cache_mutex        = G_STATIC_REC_MUTEX_INIT;
176 static GStaticMutex recursion_check_guard = G_STATIC_MUTEX_INIT;
177
178 static gboolean recursion_check = FALSE;
179
180 static gboolean
181 recursion_check_and_set ()
182 {
183   gboolean ret;
184   g_static_mutex_lock (&recursion_check_guard);
185   ret = recursion_check;
186   recursion_check = TRUE;
187   g_static_mutex_unlock (&recursion_check_guard);
188   return ret;
189 }
190
191 static void
192 recursion_check_unset ()
193 {
194   g_static_mutex_lock (&recursion_check_guard);
195   recursion_check = FALSE;
196   g_static_mutex_unlock (&recursion_check_guard);
197 }
198
199 /*---------------------------------------------------------------------------*/
200
201 static void
202 append_children (AtkObject * accessible, GQueue * traversal)
203 {
204   AtkObject *current;
205   guint i;
206   gint count = atk_object_get_n_accessible_children (accessible);
207
208   if (count < 0)
209     count = 0;
210   for (i = 0; i < count; i++)
211     {
212       current = atk_object_ref_accessible_child (accessible, i);
213       if (current)
214         {
215           g_queue_push_tail (traversal, current);
216         }
217     }
218 }
219
220 /*
221  * Adds a subtree of accessible objects
222  * to the cache at the accessible object provided.
223  *
224  * The leaf nodes do not have their children
225  * registered. A node is considered a leaf
226  * if it has the state "manages-descendants"
227  * or if it has already been registered.
228  */
229 static void
230 add_subtree (SpiCache *cache, AtkObject * accessible)
231 {
232   AtkObject *current;
233   GQueue *traversal;
234   GQueue *to_add;
235
236   g_return_if_fail (ATK_IS_OBJECT (accessible));
237
238   traversal = g_queue_new ();
239   to_add = g_queue_new ();
240
241   g_object_ref (accessible);
242   g_queue_push_tail (traversal, accessible);
243
244   while (!g_queue_is_empty (traversal))
245     {
246       AtkStateSet *set;
247       
248       current = g_queue_pop_head (traversal);
249       set = atk_object_ref_state_set (current);
250
251       if (!atk_state_set_contains_state (set, ATK_STATE_TRANSIENT))
252         {
253           g_queue_push_tail (to_add, current);
254           if (!spi_cache_in (cache, G_OBJECT (current)) &&
255               !atk_state_set_contains_state  (set, ATK_STATE_MANAGES_DESCENDANTS))
256             {
257 #ifdef SPI_ATK_DEBUG
258               g_debug ("REG  - %s - %d - %s", atk_object_get_name (current),
259                        atk_object_get_role (current),
260                        atk_dbus_object_to_path (current));
261 #endif
262               append_children (current, traversal);
263             }
264         }
265
266       g_object_unref (set);
267     }
268
269   while (!g_queue_is_empty (to_add))
270     {
271       current = g_queue_pop_head (to_add);
272       add_object (cache, G_OBJECT(current));
273       g_object_unref (G_OBJECT (current));
274     }
275
276   g_queue_free (traversal);
277   g_queue_free (to_add);
278 }
279
280 /*---------------------------------------------------------------------------*/
281
282 static gboolean
283 child_added_listener (GSignalInvocationHint * signal_hint,
284                       guint n_param_values,
285                       const GValue * param_values, gpointer data)
286 {
287   SpiCache *cache = spi_global_cache;
288
289   AtkObject *accessible;
290   AtkObject *child;
291
292   const gchar *detail = NULL;
293
294   g_static_rec_mutex_lock (&cache_mutex);
295
296   /* 
297    * Ensure that only accessibles already in the cache
298    * have their signals processed.
299    */
300   accessible = ATK_OBJECT (g_value_get_object (&param_values[0]));
301   g_return_val_if_fail (ATK_IS_OBJECT (accessible), TRUE);
302
303   if (spi_cache_in (cache, G_OBJECT(accessible)))
304     {
305 #ifdef SPI_ATK_DEBUG
306       if (recursion_check_and_set ())
307         g_warning ("AT-SPI: Recursive use of cache module");
308
309       g_debug ("AT-SPI: Tree update listener");
310 #endif
311       if (signal_hint->detail)
312         detail = g_quark_to_string (signal_hint->detail);
313
314       if (!strcmp (detail, "add"))
315         {
316           gpointer child;
317           int index = g_value_get_uint (param_values + 1);
318           child = g_value_get_pointer (param_values + 2);
319
320           if (!ATK_IS_OBJECT (child))
321            {
322              child = atk_object_ref_accessible_child (accessible, index);
323            }
324           add_subtree (cache, child);
325         }
326 #ifdef SPI_ATK_DEBUG
327       recursion_check_unset ();
328 #endif
329     }
330
331   g_static_rec_mutex_unlock (&cache_mutex);
332
333   return TRUE;
334 }
335
336 /*---------------------------------------------------------------------------*/
337
338 static void
339 toplevel_added_listener (AtkObject * accessible,
340                          guint index, AtkObject * child)
341 {
342   SpiCache *cache = spi_global_cache;
343
344   g_static_rec_mutex_lock (&cache_mutex);
345
346   g_return_if_fail (ATK_IS_OBJECT (accessible));
347
348   if (spi_cache_in (cache, G_OBJECT(accessible)))
349     {
350 #ifdef SPI_ATK_DEBUG
351       if (recursion_check_and_set ())
352         g_warning ("AT-SPI: Recursive use of registration module");
353
354       g_debug ("AT-SPI: Toplevel added listener");
355 #endif
356       if (!ATK_IS_OBJECT (child))
357         {
358           child = atk_object_ref_accessible_child (accessible, index);
359         }
360       add_subtree (cache, child);
361 #ifdef SPI_ATK_DEBUG
362       recursion_check_unset ();
363 #endif
364     }
365
366   g_static_rec_mutex_unlock (&cache_mutex);
367 }
368
369 /*---------------------------------------------------------------------------*/
370
371 void
372 spi_cache_foreach (SpiCache * cache, GHFunc func, gpointer data)
373 {
374   g_hash_table_foreach (cache->objects, func, data);
375 }
376
377 gboolean
378 spi_cache_in (SpiCache * cache, GObject * object)
379 {
380   if (g_hash_table_lookup_extended (cache->objects,
381                                     object,
382                                     NULL,
383                                     NULL))
384     return TRUE;
385   else
386     return FALSE;
387 }
388
389 /*END------------------------------------------------------------------------*/