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);
38 gboolean _atspi_process_deferred_messages (gpointer data);
40 static DBusConnection *bus = NULL;
41 static GHashTable *live_refs = NULL;
42 static gint method_call_timeout = 800;
43 static gint app_startup_time = 15000;
45 GMainLoop *atspi_main_loop;
46 GMainContext *atspi_main_context;
47 gboolean atspi_no_cache;
49 const char *atspi_path_dec = ATSPI_DBUS_PATH_DEC;
50 const char *atspi_path_registry = ATSPI_DBUS_PATH_REGISTRY;
51 const char *atspi_path_root = ATSPI_DBUS_PATH_ROOT;
52 const char *atspi_bus_registry = ATSPI_DBUS_NAME_REGISTRY;
53 const char *atspi_interface_accessible = ATSPI_DBUS_INTERFACE_ACCESSIBLE;
54 const char *atspi_interface_action = ATSPI_DBUS_INTERFACE_ACTION;
55 const char *atspi_interface_application = ATSPI_DBUS_INTERFACE_APPLICATION;
56 const char *atspi_interface_collection = ATSPI_DBUS_INTERFACE_COLLECTION;
57 const char *atspi_interface_component = ATSPI_DBUS_INTERFACE_COMPONENT;
58 const char *atspi_interface_dec = ATSPI_DBUS_INTERFACE_DEC;
59 const char *atspi_interface_device_event_listener = ATSPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER;
60 const char *atspi_interface_document = ATSPI_DBUS_INTERFACE_DOCUMENT;
61 const char *atspi_interface_editable_text = ATSPI_DBUS_INTERFACE_EDITABLE_TEXT;
62 const char *atspi_interface_event_object = ATSPI_DBUS_INTERFACE_EVENT_OBJECT;
63 const char *atspi_interface_hyperlink = ATSPI_DBUS_INTERFACE_HYPERLINK;
64 const char *atspi_interface_hypertext = ATSPI_DBUS_INTERFACE_HYPERTEXT;
65 const char *atspi_interface_image = ATSPI_DBUS_INTERFACE_IMAGE;
66 const char *atspi_interface_registry = ATSPI_DBUS_INTERFACE_REGISTRY;
67 const char *atspi_interface_selection = ATSPI_DBUS_INTERFACE_SELECTION;
68 const char *atspi_interface_table = ATSPI_DBUS_INTERFACE_TABLE;
69 const char *atspi_interface_text = ATSPI_DBUS_INTERFACE_TEXT;
70 const char *atspi_interface_cache = ATSPI_DBUS_INTERFACE_CACHE;
71 const char *atspi_interface_value = ATSPI_DBUS_INTERFACE_VALUE;
73 static const char *interfaces[] =
75 ATSPI_DBUS_INTERFACE_ACCESSIBLE,
76 ATSPI_DBUS_INTERFACE_ACTION,
77 ATSPI_DBUS_INTERFACE_APPLICATION,
78 ATSPI_DBUS_INTERFACE_COLLECTION,
79 ATSPI_DBUS_INTERFACE_COMPONENT,
80 ATSPI_DBUS_INTERFACE_DOCUMENT,
81 ATSPI_DBUS_INTERFACE_EDITABLE_TEXT,
82 ATSPI_DBUS_INTERFACE_HYPERLINK,
83 ATSPI_DBUS_INTERFACE_HYPERTEXT,
84 ATSPI_DBUS_INTERFACE_IMAGE,
85 "org.a11y.atspi.LoginHelper",
86 ATSPI_DBUS_INTERFACE_SELECTION,
87 ATSPI_DBUS_INTERFACE_TABLE,
88 ATSPI_DBUS_INTERFACE_TEXT,
89 ATSPI_DBUS_INTERFACE_VALUE,
94 _atspi_get_iface_num (const char *iface)
96 /* TODO: Use a binary search or hash to improve performance */
99 for (i = 0; interfaces[i]; i++)
101 if (!strcmp(iface, interfaces[i])) return i;
107 _atspi_get_live_refs (void)
111 live_refs = g_hash_table_new (g_direct_hash, g_direct_equal);
116 /* TODO: Add an application parameter */
123 g_error ("AT-SPI: COuldn't connect to accessibility bus. Is at-spi-bus-launcher running?");
127 #define APP_IS_REGISTRY(app) (!strcmp (app->bus_name, atspi_bus_registry))
129 static AtspiAccessible *desktop;
141 g_hash_table_destroy (refs);
146 dbus_connection_close (bus);
147 dbus_connection_unref (bus);
153 for (l = desktop->children; l;)
155 GList *next = l->next;
156 AtspiAccessible *child = l->data;
157 g_object_run_dispose (G_OBJECT (child->parent.app));
158 g_object_run_dispose (G_OBJECT (child));
161 g_object_run_dispose (G_OBJECT (desktop->parent.app));
162 g_object_unref (desktop);
166 static gboolean atspi_inited = FALSE;
168 static GHashTable *app_hash = NULL;
171 handle_get_bus_address (DBusPendingCall *pending, void *user_data)
173 AtspiApplication *app = user_data;
174 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
175 DBusMessage *message;
177 DBusPendingCall *new_pending;
179 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
181 if (dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &address,
182 DBUS_TYPE_INVALID) && address [0])
187 dbus_error_init (&error);
188 bus = dbus_connection_open_private (address, &error);
193 dbus_connection_unref (app->bus);
199 g_warning ("Unable to open bus connection: %s", error.message);
200 dbus_error_free (&error);
204 dbus_message_unref (reply);
205 dbus_pending_call_unref (pending);
208 return; /* application has gone away / been disposed */
210 message = dbus_message_new_method_call (app->bus_name,
211 "/org/a11y/atspi/cache",
212 atspi_interface_cache, "GetItems");
214 dbus_connection_send_with_reply (app->bus, message, &new_pending, 2000);
215 dbus_pending_call_set_notify (new_pending, handle_get_items, app, NULL);
216 dbus_message_unref (message);
219 static AtspiApplication *
220 get_application (const char *bus_name)
222 AtspiApplication *app = NULL;
224 DBusMessage *message;
225 DBusPendingCall *pending = NULL;
229 app_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_object_unref);
230 if (!app_hash) return NULL;
232 app = g_hash_table_lookup (app_hash, bus_name);
234 bus_name_dup = g_strdup (bus_name);
235 if (!bus_name_dup) return NULL;
236 // TODO: change below to something that will send state-change:defunct notification if necessary */
237 app = _atspi_application_new (bus_name);
238 app->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
239 app->bus = dbus_connection_ref (_atspi_bus ());
240 gettimeofday (&app->time_added, NULL);
241 app->cache = ATSPI_CACHE_UNDEFINED;
242 g_hash_table_insert (app_hash, bus_name_dup, app);
243 message = dbus_message_new_method_call (bus_name, atspi_path_root,
244 atspi_interface_application, "GetApplicationBusAddress");
246 dbus_connection_send_with_reply (app->bus, message, &pending, 2000);
247 dbus_pending_call_set_notify (pending, handle_get_bus_address, app, NULL);
248 dbus_message_unref (message);
252 static AtspiAccessible *
253 ref_accessible (const char *app_name, const char *path)
255 AtspiApplication *app;
258 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
261 app = get_application (app_name);
263 if (!strcmp (path, "/org/a11y/atspi/accessible/root"))
267 app->root = _atspi_accessible_new (app, atspi_path_root);
268 app->root->accessible_parent = atspi_get_desktop (0);
269 app->root->accessible_parent->children = g_list_append (app->root->accessible_parent->children, g_object_ref (app->root));
271 return g_object_ref (app->root);
274 a = g_hash_table_lookup (app->hash, path);
277 return g_object_ref (a);
279 a = _atspi_accessible_new (app, path);
282 g_hash_table_insert (app->hash, g_strdup (a->parent.path), g_object_ref (a));
286 static AtspiHyperlink *
287 ref_hyperlink (const char *app_name, const char *path)
289 AtspiApplication *app = get_application (app_name);
290 AtspiHyperlink *hyperlink;
292 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
295 hyperlink = g_hash_table_lookup (app->hash, path);
298 return g_object_ref (hyperlink);
300 hyperlink = _atspi_hyperlink_new (app, path);
301 g_hash_table_insert (app->hash, g_strdup (hyperlink->parent.path), hyperlink);
302 /* TODO: This should be a weak ref */
303 g_object_ref (hyperlink); /* for the hash */
316 GArray *state_bitflags;
319 static DBusHandlerResult
320 handle_remove_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
322 const char *sender = dbus_message_get_sender (message);
323 AtspiApplication *app;
325 DBusMessageIter iter, iter_struct;
326 const char *signature = dbus_message_get_signature (message);
329 if (strcmp (signature, "(so)") != 0)
331 g_warning ("AT-SPI: Unknown signature %s for RemoveAccessible", signature);
332 return DBUS_HANDLER_RESULT_HANDLED;
335 dbus_message_iter_init (message, &iter);
336 dbus_message_iter_recurse (&iter, &iter_struct);
337 dbus_message_iter_get_basic (&iter_struct, &sender);
338 dbus_message_iter_next (&iter_struct);
339 dbus_message_iter_get_basic (&iter_struct, &path);
340 app = get_application (sender);
341 a = ref_accessible (sender, path);
343 return DBUS_HANDLER_RESULT_HANDLED;
344 g_object_run_dispose (G_OBJECT (a));
345 g_hash_table_remove (app->hash, a->parent.path);
346 g_object_unref (a); /* unref our own ref */
347 return DBUS_HANDLER_RESULT_HANDLED;
350 static DBusHandlerResult
351 handle_name_owner_changed (DBusConnection *bus, DBusMessage *message, void *user_data)
353 const char *name, *new, *old;
354 static gboolean registry_lost = FALSE;
356 if (!dbus_message_get_args (message, NULL,
357 DBUS_TYPE_STRING, &name,
358 DBUS_TYPE_STRING, &old,
359 DBUS_TYPE_STRING, &new,
362 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
365 if (!strcmp (name, "org.a11y.atspi.Registry"))
367 if (registry_lost && !old[0])
369 _atspi_reregister_event_listeners ();
370 _atspi_reregister_device_listeners ();
371 registry_lost = FALSE;
374 registry_lost = TRUE;
378 AtspiAccessible *desktop = atspi_get_desktop (0);
380 for (l = desktop->children; l; l = l->next)
382 AtspiAccessible *child = l->data;
383 if (!strcmp (child->parent.app->bus_name, old))
384 g_object_run_dispose (G_OBJECT (child->parent.app));
386 g_object_unref (desktop);
388 return DBUS_HANDLER_RESULT_HANDLED;
392 add_app_to_desktop (AtspiAccessible *a, const char *bus_name)
394 AtspiAccessible *obj = ref_accessible (bus_name, atspi_path_root);
395 /* The app will be added to the desktop as a side-effect of calling
397 g_object_unref (obj);
398 return (obj != NULL);
402 send_children_changed (AtspiAccessible *parent, AtspiAccessible *child, gboolean add)
406 memset (&e, 0, sizeof (e));
407 e.type = (add? "object:children-changed:add": "object:children-changed:remove");
409 e.detail1 = g_list_index (parent->children, child);
411 _atspi_send_event (&e);
415 unref_object_and_descendants (AtspiAccessible *obj)
419 for (l = obj->children; l; l = l->next)
421 unref_object_and_descendants (l->data);
423 g_object_unref (obj);
427 remove_app_from_desktop (AtspiAccessible *a, const char *bus_name)
430 AtspiAccessible *child;
432 for (l = a->children; l; l = l->next)
435 if (!strcmp (bus_name, child->parent.app->bus_name)) break;
441 send_children_changed (a, child, FALSE);
442 a->children = g_list_remove (a->children, child);
443 unref_object_and_descendants (child);
448 get_reference_from_iter (DBusMessageIter *iter, const char **app_name, const char **path)
450 DBusMessageIter iter_struct;
452 dbus_message_iter_recurse (iter, &iter_struct);
453 dbus_message_iter_get_basic (&iter_struct, app_name);
454 dbus_message_iter_next (&iter_struct);
455 dbus_message_iter_get_basic (&iter_struct, path);
456 dbus_message_iter_next (iter);
460 add_accessible_from_iter (DBusMessageIter *iter)
462 DBusMessageIter iter_struct, iter_array;
463 const char *app_name, *path;
464 AtspiAccessible *accessible;
465 const char *name, *description;
468 dbus_message_iter_recurse (iter, &iter_struct);
471 get_reference_from_iter (&iter_struct, &app_name, &path);
472 accessible = ref_accessible (app_name, path);
476 /* Get application: TODO */
477 dbus_message_iter_next (&iter_struct);
480 get_reference_from_iter (&iter_struct, &app_name, &path);
481 if (accessible->accessible_parent)
482 g_object_unref (accessible->accessible_parent);
483 accessible->accessible_parent = ref_accessible (app_name, path);
486 while (accessible->children)
488 g_object_unref (accessible->children->data);
489 accessible->children = g_list_remove (accessible->children, accessible->children->data);
491 dbus_message_iter_recurse (&iter_struct, &iter_array);
492 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
494 AtspiAccessible *child;
495 get_reference_from_iter (&iter_array, &app_name, &path);
496 child = ref_accessible (app_name, path);
497 accessible->children = g_list_append (accessible->children, child);
501 dbus_message_iter_next (&iter_struct);
502 _atspi_dbus_set_interfaces (accessible, &iter_struct);
503 dbus_message_iter_next (&iter_struct);
506 if (accessible->name)
507 g_free (accessible->name);
508 dbus_message_iter_get_basic (&iter_struct, &name);
509 accessible->name = g_strdup (name);
510 dbus_message_iter_next (&iter_struct);
513 dbus_message_iter_get_basic (&iter_struct, &role);
514 accessible->role = role;
515 dbus_message_iter_next (&iter_struct);
518 if (accessible->description)
519 g_free (accessible->description);
520 dbus_message_iter_get_basic (&iter_struct, &description);
521 accessible->description = g_strdup (description);
522 dbus_message_iter_next (&iter_struct);
524 _atspi_dbus_set_state (accessible, &iter_struct);
525 dbus_message_iter_next (&iter_struct);
527 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_NAME | ATSPI_CACHE_ROLE |
528 ATSPI_CACHE_PARENT | ATSPI_CACHE_DESCRIPTION);
529 if (!atspi_state_set_contains (accessible->states,
530 ATSPI_STATE_MANAGES_DESCENDANTS))
531 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_CHILDREN);
533 /* This is a bit of a hack since the cache holds a ref, so we don't need
534 * the one provided for us anymore */
535 g_object_unref (accessible);
539 handle_get_items (DBusPendingCall *pending, void *user_data)
541 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
542 DBusMessageIter iter, iter_array;
544 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
546 const char *sender = dbus_message_get_sender (reply);
547 const char *error = NULL;
548 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &error,
550 g_warning ("AT-SPI: Error in GetItems, sender=%s, error=%s", sender, error);
551 dbus_message_unref (reply);
552 dbus_pending_call_unref (pending);
556 dbus_message_iter_init (reply, &iter);
557 dbus_message_iter_recurse (&iter, &iter_array);
558 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
560 add_accessible_from_iter (&iter_array);
561 dbus_message_iter_next (&iter_array);
563 dbus_message_unref (reply);
564 dbus_pending_call_unref (pending);
567 /* TODO: Do we stil need this function? */
568 static AtspiAccessible *
569 ref_accessible_desktop (AtspiApplication *app)
572 DBusMessage *message, *reply;
573 DBusMessageIter iter, iter_array;
578 g_object_ref (desktop);
581 desktop = _atspi_accessible_new (app, atspi_path_root);
586 g_hash_table_insert (app->hash, g_strdup (desktop->parent.path),
587 g_object_ref (desktop));
588 app->root = g_object_ref (desktop);
589 desktop->name = g_strdup ("main");
590 message = dbus_message_new_method_call (atspi_bus_registry,
592 atspi_interface_accessible,
597 reply = _atspi_dbus_send_with_reply_and_block (message, &error);
598 if (!reply || strcmp (dbus_message_get_signature (reply), "a(so)") != 0)
602 g_warning ("Couldn't get application list: %s", error->message);
603 g_clear_error (&error);
606 dbus_message_unref (reply);
609 dbus_message_iter_init (reply, &iter);
610 dbus_message_iter_recurse (&iter, &iter_array);
611 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
613 const char *app_name, *path;
614 get_reference_from_iter (&iter_array, &app_name, &path);
615 add_app_to_desktop (desktop, app_name);
617 dbus_message_unref (reply);
619 /* Record the alternate name as an alias for org.a11y.atspi.Registry */
620 bus_name_dup = g_strdup (dbus_message_get_sender (reply));
622 g_hash_table_insert (app_hash, bus_name_dup, app);
624 return g_object_ref (desktop);
628 _atspi_ref_accessible (const char *app, const char *path)
630 AtspiApplication *a = get_application (app);
633 if ( APP_IS_REGISTRY(a))
636 g_object_unref (ref_accessible_desktop (a)); /* sets a->root */
637 return g_object_ref (a->root);
639 return ref_accessible (app, path);
643 _atspi_dbus_return_accessible_from_message (DBusMessage *message)
645 DBusMessageIter iter;
646 AtspiAccessible *retval = NULL;
647 const char *signature;
652 signature = dbus_message_get_signature (message);
653 if (!strcmp (signature, "(so)"))
655 dbus_message_iter_init (message, &iter);
656 retval = _atspi_dbus_return_accessible_from_iter (&iter);
660 g_warning ("AT-SPI: Called _atspi_dbus_return_accessible_from_message with strange signature %s", signature);
662 dbus_message_unref (message);
667 _atspi_dbus_return_accessible_from_iter (DBusMessageIter *iter)
669 const char *app_name, *path;
671 get_reference_from_iter (iter, &app_name, &path);
672 return ref_accessible (app_name, path);
676 _atspi_dbus_return_hyperlink_from_message (DBusMessage *message)
678 DBusMessageIter iter;
679 AtspiHyperlink *retval = NULL;
680 const char *signature;
685 signature = dbus_message_get_signature (message);
686 if (!strcmp (signature, "(so)"))
688 dbus_message_iter_init (message, &iter);
689 retval = _atspi_dbus_return_hyperlink_from_iter (&iter);
693 g_warning ("AT-SPI: Called _atspi_dbus_return_hyperlink_from_message with strange signature %s", signature);
695 dbus_message_unref (message);
700 _atspi_dbus_return_hyperlink_from_iter (DBusMessageIter *iter)
702 const char *app_name, *path;
704 get_reference_from_iter (iter, &app_name, &path);
705 return ref_hyperlink (app_name, path);
708 const char *cache_signal_type = "((so)(so)(so)a(so)assusau)";
710 static DBusHandlerResult
711 handle_add_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
713 DBusMessageIter iter;
715 if (strcmp (dbus_message_get_signature (message), cache_signal_type) != 0)
717 g_warning ("AT-SPI: AddAccessible with unknown signature %s\n",
718 dbus_message_get_signature (message));
719 return DBUS_HANDLER_RESULT_HANDLED;
722 dbus_message_iter_init (message, &iter);
723 add_accessible_from_iter (&iter);
724 return DBUS_HANDLER_RESULT_HANDLED;
730 DBusMessage *message;
734 static guint process_deferred_messages_id = -1;
737 process_deferred_message (BusDataClosure *closure)
739 int type = dbus_message_get_type (closure->message);
740 const char *interface = dbus_message_get_interface (closure->message);
742 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
743 !strncmp (interface, "org.a11y.atspi.Event.", 21))
745 _atspi_dbus_handle_event (closure->bus, closure->message, closure->data);
747 if (dbus_message_is_method_call (closure->message, atspi_interface_device_event_listener, "NotifyEvent"))
749 _atspi_dbus_handle_DeviceEvent (closure->bus,
750 closure->message, closure->data);
752 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "AddAccessible"))
754 handle_add_accessible (closure->bus, closure->message, closure->data);
756 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "RemoveAccessible"))
758 handle_remove_accessible (closure->bus, closure->message, closure->data);
760 if (dbus_message_is_signal (closure->message, "org.freedesktop.DBus", "NameOwnerChanged"))
762 handle_name_owner_changed (closure->bus, closure->message, closure->data);
766 static GQueue *deferred_messages = NULL;
769 _atspi_process_deferred_messages (gpointer data)
771 static int in_process_deferred_messages = 0;
772 BusDataClosure *closure;
774 if (in_process_deferred_messages)
776 in_process_deferred_messages = 1;
777 while ((closure = g_queue_pop_head (deferred_messages)))
779 process_deferred_message (closure);
780 dbus_message_unref (closure->message);
781 dbus_connection_unref (closure->bus);
784 /* If data is NULL, assume that we were called from GLib */
786 process_deferred_messages_id = -1;
787 in_process_deferred_messages = 0;
791 static DBusHandlerResult
792 defer_message (DBusConnection *connection, DBusMessage *message, void *user_data)
794 BusDataClosure *closure = g_new (BusDataClosure, 1);
796 closure->bus = dbus_connection_ref (bus);
797 closure->message = dbus_message_ref (message);
798 closure->data = user_data;
800 g_queue_push_tail (deferred_messages, closure);
802 if (process_deferred_messages_id == -1)
804 GSource *source = g_idle_source_new ();
805 g_source_set_callback (source, _atspi_process_deferred_messages, NULL, NULL);
806 process_deferred_messages_id = g_source_attach (source, atspi_main_context);
807 g_source_unref (source);
810 return DBUS_HANDLER_RESULT_HANDLED;
813 static DBusHandlerResult
814 atspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
816 int type = dbus_message_get_type (message);
817 const char *interface = dbus_message_get_interface (message);
819 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
820 !strncmp (interface, "org.a11y.atspi.Event.", 21))
822 return defer_message (bus, message, data);
824 if (dbus_message_is_method_call (message, atspi_interface_device_event_listener, "NotifyEvent"))
826 return defer_message (bus, message, data);
828 if (dbus_message_is_signal (message, atspi_interface_cache, "AddAccessible"))
830 return defer_message (bus, message, data);
832 if (dbus_message_is_signal (message, atspi_interface_cache, "RemoveAccessible"))
834 return defer_message (bus, message, data);
836 if (dbus_message_is_signal (message, "org.freedesktop.DBus", "NameOwnerChanged"))
838 defer_message (bus, message, data);
839 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
841 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
844 static const char *signal_interfaces[] =
846 "org.a11y.atspi.Event.Object",
847 "org.a11y.atspi.Event.Window",
848 "org.a11y.atspi.Event.Mouse",
849 "org.a11y.atspi.Event.Terminal",
850 "org.a11y.atspi.Event.Document",
851 "org.a11y.atspi.Event.Focus",
856 * Returns a 'canonicalized' value for DISPLAY,
857 * with the screen number stripped off if present.
859 * TODO: Avoid having duplicate functions for this here and in at-spi2-atk
862 spi_display_name (void)
864 char *canonical_display_name = NULL;
865 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
869 display_env = g_getenv ("DISPLAY");
870 if (!display_env || !display_env[0])
871 canonical_display_name = g_strdup (":0");
874 gchar *display_p, *screen_p;
875 canonical_display_name = g_strdup (display_env);
876 display_p = g_utf8_strrchr (canonical_display_name, -1, ':');
877 screen_p = g_utf8_strrchr (canonical_display_name, -1, '.');
878 if (screen_p && display_p && (screen_p > display_p))
886 canonical_display_name = g_strdup (display_env);
889 return canonical_display_name;
895 * Connects to the accessibility registry and initializes the SPI.
897 * Returns: 0 on success, 1 if already initialized, or an integer error code.
903 const gchar *no_cache;
914 _atspi_get_live_refs();
916 bus = atspi_get_a11y_bus ();
919 dbus_bus_register (bus, NULL);
920 atspi_dbus_connection_setup_with_g_main(bus, g_main_context_default());
921 dbus_connection_add_filter (bus, atspi_dbus_filter, NULL, NULL);
922 match = g_strdup_printf ("type='signal',interface='%s',member='AddAccessible'", atspi_interface_cache);
923 dbus_bus_add_match (bus, match, NULL);
925 match = g_strdup_printf ("type='signal',interface='%s',member='RemoveAccessible'", atspi_interface_cache);
926 dbus_bus_add_match (bus, match, NULL);
928 match = g_strdup_printf ("type='signal',interface='%s',member='ChildrenChanged'", atspi_interface_event_object);
929 dbus_bus_add_match (bus, match, NULL);
931 match = g_strdup_printf ("type='signal',interface='%s',member='PropertyChange'", atspi_interface_event_object);
932 dbus_bus_add_match (bus, match, NULL);
934 match = g_strdup_printf ("type='signal',interface='%s',member='StateChanged'", atspi_interface_event_object);
935 dbus_bus_add_match (bus, match, NULL);
938 dbus_bus_add_match (bus,
939 "type='signal', interface='org.freedesktop.DBus', member='NameOwnerChanged'",
942 no_cache = g_getenv ("ATSPI_NO_CACHE");
943 if (no_cache && g_strcmp0 (no_cache, "0") != 0)
944 atspi_no_cache = TRUE;
946 deferred_messages = g_queue_new ();
952 * atspi_is_initialized:
954 * Indicates whether AT-SPI has been initialized.
956 * Returns: %True if initialized; %False otherwise.
959 atspi_is_initialized ()
967 * Starts/enters the main event loop for the AT-SPI services.
969 * NOTE: This method does not return control; it is exited via a call to
970 * #atspi_event_quit from within an event handler.
974 atspi_event_main (void)
976 atspi_main_loop = g_main_loop_new (NULL, FALSE);
977 g_main_loop_run (atspi_main_loop);
978 atspi_main_loop = NULL;
984 * Quits the last main event loop for the AT-SPI services,
985 * See: #atspi_event_main
988 atspi_event_quit (void)
990 g_main_loop_quit (atspi_main_loop);
996 * Disconnects from #AtspiRegistry instances and releases
997 * any floating resources. Call only once at exit.
999 * Returns: 0 if there were no leaks, otherwise other integer values.
1011 atspi_inited = FALSE;
1015 leaked = g_hash_table_size (live_refs);
1027 static GSList *hung_processes;
1030 remove_hung_process (DBusPendingCall *pending, void *data)
1033 hung_processes = g_slist_remove (hung_processes, data);
1035 dbus_pending_call_unref (pending);
1039 check_for_hang (DBusMessage *message, DBusError *error, DBusConnection *bus, const char *bus_name)
1041 if (!message && error->name &&
1042 !strcmp (error->name, "org.freedesktop.DBus.Error.NoReply"))
1045 DBusMessage *message;
1046 gchar *bus_name_dup;
1047 DBusPendingCall *pending = NULL;
1048 for (l = hung_processes; l; l = l->next)
1049 if (!strcmp (l->data, bus_name))
1051 message = dbus_message_new_method_call (bus_name, "/",
1052 "org.freedesktop.DBus.Peer",
1056 dbus_connection_send_with_reply (bus, message, &pending, -1);
1057 dbus_message_unref (message);
1060 bus_name_dup = g_strdup (bus_name);
1061 hung_processes = g_slist_append (hung_processes, bus_name_dup);
1062 dbus_pending_call_set_notify (pending, remove_hung_process, bus_name_dup, NULL);
1067 connection_is_hung (const char *bus_name)
1071 for (l = hung_processes; l; l = l->next)
1072 if (!strcmp (l->data, bus_name))
1078 check_app (AtspiApplication *app, GError **error)
1080 if (!app || !app->bus)
1082 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
1083 _("The application no longer exists"));
1087 if (atspi_main_loop && connection_is_hung (app->bus_name))
1089 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC,
1090 "The process appears to be hung.");
1098 set_timeout (AtspiApplication *app)
1103 if (app && app_startup_time > 0)
1105 gettimeofday (&tv, NULL);
1106 diff = (tv.tv_sec - app->time_added.tv_sec) * 1000 + (tv.tv_usec - app->time_added.tv_usec) / 1000;
1107 dbind_set_timeout (MAX(method_call_timeout, app_startup_time - diff));
1110 dbind_set_timeout (method_call_timeout);
1114 _atspi_dbus_call (gpointer obj, const char *interface, const char *method, GError **error, const char *type, ...)
1119 AtspiObject *aobj = ATSPI_OBJECT (obj);
1121 if (!check_app (aobj->app, error))
1124 va_start (args, type);
1125 dbus_error_init (&err);
1126 set_timeout (aobj->app);
1127 retval = dbind_method_call_reentrant_va (aobj->app->bus, aobj->app->bus_name,
1128 aobj->path, interface, method, &err,
1131 check_for_hang (NULL, &err, aobj->app->bus, aobj->app->bus_name);
1132 _atspi_process_deferred_messages ((gpointer)TRUE);
1133 if (dbus_error_is_set (&err))
1135 g_set_error(error, ATSPI_ERROR, ATSPI_ERROR_IPC, "%s", err.message);
1136 dbus_error_free (&err);
1142 _atspi_dbus_call_partial (gpointer obj,
1143 const char *interface,
1146 const char *type, ...)
1150 va_start (args, type);
1151 return _atspi_dbus_call_partial_va (obj, interface, method, error, type, args);
1156 _atspi_dbus_call_partial_va (gpointer obj,
1157 const char *interface,
1163 AtspiObject *aobj = ATSPI_OBJECT (obj);
1165 DBusMessage *msg = NULL, *reply = NULL;
1166 DBusMessageIter iter;
1169 dbus_error_init (&err);
1171 if (!check_app (aobj->app, error))
1174 msg = dbus_message_new_method_call (aobj->app->bus_name, aobj->path, interface, method);
1179 dbus_message_iter_init_append (msg, &iter);
1180 dbind_any_marshal_va (&iter, &p, args);
1182 set_timeout (aobj->app);
1183 reply = dbind_send_and_allow_reentry (aobj->app->bus, msg, &err);
1184 check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1188 dbus_message_unref (msg);
1189 _atspi_process_deferred_messages ((gpointer)TRUE);
1190 if (dbus_error_is_set (&err))
1192 /* TODO: Set gerror */
1193 dbus_error_free (&err);
1196 if (reply && 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);
1202 dbus_message_unref (reply);
1210 _atspi_dbus_get_property (gpointer obj, const char *interface, const char *name, GError **error, const char *type, void *data)
1212 DBusMessage *message, *reply;
1213 DBusMessageIter iter, iter_variant;
1215 dbus_bool_t retval = FALSE;
1216 AtspiObject *aobj = ATSPI_OBJECT (obj);
1217 char expected_type = (type [0] == '(' ? 'r' : type [0]);
1222 if (!check_app (aobj->app, error))
1225 message = dbus_message_new_method_call (aobj->app->bus_name,
1227 "org.freedesktop.DBus.Properties",
1231 // TODO: throw exception
1234 dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1235 dbus_error_init (&err);
1236 set_timeout (aobj->app);
1237 reply = dbind_send_and_allow_reentry (aobj->app->bus, message, &err);
1238 check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1239 dbus_message_unref (message);
1240 _atspi_process_deferred_messages ((gpointer)TRUE);
1243 // TODO: throw exception
1247 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
1249 const char *err_str = NULL;
1250 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &err_str, DBUS_TYPE_INVALID);
1252 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err_str);
1256 dbus_message_iter_init (reply, &iter);
1257 if (dbus_message_iter_get_arg_type (&iter) != 'v')
1259 g_warning ("AT-SPI: expected a variant when fetching %s from interface %s; got %s\n", name, interface, dbus_message_get_signature (reply));
1262 dbus_message_iter_recurse (&iter, &iter_variant);
1263 if (dbus_message_iter_get_arg_type (&iter_variant) != expected_type)
1265 g_warning ("atspi_dbus_get_property: Wrong type: expected %s, got %c\n", type, dbus_message_iter_get_arg_type (&iter_variant));
1268 if (!strcmp (type, "(so)"))
1270 *((AtspiAccessible **)data) = _atspi_dbus_return_accessible_from_iter (&iter_variant);
1274 dbus_message_iter_get_basic (&iter_variant, data);
1275 if (type [0] == 's')
1276 *(char **)data = g_strdup (*(char **)data);
1280 dbus_error_free (&err);
1282 dbus_message_unref (reply);
1287 _atspi_dbus_send_with_reply_and_block (DBusMessage *message, GError **error)
1291 AtspiApplication *app;
1292 DBusConnection *bus;
1294 app = get_application (dbus_message_get_destination (message));
1296 if (app && !app->bus)
1297 return NULL; /* will fail anyway; app has been disposed */
1299 bus = (app ? app->bus : _atspi_bus());
1300 dbus_error_init (&err);
1302 reply = dbind_send_and_allow_reentry (bus, message, &err);
1303 _atspi_process_deferred_messages ((gpointer)TRUE);
1304 dbus_message_unref (message);
1305 if (dbus_error_is_set (&err))
1308 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err.message);
1309 dbus_error_free (&err);
1315 _atspi_dbus_return_hash_from_message (DBusMessage *message)
1317 DBusMessageIter iter;
1323 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1325 dbus_message_iter_init (message, &iter);
1326 ret = _atspi_dbus_hash_from_iter (&iter);
1327 dbus_message_unref (message);
1332 _atspi_dbus_hash_from_iter (DBusMessageIter *iter)
1334 GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
1335 (GDestroyNotify) g_free,
1336 (GDestroyNotify) g_free);
1337 DBusMessageIter iter_array, iter_dict;
1339 dbus_message_iter_recurse (iter, &iter_array);
1340 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1342 const char *name, *value;
1343 dbus_message_iter_recurse (&iter_array, &iter_dict);
1344 dbus_message_iter_get_basic (&iter_dict, &name);
1345 dbus_message_iter_next (&iter_dict);
1346 dbus_message_iter_get_basic (&iter_dict, &value);
1347 g_hash_table_insert (hash, g_strdup (name), g_strdup (value));
1348 dbus_message_iter_next (&iter_array);
1354 _atspi_dbus_return_attribute_array_from_message (DBusMessage *message)
1356 DBusMessageIter iter;
1362 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1364 dbus_message_iter_init (message, &iter);
1366 ret = _atspi_dbus_attribute_array_from_iter (&iter);
1367 dbus_message_unref (message);
1372 _atspi_dbus_attribute_array_from_iter (DBusMessageIter *iter)
1374 DBusMessageIter iter_array, iter_dict;
1375 GArray *array = g_array_new (TRUE, TRUE, sizeof (gchar *));
1377 dbus_message_iter_recurse (iter, &iter_array);
1378 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1380 const char *name, *value;
1382 dbus_message_iter_recurse (&iter_array, &iter_dict);
1383 dbus_message_iter_get_basic (&iter_dict, &name);
1384 dbus_message_iter_next (&iter_dict);
1385 dbus_message_iter_get_basic (&iter_dict, &value);
1386 str = g_strdup_printf ("%s:%s", name, value);
1387 array = g_array_append_val (array, str);
1388 dbus_message_iter_next (&iter_array);;
1394 _atspi_dbus_set_interfaces (AtspiAccessible *accessible, DBusMessageIter *iter)
1396 DBusMessageIter iter_array;
1398 accessible->interfaces = 0;
1399 dbus_message_iter_recurse (iter, &iter_array);
1400 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1404 dbus_message_iter_get_basic (&iter_array, &iface);
1405 if (!strcmp (iface, "org.freedesktop.DBus.Introspectable")) continue;
1406 n = _atspi_get_iface_num (iface);
1409 g_warning ("AT-SPI: Unknown interface %s", iface);
1412 accessible->interfaces |= (1 << n);
1413 dbus_message_iter_next (&iter_array);
1415 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_INTERFACES);
1419 _atspi_dbus_set_state (AtspiAccessible *accessible, DBusMessageIter *iter)
1421 DBusMessageIter iter_array;
1423 dbus_uint32_t *states;
1425 dbus_message_iter_recurse (iter, &iter_array);
1426 dbus_message_iter_get_fixed_array (&iter_array, &states, &count);
1429 g_warning ("AT-SPI: expected 2 values in states array; got %d\n", count);
1430 if (!accessible->states)
1431 accessible->states = _atspi_state_set_new_internal (accessible, 0);
1435 guint64 val = ((guint64)states [1]) << 32;
1437 if (!accessible->states)
1438 accessible->states = _atspi_state_set_new_internal (accessible, val);
1440 accessible->states->states = val;
1442 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_STATES);
1446 _atspi_error_quark (void)
1448 return g_quark_from_static_string ("atspi_error");
1452 * Gets the IOR from the XDisplay.
1455 get_accessibility_bus_address_x11 (void)
1459 Display *bridge_display = NULL;
1462 unsigned char *data_x11 = NULL;
1463 unsigned long nitems;
1464 unsigned long leftover;
1467 display_name = spi_display_name ();
1468 if (display_name != NULL)
1470 bridge_display = XOpenDisplay (display_name);
1471 g_free (display_name);
1473 if (!bridge_display)
1475 g_warning ("Could not open X display");
1479 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
1480 XGetWindowProperty (bridge_display,
1481 XDefaultRootWindow (bridge_display),
1483 (long) BUFSIZ, False,
1484 (Atom) 31, &actual_type, &actual_format,
1485 &nitems, &leftover, &data_x11);
1486 XCloseDisplay (bridge_display);
1488 data = g_strdup ((gchar *)data_x11);
1494 get_accessibility_bus_address_dbus (void)
1496 DBusConnection *session_bus = NULL;
1497 DBusMessage *message;
1500 char *address = NULL;
1502 session_bus = dbus_bus_get (DBUS_BUS_SESSION, NULL);
1506 message = dbus_message_new_method_call ("org.a11y.Bus",
1511 dbus_error_init (&error);
1512 reply = dbus_connection_send_with_reply_and_block (session_bus,
1516 dbus_message_unref (message);
1520 g_warning ("Error retrieving accessibility bus address: %s: %s",
1521 error.name, error.message);
1522 dbus_error_free (&error);
1527 const char *tmp_address;
1528 if (!dbus_message_get_args (reply,
1534 dbus_message_unref (reply);
1537 address = g_strdup (tmp_address);
1538 dbus_message_unref (reply);
1544 static DBusConnection *a11y_bus;
1545 static dbus_int32_t a11y_dbus_slot = -1;
1548 a11y_bus_free (void *data)
1550 if (data == a11y_bus)
1553 dbus_connection_free_data_slot (&a11y_dbus_slot);
1558 atspi_get_a11y_bus (void)
1563 if (a11y_bus && dbus_connection_get_is_connected (a11y_bus))
1566 if (a11y_dbus_slot == -1)
1567 if (!dbus_connection_allocate_data_slot (&a11y_dbus_slot))
1568 g_warning ("at-spi: Unable to allocate D-Bus slot");
1570 address = get_accessibility_bus_address_x11 ();
1572 address = get_accessibility_bus_address_dbus ();
1576 dbus_error_init (&error);
1577 a11y_bus = dbus_connection_open_private (address, &error);
1582 g_warning ("Couldn't connect to accessibility bus: %s", error.message);
1583 dbus_error_free (&error);
1588 if (!dbus_bus_register (a11y_bus, &error))
1590 g_warning ("Couldn't register with accessibility bus: %s", error.message);
1591 dbus_error_free (&error);
1592 dbus_connection_close (a11y_bus);
1593 dbus_connection_unref (a11y_bus);
1599 /* Simulate a weak ref on the bus */
1600 dbus_connection_set_data (a11y_bus, a11y_dbus_slot, a11y_bus, a11y_bus_free);
1606 * Set the timeout used for method calls. If this is not set explicitly,
1607 * a default of 0.8 ms is used.
1608 * Note that at-spi2-registryd currently uses a timeout of 3 seconds when
1609 * sending a keyboard event notification. This means that, if an AT makes
1610 * a call in response to the keyboard notification and the application
1611 * being called does not respond before the timeout is reached,
1612 * at-spi2-registryd will time out on the keyboard event notification and
1613 * pass the key onto the application (ie, reply to indicate that the key
1614 * was not consumed), so this may make it undesirable to set a timeout
1615 * larger than 3 seconds.
1617 * @val: The timeout value, in milliseconds, or -1 to disable the timeout.
1618 * @startup_time: The amount of time, in milliseconds, to allow to pass
1619 * before enforcing timeouts on an application. Can be used to prevent
1620 * timeout exceptions if an application is likely to block for an extended
1621 * period of time on initialization. -1 can be passed to disable this
1624 * By default, the normal timeout is set to 800 ms, and the application startup
1625 * timeout is set to 15 seconds.
1628 atspi_set_timeout (gint val, gint startup_time)
1630 method_call_timeout = val;
1631 app_startup_time = startup_time;
1635 * atspi_set_main_context:
1636 * @cnx: The #GmainContext to use.
1638 * Sets the main loop context that AT-SPI should assume is in use when
1639 * setting an idle callback.
1640 * This function should be called by application-side implementors (ie,
1641 * at-spi2-atk) when it is desirable to re-enter the main loop.
1644 atspi_set_main_context (GMainContext *cnx)
1646 atspi_main_context = cnx;
1647 atspi_dbus_connection_setup_with_g_main (atspi_get_a11y_bus (), cnx);
1650 #ifdef DEBUG_REF_COUNTS
1652 print_disposed (gpointer key, gpointer value, gpointer data)
1654 AtspiAccessible *accessible = key;
1655 if (accessible->parent.app)
1657 g_print ("disposed: %s %d\n", accessible->name, accessible->role);
1663 g_hash_table_foreach (live_refs, print_disposed, NULL);
1668 _atspi_name_compat (gchar *name)
1682 * atspi_role_get_name:
1683 * @role: an #AtspiRole object to query.
1685 * Gets a localizable string that indicates the name of an #AtspiRole.
1686 * <em>DEPRECATED.</em>
1688 * Returns: a localizable string name for an #AtspiRole enumerated type.
1691 atspi_role_get_name (AtspiRole role)
1693 gchar *retval = NULL;
1694 GTypeClass *type_class;
1696 const gchar *name = NULL;
1698 type_class = g_type_class_ref (ATSPI_TYPE_ROLE);
1699 g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), NULL);
1701 value = g_enum_get_value (G_ENUM_CLASS (type_class), role);
1705 retval = g_strdup (value->value_nick);
1709 return _atspi_name_compat (g_strdup (retval));