Fix missing NULL argument to g_strconcat.
[platform/upstream/at-spi2-core.git] / registryd / registry-main.c
index 52fd9b2..083de19 100644 (file)
@@ -25,6 +25,8 @@
 #include <config.h>
 #include <string.h>
 #include <glib.h>
+#include <stdio.h>
+#include <dlfcn.h>
 
 #include <dbus/dbus.h>
 #include <dbus/dbus-glib.h>
 #include "registry.h"
 #include "deviceeventcontroller.h"
 
-#if !defined ATSPI_INTROSPECTION_PATH
-    #error "No introspection XML directory defined"
-#endif
+#define CORBA_GCONF_KEY  "/desktop/gnome/interface/at-spi-corba"
+
+static gboolean need_to_quit ();
 
 static GMainLoop *mainloop;
 static gchar *dbus_name = NULL;
-
-static DBusGConnection *bus_connection = NULL;
-static DBusGProxy      *sm_proxy = NULL;
-static char            *client_id = NULL;
-static DBusGProxy      *client_proxy = NULL;
+static gboolean use_gnome_session = FALSE;
 
 static GOptionEntry optentries[] =
 {
   {"dbus-name", 0, 0, G_OPTION_ARG_STRING, &dbus_name, "Well-known name to register with D-Bus", NULL},
+  {"use-gnome-session", 0, 0, G_OPTION_ARG_NONE, &use_gnome_session, "Should register with gnome session manager", NULL},
   {NULL}
 };
 
+static DBusGConnection *bus_connection = NULL;
+static DBusGProxy      *sm_proxy = NULL;
+static char            *client_id = NULL;
+static DBusGProxy      *client_proxy = NULL;
+
 #define SM_DBUS_NAME      "org.gnome.SessionManager"
 #define SM_DBUS_PATH      "/org/gnome/SessionManager"
 #define SM_DBUS_INTERFACE "org.gnome.SessionManager"
@@ -84,8 +88,36 @@ session_manager_connect (void)
 }
 
 static void
+kill_accessibility_bus ()
+{
+  FILE *fp;
+  const char *home;
+  char *name;
+  int pid;
+
+  home = getenv ("HOME");
+  if (!home)
+    return;
+  name = g_strconcat (home, "/", ".atspi-dbus-bus.pid", NULL);
+  if (!name)
+    return;
+
+  fp = fopen (name, "r");
+  if (fp)
+  {
+    if (fscanf (fp, "%d", &pid) == 1)
+    {
+      kill (&pid, SIGTERM);
+    }
+    fclose (fp);
+  }
+  g_free (name);
+}
+
+static void
 stop_cb (gpointer data)
 {
+        kill_accessibility_bus ();
         g_main_loop_quit (mainloop);
 }
 
@@ -119,6 +151,7 @@ query_end_session_cb (guint flags, gpointer data)
 static void
 end_session_cb (guint flags, gpointer data)
 {
+        kill_accessibility_bus ();
         end_session_response (TRUE, NULL);
         g_main_loop_quit (mainloop);
 }
@@ -170,14 +203,120 @@ register_client (void)
         return TRUE;
 }
 
