Don't return DBUS_HANDLER_RESULT_HANDLED for NameOwnerChanged signals
[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, 2010 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  */
49
50 #define SPI_ATK_PATH_PREFIX_LENGTH 27
51 #define SPI_ATK_OBJECT_PATH_PREFIX  "/org/a11y/atspi/accessible/"
52 #define SPI_ATK_OBJECT_PATH_ROOT "root"
53
54 #define SPI_ATK_OBJECT_REFERENCE_TEMPLATE SPI_ATK_OBJECT_PATH_PREFIX "%d"
55
56 #define SPI_DBUS_ID "spi-dbus-id"
57
58 SpiRegister *spi_global_register = NULL;
59
60 static const gchar * spi_register_root_path = SPI_ATK_OBJECT_PATH_PREFIX SPI_ATK_OBJECT_PATH_ROOT;
61
62 enum
63 {
64   OBJECT_REGISTERED,
65   OBJECT_DEREGISTERED,
66   LAST_SIGNAL
67 };
68 static guint register_signals[LAST_SIGNAL] = { 0 };
69
70 /*---------------------------------------------------------------------------*/
71
72 static void
73 spi_register_finalize (GObject * object);
74
75 /*---------------------------------------------------------------------------*/
76
77 G_DEFINE_TYPE (SpiRegister, spi_register, G_TYPE_OBJECT)
78
79 static void spi_register_class_init (SpiRegisterClass * klass)
80 {
81   GObjectClass *object_class = (GObjectClass *) klass;
82
83   spi_register_parent_class = g_type_class_ref (G_TYPE_OBJECT);
84
85   object_class->finalize = spi_register_finalize;
86
87   register_signals [OBJECT_REGISTERED] =
88       g_signal_new ("object-registered",
89                     SPI_REGISTER_TYPE,
90                     G_SIGNAL_ACTION,
91                     0,
92                     NULL,
93                     NULL,
94                     g_cclosure_marshal_VOID__OBJECT,
95                     G_TYPE_NONE,
96                     1,
97                     G_TYPE_OBJECT);
98
99   register_signals [OBJECT_DEREGISTERED] =
100       g_signal_new ("object-deregistered",
101                     SPI_REGISTER_TYPE,
102                     G_SIGNAL_ACTION,
103                     0,
104                     NULL,
105                     NULL,
106                     g_cclosure_marshal_VOID__OBJECT,
107                     G_TYPE_NONE,
108                     1,
109                     G_TYPE_OBJECT);
110 }
111
112 static void
113 spi_register_init (SpiRegister * reg)
114 {
115   reg->ref2ptr = g_hash_table_new (g_direct_hash, g_direct_equal);
116   reg->reference_counter = 0;
117 }
118
119 static void
120 deregister_object (gpointer data, GObject * gobj)
121 {
122   SpiRegister *reg = SPI_REGISTER (data);
123
124   spi_register_deregister_object (reg, gobj, FALSE);
125 }
126
127 static void
128 spi_register_remove_weak_ref (gpointer key, gpointer val, gpointer reg)
129 {
130   g_object_weak_unref (val, deregister_object, reg);
131 }
132
133 static void
134 spi_register_finalize (GObject * object)
135 {
136   SpiRegister *reg = SPI_REGISTER (object);
137
138   g_hash_table_foreach (reg->ref2ptr, spi_register_remove_weak_ref, reg);
139   g_hash_table_unref (reg->ref2ptr);
140
141   G_OBJECT_CLASS (spi_register_parent_class)->finalize (object);
142 }
143
144 /*---------------------------------------------------------------------------*/
145
146 /*
147  * Each AtkObject must be asssigned a D-Bus path (Reference)
148  *
149  * This function provides an integer reference for a new
150  * AtkObject.
151  *
152  * TODO: Make this reference a little more unique, this is shoddy.
153  */
154 static guint
155 assign_reference (SpiRegister * reg)
156 {
157   reg->reference_counter++;
158   /* Reference of 0 not allowed as used as direct key in hash table */
159   if (reg->reference_counter == 0)
160     reg->reference_counter++;
161   return reg->reference_counter;
162 }
163
164 /*---------------------------------------------------------------------------*/
165
166 /*
167  * Returns the reference of the object, or 0 if it is not registered.
168  */
169 static guint
170 object_to_ref (GObject * gobj)
171 {
172   return GPOINTER_TO_INT (g_object_get_data (gobj, SPI_DBUS_ID));
173 }
174
175 /*
176  * Converts the Accessible object reference to its D-Bus object path
177  */
178 static gchar *
179 ref_to_path (guint ref)
180 {
181   return g_strdup_printf (SPI_ATK_OBJECT_REFERENCE_TEMPLATE, ref);
182 }
183
184 /*---------------------------------------------------------------------------*/
185
186 /*
187  * Callback for when a registered AtkObject is destroyed.
188  *
189  * Removes the AtkObject from the reference lookup tables, meaning
190  * it is no longer exposed over D-Bus.
191  */
192 void
193 spi_register_deregister_object (SpiRegister *reg, GObject *gobj, gboolean unref)
194 {
195   guint ref;
196
197   ref = object_to_ref (gobj);
198   if (ref != 0)
199     {
200       g_signal_emit (reg,
201                      register_signals [OBJECT_DEREGISTERED],
202                      0,
203                      gobj);
204       if (unref)
205         g_object_weak_unref (gobj, deregister_object, reg);
206       g_hash_table_remove (reg->ref2ptr, GINT_TO_POINTER (ref));
207
208 #ifdef SPI_ATK_DEBUG
209       g_debug ("DEREG  - %d", ref);
210 #endif
211     }
212 }
213
214 static void
215 register_object (SpiRegister * reg, GObject * gobj)
216 {
217   guint ref;
218   g_return_if_fail (G_IS_OBJECT (gobj));
219
220   ref = assign_reference (reg);
221
222   g_hash_table_insert (reg->ref2ptr, GINT_TO_POINTER (ref), gobj);
223   g_object_set_data (G_OBJECT (gobj), SPI_DBUS_ID, GINT_TO_POINTER (ref));
224   g_object_weak_ref (G_OBJECT (gobj), deregister_object, reg);
225
226 #ifdef SPI_ATK_DEBUG
227   g_debug ("REG  - %d", ref);
228 #endif
229
230   g_signal_emit (reg, register_signals [OBJECT_REGISTERED], 0, gobj);
231 }
232
233 /*---------------------------------------------------------------------------*/
234
235 /*
236  * Used to lookup an GObject from its D-Bus path.
237  * 
238  * If the D-Bus path is not found this function returns NULL.
239  */
240 GObject *
241 spi_register_path_to_object (SpiRegister * reg, const char *path)
242 {
243   guint index;
244   void *data;
245
246   g_return_val_if_fail (path, NULL);
247
248   if (strncmp (path, SPI_ATK_OBJECT_PATH_PREFIX, SPI_ATK_PATH_PREFIX_LENGTH)
249       != 0)
250     return NULL;
251
252   path += SPI_ATK_PATH_PREFIX_LENGTH; /* Skip over the prefix */
253
254   /* Map the root path to the root object. */
255   if (!g_strcmp0 (SPI_ATK_OBJECT_PATH_ROOT, path))
256     return G_OBJECT (spi_global_app_data->root);
257
258   index = atoi (path);
259   data = g_hash_table_lookup (reg->ref2ptr, GINT_TO_POINTER (index));
260   if (data)
261     return G_OBJECT(data);
262   else
263     return NULL;
264 }
265
266 GObject *
267 spi_global_register_path_to_object (const char * path)
268 {
269   return spi_register_path_to_object (spi_global_register, path);
270 }
271
272 /*
273  * Used to lookup a D-Bus path from the GObject.
274  * 
275  * If the objects is not already registered, 
276  * this function will register it.
277  */
278 gchar *
279 spi_register_object_to_path (SpiRegister * reg, GObject * gobj)
280 {
281   guint ref;
282
283   if (gobj == NULL)
284     return NULL;
285
286   /* Map the root object to the root path. */
287   if ((void *)gobj == (void *)spi_global_app_data->root)
288     return g_strdup (spi_register_root_path);
289
290   ref = object_to_ref (gobj);
291   if (!ref)
292     {
293       register_object (reg, gobj);
294       ref = object_to_ref (gobj);
295     }
296
297   if (!ref)
298     return NULL;
299   else
300     return ref_to_path (ref);
301 }
302
303 guint
304 spi_register_object_to_ref (GObject * gobj)
305 {
306   return object_to_ref (gobj);
307 }
308   
309 /*
310  * Gets the path that indicates the accessible desktop object.
311  * This object is logically located on the registry daemon and not
312  * within any particular application.
313  */
314 gchar *
315 spi_register_root_object_path ()
316 {
317   return g_strdup (SPI_ATK_OBJECT_PATH_PREFIX SPI_ATK_OBJECT_PATH_ROOT);
318 }
319
320 /*END------------------------------------------------------------------------*/