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 2001, 2002 Sun Microsystems Inc.,
7 * Copyright 2001, 2002 Ximian, Inc.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 #include <droute/introspect-loader.h>
28 #include "accessible.h"
30 #define get_object(message) spi_dbus_get_object(dbus_message_get_path(message))
33 append_update (DBusMessageIter * iter_array, AtkObject * obj,
34 dbus_bool_t include_children, DRouteData * data)
36 DBusMessageIter iter_struct, iter_sub_array;
39 const char *name, *desc;
46 g_assert(data != NULL);
48 dbus_message_iter_open_container (iter_array, DBUS_TYPE_STRUCT, NULL,
50 path = spi_dbus_get_path (obj);
51 dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_OBJECT_PATH, &path);
52 path_parent = spi_dbus_get_path (atk_object_get_parent(obj));
53 if (!path_parent) path_parent = g_strdup("/");
54 dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_OBJECT_PATH, &path_parent);
56 dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "o",
58 childcount = atk_object_get_n_accessible_children (obj);
59 for (i = 0; i < childcount; i++)
61 AtkObject *child = atk_object_ref_accessible_child (obj, i);
62 char *child_path = spi_dbus_get_path (child);
65 dbus_message_iter_append_basic (&iter_sub_array,
66 DBUS_TYPE_OBJECT_PATH, &child_path);
70 g_object_unref (child);
72 if (!dbus_message_iter_close_container (&iter_struct, &iter_sub_array))
74 dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "s",
76 if (data) for (l = data->interfaces; l; l = g_slist_next (l))
78 DRouteInterface *iface_def = (DRouteInterface *) l->data;
80 if (iface_def->get_datum)
82 datum = (*iface_def->get_datum) (path, data->user_data);
86 dbus_message_iter_append_basic (&iter_sub_array, DBUS_TYPE_STRING,
88 if (iface_def->free_datum)
89 (*iface_def->free_datum) (datum);
91 if (!dbus_message_iter_close_container (&iter_struct, &iter_sub_array))
93 name = atk_object_get_name (obj);
96 dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &name);
97 role = spi_accessible_role_from_atk_role (atk_object_get_role (obj));
98 dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_UINT32, &role);
99 desc = atk_object_get_description (obj);
102 dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &desc);
103 if (!dbus_message_iter_close_container (iter_array, &iter_struct))
105 if (!include_children) childcount = 0;
106 for (i = 0; i < childcount; i++)
108 AtkObject *child = atk_object_ref_accessible_child (obj, i);
112 result = append_update (iter_array, child, TRUE, data);
113 g_object_unref (child);
120 if (path) g_free(path);
125 spi_dbus_append_tree (DBusMessage * message, AtkObject * obj,
128 DBusMessageIter iter, iter_array;
131 g_assert(data != NULL);
133 dbus_message_iter_init_append (message, &iter);
134 dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "(ooaoassus)",
136 result = append_update (&iter_array, obj, TRUE, data);
138 result = dbus_message_iter_close_container (&iter, &iter_array);
143 impl_getRoot (DBusConnection * bus, DBusMessage * message, void *user_data)
145 AtkObject *root = atk_get_root();
149 if (root) path = spi_dbus_get_path(root);
151 return spi_dbus_general_error (message);
152 reply = dbus_message_new_method_return (message);
153 dbus_message_append_args (reply, DBUS_TYPE_OBJECT_PATH, &path,
160 impl_getTree (DBusConnection * bus, DBusMessage * message, void *user_data)
163 AtkObject *root = atk_get_root();
165 if (!root) return spi_dbus_general_error(message);
166 reply = dbus_message_new_method_return (message);
167 spi_dbus_append_tree (reply, root, (DRouteData *) user_data);
172 impl_introspect (DBusConnection *bus, DBusMessage *message, void *user_data)
180 path = dbus_message_get_path(message);
182 output = g_string_new(spi_introspection_header);
184 g_string_append_printf(output, spi_introspection_node_element, path);
186 spi_append_interface(output, "org.freedesktop.atspi.Tree");
188 g_string_append(output, spi_introspection_footer);
189 final = g_string_free(output, FALSE);
191 reply = dbus_message_new_method_return (message);
192 g_assert(reply != NULL);
193 dbus_message_append_args(reply, DBUS_TYPE_STRING, &final,
200 static DRouteMethod methods[] = {
201 {impl_getRoot, "getRoot"},
202 {impl_getTree, "getTree", TRUE},
207 spi_initialize_tree (DRouteData * data)
209 droute_add_interface (data, "org.freedesktop.atspi.Tree", methods,
212 static GHashTable *cache_list;
215 #define UPDATE_REFRESH 2
216 #define UPDATE_REMOVE 3
218 static int update_pending = 0;
219 static gint update_pending_id;
223 DBusMessageIter iter;
228 static void handle_cache_item(char *path, guint action, CacheIterData *d)
237 if (d->removing) return;
238 obj = spi_dbus_get_object(path);
239 //printf("update %s\n", path);
240 append_update(&d->iter, obj, FALSE, d->droute);
243 //printf("remove: %s\n", path);
244 if (!d->removing) return;
245 dbus_message_iter_append_basic(&d->iter, DBUS_TYPE_OBJECT_PATH, &path);
248 g_hash_table_remove(cache_list, path);
251 gboolean spi_dbus_update_cache(DRouteData *data)
253 DBusMessage *message;
254 DBusMessageIter iter;
257 g_assert(data != NULL);
259 if (update_pending == 0) return FALSE;
260 //printf("Sending cache\n");
261 message = dbus_message_new_signal("/org/freedesktop/atspi/tree", "org.freedesktop.atspi.Tree", "UpdateTree");
262 if (!message) goto done;
263 dbus_message_iter_init_append (message, &iter);
264 dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "(ooaoassus)",
270 /* This loop is needed because appending an item may cause new children
271 * to be registered and consequently added to the hash, so they, too,
272 * will need to be sent with the update */
274 g_hash_table_foreach(cache_list, (GHFunc)handle_cache_item, &d);
275 } while (update_pending);
276 dbus_message_iter_close_container(&iter, &d.iter);
277 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "o", &d.iter);
279 g_hash_table_foreach(cache_list, (GHFunc)handle_cache_item, &d);
280 dbus_message_iter_close_container(&iter, &d.iter);
281 dbus_connection_send(data->bus, message, NULL);
286 void spi_dbus_notify_change(AtkObject *obj, gboolean new, DRouteData *data)
288 guint action = (new? UPDATE_NEW: UPDATE_REFRESH);
289 char *path = spi_dbus_get_path(obj);
293 cache_list = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
300 if (g_hash_table_lookup(cache_list, path))
305 //printf("change: %s\n", path);
306 g_hash_table_insert(cache_list, path, (gpointer)action);
307 if (update_pending != 2 && data)
309 update_pending_id = g_idle_add((GSourceFunc)spi_dbus_update_cache, data);
312 else if (!update_pending) update_pending = 1;
315 void spi_dbus_notify_remove(AtkObject *obj, DRouteData *data)
317 guint action = UPDATE_REMOVE;
319 gchar *path = spi_dbus_get_path(obj);
321 //printf("notify remove: %s\n", path);
327 cur_action = (guint)g_hash_table_lookup(cache_list, path);
328 if (cur_action == UPDATE_NEW)
330 /* No one knew that this object ever existed, so just remove it */
331 //printf("Removing object from send queue\n");
332 g_hash_table_remove(cache_list, path);
337 g_hash_table_insert(cache_list, path, (gpointer)action);
338 if (update_pending != 2 && data)
340 update_pending_id = g_idle_add((GSourceFunc)spi_dbus_update_cache, data);
343 else if (!update_pending) update_pending = 1;
347 gboolean spi_dbus_object_is_known(AtkObject *obj)
350 char *path = spi_dbus_get_path(obj);
351 if (!path) return FALSE;
352 cur_action = (guint)g_hash_table_lookup(cache_list, path);
354 return (cur_action != UPDATE_NEW);