comparison gtk4/dw.c @ 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 fad0821cb953
comparison
equal deleted inserted replaced
2346:7b04d1b9f2eb 2347:e5d4c86a0c93
10045 if(ext) 10045 if(ext)
10046 { 10046 {
10047 filter1 = gtk_file_filter_new(); 10047 filter1 = gtk_file_filter_new();
10048 snprintf(buf, 1000, "*.%s", ext); 10048 snprintf(buf, 1000, "*.%s", ext);
10049 gtk_file_filter_add_pattern( filter1, (gchar *)buf); 10049 gtk_file_filter_add_pattern( filter1, (gchar *)buf);
10050 snprintf(buf, 1000, "\"%s\" files", ext ); 10050 snprintf(buf, 1000, "\"%s\" files", ext);
10051 gtk_file_filter_set_name(filter1, (gchar *)buf); 10051 gtk_file_filter_set_name(filter1, (gchar *)buf);
10052 filter2 = gtk_file_filter_new(); 10052 filter2 = gtk_file_filter_new();
10053 gtk_file_filter_add_pattern(filter2, (gchar *)"*"); 10053 gtk_file_filter_add_pattern(filter2, (gchar *)"*");
10054 gtk_file_filter_set_name(filter2, (gchar *)"All Files"); 10054 gtk_file_filter_set_name(filter2, (gchar *)"All Files");
10055 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER( filew ), filter1); 10055 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(filew), filter1);
10056 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER( filew ), filter2); 10056 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(filew), filter2);
10057 } 10057 }
10058 10058
10059 if(defpath) 10059 if(defpath)
10060 { 10060 {
10061 GFile *path = g_file_new_for_path(defpath); 10061 GFile *path = g_file_new_for_path(defpath);
10087 gtk_window_destroy(GTK_WINDOW(filew)); 10087 gtk_window_destroy(GTK_WINDOW(filew));
10088 } 10088 }
10089 DW_FUNCTION_RETURN_THIS(filename); 10089 DW_FUNCTION_RETURN_THIS(filename);
10090 } 10090 }
10091 10091
10092 static void _dw_exec_launched(GAppLaunchContext *context, GAppInfo *info, GVariant *platform_data, gpointer data)
10093 {
10094 g_variant_lookup(platform_data, "pid", "i", data);
10095 }
10092 10096
10093 /* 10097 /*
10094 * Execute and external program in a seperate session. 10098 * Execute and external program in a seperate session.
10095 * Parameters: 10099 * Parameters:
10096 * program: Program name with optional path. 10100 * program: Program name with optional path.
10097 * type: Either DW_EXEC_CON or DW_EXEC_GUI. 10101 * type: Either DW_EXEC_CON or DW_EXEC_GUI.
10098 * params: An array of pointers to string arguements. 10102 * params: An array of pointers to string arguements.
10099 * Returns: 10103 * Returns:
10100 * -1 on error. 10104 * DW_ERROR_UNKNOWN on error.
10101 */ 10105 */
10102 int API dw_exec(const char *program, int type, char **params) 10106 int API dw_exec(const char *program, int type, char **params)
10103 { 10107 {
10104 int ret = -1; 10108 GAppInfo *appinfo = NULL;
10105 10109 char *commandline;
10106 if((ret = fork()) == 0) 10110 int retval = DW_ERROR_UNKNOWN;
10107 { 10111
10108 int i; 10112 /* Generate a command line from the parameters */
10109 10113 if(params && *params)
10110 for (i = 3; i < 256; i++) 10114 {
10111 close(i); 10115 int z = 0, len = 0;
10112 setsid(); 10116
10113 if(type == DW_EXEC_GUI) 10117 while(params[z])
10114 { 10118 {
10115 execvp(program, params); 10119 len+=strlen(params[z]) + 1;
10116 } 10120 z++;
10117 else if(type == DW_EXEC_CON) 10121 }
10118 { 10122 z=1;
10119 char **tmpargs; 10123 commandline = calloc(1, len);
10120 10124 strcpy(commandline, params[0]);
10121 if(!params) 10125 while(params[z])
10122 { 10126 {
10123 tmpargs = malloc(sizeof(char *)); 10127 strcat(commandline, " ");
10124 tmpargs[0] = NULL; 10128 strcat(commandline, params[z]);
10125 } 10129 z++;
10126 else 10130 }
10127 { 10131 }
10128 int z = 0; 10132 else
10129 10133 commandline = strdup(program);
10130 while(params[z]) 10134
10131 { 10135 /* Attempt to use app preferences to launch the application, using the selected Terminal if necessary */
10132 z++; 10136 if((appinfo = g_app_info_create_from_commandline(commandline, NULL,
10133 } 10137 type == DW_EXEC_CON ? G_APP_INFO_CREATE_NEEDS_TERMINAL : G_APP_INFO_CREATE_NONE, NULL)))
10134 tmpargs = malloc(sizeof(char *)*(z+3)); 10138 {
10135 z=0; 10139 GAppLaunchContext *context = g_app_launch_context_new();
10136 tmpargs[0] = "xterm"; 10140
10137 tmpargs[1] = "-e"; 10141 g_signal_connect(G_OBJECT(context), "launched", G_CALLBACK(_dw_exec_launched), (gpointer)&retval);
10138 while(params[z]) 10142
10139 { 10143 g_app_info_launch(appinfo, NULL, context, NULL);
10140 tmpargs[z+2] = params[z]; 10144
10141 z++; 10145 g_object_unref(appinfo);
10142 } 10146 g_object_unref(context);
10143 tmpargs[z+2] = NULL; 10147 }
10144 } 10148 free(commandline);
10145 execvp("xterm", tmpargs); 10149 return retval;
10146 free(tmpargs);
10147 }
10148 /* If we got here exec failed */
10149 _exit(-1);
10150 }
10151 return ret;
10152 } 10150 }
10153 10151
10154 /* 10152 /*
10155 * Loads a web browser pointed at the given URL. 10153 * Loads a web browser pointed at the given URL.
10156 * Parameters: 10154 * Parameters: