Fix return value of spi_check_ev and have it call spi_throw_exception if an exception...
[platform/core/uifw/at-spi2-atk.git] / cspi / spi_main.c
index f39cfc7..d544bb7 100644 (file)
@@ -1,18 +1,25 @@
-#include <libbonobo.h>
-#include <stdio.h>
-#include "spi.h"
-
-static CORBA_Environment ev;
-static AccessibilityRegistry registry;
-
-static Accessible *
-Obj_Add (Accessible object)
-{
-  /* TODO: keep list of live object refs */
-  Accessible *oref = g_malloc (sizeof (Accessible));
-  *oref = object;
-  return oref;
-}
+/*
+ * AT-SPI - Assistive Technology Service Provider Interface
+ * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
+ *
+ * Copyright 2001, 2002 Sun Microsystems Inc.,
+ * Copyright 2001, 2002 Ximian, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
 
 /*
  *
@@ -20,974 +27,1055 @@ Obj_Add (Accessible object)
  *
  */
 
-/**
- * SPI_init:
- *
- * Connects to the accessibility registry and initializes the SPI.
- *
- * Returns: 0 on success, otherwise an integer error code.
- **/
-int
-SPI_init (void)
-{
-  int argc = 0;
-  CORBA_Object oclient;
-  char *obj_id;
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <cspi/spi-private.h>
+#include "spi.h"
 
-  CORBA_exception_init(&ev);
+#undef DEBUG_OBJECTS
 
-  if (!bonobo_init (&argc, NULL))
-    {
-      g_error ("Could not initialize Bonobo");
-    }
+static DBusConnection *bus = NULL;
+static GHashTable *apps = NULL;
+static GHashTable *live_refs = NULL;
+static GQueue *exception_handlers = NULL;
+static DBusError exception;
 
-  obj_id = "OAFIID:Accessibility_Registry:proto0.1";
+static void
+cspi_object_release (gpointer value)
+{
+}
 
-  oclient = bonobo_activation_activate_from_id (obj_id, 0, NULL, &ev);
-  if (ev._major != CORBA_NO_EXCEPTION) {
-    fprintf (stderr,
-            ("AT-SPI error: during registry activation: %s\n"),
-            CORBA_exception_id(&ev));
-    CORBA_exception_free(&ev);
-    exit(-1);
+gboolean
+cspi_exception_throw (DBusError *error, const char *desc_prefix)
+{
+  SPIExceptionHandler *handler = NULL;
+  SPIException ex;
+  if (exception_handlers) handler = g_queue_peek_head (exception_handlers);
+
+  ex.type = SPI_EXCEPTION_SOURCE_UNSPECIFIED;
+  ex.source = NULL;
+  ex.code = SPI_EXCEPTION_UNSPECIFIED;
+  ex.error = error;
+  // TODO: Fill in description
+  
+  if (handler)
+    return (*handler) (&ex, FALSE);
+  else
+    return FALSE; /* means exception was not handled */
+}
+
+const char *spi_path_dec = SPI_DBUS_PATH_DEC;
+const char *spi_path_registry = SPI_DBUS_PATH_REGISTRY;
+const char *spi_bus_registry = SPI_DBUS_NAME_REGISTRY;
+const char *spi_path_desktop = SPI_DBUS_PATH_DESKTOP;
+const char *spi_interface_accessible = SPI_DBUS_INTERFACE_ACCESSIBLE;
+const char *spi_interface_action = SPI_DBUS_INTERFACE_ACTION;
+const char *spi_interface_application = SPI_DBUS_INTERFACE_APPLICATION;
+const char *spi_interface_component = SPI_DBUS_INTERFACE_COMPONENT;
+const char *spi_interface_dec = SPI_DBUS_INTERFACE_DEC;
+const char *spi_interface_desktop = SPI_DBUS_INTERFACE_DESKTOP;
+const char *spi_interface_device_event_listener = SPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER;
+const char *spi_interface_document = SPI_DBUS_INTERFACE_DOCUMENT;
+const char *spi_interface_editable_text = SPI_DBUS_INTERFACE_EDITABLE_TEXT;
+const char *spi_interface_hyperlink = SPI_DBUS_INTERFACE_HYPERLINK;
+const char *spi_interface_hypertext = SPI_DBUS_INTERFACE_HYPERTEXT;
+const char *spi_interface_image = SPI_DBUS_INTERFACE_IMAGE;
+const char *spi_interface_registry = SPI_DBUS_INTERFACE_REGISTRY;
+const char *spi_interface_selection = SPI_DBUS_INTERFACE_SELECTION;
+const char *spi_interface_table = SPI_DBUS_INTERFACE_TABLE;
+const char *spi_interface_text = SPI_DBUS_INTERFACE_TEXT;
+const char *spi_interface_tree = SPI_DBUS_INTERFACE_TREE;
+const char *spi_interface_value = SPI_DBUS_INTERFACE_VALUE;
+
+static const char *interfaces[] =
+{
+  SPI_DBUS_INTERFACE_ACCESSIBLE,
+  SPI_DBUS_INTERFACE_ACTION,
+  SPI_DBUS_INTERFACE_APPLICATION,
+  SPI_DBUS_INTERFACE_COMPONENT,
+  SPI_DBUS_INTERFACE_DOCUMENT,
+  SPI_DBUS_INTERFACE_EDITABLE_TEXT,
+  SPI_DBUS_INTERFACE_HYPERLINK,
+  SPI_DBUS_INTERFACE_HYPERTEXT,
+  SPI_DBUS_INTERFACE_IMAGE,
+  "org.freedesktop.atspi.LoginHelper",
+  SPI_DBUS_INTERFACE_SELECTION,
+  "org.freedesktop.atspi.Selector",
+  SPI_DBUS_INTERFACE_TABLE,
+  SPI_DBUS_INTERFACE_TEXT,
+  SPI_DBUS_INTERFACE_VALUE,
+  NULL
+};
+
+static gint get_iface_num (const char *iface)
+{
+  // TODO: Use a binary search or hash to improve performance
+  int i;
+
+  for (i = 0; interfaces[i]; i++)
+  {
+    if (!strcmp(iface, interfaces[i])) return i;
   }
+  return -1;
+}
+
+SPIBoolean
+cspi_accessible_is_a (Accessible *accessible,
+                     const char *interface_name)
+{
+  int n;
 
-  if (CORBA_Object_is_nil (oclient, &ev))
+  if (accessible == NULL)
     {
-      g_error ("Could not locate registry");
-      exit(-1);
+      return FALSE;
     }
 
-  registry = (Accessibility_Registry) oclient;
-
-  bonobo_activate ();
-
-  return 0;
+  n = get_iface_num (interface_name);
+  if (n == -1) return FALSE;
+  return (SPIBoolean)((accessible->interfaces & (1 << n))? TRUE: FALSE);
 }
 
-/**
- * SPI_event_main:
- * @isGNOMEApp: a #boolean indicating whether the client of the SPI
- *              will use the Gnome event loop or not.
- *
- * Starts/enters the main event loop for the SPI services.
- *
- * (NOTE: This method does not return control, it is exited via a call to exit()
- * from within an event handler).
- *
- **/
-void
-SPI_event_main (boolean isGNOMEApp)
+static GHashTable *
+cspi_get_live_refs (void)
 {
-  if (isGNOMEApp) bonobo_main();
-  else CORBA_ORB_run (bonobo_orb(), &ev);
+  if (!live_refs) 
+    {
+      live_refs = g_hash_table_new (g_direct_hash, g_direct_equal);
+    }
+  return live_refs;
 }
 
-/**
- * SPI_event_is_ready:
- *
- * Checks to see if an SPI event is waiting in the event queue.
- * Used by clients that don't wish to use SPI_event_main().
- * Not Yet Implemented.
- *
- * Returns: #TRUE if an event is waiting, otherwise #FALSE.
- *
- **/
-boolean
-SPI_eventIsReady ()
+DBusConnection *
+cspi_bus (void)
 {
-  return FALSE;
+  return bus;
 }
 
-/**
- * SPI_nextEvent:
- *
- * Gets the next event in the SPI event queue; blocks if no event
- * is pending.
- * Used by clients that don't wish to use SPI_event_main().
- * Not Yet Implemented.
- *
- * Returns: the next #AccessibleEvent in the SPI event queue.
- *
- **/
-AccessibleEvent *
-SPI_nextEvent (boolean waitForEvent)
+SPIBoolean
+cspi_exception (void)
 {
-  return NULL;
+  if (dbus_error_is_set (&exception))
+  {
+    dbus_error_free (&exception);
+    return TRUE;
+  }
+  return FALSE;
 }
 
-/**
- * SPI_exit:
- *
- * Disconnects from the Accessibility Registry and releases resources.
- * Not Yet Implemented.
- *
- **/
-void
-SPI_exit (void)
+SPIBoolean
+cspi_check_ev (const char *error_string)
 {
-  exit(0);
+  if (dbus_error_is_set (&exception))
+  {
+    cspi_exception_throw (&exception, error_string);
+    return FALSE;
+  }
+  return TRUE;
 }
 
-/**
- * createEventListener:
- * @callback : an #AccessibleEventListenerCB callback function, or NULL.
- *
- * Create a new #AccessibleEventListener with a specified callback function.
- *
- * Returns: a pointer to a newly-created #AccessibleEventListener.
- *
- **/
-AccessibleEventListener *
-createEventListener (AccessibleEventListenerCB callback)
+Accessible *
+cspi_object_add (Accessible *accessible)
 {
-  AccessibleEventListener *listener = accessible_event_listener_new ();
-  if (callback)
-    {
-      accessible_event_listener_add_callback (listener, callback);
-    }
-  return listener;
+  if (accessible) cspi_object_ref (accessible);
+  return accessible;
 }
 
-/**
- * EventListener_addCallback:
- * @listener: the #AccessibleEventListener instance to modify.
- * @callback: an #AccessibleEventListenerCB function pointer.
- *
- * Add an in-process callback function to an existing AccessibleEventListener.
- *
- * Returns: #TRUE if successful, otherwise #FALSE.
- *
- **/
-boolean
-EventListener_addCallback (AccessibleEventListener *listener,
-                           AccessibleEventListenerCB callback)
+void
+cspi_object_ref (Accessible *accessible)
 {
-  accessible_event_listener_add_callback (listener, callback);
-  return TRUE;
-}
+  g_return_if_fail (accessible != NULL);
 
-/**
- * EventListener_removeCallback:
- * @listener: the #AccessibleEventListener instance to modify.
- * @callback: an #AccessibleEventListenerCB function pointer.
- *
- * Remove an in-process callback function from an existing AccessibleEventListener.
- *
- * Returns: #TRUE if successful, otherwise #FALSE.
- *
- **/
-boolean
-EventListener_removeCallback (AccessibleEventListener *listener,
-                           AccessibleEventListenerCB callback)
-{
-  accessible_event_listener_remove_callback (listener, callback);
-  return TRUE;
+  accessible->ref_count++;
+  g_hash_table_insert (live_refs, accessible, accessible);
 }
 
-/*
- *
- * Global functions serviced by the registry
- *
- */
+#define APP_IS_REGISTRY(app) (!strcmp (app->bus_name, spi_bus_registry))
 
-/**
- * registerGlobalEventListener:
- * @listener: the #AccessibleEventListener to be registered against an event type.
- * @callback: a character string indicating the type of events for which
- *            notification is requested.  Format is
- *            EventClass:major_type:minor_type:detail
- *            where all subfields other than EventClass are optional.
- *            EventClasses include "Focus", "Window", "Mouse",
- *            and toolkit events (e.g. "Gtk", "AWT").
- *            Examples: "focus:", "Gtk:GtkWidget:button_press_event".
- *
- * NOTE: this string may be UTF-8, but should not contain byte value 56 (ascii ':'),
- *            except as a delimiter, since non-UTF-8 string delimiting
- *            functions are used internally.  In general, listening to
- *            toolkit-specific events is not recommended.
- *
- * Add an in-process callback function to an existing AccessibleEventListener.
- *
- * Returns: #TRUE if successful, otherwise #FALSE.
- *
- **/
-boolean
-registerGlobalEventListener (AccessibleEventListener *listener,
-                             char *eventType)
-{
-  Accessibility_Registry_registerGlobalEventListener (
-                         registry,
-                         (Accessibility_EventListener)
-                            bonobo_object_corba_objref (bonobo_object (listener)),
-                         eventType,
-                         &ev);
-
-  if (ev._major != CORBA_NO_EXCEPTION)
+static void
+cspi_object_unref_internal (Accessible *accessible, gboolean defunct)
+{
+  gboolean cached;
+
+  if (accessible == NULL)
     {
-    return FALSE;
+      return;
     }
-  else
+
+  if (--accessible->ref_count == 0 || (accessible->ref_count == 1 && !defunct) && g_hash_table_lookup (live_refs, accessible))
+  {
+    AccessibleEvent e;
+    memset (&e, 0, sizeof(e));
+    e.type = "object:state-change:defunct";
+    e.source = accessible;
+    e.detail1 = 1;
+    cspi_dispatch_event (&e);
+    g_hash_table_remove (live_refs, accessible);
+  }
+  if (accessible->ref_count == 0)
+  {
+    if (APP_IS_REGISTRY (accessible->app))
     {
-      return TRUE;
+      g_free (accessible->v.path);
     }
+    g_free(accessible);
+  }
 }
 
-/**
- * getDesktopCount:
- *
- * Get the number of virtual desktops.
- * NOTE: currently multiple virtual desktops are not implemented, this
- *       function always returns '1'.
- *
- * Returns: an integer indicating the number of active virtual desktops.
- *
- **/
-int
-getDesktopCount ()
+void
+cspi_object_unref (Accessible *accessible)
 {
-  return Accessibility_Registry_getDesktopCount (registry, &ev);
+  cspi_object_unref_internal (accessible, FALSE);
 }
 
-/**
- * getDesktop:
- * @i: an integer indicating which of the accessible desktops is to be returned.
- *
- * Get the virtual desktop indicated by index @i.
- * NOTE: currently multiple virtual desktops are not implemented, this
- *       function always returns '1'.
- *
- * Returns: a pointer to the 'i-th' virtual desktop's #Accessible representation.
- *
- **/
-Accessible*
-getDesktop (int n)
+static void
+cspi_cleanup (void)
 {
-  return Obj_Add (Accessibility_Registry_getDesktop (registry, (CORBA_short) n, &ev));
+  GHashTable *refs;
+
+  cspi_streams_close_all ();
+
+  refs = live_refs;
+  live_refs = NULL;
+  if (refs)
+    {
+      g_hash_table_destroy (refs);
+    }
 }
 
-/**
- * getDesktopList:
- * @list: a pointer to an array of #Accessible objects.
- *
- * Get the list of virtual desktops.  On return, @list will point
- *     to a newly-created array of virtual desktop pointers.
- *     It is the responsibility of the caller to free this array when
- *     it is no longer needed.
- *
- * Not Yet Implemented.
- *
- * Returns: an integer indicating how many virtual desktops have been
- *          placed in the list pointed to by parameter @list.
- **/
-int
-getDesktopList (Accessible **list)
+static gboolean SPI_inited = FALSE;
+
+static GHashTable *app_hash = NULL;
+
+static Accessible *
+ref_accessible (CSpiApplication *app, const char *path)
 {
-  *list = NULL;
-  return 0;
+  int id;
+  guint *id_val;
+
+  if (sscanf (path, "/org/freedesktop/atspi/accessible/%d", &id) != 1)
+  {
+    return NULL;
+  }
+  Accessible *a = g_hash_table_lookup (app->hash, &id);
+  if (a)
+  {
+    cspi_object_ref (a);
+    return a;
+  }
+  id_val = g_new (guint, 1);
+  if (!id_val) return NULL;
+  *id_val = id;
+  a = g_new0 (Accessible, 1);
+  if (!a)
+  {
+    g_free (id_val);
+    return NULL;
+  }
+  g_hash_table_insert (app->hash, id_val, a);
+  a->app = app;
+  a->v.id = id;
+  a->ref_count = 2;    /* one for the caller, one for the hash */
+  return a;
 }
 
-/**
- * registerKeystrokeListener:
- * @listener: a pointer to the #KeystrokeListener for which
- *            keystroke events are requested.
- *
- * Not Yet Implemented.
- *
- **/
-void
-registerKeystrokeListener (KeystrokeListener *listener)
+static CSpiApplication *
+cspi_get_application (const char *bus_name)
 {
-  ;
+  CSpiApplication *app = NULL;
+  char *bus_name_dup;
+
+  if (!app_hash)
+  {
+    app_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_hash_table_unref);
+    if (!app_hash) return;
+  }
+  app = g_hash_table_lookup (app_hash, bus_name);
+  if (app) return app;
+  bus_name_dup = g_strdup (bus_name);
+  if (!bus_name_dup) return NULL;
+  // TODO: change below to something that will send state-change:defunct notification if necessary */
+  app = g_new (CSpiApplication, 1);
+  if (!app) return NULL;
+  app->bus_name = bus_name_dup;
+  if (APP_IS_REGISTRY (app))
+  {
+    app->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, cspi_object_unref);
+  }
+  else
+  {
+    app->hash = g_hash_table_new_full (g_int_hash, g_int_equal, g_free, cspi_object_unref);
+  }
+  g_hash_table_insert (app_hash, bus_name_dup, app);
+  return app;
 }
 
-/**
- * generateKeyEvent:
- * @keycode: a #long indicating the keycode of the key event
- *           being synthesized.
- * @meta: a #long indicating the key modifiers to be sent
- *        with the event, if any.
- *
- * Synthesize a keyboard event (as if a hardware keyboard event occurred in the
- * current UI context).
- * Not Yet Implemented.
- *
- **/
-void
-generateKeyEvent (long keyCode, long meta)
+typedef struct
+{
+  char *path;
+  char *parent;
+  GArray *children;
+  GArray *interfaces;
+  char *name;
+  dbus_uint32_t role;
+  char *description;
+} CACHE_ADDITION;
+
+/* Update the cache with added/modified objects and free the array */
+static void
+handle_additions (CSpiApplication*app, GArray *additions)
 {
-  ;
+  gint i, j;
+  GList *l, *new_list;
+
+  if (!additions)
+  {
+    return;
+  }
+  for (i = 0; i < additions->len; i++)
+  {
+    CACHE_ADDITION *ca = &g_array_index (additions, CACHE_ADDITION, i);
+    Accessible *a = ref_accessible (app, ca->path);
+      /* Note: children don't hold refs for their parents or vice versa */
+    a->parent = ref_accessible (app, ca->parent);
+    if (a->parent) cspi_object_unref (a->parent);
+    if (a->children)
+    {
+      g_list_free (a->children);
+      a->children = NULL;
+    }
+    for (j = 0; j < ca->children->len; j++)
+    {
+      const char *child_path = g_array_index (ca->children, const char *, j);
+      Accessible *child = ref_accessible (app, child_path);
+      new_list = g_list_append (a->children, child);
+      if (new_list) a->children = new_list;
+      cspi_object_unref (child);
+    }
+    a->interfaces = 0;
+    for (j = 0; j < ca->interfaces->len; j++)
+    {
+      const char *iface = g_array_index (ca->interfaces, const char *, j);
+      if (!strcmp (iface, "org.freedesktop.DBus.Introspectable")) continue;
+      gint n = get_iface_num (iface);
+      if (n == -1)
+      {
+       g_warning ("Unknown interface %s", iface);
+      }
+      else a->interfaces |= (1 << n);
+      g_free (iface);
+    }
+    if (a->name) g_free (a->name);
+    a->name = ca->name;
+    a->role = ca->role;
+    if (a->description) g_free (a->description);
+    a->description = ca->description;
+    g_array_free (ca->interfaces, TRUE);
+    g_array_free (ca->children, TRUE);
+    /* This is a bit of a hack since ref_accessible sets ref_count to 2
+     * for a new object, one of the refs being for the cache */
+    cspi_object_unref (a);
+  }
+  g_array_free (additions, TRUE);
 }
 
-/**
- * generateMouseEvent:
- * @x: a #long indicating the screen x coordinate of the mouse event.
- * @y: a #long indicating the screen y coordinate of the mouse event.
- * @name: a string indicating which mouse event to be synthesized
- *        (e.g. "button1", "button2", "mousemove").
- *
- * Synthesize a mouse event at a specific screen coordinate.
- * Not Yet Implemented.
- *
- **/
-void
-generateMouseEvent (long x, long y, char *name)
+static void
+handle_removals (CSpiApplication *app, GArray *removals)
 {
-  ;
+  gint j;
+
+  if (!removals) return;
+  for (j = 0; j < removals->len; j++)
+  {
+    const char *path = g_array_index (removals, const char *, j);
+    Accessible *a = ref_accessible (app, path);
+    if (a->parent && g_list_find (a->parent->children, a))
+    {
+      a->parent->children = g_list_remove (a->parent->children, a);
+      /* Note: children don't hold refs for their parents or vice versa */
+    }
+    g_hash_table_remove (app->hash, &a->v.id);
+    cspi_object_unref_internal (a, TRUE);      /* unref our own ref */
+  }
+  g_array_free (removals, TRUE);
 }
 
-/*
- *
- * Accessible function prototypes
- *
- */
-
-/**
- * Accessible_ref:
- * @obj: a pointer to the #Accessible object on which to operate.
- *
- * Increment the reference count for an #Accessible object.
- *
- * Returns: (no return code implemented yet).
- *
- **/
-int
-Accessible_ref (Accessible *obj)
+static gboolean
+add_app_to_desktop (Accessible *a, const char *bus_name)
 {
-  Accessibility_Accessible_ref (*obj, &ev);
-  return 0;
+  DBusError error;
+  char *root_path;
+
+  dbus_error_init (&error);
+  if (dbind_connection_method_call (bus, bus_name, "/org/freedesktop/atspi/tree", spi_interface_tree, "getRoot", &error, "=>o", &root_path))
+  {
+    Accessible *obj = cspi_ref_accessible (bus_name, root_path);
+    if (obj)
+    {
+      GList *new_list = g_list_append (a->children, obj);
+      if (new_list)
+      {
+       a->children = new_list;
+       return TRUE;
+      }
+    }
+    g_free (root_path);
+  }
+  else
+  {
+    g_warning ("Error calling getRoot for %s: %s", bus_name, error.message);
+  }
+  return FALSE;
 }
 
-
-/**
- * Accessible_unref:
- * @obj: a pointer to the #Accessible object on which to operate.
- *
- * Decrement the reference count for an #Accessible object.
- *
- * Returns: (no return code implemented yet).
- *
- **/
-int
-Accessible_unref (Accessible *obj)
+static void
+send_children_changed (Accessible *parent, Accessible *child, gboolean add)
 {
-  Accessibility_Accessible_unref (*obj, &ev);
-  return 0;
+  AccessibleEvent e;
+
+  memset (&e, 0, sizeof(e));
+  e.type = (add? "object:children-changed:add": "object:children-changed:remove");
+  e.source = parent;
+  e.detail1 = g_list_index (parent->children, child);
+  cspi_dispatch_event (&e);
 }
 
-/**
- * Accessible_getName:
- * @obj: a pointer to the #Accessible object on which to operate.
- *
- * Get the name of an #Accessible object.
- *
- * Returns: a UTF-8 string indicating the name of the #Accessible object.
- *
- **/
-char *
-Accessible_getName (Accessible *obj)
+static void
+unref_object_and_children (Accessible *obj)
 {
-  return Accessibility_Accessible__get_name (*obj, &ev);
+  GList *l;
+
+  for (l = obj->children; l; l = l->next)
+  {
+    unref_object_and_children (l->data);
+  }
+  cspi_object_unref_internal (obj, TRUE);
 }
 
-/**
- * Accessible_getDescription:
- * @obj: a pointer to the #Accessible object on which to operate.
- *
- * Get the description of an #Accessible object.
- *
- * Returns: a UTF-8 string describing the #Accessible object.
- *
- **/
-char *
-Accessible_getDescription (Accessible *obj)
+static gboolean
+remove_app_from_desktop (Accessible *a, const char *bus_name)
 {
-  return Accessibility_Accessible__get_description (*obj, &ev);
+  GList *l;
+  Accessible *child;
+
+  for (l = a->children; l; l = l->next)
+  {
+    child = l->data;
+    if (!strcmp (bus_name, child->app->bus_name)) break;
+  }
+  if (!l)
+  {
+    g_warning ("Removing unregistered app %s; doing nothing\n", bus_name);
+    return FALSE;
+  }
+  send_children_changed (a, child, FALSE);
+  a->children = g_list_remove (a->children, child);
+  unref_object_and_children (child);
+  return TRUE;
 }
 
-/**
- * Accessible_getParent:
- * @obj: a pointer to the #Accessible object to query.
- *
- * Get an #Accessible object's parent container.
- *
- * Returns: a pointer to the #Accessible object which contains the given
- *          #Accessible instance, or NULL if the @obj has no parent container.
- *
- **/
-Accessible *
-Accessible_getParent (Accessible *obj)
+static Accessible *
+ref_accessible_desktop (CSpiApplication *app, const char *path)
 {
-  return Obj_Add (Accessibility_Accessible__get_parent (*obj, &ev));
+  char *path_dup;
+  DBusError error;
+  GArray *apps = NULL;
+  GArray *additions;
+  gint i;
+
+  Accessible *a = g_hash_table_lookup (app->hash, path);
+  if (a)
+  {
+    cspi_object_ref (a);
+    return a;
+  }
+  path_dup = g_strdup (path);
+  if (!path_dup) return NULL;
+  a = g_new0 (Accessible, 1);
+  if (!a)
+  {
+    g_free (path_dup);
+    return NULL;
+  }
+  g_hash_table_insert (app->hash, path_dup, a);
+  a->app = app;
+  a->v.path = path_dup;
+  a->ref_count = 2;    /* one for the caller, one for the hash */
+  cspi_dbus_get_property (a, SPI_DBUS_INTERFACE_ACCESSIBLE, "name", NULL, "s", &a->name);
+  dbus_error_init (&error);
+  if (!dbind_connection_method_call (bus, spi_bus_registry, a->v.path, spi_interface_desktop, "getChildren", &error, "=>as", &apps))
+  {
+    g_error ("Couldn't get desktop children: %s", error.message);
+  }
+  for (i = 0; i < apps->len; i++)
+  {
+    const char *app_name = g_array_index (apps, char *, i);
+    if (app_name[0] == '\0')
+    {
+      g_warning ("Got empty app name");
+      continue;
+    }
+    CSpiApplication *app = cspi_get_application (app_name);
+    additions = NULL;
+    dbus_error_init (&error);
+    dbind_connection_method_call (bus, app_name, "/org/freedesktop/atspi/tree", spi_interface_tree, "getTree", &error, "=>a(ooaoassus)", &additions);
+    if (error.message)
+    {
+      g_warning ("getTree (%s): %s", app_name, error.message);
+    }
+    handle_additions (app, additions);
+    add_app_to_desktop (a, app_name);
+  }
+  g_array_free (apps, TRUE);
+  return a;
 }
 
-/**
- * Accessible_getChildCount:
- *
- * @obj: a pointer to the #Accessible object on which to operate.
- *
- * Get the number of children contained by an #Accessible object.
- *
- * Returns: a #long indicating the number of #Accessible children
- *          contained by an #Accessible object.
- *
- **/
-long
-Accessible_getChildCount (Accessible *obj)
+Accessible *
+cspi_ref_accessible (const char *app, const char *path)
 {
-  return Accessibility_Accessible__get_childCount (*obj, &ev);
+  CSpiApplication *a = cspi_get_application (app);
+  if (!a) return NULL;
+  if ( APP_IS_REGISTRY(a))
+  {
+    return ref_accessible_desktop (a, path);
+  }
+  return ref_accessible (a, path);
 }
 
-/**
- * Accessible_getChildAtIndex:
- *
- * @obj: a pointer to the #Accessible object on which to operate.
- * @childIndex: a #long indicating which child is specified.
- *
- * Get the #Accessible child of an #Accessible object at a given index.
- *
- * Returns: a pointer to the #Accessible child object at index
- *          @childIndex.
- *
- **/
 Accessible *
-Accessible_getChildAtIndex (Accessible *obj,
-                            long childIndex)
+cspi_ref_related_accessible (Accessible *obj, const char *path)
 {
-  return Obj_Add (Accessibility_Accessible_getChildAtIndex (*obj, childIndex, &ev));
+  return ref_accessible (obj->app, path);
 }
 
-/**
- * Accessible_getIndexInParent:
- *
- * @obj: a pointer to the #Accessible object on which to operate.
- *
- * Get the index of an #Accessible object in its containing #Accessible.
- *
- * Returns: a #long indicating the index of the #Accessible object
- *          in its parent (i.e. containing) #Accessible instance,
- *          or -1 if @obj has no containing parent.
- *
- **/
-long
-Accessible_getIndexInParent (Accessible *obj)
+typedef struct
 {
-  return Accessibility_Accessible_getIndexInParent (*obj, &ev);
-}
+  GArray *additions;
+  GArray *removals;
+} CacheSignalData;
 
