comparison gtk/dw.c @ 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 d0f884083c63
children fad0821cb953
comparison
equal deleted inserted replaced
2348:e3d2058ea96a 2349:123e22827a82
12926 } 12926 }
12927 return (char *)dw_dialog_wait(dwwait); 12927 return (char *)dw_dialog_wait(dwwait);
12928 #endif 12928 #endif
12929 } 12929 }
12930 12930
12931 #if GLIB_CHECK_VERSION(2,36,0)
12932 static void _dw_exec_launched(GAppLaunchContext *context, GAppInfo *info, GVariant *platform_data, gpointer data)
12933 {
12934 g_variant_lookup(platform_data, "pid", "i", data);
12935 }
12936 #endif
12931 12937
12932 /* 12938 /*
12933 * Execute and external program in a seperate session. 12939 * Execute and external program in a seperate session.
12934 * Parameters: 12940 * Parameters:
12935 * program: Program name with optional path. 12941 * program: Program name with optional path.
12936 * type: Either DW_EXEC_CON or DW_EXEC_GUI. 12942 * type: Either DW_EXEC_CON or DW_EXEC_GUI.
12937 * params: An array of pointers to string arguements. 12943 * params: An array of pointers to string arguements.
12938 * Returns: 12944 * Returns:
12939 * -1 on error. 12945 * DW_ERROR_UNKNOWN on error.
12940 */ 12946 */
12941 int dw_exec(const char *program, int type, char **params) 12947 int API dw_exec(const char *program, int type, char **params)
12942 { 12948 {
12943 int ret = -1; 12949 GAppInfo *appinfo = NULL;
12944 12950 char *commandline;
12945 if((ret = fork()) == 0) 12951 int retval = DW_ERROR_UNKNOWN;
12946 { 12952
12947 int i; 12953 /* Generate a command line from the parameters */
12948 12954 if(params && *params)
12949 for (i = 3; i < 256; i++) 12955 {
12950 close(i); 12956 int z = 0, len = 0;
12951 setsid(); 12957
12952 if(type == DW_EXEC_GUI) 12958 while(params[z])
12953 { 12959 {
12954 execvp(program, params); 12960 len+=strlen(params[z]) + 1;
12955 } 12961 z++;
12956 else if(type == DW_EXEC_CON) 12962 }
12957 { 12963 z=1;
12958 char **tmpargs; 12964 commandline = calloc(1, len);
12959 12965 strcpy(commandline, params[0]);
12960 if(!params) 12966 while(params[z])
12961 { 12967 {
12962 tmpargs = malloc(sizeof(char *)); 12968 strcat(commandline, " ");
12963 tmpargs[0] = NULL; 12969 strcat(commandline, params[z]);
12964 } 12970 z++;
12965 else 12971 }
12966 { 12972 }
12967 int z = 0; 12973 else
12968 12974 commandline = strdup(program);
12969 while(params[z]) 12975
12970 { 12976 /* Attempt to use app preferences to launch the application, using the selected Terminal if necessary */
12971 z++; 12977 if((appinfo = g_app_info_create_from_commandline(commandline, NULL,
12972 } 12978 type == DW_EXEC_CON ? G_APP_INFO_CREATE_NEEDS_TERMINAL : G_APP_INFO_CREATE_NONE, NULL)))
12973 tmpargs = malloc(sizeof(char *)*(z+3)); 12979 {
12974 z=0; 12980 GAppLaunchContext *context = g_app_launch_context_new();
12975 tmpargs[0] = "xterm"; 12981
12976 tmpargs[1] = "-e"; 12982 #if GLIB_CHECK_VERSION(2,36,0)
12977 while(params[z]) 12983 g_signal_connect(G_OBJECT(context), "launched", G_CALLBACK(_dw_exec_launched), (gpointer)&retval);
12978 { 12984 #endif
12979 tmpargs[z+2] = params[z]; 12985
12980 z++; 12986 if(g_app_info_launch(appinfo, NULL, context, NULL) && retval == DW_ERROR_UNKNOWN)
12981 } 12987 retval = DW_ERROR_NONE;
12982 tmpargs[z+2] = NULL; 12988
12983 } 12989 g_object_unref(appinfo);
12984 execvp("xterm", tmpargs); 12990 g_object_unref(context);
12985 free(tmpargs); 12991 }
12986 } 12992 free(commandline);
12987 /* If we got here exec failed */ 12993 return retval;
12988 _exit(-1);
12989 }
12990 return ret;
12991 } 12994 }
12992 12995
12993 /* 12996 /*
12994 * Loads a web browser pointed at the given URL. 12997 * Loads a web browser pointed at the given URL.
12995 * Parameters: 12998 * Parameters: