Added initial support for multiple interfaces on Accessible objects
[platform/core/uifw/at-spi2-atk.git] / libspi / registry.c
index 71999af..ff794f8 100644 (file)
@@ -1,9 +1,31 @@
 /*
+ * AT-SPI - Assistive Technology Service Provider Interface
+ * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
+ *
+ * Copyright 2001 Sun Microsystems Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+/*
  * registry.c: test for accessibility implementation
  *
  */
-#define DEBUG_PRINTSTUFF
-#ifdef DEBUG_PRINTSTUFF
+
+#ifdef SPI_DEBUG
 #include <stdio.h>
 #endif
 #include <config.h>
@@ -12,7 +34,7 @@
 /*
  * This pulls the CORBA definitions for the "Accessibility::Registry" server
  */
-#include "Registry.h"
+#include <libspi/Accessibility.h>
 
 /*
  * This pulls the definition for the BonoboObject (GType)
  */
 static ListenerClass *registry_parent_class;
 
+typedef enum {
+  ETYPE_FOCUS,
+  ETYPE_WINDOW,
+  ETYPE_TOOLKIT,
+  ETYPE_LAST_DEFINED
+} EventTypeMajor;
+
+typedef struct {
+  EventTypeMajor major;
+  char * minor;
+  char * detail;
+  guint hash;
+} EventTypeStruct;
+
+typedef struct {
+  Accessibility_EventListener listener;
+  guint event_type_hash;
+} ListenerStruct;
+
+/* static function prototypes */
+static void registry_notify_listeners ( GList *listeners,
+                                        const Accessibility_Event *e,
+                                        CORBA_Environment *ev);
+
 /*
  * Implemented GObject::finalize
  */
