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.
25 #include "accessible.h"
27 #define get_object(message) spi_dbus_get_object(dbus_message_get_path(message))
30 append_update (DBusMessageIter * iter_array, AtkObject * obj,
31 dbus_bool_t include_children, DRouteData * data)
33 DBusMessageIter iter_struct, iter_sub_array;
36 const char *name, *desc;
43 dbus_message_iter_open_container (iter_array, DBUS_TYPE_STRUCT, NULL,
45 path = spi_dbus_get_path (obj);
46 dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_OBJECT_PATH, &path);
47 path_parent = spi_dbus_get_path (atk_object_get_parent(obj));
48 if (!path_parent) path_parent = g_strdup("/");
49 dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_OBJECT_PATH, &path_parent);
51 dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "o",
53 childcount = atk_object_get_n_accessible_children (obj);
54 for (i = 0; i < childcount; i++)
56 AtkObject *child = atk_object_ref_accessible_child (obj, i);
57 char *child_path = spi_dbus_get_path (child);
60 dbus_message_iter_append_basic (&iter_sub_array,
61 DBUS_TYPE_OBJECT_PATH, &child_path);
65 g_object_unref (child);
67 if (!dbus_message_iter_close_container (&iter_struct, &iter_sub_array))
69 dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "s",
71 for (l = data->interfaces; l; l = g_slist_next (l))
73 DRouteInterface *iface_def = (DRouteInterface *) l->data;
75 if (iface_def->get_datum)
77 datum = (*iface_def->get_datum) (path, data->user_data);
81 dbus_message_iter_append_basic (&iter_sub_array, DBUS_TYPE_STRING,
83 if (iface_def->free_datum)
84 (*iface_def->free_datum) (datum);
86 if (!dbus_message_iter_close_container (&iter_struct, &iter_sub_array))
88 name = atk_object_get_name (obj);
91 dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &name);
92 role = spi_accessible_role_from_atk_role (atk_object_get_role (obj));
93 dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_UINT32, &role);
94 desc = atk_object_get_description (obj);
97 dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &desc);
98 if (!dbus_message_iter_close_container (iter_array, &iter_struct))
100 if (!include_children) childcount = 0;
101 for (i = 0; i < childcount; i++)
103 AtkObject *child = atk_object_ref_accessible_child (obj, i);
107 result = append_update (iter_array, child, TRUE, data);
108 g_object_unref (child);
115 if (path) g_free(path);
120 spi_dbus_append_tree (DBusMessage * message, AtkObject * obj,
123 DBusMessageIter iter, iter_array;
126 dbus_message_iter_init_append (message, &iter);
127 dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "(ooaoassus)",
129 result = append_update (&iter_array, obj, TRUE, data);
131 result = dbus_message_iter_close_container (&iter, &iter_array);
136 impl_getRoot (DBusConnection * bus, DBusMessage * message, void *user_data)
138 AtkObject *root = atk_get_root();
142 if (root) path = spi_dbus_get_path(root);
144 return spi_dbus_general_error (message);
145 reply = dbus_message_new_method_return (message);
146 dbus_message_append_args (reply, DBUS_TYPE_OBJECT_PATH, &path,
153 impl_getTree (DBusConnection * bus, DBusMessage * message, void *user_data)
156 AtkObject *root = atk_get_root();
158 if (!root) return spi_dbus_general_error(message);
159 reply = dbus_message_new_method_return (message);
160 spi_dbus_append_tree (reply, root, (DRouteData *) user_data);
164 static DRouteMethod methods[] = {
165 {impl_getRoot, "getRoot"},
166 {impl_getTree, "getTree", TRUE},
171 spi_initialize_tree (DRouteData * data)
173 droute_add_interface (data, "org.freedesktop.atspi.Tree",
174 methods, NULL, NULL, NULL);
177 static GHashTable *cache_list;
180 #define UPDATE_REFRESH 2
181 #define UPDATE_REMOVE 3
183 static int update_pending = 0;
184 static gint update_pending_id;
188 DBusMessageIter iter;
193 static void handle_cache_item(char *path, guint action, CacheIterData *d)
202 if (d->removing) return;
203 obj = spi_dbus_get_object(path);
204 //printf("update %s\n", path);
205 append_update(&d->iter, obj, FALSE, d->droute);
208 //printf("remove: %s\n", path);
209 if (!d->removing) return;
210 dbus_message_iter_append_basic(&d->iter, DBUS_TYPE_OBJECT_PATH, &path);
213 g_hash_table_remove(cache_list, path);
216 gboolean spi_dbus_update_cache(DRouteData *data)
218 DBusMessage *message;
219 DBusMessageIter iter;
222 if (update_pending == 0) return FALSE;
223 //printf("Sending cache\n");
224 message = dbus_message_new_signal("/org/freedesktop/atspi/tree", "org.freedesktop.atspi.Tree", "UpdateTree");
225 if (!message) goto done;
226 dbus_message_iter_init_append (message, &iter);
227 dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "(ooaoassus)",
233 /* This loop is needed because appending an item may cause new children
234 * to be registered and consequently added to the hash, so they, too,
235 * will need to be sent with the update */
237 g_hash_table_foreach(cache_list, (GHFunc)handle_cache_item, &d);
238 } while (update_pending);
239 dbus_message_iter_close_container(&iter, &d.iter);
240 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "o", &d.iter);
242 g_hash_table_foreach(cache_list, (GHFunc)handle_cache_item, &d);
243 dbus_message_iter_close_container(&iter, &d.iter);
244 dbus_connection_send(data->bus, message, NULL);
249 void spi_dbus_notify_change(AtkObject *obj, gboolean new, DRouteData *data)
251 guint action = (new? UPDATE_NEW: UPDATE_REFRESH);
252 char *path = spi_dbus_get_path(obj);
256 cache_list = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
263 if (g_hash_table_lookup(cache_list, path))
268 //printf("change: %s\n", path);
269 g_hash_table_insert(cache_list, path, (gpointer)action);
270 if (update_pending != 2 && data)
272 update_pending_id = g_idle_add((GSourceFunc)spi_dbus_update_cache, data);
275 else if (!update_pending) update_pending = 1;
278 void spi_dbus_notify_remove(AtkObject *obj, DRouteData *data)
280 guint action = UPDATE_REMOVE;
282 gchar *path = spi_dbus_get_path(obj);
284 //printf("notify remove: %s\n", path);
290 cur_action = (guint)g_hash_table_lookup(cache_list, path);
291 if (cur_action == UPDATE_NEW)
293 /* No one knew that this object ever existed, so just remove it */
294 //printf("Removing object from send queue\n");
295 g_hash_table_remove(cache_list, path);
300 g_hash_table_insert(cache_list, path, (gpointer)action);
301 if (update_pending != 2 && data)
303 update_pending_id = g_idle_add((GSourceFunc)spi_dbus_update_cache, data);
306 else if (!update_pending) update_pending = 1;
310 gboolean spi_dbus_object_is_known(AtkObject *obj)
313 char *path = spi_dbus_get_path(obj);
314 if (!path) return FALSE;
315 cur_action = (guint)g_hash_table_lookup(cache_list, path);
317 return (cur_action != UPDATE_NEW);