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"
40 /* This value is not fixed, could be changed. */
41 #define HASH_TABLE_SIZE_MAX 3000
43 static void handle_get_items (DBusPendingCall *pending, void *user_data);
45 static DBusConnection *bus = NULL;
46 static GHashTable *live_refs = NULL;
47 static gint method_call_timeout = 800;
48 static gint app_startup_time = 15000;
49 static gboolean allow_sync = TRUE;
51 GMainLoop *atspi_main_loop;
52 GMainContext *atspi_main_context;
53 gboolean atspi_no_cache;
55 const char *atspi_path_dec = ATSPI_DBUS_PATH_DEC;
56 const char *atspi_path_registry = ATSPI_DBUS_PATH_REGISTRY;
57 const char *atspi_path_root = ATSPI_DBUS_PATH_ROOT;
58 const char *atspi_bus_registry = ATSPI_DBUS_NAME_REGISTRY;
59 const char *atspi_interface_accessible = ATSPI_DBUS_INTERFACE_ACCESSIBLE;
60 const char *atspi_interface_action = ATSPI_DBUS_INTERFACE_ACTION;
61 const char *atspi_interface_application = ATSPI_DBUS_INTERFACE_APPLICATION;
62 const char *atspi_interface_collection = ATSPI_DBUS_INTERFACE_COLLECTION;
63 const char *atspi_interface_component = ATSPI_DBUS_INTERFACE_COMPONENT;
64 const char *atspi_interface_dec = ATSPI_DBUS_INTERFACE_DEC;
65 const char *atspi_interface_device_event_listener = ATSPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER;
66 const char *atspi_interface_document = ATSPI_DBUS_INTERFACE_DOCUMENT;
67 const char *atspi_interface_editable_text = ATSPI_DBUS_INTERFACE_EDITABLE_TEXT;
68 const char *atspi_interface_event_object = ATSPI_DBUS_INTERFACE_EVENT_OBJECT;
69 const char *atspi_interface_hyperlink = ATSPI_DBUS_INTERFACE_HYPERLINK;
70 const char *atspi_interface_hypertext = ATSPI_DBUS_INTERFACE_HYPERTEXT;
71 const char *atspi_interface_image = ATSPI_DBUS_INTERFACE_IMAGE;
72 const char *atspi_interface_registry = ATSPI_DBUS_INTERFACE_REGISTRY;
73 const char *atspi_interface_selection = ATSPI_DBUS_INTERFACE_SELECTION;
74 const char *atspi_interface_table = ATSPI_DBUS_INTERFACE_TABLE;
75 const char *atspi_interface_table_cell = ATSPI_DBUS_INTERFACE_TABLE_CELL;
76 const char *atspi_interface_text = ATSPI_DBUS_INTERFACE_TEXT;
77 const char *atspi_interface_cache = ATSPI_DBUS_INTERFACE_CACHE;
78 const char *atspi_interface_value = ATSPI_DBUS_INTERFACE_VALUE;
80 static const char *interfaces[] =
82 ATSPI_DBUS_INTERFACE_ACCESSIBLE,
83 ATSPI_DBUS_INTERFACE_ACTION,
84 ATSPI_DBUS_INTERFACE_APPLICATION,
85 ATSPI_DBUS_INTERFACE_COLLECTION,
86 ATSPI_DBUS_INTERFACE_COMPONENT,
87 ATSPI_DBUS_INTERFACE_DOCUMENT,
88 ATSPI_DBUS_INTERFACE_EDITABLE_TEXT,
89 ATSPI_DBUS_INTERFACE_HYPERLINK,
90 ATSPI_DBUS_INTERFACE_HYPERTEXT,
91 ATSPI_DBUS_INTERFACE_IMAGE,
92 "org.a11y.atspi.LoginHelper",
93 ATSPI_DBUS_INTERFACE_SELECTION,
94 ATSPI_DBUS_INTERFACE_TABLE,
95 ATSPI_DBUS_INTERFACE_TABLE_CELL,
96 ATSPI_DBUS_INTERFACE_TEXT,
97 ATSPI_DBUS_INTERFACE_VALUE,
102 _atspi_get_iface_num (const char *iface)
104 /* TODO: Use a binary search or hash to improve performance */
107 for (i = 0; interfaces[i]; i++)
109 if (!strcmp(iface, interfaces[i])) return i;
115 _atspi_get_live_refs (void)
119 live_refs = g_hash_table_new (g_direct_hash, g_direct_equal);
124 /* TODO: Add an application parameter */
131 g_error ("AT-SPI: Couldn't connect to accessibility bus. Is at-spi-bus-launcher running?");
135 #define APP_IS_REGISTRY(app) (!strcmp (app->bus_name, atspi_bus_registry))
137 static AtspiAccessible *desktop;
139 static void cleanup_deferred_message (void);
151 g_hash_table_destroy (refs);
154 cleanup_deferred_message();
159 /* TODO: Do we need this code, or should we just dispose the desktop? */
160 for (i = desktop->children->len - 1; i >= 0; i--)
162 AtspiAccessible *child = g_ptr_array_index (desktop->children, i);
163 if (child->parent.app)
164 g_object_run_dispose (G_OBJECT (child->parent.app));
165 g_object_run_dispose (G_OBJECT (child));
168 g_object_run_dispose (G_OBJECT (desktop->parent.app));
169 g_object_unref (desktop);
175 dbus_connection_close (bus);
176 dbus_connection_unref (bus);
181 static gboolean atspi_inited = FALSE;
183 static GHashTable *app_hash = NULL;
186 handle_get_bus_address (DBusPendingCall *pending, void *user_data)
188 AtspiApplication *app = user_data;
189 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
190 DBusMessage *message;
192 DBusPendingCall *new_pending;
195 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
197 if (dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &address,
198 DBUS_TYPE_INVALID) && address [0])
203 dbus_error_init (&error);
204 bus = dbus_connection_open_private (address, &error);
209 dbus_connection_unref (app->bus);
212 atspi_dbus_connection_setup_with_g_main(bus, g_main_context_default());
216 if (!strcmp (error.name, DBUS_ERROR_FILE_NOT_FOUND))
217 g_warning ("AT-SPI: Unable to open bus connection: %s", error.message);
218 dbus_error_free (&error);
222 dbus_message_unref (reply);
223 dbus_pending_call_unref (pending);
226 return; /* application has gone away / been disposed */
228 message = dbus_message_new_method_call (app->bus_name,
229 "/org/a11y/atspi/cache",
230 atspi_interface_cache, "GetItems");
232 result = dbus_connection_send_with_reply (app->bus, message, &new_pending, 2000);
233 dbus_message_unref (message);
234 if (!result || !new_pending)
236 dbus_pending_call_set_notify (new_pending, handle_get_items, app, NULL);
239 static AtspiApplication *
240 get_application (const char *bus_name)
242 AtspiApplication *app = NULL;
244 DBusMessage *message;
245 DBusPendingCall *pending = NULL;
250 app_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_object_unref);
251 if (!app_hash) return NULL;
253 app = g_hash_table_lookup (app_hash, bus_name);
255 bus_name_dup = g_strdup (bus_name);
256 if (!bus_name_dup) return NULL;
257 // TODO: change below to something that will send state-change:defunct notification if necessary */
258 app = _atspi_application_new (bus_name);
259 app->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
260 app->bus = dbus_connection_ref (_atspi_bus ());
261 gettimeofday (&app->time_added, NULL);
262 app->cache = ATSPI_CACHE_UNDEFINED;
263 g_hash_table_insert (app_hash, bus_name_dup, app);
264 message = dbus_message_new_method_call (bus_name, atspi_path_root,
265 atspi_interface_application, "GetApplicationBusAddress");
267 result = dbus_connection_send_with_reply (app->bus, message, &pending, 2000);
268 dbus_message_unref (message);
269 if (!result || !pending)
271 g_hash_table_remove (app_hash, bus_name_dup);
274 dbus_pending_call_set_notify (pending, handle_get_bus_address, app, NULL);
279 ref_accessible (const char *app_name, const char *path)
281 AtspiApplication *app;
284 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
287 app = get_application (app_name);
289 if (!strcmp (path, "/org/a11y/atspi/accessible/root"))
293 app->root = _atspi_accessible_new (app, atspi_path_root);
294 app->root->accessible_parent = atspi_get_desktop (0);
295 g_ptr_array_add (app->root->accessible_parent->children, g_object_ref (app->root));
297 return g_object_ref (app->root);
300 a = g_hash_table_lookup (app->hash, path);
303 return g_object_ref (a);
305 a = _atspi_accessible_new (app, path);
309 if (g_hash_table_size (app->hash) > HASH_TABLE_SIZE_MAX)
311 g_warning ("Accessible hash table is bigger than MAX: %d\n", HASH_TABLE_SIZE_MAX);
312 g_hash_table_remove_all(app->hash);
315 g_hash_table_insert (app->hash, g_strdup (a->parent.path), g_object_ref (a));
319 static AtspiHyperlink *
320 ref_hyperlink (const char *app_name, const char *path)
322 AtspiApplication *app = get_application (app_name);
323 AtspiHyperlink *hyperlink;
325 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
328 hyperlink = g_hash_table_lookup (app->hash, path);
331 return g_object_ref (hyperlink);
333 hyperlink = _atspi_hyperlink_new (app, path);
334 g_hash_table_insert (app->hash, g_strdup (hyperlink->parent.path), hyperlink);
335 /* TODO: This should be a weak ref */
336 g_object_ref (hyperlink); /* for the hash */
349 GArray *state_bitflags;
352 static DBusHandlerResult
353 handle_remove_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
355 const char *sender = dbus_message_get_sender (message);
356 AtspiApplication *app;
358 DBusMessageIter iter, iter_struct;
359 const char *signature = dbus_message_get_signature (message);
362 if (strcmp (signature, "(so)") != 0)
364 g_warning ("AT-SPI: Unknown signature %s for RemoveAccessible", signature);
365 return DBUS_HANDLER_RESULT_HANDLED;
368 dbus_message_iter_init (message, &iter);
369 dbus_message_iter_recurse (&iter, &iter_struct);
370 dbus_message_iter_get_basic (&iter_struct, &sender);
371 dbus_message_iter_next (&iter_struct);
372 dbus_message_iter_get_basic (&iter_struct, &path);
373 app = get_application (sender);
374 a = ref_accessible (sender, path);
376 return DBUS_HANDLER_RESULT_HANDLED;
377 g_object_run_dispose (G_OBJECT (a));
378 g_hash_table_remove (app->hash, a->parent.path);
379 g_object_unref (a); /* unref our own ref */
380 return DBUS_HANDLER_RESULT_HANDLED;
384 remove_accessible_from_app_hash (const char *app_name, AtspiAccessible *a)
386 AtspiApplication *app;
390 app = get_application (app_name);
393 if (!g_hash_table_lookup (app->hash, a->parent.path)) return;
395 g_hash_table_remove (app->hash, a->parent.path);
398 static DBusHandlerResult
399 handle_name_owner_changed (DBusConnection *bus, DBusMessage *message, void *user_data)
401 const char *name, *new, *old;
402 static gboolean registry_lost = FALSE;
404 if (!dbus_message_get_args (message, NULL,
405 DBUS_TYPE_STRING, &name,
406 DBUS_TYPE_STRING, &old,
407 DBUS_TYPE_STRING, &new,
410 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
413 if (!strcmp (name, "org.a11y.atspi.Registry"))
415 if (registry_lost && !old[0])
417 _atspi_reregister_event_listeners ();
418 _atspi_reregister_device_listeners ();
419 registry_lost = FALSE;
422 registry_lost = TRUE;
426 AtspiApplication *app = g_hash_table_lookup (app_hash, old);
427 if (app && app->bus_name && !strcmp(app->bus_name, name))
428 g_object_run_dispose (G_OBJECT (app));
430 return DBUS_HANDLER_RESULT_HANDLED;
434 add_app_to_desktop (AtspiAccessible *a, const char *bus_name)
436 AtspiAccessible *obj = ref_accessible (bus_name, atspi_path_root);
437 /* The app will be added to the desktop as a side-effect of calling
439 g_object_unref (obj);
440 return (obj != NULL);
444 get_reference_from_iter (DBusMessageIter *iter, const char **app_name, const char **path)
446 DBusMessageIter iter_struct;
448 dbus_message_iter_recurse (iter, &iter_struct);
449 dbus_message_iter_get_basic (&iter_struct, app_name);
450 dbus_message_iter_next (&iter_struct);
451 dbus_message_iter_get_basic (&iter_struct, path);
452 dbus_message_iter_next (iter);
456 add_accessible_from_iter (DBusMessageIter *iter)
458 DBusMessageIter iter_struct, iter_array;
459 const char *app_name, *path;
460 AtspiAccessible *accessible;
461 const char *name, *description;
463 gboolean children_cached = FALSE;
464 dbus_int32_t count, index;
466 dbus_message_iter_recurse (iter, &iter_struct);
469 get_reference_from_iter (&iter_struct, &app_name, &path);
470 accessible = ref_accessible (app_name, path);
474 /* Get application: TODO */
475 dbus_message_iter_next (&iter_struct);
478 get_reference_from_iter (&iter_struct, &app_name, &path);
479 if (accessible->accessible_parent)
480 g_object_unref (accessible->accessible_parent);
481 accessible->accessible_parent = ref_accessible (app_name, path);
483 if (dbus_message_iter_get_arg_type (&iter_struct) == 'i')
485 /* Get index in parent */
486 dbus_message_iter_get_basic (&iter_struct, &index);
487 if (index >= 0 && accessible->accessible_parent)
489 if (index >= accessible->accessible_parent->children->len)
491 /* There is no room for this object */
492 g_ptr_array_set_size (accessible->accessible_parent->children, index + 1);
496 /* This place is already taken - let's free this place with dignity */
497 if (g_ptr_array_index (accessible->accessible_parent->children, index))
498 g_object_unref (g_ptr_array_index (accessible->accessible_parent->children, index));
500 g_ptr_array_index (accessible->accessible_parent->children, index) = g_object_ref (accessible);
503 /* get child count */
504 dbus_message_iter_next (&iter_struct);
505 dbus_message_iter_get_basic (&iter_struct, &count);
508 g_ptr_array_set_size (accessible->children, count);
509 children_cached = TRUE;
512 else if (dbus_message_iter_get_arg_type (&iter_struct) == 'a')
514 /* It's the old API with a list of children */
515 /* TODO: Perhaps remove this code eventually */
516 dbus_message_iter_recurse (&iter_struct, &iter_array);
517 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
519 AtspiAccessible *child;
520 get_reference_from_iter (&iter_array, &app_name, &path);
521 child = ref_accessible (app_name, path);
522 g_ptr_array_remove (accessible->children, child);
523 g_ptr_array_add (accessible->children, child);
525 children_cached = TRUE;
529 dbus_message_iter_next (&iter_struct);
530 _atspi_dbus_set_interfaces (accessible, &iter_struct);
531 dbus_message_iter_next (&iter_struct);
534 if (accessible->name)
535 g_free (accessible->name);
536 dbus_message_iter_get_basic (&iter_struct, &name);
537 accessible->name = g_strdup (name);
538 dbus_message_iter_next (&iter_struct);
541 dbus_message_iter_get_basic (&iter_struct, &role);
542 accessible->role = role;
543 dbus_message_iter_next (&iter_struct);
546 if (accessible->description)
547 g_free (accessible->description);
548 dbus_message_iter_get_basic (&iter_struct, &description);
549 accessible->description = g_strdup (description);
550 dbus_message_iter_next (&iter_struct);
552 _atspi_dbus_set_state (accessible, &iter_struct);
553 dbus_message_iter_next (&iter_struct);
555 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_NAME | ATSPI_CACHE_ROLE |
556 ATSPI_CACHE_PARENT | ATSPI_CACHE_DESCRIPTION);
557 if (!atspi_state_set_contains (accessible->states,
558 ATSPI_STATE_MANAGES_DESCENDANTS) &&
560 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_CHILDREN);
562 /* This is a bit of a hack since the cache holds a ref, so we don't need
563 * the one provided for us anymore */
564 g_object_unref (accessible);
568 handle_get_items (DBusPendingCall *pending, void *user_data)
570 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
571 DBusMessageIter iter, iter_array;
573 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
575 const char *sender = dbus_message_get_sender (reply);
576 const char *error = NULL;
577 const char *error_name = dbus_message_get_error_name (reply);
578 if (!strcmp (error_name, DBUS_ERROR_SERVICE_UNKNOWN)
579 || !strcmp (error_name, DBUS_ERROR_NO_REPLY))
584 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &error,
586 g_warning ("AT-SPI: Error in GetItems, sender=%s, error=%s", sender, error);
588 dbus_message_unref (reply);
589 dbus_pending_call_unref (pending);
593 dbus_message_iter_init (reply, &iter);
594 dbus_message_iter_recurse (&iter, &iter_array);
595 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
597 add_accessible_from_iter (&iter_array);
598 dbus_message_iter_next (&iter_array);
600 dbus_message_unref (reply);
601 dbus_pending_call_unref (pending);
604 /* TODO: Do we stil need this function? */
605 static AtspiAccessible *
606 ref_accessible_desktop (AtspiApplication *app)
609 DBusMessage *message, *reply;
610 DBusMessageIter iter, iter_array;
615 g_object_ref (desktop);
618 desktop = _atspi_accessible_new (app, atspi_path_root);
623 g_hash_table_insert (app->hash, g_strdup (desktop->parent.path),
624 g_object_ref (desktop));
625 app->root = g_object_ref (desktop);
626 desktop->name = g_strdup ("main");
627 message = dbus_message_new_method_call (atspi_bus_registry,
629 atspi_interface_accessible,
634 reply = _atspi_dbus_send_with_reply_and_block (message, &error);
635 if (!reply || strcmp (dbus_message_get_signature (reply), "a(so)") != 0)
639 g_warning ("Couldn't get application list: %s", error->message);
640 g_clear_error (&error);
643 dbus_message_unref (reply);
646 dbus_message_iter_init (reply, &iter);
647 dbus_message_iter_recurse (&iter, &iter_array);
648 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
650 const char *app_name, *path;
651 get_reference_from_iter (&iter_array, &app_name, &path);
652 add_app_to_desktop (desktop, app_name);
655 /* Record the alternate name as an alias for org.a11y.atspi.Registry */
656 bus_name_dup = g_strdup (dbus_message_get_sender (reply));
658 g_hash_table_insert (app_hash, bus_name_dup, app);
660 dbus_message_unref (reply);
662 return g_object_ref (desktop);
666 _atspi_ref_accessible (const char *app, const char *path)
668 AtspiApplication *a = get_application (app);
671 if ( APP_IS_REGISTRY(a))
674 g_object_unref (ref_accessible_desktop (a)); /* sets a->root */
675 return g_object_ref (a->root);
677 return ref_accessible (app, path);
681 _atspi_dbus_return_accessible_from_message (DBusMessage *message)
683 DBusMessageIter iter;
684 AtspiAccessible *retval = NULL;
685 const char *signature;
690 signature = dbus_message_get_signature (message);
691 if (!strcmp (signature, "(so)"))
693 dbus_message_iter_init (message, &iter);
694 retval = _atspi_dbus_return_accessible_from_iter (&iter);
698 g_warning ("AT-SPI: Called _atspi_dbus_return_accessible_from_message with strange signature %s", signature);
700 dbus_message_unref (message);
705 _atspi_dbus_return_accessible_from_iter (DBusMessageIter *iter)
707 const char *app_name, *path;
709 get_reference_from_iter (iter, &app_name, &path);
710 return ref_accessible (app_name, path);
714 _atspi_dbus_return_hyperlink_from_message (DBusMessage *message)
716 DBusMessageIter iter;
717 AtspiHyperlink *retval = NULL;
718 const char *signature;
723 signature = dbus_message_get_signature (message);
724 if (!strcmp (signature, "(so)"))
726 dbus_message_iter_init (message, &iter);
727 retval = _atspi_dbus_return_hyperlink_from_iter (&iter);
731 g_warning ("AT-SPI: Called _atspi_dbus_return_hyperlink_from_message with strange signature %s", signature);
733 dbus_message_unref (message);
738 _atspi_dbus_return_hyperlink_from_iter (DBusMessageIter *iter)
740 const char *app_name, *path;
742 get_reference_from_iter (iter, &app_name, &path);
743 return ref_hyperlink (app_name, path);
746 const char *cache_signal_type = "((so)(so)(so)iiassusau)";
747 const char *old_cache_signal_type = "((so)(so)(so)a(so)assusau)";
749 static DBusHandlerResult
750 handle_add_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
752 DBusMessageIter iter;
753 const char *signature = dbus_message_get_signature (message);
755 if (strcmp (signature, cache_signal_type) != 0 &&
756 strcmp (signature, old_cache_signal_type) != 0)
758 g_warning ("AT-SPI: AddAccessible with unknown signature %s\n", signature);
759 return DBUS_HANDLER_RESULT_HANDLED;
762 dbus_message_iter_init (message, &iter);
763 add_accessible_from_iter (&iter);
764 return DBUS_HANDLER_RESULT_HANDLED;
770 DBusMessage *message;
774 static GSource *process_deferred_messages_source = NULL;
777 process_deferred_message (BusDataClosure *closure)
779 int type = dbus_message_get_type (closure->message);
780 const char *interface = dbus_message_get_interface (closure->message);
782 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
783 !strncmp (interface, "org.a11y.atspi.Event.", 21))
785 _atspi_dbus_handle_event (closure->bus, closure->message, closure->data);
787 if (dbus_message_is_method_call (closure->message, atspi_interface_device_event_listener, "NotifyEvent"))
789 _atspi_dbus_handle_DeviceEvent (closure->bus,
790 closure->message, closure->data);
792 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "AddAccessible"))
794 handle_add_accessible (closure->bus, closure->message, closure->data);
796 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "RemoveAccessible"))
798 handle_remove_accessible (closure->bus, closure->message, closure->data);
800 if (dbus_message_is_signal (closure->message, "org.freedesktop.DBus", "NameOwnerChanged"))
802 handle_name_owner_changed (closure->bus, closure->message, closure->data);
806 static GQueue *deferred_messages = NULL;
809 destroy_deferred_message_item(gpointer ptr)
811 /* TODO this is still memory leak on c->data */
812 BusDataClosure *c = ptr;
813 dbus_message_unref (c->message);
814 dbus_connection_unref (c->bus);
819 cleanup_deferred_message(void)
821 g_queue_free_full (deferred_messages, destroy_deferred_message_item);
822 deferred_messages = NULL;
826 process_deferred_messages (void)
828 static int in_process_deferred_messages = 0;
829 BusDataClosure *closure;
831 if (in_process_deferred_messages)
833 in_process_deferred_messages = 1;
834 while ((closure = g_queue_pop_head (deferred_messages)))
836 process_deferred_message (closure);
837 dbus_message_unref (closure->message);
838 dbus_connection_unref (closure->bus);
841 in_process_deferred_messages = 0;
846 process_deferred_messages_callback (gpointer data)
848 if (process_deferred_messages ())
849 return G_SOURCE_CONTINUE;
851 process_deferred_messages_source = NULL;
852 return G_SOURCE_REMOVE;
855 static DBusHandlerResult
856 defer_message (DBusConnection *connection, DBusMessage *message, void *user_data)
858 BusDataClosure *closure = g_new (BusDataClosure, 1);
860 closure->bus = dbus_connection_ref (bus);
861 closure->message = dbus_message_ref (message);
862 closure->data = user_data;
864 g_queue_push_tail (deferred_messages, closure);
866 if (process_deferred_messages_source == NULL)
868 process_deferred_messages_source = g_idle_source_new ();
869 g_source_set_callback (process_deferred_messages_source,
870 process_deferred_messages_callback, NULL, NULL);
871 g_source_attach (process_deferred_messages_source, atspi_main_context);
872 g_source_unref (process_deferred_messages_source);
875 return DBUS_HANDLER_RESULT_HANDLED;
878 static DBusHandlerResult
879 atspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
881 int type = dbus_message_get_type (message);
882 const char *interface = dbus_message_get_interface (message);
884 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
885 !strncmp (interface, "org.a11y.atspi.Event.", 21))
887 return defer_message (bus, message, data);
889 if (dbus_message_is_method_call (message, atspi_interface_device_event_listener, "NotifyEvent"))
891 return defer_message (bus, message, data);
893 if (dbus_message_is_signal (message, atspi_interface_cache, "AddAccessible"))
895 return defer_message (bus, message, data);
897 if (dbus_message_is_signal (message, atspi_interface_cache, "RemoveAccessible"))
899 return defer_message (bus, message, data);
901 if (dbus_message_is_signal (message, "org.freedesktop.DBus", "NameOwnerChanged"))
903 defer_message (bus, message, data);
904 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
906 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
912 * Connects to the accessibility registry and initializes the SPI.
914 * Returns: 0 on success, 1 if already initialized, or an integer error code.
920 const gchar *no_cache;
929 _atspi_get_live_refs();
931 bus = atspi_get_a11y_bus ();
934 if (!dbus_bus_register (bus, NULL))
936 atspi_dbus_connection_setup_with_g_main(bus, g_main_context_default());
937 dbus_connection_add_filter (bus, atspi_dbus_filter, NULL, NULL);
938 match = g_strdup_printf ("type='signal',interface='%s',member='AddAccessible'", atspi_interface_cache);
939 dbus_bus_add_match (bus, match, NULL);
941 match = g_strdup_printf ("type='signal',interface='%s',member='RemoveAccessible'", atspi_interface_cache);
942 dbus_bus_add_match (bus, match, NULL);
944 match = g_strdup_printf ("type='signal',interface='%s',member='ChildrenChanged'", atspi_interface_event_object);
945 dbus_bus_add_match (bus, match, NULL);
947 match = g_strdup_printf ("type='signal',interface='%s',member='PropertyChange'", atspi_interface_event_object);
948 dbus_bus_add_match (bus, match, NULL);
950 match = g_strdup_printf ("type='signal',interface='%s',member='StateChanged'", atspi_interface_event_object);
951 dbus_bus_add_match (bus, match, NULL);
954 dbus_bus_add_match (bus,
955 "type='signal', interface='org.freedesktop.DBus', member='NameOwnerChanged'",
958 no_cache = g_getenv ("ATSPI_NO_CACHE");
959 if (no_cache && g_strcmp0 (no_cache, "0") != 0)
960 atspi_no_cache = TRUE;
962 deferred_messages = g_queue_new ();
968 * atspi_is_initialized:
970 * Indicates whether AT-SPI has been initialized.
972 * Returns: %True if initialized; %False otherwise.
975 atspi_is_initialized ()
983 * Starts/enters the main event loop for the AT-SPI services.
985 * NOTE: This method does not return control; it is exited via a call to
986 * #atspi_event_quit from within an event handler.
990 atspi_event_main (void)
992 atspi_main_loop = g_main_loop_new (NULL, FALSE);
993 g_main_loop_run (atspi_main_loop);
994 g_main_loop_unref(atspi_main_loop);
995 atspi_main_loop = NULL;
1001 * Quits the last main event loop for the AT-SPI services,
1002 * See: #atspi_event_main
1005 atspi_event_quit (void)
1007 g_main_loop_quit (atspi_main_loop);
1013 * Disconnects from #AtspiRegistry instances and releases
1014 * any floating resources. Call only once at exit.
1016 * Returns: 0 if there were no leaks, otherwise other integer values.
1028 atspi_inited = FALSE;
1032 leaked = g_hash_table_size (live_refs);
1044 static GSList *hung_processes;
1047 remove_hung_process (DBusPendingCall *pending, void *data)
1049 hung_processes = g_slist_remove (hung_processes, data);
1051 dbus_pending_call_unref (pending);
1055 check_for_hang (DBusMessage *message, DBusError *error, DBusConnection *bus, const char *bus_name)
1057 if (!message && error->name &&
1058 !strcmp (error->name, "org.freedesktop.DBus.Error.NoReply"))
1061 DBusMessage *message;
1062 gchar *bus_name_dup;
1063 DBusPendingCall *pending = NULL;
1065 for (l = hung_processes; l; l = l->next)
1066 if (!strcmp (l->data, bus_name))
1068 message = dbus_message_new_method_call (bus_name, "/",
1069 "org.freedesktop.DBus.Peer",
1073 result = dbus_connection_send_with_reply (bus, message, &pending, -1);
1074 dbus_message_unref (message);
1075 if (!result || !pending)
1077 bus_name_dup = g_strdup (bus_name);
1078 hung_processes = g_slist_append (hung_processes, bus_name_dup);
1079 dbus_pending_call_set_notify (pending, remove_hung_process, bus_name_dup, NULL);
1084 connection_is_hung (const char *bus_name)
1088 for (l = hung_processes; l; l = l->next)
1089 if (!strcmp (l->data, bus_name))
1095 check_app (AtspiApplication *app, GError **error)
1097 if (!app || !app->bus)
1099 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
1100 _("The application no longer exists"));
1104 if (atspi_main_loop && connection_is_hung (app->bus_name))
1106 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC,
1107 "The process appears to be hung.");
1115 set_timeout (AtspiApplication *app)
1120 if (app && app_startup_time > 0)
1122 gettimeofday (&tv, NULL);
1123 diff = (tv.tv_sec - app->time_added.tv_sec) * 1000 + (tv.tv_usec - app->time_added.tv_usec) / 1000;
1124 dbind_set_timeout (MAX(method_call_timeout, app_startup_time - diff));
1127 dbind_set_timeout (method_call_timeout);
1131 _atspi_dbus_call (gpointer obj, const char *interface, const char *method, GError **error, const char *type, ...)
1136 AtspiObject *aobj = ATSPI_OBJECT (obj);
1138 if (!check_app (aobj->app, error))
1143 _atspi_set_error_no_sync (error);
1147 va_start (args, type);
1148 dbus_error_init (&err);
1149 set_timeout (aobj->app);
1150 retval = dbind_method_call_reentrant_va (aobj->app->bus, aobj->app->bus_name,
1151 aobj->path, interface, method, &err,
1154 check_for_hang (NULL, &err, aobj->app->bus, aobj->app->bus_name);
1155 process_deferred_messages ();
1156 if (dbus_error_is_set (&err))
1158 g_set_error(error, ATSPI_ERROR, ATSPI_ERROR_IPC, "%s", err.message);
1159 dbus_error_free (&err);
1165 _atspi_dbus_call_partial (gpointer obj,
1166 const char *interface,
1169 const char *type, ...)
1172 DBusMessage * result;
1173 va_start (args, type);
1174 result = _atspi_dbus_call_partial_va (obj, interface, method, error, type, args);
1181 _atspi_dbus_call_partial_va (gpointer obj,
1182 const char *interface,
1188 AtspiObject *aobj = ATSPI_OBJECT (obj);
1190 DBusMessage *msg = NULL, *reply = NULL;
1191 DBusMessageIter iter;
1194 dbus_error_init (&err);
1196 if (!check_app (aobj->app, error))
1199 msg = dbus_message_new_method_call (aobj->app->bus_name, aobj->path, interface, method);
1204 dbus_message_iter_init_append (msg, &iter);
1205 dbind_any_marshal_va (&iter, &p, args);
1207 set_timeout (aobj->app);
1208 reply = dbind_send_and_allow_reentry (aobj->app->bus, msg, &err);
1209 check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1213 dbus_message_unref (msg);
1214 process_deferred_messages ();
1215 if (dbus_error_is_set (&err))
1217 g_set_error_literal(error, ATSPI_ERROR, ATSPI_ERROR_IPC, err.message);
1218 dbus_error_free (&err);
1220 dbus_message_unref(reply);
1223 else if (reply && dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1225 const char *err_str = NULL;
1226 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &err_str, DBUS_TYPE_INVALID);
1228 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err_str);
1229 dbus_message_unref (reply);
1236 _atspi_dbus_get_property (gpointer obj, const char *interface, const char *name, GError **error, const char *type, void *data)
1238 DBusMessage *message, *reply;
1239 DBusMessageIter iter, iter_variant;
1241 dbus_bool_t retval = FALSE;
1242 AtspiObject *aobj = ATSPI_OBJECT (obj);
1243 char expected_type = (type [0] == '(' ? 'r' : type [0]);
1248 if (!check_app (aobj->app, error))
1253 _atspi_set_error_no_sync (error);
1257 message = dbus_message_new_method_call (aobj->app->bus_name,
1259 "org.freedesktop.DBus.Properties",
1263 // TODO: throw exception
1266 dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1267 dbus_error_init (&err);
1268 set_timeout (aobj->app);
1269 reply = dbind_send_and_allow_reentry (aobj->app->bus, message, &err);
1270 check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1271 dbus_message_unref (message);
1272 process_deferred_messages ();
1275 /* TODO: throw exception */
1279 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
1281 const char *err_str = NULL;
1282 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &err_str, DBUS_TYPE_INVALID);
1284 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err_str);
1288 dbus_message_iter_init (reply, &iter);
1289 if (dbus_message_iter_get_arg_type (&iter) != 'v')
1291 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));
1294 dbus_message_iter_recurse (&iter, &iter_variant);
1295 if (dbus_message_iter_get_arg_type (&iter_variant) != expected_type)
1297 g_warning ("atspi_dbus_get_property: Wrong type: expected %s, got %c\n", type, dbus_message_iter_get_arg_type (&iter_variant));
1300 if (!strcmp (type, "(so)"))
1302 *((AtspiAccessible **)data) = _atspi_dbus_return_accessible_from_iter (&iter_variant);
1306 if (type [0] == 's')
1308 *(char**) data = NULL;
1311 dbus_message_iter_get_basic (&iter_variant, data);
1313 if (type [0] == 's')
1314 *(char **)data = g_strdup (*(char **)data);
1318 dbus_error_free (&err);
1320 dbus_message_unref (reply);
1325 _atspi_dbus_send_with_reply_and_block (DBusMessage *message, GError **error)
1329 AtspiApplication *app;
1330 DBusConnection *bus;
1332 app = get_application (dbus_message_get_destination (message));
1334 if (app && !app->bus)
1335 return NULL; /* will fail anyway; app has been disposed */
1337 bus = (app ? app->bus : _atspi_bus());
1338 dbus_error_init (&err);
1340 reply = dbind_send_and_allow_reentry (bus, message, &err);
1341 process_deferred_messages ();
1342 dbus_message_unref (message);
1343 if (dbus_error_is_set (&err))
1346 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err.message);
1347 dbus_error_free (&err);
1353 _atspi_dbus_return_hash_from_message (DBusMessage *message)
1355 DBusMessageIter iter;
1361 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1363 dbus_message_iter_init (message, &iter);
1364 ret = _atspi_dbus_hash_from_iter (&iter);
1365 dbus_message_unref (message);
1370 _atspi_dbus_hash_from_iter (DBusMessageIter *iter)
1372 GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
1373 (GDestroyNotify) g_free,
1374 (GDestroyNotify) g_free);
1375 DBusMessageIter iter_array, iter_dict;
1377 dbus_message_iter_recurse (iter, &iter_array);
1378 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1380 const char *name, *value;
1381 dbus_message_iter_recurse (&iter_array, &iter_dict);
1382 dbus_message_iter_get_basic (&iter_dict, &name);
1383 dbus_message_iter_next (&iter_dict);
1384 dbus_message_iter_get_basic (&iter_dict, &value);
1385 g_hash_table_insert (hash, g_strdup (name), g_strdup (value));
1386 dbus_message_iter_next (&iter_array);
1392 _atspi_dbus_return_attribute_array_from_message (DBusMessage *message)
1394 DBusMessageIter iter;
1400 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1402 dbus_message_iter_init (message, &iter);
1404 ret = _atspi_dbus_attribute_array_from_iter (&iter);
1405 dbus_message_unref (message);
1410 _atspi_dbus_attribute_array_from_iter (DBusMessageIter *iter)
1412 DBusMessageIter iter_array, iter_dict;
1413 GArray *array = g_array_new (TRUE, TRUE, sizeof (gchar *));
1415 dbus_message_iter_recurse (iter, &iter_array);
1416 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1418 const char *name, *value;
1420 dbus_message_iter_recurse (&iter_array, &iter_dict);
1421 dbus_message_iter_get_basic (&iter_dict, &name);
1422 dbus_message_iter_next (&iter_dict);
1423 dbus_message_iter_get_basic (&iter_dict, &value);
1424 str = g_strdup_printf ("%s:%s", name, value);
1425 array = g_array_append_val (array, str);
1426 dbus_message_iter_next (&iter_array);;
1432 _atspi_dbus_set_interfaces (AtspiAccessible *accessible, DBusMessageIter *iter)
1434 DBusMessageIter iter_array;
1435 char *iter_sig = dbus_message_iter_get_signature (iter);
1437 accessible->interfaces = 0;
1438 if (strcmp (iter_sig, "as") != 0)
1440 g_warning ("_atspi_dbus_set_interfaces: Passed iterator with invalid signature %s", iter_sig);
1441 dbus_free (iter_sig);
1444 dbus_free (iter_sig);
1445 dbus_message_iter_recurse (iter, &iter_array);
1446 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1450 dbus_message_iter_get_basic (&iter_array, &iface);
1451 if (!strcmp (iface, "org.freedesktop.DBus.Introspectable")) continue;
1452 n = _atspi_get_iface_num (iface);
1455 g_warning ("AT-SPI: Unknown interface %s", iface);
1458 accessible->interfaces |= (1 << n);
1459 dbus_message_iter_next (&iter_array);
1461 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_INTERFACES);
1465 _atspi_dbus_set_state (AtspiAccessible *accessible, DBusMessageIter *iter)
1467 DBusMessageIter iter_array;
1469 dbus_uint32_t *states;
1471 dbus_message_iter_recurse (iter, &iter_array);
1472 dbus_message_iter_get_fixed_array (&iter_array, &states, &count);
1475 g_warning ("AT-SPI: expected 2 values in states array; got %d\n", count);
1476 if (!accessible->states)
1477 accessible->states = _atspi_state_set_new_internal (accessible, 0);
1481 guint64 val = ((guint64)states [1]) << 32;
1483 if (!accessible->states)
1484 accessible->states = _atspi_state_set_new_internal (accessible, val);
1486 accessible->states->states = val;
1488 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_STATES);
1492 _atspi_error_quark (void)
1494 return g_quark_from_static_string ("atspi_error");
1498 * Gets the IOR from the XDisplay.
1502 * Returns a 'canonicalized' value for DISPLAY,
1503 * with the screen number stripped off if present.
1505 * TODO: Avoid having duplicate functions for this here and in at-spi2-atk
1508 spi_display_name (void)
1510 char *canonical_display_name = NULL;
1511 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
1515 display_env = g_getenv ("DISPLAY");
1516 if (!display_env || !display_env[0])
1520 gchar *display_p, *screen_p;
1521 canonical_display_name = g_strdup (display_env);
1522 display_p = g_utf8_strrchr (canonical_display_name, -1, ':');
1523 screen_p = g_utf8_strrchr (canonical_display_name, -1, '.');
1524 if (screen_p && display_p && (screen_p > display_p))
1532 canonical_display_name = g_strdup (display_env);
1535 return canonical_display_name;
1539 get_accessibility_bus_address_x11 (void)
1543 Display *bridge_display = NULL;
1546 unsigned char *data_x11 = NULL;
1547 unsigned long nitems;
1548 unsigned long leftover;
1551 display_name = spi_display_name ();
1555 bridge_display = XOpenDisplay (display_name);
1556 g_free (display_name);
1558 if (!bridge_display)
1560 g_warning ("Could not open X display");
1564 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
1565 XGetWindowProperty (bridge_display,
1566 XDefaultRootWindow (bridge_display),
1568 (long) BUFSIZ, False,
1569 (Atom) 31, &actual_type, &actual_format,
1570 &nitems, &leftover, &data_x11);
1571 XCloseDisplay (bridge_display);
1573 data = g_strdup ((gchar *)data_x11);
1580 get_accessibility_bus_address_dbus (void)
1582 DBusConnection *session_bus = NULL;
1583 DBusMessage *message;
1586 char *address = NULL;
1588 session_bus = dbus_bus_get (DBUS_BUS_SESSION, NULL);
1592 message = dbus_message_new_method_call ("org.a11y.Bus",
1597 dbus_error_init (&error);
1598 reply = dbus_connection_send_with_reply_and_block (session_bus,
1602 dbus_message_unref (message);
1606 g_warning ("AT-SPI: Error retrieving accessibility bus address: %s: %s",
1607 error.name, error.message);
1608 dbus_error_free (&error);
1613 const char *tmp_address;
1614 if (!dbus_message_get_args (reply,
1620 dbus_message_unref (reply);
1623 address = g_strdup (tmp_address);
1624 dbus_message_unref (reply);
1628 dbus_connection_unref (session_bus);
1632 static DBusConnection *a11y_bus;
1633 static dbus_int32_t a11y_dbus_slot = -1;
1636 a11y_bus_free (void *data)
1638 if (data == a11y_bus)
1641 dbus_connection_free_data_slot (&a11y_dbus_slot);
1646 * atspi_get_a11y_bus: (skip)
1649 atspi_get_a11y_bus (void)
1652 char *address = NULL;
1653 const char *address_env = NULL;
1655 if (a11y_bus && dbus_connection_get_is_connected (a11y_bus))
1658 if (a11y_dbus_slot == -1)
1659 if (!dbus_connection_allocate_data_slot (&a11y_dbus_slot))
1660 g_warning ("AT-SPI: Unable to allocate D-Bus slot");
1662 address_env = g_getenv ("AT_SPI_BUS_ADDRESS");
1663 if (address_env != NULL && *address_env != 0)
1664 address = g_strdup (address_env);
1666 if (!address && g_getenv ("DISPLAY") != NULL &&
1667 g_getenv ("WAYLAND_DISPLAY") == NULL)
1668 address = get_accessibility_bus_address_x11 ();
1671 address = get_accessibility_bus_address_dbus ();
1675 dbus_error_init (&error);
1676 a11y_bus = dbus_connection_open_private (address, &error);
1681 if (!g_getenv("SSH_CONNECTION"))
1682 g_warning ("Couldn't connect to accessibility bus: %s", error.message);
1683 dbus_error_free (&error);
1688 if (!dbus_bus_register (a11y_bus, &error))
1690 g_warning ("Couldn't register with accessibility bus: %s", error.message);
1691 dbus_error_free (&error);
1692 dbus_connection_close (a11y_bus);
1693 dbus_connection_unref (a11y_bus);
1699 /* Simulate a weak ref on the bus */
1700 dbus_connection_set_data (a11y_bus, a11y_dbus_slot, a11y_bus, a11y_bus_free);
1706 * atspi_set_timeout:
1707 * @val: The timeout value, in milliseconds, or -1 to disable the timeout.
1708 * @startup_time: The amount of time, in milliseconds, to allow to pass
1709 * before enforcing timeouts on an application. Can be used to prevent
1710 * timeout exceptions if an application is likely to block for an extended
1711 * period of time on initialization. -1 can be passed to disable this
1714 * Set the timeout used for method calls. If this is not set explicitly,
1715 * a default of 0.8 ms is used.
1716 * Note that at-spi2-registryd currently uses a timeout of 3 seconds when
1717 * sending a keyboard event notification. This means that, if an AT makes
1718 * a call in response to the keyboard notification and the application
1719 * being called does not respond before the timeout is reached,
1720 * at-spi2-registryd will time out on the keyboard event notification and
1721 * pass the key onto the application (ie, reply to indicate that the key
1722 * was not consumed), so this may make it undesirable to set a timeout
1723 * larger than 3 seconds.
1725 * By default, the normal timeout is set to 800 ms, and the application startup
1726 * timeout is set to 15 seconds.
1729 atspi_set_timeout (gint val, gint startup_time)
1731 method_call_timeout = val;
1732 app_startup_time = startup_time;
1736 * atspi_set_main_context:
1737 * @cnx: The #GMainContext to use.
1739 * Sets the main loop context that AT-SPI should assume is in use when
1740 * setting an idle callback.
1741 * This function should be called by application-side implementors (ie,
1742 * at-spi2-atk) when it is desirable to re-enter the main loop.
1745 atspi_set_main_context (GMainContext *cnx)
1747 if (atspi_main_context == cnx)
1749 if (process_deferred_messages_source != NULL)
1751 g_source_destroy (process_deferred_messages_source);
1752 process_deferred_messages_source = g_idle_source_new ();
1753 g_source_set_callback (process_deferred_messages_source,
1754 process_deferred_messages_callback, NULL, NULL);
1755 g_source_attach (process_deferred_messages_source, cnx);
1756 g_source_unref (process_deferred_messages_source);
1758 atspi_main_context = cnx;
1759 atspi_dbus_connection_setup_with_g_main (atspi_get_a11y_bus (), cnx);
1764 for (i = desktop->children->len - 1; i >= 0; i--)
1766 AtspiAccessible *child = g_ptr_array_index (desktop->children, i);
1767 if (child->parent.app && child->parent.app->bus)
1768 atspi_dbus_connection_setup_with_g_main (child->parent.app->bus, cnx);
1773 #ifdef DEBUG_REF_COUNTS
1775 print_disposed (gpointer key, gpointer value, gpointer data)
1777 AtspiAccessible *accessible = key;
1778 if (accessible->parent.app)
1780 g_print ("disposed: %s %d\n", accessible->name, accessible->role);
1786 g_hash_table_foreach (live_refs, print_disposed, NULL);
1791 _atspi_name_compat (gchar *name)
1805 * atspi_role_get_name:
1806 * @role: an #AtspiRole object to query.
1808 * Gets a localizable string that indicates the name of an #AtspiRole.
1809 * <em>DEPRECATED.</em>
1811 * Returns: a localizable string name for an #AtspiRole enumerated type.
1814 atspi_role_get_name (AtspiRole role)
1816 gchar *retval = NULL;
1817 GTypeClass *type_class;
1820 type_class = g_type_class_ref (ATSPI_TYPE_ROLE);
1821 g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), NULL);
1823 value = g_enum_get_value (G_ENUM_CLASS (type_class), role);
1827 retval = g_strdup (value->value_nick);
1830 g_type_class_unref (type_class);
1833 return _atspi_name_compat (retval);
1839 _atspi_dbus_update_cache_from_dict (AtspiAccessible *accessible, DBusMessageIter *iter)
1841 GHashTable *cache = _atspi_accessible_ref_cache (accessible);
1842 DBusMessageIter iter_dict, iter_dict_entry, iter_struct, iter_variant;
1844 dbus_message_iter_recurse (iter, &iter_dict);
1845 while (dbus_message_iter_get_arg_type (&iter_dict) != DBUS_TYPE_INVALID)
1849 dbus_message_iter_recurse (&iter_dict, &iter_dict_entry);
1850 dbus_message_iter_get_basic (&iter_dict_entry, &key);
1851 dbus_message_iter_next (&iter_dict_entry);
1852 dbus_message_iter_recurse (&iter_dict_entry, &iter_variant);
1853 if (!strcmp (key, "interfaces"))
1855 _atspi_dbus_set_interfaces (accessible, &iter_variant);
1857 else if (!strcmp (key, "Attributes"))
1859 char *iter_sig = dbus_message_iter_get_signature (&iter_variant);
1860 val = g_new0 (GValue, 1);;
1861 g_value_init (val, G_TYPE_HASH_TABLE);
1862 if (strcmp (iter_sig, "a{ss}") != 0)
1864 dbus_free (iter_sig);
1867 dbus_free (iter_sig);
1868 g_value_take_boxed (val, _atspi_dbus_hash_from_iter (&iter_variant));
1870 else if (!strcmp (key, "Component.ScreenExtents"))
1874 char *iter_sig = dbus_message_iter_get_signature (&iter_variant);
1875 val = g_new0 (GValue, 1);;
1876 g_value_init (val, ATSPI_TYPE_RECT);
1877 if (strcmp (iter_sig, "(iiii)") != 0)
1879 dbus_free (iter_sig);
1882 dbus_free (iter_sig);
1883 dbus_message_iter_recurse (&iter_variant, &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);
1889 dbus_message_iter_next (&iter_struct);
1890 dbus_message_iter_get_basic (&iter_struct, &d_int);
1891 extents.width = d_int;
1892 dbus_message_iter_next (&iter_struct);
1893 dbus_message_iter_get_basic (&iter_struct, &d_int);
1894 extents.height = d_int;
1895 g_value_set_boxed (val, &extents);
1898 g_hash_table_insert (cache, g_strdup (key), val);
1899 dbus_message_iter_next (&iter_dict);
1906 _atspi_get_allow_sync ()
1912 _atspi_set_allow_sync (gboolean val)
1914 gboolean ret = allow_sync;
1921 _atspi_set_error_no_sync (GError **error)
1923 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_SYNC_NOT_ALLOWED,
1924 _("Attempted synchronous call where prohibited"));
1927 static const char *sr_introspection = "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
1928 "\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
1929 "<node name=\"/org/a11y/atspi/screenreader\">\n"
1930 " <interface name=\"org.a11y.Atspi.ScreenReader\">\n"
1931 " <signal name=\"ReadingPosition\">\n"
1932 " <arg type=\"i\"/>\n"
1933 " <arg type=\"i\"/>\n"
1938 static DBusHandlerResult
1939 screen_reader_filter (DBusConnection *bus, DBusMessage *message, void *user_data)
1941 if (dbus_message_is_method_call (message, DBUS_INTERFACE_INTROSPECTABLE,
1944 DBusMessage *reply = dbus_message_new_method_return (message);
1945 dbus_message_append_args (reply, DBUS_TYPE_STRING, &sr_introspection,
1947 dbus_connection_send (bus, reply, NULL);
1948 dbus_message_unref (reply);
1949 return DBUS_HANDLER_RESULT_HANDLED;
1951 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1955 _atspi_prepare_screen_reader_interface ()
1957 static gint initialized = 0;
1958 DBusConnection *a11y_bus = _atspi_bus ();
1961 return (initialized > 0);
1963 if (dbus_bus_request_name (a11y_bus, "org.a11y.Atspi.ScreenReader", 0, NULL) < 0)
1970 dbus_connection_add_filter (a11y_bus, screen_reader_filter, NULL, NULL);
1975 _atspi_strdup_and_adjust_for_dbus (const char *s)
1977 gchar *d = g_strdup (s);
1984 for (p = d; *p; p++)
1988 memmove (p, p + 1, g_utf8_strlen (p, -1));
1996 p [1] = toupper (p [1]);
2000 d [0] = toupper (d [0]);