changeset 2349:123e22827a82

GTK2/3: If the PID did not get returned set return to DW_ERROR_NONE. Use the new code on GTK2 as well.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Thu, 04 Mar 2021 12:32:56 +0000
parents e3d2058ea96a
children 5ddc447fb367
files gtk/dw.c gtk3/dw.c
diffstat 2 files changed, 57 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/gtk/dw.c	Thu Mar 04 11:53:09 2021 +0000
+++ b/gtk/dw.c	Thu Mar 04 12:32:56 2021 +0000
@@ -12928,6 +12928,12 @@
 #endif
 }
 
+#if GLIB_CHECK_VERSION(2,36,0)
+static void _dw_exec_launched(GAppLaunchContext *context, GAppInfo *info, GVariant *platform_data, gpointer data)
+{
+   g_variant_lookup(platform_data, "pid", "i", data);
+}
+#endif
 
 /*
  * Execute and external program in a seperate session.
@@ -12936,58 +12942,55 @@
  *       type: Either DW_EXEC_CON or DW_EXEC_GUI.
  *       params: An array of pointers to string arguements.
  * Returns:
- *       -1 on error.
- */
-int dw_exec(const char *program, int type, char **params)
-{
-   int ret = -1;
-
-   if((ret = fork()) == 0)
-   {
-      int i;
-
-      for (i = 3; i < 256; i++)
-         close(i);
-      setsid();
-      if(type == DW_EXEC_GUI)
-      {
-         execvp(program, params);
-      }
-      else if(type == DW_EXEC_CON)
-      {
-         char **tmpargs;
-
-         if(!params)
-         {
-            tmpargs = malloc(sizeof(char *));
-            tmpargs[0] = NULL;
-         }
-         else
-         {
-            int z = 0;
-
-            while(params[z])
-            {
-               z++;
-            }
-            tmpargs = malloc(sizeof(char *)*(z+3));
-            z=0;
-            tmpargs[0] = "xterm";
-            tmpargs[1] = "-e";
-            while(params[z])
-            {
-               tmpargs[z+2] = params[z];
-               z++;
-            }
-            tmpargs[z+2] = NULL;
-         }
-         execvp("xterm", tmpargs);
-         free(tmpargs);
-      }
-      /* If we got here exec failed */
-      _exit(-1);
-   }
-   return ret;
+ *       DW_ERROR_UNKNOWN on error.
+ */
+int API dw_exec(const char *program, int type, char **params)
+{
+   GAppInfo *appinfo = NULL;
+   char *commandline;
+   int retval = DW_ERROR_UNKNOWN;
+   
+   /* Generate a command line from the parameters */
+   if(params && *params)
+   {
+      int z = 0, len = 0;
+      
+      while(params[z])
+      {
+         len+=strlen(params[z]) + 1;
+         z++;
+      }
+      z=1;
+      commandline = calloc(1, len);
+      strcpy(commandline, params[0]);
+      while(params[z])
+      {
+         strcat(commandline, " ");
+         strcat(commandline, params[z]);
+         z++;
+      }
+   }
+   else
+      commandline = strdup(program);
+
+   /* Attempt to use app preferences to launch the application, using the selected Terminal if necessary */
+   if((appinfo = g_app_info_create_from_commandline(commandline, NULL, 
+      type == DW_EXEC_CON ? G_APP_INFO_CREATE_NEEDS_TERMINAL : G_APP_INFO_CREATE_NONE, NULL)))
+   {
+      GAppLaunchContext *context = g_app_launch_context_new();
+
+#if GLIB_CHECK_VERSION(2,36,0)
+      g_signal_connect(G_OBJECT(context), "launched", G_CALLBACK(_dw_exec_launched), (gpointer)&retval);
+#endif
+
+      if(g_app_info_launch(appinfo, NULL, context, NULL) && retval == DW_ERROR_UNKNOWN)
+         retval = DW_ERROR_NONE;
+
+      g_object_unref(appinfo);
+      g_object_unref(context);
+   }
+   free(commandline);
+   return retval;
 }
 
 /*
--- a/gtk3/dw.c	Thu Mar 04 11:53:09 2021 +0000
+++ b/gtk3/dw.c	Thu Mar 04 12:32:56 2021 +0000
@@ -11560,7 +11560,8 @@
       g_signal_connect(G_OBJECT(context), "launched", G_CALLBACK(_dw_exec_launched), (gpointer)&retval);
 #endif
 
-      g_app_info_launch(appinfo, NULL, context, NULL);
+      if(g_app_info_launch(appinfo, NULL, context, NULL) && retval == DW_ERROR_UNKNOWN)
+         retval = DW_ERROR_NONE;
 
       g_object_unref(appinfo);
       g_object_unref(context);