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.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
26 * Basic SPI initialization and event loop function prototypes
30 #include "atspi-private.h"
35 static void handle_get_items (DBusPendingCall *pending, void *user_data);
37 static DBusConnection *bus = NULL;
38 static GHashTable *apps = NULL;
39 static GHashTable *live_refs = NULL;
40 static GQueue *exception_handlers = NULL;
41 static DBusError exception;
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;
138 static AtspiApplication *
139 get_application (const char *bus_name)
141 AtspiApplication *app = NULL;
143 DBusMessage *message;
145 DBusPendingCall *pending = NULL;
149 app_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_hash_table_unref);
150 if (!app_hash) return NULL;
152 app = g_hash_table_lookup (app_hash, bus_name);
154 bus_name_dup = g_strdup (bus_name);
155 if (!bus_name_dup) return NULL;
156 // TODO: change below to something that will send state-change:defunct notification if necessary */
157 app = _atspi_application_new (bus_name);
158 if (!app) return NULL;
159 app->bus_name = bus_name_dup;
160 app->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
161 g_hash_table_insert (app_hash, bus_name_dup, app);
162 dbus_error_init (&error);
163 message = dbus_message_new_method_call (bus_name, "/org/a11y/atspi/cache",
164 atspi_interface_cache, "GetItems");
166 dbus_connection_send_with_reply (_atspi_bus (), message, &pending, 2000);
167 dbus_pending_call_set_notify (pending, handle_get_items, app, NULL);
171 static AtspiAccessible *
172 ref_accessible (const char *app_name, const char *path)
174 AtspiApplication *app;
177 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
180 app = get_application (app_name);
182 if (!strcmp (path, "/org/a11y/atspi/accessible/root"))
186 app->root = atspi_accessible_new (app, atspi_path_root);
187 app->root->accessible_parent = atspi_get_desktop (0);
189 return g_object_ref (app->root);
192 a = g_hash_table_lookup (app->hash, path);
195 return g_object_ref (a);
197 a = atspi_accessible_new (app, path);
200 g_hash_table_insert (app->hash, a->parent.path, a);
201 g_object_ref (a); /* for the hash */
205 static AtspiHyperlink *
206 ref_hyperlink (const char *app_name, const char *path)
208 AtspiApplication *app = get_application (app_name);
209 AtspiHyperlink *hyperlink;
211 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
214 hyperlink = g_hash_table_lookup (app->hash, path);
217 return g_object_ref (hyperlink);
219 hyperlink = atspi_hyperlink_new (app, path);
222 g_hash_table_insert (app->hash, hyperlink->parent.path, hyperlink);
223 /* TODO: This should be a weak ref */
224 g_object_ref (hyperlink); /* for the hash */
237 GArray *state_bitflags;
240 static DBusHandlerResult
241 handle_remove_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
243 const char *sender = dbus_message_get_sender (message);
244 AtspiApplication *app;
246 DBusMessageIter iter, iter_struct;
247 const char *signature = dbus_message_get_signature (message);
251 if (strcmp (signature, "(so)") != 0)
253 g_warning ("at-spi: Unknown signature %s for RemoveAccessible", signature);
254 return DBUS_HANDLER_RESULT_HANDLED;
257 dbus_message_iter_init (message, &iter);
258 dbus_message_iter_recurse (&iter, &iter_struct);
259 dbus_message_iter_get_basic (&iter_struct, &sender);
260 dbus_message_iter_get_basic (&iter_struct, &path);
261 app = get_application (sender);
262 a = ref_accessible (sender, path);
264 return DBUS_HANDLER_RESULT_HANDLED;
265 if (a->accessible_parent && g_list_find (a->accessible_parent->children, a))
267 a->accessible_parent->children = g_list_remove (a->accessible_parent->children, a);
270 g_hash_table_remove (app->hash, app->bus_name);
271 g_object_unref (a); /* unref our own ref */
272 return DBUS_HANDLER_RESULT_HANDLED;
276 add_app_to_desktop (AtspiAccessible *a, const char *bus_name)
281 dbus_error_init (&error);
282 AtspiAccessible *obj = ref_accessible (bus_name, atspi_path_root);
285 GList *new_list = g_list_append (a->children, obj);
288 a->children = new_list;
294 g_warning ("Error calling getRoot for %s: %s", bus_name, error.message);
300 send_children_changed (AtspiAccessible *parent, AtspiAccessible *child, gboolean add)
304 memset (&e, 0, sizeof (e));
305 e.type = (add? "object:children-changed:add": "object:children-changed:remove");
307 e.detail1 = g_list_index (parent->children, child);
309 _atspi_send_event (&e);
313 unref_object_and_descendants (AtspiAccessible *obj)
317 for (l = obj->children; l; l = l->next)
319 unref_object_and_descendants (l->data);
321 g_object_unref (obj);
325 remove_app_from_desktop (AtspiAccessible *a, const char *bus_name)
328 AtspiAccessible *child;
330 for (l = a->children; l; l = l->next)
333 if (!strcmp (bus_name, child->parent.app->bus_name)) break;
337 g_warning ("Removing unregistered app %s; doing nothing\n", bus_name);
340 send_children_changed (a, child, FALSE);
341 a->children = g_list_remove (a->children, child);
342 unref_object_and_descendants (child);
346 static AtspiAccessible *desktop;
349 get_reference_from_iter (DBusMessageIter *iter, const char **app_name, const char **path)
351 DBusMessageIter iter_struct;
353 dbus_message_iter_recurse (iter, &iter_struct);
354 dbus_message_iter_get_basic (&iter_struct, app_name);
355 dbus_message_iter_next (&iter_struct);
356 dbus_message_iter_get_basic (&iter_struct, path);
357 dbus_message_iter_next (iter);
361 add_accessible_from_iter (DBusMessageIter *iter)
365 DBusMessageIter iter_struct, iter_array;
366 const char *app_name, *path;
367 AtspiApplication *app;
368 AtspiAccessible *accessible;
369 const char *name, *description;
372 dbus_message_iter_recurse (iter, &iter_struct);
375 get_reference_from_iter (&iter_struct, &app_name, &path);
376 accessible = ref_accessible (app_name, path);
380 /* Get application: TODO */
381 dbus_message_iter_next (&iter_struct);
384 get_reference_from_iter (&iter_struct, &app_name, &path);
385 if (accessible->accessible_parent)
386 g_object_unref (accessible->accessible_parent);
387 accessible->accessible_parent = ref_accessible (app_name, path);
390 while (accessible->children)
392 g_object_unref (accessible->children->data);
393 accessible->children = g_list_remove (accessible->children, accessible->children->data);
395 dbus_message_iter_recurse (&iter_struct, &iter_array);
396 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
398 AtspiAccessible *child;
399 get_reference_from_iter (&iter_array, &app_name, &path);
400 child = ref_accessible (app_name, path);
401 new_list = g_list_append (accessible->children, child);
402 if (new_list) accessible->children = new_list;
406 dbus_message_iter_next (&iter_struct);
407 _atspi_dbus_set_interfaces (accessible, &iter_struct);
408 dbus_message_iter_next (&iter_struct);
411 if (accessible->name)
412 g_free (accessible->name);
413 dbus_message_iter_get_basic (&iter_struct, &name);
414 accessible->name = g_strdup (name);
415 dbus_message_iter_next (&iter_struct);
418 dbus_message_iter_get_basic (&iter_struct, &role);
419 accessible->role = role;
420 dbus_message_iter_next (&iter_struct);
423 if (accessible->description)
424 g_free (accessible->description);
425 dbus_message_iter_get_basic (&iter_struct, &description);
426 accessible->description = g_strdup (description);
427 dbus_message_iter_next (&iter_struct);
429 _atspi_dbus_set_state (accessible, &iter_struct);
430 dbus_message_iter_next (&iter_struct);
432 accessible->cached_properties |= ATSPI_CACHE_NAME | ATSPI_CACHE_ROLE | ATSPI_CACHE_PARENT;
433 if (!atspi_state_set_contains (accessible->states,
434 ATSPI_STATE_MANAGES_DESCENDANTS))
435 accessible->cached_properties |= ATSPI_CACHE_CHILDREN;
437 /* This is a bit of a hack since the cache holds a ref, so we don't need
438 * the one provided for us anymore */
439 g_object_unref (accessible);
443 handle_get_items (DBusPendingCall *pending, void *user_data)
445 AtspiApplication *app = user_data;
446 DBusMessage *reply = dbus_pending_call_steal_reply (pending);
447 DBusMessageIter iter, iter_array;
449 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
451 dbus_message_unref (reply);
452 g_warning ("Atspi: Error in GetItems");
456 dbus_message_iter_init (reply, &iter);
457 dbus_message_iter_recurse (&iter, &iter_array);
458 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
460 add_accessible_from_iter (&iter_array);
461 dbus_message_iter_next (&iter_array);
463 dbus_message_unref (reply);
466 /* TODO: Do we stil need this function? */
467 static AtspiAccessible *
468 ref_accessible_desktop (AtspiApplication *app)
474 DBusMessage *message, *reply;
475 DBusMessageIter iter, iter_array;
479 g_object_ref (desktop);
482 desktop = atspi_accessible_new (app, atspi_path_root);
487 g_hash_table_insert (app->hash, desktop->parent.path, desktop);
488 g_object_ref (desktop); /* for the hash */
489 desktop->name = g_strdup ("main");
490 dbus_error_init (&error);
491 message = dbus_message_new_method_call (atspi_bus_registry,
493 atspi_interface_accessible,
497 reply = _atspi_dbus_send_with_reply_and_block (message);
498 if (!reply || strcmp (dbus_message_get_signature (reply), "a(so)") != 0)
500 g_error ("Couldn't get application list: %s", error.message);
502 dbus_message_unref (reply);
505 dbus_message_iter_init (reply, &iter);
506 dbus_message_iter_recurse (&iter, &iter_array);
507 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
509 const char *app_name, *path;
510 get_reference_from_iter (&iter_array, &app_name, &path);
511 add_app_to_desktop (desktop, app_name);
513 dbus_message_unref (reply);
518 _atspi_ref_accessible (const char *app, const char *path)
520 AtspiApplication *a = get_application (app);
522 if ( APP_IS_REGISTRY(a))
524 return ref_accessible_desktop (a);
526 return ref_accessible (app, path);
530 _atspi_dbus_return_accessible_from_message (DBusMessage *message)
532 DBusMessageIter iter;
533 AtspiAccessible *retval = NULL;
534 const char *signature = dbus_message_get_signature (message);
536 if (!strcmp (signature, "(so)"))
538 dbus_message_iter_init (message, &iter);
539 retval = _atspi_dbus_return_accessible_from_iter (&iter);
543 g_warning ("Atspi: Called _atspi_dbus_return_accessible_from_message with strange signature %s", signature);
545 dbus_message_unref (message);
550 _atspi_dbus_return_accessible_from_iter (DBusMessageIter *iter)
552 const char *app_name, *path;
554 get_reference_from_iter (iter, &app_name, &path);
555 return ref_accessible (app_name, path);
559 _atspi_dbus_return_hyperlink_from_message (DBusMessage *message)
561 DBusMessageIter iter;
562 AtspiHyperlink *retval = NULL;
563 const char *signature = dbus_message_get_signature (message);
565 if (!strcmp (signature, "(so)"))
567 dbus_message_iter_init (message, &iter);
568 retval = _atspi_dbus_return_hyperlink_from_iter (&iter);
572 g_warning ("Atspi: Called _atspi_dbus_return_hyperlink_from_message with strange signature %s", signature);
574 dbus_message_unref (message);
579 _atspi_dbus_return_hyperlink_from_iter (DBusMessageIter *iter)
581 const char *app_name, *path;
583 get_reference_from_iter (iter, &app_name, &path);
584 return ref_hyperlink (app_name, path);
587 const char *cache_signal_type = "((so)(so)(so)a(so)assusau)";
589 static DBusHandlerResult
590 handle_add_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
592 DBusMessageIter iter;
593 const char *sender = dbus_message_get_sender (message);
594 AtspiApplication *app = get_application (sender);
595 const char *type = cache_signal_type;
597 if (strcmp (dbus_message_get_signature (message), cache_signal_type) != 0)
599 g_warning ("atspi: AddAccessible with unknown signature %s\n", dbus_message_get_signature (message));
603 dbus_message_iter_init (message, &iter);
604 add_accessible_from_iter (&iter);
610 DBusMessage *message;
614 static guint process_deferred_messages_id = -1;
617 process_deferred_message (BusDataClosure *closure)
619 int type = dbus_message_get_type (closure->message);
620 const char *interface = dbus_message_get_interface (closure->message);
621 const char *member = dbus_message_get_member (closure->message);
625 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
626 !strncmp (interface, "org.a11y.atspi.Event.", 21))
628 atspi_dbus_handle_event (closure->bus, closure->message, closure->data);
630 if (dbus_message_is_method_call (closure->message, atspi_interface_device_event_listener, "NotifyEvent"))
632 atspi_dbus_handle_DeviceEvent (closure->bus,
633 closure->message, closure->data);
635 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "AddAccessible"))
637 handle_add_accessible (closure->bus, closure->message, closure->data);
639 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "RemoveAccessible"))
641 handle_remove_accessible (closure->bus, closure->message, closure->data);
645 static GList *deferred_messages = NULL;
648 _atspi_process_deferred_messages (gpointer data)
650 static int in_process_deferred_messages = 0;
652 if (in_process_deferred_messages)
654 in_process_deferred_messages = 1;
655 while (deferred_messages != NULL)
657 BusDataClosure *closure = deferred_messages->data;
658 process_deferred_message (closure);
659 deferred_messages = g_list_remove (deferred_messages, closure);
660 dbus_message_unref (closure->message);
661 dbus_connection_unref (closure->bus);
664 /* If data is NULL, assume that we were called from GLib */
666 process_deferred_messages_id = -1;
667 in_process_deferred_messages = 0;
671 static DBusHandlerResult
672 defer_message (DBusConnection *connection, DBusMessage *message, void *user_data)
674 BusDataClosure *closure = g_new (BusDataClosure, 1);
678 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
679 closure->bus = dbus_connection_ref (bus);
680 closure->message = dbus_message_ref (message);
681 closure->data = user_data;
683 new_list = g_list_append (deferred_messages, closure);
685 deferred_messages = new_list;
687 if (process_deferred_messages_id == -1)
688 process_deferred_messages_id = g_idle_add (_atspi_process_deferred_messages, NULL);
689 return DBUS_HANDLER_RESULT_HANDLED;
692 static DBusHandlerResult
693 atspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
695 int type = dbus_message_get_type (message);
696 const char *interface = dbus_message_get_interface (message);
697 const char *member = dbus_message_get_member (message);
701 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
702 !strncmp (interface, "org.a11y.atspi.Event.", 21))
704 return defer_message (bus, message, data);
706 if (dbus_message_is_method_call (message, atspi_interface_device_event_listener, "NotifyEvent"))
708 return defer_message (bus, message, data);
710 if (dbus_message_is_signal (message, atspi_interface_cache, "AddAccessible"))
712 return defer_message (bus, message, data);
714 if (dbus_message_is_signal (message, atspi_interface_cache, "RemoveAccessible"))
716 return defer_message (bus, message, data);
718 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
721 static const char *signal_interfaces[] =
723 "org.a11y.atspi.Event.Object",
724 "org.a11y.atspi.Event.Window",
725 "org.a11y.atspi.Event.Mouse",
726 "org.a11y.atspi.Event.Terminal",
727 "org.a11y.atspi.Event.Document",
728 "org.a11y.atspi.Event.Focus",
733 * Returns a 'canonicalized' value for DISPLAY,
734 * with the screen number stripped off if present.
736 * TODO: Avoid having duplicate functions for this here and in at-spi2-atk
739 spi_display_name (void)
741 static const char *canonical_display_name = NULL;
742 if (!canonical_display_name)
744 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
747 display_env = g_getenv ("DISPLAY");
748 if (!display_env || !display_env[0])
749 canonical_display_name = ":0";
752 gchar *display_p, *screen_p;
753 canonical_display_name = g_strdup (display_env);
754 display_p = g_utf8_strrchr (canonical_display_name, -1, ':');
755 screen_p = g_utf8_strrchr (canonical_display_name, -1, '.');
756 if (screen_p && display_p && (screen_p > display_p))
764 canonical_display_name = display_env;
767 return canonical_display_name;
770 /* TODO: Avoid having duplicate functions for this here and in at-spi2-atk */
771 static DBusConnection *
772 get_accessibility_bus ()
776 Display *bridge_display;
778 unsigned char *data = NULL;
779 unsigned long nitems;
780 unsigned long leftover;
782 DBusConnection *bus = NULL;
785 bridge_display = XOpenDisplay (spi_display_name ());
788 g_warning ("AT_SPI: Could not get the display\n");
792 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
793 XGetWindowProperty (bridge_display,
794 XDefaultRootWindow (bridge_display),
796 (long) BUFSIZ, False,
797 (Atom) 31, &actual_type, &actual_format,
798 &nitems, &leftover, &data);
800 dbus_error_init (&error);
805 ("AT-SPI: Accessibility bus not found - Using session bus.\n");
806 bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
809 g_warning ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
815 bus = dbus_connection_open (data, &error);
818 g_warning ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
823 if (!dbus_bus_register (bus, &error))
825 g_warning ("AT-SPI: Couldn't register with bus: %s\n", error.message);
837 * Connects to the accessibility registry and initializes the SPI.
839 * Returns: 0 on success, otherwise an integer error code.
859 dbus_error_init (&error);
860 bus = get_accessibility_bus ();
863 g_error ("Couldn't get session bus");
866 dbus_bus_register (bus, &error);
867 dbus_connection_setup_with_g_main(bus, g_main_context_default());
868 dbus_connection_add_filter (bus, atspi_dbus_filter, NULL, NULL);
869 dbind_set_timeout (1000);
870 match = g_strdup_printf ("type='signal',interface='%s',member='AddAccessible'", atspi_interface_cache);
871 dbus_error_init (&error);
872 dbus_bus_add_match (bus, match, &error);
874 match = g_strdup_printf ("type='signal',interface='%s',member='RemoveAccessible'", atspi_interface_cache);
875 dbus_bus_add_match (bus, match, &error);
877 match = g_strdup_printf ("type='signal',interface='%s',member='ChildrenChanged'", atspi_interface_event_object);
878 dbus_bus_add_match (bus, match, &error);
880 match = g_strdup_printf ("type='signal',interface='%s',member='PropertyChange'", atspi_interface_event_object);
881 dbus_bus_add_match (bus, match, &error);
883 match = g_strdup_printf ("type='signal',interface='%s',member='StateChanged'", atspi_interface_event_object);
884 dbus_bus_add_match (bus, match, &error);
889 static GMainLoop *mainloop;
894 * Starts/enters the main event loop for the AT-SPI services.
896 * (NOTE: This method does not return control, it is exited via a call to
897 * atspi_event_quit () from within an event handler).
901 atspi_event_main (void)
903 mainloop = g_main_loop_new (NULL, FALSE);
904 g_main_loop_run (mainloop);
910 * Quits the last main event loop for the SPI services,
911 * see atspi_event_main
914 atspi_event_quit (void)
916 g_main_loop_quit (mainloop);
922 * Disconnects from the Accessibility Registry and releases
923 * any floating resources. Call only once at exit.
925 * Returns: 0 if there were no leaks, otherwise non zero.
937 atspi_inited = FALSE;
941 leaked = g_hash_table_size (live_refs);
954 _atspi_dbus_call (gpointer obj, const char *interface, const char *method, GError **error, const char *type, ...)
959 AtspiObject *aobj = ATSPI_OBJECT (obj);
961 va_start (args, type);
962 dbus_error_init (&err);
963 retval = dbind_method_call_reentrant_va (_atspi_bus(), aobj->app->bus_name, aobj->path, interface, method, &err, type, args);
965 _atspi_process_deferred_messages ((gpointer)TRUE);
966 if (dbus_error_is_set (&err))
968 /* TODO: Set gerror */
969 dbus_error_free (&err);
975 _atspi_dbus_call_partial (gpointer obj,
976 const char *interface,
979 const char *type, ...)
983 va_start (args, type);
984 return _atspi_dbus_call_partial_va (obj, interface, method, error, type, args);
988 _atspi_dbus_call_partial_va (gpointer obj,
989 const char *interface,
995 AtspiObject *aobj = ATSPI_OBJECT (obj);
998 DBusMessage *msg = NULL, *reply = NULL;
999 DBusMessageIter iter;
1002 dbus_error_init (&err);
1004 msg = dbus_message_new_method_call (aobj->app->bus_name, aobj->path, interface, method);
1009 dbus_message_iter_init_append (msg, &iter);
1010 dbind_any_marshal_va (&iter, &p, args);
1012 reply = dbind_send_and_allow_reentry (_atspi_bus(), msg, &err);
1015 _atspi_process_deferred_messages ((gpointer)TRUE);
1016 if (dbus_error_is_set (&err))
1018 /* TODO: Set gerror */
1019 dbus_error_free (&err);
1025 _atspi_dbus_get_property (gpointer obj, const char *interface, const char *name, GError **error, const char *type, void *data)
1027 DBusMessage *message, *reply;
1028 DBusMessageIter iter, iter_variant;
1030 dbus_bool_t retval = FALSE;
1031 AtspiObject *aobj = ATSPI_OBJECT (obj);
1036 message = dbus_message_new_method_call (aobj->app->bus_name,
1038 "org.freedesktop.DBus.Properties",
1042 // TODO: throw exception
1045 dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1046 dbus_error_init (&err);
1047 reply = dbind_send_and_allow_reentry (_atspi_bus(), message, &err);
1048 dbus_message_unref (message);
1049 _atspi_process_deferred_messages ((gpointer)TRUE);
1052 // TODO: throw exception
1055 dbus_message_iter_init (reply, &iter);
1056 if (dbus_message_iter_get_arg_type (&iter) != 'v')
1058 g_warning ("at-spi: expected a variant when fetching %s from interface %s\n", name, interface);
1061 dbus_message_iter_recurse (&iter, &iter_variant);
1062 if (dbus_message_iter_get_arg_type (&iter_variant) != type[0])
1064 g_warning ("atspi_dbus_get_property: Wrong type: expected %s, got %c\n", type, dbus_message_iter_get_arg_type (&iter_variant));
1067 if (!strcmp (type, "(so)"))
1069 *((AtspiAccessible **)data) = _atspi_dbus_return_accessible_from_iter (&iter_variant);
1073 dbus_message_iter_get_basic (&iter_variant, data);
1074 dbus_message_unref (reply);
1075 if (type [0] == 's')
1076 *(char **)data = g_strdup (*(char **)data);
1084 _atspi_dbus_send_with_reply_and_block (DBusMessage *message)
1089 dbus_error_init (&err);
1090 reply = dbind_send_and_allow_reentry (_atspi_bus(), message, &err);
1091 _atspi_process_deferred_messages ((gpointer)TRUE);
1092 dbus_message_unref (message);
1094 g_warning ("Atspi: Got error: %s\n", err.message);
1099 _atspi_dbus_hash_from_message (DBusMessage *message)
1101 DBusMessageIter iter;
1103 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL);
1105 dbus_message_iter_init (message, &iter);
1106 return _atspi_dbus_hash_from_iter (&iter);
1110 _atspi_dbus_hash_from_iter (DBusMessageIter *iter)
1112 GHashTable *hash = g_hash_table_new (g_str_hash, g_str_equal);
1113 DBusMessageIter iter_array, iter_dict;
1115 dbus_message_iter_recurse (iter, &iter_array);
1116 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1118 const char *name, *value;
1119 dbus_message_iter_recurse (&iter_array, &iter_dict);
1120 dbus_message_iter_get_basic (&iter_dict, &name);
1121 dbus_message_iter_next (&iter_dict);
1122 dbus_message_iter_get_basic (&iter_dict, &value);
1123 g_hash_table_insert (hash, g_strdup (name), g_strdup (value));
1124 dbus_message_iter_next (&iter_array);
1130 _atspi_dbus_attribute_array_from_message (DBusMessage *message)
1132 DBusMessageIter iter;
1134 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL);
1136 dbus_message_iter_init (message, &iter);
1138 return _atspi_dbus_attribute_array_from_iter (&iter);
1142 _atspi_dbus_attribute_array_from_iter (DBusMessageIter *iter)
1144 DBusMessageIter iter_array, iter_dict;
1145 GArray *array = g_array_new (TRUE, TRUE, sizeof (gchar *));
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;
1154 dbus_message_iter_recurse (&iter_array, &iter_dict);
1155 dbus_message_iter_get_basic (&iter_dict, &name);
1156 dbus_message_iter_next (&iter_dict);
1157 dbus_message_iter_get_basic (&iter_dict, &value);
1158 str = g_strdup_printf ("%s:%s", name, value);
1159 new_array = g_array_append_val (array, str);
1162 dbus_message_iter_next (&iter_array);;
1168 _atspi_dbus_set_interfaces (AtspiAccessible *accessible, DBusMessageIter *iter)
1170 DBusMessageIter iter_array;
1172 accessible->interfaces = 0;
1173 dbus_message_iter_recurse (iter, &iter_array);
1174 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1178 dbus_message_iter_get_basic (&iter_array, &iface);
1179 if (!strcmp (iface, "org.freedesktop.DBus.Introspectable")) continue;
1180 n = _atspi_get_iface_num (iface);
1183 g_warning ("at-spi: Unknown interface %s", iface);
1186 accessible->interfaces |= (1 << n);
1187 dbus_message_iter_next (&iter_array);
1189 accessible->cached_properties |= ATSPI_CACHE_INTERFACES;
1193 _atspi_dbus_set_state (AtspiAccessible *accessible, DBusMessageIter *iter)
1195 DBusMessageIter iter_array;
1197 dbus_uint32_t *states;
1199 dbus_message_iter_recurse (iter, &iter_array);
1200 dbus_message_iter_get_fixed_array (&iter_array, &states, &count);
1203 g_warning ("at-spi: expected 2 values in states array; got %d\n", count);
1204 if (!accessible->states)
1205 accessible->states = _atspi_state_set_new_internal (accessible, 0);
1209 guint64 val = ((guint64)states [1]) << 32;
1211 if (!accessible->states)
1212 accessible->states = _atspi_state_set_new_internal (accessible, val);
1214 accessible->states->states = val;
1216 accessible->cached_properties |= ATSPI_CACHE_STATES;