2009-02-19 Mark Doffman <mark.doffman@codethink.co.uk>
[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 /*
32  * This module is responsible for keeping track of all the AtkObjects in
33  * the application, so that they can be accessed remotely and placed in
34  * a client side cache.
35  *
36  * To access an AtkObject remotely we need to provide a D-Bus object 
37  * path for it. The D-Bus object paths used have a standard prefix
38  * (SPI_ATK_OBJECT_PATH_PREFIX). Appended to this prefix is a string
39  * representation of an integer reference. So to access an AtkObject 
40  * remotely we keep a Hashtable that maps the given reference to 
41  * the AtkObject pointer. An object in this hash table is said to be 'registered'.
42  *
43  * The architecture of AT-SPI dbus is such that AtkObjects are not
44  * remotely reference counted. This means that we need to keep track of
45  * object destruction. When an object is destroyed it must be 'deregistered'
46  * To do this lookup we keep a dbus-id attribute on each AtkObject.
47  *
48  * In addition to accessing the objects remotely we must be able to update
49  * the client side cache. This is done using the 'update' signal of the 
50  * org.freedesktop.atspi.Tree interface. The update signal should send out
51  * all of the cacheable data for an Accessible object.
52  *
53  */
54
55 /*
56  * FIXME
57  *
58  * This code seems very brittle.
59  * I would prefer changes to be made to
60  * gail and the ATK interface so that all Accessible
61  * objects are registered with an exporting module.
62  *
63  * This is the same system as Qt has with the QAccessibleBridge
64  * and QAccessibleBridgePlugin. It entails some rather
65  * large structural changes to ATK though:
66  *
67  * Removing infinite spaces (Child access no longer references child).
68  * Removing lazy creation of accessible objects.
69  */
70
71 #define SPI_ATK_OBJECT_PATH_PREFIX  "/org/freedesktop/atspi/accessible"
72 #define SPI_ATK_OBJECT_PATH_DESKTOP "/root"
73
74 #define SPI_ATK_PATH_PREFIX_LENGTH 33
75 #define SPI_ATK_OBJECT_REFERENCE_TEMPLATE SPI_ATK_OBJECT_PATH_PREFIX "/%d"
76
77
78 static GHashTable *ref2ptr = NULL; /* Used for converting a D-Bus path (Reference) to the object pointer */
79
80 static guint reference_counter = 0;
81
82 static GStaticRecMutex registration_mutex = G_STATIC_REC_MUTEX_INIT;
83
84 /*---------------------------------------------------------------------------*/
85
86 static GStaticMutex   recursion_check_guard = G_STATIC_MUTEX_INIT;
87 static gboolean       recursion_check = FALSE;
88
89 static gboolean
90 recursion_check_and_set ()
91 {
92   gboolean ret;
93   g_static_mutex_lock   (&recursion_check_guard);
94   ret = recursion_check;
95   recursion_check = TRUE;
96   g_static_mutex_unlock (&recursion_check_guard);
97   return ret;
98 }
99
100 static void
101 recursion_check_unset ()
102 {
103   g_static_mutex_lock   (&recursion_check_guard);
104   recursion_check = FALSE;
105   g_static_mutex_unlock (&recursion_check_guard);
106 }
107
108 /*---------------------------------------------------------------------------*/
109
110 /*
111  * Each AtkObject must be asssigned a D-Bus path (Reference)
112  *
113  * This function provides an integer reference for a new
114  * AtkObject.
115  */
116 static guint
117 assign_reference(void)
118 {
119   reference_counter++;
120   /* Reference of 0 not allowed as used as direct key in hash table */
121   if (reference_counter == 0)
122     reference_counter++;
123   return reference_counter;
124 }
125
126 /*
127  * Returns the reference of the object, or 0 if it is not registered.
128  */
129 static guint
130 object_to_ref (AtkObject *accessible)
131 {
132   return GPOINTER_TO_INT(g_object_get_data (G_OBJECT (accessible), "dbus-id"));
133 }
134
135 /*
136  * Converts the Accessible object reference to its D-Bus object path
137  */
138 static gchar *
139 ref_to_path (guint ref)
140 {
141   return g_strdup_printf(SPI_ATK_OBJECT_REFERENCE_TEMPLATE, ref);
142 }
143
144 /*---------------------------------------------------------------------------*/
145
146 /*
147  * Callback for when a registered AtkObject is destroyed.
148  *
149  * Removes the AtkObject from the reference lookup tables, meaning
150  * it is no longer exposed over D-Bus.
151  */
152 static void
153 deregister_accessible (gpointer data, GObject *accessible)
154 {
155   guint ref;
156   g_return_if_fail (ATK_IS_OBJECT (accessible));
157
158   ref = object_to_ref (ATK_OBJECT(accessible));
159   if (ref != 0)
160     {
161       g_hash_table_remove(ref2ptr, GINT_TO_POINTER(ref));
162     }
163 }
164
165 /*
166  * Called to register an AtkObject with AT-SPI and expose it over D-Bus.
167  */
168 static void
169 register_accessible (AtkObject *accessible)
170 {
171   guint ref;
172   g_return_if_fail (ATK_IS_OBJECT(accessible));
173
174   ref = assign_reference();
175
176   g_hash_table_insert (ref2ptr, GINT_TO_POINTER(ref), accessible);
177   g_object_set_data (G_OBJECT(accessible), "dbus-id", GINT_TO_POINTER(ref));
178   g_object_weak_ref(G_OBJECT(accessible), deregister_accessible, NULL);
179 }
180
181 /*---------------------------------------------------------------------------*/
182
183 #ifdef SPI_ATK_DEBUG
184 /*
185  * This function checks that the ref-count of an accessible
186  * is greater than 1.
187  *
188  * There is not currently any remote reference counting
189  * in AT-SPI D-Bus so objects that are remotely owned are not
190  * allowed.
191  *
192  * TODO Add debug wrapper
193  */
194 static gboolean
195 non_owned_accessible (AtkObject *accessible)
196 {
197    if ((G_OBJECT (accessible))->ref_count <= 1)
198      {
199        g_warning ("AT-SPI: Child referenced that is not owned by its parent");
200        return TRUE;
201      }
202    else
203      {
204        return FALSE;
205      }
206 }
207 #endif /* SPI_ATK_DEBUG */
208
209 /*---------------------------------------------------------------------------*/
210
211 static gboolean
212 has_manages_descendants (AtkObject *accessible)
213 {
214    AtkStateSet *state;
215    gboolean result = FALSE;
216
217    /* This is dangerous, refing the state set
218     * seems to do wierd things to the tree & cause recursion
219     * by modifying the tree alot.
220     */
221    state = atk_object_ref_state_set (accessible);
222    if (atk_state_set_contains_state (state, ATK_STATE_MANAGES_DESCENDANTS))
223      {
224 #ifdef SPI_ATK_DEBUG
225        g_warning ("AT-SPI: Object with 'Manages descendants' states not currently handled by AT-SPI");
226 #endif
227        result = TRUE;
228      }
229    g_object_unref (state);
230
231    return result;
232 }
233
234 static void
235 append_children (AtkObject *accessible, GQueue *traversal)
236 {
237   AtkObject *current;
238   guint i;
239
240   for (i =0; i < atk_object_get_n_accessible_children (accessible); i++)
241     {
242       current = atk_object_ref_accessible_child (accessible, i);
243 #ifdef SPI_ATK_DEBUG
244       non_owned_accessible (current);
245 #endif
246       if (!has_manages_descendants (current))
247         {
248           g_queue_push_tail (traversal, current);
249         }
250       else
251         {
252           g_object_unref (G_OBJECT (current));
253         }
254     }
255 }
256
257 /*
258  * Registers a subtree of accessible objects
259  * rooted at the accessible object provided.
260  *
261  * The leaf nodes do not have their children
262  * registered. A node is considered a leaf
263  * if it has the state "manages-descendants"
264  * or if it has already been registered.
265  */
266 void
267 register_subtree (AtkObject *accessible)
268 {
269   AtkObject *current;
270   GQueue    *traversal;
271   GQueue    *emit_update;
272
273   g_return_if_fail (ATK_IS_OBJECT (accessible));
274
275   traversal = g_queue_new ();
276   emit_update = g_queue_new ();
277
278   g_object_ref (accessible);
279   g_queue_push_tail (traversal, accessible);
280
281   while (!g_queue_is_empty (traversal))
282     {
283       current = g_queue_pop_head (traversal);
284       g_queue_push_tail (emit_update, current);
285       if (!object_to_ref (current))
286         {
287           register_accessible (current);
288 #ifdef SPI_ATK_DEBUG
289           g_debug ("REG  - %s - %d - %s", atk_object_get_name     (current),
290                                           atk_object_get_role     (current),
291                                           atk_dbus_object_to_path (current));
292 #endif
293           append_children (current, traversal);
294         }
295     }
296
297   while (!g_queue_is_empty (emit_update))
298     {
299       current = g_queue_pop_head (emit_update);
300       spi_emit_cache_update (current, atk_adaptor_app_data->bus);
301       g_object_unref (G_OBJECT (current));
302     }
303
304   g_queue_free (traversal);
305   g_queue_free (emit_update);
306 }
307
308 /*---------------------------------------------------------------------------*/
309
310 /*
311  * Called when an already registered object is updated in such a
312  * way that client side cache needs to be updated.
313  */
314 static void
315 update_accessible (AtkObject *accessible)
316 {
317   guint  ref = 0;
318   g_return_if_fail (ATK_IS_OBJECT(accessible));
319
320   ref = object_to_ref (accessible);
321   if (ref)
322     {
323       spi_emit_cache_update (accessible, atk_adaptor_app_data->bus);
324     }
325 }
326
327 /*---------------------------------------------------------------------------*/
328
329 void
330 atk_dbus_foreach_registered(GHFunc func, gpointer data)
331 {
332   g_hash_table_foreach(ref2ptr, func, data);
333 }
334
335 /*
336  * Used to lookup an AtkObject from its D-Bus path.
337  */
338 AtkObject *
339 atk_dbus_path_to_object (const char *path)
340 {
341   guint index;
342   void *data;
343
344   g_return_val_if_fail (path, NULL);
345
346   if (strncmp(path, SPI_ATK_OBJECT_PATH_PREFIX, SPI_ATK_PATH_PREFIX_LENGTH) != 0)
347     return NULL;
348
349   path += SPI_ATK_PATH_PREFIX_LENGTH; /* Skip over the prefix */
350
351   if (!g_strcmp0 (SPI_ATK_OBJECT_PATH_DESKTOP, path))
352      return atk_get_root();
353   if (path[0] != '/')
354      return NULL;
355
356   path++;
357   index = atoi (path);
358   data = g_hash_table_lookup (ref2ptr, GINT_TO_POINTER(index));
359   if (data)
360     return ATK_OBJECT (data);
361   else
362     return NULL;
363 }
364
365 /*
366  * Used to lookup a D-Bus path from the AtkObject.
367  */
368 gchar *
369 atk_dbus_object_to_path (AtkObject *accessible)
370 {
371   guint ref;
372
373   ref = object_to_ref (accessible);
374   if (!ref)
375       return NULL;
376   else
377       return ref_to_path (ref);
378 }
379
380 gchar *
381 atk_dbus_desktop_object_path ()
382 {
383   return g_strdup (SPI_ATK_OBJECT_PATH_PREFIX SPI_ATK_OBJECT_PATH_DESKTOP);
384 }
385
386 /*---------------------------------------------------------------------------*/
387
388 /*
389  * Events are not evaluated for non-registered accessibles.
390  *
391  * When a property change is made on a registered accessible
392  * the client side cache should be updated.
393  *
394  * When a parent is changed the subtree is de-registered
395  * if the parent is not attached to the root accessible.
396  */
397 static gboolean
398 tree_update_listener (GSignalInvocationHint *signal_hint,
399                       guint                  n_param_values,
400                       const GValue          *param_values,
401                       gpointer               data)
402 {
403   AtkObject *accessible;
404   AtkPropertyValues *values;
405   const gchar *pname = NULL;
406
407   g_static_rec_mutex_lock (&registration_mutex);
408
409   /* Ensure that only registered accessibles
410    * have their signals processed.
411    */
412   accessible = g_value_get_object (&param_values[0]);
413   g_return_val_if_fail (ATK_IS_OBJECT (accessible), TRUE);
414
415   if (object_to_ref (accessible))
416     {
417 #ifdef SPI_ATK_DEBUG
418       if (recursion_check_and_set ())
419           g_warning ("AT-SPI: Recursive use of registration module");
420
421       g_debug ("AT-SPI: Tree update listener");
422 #endif
423
424
425       values = (AtkPropertyValues*) g_value_get_pointer (&param_values[1]);
426       pname = values[0].property_name;
427       if (strcmp (pname, "accessible-name") == 0 ||
428           strcmp (pname, "accessible-description") == 0)
429         {
430           update_accessible (accessible);
431         }
432       /* Parent value us updated by child-add signal of parent object */
433
434       recursion_check_unset ();
435     }
436
437   g_static_rec_mutex_unlock (&registration_mutex);
438
439   return TRUE;
440 }
441
442 /*
443  * Events are not evaluated for non registered accessibles.
444  *
445  * When the children of a registered accessible are changed
446  * the subtree, rooted at the child is registered.
447  */
448 static gboolean
449 tree_update_children_listener (GSignalInvocationHint *signal_hint,
450                                guint                  n_param_values,
451                                const GValue          *param_values,
452                                gpointer               data)
453 {
454   AtkObject *accessible;
455   const gchar *detail = NULL;
456   AtkObject *child;
457
458   g_static_rec_mutex_lock (&registration_mutex);
459
460   /* Ensure that only registered accessibles
461    * have their signals processed.
462    */
463   accessible = g_value_get_object (&param_values[0]);
464   g_return_val_if_fail (ATK_IS_OBJECT (accessible), TRUE);
465
466   if (object_to_ref (accessible))
467     {
468 #ifdef SPI_ATK_DEBUG
469       if (recursion_check_and_set ())
470           g_warning ("AT-SPI: Recursive use of registration module");
471
472       g_debug ("AT-SPI: Tree update children listener");
473 #endif
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 #ifdef SPI_ATK_DEBUG
488               non_owned_accessible (child);
489 #endif
490             }
491           register_subtree (child);
492         }
493
494       recursion_check_unset ();
495     }
496
497   g_static_rec_mutex_unlock (&registration_mutex);
498
499   return TRUE;
500 }
501
502 /*
503  * Initializes required global data. The update and removal lists
504  * and the reference lookup tables.
505  *
506  * Initializes all of the required D-Bus interfaces.
507  */
508 void
509 atk_dbus_initialize (AtkObject *root)
510 {
511   if (!ref2ptr)
512     ref2ptr = g_hash_table_new(g_direct_hash, g_direct_equal);
513
514 #ifdef SPI_ATK_DEBUG
515   if (g_thread_supported ())
516       g_message ("AT-SPI: Threads enabled");
517
518   g_debug ("AT-SPI: Initial Atk tree regisration");
519 #endif
520
521   register_subtree (root);
522
523   atk_add_global_event_listener (tree_update_listener, "Gtk:AtkObject:property-change");
524   atk_add_global_event_listener (tree_update_children_listener, "Gtk:AtkObject:children-changed");
525 }
526
527 /*END------------------------------------------------------------------------*/
528