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 dbus_message_iter_open_container (iter_array, DBUS_TYPE_STRUCT, NULL,
48 path = spi_dbus_get_path (obj);
49 dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_OBJECT_PATH, &path);
50 path_parent = spi_dbus_get_path (atk_object_get_parent(obj));
51 if (!path_parent) path_parent = g_strdup("/");
52 dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_OBJECT_PATH, &path_parent);
54 dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "o",
56 childcount = atk_object_get_n_accessible_children (obj);
57 for (i = 0; i < childcount; i++)
59 AtkObject *child = atk_object_ref_accessible_child (obj, i);
60 char *child_path = spi_dbus_get_path (child);
63 dbus_message_iter_append_basic (&iter_sub_array,
64 DBUS_TYPE_OBJECT_PATH, &child_path);
68 g_object_unref (child);
70 if (!dbus_message_iter_close_container (&iter_struct, &iter_sub_array))
72 dbus_message_iter_open_container (&iter_struct, DBUS_TYPE_ARRAY, "s",
74 if (data) for (l = data->interfaces; l; l = g_slist_next (l))
76 DRouteInterface *iface_def = (DRouteInterface *) l->data;
78 if (iface_def->get_datum)
80 datum = (*iface_def->get_datum) (path, data->user_data);
84 dbus_message_iter_append_basic (&iter_sub_array, DBUS_TYPE_STRING,
86 if (iface_def->free_datum)
87 (*iface_def->free_datum) (datum);
89 if (!dbus_message_iter_close_container (&iter_struct, &iter_sub_array))
91 name = atk_object_get_name (obj);
94 dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &name);
95 role = spi_accessible_role_from_atk_role (atk_object_get_role (obj));
96 dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_UINT32, &role);
97 desc = atk_object_get_description (obj);
100 dbus_message_iter_append_basic (&iter_struct, DBUS_TYPE_STRING, &desc);
101 if (!dbus_message_iter_close_container (iter_array, &iter_struct))
103 if (!include_children) childcount = 0;
104 for (i = 0; i < childcount; i++)
106 AtkObject *child = atk_object_ref_accessible_child (obj, i);
110 result = append_update (iter_array, child, TRUE, data);
111 g_object_unref (child);
118 if (path) g_free(path);
123 spi_dbus_append_tree (DBusMessage * message, AtkObject * obj,
126 DBusMessageIter iter, iter_array;
129 dbus_message_iter_init_append (message, &iter);
130 dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "(ooaoassus)",
132 result = append_update (&iter_array, obj, TRUE, data);
134 result = dbus_message_iter_close_container (&iter, &iter_array);
139 impl_getRoot (DBusConnection * bus, DBusMessage * message, void *user_data)
141 AtkObject *root = atk_get_root();
145 if (root) path = spi_dbus_get_path(root);
147 return spi_dbus_general_error (message);
148 reply = dbus_message_new_method_return (message);
149 dbus_message_append_args (reply, DBUS_TYPE_OBJECT_PATH, &path,
156 impl_getTree (DBusConnection * bus, DBusMessage * message, void *user_data)
159 AtkObject *root = atk_get_root();
161 if (!root) return spi_dbus_general_error(message);
162 reply = dbus_message_new_method_return (message);
163 spi_dbus_append_tree (reply, root, (DRouteData *) user_data);
168 impl_introspect (DBusConnection *bus, DBusMessage *message, void *user_data)
176 path = dbus_message_get_path(message);
178 output = g_string_new(spi_introspection_header);
180 g_string_append_printf(output, spi_introspection_node_element, path);
182 spi_append_interface(output, "org.freedesktop.atspi.Tree");
184 g_string_append(output, spi_introspection_footer);
185 final = g_string_free(output, FALSE);
187 reply = dbus_message_new_method_return (message);
188 g_assert(reply != NULL);
189 dbus_message_append_args(reply, DBUS_TYPE_STRING, &final,
196 static DBusHandlerResult
197 message_handler (DBusConnection *bus, DBusMessage *message, void *user_data)
199 const char *iface = dbus_message_get_interface (message);
200 const char *member = dbus_message_get_member (message);
202 DBusMessage *reply = NULL;
204 if (!strcmp(iface, "org.freedesktop.atspi.Tree"))
206 if (!strcmp(member, "getRoot"))
208 reply = impl_getRoot(bus, message, user_data);
211 if (!strcmp(member, "getTree"))
213 reply = impl_getTree(bus, message, user_data);
217 if (!strcmp(iface, "org.freedesktop.DBus.Introspectable"))
219 if (!strcmp(member, "Introspect"))
221 reply = impl_introspect(bus, message, user_data);
227 dbus_connection_send (bus, reply, NULL);
228 dbus_message_unref (reply);
231 return DBUS_HANDLER_RESULT_HANDLED;
234 static DBusObjectPathVTable tree_vtable =
238 NULL, NULL, NULL, NULL
242 spi_register_tree_object(DBusConnection *bus,
245 dbus_bool_t mem = FALSE;
246 mem = dbus_connection_register_object_path(bus, path, &tree_vtable, NULL);
247 g_assert(mem == TRUE);
250 static GHashTable *cache_list;
253 #define UPDATE_REFRESH 2
254 #define UPDATE_REMOVE 3
256 static int update_pending = 0;
257 static gint update_pending_id;
261 DBusMessageIter iter;
266 static void handle_cache_item(char *path, guint action, CacheIterData *d)
275 if (d->removing) return;
276 obj = spi_dbus_get_object(path);
277 //printf("update %s\n", path);
278 append_update(&d->iter, obj, FALSE, d->droute);
281 //printf("remove: %s\n", path);
282 if (!d->removing) return;
283 dbus_message_iter_append_basic(&d->iter, DBUS_TYPE_OBJECT_PATH, &path);
286 g_hash_table_remove(cache_list, path);
289 gboolean spi_dbus_update_cache(DRouteData *data)
291 DBusMessage *message;
292 DBusMessageIter iter;
295 if (update_pending == 0) return FALSE;
296 //printf("Sending cache\n");
297 message = dbus_message_new_signal("/org/freedesktop/atspi/tree", "org.freedesktop.atspi.Tree", "UpdateTree");
298 if (!message) goto done;
299 dbus_message_iter_init_append (message, &iter);
300 dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "(ooaoassus)",
306 /* This loop is needed because appending an item may cause new children
307 * to be registered and consequently added to the hash, so they, too,
308 * will need to be sent with the update */
310 g_hash_table_foreach(cache_list, (GHFunc)handle_cache_item, &d);
311 } while (update_pending);
312 dbus_message_iter_close_container(&iter, &d.iter);
313 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "o", &d.iter);
315 g_hash_table_foreach(cache_list, (GHFunc)handle_cache_item, &d);
316 dbus_message_iter_close_container(&iter, &d.iter);
317 dbus_connection_send(data->bus, message, NULL);
322 void spi_dbus_notify_change(AtkObject *obj, gboolean new, DRouteData *data)
324 guint action = (new? UPDATE_NEW: UPDATE_REFRESH);
325 char *path = spi_dbus_get_path(obj);
329 cache_list = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
336 if (g_hash_table_lookup(cache_list, path))
341 //printf("change: %s\n", path);
342 g_hash_table_insert(cache_list, path, (gpointer)action);
343 if (update_pending != 2 && data)
345 update_pending_id = g_idle_add((GSourceFunc)spi_dbus_update_cache, data);
348 else if (!update_pending) update_pending = 1;
351 void spi_dbus_notify_remove(AtkObject *obj, DRouteData *data)
353 guint action = UPDATE_REMOVE;
355 gchar *path = spi_dbus_get_path(obj);
357 //printf("notify remove: %s\n", path);
363 cur_action = (guint)g_hash_table_lookup(cache_list, path);
364 if (cur_action == UPDATE_NEW)
366 /* No one knew that this object ever existed, so just remove it */
367 //printf("Removing object from send queue\n");
368 g_hash_table_remove(cache_list, path);
373 g_hash_table_insert(cache_list, path, (gpointer)action);
374 if (update_pending != 2 && data)
376 update_pending_id = g_idle_add((GSourceFunc)spi_dbus_update_cache, data);
379 else if (!update_pending) update_pending = 1;
383 gboolean spi_dbus_object_is_known(AtkObject *obj)
386 char *path = spi_dbus_get_path(obj);
387 if (!path) return FALSE;
388 cur_action = (guint)g_hash_table_lookup(cache_list, path);
390 return (cur_action != UPDATE_NEW);