2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2001, 2002 Sun Microsystems Inc.,
6 * Copyright 2001, 2002 Ximian, Inc.
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.
26 * Basic SPI initialization and event loop function prototypes
30 #include "atspi-private.h"
32 static DBusConnection *bus = NULL;
33 static GHashTable *apps = NULL;
34 static GHashTable *live_refs = NULL;
35 static GQueue *exception_handlers = NULL;
36 static DBusError exception;
38 const char *atspi_path_dec = ATSPI_DBUS_PATH_DEC;
39 const char *atspi_path_registry = ATSPI_DBUS_PATH_REGISTRY;
40 const char *atspi_path_root = ATSPI_DBUS_PATH_ROOT;
41 const char *atspi_bus_registry = ATSPI_DBUS_NAME_REGISTRY;
42 const char *atspi_interface_accessible = ATSPI_DBUS_INTERFACE_ACCESSIBLE;
43 const char *atspi_interface_action = ATSPI_DBUS_INTERFACE_ACTION;
44 const char *atspi_interface_application = ATSPI_DBUS_INTERFACE_APPLICATION;
45 const char *atspi_interface_component = ATSPI_DBUS_INTERFACE_COMPONENT;
46 const char *atspi_interface_dec = ATSPI_DBUS_INTERFACE_DEC;
47 const char *atspi_interface_device_event_listener = ATSPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER;
48 const char *atspi_interface_document = ATSPI_DBUS_INTERFACE_DOCUMENT;
49 const char *atspi_interface_editable_text = ATSPI_DBUS_INTERFACE_EDITABLE_TEXT;
50 const char *atspi_interface_hyperlink = ATSPI_DBUS_INTERFACE_HYPERLINK;
51 const char *atspi_interface_hypertext = ATSPI_DBUS_INTERFACE_HYPERTEXT;
52 const char *atspi_interface_image = ATSPI_DBUS_INTERFACE_IMAGE;
53 const char *atspi_interface_registry = ATSPI_DBUS_INTERFACE_REGISTRY;
54 const char *atspi_interface_selection = ATSPI_DBUS_INTERFACE_SELECTION;
55 const char *atspi_interface_table = ATSPI_DBUS_INTERFACE_TABLE;
56 const char *atspi_interface_text = ATSPI_DBUS_INTERFACE_TEXT;
57 const char *atspi_interface_cache = ATSPI_DBUS_INTERFACE_CACHE;
58 const char *atspi_interface_value = ATSPI_DBUS_INTERFACE_VALUE;
60 static const char *interfaces[] =
62 ATSPI_DBUS_INTERFACE_ACCESSIBLE,
63 ATSPI_DBUS_INTERFACE_ACTION,
64 ATSPI_DBUS_INTERFACE_APPLICATION,
65 ATSPI_DBUS_INTERFACE_COLLECTION,
66 ATSPI_DBUS_INTERFACE_COMPONENT,
67 ATSPI_DBUS_INTERFACE_DOCUMENT,
68 ATSPI_DBUS_INTERFACE_EDITABLE_TEXT,
69 ATSPI_DBUS_INTERFACE_HYPERLINK,
70 ATSPI_DBUS_INTERFACE_HYPERTEXT,
71 ATSPI_DBUS_INTERFACE_IMAGE,
72 "org.a11y.atspi.LoginHelper",
73 ATSPI_DBUS_INTERFACE_SELECTION,
74 ATSPI_DBUS_INTERFACE_TABLE,
75 ATSPI_DBUS_INTERFACE_TEXT,
76 ATSPI_DBUS_INTERFACE_VALUE,
80 static gint get_iface_num (const char *iface)
82 /* TODO: Use a binary search or hash to improve performance */
85 for (i = 0; interfaces[i]; i++)
87 if (!strcmp(iface, interfaces[i])) return i;
97 live_refs = g_hash_table_new (g_direct_hash, g_direct_equal);
108 #define APP_IS_REGISTRY(app) (!strcmp (app->bus_name, atspi_bus_registry))
119 g_hash_table_destroy (refs);
123 static gboolean atspi_inited = FALSE;
125 static GHashTable *app_hash = NULL;
127 static AtspiApplication *
128 get_application (const char *bus_name)
130 AtspiApplication *app = NULL;
135 app_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_hash_table_unref);
136 if (!app_hash) return NULL;
138 app = g_hash_table_lookup (app_hash, bus_name);
140 bus_name_dup = g_strdup (bus_name);
141 if (!bus_name_dup) return NULL;
142 // TODO: change below to something that will send state-change:defunct notification if necessary */
143 app = g_new (AtspiApplication, 1);
144 if (!app) return NULL;
145 app->bus_name = bus_name_dup;
146 if (APP_IS_REGISTRY (app))
148 app->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
152 app->hash = g_hash_table_new_full (g_int_hash, g_int_equal, g_free, g_object_unref);
154 g_hash_table_insert (app_hash, bus_name_dup, app);
158 static AtspiAccessible *
159 ref_accessible (const char *app_name, const char *path)
163 AtspiApplication *app = get_application (app_name);
166 if (!strcmp (path, "/org/a11y/atspi/accessible/root"))
167 return g_object_ref (app->root);
169 if (sscanf (path, "/org/a11y/atspi/accessible/%d", &id) != 1)
174 a = g_hash_table_lookup (app->hash, &id);
180 id_val = g_new (guint, 1);
181 if (!id_val) return NULL;
183 a = atspi_accessible_new ();
190 a->path = g_strdup_printf ("/org/a11y/atspi/accessible/%d", id);
191 g_hash_table_insert (app->hash, id_val, a);
192 g_object_ref (a); /* for the hash */
205 GArray *state_bitflags;
208 static DBusHandlerResult
209 handle_remove_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
211 const char *sender = dbus_message_get_sender (message);
212 AtspiApplication *app;
214 DBusMessageIter iter, iter_struct;
215 const char *signature = dbus_message_get_signature (message);
219 if (strcmp (signature, "(so)") != 0)
221 g_warning ("at-spi: Unknown signature %s for RemoveAccessible", signature);
222 return DBUS_HANDLER_RESULT_HANDLED;
225 dbus_message_iter_init (message, &iter);
226 dbus_message_iter_recurse (&iter, &iter_struct);
227 dbus_message_iter_get_basic (&iter_struct, &sender);
228 dbus_message_iter_get_basic (&iter_struct, &path);
229 app = get_application (sender);
230 a = ref_accessible (sender, path);
231 if (a->accessible_parent && g_list_find (a->accessible_parent->children, a))
233 a->accessible_parent->children = g_list_remove (a->accessible_parent->children, a);
236 if (sscanf (a->path, "/org/a11y/atspi/accessible/%d", &id) == 1)
238 g_warning("atspi: FIX HASH REMOVE");
239 g_hash_table_remove (app->hash, id);
242 g_warning ("libspi: Strange path %s\n", a->path);
243 g_object_unref (a); /* unref our own ref */
244 return DBUS_HANDLER_RESULT_HANDLED;
248 add_app_to_desktop (AtspiAccessible *a, const char *bus_name)
253 dbus_error_init (&error);
254 AtspiAccessible *obj = ref_accessible (bus_name, atspi_path_root);
257 GList *new_list = g_list_append (a->children, obj);
260 a->children = new_list;
266 g_warning ("Error calling getRoot for %s: %s", bus_name, error.message);
272 send_children_changed (AtspiAccessible *parent, AtspiAccessible *child, gboolean add)
276 memset (&e, 0, sizeof(e));
277 e.type = (add? "object:children-changed:add": "object:children-changed:remove");
279 e.detail1 = g_list_index (parent->children, child);
280 atspi_dispatch_event (&e);
284 unref_object_and_descendants (AtspiAccessible *obj)
288 for (l = obj->children; l; l = l->next)
290 unref_object_and_descendants (l->data);
292 g_object_unref_internal (obj, TRUE);
296 remove_app_from_desktop (AtspiAccessible *a, const char *bus_name)
299 AtspiAccessible *child;
301 for (l = a->children; l; l = l->next)
304 if (!strcmp (bus_name, child->app->bus_name)) break;
308 g_warning ("Removing unregistered app %s; doing nothing\n", bus_name);
311 send_children_changed (a, child, FALSE);
312 a->children = g_list_remove (a->children, child);
313 unref_object_and_descendants (child);
317 static AtspiAccessible *desktop;
320 get_reference_from_iter (DBusMessageIter *iter, const char **app_name, const char **path)
322 DBusMessageIter iter_struct;
324 dbus_message_iter_recurse (iter, &iter_struct);
325 dbus_message_iter_get_basic (&iter_struct, &app_name);
326 dbus_message_iter_get_basic (&iter_struct, &path);
327 dbus_message_iter_next (iter);
331 add_accessible_from_iter (DBusMessageIter *iter)
335 DBusMessageIter iter_struct, iter_array;
336 const char *app_name, *path;
337 AtspiApplication *app;
338 AtspiAccessible *accessible;
339 const char *name, *description;
341 dbus_uint32_t *states;
344 dbus_message_iter_recurse (iter, &iter_struct);
347 get_reference_from_iter (&iter_struct, &app_name, &path);
348 accessible = ref_accessible (app_name, path);
351 get_reference_from_iter (&iter_struct, &app_name, &path);
352 accessible->accessible_parent = ref_accessible (app_name, path);
355 dbus_message_iter_recurse (&iter_struct, &iter_array);
356 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
358 AtspiAccessible *child;
359 get_reference_from_iter (&iter_array, &app_name, &path);
360 child = ref_accessible (app_name, path);
361 new_list = g_list_append (accessible->children, child);
362 if (new_list) accessible->children = new_list;
366 accessible->interfaces = 0;
367 dbus_message_iter_next (&iter_struct);
368 dbus_message_iter_recurse (&iter_struct, &iter_array);
369 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
373 dbus_message_iter_get_basic (&iter_array, &iface);
374 if (!strcmp (iface, "org.freedesktop.DBus.Introspectable")) continue;
375 n = get_iface_num (iface);
378 g_warning ("Unknown interface %s", iface);
380 else accessible->interfaces |= (1 << n);
382 dbus_message_iter_next (&iter_struct);
385 dbus_message_iter_get_basic (&iter_struct, &name);
386 accessible->name = g_strdup (name);
387 dbus_message_iter_next (&iter_struct);
390 dbus_message_iter_get_basic (&iter_struct, &role);
391 accessible->role = role;
392 dbus_message_iter_next (&iter_struct);
395 dbus_message_iter_get_basic (&iter_struct, &description);
396 accessible->description = g_strdup (description);
397 dbus_message_iter_next (&iter_struct);
399 dbus_message_iter_recurse (&iter_struct, &iter_array);
400 dbus_message_iter_get_fixed_array (&iter_array, &states, &count);
403 g_warning ("at-spi: expected 2 values in states array; got %d\n", count);
404 accessible->states = atspi_state_set_new (0);
408 guint64 val = ((guint64)states [1]) << 32;
410 accessible->states = atspi_state_set_new (val);
412 dbus_message_iter_next (&iter_struct);
414 /* This is a bit of a hack since the cache holds a ref, so we don't need
415 * the one provided for us anymore */
416 g_object_unref (accessible);
420 add_accessibles (const char *app_name)
423 DBusMessage *message, *reply;
424 DBusMessageIter iter, iter_array;
426 AtspiApplication *app = get_application (app_name);
427 /* TODO: Move this functionality into app initializer? */
428 dbus_error_init (&error);
429 message = dbus_message_new_method_call (app_name, "/org/a11y/atspi/accessible/cache", atspi_interface_cache, "GetItems");
430 reply = _atspi_dbus_send_with_reply_and_block (message);
431 if (!reply || strcmp (dbus_message_get_signature (reply), "a((so)(so)a(so)assusau)") != 0)
433 g_warning ("at-spi: Error in GetItems");
436 dbus_message_unref (reply);
438 dbus_message_iter_init (reply, &iter);
439 dbus_message_iter_recurse (&iter, &iter_array);
440 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
442 add_accessible_from_iter (&iter_array);
443 dbus_message_iter_next (&iter_array);
445 dbus_message_unref (reply);
448 /* TODO: Do we stil need this function? */
449 static AtspiAccessible *
450 ref_accessible_desktop (AtspiApplication *app)
456 DBusMessage *message, *reply;
457 DBusMessageIter iter, iter_array;
461 g_object_ref (desktop);
464 desktop = atspi_accessible_new ();
469 g_hash_table_insert (app->hash, 0, desktop);
471 g_object_ref (desktop); /* for the hash */
472 desktop->name = g_strdup ("main");
473 dbus_error_init (&error);
474 message = dbus_message_new_method_call (atspi_bus_registry,
476 atspi_interface_accessible,
480 reply = _atspi_dbus_send_with_reply_and_block (message);
481 if (!reply || strcmp (dbus_message_get_signature (reply), "a(so") != 0)
483 g_error ("Couldn't get application list: %s", error.message);
485 dbus_message_unref (reply);
488 dbus_message_iter_init (reply, &iter);
489 dbus_message_iter_recurse (&iter, &iter_array);
490 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
492 const char *app_name, *path;
493 get_reference_from_iter (&iter_array, &app_name, &path);
494 add_accessibles (app_name);
495 add_app_to_desktop (desktop, app_name);
497 dbus_message_unref (reply);
502 _atspi_ref_accessible (const char *app, const char *path)
504 AtspiApplication *a = get_application (app);
506 if ( APP_IS_REGISTRY(a))
508 return ref_accessible_desktop (a);
510 return ref_accessible (app, path);
514 _atspi_ref_related_accessible (AtspiAccessible *obj, const AtspiReference *ref)
516 const char *app = (ref->name && ref->name[0]? ref->name: obj->app->bus_name);
517 return ref_accessible (app, obj->path);
520 const char *cache_signal_type = "((so)(so)a(so)assusau)";
522 static DBusHandlerResult
523 handle_add_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
525 DBusMessageIter iter;
526 const char *sender = dbus_message_get_sender (message);
527 AtspiApplication *app = get_application (sender);
528 const char *type = cache_signal_type;
530 if (strcmp (dbus_message_get_signature (message), cache_signal_type) != 0)
532 g_warning ("atspi: AddAccessible with unknown signature %s\n", dbus_message_get_signature (message));
536 dbus_message_iter_init (message, &iter);
537 add_accessible_from_iter (&iter);
540 static DBusHandlerResult
541 atspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
543 int type = dbus_message_get_type (message);
544 const char *interface = dbus_message_get_interface (message);
545 const char *member = dbus_message_get_member (message);
549 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
550 !strncmp (interface, "org.a11y.atspi.Event.", 28))
552 g_warning ("atspi: TODO: event");
553 //return handle_event (bus, message, data);
555 if (dbus_message_is_method_call (message, atspi_interface_device_event_listener, "notifyEvent"))
557 g_warning ("atspi: TODO: DeviceEvent");
558 //return handle_device_event (bus, message, data);
560 if (dbus_message_is_signal (message, atspi_interface_cache, "AddAccessible"))
562 return handle_add_accessible (bus, message, data);
564 if (dbus_message_is_signal (message, atspi_interface_cache, "RemoveAccessible"))
566 return handle_remove_accessible (bus, message, data);
568 /* TODO: Handle ChildrenChanged, StateChanged, PropertyChanged */
569 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
572 static const char *signal_interfaces[] =
574 "org.a11y.atspi.Event.Object",
575 "org.a11y.atspi.Event.Window",
576 "org.a11y.atspi.Event.Mouse",
577 "org.a11y.atspi.Event.Terminal",
578 "org.a11y.atspi.Event.Document",
579 "org.a11y.atspi.Event.Focus",
586 * Connects to the accessibility registry and initializes the SPI.
588 * Returns: 0 on success, otherwise an integer error code.
609 dbus_error_init (&error);
610 bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
613 g_error ("Couldn't get session bus");
616 dbus_bus_register (bus, &error);
617 dbus_connection_setup_with_g_main(bus, g_main_context_default());
618 dbus_connection_add_filter (bus, atspi_dbus_filter, NULL, NULL);
619 match = g_strdup_printf ("type='signal',interface='%s',member='AddAccessible'", atspi_interface_cache);
620 dbus_error_init (&error);
621 dbus_bus_add_match (bus, match, &error);
623 match = g_strdup_printf ("type='signal',interface='%s',member='RemoveAccessible'", atspi_interface_cache);
624 dbus_bus_add_match (bus, match, &error);
626 for (i = 0; signal_interfaces[i]; i++)
628 match = g_strdup_printf ("type='signal',interface='%s'", signal_interfaces[i]);
629 dbus_bus_add_match (bus, match, &error);
635 static GMainLoop *mainloop;
640 * Starts/enters the main event loop for the AT-SPI services.
642 * (NOTE: This method does not return control, it is exited via a call to
643 * atspi_event_quit () from within an event handler).
647 atspi_event_main (void)
649 mainloop = g_main_loop_new (NULL, FALSE);
650 g_main_loop_run (mainloop);
656 * Quits the last main event loop for the SPI services,
657 * see atspi_event_main
660 atspi_event_quit (void)
662 g_main_loop_quit (mainloop);
668 * Disconnects from the Accessibility Registry and releases
669 * any floating resources. Call only once at exit.
671 * Returns: 0 if there were no leaks, otherwise non zero.
683 atspi_inited = FALSE;
687 leaked = g_hash_table_size (live_refs);
700 _atspi_dbus_call (AtspiAccessible *obj, const char *interface, const char *method, GError **error, const char *type, ...)
706 dbus_error_init (&err);
707 va_start (args, type);
708 retval = dbind_method_call_reentrant_va (_atspi_bus(), obj->app->bus_name, obj->path, interface, method, &err, type, args);
710 if (dbus_error_is_set (&err))
712 /* TODO: Set gerror */
713 dbus_error_free (&err);
719 _atspi_dbus_call_partial (AtspiAccessible *obj, const char *interface, const char *method, GError **error, const char *type, ...)
724 DBusMessage *msg = NULL, *reply = NULL;
725 DBusMessageIter iter;
728 dbus_error_init (&err);
729 va_start (args, type);
731 msg = dbus_message_new_method_call (obj->app->bus_name, obj->path, interface, method);
736 dbus_message_iter_init_append (msg, &iter);
737 dbind_any_marshal_va (&iter, &p, args);
739 reply = dbind_send_and_allow_reentry (_atspi_bus(), msg, &err);
742 if (dbus_error_is_set (&err))
744 /* TODO: Set gerror */
745 dbus_error_free (&err);
751 _atspi_dbus_get_property (AtspiAccessible *obj, const char *interface, const char *name, GError **error, const char *type, void *data)
753 DBusMessage *message, *reply;
754 DBusMessageIter iter, iter_variant;
756 dbus_bool_t retval = FALSE;
758 message = dbus_message_new_method_call (obj->app->bus_name, obj->path, "org.freedesktop.DBus.Properties", "Get");
761 // TODO: throw exception
764 dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
765 dbus_error_init (&err);
766 reply = dbus_connection_send_with_reply_and_block (_atspi_bus(), message, 1000, &err);
767 dbus_message_unref (message);
770 // TODO: throw exception
773 dbus_message_iter_init (reply, &iter);
774 dbus_message_iter_recurse (&iter, &iter_variant);
775 if (dbus_message_iter_get_arg_type (&iter_variant) != type[0])
777 g_warning ("atspi_dbus_get_property: Wrong type: expected %s, got %c\n", type, dbus_message_iter_get_arg_type (&iter_variant));
780 dbus_message_iter_get_basic (&iter_variant, data);
781 dbus_message_unref (reply);
782 if (type[0] == 's') *(char **)data = g_strdup (*(char **)data);
789 _atspi_dbus_send_with_reply_and_block (DBusMessage *message)
794 dbus_error_init (&err);
795 g_warning ("TODO: Write _atspi_dbus_send_with_reply_and_block");
796 reply = dbus_connection_send_with_reply_and_block (_atspi_bus(), message, 1000, &err);
797 dbus_message_unref (message);
802 _atspi_dbus_hash_from_message (DBusMessage *message)
804 GHashTable *hash = g_hash_table_new (g_str_hash, g_str_equal);
805 DBusMessageIter iter, iter_array, iter_dict;
806 const char *signature;
808 signature = dbus_message_get_signature (message);
810 if (strcmp (signature, "a{ss}") != 0)
812 g_warning ("Trying to get hash from message of unexpected type %s\n", signature);
816 dbus_message_iter_init (message, &iter);
817 dbus_message_iter_recurse (&iter, &iter_array);
820 const char *name, *value;
821 dbus_message_iter_recurse (&iter_array, &iter_dict);
822 dbus_message_iter_get_basic (&iter_dict, &name);
823 dbus_message_iter_get_basic (&iter_dict, &value);
824 g_hash_table_insert (hash, g_strdup (name), g_strdup (value));
825 } while (dbus_message_iter_next (&iter_array));