@@ -58,13 +104,91 @@ impl_accessibility_registry_register_application (PortableServer_Servant servant
 {
   Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
 
+#ifdef SPI_DEBUG
   fprintf (stderr, "registering app %p\n", application);
-  registry->desktop->applications = g_list_append (registry->desktop->applications, CORBA_Object_duplicate (application, ev));
+#endif
+  ORBit_register_objref (application);
+  registry->desktop->applications = g_list_append (registry->desktop->applications,
+                                                   CORBA_Object_duplicate (application, ev));
+
+  /* TODO: create unique string here (with libuuid call ?) */
+  Accessibility_Application__set_id (application, "test-some-unique-string", ev);
+
   /*
    * TODO: change the implementation below to a WM-aware one;
    * e.g. don't add all apps to the Desktop
    */
-  /*  registry->desktop->applications = registry->applications;*/
+}
+
+static gint
+compare_object_hash (gconstpointer p1, gconstpointer p2)
+{
+  CORBA_Environment ev;
+  long long diff = ((CORBA_Object_hash ((CORBA_Object) p2, (CORBA_unsigned_long) 0, &ev)) -
+                    (CORBA_Object_hash ((CORBA_Object) p1, (CORBA_unsigned_long) 0, &ev)));
+  return ((diff < 0) ? -1 : ((diff > 0) ? 1 : 0));
+}
+
+static gint
+compare_listener_hash (gconstpointer p1, gconstpointer p2)
+{
+  return (((ListenerStruct *)p2)->event_type_hash - ((ListenerStruct *)p1)->event_type_hash);
+}
+
+static void
+parse_event_type (EventTypeStruct *etype, char *event_name)
+{
+  static gunichar delimiter = 0;
+  char * major_delim_char;
+  char * minor_delim_char;
+  guint nbytes = 0;
+
+  if (!delimiter)
+    {
+      delimiter = g_utf8_get_char (":");
+    }
+
+  major_delim_char = g_utf8_strchr (event_name, (gssize) 32, delimiter);
+  minor_delim_char = g_utf8_strrchr (event_name, (gssize) 255, delimiter);
+
+  nbytes = (guint)((gint64) minor_delim_char -
+                   (gint64) major_delim_char);
+
+  fprintf (stderr, "nbytes = %ld\n", (long) nbytes);
+  if (!g_ascii_strncasecmp (event_name, "focus:", 6))
+    {
+      etype->major = ETYPE_FOCUS;
+    }
+  else if (!g_ascii_strncasecmp (event_name, "window:", 7))
+    {
+      etype->major = ETYPE_WINDOW;
+    }
+  else
+    {
+      etype->major = ETYPE_TOOLKIT;
+    }
+
+  if (major_delim_char)
+    {
+      etype->minor = g_strndup (major_delim_char, nbytes);
+      etype->hash = g_str_hash (major_delim_char);
+    }
+  else
+    {
+      etype->minor = g_strdup ("");
+      etype->hash = g_str_hash ("");
+    }
+  if (major_delim_char != minor_delim_char)
+    {
+      etype->detail = g_strdup (minor_delim_char);
+    }
+  else
+    {
+      etype->detail = g_strdup ("");
+    }
+
+
+  /* TODO: don't forget to free the strings from caller when done ! */
 }
 
 /**
@@ -82,7 +206,12 @@ impl_accessibility_registry_deregister_application (PortableServer_Servant serva
                                                     CORBA_Environment * ev)
 {
   Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
-  registry->applications = g_list_remove (registry->applications, application);
+  GList *list = g_list_find_custom (registry->applications, application, compare_object_hash);
+  if (list)
+    {
+      fprintf (stderr, "deregistering application\n");
+      registry->applications = g_list_delete_link (registry->applications, list);
+    }
 }
 
 /*
@@ -107,9 +236,46 @@ impl_accessibility_registry_register_global_event_listener
 
   Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
   /* fprintf(stderr, "registering %x/%x\n", listener, *listener); */
-  registry->listeners = g_list_append (registry->listeners, CORBA_Object_duplicate(listener, ev));
-  /* fprintf(stderr, "there are now %d listeners registered.\n", g_list_length(registry->listeners)); */
-  /* should use hashtable and CORBA_Object_hash (...) */
+  ListenerStruct *ls = g_malloc (sizeof (ListenerStruct));
+
+  EventTypeStruct etype;
+  parse_event_type (&etype, event_name);
+  ls->event_type_hash = etype.hash;
+
+  /* parse, check major event type and add listener accordingly */
+  switch (etype.major)
+    {
+    case (ETYPE_FOCUS) :
+      ls->listener = CORBA_Object_duplicate (listener, ev);
+      registry->focus_listeners =
+        g_list_append (registry->focus_listeners, ls);
+      break;
+    case (ETYPE_WINDOW) :
+      break;
+    case (ETYPE_TOOLKIT) :
+      break;
+    default:
+      break;
+    }
+}
+
+/*
+ * CORBA Accessibility::Registry::deregisterGlobalEventListenerAll method implementation
+ */
+static void
+impl_accessibility_registry_deregister_global_event_listener_all
+                                                   (PortableServer_Servant  servant,
+                                                    Accessibility_EventListener listener,
+                                                    CORBA_Environment      *ev)
+{
+  Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
+  GList *list = g_list_find_custom (registry->focus_listeners, listener, compare_object_hash);
+  while (list)
+    {
+      fprintf (stderr, "deregistering listener\n");
+      registry->focus_listeners = g_list_delete_link (registry->focus_listeners, list);
+      list = g_list_find_custom (registry->focus_listeners, listener, compare_object_hash);
+    }
 }
 
 /*
@@ -119,12 +285,22 @@ static void
 impl_accessibility_registry_deregister_global_event_listener
                                                    (PortableServer_Servant  servant,
                                                     Accessibility_EventListener listener,
+                                                    const CORBA_char * event_name,
                                                     CORBA_Environment      *ev)
 {
   Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
-  /* TODO: this won't work since 'listener' is a duplicate ref */
-  registry->listeners = g_list_remove (registry->listeners, listener);
-  /*  fprintf(stderr, "deregister\n"); */
+  ListenerStruct ls;
+  EventTypeStruct etype;
+  GList *list;
+  parse_event_type (&etype, event_name);
+  ls.event_type_hash = etype.hash;
+  list = g_list_find_custom (registry->focus_listeners, &ls, compare_listener_hash);
+
+  if (list)
+    {
+      fprintf (stderr, "deregistering listener\n");
+      registry->applications = g_list_delete_link (registry->focus_listeners, list);
+    }
 }
 
 
