2008-05-16 Mark Doffman <mark.doffman@codethink.co.uk>
[platform/core/uifw/at-spi2-atk.git] / registryd / registry.c
index 4e767ad..3dc13d6 100644 (file)
@@ -2,7 +2,8 @@
  * 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
  * Boston, MA 02111-1307, USA.
  */
 
-/*
- * registry.c: test for accessibility implementation
- *
- */
+/* registry.c: the main accessibility service registry implementation */
+
+#undef SPI_LISTENER_DEBUG
+#undef SPI_DEBUG
+#undef SPI_QUEUE_DEBUG
 
+#include <config.h>
 #ifdef SPI_DEBUG
-#include <stdio.h>
+#  include <stdio.h>
 #endif
-#include <config.h>
-#include <bonobo/Bonobo.h>
 
-/*
- * This pulls the CORBA definitions for the "Accessibility::Registry" server
- */
-#include <libspi/Accessibility.h>
+#include <spi-common/spi-dbus.h>
 
-/*
- * This pulls the definition for the BonoboObject (GType)
- */
 #include "registry.h"
+#include "dbus/dbus-glib-lowlevel.h"
+
+/* Our parent GObject type  */
+#define PARENT_TYPE G_OBJECT_TYPE
+
+int _dbg = 0;
+
+typedef enum {
+  ETYPE_FOCUS,
+  ETYPE_OBJECT,
+  ETYPE_PROPERTY,
+  ETYPE_WINDOW,
+  ETYPE_TOOLKIT,
+  ETYPE_KEYBOARD,
+  ETYPE_MOUSE,
+  ETYPE_LAST_DEFINED
+} EventTypeCategory;
+
+typedef struct {
+  const char *event_name;
+  EventTypeCategory type_cat;
+  GQuark major;  /* from string segment[1] */
+  GQuark minor;  /* from string segment[1]+segment[2] */
+  GQuark detail; /* from string segment[3] (not concatenated) */
+} EventTypeStruct;
+
+G_DEFINE_TYPE(SpiRegistry, spi_registry, G_TYPE_OBJECT)
 
-/*
- * Our parent GObject type
- */
-#define PARENT_TYPE LISTENER_TYPE
+static void
+spi_registry_set_debug (const char *debug_flag_string)
+{
+  if (debug_flag_string) 
+    _dbg = (int) g_ascii_strtod (debug_flag_string, NULL);
+}
 
-/*
- * A pointer to our parent object class
- */
-static ListenerClass *registry_parent_class;
+static void emit(SpiRegistry *registry, const char *name, int first_type, ...)
+{
+  va_list arg;
+
+  va_start(arg, first_type);
+  spi_dbus_emit_valist(registry->droute.bus, SPI_DBUS_PATH_REGISTRY, SPI_DBUS_INTERFACE_REGISTRY, name, first_type, arg);
+  va_end(arg);
+}
 
-/*
- * Implemented GObject::finalize
- */
 static void
