2001-12-10 Michael Meeks <michael@ximian.com>
[platform/core/uifw/at-spi2-atk.git] / cspi / spi_main.c
index 0ba8e69..0bc3a6c 100644 (file)
-#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;
-}
-
 /*
  *
  * Basic SPI initialization and event loop function prototypes
  *
  */
+#include <string.h>
+#include <stdlib.h>
+#include <cspi/spi-private.h>
 
-int
-SPI_init (void)
+#undef DEBUG_OBJECTS
+
+static CORBA_Environment ev = { 0 };
+static Accessibility_Registry registry = CORBA_OBJECT_NIL;
+static GHashTable *live_refs = NULL;
+
+static guint
+spi_object_hash (gconstpointer key)
 {
-  int argc = 0;
-  CORBA_Object oclient;
-  char *obj_id;
+  CORBA_Object object = (CORBA_Object) key;
+  guint        retval;
+  
+  retval = CORBA_Object_hash (object, 0, &ev);
 
-  CORBA_exception_init(&ev);
+  return retval;
+}
 
-  if (!bonobo_init (&argc, NULL))
-    {
-      g_error ("Could not initialize Bonobo");
-    }
+static gboolean
+spi_object_equal (gconstpointer a, gconstpointer b)
+{
+  CORBA_Object objecta = (CORBA_Object) a;
+  CORBA_Object objectb = (CORBA_Object) b;
+  gboolean     retval;
 
-  obj_id = "OAFIID:Accessibility_Registry:proto0.1";
+  retval = CORBA_Object_is_equivalent (objecta, objectb, &ev);
 
-  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);
-  }
+  return retval;
+}
 
-  if (CORBA_Object_is_nil (oclient, &ev))
-    {
-      g_error ("Could not locate registry");
-      exit(-1);
-    }
+static void
+spi_object_release (gpointer  value)
+{
+  Accessible *a = (Accessible *) value;
 
-  registry = (Accessibility_Registry) oclient;
+#ifdef DEBUG_OBJECTS
+  g_print ("releasing %p => %p\n", a, a->objref);
+#endif
 
-  bonobo_activate ();
+  bonobo_object_release_unref (a->objref, NULL);
 
-  return 0;
-}
+  memset (a, 0xaa, sizeof (Accessible));
+  a->ref_count = -1;
 
-void
-SPI_event_main (boolean isGNOMEApp)
-{
-  if (isGNOMEApp) bonobo_main();
-  else CORBA_ORB_run (bonobo_orb(), &ev);
+#ifndef DEBUG_OBJECTS
+  g_free (a);
+#endif
 }
 
-/* Not Yet Implemented */
-boolean
-SPI_eventIsReady ()
-{
-  return FALSE;
-}
 
-/* Not Yet Implemented */
-AccessibleEvent *
-SPI_nextEvent (boolean waitForEvent)
+SPIBoolean
+cspi_accessible_is_a (Accessible *obj,
+                     const char *interface_name)
 {
-  return NULL;
-}
+  SPIBoolean        retval;
+  Bonobo_Unknown unknown;
 
-void
-SPI_exit (void)
-{
-  exit(0);
+  if (obj == NULL)
+    {
+      return FALSE;
+    }
+
+  unknown = Bonobo_Unknown_queryInterface (CSPI_OBJREF (obj),
+                                          interface_name, cspi_ev ());
+
+  if (BONOBO_EX (cspi_ev ()))
+    {
+      g_error ("Exception '%s' checking if is '%s'",
+              bonobo_exception_get_text (cspi_ev ()),
+              interface_name);
+    }
+
+  if (unknown != CORBA_OBJECT_NIL)
+    {
+      retval = TRUE;
+      bonobo_object_release_unref (unknown, NULL);
+    }
+  else
+    {
+      retval = FALSE;
+    }
+
+  return retval;
 }
 
-AccessibleEventListener *
-createEventListener (AccessibleEventListenerCB callback)
+static GHashTable *
+get_live_refs (void)
 {
-  AccessibleEventListener *listener = accessible_event_listener_new ();
-  if (callback)
+  if (!live_refs) 
     {
-      accessible_event_listener_add_callback (listener, callback);
+      live_refs = g_hash_table_new_full (spi_object_hash,
+                                        spi_object_equal,
+                                        NULL,
+                                        spi_object_release);
     }
-  return listener;
+  return live_refs;
 }
 
