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);
40 gboolean _atspi_process_deferred_messages (gpointer data);
42 static DBusConnection *bus = NULL;
43 static GHashTable *live_refs = NULL;
44 static gint method_call_timeout = 800;
45 static gint app_startup_time = 15000;
47 GMainLoop *atspi_main_loop;
48 GMainContext *atspi_main_context;
49 gboolean atspi_no_cache;
51 const char *atspi_path_dec = ATSPI_DBUS_PATH_DEC;
52 const char *atspi_path_registry = ATSPI_DBUS_PATH_REGISTRY;
53 const char *atspi_path_root = ATSPI_DBUS_PATH_ROOT;
54 const char *atspi_bus_registry = ATSPI_DBUS_NAME_REGISTRY;
55 const char *atspi_interface_accessible = ATSPI_DBUS_INTERFACE_ACCESSIBLE;
56 const char *atspi_interface_action = ATSPI_DBUS_INTERFACE_ACTION;
57 const char *atspi_interface_application = ATSPI_DBUS_INTERFACE_APPLICATION;
58 const char *atspi_interface_collection = ATSPI_DBUS_INTERFACE_COLLECTION;
59 const char *atspi_interface_component = ATSPI_DBUS_INTERFACE_COMPONENT;
60 const char *atspi_interface_dec = ATSPI_DBUS_INTERFACE_DEC;
61 const char *atspi_interface_device_event_listener = ATSPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER;
62 const char *atspi_interface_document = ATSPI_DBUS_INTERFACE_DOCUMENT;
63 const char *atspi_interface_editable_text = ATSPI_DBUS_INTERFACE_EDITABLE_TEXT;
64 const char *atspi_interface_event_object = ATSPI_DBUS_INTERFACE_EVENT_OBJECT;
65 const char *atspi_interface_hyperlink = ATSPI_DBUS_INTERFACE_HYPERLINK;
66 const char *atspi_interface_hypertext = ATSPI_DBUS_INTERFACE_HYPERTEXT;
67 const char *atspi_interface_image = ATSPI_DBUS_INTERFACE_IMAGE;
68 const char *atspi_interface_registry = ATSPI_DBUS_INTERFACE_REGISTRY;
69 const char *atspi_interface_selection = ATSPI_DBUS_INTERFACE_SELECTION;
70 const char *atspi_interface_table = ATSPI_DBUS_INTERFACE_TABLE;
71 const char *atspi_interface_text = ATSPI_DBUS_INTERFACE_TEXT;
72 const char *atspi_interface_cache = ATSPI_DBUS_INTERFACE_CACHE;
73 const char *atspi_interface_value = ATSPI_DBUS_INTERFACE_VALUE;
75 static const char *interfaces[] =
77 ATSPI_DBUS_INTERFACE_ACCESSIBLE,
78 ATSPI_DBUS_INTERFACE_ACTION,
79 ATSPI_DBUS_INTERFACE_APPLICATION,
80 ATSPI_DBUS_INTERFACE_COLLECTION,
81 ATSPI_DBUS_INTERFACE_COMPONENT,
82 ATSPI_DBUS_INTERFACE_DOCUMENT,
83 ATSPI_DBUS_INTERFACE_EDITABLE_TEXT,
84 ATSPI_DBUS_INTERFACE_HYPERLINK,
85 ATSPI_DBUS_INTERFACE_HYPERTEXT,
86 ATSPI_DBUS_INTERFACE_IMAGE,
87 "org.a11y.atspi.LoginHelper",
88 ATSPI_DBUS_INTERFACE_SELECTION,
89 ATSPI_DBUS_INTERFACE_TABLE,
90 ATSPI_DBUS_INTERFACE_TEXT,
91 ATSPI_DBUS_INTERFACE_VALUE,
96 _atspi_get_iface_num (const char *iface)
98 /* TODO: Use a binary search or hash to improve performance */
101 for (i = 0; interfaces[i]; i++)
103 if (!strcmp(iface, interfaces[i])) return i;
109 _atspi_get_live_refs (void)
113 live_refs = g_hash_table_new (g_direct_hash, g_direct_equal);
118 /* TODO: Add an application parameter */
125 g_error ("AT-SPI: COuldn't connect to accessibility bus. Is at-spi-bus-launcher running?");
129 #define APP_IS_REGISTRY(app) (!strcmp (app->bus_name, atspi_bus_registry))
131 static AtspiAccessible *desktop;
143 g_hash_table_destroy (refs);
148 dbus_connection_close (bus);
149 dbus_connection_unref (bus);
155 for (l = desktop->children; l;)
157 GList *next = l->next;
158 AtspiAccessible *child = l->data;
159 g_object_run_dispose (G_OBJECT (child->parent.app));
160 g_object_run_dispose (G_OBJECT (child));
163 g_object_run_dispose (G_OBJECT (desktop->parent.app));
164 g_object_unref (desktop);
168 static gboolean atspi_inited = FALSE;
170 static GHashTable *app_hash = NULL;
173 handle_get_bus_address (DBusPendingCall *pending, void *user_data)
175 AtspiApplication *app = user_data;
176 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
177 DBusMessage *message;
179 DBusPendingCall *new_pending;
181 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
183 if (dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &address,
184 DBUS_TYPE_INVALID) && address [0])
189 dbus_error_init (&error);
190 bus = dbus_connection_open_private (address, &error);
195 dbus_connection_unref (app->bus);
201 g_warning ("Unable to open bus connection: %s", error.message);
202 dbus_error_free (&error);
206 dbus_message_unref (reply);
207 dbus_pending_call_unref (pending);
210 return; /* application has gone away / been disposed */
212 message = dbus_message_new_method_call (app->bus_name,
213 "/org/a11y/atspi/cache",
214 atspi_interface_cache, "GetItems");
216 dbus_connection_send_with_reply (app->bus, message, &new_pending, 2000);
217 dbus_pending_call_set_notify (new_pending, handle_get_items, app, NULL);
218 dbus_message_unref (message);
221 static AtspiApplication *
222 get_application (const char *bus_name)
224 AtspiApplication *app = NULL;
226 DBusMessage *message;
227 DBusPendingCall *pending = NULL;
231 app_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_object_unref);
232 if (!app_hash) return NULL;
234 app = g_hash_table_lookup (app_hash, bus_name);
236 bus_name_dup = g_strdup (bus_name);
237 if (!bus_name_dup) return NULL;
238 // TODO: change below to something that will send state-change:defunct notification if necessary */
239 app = _atspi_application_new (bus_name);
240 app->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
241 app->bus = dbus_connection_ref (_atspi_bus ());
242 gettimeofday (&app->time_added, NULL);
243 app->cache = ATSPI_CACHE_UNDEFINED;
244 g_hash_table_insert (app_hash, bus_name_dup, app);
245 message = dbus_message_new_method_call (bus_name, atspi_path_root,
246 atspi_interface_application, "GetApplicationBusAddress");
248 dbus_connection_send_with_reply (app->bus, message, &pending, 2000);
249 dbus_pending_call_set_notify (pending, handle_get_bus_address, app, NULL);
250 dbus_message_unref (message);
254 static AtspiAccessible *
255 ref_accessible (const char *app_name, const char *path)
257 AtspiApplication *app;
260 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
263 app = get_application (app_name);
265 if (!strcmp (path, "/org/a11y/atspi/accessible/root"))
269 app->root = _atspi_accessible_new (app, atspi_path_root);
270 app->root->accessible_parent = atspi_get_desktop (0);
271 app->root->accessible_parent->children = g_list_append (app->root->accessible_parent->children, g_object_ref (app->root));
273 return g_object_ref (app->root);
276 a = g_hash_table_lookup (app->hash, path);
279 return g_object_ref (a);
281 a = _atspi_accessible_new (app, path);
284 g_hash_table_insert (app->hash, g_strdup (a->parent.path), g_object_ref (a));
288 static AtspiHyperlink *
289 ref_hyperlink (const char *app_name, const char *path)
291 AtspiApplication *app = get_application (app_name);
292 AtspiHyperlink *hyperlink;
294 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
297 hyperlink = g_hash_table_lookup (app->hash, path);
300 return g_object_ref (hyperlink);
302 hyperlink = _atspi_hyperlink_new (app, path);
303 g_hash_table_insert (app->hash, g_strdup (hyperlink->parent.path), hyperlink);
304 /* TODO: This should be a weak ref */
305 g_object_ref (hyperlink); /* for the hash */
318 GArray *state_bitflags;
321 static DBusHandlerResult
322 handle_remove_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
324 const char *sender = dbus_message_get_sender (message);
325 AtspiApplication *app;
327 DBusMessageIter iter, iter_struct;
328 const char *signature = dbus_message_get_signature (message);
331 if (strcmp (signature, "(so)") != 0)
333 g_warning ("AT-SPI: Unknown signature %s for RemoveAccessible", signature);
334 return DBUS_HANDLER_RESULT_HANDLED;
337 dbus_message_iter_init (message, &iter);
338 dbus_message_iter_recurse (&iter, &iter_struct);
339 dbus_message_iter_get_basic (&iter_struct, &sender);
340 dbus_message_iter_next (&iter_struct);
341 dbus_message_iter_get_basic (&iter_struct, &path);
342 app = get_application (sender);
343 a = ref_accessible (sender, path);
345 return DBUS_HANDLER_RESULT_HANDLED;
346 g_object_run_dispose (G_OBJECT (a));
347 g_hash_table_remove (app->hash, a->parent.path);
348 g_object_unref (a); /* unref our own ref */
349 return DBUS_HANDLER_RESULT_HANDLED;
352 static DBusHandlerResult
353 handle_name_owner_changed (DBusConnection *bus, DBusMessage *message, void *user_data)
355 const char *name, *new, *old;
356 static gboolean registry_lost = FALSE;
358 if (!dbus_message_get_args (message, NULL,
359 DBUS_TYPE_STRING, &name,
360 DBUS_TYPE_STRING, &old,
361 DBUS_TYPE_STRING, &new,
364 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
367 if (!strcmp (name, "org.a11y.atspi.Registry"))
369 if (registry_lost && !old[0])
371 _atspi_reregister_event_listeners ();
372 _atspi_reregister_device_listeners ();
373 registry_lost = FALSE;
376 registry_lost = TRUE;
380 AtspiAccessible *desktop = atspi_get_desktop (0);
382 for (l = desktop->children; l; l = l->next)
384 AtspiAccessible *child = l->data;
385 if (!strcmp (child->parent.app->bus_name, old))
386 g_object_run_dispose (G_OBJECT (child->parent.app));
388 g_object_unref (desktop);
390 return DBUS_HANDLER_RESULT_HANDLED;
394 add_app_to_desktop (AtspiAccessible *a, const char *bus_name)
396 AtspiAccessible *obj = ref_accessible (bus_name, atspi_path_root);
397 /* The app will be added to the desktop as a side-effect of calling
399 g_object_unref (obj);
400 return (obj != NULL);
404 send_children_changed (AtspiAccessible *parent, AtspiAccessible *child, gboolean add)
408 memset (&e, 0, sizeof (e));
409 e.type = (add? "object:children-changed:add": "object:children-changed:remove");
411 e.detail1 = g_list_index (parent->children, child);
413 _atspi_send_event (&e);
417 unref_object_and_descendants (AtspiAccessible *obj)
421 for (l = obj->children; l; l = l->next)
423 unref_object_and_descendants (l->data);
425 g_object_unref (obj);
429 remove_app_from_desktop (AtspiAccessible *a, const char *bus_name)
432 AtspiAccessible *child;
434 for (l = a->children; l; l = l->next)
437 if (!strcmp (bus_name, child->parent.app->bus_name)) break;
443 send_children_changed (a, child, FALSE);
444 a->children = g_list_remove (a->children, child);
445 unref_object_and_descendants (child);
450 get_reference_from_iter (DBusMessageIter *iter, const char **app_name, const char **path)
452 DBusMessageIter iter_struct;
454 dbus_message_iter_recurse (iter, &iter_struct);
455 dbus_message_iter_get_basic (&iter_struct, app_name);
456 dbus_message_iter_next (&iter_struct);
457 dbus_message_iter_get_basic (&iter_struct, path);
458 dbus_message_iter_next (iter);
462 add_accessible_from_iter (DBusMessageIter *iter)
464 DBusMessageIter iter_struct, iter_array;
465 const char *app_name, *path;
466 AtspiAccessible *accessible;
467 const char *name, *description;
470 dbus_message_iter_recurse (iter, &iter_struct);
473 get_reference_from_iter (&iter_struct, &app_name, &path);
474 accessible = ref_accessible (app_name, path);
478 /* Get application: TODO */
479 dbus_message_iter_next (&iter_struct);
482 get_reference_from_iter (&iter_struct, &app_name, &path);
483 if (accessible->accessible_parent)
484 g_object_unref (accessible->accessible_parent);
485 accessible->accessible_parent = ref_accessible (app_name, path);
488 while (accessible->children)
490 g_object_unref (accessible->children->data);
491 accessible->children = g_list_remove (accessible->children, accessible->children->data);
493 dbus_message_iter_recurse (&iter_struct, &iter_array);
494 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
496 AtspiAccessible *child;
497 get_reference_from_iter (&iter_array, &app_name, &path);
498 child = ref_accessible (app_name, path);
499 accessible->children = g_list_append (accessible->children, child);
503 dbus_message_iter_next (&iter_struct);
504 _atspi_dbus_set_interfaces (accessible, &iter_struct);
505 dbus_message_iter_next (&iter_struct);
508 if (accessible->name)
509 g_free (accessible->name);
510 dbus_message_iter_get_basic (&iter_struct, &name);
511 accessible->name = g_strdup (name);
512 dbus_message_iter_next (&iter_struct);
515 dbus_message_iter_get_basic (&iter_struct, &role);
516 accessible->role = role;
517 dbus_message_iter_next (&iter_struct);
520 if (accessible->description)
521 g_free (accessible->description);
522 dbus_message_iter_get_basic (&iter_struct, &description);
523 accessible->description = g_strdup (description);
524 dbus_message_iter_next (&iter_struct);
526 _atspi_dbus_set_state (accessible, &iter_struct);
527 dbus_message_iter_next (&iter_struct);
529 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_NAME | ATSPI_CACHE_ROLE |
530 ATSPI_CACHE_PARENT | ATSPI_CACHE_DESCRIPTION);
531 if (!atspi_state_set_contains (accessible->states,
532 ATSPI_STATE_MANAGES_DESCENDANTS))
533 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_CHILDREN);
535 /* This is a bit of a hack since the cache holds a ref, so we don't need
536 * the one provided for us anymore */
537 g_object_unref (accessible);
541 handle_get_items (DBusPendingCall *pending, void *user_data)
543 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
544 DBusMessageIter iter, iter_array;
546 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
548 const char *sender = dbus_message_get_sender (reply);
549 const char *error = NULL;
550 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &error,
552 g_warning ("AT-SPI: Error in GetItems, sender=%s, error=%s", sender, error);
553 dbus_message_unref (reply);
554 dbus_pending_call_unref (pending);
558 dbus_message_iter_init (reply, &iter);
559 dbus_message_iter_recurse (&iter, &iter_array);
560 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
562 add_accessible_from_iter (&iter_array);
563 dbus_message_iter_next (&iter_array);
565 dbus_message_unref (reply);
566 dbus_pending_call_unref (pending);
569 /* TODO: Do we stil need this function? */
570 static AtspiAccessible *
571 ref_accessible_desktop (AtspiApplication *app)
574 DBusMessage *message, *reply;
575 DBusMessageIter iter, iter_array;
580 g_object_ref (desktop);
583 desktop = _atspi_accessible_new (app, atspi_path_root);
588 g_hash_table_insert (app->hash, g_strdup (desktop->parent.path),
589 g_object_ref (desktop));
590 app->root = g_object_ref (desktop);
591 desktop->name = g_strdup ("main");
592 message = dbus_message_new_method_call (atspi_bus_registry,
594 atspi_interface_accessible,
599 reply = _atspi_dbus_send_with_reply_and_block (message, &error);
600 if (!reply || strcmp (dbus_message_get_signature (reply), "a(so)") != 0)
604 g_warning ("Couldn't get application list: %s", error->message);
605 g_clear_error (&error);
608 dbus_message_unref (reply);
611 dbus_message_iter_init (reply, &iter);
612 dbus_message_iter_recurse (&iter, &iter_array);
613 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
615 const char *app_name, *path;
616 get_reference_from_iter (&iter_array, &app_name, &path);
617 add_app_to_desktop (desktop, app_name);
619 dbus_message_unref (reply);
621 /* Record the alternate name as an alias for org.a11y.atspi.Registry */
622 bus_name_dup = g_strdup (dbus_message_get_sender (reply));
624 g_hash_table_insert (app_hash, bus_name_dup, app);
626 return g_object_ref (desktop);
630 _atspi_ref_accessible (const char *app, const char *path)
632 AtspiApplication *a = get_application (app);
635 if ( APP_IS_REGISTRY(a))
638 g_object_unref (ref_accessible_desktop (a)); /* sets a->root */
639 return g_object_ref (a->root);
641 return ref_accessible (app, path);
645 _atspi_dbus_return_accessible_from_message (DBusMessage *message)
647 DBusMessageIter iter;
648 AtspiAccessible *retval = NULL;
649 const char *signature;
654 signature = dbus_message_get_signature (message);
655 if (!strcmp (signature, "(so)"))
657 dbus_message_iter_init (message, &iter);
658 retval = _atspi_dbus_return_accessible_from_iter (&iter);
662 g_warning ("AT-SPI: Called _atspi_dbus_return_accessible_from_message with strange signature %s", signature);
664 dbus_message_unref (message);
669 _atspi_dbus_return_accessible_from_iter (DBusMessageIter *iter)
671 const char *app_name, *path;
673 get_reference_from_iter (iter, &app_name, &path);
674 return ref_accessible (app_name, path);
678 _atspi_dbus_return_hyperlink_from_message (DBusMessage *message)
680 DBusMessageIter iter;
681 AtspiHyperlink *retval = NULL;
682 const char *signature;
687 signature = dbus_message_get_signature (message);
688 if (!strcmp (signature, "(so)"))
690 dbus_message_iter_init (message, &iter);
691 retval = _atspi_dbus_return_hyperlink_from_iter (&iter);
695 g_warning ("AT-SPI: Called _atspi_dbus_return_hyperlink_from_message with strange signature %s", signature);
697 dbus_message_unref (message);
702 _atspi_dbus_return_hyperlink_from_iter (DBusMessageIter *iter)
704 const char *app_name, *path;
706 get_reference_from_iter (iter, &app_name, &path);
707 return ref_hyperlink (app_name, path);
710 const char *cache_signal_type = "((so)(so)(so)a(so)assusau)";
712 static DBusHandlerResult
713 handle_add_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
715 DBusMessageIter iter;
717 if (strcmp (dbus_message_get_signature (message), cache_signal_type) != 0)
719 g_warning ("AT-SPI: AddAccessible with unknown signature %s\n",
720 dbus_message_get_signature (message));
721 return DBUS_HANDLER_RESULT_HANDLED;
724 dbus_message_iter_init (message, &iter);
725 add_accessible_from_iter (&iter);
726 return DBUS_HANDLER_RESULT_HANDLED;
732 DBusMessage *message;
736 static guint process_deferred_messages_id = -1;
739 process_deferred_message (BusDataClosure *closure)
741 int type = dbus_message_get_type (closure->message);
742 const char *interface = dbus_message_get_interface (closure->message);
744 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
745 !strncmp (interface, "org.a11y.atspi.Event.", 21))
747 _atspi_dbus_handle_event (closure->bus, closure->message, closure->data);
749 if (dbus_message_is_method_call (closure->message, atspi_interface_device_event_listener, "NotifyEvent"))
751 _atspi_dbus_handle_DeviceEvent (closure->bus,
752 closure->message, closure->data);
754 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "AddAccessible"))
756 handle_add_accessible (closure->bus, closure->message, closure->data);
758 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "RemoveAccessible"))
760 handle_remove_accessible (closure->bus, closure->message, closure->data);
762 if (dbus_message_is_signal (closure->message, "org.freedesktop.DBus", "NameOwnerChanged"))
764 handle_name_owner_changed (closure->bus, closure->message, closure->data);
768 static GQueue *deferred_messages = NULL;
771 _atspi_process_deferred_messages (gpointer data)
773 static int in_process_deferred_messages = 0;
774 BusDataClosure *closure;
776 if (in_process_deferred_messages)
778 in_process_deferred_messages = 1;
779 while ((closure = g_queue_pop_head (deferred_messages)))
781 process_deferred_message (closure);
782 dbus_message_unref (closure->message);
783 dbus_connection_unref (closure->bus);
786 /* If data is NULL, assume that we were called from GLib */
788 process_deferred_messages_id = -1;
789 in_process_deferred_messages = 0;
793 static DBusHandlerResult
794 defer_message (DBusConnection *connection, DBusMessage *message, void *user_data)
796 BusDataClosure *closure = g_new (BusDataClosure, 1);
798 closure->bus = dbus_connection_ref (bus);
799 closure->message = dbus_message_ref (message);
800 closure->data = user_data;
802 g_queue_push_tail (deferred_messages, closure);
804 if (process_deferred_messages_id == -1)
806 GSource *source = g_idle_source_new ();
807 g_source_set_callback (source, _atspi_process_deferred_messages, NULL, NULL);
808 process_deferred_messages_id = g_source_attach (source, atspi_main_context);
809 g_source_unref (source);
812 return DBUS_HANDLER_RESULT_HANDLED;
815 static DBusHandlerResult
816 atspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
818 int type = dbus_message_get_type (message);
819 const char *interface = dbus_message_get_interface (message);
821 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
822 !strncmp (interface, "org.a11y.atspi.Event.", 21))
824 return defer_message (bus, message, data);
826 if (dbus_message_is_method_call (message, atspi_interface_device_event_listener, "NotifyEvent"))
828 return defer_message (bus, message, data);
830 if (dbus_message_is_signal (message, atspi_interface_cache, "AddAccessible"))
832 return defer_message (bus, message, data);
834 if (dbus_message_is_signal (message, atspi_interface_cache, "RemoveAccessible"))
836 return defer_message (bus, message, data);
838 if (dbus_message_is_signal (message, "org.freedesktop.DBus", "NameOwnerChanged"))
840 defer_message (bus, message, data);
841 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
843 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
846 static const char *signal_interfaces[] =
848 "org.a11y.atspi.Event.Object",
849 "org.a11y.atspi.Event.Window",
850 "org.a11y.atspi.Event.Mouse",
851 "org.a11y.atspi.Event.Terminal",
852 "org.a11y.atspi.Event.Document",
853 "org.a11y.atspi.Event.Focus",
858 * Returns a 'canonicalized' value for DISPLAY,
859 * with the screen number stripped off if present.
861 * TODO: Avoid having duplicate functions for this here and in at-spi2-atk
864 spi_display_name (void)
866 char *canonical_display_name = NULL;
867 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
871 display_env = g_getenv ("DISPLAY");
872 if (!display_env || !display_env[0])
876 gchar *display_p, *screen_p;
877 canonical_display_name = g_strdup (display_env);
878 display_p = g_utf8_strrchr (canonical_display_name, -1, ':');
879 screen_p = g_utf8_strrchr (canonical_display_name, -1, '.');
880 if (screen_p && display_p && (screen_p > display_p))
888 canonical_display_name = g_strdup (display_env);
891 return canonical_display_name;
897 * Connects to the accessibility registry and initializes the SPI.
899 * Returns: 0 on success, 1 if already initialized, or an integer error code.
905 const gchar *no_cache;
916 _atspi_get_live_refs();
918 bus = atspi_get_a11y_bus ();
921 dbus_bus_register (bus, NULL);
922 atspi_dbus_connection_setup_with_g_main(bus, g_main_context_default());
923 dbus_connection_add_filter (bus, atspi_dbus_filter, NULL, NULL);
924 match = g_strdup_printf ("type='signal',interface='%s',member='AddAccessible'", atspi_interface_cache);
925 dbus_bus_add_match (bus, match, NULL);
927 match = g_strdup_printf ("type='signal',interface='%s',member='RemoveAccessible'", atspi_interface_cache);
928 dbus_bus_add_match (bus, match, NULL);
930 match = g_strdup_printf ("type='signal',interface='%s',member='ChildrenChanged'", atspi_interface_event_object);
931 dbus_bus_add_match (bus, match, NULL);
933 match = g_strdup_printf ("type='signal',interface='%s',member='PropertyChange'", atspi_interface_event_object);
934 dbus_bus_add_match (bus, match, NULL);
936 match = g_strdup_printf ("type='signal',interface='%s',member='StateChanged'", atspi_interface_event_object);
937 dbus_bus_add_match (bus, match, NULL);
940 dbus_bus_add_match (bus,
941 "type='signal', interface='org.freedesktop.DBus', member='NameOwnerChanged'",
944 no_cache = g_getenv ("ATSPI_NO_CACHE");
945 if (no_cache && g_strcmp0 (no_cache, "0") != 0)
946 atspi_no_cache = TRUE;
948 deferred_messages = g_queue_new ();
954 * atspi_is_initialized:
956 * Indicates whether AT-SPI has been initialized.
958 * Returns: %True if initialized; %False otherwise.
961 atspi_is_initialized ()
969 * Starts/enters the main event loop for the AT-SPI services.
971 * NOTE: This method does not return control; it is exited via a call to
972 * #atspi_event_quit from within an event handler.
976 atspi_event_main (void)
978 atspi_main_loop = g_main_loop_new (NULL, FALSE);
979 g_main_loop_run (atspi_main_loop);
980 atspi_main_loop = NULL;
986 * Quits the last main event loop for the AT-SPI services,
987 * See: #atspi_event_main
990 atspi_event_quit (void)
992 g_main_loop_quit (atspi_main_loop);
998 * Disconnects from #AtspiRegistry instances and releases
999 * any floating resources. Call only once at exit.
1001 * Returns: 0 if there were no leaks, otherwise other integer values.
1013 atspi_inited = FALSE;
1017 leaked = g_hash_table_size (live_refs);
1029 static GSList *hung_processes;
1032 remove_hung_process (DBusPendingCall *pending, void *data)
1035 hung_processes = g_slist_remove (hung_processes, data);
1037 dbus_pending_call_unref (pending);
1041 check_for_hang (DBusMessage *message, DBusError *error, DBusConnection *bus, const char *bus_name)
1043 if (!message && error->name &&
1044 !strcmp (error->name, "org.freedesktop.DBus.Error.NoReply"))
1047 DBusMessage *message;
1048 gchar *bus_name_dup;
1049 DBusPendingCall *pending = NULL;
1050 for (l = hung_processes; l; l = l->next)
1051 if (!strcmp (l->data, bus_name))
1053 message = dbus_message_new_method_call (bus_name, "/",
1054 "org.freedesktop.DBus.Peer",
1058 dbus_connection_send_with_reply (bus, message, &pending, -1);
1059 dbus_message_unref (message);
1062 bus_name_dup = g_strdup (bus_name);
1063 hung_processes = g_slist_append (hung_processes, bus_name_dup);
1064 dbus_pending_call_set_notify (pending, remove_hung_process, bus_name_dup, NULL);
1069 connection_is_hung (const char *bus_name)
1073 for (l = hung_processes; l; l = l->next)
1074 if (!strcmp (l->data, bus_name))
1080 check_app (AtspiApplication *app, GError **error)
1082 if (!app || !app->bus)
1084 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
1085 _("The application no longer exists"));
1089 if (atspi_main_loop && connection_is_hung (app->bus_name))
1091 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC,
1092 "The process appears to be hung.");
1100 set_timeout (AtspiApplication *app)
1105 if (app && app_startup_time > 0)
1107 gettimeofday (&tv, NULL);
1108 diff = (tv.tv_sec - app->time_added.tv_sec) * 1000 + (tv.tv_usec - app->time_added.tv_usec) / 1000;
1109 dbind_set_timeout (MAX(method_call_timeout, app_startup_time - diff));
1112 dbind_set_timeout (method_call_timeout);
1116 _atspi_dbus_call (gpointer obj, const char *interface, const char *method, GError **error, const char *type, ...)
1121 AtspiObject *aobj = ATSPI_OBJECT (obj);
1123 if (!check_app (aobj->app, error))
1126 va_start (args, type);
1127 dbus_error_init (&err);
1128 set_timeout (aobj->app);
1129 retval = dbind_method_call_reentrant_va (aobj->app->bus, aobj->app->bus_name,
1130 aobj->path, interface, method, &err,
1133 check_for_hang (NULL, &err, aobj->app->bus, aobj->app->bus_name);
1134 _atspi_process_deferred_messages ((gpointer)TRUE);
1135 if (dbus_error_is_set (&err))
1137 g_set_error(error, ATSPI_ERROR, ATSPI_ERROR_IPC, "%s", err.message);
1138 dbus_error_free (&err);
1144 _atspi_dbus_call_partial (gpointer obj,
1145 const char *interface,
1148 const char *type, ...)
1152 va_start (args, type);
1153 return _atspi_dbus_call_partial_va (obj, interface, method, error, type, args);
1158 _atspi_dbus_call_partial_va (gpointer obj,
1159 const char *interface,
1165 AtspiObject *aobj = ATSPI_OBJECT (obj);
1167 DBusMessage *msg = NULL, *reply = NULL;
1168 DBusMessageIter iter;
1171 dbus_error_init (&err);
1173 if (!check_app (aobj->app, error))
1176 msg = dbus_message_new_method_call (aobj->app->bus_name, aobj->path, interface, method);
1181 dbus_message_iter_init_append (msg, &iter);
1182 dbind_any_marshal_va (&iter, &p, args);
1184 set_timeout (aobj->app);
1185 reply = dbind_send_and_allow_reentry (aobj->app->bus, msg, &err);
1186 check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1190 dbus_message_unref (msg);
1191 _atspi_process_deferred_messages ((gpointer)TRUE);
1192 if (dbus_error_is_set (&err))
1194 /* TODO: Set gerror */
1195 dbus_error_free (&err);
1198 if (reply && dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
1200 const char *err_str = NULL;
1201 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &err_str, DBUS_TYPE_INVALID);
1203 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err_str);
1204 dbus_message_unref (reply);
1212 _atspi_dbus_get_property (gpointer obj, const char *interface, const char *name, GError **error, const char *type, void *data)
1214 DBusMessage *message, *reply;
1215 DBusMessageIter iter, iter_variant;
1217 dbus_bool_t retval = FALSE;
1218 AtspiObject *aobj = ATSPI_OBJECT (obj);
1219 char expected_type = (type [0] == '(' ? 'r' : type [0]);
1224 if (!check_app (aobj->app, error))
1227 message = dbus_message_new_method_call (aobj->app->bus_name,
1229 "org.freedesktop.DBus.Properties",
1233 // TODO: throw exception
1236 dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1237 dbus_error_init (&err);
1238 set_timeout (aobj->app);
1239 reply = dbind_send_and_allow_reentry (aobj->app->bus, message, &err);
1240 check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1241 dbus_message_unref (message);
1242 _atspi_process_deferred_messages ((gpointer)TRUE);
1245 // TODO: throw exception
1249 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
1251 const char *err_str = NULL;
1252 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &err_str, DBUS_TYPE_INVALID);
1254 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err_str);
1258 dbus_message_iter_init (reply, &iter);
1259 if (dbus_message_iter_get_arg_type (&iter) != 'v')
1261 g_warning ("AT-SPI: expected a variant when fetching %s from interface %s; got %s\n", name, interface, dbus_message_get_signature (reply));
1264 dbus_message_iter_recurse (&iter, &iter_variant);
1265 if (dbus_message_iter_get_arg_type (&iter_variant) != expected_type)
1267 g_warning ("atspi_dbus_get_property: Wrong type: expected %s, got %c\n", type, dbus_message_iter_get_arg_type (&iter_variant));
1270 if (!strcmp (type, "(so)"))
1272 *((AtspiAccessible **)data) = _atspi_dbus_return_accessible_from_iter (&iter_variant);
1276 dbus_message_iter_get_basic (&iter_variant, data);
1277 if (type [0] == 's')
1278 *(char **)data = g_strdup (*(char **)data);
1282 dbus_error_free (&err);
1284 dbus_message_unref (reply);
1289 _atspi_dbus_send_with_reply_and_block (DBusMessage *message, GError **error)
1293 AtspiApplication *app;
1294 DBusConnection *bus;
1296 app = get_application (dbus_message_get_destination (message));
1298 if (app && !app->bus)
1299 return NULL; /* will fail anyway; app has been disposed */
1301 bus = (app ? app->bus : _atspi_bus());
1302 dbus_error_init (&err);
1304 reply = dbind_send_and_allow_reentry (bus, message, &err);
1305 _atspi_process_deferred_messages ((gpointer)TRUE);
1306 dbus_message_unref (message);
1307 if (dbus_error_is_set (&err))
1310 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err.message);
1311 dbus_error_free (&err);
1317 _atspi_dbus_return_hash_from_message (DBusMessage *message)
1319 DBusMessageIter iter;
1325 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1327 dbus_message_iter_init (message, &iter);
1328 ret = _atspi_dbus_hash_from_iter (&iter);
1329 dbus_message_unref (message);
1334 _atspi_dbus_hash_from_iter (DBusMessageIter *iter)
1336 GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
1337 (GDestroyNotify) g_free,
1338 (GDestroyNotify) g_free);
1339 DBusMessageIter iter_array, iter_dict;
1341 dbus_message_iter_recurse (iter, &iter_array);
1342 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1344 const char *name, *value;
1345 dbus_message_iter_recurse (&iter_array, &iter_dict);
1346 dbus_message_iter_get_basic (&iter_dict, &name);
1347 dbus_message_iter_next (&iter_dict);
1348 dbus_message_iter_get_basic (&iter_dict, &value);
1349 g_hash_table_insert (hash, g_strdup (name), g_strdup (value));
1350 dbus_message_iter_next (&iter_array);
1356 _atspi_dbus_return_attribute_array_from_message (DBusMessage *message)
1358 DBusMessageIter iter;
1364 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1366 dbus_message_iter_init (message, &iter);
1368 ret = _atspi_dbus_attribute_array_from_iter (&iter);
1369 dbus_message_unref (message);
1374 _atspi_dbus_attribute_array_from_iter (DBusMessageIter *iter)
1376 DBusMessageIter iter_array, iter_dict;
1377 GArray *array = g_array_new (TRUE, TRUE, sizeof (gchar *));
1379 dbus_message_iter_recurse (iter, &iter_array);
1380 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1382 const char *name, *value;
1384 dbus_message_iter_recurse (&iter_array, &iter_dict);
1385 dbus_message_iter_get_basic (&iter_dict, &name);
1386 dbus_message_iter_next (&iter_dict);
1387 dbus_message_iter_get_basic (&iter_dict, &value);
1388 str = g_strdup_printf ("%s:%s", name, value);
1389 array = g_array_append_val (array, str);
1390 dbus_message_iter_next (&iter_array);;
1396 _atspi_dbus_set_interfaces (AtspiAccessible *accessible, DBusMessageIter *iter)
1398 DBusMessageIter iter_array;
1400 accessible->interfaces = 0;
1401 dbus_message_iter_recurse (iter, &iter_array);
1402 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1406 dbus_message_iter_get_basic (&iter_array, &iface);
1407 if (!strcmp (iface, "org.freedesktop.DBus.Introspectable")) continue;
1408 n = _atspi_get_iface_num (iface);
1411 g_warning ("AT-SPI: Unknown interface %s", iface);
1414 accessible->interfaces |= (1 << n);
1415 dbus_message_iter_next (&iter_array);
1417 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_INTERFACES);
1421 _atspi_dbus_set_state (AtspiAccessible *accessible, DBusMessageIter *iter)
1423 DBusMessageIter iter_array;
1425 dbus_uint32_t *states;
1427 dbus_message_iter_recurse (iter, &iter_array);
1428 dbus_message_iter_get_fixed_array (&iter_array, &states, &count);
1431 g_warning ("AT-SPI: expected 2 values in states array; got %d\n", count);
1432 if (!accessible->states)
1433 accessible->states = _atspi_state_set_new_internal (accessible, 0);
1437 guint64 val = ((guint64)states [1]) << 32;
1439 if (!accessible->states)
1440 accessible->states = _atspi_state_set_new_internal (accessible, val);
1442 accessible->states->states = val;
1444 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_STATES);
1448 _atspi_error_quark (void)
1450 return g_quark_from_static_string ("atspi_error");
1454 * Gets the IOR from the XDisplay.
1458 get_accessibility_bus_address_x11 (void)
1462 Display *bridge_display = NULL;
1465 unsigned char *data_x11 = NULL;
1466 unsigned long nitems;
1467 unsigned long leftover;
1470 display_name = spi_display_name ();
1474 bridge_display = XOpenDisplay (display_name);
1475 g_free (display_name);
1477 if (!bridge_display)
1479 g_warning ("Could not open X display");
1483 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
1484 XGetWindowProperty (bridge_display,
1485 XDefaultRootWindow (bridge_display),
1487 (long) BUFSIZ, False,
1488 (Atom) 31, &actual_type, &actual_format,
1489 &nitems, &leftover, &data_x11);
1490 XCloseDisplay (bridge_display);
1492 data = g_strdup ((gchar *)data_x11);
1499 get_accessibility_bus_address_dbus (void)
1501 DBusConnection *session_bus = NULL;
1502 DBusMessage *message;
1505 char *address = NULL;
1507 session_bus = dbus_bus_get (DBUS_BUS_SESSION, NULL);
1511 message = dbus_message_new_method_call ("org.a11y.Bus",
1516 dbus_error_init (&error);
1517 reply = dbus_connection_send_with_reply_and_block (session_bus,
1521 dbus_message_unref (message);
1525 g_warning ("Error retrieving accessibility bus address: %s: %s",
1526 error.name, error.message);
1527 dbus_error_free (&error);
1532 const char *tmp_address;
1533 if (!dbus_message_get_args (reply,
1539 dbus_message_unref (reply);
1542 address = g_strdup (tmp_address);
1543 dbus_message_unref (reply);
1549 static DBusConnection *a11y_bus;
1550 static dbus_int32_t a11y_dbus_slot = -1;
1553 a11y_bus_free (void *data)
1555 if (data == a11y_bus)
1558 dbus_connection_free_data_slot (&a11y_dbus_slot);
1563 atspi_get_a11y_bus (void)
1566 char *address = NULL;
1568 if (a11y_bus && dbus_connection_get_is_connected (a11y_bus))
1571 if (a11y_dbus_slot == -1)
1572 if (!dbus_connection_allocate_data_slot (&a11y_dbus_slot))
1573 g_warning ("at-spi: Unable to allocate D-Bus slot");
1576 address = get_accessibility_bus_address_x11 ();
1579 address = get_accessibility_bus_address_dbus ();
1583 dbus_error_init (&error);
1584 a11y_bus = dbus_connection_open_private (address, &error);
1589 g_warning ("Couldn't connect to accessibility bus: %s", error.message);
1590 dbus_error_free (&error);
1595 if (!dbus_bus_register (a11y_bus, &error))
1597 g_warning ("Couldn't register with accessibility bus: %s", error.message);
1598 dbus_error_free (&error);
1599 dbus_connection_close (a11y_bus);
1600 dbus_connection_unref (a11y_bus);
1606 /* Simulate a weak ref on the bus */
1607 dbus_connection_set_data (a11y_bus, a11y_dbus_slot, a11y_bus, a11y_bus_free);
1613 * Set the timeout used for method calls. If this is not set explicitly,
1614 * a default of 0.8 ms is used.
1615 * Note that at-spi2-registryd currently uses a timeout of 3 seconds when
1616 * sending a keyboard event notification. This means that, if an AT makes
1617 * a call in response to the keyboard notification and the application
1618 * being called does not respond before the timeout is reached,
1619 * at-spi2-registryd will time out on the keyboard event notification and
1620 * pass the key onto the application (ie, reply to indicate that the key
1621 * was not consumed), so this may make it undesirable to set a timeout
1622 * larger than 3 seconds.
1624 * @val: The timeout value, in milliseconds, or -1 to disable the timeout.
1625 * @startup_time: The amount of time, in milliseconds, to allow to pass
1626 * before enforcing timeouts on an application. Can be used to prevent
1627 * timeout exceptions if an application is likely to block for an extended
1628 * period of time on initialization. -1 can be passed to disable this
1631 * By default, the normal timeout is set to 800 ms, and the application startup
1632 * timeout is set to 15 seconds.
1635 atspi_set_timeout (gint val, gint startup_time)
1637 method_call_timeout = val;
1638 app_startup_time = startup_time;
1642 * atspi_set_main_context:
1643 * @cnx: The #GmainContext to use.
1645 * Sets the main loop context that AT-SPI should assume is in use when
1646 * setting an idle callback.
1647 * This function should be called by application-side implementors (ie,
1648 * at-spi2-atk) when it is desirable to re-enter the main loop.
1651 atspi_set_main_context (GMainContext *cnx)
1653 atspi_main_context = cnx;
1654 atspi_dbus_connection_setup_with_g_main (atspi_get_a11y_bus (), cnx);
1657 #ifdef DEBUG_REF_COUNTS
1659 print_disposed (gpointer key, gpointer value, gpointer data)
1661 AtspiAccessible *accessible = key;
1662 if (accessible->parent.app)
1664 g_print ("disposed: %s %d\n", accessible->name, accessible->role);
1670 g_hash_table_foreach (live_refs, print_disposed, NULL);
1675 _atspi_name_compat (gchar *name)
1689 * atspi_role_get_name:
1690 * @role: an #AtspiRole object to query.
1692 * Gets a localizable string that indicates the name of an #AtspiRole.
1693 * <em>DEPRECATED.</em>
1695 * Returns: a localizable string name for an #AtspiRole enumerated type.
1698 atspi_role_get_name (AtspiRole role)
1700 gchar *retval = NULL;
1701 GTypeClass *type_class;
1703 const gchar *name = NULL;
1705 type_class = g_type_class_ref (ATSPI_TYPE_ROLE);
1706 g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), NULL);
1708 value = g_enum_get_value (G_ENUM_CLASS (type_class), role);
1712 retval = g_strdup (value->value_nick);
1716 return _atspi_name_compat (retval);