-/**
- * Accessible_getRelationSet:
- *
- * Not Yet Implemented.
- *
- * Returns: a pointer to an array of #AccessibleRelations.
- *
- **/
-AccessibleRelation **
-Accessible_getRelationSet (Accessible *obj)
+static const char *cacheSignalType = "a(ooaoassus)ao";
+
+static DBusHandlerResult
+cspi_dbus_handle_update_tree (DBusConnection *bus, DBusMessage *message, void *user_data)
 {
-  return NULL;
+  DBusMessageIter iter;
+  CacheSignalData cd;
+  void *p = &cd;
+  const char *sender = dbus_message_get_sender (message);
+  CSpiApplication *app = cspi_get_application (sender);
+  const char *type = cacheSignalType;
+
+  if (!app)
+  {
+    g_warning ("UpdateTree from unknown app.  Should we add it?", sender);
+    return;
+  }
+  dbus_message_iter_init (message, &iter);
+  // TODO: Check signature
+  dbind_any_demarshal (&iter, &type, &p);      /* additions */
+  dbind_any_demarshal (&iter, &type, &p);      /* removals */
+  handle_additions (app, cd.additions);
+  handle_removals (app, cd.removals);
 }
 
-/**
- * Accessible_getRole:
- * @obj: a pointer to the #Accessible object on which to operate.
- *
- * Get the UI role of an #Accessible object.
- *
- * Returns: a UTF-8 string indicating the UI role of the #Accessible object.
- *
- **/
-char *
-Accessible_getRole (Accessible *obj)
+static DBusHandlerResult
+cspi_dbus_handle_add_application (DBusConnection *bus, DBusMessage *message, void *user_data)
 {
-  return "";
+  DBusError error;
+  dbus_uint32_t v;
+  Accessible *a;
+  char *bus_name;
+
+  dbus_error_init (&error);
+  if (!dbus_message_get_args (message, NULL, DBUS_TYPE_UINT32, &v, DBUS_TYPE_STRING, &bus_name, DBUS_TYPE_INVALID))
+  {
+    g_warning ("Error processing %s: %s\n", dbus_message_get_member(message), error.message);
+    dbus_error_free (&error);
+    return DBUS_HANDLER_RESULT_HANDLED;
+  }
+  a = cspi_ref_accessible (spi_bus_registry, dbus_message_get_path(message));
+  if (add_app_to_desktop (a, bus_name))
+  {
+    send_children_changed (a, g_list_last (a->children)->data, TRUE);
+  }
+  cspi_object_unref (a);
+  return DBUS_HANDLER_RESULT_HANDLED;
 }
 
