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 GSource *process_deferred_messages_source = NULL;
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 */
789 g_source_unref (process_deferred_messages_source);
790 process_deferred_messages_source = NULL;
792 in_process_deferred_messages = 0;
796 static DBusHandlerResult
797 defer_message (DBusConnection *connection, DBusMessage *message, void *user_data)
799 BusDataClosure *closure = g_new (BusDataClosure, 1);
801 closure->bus = dbus_connection_ref (bus);
802 closure->message = dbus_message_ref (message);
803 closure->data = user_data;
805 g_queue_push_tail (deferred_messages, closure);
807 if (process_deferred_messages_source == NULL)
809 process_deferred_messages_source = g_idle_source_new ();
810 g_source_set_callback (process_deferred_messages_source,
811 _atspi_process_deferred_messages, NULL, NULL);
812 g_source_attach (process_deferred_messages_source, atspi_main_context);
815 return DBUS_HANDLER_RESULT_HANDLED;
818 static DBusHandlerResult
819 atspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
821 int type = dbus_message_get_type (message);
822 const char *interface = dbus_message_get_interface (message);
824 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
825 !strncmp (interface, "org.a11y.atspi.Event.", 21))
827 return defer_message (bus, message, data);
829 if (dbus_message_is_method_call (message, atspi_interface_device_event_listener, "NotifyEvent"))
831 return defer_message (bus, message, data);
833 if (dbus_message_is_signal (message, atspi_interface_cache, "AddAccessible"))
835 return defer_message (bus, message, data);
837 if (dbus_message_is_signal (message, atspi_interface_cache, "RemoveAccessible"))
839 return defer_message (bus, message, data);
841 if (dbus_message_is_signal (message, "org.freedesktop.DBus", "NameOwnerChanged"))
843 defer_message (bus, message, data);
844 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
846 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
849 static const char *signal_interfaces[] =
851 "org.a11y.atspi.Event.Object",
852 "org.a11y.atspi.Event.Window",
853 "org.a11y.atspi.Event.Mouse",
854 "org.a11y.atspi.Event.Terminal",
855 "org.a11y.atspi.Event.Document",
856 "org.a11y.atspi.Event.Focus",
861 * Returns a 'canonicalized' value for DISPLAY,
862 * with the screen number stripped off if present.
864 * TODO: Avoid having duplicate functions for this here and in at-spi2-atk
867 spi_display_name (void)
869 char *canonical_display_name = NULL;
870 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
874 display_env = g_getenv ("DISPLAY");
875 if (!display_env || !display_env[0])
879 gchar *display_p, *screen_p;
880 canonical_display_name = g_strdup (display_env);
881 display_p = g_utf8_strrchr (canonical_display_name, -1, ':');
882 screen_p = g_utf8_strrchr (canonical_display_name, -1, '.');
883 if (screen_p && display_p && (screen_p > display_p))
891 canonical_display_name = g_strdup (display_env);
894 return canonical_display_name;
900 * Connects to the accessibility registry and initializes the SPI.
902 * Returns: 0 on success, 1 if already initialized, or an integer error code.
908 const gchar *no_cache;
919 _atspi_get_live_refs();
921 bus = atspi_get_a11y_bus ();
924 dbus_bus_register (bus, NULL);
925 atspi_dbus_connection_setup_with_g_main(bus, g_main_context_default());
926 dbus_connection_add_filter (bus, atspi_dbus_filter, NULL, NULL);
927 match = g_strdup_printf ("type='signal',interface='%s',member='AddAccessible'", atspi_interface_cache);
928 dbus_bus_add_match (bus, match, NULL);
930 match = g_strdup_printf ("type='signal',interface='%s',member='RemoveAccessible'", atspi_interface_cache);
931 dbus_bus_add_match (bus, match, NULL);
933 match = g_strdup_printf ("type='signal',interface='%s',member='ChildrenChanged'", atspi_interface_event_object);
934 dbus_bus_add_match (bus, match, NULL);
936 match = g_strdup_printf ("type='signal',interface='%s',member='PropertyChange'", atspi_interface_event_object);
937 dbus_bus_add_match (bus, match, NULL);
939 match = g_strdup_printf ("type='signal',interface='%s',member='StateChanged'", atspi_interface_event_object);
940 dbus_bus_add_match (bus, match, NULL);
943 dbus_bus_add_match (bus,
944 "type='signal', interface='org.freedesktop.DBus', member='NameOwnerChanged'",
947 no_cache = g_getenv ("ATSPI_NO_CACHE");
948 if (no_cache && g_strcmp0 (no_cache, "0") != 0)
949 atspi_no_cache = TRUE;
951 deferred_messages = g_queue_new ();
957 * atspi_is_initialized:
959 * Indicates whether AT-SPI has been initialized.
961 * Returns: %True if initialized; %False otherwise.
964 atspi_is_initialized ()
972 * Starts/enters the main event loop for the AT-SPI services.
974 * NOTE: This method does not return control; it is exited via a call to
975 * #atspi_event_quit from within an event handler.
979 atspi_event_main (void)
981 atspi_main_loop = g_main_loop_new (NULL, FALSE);
982 g_main_loop_run (atspi_main_loop);
983 atspi_main_loop = NULL;
989 * Quits the last main event loop for the AT-SPI services,
990 * See: #atspi_event_main
993 atspi_event_quit (void)
995 g_main_loop_quit (atspi_main_loop);
1001 * Disconnects from #AtspiRegistry instances and releases
1002 * any floating resources. Call only once at exit.
1004 * Returns: 0 if there were no leaks, otherwise other integer values.
1016 atspi_inited = FALSE;
1020 leaked = g_hash_table_size (live_refs);
1032 static GSList *hung_processes;
1035 remove_hung_process (DBusPendingCall *pending, void *data)
1038 hung_processes = g_slist_remove (hung_processes, data);
1040 dbus_pending_call_unref (pending);
1044 check_for_hang (DBusMessage *message, DBusError *error, DBusConnection *bus, const char *bus_name)
1046 if (!message && error->name &&
1047 !strcmp (error->name, "org.freedesktop.DBus.Error.NoReply"))
1050 DBusMessage *message;
1051 gchar *bus_name_dup;
1052 DBusPendingCall *pending = NULL;
1053 for (l = hung_processes; l; l = l->next)
1054 if (!strcmp (l->data, bus_name))
1056 message = dbus_message_new_method_call (bus_name, "/",
1057 "org.freedesktop.DBus.Peer",
1061 dbus_connection_send_with_reply (bus, message, &pending, -1);
1062 dbus_message_unref (message);
1065 bus_name_dup = g_strdup (bus_name);
1066 hung_processes = g_slist_append (hung_processes, bus_name_dup);
1067 dbus_pending_call_set_notify (pending, remove_hung_process, bus_name_dup, NULL);
1072 connection_is_hung (const char *bus_name)
1076 for (l = hung_processes; l; l = l->next)
1077 if (!strcmp (l->data, bus_name))
1083 check_app (AtspiApplication *app, GError **error)
1085 if (!app || !app->bus)
1087 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
1088 _("The application no longer exists"));
1092 if (atspi_main_loop && connection_is_hung (app->bus_name))
1094 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC,
1095 "The process appears to be hung.");
1103 set_timeout (AtspiApplication *app)
1108 if (app && app_startup_time > 0)
1110 gettimeofday (&tv, NULL);
1111 diff = (tv.tv_sec - app->time_added.tv_sec) * 1000 + (tv.tv_usec - app->time_added.tv_usec) / 1000;
1112 dbind_set_timeout (MAX(method_call_timeout, app_startup_time - diff));
1115 dbind_set_timeout (method_call_timeout);
1119 _atspi_dbus_call (gpointer obj, const char *interface, const char *method, GError **error, const char *type, ...)
1124 AtspiObject *aobj = ATSPI_OBJECT (obj);
1126 if (!check_app (aobj->app, error))
1129 va_start (args, type);
1130 dbus_error_init (&err);
1131 set_timeout (aobj->app);
1132 retval = dbind_method_call_reentrant_va (aobj->app->bus, aobj->app->bus_name,
1133 aobj->path, interface, method, &err,
1136 check_for_hang (NULL, &err, aobj->app->bus, aobj->app->bus_name);
1137 _atspi_process_deferred_messages ((gpointer)TRUE);
1138 if (dbus_error_is_set (&err))
1140 g_set_error(error, ATSPI_ERROR, ATSPI_ERROR_IPC, "%s", err.message);
1141 dbus_error_free (&err);
1147 _atspi_dbus_call_partial (gpointer obj,
1148 const char *interface,
1151 const char *type, ...)
1155 va_start (args, type);
1156 return _atspi_dbus_call_partial_va (obj, interface, method, error, type, args);
1161 _atspi_dbus_call_partial_va (gpointer obj,
1162 const char *interface,
1168 AtspiObject *aobj = ATSPI_OBJECT (obj);
1170 DBusMessage *msg = NULL, *reply = NULL;
1171 DBusMessageIter iter;
1174 dbus_error_init (&err);
1176 if (!check_app (aobj->app, error))
1179 msg = dbus_message_new_method_call (aobj->app->bus_name, aobj->path, interface, method);
1184 dbus_message_iter_init_append (msg, &iter);
1185 dbind_any_marshal_va (&iter, &p, args);
1187 set_timeout (aobj->app);
1188 reply = dbind_send_and_allow_reentry (aobj->app->bus, msg, &err);
1189 check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1193 dbus_message_unref (msg);
1194 _atspi_process_deferred_messages ((gpointer)TRUE);
1195 if (dbus_error_is_set (&err))
1197 /* TODO: Set gerror */
1198 dbus_error_free (&err);
1201 if (reply && dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
1203 const char *err_str = NULL;
1204 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &err_str, DBUS_TYPE_INVALID);
1206 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err_str);
1207 dbus_message_unref (reply);
1215 _atspi_dbus_get_property (gpointer obj, const char *interface, const char *name, GError **error, const char *type, void *data)
1217 DBusMessage *message, *reply;
1218 DBusMessageIter iter, iter_variant;
1220 dbus_bool_t retval = FALSE;
1221 AtspiObject *aobj = ATSPI_OBJECT (obj);
1222 char expected_type = (type [0] == '(' ? 'r' : type [0]);
1227 if (!check_app (aobj->app, error))
1230 message = dbus_message_new_method_call (aobj->app->bus_name,
1232 "org.freedesktop.DBus.Properties",
1236 // TODO: throw exception
1239 dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1240 dbus_error_init (&err);
1241 set_timeout (aobj->app);
1242 reply = dbind_send_and_allow_reentry (aobj->app->bus, message, &err);
1243 check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1244 dbus_message_unref (message);
1245 _atspi_process_deferred_messages ((gpointer)TRUE);
1248 // TODO: throw exception
1252 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
1254 const char *err_str = NULL;
1255 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &err_str, DBUS_TYPE_INVALID);
1257 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err_str);
1261 dbus_message_iter_init (reply, &iter);
1262 if (dbus_message_iter_get_arg_type (&iter) != 'v')
1264 g_warning ("AT-SPI: expected a variant when fetching %s from interface %s; got %s\n", name, interface, dbus_message_get_signature (reply));
1267 dbus_message_iter_recurse (&iter, &iter_variant);
1268 if (dbus_message_iter_get_arg_type (&iter_variant) != expected_type)
1270 g_warning ("atspi_dbus_get_property: Wrong type: expected %s, got %c\n", type, dbus_message_iter_get_arg_type (&iter_variant));
1273 if (!strcmp (type, "(so)"))
1275 *((AtspiAccessible **)data) = _atspi_dbus_return_accessible_from_iter (&iter_variant);
1279 dbus_message_iter_get_basic (&iter_variant, data);
1280 if (type [0] == 's')
1281 *(char **)data = g_strdup (*(char **)data);
1285 dbus_error_free (&err);
1287 dbus_message_unref (reply);
1292 _atspi_dbus_send_with_reply_and_block (DBusMessage *message, GError **error)
1296 AtspiApplication *app;
1297 DBusConnection *bus;
1299 app = get_application (dbus_message_get_destination (message));
1301 if (app && !app->bus)
1302 return NULL; /* will fail anyway; app has been disposed */
1304 bus = (app ? app->bus : _atspi_bus());
1305 dbus_error_init (&err);
1307 reply = dbind_send_and_allow_reentry (bus, message, &err);
1308 _atspi_process_deferred_messages ((gpointer)TRUE);
1309 dbus_message_unref (message);
1310 if (dbus_error_is_set (&err))
1313 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err.message);
1314 dbus_error_free (&err);
1320 _atspi_dbus_return_hash_from_message (DBusMessage *message)
1322 DBusMessageIter iter;
1328 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1330 dbus_message_iter_init (message, &iter);
1331 ret = _atspi_dbus_hash_from_iter (&iter);
1332 dbus_message_unref (message);
1337 _atspi_dbus_hash_from_iter (DBusMessageIter *iter)
1339 GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
1340 (GDestroyNotify) g_free,
1341 (GDestroyNotify) g_free);
1342 DBusMessageIter iter_array, iter_dict;
1344 dbus_message_iter_recurse (iter, &iter_array);
1345 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1347 const char *name, *value;
1348 dbus_message_iter_recurse (&iter_array, &iter_dict);
1349 dbus_message_iter_get_basic (&iter_dict, &name);
1350 dbus_message_iter_next (&iter_dict);
1351 dbus_message_iter_get_basic (&iter_dict, &value);
1352 g_hash_table_insert (hash, g_strdup (name), g_strdup (value));
1353 dbus_message_iter_next (&iter_array);
1359 _atspi_dbus_return_attribute_array_from_message (DBusMessage *message)
1361 DBusMessageIter iter;
1367 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1369 dbus_message_iter_init (message, &iter);
1371 ret = _atspi_dbus_attribute_array_from_iter (&iter);
1372 dbus_message_unref (message);
1377 _atspi_dbus_attribute_array_from_iter (DBusMessageIter *iter)
1379 DBusMessageIter iter_array, iter_dict;
1380 GArray *array = g_array_new (TRUE, TRUE, sizeof (gchar *));
1382 dbus_message_iter_recurse (iter, &iter_array);
1383 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1385 const char *name, *value;
1387 dbus_message_iter_recurse (&iter_array, &iter_dict);
1388 dbus_message_iter_get_basic (&iter_dict, &name);
1389 dbus_message_iter_next (&iter_dict);
1390 dbus_message_iter_get_basic (&iter_dict, &value);
1391 str = g_strdup_printf ("%s:%s", name, value);
1392 array = g_array_append_val (array, str);
1393 dbus_message_iter_next (&iter_array);;
1399 _atspi_dbus_set_interfaces (AtspiAccessible *accessible, DBusMessageIter *iter)
1401 DBusMessageIter iter_array;
1403 accessible->interfaces = 0;
1404 dbus_message_iter_recurse (iter, &iter_array);
1405 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1409 dbus_message_iter_get_basic (&iter_array, &iface);
1410 if (!strcmp (iface, "org.freedesktop.DBus.Introspectable")) continue;
1411 n = _atspi_get_iface_num (iface);
1414 g_warning ("AT-SPI: Unknown interface %s", iface);
1417 accessible->interfaces |= (1 << n);
1418 dbus_message_iter_next (&iter_array);
1420 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_INTERFACES);
1424 _atspi_dbus_set_state (AtspiAccessible *accessible, DBusMessageIter *iter)
1426 DBusMessageIter iter_array;
1428 dbus_uint32_t *states;
1430 dbus_message_iter_recurse (iter, &iter_array);
1431 dbus_message_iter_get_fixed_array (&iter_array, &states, &count);
1434 g_warning ("AT-SPI: expected 2 values in states array; got %d\n", count);
1435 if (!accessible->states)
1436 accessible->states = _atspi_state_set_new_internal (accessible, 0);
1440 guint64 val = ((guint64)states [1]) << 32;
1442 if (!accessible->states)
1443 accessible->states = _atspi_state_set_new_internal (accessible, val);
1445 accessible->states->states = val;
1447 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_STATES);
1451 _atspi_error_quark (void)
1453 return g_quark_from_static_string ("atspi_error");
1457 * Gets the IOR from the XDisplay.
1461 get_accessibility_bus_address_x11 (void)
1465 Display *bridge_display = NULL;
1468 unsigned char *data_x11 = NULL;
1469 unsigned long nitems;
1470 unsigned long leftover;
1473 display_name = spi_display_name ();
1477 bridge_display = XOpenDisplay (display_name);
1478 g_free (display_name);
1480 if (!bridge_display)
1482 g_warning ("Could not open X display");
1486 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
1487 XGetWindowProperty (bridge_display,
1488 XDefaultRootWindow (bridge_display),
1490 (long) BUFSIZ, False,
1491 (Atom) 31, &actual_type, &actual_format,
1492 &nitems, &leftover, &data_x11);
1493 XCloseDisplay (bridge_display);
1495 data = g_strdup ((gchar *)data_x11);
1502 get_accessibility_bus_address_dbus (void)
1504 DBusConnection *session_bus = NULL;
1505 DBusMessage *message;
1508 char *address = NULL;
1510 session_bus = dbus_bus_get (DBUS_BUS_SESSION, NULL);
1514 message = dbus_message_new_method_call ("org.a11y.Bus",
1519 dbus_error_init (&error);
1520 reply = dbus_connection_send_with_reply_and_block (session_bus,
1524 dbus_message_unref (message);
1528 g_warning ("Error retrieving accessibility bus address: %s: %s",
1529 error.name, error.message);
1530 dbus_error_free (&error);
1535 const char *tmp_address;
1536 if (!dbus_message_get_args (reply,
1542 dbus_message_unref (reply);
1545 address = g_strdup (tmp_address);
1546 dbus_message_unref (reply);
1552 static DBusConnection *a11y_bus;
1553 static dbus_int32_t a11y_dbus_slot = -1;
1556 a11y_bus_free (void *data)
1558 if (data == a11y_bus)
1561 dbus_connection_free_data_slot (&a11y_dbus_slot);
1566 atspi_get_a11y_bus (void)
1569 char *address = NULL;
1571 if (a11y_bus && dbus_connection_get_is_connected (a11y_bus))
1574 if (a11y_dbus_slot == -1)
1575 if (!dbus_connection_allocate_data_slot (&a11y_dbus_slot))
1576 g_warning ("at-spi: Unable to allocate D-Bus slot");
1579 address = get_accessibility_bus_address_x11 ();
1582 address = get_accessibility_bus_address_dbus ();
1586 dbus_error_init (&error);
1587 a11y_bus = dbus_connection_open_private (address, &error);
1592 g_warning ("Couldn't connect to accessibility bus: %s", error.message);
1593 dbus_error_free (&error);
1598 if (!dbus_bus_register (a11y_bus, &error))
1600 g_warning ("Couldn't register with accessibility bus: %s", error.message);
1601 dbus_error_free (&error);
1602 dbus_connection_close (a11y_bus);
1603 dbus_connection_unref (a11y_bus);
1609 /* Simulate a weak ref on the bus */
1610 dbus_connection_set_data (a11y_bus, a11y_dbus_slot, a11y_bus, a11y_bus_free);
1616 * Set the timeout used for method calls. If this is not set explicitly,
1617 * a default of 0.8 ms is used.
1618 * Note that at-spi2-registryd currently uses a timeout of 3 seconds when
1619 * sending a keyboard event notification. This means that, if an AT makes
1620 * a call in response to the keyboard notification and the application
1621 * being called does not respond before the timeout is reached,
1622 * at-spi2-registryd will time out on the keyboard event notification and
1623 * pass the key onto the application (ie, reply to indicate that the key
1624 * was not consumed), so this may make it undesirable to set a timeout
1625 * larger than 3 seconds.
1627 * @val: The timeout value, in milliseconds, or -1 to disable the timeout.
1628 * @startup_time: The amount of time, in milliseconds, to allow to pass
1629 * before enforcing timeouts on an application. Can be used to prevent
1630 * timeout exceptions if an application is likely to block for an extended
1631 * period of time on initialization. -1 can be passed to disable this
1634 * By default, the normal timeout is set to 800 ms, and the application startup
1635 * timeout is set to 15 seconds.
1638 atspi_set_timeout (gint val, gint startup_time)
1640 method_call_timeout = val;
1641 app_startup_time = startup_time;
1645 * atspi_set_main_context:
1646 * @cnx: The #GmainContext to use.
1648 * Sets the main loop context that AT-SPI should assume is in use when
1649 * setting an idle callback.
1650 * This function should be called by application-side implementors (ie,
1651 * at-spi2-atk) when it is desirable to re-enter the main loop.
1654 atspi_set_main_context (GMainContext *cnx)
1656 if (atspi_main_context == cnx)
1658 if (process_deferred_messages_source != NULL)
1660 g_source_destroy (process_deferred_messages_source);
1661 process_deferred_messages_source = g_idle_source_new ();
1662 g_source_set_callback (process_deferred_messages_source,
1663 _atspi_process_deferred_messages, NULL, NULL);
1664 g_source_attach (process_deferred_messages_source, cnx);
1666 atspi_main_context = cnx;
1667 atspi_dbus_connection_setup_with_g_main (atspi_get_a11y_bus (), cnx);
1670 #ifdef DEBUG_REF_COUNTS
1672 print_disposed (gpointer key, gpointer value, gpointer data)
1674 AtspiAccessible *accessible = key;
1675 if (accessible->parent.app)
1677 g_print ("disposed: %s %d\n", accessible->name, accessible->role);
1683 g_hash_table_foreach (live_refs, print_disposed, NULL);
1688 _atspi_name_compat (gchar *name)
1702 * atspi_role_get_name:
1703 * @role: an #AtspiRole object to query.
1705 * Gets a localizable string that indicates the name of an #AtspiRole.
1706 * <em>DEPRECATED.</em>
1708 * Returns: a localizable string name for an #AtspiRole enumerated type.
1711 atspi_role_get_name (AtspiRole role)
1713 gchar *retval = NULL;
1714 GTypeClass *type_class;
1716 const gchar *name = NULL;
1718 type_class = g_type_class_ref (ATSPI_TYPE_ROLE);
1719 g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), NULL);
1721 value = g_enum_get_value (G_ENUM_CLASS (type_class), role);
1725 retval = g_strdup (value->value_nick);
1729 return _atspi_name_compat (retval);