int main (int argc, char **argv) { int i; char **argv_copy; int argc_copy; const char *startup_id, *display_name, *home_dir; GdkDisplay *display; TerminalOptions *options; GError *error = NULL; char *working_directory; int ret = EXIT_SUCCESS; setlocale (LC_ALL, ""); bindtextdomain (GETTEXT_PACKAGE, TERM_LOCALEDIR); bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); textdomain (GETTEXT_PACKAGE); _terminal_debug_init (); /* Make a NULL-terminated copy since we may need it later */ argv_copy = g_new (char *, argc + 1); for (i = 0; i < argc; ++i) argv_copy [i] = argv [i]; argv_copy [i] = NULL; argc_copy = argc; startup_id = g_getenv ("DESKTOP_STARTUP_ID"); working_directory = g_get_current_dir (); /* Now change directory to $HOME so we don't prevent unmounting, e.g. if the * factory is started by caja-open-terminal. See bug #565328. * On failure back to /. */ home_dir = g_get_home_dir (); if (home_dir == NULL || chdir (home_dir) < 0) (void) chdir ("/"); options = terminal_options_parse (working_directory, NULL, startup_id, NULL, FALSE, FALSE, &argc, &argv, &error, gtk_get_option_group (TRUE), egg_sm_client_get_option_group (), NULL); g_free (working_directory); if (options == NULL) { g_printerr (_("Failed to parse arguments: %s\n"), error->message); g_error_free (error); exit (EXIT_FAILURE); } g_set_application_name (_("Terminal")); /* Unset the these env variables, so they doesn't end up * in the factory's env and thus in the terminals' envs. */ g_unsetenv ("DESKTOP_STARTUP_ID"); g_unsetenv ("GIO_LAUNCHED_DESKTOP_FILE_PID"); g_unsetenv ("GIO_LAUNCHED_DESKTOP_FILE"); display = gdk_display_get_default (); display_name = gdk_display_get_name (display); options->display_name = g_strdup (display_name); if (options->startup_id == NULL) { /* Create a fake one containing a timestamp that we can use */ Time timestamp; timestamp = slowly_and_stupidly_obtain_timestamp (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ())); options->startup_id = g_strdup_printf ("_TIME%lu", timestamp); } if (options->use_factory) { OwnData *data; guint owner_id; data = g_new (OwnData, 1); data->factory_name = get_factory_name_for_display (display_name); data->options = options; data->exit_code = -1; data->argv = argv_copy; data->argc = argc_copy; gtk_init(&argc, &argv); options->initial_workspace = get_initial_workspace (); owner_id = g_bus_own_name (G_BUS_TYPE_SESSION, data->factory_name, G_BUS_NAME_OWNER_FLAGS_NONE, bus_acquired_cb, name_acquired_cb, name_lost_cb, data, NULL); gtk_main (); ret = data->exit_code; g_bus_unown_name (owner_id); g_free (data->factory_name); g_free (data); } else { terminal_app_handle_options (terminal_app_get (), options, TRUE /* allow resume */, &error); terminal_options_free (options); if (error == NULL) { gtk_main (); } else { g_printerr ("Error handling options: %s\n", error->message); g_error_free (error); ret = EXIT_FAILURE; } } terminal_app_shutdown (); g_free (argv_copy); return ret; }
int main (int argc, char **argv) { int i; char **argv_copy; const char *startup_id, *display_name; GdkDisplay *display; TerminalOptions *options; TerminalFactory *factory; GError *error = NULL; char *working_directory; int exit_code = EXIT_FAILURE; setlocale (LC_ALL, ""); terminal_i18n_init (TRUE); _terminal_debug_init (); /* Make a NULL-terminated copy since we may need it later */ argv_copy = g_new (char *, argc + 1); for (i = 0; i < argc; ++i) argv_copy [i] = argv [i]; argv_copy [i] = NULL; startup_id = g_getenv ("DESKTOP_STARTUP_ID"); working_directory = g_get_current_dir (); options = terminal_options_parse (working_directory, startup_id, &argc, &argv, &error); if (options == NULL) { g_printerr (_("Failed to parse arguments: %s\n"), error->message); g_error_free (error); g_free (working_directory); g_free (argv_copy); exit (EXIT_FAILURE); } g_set_application_name (_("Terminal")); /* Do this here so that gdk_display is initialized */ if (options->startup_id == NULL) terminal_client_get_fallback_startup_id (&options->startup_id); display = gdk_display_get_default (); display_name = gdk_display_get_name (display); options->display_name = g_strdup (display_name); factory = terminal_factory_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS, options->server_app_id ? options->server_app_id : TERMINAL_APPLICATION_ID, TERMINAL_FACTORY_OBJECT_PATH, NULL /* cancellable */, &error); if (factory == NULL) { g_dbus_error_strip_remote_error (error); g_printerr ("Error constructing proxy for %s:%s: %s\n", options->server_app_id ? options->server_app_id : TERMINAL_APPLICATION_ID, TERMINAL_FACTORY_OBJECT_PATH, error->message); g_error_free (error); goto out; } if (!handle_options (factory, options, &error)) { g_dbus_error_strip_remote_error (error); g_printerr ("Failed to handle arguments: %s\n", error->message); } else { exit_code = EXIT_SUCCESS; } g_object_unref (factory); out: terminal_options_free (options); g_free (working_directory); g_free (argv_copy); return exit_code; }
static void method_call_cb (GDBusConnection *connection, const char *sender, const char *object_path, const char *interface_name, const char *method_name, GVariant *parameters, GDBusMethodInvocation *invocation, gpointer user_data) { if (g_strcmp0 (method_name, "HandleArguments") == 0) { TerminalOptions *options = NULL; GVariant *v_wd, *v_display, *v_sid, *v_envv, *v_argv; char *working_directory = NULL, *display_name = NULL, *startup_id = NULL; int initial_workspace = -1; char **envv = NULL, **argv = NULL; int argc; GError *error = NULL; g_variant_get (parameters, "(@ay@ay@ay@ayi@ay)", &v_wd, &v_display, &v_sid, &v_envv, &initial_workspace, &v_argv); working_directory = ay_to_string (v_wd, &error); if (error) goto out; display_name = ay_to_string (v_display, &error); if (error) goto out; startup_id = ay_to_string (v_sid, &error); if (error) goto out; envv = ay_to_strv (v_envv, NULL); argv = ay_to_strv (v_argv, &argc); _terminal_debug_print (TERMINAL_DEBUG_FACTORY, "Factory invoked with working-dir='%s' display='%s' startup-id='%s'" "workspace='%d'\n", working_directory ? working_directory : "(null)", display_name ? display_name : "(null)", startup_id ? startup_id : "(null)", initial_workspace); options = terminal_options_parse (working_directory, display_name, startup_id, envv, TRUE, TRUE, &argc, &argv, &error, NULL); options->initial_workspace = initial_workspace; if (options != NULL) { terminal_app_handle_options (terminal_app_get (), options, FALSE /* no resume */, &error); terminal_options_free (options); } out: g_variant_unref (v_wd); g_free (working_directory); g_variant_unref (v_display); g_free (display_name); g_variant_unref (v_sid); g_free (startup_id); g_variant_unref (v_envv); g_strfreev (envv); g_variant_unref (v_argv); g_strfreev (argv); if (error == NULL) { g_dbus_method_invocation_return_value (invocation, g_variant_new ("()")); } else { g_dbus_method_invocation_return_gerror (invocation, error); g_error_free (error); } } }