Added methods for Component Layer and MDI Z-Order information (see
[platform/core/uifw/at-spi2-atk.git] / cspi / spi_main.c
index fcf61d3..fba7fed 100644 (file)
-#include <libbonobo.h>
-#include <stdio.h>
-#include "spi.h"
-
-static CORBA_Environment ev;
-static AccessibilityRegistry registry;
-
-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)
-{
-  int argc = 0;
-  CORBA_Object oclient;
-  char *obj_id;
-
-  CORBA_exception_init(&ev);
-
-  if (!bonobo_init (&argc, NULL))
-    {
-      g_error ("Could not initialize Bonobo");
-    }
-
-  obj_id = "OAFIID:Accessibility_Registry:proto0.1";
-
-  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);
-  }
-
-  if (CORBA_Object_is_nil (oclient, &ev))
-    {
-      g_error ("Could not locate registry");
-      exit(-1);
-    }
-
-  registry = (Accessibility_Registry) oclient;
+static CORBA_Environment ev = { 0 };
+static Accessibility_Registry registry = CORBA_OBJECT_NIL;
+static SPIBoolean is_gnome_app = FALSE;
+static GSList *live_refs = NULL;
 
-  bonobo_activate ();
-
-  return 0;
-}
-
-void
-SPI_event_main (boolean isGNOMEApp)
+CORBA_Environment *
+cspi_ev (void)
 {
-  if (isGNOMEApp) bonobo_main();
-  else CORBA_ORB_run (bonobo_orb(), &ev);
+  /* This method is an ugly hack */
+  return &ev;
 }
 
-/* Not Yet Implemented */
-boolean
-SPI_eventIsReady ()
+Accessibility_Registry
+cspi_registry (void)
 {
-  return FALSE;
+  return registry;
 }
 
-/* Not Yet Implemented */
-AccessibleEvent *
-SPI_nextEvent (boolean waitForEvent)
+SPIBoolean
+cspi_is_gnome_app (void)
 {
-  return NULL;
+  return is_gnome_app; 
 }
 
-void
-SPI_exit (void)
+SPIBoolean
+cspi_exception (void)
 {
-  exit(0);
-}
+  SPIBoolean retval;
 
-AccessibleEventListener *
-CreateEventListener (AccessibleEventListenerCB callback)
-{
-  AccessibleEventListener *listener = accessible_event_listener_new ();
-  if (callback)
+  if (BONOBO_EX (&ev))
     {
-      accessible_event_listener_add_callback (listener, callback);
+      CORBA_exception_free (&ev);
+      retval = TRUE;
+    }
+  else
+    {
+      retval = FALSE;
     }
-  return listener;
-}
 
-boolean
-EventListener_addCallback (AccessibleEventListener *listener,
-                           AccessibleEventListenerCB callback)
-{
-  accessible_event_listener_add_callback (listener, callback);
-  return TRUE;
+  return retval;
 }
 
-boolean
-EventListener_removeCallback (AccessibleEventListener *listener,
-                           AccessibleEventListenerCB callback)
+Accessible *
+cspi_object_add (CORBA_Object corba_object)
 {
-  accessible_event_listener_remove_callback (listener, callback);
-  return TRUE;
-}
+  Accessible *ref;
 
