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 DBusConnection *bus = NULL;
36 static GHashTable *apps = NULL;
37 static GHashTable *live_refs = NULL;
38 static GQueue *exception_handlers = NULL;
39 static DBusError exception;
41 const char *atspi_path_dec = ATSPI_DBUS_PATH_DEC;
42 const char *atspi_path_registry = ATSPI_DBUS_PATH_REGISTRY;
43 const char *atspi_path_root = ATSPI_DBUS_PATH_ROOT;
44 const char *atspi_bus_registry = ATSPI_DBUS_NAME_REGISTRY;
45 const char *atspi_interface_accessible = ATSPI_DBUS_INTERFACE_ACCESSIBLE;
46 const char *atspi_interface_action = ATSPI_DBUS_INTERFACE_ACTION;
47 const char *atspi_interface_application = ATSPI_DBUS_INTERFACE_APPLICATION;
48 const char *atspi_interface_collection = ATSPI_DBUS_INTERFACE_COLLECTION;
49 const char *atspi_interface_component = ATSPI_DBUS_INTERFACE_COMPONENT;
50 const char *atspi_interface_dec = ATSPI_DBUS_INTERFACE_DEC;
51 const char *atspi_interface_device_event_listener = ATSPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER;
52 const char *atspi_interface_document = ATSPI_DBUS_INTERFACE_DOCUMENT;
53 const char *atspi_interface_editable_text = ATSPI_DBUS_INTERFACE_EDITABLE_TEXT;
54 const char *atspi_interface_event_object = ATSPI_DBUS_INTERFACE_EVENT_OBJECT;
55 const char *atspi_interface_hyperlink = ATSPI_DBUS_INTERFACE_HYPERLINK;
56 const char *atspi_interface_hypertext = ATSPI_DBUS_INTERFACE_HYPERTEXT;
57 const char *atspi_interface_image = ATSPI_DBUS_INTERFACE_IMAGE;
58 const char *atspi_interface_registry = ATSPI_DBUS_INTERFACE_REGISTRY;
59 const char *atspi_interface_selection = ATSPI_DBUS_INTERFACE_SELECTION;
60 const char *atspi_interface_table = ATSPI_DBUS_INTERFACE_TABLE;
61 const char *atspi_interface_text = ATSPI_DBUS_INTERFACE_TEXT;
62 const char *atspi_interface_cache = ATSPI_DBUS_INTERFACE_CACHE;
63 const char *atspi_interface_value = ATSPI_DBUS_INTERFACE_VALUE;
65 static const char *interfaces[] =
67 ATSPI_DBUS_INTERFACE_ACCESSIBLE,
68 ATSPI_DBUS_INTERFACE_ACTION,
69 ATSPI_DBUS_INTERFACE_APPLICATION,
70 ATSPI_DBUS_INTERFACE_COLLECTION,
71 ATSPI_DBUS_INTERFACE_COMPONENT,
72 ATSPI_DBUS_INTERFACE_DOCUMENT,
73 ATSPI_DBUS_INTERFACE_EDITABLE_TEXT,
74 ATSPI_DBUS_INTERFACE_HYPERLINK,
75 ATSPI_DBUS_INTERFACE_HYPERTEXT,
76 ATSPI_DBUS_INTERFACE_IMAGE,
77 "org.a11y.atspi.LoginHelper",
78 ATSPI_DBUS_INTERFACE_SELECTION,
79 ATSPI_DBUS_INTERFACE_TABLE,
80 ATSPI_DBUS_INTERFACE_TEXT,
81 ATSPI_DBUS_INTERFACE_VALUE,
86 _atspi_get_iface_num (const char *iface)
88 /* TODO: Use a binary search or hash to improve performance */
91 for (i = 0; interfaces[i]; i++)
93 if (!strcmp(iface, interfaces[i])) return i;
103 live_refs = g_hash_table_new (g_direct_hash, g_direct_equal);
108 /* TODO: Add an application parameter */
117 #define APP_IS_REGISTRY(app) (!strcmp (app->bus_name, atspi_bus_registry))
128 g_hash_table_destroy (refs);
132 static gboolean atspi_inited = FALSE;
134 static GHashTable *app_hash = NULL;
136 static AtspiApplication *
137 get_application (const char *bus_name)
139 AtspiApplication *app = NULL;
144 app_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_hash_table_unref);
145 if (!app_hash) return NULL;
147 app = g_hash_table_lookup (app_hash, bus_name);
149 bus_name_dup = g_strdup (bus_name);
150 if (!bus_name_dup) return NULL;
151 // TODO: change below to something that will send state-change:defunct notification if necessary */
152 app = _atspi_application_new (bus_name);
153 if (!app) return NULL;
154 app->bus_name = bus_name_dup;
155 app->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
156 g_hash_table_insert (app_hash, bus_name_dup, app);
160 static AtspiAccessible *
161 ref_accessible (const char *app_name, const char *path)
163 AtspiApplication *app = get_application (app_name);
166 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
169 if (!strcmp (path, "/org/a11y/atspi/accessible/root"))
173 app->root = atspi_accessible_new (app, atspi_path_root);
174 app->root->accessible_parent = atspi_get_desktop (0);
176 return g_object_ref (app->root);
179 a = g_hash_table_lookup (app->hash, path);
182 return g_object_ref (a);
184 a = atspi_accessible_new (app, path);
187 g_hash_table_insert (app->hash, a->parent.path, a);
188 g_object_ref (a); /* for the hash */
192 static AtspiHyperlink *
193 ref_hyperlink (const char *app_name, const char *path)
195 AtspiApplication *app = get_application (app_name);
196 AtspiHyperlink *hyperlink;
198 if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
201 hyperlink = g_hash_table_lookup (app->hash, path);
204 return g_object_ref (hyperlink);
206 hyperlink = atspi_hyperlink_new (app, path);
209 g_hash_table_insert (app->hash, hyperlink->parent.path, hyperlink);
210 /* TODO: This should be a weak ref */
211 g_object_ref (hyperlink); /* for the hash */
224 GArray *state_bitflags;
227 static DBusHandlerResult
228 handle_remove_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
230 const char *sender = dbus_message_get_sender (message);
231 AtspiApplication *app;
233 DBusMessageIter iter, iter_struct;
234 const char *signature = dbus_message_get_signature (message);
238 if (strcmp (signature, "(so)") != 0)
240 g_warning ("at-spi: Unknown signature %s for RemoveAccessible", signature);
241 return DBUS_HANDLER_RESULT_HANDLED;
244 dbus_message_iter_init (message, &iter);
245 dbus_message_iter_recurse (&iter, &iter_struct);
246 dbus_message_iter_get_basic (&iter_struct, &sender);
247 dbus_message_iter_get_basic (&iter_struct, &path);
248 app = get_application (sender);
249 a = ref_accessible (sender, path);
251 return DBUS_HANDLER_RESULT_HANDLED;
252 if (a->accessible_parent && g_list_find (a->accessible_parent->children, a))
254 a->accessible_parent->children = g_list_remove (a->accessible_parent->children, a);
257 g_hash_table_remove (app->hash, app->bus_name);
258 g_object_unref (a); /* unref our own ref */
259 return DBUS_HANDLER_RESULT_HANDLED;
263 add_app_to_desktop (AtspiAccessible *a, const char *bus_name)
268 dbus_error_init (&error);
269 AtspiAccessible *obj = ref_accessible (bus_name, atspi_path_root);
272 GList *new_list = g_list_append (a->children, obj);
275 a->children = new_list;
281 g_warning ("Error calling getRoot for %s: %s", bus_name, error.message);
287 send_children_changed (AtspiAccessible *parent, AtspiAccessible *child, gboolean add)
291 memset (&e, 0, sizeof (e));
292 e.type = (add? "object:children-changed:add": "object:children-changed:remove");
294 e.detail1 = g_list_index (parent->children, child);
296 _atspi_send_event (&e);
300 unref_object_and_descendants (AtspiAccessible *obj)
304 for (l = obj->children; l; l = l->next)
306 unref_object_and_descendants (l->data);
308 g_object_unref (obj);
312 remove_app_from_desktop (AtspiAccessible *a, const char *bus_name)
315 AtspiAccessible *child;
317 for (l = a->children; l; l = l->next)
320 if (!strcmp (bus_name, child->parent.app->bus_name)) break;
324 g_warning ("Removing unregistered app %s; doing nothing\n", bus_name);
327 send_children_changed (a, child, FALSE);
328 a->children = g_list_remove (a->children, child);
329 unref_object_and_descendants (child);
333 static AtspiAccessible *desktop;
336 get_reference_from_iter (DBusMessageIter *iter, const char **app_name, const char **path)
338 DBusMessageIter iter_struct;
340 dbus_message_iter_recurse (iter, &iter_struct);
341 dbus_message_iter_get_basic (&iter_struct, app_name);
342 dbus_message_iter_next (&iter_struct);
343 dbus_message_iter_get_basic (&iter_struct, path);
344 dbus_message_iter_next (iter);
348 add_accessible_from_iter (DBusMessageIter *iter)
352 DBusMessageIter iter_struct, iter_array;
353 const char *app_name, *path;
354 AtspiApplication *app;
355 AtspiAccessible *accessible;
356 const char *name, *description;
359 dbus_message_iter_recurse (iter, &iter_struct);
362 get_reference_from_iter (&iter_struct, &app_name, &path);
363 accessible = ref_accessible (app_name, path);
367 /* Get application: TODO */
368 dbus_message_iter_next (&iter_struct);
371 get_reference_from_iter (&iter_struct, &app_name, &path);
372 if (accessible->accessible_parent)
373 g_object_unref (accessible->accessible_parent);
374 accessible->accessible_parent = ref_accessible (app_name, path);
377 while (accessible->children)
379 g_object_unref (accessible->children->data);
380 accessible->children = g_list_remove (accessible->children, accessible->children->data);
382 dbus_message_iter_recurse (&iter_struct, &iter_array);
383 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
385 AtspiAccessible *child;
386 get_reference_from_iter (&iter_array, &app_name, &path);
387 child = ref_accessible (app_name, path);
388 new_list = g_list_append (accessible->children, child);
389 if (new_list) accessible->children = new_list;
393 dbus_message_iter_next (&iter_struct);
394 _atspi_dbus_set_interfaces (accessible, &iter_struct);
395 dbus_message_iter_next (&iter_struct);
398 if (accessible->name)
399 g_free (accessible->name);
400 dbus_message_iter_get_basic (&iter_struct, &name);
401 accessible->name = g_strdup (name);
402 dbus_message_iter_next (&iter_struct);
405 dbus_message_iter_get_basic (&iter_struct, &role);
406 accessible->role = role;
407 dbus_message_iter_next (&iter_struct);
410 if (accessible->description)
411 g_free (accessible->description);
412 dbus_message_iter_get_basic (&iter_struct, &description);
413 accessible->description = g_strdup (description);
414 dbus_message_iter_next (&iter_struct);
416 _atspi_dbus_set_state (accessible, &iter_struct);
417 dbus_message_iter_next (&iter_struct);
419 /* This is a bit of a hack since the cache holds a ref, so we don't need
420 * the one provided for us anymore */
421 g_object_unref (accessible);
425 add_accessibles (const char *app_name)
428 DBusMessage *message, *reply;
429 DBusMessageIter iter, iter_array;
431 AtspiApplication *app = get_application (app_name);
432 /* TODO: Move this functionality into app initializer? */
433 dbus_error_init (&error);
434 message = dbus_message_new_method_call (app_name, "/org/a11y/atspi/cache", atspi_interface_cache, "GetItems");
435 reply = _atspi_dbus_send_with_reply_and_block (message);
436 if (!reply || strcmp (dbus_message_get_signature (reply), "a((so)(so)(so)a(so)assusau)") != 0)
438 g_warning ("at-spi: Error in GetItems");
441 dbus_message_unref (reply);
443 dbus_message_iter_init (reply, &iter);
444 dbus_message_iter_recurse (&iter, &iter_array);
445 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
447 add_accessible_from_iter (&iter_array);
448 dbus_message_iter_next (&iter_array);
450 dbus_message_unref (reply);
453 /* TODO: Do we stil need this function? */
454 static AtspiAccessible *
455 ref_accessible_desktop (AtspiApplication *app)
461 DBusMessage *message, *reply;
462 DBusMessageIter iter, iter_array;
466 g_object_ref (desktop);
469 desktop = atspi_accessible_new (app, atspi_path_root);
474 g_hash_table_insert (app->hash, desktop->parent.path, desktop);
475 g_object_ref (desktop); /* for the hash */
476 desktop->name = g_strdup ("main");
477 dbus_error_init (&error);
478 message = dbus_message_new_method_call (atspi_bus_registry,
480 atspi_interface_accessible,
484 reply = _atspi_dbus_send_with_reply_and_block (message);
485 if (!reply || strcmp (dbus_message_get_signature (reply), "a(so)") != 0)
487 g_error ("Couldn't get application list: %s", error.message);
489 dbus_message_unref (reply);
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 const char *app_name, *path;
497 get_reference_from_iter (&iter_array, &app_name, &path);
498 add_accessibles (app_name);
499 add_app_to_desktop (desktop, app_name);
501 dbus_message_unref (reply);
506 _atspi_ref_accessible (const char *app, const char *path)
508 AtspiApplication *a = get_application (app);
510 if ( APP_IS_REGISTRY(a))
512 return ref_accessible_desktop (a);
514 return ref_accessible (app, path);
518 _atspi_dbus_return_accessible_from_message (DBusMessage *message)
520 DBusMessageIter iter;
521 AtspiAccessible *retval = NULL;
522 const char *signature = dbus_message_get_signature (message);
524 if (!strcmp (signature, "(so)"))
526 dbus_message_iter_init (message, &iter);
527 retval = _atspi_dbus_return_accessible_from_iter (&iter);
531 g_warning ("Atspi: Called _atspi_dbus_return_accessible_from_message with strange signature %s", signature);
533 dbus_message_unref (message);
538 _atspi_dbus_return_accessible_from_iter (DBusMessageIter *iter)
540 const char *app_name, *path;
542 get_reference_from_iter (iter, &app_name, &path);
543 return ref_accessible (app_name, path);
547 _atspi_dbus_return_hyperlink_from_message (DBusMessage *message)
549 DBusMessageIter iter;
550 AtspiHyperlink *retval = NULL;
551 const char *signature = dbus_message_get_signature (message);
553 if (!strcmp (signature, "(so)"))
555 dbus_message_iter_init (message, &iter);
556 retval = _atspi_dbus_return_hyperlink_from_iter (&iter);
560 g_warning ("Atspi: Called _atspi_dbus_return_hyperlink_from_message with strange signature %s", signature);
562 dbus_message_unref (message);
567 _atspi_dbus_return_hyperlink_from_iter (DBusMessageIter *iter)
569 const char *app_name, *path;
571 get_reference_from_iter (iter, &app_name, &path);
572 return ref_hyperlink (app_name, path);
575 const char *cache_signal_type = "((so)(so)(so)a(so)assusau)";
577 static DBusHandlerResult
578 handle_add_accessible (DBusConnection *bus, DBusMessage *message, void *user_data)
580 DBusMessageIter iter;
581 const char *sender = dbus_message_get_sender (message);
582 AtspiApplication *app = get_application (sender);
583 const char *type = cache_signal_type;
585 if (strcmp (dbus_message_get_signature (message), cache_signal_type) != 0)
587 g_warning ("atspi: AddAccessible with unknown signature %s\n", dbus_message_get_signature (message));
591 dbus_message_iter_init (message, &iter);
592 add_accessible_from_iter (&iter);
598 DBusMessage *message;
602 static guint process_deferred_messages_id = -1;
605 process_deferred_message (BusDataClosure *closure)
607 int type = dbus_message_get_type (closure->message);
608 const char *interface = dbus_message_get_interface (closure->message);
609 const char *member = dbus_message_get_member (closure->message);
613 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
614 !strncmp (interface, "org.a11y.atspi.Event.", 21))
616 atspi_dbus_handle_event (closure->bus, closure->message, closure->data);
618 if (dbus_message_is_method_call (closure->message, atspi_interface_device_event_listener, "NotifyEvent"))
620 atspi_dbus_handle_DeviceEvent (closure->bus,
621 closure->message, closure->data);
623 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "AddAccessible"))
625 handle_add_accessible (closure->bus, closure->message, closure->data);
627 if (dbus_message_is_signal (closure->message, atspi_interface_cache, "RemoveAccessible"))
629 handle_remove_accessible (closure->bus, closure->message, closure->data);
633 static GList *deferred_messages = NULL;
636 _atspi_process_deferred_messages (gpointer data)
638 static int in_process_deferred_messages = 0;
640 if (in_process_deferred_messages)
642 in_process_deferred_messages = 1;
643 while (deferred_messages != NULL)
645 BusDataClosure *closure = deferred_messages->data;
646 process_deferred_message (closure);
647 deferred_messages = g_list_remove (deferred_messages, closure);
648 dbus_message_unref (closure->message);
649 dbus_connection_unref (closure->bus);
652 /* If data is NULL, assume that we were called from GLib */
654 process_deferred_messages_id = -1;
655 in_process_deferred_messages = 0;
659 static DBusHandlerResult
660 defer_message (DBusConnection *connection, DBusMessage *message, void *user_data)
662 BusDataClosure *closure = g_new (BusDataClosure, 1);
666 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
667 closure->bus = dbus_connection_ref (bus);
668 closure->message = dbus_message_ref (message);
669 closure->data = user_data;
671 new_list = g_list_append (deferred_messages, closure);
673 deferred_messages = new_list;
675 if (process_deferred_messages_id == -1)
676 process_deferred_messages_id = g_idle_add (_atspi_process_deferred_messages, NULL);
677 return DBUS_HANDLER_RESULT_HANDLED;
680 static DBusHandlerResult
681 atspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
683 int type = dbus_message_get_type (message);
684 const char *interface = dbus_message_get_interface (message);
685 const char *member = dbus_message_get_member (message);
689 if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
690 !strncmp (interface, "org.a11y.atspi.Event.", 21))
692 return defer_message (bus, message, data);
694 if (dbus_message_is_method_call (message, atspi_interface_device_event_listener, "NotifyEvent"))
696 return defer_message (bus, message, data);
698 if (dbus_message_is_signal (message, atspi_interface_cache, "AddAccessible"))
700 return defer_message (bus, message, data);
702 if (dbus_message_is_signal (message, atspi_interface_cache, "RemoveAccessible"))
704 return defer_message (bus, message, data);
706 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
709 static const char *signal_interfaces[] =
711 "org.a11y.atspi.Event.Object",
712 "org.a11y.atspi.Event.Window",
713 "org.a11y.atspi.Event.Mouse",
714 "org.a11y.atspi.Event.Terminal",
715 "org.a11y.atspi.Event.Document",
716 "org.a11y.atspi.Event.Focus",
721 * Returns a 'canonicalized' value for DISPLAY,
722 * with the screen number stripped off if present.
724 * TODO: Avoid having duplicate functions for this here and in at-spi2-atk
727 spi_display_name (void)
729 static const char *canonical_display_name = NULL;
730 if (!canonical_display_name)
732 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
735 display_env = g_getenv ("DISPLAY");
736 if (!display_env || !display_env[0])
737 canonical_display_name = ":0";
740 gchar *display_p, *screen_p;
741 canonical_display_name = g_strdup (display_env);
742 display_p = g_utf8_strrchr (canonical_display_name, -1, ':');
743 screen_p = g_utf8_strrchr (canonical_display_name, -1, '.');
744 if (screen_p && display_p && (screen_p > display_p))
752 canonical_display_name = display_env;
755 return canonical_display_name;
758 /* TODO: Avoid having duplicate functions for this here and in at-spi2-atk */
759 static DBusConnection *
760 get_accessibility_bus ()
764 Display *bridge_display;
766 unsigned char *data = NULL;
767 unsigned long nitems;
768 unsigned long leftover;
770 DBusConnection *bus = NULL;
773 bridge_display = XOpenDisplay (spi_display_name ());
776 g_warning ("AT_SPI: Could not get the display\n");
780 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
781 XGetWindowProperty (bridge_display,
782 XDefaultRootWindow (bridge_display),
784 (long) BUFSIZ, False,
785 (Atom) 31, &actual_type, &actual_format,
786 &nitems, &leftover, &data);
788 dbus_error_init (&error);
793 ("AT-SPI: Accessibility bus not found - Using session bus.\n");
794 bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
797 g_warning ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
803 bus = dbus_connection_open (data, &error);
806 g_warning ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
811 if (!dbus_bus_register (bus, &error))
813 g_warning ("AT-SPI: Couldn't register with bus: %s\n", error.message);
825 * Connects to the accessibility registry and initializes the SPI.
827 * Returns: 0 on success, otherwise an integer error code.
847 dbus_error_init (&error);
848 bus = get_accessibility_bus ();
851 g_error ("Couldn't get session bus");
854 dbus_bus_register (bus, &error);
855 dbus_connection_setup_with_g_main(bus, g_main_context_default());
856 dbus_connection_add_filter (bus, atspi_dbus_filter, NULL, NULL);
857 dbind_set_timeout (1000);
858 match = g_strdup_printf ("type='signal',interface='%s',member='AddAccessible'", atspi_interface_cache);
859 dbus_error_init (&error);
860 dbus_bus_add_match (bus, match, &error);
862 match = g_strdup_printf ("type='signal',interface='%s',member='RemoveAccessible'", atspi_interface_cache);
863 dbus_bus_add_match (bus, match, &error);
865 match = g_strdup_printf ("type='signal',interface='%s',member='ChildrenChanged'", atspi_interface_event_object);
866 dbus_bus_add_match (bus, match, &error);
868 match = g_strdup_printf ("type='signal',interface='%s',member='PropertyChange'", atspi_interface_event_object);
869 dbus_bus_add_match (bus, match, &error);
871 match = g_strdup_printf ("type='signal',interface='%s',member='StateChanged'", atspi_interface_event_object);
872 dbus_bus_add_match (bus, match, &error);
877 static GMainLoop *mainloop;
882 * Starts/enters the main event loop for the AT-SPI services.
884 * (NOTE: This method does not return control, it is exited via a call to
885 * atspi_event_quit () from within an event handler).
889 atspi_event_main (void)
891 mainloop = g_main_loop_new (NULL, FALSE);
892 g_main_loop_run (mainloop);
898 * Quits the last main event loop for the SPI services,
899 * see atspi_event_main
902 atspi_event_quit (void)
904 g_main_loop_quit (mainloop);
910 * Disconnects from the Accessibility Registry and releases
911 * any floating resources. Call only once at exit.
913 * Returns: 0 if there were no leaks, otherwise non zero.
925 atspi_inited = FALSE;
929 leaked = g_hash_table_size (live_refs);
942 _atspi_dbus_call (gpointer obj, const char *interface, const char *method, GError **error, const char *type, ...)
947 AtspiObject *aobj = ATSPI_OBJECT (obj);
949 va_start (args, type);
950 dbus_error_init (&err);
951 retval = dbind_method_call_reentrant_va (_atspi_bus(), aobj->app->bus_name, aobj->path, interface, method, &err, type, args);
953 _atspi_process_deferred_messages ((gpointer)TRUE);
954 if (dbus_error_is_set (&err))
956 /* TODO: Set gerror */
957 dbus_error_free (&err);
963 _atspi_dbus_call_partial (gpointer obj,
964 const char *interface,
967 const char *type, ...)
971 va_start (args, type);
972 return _atspi_dbus_call_partial_va (obj, interface, method, error, type, args);
976 _atspi_dbus_call_partial_va (gpointer obj,
977 const char *interface,
983 AtspiObject *aobj = ATSPI_OBJECT (obj);
986 DBusMessage *msg = NULL, *reply = NULL;
987 DBusMessageIter iter;
990 dbus_error_init (&err);
992 msg = dbus_message_new_method_call (aobj->app->bus_name, aobj->path, interface, method);
997 dbus_message_iter_init_append (msg, &iter);
998 dbind_any_marshal_va (&iter, &p, args);
1000 reply = dbind_send_and_allow_reentry (_atspi_bus(), msg, &err);
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 message = dbus_message_new_method_call (aobj->app->bus_name,
1026 "org.freedesktop.DBus.Properties",
1030 // TODO: throw exception
1033 dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
1034 dbus_error_init (&err);
1035 reply = dbind_send_and_allow_reentry (_atspi_bus(), message, &err);
1036 dbus_message_unref (message);
1037 _atspi_process_deferred_messages ((gpointer)TRUE);
1040 // TODO: throw exception
1043 dbus_message_iter_init (reply, &iter);
1044 dbus_message_iter_recurse (&iter, &iter_variant);
1045 if (dbus_message_iter_get_arg_type (&iter_variant) != type[0])
1047 g_warning ("atspi_dbus_get_property: Wrong type: expected %s, got %c\n", type, dbus_message_iter_get_arg_type (&iter_variant));
1050 if (!strcmp (type, "(so)"))
1052 *((AtspiAccessible **)data) = _atspi_dbus_return_accessible_from_iter (&iter_variant);
1056 dbus_message_iter_get_basic (&iter_variant, data);
1057 dbus_message_unref (reply);
1058 if (type [0] == 's')
1059 *(char **)data = g_strdup (*(char **)data);
1067 _atspi_dbus_send_with_reply_and_block (DBusMessage *message)
1072 dbus_error_init (&err);
1073 reply = dbind_send_and_allow_reentry (_atspi_bus(), message, &err);
1074 _atspi_process_deferred_messages ((gpointer)TRUE);
1075 dbus_message_unref (message);
1077 g_warning ("Atspi: Got error: %s\n", err.message);
1082 _atspi_dbus_hash_from_message (DBusMessage *message)
1084 DBusMessageIter iter;
1086 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL);
1088 dbus_message_iter_init (message, &iter);
1089 return _atspi_dbus_hash_from_iter (&iter);
1093 _atspi_dbus_hash_from_iter (DBusMessageIter *iter)
1095 GHashTable *hash = g_hash_table_new (g_str_hash, g_str_equal);
1096 DBusMessageIter iter_array, iter_dict;
1098 dbus_message_iter_recurse (iter, &iter_array);
1099 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1101 const char *name, *value;
1102 dbus_message_iter_recurse (&iter_array, &iter_dict);
1103 dbus_message_iter_get_basic (&iter_dict, &name);
1104 dbus_message_iter_next (&iter_dict);
1105 dbus_message_iter_get_basic (&iter_dict, &value);
1106 g_hash_table_insert (hash, g_strdup (name), g_strdup (value));
1107 dbus_message_iter_next (&iter_array);
1113 _atspi_dbus_attribute_array_from_message (DBusMessage *message)
1115 DBusMessageIter iter;
1117 _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL);
1119 dbus_message_iter_init (message, &iter);
1121 return _atspi_dbus_attribute_array_from_iter (&iter);
1125 _atspi_dbus_attribute_array_from_iter (DBusMessageIter *iter)
1127 DBusMessageIter iter_array, iter_dict;
1128 GArray *array = g_array_new (TRUE, TRUE, sizeof (gchar *));
1131 dbus_message_iter_recurse (iter, &iter_array);
1132 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1134 const char *name, *value;
1137 dbus_message_iter_recurse (&iter_array, &iter_dict);
1138 dbus_message_iter_get_basic (&iter_dict, &name);
1139 dbus_message_iter_next (&iter_dict);
1140 dbus_message_iter_get_basic (&iter_dict, &value);
1141 str = g_strdup_printf ("%s:%s", name, value);
1142 new_array = g_array_append_val (array, str);
1145 dbus_message_iter_next (&iter_array);;
1151 _atspi_dbus_set_interfaces (AtspiAccessible *accessible, DBusMessageIter *iter)
1153 DBusMessageIter iter_array;
1155 accessible->interfaces = 0;
1156 dbus_message_iter_recurse (iter, &iter_array);
1157 while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
1161 dbus_message_iter_get_basic (&iter_array, &iface);
1162 if (!strcmp (iface, "org.freedesktop.DBus.Introspectable")) continue;
1163 n = _atspi_get_iface_num (iface);
1166 g_warning ("at-spi: Unknown interface %s", iface);
1169 accessible->interfaces |= (1 << n);
1170 dbus_message_iter_next (&iter_array);
1172 accessible->cached_properties |= ATSPI_CACHE_INTERFACES;
1176 _atspi_dbus_set_state (AtspiAccessible *accessible, DBusMessageIter *iter)
1178 DBusMessageIter iter_array;
1180 dbus_uint32_t *states;
1182 dbus_message_iter_recurse (iter, &iter_array);
1183 dbus_message_iter_get_fixed_array (&iter_array, &states, &count);
1186 g_warning ("at-spi: expected 2 values in states array; got %d\n", count);
1187 if (!accessible->states)
1188 accessible->states = _atspi_state_set_new_internal (accessible, 0);
1192 guint64 val = ((guint64)states [1]) << 32;
1194 if (!accessible->states)
1195 accessible->states = _atspi_state_set_new_internal (accessible, val);
1197 accessible->states->states = val;
1199 accessible->cached_properties |= ATSPI_CACHE_STATES;