-boolean
-EventListener_addCallback (AccessibleEventListener *listener,
-                           AccessibleEventListenerCB callback)
+CORBA_Environment *
+cspi_ev (void)
 {
-  accessible_event_listener_add_callback (listener, callback);
-  return TRUE;
+  /* This method is an ugly hack */
+  return &ev;
 }
 
-boolean
-EventListener_removeCallback (AccessibleEventListener *listener,
-                           AccessibleEventListenerCB callback)
+Accessibility_Registry
+cspi_registry (void)
 {
-  accessible_event_listener_remove_callback (listener, callback);
-  return TRUE;
+  return registry;
 }
 
-/*
- *
- * Global functions serviced by the registry
- *
- */
-
-boolean
-registerGlobalEventListener (AccessibleEventListener *listener,
-                             char *eventType)
+SPIBoolean
+cspi_exception (void)
 {
-  Accessibility_Registry_registerGlobalEventListener (
-                         registry,
-                         (Accessibility_EventListener)
-                            bonobo_object_corba_objref (bonobo_object (listener)),
-                         eventType,
-                         &ev);
+  SPIBoolean retval;
 
-  if (ev._major != CORBA_NO_EXCEPTION)
+  if (BONOBO_EX (&ev))
     {
-    return FALSE;
+      CORBA_exception_free (&ev);
+      retval = TRUE;
     }
   else
     {
-      return TRUE;
+      retval = FALSE;
     }
-}
 
-int
-getDesktopCount ()
-{
-  return Accessibility_Registry_getDesktopCount (registry, &ev);
+  return retval;
 }
 
-Accessible*
-getDesktop (int n)
+Accessible *
+cspi_object_add (CORBA_Object corba_object)
 {
-  return Obj_Add (Accessibility_Registry_getDesktop (registry, (CORBA_short) n, &ev));
-}
+  Accessible *ref;
 
