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"
33 #include "atspi-gmain.h"
37 static void handle_get_items (DBusPendingCall *pending, void *user_data);
39 static DBusConnection *bus = NULL;
40 static GHashTable *live_refs = NULL;
41 static gint method_call_timeout = 800;
42 static gint app_startup_time = 15000;
44 GMainLoop *atspi_main_loop;
45 gboolean atspi_no_cache;
47 const char *atspi_path_dec = ATSPI_DBUS_PATH_DEC;
48 const char *atspi_path_registry = ATSPI_DBUS_PATH_REGISTRY;
49 const char *atspi_path_root = ATSPI_DBUS_PATH_ROOT;
50 const char *atspi_bus_registry = ATSPI_DBUS_NAME_REGISTRY;
51 const char *atspi_interface_accessible = ATSPI_DBUS_INTERFACE_ACCESSIBLE;
52 const char *atspi_interface_action = ATSPI_DBUS_INTERFACE_ACTION;
53 const char *atspi_interface_application = ATSPI_DBUS_INTERFACE_APPLICATION;
54 const char *atspi_interface_collection = ATSPI_DBUS_INTERFACE_COLLECTION;
55 const char *atspi_interface_component = ATSPI_DBUS_INTERFACE_COMPONENT;
56 const char *atspi_interface_dec = ATSPI_DBUS_INTERFACE_DEC;
57 const char *atspi_interface_device_event_listener = ATSPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER;
58 const char *atspi_interface_document = ATSPI_DBUS_INTERFACE_DOCUMENT;
59 const char *atspi_interface_editable_text = ATSPI_DBUS_INTERFACE_EDITABLE_TEXT;
60 const char *atspi_interface_event_object = ATSPI_DBUS_INTERFACE_EVENT_OBJECT;
61 const char *atspi_interface_hyperlink = ATSPI_DBUS_INTERFACE_HYPERLINK;
62 const char *atspi_interface_hypertext = ATSPI_DBUS_INTERFACE_HYPERTEXT;
63 const char *atspi_interface_image = ATSPI_DBUS_INTERFACE_IMAGE;
64 const char *atspi_interface_registry = ATSPI_DBUS_INTERFACE_REGISTRY;
65 const char *atspi_interface_selection = ATSPI_DBUS_INTERFACE_SELECTION;
66 const char *atspi_interface_table = ATSPI_DBUS_INTERFACE_TABLE;
67 const char *atspi_interface_text = ATSPI_DBUS_INTERFACE_TEXT;
68 const char *atspi_interface_cache = ATSPI_DBUS_INTERFACE_CACHE;
69 const char *atspi_interface_value = ATSPI_DBUS_INTERFACE_VALUE;
71 static const char *interfaces[] =
73 ATSPI_DBUS_INTERFACE_ACCESSIBLE,
74 ATSPI_DBUS_INTERFACE_ACTION,
75 ATSPI_DBUS_INTERFACE_APPLICATION,
76 ATSPI_DBUS_INTERFACE_COLLECTION,
77 ATSPI_DBUS_INTERFACE_COMPONENT,
78 ATSPI_DBUS_INTERFACE_DOCUMENT,
79 ATSPI_DBUS_INTERFACE_EDITABLE_TEXT,
80 ATSPI_DBUS_INTERFACE_HYPERLINK,
81 ATSPI_DBUS_INTERFACE_HYPERTEXT,
82 ATSPI_DBUS_INTERFACE_IMAGE,
83 "org.a11y.atspi.LoginHelper",
84 ATSPI_DBUS_INTERFACE_SELECTION,
85 ATSPI_DBUS_INTERFACE_TABLE,
86 ATSPI_DBUS_INTERFACE_TEXT,
87 ATSPI_DBUS_INTERFACE_VALUE,
92 _atspi_get_iface_num (const char *iface)
94 /* TODO: Use a binary search or hash to improve performance */
97 for (i = 0; interfaces[i]; i++)
99 if (!strcmp(iface, interfaces[i])) return i;
109 live_refs = g_hash_table_new (g_direct_hash, g_direct_equal);
114 /* TODO: Add an application parameter */
121 g_error ("AT-SPI: COuldn't connect to accessibility bus. Is at-spi-bus-launcher running?");
125 #define APP_IS_REGISTRY(app) (!strcmp (app->bus_name, atspi_bus_registry))
136 g_hash_table_destroy (refs);
141 dbus_connection_close (bus);
142 dbus_connection_unref (bus);
147 static gboolean atspi_inited = FALSE;
149 static GHashTable *app_hash = NULL;
152 handle_get_bus_address (DBusPendingCall *pending, void *user_data)
154 AtspiApplication *app = user_data;
155 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
156 DBusMessage *message;
158 DBusPendingCall *new_pending;
160 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
162 if (dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &address,
163 DBUS_TYPE_INVALID) && address [0])
168 dbus_error_init (&error);
169 bus = dbus_connection_open_private (address, &error);
174 dbus_connection_unref (app->bus);
180 g_warning ("Unable to open bus connection: %s", error.message);
181 dbus_error_free (&error);
185 dbus_message_unref (reply);
186 dbus_pending_call_unref (pending);
189 return; /* application has gone away / been disposed */
191 message = dbus_message_new_method_call (app->bus_name,
192 "/org/a11y/atspi/cache",
193 atspi_interface_cache, "GetItems");
195 dbus_connection_send_with_reply (app->bus, message, &new_pending, 2000);
196 dbus_pending_call_set_notify (new_pending, handle_get_items, app, NULL);
197 dbus_message_unref (message);
200 static AtspiApplication *
201 get_application (const char *bus_name)
203 AtspiApplication *app = NULL;
205 DBusMessage *message;
206 DBusPendingCall *pending = NULL;
210 app_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_object_unref);
211 if (!app_hash) return NULL;
213 app = g_hash_table_lookup (app_hash, bus_name);
215 bus_name_dup = g_strdup (bus_name);
216 if (!bus_name_dup) return NULL;
217 // TODO: change below to something that will send state-change:defunct notification if necessary */
218 app = _atspi_application_new (bus_name);
219 app->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
220 app->bus = dbus_connection_ref (_atspi_bus ());
221 gettimeofday (&app->time_added, NULL);
222 app->cache = ATSPI_CACHE_UNDEFINED;
223 g_hash_table_insert (app_hash, bus_name_dup, app);
224 message = dbus_message_new_method_call (bus_name, atspi_path_root,
225 atspi_interface_application, "GetApplicationBusAddress");
227 dbus_connection_send_with_reply (app->bus, message, &pending, 2000);
228 dbus_pending_call_set_notify (pending, handle_get_bus_address, app, NULL);
229 dbus_message_unref (message);
233 static AtspiAccessible *
234 ref_accessible (const char *app_name, const char *path)
236 AtspiApplication *app;
239 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
242 app = get_application (app_name);
244 if (!strcmp (path, "/org/a11y/atspi/accessible/root"))
248 app->root = _atspi_accessible_new (app, atspi_path_root);
249 app->root->accessible_parent = atspi_get_desktop (0);
251 return g_object_ref (app->root);
254 a = g_hash_table_lookup (app->hash, path);
257 return g_object_ref (a);
259 a = _atspi_accessible_new (app, path);
262 g_hash_table_insert (app->hash, g_strdup (a->parent.path), a);
263 g_object_ref (a); /* for the hash */
267 static AtspiHyperlink *
268 ref_hyperlink (const char *app_name, const char *path)
270 AtspiApplication *app = get_application (app_name);
271 AtspiHyperlink *hyperlink;
273 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
276 hyperlink = g_hash_table_lookup (app->hash, path);
279 return g_object_ref (hyperlink);
281 hyperlink = _atspi_hyperlink_new (app, path);
282 g_hash_table_insert (app->hash, g_strdup (hyperlink->parent.path), hyperlink);
283 /* TODO: This should be a weak ref */
284 g_object_ref (hyperlink); /* for the hash */
297 GArray *state_bitflags;
300 static DBusHandlerResult
301 handle_remove_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
303 const char *sender = dbus_message_get_sender (message);
304 AtspiApplication *app;
306 DBusMessageIter iter, iter_struct;
307 const char *signature = dbus_message_get_signature (message);
310 if (strcmp (signature, "(so)") != 0)
312 g_warning ("AT-SPI: Unknown signature %s for RemoveAccessible", signature);
313 return DBUS_HANDLER_RESULT_HANDLED;
316 dbus_message_iter_init (message, &iter);
317 dbus_message_iter_recurse (&iter, &iter_struct);
318 dbus_message_iter_get_basic (&iter_struct, &sender);
319 dbus_message_iter_next (&iter_struct);
320 dbus_message_iter_get_basic (&iter_struct, &path);
321 app = get_application (sender);
322 a = ref_accessible (sender, path);
324 return DBUS_HANDLER_RESULT_HANDLED;
325 g_object_run_dispose (G_OBJECT (a));
326 g_hash_table_remove (app->hash, a->parent.path);
327 g_object_unref (a); /* unref our own ref */
328 return DBUS_HANDLER_RESULT_HANDLED;
331 static DBusHandlerResult
332 handle_name_owner_changed (DBusConnection *bus, DBusMessage *message, void *user_data)
334 const char *name, *new, *old;
335 static gboolean registry_lost = FALSE;
337 if (!dbus_message_get_args (message, NULL,
338 DBUS_TYPE_STRING, &name,
339 DBUS_TYPE_STRING, &old,
340 DBUS_TYPE_STRING, &new,
346 if (!strcmp (name, "org.a11y.atspi.Registry"))
348 if (registry_lost && !old[0])
350 _atspi_reregister_event_listeners ();
351 _atspi_reregister_device_listeners ();
352 registry_lost = FALSE;
355 registry_lost = TRUE;
357 return DBUS_HANDLER_RESULT_HANDLED;
361 add_app_to_desktop (AtspiAccessible *a, const char *bus_name)
363 AtspiAccessible *obj = ref_accessible (bus_name, atspi_path_root);
366 a->children = g_list_append (a->children, obj);
371 g_warning ("AT-SPI: Error calling getRoot for %s", bus_name);
377 send_children_changed (AtspiAccessible *parent, AtspiAccessible *child, gboolean add)
381 memset (&e, 0, sizeof (e));
382 e.type = (add? "object:children-changed:add": "object:children-changed:remove");
384 e.detail1 = g_list_index (parent->children, child);
386 _atspi_send_event (&e);
390 unref_object_and_descendants (AtspiAccessible *obj)
394 for (l = obj->children; l; l = l->next)
396 unref_object_and_descendants (l->data);
398 g_object_unref (obj);
402 remove_app_from_desktop (AtspiAccessible *a, const char *bus_name)
405 AtspiAccessible *child;
407 for (l = a->children; l; l = l->next)
410 if (!strcmp (bus_name, child->parent.app->bus_name)) break;
416 send_children_changed (a, child, FALSE);
417 a->children = g_list_remove (a->children, child);
418 unref_object_and_descendants (child);
422 static AtspiAccessible *desktop;
425 get_reference_from_iter (DBusMessageIter *iter, const char **app_name, const char **path)
427 DBusMessageIter iter_struct;
429 dbus_message_iter_recurse (iter, &iter_struct);
430 dbus_message_iter_get_basic (&iter_struct, app_name);
431 dbus_message_iter_next (&iter_struct);
432 dbus_message_iter_get_basic (&iter_struct, path);
433 dbus_message_iter_next (iter);
437 add_accessible_from_iter (DBusMessageIter *iter)
439 DBusMessageIter iter_struct, iter_array;
440 const char *app_name, *path;
441 AtspiAccessible *accessible;
442 const char *name, *description;
445 dbus_message_iter_recurse (iter, &iter_struct);
448 get_reference_from_iter (&iter_struct, &app_name, &path);
449 accessible = ref_accessible (app_name, path);
453 /* Get application: TODO */
454 dbus_message_iter_next (&iter_struct);
457 get_reference_from_iter (&iter_struct, &app_name, &path);
458 if (accessible->accessible_parent)
459 g_object_unref (accessible->accessible_parent);
460 accessible->accessible_parent = ref_accessible (app_name, path);
463 while (accessible->children)
465 g_object_unref (accessible->children->data);
466 accessible->children = g_list_remove (accessible->children, accessible->children->data);
468 dbus_message_iter_recurse (&iter_struct, &iter_array);
469 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
471 AtspiAccessible *child;
472 get_reference_from_iter (&iter_array, &app_name, &path);
473 child = ref_accessible (app_name, path);
474 accessible->children = g_list_append (accessible->children, child);
478 dbus_message_iter_next (&iter_struct);
479 _atspi_dbus_set_interfaces (accessible, &iter_struct);
480 dbus_message_iter_next (&iter_struct);
483 if (accessible->name)
484 g_free (accessible->name);
485 dbus_message_iter_get_basic (&iter_struct, &name);
486 accessible->name = g_strdup (name);
487 dbus_message_iter_next (&iter_struct);
490 dbus_message_iter_get_basic (&iter_struct, &role);
491 accessible->role = role;
492 dbus_message_iter_next (&iter_struct);
495 if (accessible->description)
496 g_free (accessible->description);
497 dbus_message_iter_get_basic (&iter_struct, &description);
498 accessible->description = g_strdup (description);
499 dbus_message_iter_next (&iter_struct);
501 _atspi_dbus_set_state (accessible, &iter_struct);
502 dbus_message_iter_next (&iter_struct);
504 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_NAME | ATSPI_CACHE_ROLE |
505 ATSPI_CACHE_PARENT | ATSPI_CACHE_DESCRIPTION);
506 if (!atspi_state_set_contains (accessible->states,
507 ATSPI_STATE_MANAGES_DESCENDANTS))
508 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_CHILDREN);
510 /* This is a bit of a hack since the cache holds a ref, so we don't need
511 * the one provided for us anymore */
512 g_object_unref (accessible);
516 handle_get_items (DBusPendingCall *pending, void *user_data)
518 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
519 DBusMessageIter iter, iter_array;
521 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
523 const char *sender = dbus_message_get_sender (reply);
524 const char *error = NULL;
525 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &error,
527 g_warning ("AT-SPI: Error in GetItems, sender=%s, error=%s", sender, error);
528 dbus_message_unref (reply);
529 dbus_pending_call_unref (pending);
533 dbus_message_iter_init (reply, &iter);
534 dbus_message_iter_recurse (&iter, &iter_array);
535 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
537 add_accessible_from_iter (&iter_array);
538 dbus_message_iter_next (&iter_array);
540 dbus_message_unref (reply);
541 dbus_pending_call_unref (pending);
544 /* TODO: Do we stil need this function? */
545 static AtspiAccessible *
546 ref_accessible_desktop (AtspiApplication *app)
549 DBusMessage *message, *reply;
550 DBusMessageIter iter, iter_array;
555 g_object_ref (desktop);
558 desktop = _atspi_accessible_new (app, atspi_path_root);
563 g_hash_table_insert (app->hash, desktop->parent.path, desktop);
564 g_object_ref (desktop); /* for the hash */
565 desktop->name = g_strdup ("main");
566 message = dbus_message_new_method_call (atspi_bus_registry,
568 atspi_interface_accessible,
573 reply = _atspi_dbus_send_with_reply_and_block (message, &error);
574 if (!reply || strcmp (dbus_message_get_signature (reply), "a(so)") != 0)
578 g_warning ("Couldn't get application list: %s", error->message);
579 g_clear_error (&error);
582 dbus_message_unref (reply);
585 dbus_message_iter_init (reply, &iter);
586 dbus_message_iter_recurse (&iter, &iter_array);
587 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
589 const char *app_name, *path;
590 get_reference_from_iter (&iter_array, &app_name, &path);
591 add_app_to_desktop (desktop, app_name);
593 dbus_message_unref (reply);
595 /* Record the alternate name as an alias for org.a11y.atspi.Registry */
596 bus_name_dup = g_strdup (dbus_message_get_sender (reply));
598 g_hash_table_insert (app_hash, bus_name_dup, app);
604 _atspi_ref_accessible (const char *app, const char *path)
606 AtspiApplication *a = get_application (app);
608 if ( APP_IS_REGISTRY(a))
610 return a->root = ref_accessible_desktop (a);
612 return ref_accessible (app, path);
616 _atspi_dbus_return_accessible_from_message (DBusMessage *message)
618 DBusMessageIter iter;
619 AtspiAccessible *retval = NULL;
620 const char *signature;
625 signature = dbus_message_get_signature (message);
626 if (!strcmp (signature, "(so)"))
628 dbus_message_iter_init (message, &iter);
629 retval = _atspi_dbus_return_accessible_from_iter (&iter);
633 g_warning ("AT-SPI: Called _atspi_dbus_return_accessible_from_message with strange signature %s", signature);
635 dbus_message_unref (message);
640 _atspi_dbus_return_accessible_from_iter (DBusMessageIter *iter)
642 const char *app_name, *path;
644 get_reference_from_iter (iter, &app_name, &path);
645 return ref_accessible (app_name, path);
649 _atspi_dbus_return_hyperlink_from_message (DBusMessage *message)
651 DBusMessageIter iter;
652 AtspiHyperlink *retval = NULL;
653 const char *signature;
658 signature = dbus_message_get_signature (message);
659 if (!strcmp (signature, "(so)"))
661 dbus_message_iter_init (message, &iter);
662 retval = _atspi_dbus_return_hyperlink_from_iter (&iter);
666 g_warning ("AT-SPI: Called _atspi_dbus_return_hyperlink_from_message with strange signature %s", signature);
668 dbus_message_unref (message);
673 _atspi_dbus_return_hyperlink_from_iter (DBusMessageIter *iter)
675 const char *app_name, *path;
677 get_reference_from_iter (iter, &app_name, &path);
678 return ref_hyperlink (app_name, path);
681 const char *cache_signal_type = "((so)(so)(so)a(so)assusau)";
683 static DBusHandlerResult
684 handle_add_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
686 DBusMessageIter iter;
687 const char *sender = dbus_message_get_sender (message);
689 if (strcmp (dbus_message_get_signature (message), cache_signal_type) != 0)
691 g_warning ("AT-SPI: AddAccessible with unknown signature %s\n",
692 dbus_message_get_signature (message));
693 return DBUS_HANDLER_RESULT_HANDLED;
696 dbus_message_iter_init (message, &iter);
697 add_accessible_from_iter (&iter);
698 return DBUS_HANDLER_RESULT_HANDLED;
704 DBusMessage *message;
708 static guint process_deferred_messages_id = -1;
711 process_deferred_message (BusDataClosure *closure)
713 int type = dbus_message_get_type (closure->message);
714 const char *interface = dbus_message_get_interface (closure->message);
716 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
717 !strncmp (interface, "org.a11y.atspi.Event.", 21))
719 _atspi_dbus_handle_event (closure->bus, closure->message, closure->data);
721 if (dbus_message_is_method_call (closure->message, atspi_interface_device_event_listener, "NotifyEvent"))
723 _atspi_dbus_handle_DeviceEvent (closure->bus,
724 closure->message, closure->data);
726 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "AddAccessible"))
728 handle_add_accessible (closure->bus, closure->message, closure->data);
730 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "RemoveAccessible"))
732 handle_remove_accessible (closure->bus, closure->message, closure->data);
734 if (dbus_message_is_signal (closure->message, "org.freedesktop.DBus", "NameOwnerChanged"))
736 handle_name_owner_changed (closure->bus, closure->message, closure->data);
740 static GQueue *deferred_messages = NULL;
743 _atspi_process_deferred_messages (gpointer data)
745 static int in_process_deferred_messages = 0;
746 BusDataClosure *closure;
748 if (in_process_deferred_messages)
750 in_process_deferred_messages = 1;
751 while (closure = g_queue_pop_head (deferred_messages))
753 process_deferred_message (closure);
754 dbus_message_unref (closure->message);
755 dbus_connection_unref (closure->bus);
758 /* If data is NULL, assume that we were called from GLib */
760 process_deferred_messages_id = -1;
761 in_process_deferred_messages = 0;
765 static DBusHandlerResult
766 defer_message (DBusConnection *connection, DBusMessage *message, void *user_data)
768 BusDataClosure *closure = g_new (BusDataClosure, 1);
770 closure->bus = dbus_connection_ref (bus);
771 closure->message = dbus_message_ref (message);
772 closure->data = user_data;
774 g_queue_push_tail (deferred_messages, closure);
776 if (process_deferred_messages_id == -1)
777 process_deferred_messages_id = g_idle_add (_atspi_process_deferred_messages, NULL);
778 return DBUS_HANDLER_RESULT_HANDLED;
781 static DBusHandlerResult
782 atspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
784 int type = dbus_message_get_type (message);
785 const char *interface = dbus_message_get_interface (message);
787 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
788 !strncmp (interface, "org.a11y.atspi.Event.", 21))
790 return defer_message (bus, message, data);
792 if (dbus_message_is_method_call (message, atspi_interface_device_event_listener, "NotifyEvent"))
794 return defer_message (bus, message, data);
796 if (dbus_message_is_signal (message, atspi_interface_cache, "AddAccessible"))
798 return defer_message (bus, message, data);
800 if (dbus_message_is_signal (message, atspi_interface_cache, "RemoveAccessible"))
802 return defer_message (bus, message, data);
804 if (dbus_message_is_signal (message, "org.freedesktop.DBus", "NameOwnerChanged"))
806 defer_message (bus, message, data);
807 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
809 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
812 static const char *signal_interfaces[] =
814 "org.a11y.atspi.Event.Object",
815 "org.a11y.atspi.Event.Window",
816 "org.a11y.atspi.Event.Mouse",
817 "org.a11y.atspi.Event.Terminal",
818 "org.a11y.atspi.Event.Document",
819 "org.a11y.atspi.Event.Focus",
824 * Returns a 'canonicalized' value for DISPLAY,
825 * with the screen number stripped off if present.
827 * TODO: Avoid having duplicate functions for this here and in at-spi2-atk
830 spi_display_name (void)
832 char *canonical_display_name = NULL;
833 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
837 display_env = g_getenv ("DISPLAY");
838 if (!display_env || !display_env[0])
839 canonical_display_name = g_strdup (":0");
842 gchar *display_p, *screen_p;
843 canonical_display_name = g_strdup (display_env);
844 display_p = g_utf8_strrchr (canonical_display_name, -1, ':');
845 screen_p = g_utf8_strrchr (canonical_display_name, -1, '.');
846 if (screen_p && display_p && (screen_p > display_p))
854 canonical_display_name = g_strdup (display_env);
857 return canonical_display_name;
863 * Connects to the accessibility registry and initializes the SPI.
865 * Returns: 0 on success, otherwise an integer error code.
871 const gchar *no_cache;
884 bus = atspi_get_a11y_bus ();
887 dbus_bus_register (bus, NULL);
888 atspi_dbus_connection_setup_with_g_main(bus, g_main_context_default());
889 dbus_connection_add_filter (bus, atspi_dbus_filter, NULL, NULL);
890 match = g_strdup_printf ("type='signal',interface='%s',member='AddAccessible'", atspi_interface_cache);
891 dbus_bus_add_match (bus, match, NULL);
893 match = g_strdup_printf ("type='signal',interface='%s',member='RemoveAccessible'", atspi_interface_cache);
894 dbus_bus_add_match (bus, match, NULL);
896 match = g_strdup_printf ("type='signal',interface='%s',member='ChildrenChanged'", atspi_interface_event_object);
897 dbus_bus_add_match (bus, match, NULL);
899 match = g_strdup_printf ("type='signal',interface='%s',member='PropertyChange'", atspi_interface_event_object);
900 dbus_bus_add_match (bus, match, NULL);
902 match = g_strdup_printf ("type='signal',interface='%s',member='StateChanged'", atspi_interface_event_object);
903 dbus_bus_add_match (bus, match, NULL);
906 dbus_bus_add_match (bus,
907 "type='signal', interface='org.freedesktop.DBus', member='NameOwnerChanged'",
910 no_cache = g_getenv ("ATSPI_NO_CACHE");
911 if (no_cache && g_strcmp0 (no_cache, "0") != 0)
912 atspi_no_cache = TRUE;
914 deferred_messages = g_queue_new ();
922 * Starts/enters the main event loop for the AT-SPI services.
924 * NOTE: This method does not return control; it is exited via a call to
925 * #atspi_event_quit from within an event handler.
929 atspi_event_main (void)
931 atspi_main_loop = g_main_loop_new (NULL, FALSE);
932 g_main_loop_run (atspi_main_loop);
933 atspi_main_loop = NULL;
939 * Quits the last main event loop for the AT-SPI services,
940 * See: #atspi_event_main
943 atspi_event_quit (void)
945 g_main_loop_quit (atspi_main_loop);
951 * Disconnects from #AtspiRegistry instances and releases
952 * any floating resources. Call only once at exit.
954 * Returns: 0 if there were no leaks, otherwise other integer values.
966 atspi_inited = FALSE;
970 leaked = g_hash_table_size (live_refs);
982 static GSList *hung_processes;
985 remove_hung_process (DBusPendingCall *pending, void *data)
987 gchar *bus_name = data;
989 hung_processes = g_slist_remove (hung_processes, data);
991 dbus_pending_call_unref (pending);
995 check_for_hang (DBusMessage *message, DBusError *error, DBusConnection *bus, const char *bus_name)
997 if (!message && error->name &&
998 !strcmp (error->name, "org.freedesktop.DBus.Error.NoReply"))
1001 DBusMessage *message;
1002 gchar *bus_name_dup;
1003 DBusPendingCall *pending = NULL;
1004 for (l = hung_processes; l; l = l->next)
1005 if (!strcmp (l->data, bus_name))
1007 message = dbus_message_new_method_call (bus_name, "/",
1008 "org.freedesktop.DBus.Peer",
1012 dbus_connection_send_with_reply (bus, message, &pending, -1);
1013 dbus_message_unref (message);
1016 bus_name_dup = g_strdup (bus_name);
1017 hung_processes = g_slist_append (hung_processes, bus_name_dup);
1018 dbus_pending_call_set_notify (pending, remove_hung_process, bus_name_dup, NULL);
1023 connection_is_hung (const char *bus_name)
1027 for (l = hung_processes; l; l = l->next)
1028 if (!strcmp (l->data, bus_name))
1034 check_app (AtspiApplication *app, GError **error)
1036 if (!app || !app->bus)
1038 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
1039 _("The application no longer exists"));
1043 if (atspi_main_loop && connection_is_hung (app->bus_name))
1045 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC,
1046 "The process appears to be hung.");
1054 set_timeout (AtspiApplication *app)
1059 if (app && app_startup_time > 0)
1061 gettimeofday (&tv, NULL);
1062 diff = (tv.tv_sec - app->time_added.tv_sec) * 1000 + (tv.tv_usec - app->time_added.tv_usec) / 1000;
1063 dbind_set_timeout (MAX(method_call_timeout, app_startup_time - diff));
1066 dbind_set_timeout (method_call_timeout);
1070 _atspi_dbus_call (gpointer obj, const char *interface, const char *method, GError **error, const char *type, ...)
1075 AtspiObject *aobj = ATSPI_OBJECT (obj);
1077 if (!check_app (aobj->app, error))
1080 va_start (args, type);
1081 dbus_error_init (&err);
1082 set_timeout (aobj->app);
1083 retval = dbind_method_call_reentrant_va (aobj->app->bus, aobj->app->bus_name,
1084 aobj->path, interface, method, &err,
1087 check_for_hang (NULL, &err, aobj->app->bus, aobj->app->bus_name);
1088 _atspi_process_deferred_messages ((gpointer)TRUE);
1089 if (dbus_error_is_set (&err))
1091 g_set_error(error, ATSPI_ERROR, ATSPI_ERROR_IPC, "%s", err.message);
1092 dbus_error_free (&err);
1098 _atspi_dbus_call_partial (gpointer obj,
1099 const char *interface,
1102 const char *type, ...)
1106 va_start (args, type);
1107 return _atspi_dbus_call_partial_va (obj, interface, method, error, type, args);
1112 _atspi_dbus_call_partial_va (gpointer obj,
1113 const char *interface,
1119 AtspiObject *aobj = ATSPI_OBJECT (obj);
1121 DBusMessage *msg = NULL, *reply = NULL;
1122 DBusMessageIter iter;
1125 dbus_error_init (&err);
1127 if (!check_app (aobj->app, error))
1130 msg = dbus_message_new_method_call (aobj->app->bus_name, aobj->path, interface, method);
1135 dbus_message_iter_init_append (msg, &iter);
1136 dbind_any_marshal_va (&iter, &p, args);
1138 set_timeout (aobj->app);
1139 reply = dbind_send_and_allow_reentry (aobj->app->bus, msg, &err);
1140 check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1144 dbus_message_unref (msg);
1145 _atspi_process_deferred_messages ((gpointer)TRUE);
1146 if (dbus_error_is_set (&err))
1148 /* TODO: Set gerror */
1149 dbus_error_free (&err);
1155 _atspi_dbus_get_property (gpointer obj, const char *interface, const char *name, GError **error, const char *type, void *data)
1157 DBusMessage *message, *reply;
1158 DBusMessageIter iter, iter_variant;
1160 dbus_bool_t retval = FALSE;
1161 AtspiObject *aobj = ATSPI_OBJECT (obj);
1162 char expected_type = (type [0] == '(' ? 'r' : type [0]);
1167 if (!check_app (aobj->app, error))
1170 message = dbus_message_new_method_call (aobj->app->bus_name,
1172 "org.freedesktop.DBus.Properties",
1176 // TODO: throw exception
1179 dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1180 dbus_error_init (&err);
1181 set_timeout (aobj->app);
1182 reply = dbind_send_and_allow_reentry (aobj->app->bus, message, &err);
1183 check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1184 dbus_message_unref (message);
1185 _atspi_process_deferred_messages ((gpointer)TRUE);
1188 // TODO: throw exception
1192 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
1194 const char *err_str = NULL;
1195 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &err_str, DBUS_TYPE_INVALID);
1197 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err_str);
1201 dbus_message_iter_init (reply, &iter);
1202 if (dbus_message_iter_get_arg_type (&iter) != 'v')
1204 g_warning ("AT-SPI: expected a variant when fetching %s from interface %s; got %s\n", name, interface, dbus_message_get_signature (reply));
1207 dbus_message_iter_recurse (&iter, &iter_variant);
1208 if (dbus_message_iter_get_arg_type (&iter_variant) != expected_type)
1210 g_warning ("atspi_dbus_get_property: Wrong type: expected %s, got %c\n", type, dbus_message_iter_get_arg_type (&iter_variant));
1213 if (!strcmp (type, "(so)"))
1215 *((AtspiAccessible **)data) = _atspi_dbus_return_accessible_from_iter (&iter_variant);
1219 dbus_message_iter_get_basic (&iter_variant, data);
1220 if (type [0] == 's')
1221 *(char **)data = g_strdup (*(char **)data);
1225 dbus_error_free (&err);
1227 dbus_message_unref (reply);
1232 _atspi_dbus_send_with_reply_and_block (DBusMessage *message, GError **error)
1236 AtspiApplication *app;
1237 DBusConnection *bus;
1239 app = get_application (dbus_message_get_destination (message));
1241 if (app && !app->bus)
1242 return NULL; /* will fail anyway; app has been disposed */
1244 bus = (app ? app->bus : _atspi_bus());
1245 dbus_error_init (&err);
1247 reply = dbind_send_and_allow_reentry (bus, message, &err);
1248 _atspi_process_deferred_messages ((gpointer)TRUE);
1249 dbus_message_unref (message);
1250 if (dbus_error_is_set (&err))
1253 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err.message);
1254 dbus_error_free (&err);
1260 _atspi_dbus_return_hash_from_message (DBusMessage *message)
1262 DBusMessageIter iter;
1268 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1270 dbus_message_iter_init (message, &iter);
1271 ret = _atspi_dbus_hash_from_iter (&iter);
1272 dbus_message_unref (message);
1277 _atspi_dbus_hash_from_iter (DBusMessageIter *iter)
1279 GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
1280 (GDestroyNotify) g_free,
1281 (GDestroyNotify) g_free);
1282 DBusMessageIter iter_array, iter_dict;
1284 dbus_message_iter_recurse (iter, &iter_array);
1285 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1287 const char *name, *value;
1288 dbus_message_iter_recurse (&iter_array, &iter_dict);
1289 dbus_message_iter_get_basic (&iter_dict, &name);
1290 dbus_message_iter_next (&iter_dict);
1291 dbus_message_iter_get_basic (&iter_dict, &value);
1292 g_hash_table_insert (hash, g_strdup (name), g_strdup (value));
1293 dbus_message_iter_next (&iter_array);
1299 _atspi_dbus_return_attribute_array_from_message (DBusMessage *message)
1301 DBusMessageIter iter;
1307 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1309 dbus_message_iter_init (message, &iter);
1311 ret = _atspi_dbus_attribute_array_from_iter (&iter);
1312 dbus_message_unref (message);
1317 _atspi_dbus_attribute_array_from_iter (DBusMessageIter *iter)
1319 DBusMessageIter iter_array, iter_dict;
1320 GArray *array = g_array_new (TRUE, TRUE, sizeof (gchar *));
1322 dbus_message_iter_recurse (iter, &iter_array);
1323 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1325 const char *name, *value;
1327 dbus_message_iter_recurse (&iter_array, &iter_dict);
1328 dbus_message_iter_get_basic (&iter_dict, &name);
1329 dbus_message_iter_next (&iter_dict);
1330 dbus_message_iter_get_basic (&iter_dict, &value);
1331 str = g_strdup_printf ("%s:%s", name, value);
1332 array = g_array_append_val (array, str);
1333 dbus_message_iter_next (&iter_array);;
1339 _atspi_dbus_set_interfaces (AtspiAccessible *accessible, DBusMessageIter *iter)
1341 DBusMessageIter iter_array;
1343 accessible->interfaces = 0;
1344 dbus_message_iter_recurse (iter, &iter_array);
1345 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1349 dbus_message_iter_get_basic (&iter_array, &iface);
1350 if (!strcmp (iface, "org.freedesktop.DBus.Introspectable")) continue;
1351 n = _atspi_get_iface_num (iface);
1354 g_warning ("AT-SPI: Unknown interface %s", iface);
1357 accessible->interfaces |= (1 << n);
1358 dbus_message_iter_next (&iter_array);
1360 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_INTERFACES);
1364 _atspi_dbus_set_state (AtspiAccessible *accessible, DBusMessageIter *iter)
1366 DBusMessageIter iter_array;
1368 dbus_uint32_t *states;
1370 dbus_message_iter_recurse (iter, &iter_array);
1371 dbus_message_iter_get_fixed_array (&iter_array, &states, &count);
1374 g_warning ("AT-SPI: expected 2 values in states array; got %d\n", count);
1375 if (!accessible->states)
1376 accessible->states = _atspi_state_set_new_internal (accessible, 0);
1380 guint64 val = ((guint64)states [1]) << 32;
1382 if (!accessible->states)
1383 accessible->states = _atspi_state_set_new_internal (accessible, val);
1385 accessible->states->states = val;
1387 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_STATES);
1391 _atspi_error_quark (void)
1393 return g_quark_from_static_string ("atspi_error");
1397 * Gets the IOR from the XDisplay.
1400 get_accessibility_bus_address_x11 (void)
1404 Display *bridge_display = NULL;
1407 unsigned char *data_x11 = NULL;
1408 unsigned long nitems;
1409 unsigned long leftover;
1412 display_name = spi_display_name ();
1413 if (display_name != NULL)
1415 bridge_display = XOpenDisplay (display_name);
1416 g_free (display_name);
1418 if (!bridge_display)
1420 g_warning ("Could not open X display");
1424 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
1425 XGetWindowProperty (bridge_display,
1426 XDefaultRootWindow (bridge_display),
1428 (long) BUFSIZ, False,
1429 (Atom) 31, &actual_type, &actual_format,
1430 &nitems, &leftover, &data_x11);
1431 XCloseDisplay (bridge_display);
1433 data = g_strdup (data_x11);
1439 get_accessibility_bus_address_dbus (void)
1441 DBusConnection *session_bus = NULL;
1442 DBusMessage *message;
1445 char *address = NULL;
1447 session_bus = dbus_bus_get (DBUS_BUS_SESSION, NULL);
1451 message = dbus_message_new_method_call ("org.a11y.Bus",
1456 dbus_error_init (&error);
1457 reply = dbus_connection_send_with_reply_and_block (session_bus,
1461 dbus_message_unref (message);
1465 g_warning ("Error retrieving accessibility bus address: %s: %s",
1466 error.name, error.message);
1467 dbus_error_free (&error);
1472 const char *tmp_address;
1473 if (!dbus_message_get_args (reply,
1479 dbus_message_unref (reply);
1482 address = g_strdup (tmp_address);
1483 dbus_message_unref (reply);
1489 static DBusConnection *a11y_bus;
1490 static dbus_int32_t a11y_dbus_slot = -1;
1493 a11y_bus_free (void *data)
1495 if (data == a11y_bus)
1498 dbus_connection_free_data_slot (&a11y_dbus_slot);
1503 atspi_get_a11y_bus (void)
1505 DBusConnection *bus = NULL;
1509 if (a11y_bus && dbus_connection_get_is_connected (a11y_bus))
1512 if (a11y_dbus_slot == -1)
1513 if (!dbus_connection_allocate_data_slot (&a11y_dbus_slot))
1514 g_warning ("at-spi: Unable to allocate D-Bus slot");
1516 address = get_accessibility_bus_address_x11 ();
1518 address = get_accessibility_bus_address_dbus ();
1522 dbus_error_init (&error);
1523 a11y_bus = dbus_connection_open_private (address, &error);
1528 g_warning ("Couldn't connect to accessibility bus: %s", error.message);
1529 dbus_error_free (&error);
1534 if (!dbus_bus_register (a11y_bus, &error))
1536 g_warning ("Couldn't register with accessibility bus: %s", error.message);
1537 dbus_error_free (&error);
1538 dbus_connection_close (a11y_bus);
1539 dbus_connection_unref (a11y_bus);
1545 /* Simulate a weak ref on the bus */
1546 dbus_connection_set_data (a11y_bus, a11y_dbus_slot, a11y_bus, a11y_bus_free);
1552 * Set the timeout used for method calls. If this is not set explicitly,
1553 * a default of 0.8 ms is used.
1554 * Note that at-spi2-registryd currently uses a timeout of 3 seconds when
1555 * sending a keyboard event notification. This means that, if an AT makes
1556 * a call in response to the keyboard notification and the application
1557 * being called does not respond before the timeout is reached,
1558 * at-spi2-registryd will time out on the keyboard event notification and
1559 * pass the key onto the application (ie, reply to indicate that the key
1560 * was not consumed), so this may make it undesirable to set a timeout
1561 * larger than 3 seconds.
1563 * @val: The timeout value, in milliseconds, or -1 to disable the timeout.
1564 * @startup_time: The amount of time, in milliseconds, to allow to pass
1565 * before enforcing timeouts on an application. Can be used to prevent
1566 * timeout exceptions if an application is likely to block for an extended
1567 * period of time on initialization. -1 can be passed to disable this
1570 * By default, the normal timeout is set to 800 ms, and the application startup
1571 * timeout is set to 15 seconds.
1574 atspi_set_timeout (gint val, gint startup_time)
1576 method_call_timeout = val;
1577 app_startup_time = startup_time;