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"
35 static void handle_get_items (DBusPendingCall *pending, void *user_data);
37 static DBusConnection *bus = NULL;
38 static GHashTable *apps = NULL;
39 static GHashTable *live_refs = NULL;
40 static GQueue *exception_handlers = NULL;
41 static DBusError exception;
43 const char *atspi_path_dec = ATSPI_DBUS_PATH_DEC;
44 const char *atspi_path_registry = ATSPI_DBUS_PATH_REGISTRY;
45 const char *atspi_path_root = ATSPI_DBUS_PATH_ROOT;
46 const char *atspi_bus_registry = ATSPI_DBUS_NAME_REGISTRY;
47 const char *atspi_interface_accessible = ATSPI_DBUS_INTERFACE_ACCESSIBLE;
48 const char *atspi_interface_action = ATSPI_DBUS_INTERFACE_ACTION;
49 const char *atspi_interface_application = ATSPI_DBUS_INTERFACE_APPLICATION;
50 const char *atspi_interface_collection = ATSPI_DBUS_INTERFACE_COLLECTION;
51 const char *atspi_interface_component = ATSPI_DBUS_INTERFACE_COMPONENT;
52 const char *atspi_interface_dec = ATSPI_DBUS_INTERFACE_DEC;
53 const char *atspi_interface_device_event_listener = ATSPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER;
54 const char *atspi_interface_document = ATSPI_DBUS_INTERFACE_DOCUMENT;
55 const char *atspi_interface_editable_text = ATSPI_DBUS_INTERFACE_EDITABLE_TEXT;
56 const char *atspi_interface_event_object = ATSPI_DBUS_INTERFACE_EVENT_OBJECT;
57 const char *atspi_interface_hyperlink = ATSPI_DBUS_INTERFACE_HYPERLINK;
58 const char *atspi_interface_hypertext = ATSPI_DBUS_INTERFACE_HYPERTEXT;
59 const char *atspi_interface_image = ATSPI_DBUS_INTERFACE_IMAGE;
60 const char *atspi_interface_registry = ATSPI_DBUS_INTERFACE_REGISTRY;
61 const char *atspi_interface_selection = ATSPI_DBUS_INTERFACE_SELECTION;
62 const char *atspi_interface_table = ATSPI_DBUS_INTERFACE_TABLE;
63 const char *atspi_interface_text = ATSPI_DBUS_INTERFACE_TEXT;
64 const char *atspi_interface_cache = ATSPI_DBUS_INTERFACE_CACHE;
65 const char *atspi_interface_value = ATSPI_DBUS_INTERFACE_VALUE;
67 static const char *interfaces[] =
69 ATSPI_DBUS_INTERFACE_ACCESSIBLE,
70 ATSPI_DBUS_INTERFACE_ACTION,
71 ATSPI_DBUS_INTERFACE_APPLICATION,
72 ATSPI_DBUS_INTERFACE_COLLECTION,
73 ATSPI_DBUS_INTERFACE_COMPONENT,
74 ATSPI_DBUS_INTERFACE_DOCUMENT,
75 ATSPI_DBUS_INTERFACE_EDITABLE_TEXT,
76 ATSPI_DBUS_INTERFACE_HYPERLINK,
77 ATSPI_DBUS_INTERFACE_HYPERTEXT,
78 ATSPI_DBUS_INTERFACE_IMAGE,
79 "org.a11y.atspi.LoginHelper",
80 ATSPI_DBUS_INTERFACE_SELECTION,
81 ATSPI_DBUS_INTERFACE_TABLE,
82 ATSPI_DBUS_INTERFACE_TEXT,
83 ATSPI_DBUS_INTERFACE_VALUE,
88 _atspi_get_iface_num (const char *iface)
90 /* TODO: Use a binary search or hash to improve performance */
93 for (i = 0; interfaces[i]; i++)
95 if (!strcmp(iface, interfaces[i])) return i;
105 live_refs = g_hash_table_new (g_direct_hash, g_direct_equal);
110 /* TODO: Add an application parameter */
119 #define APP_IS_REGISTRY(app) (!strcmp (app->bus_name, atspi_bus_registry))
130 g_hash_table_destroy (refs);
134 static gboolean atspi_inited = FALSE;
136 static GHashTable *app_hash = NULL;
139 handle_get_bus_address (DBusPendingCall *pending, void *user_data)
141 AtspiApplication *app = user_data;
142 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
143 DBusMessage *message;
145 DBusPendingCall *new_pending;
147 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
149 if (dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &address,
153 dbus_error_init (&error);
154 DBusConnection *bus = dbus_connection_open (address, &error);
158 dbus_connection_unref (app->bus);
164 message = dbus_message_new_method_call (app->bus_name,
165 "/org/a11y/atspi/cache",
166 atspi_interface_cache, "GetItems");
168 dbus_connection_send_with_reply (app->bus, message, &new_pending, 2000);
169 dbus_pending_call_set_notify (new_pending, handle_get_items, app, NULL);
172 static AtspiApplication *
173 get_application (const char *bus_name)
175 AtspiApplication *app = NULL;
177 DBusMessage *message;
179 DBusPendingCall *pending = NULL;
183 app_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_hash_table_unref);
184 if (!app_hash) return NULL;
186 app = g_hash_table_lookup (app_hash, bus_name);
188 bus_name_dup = g_strdup (bus_name);
189 if (!bus_name_dup) return NULL;
190 // TODO: change below to something that will send state-change:defunct notification if necessary */
191 app = _atspi_application_new (bus_name);
192 if (!app) return NULL;
193 app->bus_name = bus_name_dup;
194 app->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
195 app->bus = _atspi_bus ();
196 g_hash_table_insert (app_hash, bus_name_dup, app);
197 dbus_error_init (&error);
198 message = dbus_message_new_method_call (bus_name, atspi_path_root,
199 atspi_interface_application, "GetApplicationBusAddress");
201 dbus_connection_send_with_reply (app->bus, message, &pending, 2000);
202 dbus_pending_call_set_notify (pending, handle_get_bus_address, app, NULL);
206 static AtspiAccessible *
207 ref_accessible (const char *app_name, const char *path)
209 AtspiApplication *app;
212 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
215 app = get_application (app_name);
217 if (!strcmp (path, "/org/a11y/atspi/accessible/root"))
221 app->root = atspi_accessible_new (app, atspi_path_root);
222 app->root->accessible_parent = atspi_get_desktop (0);
224 return g_object_ref (app->root);
227 a = g_hash_table_lookup (app->hash, path);
230 return g_object_ref (a);
232 a = atspi_accessible_new (app, path);
235 g_hash_table_insert (app->hash, g_strdup (a->parent.path), a);
236 g_object_ref (a); /* for the hash */
240 static AtspiHyperlink *
241 ref_hyperlink (const char *app_name, const char *path)
243 AtspiApplication *app = get_application (app_name);
244 AtspiHyperlink *hyperlink;
246 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
249 hyperlink = g_hash_table_lookup (app->hash, path);
252 return g_object_ref (hyperlink);
254 hyperlink = atspi_hyperlink_new (app, path);
257 g_hash_table_insert (app->hash, hyperlink->parent.path, hyperlink);
258 /* TODO: This should be a weak ref */
259 g_object_ref (hyperlink); /* for the hash */
272 GArray *state_bitflags;
275 static DBusHandlerResult
276 handle_remove_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
278 const char *sender = dbus_message_get_sender (message);
279 AtspiApplication *app;
281 DBusMessageIter iter, iter_struct;
282 const char *signature = dbus_message_get_signature (message);
286 if (strcmp (signature, "(so)") != 0)
288 g_warning (_("AT-SPI: Unknown signature %s for RemoveAccessible"), signature);
289 return DBUS_HANDLER_RESULT_HANDLED;
292 dbus_message_iter_init (message, &iter);
293 dbus_message_iter_recurse (&iter, &iter_struct);
294 dbus_message_iter_get_basic (&iter_struct, &sender);
295 dbus_message_iter_next (&iter_struct);
296 dbus_message_iter_get_basic (&iter_struct, &path);
297 app = get_application (sender);
298 a = ref_accessible (sender, path);
300 return DBUS_HANDLER_RESULT_HANDLED;
301 g_object_run_dispose (G_OBJECT (a));
302 g_hash_table_remove (app->hash, a->parent.path);
303 g_object_unref (a); /* unref our own ref */
304 return DBUS_HANDLER_RESULT_HANDLED;
308 add_app_to_desktop (AtspiAccessible *a, const char *bus_name)
313 dbus_error_init (&error);
314 AtspiAccessible *obj = ref_accessible (bus_name, atspi_path_root);
317 GList *new_list = g_list_append (a->children, obj);
320 a->children = new_list;
326 g_warning (_("AT-SPI: Error calling getRoot for %s: %s"), bus_name, error.message);
332 send_children_changed (AtspiAccessible *parent, AtspiAccessible *child, gboolean add)
336 memset (&e, 0, sizeof (e));
337 e.type = (add? "object:children-changed:add": "object:children-changed:remove");
339 e.detail1 = g_list_index (parent->children, child);
341 _atspi_send_event (&e);
345 unref_object_and_descendants (AtspiAccessible *obj)
349 for (l = obj->children; l; l = l->next)
351 unref_object_and_descendants (l->data);
353 g_object_unref (obj);
357 remove_app_from_desktop (AtspiAccessible *a, const char *bus_name)
360 AtspiAccessible *child;
362 for (l = a->children; l; l = l->next)
365 if (!strcmp (bus_name, child->parent.app->bus_name)) break;
371 send_children_changed (a, child, FALSE);
372 a->children = g_list_remove (a->children, child);
373 unref_object_and_descendants (child);
377 static AtspiAccessible *desktop;
380 get_reference_from_iter (DBusMessageIter *iter, const char **app_name, const char **path)
382 DBusMessageIter iter_struct;
384 dbus_message_iter_recurse (iter, &iter_struct);
385 dbus_message_iter_get_basic (&iter_struct, app_name);
386 dbus_message_iter_next (&iter_struct);
387 dbus_message_iter_get_basic (&iter_struct, path);
388 dbus_message_iter_next (iter);
392 add_accessible_from_iter (DBusMessageIter *iter)
396 DBusMessageIter iter_struct, iter_array;
397 const char *app_name, *path;
398 AtspiApplication *app;
399 AtspiAccessible *accessible;
400 const char *name, *description;
403 dbus_message_iter_recurse (iter, &iter_struct);
406 get_reference_from_iter (&iter_struct, &app_name, &path);
407 accessible = ref_accessible (app_name, path);
411 /* Get application: TODO */
412 dbus_message_iter_next (&iter_struct);
415 get_reference_from_iter (&iter_struct, &app_name, &path);
416 if (accessible->accessible_parent)
417 g_object_unref (accessible->accessible_parent);
418 accessible->accessible_parent = ref_accessible (app_name, path);
421 while (accessible->children)
423 g_object_unref (accessible->children->data);
424 accessible->children = g_list_remove (accessible->children, accessible->children->data);
426 dbus_message_iter_recurse (&iter_struct, &iter_array);
427 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
429 AtspiAccessible *child;
430 get_reference_from_iter (&iter_array, &app_name, &path);
431 child = ref_accessible (app_name, path);
432 new_list = g_list_append (accessible->children, child);
433 if (new_list) accessible->children = new_list;
437 dbus_message_iter_next (&iter_struct);
438 _atspi_dbus_set_interfaces (accessible, &iter_struct);
439 dbus_message_iter_next (&iter_struct);
442 if (accessible->name)
443 g_free (accessible->name);
444 dbus_message_iter_get_basic (&iter_struct, &name);
445 accessible->name = g_strdup (name);
446 dbus_message_iter_next (&iter_struct);
449 dbus_message_iter_get_basic (&iter_struct, &role);
450 accessible->role = role;
451 dbus_message_iter_next (&iter_struct);
454 if (accessible->description)
455 g_free (accessible->description);
456 dbus_message_iter_get_basic (&iter_struct, &description);
457 accessible->description = g_strdup (description);
458 dbus_message_iter_next (&iter_struct);
460 _atspi_dbus_set_state (accessible, &iter_struct);
461 dbus_message_iter_next (&iter_struct);
463 accessible->cached_properties |= ATSPI_CACHE_NAME | ATSPI_CACHE_ROLE | ATSPI_CACHE_PARENT;
464 if (!atspi_state_set_contains (accessible->states,
465 ATSPI_STATE_MANAGES_DESCENDANTS))
466 accessible->cached_properties |= ATSPI_CACHE_CHILDREN;
468 /* This is a bit of a hack since the cache holds a ref, so we don't need
469 * the one provided for us anymore */
470 g_object_unref (accessible);
474 handle_get_items (DBusPendingCall *pending, void *user_data)
476 AtspiApplication *app = user_data;
477 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
478 DBusMessageIter iter, iter_array;
480 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
482 const char *sender = dbus_message_get_sender (reply);
483 const char *error = NULL;
484 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &error,
486 g_warning (_("AT-SPI: Error in GetItems, sender=%s, error=%s"), sender, error);
487 dbus_message_unref (reply);
491 dbus_message_iter_init (reply, &iter);
492 dbus_message_iter_recurse (&iter, &iter_array);
493 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
495 add_accessible_from_iter (&iter_array);
496 dbus_message_iter_next (&iter_array);
498 dbus_message_unref (reply);
501 /* TODO: Do we stil need this function? */
502 static AtspiAccessible *
503 ref_accessible_desktop (AtspiApplication *app)
509 DBusMessage *message, *reply;
510 DBusMessageIter iter, iter_array;
515 g_object_ref (desktop);
518 desktop = atspi_accessible_new (app, atspi_path_root);
523 g_hash_table_insert (app->hash, desktop->parent.path, desktop);
524 g_object_ref (desktop); /* for the hash */
525 desktop->name = g_strdup ("main");
526 dbus_error_init (&error);
527 message = dbus_message_new_method_call (atspi_bus_registry,
529 atspi_interface_accessible,
533 reply = _atspi_dbus_send_with_reply_and_block (message);
534 if (!reply || strcmp (dbus_message_get_signature (reply), "a(so)") != 0)
536 g_error ("Couldn't get application list: %s", error.message);
538 dbus_message_unref (reply);
541 dbus_message_iter_init (reply, &iter);
542 dbus_message_iter_recurse (&iter, &iter_array);
543 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
545 const char *app_name, *path;
546 get_reference_from_iter (&iter_array, &app_name, &path);
547 add_app_to_desktop (desktop, app_name);
549 dbus_message_unref (reply);
551 /* Record the alternate name as an alias for org.a11y.atspi.Registry */
552 bus_name_dup = g_strdup (dbus_message_get_sender (reply));
554 g_hash_table_insert (app_hash, bus_name_dup, app);
560 _atspi_ref_accessible (const char *app, const char *path)
562 AtspiApplication *a = get_application (app);
564 if ( APP_IS_REGISTRY(a))
566 return ref_accessible_desktop (a);
568 return ref_accessible (app, path);
572 _atspi_dbus_return_accessible_from_message (DBusMessage *message)
574 DBusMessageIter iter;
575 AtspiAccessible *retval = NULL;
576 const char *signature;
581 signature = dbus_message_get_signature (message);
582 if (!strcmp (signature, "(so)"))
584 dbus_message_iter_init (message, &iter);
585 retval = _atspi_dbus_return_accessible_from_iter (&iter);
589 g_warning (_("AT-SPI: Called _atspi_dbus_return_accessible_from_message with strange signature %s"), signature);
591 dbus_message_unref (message);
596 _atspi_dbus_return_accessible_from_iter (DBusMessageIter *iter)
598 const char *app_name, *path;
600 get_reference_from_iter (iter, &app_name, &path);
601 return ref_accessible (app_name, path);
605 _atspi_dbus_return_hyperlink_from_message (DBusMessage *message)
607 DBusMessageIter iter;
608 AtspiHyperlink *retval = NULL;
609 const char *signature = dbus_message_get_signature (message);
611 if (!strcmp (signature, "(so)"))
613 dbus_message_iter_init (message, &iter);
614 retval = _atspi_dbus_return_hyperlink_from_iter (&iter);
618 g_warning (_("AT-SPI: Called _atspi_dbus_return_hyperlink_from_message with strange signature %s"), signature);
620 dbus_message_unref (message);
625 _atspi_dbus_return_hyperlink_from_iter (DBusMessageIter *iter)
627 const char *app_name, *path;
629 get_reference_from_iter (iter, &app_name, &path);
630 return ref_hyperlink (app_name, path);
633 const char *cache_signal_type = "((so)(so)(so)a(so)assusau)";
635 static DBusHandlerResult
636 handle_add_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
638 DBusMessageIter iter;
639 const char *sender = dbus_message_get_sender (message);
640 AtspiApplication *app = get_application (sender);
641 const char *type = cache_signal_type;
643 if (strcmp (dbus_message_get_signature (message), cache_signal_type) != 0)
645 g_warning (_("AT-SPI: AddAccessible with unknown signature %s\n"),
646 dbus_message_get_signature (message));
650 dbus_message_iter_init (message, &iter);
651 add_accessible_from_iter (&iter);
657 DBusMessage *message;
661 static guint process_deferred_messages_id = -1;
664 process_deferred_message (BusDataClosure *closure)
666 int type = dbus_message_get_type (closure->message);
667 const char *interface = dbus_message_get_interface (closure->message);
668 const char *member = dbus_message_get_member (closure->message);
672 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
673 !strncmp (interface, "org.a11y.atspi.Event.", 21))
675 atspi_dbus_handle_event (closure->bus, closure->message, closure->data);
677 if (dbus_message_is_method_call (closure->message, atspi_interface_device_event_listener, "NotifyEvent"))
679 atspi_dbus_handle_DeviceEvent (closure->bus,
680 closure->message, closure->data);
682 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "AddAccessible"))
684 handle_add_accessible (closure->bus, closure->message, closure->data);
686 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "RemoveAccessible"))
688 handle_remove_accessible (closure->bus, closure->message, closure->data);
692 static GList *deferred_messages = NULL;
695 _atspi_process_deferred_messages (gpointer data)
697 static int in_process_deferred_messages = 0;
699 if (in_process_deferred_messages)
701 in_process_deferred_messages = 1;
702 while (deferred_messages != NULL)
704 BusDataClosure *closure = deferred_messages->data;
705 process_deferred_message (closure);
706 deferred_messages = g_list_remove (deferred_messages, closure);
707 dbus_message_unref (closure->message);
708 dbus_connection_unref (closure->bus);
711 /* If data is NULL, assume that we were called from GLib */
713 process_deferred_messages_id = -1;
714 in_process_deferred_messages = 0;
718 static DBusHandlerResult
719 defer_message (DBusConnection *connection, DBusMessage *message, void *user_data)
721 BusDataClosure *closure = g_new (BusDataClosure, 1);
725 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
726 closure->bus = dbus_connection_ref (bus);
727 closure->message = dbus_message_ref (message);
728 closure->data = user_data;
730 new_list = g_list_append (deferred_messages, closure);
732 deferred_messages = new_list;
734 if (process_deferred_messages_id == -1)
735 process_deferred_messages_id = g_idle_add (_atspi_process_deferred_messages, NULL);
736 return DBUS_HANDLER_RESULT_HANDLED;
739 static DBusHandlerResult
740 atspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
742 int type = dbus_message_get_type (message);
743 const char *interface = dbus_message_get_interface (message);
744 const char *member = dbus_message_get_member (message);
748 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
749 !strncmp (interface, "org.a11y.atspi.Event.", 21))
751 return defer_message (bus, message, data);
753 if (dbus_message_is_method_call (message, atspi_interface_device_event_listener, "NotifyEvent"))
755 return defer_message (bus, message, data);
757 if (dbus_message_is_signal (message, atspi_interface_cache, "AddAccessible"))
759 return defer_message (bus, message, data);
761 if (dbus_message_is_signal (message, atspi_interface_cache, "RemoveAccessible"))
763 return defer_message (bus, message, data);
765 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
768 static const char *signal_interfaces[] =
770 "org.a11y.atspi.Event.Object",
771 "org.a11y.atspi.Event.Window",
772 "org.a11y.atspi.Event.Mouse",
773 "org.a11y.atspi.Event.Terminal",
774 "org.a11y.atspi.Event.Document",
775 "org.a11y.atspi.Event.Focus",
780 * Returns a 'canonicalized' value for DISPLAY,
781 * with the screen number stripped off if present.
783 * TODO: Avoid having duplicate functions for this here and in at-spi2-atk
786 spi_display_name (void)
788 static const char *canonical_display_name = NULL;
789 if (!canonical_display_name)
791 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
794 display_env = g_getenv ("DISPLAY");
795 if (!display_env || !display_env[0])
796 canonical_display_name = ":0";
799 gchar *display_p, *screen_p;
800 canonical_display_name = g_strdup (display_env);
801 display_p = g_utf8_strrchr (canonical_display_name, -1, ':');
802 screen_p = g_utf8_strrchr (canonical_display_name, -1, '.');
803 if (screen_p && display_p && (screen_p > display_p))
811 canonical_display_name = display_env;
814 return canonical_display_name;
817 /* TODO: Avoid having duplicate functions for this here and in at-spi2-atk */
818 static DBusConnection *
819 get_accessibility_bus ()
823 Display *bridge_display;
825 unsigned char *data = NULL;
826 unsigned long nitems;
827 unsigned long leftover;
829 DBusConnection *bus = NULL;
832 bridge_display = XOpenDisplay (spi_display_name ());
835 g_warning (_("AT-SPI: Could not get the display\n"));
839 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
840 XGetWindowProperty (bridge_display,
841 XDefaultRootWindow (bridge_display),
843 (long) BUFSIZ, False,
844 (Atom) 31, &actual_type, &actual_format,
845 &nitems, &leftover, &data);
847 dbus_error_init (&error);
852 (_("AT-SPI: Accessibility bus not found - Using session bus.\n"));
853 bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
856 g_warning (_("AT-SPI: Couldn't connect to bus: %s\n"), error.message);
862 bus = dbus_connection_open (data, &error);
865 g_warning (_("AT-SPI: Couldn't connect to bus: %s\n"), error.message);
870 if (!dbus_bus_register (bus, &error))
872 g_warning (_("AT-SPI: Couldn't register with bus: %s\n"), error.message);
884 * Connects to the accessibility registry and initializes the SPI.
886 * Returns: 0 on success, otherwise an integer error code.
906 dbus_error_init (&error);
907 bus = get_accessibility_bus ();
910 g_error ("Couldn't get session bus");
913 dbus_bus_register (bus, &error);
914 dbus_connection_setup_with_g_main(bus, g_main_context_default());
915 dbus_connection_add_filter (bus, atspi_dbus_filter, NULL, NULL);
916 dbind_set_timeout (1000);
917 match = g_strdup_printf ("type='signal',interface='%s',member='AddAccessible'", atspi_interface_cache);
918 dbus_error_init (&error);
919 dbus_bus_add_match (bus, match, &error);
921 match = g_strdup_printf ("type='signal',interface='%s',member='RemoveAccessible'", atspi_interface_cache);
922 dbus_bus_add_match (bus, match, &error);
924 match = g_strdup_printf ("type='signal',interface='%s',member='ChildrenChanged'", atspi_interface_event_object);
925 dbus_bus_add_match (bus, match, &error);
927 match = g_strdup_printf ("type='signal',interface='%s',member='PropertyChange'", atspi_interface_event_object);
928 dbus_bus_add_match (bus, match, &error);
930 match = g_strdup_printf ("type='signal',interface='%s',member='StateChanged'", atspi_interface_event_object);
931 dbus_bus_add_match (bus, match, &error);
936 static GMainLoop *mainloop;
941 * Starts/enters the main event loop for the AT-SPI services.
943 * (NOTE: This method does not return control, it is exited via a call to
944 * atspi_event_quit () from within an event handler).
948 atspi_event_main (void)
950 mainloop = g_main_loop_new (NULL, FALSE);
951 g_main_loop_run (mainloop);
957 * Quits the last main event loop for the SPI services,
958 * see atspi_event_main
961 atspi_event_quit (void)
963 g_main_loop_quit (mainloop);
969 * Disconnects from the Accessibility Registry and releases
970 * any floating resources. Call only once at exit.
972 * Returns: 0 if there were no leaks, otherwise non zero.
984 atspi_inited = FALSE;
988 leaked = g_hash_table_size (live_refs);
1001 _atspi_dbus_call (gpointer obj, const char *interface, const char *method, GError **error, const char *type, ...)
1006 AtspiObject *aobj = ATSPI_OBJECT (obj);
1008 if (!aobj->app || !aobj->app->bus)
1010 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
1011 _("The application no longer exists"));
1015 va_start (args, type);
1016 dbus_error_init (&err);
1017 retval = dbind_method_call_reentrant_va (aobj->app->bus, aobj->app->bus_name,
1018 aobj->path, interface, method, &err,
1021 _atspi_process_deferred_messages ((gpointer)TRUE);
1022 if (dbus_error_is_set (&err))
1024 /* TODO: Set gerror */
1025 dbus_error_free (&err);
1031 _atspi_dbus_call_partial (gpointer obj,
1032 const char *interface,
1035 const char *type, ...)
1039 va_start (args, type);
1040 return _atspi_dbus_call_partial_va (obj, interface, method, error, type, args);
1044 _atspi_dbus_call_partial_va (gpointer obj,
1045 const char *interface,
1051 AtspiObject *aobj = ATSPI_OBJECT (obj);
1054 DBusMessage *msg = NULL, *reply = NULL;
1055 DBusMessageIter iter;
1058 dbus_error_init (&err);
1060 if (!aobj->app || !aobj->app->bus)
1062 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
1063 _("The application no longer exists"));
1067 msg = dbus_message_new_method_call (aobj->app->bus_name, aobj->path, interface, method);
1072 dbus_message_iter_init_append (msg, &iter);
1073 dbind_any_marshal_va (&iter, &p, args);
1075 reply = dbind_send_and_allow_reentry (aobj->app->bus, msg, &err);
1078 _atspi_process_deferred_messages ((gpointer)TRUE);
1079 if (dbus_error_is_set (&err))
1081 /* TODO: Set gerror */
1082 dbus_error_free (&err);
1088 _atspi_dbus_get_property (gpointer obj, const char *interface, const char *name, GError **error, const char *type, void *data)
1090 DBusMessage *message, *reply;
1091 DBusMessageIter iter, iter_variant;
1093 dbus_bool_t retval = FALSE;
1094 AtspiObject *aobj = ATSPI_OBJECT (obj);
1099 if (!aobj->app || !aobj->app->bus)
1101 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
1102 _("The application no longer exists"));
1106 message = dbus_message_new_method_call (aobj->app->bus_name,
1108 "org.freedesktop.DBus.Properties",
1112 // TODO: throw exception
1115 dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1116 dbus_error_init (&err);
1117 reply = dbind_send_and_allow_reentry (aobj->app->bus, message, &err);
1118 dbus_message_unref (message);
1119 _atspi_process_deferred_messages ((gpointer)TRUE);
1122 // TODO: throw exception
1125 dbus_message_iter_init (reply, &iter);
1126 if (dbus_message_iter_get_arg_type (&iter) != 'v')
1128 g_warning (_("AT-SPI: expected a variant when fetching %s from interface %s; got %s\n"), name, interface, dbus_message_get_signature (reply));
1131 dbus_message_iter_recurse (&iter, &iter_variant);
1132 if (dbus_message_iter_get_arg_type (&iter_variant) != type[0])
1134 g_warning (_("atspi_dbus_get_property: Wrong type: expected %s, got %c\n"), type, dbus_message_iter_get_arg_type (&iter_variant));
1137 if (!strcmp (type, "(so)"))
1139 *((AtspiAccessible **)data) = _atspi_dbus_return_accessible_from_iter (&iter_variant);
1143 dbus_message_iter_get_basic (&iter_variant, data);
1144 dbus_message_unref (reply);
1145 if (type [0] == 's')
1146 *(char **)data = g_strdup (*(char **)data);
1154 _atspi_dbus_send_with_reply_and_block (DBusMessage *message)
1158 AtspiApplication *app;
1159 DBusConnection *bus;
1161 app = get_application (dbus_message_get_destination (message));
1162 bus = (app ? app->bus : _atspi_bus());
1163 dbus_error_init (&err);
1164 reply = dbind_send_and_allow_reentry (bus, message, &err);
1165 _atspi_process_deferred_messages ((gpointer)TRUE);
1166 dbus_message_unref (message);
1168 g_warning (_("AT-SPI: Got error: %s\n"), err.message);
1173 _atspi_dbus_return_hash_from_message (DBusMessage *message)
1175 DBusMessageIter iter;
1178 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL);
1180 dbus_message_iter_init (message, &iter);
1181 ret = _atspi_dbus_hash_from_iter (&iter);
1182 dbus_message_unref (message);
1187 _atspi_dbus_hash_from_iter (DBusMessageIter *iter)
1189 GHashTable *hash = g_hash_table_new (g_str_hash, g_str_equal);
1190 DBusMessageIter iter_array, iter_dict;
1192 dbus_message_iter_recurse (iter, &iter_array);
1193 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1195 const char *name, *value;
1196 dbus_message_iter_recurse (&iter_array, &iter_dict);
1197 dbus_message_iter_get_basic (&iter_dict, &name);
1198 dbus_message_iter_next (&iter_dict);
1199 dbus_message_iter_get_basic (&iter_dict, &value);
1200 g_hash_table_insert (hash, g_strdup (name), g_strdup (value));
1201 dbus_message_iter_next (&iter_array);
1207 _atspi_dbus_return_attribute_array_from_message (DBusMessage *message)
1209 DBusMessageIter iter;
1212 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL);
1214 dbus_message_iter_init (message, &iter);
1216 ret = _atspi_dbus_attribute_array_from_iter (&iter);
1217 dbus_message_unref (message);
1222 _atspi_dbus_attribute_array_from_iter (DBusMessageIter *iter)
1224 DBusMessageIter iter_array, iter_dict;
1225 GArray *array = g_array_new (TRUE, TRUE, sizeof (gchar *));
1228 dbus_message_iter_recurse (iter, &iter_array);
1229 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1231 const char *name, *value;
1234 dbus_message_iter_recurse (&iter_array, &iter_dict);
1235 dbus_message_iter_get_basic (&iter_dict, &name);
1236 dbus_message_iter_next (&iter_dict);
1237 dbus_message_iter_get_basic (&iter_dict, &value);
1238 str = g_strdup_printf ("%s:%s", name, value);
1239 new_array = g_array_append_val (array, str);
1242 dbus_message_iter_next (&iter_array);;
1248 _atspi_dbus_set_interfaces (AtspiAccessible *accessible, DBusMessageIter *iter)
1250 DBusMessageIter iter_array;
1252 accessible->interfaces = 0;
1253 dbus_message_iter_recurse (iter, &iter_array);
1254 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1258 dbus_message_iter_get_basic (&iter_array, &iface);
1259 if (!strcmp (iface, "org.freedesktop.DBus.Introspectable")) continue;
1260 n = _atspi_get_iface_num (iface);
1263 g_warning (_("AT-SPI: Unknown interface %s"), iface);
1266 accessible->interfaces |= (1 << n);
1267 dbus_message_iter_next (&iter_array);
1269 accessible->cached_properties |= ATSPI_CACHE_INTERFACES;
1273 _atspi_dbus_set_state (AtspiAccessible *accessible, DBusMessageIter *iter)
1275 DBusMessageIter iter_array;
1277 dbus_uint32_t *states;
1279 dbus_message_iter_recurse (iter, &iter_array);
1280 dbus_message_iter_get_fixed_array (&iter_array, &states, &count);
1283 g_warning (_("AT-SPI: expected 2 values in states array; got %d\n"), count);
1284 if (!accessible->states)
1285 accessible->states = _atspi_state_set_new_internal (accessible, 0);
1289 guint64 val = ((guint64)states [1]) << 32;
1291 if (!accessible->states)
1292 accessible->states = _atspi_state_set_new_internal (accessible, val);
1294 accessible->states->states = val;
1296 accessible->cached_properties |= ATSPI_CACHE_STATES;
1300 atspi_error_quark (void)
1302 return g_quark_from_static_string ("atspi_error");