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))
127 static AtspiAccessible *desktop;
139 g_hash_table_destroy (refs);
144 dbus_connection_close (bus);
145 dbus_connection_unref (bus);
151 for (l = desktop->children; l;)
153 GList *next = l->next;
154 AtspiAccessible *child = l->data;
155 g_object_run_dispose (G_OBJECT (child->parent.app));
156 g_object_run_dispose (G_OBJECT (child));
159 g_object_run_dispose (G_OBJECT (desktop->parent.app));
160 g_object_unref (desktop);
164 static gboolean atspi_inited = FALSE;
166 static GHashTable *app_hash = NULL;
169 handle_get_bus_address (DBusPendingCall *pending, void *user_data)
171 AtspiApplication *app = user_data;
172 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
173 DBusMessage *message;
175 DBusPendingCall *new_pending;
177 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
179 if (dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &address,
180 DBUS_TYPE_INVALID) && address [0])
185 dbus_error_init (&error);
186 bus = dbus_connection_open_private (address, &error);
191 dbus_connection_unref (app->bus);
197 g_warning ("Unable to open bus connection: %s", error.message);
198 dbus_error_free (&error);
202 dbus_message_unref (reply);
203 dbus_pending_call_unref (pending);
206 return; /* application has gone away / been disposed */
208 message = dbus_message_new_method_call (app->bus_name,
209 "/org/a11y/atspi/cache",
210 atspi_interface_cache, "GetItems");
212 dbus_connection_send_with_reply (app->bus, message, &new_pending, 2000);
213 dbus_pending_call_set_notify (new_pending, handle_get_items, app, NULL);
214 dbus_message_unref (message);
217 static AtspiApplication *
218 get_application (const char *bus_name)
220 AtspiApplication *app = NULL;
222 DBusMessage *message;
223 DBusPendingCall *pending = NULL;
227 app_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_object_unref);
228 if (!app_hash) return NULL;
230 app = g_hash_table_lookup (app_hash, bus_name);
232 bus_name_dup = g_strdup (bus_name);
233 if (!bus_name_dup) return NULL;
234 // TODO: change below to something that will send state-change:defunct notification if necessary */
235 app = _atspi_application_new (bus_name);
236 app->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
237 app->bus = dbus_connection_ref (_atspi_bus ());
238 gettimeofday (&app->time_added, NULL);
239 app->cache = ATSPI_CACHE_UNDEFINED;
240 g_hash_table_insert (app_hash, bus_name_dup, app);
241 message = dbus_message_new_method_call (bus_name, atspi_path_root,
242 atspi_interface_application, "GetApplicationBusAddress");
244 dbus_connection_send_with_reply (app->bus, message, &pending, 2000);
245 dbus_pending_call_set_notify (pending, handle_get_bus_address, app, NULL);
246 dbus_message_unref (message);
250 static AtspiAccessible *
251 ref_accessible (const char *app_name, const char *path)
253 AtspiApplication *app;
256 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
259 app = get_application (app_name);
261 if (!strcmp (path, "/org/a11y/atspi/accessible/root"))
265 app->root = _atspi_accessible_new (app, atspi_path_root);
266 app->root->accessible_parent = atspi_get_desktop (0);
267 app->root->accessible_parent->children = g_list_append (app->root->accessible_parent->children, g_object_ref (app->root));
269 return g_object_ref (app->root);
272 a = g_hash_table_lookup (app->hash, path);
275 return g_object_ref (a);
277 a = _atspi_accessible_new (app, path);
280 g_hash_table_insert (app->hash, g_strdup (a->parent.path), g_object_ref (a));
284 static AtspiHyperlink *
285 ref_hyperlink (const char *app_name, const char *path)
287 AtspiApplication *app = get_application (app_name);
288 AtspiHyperlink *hyperlink;
290 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
293 hyperlink = g_hash_table_lookup (app->hash, path);
296 return g_object_ref (hyperlink);
298 hyperlink = _atspi_hyperlink_new (app, path);
299 g_hash_table_insert (app->hash, g_strdup (hyperlink->parent.path), hyperlink);
300 /* TODO: This should be a weak ref */
301 g_object_ref (hyperlink); /* for the hash */
314 GArray *state_bitflags;
317 static DBusHandlerResult
318 handle_remove_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
320 const char *sender = dbus_message_get_sender (message);
321 AtspiApplication *app;
323 DBusMessageIter iter, iter_struct;
324 const char *signature = dbus_message_get_signature (message);
327 if (strcmp (signature, "(so)") != 0)
329 g_warning ("AT-SPI: Unknown signature %s for RemoveAccessible", signature);
330 return DBUS_HANDLER_RESULT_HANDLED;
333 dbus_message_iter_init (message, &iter);
334 dbus_message_iter_recurse (&iter, &iter_struct);
335 dbus_message_iter_get_basic (&iter_struct, &sender);
336 dbus_message_iter_next (&iter_struct);
337 dbus_message_iter_get_basic (&iter_struct, &path);
338 app = get_application (sender);
339 a = ref_accessible (sender, path);
341 return DBUS_HANDLER_RESULT_HANDLED;
342 g_object_run_dispose (G_OBJECT (a));
343 g_hash_table_remove (app->hash, a->parent.path);
344 g_object_unref (a); /* unref our own ref */
345 return DBUS_HANDLER_RESULT_HANDLED;
348 static DBusHandlerResult
349 handle_name_owner_changed (DBusConnection *bus, DBusMessage *message, void *user_data)
351 const char *name, *new, *old;
352 static gboolean registry_lost = FALSE;
354 if (!dbus_message_get_args (message, NULL,
355 DBUS_TYPE_STRING, &name,
356 DBUS_TYPE_STRING, &old,
357 DBUS_TYPE_STRING, &new,
363 if (!strcmp (name, "org.a11y.atspi.Registry"))
365 if (registry_lost && !old[0])
367 _atspi_reregister_event_listeners ();
368 _atspi_reregister_device_listeners ();
369 registry_lost = FALSE;
372 registry_lost = TRUE;
376 AtspiAccessible *desktop = atspi_get_desktop (0);
378 for (l = desktop->children; l; l = l->next)
380 AtspiAccessible *child = l->data;
381 if (!strcmp (child->parent.app->bus_name, old))
382 g_object_run_dispose (G_OBJECT (child->parent.app));
384 g_object_unref (desktop);
386 return DBUS_HANDLER_RESULT_HANDLED;
390 add_app_to_desktop (AtspiAccessible *a, const char *bus_name)
392 AtspiAccessible *obj = ref_accessible (bus_name, atspi_path_root);
393 /* The app will be added to the desktop as a side-effect of calling
395 g_object_unref (obj);
396 return (obj != NULL);
400 send_children_changed (AtspiAccessible *parent, AtspiAccessible *child, gboolean add)
404 memset (&e, 0, sizeof (e));
405 e.type = (add? "object:children-changed:add": "object:children-changed:remove");
407 e.detail1 = g_list_index (parent->children, child);
409 _atspi_send_event (&e);
413 unref_object_and_descendants (AtspiAccessible *obj)
417 for (l = obj->children; l; l = l->next)
419 unref_object_and_descendants (l->data);
421 g_object_unref (obj);
425 remove_app_from_desktop (AtspiAccessible *a, const char *bus_name)
428 AtspiAccessible *child;
430 for (l = a->children; l; l = l->next)
433 if (!strcmp (bus_name, child->parent.app->bus_name)) break;
439 send_children_changed (a, child, FALSE);
440 a->children = g_list_remove (a->children, child);
441 unref_object_and_descendants (child);
446 get_reference_from_iter (DBusMessageIter *iter, const char **app_name, const char **path)
448 DBusMessageIter iter_struct;
450 dbus_message_iter_recurse (iter, &iter_struct);
451 dbus_message_iter_get_basic (&iter_struct, app_name);
452 dbus_message_iter_next (&iter_struct);
453 dbus_message_iter_get_basic (&iter_struct, path);
454 dbus_message_iter_next (iter);
458 add_accessible_from_iter (DBusMessageIter *iter)
460 DBusMessageIter iter_struct, iter_array;
461 const char *app_name, *path;
462 AtspiAccessible *accessible;
463 const char *name, *description;
466 dbus_message_iter_recurse (iter, &iter_struct);
469 get_reference_from_iter (&iter_struct, &app_name, &path);
470 accessible = ref_accessible (app_name, path);
474 /* Get application: TODO */
475 dbus_message_iter_next (&iter_struct);
478 get_reference_from_iter (&iter_struct, &app_name, &path);
479 if (accessible->accessible_parent)
480 g_object_unref (accessible->accessible_parent);
481 accessible->accessible_parent = ref_accessible (app_name, path);
484 while (accessible->children)
486 g_object_unref (accessible->children->data);
487 accessible->children = g_list_remove (accessible->children, accessible->children->data);
489 dbus_message_iter_recurse (&iter_struct, &iter_array);
490 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
492 AtspiAccessible *child;
493 get_reference_from_iter (&iter_array, &app_name, &path);
494 child = ref_accessible (app_name, path);
495 accessible->children = g_list_append (accessible->children, child);
499 dbus_message_iter_next (&iter_struct);
500 _atspi_dbus_set_interfaces (accessible, &iter_struct);
501 dbus_message_iter_next (&iter_struct);
504 if (accessible->name)
505 g_free (accessible->name);
506 dbus_message_iter_get_basic (&iter_struct, &name);
507 accessible->name = g_strdup (name);
508 dbus_message_iter_next (&iter_struct);
511 dbus_message_iter_get_basic (&iter_struct, &role);
512 accessible->role = role;
513 dbus_message_iter_next (&iter_struct);
516 if (accessible->description)
517 g_free (accessible->description);
518 dbus_message_iter_get_basic (&iter_struct, &description);
519 accessible->description = g_strdup (description);
520 dbus_message_iter_next (&iter_struct);
522 _atspi_dbus_set_state (accessible, &iter_struct);
523 dbus_message_iter_next (&iter_struct);
525 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_NAME | ATSPI_CACHE_ROLE |
526 ATSPI_CACHE_PARENT | ATSPI_CACHE_DESCRIPTION);
527 if (!atspi_state_set_contains (accessible->states,
528 ATSPI_STATE_MANAGES_DESCENDANTS))
529 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_CHILDREN);
531 /* This is a bit of a hack since the cache holds a ref, so we don't need
532 * the one provided for us anymore */
533 g_object_unref (accessible);
537 handle_get_items (DBusPendingCall *pending, void *user_data)
539 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
540 DBusMessageIter iter, iter_array;
542 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
544 const char *sender = dbus_message_get_sender (reply);
545 const char *error = NULL;
546 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &error,
548 g_warning ("AT-SPI: Error in GetItems, sender=%s, error=%s", sender, error);
549 dbus_message_unref (reply);
550 dbus_pending_call_unref (pending);
554 dbus_message_iter_init (reply, &iter);
555 dbus_message_iter_recurse (&iter, &iter_array);
556 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
558 add_accessible_from_iter (&iter_array);
559 dbus_message_iter_next (&iter_array);
561 dbus_message_unref (reply);
562 dbus_pending_call_unref (pending);
565 /* TODO: Do we stil need this function? */
566 static AtspiAccessible *
567 ref_accessible_desktop (AtspiApplication *app)
570 DBusMessage *message, *reply;
571 DBusMessageIter iter, iter_array;
576 g_object_ref (desktop);
579 desktop = _atspi_accessible_new (app, atspi_path_root);
584 g_hash_table_insert (app->hash, g_strdup (desktop->parent.path),
585 g_object_ref (desktop));
586 app->root = g_object_ref (desktop);
587 desktop->name = g_strdup ("main");
588 message = dbus_message_new_method_call (atspi_bus_registry,
590 atspi_interface_accessible,
595 reply = _atspi_dbus_send_with_reply_and_block (message, &error);
596 if (!reply || strcmp (dbus_message_get_signature (reply), "a(so)") != 0)
600 g_warning ("Couldn't get application list: %s", error->message);
601 g_clear_error (&error);
604 dbus_message_unref (reply);
607 dbus_message_iter_init (reply, &iter);
608 dbus_message_iter_recurse (&iter, &iter_array);
609 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
611 const char *app_name, *path;
612 get_reference_from_iter (&iter_array, &app_name, &path);
613 add_app_to_desktop (desktop, app_name);
615 dbus_message_unref (reply);
617 /* Record the alternate name as an alias for org.a11y.atspi.Registry */
618 bus_name_dup = g_strdup (dbus_message_get_sender (reply));
620 g_hash_table_insert (app_hash, bus_name_dup, app);
622 return g_object_ref (desktop);
626 _atspi_ref_accessible (const char *app, const char *path)
628 AtspiApplication *a = get_application (app);
631 if ( APP_IS_REGISTRY(a))
634 g_object_unref (ref_accessible_desktop (a)); /* sets a->root */
635 return g_object_ref (a->root);
637 return ref_accessible (app, path);
641 _atspi_dbus_return_accessible_from_message (DBusMessage *message)
643 DBusMessageIter iter;
644 AtspiAccessible *retval = NULL;
645 const char *signature;
650 signature = dbus_message_get_signature (message);
651 if (!strcmp (signature, "(so)"))
653 dbus_message_iter_init (message, &iter);
654 retval = _atspi_dbus_return_accessible_from_iter (&iter);
658 g_warning ("AT-SPI: Called _atspi_dbus_return_accessible_from_message with strange signature %s", signature);
660 dbus_message_unref (message);
665 _atspi_dbus_return_accessible_from_iter (DBusMessageIter *iter)
667 const char *app_name, *path;
669 get_reference_from_iter (iter, &app_name, &path);
670 return ref_accessible (app_name, path);
674 _atspi_dbus_return_hyperlink_from_message (DBusMessage *message)
676 DBusMessageIter iter;
677 AtspiHyperlink *retval = NULL;
678 const char *signature;
683 signature = dbus_message_get_signature (message);
684 if (!strcmp (signature, "(so)"))
686 dbus_message_iter_init (message, &iter);
687 retval = _atspi_dbus_return_hyperlink_from_iter (&iter);
691 g_warning ("AT-SPI: Called _atspi_dbus_return_hyperlink_from_message with strange signature %s", signature);
693 dbus_message_unref (message);
698 _atspi_dbus_return_hyperlink_from_iter (DBusMessageIter *iter)
700 const char *app_name, *path;
702 get_reference_from_iter (iter, &app_name, &path);
703 return ref_hyperlink (app_name, path);
706 const char *cache_signal_type = "((so)(so)(so)a(so)assusau)";
708 static DBusHandlerResult
709 handle_add_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
711 DBusMessageIter iter;
712 const char *sender = dbus_message_get_sender (message);
714 if (strcmp (dbus_message_get_signature (message), cache_signal_type) != 0)
716 g_warning ("AT-SPI: AddAccessible with unknown signature %s\n",
717 dbus_message_get_signature (message));
718 return DBUS_HANDLER_RESULT_HANDLED;
721 dbus_message_iter_init (message, &iter);
722 add_accessible_from_iter (&iter);
723 return DBUS_HANDLER_RESULT_HANDLED;
729 DBusMessage *message;
733 static guint process_deferred_messages_id = -1;
736 process_deferred_message (BusDataClosure *closure)
738 int type = dbus_message_get_type (closure->message);
739 const char *interface = dbus_message_get_interface (closure->message);
741 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
742 !strncmp (interface, "org.a11y.atspi.Event.", 21))
744 _atspi_dbus_handle_event (closure->bus, closure->message, closure->data);
746 if (dbus_message_is_method_call (closure->message, atspi_interface_device_event_listener, "NotifyEvent"))
748 _atspi_dbus_handle_DeviceEvent (closure->bus,
749 closure->message, closure->data);
751 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "AddAccessible"))
753 handle_add_accessible (closure->bus, closure->message, closure->data);
755 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "RemoveAccessible"))
757 handle_remove_accessible (closure->bus, closure->message, closure->data);
759 if (dbus_message_is_signal (closure->message, "org.freedesktop.DBus", "NameOwnerChanged"))
761 handle_name_owner_changed (closure->bus, closure->message, closure->data);
765 static GQueue *deferred_messages = NULL;
768 _atspi_process_deferred_messages (gpointer data)
770 static int in_process_deferred_messages = 0;
771 BusDataClosure *closure;
773 if (in_process_deferred_messages)
775 in_process_deferred_messages = 1;
776 while (closure = g_queue_pop_head (deferred_messages))
778 process_deferred_message (closure);
779 dbus_message_unref (closure->message);
780 dbus_connection_unref (closure->bus);
783 /* If data is NULL, assume that we were called from GLib */
785 process_deferred_messages_id = -1;
786 in_process_deferred_messages = 0;
790 static DBusHandlerResult
791 defer_message (DBusConnection *connection, DBusMessage *message, void *user_data)
793 BusDataClosure *closure = g_new (BusDataClosure, 1);
795 closure->bus = dbus_connection_ref (bus);
796 closure->message = dbus_message_ref (message);
797 closure->data = user_data;
799 g_queue_push_tail (deferred_messages, closure);
801 if (process_deferred_messages_id == -1)
802 process_deferred_messages_id = g_idle_add (_atspi_process_deferred_messages, NULL);
803 return DBUS_HANDLER_RESULT_HANDLED;
806 static DBusHandlerResult
807 atspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
809 int type = dbus_message_get_type (message);
810 const char *interface = dbus_message_get_interface (message);
812 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
813 !strncmp (interface, "org.a11y.atspi.Event.", 21))
815 return defer_message (bus, message, data);
817 if (dbus_message_is_method_call (message, atspi_interface_device_event_listener, "NotifyEvent"))
819 return defer_message (bus, message, data);
821 if (dbus_message_is_signal (message, atspi_interface_cache, "AddAccessible"))
823 return defer_message (bus, message, data);
825 if (dbus_message_is_signal (message, atspi_interface_cache, "RemoveAccessible"))
827 return defer_message (bus, message, data);
829 if (dbus_message_is_signal (message, "org.freedesktop.DBus", "NameOwnerChanged"))
831 defer_message (bus, message, data);
832 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
834 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
837 static const char *signal_interfaces[] =
839 "org.a11y.atspi.Event.Object",
840 "org.a11y.atspi.Event.Window",
841 "org.a11y.atspi.Event.Mouse",
842 "org.a11y.atspi.Event.Terminal",
843 "org.a11y.atspi.Event.Document",
844 "org.a11y.atspi.Event.Focus",
849 * Returns a 'canonicalized' value for DISPLAY,
850 * with the screen number stripped off if present.
852 * TODO: Avoid having duplicate functions for this here and in at-spi2-atk
855 spi_display_name (void)
857 char *canonical_display_name = NULL;
858 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
862 display_env = g_getenv ("DISPLAY");
863 if (!display_env || !display_env[0])
864 canonical_display_name = g_strdup (":0");
867 gchar *display_p, *screen_p;
868 canonical_display_name = g_strdup (display_env);
869 display_p = g_utf8_strrchr (canonical_display_name, -1, ':');
870 screen_p = g_utf8_strrchr (canonical_display_name, -1, '.');
871 if (screen_p && display_p && (screen_p > display_p))
879 canonical_display_name = g_strdup (display_env);
882 return canonical_display_name;
888 * Connects to the accessibility registry and initializes the SPI.
890 * Returns: 0 on success, 1 if already initialized, or an integer error code.
896 const gchar *no_cache;
909 bus = atspi_get_a11y_bus ();
912 dbus_bus_register (bus, NULL);
913 atspi_dbus_connection_setup_with_g_main(bus, g_main_context_default());
914 dbus_connection_add_filter (bus, atspi_dbus_filter, NULL, NULL);
915 match = g_strdup_printf ("type='signal',interface='%s',member='AddAccessible'", atspi_interface_cache);
916 dbus_bus_add_match (bus, match, NULL);
918 match = g_strdup_printf ("type='signal',interface='%s',member='RemoveAccessible'", atspi_interface_cache);
919 dbus_bus_add_match (bus, match, NULL);
921 match = g_strdup_printf ("type='signal',interface='%s',member='ChildrenChanged'", atspi_interface_event_object);
922 dbus_bus_add_match (bus, match, NULL);
924 match = g_strdup_printf ("type='signal',interface='%s',member='PropertyChange'", atspi_interface_event_object);
925 dbus_bus_add_match (bus, match, NULL);
927 match = g_strdup_printf ("type='signal',interface='%s',member='StateChanged'", atspi_interface_event_object);
928 dbus_bus_add_match (bus, match, NULL);
931 dbus_bus_add_match (bus,
932 "type='signal', interface='org.freedesktop.DBus', member='NameOwnerChanged'",
935 no_cache = g_getenv ("ATSPI_NO_CACHE");
936 if (no_cache && g_strcmp0 (no_cache, "0") != 0)
937 atspi_no_cache = TRUE;
939 deferred_messages = g_queue_new ();
947 * Starts/enters the main event loop for the AT-SPI services.
949 * NOTE: This method does not return control; it is exited via a call to
950 * #atspi_event_quit from within an event handler.
954 atspi_event_main (void)
956 atspi_main_loop = g_main_loop_new (NULL, FALSE);
957 g_main_loop_run (atspi_main_loop);
958 atspi_main_loop = NULL;
964 * Quits the last main event loop for the AT-SPI services,
965 * See: #atspi_event_main
968 atspi_event_quit (void)
970 g_main_loop_quit (atspi_main_loop);
976 * Disconnects from #AtspiRegistry instances and releases
977 * any floating resources. Call only once at exit.
979 * Returns: 0 if there were no leaks, otherwise other integer values.
991 atspi_inited = FALSE;
995 leaked = g_hash_table_size (live_refs);
1007 static GSList *hung_processes;
1010 remove_hung_process (DBusPendingCall *pending, void *data)
1012 gchar *bus_name = data;
1014 hung_processes = g_slist_remove (hung_processes, data);
1016 dbus_pending_call_unref (pending);
1020 check_for_hang (DBusMessage *message, DBusError *error, DBusConnection *bus, const char *bus_name)
1022 if (!message && error->name &&
1023 !strcmp (error->name, "org.freedesktop.DBus.Error.NoReply"))
1026 DBusMessage *message;
1027 gchar *bus_name_dup;
1028 DBusPendingCall *pending = NULL;
1029 for (l = hung_processes; l; l = l->next)
1030 if (!strcmp (l->data, bus_name))
1032 message = dbus_message_new_method_call (bus_name, "/",
1033 "org.freedesktop.DBus.Peer",
1037 dbus_connection_send_with_reply (bus, message, &pending, -1);
1038 dbus_message_unref (message);
1041 bus_name_dup = g_strdup (bus_name);
1042 hung_processes = g_slist_append (hung_processes, bus_name_dup);
1043 dbus_pending_call_set_notify (pending, remove_hung_process, bus_name_dup, NULL);
1048 connection_is_hung (const char *bus_name)
1052 for (l = hung_processes; l; l = l->next)
1053 if (!strcmp (l->data, bus_name))
1059 check_app (AtspiApplication *app, GError **error)
1061 if (!app || !app->bus)
1063 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
1064 _("The application no longer exists"));
1068 if (atspi_main_loop && connection_is_hung (app->bus_name))
1070 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC,
1071 "The process appears to be hung.");
1079 set_timeout (AtspiApplication *app)
1084 if (app && app_startup_time > 0)
1086 gettimeofday (&tv, NULL);
1087 diff = (tv.tv_sec - app->time_added.tv_sec) * 1000 + (tv.tv_usec - app->time_added.tv_usec) / 1000;
1088 dbind_set_timeout (MAX(method_call_timeout, app_startup_time - diff));
1091 dbind_set_timeout (method_call_timeout);
1095 _atspi_dbus_call (gpointer obj, const char *interface, const char *method, GError **error, const char *type, ...)
1100 AtspiObject *aobj = ATSPI_OBJECT (obj);
1102 if (!check_app (aobj->app, error))
1105 va_start (args, type);
1106 dbus_error_init (&err);
1107 set_timeout (aobj->app);
1108 retval = dbind_method_call_reentrant_va (aobj->app->bus, aobj->app->bus_name,
1109 aobj->path, interface, method, &err,
1112 check_for_hang (NULL, &err, aobj->app->bus, aobj->app->bus_name);
1113 _atspi_process_deferred_messages ((gpointer)TRUE);
1114 if (dbus_error_is_set (&err))
1116 g_set_error(error, ATSPI_ERROR, ATSPI_ERROR_IPC, "%s", err.message);
1117 dbus_error_free (&err);
1123 _atspi_dbus_call_partial (gpointer obj,
1124 const char *interface,
1127 const char *type, ...)
1131 va_start (args, type);
1132 return _atspi_dbus_call_partial_va (obj, interface, method, error, type, args);
1137 _atspi_dbus_call_partial_va (gpointer obj,
1138 const char *interface,
1144 AtspiObject *aobj = ATSPI_OBJECT (obj);
1146 DBusMessage *msg = NULL, *reply = NULL;
1147 DBusMessageIter iter;
1150 dbus_error_init (&err);
1152 if (!check_app (aobj->app, error))
1155 msg = dbus_message_new_method_call (aobj->app->bus_name, aobj->path, interface, method);
1160 dbus_message_iter_init_append (msg, &iter);
1161 dbind_any_marshal_va (&iter, &p, args);
1163 set_timeout (aobj->app);
1164 reply = dbind_send_and_allow_reentry (aobj->app->bus, msg, &err);
1165 check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1169 dbus_message_unref (msg);
1170 _atspi_process_deferred_messages ((gpointer)TRUE);
1171 if (dbus_error_is_set (&err))
1173 /* TODO: Set gerror */
1174 dbus_error_free (&err);
1177 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
1179 const char *err_str = NULL;
1180 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &err_str, DBUS_TYPE_INVALID);
1182 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err_str);
1183 dbus_message_unref (reply);
1191 _atspi_dbus_get_property (gpointer obj, const char *interface, const char *name, GError **error, const char *type, void *data)
1193 DBusMessage *message, *reply;
1194 DBusMessageIter iter, iter_variant;
1196 dbus_bool_t retval = FALSE;
1197 AtspiObject *aobj = ATSPI_OBJECT (obj);
1198 char expected_type = (type [0] == '(' ? 'r' : type [0]);
1203 if (!check_app (aobj->app, error))
1206 message = dbus_message_new_method_call (aobj->app->bus_name,
1208 "org.freedesktop.DBus.Properties",
1212 // TODO: throw exception
1215 dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1216 dbus_error_init (&err);
1217 set_timeout (aobj->app);
1218 reply = dbind_send_and_allow_reentry (aobj->app->bus, message, &err);
1219 check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1220 dbus_message_unref (message);
1221 _atspi_process_deferred_messages ((gpointer)TRUE);
1224 // TODO: throw exception
1228 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
1230 const char *err_str = NULL;
1231 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &err_str, DBUS_TYPE_INVALID);
1233 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err_str);
1237 dbus_message_iter_init (reply, &iter);
1238 if (dbus_message_iter_get_arg_type (&iter) != 'v')
1240 g_warning ("AT-SPI: expected a variant when fetching %s from interface %s; got %s\n", name, interface, dbus_message_get_signature (reply));
1243 dbus_message_iter_recurse (&iter, &iter_variant);
1244 if (dbus_message_iter_get_arg_type (&iter_variant) != expected_type)
1246 g_warning ("atspi_dbus_get_property: Wrong type: expected %s, got %c\n", type, dbus_message_iter_get_arg_type (&iter_variant));
1249 if (!strcmp (type, "(so)"))
1251 *((AtspiAccessible **)data) = _atspi_dbus_return_accessible_from_iter (&iter_variant);
1255 dbus_message_iter_get_basic (&iter_variant, data);
1256 if (type [0] == 's')
1257 *(char **)data = g_strdup (*(char **)data);
1261 dbus_error_free (&err);
1263 dbus_message_unref (reply);
1268 _atspi_dbus_send_with_reply_and_block (DBusMessage *message, GError **error)
1272 AtspiApplication *app;
1273 DBusConnection *bus;
1275 app = get_application (dbus_message_get_destination (message));
1277 if (app && !app->bus)
1278 return NULL; /* will fail anyway; app has been disposed */
1280 bus = (app ? app->bus : _atspi_bus());
1281 dbus_error_init (&err);
1283 reply = dbind_send_and_allow_reentry (bus, message, &err);
1284 _atspi_process_deferred_messages ((gpointer)TRUE);
1285 dbus_message_unref (message);
1286 if (dbus_error_is_set (&err))
1289 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err.message);
1290 dbus_error_free (&err);
1296 _atspi_dbus_return_hash_from_message (DBusMessage *message)
1298 DBusMessageIter iter;
1304 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1306 dbus_message_iter_init (message, &iter);
1307 ret = _atspi_dbus_hash_from_iter (&iter);
1308 dbus_message_unref (message);
1313 _atspi_dbus_hash_from_iter (DBusMessageIter *iter)
1315 GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
1316 (GDestroyNotify) g_free,
1317 (GDestroyNotify) g_free);
1318 DBusMessageIter iter_array, iter_dict;
1320 dbus_message_iter_recurse (iter, &iter_array);
1321 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1323 const char *name, *value;
1324 dbus_message_iter_recurse (&iter_array, &iter_dict);
1325 dbus_message_iter_get_basic (&iter_dict, &name);
1326 dbus_message_iter_next (&iter_dict);
1327 dbus_message_iter_get_basic (&iter_dict, &value);
1328 g_hash_table_insert (hash, g_strdup (name), g_strdup (value));
1329 dbus_message_iter_next (&iter_array);
1335 _atspi_dbus_return_attribute_array_from_message (DBusMessage *message)
1337 DBusMessageIter iter;
1343 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1345 dbus_message_iter_init (message, &iter);
1347 ret = _atspi_dbus_attribute_array_from_iter (&iter);
1348 dbus_message_unref (message);
1353 _atspi_dbus_attribute_array_from_iter (DBusMessageIter *iter)
1355 DBusMessageIter iter_array, iter_dict;
1356 GArray *array = g_array_new (TRUE, TRUE, sizeof (gchar *));
1358 dbus_message_iter_recurse (iter, &iter_array);
1359 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1361 const char *name, *value;
1363 dbus_message_iter_recurse (&iter_array, &iter_dict);
1364 dbus_message_iter_get_basic (&iter_dict, &name);
1365 dbus_message_iter_next (&iter_dict);
1366 dbus_message_iter_get_basic (&iter_dict, &value);
1367 str = g_strdup_printf ("%s:%s", name, value);
1368 array = g_array_append_val (array, str);
1369 dbus_message_iter_next (&iter_array);;
1375 _atspi_dbus_set_interfaces (AtspiAccessible *accessible, DBusMessageIter *iter)
1377 DBusMessageIter iter_array;
1379 accessible->interfaces = 0;
1380 dbus_message_iter_recurse (iter, &iter_array);
1381 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1385 dbus_message_iter_get_basic (&iter_array, &iface);
1386 if (!strcmp (iface, "org.freedesktop.DBus.Introspectable")) continue;
1387 n = _atspi_get_iface_num (iface);
1390 g_warning ("AT-SPI: Unknown interface %s", iface);
1393 accessible->interfaces |= (1 << n);
1394 dbus_message_iter_next (&iter_array);
1396 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_INTERFACES);
1400 _atspi_dbus_set_state (AtspiAccessible *accessible, DBusMessageIter *iter)
1402 DBusMessageIter iter_array;
1404 dbus_uint32_t *states;
1406 dbus_message_iter_recurse (iter, &iter_array);
1407 dbus_message_iter_get_fixed_array (&iter_array, &states, &count);
1410 g_warning ("AT-SPI: expected 2 values in states array; got %d\n", count);
1411 if (!accessible->states)
1412 accessible->states = _atspi_state_set_new_internal (accessible, 0);
1416 guint64 val = ((guint64)states [1]) << 32;
1418 if (!accessible->states)
1419 accessible->states = _atspi_state_set_new_internal (accessible, val);
1421 accessible->states->states = val;
1423 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_STATES);
1427 _atspi_error_quark (void)
1429 return g_quark_from_static_string ("atspi_error");
1433 * Gets the IOR from the XDisplay.
1436 get_accessibility_bus_address_x11 (void)
1440 Display *bridge_display = NULL;
1443 unsigned char *data_x11 = NULL;
1444 unsigned long nitems;
1445 unsigned long leftover;
1448 display_name = spi_display_name ();
1449 if (display_name != NULL)
1451 bridge_display = XOpenDisplay (display_name);
1452 g_free (display_name);
1454 if (!bridge_display)
1456 g_warning ("Could not open X display");
1460 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
1461 XGetWindowProperty (bridge_display,
1462 XDefaultRootWindow (bridge_display),
1464 (long) BUFSIZ, False,
1465 (Atom) 31, &actual_type, &actual_format,
1466 &nitems, &leftover, &data_x11);
1467 XCloseDisplay (bridge_display);
1469 data = g_strdup (data_x11);
1475 get_accessibility_bus_address_dbus (void)
1477 DBusConnection *session_bus = NULL;
1478 DBusMessage *message;
1481 char *address = NULL;
1483 session_bus = dbus_bus_get (DBUS_BUS_SESSION, NULL);
1487 message = dbus_message_new_method_call ("org.a11y.Bus",
1492 dbus_error_init (&error);
1493 reply = dbus_connection_send_with_reply_and_block (session_bus,
1497 dbus_message_unref (message);
1501 g_warning ("Error retrieving accessibility bus address: %s: %s",
1502 error.name, error.message);
1503 dbus_error_free (&error);
1508 const char *tmp_address;
1509 if (!dbus_message_get_args (reply,
1515 dbus_message_unref (reply);
1518 address = g_strdup (tmp_address);
1519 dbus_message_unref (reply);
1525 static DBusConnection *a11y_bus;
1526 static dbus_int32_t a11y_dbus_slot = -1;
1529 a11y_bus_free (void *data)
1531 if (data == a11y_bus)
1534 dbus_connection_free_data_slot (&a11y_dbus_slot);
1539 atspi_get_a11y_bus (void)
1541 DBusConnection *bus = NULL;
1545 if (a11y_bus && dbus_connection_get_is_connected (a11y_bus))
1548 if (a11y_dbus_slot == -1)
1549 if (!dbus_connection_allocate_data_slot (&a11y_dbus_slot))
1550 g_warning ("at-spi: Unable to allocate D-Bus slot");
1552 address = get_accessibility_bus_address_x11 ();
1554 address = get_accessibility_bus_address_dbus ();
1558 dbus_error_init (&error);
1559 a11y_bus = dbus_connection_open_private (address, &error);
1564 g_warning ("Couldn't connect to accessibility bus: %s", error.message);
1565 dbus_error_free (&error);
1570 if (!dbus_bus_register (a11y_bus, &error))
1572 g_warning ("Couldn't register with accessibility bus: %s", error.message);
1573 dbus_error_free (&error);
1574 dbus_connection_close (a11y_bus);
1575 dbus_connection_unref (a11y_bus);
1581 /* Simulate a weak ref on the bus */
1582 dbus_connection_set_data (a11y_bus, a11y_dbus_slot, a11y_bus, a11y_bus_free);
1588 * Set the timeout used for method calls. If this is not set explicitly,
1589 * a default of 0.8 ms is used.
1590 * Note that at-spi2-registryd currently uses a timeout of 3 seconds when
1591 * sending a keyboard event notification. This means that, if an AT makes
1592 * a call in response to the keyboard notification and the application
1593 * being called does not respond before the timeout is reached,
1594 * at-spi2-registryd will time out on the keyboard event notification and
1595 * pass the key onto the application (ie, reply to indicate that the key
1596 * was not consumed), so this may make it undesirable to set a timeout
1597 * larger than 3 seconds.
1599 * @val: The timeout value, in milliseconds, or -1 to disable the timeout.
1600 * @startup_time: The amount of time, in milliseconds, to allow to pass
1601 * before enforcing timeouts on an application. Can be used to prevent
1602 * timeout exceptions if an application is likely to block for an extended
1603 * period of time on initialization. -1 can be passed to disable this
1606 * By default, the normal timeout is set to 800 ms, and the application startup
1607 * timeout is set to 15 seconds.
1610 atspi_set_timeout (gint val, gint startup_time)
1612 method_call_timeout = val;
1613 app_startup_time = startup_time;