-int
-getDesktopList (Accessible **list)
-{
-  *list = NULL;
-  return 0;
+  if (corba_object == CORBA_OBJECT_NIL)
+    {
+      ref = NULL;
+    }
+  else if (!cspi_check_ev ("pre method check"))
+    {
+      ref = NULL;
+    }
+  else
+    {
+      if ((ref = g_hash_table_lookup (get_live_refs (), corba_object)))
+        {
+          g_assert (ref->ref_count > 0);
+         ref->ref_count++;
+          bonobo_object_release_unref (corba_object, NULL);
+#ifdef DEBUG_OBJECTS
+          g_print ("returning cached %p => %p\n", ref, ref->objref);
+#endif
+       }
+      else
+        {
+          ref = g_new (Accessible, 1);
+
+#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;
 }
 
-/* Not Yet Implemented */
 void
-registerKeystrokeListener (KeystrokeListener *listener)
+cspi_object_ref (Accessible *accessible)
 {
-  ;
+  g_return_if_fail (accessible != NULL);
+
+  accessible->ref_count++;
 }
 
-/* Not Yet Implemented */
 void
-generateKeyEvent (long keyCode, long meta)
+cspi_object_unref (Accessible *accessible)
 {
-  ;
+  if (accessible == NULL)
+    {
+      return;
+    }
+
+  if (--accessible->ref_count == 0)
+    {
+      g_hash_table_remove (get_live_refs (), accessible->objref);
+    }
 }
 
-/* Not Yet Implemented */
-void
-generateMouseEvent (long x, long y, char *name)
+static void
+cspi_cleanup (void)
 {
-  ;
+  GHashTable *refs;
+
+  refs = live_refs;
+  live_refs = NULL;
+  if (refs)
+    {
+      g_hash_table_destroy (refs);
+    }
+
+  if (registry != CORBA_OBJECT_NIL)
+    {
+      bonobo_object_release_unref (registry, NULL);
+      registry = CORBA_OBJECT_NIL;
+    }
 }
 
-/*
+static gboolean SPI_inited = FALSE;
+
+/**
+ * SPI_init:
  *
- * Accessible function prototypes
+ * Connects to the accessibility registry and initializes the SPI.
  *
- */
-
+ * Returns: 0 on success, otherwise an integer error code.
+ **/
 int
-Accessible_ref (Accessible *obj)
+SPI_init (void)
 {
-  Accessibility_Accessible_ref (*obj, &ev);
-  return 0;
-}
+  int argc = 0;
+  char *obj_id;
 
+  if (SPI_inited)
+    {
+      return 1;
+    }
 
-int
-Accessible_unref (Accessible *obj)
-{
-  Accessibility_Accessible_unref (*obj, &ev);
-  return 0;
-}
+  SPI_inited = TRUE;
 
-char *
-Accessible_getName (Accessible *obj)
-{
-  return Accessibility_Accessible__get_name (*obj, &ev);
-}
+  CORBA_exception_init (&ev);
 
-char *
-Accessible_getDescription (Accessible *obj)
-{
-  return Accessibility_Accessible__get_description (*obj, &ev);
-}
+  if (!bonobo_init (&argc, NULL))
+    {
+      g_error ("Could not initialize Bonobo");
+    }
 
-Accessible *
-Accessible_getParent (Accessible *obj)
-{
-  return Obj_Add (Accessibility_Accessible__get_parent (*obj, &ev));
-}
+  obj_id = "OAFIID:Accessibility_Registry:proto0.1";
 
-long
-Accessible_getChildCount (Accessible *obj)
-{
-  return Accessibility_Accessible__get_childCount (*obj, &ev);
-}
+  registry = bonobo_activation_activate_from_id (
+         obj_id, 0, NULL, cspi_ev ());
 
-Accessible *
-Accessible_getChildAtIndex (Accessible *obj,
-                            long childIndex)
-{
-  return Obj_Add (Accessibility_Accessible_getChildAtIndex (*obj, childIndex, &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 ();
+
+  g_atexit (cspi_cleanup);
+  
+  return 0;
 }
 
-long
-Accessible_getIndexInParent (Accessible *obj)
+/**
+ * SPI_event_main:
+ *
+ * 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).
+ *
+ **/
+void
+SPI_event_main (void)
 {
-  return Accessibility_Accessible_getIndexInParent (*obj, &ev);
+  bonobo_main ();
 }
 
-/* Not Yet Implemented */
-AccessibleRelation **
-Accessible_getRelationSet (Accessible *obj)
+/**
+ * SPI_event_quit:
+ *
+ * Quits the last main event loop for the SPI services,
+ * see SPI_event_main
+ **/
+void
+SPI_event_quit (void)
 {
-  return NULL;
+  bonobo_main_quit ();
 }
 
-/* Not Yet Implemented */
-char *
-Accessible_getRole (Accessible *obj)
+/**
+ * SPI_eventIsReady:
+ *
+ * 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.
+ *
+ **/
+SPIBoolean
+SPI_eventIsReady ()
 {
-  return "";
+  return FALSE;
 }
 
-/* Not Yet Implemented */
-AccessibleStateSet *
-Accessible_getStateSet (Accessible *obj)
+/**
+ * SPI_nextEvent:
+ * @waitForEvent: a #SPIBoolean indicating whether to block or not.
+ *
+ * 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.
+ **/
+AccessibleEvent *
+SPI_nextEvent (SPIBoolean waitForEvent)
 {
   return NULL;
 }
 
-
-/*
+/**
+ * SPI_exit:
  *
- * AccessibleApplication function prototypes
+ * 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_ref (AccessibleApplication *obj)
+SPI_exit (void)
 {
-  Accessibility_Application_ref (*obj, &ev);
-  return 0;
-}
+  int leaked;
 
-int
-AccessibleApplication_unref (AccessibleApplication *obj)
-{
-  Accessibility_Application_unref (*obj, &ev);
-  return 0;
-}
+  if (!SPI_inited)
+    {
+      return 0;
+    }
 
-char *
-AccessibleApplication_getToolkitName (AccessibleApplication *obj)
-{
-  return Accessibility_Application__get_toolkitName (*obj, &ev);
-}
+  SPI_inited = FALSE;
 
-char *
-AccessibleApplication_getVersion (AccessibleApplication *obj)
-{
-  return Accessibility_Application__get_version (*obj, &ev);
-}
+  if (live_refs)
+    {
+      leaked = g_hash_table_size (live_refs);
+    }
+  else
+    {
+      leaked = 0;
+    }
 
-long
-AccessibleApplication_getID (AccessibleApplication *obj)
-{
-  return Accessibility_Application__get_id (*obj, &ev);
-}
+#ifdef DEBUG_OBJECTS
+  if (leaked)
+    {
+      fprintf (stderr, "Leaked %d SPI handles\n", leaked);
+    }
+#endif
 
-/* Not Yet Implemented */
-boolean
-AccessibleApplication_pause (AccessibleApplication *obj)
-{
-  return FALSE;
-}
+  cspi_cleanup ();
 
-/* Not Yet Implemented */
-boolean
-AccessibleApplication_resume (AccessibleApplication *obj)
-{
-  return FALSE;
-}
+  fprintf (stderr, "bye-bye!\n");
 
+  return leaked;
+}