-/*
- *
- * Global functions serviced by the registry
- *
- */
+  if (corba_object != CORBA_OBJECT_NIL)
+    {
+      ref = g_new (Accessible, 1);
 
-boolean
-RegisterGlobalEventListener (AccessibleEventListener *listener,
-                             char *eventType)
-{
-  Accessibility_Registry_registerGlobalEventListener (
-                         registry,
-                         (Accessibility_EventListener)
-                            bonobo_object_corba_objref (bonobo_object (listener)),
-                         eventType,
-                         &ev);
+      ref->objref = CORBA_Object_duplicate (corba_object, cspi_ev());
+      ref->ref_count = 1;
 
-  if (ev._major != CORBA_NO_EXCEPTION)
-    {
-    return FALSE;
+      live_refs = g_slist_prepend (live_refs, ref);
     }
   else
-    {
-      return TRUE;
-    }
-}
+   {
+     ref = NULL;
+   }
 
-int
-GetDesktopCount ()
-{
-  return Accessibility_Registry_getDesktopCount (registry, &ev);
+  return ref;
 }
 
-Accessible
-*getDesktop (int n)
-{
-  return Obj_Add (Accessibility_Registry_getDesktop (registry, (CORBA_short) n, &ev));
-}
-
-int
-getDesktopList (Accessible **list)
-{
-  *list = NULL;
-  return 0;
-}
-
-/* Not Yet Implemented */
 void
-registerKeystrokeListener (KeystrokeListener *listener)
+cspi_object_ref (Accessible *accessible)
 {
-  ;
-}
+  g_return_if_fail (accessible != NULL);
 
-/* Not Yet Implemented */
-void
-generateKeyEvent (long keyCode, long meta)
-{
-  ;
+  accessible->ref_count++;
 }
 
-/* Not Yet Implemented */
 void
-generateMouseEvent (long x, long y, char *name)
+cspi_object_unref (Accessible *accessible)
 {
-  ;
-}
+  g_return_if_fail (accessible != NULL);
+       
+  if (--accessible->ref_count == 0)
+    {
+      live_refs = g_slist_remove (live_refs, accessible);
 
-/*
- *
- * Accessible function prototypes
- *
- */
+      bonobo_object_release_unref (accessible->objref, cspi_ev ());
 
-int
-Accessible_ref (Accessible *obj)
-{
-  Accessibility_Accessible_ref (*obj, &ev);
-  return 0;
-}
+      cspi_check_ev (cspi_ev (), "unref");
 
+      memset (accessible, 0xaa, sizeof (Accessible));
 
-int
-Accessible_unref (Accessible *obj)
-{
-  Accessibility_Accessible_unref (*obj, &ev);
-  return 0;
+      g_free (accessible);
+    }
 }
 
-char *
-Accessible_getName (Accessible *obj)
+static void
+cspi_cleanup (void)
 {
-  return Accessibility_Accessible__get_name (*obj, &ev);
-}
+  GSList *l, *refs;
 
-char *
-Accessible_getDescription (Accessible *obj)
-{
-  return Accessibility_Accessible__get_description (*obj, &ev);
-}
+  refs = live_refs;
+  live_refs = NULL;
 
-Accessible *
-Accessible_getParent (Accessible *obj)
-{
-  return Obj_Add (Accessibility_Accessible__get_parent (*obj, &ev));
-}
+  for (l = refs; l; l = l->next)
+    {
+      Accessible *a = l->data;
 
-long
-Accessible_getChildCount (Accessible *obj)
-{
-  return Accessibility_Accessible__get_childCount (*obj, &ev);
-}
+      g_print ("releasing %p\n", l->data);
+      bonobo_object_release_unref (a->objref, NULL);
 
-Accessible *
-Accessible_getChildAtIndex (Accessible *obj,
-                            long childIndex)
-{
-  return Obj_Add (Accessibility_Accessible_getChildAtIndex (*obj, childIndex, &ev));
-}
+      g_free (a);
+    }
 
-long
-Accessible_getIndexInParent (Accessible *obj)
-{
-  return Accessibility_Accessible_getIndexInParent (*obj, &ev);
-}
+  g_slist_free (refs);
 
-/* Not Yet Implemented */
-AccessibleRelation **
-Accessible_getRelationSet (Accessible *obj)
-{
-  return NULL;
+  if (registry != CORBA_OBJECT_NIL)
+    {
+      bonobo_object_release_unref (registry, NULL);
+      registry = CORBA_OBJECT_NIL;
+    }
 }
 
-/* Not Yet Implemented */
-char *
-Accessible_getRole (Accessible *obj)
+/**
+ * SPI_init:
+ *
+ * Connects to the accessibility registry and initializes the SPI.
+ *
+ * Returns: 0 on success, otherwise an integer error code.
+ **/
+int
+SPI_init (void)
 {
-  return "";
-}
+  int argc = 0;
+  char *obj_id;
+  static gboolean inited = FALSE;
 
-/* Not Yet Implemented */
-AccessibleStateSet *
-Accessible_getStateSet (Accessible *obj)
-{
-  return NULL;
-}
+  if (inited)
+    {
+      return 1;
+    }
 
+  inited = TRUE;
 
-/*
- *
- * AccessibleApplication function prototypes
- *
- */
+  CORBA_exception_init (&ev);
 
-int
-AccessibleApplication_ref (AccessibleApplication *obj)
-{
-  Accessibility_Application_ref (*obj, &ev);
-  return 0;
-}
+  if (!bonobo_init (&argc, NULL))
+    {
+      g_error ("Could not initialize Bonobo");
+    }
 
-int
-AccessibleApplication_unref (AccessibleApplication *obj)
-{
-  Accessibility_Application_unref (*obj, &ev);
-  return 0;
-}
+  obj_id = "OAFIID:Accessibility_Registry:proto0.1";
 
-char *
-AccessibleApplication_getToolkitName (AccessibleApplication *obj)
-{
-  return Accessibility_Application__get_toolkitName (*obj, &ev);
+  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");
+    }
+
+  Accessibility_Registry_ref (registry, &ev);
+  
+  bonobo_activate ();
+  
+  return 0;
 }
 
-char *
-AccessibleApplication_getVersion (AccessibleApplication *obj)
+/**
+ * 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).
+ *
+ **/
+void
+SPI_event_main (SPIBoolean isGNOMEApp)
 {
-  return Accessibility_Application__get_version (*obj, &ev);
+  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");
+    }
 }
 
-long
-AccessibleApplication_getID (AccessibleApplication *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 Accessibility_Application__get_id (*obj, &ev);
+  return FALSE;
 }
 
-/* Not Yet Implemented */
-boolean
-AccessibleApplication_pause (AccessibleApplication *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 FALSE;
+  return NULL;
 }
 
-/* Not Yet Implemented */
-boolean
-AccessibleApplication_resume (AccessibleApplication *obj)
+/**
+ * SPI_exit:
+ *
+ * Disconnects from the Accessibility Registry and releases 
+ * any floating resources.
+ **/
+void
+SPI_exit (void)
 {
-  return FALSE;
+  cspi_cleanup();
+  if (cspi_is_gnome_app ())
+    {
+      bonobo_main_quit ();
+    }
+  else
+    {
+      CORBA_ORB_shutdown (bonobo_orb (), TRUE, cspi_ev ());        
+    }
+  fprintf (stderr, "bye-bye!\n");
 }