#include <stdlib.h>
#include <cspi/spi-private.h>
+#undef DEBUG_OBJECTS
+
static CORBA_Environment ev = { 0 };
static Accessibility_Registry registry = CORBA_OBJECT_NIL;
-static SPIBoolean is_gnome_app = FALSE;
-static GSList *live_refs = NULL;
+static GHashTable *live_refs = NULL;
+
+static guint
+spi_object_hash (gconstpointer key)
+{
+ CORBA_Object object = (CORBA_Object) key;
+ guint retval;
+
+ retval = CORBA_Object_hash (object, 0, &ev);
+
+ return retval;
+}
+
+static gboolean
+spi_object_equal (gconstpointer a, gconstpointer b)
+{
+ CORBA_Object objecta = (CORBA_Object) a;
+ CORBA_Object objectb = (CORBA_Object) b;
+ gboolean retval;
+
+ retval = CORBA_Object_is_equivalent (objecta, objectb, &ev);
+
+ return retval;
+}
+
+static void
+spi_object_release (gpointer value)
+{
+ Accessible *a = (Accessible *) value;
+
+#ifdef DEBUG_OBJECTS
+ g_print ("releasing %p => %p\n", a, a->objref);
+#endif
+
+ cspi_release_unref (a->objref);
+
+ memset (a, 0xaa, sizeof (Accessible));
+ a->ref_count = -1;
+
+#ifndef DEBUG_OBJECTS
+ free (a);
+#endif
+}
+
+SPIBoolean
+cspi_accessible_is_a (Accessible *obj,
+ const char *interface_name)
+{
+ SPIBoolean retval;
+ Bonobo_Unknown unknown;
+
+ if (obj == NULL)
+ {
+ return FALSE;
+ }
+
+ unknown = Bonobo_Unknown_queryInterface (CSPI_OBJREF (obj),
+ interface_name, cspi_ev ());
+
+ if (ev._major != CORBA_NO_EXCEPTION)
+ {
+ g_error ("Exception '%s' checking if is '%s'",
+ cspi_exception_get_text (),
+ interface_name);
+ }
+
+ if (unknown != CORBA_OBJECT_NIL)
+ {
+ retval = TRUE;
+ cspi_release_unref (unknown);
+ }
+ else
+ {
+ retval = FALSE;
+ }
+
+ return retval;
+}
+
+static GHashTable *
+get_live_refs (void)
+{
+ if (!live_refs)
+ {
+ live_refs = g_hash_table_new_full (spi_object_hash,
+ spi_object_equal,
+ NULL,
+ spi_object_release);
+ }
+ return live_refs;
+}
CORBA_Environment *
cspi_ev (void)
}
SPIBoolean
-cspi_is_gnome_app (void)
-{
- return is_gnome_app;
-}
-
-SPIBoolean
cspi_exception (void)
{
SPIBoolean retval;
- if (BONOBO_EX (&ev))
+ if (ev._major != CORBA_NO_EXCEPTION)
{
CORBA_exception_free (&ev);
retval = TRUE;
{
Accessible *ref;
- if (corba_object != CORBA_OBJECT_NIL)
+ 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);
+ ref = NULL;
+ }
+ else if (!cspi_check_ev ("pre method check"))
+ {
+ ref = NULL;
}
else
- {
- ref = NULL;
- }
+ {
+ if ((ref = g_hash_table_lookup (get_live_refs (), corba_object)))
+ {
+ g_assert (ref->ref_count > 0);
+ ref->ref_count++;
+ cspi_release_unref (corba_object);
+#ifdef DEBUG_OBJECTS
+ g_print ("returning cached %p => %p\n", ref, ref->objref);
+#endif
+ }
+ else
+ {
+ ref = malloc (sizeof (Accessible));
+
+#ifdef DEBUG_OBJECTS
+ g_print ("allocating %p => %p\n", ref, corba_object);
+#endif
+
+ ref->objref = corba_object;
+ ref->ref_count = 1;
+
+ g_hash_table_insert (get_live_refs (), ref->objref, ref);
+ }
+ }
return ref;
}
void
cspi_object_unref (Accessible *accessible)
{
- g_return_if_fail (accessible != NULL);
-
- if (--accessible->ref_count == 0)
+ if (accessible == NULL)
{
- 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));
+ return;
+ }
- g_free (accessible);
+ if (--accessible->ref_count == 0)
+ {
+ g_hash_table_remove (get_live_refs (), accessible->objref);
}
}
static void
cspi_cleanup (void)
{
- GSList *l, *refs;
+ GHashTable *refs;
refs = live_refs;
live_refs = NULL;
-
- for (l = refs; l; l = l->next)
+ if (refs)
{
- Accessible *a = l->data;
-
- g_print ("releasing %p\n", l->data);
- bonobo_object_release_unref (a->objref, NULL);
-
- g_free (a);
+ g_hash_table_destroy (refs);
}
- g_slist_free (refs);
-
if (registry != CORBA_OBJECT_NIL)
{
- bonobo_object_release_unref (registry, NULL);
+ cspi_release_unref (registry);
registry = CORBA_OBJECT_NIL;
}
}
+static gboolean SPI_inited = FALSE;
+
/**
* SPI_init:
*
int
SPI_init (void)
{
- int argc = 0;
- char *obj_id;
- static gboolean inited = FALSE;
-
- if (inited)
+ if (SPI_inited)
{
return 1;
}
- inited = TRUE;
+ SPI_inited = TRUE;
CORBA_exception_init (&ev);
- if (!bonobo_init (&argc, NULL))
- {
- g_error ("Could not initialize Bonobo");
- }
-
- obj_id = "OAFIID:Accessibility_Registry:proto0.1";
-
- registry = bonobo_activation_activate_from_id (
- obj_id, 0, NULL, cspi_ev ());
-
- if (ev._major != CORBA_NO_EXCEPTION)
- {
- g_error ("AT-SPI error: during registry activation: %s\n",
- bonobo_exception_get_text (cspi_ev ()));
- }
+ registry = cspi_init ();
- if (registry == CORBA_OBJECT_NIL)
- {
- g_error ("Could not locate registry");
- }
-
- Accessibility_Registry_ref (registry, &ev);
-
- bonobo_activate ();
+ g_atexit (cspi_cleanup);
return 0;
}
/**
* SPI_event_main:
- * @isGNOMEApp: a #SPIBoolean indicating whether the client of the SPI
- * will use the Gnome event loop or not. Clients that have
- * their own GUIS will usually specify #TRUE here, and must
- * do so if they use Gnome GUI components.
*
* 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_exit()
- * from within an event handler).
+ * (NOTE: This method does not return control, it is exited via a call to
+ * SPI_event_quit () from within an event handler).
*
**/
void
-SPI_event_main (SPIBoolean isGNOMEApp)
+SPI_event_main (void)
{
- is_gnome_app = isGNOMEApp;
- if (isGNOMEApp)
- {
- g_atexit (cspi_cleanup);
- bonobo_main ();
- }
- else
- {
- /* TODO: install signal handlers to do cleanup */
- CORBA_ORB_run (bonobo_orb (), cspi_ev ());
- fprintf (stderr, "orb loop exited...\n");
- }
+ cspi_main ();
+}
+
+/**
+ * SPI_event_quit:
+ *
+ * Quits the last main event loop for the SPI services,
+ * see SPI_event_main
+ **/
+void
+SPI_event_quit (void)
+{
+ cspi_main_quit ();
}
/**
* SPI_exit:
*
* Disconnects from the Accessibility Registry and releases
- * any floating resources.
+ * any floating resources. Call only once at exit.
+ *
+ * Returns: 0 if there were no leaks, otherwise non zero.
**/
-void
+int
SPI_exit (void)
{
- cspi_cleanup();
- if (cspi_is_gnome_app ())
+ int leaked;
+
+ if (!SPI_inited)
{
- bonobo_main_quit ();
+ return 0;
+ }
+
+ SPI_inited = FALSE;
+
+ if (live_refs)
+ {
+ leaked = g_hash_table_size (live_refs);
}
else
{
- CORBA_ORB_shutdown (bonobo_orb (), TRUE, cspi_ev ());
+ leaked = 0;
}
+
+#ifdef DEBUG_OBJECTS
+ if (leaked)
+ {
+ fprintf (stderr, "Leaked %d SPI handles\n", leaked);
+ }
+#endif
+
+ cspi_cleanup ();
+
fprintf (stderr, "bye-bye!\n");
+
+ return leaked;
}
+/**
+ * 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().
+ * This API should not be used to free strings
+ * from other libraries or allocated by the client.
+ **/
+void
+SPI_freeString (char *s)
+{
+ CORBA_free (s);
+}