-/**
- * Accessible_getStateSet:
- *
- * Not Yet Implemented.
- *
- * Returns: a pointer to an #AccessibleStateSet representing the object's current state.
- **/
-AccessibleStateSet *
-Accessible_getStateSet (Accessible *obj)
+static DBusHandlerResult
+cspi_dbus_handle_remove_application (DBusConnection *bus, DBusMessage *message, void *user_data)
 {
-  return NULL;
+  DBusError error;
+  dbus_uint32_t v;
+  Accessible *a;
+  char *bus_name;
+
+  dbus_error_init (&error);
+  if (!dbus_message_get_args (message, NULL, DBUS_TYPE_UINT32, &v, DBUS_TYPE_STRING, &bus_name, DBUS_TYPE_INVALID))
+  {
+    g_warning ("Error processing %s: %s\n", dbus_message_get_member(message), error.message);
+    dbus_error_free (&error);
+    return DBUS_HANDLER_RESULT_HANDLED;
+  }
+  a = cspi_ref_accessible (spi_bus_registry, dbus_message_get_path(message));
+  remove_app_from_desktop (a, bus_name);
+  cspi_object_unref (a);
+  return DBUS_HANDLER_RESULT_HANDLED;
 }
 
-/* Interface query methods */
 
-/**
- * Accessible_isAction:
- * @obj: a pointer to the #Accessible instance to query.
- *
- * Query whether the specified #Accessible implements #AccessibleAction.
- * Not Yet Implemented.
- *
- * Returns: #TRUE if @obj implements the #AccessibleAction interface,
- *          #FALSE otherwise.
- **/
-boolean
-Accessible_isAction (Accessible *obj)
+static DBusHandlerResult
+cspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
 {
-  return FALSE;
+  int type = dbus_message_get_type (message);
+  const char *interface = dbus_message_get_interface (message);
+  const char *member = dbus_message_get_member (message); 
+  dbus_uint32_t v;
+  char *bus_name;
+
+  if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
+      !strcmp (interface, SPI_DBUS_INTERFACE_ACCESSIBLE))
+  {
+    return cspi_dbus_handle_event (bus, message, data);
+  }
+  if (dbus_message_is_method_call (message, spi_interface_device_event_listener, "notifyEvent"))
+  {
+    return cspi_dbus_handle_deviceEvent (bus, message, data);
+  }
+  if (dbus_message_is_signal (message, spi_interface_tree, "updateTree"))
+  {
+    return cspi_dbus_handle_update_tree (bus, message, data);
+  }
+  if (dbus_message_is_signal (message, spi_interface_registry, "applicationAdd"))
+  {
+    return cspi_dbus_handle_add_application (bus, message, data);
+  }
+  if (dbus_message_is_signal (message, spi_interface_registry, "applicationRemove"))
+  {
+    return cspi_dbus_handle_remove_application (bus, message, data);
+  }
+if (type == DBUS_MESSAGE_TYPE_SIGNAL) printf("got %s of %s from %s\n", dbus_message_get_member (message), dbus_message_get_interface(message), dbus_message_get_sender(message));
+  return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 }
 
 /**
- * Accessible_isComponent:
- * @obj: a pointer to the #Accessible instance to query.
+ * SPI_init:
  *
- * Query whether the specified #Accessible implements #AccessibleComponent.
+ * Connects to the accessibility registry and initializes the SPI.
  *
- * Returns: #TRUE if @obj implements the #AccessibleComponent interface,
- *          #FALSE otherwise.
+ * Returns: 0 on success, otherwise an integer error code.  
  **/
