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 GMainLoop *atspi_main_loop;
43 gboolean atspi_no_cache;
45 const char *atspi_path_dec = ATSPI_DBUS_PATH_DEC;
46 const char *atspi_path_registry = ATSPI_DBUS_PATH_REGISTRY;
47 const char *atspi_path_root = ATSPI_DBUS_PATH_ROOT;
48 const char *atspi_bus_registry = ATSPI_DBUS_NAME_REGISTRY;
49 const char *atspi_interface_accessible = ATSPI_DBUS_INTERFACE_ACCESSIBLE;
50 const char *atspi_interface_action = ATSPI_DBUS_INTERFACE_ACTION;
51 const char *atspi_interface_application = ATSPI_DBUS_INTERFACE_APPLICATION;
52 const char *atspi_interface_collection = ATSPI_DBUS_INTERFACE_COLLECTION;
53 const char *atspi_interface_component = ATSPI_DBUS_INTERFACE_COMPONENT;
54 const char *atspi_interface_dec = ATSPI_DBUS_INTERFACE_DEC;
55 const char *atspi_interface_device_event_listener = ATSPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER;
56 const char *atspi_interface_document = ATSPI_DBUS_INTERFACE_DOCUMENT;
57 const char *atspi_interface_editable_text = ATSPI_DBUS_INTERFACE_EDITABLE_TEXT;
58 const char *atspi_interface_event_object = ATSPI_DBUS_INTERFACE_EVENT_OBJECT;
59 const char *atspi_interface_hyperlink = ATSPI_DBUS_INTERFACE_HYPERLINK;
60 const char *atspi_interface_hypertext = ATSPI_DBUS_INTERFACE_HYPERTEXT;
61 const char *atspi_interface_image = ATSPI_DBUS_INTERFACE_IMAGE;
62 const char *atspi_interface_registry = ATSPI_DBUS_INTERFACE_REGISTRY;
63 const char *atspi_interface_selection = ATSPI_DBUS_INTERFACE_SELECTION;
64 const char *atspi_interface_table = ATSPI_DBUS_INTERFACE_TABLE;
65 const char *atspi_interface_text = ATSPI_DBUS_INTERFACE_TEXT;
66 const char *atspi_interface_cache = ATSPI_DBUS_INTERFACE_CACHE;
67 const char *atspi_interface_value = ATSPI_DBUS_INTERFACE_VALUE;
69 static const char *interfaces[] =
71 ATSPI_DBUS_INTERFACE_ACCESSIBLE,
72 ATSPI_DBUS_INTERFACE_ACTION,
73 ATSPI_DBUS_INTERFACE_APPLICATION,
74 ATSPI_DBUS_INTERFACE_COLLECTION,
75 ATSPI_DBUS_INTERFACE_COMPONENT,
76 ATSPI_DBUS_INTERFACE_DOCUMENT,
77 ATSPI_DBUS_INTERFACE_EDITABLE_TEXT,
78 ATSPI_DBUS_INTERFACE_HYPERLINK,
79 ATSPI_DBUS_INTERFACE_HYPERTEXT,
80 ATSPI_DBUS_INTERFACE_IMAGE,
81 "org.a11y.atspi.LoginHelper",
82 ATSPI_DBUS_INTERFACE_SELECTION,
83 ATSPI_DBUS_INTERFACE_TABLE,
84 ATSPI_DBUS_INTERFACE_TEXT,
85 ATSPI_DBUS_INTERFACE_VALUE,
90 _atspi_get_iface_num (const char *iface)
92 /* TODO: Use a binary search or hash to improve performance */
95 for (i = 0; interfaces[i]; i++)
97 if (!strcmp(iface, interfaces[i])) return i;
107 live_refs = g_hash_table_new (g_direct_hash, g_direct_equal);
112 /* TODO: Add an application parameter */
119 g_error ("AT-SPI: COuldn't connect to accessibility bus. Is at-spi-bus-launcher running?");
123 #define APP_IS_REGISTRY(app) (!strcmp (app->bus_name, atspi_bus_registry))
134 g_hash_table_destroy (refs);
138 static gboolean atspi_inited = FALSE;
140 static GHashTable *app_hash = NULL;
143 handle_get_bus_address (DBusPendingCall *pending, void *user_data)
145 AtspiApplication *app = user_data;
146 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
147 DBusMessage *message;
149 DBusPendingCall *new_pending;
151 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
153 if (dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &address,
157 dbus_error_init (&error);
158 DBusConnection *bus = dbus_connection_open (address, &error);
162 dbus_connection_unref (app->bus);
167 dbus_message_unref (reply);
168 dbus_pending_call_unref (pending);
171 return; /* application has gone away / been disposed */
173 message = dbus_message_new_method_call (app->bus_name,
174 "/org/a11y/atspi/cache",
175 atspi_interface_cache, "GetItems");
177 dbus_connection_send_with_reply (app->bus, message, &new_pending, 2000);
178 dbus_pending_call_set_notify (new_pending, handle_get_items, app, NULL);
179 dbus_message_unref (message);
182 static AtspiApplication *
183 get_application (const char *bus_name)
185 AtspiApplication *app = NULL;
187 DBusMessage *message;
189 DBusPendingCall *pending = NULL;
193 app_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_object_unref);
194 if (!app_hash) return NULL;
196 app = g_hash_table_lookup (app_hash, bus_name);
198 bus_name_dup = g_strdup (bus_name);
199 if (!bus_name_dup) return NULL;
200 // TODO: change below to something that will send state-change:defunct notification if necessary */
201 app = _atspi_application_new (bus_name);
202 if (!app) return NULL;
203 app->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
204 app->bus = dbus_connection_ref (_atspi_bus ());
205 app->cache = ATSPI_CACHE_UNDEFINED;
206 g_hash_table_insert (app_hash, bus_name_dup, app);
207 dbus_error_init (&error);
208 message = dbus_message_new_method_call (bus_name, atspi_path_root,
209 atspi_interface_application, "GetApplicationBusAddress");
211 dbus_connection_send_with_reply (app->bus, message, &pending, 2000);
212 dbus_pending_call_set_notify (pending, handle_get_bus_address, app, NULL);
213 dbus_message_unref (message);
217 static AtspiAccessible *
218 ref_accessible (const char *app_name, const char *path)
220 AtspiApplication *app;
223 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
226 app = get_application (app_name);
228 if (!strcmp (path, "/org/a11y/atspi/accessible/root"))
232 app->root = atspi_accessible_new (app, atspi_path_root);
233 app->root->accessible_parent = atspi_get_desktop (0);
235 return g_object_ref (app->root);
238 a = g_hash_table_lookup (app->hash, path);
241 return g_object_ref (a);
243 a = atspi_accessible_new (app, path);
246 g_hash_table_insert (app->hash, g_strdup (a->parent.path), a);
247 g_object_ref (a); /* for the hash */
251 static AtspiHyperlink *
252 ref_hyperlink (const char *app_name, const char *path)
254 AtspiApplication *app = get_application (app_name);
255 AtspiHyperlink *hyperlink;
257 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
260 hyperlink = g_hash_table_lookup (app->hash, path);
263 return g_object_ref (hyperlink);
265 hyperlink = _atspi_hyperlink_new (app, path);
266 g_hash_table_insert (app->hash, g_strdup (hyperlink->parent.path), hyperlink);
267 /* TODO: This should be a weak ref */
268 g_object_ref (hyperlink); /* for the hash */
281 GArray *state_bitflags;
284 static DBusHandlerResult
285 handle_remove_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
287 const char *sender = dbus_message_get_sender (message);
288 AtspiApplication *app;
290 DBusMessageIter iter, iter_struct;
291 const char *signature = dbus_message_get_signature (message);
294 if (strcmp (signature, "(so)") != 0)
296 g_warning ("AT-SPI: Unknown signature %s for RemoveAccessible", signature);
297 return DBUS_HANDLER_RESULT_HANDLED;
300 dbus_message_iter_init (message, &iter);
301 dbus_message_iter_recurse (&iter, &iter_struct);
302 dbus_message_iter_get_basic (&iter_struct, &sender);
303 dbus_message_iter_next (&iter_struct);
304 dbus_message_iter_get_basic (&iter_struct, &path);
305 app = get_application (sender);
306 a = ref_accessible (sender, path);
308 return DBUS_HANDLER_RESULT_HANDLED;
309 g_object_run_dispose (G_OBJECT (a));
310 g_hash_table_remove (app->hash, a->parent.path);
311 g_object_unref (a); /* unref our own ref */
312 return DBUS_HANDLER_RESULT_HANDLED;
316 add_app_to_desktop (AtspiAccessible *a, const char *bus_name)
320 dbus_error_init (&error);
321 AtspiAccessible *obj = ref_accessible (bus_name, atspi_path_root);
324 GList *new_list = g_list_append (a->children, obj);
327 a->children = new_list;
333 g_warning ("AT-SPI: Error calling getRoot for %s: %s", bus_name, error.message);
339 send_children_changed (AtspiAccessible *parent, AtspiAccessible *child, gboolean add)
343 memset (&e, 0, sizeof (e));
344 e.type = (add? "object:children-changed:add": "object:children-changed:remove");
346 e.detail1 = g_list_index (parent->children, child);
348 _atspi_send_event (&e);
352 unref_object_and_descendants (AtspiAccessible *obj)
356 for (l = obj->children; l; l = l->next)
358 unref_object_and_descendants (l->data);
360 g_object_unref (obj);
364 remove_app_from_desktop (AtspiAccessible *a, const char *bus_name)
367 AtspiAccessible *child;
369 for (l = a->children; l; l = l->next)
372 if (!strcmp (bus_name, child->parent.app->bus_name)) break;
378 send_children_changed (a, child, FALSE);
379 a->children = g_list_remove (a->children, child);
380 unref_object_and_descendants (child);
384 static AtspiAccessible *desktop;
387 get_reference_from_iter (DBusMessageIter *iter, const char **app_name, const char **path)
389 DBusMessageIter iter_struct;
391 dbus_message_iter_recurse (iter, &iter_struct);
392 dbus_message_iter_get_basic (&iter_struct, app_name);
393 dbus_message_iter_next (&iter_struct);
394 dbus_message_iter_get_basic (&iter_struct, path);
395 dbus_message_iter_next (iter);
399 add_accessible_from_iter (DBusMessageIter *iter)
402 DBusMessageIter iter_struct, iter_array;
403 const char *app_name, *path;
404 AtspiAccessible *accessible;
405 const char *name, *description;
408 dbus_message_iter_recurse (iter, &iter_struct);
411 get_reference_from_iter (&iter_struct, &app_name, &path);
412 accessible = ref_accessible (app_name, path);
416 /* Get application: TODO */
417 dbus_message_iter_next (&iter_struct);
420 get_reference_from_iter (&iter_struct, &app_name, &path);
421 if (accessible->accessible_parent)
422 g_object_unref (accessible->accessible_parent);
423 accessible->accessible_parent = ref_accessible (app_name, path);
426 while (accessible->children)
428 g_object_unref (accessible->children->data);
429 accessible->children = g_list_remove (accessible->children, accessible->children->data);
431 dbus_message_iter_recurse (&iter_struct, &iter_array);
432 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
434 AtspiAccessible *child;
435 get_reference_from_iter (&iter_array, &app_name, &path);
436 child = ref_accessible (app_name, path);
437 new_list = g_list_append (accessible->children, child);
438 if (new_list) accessible->children = new_list;
442 dbus_message_iter_next (&iter_struct);
443 _atspi_dbus_set_interfaces (accessible, &iter_struct);
444 dbus_message_iter_next (&iter_struct);
447 if (accessible->name)
448 g_free (accessible->name);
449 dbus_message_iter_get_basic (&iter_struct, &name);
450 accessible->name = g_strdup (name);
451 dbus_message_iter_next (&iter_struct);
454 dbus_message_iter_get_basic (&iter_struct, &role);
455 accessible->role = role;
456 dbus_message_iter_next (&iter_struct);
459 if (accessible->description)
460 g_free (accessible->description);
461 dbus_message_iter_get_basic (&iter_struct, &description);
462 accessible->description = g_strdup (description);
463 dbus_message_iter_next (&iter_struct);
465 _atspi_dbus_set_state (accessible, &iter_struct);
466 dbus_message_iter_next (&iter_struct);
468 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_NAME | ATSPI_CACHE_ROLE |
469 ATSPI_CACHE_PARENT | ATSPI_CACHE_DESCRIPTION);
470 if (!atspi_state_set_contains (accessible->states,
471 ATSPI_STATE_MANAGES_DESCENDANTS))
472 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_CHILDREN);
474 /* This is a bit of a hack since the cache holds a ref, so we don't need
475 * the one provided for us anymore */
476 g_object_unref (accessible);
480 handle_get_items (DBusPendingCall *pending, void *user_data)
482 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
483 DBusMessageIter iter, iter_array;
485 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
487 const char *sender = dbus_message_get_sender (reply);
488 const char *error = NULL;
489 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &error,
491 g_warning ("AT-SPI: Error in GetItems, sender=%s, error=%s", sender, error);
492 dbus_message_unref (reply);
493 dbus_pending_call_unref (pending);
497 dbus_message_iter_init (reply, &iter);
498 dbus_message_iter_recurse (&iter, &iter_array);
499 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
501 add_accessible_from_iter (&iter_array);
502 dbus_message_iter_next (&iter_array);
504 dbus_message_unref (reply);
505 dbus_pending_call_unref (pending);
508 /* TODO: Do we stil need this function? */
509 static AtspiAccessible *
510 ref_accessible_desktop (AtspiApplication *app)
513 DBusMessage *message, *reply;
514 DBusMessageIter iter, iter_array;
519 g_object_ref (desktop);
522 desktop = atspi_accessible_new (app, atspi_path_root);
527 g_hash_table_insert (app->hash, desktop->parent.path, desktop);
528 g_object_ref (desktop); /* for the hash */
529 desktop->name = g_strdup ("main");
530 dbus_error_init (&error);
531 message = dbus_message_new_method_call (atspi_bus_registry,
533 atspi_interface_accessible,
537 reply = _atspi_dbus_send_with_reply_and_block (message, NULL);
538 if (!reply || strcmp (dbus_message_get_signature (reply), "a(so)") != 0)
540 g_warning ("Couldn't get application list: %s", error.message);
542 dbus_message_unref (reply);
545 dbus_message_iter_init (reply, &iter);
546 dbus_message_iter_recurse (&iter, &iter_array);
547 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
549 const char *app_name, *path;
550 get_reference_from_iter (&iter_array, &app_name, &path);
551 add_app_to_desktop (desktop, app_name);
553 dbus_message_unref (reply);
555 /* Record the alternate name as an alias for org.a11y.atspi.Registry */
556 bus_name_dup = g_strdup (dbus_message_get_sender (reply));
558 g_hash_table_insert (app_hash, bus_name_dup, app);
564 _atspi_ref_accessible (const char *app, const char *path)
566 AtspiApplication *a = get_application (app);
568 if ( APP_IS_REGISTRY(a))
570 return a->root = ref_accessible_desktop (a);
572 return ref_accessible (app, path);
576 _atspi_dbus_return_accessible_from_message (DBusMessage *message)
578 DBusMessageIter iter;
579 AtspiAccessible *retval = NULL;
580 const char *signature;
585 signature = dbus_message_get_signature (message);
586 if (!strcmp (signature, "(so)"))
588 dbus_message_iter_init (message, &iter);
589 retval = _atspi_dbus_return_accessible_from_iter (&iter);
593 g_warning ("AT-SPI: Called _atspi_dbus_return_accessible_from_message with strange signature %s", signature);
595 dbus_message_unref (message);
600 _atspi_dbus_return_accessible_from_iter (DBusMessageIter *iter)
602 const char *app_name, *path;
604 get_reference_from_iter (iter, &app_name, &path);
605 return ref_accessible (app_name, path);
609 _atspi_dbus_return_hyperlink_from_message (DBusMessage *message)
611 DBusMessageIter iter;
612 AtspiHyperlink *retval = NULL;
613 const char *signature = dbus_message_get_signature (message);
615 if (!strcmp (signature, "(so)"))
617 dbus_message_iter_init (message, &iter);
618 retval = _atspi_dbus_return_hyperlink_from_iter (&iter);
622 g_warning ("AT-SPI: Called _atspi_dbus_return_hyperlink_from_message with strange signature %s", signature);
624 dbus_message_unref (message);
629 _atspi_dbus_return_hyperlink_from_iter (DBusMessageIter *iter)
631 const char *app_name, *path;
633 get_reference_from_iter (iter, &app_name, &path);
634 return ref_hyperlink (app_name, path);
637 const char *cache_signal_type = "((so)(so)(so)a(so)assusau)";
639 static DBusHandlerResult
640 handle_add_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
642 DBusMessageIter iter;
643 const char *sender = dbus_message_get_sender (message);
645 if (strcmp (dbus_message_get_signature (message), cache_signal_type) != 0)
647 g_warning ("AT-SPI: AddAccessible with unknown signature %s\n",
648 dbus_message_get_signature (message));
649 return DBUS_HANDLER_RESULT_HANDLED;
652 dbus_message_iter_init (message, &iter);
653 add_accessible_from_iter (&iter);
654 return DBUS_HANDLER_RESULT_HANDLED;
660 DBusMessage *message;
664 static guint process_deferred_messages_id = -1;
667 process_deferred_message (BusDataClosure *closure)
669 int type = dbus_message_get_type (closure->message);
670 const char *interface = dbus_message_get_interface (closure->message);
672 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
673 !strncmp (interface, "org.a11y.atspi.Event.", 21))
675 _atspi_dbus_handle_event (closure->bus, closure->message, closure->data);
677 if (dbus_message_is_method_call (closure->message, atspi_interface_device_event_listener, "NotifyEvent"))
679 _atspi_dbus_handle_DeviceEvent (closure->bus,
680 closure->message, closure->data);
682 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "AddAccessible"))
684 handle_add_accessible (closure->bus, closure->message, closure->data);
686 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "RemoveAccessible"))
688 handle_remove_accessible (closure->bus, closure->message, closure->data);
692 static GQueue *deferred_messages = NULL;
695 _atspi_process_deferred_messages (gpointer data)
697 static int in_process_deferred_messages = 0;
698 BusDataClosure *closure;
700 if (in_process_deferred_messages)
702 in_process_deferred_messages = 1;
703 while (closure = g_queue_pop_head (deferred_messages))
705 process_deferred_message (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);
722 closure->bus = dbus_connection_ref (bus);
723 closure->message = dbus_message_ref (message);
724 closure->data = user_data;
726 g_queue_push_tail (deferred_messages, closure);
728 if (process_deferred_messages_id == -1)
729 process_deferred_messages_id = g_idle_add (_atspi_process_deferred_messages, NULL);
730 return DBUS_HANDLER_RESULT_HANDLED;
733 static DBusHandlerResult
734 atspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
736 int type = dbus_message_get_type (message);
737 const char *interface = dbus_message_get_interface (message);
739 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
740 !strncmp (interface, "org.a11y.atspi.Event.", 21))
742 return defer_message (bus, message, data);
744 if (dbus_message_is_method_call (message, atspi_interface_device_event_listener, "NotifyEvent"))
746 return defer_message (bus, message, data);
748 if (dbus_message_is_signal (message, atspi_interface_cache, "AddAccessible"))
750 return defer_message (bus, message, data);
752 if (dbus_message_is_signal (message, atspi_interface_cache, "RemoveAccessible"))
754 return defer_message (bus, message, data);
756 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
759 static const char *signal_interfaces[] =
761 "org.a11y.atspi.Event.Object",
762 "org.a11y.atspi.Event.Window",
763 "org.a11y.atspi.Event.Mouse",
764 "org.a11y.atspi.Event.Terminal",
765 "org.a11y.atspi.Event.Document",
766 "org.a11y.atspi.Event.Focus",
771 * Returns a 'canonicalized' value for DISPLAY,
772 * with the screen number stripped off if present.
774 * TODO: Avoid having duplicate functions for this here and in at-spi2-atk
777 spi_display_name (void)
779 static const char *canonical_display_name = NULL;
780 if (!canonical_display_name)
782 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
785 display_env = g_getenv ("DISPLAY");
786 if (!display_env || !display_env[0])
787 canonical_display_name = ":0";
790 gchar *display_p, *screen_p;
791 canonical_display_name = g_strdup (display_env);
792 display_p = g_utf8_strrchr (canonical_display_name, -1, ':');
793 screen_p = g_utf8_strrchr (canonical_display_name, -1, '.');
794 if (screen_p && display_p && (screen_p > display_p))
802 canonical_display_name = display_env;
805 return canonical_display_name;
811 * Connects to the accessibility registry and initializes the SPI.
813 * Returns: 0 on success, otherwise an integer error code.
820 const gchar *no_cache;
833 dbus_error_init (&error);
834 bus = atspi_get_a11y_bus ();
837 dbus_bus_register (bus, &error);
838 atspi_dbus_connection_setup_with_g_main(bus, g_main_context_default());
839 dbus_connection_add_filter (bus, atspi_dbus_filter, NULL, NULL);
840 dbind_set_timeout (1000);
841 match = g_strdup_printf ("type='signal',interface='%s',member='AddAccessible'", atspi_interface_cache);
842 dbus_error_init (&error);
843 dbus_bus_add_match (bus, match, &error);
845 match = g_strdup_printf ("type='signal',interface='%s',member='RemoveAccessible'", atspi_interface_cache);
846 dbus_bus_add_match (bus, match, &error);
848 match = g_strdup_printf ("type='signal',interface='%s',member='ChildrenChanged'", atspi_interface_event_object);
849 dbus_bus_add_match (bus, match, &error);
851 match = g_strdup_printf ("type='signal',interface='%s',member='PropertyChange'", atspi_interface_event_object);
852 dbus_bus_add_match (bus, match, &error);
854 match = g_strdup_printf ("type='signal',interface='%s',member='StateChanged'", atspi_interface_event_object);
855 dbus_bus_add_match (bus, match, &error);
858 no_cache = g_getenv ("ATSPI_NO_CACHE");
859 if (no_cache && g_strcmp0 (no_cache, "0") != 0)
860 atspi_no_cache = TRUE;
862 deferred_messages = g_queue_new ();
870 * Starts/enters the main event loop for the AT-SPI services.
872 * (NOTE: This method does not return control, it is exited via a call to
873 * atspi_event_quit () from within an event handler).
877 atspi_event_main (void)
879 atspi_main_loop = g_main_loop_new (NULL, FALSE);
880 g_main_loop_run (atspi_main_loop);
881 atspi_main_loop = NULL;
887 * Quits the last main event loop for the SPI services,
888 * see atspi_event_main
891 atspi_event_quit (void)
893 g_main_loop_quit (atspi_main_loop);
899 * Disconnects from the Accessibility Registry and releases
900 * any floating resources. Call only once at exit.
902 * Returns: 0 if there were no leaks, otherwise non zero.
914 atspi_inited = FALSE;
918 leaked = g_hash_table_size (live_refs);
931 _atspi_dbus_call (gpointer obj, const char *interface, const char *method, GError **error, const char *type, ...)
936 AtspiObject *aobj = ATSPI_OBJECT (obj);
938 if (!aobj->app || !aobj->app->bus)
940 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
941 _("The application no longer exists"));
945 va_start (args, type);
946 dbus_error_init (&err);
947 retval = dbind_method_call_reentrant_va (aobj->app->bus, aobj->app->bus_name,
948 aobj->path, interface, method, &err,
951 _atspi_process_deferred_messages ((gpointer)TRUE);
952 if (dbus_error_is_set (&err))
954 g_set_error(error, ATSPI_ERROR, ATSPI_ERROR_IPC, err.message);
955 dbus_error_free (&err);
961 _atspi_dbus_call_partial (gpointer obj,
962 const char *interface,
965 const char *type, ...)
969 va_start (args, type);
970 return _atspi_dbus_call_partial_va (obj, interface, method, error, type, args);
974 _atspi_dbus_call_partial_va (gpointer obj,
975 const char *interface,
981 AtspiObject *aobj = ATSPI_OBJECT (obj);
983 DBusMessage *msg = NULL, *reply = NULL;
984 DBusMessageIter iter;
987 dbus_error_init (&err);
989 if (!aobj->app || !aobj->app->bus)
991 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
992 _("The application no longer exists"));
996 msg = dbus_message_new_method_call (aobj->app->bus_name, aobj->path, interface, method);
1001 dbus_message_iter_init_append (msg, &iter);
1002 dbind_any_marshal_va (&iter, &p, args);
1004 reply = dbind_send_and_allow_reentry (aobj->app->bus, msg, &err);
1008 dbus_message_unref (msg);
1009 _atspi_process_deferred_messages ((gpointer)TRUE);
1010 if (dbus_error_is_set (&err))
1012 /* TODO: Set gerror */
1013 dbus_error_free (&err);
1019 _atspi_dbus_get_property (gpointer obj, const char *interface, const char *name, GError **error, const char *type, void *data)
1021 DBusMessage *message, *reply;
1022 DBusMessageIter iter, iter_variant;
1024 dbus_bool_t retval = FALSE;
1025 AtspiObject *aobj = ATSPI_OBJECT (obj);
1026 char expected_type = (type [0] == '(' ? 'r' : type [0]);
1031 if (!aobj->app || !aobj->app->bus)
1033 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
1034 _("The application no longer exists"));
1038 message = dbus_message_new_method_call (aobj->app->bus_name,
1040 "org.freedesktop.DBus.Properties",
1044 // TODO: throw exception
1047 dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1048 dbus_error_init (&err);
1049 reply = dbind_send_and_allow_reentry (aobj->app->bus, message, &err);
1050 dbus_message_unref (message);
1051 _atspi_process_deferred_messages ((gpointer)TRUE);
1054 // TODO: throw exception
1058 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
1061 dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &err, DBUS_TYPE_INVALID);
1063 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err);
1067 dbus_message_iter_init (reply, &iter);
1068 if (dbus_message_iter_get_arg_type (&iter) != 'v')
1070 g_warning ("AT-SPI: expected a variant when fetching %s from interface %s; got %s\n", name, interface, dbus_message_get_signature (reply));
1073 dbus_message_iter_recurse (&iter, &iter_variant);
1074 if (dbus_message_iter_get_arg_type (&iter_variant) != expected_type)
1076 g_warning ("atspi_dbus_get_property: Wrong type: expected %s, got %c\n", type, dbus_message_iter_get_arg_type (&iter_variant));
1079 if (!strcmp (type, "(so)"))
1081 *((AtspiAccessible **)data) = _atspi_dbus_return_accessible_from_iter (&iter_variant);
1085 dbus_message_iter_get_basic (&iter_variant, data);
1086 if (type [0] == 's')
1087 *(char **)data = g_strdup (*(char **)data);
1092 dbus_message_unref (reply);
1097 _atspi_dbus_send_with_reply_and_block (DBusMessage *message, GError **error)
1101 AtspiApplication *app;
1102 DBusConnection *bus;
1104 app = get_application (dbus_message_get_destination (message));
1106 if (app && !app->bus)
1107 return NULL; /* will fail anyway; app has been disposed */
1109 bus = (app ? app->bus : _atspi_bus());
1110 dbus_error_init (&err);
1111 reply = dbind_send_and_allow_reentry (bus, message, &err);
1112 _atspi_process_deferred_messages ((gpointer)TRUE);
1113 dbus_message_unref (message);
1117 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err.message);
1118 dbus_error_free (&err);
1124 _atspi_dbus_return_hash_from_message (DBusMessage *message)
1126 DBusMessageIter iter;
1132 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1134 dbus_message_iter_init (message, &iter);
1135 ret = _atspi_dbus_hash_from_iter (&iter);
1136 dbus_message_unref (message);
1141 _atspi_dbus_hash_from_iter (DBusMessageIter *iter)
1143 GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
1144 (GDestroyNotify) g_free,
1145 (GDestroyNotify) g_free);
1146 DBusMessageIter iter_array, iter_dict;
1148 dbus_message_iter_recurse (iter, &iter_array);
1149 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1151 const char *name, *value;
1152 dbus_message_iter_recurse (&iter_array, &iter_dict);
1153 dbus_message_iter_get_basic (&iter_dict, &name);
1154 dbus_message_iter_next (&iter_dict);
1155 dbus_message_iter_get_basic (&iter_dict, &value);
1156 g_hash_table_insert (hash, g_strdup (name), g_strdup (value));
1157 dbus_message_iter_next (&iter_array);
1163 _atspi_dbus_return_attribute_array_from_message (DBusMessage *message)
1165 DBusMessageIter iter;
1171 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1173 dbus_message_iter_init (message, &iter);
1175 ret = _atspi_dbus_attribute_array_from_iter (&iter);
1176 dbus_message_unref (message);
1181 _atspi_dbus_attribute_array_from_iter (DBusMessageIter *iter)
1183 DBusMessageIter iter_array, iter_dict;
1184 GArray *array = g_array_new (TRUE, TRUE, sizeof (gchar *));
1186 dbus_message_iter_recurse (iter, &iter_array);
1187 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1189 const char *name, *value;
1191 dbus_message_iter_recurse (&iter_array, &iter_dict);
1192 dbus_message_iter_get_basic (&iter_dict, &name);
1193 dbus_message_iter_next (&iter_dict);
1194 dbus_message_iter_get_basic (&iter_dict, &value);
1195 str = g_strdup_printf ("%s:%s", name, value);
1196 array = g_array_append_val (array, str);
1197 dbus_message_iter_next (&iter_array);;
1203 _atspi_dbus_set_interfaces (AtspiAccessible *accessible, DBusMessageIter *iter)
1205 DBusMessageIter iter_array;
1207 accessible->interfaces = 0;
1208 dbus_message_iter_recurse (iter, &iter_array);
1209 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1213 dbus_message_iter_get_basic (&iter_array, &iface);
1214 if (!strcmp (iface, "org.freedesktop.DBus.Introspectable")) continue;
1215 n = _atspi_get_iface_num (iface);
1218 g_warning ("AT-SPI: Unknown interface %s", iface);
1221 accessible->interfaces |= (1 << n);
1222 dbus_message_iter_next (&iter_array);
1224 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_INTERFACES);
1228 _atspi_dbus_set_state (AtspiAccessible *accessible, DBusMessageIter *iter)
1230 DBusMessageIter iter_array;
1232 dbus_uint32_t *states;
1234 dbus_message_iter_recurse (iter, &iter_array);
1235 dbus_message_iter_get_fixed_array (&iter_array, &states, &count);
1238 g_warning ("AT-SPI: expected 2 values in states array; got %d\n", count);
1239 if (!accessible->states)
1240 accessible->states = _atspi_state_set_new_internal (accessible, 0);
1244 guint64 val = ((guint64)states [1]) << 32;
1246 if (!accessible->states)
1247 accessible->states = _atspi_state_set_new_internal (accessible, val);
1249 accessible->states->states = val;
1251 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_STATES);
1255 _atspi_error_quark (void)
1257 return g_quark_from_static_string ("atspi_error");
1261 * Gets the IOR from the XDisplay.
1264 get_accessibility_bus_address_x11 (void)
1268 Display *bridge_display;
1270 unsigned char *data = NULL;
1271 unsigned long nitems;
1272 unsigned long leftover;
1274 bridge_display = XOpenDisplay (spi_display_name ());
1275 if (!bridge_display)
1277 g_warning ("Could not open X display");
1281 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
1282 XGetWindowProperty (bridge_display,
1283 XDefaultRootWindow (bridge_display),
1285 (long) BUFSIZ, False,
1286 (Atom) 31, &actual_type, &actual_format,
1287 &nitems, &leftover, &data);
1288 XCloseDisplay (bridge_display);
1290 return g_strdup (data);
1294 get_accessibility_bus_address_dbus (void)
1296 DBusConnection *session_bus = NULL;
1297 DBusMessage *message;
1300 char *address = NULL;
1302 session_bus = dbus_bus_get (DBUS_BUS_SESSION, NULL);
1306 message = dbus_message_new_method_call ("org.a11y.Bus",
1311 dbus_error_init (&error);
1312 reply = dbus_connection_send_with_reply_and_block (session_bus,
1316 dbus_message_unref (message);
1320 g_warning ("Error retrieving accessibility bus address: %s: %s",
1321 error.name, error.message);
1322 dbus_error_init (&error);
1327 const char *tmp_address;
1328 if (!dbus_message_get_args (reply,
1334 dbus_message_unref (reply);
1337 address = g_strdup (tmp_address);
1338 dbus_message_unref (reply);
1345 atspi_get_a11y_bus (void)
1347 DBusConnection *bus = NULL;
1351 address = get_accessibility_bus_address_x11 ();
1353 address = get_accessibility_bus_address_dbus ();
1357 dbus_error_init (&error);
1358 bus = dbus_connection_open (address, &error);
1363 g_warning ("Couldn't connect to accessibility bus: %s", error.message);
1368 if (!dbus_bus_register (bus, &error))
1370 g_warning ("Couldn't register with accessibility bus: %s", error.message);