Fixed subtle error in string comparison for tree-update signal.
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / accessible-register.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2008 Novell, Inc.
6  * Copyright 2008, 2009 Codethink Ltd.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "bridge.h"
29 #include "accessible-register.h"
30
31 #define ATK_BRIDGE_OBJECT_PATH_PREFIX "/org/freedesktop/atspi/accessible"
32 #define ATK_BRIDGE_OBJECT_REFERENCE_TEMPLATE ATK_BRIDGE_OBJECT_PATH_PREFIX "/%d"
33 #define ATK_BRIDGE_PATH_PREFIX_LENGTH 33
34
35 /*
36  * This module is responsible for keeping track of all the AtkObjects in
37  * the application, so that they can be accessed remotely and placed in
38  * a client side cache.
39  *
40  * To access an AtkObject remotely we need to provide a D-Bus object 
41  * path for it. The D-Bus object paths used have a standard prefix
42  * (ATK_BRIDGE_OBJECT_PATH_PREFIX). Appended to this prefix is a string
43  * representation of an integer reference. So to access an AtkObject 
44  * remotely we keep a Hashtable that maps the given reference to 
45  * the AtkObject pointer. An object in this hash table is said to be 'registered'.
46  *
47  * The architecture of AT-SPI dbus is such that AtkObjects are not
48  * remotely reference counted. This means that we need to keep track of
49  * object destruction. When an object is destroyed it must be 'deregistered'
50  * To do this lookup we keep a dbus-id attribute on each AtkObject.
51  *
52  * In addition to accessing the objects remotely we must be able to update
53  * the client side cache. This is done using the 'update' signal of the 
54  * org.freedesktop.atspi.Tree interface. The update signal should send out
55  * all of the cacheable data for an Accessible object.
56  *
57  */
58
59 /*
60  * FIXME
61  *
62  * While traversing the ATK tree we may modify it unintentionally.
63  * This is either a bug in the Gail implementation or this module.
64  * If a change is caused that recurses, via a signal into this module
65  * we should catch it.
66  *
67  * Things could also be changed that do not cause signal emission,
68  * but do cause a failure. Not sure what these would be.
69  *
70  * The other option is that there are threads that modify the GUI.
71  * This IS A BUG IN THE PROGRAM. But it may happen. If seeing very 
72  * odd bugs change this to take the GDK lock. Just to make sure.
73  */
74
75 static GHashTable *ref2ptr = NULL; /* Used for converting a D-Bus path (Reference) to the object pointer */
76
77 static guint counter = 1;
78
79 static GStaticRecMutex registration_mutex = G_STATIC_REC_MUTEX_INIT;
80
81 /*---------------------------------------------------------------------------*/
82
83 static GStaticMutex   recursion_check_guard = G_STATIC_MUTEX_INIT;
84 static gboolean       recursion_check = FALSE;
85
86 static gboolean
87 recursion_check_and_set ()
88 {
89   gboolean ret;
90   g_static_mutex_lock   (&recursion_check_guard);
91   ret = recursion_check;
92   recursion_check = TRUE;
93   g_static_mutex_unlock (&recursion_check_guard);
94   return ret;
95 }
96
97 static void
98 recursion_check_unset ()
99 {
100   g_static_mutex_lock   (&recursion_check_guard);
101   recursion_check = FALSE;
102   g_static_mutex_unlock (&recursion_check_guard);
103 }
104
105 /*---------------------------------------------------------------------------*/
106
107 /*
108  * Each AtkObject must be asssigned a D-Bus path (Reference)
109  *
110  * This function provides an integer reference for a new
111  * AtkObject.
112  */
113 static guint
114 assign_reference(void)
115 {
116   counter++;
117   /* Reference of 0 not allowed as used as direct key in hash table */
118   if (counter == 0)
119     counter++;
120 }
121
122 /*
123  * Returns the reference of the object, or 0 if it is not registered.
124  */
125 static guint
126 object_to_ref (AtkObject *accessible)
127 {
128   return GPOINTER_TO_INT(g_object_get_data (G_OBJECT (accessible), "dbus-id"));
129 }
130
131 /*
132  * Converts the Accessible object reference to its D-Bus object path
133  */
134 static gchar *
135 ref_to_path (guint ref)
136 {
137   return g_strdup_printf(ATK_BRIDGE_OBJECT_REFERENCE_TEMPLATE, ref);
138 }
139
140 /*---------------------------------------------------------------------------*/
141
142 /*
143  * Callback for when a registered AtkObject is destroyed.
144  *
145  * Removes the AtkObject from the reference lookup tables, meaning
146  * it is no longer exposed over D-Bus.
147  */
148 static void
149 deregister_accessible (gpointer data, GObject *accessible)
150 {
151   guint ref;
152   g_assert (ATK_IS_OBJECT (accessible));
153
154   ref = object_to_ref (ATK_OBJECT(accessible));
155   if (ref != 0)
156     {
157       g_hash_table_remove(ref2ptr, GINT_TO_POINTER(ref));
158     }
159 }
160
161 /*
162  * Called to register an AtkObject with AT-SPI and expose it over D-Bus.
163  */
164 static void
165 register_accessible (AtkObject *accessible)
166 {
167   guint ref;
168   g_assert(ATK_IS_OBJECT(accessible));
169
170   ref = assign_reference();
171
172   g_hash_table_insert (ref2ptr, GINT_TO_POINTER(ref), accessible);
173   g_object_set_data (G_OBJECT(accessible), "dbus-id", GINT_TO_POINTER(ref));
174   g_object_weak_ref(G_OBJECT(accessible), deregister_accessible, NULL);
175 }
176
177 /*---------------------------------------------------------------------------*/
178
179 /*
180  * This function checks that the ref-count of an accessible
181  * is greater than 1.
182  *
183  * There is not currently any remote reference counting
184  * in AT-SPI D-Bus so objects that are remotely owned are not
185  * allowed.
186  *
187  * TODO Add debug wrapper
188  */
189 static gboolean
190 non_owned_accessible (AtkObject *accessible)
191 {
192    if ((G_OBJECT (accessible))->ref_count <= 1)
193      {
194        g_warning ("AT-SPI: Child referenced that is not owned by its parent");
195        return TRUE;
196      }
197    else
198      {
199        return FALSE;
200      }
201 }
202
203 /*---------------------------------------------------------------------------*/
204
205 static gboolean
206 has_manages_descendants (AtkObject *accessible)
207 {
208    AtkStateSet *state;
209    gboolean result = FALSE;
210
211    /* This is dangerous, refing the state set
212     * seems to do wierd things to the tree & cause recursion
213     * by modifying the tree alot.
214     */
215    state = atk_object_ref_state_set (accessible);
216    if (atk_state_set_contains_state (state, ATK_STATE_MANAGES_DESCENDANTS))
217      {
218        g_warning ("AT-SPI: Object with 'Manages descendants' states not currently handled by AT-SPI");
219        result = TRUE;
220      }
221    g_object_unref (state);
222
223    return result;
224 }
225
226 /*
227  * Registers a subtree of accessible objects
228  * rooted at the accessible object provided.
229  *
230  * The leaf nodes do not have their children
231  * registered. A node is considered a leaf
232  * if it has the state "manages-descendants"
233  * or if it has already been registered.
234  */
235 void
236 register_subtree (AtkObject *accessible)
237 {
238   AtkObject *current, *tmp;
239   GQueue    *stack;
240   guint      i;
241   gboolean   recurse;
242
243
244   current = g_object_ref (accessible);
245   if (has_manages_descendants (current))
246     {
247       g_object_unref (current);
248       return;
249     }
250
251   stack = g_queue_new ();
252
253   register_accessible (current);
254   g_queue_push_head (stack, GINT_TO_POINTER (0));
255
256   /*
257    * The index held on the stack is the next child node
258    * that needs processing at the corresponding level in the tree.
259    */
260   while (!g_queue_is_empty (stack))
261     {
262       /* Find the next child node that needs processing */
263
264       i = GPOINTER_TO_INT(g_queue_peek_head (stack));
265       recurse = FALSE;
266
267       while (i < atk_object_get_n_accessible_children (current) &&
268              recurse == FALSE)
269         {
270           tmp = atk_object_ref_accessible_child (current, i);
271
272           /* TODO Add debug wrapper */
273           non_owned_accessible (tmp);
274
275           if (object_to_ref (tmp))
276             {
277               /* If its already registered, just update */
278               spi_emit_cache_update (tmp, atk_adaptor_app_data->bus);
279             }
280           else if (has_manages_descendants (tmp))
281             {
282               /* If it has manages descendants, just register and update */
283               register_accessible (tmp);
284               spi_emit_cache_update (tmp, atk_adaptor_app_data->bus);
285             }
286           else
287             {
288               recurse = TRUE;
289             }
290
291           if (!recurse)
292             {
293               g_object_unref (G_OBJECT (tmp));
294             }
295
296           i++;
297         }
298
299       if (recurse)
300         {
301           /* Push onto stack */
302           current = tmp;
303           register_accessible (current);
304
305           g_queue_peek_head_link (stack)->data = GINT_TO_POINTER (i);
306           g_queue_push_head (stack, GINT_TO_POINTER (0));
307         }
308       else
309         {
310           /* Pop from stack */
311           spi_emit_cache_update (current, atk_adaptor_app_data->bus);
312           tmp = current;
313           current = atk_object_get_parent (current);
314           g_object_unref (G_OBJECT (tmp));
315           g_queue_pop_head (stack);
316         }
317     }
318     g_queue_free (stack);
319 }
320
321 /*---------------------------------------------------------------------------*/
322
323 /*
324  * Called when an already registered object is updated in such a
325  * way that client side cache needs to be updated.
326  */
327 static void
328 update_accessible (AtkObject *accessible)
329 {
330   guint  ref = 0;
331   g_assert(ATK_IS_OBJECT(accessible));
332
333   ref = object_to_ref (accessible);
334   if (ref)
335     {
336       spi_emit_cache_update (accessible, atk_adaptor_app_data->bus);
337     }
338 }
339
340 /*---------------------------------------------------------------------------*/
341
342 void
343 atk_dbus_foreach_registered(GHFunc func, gpointer data)
344 {
345   g_hash_table_foreach(ref2ptr, func, data);
346 }
347
348 /*
349  * Used to lookup an AtkObject from its D-Bus path.
350  */
351 AtkObject *
352 atk_dbus_path_to_object (const char *path)
353 {
354   guint index;
355   void *data;
356
357   g_assert (path);
358
359   if (strncmp(path, ATK_BRIDGE_OBJECT_PATH_PREFIX, ATK_BRIDGE_PATH_PREFIX_LENGTH) != 0) 
360     return NULL;
361
362   path += ATK_BRIDGE_PATH_PREFIX_LENGTH; /* Skip over the prefix */
363
364   if (path[0] == '\0')
365      return atk_get_root();
366   if (path[0] != '/')
367      return NULL;
368
369   path++;
370   index = atoi (path);
371   data = g_hash_table_lookup (ref2ptr, GINT_TO_POINTER(index));
372   if (data)
373     return ATK_OBJECT (data);
374   else
375     return NULL;
376 }
377
378 /*
379  * Used to lookup a D-Bus path from the AtkObject.
380  */
381 gchar *
382 atk_dbus_object_to_path (AtkObject *accessible)
383 {
384   guint ref;
385
386   ref = object_to_ref (accessible);
387   if (!ref)
388       return NULL;
389   else
390       return ref_to_path (ref);
391 }
392
393 /*---------------------------------------------------------------------------*/
394
395 /*
396  * Events are not evaluated for non-registered accessibles.
397  *
398  * When a property change is made on a registered accessible
399  * the client side cache should be updated.
400  *
401  * When a parent is changed the subtree is de-registered
402  * if the parent is not attached to the root accessible.
403  */
404 static gboolean
405 tree_update_listener (GSignalInvocationHint *signal_hint,
406                       guint                  n_param_values,
407                       const GValue          *param_values,
408                       gpointer               data)
409 {
410   AtkObject *accessible;
411   AtkPropertyValues *values;
412   const gchar *pname = NULL;
413
414   g_static_rec_mutex_lock (&registration_mutex);
415
416   /* Ensure that only registered accessibles
417    * have their signals processed.
418    */
419   accessible = g_value_get_object (&param_values[0]);
420   g_assert (ATK_IS_OBJECT (accessible));
421
422   if (object_to_ref (accessible))
423     {
424       /* TODO Add debug wrapper */
425       if (recursion_check_and_set ())
426           g_warning ("AT-SPI: Recursive use of registration module");
427
428       values = (AtkPropertyValues*) g_value_get_pointer (&param_values[1]);
429       pname = values[0].property_name;
430       if (strcmp (pname, "accessible-name") == 0 ||
431           strcmp (pname, "accessible-description") == 0)
432         {
433           update_accessible (accessible);
434         }
435       /* Parent value us updated by child-add signal of parent object */
436
437       recursion_check_unset ();
438     }
439
440   g_static_rec_mutex_unlock (&registration_mutex);
441
442   return TRUE;
443 }
444
445 /*
446  * Events are not evaluated for non registered accessibles.
447  *
448  * When the children of a registered accessible are changed
449  * the subtree, rooted at the child is registered.
450  */
451 static gboolean
452 tree_update_children_listener (GSignalInvocationHint *signal_hint,
453                                guint                  n_param_values,
454                                const GValue          *param_values,
455                                gpointer               data)
456 {
457   AtkObject *accessible;
458   const gchar *detail = NULL;
459   AtkObject *child;
460
461   g_static_rec_mutex_lock (&registration_mutex);
462
463   /* Ensure that only registered accessibles
464    * have their signals processed.
465    */
466   accessible = g_value_get_object (&param_values[0]);
467   g_assert (ATK_IS_OBJECT (accessible));
468
469   if (object_to_ref (accessible))
470     {
471       /* TODO Add debug wrapper */
472       if (recursion_check_and_set ())
473           g_warning ("AT-SPI: Recursive use of registration module");
474
475       if (signal_hint->detail)
476           detail = g_quark_to_string (signal_hint->detail);
477
478       if (!strcmp (detail, "add"))
479         {
480           gpointer child;
481           int index = g_value_get_uint (param_values + 1);
482           child = g_value_get_pointer (param_values + 2);
483
484           if (!ATK_IS_OBJECT (child))
485             {
486               child = atk_object_ref_accessible_child (accessible, index);
487               /* TODO Add debug wrapper */
488               non_owned_accessible (child);
489             }
490           register_subtree (child);
491         }
492
493       recursion_check_unset ();
494     }
495
496   g_static_rec_mutex_unlock (&registration_mutex);
497
498   return TRUE;
499 }
500
501 /*
502  * Initializes required global data. The update and removal lists
503  * and the reference lookup tables.
504  *
505  * Initializes all of the required D-Bus interfaces.
506  */
507 void
508 atk_dbus_initialize (AtkObject *root)
509 {
510   if (!ref2ptr)
511     ref2ptr = g_hash_table_new(g_direct_hash, g_direct_equal);
512
513   if (g_thread_supported ())
514       g_message ("AT-SPI: Threads enabled");
515
516   register_subtree (root);
517
518   atk_add_global_event_listener (tree_update_listener, "Gtk:AtkObject:property-change");
519   atk_add_global_event_listener (tree_update_children_listener, "Gtk:AtkObject:children-changed");
520 }
521
522 /*END------------------------------------------------------------------------*/
523