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.
7 * Copyright 2010, 2011 Novell, 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.
27 * Basic SPI initialization and event loop function prototypes
31 #include "atspi-private.h"
35 #include "atspi-gmain.h"
39 static void handle_get_items (DBusPendingCall *pending, void *user_data);
41 static DBusConnection *bus = NULL;
42 static GHashTable *live_refs = NULL;
43 static gint method_call_timeout = 800;
44 static gint app_startup_time = 15000;
46 GMainLoop *atspi_main_loop;
47 GMainContext *atspi_main_context;
48 gboolean atspi_no_cache;
50 const char *atspi_path_dec = ATSPI_DBUS_PATH_DEC;
51 const char *atspi_path_registry = ATSPI_DBUS_PATH_REGISTRY;
52 const char *atspi_path_root = ATSPI_DBUS_PATH_ROOT;
53 const char *atspi_bus_registry = ATSPI_DBUS_NAME_REGISTRY;
54 const char *atspi_interface_accessible = ATSPI_DBUS_INTERFACE_ACCESSIBLE;
55 const char *atspi_interface_action = ATSPI_DBUS_INTERFACE_ACTION;
56 const char *atspi_interface_application = ATSPI_DBUS_INTERFACE_APPLICATION;
57 const char *atspi_interface_collection = ATSPI_DBUS_INTERFACE_COLLECTION;
58 const char *atspi_interface_component = ATSPI_DBUS_INTERFACE_COMPONENT;
59 const char *atspi_interface_dec = ATSPI_DBUS_INTERFACE_DEC;
60 const char *atspi_interface_device_event_listener = ATSPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER;
61 const char *atspi_interface_document = ATSPI_DBUS_INTERFACE_DOCUMENT;
62 const char *atspi_interface_editable_text = ATSPI_DBUS_INTERFACE_EDITABLE_TEXT;
63 const char *atspi_interface_event_object = ATSPI_DBUS_INTERFACE_EVENT_OBJECT;
64 const char *atspi_interface_hyperlink = ATSPI_DBUS_INTERFACE_HYPERLINK;
65 const char *atspi_interface_hypertext = ATSPI_DBUS_INTERFACE_HYPERTEXT;
66 const char *atspi_interface_image = ATSPI_DBUS_INTERFACE_IMAGE;
67 const char *atspi_interface_registry = ATSPI_DBUS_INTERFACE_REGISTRY;
68 const char *atspi_interface_selection = ATSPI_DBUS_INTERFACE_SELECTION;
69 const char *atspi_interface_table = ATSPI_DBUS_INTERFACE_TABLE;
70 const char *atspi_interface_text = ATSPI_DBUS_INTERFACE_TEXT;
71 const char *atspi_interface_cache = ATSPI_DBUS_INTERFACE_CACHE;
72 const char *atspi_interface_value = ATSPI_DBUS_INTERFACE_VALUE;
74 static const char *interfaces[] =
76 ATSPI_DBUS_INTERFACE_ACCESSIBLE,
77 ATSPI_DBUS_INTERFACE_ACTION,
78 ATSPI_DBUS_INTERFACE_APPLICATION,
79 ATSPI_DBUS_INTERFACE_COLLECTION,
80 ATSPI_DBUS_INTERFACE_COMPONENT,
81 ATSPI_DBUS_INTERFACE_DOCUMENT,
82 ATSPI_DBUS_INTERFACE_EDITABLE_TEXT,
83 ATSPI_DBUS_INTERFACE_HYPERLINK,
84 ATSPI_DBUS_INTERFACE_HYPERTEXT,
85 ATSPI_DBUS_INTERFACE_IMAGE,
86 "org.a11y.atspi.LoginHelper",
87 ATSPI_DBUS_INTERFACE_SELECTION,
88 ATSPI_DBUS_INTERFACE_TABLE,
89 ATSPI_DBUS_INTERFACE_TEXT,
90 ATSPI_DBUS_INTERFACE_VALUE,
95 _atspi_get_iface_num (const char *iface)
97 /* TODO: Use a binary search or hash to improve performance */
100 for (i = 0; interfaces[i]; i++)
102 if (!strcmp(iface, interfaces[i])) return i;
108 _atspi_get_live_refs (void)
112 live_refs = g_hash_table_new (g_direct_hash, g_direct_equal);
117 /* TODO: Add an application parameter */
124 g_error ("AT-SPI: COuldn't connect to accessibility bus. Is at-spi-bus-launcher running?");
128 #define APP_IS_REGISTRY(app) (!strcmp (app->bus_name, atspi_bus_registry))
130 static AtspiAccessible *desktop;
142 g_hash_table_destroy (refs);
147 dbus_connection_close (bus);
148 dbus_connection_unref (bus);
154 for (l = desktop->children; l;)
156 GList *next = l->next;
157 AtspiAccessible *child = l->data;
158 g_object_run_dispose (G_OBJECT (child->parent.app));
159 g_object_run_dispose (G_OBJECT (child));
162 g_object_run_dispose (G_OBJECT (desktop->parent.app));
163 g_object_unref (desktop);
167 static gboolean atspi_inited = FALSE;
169 static GHashTable *app_hash = NULL;
172 handle_get_bus_address (DBusPendingCall *pending, void *user_data)
174 AtspiApplication *app = user_data;
175 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
176 DBusMessage *message;
178 DBusPendingCall *new_pending;
180 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
182 if (dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &address,
183 DBUS_TYPE_INVALID) && address [0])
188 dbus_error_init (&error);
189 bus = dbus_connection_open_private (address, &error);
194 dbus_connection_unref (app->bus);
200 g_warning ("Unable to open bus connection: %s", error.message);
201 dbus_error_free (&error);
205 dbus_message_unref (reply);
206 dbus_pending_call_unref (pending);
209 return; /* application has gone away / been disposed */
211 message = dbus_message_new_method_call (app->bus_name,
212 "/org/a11y/atspi/cache",
213 atspi_interface_cache, "GetItems");
215 dbus_connection_send_with_reply (app->bus, message, &new_pending, 2000);
216 dbus_pending_call_set_notify (new_pending, handle_get_items, app, NULL);
217 dbus_message_unref (message);
220 static AtspiApplication *
221 get_application (const char *bus_name)
223 AtspiApplication *app = NULL;
225 DBusMessage *message;
226 DBusPendingCall *pending = NULL;
230 app_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_object_unref);
231 if (!app_hash) return NULL;
233 app = g_hash_table_lookup (app_hash, bus_name);
235 bus_name_dup = g_strdup (bus_name);
236 if (!bus_name_dup) return NULL;
237 // TODO: change below to something that will send state-change:defunct notification if necessary */
238 app = _atspi_application_new (bus_name);
239 app->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
240 app->bus = dbus_connection_ref (_atspi_bus ());
241 gettimeofday (&app->time_added, NULL);
242 app->cache = ATSPI_CACHE_UNDEFINED;
243 g_hash_table_insert (app_hash, bus_name_dup, app);
244 message = dbus_message_new_method_call (bus_name, atspi_path_root,
245 atspi_interface_application, "GetApplicationBusAddress");
247 dbus_connection_send_with_reply (app->bus, message, &pending, 2000);
248 dbus_pending_call_set_notify (pending, handle_get_bus_address, app, NULL);
249 dbus_message_unref (message);
253 static AtspiAccessible *
254 ref_accessible (const char *app_name, const char *path)
256 AtspiApplication *app;
259 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
262 app = get_application (app_name);
264 if (!strcmp (path, "/org/a11y/atspi/accessible/root"))
268 app->root = _atspi_accessible_new (app, atspi_path_root);
269 app->root->accessible_parent = atspi_get_desktop (0);
270 app->root->accessible_parent->children = g_list_append (app->root->accessible_parent->children, g_object_ref (app->root));
272 return g_object_ref (app->root);
275 a = g_hash_table_lookup (app->hash, path);
278 return g_object_ref (a);
280 a = _atspi_accessible_new (app, path);
283 g_hash_table_insert (app->hash, g_strdup (a->parent.path), g_object_ref (a));
287 static AtspiHyperlink *
288 ref_hyperlink (const char *app_name, const char *path)
290 AtspiApplication *app = get_application (app_name);
291 AtspiHyperlink *hyperlink;
293 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
296 hyperlink = g_hash_table_lookup (app->hash, path);
299 return g_object_ref (hyperlink);
301 hyperlink = _atspi_hyperlink_new (app, path);
302 g_hash_table_insert (app->hash, g_strdup (hyperlink->parent.path), hyperlink);
303 /* TODO: This should be a weak ref */
304 g_object_ref (hyperlink); /* for the hash */
317 GArray *state_bitflags;
320 static DBusHandlerResult
321 handle_remove_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
323 const char *sender = dbus_message_get_sender (message);
324 AtspiApplication *app;
326 DBusMessageIter iter, iter_struct;
327 const char *signature = dbus_message_get_signature (message);
330 if (strcmp (signature, "(so)") != 0)
332 g_warning ("AT-SPI: Unknown signature %s for RemoveAccessible", signature);
333 return DBUS_HANDLER_RESULT_HANDLED;
336 dbus_message_iter_init (message, &iter);
337 dbus_message_iter_recurse (&iter, &iter_struct);
338 dbus_message_iter_get_basic (&iter_struct, &sender);
339 dbus_message_iter_next (&iter_struct);
340 dbus_message_iter_get_basic (&iter_struct, &path);
341 app = get_application (sender);
342 a = ref_accessible (sender, path);
344 return DBUS_HANDLER_RESULT_HANDLED;
345 g_object_run_dispose (G_OBJECT (a));
346 g_hash_table_remove (app->hash, a->parent.path);
347 g_object_unref (a); /* unref our own ref */
348 return DBUS_HANDLER_RESULT_HANDLED;
351 static DBusHandlerResult
352 handle_name_owner_changed (DBusConnection *bus, DBusMessage *message, void *user_data)
354 const char *name, *new, *old;
355 static gboolean registry_lost = FALSE;
357 if (!dbus_message_get_args (message, NULL,
358 DBUS_TYPE_STRING, &name,
359 DBUS_TYPE_STRING, &old,
360 DBUS_TYPE_STRING, &new,
363 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
366 if (!strcmp (name, "org.a11y.atspi.Registry"))
368 if (registry_lost && !old[0])
370 _atspi_reregister_event_listeners ();
371 _atspi_reregister_device_listeners ();
372 registry_lost = FALSE;
375 registry_lost = TRUE;
379 AtspiAccessible *desktop = atspi_get_desktop (0);
381 for (l = desktop->children; l; l = l->next)
383 AtspiAccessible *child = l->data;
384 if (!strcmp (child->parent.app->bus_name, old))
385 g_object_run_dispose (G_OBJECT (child->parent.app));
387 g_object_unref (desktop);
389 return DBUS_HANDLER_RESULT_HANDLED;
393 add_app_to_desktop (AtspiAccessible *a, const char *bus_name)
395 AtspiAccessible *obj = ref_accessible (bus_name, atspi_path_root);
396 /* The app will be added to the desktop as a side-effect of calling
398 g_object_unref (obj);
399 return (obj != NULL);
403 get_reference_from_iter (DBusMessageIter *iter, const char **app_name, const char **path)
405 DBusMessageIter iter_struct;
407 dbus_message_iter_recurse (iter, &iter_struct);
408 dbus_message_iter_get_basic (&iter_struct, app_name);
409 dbus_message_iter_next (&iter_struct);
410 dbus_message_iter_get_basic (&iter_struct, path);
411 dbus_message_iter_next (iter);
415 add_accessible_from_iter (DBusMessageIter *iter)
417 DBusMessageIter iter_struct, iter_array;
418 const char *app_name, *path;
419 AtspiAccessible *accessible;
420 const char *name, *description;
423 dbus_message_iter_recurse (iter, &iter_struct);
426 get_reference_from_iter (&iter_struct, &app_name, &path);
427 accessible = ref_accessible (app_name, path);
431 /* Get application: TODO */
432 dbus_message_iter_next (&iter_struct);
435 get_reference_from_iter (&iter_struct, &app_name, &path);
436 if (accessible->accessible_parent)
437 g_object_unref (accessible->accessible_parent);
438 accessible->accessible_parent = ref_accessible (app_name, path);
441 while (accessible->children)
443 g_object_unref (accessible->children->data);
444 accessible->children = g_list_remove (accessible->children, accessible->children->data);
446 dbus_message_iter_recurse (&iter_struct, &iter_array);
447 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
449 AtspiAccessible *child;
450 get_reference_from_iter (&iter_array, &app_name, &path);
451 child = ref_accessible (app_name, path);
452 accessible->children = g_list_append (accessible->children, child);
456 dbus_message_iter_next (&iter_struct);
457 _atspi_dbus_set_interfaces (accessible, &iter_struct);
458 dbus_message_iter_next (&iter_struct);
461 if (accessible->name)
462 g_free (accessible->name);
463 dbus_message_iter_get_basic (&iter_struct, &name);
464 accessible->name = g_strdup (name);
465 dbus_message_iter_next (&iter_struct);
468 dbus_message_iter_get_basic (&iter_struct, &role);
469 accessible->role = role;
470 dbus_message_iter_next (&iter_struct);
473 if (accessible->description)
474 g_free (accessible->description);
475 dbus_message_iter_get_basic (&iter_struct, &description);
476 accessible->description = g_strdup (description);
477 dbus_message_iter_next (&iter_struct);
479 _atspi_dbus_set_state (accessible, &iter_struct);
480 dbus_message_iter_next (&iter_struct);
482 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_NAME | ATSPI_CACHE_ROLE |
483 ATSPI_CACHE_PARENT | ATSPI_CACHE_DESCRIPTION);
484 if (!atspi_state_set_contains (accessible->states,
485 ATSPI_STATE_MANAGES_DESCENDANTS))
486 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_CHILDREN);
488 /* This is a bit of a hack since the cache holds a ref, so we don't need
489 * the one provided for us anymore */
490 g_object_unref (accessible);
494 handle_get_items (DBusPendingCall *pending, void *user_data)
496 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
497 DBusMessageIter iter, iter_array;
499 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
501 const char *sender = dbus_message_get_sender (reply);
502 const char *error = NULL;
503 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &error,
505 g_warning ("AT-SPI: Error in GetItems, sender=%s, error=%s", sender, error);
506 dbus_message_unref (reply);
507 dbus_pending_call_unref (pending);
511 dbus_message_iter_init (reply, &iter);
512 dbus_message_iter_recurse (&iter, &iter_array);
513 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
515 add_accessible_from_iter (&iter_array);
516 dbus_message_iter_next (&iter_array);
518 dbus_message_unref (reply);
519 dbus_pending_call_unref (pending);
522 /* TODO: Do we stil need this function? */
523 static AtspiAccessible *
524 ref_accessible_desktop (AtspiApplication *app)
527 DBusMessage *message, *reply;
528 DBusMessageIter iter, iter_array;
533 g_object_ref (desktop);
536 desktop = _atspi_accessible_new (app, atspi_path_root);
541 g_hash_table_insert (app->hash, g_strdup (desktop->parent.path),
542 g_object_ref (desktop));
543 app->root = g_object_ref (desktop);
544 desktop->name = g_strdup ("main");
545 message = dbus_message_new_method_call (atspi_bus_registry,
547 atspi_interface_accessible,
552 reply = _atspi_dbus_send_with_reply_and_block (message, &error);
553 if (!reply || strcmp (dbus_message_get_signature (reply), "a(so)") != 0)
557 g_warning ("Couldn't get application list: %s", error->message);
558 g_clear_error (&error);
561 dbus_message_unref (reply);
564 dbus_message_iter_init (reply, &iter);
565 dbus_message_iter_recurse (&iter, &iter_array);
566 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
568 const char *app_name, *path;
569 get_reference_from_iter (&iter_array, &app_name, &path);
570 add_app_to_desktop (desktop, app_name);
572 dbus_message_unref (reply);
574 /* Record the alternate name as an alias for org.a11y.atspi.Registry */
575 bus_name_dup = g_strdup (dbus_message_get_sender (reply));
577 g_hash_table_insert (app_hash, bus_name_dup, app);
579 return g_object_ref (desktop);
583 _atspi_ref_accessible (const char *app, const char *path)
585 AtspiApplication *a = get_application (app);
588 if ( APP_IS_REGISTRY(a))
591 g_object_unref (ref_accessible_desktop (a)); /* sets a->root */
592 return g_object_ref (a->root);
594 return ref_accessible (app, path);
598 _atspi_dbus_return_accessible_from_message (DBusMessage *message)
600 DBusMessageIter iter;
601 AtspiAccessible *retval = NULL;
602 const char *signature;
607 signature = dbus_message_get_signature (message);
608 if (!strcmp (signature, "(so)"))
610 dbus_message_iter_init (message, &iter);
611 retval = _atspi_dbus_return_accessible_from_iter (&iter);
615 g_warning ("AT-SPI: Called _atspi_dbus_return_accessible_from_message with strange signature %s", signature);
617 dbus_message_unref (message);
622 _atspi_dbus_return_accessible_from_iter (DBusMessageIter *iter)
624 const char *app_name, *path;
626 get_reference_from_iter (iter, &app_name, &path);
627 return ref_accessible (app_name, path);
631 _atspi_dbus_return_hyperlink_from_message (DBusMessage *message)
633 DBusMessageIter iter;
634 AtspiHyperlink *retval = NULL;
635 const char *signature;
640 signature = dbus_message_get_signature (message);
641 if (!strcmp (signature, "(so)"))
643 dbus_message_iter_init (message, &iter);
644 retval = _atspi_dbus_return_hyperlink_from_iter (&iter);
648 g_warning ("AT-SPI: Called _atspi_dbus_return_hyperlink_from_message with strange signature %s", signature);
650 dbus_message_unref (message);
655 _atspi_dbus_return_hyperlink_from_iter (DBusMessageIter *iter)
657 const char *app_name, *path;
659 get_reference_from_iter (iter, &app_name, &path);
660 return ref_hyperlink (app_name, path);
663 const char *cache_signal_type = "((so)(so)(so)a(so)assusau)";
665 static DBusHandlerResult
666 handle_add_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
668 DBusMessageIter iter;
670 if (strcmp (dbus_message_get_signature (message), cache_signal_type) != 0)
672 g_warning ("AT-SPI: AddAccessible with unknown signature %s\n",
673 dbus_message_get_signature (message));
674 return DBUS_HANDLER_RESULT_HANDLED;
677 dbus_message_iter_init (message, &iter);
678 add_accessible_from_iter (&iter);
679 return DBUS_HANDLER_RESULT_HANDLED;
685 DBusMessage *message;
689 static GSource *process_deferred_messages_source = NULL;
692 process_deferred_message (BusDataClosure *closure)
694 int type = dbus_message_get_type (closure->message);
695 const char *interface = dbus_message_get_interface (closure->message);
697 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
698 !strncmp (interface, "org.a11y.atspi.Event.", 21))
700 _atspi_dbus_handle_event (closure->bus, closure->message, closure->data);
702 if (dbus_message_is_method_call (closure->message, atspi_interface_device_event_listener, "NotifyEvent"))
704 _atspi_dbus_handle_DeviceEvent (closure->bus,
705 closure->message, closure->data);
707 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "AddAccessible"))
709 handle_add_accessible (closure->bus, closure->message, closure->data);
711 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "RemoveAccessible"))
713 handle_remove_accessible (closure->bus, closure->message, closure->data);
715 if (dbus_message_is_signal (closure->message, "org.freedesktop.DBus", "NameOwnerChanged"))
717 handle_name_owner_changed (closure->bus, closure->message, closure->data);
721 static GQueue *deferred_messages = NULL;
724 process_deferred_messages (void)
726 static int in_process_deferred_messages = 0;
727 BusDataClosure *closure;
729 if (in_process_deferred_messages)
731 in_process_deferred_messages = 1;
732 while ((closure = g_queue_pop_head (deferred_messages)))
734 process_deferred_message (closure);
735 dbus_message_unref (closure->message);
736 dbus_connection_unref (closure->bus);
739 in_process_deferred_messages = 0;
744 process_deferred_messages_callback (gpointer data)
746 if (process_deferred_messages ())
747 return G_SOURCE_CONTINUE;
749 process_deferred_messages_source = NULL;
750 return G_SOURCE_REMOVE;
753 static DBusHandlerResult
754 defer_message (DBusConnection *connection, DBusMessage *message, void *user_data)
756 BusDataClosure *closure = g_new (BusDataClosure, 1);
758 closure->bus = dbus_connection_ref (bus);
759 closure->message = dbus_message_ref (message);
760 closure->data = user_data;
762 g_queue_push_tail (deferred_messages, closure);
764 if (process_deferred_messages_source == NULL)
766 process_deferred_messages_source = g_idle_source_new ();
767 g_source_set_callback (process_deferred_messages_source,
768 process_deferred_messages_callback, NULL, NULL);
769 g_source_attach (process_deferred_messages_source, atspi_main_context);
772 return DBUS_HANDLER_RESULT_HANDLED;
775 static DBusHandlerResult
776 atspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
778 int type = dbus_message_get_type (message);
779 const char *interface = dbus_message_get_interface (message);
781 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
782 !strncmp (interface, "org.a11y.atspi.Event.", 21))
784 return defer_message (bus, message, data);
786 if (dbus_message_is_method_call (message, atspi_interface_device_event_listener, "NotifyEvent"))
788 return defer_message (bus, message, data);
790 if (dbus_message_is_signal (message, atspi_interface_cache, "AddAccessible"))
792 return defer_message (bus, message, data);
794 if (dbus_message_is_signal (message, atspi_interface_cache, "RemoveAccessible"))
796 return defer_message (bus, message, data);
798 if (dbus_message_is_signal (message, "org.freedesktop.DBus", "NameOwnerChanged"))
800 defer_message (bus, message, data);
801 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
803 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
807 * Returns a 'canonicalized' value for DISPLAY,
808 * with the screen number stripped off if present.
810 * TODO: Avoid having duplicate functions for this here and in at-spi2-atk
813 spi_display_name (void)
815 char *canonical_display_name = NULL;
816 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
820 display_env = g_getenv ("DISPLAY");
821 if (!display_env || !display_env[0])
825 gchar *display_p, *screen_p;
826 canonical_display_name = g_strdup (display_env);
827 display_p = g_utf8_strrchr (canonical_display_name, -1, ':');
828 screen_p = g_utf8_strrchr (canonical_display_name, -1, '.');
829 if (screen_p && display_p && (screen_p > display_p))
837 canonical_display_name = g_strdup (display_env);
840 return canonical_display_name;
846 * Connects to the accessibility registry and initializes the SPI.
848 * Returns: 0 on success, 1 if already initialized, or an integer error code.
854 const gchar *no_cache;
863 _atspi_get_live_refs();
865 bus = atspi_get_a11y_bus ();
868 dbus_bus_register (bus, NULL);
869 atspi_dbus_connection_setup_with_g_main(bus, g_main_context_default());
870 dbus_connection_add_filter (bus, atspi_dbus_filter, NULL, NULL);
871 match = g_strdup_printf ("type='signal',interface='%s',member='AddAccessible'", atspi_interface_cache);
872 dbus_bus_add_match (bus, match, NULL);
874 match = g_strdup_printf ("type='signal',interface='%s',member='RemoveAccessible'", atspi_interface_cache);
875 dbus_bus_add_match (bus, match, NULL);
877 match = g_strdup_printf ("type='signal',interface='%s',member='ChildrenChanged'", atspi_interface_event_object);
878 dbus_bus_add_match (bus, match, NULL);
880 match = g_strdup_printf ("type='signal',interface='%s',member='PropertyChange'", atspi_interface_event_object);
881 dbus_bus_add_match (bus, match, NULL);
883 match = g_strdup_printf ("type='signal',interface='%s',member='StateChanged'", atspi_interface_event_object);
884 dbus_bus_add_match (bus, match, NULL);
887 dbus_bus_add_match (bus,
888 "type='signal', interface='org.freedesktop.DBus', member='NameOwnerChanged'",
891 no_cache = g_getenv ("ATSPI_NO_CACHE");
892 if (no_cache && g_strcmp0 (no_cache, "0") != 0)
893 atspi_no_cache = TRUE;
895 deferred_messages = g_queue_new ();
901 * atspi_is_initialized:
903 * Indicates whether AT-SPI has been initialized.
905 * Returns: %True if initialized; %False otherwise.
908 atspi_is_initialized ()
916 * Starts/enters the main event loop for the AT-SPI services.
918 * NOTE: This method does not return control; it is exited via a call to
919 * #atspi_event_quit from within an event handler.
923 atspi_event_main (void)
925 atspi_main_loop = g_main_loop_new (NULL, FALSE);
926 g_main_loop_run (atspi_main_loop);
927 atspi_main_loop = NULL;
933 * Quits the last main event loop for the AT-SPI services,
934 * See: #atspi_event_main
937 atspi_event_quit (void)
939 g_main_loop_quit (atspi_main_loop);
945 * Disconnects from #AtspiRegistry instances and releases
946 * any floating resources. Call only once at exit.
948 * Returns: 0 if there were no leaks, otherwise other integer values.
960 atspi_inited = FALSE;
964 leaked = g_hash_table_size (live_refs);
976 static GSList *hung_processes;
979 remove_hung_process (DBusPendingCall *pending, void *data)
982 hung_processes = g_slist_remove (hung_processes, data);
984 dbus_pending_call_unref (pending);
988 check_for_hang (DBusMessage *message, DBusError *error, DBusConnection *bus, const char *bus_name)
990 if (!message && error->name &&
991 !strcmp (error->name, "org.freedesktop.DBus.Error.NoReply"))
994 DBusMessage *message;
996 DBusPendingCall *pending = NULL;
997 for (l = hung_processes; l; l = l->next)
998 if (!strcmp (l->data, bus_name))
1000 message = dbus_message_new_method_call (bus_name, "/",
1001 "org.freedesktop.DBus.Peer",
1005 dbus_connection_send_with_reply (bus, message, &pending, -1);
1006 dbus_message_unref (message);
1009 bus_name_dup = g_strdup (bus_name);
1010 hung_processes = g_slist_append (hung_processes, bus_name_dup);
1011 dbus_pending_call_set_notify (pending, remove_hung_process, bus_name_dup, NULL);
1016 connection_is_hung (const char *bus_name)
1020 for (l = hung_processes; l; l = l->next)
1021 if (!strcmp (l->data, bus_name))
1027 check_app (AtspiApplication *app, GError **error)
1029 if (!app || !app->bus)
1031 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
1032 _("The application no longer exists"));
1036 if (atspi_main_loop && connection_is_hung (app->bus_name))
1038 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC,
1039 "The process appears to be hung.");
1047 set_timeout (AtspiApplication *app)
1052 if (app && app_startup_time > 0)
1054 gettimeofday (&tv, NULL);
1055 diff = (tv.tv_sec - app->time_added.tv_sec) * 1000 + (tv.tv_usec - app->time_added.tv_usec) / 1000;
1056 dbind_set_timeout (MAX(method_call_timeout, app_startup_time - diff));
1059 dbind_set_timeout (method_call_timeout);
1063 _atspi_dbus_call (gpointer obj, const char *interface, const char *method, GError **error, const char *type, ...)
1068 AtspiObject *aobj = ATSPI_OBJECT (obj);
1070 if (!check_app (aobj->app, error))
1073 va_start (args, type);
1074 dbus_error_init (&err);
1075 set_timeout (aobj->app);
1076 retval = dbind_method_call_reentrant_va (aobj->app->bus, aobj->app->bus_name,
1077 aobj->path, interface, method, &err,
1080 check_for_hang (NULL, &err, aobj->app->bus, aobj->app->bus_name);
1081 process_deferred_messages ();
1082 if (dbus_error_is_set (&err))
1084 g_set_error(error, ATSPI_ERROR, ATSPI_ERROR_IPC, "%s", err.message);
1085 dbus_error_free (&err);
1091 _atspi_dbus_call_partial (gpointer obj,
1092 const char *interface,
1095 const char *type, ...)
1099 va_start (args, type);
1100 return _atspi_dbus_call_partial_va (obj, interface, method, error, type, args);
1105 _atspi_dbus_call_partial_va (gpointer obj,
1106 const char *interface,
1112 AtspiObject *aobj = ATSPI_OBJECT (obj);
1114 DBusMessage *msg = NULL, *reply = NULL;
1115 DBusMessageIter iter;
1118 dbus_error_init (&err);
1120 if (!check_app (aobj->app, error))
1123 msg = dbus_message_new_method_call (aobj->app->bus_name, aobj->path, interface, method);
1128 dbus_message_iter_init_append (msg, &iter);
1129 dbind_any_marshal_va (&iter, &p, args);
1131 set_timeout (aobj->app);
1132 reply = dbind_send_and_allow_reentry (aobj->app->bus, msg, &err);
1133 check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1137 dbus_message_unref (msg);
1138 process_deferred_messages ();
1139 if (dbus_error_is_set (&err))
1141 /* TODO: Set gerror */
1142 dbus_error_free (&err);
1145 if (reply && dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
1147 const char *err_str = NULL;
1148 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &err_str, DBUS_TYPE_INVALID);
1150 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err_str);
1151 dbus_message_unref (reply);
1159 _atspi_dbus_get_property (gpointer obj, const char *interface, const char *name, GError **error, const char *type, void *data)
1161 DBusMessage *message, *reply;
1162 DBusMessageIter iter, iter_variant;
1164 dbus_bool_t retval = FALSE;
1165 AtspiObject *aobj = ATSPI_OBJECT (obj);
1166 char expected_type = (type [0] == '(' ? 'r' : type [0]);
1171 if (!check_app (aobj->app, error))
1174 message = dbus_message_new_method_call (aobj->app->bus_name,
1176 "org.freedesktop.DBus.Properties",
1180 // TODO: throw exception
1183 dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1184 dbus_error_init (&err);
1185 set_timeout (aobj->app);
1186 reply = dbind_send_and_allow_reentry (aobj->app->bus, message, &err);
1187 check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1188 dbus_message_unref (message);
1189 process_deferred_messages ();
1192 // TODO: throw exception
1196 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
1198 const char *err_str = NULL;
1199 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &err_str, DBUS_TYPE_INVALID);
1201 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err_str);
1205 dbus_message_iter_init (reply, &iter);
1206 if (dbus_message_iter_get_arg_type (&iter) != 'v')
1208 g_warning ("AT-SPI: expected a variant when fetching %s from interface %s; got %s\n", name, interface, dbus_message_get_signature (reply));
1211 dbus_message_iter_recurse (&iter, &iter_variant);
1212 if (dbus_message_iter_get_arg_type (&iter_variant) != expected_type)
1214 g_warning ("atspi_dbus_get_property: Wrong type: expected %s, got %c\n", type, dbus_message_iter_get_arg_type (&iter_variant));
1217 if (!strcmp (type, "(so)"))
1219 *((AtspiAccessible **)data) = _atspi_dbus_return_accessible_from_iter (&iter_variant);
1223 dbus_message_iter_get_basic (&iter_variant, data);
1224 if (type [0] == 's')
1225 *(char **)data = g_strdup (*(char **)data);
1229 dbus_error_free (&err);
1231 dbus_message_unref (reply);
1236 _atspi_dbus_send_with_reply_and_block (DBusMessage *message, GError **error)
1240 AtspiApplication *app;
1241 DBusConnection *bus;
1243 app = get_application (dbus_message_get_destination (message));
1245 if (app && !app->bus)
1246 return NULL; /* will fail anyway; app has been disposed */
1248 bus = (app ? app->bus : _atspi_bus());
1249 dbus_error_init (&err);
1251 reply = dbind_send_and_allow_reentry (bus, message, &err);
1252 process_deferred_messages ();
1253 dbus_message_unref (message);
1254 if (dbus_error_is_set (&err))
1257 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err.message);
1258 dbus_error_free (&err);
1264 _atspi_dbus_return_hash_from_message (DBusMessage *message)
1266 DBusMessageIter iter;
1272 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1274 dbus_message_iter_init (message, &iter);
1275 ret = _atspi_dbus_hash_from_iter (&iter);
1276 dbus_message_unref (message);
1281 _atspi_dbus_hash_from_iter (DBusMessageIter *iter)
1283 GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
1284 (GDestroyNotify) g_free,
1285 (GDestroyNotify) g_free);
1286 DBusMessageIter iter_array, iter_dict;
1288 dbus_message_iter_recurse (iter, &iter_array);
1289 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1291 const char *name, *value;
1292 dbus_message_iter_recurse (&iter_array, &iter_dict);
1293 dbus_message_iter_get_basic (&iter_dict, &name);
1294 dbus_message_iter_next (&iter_dict);
1295 dbus_message_iter_get_basic (&iter_dict, &value);
1296 g_hash_table_insert (hash, g_strdup (name), g_strdup (value));
1297 dbus_message_iter_next (&iter_array);
1303 _atspi_dbus_return_attribute_array_from_message (DBusMessage *message)
1305 DBusMessageIter iter;
1311 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1313 dbus_message_iter_init (message, &iter);
1315 ret = _atspi_dbus_attribute_array_from_iter (&iter);
1316 dbus_message_unref (message);
1321 _atspi_dbus_attribute_array_from_iter (DBusMessageIter *iter)
1323 DBusMessageIter iter_array, iter_dict;
1324 GArray *array = g_array_new (TRUE, TRUE, sizeof (gchar *));
1326 dbus_message_iter_recurse (iter, &iter_array);
1327 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1329 const char *name, *value;
1331 dbus_message_iter_recurse (&iter_array, &iter_dict);
1332 dbus_message_iter_get_basic (&iter_dict, &name);
1333 dbus_message_iter_next (&iter_dict);
1334 dbus_message_iter_get_basic (&iter_dict, &value);
1335 str = g_strdup_printf ("%s:%s", name, value);
1336 array = g_array_append_val (array, str);
1337 dbus_message_iter_next (&iter_array);;
1343 _atspi_dbus_set_interfaces (AtspiAccessible *accessible, DBusMessageIter *iter)
1345 DBusMessageIter iter_array;
1347 accessible->interfaces = 0;
1348 dbus_message_iter_recurse (iter, &iter_array);
1349 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1353 dbus_message_iter_get_basic (&iter_array, &iface);
1354 if (!strcmp (iface, "org.freedesktop.DBus.Introspectable")) continue;
1355 n = _atspi_get_iface_num (iface);
1358 g_warning ("AT-SPI: Unknown interface %s", iface);
1361 accessible->interfaces |= (1 << n);
1362 dbus_message_iter_next (&iter_array);
1364 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_INTERFACES);
1368 _atspi_dbus_set_state (AtspiAccessible *accessible, DBusMessageIter *iter)
1370 DBusMessageIter iter_array;
1372 dbus_uint32_t *states;
1374 dbus_message_iter_recurse (iter, &iter_array);
1375 dbus_message_iter_get_fixed_array (&iter_array, &states, &count);
1378 g_warning ("AT-SPI: expected 2 values in states array; got %d\n", count);
1379 if (!accessible->states)
1380 accessible->states = _atspi_state_set_new_internal (accessible, 0);
1384 guint64 val = ((guint64)states [1]) << 32;
1386 if (!accessible->states)
1387 accessible->states = _atspi_state_set_new_internal (accessible, val);
1389 accessible->states->states = val;
1391 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_STATES);
1395 _atspi_error_quark (void)
1397 return g_quark_from_static_string ("atspi_error");
1401 * Gets the IOR from the XDisplay.
1405 get_accessibility_bus_address_x11 (void)
1409 Display *bridge_display = NULL;
1412 unsigned char *data_x11 = NULL;
1413 unsigned long nitems;
1414 unsigned long leftover;
1417 display_name = spi_display_name ();
1421 bridge_display = XOpenDisplay (display_name);
1422 g_free (display_name);
1424 if (!bridge_display)
1426 g_warning ("Could not open X display");
1430 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
1431 XGetWindowProperty (bridge_display,
1432 XDefaultRootWindow (bridge_display),
1434 (long) BUFSIZ, False,
1435 (Atom) 31, &actual_type, &actual_format,
1436 &nitems, &leftover, &data_x11);
1437 XCloseDisplay (bridge_display);
1439 data = g_strdup ((gchar *)data_x11);
1446 get_accessibility_bus_address_dbus (void)
1448 DBusConnection *session_bus = NULL;
1449 DBusMessage *message;
1452 char *address = NULL;
1454 session_bus = dbus_bus_get (DBUS_BUS_SESSION, NULL);
1458 message = dbus_message_new_method_call ("org.a11y.Bus",
1463 dbus_error_init (&error);
1464 reply = dbus_connection_send_with_reply_and_block (session_bus,
1468 dbus_message_unref (message);
1472 g_warning ("Error retrieving accessibility bus address: %s: %s",
1473 error.name, error.message);
1474 dbus_error_free (&error);
1479 const char *tmp_address;
1480 if (!dbus_message_get_args (reply,
1486 dbus_message_unref (reply);
1489 address = g_strdup (tmp_address);
1490 dbus_message_unref (reply);
1496 static DBusConnection *a11y_bus;
1497 static dbus_int32_t a11y_dbus_slot = -1;
1500 a11y_bus_free (void *data)
1502 if (data == a11y_bus)
1505 dbus_connection_free_data_slot (&a11y_dbus_slot);
1510 atspi_get_a11y_bus (void)
1513 char *address = NULL;
1515 if (a11y_bus && dbus_connection_get_is_connected (a11y_bus))
1518 if (a11y_dbus_slot == -1)
1519 if (!dbus_connection_allocate_data_slot (&a11y_dbus_slot))
1520 g_warning ("at-spi: Unable to allocate D-Bus slot");
1523 address = get_accessibility_bus_address_x11 ();
1526 address = get_accessibility_bus_address_dbus ();
1530 dbus_error_init (&error);
1531 a11y_bus = dbus_connection_open_private (address, &error);
1536 g_warning ("Couldn't connect to accessibility bus: %s", error.message);
1537 dbus_error_free (&error);
1542 if (!dbus_bus_register (a11y_bus, &error))
1544 g_warning ("Couldn't register with accessibility bus: %s", error.message);
1545 dbus_error_free (&error);
1546 dbus_connection_close (a11y_bus);
1547 dbus_connection_unref (a11y_bus);
1553 /* Simulate a weak ref on the bus */
1554 dbus_connection_set_data (a11y_bus, a11y_dbus_slot, a11y_bus, a11y_bus_free);
1560 * atspi_set_timeout:
1561 * @val: The timeout value, in milliseconds, or -1 to disable the timeout.
1562 * @startup_time: The amount of time, in milliseconds, to allow to pass
1563 * before enforcing timeouts on an application. Can be used to prevent
1564 * timeout exceptions if an application is likely to block for an extended
1565 * period of time on initialization. -1 can be passed to disable this
1568 * Set the timeout used for method calls. If this is not set explicitly,
1569 * a default of 0.8 ms is used.
1570 * Note that at-spi2-registryd currently uses a timeout of 3 seconds when
1571 * sending a keyboard event notification. This means that, if an AT makes
1572 * a call in response to the keyboard notification and the application
1573 * being called does not respond before the timeout is reached,
1574 * at-spi2-registryd will time out on the keyboard event notification and
1575 * pass the key onto the application (ie, reply to indicate that the key
1576 * was not consumed), so this may make it undesirable to set a timeout
1577 * larger than 3 seconds.
1579 * By default, the normal timeout is set to 800 ms, and the application startup
1580 * timeout is set to 15 seconds.
1583 atspi_set_timeout (gint val, gint startup_time)
1585 method_call_timeout = val;
1586 app_startup_time = startup_time;
1590 * atspi_set_main_context:
1591 * @cnx: The #GmainContext to use.
1593 * Sets the main loop context that AT-SPI should assume is in use when
1594 * setting an idle callback.
1595 * This function should be called by application-side implementors (ie,
1596 * at-spi2-atk) when it is desirable to re-enter the main loop.
1599 atspi_set_main_context (GMainContext *cnx)
1601 if (atspi_main_context == cnx)
1603 if (process_deferred_messages_source != NULL)
1605 g_source_destroy (process_deferred_messages_source);
1606 process_deferred_messages_source = g_idle_source_new ();
1607 g_source_set_callback (process_deferred_messages_source,
1608 process_deferred_messages_callback, NULL, NULL);
1609 g_source_attach (process_deferred_messages_source, cnx);
1611 atspi_main_context = cnx;
1612 atspi_dbus_connection_setup_with_g_main (atspi_get_a11y_bus (), cnx);
1615 #ifdef DEBUG_REF_COUNTS
1617 print_disposed (gpointer key, gpointer value, gpointer data)
1619 AtspiAccessible *accessible = key;
1620 if (accessible->parent.app)
1622 g_print ("disposed: %s %d\n", accessible->name, accessible->role);
1628 g_hash_table_foreach (live_refs, print_disposed, NULL);
1633 _atspi_name_compat (gchar *name)
1647 * atspi_role_get_name:
1648 * @role: an #AtspiRole object to query.
1650 * Gets a localizable string that indicates the name of an #AtspiRole.
1651 * <em>DEPRECATED.</em>
1653 * Returns: a localizable string name for an #AtspiRole enumerated type.
1656 atspi_role_get_name (AtspiRole role)
1658 gchar *retval = NULL;
1659 GTypeClass *type_class;
1662 type_class = g_type_class_ref (ATSPI_TYPE_ROLE);
1663 g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), NULL);
1665 value = g_enum_get_value (G_ENUM_CLASS (type_class), role);
1669 retval = g_strdup (value->value_nick);
1673 return _atspi_name_compat (retval);