diff --git a/main_orig.c b/main_new.c index 97ff07f..7b17bf9 100755 --- a/main_orig.c +++ b/main_new.c @@ -49,7 +49,46 @@ static const GOptionEntry option_entries[] = { NULL } }; +/** + * @return: TRUE = "attach" to working process (=create new window in working process) + * FALSE = could not use working process (probably no process running) + * -1 = Error occured (and was already printed) + **/ +gint +couldConnectToInstance (gchar *working_directory) +{ + GError *error = NULL; + + if (mousepad_dbus_client_launch_files (filenames, working_directory, &error)) + { + /* stop any running startup notification */ + gdk_notify_startup_complete (); + + /* cleanup */ + g_free (working_directory); + g_strfreev (filenames); + + /* print errors, if needed */ + if (G_UNLIKELY (error)) + { + g_printerr ("Mousepad: %s\n", error->message); + g_error_free (error); + return -1; + } + + return TRUE; + } + + /* No instance may own the service name, so we need to clean the error */ + if (G_UNLIKELY (error)) + { + g_error_free (error); + error = NULL; + } + + return FALSE; +} gint main (gint argc, gchar **argv) @@ -131,37 +170,58 @@ main (gint argc, gchar **argv) #ifdef HAVE_DBUS if (G_LIKELY (!opt_disable_server)) - { - /* check if we can reuse an existing instance */ - if (mousepad_dbus_client_launch_files (filenames, working_directory, &error)) - { - /* stop any running startup notification */ - gdk_notify_startup_complete (); + { + gint ret = couldConnectToInstance(working_directory); + + if(-1 == ret){ /* error => exit with error */ + g_print("run-style: error occured\n"); + return EXIT_FAILURE; + } else if(TRUE == ret) { /* TRUE => exit now*/ + g_print("run-style: attach to process\n"); + return EXIT_SUCCESS; + } else{ /* FALSE => are the first instance*/ + g_print("run-style: no process found.\n"); + + // argument "--disable-server" will not enter this area here, + // but will directly trigger the part below + char * execArray[] = {argv[0], "--disable-server", NULL}; + + g_print("start new process\n"); + g_spawn_async (working_directory, execArray, NULL,0,NULL, NULL, NULL, NULL); + g_print("process spawned > > > > Now try to create new window in that process\n"); + // try again to attach (for several times): + for ( int i = 0; i< 20000; ++i) + { + gint ret = couldConnectToInstance(working_directory); + if(-1 == ret){ /* error => exit with error */ + g_print("run-style: error occured\n"); + return EXIT_FAILURE; + } else if(TRUE == ret) { /* TRUE => exit now*/ + g_print("run-style: attach to process and terminate instance\n"); + return EXIT_SUCCESS; + } + } + g_print("ERROR: This case should not happen."); + g_print("ERROR: Could not attach to a mousepad 'server' process.\n"); - /* cleanup */ - g_free (working_directory); - g_strfreev (filenames); + /* TODO: Fallback: start mousepad in "old" "--disable-server"-mode, where a windows gets opened.*/ - /* print errors, if needed */ - if (G_UNLIKELY (error)) - { - g_printerr ("Mousepad: %s\n", error->message); - g_error_free (error); + // do nothing + } + - return EXIT_FAILURE; - } + } + +#endif /* !HAVE_DBUS */ - return EXIT_SUCCESS; - } + /** + * ======================================================== + * Everything from here will be for "server-mode only" + * if the --disable-server cmd option is given, the program + * will jump right into here + * + **/ - /* No instance may own the service name, so we need to clean the error */ - if (G_UNLIKELY (error)) - { - g_error_free (error); - error = NULL; - } - } -#endif /* !HAVE_DBUS */ /* use the Mousepad icon as default for new windows */ gtk_window_set_default_icon_name ("accessories-text-editor"); @@ -172,21 +232,26 @@ main (gint argc, gchar **argv) g_printerr ("Failed to initialize xfconf"); g_error_free (error); - return EXIT_FAILURE; + return EXIT_FAILURE;/// } /* create a new mousepad application */ application = mousepad_application_get (); - /* open an empty window (with an empty document or the files) */ - mousepad_application_new_window_with_files (application, NULL, working_directory, filenames); + /* For now we will not need a window anymore (because this is the 'server' process). */ + /* OBSOLETE: open an empty window (with an empty document or the files) */ + //mousepad_application_new_window_with_files (application, NULL, working_directory, filenames); + /* TODO: create new cmd argument, so we don't need to use `--disable-server` anymore. + Why? Because the current impl changes the behaviour of --disable-server */ + /* FYI: Cleanig up the server is basically already implemented within the "closewindow" method.*/ /* cleanup */ g_free (working_directory); g_strfreev (filenames); - /* do not enter the main loop, unless we have atleast one window */ - if (G_LIKELY (mousepad_application_has_windows (application))) + /* OBSOLETE: do not enter the main loop, unless we have atleast one window */ + //if (G_LIKELY (mousepad_application_has_windows (application))) + /*Since the 'server' has no window, we don't wait for one. Just go on*/ { #ifdef HAVE_DBUS /* register with dbus */