2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2001, 2002 Sun Microsystems Inc.,
6 * Copyright 2001, 2002 Ximian, Inc.
7 * Copyright 2010, 2011 Novell, Inc.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
27 * Basic SPI initialization and event loop function prototypes
31 #include "atspi-private.h"
35 #include "atspi-gmain.h"
39 static void handle_get_items (DBusPendingCall *pending, void *user_data);
41 static DBusConnection *bus = NULL;
42 static GHashTable *live_refs = NULL;
43 static gint method_call_timeout = 800;
44 static gint app_startup_time = 15000;
45 static gboolean allow_sync = TRUE;
47 GMainLoop *atspi_main_loop;
48 GMainContext *atspi_main_context;
49 gboolean atspi_no_cache;
51 const char *atspi_path_dec = ATSPI_DBUS_PATH_DEC;
52 const char *atspi_path_registry = ATSPI_DBUS_PATH_REGISTRY;
53 const char *atspi_path_root = ATSPI_DBUS_PATH_ROOT;
54 const char *atspi_bus_registry = ATSPI_DBUS_NAME_REGISTRY;
55 const char *atspi_interface_accessible = ATSPI_DBUS_INTERFACE_ACCESSIBLE;
56 const char *atspi_interface_action = ATSPI_DBUS_INTERFACE_ACTION;
57 const char *atspi_interface_application = ATSPI_DBUS_INTERFACE_APPLICATION;
58 const char *atspi_interface_collection = ATSPI_DBUS_INTERFACE_COLLECTION;
59 const char *atspi_interface_component = ATSPI_DBUS_INTERFACE_COMPONENT;
60 const char *atspi_interface_dec = ATSPI_DBUS_INTERFACE_DEC;
61 const char *atspi_interface_device_event_listener = ATSPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER;
62 const char *atspi_interface_document = ATSPI_DBUS_INTERFACE_DOCUMENT;
63 const char *atspi_interface_editable_text = ATSPI_DBUS_INTERFACE_EDITABLE_TEXT;
64 const char *atspi_interface_event_object = ATSPI_DBUS_INTERFACE_EVENT_OBJECT;
65 const char *atspi_interface_hyperlink = ATSPI_DBUS_INTERFACE_HYPERLINK;
66 const char *atspi_interface_hypertext = ATSPI_DBUS_INTERFACE_HYPERTEXT;
67 const char *atspi_interface_image = ATSPI_DBUS_INTERFACE_IMAGE;
68 const char *atspi_interface_registry = ATSPI_DBUS_INTERFACE_REGISTRY;
69 const char *atspi_interface_selection = ATSPI_DBUS_INTERFACE_SELECTION;
70 const char *atspi_interface_table = ATSPI_DBUS_INTERFACE_TABLE;
71 const char *atspi_interface_table_cell = ATSPI_DBUS_INTERFACE_TABLE_CELL;
72 const char *atspi_interface_text = ATSPI_DBUS_INTERFACE_TEXT;
73 const char *atspi_interface_cache = ATSPI_DBUS_INTERFACE_CACHE;
74 const char *atspi_interface_value = ATSPI_DBUS_INTERFACE_VALUE;
76 static const char *interfaces[] =
78 ATSPI_DBUS_INTERFACE_ACCESSIBLE,
79 ATSPI_DBUS_INTERFACE_ACTION,
80 ATSPI_DBUS_INTERFACE_APPLICATION,
81 ATSPI_DBUS_INTERFACE_COLLECTION,
82 ATSPI_DBUS_INTERFACE_COMPONENT,
83 ATSPI_DBUS_INTERFACE_DOCUMENT,
84 ATSPI_DBUS_INTERFACE_EDITABLE_TEXT,
85 ATSPI_DBUS_INTERFACE_HYPERLINK,
86 ATSPI_DBUS_INTERFACE_HYPERTEXT,
87 ATSPI_DBUS_INTERFACE_IMAGE,
88 "org.a11y.atspi.LoginHelper",
89 ATSPI_DBUS_INTERFACE_SELECTION,
90 ATSPI_DBUS_INTERFACE_TABLE,
91 ATSPI_DBUS_INTERFACE_TABLE_CELL,
92 ATSPI_DBUS_INTERFACE_TEXT,
93 ATSPI_DBUS_INTERFACE_VALUE,
98 _atspi_get_iface_num (const char *iface)
100 /* TODO: Use a binary search or hash to improve performance */
103 for (i = 0; interfaces[i]; i++)
105 if (!strcmp(iface, interfaces[i])) return i;
111 _atspi_get_live_refs (void)
115 live_refs = g_hash_table_new (g_direct_hash, g_direct_equal);
120 /* TODO: Add an application parameter */
127 g_error ("AT-SPI: Couldn't connect to accessibility bus. Is at-spi-bus-launcher running?");
131 #define APP_IS_REGISTRY(app) (!strcmp (app->bus_name, atspi_bus_registry))
133 static AtspiAccessible *desktop;
145 g_hash_table_destroy (refs);
150 dbus_connection_close (bus);
151 dbus_connection_unref (bus);
158 /* TODO: Do we need this code, or should we just dispose the desktop? */
159 for (i = desktop->children->len - 1; i >= 0; i--)
161 AtspiAccessible *child = g_ptr_array_index (desktop->children, i);
162 if (child->parent.app)
163 g_object_run_dispose (G_OBJECT (child->parent.app));
164 g_object_run_dispose (G_OBJECT (child));
167 g_object_run_dispose (G_OBJECT (desktop->parent.app));
168 g_object_unref (desktop);
172 static gboolean atspi_inited = FALSE;
174 static GHashTable *app_hash = NULL;
177 handle_get_bus_address (DBusPendingCall *pending, void *user_data)
179 AtspiApplication *app = user_data;
180 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
181 DBusMessage *message;
183 DBusPendingCall *new_pending;
186 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
188 if (dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &address,
189 DBUS_TYPE_INVALID) && address [0])
194 dbus_error_init (&error);
195 bus = dbus_connection_open_private (address, &error);
200 dbus_connection_unref (app->bus);
203 atspi_dbus_connection_setup_with_g_main(bus, g_main_context_default());
207 if (!strcmp (error.name, DBUS_ERROR_FILE_NOT_FOUND))
208 g_warning ("AT-SPI: Unable to open bus connection: %s", error.message);
209 dbus_error_free (&error);
213 dbus_message_unref (reply);
214 dbus_pending_call_unref (pending);
217 return; /* application has gone away / been disposed */
219 message = dbus_message_new_method_call (app->bus_name,
220 "/org/a11y/atspi/cache",
221 atspi_interface_cache, "GetItems");
223 result = dbus_connection_send_with_reply (app->bus, message, &new_pending, 2000);
224 dbus_message_unref (message);
225 if (!result || !new_pending)
227 dbus_pending_call_set_notify (new_pending, handle_get_items, app, NULL);
230 static AtspiApplication *
231 get_application (const char *bus_name)
233 AtspiApplication *app = NULL;
235 DBusMessage *message;
236 DBusPendingCall *pending = NULL;
241 app_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_object_unref);
242 if (!app_hash) return NULL;
244 app = g_hash_table_lookup (app_hash, bus_name);
246 bus_name_dup = g_strdup (bus_name);
247 if (!bus_name_dup) return NULL;
248 // TODO: change below to something that will send state-change:defunct notification if necessary */
249 app = _atspi_application_new (bus_name);
250 app->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
251 app->bus = dbus_connection_ref (_atspi_bus ());
252 gettimeofday (&app->time_added, NULL);
253 app->cache = ATSPI_CACHE_UNDEFINED;
254 g_hash_table_insert (app_hash, bus_name_dup, app);
255 message = dbus_message_new_method_call (bus_name, atspi_path_root,
256 atspi_interface_application, "GetApplicationBusAddress");
258 result = dbus_connection_send_with_reply (app->bus, message, &pending, 2000);
259 dbus_message_unref (message);
260 if (!result || !pending)
262 g_hash_table_remove (app_hash, bus_name_dup);
265 dbus_pending_call_set_notify (pending, handle_get_bus_address, app, NULL);
270 ref_accessible (const char *app_name, const char *path)
272 AtspiApplication *app;
275 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
278 app = get_application (app_name);
280 if (!strcmp (path, "/org/a11y/atspi/accessible/root"))
284 app->root = _atspi_accessible_new (app, atspi_path_root);
285 app->root->accessible_parent = atspi_get_desktop (0);
286 g_ptr_array_add (app->root->accessible_parent->children, g_object_ref (app->root));
288 return g_object_ref (app->root);
291 a = g_hash_table_lookup (app->hash, path);
294 return g_object_ref (a);
296 a = _atspi_accessible_new (app, path);
299 g_hash_table_insert (app->hash, g_strdup (a->parent.path), g_object_ref (a));
303 static AtspiHyperlink *
304 ref_hyperlink (const char *app_name, const char *path)
306 AtspiApplication *app = get_application (app_name);
307 AtspiHyperlink *hyperlink;
309 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
312 hyperlink = g_hash_table_lookup (app->hash, path);
315 return g_object_ref (hyperlink);
317 hyperlink = _atspi_hyperlink_new (app, path);
318 g_hash_table_insert (app->hash, g_strdup (hyperlink->parent.path), hyperlink);
319 /* TODO: This should be a weak ref */
320 g_object_ref (hyperlink); /* for the hash */
333 GArray *state_bitflags;
336 static DBusHandlerResult
337 handle_remove_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
339 const char *sender = dbus_message_get_sender (message);
340 AtspiApplication *app;
342 DBusMessageIter iter, iter_struct;
343 const char *signature = dbus_message_get_signature (message);
346 if (strcmp (signature, "(so)") != 0)
348 g_warning ("AT-SPI: Unknown signature %s for RemoveAccessible", signature);
349 return DBUS_HANDLER_RESULT_HANDLED;
352 dbus_message_iter_init (message, &iter);
353 dbus_message_iter_recurse (&iter, &iter_struct);
354 dbus_message_iter_get_basic (&iter_struct, &sender);
355 dbus_message_iter_next (&iter_struct);
356 dbus_message_iter_get_basic (&iter_struct, &path);
357 app = get_application (sender);
358 a = ref_accessible (sender, path);
360 return DBUS_HANDLER_RESULT_HANDLED;
361 g_object_run_dispose (G_OBJECT (a));
362 g_hash_table_remove (app->hash, a->parent.path);
363 g_object_unref (a); /* unref our own ref */
364 return DBUS_HANDLER_RESULT_HANDLED;
367 static DBusHandlerResult
368 handle_name_owner_changed (DBusConnection *bus, DBusMessage *message, void *user_data)
370 const char *name, *new, *old;
371 static gboolean registry_lost = FALSE;
373 if (!dbus_message_get_args (message, NULL,
374 DBUS_TYPE_STRING, &name,
375 DBUS_TYPE_STRING, &old,
376 DBUS_TYPE_STRING, &new,
379 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
382 if (!strcmp (name, "org.a11y.atspi.Registry"))
384 if (registry_lost && !old[0])
386 _atspi_reregister_event_listeners ();
387 _atspi_reregister_device_listeners ();
388 registry_lost = FALSE;
391 registry_lost = TRUE;
395 AtspiApplication *app = g_hash_table_lookup (app_hash, old);
396 if (app && app->bus_name && !strcmp(app->bus_name, name))
397 g_object_run_dispose (G_OBJECT (app));
399 return DBUS_HANDLER_RESULT_HANDLED;
403 add_app_to_desktop (AtspiAccessible *a, const char *bus_name)
405 AtspiAccessible *obj = ref_accessible (bus_name, atspi_path_root);
406 /* The app will be added to the desktop as a side-effect of calling
408 g_object_unref (obj);
409 return (obj != NULL);
413 get_reference_from_iter (DBusMessageIter *iter, const char **app_name, const char **path)
415 DBusMessageIter iter_struct;
417 dbus_message_iter_recurse (iter, &iter_struct);
418 dbus_message_iter_get_basic (&iter_struct, app_name);
419 dbus_message_iter_next (&iter_struct);
420 dbus_message_iter_get_basic (&iter_struct, path);
421 dbus_message_iter_next (iter);
425 add_accessible_from_iter (DBusMessageIter *iter)
427 DBusMessageIter iter_struct, iter_array;
428 const char *app_name, *path;
429 AtspiAccessible *accessible;
430 const char *name, *description;
432 gboolean children_cached = FALSE;
433 dbus_int32_t count, index;
435 dbus_message_iter_recurse (iter, &iter_struct);
438 get_reference_from_iter (&iter_struct, &app_name, &path);
439 accessible = ref_accessible (app_name, path);
443 /* Get application: TODO */
444 dbus_message_iter_next (&iter_struct);
447 get_reference_from_iter (&iter_struct, &app_name, &path);
448 if (accessible->accessible_parent)
449 g_object_unref (accessible->accessible_parent);
450 accessible->accessible_parent = ref_accessible (app_name, path);
452 if (dbus_message_iter_get_arg_type (&iter_struct) == 'i')
454 /* Get index in parent */
455 dbus_message_iter_get_basic (&iter_struct, &index);
456 if (index >= 0 && accessible->accessible_parent)
458 if (index >= accessible->accessible_parent->children->len)
459 g_ptr_array_set_size (accessible->accessible_parent->children, index + 1);
460 g_ptr_array_index (accessible->accessible_parent->children, index) = g_object_ref (accessible);
463 /* get child count */
464 dbus_message_iter_next (&iter_struct);
465 dbus_message_iter_get_basic (&iter_struct, &count);
468 g_ptr_array_set_size (accessible->children, count);
469 children_cached = TRUE;
472 else if (dbus_message_iter_get_arg_type (&iter_struct) == 'a')
474 /* It's the old API with a list of children */
475 /* TODO: Perhaps remove this code eventually */
476 dbus_message_iter_recurse (&iter_struct, &iter_array);
477 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
479 AtspiAccessible *child;
480 get_reference_from_iter (&iter_array, &app_name, &path);
481 child = ref_accessible (app_name, path);
482 g_ptr_array_remove (accessible->children, child);
483 g_ptr_array_add (accessible->children, child);
485 children_cached = TRUE;
489 dbus_message_iter_next (&iter_struct);
490 _atspi_dbus_set_interfaces (accessible, &iter_struct);
491 dbus_message_iter_next (&iter_struct);
494 if (accessible->name)
495 g_free (accessible->name);
496 dbus_message_iter_get_basic (&iter_struct, &name);
497 accessible->name = g_strdup (name);
498 dbus_message_iter_next (&iter_struct);
501 dbus_message_iter_get_basic (&iter_struct, &role);
502 accessible->role = role;
503 dbus_message_iter_next (&iter_struct);
506 if (accessible->description)
507 g_free (accessible->description);
508 dbus_message_iter_get_basic (&iter_struct, &description);
509 accessible->description = g_strdup (description);
510 dbus_message_iter_next (&iter_struct);
512 _atspi_dbus_set_state (accessible, &iter_struct);
513 dbus_message_iter_next (&iter_struct);
515 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_NAME | ATSPI_CACHE_ROLE |
516 ATSPI_CACHE_PARENT | ATSPI_CACHE_DESCRIPTION);
517 if (!atspi_state_set_contains (accessible->states,
518 ATSPI_STATE_MANAGES_DESCENDANTS) &&
520 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_CHILDREN);
522 /* This is a bit of a hack since the cache holds a ref, so we don't need
523 * the one provided for us anymore */
524 g_object_unref (accessible);
528 handle_get_items (DBusPendingCall *pending, void *user_data)
530 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
531 DBusMessageIter iter, iter_array;
533 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
535 const char *sender = dbus_message_get_sender (reply);
536 const char *error = NULL;
537 const char *error_name = dbus_message_get_error_name (reply);
538 if (!strcmp (error_name, DBUS_ERROR_SERVICE_UNKNOWN)
539 || !strcmp (error_name, DBUS_ERROR_NO_REPLY))
544 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &error,
546 g_warning ("AT-SPI: Error in GetItems, sender=%s, error=%s", sender, error);
548 dbus_message_unref (reply);
549 dbus_pending_call_unref (pending);
553 dbus_message_iter_init (reply, &iter);
554 dbus_message_iter_recurse (&iter, &iter_array);
555 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
557 add_accessible_from_iter (&iter_array);
558 dbus_message_iter_next (&iter_array);
560 dbus_message_unref (reply);
561 dbus_pending_call_unref (pending);
564 /* TODO: Do we stil need this function? */
565 static AtspiAccessible *
566 ref_accessible_desktop (AtspiApplication *app)
569 DBusMessage *message, *reply;
570 DBusMessageIter iter, iter_array;
575 g_object_ref (desktop);
578 desktop = _atspi_accessible_new (app, atspi_path_root);
583 g_hash_table_insert (app->hash, g_strdup (desktop->parent.path),
584 g_object_ref (desktop));
585 app->root = g_object_ref (desktop);
586 desktop->name = g_strdup ("main");
587 message = dbus_message_new_method_call (atspi_bus_registry,
589 atspi_interface_accessible,
594 reply = _atspi_dbus_send_with_reply_and_block (message, &error);
595 if (!reply || strcmp (dbus_message_get_signature (reply), "a(so)") != 0)
599 g_warning ("Couldn't get application list: %s", error->message);
600 g_clear_error (&error);
603 dbus_message_unref (reply);
606 dbus_message_iter_init (reply, &iter);
607 dbus_message_iter_recurse (&iter, &iter_array);
608 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
610 const char *app_name, *path;
611 get_reference_from_iter (&iter_array, &app_name, &path);
612 add_app_to_desktop (desktop, app_name);
615 /* Record the alternate name as an alias for org.a11y.atspi.Registry */
616 bus_name_dup = g_strdup (dbus_message_get_sender (reply));
618 g_hash_table_insert (app_hash, bus_name_dup, app);
620 dbus_message_unref (reply);
622 return g_object_ref (desktop);
626 _atspi_ref_accessible (const char *app, const char *path)
628 AtspiApplication *a = get_application (app);
631 if ( APP_IS_REGISTRY(a))
634 g_object_unref (ref_accessible_desktop (a)); /* sets a->root */
635 return g_object_ref (a->root);
637 return ref_accessible (app, path);
641 _atspi_dbus_return_accessible_from_message (DBusMessage *message)
643 DBusMessageIter iter;
644 AtspiAccessible *retval = NULL;
645 const char *signature;
650 signature = dbus_message_get_signature (message);
651 if (!strcmp (signature, "(so)"))
653 dbus_message_iter_init (message, &iter);
654 retval = _atspi_dbus_return_accessible_from_iter (&iter);
658 g_warning ("AT-SPI: Called _atspi_dbus_return_accessible_from_message with strange signature %s", signature);
660 dbus_message_unref (message);
665 _atspi_dbus_return_accessible_from_iter (DBusMessageIter *iter)
667 const char *app_name, *path;
669 get_reference_from_iter (iter, &app_name, &path);
670 return ref_accessible (app_name, path);
674 _atspi_dbus_return_hyperlink_from_message (DBusMessage *message)
676 DBusMessageIter iter;
677 AtspiHyperlink *retval = NULL;
678 const char *signature;
683 signature = dbus_message_get_signature (message);
684 if (!strcmp (signature, "(so)"))
686 dbus_message_iter_init (message, &iter);
687 retval = _atspi_dbus_return_hyperlink_from_iter (&iter);
691 g_warning ("AT-SPI: Called _atspi_dbus_return_hyperlink_from_message with strange signature %s", signature);
693 dbus_message_unref (message);
698 _atspi_dbus_return_hyperlink_from_iter (DBusMessageIter *iter)
700 const char *app_name, *path;
702 get_reference_from_iter (iter, &app_name, &path);
703 return ref_hyperlink (app_name, path);
706 const char *cache_signal_type = "((so)(so)(so)iiassusau)";
707 const char *old_cache_signal_type = "((so)(so)(so)a(so)assusau)";
709 static DBusHandlerResult
710 handle_add_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
712 DBusMessageIter iter;
713 const char *signature = dbus_message_get_signature (message);
715 if (strcmp (signature, cache_signal_type) != 0 &&
716 strcmp (signature, old_cache_signal_type) != 0)
718 g_warning ("AT-SPI: AddAccessible with unknown signature %s\n", signature);
719 return DBUS_HANDLER_RESULT_HANDLED;
722 dbus_message_iter_init (message, &iter);
723 add_accessible_from_iter (&iter);
724 return DBUS_HANDLER_RESULT_HANDLED;
730 DBusMessage *message;
734 static GSource *process_deferred_messages_source = NULL;
737 process_deferred_message (BusDataClosure *closure)
739 int type = dbus_message_get_type (closure->message);
740 const char *interface = dbus_message_get_interface (closure->message);
742 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
743 !strncmp (interface, "org.a11y.atspi.Event.", 21))
745 _atspi_dbus_handle_event (closure->bus, closure->message, closure->data);
747 if (dbus_message_is_method_call (closure->message, atspi_interface_device_event_listener, "NotifyEvent"))
749 _atspi_dbus_handle_DeviceEvent (closure->bus,
750 closure->message, closure->data);
752 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "AddAccessible"))
754 handle_add_accessible (closure->bus, closure->message, closure->data);
756 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "RemoveAccessible"))
758 handle_remove_accessible (closure->bus, closure->message, closure->data);
760 if (dbus_message_is_signal (closure->message, "org.freedesktop.DBus", "NameOwnerChanged"))
762 handle_name_owner_changed (closure->bus, closure->message, closure->data);
766 static GQueue *deferred_messages = NULL;
769 process_deferred_messages (void)
771 static int in_process_deferred_messages = 0;
772 BusDataClosure *closure;
774 if (in_process_deferred_messages)
776 in_process_deferred_messages = 1;
777 while ((closure = g_queue_pop_head (deferred_messages)))
779 process_deferred_message (closure);
780 dbus_message_unref (closure->message);
781 dbus_connection_unref (closure->bus);
784 in_process_deferred_messages = 0;
789 process_deferred_messages_callback (gpointer data)
791 if (process_deferred_messages ())
792 return G_SOURCE_CONTINUE;
794 process_deferred_messages_source = NULL;
795 return G_SOURCE_REMOVE;
798 static DBusHandlerResult
799 defer_message (DBusConnection *connection, DBusMessage *message, void *user_data)
801 BusDataClosure *closure = g_new (BusDataClosure, 1);
803 closure->bus = dbus_connection_ref (bus);
804 closure->message = dbus_message_ref (message);
805 closure->data = user_data;
807 g_queue_push_tail (deferred_messages, closure);
809 if (process_deferred_messages_source == NULL)
811 process_deferred_messages_source = g_idle_source_new ();
812 g_source_set_callback (process_deferred_messages_source,
813 process_deferred_messages_callback, NULL, NULL);
814 g_source_attach (process_deferred_messages_source, atspi_main_context);
815 g_source_unref (process_deferred_messages_source);
818 return DBUS_HANDLER_RESULT_HANDLED;
821 static DBusHandlerResult
822 atspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
824 int type = dbus_message_get_type (message);
825 const char *interface = dbus_message_get_interface (message);
827 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
828 !strncmp (interface, "org.a11y.atspi.Event.", 21))
830 return defer_message (bus, message, data);
832 if (dbus_message_is_method_call (message, atspi_interface_device_event_listener, "NotifyEvent"))
834 return defer_message (bus, message, data);
836 if (dbus_message_is_signal (message, atspi_interface_cache, "AddAccessible"))
838 return defer_message (bus, message, data);
840 if (dbus_message_is_signal (message, atspi_interface_cache, "RemoveAccessible"))
842 return defer_message (bus, message, data);
844 if (dbus_message_is_signal (message, "org.freedesktop.DBus", "NameOwnerChanged"))
846 defer_message (bus, message, data);
847 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
849 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
853 * Returns a 'canonicalized' value for DISPLAY,
854 * with the screen number stripped off if present.
856 * TODO: Avoid having duplicate functions for this here and in at-spi2-atk
859 spi_display_name (void)
861 char *canonical_display_name = NULL;
862 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
866 display_env = g_getenv ("DISPLAY");
867 if (!display_env || !display_env[0])
871 gchar *display_p, *screen_p;
872 canonical_display_name = g_strdup (display_env);
873 display_p = g_utf8_strrchr (canonical_display_name, -1, ':');
874 screen_p = g_utf8_strrchr (canonical_display_name, -1, '.');
875 if (screen_p && display_p && (screen_p > display_p))
883 canonical_display_name = g_strdup (display_env);
886 return canonical_display_name;
892 * Connects to the accessibility registry and initializes the SPI.
894 * Returns: 0 on success, 1 if already initialized, or an integer error code.
900 const gchar *no_cache;
909 _atspi_get_live_refs();
911 bus = atspi_get_a11y_bus ();
914 if (!dbus_bus_register (bus, NULL))
916 atspi_dbus_connection_setup_with_g_main(bus, g_main_context_default());
917 dbus_connection_add_filter (bus, atspi_dbus_filter, NULL, NULL);
918 match = g_strdup_printf ("type='signal',interface='%s',member='AddAccessible'", atspi_interface_cache);
919 dbus_bus_add_match (bus, match, NULL);
921 match = g_strdup_printf ("type='signal',interface='%s',member='RemoveAccessible'", atspi_interface_cache);
922 dbus_bus_add_match (bus, match, NULL);
924 match = g_strdup_printf ("type='signal',interface='%s',member='ChildrenChanged'", atspi_interface_event_object);
925 dbus_bus_add_match (bus, match, NULL);
927 match = g_strdup_printf ("type='signal',interface='%s',member='PropertyChange'", atspi_interface_event_object);
928 dbus_bus_add_match (bus, match, NULL);
930 match = g_strdup_printf ("type='signal',interface='%s',member='StateChanged'", atspi_interface_event_object);
931 dbus_bus_add_match (bus, match, NULL);
934 dbus_bus_add_match (bus,
935 "type='signal', interface='org.freedesktop.DBus', member='NameOwnerChanged'",
938 no_cache = g_getenv ("ATSPI_NO_CACHE");
939 if (no_cache && g_strcmp0 (no_cache, "0") != 0)
940 atspi_no_cache = TRUE;
942 deferred_messages = g_queue_new ();
948 * atspi_is_initialized:
950 * Indicates whether AT-SPI has been initialized.
952 * Returns: %True if initialized; %False otherwise.
955 atspi_is_initialized ()
963 * Starts/enters the main event loop for the AT-SPI services.
965 * NOTE: This method does not return control; it is exited via a call to
966 * #atspi_event_quit from within an event handler.
970 atspi_event_main (void)
972 atspi_main_loop = g_main_loop_new (NULL, FALSE);
973 g_main_loop_run (atspi_main_loop);
974 g_main_loop_unref(atspi_main_loop);
975 atspi_main_loop = NULL;
981 * Quits the last main event loop for the AT-SPI services,
982 * See: #atspi_event_main
985 atspi_event_quit (void)
987 g_main_loop_quit (atspi_main_loop);
993 * Disconnects from #AtspiRegistry instances and releases
994 * any floating resources. Call only once at exit.
996 * Returns: 0 if there were no leaks, otherwise other integer values.
1008 atspi_inited = FALSE;
1012 leaked = g_hash_table_size (live_refs);
1024 static GSList *hung_processes;
1027 remove_hung_process (DBusPendingCall *pending, void *data)
1029 hung_processes = g_slist_remove (hung_processes, data);
1031 dbus_pending_call_unref (pending);
1035 check_for_hang (DBusMessage *message, DBusError *error, DBusConnection *bus, const char *bus_name)
1037 if (!message && error->name &&
1038 !strcmp (error->name, "org.freedesktop.DBus.Error.NoReply"))
1041 DBusMessage *message;
1042 gchar *bus_name_dup;
1043 DBusPendingCall *pending = NULL;
1045 for (l = hung_processes; l; l = l->next)
1046 if (!strcmp (l->data, bus_name))
1048 message = dbus_message_new_method_call (bus_name, "/",
1049 "org.freedesktop.DBus.Peer",
1053 result = dbus_connection_send_with_reply (bus, message, &pending, -1);
1054 dbus_message_unref (message);
1055 if (!result || !pending)
1057 bus_name_dup = g_strdup (bus_name);
1058 hung_processes = g_slist_append (hung_processes, bus_name_dup);
1059 dbus_pending_call_set_notify (pending, remove_hung_process, bus_name_dup, NULL);
1064 connection_is_hung (const char *bus_name)
1068 for (l = hung_processes; l; l = l->next)
1069 if (!strcmp (l->data, bus_name))
1075 check_app (AtspiApplication *app, GError **error)
1077 if (!app || !app->bus)
1079 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
1080 _("The application no longer exists"));
1084 if (atspi_main_loop && connection_is_hung (app->bus_name))
1086 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC,
1087 "The process appears to be hung.");
1095 set_timeout (AtspiApplication *app)
1100 if (app && app_startup_time > 0)
1102 gettimeofday (&tv, NULL);
1103 diff = (tv.tv_sec - app->time_added.tv_sec) * 1000 + (tv.tv_usec - app->time_added.tv_usec) / 1000;
1104 dbind_set_timeout (MAX(method_call_timeout, app_startup_time - diff));
1107 dbind_set_timeout (method_call_timeout);
1111 _atspi_dbus_call (gpointer obj, const char *interface, const char *method, GError **error, const char *type, ...)
1116 AtspiObject *aobj = ATSPI_OBJECT (obj);
1118 if (!check_app (aobj->app, error))
1123 _atspi_set_error_no_sync (error);
1127 va_start (args, type);
1128 dbus_error_init (&err);
1129 set_timeout (aobj->app);
1130 retval = dbind_method_call_reentrant_va (aobj->app->bus, aobj->app->bus_name,
1131 aobj->path, interface, method, &err,
1134 check_for_hang (NULL, &err, aobj->app->bus, aobj->app->bus_name);
1135 process_deferred_messages ();
1136 if (dbus_error_is_set (&err))
1138 g_set_error(error, ATSPI_ERROR, ATSPI_ERROR_IPC, "%s", err.message);
1139 dbus_error_free (&err);
1145 _atspi_dbus_call_partial (gpointer obj,
1146 const char *interface,
1149 const char *type, ...)
1152 DBusMessage * result;
1153 va_start (args, type);
1154 result = _atspi_dbus_call_partial_va (obj, interface, method, error, type, args);
1161 _atspi_dbus_call_partial_va (gpointer obj,
1162 const char *interface,
1168 AtspiObject *aobj = ATSPI_OBJECT (obj);
1170 DBusMessage *msg = NULL, *reply = NULL;
1171 DBusMessageIter iter;
1174 dbus_error_init (&err);
1176 if (!check_app (aobj->app, error))
1179 msg = dbus_message_new_method_call (aobj->app->bus_name, aobj->path, interface, method);
1184 dbus_message_iter_init_append (msg, &iter);
1185 dbind_any_marshal_va (&iter, &p, args);
1187 set_timeout (aobj->app);
1188 reply = dbind_send_and_allow_reentry (aobj->app->bus, msg, &err);
1189 check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1193 dbus_message_unref (msg);
1194 process_deferred_messages ();
1195 if (dbus_error_is_set (&err))
1197 g_set_error_literal(error, ATSPI_ERROR, ATSPI_ERROR_IPC, err.message);
1198 dbus_error_free (&err);
1200 dbus_message_unref(reply);
1203 else if (reply && dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1205 const char *err_str = NULL;
1206 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &err_str, DBUS_TYPE_INVALID);
1208 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err_str);
1209 dbus_message_unref (reply);
1216 _atspi_dbus_get_property (gpointer obj, const char *interface, const char *name, GError **error, const char *type, void *data)
1218 DBusMessage *message, *reply;
1219 DBusMessageIter iter, iter_variant;
1221 dbus_bool_t retval = FALSE;
1222 AtspiObject *aobj = ATSPI_OBJECT (obj);
1223 char expected_type = (type [0] == '(' ? 'r' : type [0]);
1228 if (!check_app (aobj->app, error))
1233 _atspi_set_error_no_sync (error);
1237 message = dbus_message_new_method_call (aobj->app->bus_name,
1239 "org.freedesktop.DBus.Properties",
1243 // TODO: throw exception
1246 dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1247 dbus_error_init (&err);
1248 set_timeout (aobj->app);
1249 reply = dbind_send_and_allow_reentry (aobj->app->bus, message, &err);
1250 check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1251 dbus_message_unref (message);
1252 process_deferred_messages ();
1255 // TODO: throw exception
1259 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
1261 const char *err_str = NULL;
1262 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &err_str, DBUS_TYPE_INVALID);
1264 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err_str);
1268 dbus_message_iter_init (reply, &iter);
1269 if (dbus_message_iter_get_arg_type (&iter) != 'v')
1271 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));
1274 dbus_message_iter_recurse (&iter, &iter_variant);
1275 if (dbus_message_iter_get_arg_type (&iter_variant) != expected_type)
1277 g_warning ("atspi_dbus_get_property: Wrong type: expected %s, got %c\n", type, dbus_message_iter_get_arg_type (&iter_variant));
1280 if (!strcmp (type, "(so)"))
1282 *((AtspiAccessible **)data) = _atspi_dbus_return_accessible_from_iter (&iter_variant);
1286 dbus_message_iter_get_basic (&iter_variant, data);
1287 if (type [0] == 's')
1288 *(char **)data = g_strdup (*(char **)data);
1292 dbus_error_free (&err);
1294 dbus_message_unref (reply);
1299 _atspi_dbus_send_with_reply_and_block (DBusMessage *message, GError **error)
1303 AtspiApplication *app;
1304 DBusConnection *bus;
1306 app = get_application (dbus_message_get_destination (message));
1308 if (app && !app->bus)
1309 return NULL; /* will fail anyway; app has been disposed */
1311 bus = (app ? app->bus : _atspi_bus());
1312 dbus_error_init (&err);
1314 reply = dbind_send_and_allow_reentry (bus, message, &err);
1315 process_deferred_messages ();
1316 dbus_message_unref (message);
1317 if (dbus_error_is_set (&err))
1320 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err.message);
1321 dbus_error_free (&err);
1327 _atspi_dbus_return_hash_from_message (DBusMessage *message)
1329 DBusMessageIter iter;
1335 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1337 dbus_message_iter_init (message, &iter);
1338 ret = _atspi_dbus_hash_from_iter (&iter);
1339 dbus_message_unref (message);
1344 _atspi_dbus_hash_from_iter (DBusMessageIter *iter)
1346 GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
1347 (GDestroyNotify) g_free,
1348 (GDestroyNotify) g_free);
1349 DBusMessageIter iter_array, iter_dict;
1351 dbus_message_iter_recurse (iter, &iter_array);
1352 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1354 const char *name, *value;
1355 dbus_message_iter_recurse (&iter_array, &iter_dict);
1356 dbus_message_iter_get_basic (&iter_dict, &name);
1357 dbus_message_iter_next (&iter_dict);
1358 dbus_message_iter_get_basic (&iter_dict, &value);
1359 g_hash_table_insert (hash, g_strdup (name), g_strdup (value));
1360 dbus_message_iter_next (&iter_array);
1366 _atspi_dbus_return_attribute_array_from_message (DBusMessage *message)
1368 DBusMessageIter iter;
1374 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1376 dbus_message_iter_init (message, &iter);
1378 ret = _atspi_dbus_attribute_array_from_iter (&iter);
1379 dbus_message_unref (message);
1384 _atspi_dbus_attribute_array_from_iter (DBusMessageIter *iter)
1386 DBusMessageIter iter_array, iter_dict;
1387 GArray *array = g_array_new (TRUE, TRUE, sizeof (gchar *));
1389 dbus_message_iter_recurse (iter, &iter_array);
1390 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1392 const char *name, *value;
1394 dbus_message_iter_recurse (&iter_array, &iter_dict);
1395 dbus_message_iter_get_basic (&iter_dict, &name);
1396 dbus_message_iter_next (&iter_dict);
1397 dbus_message_iter_get_basic (&iter_dict, &value);
1398 str = g_strdup_printf ("%s:%s", name, value);
1399 array = g_array_append_val (array, str);
1400 dbus_message_iter_next (&iter_array);;
1406 _atspi_dbus_set_interfaces (AtspiAccessible *accessible, DBusMessageIter *iter)
1408 DBusMessageIter iter_array;
1409 char *iter_sig = dbus_message_iter_get_signature (iter);
1411 accessible->interfaces = 0;
1412 if (strcmp (iter_sig, "as") != 0)
1414 g_warning ("_atspi_dbus_set_interfaces: Passed iterator with invalid signature %s", dbus_message_iter_get_signature (iter));
1415 dbus_free (iter_sig);
1418 dbus_free (iter_sig);
1419 dbus_message_iter_recurse (iter, &iter_array);
1420 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1424 dbus_message_iter_get_basic (&iter_array, &iface);
1425 if (!strcmp (iface, "org.freedesktop.DBus.Introspectable")) continue;
1426 n = _atspi_get_iface_num (iface);
1429 g_warning ("AT-SPI: Unknown interface %s", iface);
1432 accessible->interfaces |= (1 << n);
1433 dbus_message_iter_next (&iter_array);
1435 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_INTERFACES);
1439 _atspi_dbus_set_state (AtspiAccessible *accessible, DBusMessageIter *iter)
1441 DBusMessageIter iter_array;
1443 dbus_uint32_t *states;
1445 dbus_message_iter_recurse (iter, &iter_array);
1446 dbus_message_iter_get_fixed_array (&iter_array, &states, &count);
1449 g_warning ("AT-SPI: expected 2 values in states array; got %d\n", count);
1450 if (!accessible->states)
1451 accessible->states = _atspi_state_set_new_internal (accessible, 0);
1455 guint64 val = ((guint64)states [1]) << 32;
1457 if (!accessible->states)
1458 accessible->states = _atspi_state_set_new_internal (accessible, val);
1460 accessible->states->states = val;
1462 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_STATES);
1466 _atspi_error_quark (void)
1468 return g_quark_from_static_string ("atspi_error");
1472 * Gets the IOR from the XDisplay.
1476 get_accessibility_bus_address_x11 (void)
1480 Display *bridge_display = NULL;
1483 unsigned char *data_x11 = NULL;
1484 unsigned long nitems;
1485 unsigned long leftover;
1488 display_name = spi_display_name ();
1492 bridge_display = XOpenDisplay (display_name);
1493 g_free (display_name);
1495 if (!bridge_display)
1497 g_warning ("Could not open X display");
1501 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
1502 XGetWindowProperty (bridge_display,
1503 XDefaultRootWindow (bridge_display),
1505 (long) BUFSIZ, False,
1506 (Atom) 31, &actual_type, &actual_format,
1507 &nitems, &leftover, &data_x11);
1508 XCloseDisplay (bridge_display);
1510 data = g_strdup ((gchar *)data_x11);
1517 get_accessibility_bus_address_dbus (void)
1519 DBusConnection *session_bus = NULL;
1520 DBusMessage *message;
1523 char *address = NULL;
1525 session_bus = dbus_bus_get (DBUS_BUS_SESSION, NULL);
1529 message = dbus_message_new_method_call ("org.a11y.Bus",
1534 dbus_error_init (&error);
1535 reply = dbus_connection_send_with_reply_and_block (session_bus,
1539 dbus_message_unref (message);
1543 g_warning ("AT-SPI: Error retrieving accessibility bus address: %s: %s",
1544 error.name, error.message);
1545 dbus_error_free (&error);
1550 const char *tmp_address;
1551 if (!dbus_message_get_args (reply,
1557 dbus_message_unref (reply);
1560 address = g_strdup (tmp_address);
1561 dbus_message_unref (reply);
1565 dbus_connection_unref (session_bus);
1569 static DBusConnection *a11y_bus;
1570 static dbus_int32_t a11y_dbus_slot = -1;
1573 a11y_bus_free (void *data)
1575 if (data == a11y_bus)
1578 dbus_connection_free_data_slot (&a11y_dbus_slot);
1583 * atspi_get_a11y_bus: (skip)
1586 atspi_get_a11y_bus (void)
1589 char *address = NULL;
1590 const char *address_env = NULL;
1592 if (a11y_bus && dbus_connection_get_is_connected (a11y_bus))
1595 if (a11y_dbus_slot == -1)
1596 if (!dbus_connection_allocate_data_slot (&a11y_dbus_slot))
1597 g_warning ("AT-SPI: Unable to allocate D-Bus slot");
1599 address_env = g_getenv ("AT_SPI_BUS_ADDRESS");
1600 if (address_env != NULL && *address_env != 0)
1601 address = g_strdup (address_env);
1603 if (!address && g_getenv ("DISPLAY") != NULL &&
1604 g_getenv ("WAYLAND_DISPLAY") == NULL)
1605 address = get_accessibility_bus_address_x11 ();
1608 address = get_accessibility_bus_address_dbus ();
1612 dbus_error_init (&error);
1613 a11y_bus = dbus_connection_open_private (address, &error);
1618 if (!g_getenv("SSH_CONNECTION"))
1619 g_warning ("Couldn't connect to accessibility bus: %s", error.message);
1620 dbus_error_free (&error);
1625 if (!dbus_bus_register (a11y_bus, &error))
1627 g_warning ("Couldn't register with accessibility bus: %s", error.message);
1628 dbus_error_free (&error);
1629 dbus_connection_close (a11y_bus);
1630 dbus_connection_unref (a11y_bus);
1636 /* Simulate a weak ref on the bus */
1637 dbus_connection_set_data (a11y_bus, a11y_dbus_slot, a11y_bus, a11y_bus_free);
1643 * atspi_set_timeout:
1644 * @val: The timeout value, in milliseconds, or -1 to disable the timeout.
1645 * @startup_time: The amount of time, in milliseconds, to allow to pass
1646 * before enforcing timeouts on an application. Can be used to prevent
1647 * timeout exceptions if an application is likely to block for an extended
1648 * period of time on initialization. -1 can be passed to disable this
1651 * Set the timeout used for method calls. If this is not set explicitly,
1652 * a default of 0.8 ms is used.
1653 * Note that at-spi2-registryd currently uses a timeout of 3 seconds when
1654 * sending a keyboard event notification. This means that, if an AT makes
1655 * a call in response to the keyboard notification and the application
1656 * being called does not respond before the timeout is reached,
1657 * at-spi2-registryd will time out on the keyboard event notification and
1658 * pass the key onto the application (ie, reply to indicate that the key
1659 * was not consumed), so this may make it undesirable to set a timeout
1660 * larger than 3 seconds.
1662 * By default, the normal timeout is set to 800 ms, and the application startup
1663 * timeout is set to 15 seconds.
1666 atspi_set_timeout (gint val, gint startup_time)
1668 method_call_timeout = val;
1669 app_startup_time = startup_time;
1673 * atspi_set_main_context:
1674 * @cnx: The #GMainContext to use.
1676 * Sets the main loop context that AT-SPI should assume is in use when
1677 * setting an idle callback.
1678 * This function should be called by application-side implementors (ie,
1679 * at-spi2-atk) when it is desirable to re-enter the main loop.
1682 atspi_set_main_context (GMainContext *cnx)
1684 if (atspi_main_context == cnx)
1686 if (process_deferred_messages_source != NULL)
1688 g_source_destroy (process_deferred_messages_source);
1689 process_deferred_messages_source = g_idle_source_new ();
1690 g_source_set_callback (process_deferred_messages_source,
1691 process_deferred_messages_callback, NULL, NULL);
1692 g_source_attach (process_deferred_messages_source, cnx);
1693 g_source_unref (process_deferred_messages_source);
1695 atspi_main_context = cnx;
1696 atspi_dbus_connection_setup_with_g_main (atspi_get_a11y_bus (), cnx);
1701 for (i = desktop->children->len - 1; i >= 0; i--)
1703 AtspiAccessible *child = g_ptr_array_index (desktop->children, i);
1704 if (child->parent.app && child->parent.app->bus)
1705 atspi_dbus_connection_setup_with_g_main (child->parent.app->bus, cnx);
1710 #ifdef DEBUG_REF_COUNTS
1712 print_disposed (gpointer key, gpointer value, gpointer data)
1714 AtspiAccessible *accessible = key;
1715 if (accessible->parent.app)
1717 g_print ("disposed: %s %d\n", accessible->name, accessible->role);
1723 g_hash_table_foreach (live_refs, print_disposed, NULL);
1728 _atspi_name_compat (gchar *name)
1742 * atspi_role_get_name:
1743 * @role: an #AtspiRole object to query.
1745 * Gets a localizable string that indicates the name of an #AtspiRole.
1746 * <em>DEPRECATED.</em>
1748 * Returns: a localizable string name for an #AtspiRole enumerated type.
1751 atspi_role_get_name (AtspiRole role)
1753 gchar *retval = NULL;
1754 GTypeClass *type_class;
1757 type_class = g_type_class_ref (ATSPI_TYPE_ROLE);
1758 g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), NULL);
1760 value = g_enum_get_value (G_ENUM_CLASS (type_class), role);
1764 retval = g_strdup (value->value_nick);
1767 g_type_class_unref (type_class);
1770 return _atspi_name_compat (retval);
1776 _atspi_dbus_update_cache_from_dict (AtspiAccessible *accessible, DBusMessageIter *iter)
1778 GHashTable *cache = _atspi_accessible_ref_cache (accessible);
1779 DBusMessageIter iter_dict, iter_dict_entry, iter_struct, iter_variant;
1781 dbus_message_iter_recurse (iter, &iter_dict);
1782 while (dbus_message_iter_get_arg_type (&iter_dict) != DBUS_TYPE_INVALID)
1786 dbus_message_iter_recurse (&iter_dict, &iter_dict_entry);
1787 dbus_message_iter_get_basic (&iter_dict_entry, &key);
1788 dbus_message_iter_next (&iter_dict_entry);
1789 dbus_message_iter_recurse (&iter_dict_entry, &iter_variant);
1790 if (!strcmp (key, "interfaces"))
1792 _atspi_dbus_set_interfaces (accessible, &iter_variant);
1794 else if (!strcmp (key, "Attributes"))
1796 char *iter_sig = dbus_message_iter_get_signature (&iter_variant);
1797 val = g_new0 (GValue, 1);;
1798 g_value_init (val, G_TYPE_HASH_TABLE);
1799 if (strcmp (iter_sig, "a{ss}") != 0)
1801 dbus_free (iter_sig);
1804 dbus_free (iter_sig);
1805 g_value_take_boxed (val, _atspi_dbus_hash_from_iter (&iter_variant));
1807 else if (!strcmp (key, "Component.ScreenExtents"))
1811 char *iter_sig = dbus_message_iter_get_signature (&iter_variant);
1812 val = g_new0 (GValue, 1);;
1813 g_value_init (val, ATSPI_TYPE_RECT);
1814 if (strcmp (iter_sig, "(iiii)") != 0)
1816 dbus_free (iter_sig);
1819 dbus_free (iter_sig);
1820 dbus_message_iter_recurse (&iter_variant, &iter_struct);
1821 dbus_message_iter_get_basic (&iter_struct, &d_int);
1823 dbus_message_iter_next (&iter_struct);
1824 dbus_message_iter_get_basic (&iter_struct, &d_int);
1826 dbus_message_iter_next (&iter_struct);
1827 dbus_message_iter_get_basic (&iter_struct, &d_int);
1828 extents.width = d_int;
1829 dbus_message_iter_next (&iter_struct);
1830 dbus_message_iter_get_basic (&iter_struct, &d_int);
1831 extents.height = d_int;
1832 g_value_set_boxed (val, &extents);
1835 g_hash_table_insert (cache, g_strdup (key), val);
1836 dbus_message_iter_next (&iter_dict);
1843 _atspi_get_allow_sync ()
1849 _atspi_set_allow_sync (gboolean val)
1851 gboolean ret = allow_sync;
1858 _atspi_set_error_no_sync (GError **error)
1860 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_SYNC_NOT_ALLOWED,
1861 _("Attempted synchronous call where prohibited"));
1864 static const char *sr_introspection = "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
1865 "\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
1866 "<node name=\"/org/a11y/atspi/screenreader\">\n"
1867 " <interface name=\"org.a11y.Atspi.ScreenReader\">\n"
1868 " <signal name=\"ReadingPosition\">\n"
1869 " <arg type=\"i\"/>\n"
1870 " <arg type=\"i\"/>\n"
1875 static DBusHandlerResult
1876 screen_reader_filter (DBusConnection *bus, DBusMessage *message, void *user_data)
1878 if (dbus_message_is_method_call (message, DBUS_INTERFACE_INTROSPECTABLE,
1881 DBusMessage *reply = dbus_message_new_method_return (message);
1882 dbus_message_append_args (reply, DBUS_TYPE_STRING, &sr_introspection,
1884 dbus_connection_send (bus, reply, NULL);
1885 dbus_message_unref (reply);
1886 return DBUS_HANDLER_RESULT_HANDLED;
1888 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1892 _atspi_prepare_screen_reader_interface ()
1894 static gint initialized = 0;
1895 DBusConnection *a11y_bus = _atspi_bus ();
1898 return (initialized > 0);
1900 if (dbus_bus_request_name (a11y_bus, "org.a11y.Atspi.ScreenReader", 0, NULL) < 0)
1907 dbus_connection_add_filter (a11y_bus, screen_reader_filter, NULL, NULL);