autoconf: include missing meson build files
[platform/upstream/at-spi2-core.git] / registryd / registry-main.c
index b1b5cde..d8b5dc5 100644 (file)
 #include <config.h>
 #include <string.h>
 #include <glib.h>
+#include <gio/gio.h>
 #include <stdio.h>
 #include <dlfcn.h>
 
 #include <dbus/dbus.h>
-#include <dbus/dbus-glib.h>
 
 #include "paths.h"
 #include "registry.h"
 #include "deviceeventcontroller.h"
-
-#define CORBA_GCONF_KEY  "/desktop/gnome/interface/at-spi-corba"
-
-static gboolean need_to_quit ();
+#include "atspi/atspi.h"
 
 static GMainLoop *mainloop;
 static gchar *dbus_name = NULL;
@@ -50,10 +47,9 @@ static GOptionEntry optentries[] =
   {NULL}
 };
 
-static DBusGConnection *bus_connection = NULL;
-static DBusGProxy      *sm_proxy = NULL;
+static GDBusProxy      *sm_proxy = NULL;
 static char            *client_id = NULL;
-static DBusGProxy      *client_proxy = NULL;
+static GDBusProxy      *client_proxy = NULL;
 
 #define SM_DBUS_NAME      "org.gnome.SessionManager"
 #define SM_DBUS_PATH      "/org/gnome/SessionManager"
@@ -61,113 +57,138 @@ static DBusGProxy      *client_proxy = NULL;
 
 #define SM_CLIENT_DBUS_INTERFACE "org.gnome.SessionManager.ClientPrivate"
 
