2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2001, 2002 Sun Microsystems Inc.,
6 * Copyright 2001, 2002 Ximian, Inc.
7 * Copyright 2010, 2011 Novell, Inc.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
27 * Basic SPI initialization and event loop function prototypes
31 #include "atspi-private.h"
33 #include "atspi-gmain.h"
37 static void handle_get_items (DBusPendingCall *pending, void *user_data);
39 static DBusConnection *bus = NULL;
40 static GHashTable *live_refs = NULL;
41 static gint method_call_timeout = 800;
42 static gint app_startup_time = 15000;
44 GMainLoop *atspi_main_loop;
45 gboolean atspi_no_cache;
47 const char *atspi_path_dec = ATSPI_DBUS_PATH_DEC;
48 const char *atspi_path_registry = ATSPI_DBUS_PATH_REGISTRY;
49 const char *atspi_path_root = ATSPI_DBUS_PATH_ROOT;
50 const char *atspi_bus_registry = ATSPI_DBUS_NAME_REGISTRY;
51 const char *atspi_interface_accessible = ATSPI_DBUS_INTERFACE_ACCESSIBLE;
52 const char *atspi_interface_action = ATSPI_DBUS_INTERFACE_ACTION;
53 const char *atspi_interface_application = ATSPI_DBUS_INTERFACE_APPLICATION;
54 const char *atspi_interface_collection = ATSPI_DBUS_INTERFACE_COLLECTION;
55 const char *atspi_interface_component = ATSPI_DBUS_INTERFACE_COMPONENT;
56 const char *atspi_interface_dec = ATSPI_DBUS_INTERFACE_DEC;
57 const char *atspi_interface_device_event_listener = ATSPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER;
58 const char *atspi_interface_document = ATSPI_DBUS_INTERFACE_DOCUMENT;
59 const char *atspi_interface_editable_text = ATSPI_DBUS_INTERFACE_EDITABLE_TEXT;
60 const char *atspi_interface_event_object = ATSPI_DBUS_INTERFACE_EVENT_OBJECT;
61 const char *atspi_interface_hyperlink = ATSPI_DBUS_INTERFACE_HYPERLINK;
62 const char *atspi_interface_hypertext = ATSPI_DBUS_INTERFACE_HYPERTEXT;
63 const char *atspi_interface_image = ATSPI_DBUS_INTERFACE_IMAGE;
64 const char *atspi_interface_registry = ATSPI_DBUS_INTERFACE_REGISTRY;
65 const char *atspi_interface_selection = ATSPI_DBUS_INTERFACE_SELECTION;
66 const char *atspi_interface_table = ATSPI_DBUS_INTERFACE_TABLE;
67 const char *atspi_interface_text = ATSPI_DBUS_INTERFACE_TEXT;
68 const char *atspi_interface_cache = ATSPI_DBUS_INTERFACE_CACHE;
69 const char *atspi_interface_value = ATSPI_DBUS_INTERFACE_VALUE;
71 static const char *interfaces[] =
73 ATSPI_DBUS_INTERFACE_ACCESSIBLE,
74 ATSPI_DBUS_INTERFACE_ACTION,
75 ATSPI_DBUS_INTERFACE_APPLICATION,
76 ATSPI_DBUS_INTERFACE_COLLECTION,
77 ATSPI_DBUS_INTERFACE_COMPONENT,
78 ATSPI_DBUS_INTERFACE_DOCUMENT,
79 ATSPI_DBUS_INTERFACE_EDITABLE_TEXT,
80 ATSPI_DBUS_INTERFACE_HYPERLINK,
81 ATSPI_DBUS_INTERFACE_HYPERTEXT,
82 ATSPI_DBUS_INTERFACE_IMAGE,
83 "org.a11y.atspi.LoginHelper",
84 ATSPI_DBUS_INTERFACE_SELECTION,
85 ATSPI_DBUS_INTERFACE_TABLE,
86 ATSPI_DBUS_INTERFACE_TEXT,
87 ATSPI_DBUS_INTERFACE_VALUE,
92 _atspi_get_iface_num (const char *iface)
94 /* TODO: Use a binary search or hash to improve performance */
97 for (i = 0; interfaces[i]; i++)
99 if (!strcmp(iface, interfaces[i])) return i;
109 live_refs = g_hash_table_new (g_direct_hash, g_direct_equal);
114 /* TODO: Add an application parameter */
121 g_error ("AT-SPI: COuldn't connect to accessibility bus. Is at-spi-bus-launcher running?");
125 #define APP_IS_REGISTRY(app) (!strcmp (app->bus_name, atspi_bus_registry))
136 g_hash_table_destroy (refs);
140 static gboolean atspi_inited = FALSE;
142 static GHashTable *app_hash = NULL;
145 handle_get_bus_address (DBusPendingCall *pending, void *user_data)
147 AtspiApplication *app = user_data;
148 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
149 DBusMessage *message;
151 DBusPendingCall *new_pending;
153 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
155 if (dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &address,
159 dbus_error_init (&error);
160 DBusConnection *bus = dbus_connection_open (address, &error);
164 dbus_connection_unref (app->bus);
169 dbus_message_unref (reply);
170 dbus_pending_call_unref (pending);
173 return; /* application has gone away / been disposed */
175 message = dbus_message_new_method_call (app->bus_name,
176 "/org/a11y/atspi/cache",
177 atspi_interface_cache, "GetItems");
179 dbus_connection_send_with_reply (app->bus, message, &new_pending, 2000);
180 dbus_pending_call_set_notify (new_pending, handle_get_items, app, NULL);
181 dbus_message_unref (message);
184 static AtspiApplication *
185 get_application (const char *bus_name)
187 AtspiApplication *app = NULL;
189 DBusMessage *message;
191 DBusPendingCall *pending = NULL;
195 app_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_object_unref);
196 if (!app_hash) return NULL;
198 app = g_hash_table_lookup (app_hash, bus_name);
200 bus_name_dup = g_strdup (bus_name);
201 if (!bus_name_dup) return NULL;
202 // TODO: change below to something that will send state-change:defunct notification if necessary */
203 app = _atspi_application_new (bus_name);
204 if (!app) return NULL;
205 app->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
206 app->bus = dbus_connection_ref (_atspi_bus ());
207 gettimeofday (&app->time_added, NULL);
208 app->cache = ATSPI_CACHE_UNDEFINED;
209 g_hash_table_insert (app_hash, bus_name_dup, app);
210 dbus_error_init (&error);
211 message = dbus_message_new_method_call (bus_name, atspi_path_root,
212 atspi_interface_application, "GetApplicationBusAddress");
214 dbus_connection_send_with_reply (app->bus, message, &pending, 2000);
215 dbus_pending_call_set_notify (pending, handle_get_bus_address, app, NULL);
216 dbus_message_unref (message);
220 static AtspiAccessible *
221 ref_accessible (const char *app_name, const char *path)
223 AtspiApplication *app;
226 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
229 app = get_application (app_name);
231 if (!strcmp (path, "/org/a11y/atspi/accessible/root"))
235 app->root = _atspi_accessible_new (app, atspi_path_root);
236 app->root->accessible_parent = atspi_get_desktop (0);
238 return g_object_ref (app->root);
241 a = g_hash_table_lookup (app->hash, path);
244 return g_object_ref (a);
246 a = _atspi_accessible_new (app, path);
249 g_hash_table_insert (app->hash, g_strdup (a->parent.path), a);
250 g_object_ref (a); /* for the hash */
254 static AtspiHyperlink *
255 ref_hyperlink (const char *app_name, const char *path)
257 AtspiApplication *app = get_application (app_name);
258 AtspiHyperlink *hyperlink;
260 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
263 hyperlink = g_hash_table_lookup (app->hash, path);
266 return g_object_ref (hyperlink);
268 hyperlink = _atspi_hyperlink_new (app, path);
269 g_hash_table_insert (app->hash, g_strdup (hyperlink->parent.path), hyperlink);
270 /* TODO: This should be a weak ref */
271 g_object_ref (hyperlink); /* for the hash */
284 GArray *state_bitflags;
287 static DBusHandlerResult
288 handle_remove_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
290 const char *sender = dbus_message_get_sender (message);
291 AtspiApplication *app;
293 DBusMessageIter iter, iter_struct;
294 const char *signature = dbus_message_get_signature (message);
297 if (strcmp (signature, "(so)") != 0)
299 g_warning ("AT-SPI: Unknown signature %s for RemoveAccessible", signature);
300 return DBUS_HANDLER_RESULT_HANDLED;
303 dbus_message_iter_init (message, &iter);
304 dbus_message_iter_recurse (&iter, &iter_struct);
305 dbus_message_iter_get_basic (&iter_struct, &sender);
306 dbus_message_iter_next (&iter_struct);
307 dbus_message_iter_get_basic (&iter_struct, &path);
308 app = get_application (sender);
309 a = ref_accessible (sender, path);
311 return DBUS_HANDLER_RESULT_HANDLED;
312 g_object_run_dispose (G_OBJECT (a));
313 g_hash_table_remove (app->hash, a->parent.path);
314 g_object_unref (a); /* unref our own ref */
315 return DBUS_HANDLER_RESULT_HANDLED;
319 add_app_to_desktop (AtspiAccessible *a, const char *bus_name)
323 dbus_error_init (&error);
324 AtspiAccessible *obj = ref_accessible (bus_name, atspi_path_root);
327 GList *new_list = g_list_append (a->children, obj);
330 a->children = new_list;
336 g_warning ("AT-SPI: Error calling getRoot for %s: %s", bus_name, error.message);
342 send_children_changed (AtspiAccessible *parent, AtspiAccessible *child, gboolean add)
346 memset (&e, 0, sizeof (e));
347 e.type = (add? "object:children-changed:add": "object:children-changed:remove");
349 e.detail1 = g_list_index (parent->children, child);
351 _atspi_send_event (&e);
355 unref_object_and_descendants (AtspiAccessible *obj)
359 for (l = obj->children; l; l = l->next)
361 unref_object_and_descendants (l->data);
363 g_object_unref (obj);
367 remove_app_from_desktop (AtspiAccessible *a, const char *bus_name)
370 AtspiAccessible *child;
372 for (l = a->children; l; l = l->next)
375 if (!strcmp (bus_name, child->parent.app->bus_name)) break;
381 send_children_changed (a, child, FALSE);
382 a->children = g_list_remove (a->children, child);
383 unref_object_and_descendants (child);
387 static AtspiAccessible *desktop;
390 get_reference_from_iter (DBusMessageIter *iter, const char **app_name, const char **path)
392 DBusMessageIter iter_struct;
394 dbus_message_iter_recurse (iter, &iter_struct);
395 dbus_message_iter_get_basic (&iter_struct, app_name);
396 dbus_message_iter_next (&iter_struct);
397 dbus_message_iter_get_basic (&iter_struct, path);
398 dbus_message_iter_next (iter);
402 add_accessible_from_iter (DBusMessageIter *iter)
404 DBusMessageIter iter_struct, iter_array;
405 const char *app_name, *path;
406 AtspiAccessible *accessible;
407 const char *name, *description;
410 dbus_message_iter_recurse (iter, &iter_struct);
413 get_reference_from_iter (&iter_struct, &app_name, &path);
414 accessible = ref_accessible (app_name, path);
418 /* Get application: TODO */
419 dbus_message_iter_next (&iter_struct);
422 get_reference_from_iter (&iter_struct, &app_name, &path);
423 if (accessible->accessible_parent)
424 g_object_unref (accessible->accessible_parent);
425 accessible->accessible_parent = ref_accessible (app_name, path);
428 while (accessible->children)
430 g_object_unref (accessible->children->data);
431 accessible->children = g_list_remove (accessible->children, accessible->children->data);
433 dbus_message_iter_recurse (&iter_struct, &iter_array);
434 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
436 AtspiAccessible *child;
437 get_reference_from_iter (&iter_array, &app_name, &path);
438 child = ref_accessible (app_name, path);
439 accessible->children = g_list_append (accessible->children, child);
443 dbus_message_iter_next (&iter_struct);
444 _atspi_dbus_set_interfaces (accessible, &iter_struct);
445 dbus_message_iter_next (&iter_struct);
448 if (accessible->name)
449 g_free (accessible->name);
450 dbus_message_iter_get_basic (&iter_struct, &name);
451 accessible->name = g_strdup (name);
452 dbus_message_iter_next (&iter_struct);
455 dbus_message_iter_get_basic (&iter_struct, &role);
456 accessible->role = role;
457 dbus_message_iter_next (&iter_struct);
460 if (accessible->description)
461 g_free (accessible->description);
462 dbus_message_iter_get_basic (&iter_struct, &description);
463 accessible->description = g_strdup (description);
464 dbus_message_iter_next (&iter_struct);
466 _atspi_dbus_set_state (accessible, &iter_struct);
467 dbus_message_iter_next (&iter_struct);
469 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_NAME | ATSPI_CACHE_ROLE |
470 ATSPI_CACHE_PARENT | ATSPI_CACHE_DESCRIPTION);
471 if (!atspi_state_set_contains (accessible->states,
472 ATSPI_STATE_MANAGES_DESCENDANTS))
473 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_CHILDREN);
475 /* This is a bit of a hack since the cache holds a ref, so we don't need
476 * the one provided for us anymore */
477 g_object_unref (accessible);
481 handle_get_items (DBusPendingCall *pending, void *user_data)
483 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
484 DBusMessageIter iter, iter_array;
486 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
488 const char *sender = dbus_message_get_sender (reply);
489 const char *error = NULL;
490 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &error,
492 g_warning ("AT-SPI: Error in GetItems, sender=%s, error=%s", sender, error);
493 dbus_message_unref (reply);
494 dbus_pending_call_unref (pending);
498 dbus_message_iter_init (reply, &iter);
499 dbus_message_iter_recurse (&iter, &iter_array);
500 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
502 add_accessible_from_iter (&iter_array);
503 dbus_message_iter_next (&iter_array);
505 dbus_message_unref (reply);
506 dbus_pending_call_unref (pending);
509 /* TODO: Do we stil need this function? */
510 static AtspiAccessible *
511 ref_accessible_desktop (AtspiApplication *app)
514 DBusMessage *message, *reply;
515 DBusMessageIter iter, iter_array;
520 g_object_ref (desktop);
523 desktop = _atspi_accessible_new (app, atspi_path_root);
528 g_hash_table_insert (app->hash, desktop->parent.path, desktop);
529 g_object_ref (desktop); /* for the hash */
530 desktop->name = g_strdup ("main");
531 dbus_error_init (&error);
532 message = dbus_message_new_method_call (atspi_bus_registry,
534 atspi_interface_accessible,
538 reply = _atspi_dbus_send_with_reply_and_block (message, NULL);
539 if (!reply || strcmp (dbus_message_get_signature (reply), "a(so)") != 0)
541 g_warning ("Couldn't get application list: %s", error.message);
543 dbus_message_unref (reply);
546 dbus_message_iter_init (reply, &iter);
547 dbus_message_iter_recurse (&iter, &iter_array);
548 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
550 const char *app_name, *path;
551 get_reference_from_iter (&iter_array, &app_name, &path);
552 add_app_to_desktop (desktop, app_name);
554 dbus_message_unref (reply);
556 /* Record the alternate name as an alias for org.a11y.atspi.Registry */
557 bus_name_dup = g_strdup (dbus_message_get_sender (reply));
559 g_hash_table_insert (app_hash, bus_name_dup, app);
565 _atspi_ref_accessible (const char *app, const char *path)
567 AtspiApplication *a = get_application (app);
569 if ( APP_IS_REGISTRY(a))
571 return a->root = ref_accessible_desktop (a);
573 return ref_accessible (app, path);
577 _atspi_dbus_return_accessible_from_message (DBusMessage *message)
579 DBusMessageIter iter;
580 AtspiAccessible *retval = NULL;
581 const char *signature;
586 signature = dbus_message_get_signature (message);
587 if (!strcmp (signature, "(so)"))
589 dbus_message_iter_init (message, &iter);
590 retval = _atspi_dbus_return_accessible_from_iter (&iter);
594 g_warning ("AT-SPI: Called _atspi_dbus_return_accessible_from_message with strange signature %s", signature);
596 dbus_message_unref (message);
601 _atspi_dbus_return_accessible_from_iter (DBusMessageIter *iter)
603 const char *app_name, *path;
605 get_reference_from_iter (iter, &app_name, &path);
606 return ref_accessible (app_name, path);
610 _atspi_dbus_return_hyperlink_from_message (DBusMessage *message)
612 DBusMessageIter iter;
613 AtspiHyperlink *retval = NULL;
614 const char *signature;
619 signature = dbus_message_get_signature (message);
620 if (!strcmp (signature, "(so)"))
622 dbus_message_iter_init (message, &iter);
623 retval = _atspi_dbus_return_hyperlink_from_iter (&iter);
627 g_warning ("AT-SPI: Called _atspi_dbus_return_hyperlink_from_message with strange signature %s", signature);
629 dbus_message_unref (message);
634 _atspi_dbus_return_hyperlink_from_iter (DBusMessageIter *iter)
636 const char *app_name, *path;
638 get_reference_from_iter (iter, &app_name, &path);
639 return ref_hyperlink (app_name, path);
642 const char *cache_signal_type = "((so)(so)(so)a(so)assusau)";
644 static DBusHandlerResult
645 handle_add_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
647 DBusMessageIter iter;
648 const char *sender = dbus_message_get_sender (message);
650 if (strcmp (dbus_message_get_signature (message), cache_signal_type) != 0)
652 g_warning ("AT-SPI: AddAccessible with unknown signature %s\n",
653 dbus_message_get_signature (message));
654 return DBUS_HANDLER_RESULT_HANDLED;
657 dbus_message_iter_init (message, &iter);
658 add_accessible_from_iter (&iter);
659 return DBUS_HANDLER_RESULT_HANDLED;
665 DBusMessage *message;
669 static guint process_deferred_messages_id = -1;
672 process_deferred_message (BusDataClosure *closure)
674 int type = dbus_message_get_type (closure->message);
675 const char *interface = dbus_message_get_interface (closure->message);
677 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
678 !strncmp (interface, "org.a11y.atspi.Event.", 21))
680 _atspi_dbus_handle_event (closure->bus, closure->message, closure->data);
682 if (dbus_message_is_method_call (closure->message, atspi_interface_device_event_listener, "NotifyEvent"))
684 _atspi_dbus_handle_DeviceEvent (closure->bus,
685 closure->message, closure->data);
687 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "AddAccessible"))
689 handle_add_accessible (closure->bus, closure->message, closure->data);
691 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "RemoveAccessible"))
693 handle_remove_accessible (closure->bus, closure->message, closure->data);
697 static GQueue *deferred_messages = NULL;
700 _atspi_process_deferred_messages (gpointer data)
702 static int in_process_deferred_messages = 0;
703 BusDataClosure *closure;
705 if (in_process_deferred_messages)
707 in_process_deferred_messages = 1;
708 while (closure = g_queue_pop_head (deferred_messages))
710 process_deferred_message (closure);
711 dbus_message_unref (closure->message);
712 dbus_connection_unref (closure->bus);
715 /* If data is NULL, assume that we were called from GLib */
717 process_deferred_messages_id = -1;
718 in_process_deferred_messages = 0;
722 static DBusHandlerResult
723 defer_message (DBusConnection *connection, DBusMessage *message, void *user_data)
725 BusDataClosure *closure = g_new (BusDataClosure, 1);
727 closure->bus = dbus_connection_ref (bus);
728 closure->message = dbus_message_ref (message);
729 closure->data = user_data;
731 g_queue_push_tail (deferred_messages, closure);
733 if (process_deferred_messages_id == -1)
734 process_deferred_messages_id = g_idle_add (_atspi_process_deferred_messages, NULL);
735 return DBUS_HANDLER_RESULT_HANDLED;
738 static DBusHandlerResult
739 atspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
741 int type = dbus_message_get_type (message);
742 const char *interface = dbus_message_get_interface (message);
744 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
745 !strncmp (interface, "org.a11y.atspi.Event.", 21))
747 return defer_message (bus, message, data);
749 if (dbus_message_is_method_call (message, atspi_interface_device_event_listener, "NotifyEvent"))
751 return defer_message (bus, message, data);
753 if (dbus_message_is_signal (message, atspi_interface_cache, "AddAccessible"))
755 return defer_message (bus, message, data);
757 if (dbus_message_is_signal (message, atspi_interface_cache, "RemoveAccessible"))
759 return defer_message (bus, message, data);
761 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
764 static const char *signal_interfaces[] =
766 "org.a11y.atspi.Event.Object",
767 "org.a11y.atspi.Event.Window",
768 "org.a11y.atspi.Event.Mouse",
769 "org.a11y.atspi.Event.Terminal",
770 "org.a11y.atspi.Event.Document",
771 "org.a11y.atspi.Event.Focus",
776 * Returns a 'canonicalized' value for DISPLAY,
777 * with the screen number stripped off if present.
779 * TODO: Avoid having duplicate functions for this here and in at-spi2-atk
782 spi_display_name (void)
784 static const char *canonical_display_name = NULL;
785 if (!canonical_display_name)
787 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
790 display_env = g_getenv ("DISPLAY");
791 if (!display_env || !display_env[0])
792 canonical_display_name = ":0";
795 gchar *display_p, *screen_p;
796 canonical_display_name = g_strdup (display_env);
797 display_p = g_utf8_strrchr (canonical_display_name, -1, ':');
798 screen_p = g_utf8_strrchr (canonical_display_name, -1, '.');
799 if (screen_p && display_p && (screen_p > display_p))
807 canonical_display_name = display_env;
810 return canonical_display_name;
816 * Connects to the accessibility registry and initializes the SPI.
818 * Returns: 0 on success, otherwise an integer error code.
825 const gchar *no_cache;
838 dbus_error_init (&error);
839 bus = atspi_get_a11y_bus ();
842 dbus_bus_register (bus, &error);
843 atspi_dbus_connection_setup_with_g_main(bus, g_main_context_default());
844 dbus_connection_add_filter (bus, atspi_dbus_filter, NULL, NULL);
845 match = g_strdup_printf ("type='signal',interface='%s',member='AddAccessible'", atspi_interface_cache);
846 dbus_error_init (&error);
847 dbus_bus_add_match (bus, match, &error);
849 match = g_strdup_printf ("type='signal',interface='%s',member='RemoveAccessible'", atspi_interface_cache);
850 dbus_bus_add_match (bus, match, &error);
852 match = g_strdup_printf ("type='signal',interface='%s',member='ChildrenChanged'", atspi_interface_event_object);
853 dbus_bus_add_match (bus, match, &error);
855 match = g_strdup_printf ("type='signal',interface='%s',member='PropertyChange'", atspi_interface_event_object);
856 dbus_bus_add_match (bus, match, &error);
858 match = g_strdup_printf ("type='signal',interface='%s',member='StateChanged'", atspi_interface_event_object);
859 dbus_bus_add_match (bus, match, &error);
862 no_cache = g_getenv ("ATSPI_NO_CACHE");
863 if (no_cache && g_strcmp0 (no_cache, "0") != 0)
864 atspi_no_cache = TRUE;
866 deferred_messages = g_queue_new ();
874 * Starts/enters the main event loop for the AT-SPI services.
876 * NOTE: This method does not return control; it is exited via a call to
877 * #atspi_event_quit from within an event handler.
881 atspi_event_main (void)
883 atspi_main_loop = g_main_loop_new (NULL, FALSE);
884 g_main_loop_run (atspi_main_loop);
885 atspi_main_loop = NULL;
891 * Quits the last main event loop for the AT-SPI services,
892 * See: #atspi_event_main
895 atspi_event_quit (void)
897 g_main_loop_quit (atspi_main_loop);
903 * Disconnects from #AtspiRegistry instances and releases
904 * any floating resources. Call only once at exit.
906 * Returns: 0 if there were no leaks, otherwise other integer values.
918 atspi_inited = FALSE;
922 leaked = g_hash_table_size (live_refs);
934 static GSList *hung_processes;
937 remove_hung_process (DBusPendingCall *pending, void *data)
939 gchar *bus_name = data;
941 hung_processes = g_slist_remove (hung_processes, data);
943 dbus_pending_call_unref (pending);
947 check_for_hang (DBusMessage *message, DBusError *error, DBusConnection *bus, const char *bus_name)
949 if (!message && error->name &&
950 !strcmp (error->name, "org.freedesktop.DBus.Error.NoReply"))
953 DBusMessage *message;
955 DBusPendingCall *pending = NULL;
956 for (l = hung_processes; l; l = l->next)
957 if (!strcmp (l->data, bus_name))
959 message = dbus_message_new_method_call (bus_name, "/",
960 "org.freedesktop.DBus.Peer",
964 dbus_connection_send_with_reply (bus, message, &pending, -1);
965 dbus_message_unref (message);
968 bus_name_dup = g_strdup (bus_name);
969 hung_processes = g_slist_append (hung_processes, bus_name_dup);
970 dbus_pending_call_set_notify (pending, remove_hung_process, bus_name_dup, NULL);
975 connection_is_hung (const char *bus_name)
979 for (l = hung_processes; l; l = l->next)
980 if (!strcmp (l->data, bus_name))
986 check_app (AtspiApplication *app, GError **error)
988 if (!app || !app->bus)
990 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
991 _("The application no longer exists"));
995 if (atspi_main_loop && connection_is_hung (app->bus_name))
997 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC,
998 "The process appears to be hung.");
1006 set_timeout (AtspiApplication *app)
1011 if (app && app_startup_time > 0)
1013 gettimeofday (&tv, NULL);
1014 diff = (tv.tv_sec - app->time_added.tv_sec) * 1000 + (tv.tv_usec - app->time_added.tv_usec) / 1000;
1015 dbind_set_timeout (MAX(method_call_timeout, app_startup_time - diff));
1018 dbind_set_timeout (method_call_timeout);
1022 _atspi_dbus_call (gpointer obj, const char *interface, const char *method, GError **error, const char *type, ...)
1027 AtspiObject *aobj = ATSPI_OBJECT (obj);
1029 if (!check_app (aobj->app, error))
1032 va_start (args, type);
1033 dbus_error_init (&err);
1034 set_timeout (aobj->app);
1035 retval = dbind_method_call_reentrant_va (aobj->app->bus, aobj->app->bus_name,
1036 aobj->path, interface, method, &err,
1039 check_for_hang (NULL, &err, aobj->app->bus, aobj->app->bus_name);
1040 _atspi_process_deferred_messages ((gpointer)TRUE);
1041 if (dbus_error_is_set (&err))
1043 g_set_error(error, ATSPI_ERROR, ATSPI_ERROR_IPC, "%s", err.message);
1044 dbus_error_free (&err);
1050 _atspi_dbus_call_partial (gpointer obj,
1051 const char *interface,
1054 const char *type, ...)
1058 va_start (args, type);
1059 return _atspi_dbus_call_partial_va (obj, interface, method, error, type, args);
1064 _atspi_dbus_call_partial_va (gpointer obj,
1065 const char *interface,
1071 AtspiObject *aobj = ATSPI_OBJECT (obj);
1073 DBusMessage *msg = NULL, *reply = NULL;
1074 DBusMessageIter iter;
1077 dbus_error_init (&err);
1079 if (!check_app (aobj->app, error))
1082 msg = dbus_message_new_method_call (aobj->app->bus_name, aobj->path, interface, method);
1087 dbus_message_iter_init_append (msg, &iter);
1088 dbind_any_marshal_va (&iter, &p, args);
1090 set_timeout (aobj->app);
1091 reply = dbind_send_and_allow_reentry (aobj->app->bus, msg, &err);
1092 check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1096 dbus_message_unref (msg);
1097 _atspi_process_deferred_messages ((gpointer)TRUE);
1098 if (dbus_error_is_set (&err))
1100 /* TODO: Set gerror */
1101 dbus_error_free (&err);
1107 _atspi_dbus_get_property (gpointer obj, const char *interface, const char *name, GError **error, const char *type, void *data)
1109 DBusMessage *message, *reply;
1110 DBusMessageIter iter, iter_variant;
1112 dbus_bool_t retval = FALSE;
1113 AtspiObject *aobj = ATSPI_OBJECT (obj);
1114 char expected_type = (type [0] == '(' ? 'r' : type [0]);
1119 if (!check_app (aobj->app, error))
1122 message = dbus_message_new_method_call (aobj->app->bus_name,
1124 "org.freedesktop.DBus.Properties",
1128 // TODO: throw exception
1131 dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1132 dbus_error_init (&err);
1133 set_timeout (aobj->app);
1134 reply = dbind_send_and_allow_reentry (aobj->app->bus, message, &err);
1135 check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
1136 dbus_message_unref (message);
1137 _atspi_process_deferred_messages ((gpointer)TRUE);
1140 // TODO: throw exception
1144 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
1146 const char *err = NULL;
1147 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &err, DBUS_TYPE_INVALID);
1149 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err);
1153 dbus_message_iter_init (reply, &iter);
1154 if (dbus_message_iter_get_arg_type (&iter) != 'v')
1156 g_warning ("AT-SPI: expected a variant when fetching %s from interface %s; got %s\n", name, interface, dbus_message_get_signature (reply));
1159 dbus_message_iter_recurse (&iter, &iter_variant);
1160 if (dbus_message_iter_get_arg_type (&iter_variant) != expected_type)
1162 g_warning ("atspi_dbus_get_property: Wrong type: expected %s, got %c\n", type, dbus_message_iter_get_arg_type (&iter_variant));
1165 if (!strcmp (type, "(so)"))
1167 *((AtspiAccessible **)data) = _atspi_dbus_return_accessible_from_iter (&iter_variant);
1171 dbus_message_iter_get_basic (&iter_variant, data);
1172 if (type [0] == 's')
1173 *(char **)data = g_strdup (*(char **)data);
1177 dbus_error_free (&err);
1179 dbus_message_unref (reply);
1184 _atspi_dbus_send_with_reply_and_block (DBusMessage *message, GError **error)
1188 AtspiApplication *app;
1189 DBusConnection *bus;
1191 app = get_application (dbus_message_get_destination (message));
1193 if (app && !app->bus)
1194 return NULL; /* will fail anyway; app has been disposed */
1196 bus = (app ? app->bus : _atspi_bus());
1197 dbus_error_init (&err);
1199 reply = dbind_send_and_allow_reentry (bus, message, &err);
1200 _atspi_process_deferred_messages ((gpointer)TRUE);
1201 dbus_message_unref (message);
1205 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err.message);
1206 dbus_error_free (&err);
1212 _atspi_dbus_return_hash_from_message (DBusMessage *message)
1214 DBusMessageIter iter;
1220 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1222 dbus_message_iter_init (message, &iter);
1223 ret = _atspi_dbus_hash_from_iter (&iter);
1224 dbus_message_unref (message);
1229 _atspi_dbus_hash_from_iter (DBusMessageIter *iter)
1231 GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
1232 (GDestroyNotify) g_free,
1233 (GDestroyNotify) g_free);
1234 DBusMessageIter iter_array, iter_dict;
1236 dbus_message_iter_recurse (iter, &iter_array);
1237 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1239 const char *name, *value;
1240 dbus_message_iter_recurse (&iter_array, &iter_dict);
1241 dbus_message_iter_get_basic (&iter_dict, &name);
1242 dbus_message_iter_next (&iter_dict);
1243 dbus_message_iter_get_basic (&iter_dict, &value);
1244 g_hash_table_insert (hash, g_strdup (name), g_strdup (value));
1245 dbus_message_iter_next (&iter_array);
1251 _atspi_dbus_return_attribute_array_from_message (DBusMessage *message)
1253 DBusMessageIter iter;
1259 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1261 dbus_message_iter_init (message, &iter);
1263 ret = _atspi_dbus_attribute_array_from_iter (&iter);
1264 dbus_message_unref (message);
1269 _atspi_dbus_attribute_array_from_iter (DBusMessageIter *iter)
1271 DBusMessageIter iter_array, iter_dict;
1272 GArray *array = g_array_new (TRUE, TRUE, sizeof (gchar *));
1274 dbus_message_iter_recurse (iter, &iter_array);
1275 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1277 const char *name, *value;
1279 dbus_message_iter_recurse (&iter_array, &iter_dict);
1280 dbus_message_iter_get_basic (&iter_dict, &name);
1281 dbus_message_iter_next (&iter_dict);
1282 dbus_message_iter_get_basic (&iter_dict, &value);
1283 str = g_strdup_printf ("%s:%s", name, value);
1284 array = g_array_append_val (array, str);
1285 dbus_message_iter_next (&iter_array);;
1291 _atspi_dbus_set_interfaces (AtspiAccessible *accessible, DBusMessageIter *iter)
1293 DBusMessageIter iter_array;
1295 accessible->interfaces = 0;
1296 dbus_message_iter_recurse (iter, &iter_array);
1297 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1301 dbus_message_iter_get_basic (&iter_array, &iface);
1302 if (!strcmp (iface, "org.freedesktop.DBus.Introspectable")) continue;
1303 n = _atspi_get_iface_num (iface);
1306 g_warning ("AT-SPI: Unknown interface %s", iface);
1309 accessible->interfaces |= (1 << n);
1310 dbus_message_iter_next (&iter_array);
1312 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_INTERFACES);
1316 _atspi_dbus_set_state (AtspiAccessible *accessible, DBusMessageIter *iter)
1318 DBusMessageIter iter_array;
1320 dbus_uint32_t *states;
1322 dbus_message_iter_recurse (iter, &iter_array);
1323 dbus_message_iter_get_fixed_array (&iter_array, &states, &count);
1326 g_warning ("AT-SPI: expected 2 values in states array; got %d\n", count);
1327 if (!accessible->states)
1328 accessible->states = _atspi_state_set_new_internal (accessible, 0);
1332 guint64 val = ((guint64)states [1]) << 32;
1334 if (!accessible->states)
1335 accessible->states = _atspi_state_set_new_internal (accessible, val);
1337 accessible->states->states = val;
1339 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_STATES);
1343 _atspi_error_quark (void)
1345 return g_quark_from_static_string ("atspi_error");
1349 * Gets the IOR from the XDisplay.
1352 get_accessibility_bus_address_x11 (void)
1356 Display *bridge_display;
1358 unsigned char *data = NULL;
1359 unsigned long nitems;
1360 unsigned long leftover;
1362 bridge_display = XOpenDisplay (spi_display_name ());
1363 if (!bridge_display)
1365 g_warning ("Could not open X display");
1369 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
1370 XGetWindowProperty (bridge_display,
1371 XDefaultRootWindow (bridge_display),
1373 (long) BUFSIZ, False,
1374 (Atom) 31, &actual_type, &actual_format,
1375 &nitems, &leftover, &data);
1376 XCloseDisplay (bridge_display);
1378 return g_strdup (data);
1382 get_accessibility_bus_address_dbus (void)
1384 DBusConnection *session_bus = NULL;
1385 DBusMessage *message;
1388 char *address = NULL;
1390 session_bus = dbus_bus_get (DBUS_BUS_SESSION, NULL);
1394 message = dbus_message_new_method_call ("org.a11y.Bus",
1399 dbus_error_init (&error);
1400 reply = dbus_connection_send_with_reply_and_block (session_bus,
1404 dbus_message_unref (message);
1408 g_warning ("Error retrieving accessibility bus address: %s: %s",
1409 error.name, error.message);
1410 dbus_error_free (&error);
1415 const char *tmp_address;
1416 if (!dbus_message_get_args (reply,
1422 dbus_message_unref (reply);
1425 address = g_strdup (tmp_address);
1426 dbus_message_unref (reply);
1433 atspi_get_a11y_bus (void)
1435 DBusConnection *bus = NULL;
1439 address = get_accessibility_bus_address_x11 ();
1441 address = get_accessibility_bus_address_dbus ();
1445 dbus_error_init (&error);
1446 bus = dbus_connection_open (address, &error);
1451 g_warning ("Couldn't connect to accessibility bus: %s", error.message);
1456 if (!dbus_bus_register (bus, &error))
1458 g_warning ("Couldn't register with accessibility bus: %s", error.message);
1467 * Set the timeout used for method calls. If this is not set explicitly,
1468 * a default of 0.8 ms is used.
1469 * Note that at-spi2-registryd currently uses a timeout of 3 seconds when
1470 * sending a keyboard event notification. This means that, if an AT makes
1471 * a call in response to the keyboard notification and the application
1472 * being called does not respond before the timeout is reached,
1473 * at-spi2-registryd will time out on the keyboard event notification and
1474 * pass the key onto the application (ie, reply to indicate that the key
1475 * was not consumed), so this may make it undesirable to set a timeout
1476 * larger than 3 seconds.
1478 * @val: The timeout value, in milliseconds, or -1 to disable the timeout.
1479 * @startup_time: The amount of time, in milliseconds, to allow to pass
1480 * before enforcing timeouts on an application. Can be used to prevent
1481 * timeout exceptions if an application is likely to block for an extended
1482 * period of time on initialization. -1 can be passed to disable this
1485 * By default, the normal timeout is set to 800 ms, and the application startup
1486 * timeout is set to 15 seconds.
1489 atspi_set_timeout (gint val, gint startup_time)
1491 method_call_timeout = val;
1492 app_startup_time = startup_time;