comparison gtk3/dw.c @ 2348:e3d2058ea96a

GTK3: Port new dw_exec() from GTK4 with additional GLib version checks.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Thu, 04 Mar 2021 11:53:09 +0000
parents 88f7e871b283
children 123e22827a82
comparison
equal deleted inserted replaced
2347:e5d4c86a0c93 2348:e3d2058ea96a
1211 static void _html_result_event(GObject *object, GAsyncResult *result, gpointer script_data) 1211 static void _html_result_event(GObject *object, GAsyncResult *result, gpointer script_data)
1212 { 1212 {
1213 #if USE_WEBKIT2 1213 #if USE_WEBKIT2
1214 pthread_t saved_thread = _dw_thread; 1214 pthread_t saved_thread = _dw_thread;
1215 WebKitJavascriptResult *js_result; 1215 WebKitJavascriptResult *js_result;
1216 #if WEBKIT_CHECK_VERSION(2, 22, 0) 1216 #if WEBKIT_CHECK_VERSION(2, 22, 0)
1217 JSCValue *value; 1217 JSCValue *value;
1218 #else 1218 #else
1219 JSValueRef value; 1219 JSValueRef value;
1220 JSGlobalContextRef context; 1220 JSGlobalContextRef context;
1221 #endif 1221 #endif
1222 GError *error = NULL; 1222 GError *error = NULL;
1223 int (*htmlresultfunc)(HWND, int, char *, void *, void *) = NULL; 1223 int (*htmlresultfunc)(HWND, int, char *, void *, void *) = NULL;
1224 gint handlerdata = GPOINTER_TO_INT(g_object_get_data(object, "_dw_html_result_id")); 1224 gint handlerdata = GPOINTER_TO_INT(g_object_get_data(object, "_dw_html_result_id"));
1225 void *user_data = NULL; 1225 void *user_data = NULL;
1226 1226
11503 11503
11504 gtk_widget_destroy( filew ); 11504 gtk_widget_destroy( filew );
11505 return filename; 11505 return filename;
11506 } 11506 }
11507 11507
11508 #if GLIB_CHECK_VERSION(2,36,0)
11509 static void _dw_exec_launched(GAppLaunchContext *context, GAppInfo *info, GVariant *platform_data, gpointer data)
11510 {
11511 g_variant_lookup(platform_data, "pid", "i", data);
11512 }
11513 #endif
11508 11514
11509 /* 11515 /*
11510 * Execute and external program in a seperate session. 11516 * Execute and external program in a seperate session.
11511 * Parameters: 11517 * Parameters:
11512 * program: Program name with optional path. 11518 * program: Program name with optional path.
11513 * type: Either DW_EXEC_CON or DW_EXEC_GUI. 11519 * type: Either DW_EXEC_CON or DW_EXEC_GUI.
11514 * params: An array of pointers to string arguements. 11520 * params: An array of pointers to string arguements.
11515 * Returns: 11521 * Returns:
11516 * -1 on error. 11522 * DW_ERROR_UNKNOWN on error.
11517 */ 11523 */
11518 int dw_exec(const char *program, int type, char **params) 11524 int API dw_exec(const char *program, int type, char **params)
11519 { 11525 {
11520 int ret = -1; 11526 GAppInfo *appinfo = NULL;
11521 11527 char *commandline;
11522 if((ret = fork()) == 0) 11528 int retval = DW_ERROR_UNKNOWN;
11523 { 11529
11524 int i; 11530 /* Generate a command line from the parameters */
11525 11531 if(params && *params)
11526 for (i = 3; i < 256; i++) 11532 {
11527 close(i); 11533 int z = 0, len = 0;
11528 setsid(); 11534
11529 if(type == DW_EXEC_GUI) 11535 while(params[z])
11530 { 11536 {
11531 execvp(program, params); 11537 len+=strlen(params[z]) + 1;
11532 } 11538 z++;
11533 else if(type == DW_EXEC_CON) 11539 }
11534 { 11540 z=1;
11535 char **tmpargs; 11541 commandline = calloc(1, len);
11536 11542 strcpy(commandline, params[0]);
11537 if(!params) 11543 while(params[z])
11538 { 11544 {
11539 tmpargs = malloc(sizeof(char *)); 11545 strcat(commandline, " ");
11540 tmpargs[0] = NULL; 11546 strcat(commandline, params[z]);
11541 } 11547 z++;
11542 else 11548 }
11543 { 11549 }
11544 int z = 0; 11550 else
11545 11551 commandline = strdup(program);
11546 while(params[z]) 11552
11547 { 11553 /* Attempt to use app preferences to launch the application, using the selected Terminal if necessary */
11548 z++; 11554 if((appinfo = g_app_info_create_from_commandline(commandline, NULL,
11549 } 11555 type == DW_EXEC_CON ? G_APP_INFO_CREATE_NEEDS_TERMINAL : G_APP_INFO_CREATE_NONE, NULL)))
11550 tmpargs = malloc(sizeof(char *)*(z+3)); 11556 {
11551 z=0; 11557 GAppLaunchContext *context = g_app_launch_context_new();
11552 tmpargs[0] = "xterm"; 11558
11553 tmpargs[1] = "-e"; 11559 #if GLIB_CHECK_VERSION(2,36,0)
11554 while(params[z]) 11560 g_signal_connect(G_OBJECT(context), "launched", G_CALLBACK(_dw_exec_launched), (gpointer)&retval);
11555 { 11561 #endif
11556 tmpargs[z+2] = params[z]; 11562
11557 z++; 11563 g_app_info_launch(appinfo, NULL, context, NULL);
11558 } 11564
11559 tmpargs[z+2] = NULL; 11565 g_object_unref(appinfo);
11560 } 11566 g_object_unref(context);
11561 execvp("xterm", tmpargs); 11567 }
11562 free(tmpargs); 11568 free(commandline);
11563 } 11569 return retval;
11564 /* If we got here exec failed */
11565 _exit(-1);
11566 }
11567 return ret;
11568 } 11570 }
11569 11571
11570 /* 11572 /*
11571 * Loads a web browser pointed at the given URL. 11573 * Loads a web browser pointed at the given URL.
11572 * Parameters: 11574 * Parameters: