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"
32 #include "libregistry-internals.h"
34 #include "dbus/dbus-glib.h"
38 static void handle_get_items (DBusPendingCall *pending, void *user_data);
40 static DBusConnection *bus = NULL;
41 static GHashTable *live_refs = NULL;
43 const char *atspi_path_dec = ATSPI_DBUS_PATH_DEC;
44 const char *atspi_path_registry = ATSPI_DBUS_PATH_REGISTRY;
45 const char *atspi_path_root = ATSPI_DBUS_PATH_ROOT;
46 const char *atspi_bus_registry = ATSPI_DBUS_NAME_REGISTRY;
47 const char *atspi_interface_accessible = ATSPI_DBUS_INTERFACE_ACCESSIBLE;
48 const char *atspi_interface_action = ATSPI_DBUS_INTERFACE_ACTION;
49 const char *atspi_interface_application = ATSPI_DBUS_INTERFACE_APPLICATION;
50 const char *atspi_interface_collection = ATSPI_DBUS_INTERFACE_COLLECTION;
51 const char *atspi_interface_component = ATSPI_DBUS_INTERFACE_COMPONENT;
52 const char *atspi_interface_dec = ATSPI_DBUS_INTERFACE_DEC;
53 const char *atspi_interface_device_event_listener = ATSPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER;
54 const char *atspi_interface_document = ATSPI_DBUS_INTERFACE_DOCUMENT;
55 const char *atspi_interface_editable_text = ATSPI_DBUS_INTERFACE_EDITABLE_TEXT;
56 const char *atspi_interface_event_object = ATSPI_DBUS_INTERFACE_EVENT_OBJECT;
57 const char *atspi_interface_hyperlink = ATSPI_DBUS_INTERFACE_HYPERLINK;
58 const char *atspi_interface_hypertext = ATSPI_DBUS_INTERFACE_HYPERTEXT;
59 const char *atspi_interface_image = ATSPI_DBUS_INTERFACE_IMAGE;
60 const char *atspi_interface_registry = ATSPI_DBUS_INTERFACE_REGISTRY;
61 const char *atspi_interface_selection = ATSPI_DBUS_INTERFACE_SELECTION;
62 const char *atspi_interface_table = ATSPI_DBUS_INTERFACE_TABLE;
63 const char *atspi_interface_text = ATSPI_DBUS_INTERFACE_TEXT;
64 const char *atspi_interface_cache = ATSPI_DBUS_INTERFACE_CACHE;
65 const char *atspi_interface_value = ATSPI_DBUS_INTERFACE_VALUE;
67 static const char *interfaces[] =
69 ATSPI_DBUS_INTERFACE_ACCESSIBLE,
70 ATSPI_DBUS_INTERFACE_ACTION,
71 ATSPI_DBUS_INTERFACE_APPLICATION,
72 ATSPI_DBUS_INTERFACE_COLLECTION,
73 ATSPI_DBUS_INTERFACE_COMPONENT,
74 ATSPI_DBUS_INTERFACE_DOCUMENT,
75 ATSPI_DBUS_INTERFACE_EDITABLE_TEXT,
76 ATSPI_DBUS_INTERFACE_HYPERLINK,
77 ATSPI_DBUS_INTERFACE_HYPERTEXT,
78 ATSPI_DBUS_INTERFACE_IMAGE,
79 "org.a11y.atspi.LoginHelper",
80 ATSPI_DBUS_INTERFACE_SELECTION,
81 ATSPI_DBUS_INTERFACE_TABLE,
82 ATSPI_DBUS_INTERFACE_TEXT,
83 ATSPI_DBUS_INTERFACE_VALUE,
88 _atspi_get_iface_num (const char *iface)
90 /* TODO: Use a binary search or hash to improve performance */
93 for (i = 0; interfaces[i]; i++)
95 if (!strcmp(iface, interfaces[i])) return i;
105 live_refs = g_hash_table_new (g_direct_hash, g_direct_equal);
110 /* TODO: Add an application parameter */
119 #define APP_IS_REGISTRY(app) (!strcmp (app->bus_name, atspi_bus_registry))
130 g_hash_table_destroy (refs);
134 static gboolean atspi_inited = FALSE;
136 static GHashTable *app_hash = NULL;
139 handle_get_bus_address (DBusPendingCall *pending, void *user_data)
141 AtspiApplication *app = user_data;
142 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
143 DBusMessage *message;
145 DBusPendingCall *new_pending;
147 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
149 if (dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &address,
153 dbus_error_init (&error);
154 DBusConnection *bus = dbus_connection_open (address, &error);
158 dbus_connection_unref (app->bus);
163 dbus_message_unref (reply);
164 dbus_pending_call_unref (pending);
166 message = dbus_message_new_method_call (app->bus_name,
167 "/org/a11y/atspi/cache",
168 atspi_interface_cache, "GetItems");
170 dbus_connection_send_with_reply (app->bus, message, &new_pending, 2000);
171 dbus_pending_call_set_notify (new_pending, handle_get_items, app, NULL);
172 dbus_message_unref (message);
175 static AtspiApplication *
176 get_application (const char *bus_name)
178 AtspiApplication *app = NULL;
180 DBusMessage *message;
182 DBusPendingCall *pending = NULL;
186 app_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_object_unref);
187 if (!app_hash) return NULL;
189 app = g_hash_table_lookup (app_hash, bus_name);
191 bus_name_dup = g_strdup (bus_name);
192 if (!bus_name_dup) return NULL;
193 // TODO: change below to something that will send state-change:defunct notification if necessary */
194 app = _atspi_application_new (bus_name);
195 if (!app) return NULL;
196 app->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
197 app->bus = dbus_connection_ref (_atspi_bus ());
198 app->cache = ATSPI_CACHE_UNDEFINED;
199 g_hash_table_insert (app_hash, bus_name_dup, app);
200 dbus_error_init (&error);
201 message = dbus_message_new_method_call (bus_name, atspi_path_root,
202 atspi_interface_application, "GetApplicationBusAddress");
204 dbus_connection_send_with_reply (app->bus, message, &pending, 2000);
205 dbus_pending_call_set_notify (pending, handle_get_bus_address, app, NULL);
206 dbus_message_unref (message);
210 static AtspiAccessible *
211 ref_accessible (const char *app_name, const char *path)
213 AtspiApplication *app;
216 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
219 app = get_application (app_name);
221 if (!strcmp (path, "/org/a11y/atspi/accessible/root"))
225 app->root = atspi_accessible_new (app, atspi_path_root);
226 app->root->accessible_parent = atspi_get_desktop (0);
228 return g_object_ref (app->root);
231 a = g_hash_table_lookup (app->hash, path);
234 return g_object_ref (a);
236 a = atspi_accessible_new (app, path);
239 g_hash_table_insert (app->hash, g_strdup (a->parent.path), a);
240 g_object_ref (a); /* for the hash */
244 static AtspiHyperlink *
245 ref_hyperlink (const char *app_name, const char *path)
247 AtspiApplication *app = get_application (app_name);
248 AtspiHyperlink *hyperlink;
250 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
253 hyperlink = g_hash_table_lookup (app->hash, path);
256 return g_object_ref (hyperlink);
258 hyperlink = atspi_hyperlink_new (app, path);
261 g_hash_table_insert (app->hash, g_strdup (hyperlink->parent.path), hyperlink);
262 /* TODO: This should be a weak ref */
263 g_object_ref (hyperlink); /* for the hash */
276 GArray *state_bitflags;
279 static DBusHandlerResult
280 handle_remove_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
282 const char *sender = dbus_message_get_sender (message);
283 AtspiApplication *app;
285 DBusMessageIter iter, iter_struct;
286 const char *signature = dbus_message_get_signature (message);
289 if (strcmp (signature, "(so)") != 0)
291 g_warning (_("AT-SPI: Unknown signature %s for RemoveAccessible"), signature);
292 return DBUS_HANDLER_RESULT_HANDLED;
295 dbus_message_iter_init (message, &iter);
296 dbus_message_iter_recurse (&iter, &iter_struct);
297 dbus_message_iter_get_basic (&iter_struct, &sender);
298 dbus_message_iter_next (&iter_struct);
299 dbus_message_iter_get_basic (&iter_struct, &path);
300 app = get_application (sender);
301 a = ref_accessible (sender, path);
303 return DBUS_HANDLER_RESULT_HANDLED;
304 g_object_run_dispose (G_OBJECT (a));
305 g_hash_table_remove (app->hash, a->parent.path);
306 g_object_unref (a); /* unref our own ref */
307 return DBUS_HANDLER_RESULT_HANDLED;
311 add_app_to_desktop (AtspiAccessible *a, const char *bus_name)
315 dbus_error_init (&error);
316 AtspiAccessible *obj = ref_accessible (bus_name, atspi_path_root);
319 GList *new_list = g_list_append (a->children, obj);
322 a->children = new_list;
328 g_warning (_("AT-SPI: Error calling getRoot for %s: %s"), bus_name, error.message);
334 send_children_changed (AtspiAccessible *parent, AtspiAccessible *child, gboolean add)
338 memset (&e, 0, sizeof (e));
339 e.type = (add? "object:children-changed:add": "object:children-changed:remove");
341 e.detail1 = g_list_index (parent->children, child);
343 _atspi_send_event (&e);
347 unref_object_and_descendants (AtspiAccessible *obj)
351 for (l = obj->children; l; l = l->next)
353 unref_object_and_descendants (l->data);
355 g_object_unref (obj);
359 remove_app_from_desktop (AtspiAccessible *a, const char *bus_name)
362 AtspiAccessible *child;
364 for (l = a->children; l; l = l->next)
367 if (!strcmp (bus_name, child->parent.app->bus_name)) break;
373 send_children_changed (a, child, FALSE);
374 a->children = g_list_remove (a->children, child);
375 unref_object_and_descendants (child);
379 static AtspiAccessible *desktop;
382 get_reference_from_iter (DBusMessageIter *iter, const char **app_name, const char **path)
384 DBusMessageIter iter_struct;
386 dbus_message_iter_recurse (iter, &iter_struct);
387 dbus_message_iter_get_basic (&iter_struct, app_name);
388 dbus_message_iter_next (&iter_struct);
389 dbus_message_iter_get_basic (&iter_struct, path);
390 dbus_message_iter_next (iter);
394 add_accessible_from_iter (DBusMessageIter *iter)
397 DBusMessageIter iter_struct, iter_array;
398 const char *app_name, *path;
399 AtspiAccessible *accessible;
400 const char *name, *description;
403 dbus_message_iter_recurse (iter, &iter_struct);
406 get_reference_from_iter (&iter_struct, &app_name, &path);
407 accessible = ref_accessible (app_name, path);
411 /* Get application: TODO */
412 dbus_message_iter_next (&iter_struct);
415 get_reference_from_iter (&iter_struct, &app_name, &path);
416 if (accessible->accessible_parent)
417 g_object_unref (accessible->accessible_parent);
418 accessible->accessible_parent = ref_accessible (app_name, path);
421 while (accessible->children)
423 g_object_unref (accessible->children->data);
424 accessible->children = g_list_remove (accessible->children, accessible->children->data);
426 dbus_message_iter_recurse (&iter_struct, &iter_array);
427 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
429 AtspiAccessible *child;
430 get_reference_from_iter (&iter_array, &app_name, &path);
431 child = ref_accessible (app_name, path);
432 new_list = g_list_append (accessible->children, child);
433 if (new_list) accessible->children = new_list;
437 dbus_message_iter_next (&iter_struct);
438 _atspi_dbus_set_interfaces (accessible, &iter_struct);
439 dbus_message_iter_next (&iter_struct);
442 if (accessible->name)
443 g_free (accessible->name);
444 dbus_message_iter_get_basic (&iter_struct, &name);
445 accessible->name = g_strdup (name);
446 dbus_message_iter_next (&iter_struct);
449 dbus_message_iter_get_basic (&iter_struct, &role);
450 accessible->role = role;
451 dbus_message_iter_next (&iter_struct);
454 if (accessible->description)
455 g_free (accessible->description);
456 dbus_message_iter_get_basic (&iter_struct, &description);
457 accessible->description = g_strdup (description);
458 dbus_message_iter_next (&iter_struct);
460 _atspi_dbus_set_state (accessible, &iter_struct);
461 dbus_message_iter_next (&iter_struct);
463 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_NAME | ATSPI_CACHE_ROLE |
464 ATSPI_CACHE_PARENT | ATSPI_CACHE_DESCRIPTION);
465 if (!atspi_state_set_contains (accessible->states,
466 ATSPI_STATE_MANAGES_DESCENDANTS))
467 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_CHILDREN);
469 /* This is a bit of a hack since the cache holds a ref, so we don't need
470 * the one provided for us anymore */
471 g_object_unref (accessible);
475 handle_get_items (DBusPendingCall *pending, void *user_data)
477 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
478 DBusMessageIter iter, iter_array;
480 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
482 const char *sender = dbus_message_get_sender (reply);
483 const char *error = NULL;
484 dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &error,
486 g_warning (_("AT-SPI: Error in GetItems, sender=%s, error=%s"), sender, error);
487 dbus_message_unref (reply);
488 dbus_pending_call_unref (pending);
492 dbus_message_iter_init (reply, &iter);
493 dbus_message_iter_recurse (&iter, &iter_array);
494 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
496 add_accessible_from_iter (&iter_array);
497 dbus_message_iter_next (&iter_array);
499 dbus_message_unref (reply);
500 dbus_pending_call_unref (pending);
503 /* TODO: Do we stil need this function? */
504 static AtspiAccessible *
505 ref_accessible_desktop (AtspiApplication *app)
508 DBusMessage *message, *reply;
509 DBusMessageIter iter, iter_array;
514 g_object_ref (desktop);
517 desktop = atspi_accessible_new (app, atspi_path_root);
522 g_hash_table_insert (app->hash, desktop->parent.path, desktop);
523 g_object_ref (desktop); /* for the hash */
524 desktop->name = g_strdup ("main");
525 dbus_error_init (&error);
526 message = dbus_message_new_method_call (atspi_bus_registry,
528 atspi_interface_accessible,
532 reply = _atspi_dbus_send_with_reply_and_block (message, NULL);
533 if (!reply || strcmp (dbus_message_get_signature (reply), "a(so)") != 0)
535 g_warning ("Couldn't get application list: %s", error.message);
537 dbus_message_unref (reply);
540 dbus_message_iter_init (reply, &iter);
541 dbus_message_iter_recurse (&iter, &iter_array);
542 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
544 const char *app_name, *path;
545 get_reference_from_iter (&iter_array, &app_name, &path);
546 add_app_to_desktop (desktop, app_name);
548 dbus_message_unref (reply);
550 /* Record the alternate name as an alias for org.a11y.atspi.Registry */
551 bus_name_dup = g_strdup (dbus_message_get_sender (reply));
553 g_hash_table_insert (app_hash, bus_name_dup, app);
559 _atspi_ref_accessible (const char *app, const char *path)
561 AtspiApplication *a = get_application (app);
563 if ( APP_IS_REGISTRY(a))
565 return a->root = ref_accessible_desktop (a);
567 return ref_accessible (app, path);
571 _atspi_dbus_return_accessible_from_message (DBusMessage *message)
573 DBusMessageIter iter;
574 AtspiAccessible *retval = NULL;
575 const char *signature;
580 signature = dbus_message_get_signature (message);
581 if (!strcmp (signature, "(so)"))
583 dbus_message_iter_init (message, &iter);
584 retval = _atspi_dbus_return_accessible_from_iter (&iter);
588 g_warning (_("AT-SPI: Called _atspi_dbus_return_accessible_from_message with strange signature %s"), signature);
590 dbus_message_unref (message);
595 _atspi_dbus_return_accessible_from_iter (DBusMessageIter *iter)
597 const char *app_name, *path;
599 get_reference_from_iter (iter, &app_name, &path);
600 return ref_accessible (app_name, path);
604 _atspi_dbus_return_hyperlink_from_message (DBusMessage *message)
606 DBusMessageIter iter;
607 AtspiHyperlink *retval = NULL;
608 const char *signature = dbus_message_get_signature (message);
610 if (!strcmp (signature, "(so)"))
612 dbus_message_iter_init (message, &iter);
613 retval = _atspi_dbus_return_hyperlink_from_iter (&iter);
617 g_warning (_("AT-SPI: Called _atspi_dbus_return_hyperlink_from_message with strange signature %s"), signature);
619 dbus_message_unref (message);
624 _atspi_dbus_return_hyperlink_from_iter (DBusMessageIter *iter)
626 const char *app_name, *path;
628 get_reference_from_iter (iter, &app_name, &path);
629 return ref_hyperlink (app_name, path);
632 const char *cache_signal_type = "((so)(so)(so)a(so)assusau)";
634 static DBusHandlerResult
635 handle_add_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
637 DBusMessageIter iter;
638 const char *sender = dbus_message_get_sender (message);
640 if (strcmp (dbus_message_get_signature (message), cache_signal_type) != 0)
642 g_warning (_("AT-SPI: AddAccessible with unknown signature %s\n"),
643 dbus_message_get_signature (message));
644 return DBUS_HANDLER_RESULT_HANDLED;
647 dbus_message_iter_init (message, &iter);
648 add_accessible_from_iter (&iter);
649 return DBUS_HANDLER_RESULT_HANDLED;
655 DBusMessage *message;
659 static guint process_deferred_messages_id = -1;
662 process_deferred_message (BusDataClosure *closure)
664 int type = dbus_message_get_type (closure->message);
665 const char *interface = dbus_message_get_interface (closure->message);
667 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
668 !strncmp (interface, "org.a11y.atspi.Event.", 21))
670 atspi_dbus_handle_event (closure->bus, closure->message, closure->data);
672 if (dbus_message_is_method_call (closure->message, atspi_interface_device_event_listener, "NotifyEvent"))
674 atspi_dbus_handle_DeviceEvent (closure->bus,
675 closure->message, closure->data);
677 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "AddAccessible"))
679 handle_add_accessible (closure->bus, closure->message, closure->data);
681 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "RemoveAccessible"))
683 handle_remove_accessible (closure->bus, closure->message, closure->data);
687 static GList *deferred_messages = NULL;
690 _atspi_process_deferred_messages (gpointer data)
692 static int in_process_deferred_messages = 0;
694 if (in_process_deferred_messages)
696 in_process_deferred_messages = 1;
697 while (deferred_messages != NULL)
699 BusDataClosure *closure = deferred_messages->data;
700 process_deferred_message (closure);
701 deferred_messages = g_list_remove (deferred_messages, closure);
702 dbus_message_unref (closure->message);
703 dbus_connection_unref (closure->bus);
706 /* If data is NULL, assume that we were called from GLib */
708 process_deferred_messages_id = -1;
709 in_process_deferred_messages = 0;
713 static DBusHandlerResult
714 defer_message (DBusConnection *connection, DBusMessage *message, void *user_data)
716 BusDataClosure *closure = g_new (BusDataClosure, 1);
720 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
721 closure->bus = dbus_connection_ref (bus);
722 closure->message = dbus_message_ref (message);
723 closure->data = user_data;
725 new_list = g_list_append (deferred_messages, closure);
727 deferred_messages = new_list;
729 if (process_deferred_messages_id == -1)
730 process_deferred_messages_id = g_idle_add (_atspi_process_deferred_messages, NULL);
731 return DBUS_HANDLER_RESULT_HANDLED;
734 static DBusHandlerResult
735 atspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
737 int type = dbus_message_get_type (message);
738 const char *interface = dbus_message_get_interface (message);
740 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
741 !strncmp (interface, "org.a11y.atspi.Event.", 21))
743 return defer_message (bus, message, data);
745 if (dbus_message_is_method_call (message, atspi_interface_device_event_listener, "NotifyEvent"))
747 return defer_message (bus, message, data);
749 if (dbus_message_is_signal (message, atspi_interface_cache, "AddAccessible"))
751 return defer_message (bus, message, data);
753 if (dbus_message_is_signal (message, atspi_interface_cache, "RemoveAccessible"))
755 return defer_message (bus, message, data);
757 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
760 static const char *signal_interfaces[] =
762 "org.a11y.atspi.Event.Object",
763 "org.a11y.atspi.Event.Window",
764 "org.a11y.atspi.Event.Mouse",
765 "org.a11y.atspi.Event.Terminal",
766 "org.a11y.atspi.Event.Document",
767 "org.a11y.atspi.Event.Focus",
772 * Returns a 'canonicalized' value for DISPLAY,
773 * with the screen number stripped off if present.
775 * TODO: Avoid having duplicate functions for this here and in at-spi2-atk
778 spi_display_name (void)
780 static const char *canonical_display_name = NULL;
781 if (!canonical_display_name)
783 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
786 display_env = g_getenv ("DISPLAY");
787 if (!display_env || !display_env[0])
788 canonical_display_name = ":0";
791 gchar *display_p, *screen_p;
792 canonical_display_name = g_strdup (display_env);
793 display_p = g_utf8_strrchr (canonical_display_name, -1, ':');
794 screen_p = g_utf8_strrchr (canonical_display_name, -1, '.');
795 if (screen_p && display_p && (screen_p > display_p))
803 canonical_display_name = display_env;
806 return canonical_display_name;
812 * Connects to the accessibility registry and initializes the SPI.
814 * Returns: 0 on success, otherwise an integer error code.
833 dbus_error_init (&error);
834 bus = _libregistry_get_a11y_bus ();
837 dbus_bus_register (bus, &error);
838 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);
860 static GMainLoop *mainloop;
865 * Starts/enters the main event loop for the AT-SPI services.
867 * (NOTE: This method does not return control, it is exited via a call to
868 * atspi_event_quit () from within an event handler).
872 atspi_event_main (void)
874 mainloop = g_main_loop_new (NULL, FALSE);
875 g_main_loop_run (mainloop);
881 * Quits the last main event loop for the SPI services,
882 * see atspi_event_main
885 atspi_event_quit (void)
887 g_main_loop_quit (mainloop);
893 * Disconnects from the Accessibility Registry and releases
894 * any floating resources. Call only once at exit.
896 * Returns: 0 if there were no leaks, otherwise non zero.
908 atspi_inited = FALSE;
912 leaked = g_hash_table_size (live_refs);
925 _atspi_dbus_call (gpointer obj, const char *interface, const char *method, GError **error, const char *type, ...)
930 AtspiObject *aobj = ATSPI_OBJECT (obj);
932 if (!aobj->app || !aobj->app->bus)
934 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
935 _("The application no longer exists"));
939 va_start (args, type);
940 dbus_error_init (&err);
941 retval = dbind_method_call_reentrant_va (aobj->app->bus, aobj->app->bus_name,
942 aobj->path, interface, method, &err,
945 _atspi_process_deferred_messages ((gpointer)TRUE);
946 if (dbus_error_is_set (&err))
948 /* TODO: Set gerror */
949 dbus_error_free (&err);
955 _atspi_dbus_call_partial (gpointer obj,
956 const char *interface,
959 const char *type, ...)
963 va_start (args, type);
964 return _atspi_dbus_call_partial_va (obj, interface, method, error, type, args);
968 _atspi_dbus_call_partial_va (gpointer obj,
969 const char *interface,
975 AtspiObject *aobj = ATSPI_OBJECT (obj);
977 DBusMessage *msg = NULL, *reply = NULL;
978 DBusMessageIter iter;
981 dbus_error_init (&err);
983 if (!aobj->app || !aobj->app->bus)
985 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
986 _("The application no longer exists"));
990 msg = dbus_message_new_method_call (aobj->app->bus_name, aobj->path, interface, method);
995 dbus_message_iter_init_append (msg, &iter);
996 dbind_any_marshal_va (&iter, &p, args);
998 reply = dbind_send_and_allow_reentry (aobj->app->bus, msg, &err);
1002 dbus_message_unref (msg);
1003 _atspi_process_deferred_messages ((gpointer)TRUE);
1004 if (dbus_error_is_set (&err))
1006 /* TODO: Set gerror */
1007 dbus_error_free (&err);
1013 _atspi_dbus_get_property (gpointer obj, const char *interface, const char *name, GError **error, const char *type, void *data)
1015 DBusMessage *message, *reply;
1016 DBusMessageIter iter, iter_variant;
1018 dbus_bool_t retval = FALSE;
1019 AtspiObject *aobj = ATSPI_OBJECT (obj);
1024 if (!aobj->app || !aobj->app->bus)
1026 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
1027 _("The application no longer exists"));
1031 message = dbus_message_new_method_call (aobj->app->bus_name,
1033 "org.freedesktop.DBus.Properties",
1037 // TODO: throw exception
1040 dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1041 dbus_error_init (&err);
1042 reply = dbind_send_and_allow_reentry (aobj->app->bus, message, &err);
1043 dbus_message_unref (message);
1044 _atspi_process_deferred_messages ((gpointer)TRUE);
1047 // TODO: throw exception
1051 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
1054 dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &err, DBUS_TYPE_INVALID);
1056 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err);
1060 dbus_message_iter_init (reply, &iter);
1061 if (dbus_message_iter_get_arg_type (&iter) != 'v')
1063 g_warning (_("AT-SPI: expected a variant when fetching %s from interface %s; got %s\n"), name, interface, dbus_message_get_signature (reply));
1066 dbus_message_iter_recurse (&iter, &iter_variant);
1067 if (dbus_message_iter_get_arg_type (&iter_variant) != type[0])
1069 g_warning (_("atspi_dbus_get_property: Wrong type: expected %s, got %c\n"), type, dbus_message_iter_get_arg_type (&iter_variant));
1072 if (!strcmp (type, "(so)"))
1074 *((AtspiAccessible **)data) = _atspi_dbus_return_accessible_from_iter (&iter_variant);
1078 dbus_message_iter_get_basic (&iter_variant, data);
1079 if (type [0] == 's')
1080 *(char **)data = g_strdup (*(char **)data);
1085 dbus_message_unref (reply);
1090 _atspi_dbus_send_with_reply_and_block (DBusMessage *message, GError **error)
1094 AtspiApplication *app;
1095 DBusConnection *bus;
1097 app = get_application (dbus_message_get_destination (message));
1099 if (app && !app->bus)
1100 return NULL; /* will fail anyway; app has been disposed */
1102 bus = (app ? app->bus : _atspi_bus());
1103 dbus_error_init (&err);
1104 reply = dbind_send_and_allow_reentry (bus, message, &err);
1105 _atspi_process_deferred_messages ((gpointer)TRUE);
1106 dbus_message_unref (message);
1110 g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err.message);
1111 dbus_error_free (&err);
1117 _atspi_dbus_return_hash_from_message (DBusMessage *message)
1119 DBusMessageIter iter;
1125 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1127 dbus_message_iter_init (message, &iter);
1128 ret = _atspi_dbus_hash_from_iter (&iter);
1129 dbus_message_unref (message);
1134 _atspi_dbus_hash_from_iter (DBusMessageIter *iter)
1136 GHashTable *hash = g_hash_table_new (g_str_hash, g_str_equal);
1137 DBusMessageIter iter_array, iter_dict;
1139 dbus_message_iter_recurse (iter, &iter_array);
1140 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1142 const char *name, *value;
1143 dbus_message_iter_recurse (&iter_array, &iter_dict);
1144 dbus_message_iter_get_basic (&iter_dict, &name);
1145 dbus_message_iter_next (&iter_dict);
1146 dbus_message_iter_get_basic (&iter_dict, &value);
1147 g_hash_table_insert (hash, g_strdup (name), g_strdup (value));
1148 dbus_message_iter_next (&iter_array);
1154 _atspi_dbus_return_attribute_array_from_message (DBusMessage *message)
1156 DBusMessageIter iter;
1162 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1164 dbus_message_iter_init (message, &iter);
1166 ret = _atspi_dbus_attribute_array_from_iter (&iter);
1167 dbus_message_unref (message);
1172 _atspi_dbus_attribute_array_from_iter (DBusMessageIter *iter)
1174 DBusMessageIter iter_array, iter_dict;
1175 GArray *array = g_array_new (TRUE, TRUE, sizeof (gchar *));
1177 dbus_message_iter_recurse (iter, &iter_array);
1178 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1180 const char *name, *value;
1183 dbus_message_iter_recurse (&iter_array, &iter_dict);
1184 dbus_message_iter_get_basic (&iter_dict, &name);
1185 dbus_message_iter_next (&iter_dict);
1186 dbus_message_iter_get_basic (&iter_dict, &value);
1187 str = g_strdup_printf ("%s:%s", name, value);
1188 new_array = g_array_append_val (array, str);
1191 dbus_message_iter_next (&iter_array);;
1197 _atspi_dbus_set_interfaces (AtspiAccessible *accessible, DBusMessageIter *iter)
1199 DBusMessageIter iter_array;
1201 accessible->interfaces = 0;
1202 dbus_message_iter_recurse (iter, &iter_array);
1203 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1207 dbus_message_iter_get_basic (&iter_array, &iface);
1208 if (!strcmp (iface, "org.freedesktop.DBus.Introspectable")) continue;
1209 n = _atspi_get_iface_num (iface);
1212 g_warning (_("AT-SPI: Unknown interface %s"), iface);
1215 accessible->interfaces |= (1 << n);
1216 dbus_message_iter_next (&iter_array);
1218 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_INTERFACES);
1222 _atspi_dbus_set_state (AtspiAccessible *accessible, DBusMessageIter *iter)
1224 DBusMessageIter iter_array;
1226 dbus_uint32_t *states;
1228 dbus_message_iter_recurse (iter, &iter_array);
1229 dbus_message_iter_get_fixed_array (&iter_array, &states, &count);
1232 g_warning (_("AT-SPI: expected 2 values in states array; got %d\n"), count);
1233 if (!accessible->states)
1234 accessible->states = _atspi_state_set_new_internal (accessible, 0);
1238 guint64 val = ((guint64)states [1]) << 32;
1240 if (!accessible->states)
1241 accessible->states = _atspi_state_set_new_internal (accessible, val);
1243 accessible->states->states = val;
1245 _atspi_accessible_add_cache (accessible, ATSPI_CACHE_STATES);
1249 atspi_error_quark (void)
1251 return g_quark_from_static_string ("atspi_error");