-boolean
-Accessible_isComponent (Accessible *obj)
+int
+SPI_init (void)
 {
-  Bonobo_Unknown iface =
-    Accessibility_Accessible_queryInterface (*obj,
-                                             "IDL:Accessibility/Component:1.0",
-                                             &ev);
-  return (iface != NULL) ? TRUE : FALSE;
+  DBusError error;
+  char *match;
+
+  if (SPI_inited)
+    {
+      return 1;
+    }
+
+  SPI_inited = TRUE;
+
+  g_type_init ();
+
+  cspi_get_live_refs();
+  g_atexit (cspi_cleanup);
+
+  dbus_error_init (&error);
+  bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
+  if (!bus)
+  {
+    g_error ("Couldn't get session bus");
+    return 2;
+  }
+  dbus_bus_register (bus, &error);
+  dbus_connection_setup_with_g_main(bus, g_main_context_default());
+  dbus_connection_add_filter (bus, cspi_dbus_filter, NULL, NULL);
+  match = g_strdup_printf ("type='signal',interface='%s',member='updateTree'", spi_interface_tree);
+  dbus_error_init (&error);
+  dbus_bus_add_match (bus, match, &error);
+  g_free (match);
+  match = g_strdup_printf ("type='signal',sender='%s'", spi_bus_registry);
+  dbus_bus_add_match (bus, match, &error);
+  g_free (match);
+  return 0;
 }
 
