2002-03-27 Michael Meeks <michael@ximian.com>
[platform/core/uifw/at-spi2-atk.git] / cspi / spi_main.c
index 864dc14..ad9bd7b 100644 (file)
 
 static CORBA_Environment ev = { 0 };
 static Accessibility_Registry registry = CORBA_OBJECT_NIL;
-static SPIBoolean is_gnome_app = TRUE;
 static GHashTable *live_refs = NULL;
 
 static guint
-spi_object_hash (gconstpointer key)
+cspi_object_hash (gconstpointer key)
 {
   CORBA_Object object = (CORBA_Object) key;
   guint        retval;
@@ -26,7 +25,7 @@ spi_object_hash (gconstpointer key)
 }
 
 static gboolean
-spi_object_equal (gconstpointer a, gconstpointer b)
+cspi_object_equal (gconstpointer a, gconstpointer b)
 {
   CORBA_Object objecta = (CORBA_Object) a;
   CORBA_Object objectb = (CORBA_Object) b;
@@ -38,7 +37,7 @@ spi_object_equal (gconstpointer a, gconstpointer b)
 }
 
 static void
-spi_object_release (gpointer  value)
+cspi_object_release (gpointer  value)
 {
   Accessible *a = (Accessible *) value;
 
@@ -46,17 +45,16 @@ spi_object_release (gpointer  value)
   g_print ("releasing %p => %p\n", a, a->objref);
 #endif
 
-  bonobo_object_release_unref (a->objref, NULL);
+  cspi_release_unref (a->objref);
 
   memset (a, 0xaa, sizeof (Accessible));
   a->ref_count = -1;
 
 #ifndef DEBUG_OBJECTS
-  g_free (a);
+  free (a);
 #endif
 }
 
-
 SPIBoolean
 cspi_accessible_is_a (Accessible *obj,
                      const char *interface_name)
@@ -72,17 +70,17 @@ cspi_accessible_is_a (Accessible *obj,
   unknown = Bonobo_Unknown_queryInterface (CSPI_OBJREF (obj),
                                           interface_name, cspi_ev ());
 
-  if (BONOBO_EX (cspi_ev ()))
+  if (ev._major != CORBA_NO_EXCEPTION)
     {
       g_error ("Exception '%s' checking if is '%s'",
-              bonobo_exception_get_text (cspi_ev ()),
+              cspi_exception_get_text (),
               interface_name);
     }
 
   if (unknown != CORBA_OBJECT_NIL)
     {
       retval = TRUE;
-      bonobo_object_release_unref (unknown, NULL);
+      cspi_release_unref (unknown);
     }
   else
     {
@@ -93,14 +91,14 @@ cspi_accessible_is_a (Accessible *obj,
 }
 
 static GHashTable *
-get_live_refs (void)
+cspi_get_live_refs (void)
 {
   if (!live_refs) 
     {
-      live_refs = g_hash_table_new_full (spi_object_hash,
-                                        spi_object_equal,
+      live_refs = g_hash_table_new_full (cspi_object_hash,
+                                        cspi_object_equal,
                                         NULL,
-                                        spi_object_release);
+                                        cspi_object_release);
     }
   return live_refs;
 }
@@ -119,17 +117,11 @@ cspi_registry (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;
@@ -151,20 +143,24 @@ cspi_object_add (CORBA_Object corba_object)
     {
       ref = NULL;
     }
+  else if (!cspi_check_ev ("pre method check"))
+    {
+      ref = NULL;
+    }
   else
     {
-      if ((ref = g_hash_table_lookup (get_live_refs (), corba_object)))
+      if ((ref = g_hash_table_lookup (cspi_get_live_refs (), corba_object)))
         {
           g_assert (ref->ref_count > 0);
          ref->ref_count++;
-          bonobo_object_release_unref (corba_object, NULL);
+          cspi_release_unref (corba_object);
 #ifdef DEBUG_OBJECTS
           g_print ("returning cached %p => %p\n", ref, ref->objref);
 #endif
        }
       else
         {
-          ref = g_new (Accessible, 1);
+          ref = malloc (sizeof (Accessible));
 
 #ifdef DEBUG_OBJECTS
           g_print ("allocating %p => %p\n", ref, corba_object);
@@ -173,38 +169,13 @@ cspi_object_add (CORBA_Object corba_object)
           ref->objref = corba_object;
           ref->ref_count = 1;
 
-          g_hash_table_insert (get_live_refs (), ref->objref, ref);
+          g_hash_table_insert (cspi_get_live_refs (), ref->objref, ref);
        }
     }
 
   return ref;
 }
 
-Accessible *
-cspi_object_add_check (CORBA_Object corba_object)
-{
-  Accessible *retval;
-
-  if (ev._major == CORBA_USER_EXCEPTION &&
-      !strcmp (ev._id, ex_Accessibility_ChildGone))
-    {
-      retval = NULL;
-    }
-  else if (ev._major != CORBA_NO_EXCEPTION)
-    {
-      cspi_check_ev (cspi_ev (), "pre method check");
-      retval = NULL;
-    }
-  else
-    {
-      retval = cspi_object_add (corba_object);
-
-      cspi_check_ev (cspi_ev (), "post method check");
-    }
-
-  return retval;
-}
-
 void
 cspi_object_ref (Accessible *accessible)
 {
@@ -223,7 +194,7 @@ cspi_object_unref (Accessible *accessible)
 
   if (--accessible->ref_count == 0)
     {
-      g_hash_table_remove (get_live_refs (), accessible->objref);
+      g_hash_table_remove (cspi_get_live_refs (), accessible->objref);
     }
 }
 
@@ -241,7 +212,7 @@ cspi_cleanup (void)
 
   if (registry != CORBA_OBJECT_NIL)
     {
-      bonobo_object_release_unref (registry, NULL);
+      cspi_release_unref (registry);
       registry = CORBA_OBJECT_NIL;
     }
 }
@@ -250,22 +221,14 @@ static gboolean SPI_inited = FALSE;
 
 /**
  * SPI_init:
- * @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.
  *
  * Connects to the accessibility registry and initializes the SPI.
  *
  * Returns: 0 on success, otherwise an integer error code.
  **/
 int
-SPI_init (SPIBoolean isGNOMEApp)
+SPI_init (void)
 {
-  int argc = 0;
-  char *obj_id;
-  is_gnome_app = isGNOMEApp;
-
   if (SPI_inited)
     {
       return 1;
@@ -275,33 +238,9 @@ SPI_init (SPIBoolean isGNOMEApp)
 
   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 ()));
-    }
-
-  if (registry == CORBA_OBJECT_NIL)
-    {
-      g_error ("Could not locate registry");
-    }
-
-  bonobo_activate ();
+  registry = cspi_init ();
 
-  if (isGNOMEApp)
-    {
-      g_atexit (cspi_cleanup);
-    }
+  g_atexit (cspi_cleanup);
   
   return 0;
 }
@@ -311,23 +250,26 @@ SPI_init (SPIBoolean isGNOMEApp)
  *
  * 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 ()
+SPI_event_main (void)
 {
-  if (cspi_is_gnome_app ())
-    {
-      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 ();
 }
 
 /**
@@ -369,28 +311,56 @@ SPI_nextEvent (SPIBoolean waitForEvent)
  * 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)
 {
+  int leaked;
+
   if (!SPI_inited)
     {
-      return;
+      return 0;
     }
 
   SPI_inited = FALSE;
 
-  cspi_cleanup ();
-
-  if (cspi_is_gnome_app ())
+  if (live_refs)
     {
-      bonobo_main_quit ();
+      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);
+}