+#include <string.h>
+#include <stdlib.h>
+#include <cspi/spi-private.h>
+
+static CORBA_Environment ev = { 0 };
+static Accessibility_Registry registry = CORBA_OBJECT_NIL;
+static SPIBoolean is_gnome_app = FALSE;
+static GSList *live_refs = NULL;
+
+CORBA_Environment *
+cspi_ev (void)
+{
+ /* This method is an ugly hack */
+ return &ev;
+}
+
+Accessibility_Registry
+cspi_registry (void)
+{
+ return registry;
+}
+
+SPIBoolean
+cspi_is_gnome_app (void)
+{
+ return is_gnome_app;
+}
+
+SPIBoolean
+cspi_exception (void)
+{
+ SPIBoolean retval;
+
+ if (BONOBO_EX (&ev))
+ {
+ CORBA_exception_free (&ev);
+ retval = TRUE;
+ }
+ else
+ {
+ retval = FALSE;
+ }
+
+ return retval;
+}
+
+Accessible *
+cspi_object_add (CORBA_Object corba_object)
+{
+ Accessible *ref;
+
+ if (corba_object != CORBA_OBJECT_NIL)
+ {
+ ref = g_new (Accessible, 1);
+
+ ref->objref = CORBA_Object_duplicate (corba_object, cspi_ev());
+ ref->ref_count = 1;
+
+ live_refs = g_slist_prepend (live_refs, ref);
+ }
+ else
+ {
+ ref = NULL;
+ }
+
+ return ref;
+}
+
+void
+cspi_object_ref (Accessible *accessible)
+{
+ g_return_if_fail (accessible != NULL);
+
+ accessible->ref_count++;
+}
+
+void
+cspi_object_unref (Accessible *accessible)
+{
+ g_return_if_fail (accessible != NULL);
+
+ if (--accessible->ref_count == 0)
+ {
+ live_refs = g_slist_remove (live_refs, accessible);
+
+ bonobo_object_release_unref (accessible->objref, cspi_ev ());
+
+ cspi_check_ev (cspi_ev (), "unref");
+
+ memset (accessible, 0xaa, sizeof (Accessible));
+
+ g_free (accessible);
+ }
+}
+
+static void
+cspi_cleanup (void)
+{
+ GSList *l, *refs;
+
+ refs = live_refs;
+ live_refs = NULL;
+
+ for (l = refs; l; l = l->next)
+ {
+ Accessible *a = l->data;
+
+ g_print ("releasing %p\n", l->data);
+ bonobo_object_release_unref (a->objref, NULL);
+
+ g_free (a);
+ }
+
+ g_slist_free (refs);
+
+ if (registry != CORBA_OBJECT_NIL)
+ {
+ bonobo_object_release_unref (registry, NULL);
+ registry = CORBA_OBJECT_NIL;
+ }
+}