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;
42 const char *atspi_path_dec = ATSPI_DBUS_PATH_DEC;
43 const char *atspi_path_registry = ATSPI_DBUS_PATH_REGISTRY;
44 const char *atspi_path_root = ATSPI_DBUS_PATH_ROOT;
45 const char *atspi_bus_registry = ATSPI_DBUS_NAME_REGISTRY;
46 const char *atspi_interface_accessible = ATSPI_DBUS_INTERFACE_ACCESSIBLE;
47 const char *atspi_interface_action = ATSPI_DBUS_INTERFACE_ACTION;
48 const char *atspi_interface_application = ATSPI_DBUS_INTERFACE_APPLICATION;
49 const char *atspi_interface_collection = ATSPI_DBUS_INTERFACE_COLLECTION;
50 const char *atspi_interface_component = ATSPI_DBUS_INTERFACE_COMPONENT;
51 const char *atspi_interface_dec = ATSPI_DBUS_INTERFACE_DEC;
52 const char *atspi_interface_device_event_listener = ATSPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER;
53 const char *atspi_interface_document = ATSPI_DBUS_INTERFACE_DOCUMENT;
54 const char *atspi_interface_editable_text = ATSPI_DBUS_INTERFACE_EDITABLE_TEXT;
55 const char *atspi_interface_event_object = ATSPI_DBUS_INTERFACE_EVENT_OBJECT;
56 const char *atspi_interface_hyperlink = ATSPI_DBUS_INTERFACE_HYPERLINK;
57 const char *atspi_interface_hypertext = ATSPI_DBUS_INTERFACE_HYPERTEXT;
58 const char *atspi_interface_image = ATSPI_DBUS_INTERFACE_IMAGE;
59 const char *atspi_interface_registry = ATSPI_DBUS_INTERFACE_REGISTRY;
60 const char *atspi_interface_selection = ATSPI_DBUS_INTERFACE_SELECTION;
61 const char *atspi_interface_table = ATSPI_DBUS_INTERFACE_TABLE;
62 const char *atspi_interface_text = ATSPI_DBUS_INTERFACE_TEXT;
63 const char *atspi_interface_cache = ATSPI_DBUS_INTERFACE_CACHE;
64 const char *atspi_interface_value = ATSPI_DBUS_INTERFACE_VALUE;
66 static const char *interfaces[] =
68 ATSPI_DBUS_INTERFACE_ACCESSIBLE,
69 ATSPI_DBUS_INTERFACE_ACTION,
70 ATSPI_DBUS_INTERFACE_APPLICATION,
71 ATSPI_DBUS_INTERFACE_COLLECTION,
72 ATSPI_DBUS_INTERFACE_COMPONENT,
73 ATSPI_DBUS_INTERFACE_DOCUMENT,
74 ATSPI_DBUS_INTERFACE_EDITABLE_TEXT,
75 ATSPI_DBUS_INTERFACE_HYPERLINK,
76 ATSPI_DBUS_INTERFACE_HYPERTEXT,
77 ATSPI_DBUS_INTERFACE_IMAGE,
78 "org.a11y.atspi.LoginHelper",
79 ATSPI_DBUS_INTERFACE_SELECTION,
80 ATSPI_DBUS_INTERFACE_TABLE,
81 ATSPI_DBUS_INTERFACE_TEXT,
82 ATSPI_DBUS_INTERFACE_VALUE,
87 _atspi_get_iface_num (const char *iface)
89 /* TODO: Use a binary search or hash to improve performance */
92 for (i = 0; interfaces[i]; i++)
94 if (!strcmp(iface, interfaces[i])) return i;
104 live_refs = g_hash_table_new (g_direct_hash, g_direct_equal);
109 /* TODO: Add an application parameter */
116 g_error ("AT-SPI: COuldn't connect to accessibility bus. Is at-spi-bus-launcher running?");
120 #define APP_IS_REGISTRY(app) (!strcmp (app->bus_name, atspi_bus_registry))
131 g_hash_table_destroy (refs);
135 static gboolean atspi_inited = FALSE;
137 static GHashTable *app_hash = NULL;
140 handle_get_bus_address (DBusPendingCall *pending, void *user_data)
142 AtspiApplication *app = user_data;
143 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
144 DBusMessage *message;
146 DBusPendingCall *new_pending;
148 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
150 if (dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &address,
154 dbus_error_init (&error);
155 DBusConnection *bus = dbus_connection_open (address, &error);
159 dbus_connection_unref (app->bus);
164 dbus_message_unref (reply);
165 dbus_pending_call_unref (pending);
168 return; /* application has gone away / been disposed */
170 message = dbus_message_new_method_call (app->bus_name,
171 "/org/a11y/atspi/cache",
172 atspi_interface_cache, "GetItems");
174 dbus_connection_send_with_reply (app->bus, message, &new_pending, 2000);
175 dbus_pending_call_set_notify (new_pending, handle_get_items, app, NULL);
176 dbus_message_unref (message);
179 static AtspiApplication *
180 get_application (const char *bus_name)
182 AtspiApplication *app = NULL;
184 DBusMessage *message;
186 DBusPendingCall *pending = NULL;
190 app_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_object_unref);
191 if (!app_hash) return NULL;
193 app = g_hash_table_lookup (app_hash, bus_name);
195 bus_name_dup = g_strdup (bus_name);
196 if (!bus_name_dup) return NULL;
197 // TODO: change below to something that will send state-change:defunct notification if necessary */
198 app = _atspi_application_new (bus_name);
199 if (!app) return NULL;
200 app->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
201 app->bus = dbus_connection_ref (_atspi_bus ());
202 app->cache = ATSPI_CACHE_UNDEFINED;
203 g_hash_table_insert (app_hash, bus_name_dup, app);
204 dbus_error_init (&error);
205 message = dbus_message_new_method_call (bus_name, atspi_path_root,
206 atspi_interface_application, "GetApplicationBusAddress");
208 dbus_connection_send_with_reply (app->bus, message, &pending, 2000);
209 dbus_pending_call_set_notify (pending, handle_get_bus_address, app, NULL);
210 dbus_message_unref (message);
214 static AtspiAccessible *
215 ref_accessible (const char *app_name, const char *path)
217 AtspiApplication *app;
220 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
223 app = get_application (app_name);
225 if (!strcmp (path, "/org/a11y/atspi/accessible/root"))
229 app->root = atspi_accessible_new (app, atspi_path_root);
230 app->root->accessible_parent = atspi_get_desktop (0);
232 return g_object_ref (app->root);
235 a = g_hash_table_lookup (app->hash, path);
238 return g_object_ref (a);
240 a = atspi_accessible_new (app, path);
243 g_hash_table_insert (app->hash, g_strdup (a->parent.path), a);
244 g_object_ref (a); /* for the hash */
248 static AtspiHyperlink *
249 ref_hyperlink (const char *app_name, const char *path)
251 AtspiApplication *app = get_application (app_name);
252 AtspiHyperlink *hyperlink;
254 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
257 hyperlink = g_hash_table_lookup (app->hash, path);
260 return g_object_ref (hyperlink);
262 hyperlink = atspi_hyperlink_new (app, path);
265 g_hash_table_insert (app->hash, g_strdup (hyperlink->parent.path), hyperlink);
266 /* TODO: This should be a weak ref */
267 g_object_ref (hyperlink); /* for the hash */
280 GArray *state_bitflags;
283 static DBusHandlerResult
284 handle_remove_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
286 const char *sender = dbus_message_get_sender (message);
287 AtspiApplication *app;
289 DBusMessageIter iter, iter_struct;
290 const char *signature = dbus_message_get_signature (message);
293 if (strcmp (signature, "(so)") != 0)
295 g_warning ("AT-SPI: Unknown signature %s for RemoveAccessible", signature);
296 return DBUS_HANDLER_RESULT_HANDLED;
299 dbus_message_iter_init (message, &iter);
300 dbus_message_iter_recurse (&iter, &iter_struct);
301 dbus_message_iter_get_basic (&iter_struct, &sender);
302 dbus_message_iter_next (&iter_struct);
303 dbus_message_iter_get_basic (&iter_struct, &path);
304 app = get_application (sender);
305 a = ref_accessible (sender, path);
307 return DBUS_HANDLER_RESULT_HANDLED;
308 g_object_run_dispose (G_OBJECT (a));
309 g_hash_table_remove (app->hash, a->parent.path);
310 g_object_unref (a); /* unref our own ref */
311 return DBUS_HANDLER_RESULT_HANDLED;
315 add_app_to_desktop (AtspiAccessible *a, const char *bus_name)
319 dbus_error_init (&error);
320 AtspiAccessible *obj = ref_accessible (bus_name, atspi_path_root);
323 GList *new_list = g_list_append (a->children, obj);
326 a->children = new_list;
332 g_warning ("AT-SPI: Error calling getRoot for %s: %s", bus_name, error.message);
338 send_children_changed (AtspiAccessible *parent, AtspiAccessible *child, gboolean add)
342 memset (&e, 0, sizeof (e));
343 e.type = (add? "object:children-changed:add": "object:children-changed:remove");
345 e.detail1 = g_list_index (parent->children, child);
347 _atspi_send_event (&e);
351 unref_object_and_descendants (AtspiAccessible *obj)
355 for (l = obj->children; l; l = l->next)
357 unref_object_and_descendants (l->data);
359 g_object_unref (obj);
363 remove_app_from_desktop (AtspiAccessible *a, const char *bus_name)
366 AtspiAccessible *child;
368 for (l = a->children; l; l = l->next)
371 if (!strcmp (bus_name, child->parent.app->bus_name)) break;
377 send_children_changed (a, child, FALSE);
378 a->children = g_list_remove (a->children, child);
379 unref_object_and_descendants (child);
383 static AtspiAccessible *desktop;
386 get_reference_from_iter (DBusMessageIter *iter, const char **app_name, const char **path)
388 DBusMessageIter iter_struct;
390 dbus_message_iter_recurse (iter, &iter_struct);
391 dbus_message_iter_get_basic (&iter_struct, app_name);
392 dbus_message_iter_next (&iter_struct);
393 dbus_message_iter_get_basic (&iter_struct, path);
394 dbus_message_iter_next (iter);
398 add_accessible_from_iter (DBusMessageIter *iter)
401 DBusMessageIter iter_struct, iter_array;
402 const char *app_name, *path;
403 AtspiAccessible *accessible;
404 const char *name, *description;
407 dbus_message_iter_recurse (iter, &iter_struct);
410 get_reference_from_iter (&iter_struct, &app_name, &path);
411 accessible = ref_accessible (app_name, path);
415 /* Get application: TODO */
416 dbus_message_iter_next (&iter_struct);
419 get_reference_from_iter (&iter_struct, &app_name, &path);
420 if (accessible->accessible_parent)
421 g_object_unref (accessible->accessible_parent);
422 accessible->accessible_parent = ref_accessible (app_name, path);
425 while (accessible->children)
427 g_object_unref (accessible->children->data);
428 accessible->children = g_list_remove (accessible->children, accessible->children->data);
430 dbus_message_iter_recurse (&iter_struct, &iter_array);
431 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
433 AtspiAccessible *child;
434 get_reference_from_iter (&iter_array, &app_name, &path);
435 child = ref_accessible (app_name, path);
436 new_list = g_list_append (accessible->children, child);
437 if (new_list) accessible->children = new_list;
441 dbus_message_iter_next (&iter_struct);
442 _atspi_dbus_set_interfaces (accessible, &iter_struct);
443 dbus_message_iter_next (&iter_struct);
446 if (accessible->name)
447 g_free (accessible->name);
448 dbus_message_iter_get_basic (&iter_struct, &name);
449 accessible->name = g_strdup (name);
450 dbus_message_iter_next (&iter_struct);
453 dbus_message_iter_get_basic (&iter_struct, &role);
454 accessible->role = role;
455 dbus_message_iter_next (&iter_struct);
458 if (accessible->description)
459 g_free (accessible->description);
460 dbus_message_iter_get_basic (&iter_struct, &description);
461 accessible->description = g_strdup (description);
462 dbus_message_iter_next (&iter_struct);
464 _atspi_dbus_set_state (accessible, &iter_struct);
465 dbus_message_iter_next (&iter_struct);
467 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_NAME | ATSPI_CACHE_ROLE |
468 ATSPI_CACHE_PARENT | ATSPI_CACHE_DESCRIPTION);
469 if (!atspi_state_set_contains (accessible->states,
470 ATSPI_STATE_MANAGES_DESCENDANTS))
471 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_CHILDREN);
473 /* This is a bit of a hack since the cache holds a ref, so we don't need
474 * the one provided for us anymore */
475 g_object_unref (accessible);
479 handle_get_items (DBusPendingCall *pending, void *user_data)
481 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
482 DBusMessageIter iter, iter_array;
484 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
486 const char *sender = dbus_message_get_sender (reply);
487 const char *error = NULL;
488 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &error,
490 g_warning ("AT-SPI: Error in GetItems, sender=%s, error=%s", sender, error);
491 dbus_message_unref (reply);
492 dbus_pending_call_unref (pending);
496 dbus_message_iter_init (reply, &iter);
497 dbus_message_iter_recurse (&iter, &iter_array);
498 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
500 add_accessible_from_iter (&iter_array);
501 dbus_message_iter_next (&iter_array);
503 dbus_message_unref (reply);
504 dbus_pending_call_unref (pending);
507 /* TODO: Do we stil need this function? */
508 static AtspiAccessible *
509 ref_accessible_desktop (AtspiApplication *app)
512 DBusMessage *message, *reply;
513 DBusMessageIter iter, iter_array;
518 g_object_ref (desktop);
521 desktop = atspi_accessible_new (app, atspi_path_root);
526 g_hash_table_insert (app->hash, desktop->parent.path, desktop);
527 g_object_ref (desktop); /* for the hash */
528 desktop->name = g_strdup ("main");
529 dbus_error_init (&error);
530 message = dbus_message_new_method_call (atspi_bus_registry,
532 atspi_interface_accessible,
536 reply = _atspi_dbus_send_with_reply_and_block (message, NULL);
537 if (!reply || strcmp (dbus_message_get_signature (reply), "a(so)") != 0)
539 g_warning ("Couldn't get application list: %s", error.message);
541 dbus_message_unref (reply);
544 dbus_message_iter_init (reply, &iter);
545 dbus_message_iter_recurse (&iter, &iter_array);
546 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
548 const char *app_name, *path;
549 get_reference_from_iter (&iter_array, &app_name, &path);
550 add_app_to_desktop (desktop, app_name);
552 dbus_message_unref (reply);
554 /* Record the alternate name as an alias for org.a11y.atspi.Registry */
555 bus_name_dup = g_strdup (dbus_message_get_sender (reply));
557 g_hash_table_insert (app_hash, bus_name_dup, app);
563 _atspi_ref_accessible (const char *app, const char *path)
565 AtspiApplication *a = get_application (app);
567 if ( APP_IS_REGISTRY(a))
569 return a->root = ref_accessible_desktop (a);
571 return ref_accessible (app, path);
575 _atspi_dbus_return_accessible_from_message (DBusMessage *message)
577 DBusMessageIter iter;
578 AtspiAccessible *retval = NULL;
579 const char *signature;
584 signature = dbus_message_get_signature (message);
585 if (!strcmp (signature, "(so)"))
587 dbus_message_iter_init (message, &iter);
588 retval = _atspi_dbus_return_accessible_from_iter (&iter);
592 g_warning ("AT-SPI: Called _atspi_dbus_return_accessible_from_message with strange signature %s", signature);
594 dbus_message_unref (message);
599 _atspi_dbus_return_accessible_from_iter (DBusMessageIter *iter)
601 const char *app_name, *path;
603 get_reference_from_iter (iter, &app_name, &path);
604 return ref_accessible (app_name, path);
608 _atspi_dbus_return_hyperlink_from_message (DBusMessage *message)
610 DBusMessageIter iter;
611 AtspiHyperlink *retval = NULL;
612 const char *signature = dbus_message_get_signature (message);
614 if (!strcmp (signature, "(so)"))
616 dbus_message_iter_init (message, &iter);
617 retval = _atspi_dbus_return_hyperlink_from_iter (&iter);
621 g_warning ("AT-SPI: Called _atspi_dbus_return_hyperlink_from_message with strange signature %s", signature);
623 dbus_message_unref (message);
628 _atspi_dbus_return_hyperlink_from_iter (DBusMessageIter *iter)
630 const char *app_name, *path;
632 get_reference_from_iter (iter, &app_name, &path);
633 return ref_hyperlink (app_name, path);
636 const char *cache_signal_type = "((so)(so)(so)a(so)assusau)";
638 static DBusHandlerResult
639 handle_add_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
641 DBusMessageIter iter;
642 const char *sender = dbus_message_get_sender (message);
644 if (strcmp (dbus_message_get_signature (message), cache_signal_type) != 0)
646 g_warning ("AT-SPI: AddAccessible with unknown signature %s\n",
647 dbus_message_get_signature (message));
648 return DBUS_HANDLER_RESULT_HANDLED;
651 dbus_message_iter_init (message, &iter);
652 add_accessible_from_iter (&iter);
653 return DBUS_HANDLER_RESULT_HANDLED;
659 DBusMessage *message;
663 static guint process_deferred_messages_id = -1;
666 process_deferred_message (BusDataClosure *closure)
668 int type = dbus_message_get_type (closure->message);
669 const char *interface = dbus_message_get_interface (closure->message);
671 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
672 !strncmp (interface, "org.a11y.atspi.Event.", 21))
674 atspi_dbus_handle_event (closure->bus, closure->message, closure->data);
676 if (dbus_message_is_method_call (closure->message, atspi_interface_device_event_listener, "NotifyEvent"))
678 atspi_dbus_handle_DeviceEvent (closure->bus,
679 closure->message, closure->data);
681 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "AddAccessible"))
683 handle_add_accessible (closure->bus, closure->message, closure->data);
685 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "RemoveAccessible"))
687 handle_remove_accessible (closure->bus, closure->message, closure->data);
691 static GList *deferred_messages = NULL;
694 _atspi_process_deferred_messages (gpointer data)
696 static int in_process_deferred_messages = 0;
698 if (in_process_deferred_messages)
700 in_process_deferred_messages = 1;
701 while (deferred_messages != NULL)
703 BusDataClosure *closure = deferred_messages->data;
704 process_deferred_message (closure);
705 deferred_messages = g_list_remove (deferred_messages, closure);
706 dbus_message_unref (closure->message);
707 dbus_connection_unref (closure->bus);
710 /* If data is NULL, assume that we were called from GLib */
712 process_deferred_messages_id = -1;
713 in_process_deferred_messages = 0;
717 static DBusHandlerResult
718 defer_message (DBusConnection *connection, DBusMessage *message, void *user_data)
720 BusDataClosure *closure = g_new (BusDataClosure, 1);
723 closure->bus = dbus_connection_ref (bus);
724 closure->message = dbus_message_ref (message);
725 closure->data = user_data;
727 new_list = g_list_append (deferred_messages, closure);
729 deferred_messages = new_list;
731 if (process_deferred_messages_id == -1)
732 process_deferred_messages_id = g_idle_add (_atspi_process_deferred_messages, NULL);
733 return DBUS_HANDLER_RESULT_HANDLED;
736 static DBusHandlerResult
737 atspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
739 int type = dbus_message_get_type (message);
740 const char *interface = dbus_message_get_interface (message);
742 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
743 !strncmp (interface, "org.a11y.atspi.Event.", 21))
745 return defer_message (bus, message, data);
747 if (dbus_message_is_method_call (message, atspi_interface_device_event_listener, "NotifyEvent"))
749 return defer_message (bus, message, data);
751 if (dbus_message_is_signal (message, atspi_interface_cache, "AddAccessible"))
753 return defer_message (bus, message, data);
755 if (dbus_message_is_signal (message, atspi_interface_cache, "RemoveAccessible"))
757 return defer_message (bus, message, data);
759 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
762 static const char *signal_interfaces[] =
764 "org.a11y.atspi.Event.Object",
765 "org.a11y.atspi.Event.Window",
766 "org.a11y.atspi.Event.Mouse",
767 "org.a11y.atspi.Event.Terminal",
768 "org.a11y.atspi.Event.Document",
769 "org.a11y.atspi.Event.Focus",
774 * Returns a 'canonicalized' value for DISPLAY,
775 * with the screen number stripped off if present.
777 * TODO: Avoid having duplicate functions for this here and in at-spi2-atk
780 spi_display_name (void)
782 static const char *canonical_display_name = NULL;
783 if (!canonical_display_name)
785 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
788 display_env = g_getenv ("DISPLAY");
789 if (!display_env || !display_env[0])
790 canonical_display_name = ":0";
793 gchar *display_p, *screen_p;
794 canonical_display_name = g_strdup (display_env);
795 display_p = g_utf8_strrchr (canonical_display_name, -1, ':');
796 screen_p = g_utf8_strrchr (canonical_display_name, -1, '.');
797 if (screen_p && display_p && (screen_p > display_p))
805 canonical_display_name = display_env;
808 return canonical_display_name;
814 * Connects to the accessibility registry and initializes the SPI.
816 * Returns: 0 on success, otherwise an integer error code.
835 dbus_error_init (&error);
836 bus = atspi_get_a11y_bus ();
839 dbus_bus_register (bus, &error);
840 atspi_dbus_connection_setup_with_g_main(bus, g_main_context_default());
841 dbus_connection_add_filter (bus, atspi_dbus_filter, NULL, NULL);
842 dbind_set_timeout (1000);
843 match = g_strdup_printf ("type='signal',interface='%s',member='AddAccessible'", atspi_interface_cache);
844 dbus_error_init (&error);
845 dbus_bus_add_match (bus, match, &error);
847 match = g_strdup_printf ("type='signal',interface='%s',member='RemoveAccessible'", atspi_interface_cache);
848 dbus_bus_add_match (bus, match, &error);
850 match = g_strdup_printf ("type='signal',interface='%s',member='ChildrenChanged'", atspi_interface_event_object);
851 dbus_bus_add_match (bus, match, &error);
853 match = g_strdup_printf ("type='signal',interface='%s',member='PropertyChange'", atspi_interface_event_object);
854 dbus_bus_add_match (bus, match, &error);
856 match = g_strdup_printf ("type='signal',interface='%s',member='StateChanged'", atspi_interface_event_object);
857 dbus_bus_add_match (bus, match, &error);
862 static GMainLoop *mainloop;
867 * Starts/enters the main event loop for the AT-SPI services.
869 * (NOTE: This method does not return control, it is exited via a call to
870 * atspi_event_quit () from within an event handler).
874 atspi_event_main (void)
876 mainloop = g_main_loop_new (NULL, FALSE);
877 g_main_loop_run (mainloop);
883 * Quits the last main event loop for the SPI services,
884 * see atspi_event_main
887 atspi_event_quit (void)
889 g_main_loop_quit (mainloop);
895 * Disconnects from the Accessibility Registry and releases
896 * any floating resources. Call only once at exit.
898 * Returns: 0 if there were no leaks, otherwise non zero.
910 atspi_inited = FALSE;
914 leaked = g_hash_table_size (live_refs);
927 _atspi_dbus_call (gpointer obj, const char *interface, const char *method, GError **error, const char *type, ...)
932 AtspiObject *aobj = ATSPI_OBJECT (obj);
934 if (!aobj->app || !aobj->app->bus)
936 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
937 _("The application no longer exists"));
941 va_start (args, type);
942 dbus_error_init (&err);
943 retval = dbind_method_call_reentrant_va (aobj->app->bus, aobj->app->bus_name,
944 aobj->path, interface, method, &err,
947 _atspi_process_deferred_messages ((gpointer)TRUE);
948 if (dbus_error_is_set (&err))
950 /* TODO: Set gerror */
951 dbus_error_free (&err);
957 _atspi_dbus_call_partial (gpointer obj,
958 const char *interface,
961 const char *type, ...)
965 va_start (args, type);
966 return _atspi_dbus_call_partial_va (obj, interface, method, error, type, args);
970 _atspi_dbus_call_partial_va (gpointer obj,
971 const char *interface,
977 AtspiObject *aobj = ATSPI_OBJECT (obj);
979 DBusMessage *msg = NULL, *reply = NULL;
980 DBusMessageIter iter;
983 dbus_error_init (&err);
985 if (!aobj->app || !aobj->app->bus)
987 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
988 _("The application no longer exists"));
992 msg = dbus_message_new_method_call (aobj->app->bus_name, aobj->path, interface, method);
997 dbus_message_iter_init_append (msg, &iter);
998 dbind_any_marshal_va (&iter, &p, args);
1000 reply = dbind_send_and_allow_reentry (aobj->app->bus, msg, &err);
1004 dbus_message_unref (msg);
1005 _atspi_process_deferred_messages ((gpointer)TRUE);
1006 if (dbus_error_is_set (&err))
1008 /* TODO: Set gerror */
1009 dbus_error_free (&err);
1015 _atspi_dbus_get_property (gpointer obj, const char *interface, const char *name, GError **error, const char *type, void *data)
1017 DBusMessage *message, *reply;
1018 DBusMessageIter iter, iter_variant;
1020 dbus_bool_t retval = FALSE;
1021 AtspiObject *aobj = ATSPI_OBJECT (obj);
1026 if (!aobj->app || !aobj->app->bus)
1028 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
1029 _("The application no longer exists"));
1033 message = dbus_message_new_method_call (aobj->app->bus_name,
1035 "org.freedesktop.DBus.Properties",
1039 // TODO: throw exception
1042 dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1043 dbus_error_init (&err);
1044 reply = dbind_send_and_allow_reentry (aobj->app->bus, message, &err);
1045 dbus_message_unref (message);
1046 _atspi_process_deferred_messages ((gpointer)TRUE);
1049 // TODO: throw exception
1053 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
1056 dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &err, DBUS_TYPE_INVALID);
1058 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err);
1062 dbus_message_iter_init (reply, &iter);
1063 if (dbus_message_iter_get_arg_type (&iter) != 'v')
1065 g_warning ("AT-SPI: expected a variant when fetching %s from interface %s; got %s\n", name, interface, dbus_message_get_signature (reply));
1068 dbus_message_iter_recurse (&iter, &iter_variant);
1069 if (dbus_message_iter_get_arg_type (&iter_variant) != type[0])
1071 g_warning ("atspi_dbus_get_property: Wrong type: expected %s, got %c\n", type, dbus_message_iter_get_arg_type (&iter_variant));
1074 if (!strcmp (type, "(so)"))
1076 *((AtspiAccessible **)data) = _atspi_dbus_return_accessible_from_iter (&iter_variant);
1080 dbus_message_iter_get_basic (&iter_variant, data);
1081 if (type [0] == 's')
1082 *(char **)data = g_strdup (*(char **)data);
1087 dbus_message_unref (reply);
1092 _atspi_dbus_send_with_reply_and_block (DBusMessage *message, GError **error)
1096 AtspiApplication *app;
1097 DBusConnection *bus;
1099 app = get_application (dbus_message_get_destination (message));
1101 if (app && !app->bus)
1102 return NULL; /* will fail anyway; app has been disposed */
1104 bus = (app ? app->bus : _atspi_bus());
1105 dbus_error_init (&err);
1106 reply = dbind_send_and_allow_reentry (bus, message, &err);
1107 _atspi_process_deferred_messages ((gpointer)TRUE);
1108 dbus_message_unref (message);
1112 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err.message);
1113 dbus_error_free (&err);
1119 _atspi_dbus_return_hash_from_message (DBusMessage *message)
1121 DBusMessageIter iter;
1127 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1129 dbus_message_iter_init (message, &iter);
1130 ret = _atspi_dbus_hash_from_iter (&iter);
1131 dbus_message_unref (message);
1136 _atspi_dbus_hash_from_iter (DBusMessageIter *iter)
1138 GHashTable *hash = g_hash_table_new (g_str_hash, g_str_equal);
1139 DBusMessageIter iter_array, iter_dict;
1141 dbus_message_iter_recurse (iter, &iter_array);
1142 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1144 const char *name, *value;
1145 dbus_message_iter_recurse (&iter_array, &iter_dict);
1146 dbus_message_iter_get_basic (&iter_dict, &name);
1147 dbus_message_iter_next (&iter_dict);
1148 dbus_message_iter_get_basic (&iter_dict, &value);
1149 g_hash_table_insert (hash, g_strdup (name), g_strdup (value));
1150 dbus_message_iter_next (&iter_array);
1156 _atspi_dbus_return_attribute_array_from_message (DBusMessage *message)
1158 DBusMessageIter iter;
1164 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1166 dbus_message_iter_init (message, &iter);
1168 ret = _atspi_dbus_attribute_array_from_iter (&iter);
1169 dbus_message_unref (message);
1174 _atspi_dbus_attribute_array_from_iter (DBusMessageIter *iter)
1176 DBusMessageIter iter_array, iter_dict;
1177 GArray *array = g_array_new (TRUE, TRUE, sizeof (gchar *));
1179 dbus_message_iter_recurse (iter, &iter_array);
1180 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1182 const char *name, *value;
1185 dbus_message_iter_recurse (&iter_array, &iter_dict);
1186 dbus_message_iter_get_basic (&iter_dict, &name);
1187 dbus_message_iter_next (&iter_dict);
1188 dbus_message_iter_get_basic (&iter_dict, &value);
1189 str = g_strdup_printf ("%s:%s", name, value);
1190 new_array = g_array_append_val (array, str);
1193 dbus_message_iter_next (&iter_array);;
1199 _atspi_dbus_set_interfaces (AtspiAccessible *accessible, DBusMessageIter *iter)
1201 DBusMessageIter iter_array;
1203 accessible->interfaces = 0;
1204 dbus_message_iter_recurse (iter, &iter_array);
1205 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1209 dbus_message_iter_get_basic (&iter_array, &iface);
1210 if (!strcmp (iface, "org.freedesktop.DBus.Introspectable")) continue;
1211 n = _atspi_get_iface_num (iface);
1214 g_warning ("AT-SPI: Unknown interface %s", iface);
1217 accessible->interfaces |= (1 << n);
1218 dbus_message_iter_next (&iter_array);
1220 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_INTERFACES);
1224 _atspi_dbus_set_state (AtspiAccessible *accessible, DBusMessageIter *iter)
1226 DBusMessageIter iter_array;
1228 dbus_uint32_t *states;
1230 dbus_message_iter_recurse (iter, &iter_array);
1231 dbus_message_iter_get_fixed_array (&iter_array, &states, &count);
1234 g_warning ("AT-SPI: expected 2 values in states array; got %d\n", count);
1235 if (!accessible->states)
1236 accessible->states = _atspi_state_set_new_internal (accessible, 0);
1240 guint64 val = ((guint64)states [1]) << 32;
1242 if (!accessible->states)
1243 accessible->states = _atspi_state_set_new_internal (accessible, val);
1245 accessible->states->states = val;
1247 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_STATES);
1251 atspi_error_quark (void)
1253 return g_quark_from_static_string ("atspi_error");
1257 * Gets the IOR from the XDisplay.
1260 get_accessibility_bus_address_x11 (void)
1264 Display *bridge_display;
1266 unsigned char *data = NULL;
1267 unsigned long nitems;
1268 unsigned long leftover;
1270 bridge_display = XOpenDisplay (spi_display_name ());
1271 if (!bridge_display)
1273 g_warning ("Could not open X display");
1277 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
1278 XGetWindowProperty (bridge_display,
1279 XDefaultRootWindow (bridge_display),
1281 (long) BUFSIZ, False,
1282 (Atom) 31, &actual_type, &actual_format,
1283 &nitems, &leftover, &data);
1284 XCloseDisplay (bridge_display);
1286 return g_strdup (data);
1290 get_accessibility_bus_address_dbus (void)
1292 DBusConnection *session_bus = NULL;
1293 DBusMessage *message;
1296 char *address = NULL;
1298 session_bus = dbus_bus_get (DBUS_BUS_SESSION, NULL);
1302 message = dbus_message_new_method_call ("org.a11y.Bus",
1307 dbus_error_init (&error);
1308 reply = dbus_connection_send_with_reply_and_block (session_bus,
1312 dbus_message_unref (message);
1316 g_warning ("Error retrieving accessibility bus address: %s: %s",
1317 error.name, error.message);
1318 dbus_error_init (&error);
1323 const char *tmp_address;
1324 if (!dbus_message_get_args (reply,
1330 dbus_message_unref (reply);
1333 address = g_strdup (tmp_address);
1334 dbus_message_unref (reply);
1341 atspi_get_a11y_bus (void)
1343 DBusConnection *bus = NULL;
1347 address = get_accessibility_bus_address_x11 ();
1349 address = get_accessibility_bus_address_dbus ();
1353 dbus_error_init (&error);
1354 bus = dbus_connection_open (address, &error);
1359 g_warning ("Couldn't connect to accessibility bus: %s", error.message);
1364 if (!dbus_bus_register (bus, &error))
1366 g_warning ("Couldn't register with accessibility bus: %s", error.message);