+  static GMainLoop *mainloop;
+
 /**
- * Accessible_isEditableText:
- * @obj: a pointer to the #Accessible instance to query.
+ * SPI_event_main:
  *
- * Query whether the specified #Accessible implements #AccessibleEditableText.
- * Not Yet Implemented.
+ * Starts/enters the main event loop for the SPI services.
+ *
+ * (NOTE: This method does not return control, it is exited via a call to
+ *  SPI_event_quit () from within an event handler).
  *
- * Returns: #TRUE if @obj implements the #AccessibleEditableText interface,
- *          #FALSE otherwise.
  **/
-boolean
-Accessible_isEditableText (Accessible *obj)
+void
+SPI_event_main (void)
 {
-  return FALSE;
+
+  mainloop = g_main_loop_new (NULL, FALSE);
+  g_main_loop_run (mainloop);
 }
 
 /**
- * Accessible_isHypertext:
- * @obj: a pointer to the #Accessible instance to query.
- *
- * Query whether the specified #Accessible implements #AccessibleHypertext.
- * Not Yet Implemented.
+ * SPI_event_quit:
  *
- * Returns: #TRUE if @obj implements the #AccessibleHypertext interface,
- *          #FALSE otherwise.
+ * Quits the last main event loop for the SPI services,
+ * see SPI_event_main
  **/
