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 /* This value is not fixed, could be changed. */
40 #define HASH_TABLE_SIZE_MAX 1000
42 static void handle_get_items (DBusPendingCall *pending, void *user_data);
44 static DBusConnection *bus = NULL;
45 static GHashTable *live_refs = NULL;
46 static gint method_call_timeout = 800;
47 static gint app_startup_time = 15000;
48 static gboolean allow_sync = TRUE;
50 GMainLoop *atspi_main_loop;
51 GMainContext *atspi_main_context;
52 gboolean atspi_no_cache;
54 const char *atspi_path_dec = ATSPI_DBUS_PATH_DEC;
55 const char *atspi_path_registry = ATSPI_DBUS_PATH_REGISTRY;
56 const char *atspi_path_root = ATSPI_DBUS_PATH_ROOT;
57 const char *atspi_bus_registry = ATSPI_DBUS_NAME_REGISTRY;
58 const char *atspi_interface_accessible = ATSPI_DBUS_INTERFACE_ACCESSIBLE;
59 const char *atspi_interface_action = ATSPI_DBUS_INTERFACE_ACTION;
60 const char *atspi_interface_application = ATSPI_DBUS_INTERFACE_APPLICATION;
61 const char *atspi_interface_collection = ATSPI_DBUS_INTERFACE_COLLECTION;
62 const char *atspi_interface_component = ATSPI_DBUS_INTERFACE_COMPONENT;
63 const char *atspi_interface_dec = ATSPI_DBUS_INTERFACE_DEC;
64 const char *atspi_interface_device_event_listener = ATSPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER;
65 const char *atspi_interface_document = ATSPI_DBUS_INTERFACE_DOCUMENT;
66 const char *atspi_interface_editable_text = ATSPI_DBUS_INTERFACE_EDITABLE_TEXT;
67 const char *atspi_interface_event_object = ATSPI_DBUS_INTERFACE_EVENT_OBJECT;
68 const char *atspi_interface_hyperlink = ATSPI_DBUS_INTERFACE_HYPERLINK;
69 const char *atspi_interface_hypertext = ATSPI_DBUS_INTERFACE_HYPERTEXT;
70 const char *atspi_interface_image = ATSPI_DBUS_INTERFACE_IMAGE;
71 const char *atspi_interface_registry = ATSPI_DBUS_INTERFACE_REGISTRY;
72 const char *atspi_interface_selection = ATSPI_DBUS_INTERFACE_SELECTION;
73 const char *atspi_interface_table = ATSPI_DBUS_INTERFACE_TABLE;
74 const char *atspi_interface_table_cell = ATSPI_DBUS_INTERFACE_TABLE_CELL;
75 const char *atspi_interface_text = ATSPI_DBUS_INTERFACE_TEXT;
76 const char *atspi_interface_cache = ATSPI_DBUS_INTERFACE_CACHE;
77 const char *atspi_interface_value = ATSPI_DBUS_INTERFACE_VALUE;
79 static const char *interfaces[] =
81 ATSPI_DBUS_INTERFACE_ACCESSIBLE,
82 ATSPI_DBUS_INTERFACE_ACTION,
83 ATSPI_DBUS_INTERFACE_APPLICATION,
84 ATSPI_DBUS_INTERFACE_COLLECTION,
85 ATSPI_DBUS_INTERFACE_COMPONENT,
86 ATSPI_DBUS_INTERFACE_DOCUMENT,
87 ATSPI_DBUS_INTERFACE_EDITABLE_TEXT,
88 ATSPI_DBUS_INTERFACE_HYPERLINK,
89 ATSPI_DBUS_INTERFACE_HYPERTEXT,
90 ATSPI_DBUS_INTERFACE_IMAGE,
91 "org.a11y.atspi.LoginHelper",
92 ATSPI_DBUS_INTERFACE_SELECTION,
93 ATSPI_DBUS_INTERFACE_TABLE,
94 ATSPI_DBUS_INTERFACE_TABLE_CELL,
95 ATSPI_DBUS_INTERFACE_TEXT,
96 ATSPI_DBUS_INTERFACE_VALUE,
101 _atspi_get_iface_num (const char *iface)
103 /* TODO: Use a binary search or hash to improve performance */
106 for (i = 0; interfaces[i]; i++)
108 if (!strcmp(iface, interfaces[i])) return i;
114 _atspi_get_live_refs (void)
118 live_refs = g_hash_table_new (g_direct_hash, g_direct_equal);
123 /* TODO: Add an application parameter */
130 g_error ("AT-SPI: Couldn't connect to accessibility bus. Is at-spi-bus-launcher running?");
134 #define APP_IS_REGISTRY(app) (!strcmp (app->bus_name, atspi_bus_registry))
136 static AtspiAccessible *desktop;
138 static void cleanup_deferred_message (void);
150 g_hash_table_destroy (refs);
153 cleanup_deferred_message();
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);
174 dbus_connection_close (bus);
175 dbus_connection_unref (bus);
180 static gboolean atspi_inited = FALSE;
182 static GHashTable *app_hash = NULL;
185 handle_get_bus_address (DBusPendingCall *pending, void *user_data)
187 AtspiApplication *app = user_data;
188 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
189 DBusMessage *message;
191 DBusPendingCall *new_pending;
194 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
196 if (dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &address,
197 DBUS_TYPE_INVALID) && address [0])
202 dbus_error_init (&error);
203 bus = dbus_connection_open_private (address, &error);
208 dbus_connection_unref (app->bus);
211 atspi_dbus_connection_setup_with_g_main(bus, g_main_context_default());
215 if (!strcmp (error.name, DBUS_ERROR_FILE_NOT_FOUND))
216 g_warning ("AT-SPI: Unable to open bus connection: %s", error.message);
217 dbus_error_free (&error);
221 dbus_message_unref (reply);
222 dbus_pending_call_unref (pending);
225 return; /* application has gone away / been disposed */
227 message = dbus_message_new_method_call (app->bus_name,
228 "/org/a11y/atspi/cache",
229 atspi_interface_cache, "GetItems");
231 result = dbus_connection_send_with_reply (app->bus, message, &new_pending, 2000);
232 dbus_message_unref (message);
233 if (!result || !new_pending)
235 dbus_pending_call_set_notify (new_pending, handle_get_items, app, NULL);
238 static AtspiApplication *
239 get_application (const char *bus_name)
241 AtspiApplication *app = NULL;
243 DBusMessage *message;
244 DBusPendingCall *pending = NULL;
249 app_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_object_unref);
250 if (!app_hash) return NULL;
252 app = g_hash_table_lookup (app_hash, bus_name);
254 bus_name_dup = g_strdup (bus_name);
255 if (!bus_name_dup) return NULL;
256 // TODO: change below to something that will send state-change:defunct notification if necessary */
257 app = _atspi_application_new (bus_name);
258 app->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
259 app->bus = dbus_connection_ref (_atspi_bus ());
260 gettimeofday (&app->time_added, NULL);
261 app->cache = ATSPI_CACHE_UNDEFINED;
262 g_hash_table_insert (app_hash, bus_name_dup, app);
263 message = dbus_message_new_method_call (bus_name, atspi_path_root,
264 atspi_interface_application, "GetApplicationBusAddress");
266 result = dbus_connection_send_with_reply (app->bus, message, &pending, 2000);
267 dbus_message_unref (message);
268 if (!result || !pending)
270 g_hash_table_remove (app_hash, bus_name_dup);
273 dbus_pending_call_set_notify (pending, handle_get_bus_address, app, NULL);
278 ref_accessible (const char *app_name, const char *path)
280 AtspiApplication *app;
283 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
286 app = get_application (app_name);
288 if (!strcmp (path, "/org/a11y/atspi/accessible/root"))
292 app->root = _atspi_accessible_new (app, atspi_path_root);
293 app->root->accessible_parent = atspi_get_desktop (0);
294 g_ptr_array_add (app->root->accessible_parent->children, g_object_ref (app->root));
296 return g_object_ref (app->root);
299 a = g_hash_table_lookup (app->hash, path);
302 return g_object_ref (a);
304 a = _atspi_accessible_new (app, path);
308 if (g_hash_table_size (app->hash) > HASH_TABLE_SIZE_MAX)
310 g_warning ("Accessible hash table is bigger than MAX: %d\n", HASH_TABLE_SIZE_MAX);
311 g_hash_table_remove_all(app->hash);
314 g_hash_table_insert (app->hash, g_strdup (a->parent.path), g_object_ref (a));
318 static AtspiHyperlink *
319 ref_hyperlink (const char *app_name, const char *path)
321 AtspiApplication *app = get_application (app_name);
322 AtspiHyperlink *hyperlink;
324 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
327 hyperlink = g_hash_table_lookup (app->hash, path);
330 return g_object_ref (hyperlink);
332 hyperlink = _atspi_hyperlink_new (app, path);
333 g_hash_table_insert (app->hash, g_strdup (hyperlink->parent.path), hyperlink);
334 /* TODO: This should be a weak ref */
335 g_object_ref (hyperlink); /* for the hash */
348 GArray *state_bitflags;
351 static DBusHandlerResult
352 handle_remove_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
354 const char *sender = dbus_message_get_sender (message);
355 AtspiApplication *app;
357 DBusMessageIter iter, iter_struct;
358 const char *signature = dbus_message_get_signature (message);
361 if (strcmp (signature, "(so)") != 0)
363 g_warning ("AT-SPI: Unknown signature %s for RemoveAccessible", signature);
364 return DBUS_HANDLER_RESULT_HANDLED;
367 dbus_message_iter_init (message, &iter);
368 dbus_message_iter_recurse (&iter, &iter_struct);
369 dbus_message_iter_get_basic (&iter_struct, &sender);
370 dbus_message_iter_next (&iter_struct);
371 dbus_message_iter_get_basic (&iter_struct, &path);
372 app = get_application (sender);
373 a = ref_accessible (sender, path);
375 return DBUS_HANDLER_RESULT_HANDLED;
376 g_object_run_dispose (G_OBJECT (a));
377 g_hash_table_remove (app->hash, a->parent.path);
378 g_object_unref (a); /* unref our own ref */
379 return DBUS_HANDLER_RESULT_HANDLED;
383 remove_accessible_from_app_hash (const char *app_name, AtspiAccessible *a)
385 AtspiApplication *app;
389 app = get_application (app_name);
392 if (!g_hash_table_lookup (app->hash, a->parent.path)) return;
394 g_hash_table_remove (app->hash, a->parent.path);
397 static DBusHandlerResult
398 handle_name_owner_changed (DBusConnection *bus, DBusMessage *message, void *user_data)
400 const char *name, *new, *old;
401 static gboolean registry_lost = FALSE;
403 if (!dbus_message_get_args (message, NULL,
404 DBUS_TYPE_STRING, &name,
405 DBUS_TYPE_STRING, &old,
406 DBUS_TYPE_STRING, &new,
409 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
412 if (!strcmp (name, "org.a11y.atspi.Registry"))
414 if (registry_lost && !old[0])
416 _atspi_reregister_event_listeners ();
417 _atspi_reregister_device_listeners ();
418 registry_lost = FALSE;
421 registry_lost = TRUE;
425 AtspiApplication *app = g_hash_table_lookup (app_hash, old);
426 if (app && app->bus_name && !strcmp(app->bus_name, name))
427 g_object_run_dispose (G_OBJECT (app));
429 return DBUS_HANDLER_RESULT_HANDLED;
433 add_app_to_desktop (AtspiAccessible *a, const char *bus_name)
435 AtspiAccessible *obj = ref_accessible (bus_name, atspi_path_root);
436 /* The app will be added to the desktop as a side-effect of calling
438 g_object_unref (obj);
439 return (obj != NULL);
443 get_reference_from_iter (DBusMessageIter *iter, const char **app_name, const char **path)
445 DBusMessageIter iter_struct;
447 dbus_message_iter_recurse (iter, &iter_struct);
448 dbus_message_iter_get_basic (&iter_struct, app_name);
449 dbus_message_iter_next (&iter_struct);
450 dbus_message_iter_get_basic (&iter_struct, path);
451 dbus_message_iter_next (iter);
455 add_accessible_from_iter (DBusMessageIter *iter)
457 DBusMessageIter iter_struct, iter_array;
458 const char *app_name, *path;
459 AtspiAccessible *accessible;
460 const char *name, *description;
462 gboolean children_cached = FALSE;
463 dbus_int32_t count, index;
465 dbus_message_iter_recurse (iter, &iter_struct);
468 get_reference_from_iter (&iter_struct, &app_name, &path);
469 accessible = ref_accessible (app_name, path);
473 /* Get application: TODO */
474 dbus_message_iter_next (&iter_struct);
477 get_reference_from_iter (&iter_struct, &app_name, &path);
478 if (accessible->accessible_parent)
479 g_object_unref (accessible->accessible_parent);
480 accessible->accessible_parent = ref_accessible (app_name, path);
482 if (dbus_message_iter_get_arg_type (&iter_struct) == 'i')
484 /* Get index in parent */
485 dbus_message_iter_get_basic (&iter_struct, &index);
486 if (index >= 0 && accessible->accessible_parent)
488 if (index >= accessible->accessible_parent->children->len) {
489 /* There is no room for this object */
490 g_ptr_array_set_size (accessible->accessible_parent->children, index + 1);
492 /* This place is already taken - let's free this place with dignity */
493 g_object_unref (g_ptr_array_index (accessible->accessible_parent->children, index));
495 g_ptr_array_index (accessible->accessible_parent->children, index) = g_object_ref (accessible);
498 /* get child count */
499 dbus_message_iter_next (&iter_struct);
500 dbus_message_iter_get_basic (&iter_struct, &count);
503 g_ptr_array_set_size (accessible->children, count);
504 children_cached = TRUE;
507 else if (dbus_message_iter_get_arg_type (&iter_struct) == 'a')
509 /* It's the old API with a list of children */
510 /* TODO: Perhaps remove this code eventually */
511 dbus_message_iter_recurse (&iter_struct, &iter_array);
512 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
514 AtspiAccessible *child;
515 get_reference_from_iter (&iter_array, &app_name, &path);
516 child = ref_accessible (app_name, path);
517 g_ptr_array_remove (accessible->children, child);
518 g_ptr_array_add (accessible->children, child);
520 children_cached = TRUE;
524 dbus_message_iter_next (&iter_struct);
525 _atspi_dbus_set_interfaces (accessible, &iter_struct);
526 dbus_message_iter_next (&iter_struct);
529 if (accessible->name)
530 g_free (accessible->name);
531 dbus_message_iter_get_basic (&iter_struct, &name);
532 accessible->name = g_strdup (name);
533 dbus_message_iter_next (&iter_struct);
536 dbus_message_iter_get_basic (&iter_struct, &role);
537 accessible->role = role;
538 dbus_message_iter_next (&iter_struct);
541 if (accessible->description)
542 g_free (accessible->description);
543 dbus_message_iter_get_basic (&iter_struct, &description);
544 accessible->description = g_strdup (description);
545 dbus_message_iter_next (&iter_struct);
547 _atspi_dbus_set_state (accessible, &iter_struct);
548 dbus_message_iter_next (&iter_struct);
550 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_NAME | ATSPI_CACHE_ROLE |
551 ATSPI_CACHE_PARENT | ATSPI_CACHE_DESCRIPTION);
552 if (!atspi_state_set_contains (accessible->states,
553 ATSPI_STATE_MANAGES_DESCENDANTS) &&
555 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_CHILDREN);
557 /* This is a bit of a hack since the cache holds a ref, so we don't need
558 * the one provided for us anymore */
559 g_object_unref (accessible);
563 handle_get_items (DBusPendingCall *pending, void *user_data)
565 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
566 DBusMessageIter iter, iter_array;
568 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
570 const char *sender = dbus_message_get_sender (reply);
571 const char *error = NULL;
572 const char *error_name = dbus_message_get_error_name (reply);
573 if (!strcmp (error_name, DBUS_ERROR_SERVICE_UNKNOWN)
574 || !strcmp (error_name, DBUS_ERROR_NO_REPLY))
579 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &error,
581 g_warning ("AT-SPI: Error in GetItems, sender=%s, error=%s", sender, error);
583 dbus_message_unref (reply);
584 dbus_pending_call_unref (pending);
588 dbus_message_iter_init (reply, &iter);
589 dbus_message_iter_recurse (&iter, &iter_array);
590 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
592 add_accessible_from_iter (&iter_array);
593 dbus_message_iter_next (&iter_array);
595 dbus_message_unref (reply);
596 dbus_pending_call_unref (pending);
599 /* TODO: Do we stil need this function? */
600 static AtspiAccessible *
601 ref_accessible_desktop (AtspiApplication *app)
604 DBusMessage *message, *reply;
605 DBusMessageIter iter, iter_array;
610 g_object_ref (desktop);
613 desktop = _atspi_accessible_new (app, atspi_path_root);
618 g_hash_table_insert (app->hash, g_strdup (desktop->parent.path),
619 g_object_ref (desktop));
620 app->root = g_object_ref (desktop);
621 desktop->name = g_strdup ("main");
622 message = dbus_message_new_method_call (atspi_bus_registry,
624 atspi_interface_accessible,
629 reply = _atspi_dbus_send_with_reply_and_block (message, &error);
630 if (!reply || strcmp (dbus_message_get_signature (reply), "a(so)") != 0)
634 g_warning ("Couldn't get application list: %s", error->message);
635 g_clear_error (&error);
638 dbus_message_unref (reply);
641 dbus_message_iter_init (reply, &iter);
642 dbus_message_iter_recurse (&iter, &iter_array);
643 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
645 const char *app_name, *path;
646 get_reference_from_iter (&iter_array, &app_name, &path);
647 add_app_to_desktop (desktop, app_name);
650 /* Record the alternate name as an alias for org.a11y.atspi.Registry */
651 bus_name_dup = g_strdup (dbus_message_get_sender (reply));
653 g_hash_table_insert (app_hash, bus_name_dup, app);
655 dbus_message_unref (reply);
657 return g_object_ref (desktop);
661 _atspi_ref_accessible (const char *app, const char *path)
663 AtspiApplication *a = get_application (app);
666 if ( APP_IS_REGISTRY(a))
669 g_object_unref (ref_accessible_desktop (a)); /* sets a->root */
670 return g_object_ref (a->root);
672 return ref_accessible (app, path);
676 _atspi_dbus_return_accessible_from_message (DBusMessage *message)
678 DBusMessageIter iter;
679 AtspiAccessible *retval = NULL;
680 const char *signature;
685 signature = dbus_message_get_signature (message);
686 if (!strcmp (signature, "(so)"))
688 dbus_message_iter_init (message, &iter);
689 retval = _atspi_dbus_return_accessible_from_iter (&iter);
693 g_warning ("AT-SPI: Called _atspi_dbus_return_accessible_from_message with strange signature %s", signature);
695 dbus_message_unref (message);
700 _atspi_dbus_return_accessible_from_iter (DBusMessageIter *iter)
702 const char *app_name, *path;
704 get_reference_from_iter (iter, &app_name, &path);
705 return ref_accessible (app_name, path);
709 _atspi_dbus_return_hyperlink_from_message (DBusMessage *message)
711 DBusMessageIter iter;
712 AtspiHyperlink *retval = NULL;
713 const char *signature;
718 signature = dbus_message_get_signature (message);
719 if (!strcmp (signature, "(so)"))
721 dbus_message_iter_init (message, &iter);
722 retval = _atspi_dbus_return_hyperlink_from_iter (&iter);
726 g_warning ("AT-SPI: Called _atspi_dbus_return_hyperlink_from_message with strange signature %s", signature);
728 dbus_message_unref (message);
733 _atspi_dbus_return_hyperlink_from_iter (DBusMessageIter *iter)
735 const char *app_name, *path;
737 get_reference_from_iter (iter, &app_name, &path);
738 return ref_hyperlink (app_name, path);
741 const char *cache_signal_type = "((so)(so)(so)iiassusau)";
742 const char *old_cache_signal_type = "((so)(so)(so)a(so)assusau)";
744 static DBusHandlerResult
745 handle_add_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
747 DBusMessageIter iter;
748 const char *signature = dbus_message_get_signature (message);
750 if (strcmp (signature, cache_signal_type) != 0 &&
751 strcmp (signature, old_cache_signal_type) != 0)
753 g_warning ("AT-SPI: AddAccessible with unknown signature %s\n", signature);
754 return DBUS_HANDLER_RESULT_HANDLED;
757 dbus_message_iter_init (message, &iter);
758 add_accessible_from_iter (&iter);
759 return DBUS_HANDLER_RESULT_HANDLED;
765 DBusMessage *message;
769 static GSource *process_deferred_messages_source = NULL;
772 process_deferred_message (BusDataClosure *closure)
774 int type = dbus_message_get_type (closure->message);
775 const char *interface = dbus_message_get_interface (closure->message);
777 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
778 !strncmp (interface, "org.a11y.atspi.Event.", 21))
780 _atspi_dbus_handle_event (closure->bus, closure->message, closure->data);
782 if (dbus_message_is_method_call (closure->message, atspi_interface_device_event_listener, "NotifyEvent"))
784 _atspi_dbus_handle_DeviceEvent (closure->bus,
785 closure->message, closure->data);
787 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "AddAccessible"))
789 handle_add_accessible (closure->bus, closure->message, closure->data);
791 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "RemoveAccessible"))
793 handle_remove_accessible (closure->bus, closure->message, closure->data);
795 if (dbus_message_is_signal (closure->message, "org.freedesktop.DBus", "NameOwnerChanged"))
797 handle_name_owner_changed (closure->bus, closure->message, closure->data);
801 static GQueue *deferred_messages = NULL;
804 destroy_deferred_message_item(gpointer ptr)
806 /* TODO this is still memory leak on c->data */
807 BusDataClosure *c = ptr;
808 dbus_message_unref (c->message);
809 dbus_connection_unref (c->bus);
814 cleanup_deferred_message(void)
816 g_queue_free_full(deferred_messages, destroy_deferred_message_item);
817 deferred_messages = NULL;
821 process_deferred_messages (void)
823 static int in_process_deferred_messages = 0;
824 BusDataClosure *closure;
826 if (in_process_deferred_messages)
828 in_process_deferred_messages = 1;
829 while ((closure = g_queue_pop_head (deferred_messages)))
831 process_deferred_message (closure);
832 dbus_message_unref (closure->message);
833 dbus_connection_unref (closure->bus);
836 in_process_deferred_messages = 0;
841 process_deferred_messages_callback (gpointer data)
843 if (process_deferred_messages ())
844 return G_SOURCE_CONTINUE;
846 process_deferred_messages_source = NULL;
847 return G_SOURCE_REMOVE;
850 static DBusHandlerResult
851 defer_message (DBusConnection *connection, DBusMessage *message, void *user_data)
853 BusDataClosure *closure = g_new (BusDataClosure, 1);
855 closure->bus = dbus_connection_ref (bus);
856 closure->message = dbus_message_ref (message);
857 closure->data = user_data;
859 g_queue_push_tail (deferred_messages, closure);
861 if (process_deferred_messages_source == NULL)
863 process_deferred_messages_source = g_idle_source_new ();
864 g_source_set_callback (process_deferred_messages_source,
865 process_deferred_messages_callback, NULL, NULL);
866 g_source_attach (process_deferred_messages_source, atspi_main_context);
867 g_source_unref (process_deferred_messages_source);
870 return DBUS_HANDLER_RESULT_HANDLED;
873 static DBusHandlerResult
874 atspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
876 int type = dbus_message_get_type (message);
877 const char *interface = dbus_message_get_interface (message);
879 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
880 !strncmp (interface, "org.a11y.atspi.Event.", 21))
882 return defer_message (bus, message, data);
884 if (dbus_message_is_method_call (message, atspi_interface_device_event_listener, "NotifyEvent"))
886 return defer_message (bus, message, data);
888 if (dbus_message_is_signal (message, atspi_interface_cache, "AddAccessible"))
890 return defer_message (bus, message, data);
892 if (dbus_message_is_signal (message, atspi_interface_cache, "RemoveAccessible"))
894 return defer_message (bus, message, data);
896 if (dbus_message_is_signal (message, "org.freedesktop.DBus", "NameOwnerChanged"))
898 defer_message (bus, message, data);
899 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
901 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
905 * Returns a 'canonicalized' value for DISPLAY,
906 * with the screen number stripped off if present.
908 * TODO: Avoid having duplicate functions for this here and in at-spi2-atk
911 spi_display_name (void)
913 char *canonical_display_name = NULL;
914 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
918 display_env = g_getenv ("DISPLAY");
919 if (!display_env || !display_env[0])
923 gchar *display_p, *screen_p;
924 canonical_display_name = g_strdup (display_env);
925 display_p = g_utf8_strrchr (canonical_display_name, -1, ':');
926 screen_p = g_utf8_strrchr (canonical_display_name, -1, '.');
927 if (screen_p && display_p && (screen_p > display_p))
935 canonical_display_name = g_strdup (display_env);
938 return canonical_display_name;
944 * Connects to the accessibility registry and initializes the SPI.
946 * Returns: 0 on success, 1 if already initialized, or an integer error code.
952 const gchar *no_cache;
961 _atspi_get_live_refs();
963 bus = atspi_get_a11y_bus ();
966 if (!dbus_bus_register (bus, NULL))
968 atspi_dbus_connection_setup_with_g_main(bus, g_main_context_default());
969 dbus_connection_add_filter (bus, atspi_dbus_filter, NULL, NULL);
970 match = g_strdup_printf ("type='signal',interface='%s',member='AddAccessible'", atspi_interface_cache);
971 dbus_bus_add_match (bus, match, NULL);
973 match = g_strdup_printf ("type='signal',interface='%s',member='RemoveAccessible'", atspi_interface_cache);
974 dbus_bus_add_match (bus, match, NULL);
976 match = g_strdup_printf ("type='signal',interface='%s',member='ChildrenChanged'", atspi_interface_event_object);
977 dbus_bus_add_match (bus, match, NULL);
979 match = g_strdup_printf ("type='signal',interface='%s',member='PropertyChange'", atspi_interface_event_object);
980 dbus_bus_add_match (bus, match, NULL);
982 match = g_strdup_printf ("type='signal',interface='%s',member='StateChanged'", atspi_interface_event_object);
983 dbus_bus_add_match (bus, match, NULL);
986 dbus_bus_add_match (bus,
987 "type='signal', interface='org.freedesktop.DBus', member='NameOwnerChanged'",
990 no_cache = g_getenv ("ATSPI_NO_CACHE");
991 if (no_cache && g_strcmp0 (no_cache, "0") != 0)
992 atspi_no_cache = TRUE;
994 deferred_messages = g_queue_new ();
1000 * atspi_is_initialized:
1002 * Indicates whether AT-SPI has been initialized.
1004 * Returns: %True if initialized; %False otherwise.
1007 atspi_is_initialized ()
1009 return atspi_inited;
1015 * Starts/enters the main event loop for the AT-SPI services.
1017 * NOTE: This method does not return control; it is exited via a call to
1018 * #atspi_event_quit from within an event handler.
1022 atspi_event_main (void)
1024 atspi_main_loop = g_main_loop_new (NULL, FALSE);
1025 g_main_loop_run (atspi_main_loop);
1026 g_main_loop_unref(atspi_main_loop);
1027 atspi_main_loop = NULL;
1033 * Quits the last main event loop for the AT-SPI services,
1034 * See: #atspi_event_main
1037 atspi_event_quit (void)
1039 g_main_loop_quit (atspi_main_loop);
1045 * Disconnects from #AtspiRegistry instances and releases
1046 * any floating resources. Call only once at exit.
1048 * Returns: 0 if there were no leaks, otherwise other integer values.
1060 atspi_inited = FALSE;
1064 leaked = g_hash_table_size (live_refs);
1076 static GSList *hung_processes;
1079 remove_hung_process (DBusPendingCall *pending, void *data)
1081 hung_processes = g_slist_remove (hung_processes, data);
1083 dbus_pending_call_unref (pending);
1087 check_for_hang (DBusMessage *message, DBusError *error, DBusConnection *bus, const char *bus_name)
1089 if (!message && error->name &&
1090 !strcmp (error->name, "org.freedesktop.DBus.Error.NoReply"))
1093 DBusMessage *message;
1094 gchar *bus_name_dup;
1095 DBusPendingCall *pending = NULL;
1097 for (l = hung_processes; l; l = l->next)
1098 if (!strcmp (l->data, bus_name))
1100 message = dbus_message_new_method_call (bus_name, "/",
1101 "org.freedesktop.DBus.Peer",
1105 result = dbus_connection_send_with_reply (bus, message, &pending, -1);
1106 dbus_message_unref (message);
1107 if (!result || !pending)
1109 bus_name_dup = g_strdup (bus_name);
1110 hung_processes = g_slist_append (hung_processes, bus_name_dup);
1111 dbus_pending_call_set_notify (pending, remove_hung_process, bus_name_dup, NULL);
1116 connection_is_hung (const char *bus_name)
1120 for (l = hung_processes; l; l = l->next)
1121 if (!strcmp (l->data, bus_name))
1127 check_app (AtspiApplication *app, GError **error)
1129 if (!app || !app->bus)
1131 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
1132 _("The application no longer exists"));
1136 if (atspi_main_loop && connection_is_hung (app->bus_name))
1138 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC,
1139 "The process appears to be hung.");
1147 set_timeout (AtspiApplication *app)
1152 if (app && app_startup_time > 0)
1154 gettimeofday (&tv, NULL);
1155 diff = (tv.tv_sec - app->time_added.tv_sec) * 1000 + (tv.tv_usec - app->time_added.tv_usec) / 1000;
1156 dbind_set_timeout (MAX(method_call_timeout, app_startup_time - diff));
1159 dbind_set_timeout (method_call_timeout);
1163 _atspi_dbus_call (gpointer obj, const char *interface, const char *method, GError **error, const char *type, ...)
1168 AtspiObject *aobj = ATSPI_OBJECT (obj);
1170 if (!check_app (aobj->app, error))
1175 _atspi_set_error_no_sync (error);
1179 va_start (args, type);
1180 dbus_error_init (&err);
1181 set_timeout (aobj->app);
1182 retval = dbind_method_call_reentrant_va (aobj->app->bus, aobj->app->bus_name,
1183 aobj->path, interface, method, &err,
1186 check_for_hang (NULL, &err, aobj->app->bus, aobj->app->bus_name);
1187 process_deferred_messages ();
1188 if (dbus_error_is_set (&err))
1190 g_set_error(error, ATSPI_ERROR, ATSPI_ERROR_IPC, "%s", err.message);
1191 dbus_error_free (&err);
1197 _atspi_dbus_call_partial (gpointer obj,
1198 const char *interface,
1201 const char *type, ...)
1204 DBusMessage * result;
1205 va_start (args, type);
1206 result = _atspi_dbus_call_partial_va (obj, interface, method, error, type, args);
1213 _atspi_dbus_call_partial_va (gpointer obj,
1214 const char *interface,
1220 AtspiObject *aobj = ATSPI_OBJECT (obj);
1222 DBusMessage *msg = NULL, *reply = NULL;
1223 DBusMessageIter iter;
1226 dbus_error_init (&err);
1228 if (!check_app (aobj->app, error))
1231 msg = dbus_message_new_method_call (aobj->app->bus_name, aobj->path, interface, method);
1236 dbus_message_iter_init_append (msg, &iter);
1237 dbind_any_marshal_va (&iter, &p, args);
1239 set_timeout (aobj->app);
1240 reply = dbind_send_and_allow_reentry (aobj->app->bus, msg, &err);
1241 check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1245 dbus_message_unref (msg);
1246 process_deferred_messages ();
1247 if (dbus_error_is_set (&err))
1249 g_set_error_literal(error, ATSPI_ERROR, ATSPI_ERROR_IPC, err.message);
1250 dbus_error_free (&err);
1252 dbus_message_unref(reply);
1255 else if (reply && dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1257 const char *err_str = NULL;
1258 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &err_str, DBUS_TYPE_INVALID);
1260 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err_str);
1261 dbus_message_unref (reply);
1268 _atspi_dbus_get_property (gpointer obj, const char *interface, const char *name, GError **error, const char *type, void *data)
1270 DBusMessage *message, *reply;
1271 DBusMessageIter iter, iter_variant;
1273 dbus_bool_t retval = FALSE;
1274 AtspiObject *aobj = ATSPI_OBJECT (obj);
1275 char expected_type = (type [0] == '(' ? 'r' : type [0]);
1280 if (!check_app (aobj->app, error))
1285 _atspi_set_error_no_sync (error);
1289 message = dbus_message_new_method_call (aobj->app->bus_name,
1291 "org.freedesktop.DBus.Properties",
1295 // TODO: throw exception
1298 dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1299 dbus_error_init (&err);
1300 set_timeout (aobj->app);
1301 reply = dbind_send_and_allow_reentry (aobj->app->bus, message, &err);
1302 check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1303 dbus_message_unref (message);
1304 process_deferred_messages ();
1307 // TODO: throw exception
1311 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
1313 const char *err_str = NULL;
1314 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &err_str, DBUS_TYPE_INVALID);
1316 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err_str);
1320 dbus_message_iter_init (reply, &iter);
1321 if (dbus_message_iter_get_arg_type (&iter) != 'v')
1323 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));
1326 dbus_message_iter_recurse (&iter, &iter_variant);
1327 if (dbus_message_iter_get_arg_type (&iter_variant) != expected_type)
1329 g_warning ("atspi_dbus_get_property: Wrong type: expected %s, got %c\n", type, dbus_message_iter_get_arg_type (&iter_variant));
1332 if (!strcmp (type, "(so)"))
1334 g_object_unref(*(AtspiAccessible**)data);
1335 *((AtspiAccessible **)data) = _atspi_dbus_return_accessible_from_iter (&iter_variant);
1339 if (type [0] == 's')
1341 g_free(*(char**)data);
1342 *(char**) data = NULL;
1345 dbus_message_iter_get_basic (&iter_variant, data);
1347 if (type [0] == 's')
1348 *(char **)data = g_strdup (*(char **)data);
1352 dbus_error_free (&err);
1354 dbus_message_unref (reply);
1359 _atspi_dbus_send_with_reply_and_block (DBusMessage *message, GError **error)
1363 AtspiApplication *app;
1364 DBusConnection *bus;
1366 app = get_application (dbus_message_get_destination (message));
1368 if (app && !app->bus)
1369 return NULL; /* will fail anyway; app has been disposed */
1371 bus = (app ? app->bus : _atspi_bus());
1372 dbus_error_init (&err);
1374 reply = dbind_send_and_allow_reentry (bus, message, &err);
1375 process_deferred_messages ();
1376 dbus_message_unref (message);
1377 if (dbus_error_is_set (&err))
1380 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err.message);
1381 dbus_error_free (&err);
1387 _atspi_dbus_return_hash_from_message (DBusMessage *message)
1389 DBusMessageIter iter;
1395 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1397 dbus_message_iter_init (message, &iter);
1398 ret = _atspi_dbus_hash_from_iter (&iter);
1399 dbus_message_unref (message);
1404 _atspi_dbus_hash_from_iter (DBusMessageIter *iter)
1406 GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
1407 (GDestroyNotify) g_free,
1408 (GDestroyNotify) g_free);
1409 DBusMessageIter iter_array, iter_dict;
1411 dbus_message_iter_recurse (iter, &iter_array);
1412 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1414 const char *name, *value;
1415 dbus_message_iter_recurse (&iter_array, &iter_dict);
1416 dbus_message_iter_get_basic (&iter_dict, &name);
1417 dbus_message_iter_next (&iter_dict);
1418 dbus_message_iter_get_basic (&iter_dict, &value);
1419 g_hash_table_insert (hash, g_strdup (name), g_strdup (value));
1420 dbus_message_iter_next (&iter_array);
1426 _atspi_dbus_return_attribute_array_from_message (DBusMessage *message)
1428 DBusMessageIter iter;
1434 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1436 dbus_message_iter_init (message, &iter);
1438 ret = _atspi_dbus_attribute_array_from_iter (&iter);
1439 dbus_message_unref (message);
1444 _atspi_dbus_attribute_array_from_iter (DBusMessageIter *iter)
1446 DBusMessageIter iter_array, iter_dict;
1447 GArray *array = g_array_new (TRUE, TRUE, sizeof (gchar *));
1449 dbus_message_iter_recurse (iter, &iter_array);
1450 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1452 const char *name, *value;
1454 dbus_message_iter_recurse (&iter_array, &iter_dict);
1455 dbus_message_iter_get_basic (&iter_dict, &name);
1456 dbus_message_iter_next (&iter_dict);
1457 dbus_message_iter_get_basic (&iter_dict, &value);
1458 str = g_strdup_printf ("%s:%s", name, value);
1459 array = g_array_append_val (array, str);
1460 dbus_message_iter_next (&iter_array);;
1466 _atspi_dbus_set_interfaces (AtspiAccessible *accessible, DBusMessageIter *iter)
1468 DBusMessageIter iter_array;
1469 char *iter_sig = dbus_message_iter_get_signature (iter);
1471 accessible->interfaces = 0;
1472 if (strcmp (iter_sig, "as") != 0)
1474 g_warning ("_atspi_dbus_set_interfaces: Passed iterator with invalid signature %s", iter_sig);
1475 dbus_free (iter_sig);
1478 dbus_free (iter_sig);
1479 dbus_message_iter_recurse (iter, &iter_array);
1480 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1484 dbus_message_iter_get_basic (&iter_array, &iface);
1485 if (!strcmp (iface, "org.freedesktop.DBus.Introspectable")) continue;
1486 n = _atspi_get_iface_num (iface);
1489 g_warning ("AT-SPI: Unknown interface %s", iface);
1492 accessible->interfaces |= (1 << n);
1493 dbus_message_iter_next (&iter_array);
1495 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_INTERFACES);
1499 _atspi_dbus_set_state (AtspiAccessible *accessible, DBusMessageIter *iter)
1501 DBusMessageIter iter_array;
1503 dbus_uint32_t *states;
1505 dbus_message_iter_recurse (iter, &iter_array);
1506 dbus_message_iter_get_fixed_array (&iter_array, &states, &count);
1509 g_warning ("AT-SPI: expected 2 values in states array; got %d\n", count);
1510 if (!accessible->states)
1511 accessible->states = _atspi_state_set_new_internal (accessible, 0);
1515 guint64 val = ((guint64)states [1]) << 32;
1517 if (!accessible->states)
1518 accessible->states = _atspi_state_set_new_internal (accessible, val);
1520 accessible->states->states = val;
1522 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_STATES);
1526 _atspi_error_quark (void)
1528 return g_quark_from_static_string ("atspi_error");
1532 * Gets the IOR from the XDisplay.
1536 get_accessibility_bus_address_x11 (void)
1540 Display *bridge_display = NULL;
1543 unsigned char *data_x11 = NULL;
1544 unsigned long nitems;
1545 unsigned long leftover;
1548 display_name = spi_display_name ();
1552 bridge_display = XOpenDisplay (display_name);
1553 g_free (display_name);
1555 if (!bridge_display)
1557 g_warning ("Could not open X display");
1561 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
1562 XGetWindowProperty (bridge_display,
1563 XDefaultRootWindow (bridge_display),
1565 (long) BUFSIZ, False,
1566 (Atom) 31, &actual_type, &actual_format,
1567 &nitems, &leftover, &data_x11);
1568 XCloseDisplay (bridge_display);
1570 data = g_strdup ((gchar *)data_x11);
1577 get_accessibility_bus_address_dbus (void)
1579 DBusConnection *session_bus = NULL;
1580 DBusMessage *message;
1583 char *address = NULL;
1585 session_bus = dbus_bus_get (DBUS_BUS_SESSION, NULL);
1589 message = dbus_message_new_method_call ("org.a11y.Bus",
1594 dbus_error_init (&error);
1595 reply = dbus_connection_send_with_reply_and_block (session_bus,
1599 dbus_message_unref (message);
1603 g_warning ("AT-SPI: Error retrieving accessibility bus address: %s: %s",
1604 error.name, error.message);
1605 dbus_error_free (&error);
1610 const char *tmp_address;
1611 if (!dbus_message_get_args (reply,
1617 dbus_message_unref (reply);
1620 address = g_strdup (tmp_address);
1621 dbus_message_unref (reply);
1625 dbus_connection_unref (session_bus);
1629 static DBusConnection *a11y_bus;
1630 static dbus_int32_t a11y_dbus_slot = -1;
1633 a11y_bus_free (void *data)
1635 if (data == a11y_bus)
1638 dbus_connection_free_data_slot (&a11y_dbus_slot);
1643 * atspi_get_a11y_bus: (skip)
1646 atspi_get_a11y_bus (void)
1649 char *address = NULL;
1650 const char *address_env = NULL;
1652 if (a11y_bus && dbus_connection_get_is_connected (a11y_bus))
1655 if (a11y_dbus_slot == -1)
1656 if (!dbus_connection_allocate_data_slot (&a11y_dbus_slot))
1657 g_warning ("AT-SPI: Unable to allocate D-Bus slot");
1659 address_env = g_getenv ("AT_SPI_BUS_ADDRESS");
1660 if (address_env != NULL && *address_env != 0)
1661 address = g_strdup (address_env);
1663 if (!address && g_getenv ("DISPLAY") != NULL &&
1664 g_getenv ("WAYLAND_DISPLAY") == NULL)
1665 address = get_accessibility_bus_address_x11 ();
1668 address = get_accessibility_bus_address_dbus ();
1672 dbus_error_init (&error);
1673 a11y_bus = dbus_connection_open_private (address, &error);
1678 if (!g_getenv("SSH_CONNECTION"))
1679 g_warning ("Couldn't connect to accessibility bus: %s", error.message);
1680 dbus_error_free (&error);
1685 if (!dbus_bus_register (a11y_bus, &error))
1687 g_warning ("Couldn't register with accessibility bus: %s", error.message);
1688 dbus_error_free (&error);
1689 dbus_connection_close (a11y_bus);
1690 dbus_connection_unref (a11y_bus);
1696 /* Simulate a weak ref on the bus */
1697 dbus_connection_set_data (a11y_bus, a11y_dbus_slot, a11y_bus, a11y_bus_free);
1703 * atspi_set_timeout:
1704 * @val: The timeout value, in milliseconds, or -1 to disable the timeout.
1705 * @startup_time: The amount of time, in milliseconds, to allow to pass
1706 * before enforcing timeouts on an application. Can be used to prevent
1707 * timeout exceptions if an application is likely to block for an extended
1708 * period of time on initialization. -1 can be passed to disable this
1711 * Set the timeout used for method calls. If this is not set explicitly,
1712 * a default of 0.8 ms is used.
1713 * Note that at-spi2-registryd currently uses a timeout of 3 seconds when
1714 * sending a keyboard event notification. This means that, if an AT makes
1715 * a call in response to the keyboard notification and the application
1716 * being called does not respond before the timeout is reached,
1717 * at-spi2-registryd will time out on the keyboard event notification and
1718 * pass the key onto the application (ie, reply to indicate that the key
1719 * was not consumed), so this may make it undesirable to set a timeout
1720 * larger than 3 seconds.
1722 * By default, the normal timeout is set to 800 ms, and the application startup
1723 * timeout is set to 15 seconds.
1726 atspi_set_timeout (gint val, gint startup_time)
1728 method_call_timeout = val;
1729 app_startup_time = startup_time;
1733 * atspi_set_main_context:
1734 * @cnx: The #GMainContext to use.
1736 * Sets the main loop context that AT-SPI should assume is in use when
1737 * setting an idle callback.
1738 * This function should be called by application-side implementors (ie,
1739 * at-spi2-atk) when it is desirable to re-enter the main loop.
1742 atspi_set_main_context (GMainContext *cnx)
1744 if (atspi_main_context == cnx)
1746 if (process_deferred_messages_source != NULL)
1748 g_source_destroy (process_deferred_messages_source);
1749 process_deferred_messages_source = g_idle_source_new ();
1750 g_source_set_callback (process_deferred_messages_source,
1751 process_deferred_messages_callback, NULL, NULL);
1752 g_source_attach (process_deferred_messages_source, cnx);
1753 g_source_unref (process_deferred_messages_source);
1755 atspi_main_context = cnx;
1756 atspi_dbus_connection_setup_with_g_main (atspi_get_a11y_bus (), cnx);
1761 for (i = desktop->children->len - 1; i >= 0; i--)
1763 AtspiAccessible *child = g_ptr_array_index (desktop->children, i);
1764 if (child->parent.app && child->parent.app->bus)
1765 atspi_dbus_connection_setup_with_g_main (child->parent.app->bus, cnx);
1770 #ifdef DEBUG_REF_COUNTS
1772 print_disposed (gpointer key, gpointer value, gpointer data)
1774 AtspiAccessible *accessible = key;
1775 if (accessible->parent.app)
1777 g_print ("disposed: %s %d\n", accessible->name, accessible->role);
1783 g_hash_table_foreach (live_refs, print_disposed, NULL);
1788 _atspi_name_compat (gchar *name)
1802 * atspi_role_get_name:
1803 * @role: an #AtspiRole object to query.
1805 * Gets a localizable string that indicates the name of an #AtspiRole.
1806 * <em>DEPRECATED.</em>
1808 * Returns: a localizable string name for an #AtspiRole enumerated type.
1811 atspi_role_get_name (AtspiRole role)
1813 gchar *retval = NULL;
1814 GTypeClass *type_class;
1817 type_class = g_type_class_ref (ATSPI_TYPE_ROLE);
1818 g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), NULL);
1820 value = g_enum_get_value (G_ENUM_CLASS (type_class), role);
1824 retval = g_strdup (value->value_nick);
1827 g_type_class_unref (type_class);
1830 return _atspi_name_compat (retval);
1836 _atspi_dbus_update_cache_from_dict (AtspiAccessible *accessible, DBusMessageIter *iter)
1838 GHashTable *cache = _atspi_accessible_ref_cache (accessible);
1839 DBusMessageIter iter_dict, iter_dict_entry, iter_struct, iter_variant;
1841 dbus_message_iter_recurse (iter, &iter_dict);
1842 while (dbus_message_iter_get_arg_type (&iter_dict) != DBUS_TYPE_INVALID)
1846 dbus_message_iter_recurse (&iter_dict, &iter_dict_entry);
1847 dbus_message_iter_get_basic (&iter_dict_entry, &key);
1848 dbus_message_iter_next (&iter_dict_entry);
1849 dbus_message_iter_recurse (&iter_dict_entry, &iter_variant);
1850 if (!strcmp (key, "interfaces"))
1852 _atspi_dbus_set_interfaces (accessible, &iter_variant);
1854 else if (!strcmp (key, "Attributes"))
1856 char *iter_sig = dbus_message_iter_get_signature (&iter_variant);
1857 val = g_new0 (GValue, 1);;
1858 g_value_init (val, G_TYPE_HASH_TABLE);
1859 if (strcmp (iter_sig, "a{ss}") != 0)
1861 dbus_free (iter_sig);
1864 dbus_free (iter_sig);
1865 g_value_take_boxed (val, _atspi_dbus_hash_from_iter (&iter_variant));
1867 else if (!strcmp (key, "Component.ScreenExtents"))
1871 char *iter_sig = dbus_message_iter_get_signature (&iter_variant);
1872 val = g_new0 (GValue, 1);;
1873 g_value_init (val, ATSPI_TYPE_RECT);
1874 if (strcmp (iter_sig, "(iiii)") != 0)
1876 dbus_free (iter_sig);
1879 dbus_free (iter_sig);
1880 dbus_message_iter_recurse (&iter_variant, &iter_struct);
1881 dbus_message_iter_get_basic (&iter_struct, &d_int);
1883 dbus_message_iter_next (&iter_struct);
1884 dbus_message_iter_get_basic (&iter_struct, &d_int);
1886 dbus_message_iter_next (&iter_struct);
1887 dbus_message_iter_get_basic (&iter_struct, &d_int);
1888 extents.width = d_int;
1889 dbus_message_iter_next (&iter_struct);
1890 dbus_message_iter_get_basic (&iter_struct, &d_int);
1891 extents.height = d_int;
1892 g_value_set_boxed (val, &extents);
1895 g_hash_table_insert (cache, g_strdup (key), val);
1896 dbus_message_iter_next (&iter_dict);
1903 _atspi_get_allow_sync ()
1909 _atspi_set_allow_sync (gboolean val)
1911 gboolean ret = allow_sync;
1918 _atspi_set_error_no_sync (GError **error)
1920 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_SYNC_NOT_ALLOWED,
1921 _("Attempted synchronous call where prohibited"));
1924 static const char *sr_introspection = "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
1925 "\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
1926 "<node name=\"/org/a11y/atspi/screenreader\">\n"
1927 " <interface name=\"org.a11y.Atspi.ScreenReader\">\n"
1928 " <signal name=\"ReadingPosition\">\n"
1929 " <arg type=\"i\"/>\n"
1930 " <arg type=\"i\"/>\n"
1935 static DBusHandlerResult
1936 screen_reader_filter (DBusConnection *bus, DBusMessage *message, void *user_data)
1938 if (dbus_message_is_method_call (message, DBUS_INTERFACE_INTROSPECTABLE,
1941 DBusMessage *reply = dbus_message_new_method_return (message);
1942 dbus_message_append_args (reply, DBUS_TYPE_STRING, &sr_introspection,
1944 dbus_connection_send (bus, reply, NULL);
1945 dbus_message_unref (reply);
1946 return DBUS_HANDLER_RESULT_HANDLED;
1948 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1952 _atspi_prepare_screen_reader_interface ()
1954 static gint initialized = 0;
1955 DBusConnection *a11y_bus = _atspi_bus ();
1958 return (initialized > 0);
1960 if (dbus_bus_request_name (a11y_bus, "org.a11y.Atspi.ScreenReader", 0, NULL) < 0)
1967 dbus_connection_add_filter (a11y_bus, screen_reader_filter, NULL, NULL);