changeset 2347:e5d4c86a0c93

GTK4: Rewrite dw_exec() using GLib. Will port to GTK2 and 3 as well.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Thu, 04 Mar 2021 11:19:44 +0000
parents 7b04d1b9f2eb
children e3d2058ea96a
files gtk4/dw.c
diffstat 1 files changed, 50 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/gtk4/dw.c	Thu Mar 04 08:40:14 2021 +0000
+++ b/gtk4/dw.c	Thu Mar 04 11:19:44 2021 +0000
@@ -10047,13 +10047,13 @@
          filter1 = gtk_file_filter_new();
          snprintf(buf, 1000, "*.%s", ext);
          gtk_file_filter_add_pattern( filter1, (gchar *)buf);
-         snprintf(buf, 1000, "\"%s\" files", ext );
+         snprintf(buf, 1000, "\"%s\" files", ext);
          gtk_file_filter_set_name(filter1, (gchar *)buf);
          filter2 = gtk_file_filter_new();
          gtk_file_filter_add_pattern(filter2, (gchar *)"*");
          gtk_file_filter_set_name(filter2, (gchar *)"All Files");
-         gtk_file_chooser_add_filter(GTK_FILE_CHOOSER( filew ), filter1);
-         gtk_file_chooser_add_filter(GTK_FILE_CHOOSER( filew ), filter2);
+         gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(filew), filter1);
+         gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(filew), filter2);
       }
 
       if(defpath)
@@ -10089,6 +10089,10 @@
    DW_FUNCTION_RETURN_THIS(filename);
 }
 
+static void _dw_exec_launched(GAppLaunchContext *context, GAppInfo *info, GVariant *platform_data, gpointer data)
+{
+   g_variant_lookup(platform_data, "pid", "i", data);
+}
 
 /*
  * Execute and external program in a seperate session.
@@ -10097,58 +10101,52 @@
  *       type: Either DW_EXEC_CON or DW_EXEC_GUI.
  *       params: An array of pointers to string arguements.
  * Returns:
- *       -1 on error.
+ *       DW_ERROR_UNKNOWN on error.
  */
 int API 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;
+   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();
+
+      g_signal_connect(G_OBJECT(context), "launched", G_CALLBACK(_dw_exec_launched), (gpointer)&retval);
+
+      g_app_info_launch(appinfo, NULL, context, NULL);
+
+      g_object_unref(appinfo);
+      g_object_unref(context);
+   }
+   free(commandline);
+   return retval;
 }
 
 /*