-boolean
-Accessible_isHypertext (Accessible *obj)
+void
+SPI_event_quit (void)
 {
-  return FALSE;
+  g_main_loop_quit (mainloop);
 }
 
 /**
- * Accessible_isImage:
- * @obj: a pointer to the #Accessible instance to query.
+ * SPI_eventIsReady:
  *
- * Query whether the specified #Accessible implements #AccessibleImage.
- * Not Yet Implemented.
- *
- * Returns: #TRUE if @obj implements the #AccessibleImage interface,
- *          #FALSE otherwise.
-**/
-boolean
-Accessible_isImage (Accessible *obj)
-{
-  return FALSE;
-}
-
-/**
-  * Accessible_isSelection:
- * @obj: a pointer to the #Accessible instance to query.
+ * Checks to see if an SPI event is waiting in the event queue.
+ * Used by clients that don't wish to use SPI_event_main().
  *
- * Query whether the specified #Accessible implements #AccessibleSelection.
  * Not Yet Implemented.
  *
- * Returns: #TRUE if @obj implements the #AccessibleSelection interface,
- *          #FALSE otherwise.
-**/
-boolean
-Accessible_isSelection (Accessible *obj)
-{
-  return FALSE;
-}
-
-/**
- * Accessible_isTable:
- * @obj: a pointer to the #Accessible instance to query.
- *
- * Query whether the specified #Accessible implements #AccessibleTable.
- * Not Yet Implemented.
+ * Returns: #TRUE if an event is waiting, otherwise #FALSE.
  *
- * Returns: #TRUE if @obj implements the #AccessibleTable interface,
- *          #FALSE otherwise.
-**/
-boolean
-Accessible_isTable (Accessible *obj)
+ **/
+SPIBoolean
+SPI_eventIsReady (void)
 {
   return FALSE;
 }
 
 /**
- * Accessible_isText:
- * @obj: a pointer to the #Accessible instance to query.
- *
- * Query whether the specified #Accessible implements #AccessibleText.
- * Not Yet Implemented.
+ * SPI_nextEvent:
+ * @waitForEvent: a #SPIBoolean indicating whether to block or not.
  *
- * Returns: #TRUE if @obj implements the #AccessibleText interface,
- *          #FALSE otherwise.
-**/
-boolean
-Accessible_isText (Accessible *obj)
-{
-  return FALSE;
-}
-
-/**
- * Accessible_getAction:
+ * Gets the next event in the SPI event queue; blocks if no event
+ * is pending and @waitForEvent is #TRUE.
+ * Used by clients that don't wish to use SPI_event_main().
  *
  * Not Yet Implemented.
  *
+ * Returns: the next #AccessibleEvent in the SPI event queue.
  **/
-AccessibleAction *
-Accessible_getAction (Accessible *obj)
+AccessibleEvent *
+SPI_nextEvent (SPIBoolean waitForEvent)
 {
   return NULL;
 }
 
-/**
- * Accessible_getComponent:
- * @obj: a pointer to the #Accessible instance to query.
- *
- * Get the #AccessibleComponent interface for an #Accessible.
- *
- * Returns: a pointer to an #AccessibleComponent interface instance, or
- *          NULL if @obj does not implement #AccessibleComponent.
- **/
-AccessibleComponent *
-Accessible_getComponent (Accessible *obj)
+#ifdef PRINT_LEAKS
+static void
+report_leaked_ref (gpointer key, gpointer val, gpointer user_data)
 {
-  AccessibleComponent iface =
-    Accessibility_Accessible_queryInterface (*obj,
-                                             "IDL:Accessibility/Component:1.0",
-                                             &ev);
-  return Obj_Add (iface);
-}
+  char *name, *role;
+  Accessible *a = (Accessible *) val;
+  
+  name = Accessible_getName (a);
+  if (cspi_exception ())
+    {
+      name = NULL;
+    }
 
-/**
- * Accessible_queryInterface:
- * @obj: a pointer to the #Accessible instance to query.
- * @interface_name: a UTF-8 character string specifiying the requested interface.
- *
- * Query an #Accessible object to for a named interface.
- *
- * Returns: an instance of the named interface object, if it is implemented
- *          by @obj, or NULL otherwise.
- *
- **/
-GenericInterface *
-Accessible_queryInterface (Accessible *obj, char *interface_name)
-{
-  GenericInterface iface;
-  iface = Accessibility_Accessible_queryInterface (*obj,
-                                                    interface_name,
-                                                    &ev);
-  return (iface != NULL) ? Obj_Add (iface) : NULL;
-}
+  role = Accessible_getRoleName (a);
+  if (cspi_exception ())
+    {
+      role = NULL;
+    }
 
-/*
- *
- * AccessibleApplication function prototypes
- *
- */
+  fprintf (stderr, "leaked %d references to object %s, role %s %p\n",
+          a->ref_count, name ? name : "<?>", role ? role : "<?>", a);
 
-/**
- * AccessibleApplication_ref:
- * @obj: a pointer to the #AccessibleApplication on which to operate.
- *
- * Increment the reference count for an #AccessibleApplication.
- *
- * Returns: (no return code implemented yet).
- *
- **/
-int
-AccessibleApplication_ref (AccessibleApplication *obj)
-{
-  Accessibility_Application_ref (*obj, &ev);
-  return 0;
+  SPI_freeString (name);
 }
+#endif
 
 /**
- * AccessibleApplication_unref:
- * @obj: a pointer to the #AccessibleApplication object on which to operate.
- *
- * Decrement the reference count for an #AccessibleApplication.
+ * SPI_exit:
  *
- * Returns: (no return code implemented yet).
+ * Disconnects from the Accessibility Registry and releases 
+ * any floating resources. Call only once at exit.
  *
+ * Returns: 0 if there were no leaks, otherwise non zero.
  **/
 int