-registry_object_finalize (GObject *object)
+desktop_add_application (SpiDesktop *desktop,
+                        guint index, gpointer data)
 {
-/*        Registry *registry = REGISTRY (object); */
-        GObjectClass *object_class = G_OBJECT_GET_CLASS( object);
+  SpiRegistry *registry = SPI_REGISTRY (data);
+  const char *name = g_list_nth_data(desktop->applications, index);
+  
+  emit(registry, "ApplicationAdd", DBUS_TYPE_UINT32, &index, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
+}
+
 
-        printf("registry_object_finalize called\n");
 
-        object_class->finalize (object);
+static void
+desktop_remove_application (SpiDesktop *desktop,
+                           guint index, gpointer data)
+{
+  SpiRegistry *registry = SPI_REGISTRY (data);
+  const char *name = g_list_nth_data(desktop->applications, index);
+  
+  emit(registry, "ApplicationRemove", DBUS_TYPE_UINT32, &index, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
+}
+
+
+static void
+spi_registry_object_finalize (GObject *object)
+{
+  DBG (1, g_warning ("spi_registry_object_finalize called\n"));
+  g_object_unref (SPI_REGISTRY (object)->de_controller);
+
+  G_OBJECT_CLASS (spi_registry_parent_class)->finalize (object);
 }
 
 /**
@@ -73,23 +119,22 @@ registry_object_finalize (GObject *object)
  * Register a new application with the accessibility broker.
  *
  **/
-static void
-impl_accessibility_registry_register_application (PortableServer_Servant servant,
-                                                  const Accessibility_Application application,
-                                                  CORBA_Environment * ev)
+static DBusMessage *
+impl_accessibility_registry_register_application (DBusConnection *bus, DBusMessage *message, void *user_data)
 {
-  Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
+  SpiRegistry *registry = SPI_REGISTRY (user_data);
+  const char *application = dbus_message_get_sender (message);
 
 #ifdef SPI_DEBUG
-  fprintf (stderr, "registering app %p\n", application);
+  fprintf (stderr, "registering app %s\n", application);
 #endif
+  spi_desktop_add_application (registry->desktop, application);
 
-  registry->desktop->applications = g_list_append (registry->desktop->applications, CORBA_Object_duplicate (application, ev));
   /*
    * TODO: change the implementation below to a WM-aware one;
-   * e.g. don't add all apps to the Desktop
+   * e.g. don't add all apps to the SpiDesktop
    */
-  /*  registry->desktop->applications = registry->applications;*/
+  return dbus_message_new_method_return (message);
 }
 
 /**
@@ -101,58 +146,20 @@ impl_accessibility_registry_register_application (PortableServer_Servant servant
  * De-register an application previously registered with the broker.
  *
  **/
-static void
-impl_accessibility_registry_deregister_application (PortableServer_Servant servant,
-                                                    const Accessibility_Application application,
-                                                    CORBA_Environment * ev)
+static DBusMessage *
+impl_accessibility_registry_deregister_application (DBusConnection *bus, DBusMessage *message, void *user_data)
 {
-  Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
-  registry->applications = g_list_remove (registry->applications, application);
-}
+  SpiRegistry *registry = SPI_REGISTRY (user_data);
+  const char *application = dbus_message_get_sender (message);
 
-/*
- * CORBA Accessibility::Registry::registerGlobalEventListener method implementation
- */
-static void
-impl_accessibility_registry_register_global_event_listener
-                                            (PortableServer_Servant  servant,
-                                             Accessibility_EventListener listener,
-                                             const CORBA_char *event_name,
-                                             CORBA_Environment      *ev)
-{
-  /**
-   *  TODO:
-   *
-   *  distinguish between event types
-   *  register with app toolkits only for requested event types
-   *  maintain list of requested types and number of listeners
-   *  find non-strcmp method of matching event types to listeners
-   *
-   **/
-
-  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 (...) */
-}
+  spi_desktop_remove_application (registry->desktop, application);
 
-/*
- * CORBA Accessibility::Registry::deregisterGlobalEventListener method implementation
- */
-static void
-impl_accessibility_registry_deregister_global_event_listener
-                                                   (PortableServer_Servant  servant,
-                                                    Accessibility_EventListener listener,
-                                                    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"); */
+#ifdef SPI_DEBUG
+  fprintf (stderr, "de-registered app %s\n", application);
+#endif
+  return dbus_message_new_method_return (message);
 }
 
-
 /**
  * getDesktopCount:
  * return values: a short integer indicating the current number of
@@ -161,14 +168,19 @@ impl_accessibility_registry_deregister_global_event_listener
  * Get the current number of desktops.
  *
  **/
-static short
-impl_accessibility_registry_get_desktop_count (PortableServer_Servant servant,
-                                               CORBA_Environment * ev)
+static DBusMessage *
+impl_accessibility_registry_get_desktop_count (DBusConnection *bus, DBusMessage *message, void *user_data)
 {
+  dbus_int16_t n_desktops = 1;
+  DBusMessage *reply;
+
   /* TODO: implement support for multiple virtual desktops */
-  CORBA_short n_desktops;
-  n_desktops = (CORBA_short) 1;
-  return n_desktops;
+  reply = dbus_message_new_method_return (message);
+  if (reply)
+  {
+    dbus_message_append_args (reply, DBUS_TYPE_INT16, &n_desktops, DBUS_TYPE_INVALID);
+  }
+  return reply;
 }
 
 /**
@@ -179,26 +191,30 @@ impl_accessibility_registry_get_desktop_count (PortableServer_Servant servant,
  * Get the nth accessible desktop.
  *
  **/
-static Accessibility_Desktop
-impl_accessibility_registry_get_desktop (PortableServer_Servant servant,
-                                         const CORBA_short n,
-                                         CORBA_Environment * ev)
+static DBusMessage *
+impl_accessibility_registry_get_desktop (DBusConnection *bus, DBusMessage *message, void *user_data)
 {
-  Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
+  DBusError error;
+  dbus_int16_t n;
+  const char *path;
+  DBusMessage *reply;
 
   /* TODO: implement support for multiple virtual desktops */
-  if (n == 0)
-    {
-      return (Accessibility_Desktop)
-        CORBA_Object_duplicate (
-             bonobo_object_corba_objref (bonobo_object (registry->desktop)), ev);
-    }
-  else
-    {
-      return (Accessibility_Desktop) CORBA_OBJECT_NIL;
-    }
+  dbus_error_init (&error);
+  if (!dbus_message_get_args (message, &error, DBUS_TYPE_INT16, &n, DBUS_TYPE_INVALID))
+  {
+    return spi_dbus_general_error (message);
+  }
+  path = (n == 0? SPI_DBUS_PATH_DESKTOP: "/");
+  reply = dbus_message_new_method_return (message);
+  if (reply)
+  {
+    dbus_message_append_args (reply, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID);
+  }
+  return reply;
 }
 
+
 /**
  * getDesktopList:
  * return values: a sequence containing references to
@@ -207,124 +223,128 @@ impl_accessibility_registry_get_desktop (PortableServer_Servant servant,
  * Get a list of accessible desktops.
  *
  **/
-static Accessibility_DesktopSeq *
-impl_accessibility_registry_get_desktop_list (PortableServer_Servant servant,
-                                              CORBA_Environment * ev)
+static DBusMessage *
+impl_accessibility_registry_get_desktop_list (DBusConnection *bus, DBusMessage *message, void *user_data)
 {
-  /* TODO: implement support for multiple virtual desktops */
-  return (Accessibility_DesktopSeq *) NULL;
+  DBusMessage *reply;
+  DBusMessageIter iter, iter_array;
+  const char *path = SPI_DBUS_PATH_DESKTOP;
+
+  reply = dbus_message_new_method_return (message);
+  if (!reply) return NULL;
+  dbus_message_iter_init_append(reply, &iter);
+  if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "o", &iter_array)) goto oom;
+  dbus_message_iter_append_basic(&iter_array, DBUS_TYPE_STRING, &path);
+  if (!dbus_message_iter_close_container (&iter, &iter_array)) goto oom;
+  return reply;
+oom:
+  // TODO: handle out-of-memory
+  return reply;
 }
 
