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 Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 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 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, 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);
41 static DBusConnection *bus = NULL;
42 static GHashTable *live_refs = NULL;
43 static gint method_call_timeout = 800;
44 static gint app_startup_time = 15000;
45 static gboolean allow_sync = TRUE;
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_table_cell = ATSPI_DBUS_INTERFACE_TABLE_CELL;
72 const char *atspi_interface_text = ATSPI_DBUS_INTERFACE_TEXT;
73 const char *atspi_interface_cache = ATSPI_DBUS_INTERFACE_CACHE;
74 const char *atspi_interface_value = ATSPI_DBUS_INTERFACE_VALUE;
76 static const char *interfaces[] =
78 ATSPI_DBUS_INTERFACE_ACCESSIBLE,
79 ATSPI_DBUS_INTERFACE_ACTION,
80 ATSPI_DBUS_INTERFACE_APPLICATION,
81 ATSPI_DBUS_INTERFACE_COLLECTION,
82 ATSPI_DBUS_INTERFACE_COMPONENT,
83 ATSPI_DBUS_INTERFACE_DOCUMENT,
84 ATSPI_DBUS_INTERFACE_EDITABLE_TEXT,
85 ATSPI_DBUS_INTERFACE_HYPERLINK,
86 ATSPI_DBUS_INTERFACE_HYPERTEXT,
87 ATSPI_DBUS_INTERFACE_IMAGE,
88 "org.a11y.atspi.LoginHelper",
89 ATSPI_DBUS_INTERFACE_SELECTION,
90 ATSPI_DBUS_INTERFACE_TABLE,
91 ATSPI_DBUS_INTERFACE_TABLE_CELL,
92 ATSPI_DBUS_INTERFACE_TEXT,
93 ATSPI_DBUS_INTERFACE_VALUE,
98 _atspi_get_iface_num (const char *iface)
100 /* TODO: Use a binary search or hash to improve performance */
103 for (i = 0; interfaces[i]; i++)
105 if (!strcmp(iface, interfaces[i])) return i;
111 _atspi_get_live_refs (void)
115 live_refs = g_hash_table_new (g_direct_hash, g_direct_equal);
120 /* TODO: Add an application parameter */
127 g_error ("AT-SPI: Couldn't connect to accessibility bus. Is at-spi-bus-launcher running?");
131 #define APP_IS_REGISTRY(app) (!strcmp (app->bus_name, atspi_bus_registry))
133 static AtspiAccessible *desktop;
145 g_hash_table_destroy (refs);
150 dbus_connection_close (bus);
151 dbus_connection_unref (bus);
158 /* TODO: Do we need this code, or should we just dispose the desktop? */
159 for (i = desktop->children->len - 1; i >= 0; i--)
161 AtspiAccessible *child = g_ptr_array_index (desktop->children, i);
162 if (child->parent.app)
163 g_object_run_dispose (G_OBJECT (child->parent.app));
164 g_object_run_dispose (G_OBJECT (child));
167 g_object_run_dispose (G_OBJECT (desktop->parent.app));
168 g_object_unref (desktop);
172 static gboolean atspi_inited = FALSE;
174 static GHashTable *app_hash = NULL;
177 handle_get_bus_address (DBusPendingCall *pending, void *user_data)
179 AtspiApplication *app = user_data;
180 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
181 DBusMessage *message;
183 DBusPendingCall *new_pending;
185 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
187 if (dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &address,
188 DBUS_TYPE_INVALID) && address [0])
193 dbus_error_init (&error);
194 bus = dbus_connection_open_private (address, &error);
199 dbus_connection_unref (app->bus);
202 atspi_dbus_connection_setup_with_g_main(bus, g_main_context_default());
206 if (!strcmp (error.name, DBUS_ERROR_FILE_NOT_FOUND))
207 g_warning ("AT-SPI: Unable to open bus connection: %s", error.message);
208 dbus_error_free (&error);
212 dbus_message_unref (reply);
213 dbus_pending_call_unref (pending);
216 return; /* application has gone away / been disposed */
218 message = dbus_message_new_method_call (app->bus_name,
219 "/org/a11y/atspi/cache",
220 atspi_interface_cache, "GetItems");
222 dbus_connection_send_with_reply (app->bus, message, &new_pending, 2000);
223 dbus_message_unref (message);
226 dbus_pending_call_set_notify (new_pending, handle_get_items, app, NULL);
229 static AtspiApplication *
230 get_application (const char *bus_name)
232 AtspiApplication *app = NULL;
234 DBusMessage *message;
235 DBusPendingCall *pending = NULL;
239 app_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_object_unref);
240 if (!app_hash) return NULL;
242 app = g_hash_table_lookup (app_hash, bus_name);
244 bus_name_dup = g_strdup (bus_name);
245 if (!bus_name_dup) return NULL;
246 // TODO: change below to something that will send state-change:defunct notification if necessary */
247 app = _atspi_application_new (bus_name);
248 app->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
249 app->bus = dbus_connection_ref (_atspi_bus ());
250 gettimeofday (&app->time_added, NULL);
251 app->cache = ATSPI_CACHE_UNDEFINED;
252 g_hash_table_insert (app_hash, bus_name_dup, app);
253 message = dbus_message_new_method_call (bus_name, atspi_path_root,
254 atspi_interface_application, "GetApplicationBusAddress");
256 dbus_connection_send_with_reply (app->bus, message, &pending, 2000);
257 dbus_message_unref (message);
260 g_hash_table_remove (app_hash, bus_name_dup);
263 dbus_pending_call_set_notify (pending, handle_get_bus_address, app, NULL);
267 static AtspiAccessible *
268 ref_accessible (const char *app_name, const char *path)
270 AtspiApplication *app;
273 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
276 app = get_application (app_name);
278 if (!strcmp (path, "/org/a11y/atspi/accessible/root"))
282 app->root = _atspi_accessible_new (app, atspi_path_root);
283 app->root->accessible_parent = atspi_get_desktop (0);
284 g_ptr_array_add (app->root->accessible_parent->children, g_object_ref (app->root));
286 return g_object_ref (app->root);
289 a = g_hash_table_lookup (app->hash, path);
292 return g_object_ref (a);
294 a = _atspi_accessible_new (app, path);
297 g_hash_table_insert (app->hash, g_strdup (a->parent.path), g_object_ref (a));
301 static AtspiHyperlink *
302 ref_hyperlink (const char *app_name, const char *path)
304 AtspiApplication *app = get_application (app_name);
305 AtspiHyperlink *hyperlink;
307 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
310 hyperlink = g_hash_table_lookup (app->hash, path);
313 return g_object_ref (hyperlink);
315 hyperlink = _atspi_hyperlink_new (app, path);
316 g_hash_table_insert (app->hash, g_strdup (hyperlink->parent.path), hyperlink);
317 /* TODO: This should be a weak ref */
318 g_object_ref (hyperlink); /* for the hash */
331 GArray *state_bitflags;
334 static DBusHandlerResult
335 handle_remove_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
337 const char *sender = dbus_message_get_sender (message);
338 AtspiApplication *app;
340 DBusMessageIter iter, iter_struct;
341 const char *signature = dbus_message_get_signature (message);
344 if (strcmp (signature, "(so)") != 0)
346 g_warning ("AT-SPI: Unknown signature %s for RemoveAccessible", signature);
347 return DBUS_HANDLER_RESULT_HANDLED;
350 dbus_message_iter_init (message, &iter);
351 dbus_message_iter_recurse (&iter, &iter_struct);
352 dbus_message_iter_get_basic (&iter_struct, &sender);
353 dbus_message_iter_next (&iter_struct);
354 dbus_message_iter_get_basic (&iter_struct, &path);
355 app = get_application (sender);
356 a = ref_accessible (sender, path);
358 return DBUS_HANDLER_RESULT_HANDLED;
359 g_object_run_dispose (G_OBJECT (a));
360 g_hash_table_remove (app->hash, a->parent.path);
361 g_object_unref (a); /* unref our own ref */
362 return DBUS_HANDLER_RESULT_HANDLED;
365 static DBusHandlerResult
366 handle_name_owner_changed (DBusConnection *bus, DBusMessage *message, void *user_data)
368 const char *name, *new, *old;
369 static gboolean registry_lost = FALSE;
371 if (!dbus_message_get_args (message, NULL,
372 DBUS_TYPE_STRING, &name,
373 DBUS_TYPE_STRING, &old,
374 DBUS_TYPE_STRING, &new,
377 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
380 if (!strcmp (name, "org.a11y.atspi.Registry"))
382 if (registry_lost && !old[0])
384 _atspi_reregister_event_listeners ();
385 _atspi_reregister_device_listeners ();
386 registry_lost = FALSE;
389 registry_lost = TRUE;
393 AtspiApplication *app = g_hash_table_lookup (app_hash, old);
394 if (app && !strcmp (app->bus_name, old))
395 g_object_run_dispose (G_OBJECT (app));
397 return DBUS_HANDLER_RESULT_HANDLED;
401 add_app_to_desktop (AtspiAccessible *a, const char *bus_name)
403 AtspiAccessible *obj = ref_accessible (bus_name, atspi_path_root);
404 /* The app will be added to the desktop as a side-effect of calling
406 g_object_unref (obj);
407 return (obj != NULL);
411 get_reference_from_iter (DBusMessageIter *iter, const char **app_name, const char **path)
413 DBusMessageIter iter_struct;
415 dbus_message_iter_recurse (iter, &iter_struct);
416 dbus_message_iter_get_basic (&iter_struct, app_name);
417 dbus_message_iter_next (&iter_struct);
418 dbus_message_iter_get_basic (&iter_struct, path);
419 dbus_message_iter_next (iter);
423 add_accessible_from_iter (DBusMessageIter *iter)
425 DBusMessageIter iter_struct, iter_array;
426 const char *app_name, *path;
427 AtspiAccessible *accessible;
428 const char *name, *description;
430 gboolean children_cached = FALSE;
431 dbus_int32_t count, index;
433 dbus_message_iter_recurse (iter, &iter_struct);
436 get_reference_from_iter (&iter_struct, &app_name, &path);
437 accessible = ref_accessible (app_name, path);
441 /* Get application: TODO */
442 dbus_message_iter_next (&iter_struct);
445 get_reference_from_iter (&iter_struct, &app_name, &path);
446 if (accessible->accessible_parent)
447 g_object_unref (accessible->accessible_parent);
448 accessible->accessible_parent = ref_accessible (app_name, path);
450 if (dbus_message_iter_get_arg_type (&iter_struct) == 'i')
452 /* Get index in parent */
453 dbus_message_iter_get_basic (&iter_struct, &index);
454 if (index >= 0 && accessible->accessible_parent)
456 if (index >= accessible->accessible_parent->children->len)
457 g_ptr_array_set_size (accessible->accessible_parent->children, index + 1);
458 g_ptr_array_index (accessible->accessible_parent->children, index) = g_object_ref (accessible);
461 /* get child count */
462 dbus_message_iter_next (&iter_struct);
463 dbus_message_iter_get_basic (&iter_struct, &count);
466 g_ptr_array_set_size (accessible->children, count);
467 children_cached = TRUE;
470 else if (dbus_message_iter_get_arg_type (&iter_struct) == 'a')
472 /* It's the old API with a list of children */
473 /* TODO: Perhaps remove this code eventually */
474 dbus_message_iter_recurse (&iter_struct, &iter_array);
475 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
477 AtspiAccessible *child;
478 get_reference_from_iter (&iter_array, &app_name, &path);
479 child = ref_accessible (app_name, path);
480 g_ptr_array_remove (accessible->children, child);
481 g_ptr_array_add (accessible->children, child);
483 children_cached = TRUE;
487 dbus_message_iter_next (&iter_struct);
488 _atspi_dbus_set_interfaces (accessible, &iter_struct);
489 dbus_message_iter_next (&iter_struct);
492 if (accessible->name)
493 g_free (accessible->name);
494 dbus_message_iter_get_basic (&iter_struct, &name);
495 accessible->name = g_strdup (name);
496 dbus_message_iter_next (&iter_struct);
499 dbus_message_iter_get_basic (&iter_struct, &role);
500 accessible->role = role;
501 dbus_message_iter_next (&iter_struct);
504 if (accessible->description)
505 g_free (accessible->description);
506 dbus_message_iter_get_basic (&iter_struct, &description);
507 accessible->description = g_strdup (description);
508 dbus_message_iter_next (&iter_struct);
510 _atspi_dbus_set_state (accessible, &iter_struct);
511 dbus_message_iter_next (&iter_struct);
513 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_NAME | ATSPI_CACHE_ROLE |
514 ATSPI_CACHE_PARENT | ATSPI_CACHE_DESCRIPTION);
515 if (!atspi_state_set_contains (accessible->states,
516 ATSPI_STATE_MANAGES_DESCENDANTS) &&
518 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_CHILDREN);
520 /* This is a bit of a hack since the cache holds a ref, so we don't need
521 * the one provided for us anymore */
522 g_object_unref (accessible);
526 handle_get_items (DBusPendingCall *pending, void *user_data)
528 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
529 DBusMessageIter iter, iter_array;
531 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
533 const char *sender = dbus_message_get_sender (reply);
534 const char *error = NULL;
535 const char *error_name = dbus_message_get_error_name (reply);
536 if (!strcmp (error_name, DBUS_ERROR_SERVICE_UNKNOWN)
537 || !strcmp (error_name, DBUS_ERROR_NO_REPLY))
542 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &error,
544 g_warning ("AT-SPI: Error in GetItems, sender=%s, error=%s", sender, error);
546 dbus_message_unref (reply);
547 dbus_pending_call_unref (pending);
551 dbus_message_iter_init (reply, &iter);
552 dbus_message_iter_recurse (&iter, &iter_array);
553 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
555 add_accessible_from_iter (&iter_array);
556 dbus_message_iter_next (&iter_array);
558 dbus_message_unref (reply);
559 dbus_pending_call_unref (pending);
562 /* TODO: Do we stil need this function? */
563 static AtspiAccessible *
564 ref_accessible_desktop (AtspiApplication *app)
567 DBusMessage *message, *reply;
568 DBusMessageIter iter, iter_array;
573 g_object_ref (desktop);
576 desktop = _atspi_accessible_new (app, atspi_path_root);
581 g_hash_table_insert (app->hash, g_strdup (desktop->parent.path),
582 g_object_ref (desktop));
583 app->root = g_object_ref (desktop);
584 desktop->name = g_strdup ("main");
585 message = dbus_message_new_method_call (atspi_bus_registry,
587 atspi_interface_accessible,
592 reply = _atspi_dbus_send_with_reply_and_block (message, &error);
593 if (!reply || strcmp (dbus_message_get_signature (reply), "a(so)") != 0)
597 g_warning ("Couldn't get application list: %s", error->message);
598 g_clear_error (&error);
601 dbus_message_unref (reply);
604 dbus_message_iter_init (reply, &iter);
605 dbus_message_iter_recurse (&iter, &iter_array);
606 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
608 const char *app_name, *path;
609 get_reference_from_iter (&iter_array, &app_name, &path);
610 add_app_to_desktop (desktop, app_name);
613 /* Record the alternate name as an alias for org.a11y.atspi.Registry */
614 bus_name_dup = g_strdup (dbus_message_get_sender (reply));
616 g_hash_table_insert (app_hash, bus_name_dup, app);
618 dbus_message_unref (reply);
620 return g_object_ref (desktop);
624 _atspi_ref_accessible (const char *app, const char *path)
626 AtspiApplication *a = get_application (app);
629 if ( APP_IS_REGISTRY(a))
632 g_object_unref (ref_accessible_desktop (a)); /* sets a->root */
633 return g_object_ref (a->root);
635 return ref_accessible (app, path);
639 _atspi_dbus_return_accessible_from_message (DBusMessage *message)
641 DBusMessageIter iter;
642 AtspiAccessible *retval = NULL;
643 const char *signature;
648 signature = dbus_message_get_signature (message);
649 if (!strcmp (signature, "(so)"))
651 dbus_message_iter_init (message, &iter);
652 retval = _atspi_dbus_return_accessible_from_iter (&iter);
656 g_warning ("AT-SPI: Called _atspi_dbus_return_accessible_from_message with strange signature %s", signature);
658 dbus_message_unref (message);
663 _atspi_dbus_return_accessible_from_iter (DBusMessageIter *iter)
665 const char *app_name, *path;
667 get_reference_from_iter (iter, &app_name, &path);
668 return ref_accessible (app_name, path);
672 _atspi_dbus_return_hyperlink_from_message (DBusMessage *message)
674 DBusMessageIter iter;
675 AtspiHyperlink *retval = NULL;
676 const char *signature;
681 signature = dbus_message_get_signature (message);
682 if (!strcmp (signature, "(so)"))
684 dbus_message_iter_init (message, &iter);
685 retval = _atspi_dbus_return_hyperlink_from_iter (&iter);
689 g_warning ("AT-SPI: Called _atspi_dbus_return_hyperlink_from_message with strange signature %s", signature);
691 dbus_message_unref (message);
696 _atspi_dbus_return_hyperlink_from_iter (DBusMessageIter *iter)
698 const char *app_name, *path;
700 get_reference_from_iter (iter, &app_name, &path);
701 return ref_hyperlink (app_name, path);
704 const char *cache_signal_type = "((so)(so)(so)iiassusau)";
705 const char *old_cache_signal_type = "((so)(so)(so)a(so)assusau)";
707 static DBusHandlerResult
708 handle_add_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
710 DBusMessageIter iter;
711 const char *signature = dbus_message_get_signature (message);
713 if (strcmp (signature, cache_signal_type) != 0 &&
714 strcmp (signature, old_cache_signal_type) != 0)
716 g_warning ("AT-SPI: AddAccessible with unknown signature %s\n", signature);
717 return DBUS_HANDLER_RESULT_HANDLED;
720 dbus_message_iter_init (message, &iter);
721 add_accessible_from_iter (&iter);
722 return DBUS_HANDLER_RESULT_HANDLED;
728 DBusMessage *message;
732 static GSource *process_deferred_messages_source = NULL;
735 process_deferred_message (BusDataClosure *closure)
737 int type = dbus_message_get_type (closure->message);
738 const char *interface = dbus_message_get_interface (closure->message);
740 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
741 !strncmp (interface, "org.a11y.atspi.Event.", 21))
743 _atspi_dbus_handle_event (closure->bus, closure->message, closure->data);
745 if (dbus_message_is_method_call (closure->message, atspi_interface_device_event_listener, "NotifyEvent"))
747 _atspi_dbus_handle_DeviceEvent (closure->bus,
748 closure->message, closure->data);
750 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "AddAccessible"))
752 handle_add_accessible (closure->bus, closure->message, closure->data);
754 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "RemoveAccessible"))
756 handle_remove_accessible (closure->bus, closure->message, closure->data);
758 if (dbus_message_is_signal (closure->message, "org.freedesktop.DBus", "NameOwnerChanged"))
760 handle_name_owner_changed (closure->bus, closure->message, closure->data);
764 static GQueue *deferred_messages = NULL;
767 process_deferred_messages (void)
769 static int in_process_deferred_messages = 0;
770 BusDataClosure *closure;
772 if (in_process_deferred_messages)
774 in_process_deferred_messages = 1;
775 while ((closure = g_queue_pop_head (deferred_messages)))
777 process_deferred_message (closure);
778 dbus_message_unref (closure->message);
779 dbus_connection_unref (closure->bus);
782 in_process_deferred_messages = 0;
787 process_deferred_messages_callback (gpointer data)
789 if (process_deferred_messages ())
790 return G_SOURCE_CONTINUE;
792 process_deferred_messages_source = NULL;
793 return G_SOURCE_REMOVE;
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 process_deferred_messages_callback, NULL, NULL);
812 g_source_attach (process_deferred_messages_source, atspi_main_context);
813 g_source_unref (process_deferred_messages_source);
816 return DBUS_HANDLER_RESULT_HANDLED;
819 static DBusHandlerResult
820 atspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
822 int type = dbus_message_get_type (message);
823 const char *interface = dbus_message_get_interface (message);
825 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
826 !strncmp (interface, "org.a11y.atspi.Event.", 21))
828 return defer_message (bus, message, data);
830 if (dbus_message_is_method_call (message, atspi_interface_device_event_listener, "NotifyEvent"))
832 return defer_message (bus, message, data);
834 if (dbus_message_is_signal (message, atspi_interface_cache, "AddAccessible"))
836 return defer_message (bus, message, data);
838 if (dbus_message_is_signal (message, atspi_interface_cache, "RemoveAccessible"))
840 return defer_message (bus, message, data);
842 if (dbus_message_is_signal (message, "org.freedesktop.DBus", "NameOwnerChanged"))
844 defer_message (bus, message, data);
845 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
847 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
851 * Returns a 'canonicalized' value for DISPLAY,
852 * with the screen number stripped off if present.
854 * TODO: Avoid having duplicate functions for this here and in at-spi2-atk
857 spi_display_name (void)
859 char *canonical_display_name = NULL;
860 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
864 display_env = g_getenv ("DISPLAY");
865 if (!display_env || !display_env[0])
869 gchar *display_p, *screen_p;
870 canonical_display_name = g_strdup (display_env);
871 display_p = g_utf8_strrchr (canonical_display_name, -1, ':');
872 screen_p = g_utf8_strrchr (canonical_display_name, -1, '.');
873 if (screen_p && display_p && (screen_p > display_p))
881 canonical_display_name = g_strdup (display_env);
884 return canonical_display_name;
890 * Connects to the accessibility registry and initializes the SPI.
892 * Returns: 0 on success, 1 if already initialized, or an integer error code.
898 const gchar *no_cache;
907 _atspi_get_live_refs();
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 ();
945 * atspi_is_initialized:
947 * Indicates whether AT-SPI has been initialized.
949 * Returns: %True if initialized; %False otherwise.
952 atspi_is_initialized ()
960 * Starts/enters the main event loop for the AT-SPI services.
962 * NOTE: This method does not return control; it is exited via a call to
963 * #atspi_event_quit from within an event handler.
967 atspi_event_main (void)
969 atspi_main_loop = g_main_loop_new (NULL, FALSE);
970 g_main_loop_run (atspi_main_loop);
971 atspi_main_loop = NULL;
977 * Quits the last main event loop for the AT-SPI services,
978 * See: #atspi_event_main
981 atspi_event_quit (void)
983 g_main_loop_quit (atspi_main_loop);
989 * Disconnects from #AtspiRegistry instances and releases
990 * any floating resources. Call only once at exit.
992 * Returns: 0 if there were no leaks, otherwise other integer values.
1004 atspi_inited = FALSE;
1008 leaked = g_hash_table_size (live_refs);
1020 static GSList *hung_processes;
1023 remove_hung_process (DBusPendingCall *pending, void *data)
1025 hung_processes = g_slist_remove (hung_processes, data);
1027 dbus_pending_call_unref (pending);
1031 check_for_hang (DBusMessage *message, DBusError *error, DBusConnection *bus, const char *bus_name)
1033 if (!message && error->name &&
1034 !strcmp (error->name, "org.freedesktop.DBus.Error.NoReply"))
1037 DBusMessage *message;
1038 gchar *bus_name_dup;
1039 DBusPendingCall *pending = NULL;
1040 for (l = hung_processes; l; l = l->next)
1041 if (!strcmp (l->data, bus_name))
1043 message = dbus_message_new_method_call (bus_name, "/",
1044 "org.freedesktop.DBus.Peer",
1048 dbus_connection_send_with_reply (bus, message, &pending, -1);
1049 dbus_message_unref (message);
1052 bus_name_dup = g_strdup (bus_name);
1053 hung_processes = g_slist_append (hung_processes, bus_name_dup);
1054 dbus_pending_call_set_notify (pending, remove_hung_process, bus_name_dup, NULL);
1059 connection_is_hung (const char *bus_name)
1063 for (l = hung_processes; l; l = l->next)
1064 if (!strcmp (l->data, bus_name))
1070 check_app (AtspiApplication *app, GError **error)
1072 if (!app || !app->bus)
1074 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
1075 _("The application no longer exists"));
1079 if (atspi_main_loop && connection_is_hung (app->bus_name))
1081 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC,
1082 "The process appears to be hung.");
1090 set_timeout (AtspiApplication *app)
1095 if (app && app_startup_time > 0)
1097 gettimeofday (&tv, NULL);
1098 diff = (tv.tv_sec - app->time_added.tv_sec) * 1000 + (tv.tv_usec - app->time_added.tv_usec) / 1000;
1099 dbind_set_timeout (MAX(method_call_timeout, app_startup_time - diff));
1102 dbind_set_timeout (method_call_timeout);
1106 _atspi_dbus_call (gpointer obj, const char *interface, const char *method, GError **error, const char *type, ...)
1111 AtspiObject *aobj = ATSPI_OBJECT (obj);
1113 if (!check_app (aobj->app, error))
1118 _atspi_set_error_no_sync (error);
1122 va_start (args, type);
1123 dbus_error_init (&err);
1124 set_timeout (aobj->app);
1125 retval = dbind_method_call_reentrant_va (aobj->app->bus, aobj->app->bus_name,
1126 aobj->path, interface, method, &err,
1129 check_for_hang (NULL, &err, aobj->app->bus, aobj->app->bus_name);
1130 process_deferred_messages ();
1131 if (dbus_error_is_set (&err))
1133 g_set_error(error, ATSPI_ERROR, ATSPI_ERROR_IPC, "%s", err.message);
1134 dbus_error_free (&err);
1140 _atspi_dbus_call_partial (gpointer obj,
1141 const char *interface,
1144 const char *type, ...)
1148 va_start (args, type);
1149 return _atspi_dbus_call_partial_va (obj, interface, method, error, type, args);
1154 _atspi_dbus_call_partial_va (gpointer obj,
1155 const char *interface,
1161 AtspiObject *aobj = ATSPI_OBJECT (obj);
1163 DBusMessage *msg = NULL, *reply = NULL;
1164 DBusMessageIter iter;
1167 dbus_error_init (&err);
1169 if (!check_app (aobj->app, error))
1172 msg = dbus_message_new_method_call (aobj->app->bus_name, aobj->path, interface, method);
1177 dbus_message_iter_init_append (msg, &iter);
1178 dbind_any_marshal_va (&iter, &p, args);
1180 set_timeout (aobj->app);
1181 reply = dbind_send_and_allow_reentry (aobj->app->bus, msg, &err);
1182 check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1186 dbus_message_unref (msg);
1187 process_deferred_messages ();
1188 if (dbus_error_is_set (&err))
1190 /* TODO: Set gerror */
1191 dbus_error_free (&err);
1194 if (reply && dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
1196 const char *err_str = NULL;
1197 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &err_str, DBUS_TYPE_INVALID);
1199 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err_str);
1200 dbus_message_unref (reply);
1208 _atspi_dbus_get_property (gpointer obj, const char *interface, const char *name, GError **error, const char *type, void *data)
1210 DBusMessage *message, *reply;
1211 DBusMessageIter iter, iter_variant;
1213 dbus_bool_t retval = FALSE;
1214 AtspiObject *aobj = ATSPI_OBJECT (obj);
1215 char expected_type = (type [0] == '(' ? 'r' : type [0]);
1220 if (!check_app (aobj->app, error))
1225 _atspi_set_error_no_sync (error);
1229 message = dbus_message_new_method_call (aobj->app->bus_name,
1231 "org.freedesktop.DBus.Properties",
1235 // TODO: throw exception
1238 dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1239 dbus_error_init (&err);
1240 set_timeout (aobj->app);
1241 reply = dbind_send_and_allow_reentry (aobj->app->bus, message, &err);
1242 check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1243 dbus_message_unref (message);
1244 process_deferred_messages ();
1247 // TODO: throw exception
1251 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
1253 const char *err_str = NULL;
1254 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &err_str, DBUS_TYPE_INVALID);
1256 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err_str);
1260 dbus_message_iter_init (reply, &iter);
1261 if (dbus_message_iter_get_arg_type (&iter) != 'v')
1263 g_warning ("atspi_dbus_get_property: expected a variant when fetching %s from interface %s; got %s\n", name, interface, dbus_message_get_signature (reply));
1266 dbus_message_iter_recurse (&iter, &iter_variant);
1267 if (dbus_message_iter_get_arg_type (&iter_variant) != expected_type)
1269 g_warning ("atspi_dbus_get_property: Wrong type: expected %s, got %c\n", type, dbus_message_iter_get_arg_type (&iter_variant));
1272 if (!strcmp (type, "(so)"))
1274 *((AtspiAccessible **)data) = _atspi_dbus_return_accessible_from_iter (&iter_variant);
1278 dbus_message_iter_get_basic (&iter_variant, data);
1279 if (type [0] == 's')
1280 *(char **)data = g_strdup (*(char **)data);
1284 dbus_error_free (&err);
1286 dbus_message_unref (reply);
1291 _atspi_dbus_send_with_reply_and_block (DBusMessage *message, GError **error)
1295 AtspiApplication *app;
1296 DBusConnection *bus;
1298 app = get_application (dbus_message_get_destination (message));
1300 if (app && !app->bus)
1301 return NULL; /* will fail anyway; app has been disposed */
1303 bus = (app ? app->bus : _atspi_bus());
1304 dbus_error_init (&err);
1306 reply = dbind_send_and_allow_reentry (bus, message, &err);
1307 process_deferred_messages ();
1308 dbus_message_unref (message);
1309 if (dbus_error_is_set (&err))
1312 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err.message);
1313 dbus_error_free (&err);
1319 _atspi_dbus_return_hash_from_message (DBusMessage *message)
1321 DBusMessageIter iter;
1327 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1329 dbus_message_iter_init (message, &iter);
1330 ret = _atspi_dbus_hash_from_iter (&iter);
1331 dbus_message_unref (message);
1336 _atspi_dbus_hash_from_iter (DBusMessageIter *iter)
1338 GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
1339 (GDestroyNotify) g_free,
1340 (GDestroyNotify) g_free);
1341 DBusMessageIter iter_array, iter_dict;
1343 dbus_message_iter_recurse (iter, &iter_array);
1344 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1346 const char *name, *value;
1347 dbus_message_iter_recurse (&iter_array, &iter_dict);
1348 dbus_message_iter_get_basic (&iter_dict, &name);
1349 dbus_message_iter_next (&iter_dict);
1350 dbus_message_iter_get_basic (&iter_dict, &value);
1351 g_hash_table_insert (hash, g_strdup (name), g_strdup (value));
1352 dbus_message_iter_next (&iter_array);
1358 _atspi_dbus_return_attribute_array_from_message (DBusMessage *message)
1360 DBusMessageIter iter;
1366 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1368 dbus_message_iter_init (message, &iter);
1370 ret = _atspi_dbus_attribute_array_from_iter (&iter);
1371 dbus_message_unref (message);
1376 _atspi_dbus_attribute_array_from_iter (DBusMessageIter *iter)
1378 DBusMessageIter iter_array, iter_dict;
1379 GArray *array = g_array_new (TRUE, TRUE, sizeof (gchar *));
1381 dbus_message_iter_recurse (iter, &iter_array);
1382 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1384 const char *name, *value;
1386 dbus_message_iter_recurse (&iter_array, &iter_dict);
1387 dbus_message_iter_get_basic (&iter_dict, &name);
1388 dbus_message_iter_next (&iter_dict);
1389 dbus_message_iter_get_basic (&iter_dict, &value);
1390 str = g_strdup_printf ("%s:%s", name, value);
1391 array = g_array_append_val (array, str);
1392 dbus_message_iter_next (&iter_array);;
1398 _atspi_dbus_set_interfaces (AtspiAccessible *accessible, DBusMessageIter *iter)
1400 DBusMessageIter iter_array;
1401 char *iter_sig = dbus_message_iter_get_signature (iter);
1403 accessible->interfaces = 0;
1404 if (strcmp (iter_sig, "as") != 0)
1406 g_warning ("_atspi_dbus_set_interfaces: Passed iterator with invalid signature %s", dbus_message_iter_get_signature (iter));
1407 dbus_free (iter_sig);
1410 dbus_free (iter_sig);
1411 dbus_message_iter_recurse (iter, &iter_array);
1412 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1416 dbus_message_iter_get_basic (&iter_array, &iface);
1417 if (!strcmp (iface, "org.freedesktop.DBus.Introspectable")) continue;
1418 n = _atspi_get_iface_num (iface);
1421 g_warning ("AT-SPI: Unknown interface %s", iface);
1424 accessible->interfaces |= (1 << n);
1425 dbus_message_iter_next (&iter_array);
1427 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_INTERFACES);
1431 _atspi_dbus_set_state (AtspiAccessible *accessible, DBusMessageIter *iter)
1433 DBusMessageIter iter_array;
1435 dbus_uint32_t *states;
1437 dbus_message_iter_recurse (iter, &iter_array);
1438 dbus_message_iter_get_fixed_array (&iter_array, &states, &count);
1441 g_warning ("AT-SPI: expected 2 values in states array; got %d\n", count);
1442 if (!accessible->states)
1443 accessible->states = _atspi_state_set_new_internal (accessible, 0);
1447 guint64 val = ((guint64)states [1]) << 32;
1449 if (!accessible->states)
1450 accessible->states = _atspi_state_set_new_internal (accessible, val);
1452 accessible->states->states = val;
1454 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_STATES);
1458 _atspi_error_quark (void)
1460 return g_quark_from_static_string ("atspi_error");
1464 * Gets the IOR from the XDisplay.
1468 get_accessibility_bus_address_x11 (void)
1472 Display *bridge_display = NULL;
1475 unsigned char *data_x11 = NULL;
1476 unsigned long nitems;
1477 unsigned long leftover;
1480 display_name = spi_display_name ();
1484 bridge_display = XOpenDisplay (display_name);
1485 g_free (display_name);
1487 if (!bridge_display)
1489 g_warning ("Could not open X display");
1493 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
1494 XGetWindowProperty (bridge_display,
1495 XDefaultRootWindow (bridge_display),
1497 (long) BUFSIZ, False,
1498 (Atom) 31, &actual_type, &actual_format,
1499 &nitems, &leftover, &data_x11);
1500 XCloseDisplay (bridge_display);
1502 data = g_strdup ((gchar *)data_x11);
1509 get_accessibility_bus_address_dbus (void)
1511 DBusConnection *session_bus = NULL;
1512 DBusMessage *message;
1515 char *address = NULL;
1517 session_bus = dbus_bus_get (DBUS_BUS_SESSION, NULL);
1521 message = dbus_message_new_method_call ("org.a11y.Bus",
1526 dbus_error_init (&error);
1527 reply = dbus_connection_send_with_reply_and_block (session_bus,
1531 dbus_message_unref (message);
1535 g_warning ("AT-SPI: Error retrieving accessibility bus address: %s: %s",
1536 error.name, error.message);
1537 dbus_error_free (&error);
1542 const char *tmp_address;
1543 if (!dbus_message_get_args (reply,
1549 dbus_message_unref (reply);
1552 address = g_strdup (tmp_address);
1553 dbus_message_unref (reply);
1557 dbus_connection_unref (session_bus);
1561 static DBusConnection *a11y_bus;
1562 static dbus_int32_t a11y_dbus_slot = -1;
1565 a11y_bus_free (void *data)
1567 if (data == a11y_bus)
1570 dbus_connection_free_data_slot (&a11y_dbus_slot);
1575 * atspi_get_a11y_bus: (skip)
1578 atspi_get_a11y_bus (void)
1581 char *address = NULL;
1582 const char *address_env = NULL;
1584 if (a11y_bus && dbus_connection_get_is_connected (a11y_bus))
1587 if (a11y_dbus_slot == -1)
1588 if (!dbus_connection_allocate_data_slot (&a11y_dbus_slot))
1589 g_warning ("AT-SPI: Unable to allocate D-Bus slot");
1591 address_env = g_getenv ("AT_SPI_BUS_ADDRESS");
1592 if (address_env != NULL && *address_env != 0)
1593 address = g_strdup (address_env);
1595 if (!address && g_getenv ("DISPLAY") != NULL &&
1596 g_getenv ("WAYLAND_DISPLAY") == NULL)
1597 address = get_accessibility_bus_address_x11 ();
1600 address = get_accessibility_bus_address_dbus ();
1604 dbus_error_init (&error);
1605 a11y_bus = dbus_connection_open_private (address, &error);
1610 if (!g_getenv("SSH_CONNECTION"))
1611 g_warning ("Couldn't connect to accessibility bus: %s", error.message);
1612 dbus_error_free (&error);
1617 if (!dbus_bus_register (a11y_bus, &error))
1619 g_warning ("Couldn't register with accessibility bus: %s", error.message);
1620 dbus_error_free (&error);
1621 dbus_connection_close (a11y_bus);
1622 dbus_connection_unref (a11y_bus);
1628 /* Simulate a weak ref on the bus */
1629 dbus_connection_set_data (a11y_bus, a11y_dbus_slot, a11y_bus, a11y_bus_free);
1635 * atspi_set_timeout:
1636 * @val: The timeout value, in milliseconds, or -1 to disable the timeout.
1637 * @startup_time: The amount of time, in milliseconds, to allow to pass
1638 * before enforcing timeouts on an application. Can be used to prevent
1639 * timeout exceptions if an application is likely to block for an extended
1640 * period of time on initialization. -1 can be passed to disable this
1643 * Set the timeout used for method calls. If this is not set explicitly,
1644 * a default of 0.8 ms is used.
1645 * Note that at-spi2-registryd currently uses a timeout of 3 seconds when
1646 * sending a keyboard event notification. This means that, if an AT makes
1647 * a call in response to the keyboard notification and the application
1648 * being called does not respond before the timeout is reached,
1649 * at-spi2-registryd will time out on the keyboard event notification and
1650 * pass the key onto the application (ie, reply to indicate that the key
1651 * was not consumed), so this may make it undesirable to set a timeout
1652 * larger than 3 seconds.
1654 * By default, the normal timeout is set to 800 ms, and the application startup
1655 * timeout is set to 15 seconds.
1658 atspi_set_timeout (gint val, gint startup_time)
1660 method_call_timeout = val;
1661 app_startup_time = startup_time;
1665 * atspi_set_main_context:
1666 * @cnx: The #GMainContext to use.
1668 * Sets the main loop context that AT-SPI should assume is in use when
1669 * setting an idle callback.
1670 * This function should be called by application-side implementors (ie,
1671 * at-spi2-atk) when it is desirable to re-enter the main loop.
1674 atspi_set_main_context (GMainContext *cnx)
1676 if (atspi_main_context == cnx)
1678 if (process_deferred_messages_source != NULL)
1680 g_source_destroy (process_deferred_messages_source);
1681 process_deferred_messages_source = g_idle_source_new ();
1682 g_source_set_callback (process_deferred_messages_source,
1683 process_deferred_messages_callback, NULL, NULL);
1684 g_source_attach (process_deferred_messages_source, cnx);
1685 g_source_unref (process_deferred_messages_source);
1687 atspi_main_context = cnx;
1688 atspi_dbus_connection_setup_with_g_main (atspi_get_a11y_bus (), cnx);
1693 for (i = desktop->children->len - 1; i >= 0; i--)
1695 AtspiAccessible *child = g_ptr_array_index (desktop->children, i);
1696 if (child->parent.app && child->parent.app->bus)
1697 atspi_dbus_connection_setup_with_g_main (child->parent.app->bus, cnx);
1702 #ifdef DEBUG_REF_COUNTS
1704 print_disposed (gpointer key, gpointer value, gpointer data)
1706 AtspiAccessible *accessible = key;
1707 if (accessible->parent.app)
1709 g_print ("disposed: %s %d\n", accessible->name, accessible->role);
1715 g_hash_table_foreach (live_refs, print_disposed, NULL);
1720 _atspi_name_compat (gchar *name)
1734 * atspi_role_get_name:
1735 * @role: an #AtspiRole object to query.
1737 * Gets a localizable string that indicates the name of an #AtspiRole.
1738 * <em>DEPRECATED.</em>
1740 * Returns: a localizable string name for an #AtspiRole enumerated type.
1743 atspi_role_get_name (AtspiRole role)
1745 gchar *retval = NULL;
1746 GTypeClass *type_class;
1749 type_class = g_type_class_ref (ATSPI_TYPE_ROLE);
1750 g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), NULL);
1752 value = g_enum_get_value (G_ENUM_CLASS (type_class), role);
1756 retval = g_strdup (value->value_nick);
1759 g_type_class_unref (type_class);
1762 return _atspi_name_compat (retval);
1768 _atspi_dbus_update_cache_from_dict (AtspiAccessible *accessible, DBusMessageIter *iter)
1770 GHashTable *cache = _atspi_accessible_ref_cache (accessible);
1771 DBusMessageIter iter_dict, iter_dict_entry, iter_struct, iter_variant;
1773 dbus_message_iter_recurse (iter, &iter_dict);
1774 while (dbus_message_iter_get_arg_type (&iter_dict) != DBUS_TYPE_INVALID)
1778 dbus_message_iter_recurse (&iter_dict, &iter_dict_entry);
1779 dbus_message_iter_get_basic (&iter_dict_entry, &key);
1780 dbus_message_iter_next (&iter_dict_entry);
1781 dbus_message_iter_recurse (&iter_dict_entry, &iter_variant);
1782 if (!strcmp (key, "interfaces"))
1784 _atspi_dbus_set_interfaces (accessible, &iter_variant);
1786 else if (!strcmp (key, "Attributes"))
1788 char *iter_sig = dbus_message_iter_get_signature (&iter_variant);
1789 val = g_new0 (GValue, 1);;
1790 g_value_init (val, G_TYPE_HASH_TABLE);
1791 if (strcmp (iter_sig, "a{ss}") != 0)
1793 dbus_free (iter_sig);
1796 dbus_free (iter_sig);
1797 g_value_take_boxed (val, _atspi_dbus_hash_from_iter (&iter_variant));
1799 else if (!strcmp (key, "Component.ScreenExtents"))
1803 char *iter_sig = dbus_message_iter_get_signature (&iter_variant);
1804 val = g_new0 (GValue, 1);;
1805 g_value_init (val, ATSPI_TYPE_RECT);
1806 if (strcmp (iter_sig, "(iiii)") != 0)
1808 dbus_free (iter_sig);
1811 dbus_free (iter_sig);
1812 dbus_message_iter_recurse (&iter_variant, &iter_struct);
1813 dbus_message_iter_get_basic (&iter_struct, &d_int);
1815 dbus_message_iter_next (&iter_struct);
1816 dbus_message_iter_get_basic (&iter_struct, &d_int);
1818 dbus_message_iter_next (&iter_struct);
1819 dbus_message_iter_get_basic (&iter_struct, &d_int);
1820 extents.width = d_int;
1821 dbus_message_iter_next (&iter_struct);
1822 dbus_message_iter_get_basic (&iter_struct, &d_int);
1823 extents.height = d_int;
1824 g_value_set_boxed (val, &extents);
1827 g_hash_table_insert (cache, g_strdup (key), val);
1828 dbus_message_iter_next (&iter_dict);
1835 _atspi_get_allow_sync ()
1841 _atspi_set_allow_sync (gboolean val)
1843 gboolean ret = allow_sync;
1850 _atspi_set_error_no_sync (GError **error)
1852 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_SYNC_NOT_ALLOWED,
1853 _("Attempted synchronous call where prohibited"));
1856 static const char *sr_introspection = "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
1857 "\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
1858 "<node name=\"/org/a11y/atspi/screenreader\">\n"
1859 " <interface name=\"org.a11y.Atspi.ScreenReader\">\n"
1860 " <signal name=\"ReadingPosition\">\n"
1861 " <arg type=\"i\"/>\n"
1862 " <arg type=\"i\"/>\n"
1867 static DBusHandlerResult
1868 screen_reader_filter (DBusConnection *bus, DBusMessage *message, void *user_data)
1870 if (dbus_message_is_method_call (message, DBUS_INTERFACE_INTROSPECTABLE,
1873 DBusMessage *reply = dbus_message_new_method_return (message);
1874 dbus_message_append_args (reply, DBUS_TYPE_STRING, &sr_introspection,
1876 dbus_connection_send (bus, reply, NULL);
1877 dbus_message_unref (reply);
1878 return DBUS_HANDLER_RESULT_HANDLED;
1880 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1884 _atspi_prepare_screen_reader_interface ()
1886 static gint initialized = 0;
1887 DBusConnection *a11y_bus = _atspi_bus ();
1890 return (initialized > 0);
1892 if (dbus_bus_request_name (a11y_bus, "org.a11y.Atspi.ScreenReader", 0, NULL) < 0)
1899 dbus_connection_add_filter (a11y_bus, screen_reader_filter, NULL, NULL);