int
main (int    argc,
      char **argv)
{
    GMainLoop        *main_loop;
    GOptionContext   *context;
    DBusGConnection  *connection;
    MdmSlave         *slave;
    static char      *display_id = NULL;
    MdmSignalHandler *signal_handler;
    static GOptionEntry entries []   = {
        { "display-id", 0, 0, G_OPTION_ARG_STRING, &display_id, N_("Display ID"), N_("ID") },
        { NULL }
    };

    bindtextdomain (GETTEXT_PACKAGE, MATELOCALEDIR);
    textdomain (GETTEXT_PACKAGE);
    setlocale (LC_ALL, "");

    mdm_set_fatal_warnings_if_unstable ();

    g_type_init ();

    context = g_option_context_new (_("MATE Display Manager Slave"));
    g_option_context_add_main_entries (context, entries, NULL);

    g_option_context_parse (context, &argc, &argv, NULL);
    g_option_context_free (context);

    connection = get_system_bus ();
    if (connection == NULL) {
        goto out;
    }

    mdm_xerrors_init ();
    mdm_log_init ();

    settings = mdm_settings_new ();
    if (settings == NULL) {
        g_warning ("Unable to initialize settings");
        goto out;
    }

    if (! mdm_settings_direct_init (settings, DATADIR "/mdm/mdm.schemas", "/")) {
        g_warning ("Unable to initialize settings");
        goto out;
    }

    mdm_log_set_debug (is_debug_set ());

    if (display_id == NULL) {
        g_critical ("No display ID set");
        exit (1);
    }

    main_loop = g_main_loop_new (NULL, FALSE);

    signal_handler = mdm_signal_handler_new ();
    mdm_signal_handler_set_fatal_func (signal_handler,
                                       (GDestroyNotify)g_main_loop_quit,
                                       main_loop);
    mdm_signal_handler_add (signal_handler, SIGTERM, signal_cb, NULL);
    mdm_signal_handler_add (signal_handler, SIGINT, signal_cb, NULL);
    mdm_signal_handler_add (signal_handler, SIGILL, signal_cb, NULL);
    mdm_signal_handler_add (signal_handler, SIGBUS, signal_cb, NULL);
    mdm_signal_handler_add (signal_handler, SIGFPE, signal_cb, NULL);
    mdm_signal_handler_add (signal_handler, SIGHUP, signal_cb, NULL);
    mdm_signal_handler_add (signal_handler, SIGSEGV, signal_cb, NULL);
    mdm_signal_handler_add (signal_handler, SIGABRT, signal_cb, NULL);
    mdm_signal_handler_add (signal_handler, SIGUSR1, signal_cb, NULL);
    mdm_signal_handler_add (signal_handler, SIGUSR2, signal_cb, NULL);

    slave = mdm_product_slave_new (display_id);
    if (slave == NULL) {
        goto out;
    }
    g_signal_connect (slave,
                      "stopped",
                      G_CALLBACK (on_slave_stopped),
                      main_loop);
    mdm_slave_start (slave);

    g_main_loop_run (main_loop);

    if (slave != NULL) {
        g_object_unref (slave);
    }

    if (signal_handler != NULL) {
        g_object_unref (signal_handler);
    }

    if (main_loop != NULL) {
        g_main_loop_unref (main_loop);
    }

out:

    g_debug ("Slave finished");

    return mdm_return_code;
}
int main(int argc, char** argv)
{
	struct sigaction sa;
	GError* error;
#if GTK_CHECK_VERSION (3, 0, 0)
	const char* display_str;
#else
	char* display_str;
#endif
	GsmManager* manager;
	GsmStore* client_store;
	GsmXsmpServer* xsmp_server;
	GSettings* accessibility_settings;
	MdmSignalHandler* signal_handler;
	static char** override_autostart_dirs = NULL;

	static GOptionEntry entries[] = {
		{"autostart", 'a', 0, G_OPTION_ARG_STRING_ARRAY, &override_autostart_dirs, N_("Override standard autostart directories"), NULL},
		{"debug", 0, 0, G_OPTION_ARG_NONE, &debug, N_("Enable debugging code"), NULL},
		{"failsafe", 'f', 0, G_OPTION_ARG_NONE, &failsafe, N_("Do not load user-specified applications"), NULL},
		{"version", 0, 0, G_OPTION_ARG_NONE, &show_version, N_("Version of this application"), NULL},
		{NULL, 0, 0, 0, NULL, NULL, NULL }
	};

	/* Make sure that we have a session bus */
	if (!require_dbus_session(argc, argv, &error))
	{
		gsm_util_init_error(TRUE, "%s", error->message);
	}

	bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
	bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
	textdomain(GETTEXT_PACKAGE);

	sa.sa_handler = SIG_IGN;
	sa.sa_flags = 0;
	sigemptyset(&sa.sa_mask);
	sigaction(SIGPIPE, &sa, 0);

	error = NULL;
	gtk_init_with_args(&argc, &argv, (char*) _(" - the MATE session manager"), entries, GETTEXT_PACKAGE, &error);

	if (error != NULL)
	{
		g_warning("%s", error->message);
		exit(1);
	}

	if (show_version)
	{
		g_print("%s %s\n", argv [0], VERSION);
		exit(1);
	}

	mdm_log_init();
	mdm_log_set_debug(debug);

	/* Set DISPLAY explicitly for all our children, in case --display
	 * was specified on the command line.
	 */
#if GTK_CHECK_VERSION (3, 0, 0)
	display_str = gdk_display_get_name (gdk_display_get_default());
#else
	display_str = gdk_get_display();
#endif
	gsm_util_setenv("DISPLAY", display_str);
#if !GTK_CHECK_VERSION (3, 0, 0)
	g_free(display_str);
#endif

	/* Some third-party programs rely on MATE_DESKTOP_SESSION_ID to
	 * detect if MATE is running. We keep this for compatibility reasons.
	 */
	gsm_util_setenv("MATE_DESKTOP_SESSION_ID", "this-is-deprecated");

	/*
	 * Make sure gsettings is set up correctly.  If not, then bail.
	 */

	if (initialize_gsettings () != TRUE)
		exit (1);

	/* Look if accessibility is enabled */
	accessibility_settings = g_settings_new (ACCESSIBILITY_SCHEMA);
	if (g_settings_get_boolean (accessibility_settings, ACCESSIBILITY_KEY))
	{
		gsm_util_setenv("GTK_MODULES", "gail:atk-bridge");
	}
	g_object_unref (accessibility_settings);

	client_store = gsm_store_new();

	xsmp_server = gsm_xsmp_server_new(client_store);

	/* Now make sure they succeeded. (They'll call
	 * gsm_util_init_error() if they failed.)
	 */
	acquire_name();

	/* Starts gnome compat mode */
	msm_gnome_start();

	manager = gsm_manager_new(client_store, failsafe);

	signal_handler = mdm_signal_handler_new();
	mdm_signal_handler_add_fatal(signal_handler);
	mdm_signal_handler_add(signal_handler, SIGFPE, signal_cb, NULL);
	mdm_signal_handler_add(signal_handler, SIGHUP, signal_cb, NULL);
	mdm_signal_handler_add(signal_handler, SIGUSR1, signal_cb, NULL);
	mdm_signal_handler_add(signal_handler, SIGTERM, signal_cb, manager);
	mdm_signal_handler_add(signal_handler, SIGINT, signal_cb, manager);
	mdm_signal_handler_set_fatal_func(signal_handler, shutdown_cb, manager);

	if (override_autostart_dirs != NULL)
	{
		load_override_apps(manager, override_autostart_dirs);
	}
	else
	{
		load_standard_apps(manager, GSM_DEFAULT_SESSION_KEY);
	}

	gsm_xsmp_server_start(xsmp_server);
	gsm_manager_start(manager);

	gtk_main();

	if (xsmp_server != NULL)
	{
		g_object_unref(xsmp_server);
	}

	if (manager != NULL)
	{
		g_debug("Unreffing manager");
		g_object_unref(manager);
	}

	if (client_store != NULL)
	{
		g_object_unref(client_store);
	}

	msm_gnome_stop();
	mdm_log_shutdown();

	return 0;
}