@@ -203,29 +379,65 @@ impl_registry_notify_event (PortableServer_Servant servant,
                             const Accessibility_Event *e,
                             CORBA_Environment *ev)
 {
-  int n;
-  int len;
   Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
+  EventTypeStruct etype;
 
-  /**
-   *  TODO:
-   *
-   *  distinguish between event types
-   *  find non-strcmp method of matching event types to listeners
-   *
-   **/
+  parse_event_type (&etype, e->type);
+
+  switch (etype.major)
+    {
+    case (ETYPE_FOCUS) :
+      registry_notify_listeners (registry->focus_listeners, e, ev);
+      break;
+    case (ETYPE_WINDOW) :
+      registry_notify_listeners (registry->window_listeners, e, ev);
+      break;
+    case (ETYPE_TOOLKIT) :
+      registry_notify_listeners (registry->toolkit_listeners, e, ev);
+      break;
+    default:
+      break;
+    }
+  Accessibility_Accessible_unref (e->target, ev);
+}
 
-  len = g_list_length (registry->listeners);
-  /* fprintf(stderr, "%d listeners registered\n", len); */
+static void
+registry_notify_listeners ( GList *listeners,
+                            const Accessibility_Event *e,
+                            CORBA_Environment *ev)
+{
+  int n;
+  int len;
+  ListenerStruct *ls;
+  EventTypeStruct etype;
+  guint minor_hash;
+  parse_event_type (&etype, e->type);
+  minor_hash = g_str_hash (etype.minor);
+  len = g_list_length (listeners);
 
   for (n=0; n<len; ++n)
     {
-      /*      fprintf(stderr, "notifying listener #%d\n", n);*/
-      /*      fprintf(stderr, "event name %s\n", Accessibility_Accessible__get_name(e->target, ev));*/
-      Accessibility_EventListener_notifyEvent (
-               (Accessibility_EventListener) g_list_nth_data (registry->listeners, n),
-               e,
-               ev);
+      ls =  (ListenerStruct *) g_list_nth_data (listeners, n);
+#ifdef SPI_DEBUG
+      fprintf(stderr, "event hashes: %lx %lx\n", ls->event_type_hash, etype.hash);
+#endif
+      if ((ls->event_type_hash == etype.hash) || (ls->event_type_hash == minor_hash))
+        {
+#ifdef SPI_DEBUG
+          fprintf(stderr, "notifying listener #%d\n", n);
+          fprintf(stderr, "event name %s\n", Accessibility_Accessible__get_name(e->target, ev));
+#endif
+          Accessibility_Accessible_ref (e->target, ev);
+          Accessibility_EventListener_notifyEvent ((Accessibility_EventListener) ls->listener,
+                                                   e,
+                                                   ev);
+          if (ev->_major != CORBA_NO_EXCEPTION) {
+                fprintf(stderr,
+                ("Accessibility app error: exception during event notification: %s\n"),
+                        CORBA_exception_id(ev));
+                exit(-1);
+          }
+        }
     }
 }
 
@@ -243,6 +455,7 @@ registry_class_init (RegistryClass *klass)
         epv->deregisterApplication = impl_accessibility_registry_deregister_application;
         epv->registerGlobalEventListener = impl_accessibility_registry_register_global_event_listener;
         epv->deregisterGlobalEventListener = impl_accessibility_registry_deregister_global_event_listener;
+        epv->deregisterGlobalEventListenerAll = impl_accessibility_registry_deregister_global_event_listener_all;
         epv->getDesktopCount = impl_accessibility_registry_get_desktop_count;
         epv->getDesktop = impl_accessibility_registry_get_desktop;
         epv->getDesktopList = impl_accessibility_registry_get_desktop_list;
@@ -253,7 +466,9 @@ registry_class_init (RegistryClass *klass)
 static void
 registry_init (Registry *registry)
 {
-  registry->listeners = NULL;
+  registry->focus_listeners = NULL;
+  registry->window_listeners = NULL;
+  registry->toolkit_listeners = NULL;
   registry->applications = NULL;
   registry->desktop = desktop_new();
 }
@@ -277,12 +492,12 @@ registry_get_type (void)
                         NULL /* value table */
                 };
                 /*
-                 *   Here we use bonobo_x_type_unique instead of
+                 *   Here we use bonobo_type_unique instead of
                  * gtk_type_unique, this auto-generates a load of
                  * CORBA structures for us. All derived types must
-                 * use bonobo_x_type_unique.
+                 * use bonobo_type_unique.
                  */
-                type = bonobo_x_type_unique (
+                type = bonobo_type_unique (
                         PARENT_TYPE,
                         POA_Accessibility_Registry__init,
                         NULL,