-AccessibleApplication_unref (AccessibleApplication *obj)
+SPI_exit (void)
 {
-  Accessibility_Application_unref (*obj, &ev);
-  return 0;
+  int leaked;
+
+  if (!SPI_inited)
+    {
+      return 0;
+    }
+
+  SPI_inited = FALSE;
+
+  if (live_refs)
+    {
+      leaked = g_hash_table_size (live_refs);
+#ifdef DEBUG_OBJECTS
+      fprintf (stderr, "Leaked %d SPI handles\n", leaked);
+
+#define PRINT_LEAKS
+#ifdef PRINT_LEAKS
+      g_hash_table_foreach (live_refs, report_leaked_ref, NULL);
+#endif
+
+#endif
+    }
+  else
+    {
+      leaked = 0;
+    }
+
+  cspi_cleanup ();
+
+  return leaked;
 }
 
 /**
- * AccessibleApplication_getToolkitName:
- * @obj: a pointer to the #AccessibleApplication to query.
- *
- * Get the name of the UI toolkit used by an #AccessibleApplication.
- *
- * Returns: a UTF-8 string indicating which UI toolkit is
- *          used by an application.
- *
+ * SPI_freeString:
+ * @s: a character string returned from another at-spi call.
+ *
+ * Free a character string returned from an at-spi call.  Clients of
+ * at-spi should use this function instead of free () or g_free().
+ * A NULL string @s will be silently ignored.
+ * This API should not be used to free strings
+ * from other libraries or allocated by the client.
  **/
-char *
-AccessibleApplication_getToolkitName (AccessibleApplication *obj)
+void
+SPI_freeString (char *s)
 {
-  return Accessibility_Application__get_toolkitName (*obj, &ev);
+  if (s)
+    {
+      g_free (s);
+    }
 }
 
 /**
- * AccessibleApplication_getVersion:
- * @obj: a pointer to the #AccessibleApplication being queried.
+ * SPI_freeRect:
+ * @r: a pointer to an SPIRect returned from another at-spi call.
  *
- * Get the version of the at-spi bridge exported by an
- *      #AccessibleApplication instance.
- *
- * Returns: a UTF-8 string indicating the application's
- *          at-spi version.
+ * Free a SPIRect structure returned from an at-spi call.  Clients of
+ * at-spi should use this function instead of free () or g_free().
+ * A NULL rect @r will be silently ignored.
+ * This API should not be used to free data
+ * from other libraries or allocated by the client.
  *
+ * @Since: AT-SPI 1.6
  **/
-char *
-AccessibleApplication_getVersion (AccessibleApplication *obj)
+void
+SPI_freeRect (SPIRect *r)
 {
-  return Accessibility_Application__get_version (*obj, &ev);
+  if (r)
+    {
+      /* err, okay, in this case the client _could_ 
+        have called g_free, but we don't want to guarantee it */
+      g_free (r);
+    }
 }
 
 /**
- * AccessibleApplication_getID:
- * @obj: a pointer to the #AccessibleApplication being queried.
- *
- * Get the unique ID assigned by the Registry to an
- *      #AccessibleApplication instance.
- * (Not Yet Implemented by the registry).
+ * SPI_dupString:
+ * @s: a UTF-8 string to be duplicated
+ * 
+ * @Since: AT-SPI 1.4
  *
- * Returns: a unique #long integer associated with the application
- *          by the Registry, or 0 if the application is not registered.
-long
-AccessibleApplication_getID (AccessibleApplication *obj)
+ * Returns: a duplicate of the string passed as a parameter, which should
+ * be freed via SPI_freeString after use.
+ **/
+char *
+SPI_dupString (char *s)
 {
-  return Accessibility_Application__get_id (*obj, &ev);
+  if (s)
+    {
+      return g_strdup (s);
+    }
+  else 
+    return NULL;
 }
 
 /**
- * AccessibleApplication_pause:
+ * SPI_exceptionHandlerPush:
+ * @handler: an #SPIExceptionHandler to install as the first code to deal with exceptions.
  *
- * Attempt to pause the application (used when client event queue is
- *  over-full).
- * Not Yet Implemented.
+ * Install a client-side handler for #SPIException instances, which can see and handle any
+ * exceptions before chaining them to the next exception handler in the stack.
  *
- * Returns: #TRUE if the application was paused successfully, #FALSE otherwise.
+ * @Since: AT-SPI 1.4
  *
+ * Returns %TRUE if the result succeeded, %FALSE if @hander could not be registered.
  **/
-boolean
-AccessibleApplication_pause (AccessibleApplication *obj)
+SPIBoolean SPI_exceptionHandlerPush (SPIExceptionHandler *handler)
 {
-  return FALSE;
+  if (!exception_handlers)
+    exception_handlers = g_queue_new ();
+  g_queue_push_head (exception_handlers, handler);
+  return TRUE;
 }
 
 /**
- * AccessibleApplication_resume:
+ * SPI_exceptionHandlerPop:
+ * 
+ * Remove/pop an #SPIExceptionHandler off the error handler stack and return the new handler.
  *
- * Attempt to resume the application (used after #AccessibleApplication_pause).
- * Not Yet Implemented.
- *
- * Returns: #TRUE if application processing resumed successfully, #FALSE otherwise.
+ * @Since: AT-SPI 1.4
  *
+ * Returns the #SPIExceptionHandler which is now at the top of the error handler stack after the call.
  **/
-boolean
-AccessibleApplication_resume (AccessibleApplication *obj)
+SPIExceptionHandler* SPI_exceptionHandlerPop (void)
 {
-  return FALSE;
+  return (SPIExceptionHandler *) g_queue_pop_head (exception_handlers);
 }
 
-/*
- *
- * AccessibleComponent function implementations
- *
- */
-
 /**
- * AccessibleComponent_ref:
- * @obj: a pointer to an object implementing #AccessibleComponent on which to operate.
+ * SPIException_getSourceType:
+ * @err: the exception being queried
  *
- * Increment the reference count for an #AccessibleComponent.
+ * Get the #SPIExceptionType of an exception which has been thrown.
  *
- * Returns: (no return code implemented yet).
+ * @Since: AT-SPI 1.4
  *
+ * Returns: the #SPIExceptionType corresponding to exception @err.
  **/
-int
-AccessibleComponent_ref (AccessibleComponent *obj)
+SPIExceptionType SPIException_getSourceType (SPIException *err)
 {
-  Accessibility_Component_ref (*obj, &ev);
-  return 0;
+  if (err)
+    return err->type;
+  else
+    return SPI_EXCEPTION_SOURCE_UNSPECIFIED;
 }
 
 /**
- * AccessibleComponent_unref:
- * @obj: a pointer to the object implementing #AccessibleComponent on which to operate.
+ * SPIException_getExceptionCode:
+ * @err: the #SPIException being queried.
  *
- * Decrement the reference count for an #AccessibleComponent.
+ * Get the #SPIExceptionCode telling what type of exception condition has occurred.
  *
- * Returns: (no return code implemented yet).
+ * @Since: AT-SPI 1.4
  *
+ * Returns: the #SPIExceptionCode corresponding to exception @err.
  **/