+/*---------------------------------------------------------------------------*/
+
+/*
+ * Returns a 'canonicalized' value for DISPLAY,
+ * with the screen number stripped off if present.
+ *
+ */
+static const gchar*
+spi_display_name (void)
+{
+    static const char *canonical_display_name = NULL;
+    if (!canonical_display_name)
+      {
+        const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
+        if (!display_env)
+          {
+            display_env = g_getenv ("DISPLAY");
+            if (!display_env || !display_env[0]) 
+                canonical_display_name = ":0";
+            else
+              {
+                gchar *display_p, *screen_p;
+                canonical_display_name = g_strdup (display_env);
+                display_p = strrchr (canonical_display_name, ':');
+                screen_p = strrchr (canonical_display_name, '.');
+                if (screen_p && display_p && (screen_p > display_p))
+                  {
+                    *screen_p = '\0';
+                  }
+              }
+          }
+        else
+          {
+            canonical_display_name = display_env;
+          }
+      }
+    return canonical_display_name;
+}
+
+/*---------------------------------------------------------------------------*/
+
+/*
+ * Gets the IOR from the XDisplay.
+ * Not currently used in D-Bus version, but something similar
+ * may be employed in the future for accessing the registry daemon
+ * bus name.
+ */
+
+static DBusConnection *
+spi_get_bus (void)
+{
+     Atom AT_SPI_BUS;
+     Atom actual_type;
+     Display *bridge_display;
+     int actual_format;
+     unsigned char *data = NULL;  
+     unsigned long nitems;
+     unsigned long leftover;
+
+     DBusConnection *bus = NULL;
+     DBusError       error;
+
+     bridge_display = XOpenDisplay (spi_display_name ());
+     if (!bridge_display)
+       g_error ("AT_SPI: Could not get the display");
+
+     AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", FALSE); 
+     XGetWindowProperty(bridge_display, 
+                        XDefaultRootWindow (bridge_display),
+                        AT_SPI_BUS, 0L,
+                        (long)BUFSIZ, False,
+                        (Atom) 31, &actual_type, &actual_format,
+                        &nitems, &leftover, &data);
+
+     dbus_error_init (&error);
+
+     if (data == NULL)
+     {
+         g_warning ("AT-SPI: Accessibility bus bus not found - Using session bus.\n");
+         bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
+         if (!bus)
+             g_error ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
+     }
+     else
+     {
+        bus = dbus_connection_open (data, &error);
+        XFree (data);
+         if (!bus)
+         {
+             g_error ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
+         }
+        else
+         {
+            if (!dbus_bus_register (bus, &error))
+                g_error ("AT-SPI: Couldn't register with bus: %s\n", error.message);
+         } 
+     }
+
+  XCloseDisplay (bridge_display);
+     return bus;
+}
+
+/*---------------------------------------------------------------------------*/
+
+typedef GObject *(*gconf_client_get_default_t) ();
+typedef gboolean (*gconf_client_get_bool_t)(GObject *, const char *, void *);
+
 int
 main (int argc, char **argv)
 {
   SpiRegistry *registry;
   SpiDEController *dec;
-  gchar *introspection_directory;
 
-  DBusConnection *bus;
+  DBusConnection *bus = NULL;
 
   GOptionContext *opt;
 
@@ -185,10 +324,10 @@ main (int argc, char **argv)
   DBusError error;
   int ret;
 
-  g_type_init();
+  if (need_to_quit ())
+    return 0;
 
-  /* We depend on GDK as well as XLib for device event processing */
-  gdk_init(&argc, &argv);
+  g_type_init();
 
   /*Parse command options*/
   opt = g_option_context_new(NULL);
@@ -202,13 +341,14 @@ main (int argc, char **argv)
 
   dbus_error_init (&error);
   bus = dbus_bus_get(DBUS_BUS_SESSION, &error);
+  bus = spi_get_bus ();
   if (!bus)
   {
-    g_warning("Couldn't connect to dbus: %s\n", error.message);
+    return 0;
   }
 
   mainloop = g_main_loop_new (NULL, FALSE);
-  dbus_connection_setup_with_g_main(bus, g_main_context_default());
+  dbus_connection_setup_with_g_main(bus, NULL);
 
   ret = dbus_bus_request_name(bus, dbus_name, DBUS_NAME_FLAG_DO_NOT_QUEUE, &error);
   if (ret == DBUS_REQUEST_NAME_REPLY_EXISTS)
@@ -220,24 +360,54 @@ main (int argc, char **argv)
       g_print ("SpiRegistry daemon is running with well-known name - %s\n", dbus_name);
     }
 
-  /* Get D-Bus introspection directory */
-  introspection_directory = (char *) g_getenv("ATSPI_INTROSPECTION_PATH");
-  if (introspection_directory == NULL)
-      introspection_directory = ATSPI_INTROSPECTION_PATH;
-
   registry = spi_registry_new (bus);
   dec = spi_registry_dec_new (registry, bus);
 
-  if (!session_manager_connect ())
+  if (use_gnome_session)
     {
-      g_warning ("Unable to connect to session manager");
-    }     
+      if (!session_manager_connect ())
+          g_warning ("Unable to connect to session manager");
 
-  if (!register_client ())
-    {
-      g_warning ("Unable to register client with session manager");
+      if (!register_client ())
+          g_warning ("Unable to register client with session manager");
     }
 
   g_main_loop_run (mainloop);
   return 0;
 }
+
+static gboolean
+need_to_quit ()
+{
+  void *gconf = NULL;
+  gconf_client_get_default_t gconf_client_get_default = NULL;
+  gconf_client_get_bool_t gconf_client_get_bool = NULL;
+  GObject *gconf_client;       /* really a GConfClient */
+  gboolean ret;
+
+  g_type_init ();
+
+  gconf = dlopen ("libgconf-2.so", RTLD_LAZY);
+  if (gconf)
+    {
+      gconf_client_get_default = dlsym (gconf, "gconf_client_get_default");
+      gconf_client_get_bool = dlsym (gconf, "gconf_client_get_bool");
+  }
+
+  if (!gconf_client_get_default || !gconf_client_get_bool)
+    {
+      if (gconf)
+        dlclose (gconf);
+      return FALSE;
+    }
+
+  /* If we've been relocated, we will exit if the at-spi-corba gconf key
+ * has been set.  If we have not been relocated, we will only run if the
+ * at-spi-dbus gconf key has been set.
+   */
+  gconf_client = gconf_client_get_default ();
+  ret = gconf_client_get_bool (gconf_client, CORBA_GCONF_KEY, NULL);
+  g_object_unref (gconf_client);
+
+  return ret;
+}