* AT-SPI - Assistive Technology Service Provider Interface
* (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
*
- * Copyright 2001 Sun Microsystems Inc.
+ * 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
cspi_object_hash (gconstpointer key)
{
CORBA_Object object = (CORBA_Object) key;
- guint retval;
-
- retval = CORBA_Object_hash (object, 0, &ev);
- return retval;
+ return CORBA_Object_hash (object, 0, &ev);
}
static gboolean
{
CORBA_Object objecta = (CORBA_Object) a;
CORBA_Object objectb = (CORBA_Object) b;
- gboolean retval;
- retval = CORBA_Object_is_equivalent (objecta, objectb, &ev);
-
- return retval;
+ return CORBA_Object_is_equivalent (objecta, objectb, &ev);
}
static void
-cspi_object_release (gpointer value)
+cspi_object_release (gpointer value)
{
Accessible *a = (Accessible *) value;
g_print ("releasing %p => %p\n", a, a->objref);
#endif
- cspi_release_unref (a->objref);
+ if (!a->on_loan)
+ {
+ cspi_release_unref (a->objref);
+ }
memset (a, 0xaa, sizeof (Accessible));
a->ref_count = -1;
}
SPIBoolean
-cspi_accessible_is_a (Accessible *obj,
+cspi_accessible_is_a (Accessible *accessible,
const char *interface_name)
{
SPIBoolean retval;
Bonobo_Unknown unknown;
- if (obj == NULL)
+ if (accessible == NULL)
{
return FALSE;
}
- unknown = Bonobo_Unknown_queryInterface (CSPI_OBJREF (obj),
+ unknown = Bonobo_Unknown_queryInterface (CSPI_OBJREF (accessible),
interface_name, cspi_ev ());
if (ev._major != CORBA_NO_EXCEPTION)
CORBA_Environment *
cspi_ev (void)
{
- /* This method is an ugly hack */
+ CORBA_exception_init (&ev);
return &ev;
}
Accessibility_Registry
cspi_registry (void)
{
+ if (!cspi_ping (registry))
+ {
+ registry = cspi_init ();
+ }
return registry;
}
return retval;
}
-Accessible *
-cspi_object_add (CORBA_Object corba_object)
+/*
+ * This method swallows the corba_object BonoboUnknown
+ * reference, and returns an Accessible associated with it.
+ * If the reference is loaned, it means it is only valid
+ * between a borrow / return pair.
+ */
+static Accessible *
+cspi_object_get_ref (CORBA_Object corba_object, gboolean on_loan)
{
Accessible *ref;
{
ref = NULL;
}
- else if (!cspi_check_ev ("pre method check"))
+ else if (!cspi_check_ev ("pre method check: add"))
{
ref = NULL;
}
{
g_assert (ref->ref_count > 0);
ref->ref_count++;
- cspi_release_unref (corba_object);
+ if (!on_loan)
+ {
+ if (ref->on_loan) /* Convert to a permanant ref */
+ {
+ ref->on_loan = FALSE;
+ }
+ else
+ {
+ cspi_release_unref (corba_object);
+ }
+ }
#ifdef DEBUG_OBJECTS
g_print ("returning cached %p => %p\n", ref, ref->objref);
#endif
}
else
{
- ref = malloc (sizeof (Accessible));
-
+ ref = malloc (sizeof (Accessible));
+ ref->objref = corba_object;
+ ref->ref_count = 1;
+ ref->on_loan = on_loan;
#ifdef DEBUG_OBJECTS
- g_print ("allocating %p => %p\n", ref, corba_object);
+ g_print ("allocated %p => %p\n", ref, corba_object);
#endif
-
- ref->objref = corba_object;
- ref->ref_count = 1;
-
g_hash_table_insert (cspi_get_live_refs (), ref->objref, ref);
}
}
return ref;
}
+Accessible *
+cspi_object_add (CORBA_Object corba_object)
+{
+ return cspi_object_get_ref (corba_object, FALSE);
+}
+
+Accessible *
+cspi_object_borrow (CORBA_Object corba_object)
+{
+ return cspi_object_get_ref (corba_object, TRUE);
+}
+
+void
+cspi_object_return (Accessible *accessible)
+{
+ g_return_if_fail (accessible != NULL);
+
+ if (!accessible->on_loan ||
+ accessible->ref_count == 1)
+ {
+ cspi_object_unref (accessible);
+ }
+ else /* Convert to a permanant ref */
+ {
+ accessible->on_loan = FALSE;
+ accessible->objref = cspi_dup_ref (accessible->objref);
+ accessible->ref_count--;
+ }
+}
+
+Accessible *
+cspi_object_take (CORBA_Object corba_object)
+{
+ Accessible *accessible;
+ accessible = cspi_object_borrow (corba_object);
+
+ cspi_object_ref (accessible);
+ /*
+ * if the remote object is dead,
+ * cspi_object_return will throw an exception.
+ * FIXME: what clears that exception context ever ?
+ */
+ cspi_object_return (accessible);
+ if (cspi_exception ())
+ {
+ cspi_object_unref (accessible);
+ accessible = NULL;
+ }
+ return accessible;
+}
+
void
cspi_object_ref (Accessible *accessible)
{
return NULL;
}
+static void
+report_leaked_ref (gpointer key, gpointer val, gpointer user_data)
+{
+ char *name, *role;
+ Accessible *a = (Accessible *) val;
+
+ name = Accessible_getName (a);
+ if (cspi_exception ())
+ {
+ name = NULL;
+ }
+
+ role = Accessible_getRoleName (a);
+ if (cspi_exception ())
+ {
+ role = NULL;
+ }
+
+ fprintf (stderr, "leaked %d references to object %s, role %s %p\n",
+ a->ref_count, name ? name : "<?>", role ? role : "<?>", a);
+
+ SPI_freeString (name);
+}
+
+
/**
* SPI_exit:
*
if (live_refs)
{
leaked = g_hash_table_size (live_refs);
+#define PRINT_LEAKS
+#ifdef PRINT_LEAKS
+ g_hash_table_foreach (live_refs, report_leaked_ref, NULL);
+#endif
}
else
{
*
* 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.
**/
void
SPI_freeString (char *s)
{
- CORBA_free (s);
+ if (s)
+ {
+ CORBA_free (s);
+ }
}