2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2008 Novell, Inc.
6 * Copyright 2008, 2009, 2010 Codethink Ltd.
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.
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.
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.
29 #include "accessible-register.h"
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.
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'.
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.
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"
54 #define SPI_ATK_OBJECT_REFERENCE_TEMPLATE SPI_ATK_OBJECT_PATH_PREFIX "%d"
56 #define SPI_DBUS_ID "spi-dbus-id"
58 SpiRegister *spi_global_register = NULL;
60 static const gchar * spi_register_root_path = SPI_ATK_OBJECT_PATH_PREFIX SPI_ATK_OBJECT_PATH_ROOT;
68 static guint register_signals[LAST_SIGNAL] = { 0 };
70 /*---------------------------------------------------------------------------*/
73 spi_register_finalize (GObject * object);
76 spi_register_dispose (GObject * object);
78 /*---------------------------------------------------------------------------*/
80 G_DEFINE_TYPE (SpiRegister, spi_register, G_TYPE_OBJECT)
82 static void spi_register_class_init (SpiRegisterClass * klass)
84 GObjectClass *object_class = (GObjectClass *) klass;
86 spi_register_parent_class = g_type_class_ref (G_TYPE_OBJECT);
88 object_class->finalize = spi_register_finalize;
89 object_class->dispose = spi_register_dispose;
91 register_signals [OBJECT_REGISTERED] =
92 g_signal_new ("object-registered",
98 g_cclosure_marshal_VOID__OBJECT,
103 register_signals [OBJECT_DEREGISTERED] =
104 g_signal_new ("object-deregistered",
110 g_cclosure_marshal_VOID__OBJECT,
117 spi_register_init (SpiRegister * reg)
119 reg->ref2ptr = g_hash_table_new (g_direct_hash, g_direct_equal);
120 reg->reference_counter = 0;
124 spi_register_finalize (GObject * object)
126 SpiRegister *reg = SPI_REGISTER (object);
128 g_free (reg->ref2ptr);
130 G_OBJECT_CLASS (spi_register_parent_class)->finalize (object);
134 spi_register_dispose (GObject * object)
136 SpiRegister *reg = SPI_REGISTER (object);
138 G_OBJECT_CLASS (spi_register_parent_class)->dispose (object);
141 /*---------------------------------------------------------------------------*/
144 * Each AtkObject must be asssigned a D-Bus path (Reference)
146 * This function provides an integer reference for a new
149 * TODO: Make this reference a little more unique, this is shoddy.
152 assign_reference (SpiRegister * reg)
154 reg->reference_counter++;
155 /* Reference of 0 not allowed as used as direct key in hash table */
156 if (reg->reference_counter == 0)
157 reg->reference_counter++;
158 return reg->reference_counter;
161 /*---------------------------------------------------------------------------*/
164 * Returns the reference of the object, or 0 if it is not registered.
167 object_to_ref (GObject * gobj)
169 return GPOINTER_TO_INT (g_object_get_data (gobj, SPI_DBUS_ID));
173 * Converts the Accessible object reference to its D-Bus object path
176 ref_to_path (guint ref)
178 return g_strdup_printf (SPI_ATK_OBJECT_REFERENCE_TEMPLATE, ref);
181 /*---------------------------------------------------------------------------*/
184 * Callback for when a registered AtkObject is destroyed.
186 * Removes the AtkObject from the reference lookup tables, meaning
187 * it is no longer exposed over D-Bus.
190 deregister_object (gpointer data, GObject * gobj)
192 SpiRegister *reg = SPI_REGISTER (data);
195 ref = object_to_ref (gobj);
199 register_signals [OBJECT_DEREGISTERED],
202 g_hash_table_remove (reg->ref2ptr, GINT_TO_POINTER (ref));
205 g_debug ("DEREG - %d", ref);
211 register_object (SpiRegister * reg, GObject * gobj)
214 g_return_if_fail (G_IS_OBJECT (gobj));
216 ref = assign_reference (reg);
218 g_hash_table_insert (reg->ref2ptr, GINT_TO_POINTER (ref), gobj);
219 g_object_set_data (G_OBJECT (gobj), SPI_DBUS_ID, GINT_TO_POINTER (ref));
220 g_object_weak_ref (G_OBJECT (gobj), deregister_object, reg);
223 g_debug ("REG - %d", ref);
226 g_signal_emit (reg, register_signals [OBJECT_REGISTERED], 0, gobj);
229 /*---------------------------------------------------------------------------*/
232 * Used to lookup an GObject from its D-Bus path.
234 * If the D-Bus path is not found this function returns NULL.
237 spi_register_path_to_object (SpiRegister * reg, const char *path)
242 g_return_val_if_fail (path, NULL);
244 if (strncmp (path, SPI_ATK_OBJECT_PATH_PREFIX, SPI_ATK_PATH_PREFIX_LENGTH)
248 path += SPI_ATK_PATH_PREFIX_LENGTH; /* Skip over the prefix */
250 /* Map the root path to the root object. */
251 if (!g_strcmp0 (SPI_ATK_OBJECT_PATH_ROOT, path))
252 return G_OBJECT (spi_global_app_data->root);
255 data = g_hash_table_lookup (reg->ref2ptr, GINT_TO_POINTER (index));
257 return G_OBJECT(data);
263 spi_global_register_path_to_object (const char * path)
265 return spi_register_path_to_object (spi_global_register, path);
269 * Used to lookup a D-Bus path from the GObject.
271 * If the objects is not already registered,
272 * this function will register it.
275 spi_register_object_to_path (SpiRegister * reg, GObject * gobj)
282 /* Map the root object to the root path. */
283 if (gobj == spi_global_app_data->root)
284 return g_strdup (spi_register_root_path);
286 ref = object_to_ref (gobj);
289 register_object (reg, gobj);
290 ref = object_to_ref (gobj);
296 return ref_to_path (ref);
300 spi_register_object_to_ref (GObject * gobj)
302 return object_to_ref (gobj);
306 * Gets the path that indicates the accessible desktop object.
307 * This object is logically located on the registry daemon and not
308 * within any particular application.
311 spi_register_root_object_path ()
313 return g_strdup (SPI_ATK_OBJECT_PATH_PREFIX SPI_ATK_OBJECT_PATH_ROOT);
316 /*END------------------------------------------------------------------------*/