-static void registry_session_init (const char *previous_client_id, const char *exe);
+static gboolean register_client (void);
+
+static void
+on_session_signal (GDBusProxy *proxy,
+                   gchar      *sender_name,
+                   gchar      *signal_name,
+                   GVariant   *parameters,
+                   gpointer    user_data)
+{
+        if (g_strcmp0 (signal_name, "SessionOver") == 0) {
+                g_main_loop_quit (mainloop);
+        } else if (g_strcmp0 (signal_name, "SessionRunning") == 0) {
+                if (!register_client ())
+                        g_warning ("Unable to register client with session manager");
+        }
+}
 
 static gboolean
 session_manager_connect (void)
 {
+        GVariant *res;
+        gboolean is_running;
 
-        if (bus_connection == NULL) {
-                GError *error;
-
-                error = NULL;
-                bus_connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
-                if (bus_connection == NULL) {
-                        g_message ("Failed to connect to the session bus: %s",
-                                   error->message);
-                        g_error_free (error);
-                        exit (1);
+        sm_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION, 0, NULL,
+                                              SM_DBUS_NAME,
+                                              SM_DBUS_PATH,
+                                              SM_DBUS_INTERFACE, NULL, NULL);
+
+        res = g_dbus_proxy_call_sync (sm_proxy,
+                                 "IsSessionRunning", NULL,
+                                  0, 1000, NULL, NULL);
+
+        if (res) {
+                g_variant_get (res, "(b)", &is_running);
+                g_variant_unref (res);
+                if (is_running) {
+                        if (!register_client ())
+                                g_warning ("Unable to register client with session manager");
                 }
         }
 
-        sm_proxy = dbus_g_proxy_new_for_name (bus_connection,
-                                              SM_DBUS_NAME,
-                                              SM_DBUS_PATH,
-                                              SM_DBUS_INTERFACE);
-        return (sm_proxy != NULL);
-}
+        g_signal_connect (G_OBJECT (sm_proxy), "g-signal",
+                          G_CALLBACK (on_session_signal), NULL);
 
-static void
-stop_cb (gpointer data)
-{
-        g_main_loop_quit (mainloop);
+        return (sm_proxy != NULL);
 }
 
 static gboolean
 end_session_response (gboolean is_okay, const gchar *reason)
 {
-        gboolean ret;
+        GVariant *ret;
         GError *error = NULL;
 
-        ret = dbus_g_proxy_call (client_proxy, "EndSessionResponse",
-                                 &error,
-                                 G_TYPE_BOOLEAN, is_okay,
-                                 G_TYPE_STRING, reason,
-                                 G_TYPE_INVALID,
-                                 G_TYPE_INVALID);
+        if (!reason)
+                reason = "";
+
+        ret = g_dbus_proxy_call_sync (client_proxy, "EndSessionResponse",
+                                      g_variant_new ("(bs)", is_okay, reason),
+                                      0, 1000, NULL, &error);
 
         if (!ret) {
                 g_warning ("Failed to send session response %s", error->message);
                 g_error_free (error);
+                return FALSE;
         }
+        else
+                g_variant_unref (ret);
 
-        return ret;
+        return TRUE;
 }
 
 static void
-query_end_session_cb (guint flags, gpointer data)
+client_proxy_signal_cb (GDBusProxy *proxy,
+                        gchar *sender_name,
+                        gchar *signal_name,
+                        GVariant *parameters,
+                        gpointer user_data)
 {
-        end_session_response (TRUE, NULL);
+        if (g_strcmp0 (signal_name, "QueryEndSession") == 0) {
+                g_debug ("Got QueryEndSession signal");
+                end_session_response (TRUE, NULL);
+        } else if (g_strcmp0 (signal_name, "EndSession") == 0) {
+                g_debug ("Got EndSession signal");
+                end_session_response (TRUE, NULL);
+                g_main_loop_quit (mainloop);
+        } else if (g_strcmp0 (signal_name, "Stop") == 0) {
+                g_debug ("Got Stop signal");
+                g_main_loop_quit (mainloop);
+        }
 }
 
-static void
-end_session_cb (guint flags, gpointer data)
-{
-        end_session_response (TRUE, NULL);
-        g_main_loop_quit (mainloop);
-}
 static gboolean
 register_client (void)
 {
         GError     *error;
-        gboolean    res;
+        GVariant *res;
         const char *startup_id;
         const char *app_id;
 
+        if (client_proxy)
+                return TRUE;
+
         startup_id = g_getenv ("DESKTOP_AUTOSTART_ID");
+        if (!startup_id)
+                startup_id = "";
         app_id = "at-spi-registryd.desktop";
 
         error = NULL;
-        res = dbus_g_proxy_call (sm_proxy,
+        res = g_dbus_proxy_call_sync (sm_proxy,
                                  "RegisterClient",
-                                 &error,
-                                 G_TYPE_STRING, app_id,
-                                 G_TYPE_STRING, startup_id,
-                                 G_TYPE_INVALID,
-                                 DBUS_TYPE_G_OBJECT_PATH, &client_id,
-                                 G_TYPE_INVALID);
+                                      g_variant_new ("(ss)", app_id,
+                                                     startup_id),
+                                      0, 1000, NULL, &error);
         if (! res) {
-                g_warning ("Failed to register client: %s", error->message);
-                g_error_free (error);
+                const char *message = (error && error->message ? error->message
+                                       : "no error");
+                g_warning ("Failed to register client: %s", message);
+                if (error)
+                  g_error_free (error);
                 return FALSE;
         }
+        g_variant_get (res, "(o)", &client_id);
+        g_variant_unref (res);
 
-        client_proxy = dbus_g_proxy_new_for_name (bus_connection,
+        client_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION, 0, NULL,
                                                   SM_DBUS_NAME,
                                                   client_id,
-                                                  SM_CLIENT_DBUS_INTERFACE);
-
-        dbus_g_proxy_add_signal (client_proxy, "Stop", G_TYPE_INVALID);
-        dbus_g_proxy_connect_signal (client_proxy, "Stop",
-                                     G_CALLBACK (stop_cb), NULL, NULL);
+                                                  SM_CLIENT_DBUS_INTERFACE,
+                                                  NULL, NULL);
 
-        dbus_g_proxy_add_signal (client_proxy, "QueryEndSession", G_TYPE_UINT, G_TYPE_INVALID);
-        dbus_g_proxy_connect_signal (client_proxy, "QueryEndSession",
-                                     G_CALLBACK (query_end_session_cb), NULL, NULL);
-
-        dbus_g_proxy_add_signal (client_proxy, "EndSession", G_TYPE_UINT, G_TYPE_INVALID);
-        dbus_g_proxy_connect_signal (client_proxy, "EndSession",
-                                     G_CALLBACK (end_session_cb), NULL, NULL);
+        g_signal_connect (client_proxy, "g-signal",
+                          G_CALLBACK (client_proxy_signal_cb), NULL);
 
         g_unsetenv ("DESKTOP_AUTOSTART_ID");
 
@@ -176,105 +197,6 @@ register_client (void)
 
 /*---------------------------------------------------------------------------*/
 
-/*
- * 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;
-}
 
 /*---------------------------------------------------------------------------*/
 
@@ -292,39 +214,34 @@ main (int argc, char **argv)
   GOptionContext *opt;
 
   GError *err = NULL;
-  DBusError error;
   int ret;
 
-  if (need_to_quit ())
-    return 0;
-
-  g_type_init();
-
   /*Parse command options*/
   opt = g_option_context_new(NULL);
   g_option_context_add_main_entries(opt, optentries, NULL);
 
   if (!g_option_context_parse(opt, &argc, &argv, &err))
+    {
       g_error("Option parsing failed: %s\n", err->message);
+      g_clear_error (&err);
+    }
 
   if (dbus_name == NULL)
       dbus_name = SPI_DBUS_NAME_REGISTRY;
 
-  dbus_error_init (&error);
-  bus = dbus_bus_get(DBUS_BUS_SESSION, &error);
-  bus = spi_get_bus ();
+  bus = atspi_get_a11y_bus ();
   if (!bus)
   {
     return 0;
   }
 
   mainloop = g_main_loop_new (NULL, FALSE);
-  dbus_connection_setup_with_g_main(bus, NULL);
+  atspi_dbus_connection_setup_with_g_main(bus, NULL);
 
-  ret = dbus_bus_request_name(bus, dbus_name, DBUS_NAME_FLAG_DO_NOT_QUEUE, &error);
+  ret = dbus_bus_request_name(bus, dbus_name, DBUS_NAME_FLAG_DO_NOT_QUEUE, NULL);
   if (ret == DBUS_REQUEST_NAME_REPLY_EXISTS)
     {
-      g_error("Could not obtain D-Bus name - %s\n", dbus_name);
+      exit (0);        /* most likely already running */
     }
   else
     {
@@ -338,47 +255,14 @@ main (int argc, char **argv)
     {
       if (!session_manager_connect ())
           g_warning ("Unable to connect to 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;
+  dbus_connection_close (bus);
+  dbus_connection_unref (bus);
+  g_object_unref (dec);
+  g_object_unref (registry);
 
-  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;
+  return 0;
 }