-int
-AccessibleComponent_unref (AccessibleComponent *obj)
-{
-  Accessibility_Component_unref (*obj, &ev);
-  return 0;
+SPIExceptionCode SPIException_getExceptionCode (SPIException *err)
+{  
+  return err->code;
 }
 
 /**
- * AccessibleComponent_contains:
- * @obj: a pointer to the #AccessibleComponent to query.
- * @x: a #long specifying the x coordinate in question.
- * @y: a #long specifying the y coordinate in question.
- * @ctype: the desired coordinate system of the point (@x, @y)
- *         (e.g. COORD_TYPE_WINDOW, COORD_TYPE_SCREEN).
+ * SPIAccessibleException_getSource:
+ * @err: the #SPIException being queried.
  *
- * Query whether a given #AccessibleComponent contains a particular point.
+ * Get the identity of the object which threw an exception.
  *
- **/
-boolean
-AccessibleComponent_contains (AccessibleComponent *obj,
-                              long x,
-                              long y,
-                              AccessibleCoordType ctype)
-{
-  return Accessibility_Component_contains (*obj,
-                                           (CORBA_long) x,
-                                           (CORBA_long) y,
-                                           ctype,
-                                           &ev);
-}
-
-/**
- * AccessibleComponent_getAccessibleAtPoint:
- * @obj: a pointer to the #AccessibleComponent to query.
- * @x: a #long specifying the x coordinate of the point in question.
- * @y: a #long specifying the y coordinate of the point in question.
- * @ctype: the coordinate system of the point (@x, @y)
- *         (e.g. COORD_TYPE_WINDOW, COORD_TYPE_SCREEN).
- *
- * Get the accessible child at a given coordinate within an #AccessibleComponent.
+ * @Since: AT-SPI 1.4
  *
- * Returns: a pointer to an #Accessible child of the specified component which
- *          contains the point (@x, @y), or NULL of no child contains the point.
+ * Returns: a pointer to the #Accessible object which threw the exception.
  **/
-Accessible *
-AccessibleComponent_getAccessibleAtPoint (AccessibleComponent *obj,
-                                          long x,
-                                          long y,
-                                          AccessibleCoordType ctype)
+Accessible* SPIAccessibleException_getSource (SPIException *err)
 {
-  Accessible child;
-  child = Accessibility_Component_getAccessibleAtPoint(*obj,
-                                                       (CORBA_long) x,
-                                                       (CORBA_long) y,
-                                                       ctype,
-                                                       &ev);
-  return (child != NULL) ? Obj_Add (child) : NULL;
+  if (err->type == SPI_EXCEPTION_SOURCE_ACCESSIBLE)
+    cspi_object_ref (err->source);
+    return err->source;
+  return NULL;
 }
 
 /**
- * AccessibleComponent_getExtents:
- * @obj: a pointer to the #AccessibleComponent to query.
- * @x: a pointer to a #long into which the minimum x coordinate will be returned.
- * @y: a pointer to a #long into which the minimum y coordinate will be returned.
- * @width: a pointer to a #long into which the x extents (width) will be returned.
- * @height: a pointer to a #long into which the y extents (height) will be returned.
- * @ctype: the desired coordinate system into which to return the results,
- *         (e.g. COORD_TYPE_WINDOW, COORD_TYPE_SCREEN).
+ * SPIException_getDescription:
+ * @err: the #SPIException being queried.
  *
- * Get the bounding box of the specified #AccessibleComponent.
+ * Get a text description of the exception that has been thrown.
+ * Unfortunately these descriptions tend to be terse and limited in
+ * the detail which they can provide.
  *
+ * Returns: a brief character string describing the exception.
  **/
-void
-AccessibleComponent_getExtents (AccessibleComponent *obj,
-                                long *x,
-                                long *y,
-                                long *width,
-                                long *height,
-                                AccessibleCoordType ctype)
-{
-  /* TODO: remove assumption that CORBA_long == long in typecast */
-  Accessibility_Component_getExtents (*obj,
-                                     (CORBA_long *) x,
-                                     (CORBA_long *) y,
-                                     (CORBA_long *) width,
-                                     (CORBA_long *) height,
-                                     ctype,
-                                     &ev);
+char* SPIException_getDescription (SPIException *err)
+{
+  /* TODO: friendlier error messages? */
+  if (err->error)
+    return err->error->message;
+  return NULL;
 }
 
-/**
- * AccessibleComponent_getPosition:
- * @obj: a pointer to the #AccessibleComponent to query.
- * @x: a pointer to a #long into which the minimum x coordinate will be returned.
- * @y: a pointer to a #long into which the minimum y coordinate will be returned.
- * @ctype: the desired coordinate system into which to return the results,
- *         (e.g. COORD_TYPE_WINDOW, COORD_TYPE_SCREEN).
- *
- * Get the minimum x and y coordinates of the specified #AccessibleComponent.
- *
- **/
-void
-AccessibleComponent_getPosition (AccessibleComponent *obj,
-                                 long *x,
-                                 long *y,
-                                 AccessibleCoordType ctype)
+static char *
+get_path (Accessible *obj)
 {
-  Accessibility_Component_getPosition (*obj,
-                                       (CORBA_long *) x,
-                                       (CORBA_long *) y,
-                                       ctype,
-                                       &ev);
+  if (APP_IS_REGISTRY (obj->app))
+  {
+    return g_strdup_printf (SPI_DBUS_PATH_DESKTOP);
+  }
+  return g_strdup_printf ("/org/freedesktop/atspi/accessible/%d", obj->v.id);
 }
 
-/**
- * AccessibleComponent_getSize:
- * @obj: a pointer to the #AccessibleComponent to query.
- * @width: a pointer to a #long into which the x extents (width) will be returned.
- * @height: a pointer to a #long into which the y extents (height) will be returned.
- *
- * Get the size of the specified #AccessibleComponent.
- *
- **/
-void
-AccessibleComponent_getSize (AccessibleComponent *obj,
-                             long *width,
-                             long *height)
+dbus_bool_t
+cspi_dbus_call (Accessible *obj, const char *interface, const char *method, DBusError *error, const char *type, ...)
 {
-  Accessibility_Component_getSize (*obj,
-                                   (CORBA_long *) width,
-                                   (CORBA_long *) height,
-                                   &ev);
+  va_list args;
+  char *path = get_path (obj);
+  dbus_bool_t retval;
+  DBusError err;
+
+  if (!error) error = &err;
+  dbus_error_init (error);
+  va_start (args, type);
+  retval = dbind_connection_method_call_va (cspi_bus(), obj->app->bus_name, path, interface, method, error, type, args);
+  va_end (args);
+  g_free (path);
+  if (dbus_error_is_set (error))
+  {
+    if (!dbus_error_is_set (&exception))
+    {
+      // TODO: Should we call cspi_exception_throw?
+      dbus_move_error (error, &exception);
+    }
+    else if (error == &err) dbus_error_free (error);
+  }
+  return retval;
 }
 
-/* Not Yet Implemented */
-void
-AccessibleComponent_grabFocus (AccessibleComponent *obj)
+dbus_bool_t
+cspi_dbus_get_property (Accessible *obj, const char *interface, const char *name, DBusError *error, const char *type, void *data)
 {
-  ;
+  DBusMessage *message, *reply;
+  char *path = get_path (obj);
+  DBusMessageIter iter, iter_variant;
+  DBusError err;
+  dbus_bool_t retval = FALSE;
+
+  message = dbus_message_new_method_call (obj->app->bus_name, path, "org.freedesktop.DBus.Properties", "Get");
+  if (!message)
+  {
+    // TODO: throw exception
+    goto done;
+  }
+  dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
+  if (!error) error = &err;
+  dbus_error_init (error);
+  reply = dbus_connection_send_with_reply_and_block (cspi_bus(), message, 1000, error);
+  dbus_message_unref (message);
+  if (!reply)
+  {
+    // TODO: throw exception
+    goto done;
+  }
+  dbus_message_iter_init (reply, &iter);
+  dbus_message_iter_recurse (&iter, &iter_variant);
+  if (dbus_message_iter_get_arg_type (&iter_variant) != type[0])
+  {
+    g_warning ("cspi_dbus_get_property: Wrong type: expected %s, got %c\n", type, dbus_message_iter_get_arg_type (&iter_variant));
+    goto done;
+  }
+  dbus_message_iter_get_basic (&iter_variant, data);
+  dbus_message_unref (reply);
+  if (type[0] == 's') *(char **)data = g_strdup (*(char **)data);
+  retval = TRUE;
+done:
+  g_free (path);
+  return retval;
 }