-static CORBA_Object
-impl_accessibility_registry_get_device_event_controller (PortableServer_Servant servant,
-                                                         CORBA_Environment * ev)
+
+static DBusMessage *
+impl_accessibility_registry_get_device_event_controller (DBusConnection *bus, DBusMessage *message, void *user_data)
 {
-  /* TODO: not yet implemented! */
-  return CORBA_OBJECT_NIL;
+  DBusMessage *reply;
+  const char *path = SPI_DBUS_PATH_DEC;
+
+  reply = dbus_message_new_method_return (message);
+  if (reply)
+  {
+    dbus_message_append_args (reply, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID);
+  }
+  return reply;
 }
 
+
 static void
-impl_registry_notify_event (PortableServer_Servant servant,
-                            const Accessibility_Event *e,
-                            CORBA_Environment *ev)
+spi_registry_class_init (SpiRegistryClass *klass)
 {
-  int n;
-  int len;
-  Registry *registry = REGISTRY (bonobo_object_from_servant (servant));
-
-  /**
-   *  TODO:
-   *
-   *  distinguish between event types
-   *  find non-strcmp method of matching event types to listeners
-   *
-   **/
-
-  len = g_list_length (registry->listeners);
-  /* fprintf(stderr, "%d listeners registered\n", len); */
-
-  for (n=0; n<len; ++n)
-    {
-#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_EventListener_notifyEvent (
-               (Accessibility_EventListener) g_list_nth_data (registry->listeners, n),
-               e,
-               ev);
-    }
+  GObjectClass * object_class = (GObjectClass *) klass;
+
+  spi_registry_parent_class = g_type_class_ref (G_TYPE_OBJECT);
+  
+  object_class->finalize = spi_registry_object_finalize;
 }
 
-static void
-registry_class_init (RegistryClass *klass)
+static DBusObjectPathVTable droute_vtable =
 {
-        GObjectClass * object_class = (GObjectClass *) klass;
-        POA_Accessibility_Registry__epv *epv = &klass->epv;
-
-        registry_parent_class = g_type_class_ref (LISTENER_TYPE);
-
-        object_class->finalize = registry_object_finalize;
-
-        epv->registerApplication = impl_accessibility_registry_register_application;
-        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->getDesktopCount = impl_accessibility_registry_get_desktop_count;
-        epv->getDesktop = impl_accessibility_registry_get_desktop;
-        epv->getDesktopList = impl_accessibility_registry_get_desktop_list;
-        epv->getDeviceEventController = impl_accessibility_registry_get_device_event_controller;
-        ((ListenerClass *) klass)->epv.notifyEvent = impl_registry_notify_event;
-}
+  NULL,
+  &droute_message,
+  NULL, NULL, NULL, NULL
+};
 
 static void
-registry_init (Registry *registry)
+spi_registry_init (SpiRegistry *registry)
 {
-  registry->listeners = NULL;
-  registry->applications = NULL;
-  registry->desktop = desktop_new();
+  DBusError error;
+
+  spi_registry_set_debug (g_getenv ("AT_SPI_DEBUG"));
+  /*
+   * TODO: FIXME, this module makes the foolish assumptions that
+   * registryd uses the same display as the apps, and that the
+   * DISPLAY environment variable is set.
+   */
+  gdk_init (NULL, NULL);
+
+  registry->exit_notify_timeout = 200;
+  registry->queue_handler_id  = 0;
+
+  dbus_error_init (&error);
+  registry->droute.bus = dbus_bus_get(DBUS_BUS_SESSION, &error);
+  if (!registry->droute.bus)
+  {
+    g_warning("Couldn't connect to dbus: %s\n", error.message);
+    return;
+  }
+  spi_registry_initialize_registry_interface (&registry->droute);
+  spi_registry_initialize_desktop_interface (&registry->droute);
+  spi_registry_initialize_dec_interface (&registry->droute);
+  // todo: initialize accessible and component interfaces, for desktop?
+  if (!dbus_connection_try_register_fallback (registry->droute.bus, "/org/freedesktop/atspi", &droute_vtable, &registry->droute, &error))
+  {
+    g_warning("Couldn't register droute.\n");
+  }
+  dbus_connection_setup_with_g_main(registry->droute.bus, g_main_context_default());
+
+  // TODO: decide whether focus_object is still relevant
+  registry->desktop = spi_desktop_new ();
+  /* Register callback notification for application addition and removal */
+  g_signal_connect (G_OBJECT (registry->desktop),
+                   "application_added",
+                   G_CALLBACK (desktop_add_application),
+                   registry);
+
+  g_signal_connect (G_OBJECT (registry->desktop),
+                   "application_removed",
+                   G_CALLBACK (desktop_remove_application),
+                   registry);
+
+  registry->de_controller = spi_device_event_controller_new (registry);
 }
 
-GType
-registry_get_type (void)
+SpiRegistry *
+spi_registry_new (void)
 {
-        static GType type = 0;
-
-        if (!type) {
-                static const GTypeInfo tinfo = {
-                        sizeof (RegistryClass),
-                        (GBaseInitFunc) NULL,
-                        (GBaseFinalizeFunc) NULL,
-                        (GClassInitFunc) registry_class_init,
-                        (GClassFinalizeFunc) NULL,
-                        NULL, /* class data */
-                        sizeof (Registry),
-                        0, /* n preallocs */
-                        (GInstanceInitFunc) registry_init,
-                        NULL /* value table */
-                };
-                /*
-                 *   Here we use bonobo_x_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.
-                 */
-                type = bonobo_type_unique (
-                        PARENT_TYPE,
-                        POA_Accessibility_Registry__init,
-                        NULL,
-                        G_STRUCT_OFFSET (RegistryClass, epv),
-                        &tinfo,
-                        "Registry");
-        }
-
-        return type;
+  SpiRegistry *retval = g_object_new (SPI_REGISTRY_TYPE, NULL);
+  return retval;
 }
 
-Registry *
-registry_new (void)
+static DRouteMethod methods[] =
 {
-    Registry *retval =
-               REGISTRY (g_object_new (registry_get_type (), NULL));
-    return retval;
-}
+  { impl_accessibility_registry_register_application , "registerApplication" },
+  { impl_accessibility_registry_deregister_application, "deregisterApplication" },
+  { impl_accessibility_registry_get_desktop_count, "getDesktopCount" },
+  { impl_accessibility_registry_get_desktop, "getDesktop" },
+  { impl_accessibility_registry_get_desktop_list, "getDesktopList" },
+  { impl_accessibility_registry_get_device_event_controller, "getDeviceEventController" },
+  { NULL, NULL }
+};
+
+void
+spi_registry_initialize_registry_interface (DRouteData * data)
+{
+  droute_add_interface (data, "org.freedesktop.atspi.Registry", methods,
+                       NULL, NULL, NULL);
+};