Merge branch 'support-xdg-terminal-exec' into 'main'

Add support for xdg-terminal-exec for handling desktop applications using 'Terminal=true'

See merge request GNOME/glib!2839
This commit is contained in:
Marco Trevisan 2022-10-26 17:03:12 +00:00
commit b9e68b0bd4
2 changed files with 76 additions and 70 deletions

View File

@ -2627,12 +2627,28 @@ prepend_terminal_to_vector (int *argc,
{
#ifndef G_OS_WIN32
char **real_argv;
int real_argc;
int i, j;
char **term_argv = NULL;
int term_argc = 0;
char *check;
size_t real_argc;
size_t i;
size_t term_argc;
char *found_terminal;
char **the_argv;
const char *term_arg;
static const struct {
const char *exec;
const char *exec_arg;
} known_terminals[] = {
{ "xdg-terminal-exec", NULL },
{ "gnome-terminal", "--" },
{ "mate-terminal", "-x" },
{ "xfce4-terminal", "-x" },
{ "tilix", "-e" },
{ "konsole", "-e" },
{ "nxterm", "-e" },
{ "color-xterm", "-e" },
{ "rxvt", "-e" },
{ "dtterm", "-e" },
{ "xterm", "-e" }
};
g_return_val_if_fail (argc != NULL, FALSE);
g_return_val_if_fail (argv != NULL, FALSE);
@ -2646,69 +2662,41 @@ prepend_terminal_to_vector (int *argc,
/* compute size if not given */
if (*argc < 0)
{
for (i = 0; the_argv[i] != NULL; i++)
for ((*argc) = 0; the_argv[*argc] != NULL; (*argc)++)
;
*argc = i;
}
term_argc = 2;
term_argv = g_new0 (char *, 3);
for (i = 0, found_terminal = NULL; i < G_N_ELEMENTS (known_terminals); i++)
{
found_terminal = g_find_program_in_path (known_terminals[i].exec);
if (found_terminal != NULL)
{
term_arg = known_terminals[i].exec_arg;
break;
}
}
check = g_find_program_in_path ("gnome-terminal");
if (check != NULL)
{
term_argv[0] = check;
/* Since 2017, gnome-terminal has preferred `--` over `-x` or `-e`. */
term_argv[1] = g_strdup ("--");
}
else
{
if (check == NULL)
check = g_find_program_in_path ("mate-terminal");
if (check == NULL)
check = g_find_program_in_path ("xfce4-terminal");
if (check != NULL)
{
term_argv[0] = check;
/* Note that gnome-terminal takes -x and
* as -e in gnome-terminal is broken we use that. */
term_argv[1] = g_strdup ("-x");
}
else
{
if (check == NULL)
check = g_find_program_in_path ("tilix");
if (check == NULL)
check = g_find_program_in_path ("konsole");
if (check == NULL)
check = g_find_program_in_path ("nxterm");
if (check == NULL)
check = g_find_program_in_path ("color-xterm");
if (check == NULL)
check = g_find_program_in_path ("rxvt");
if (check == NULL)
check = g_find_program_in_path ("dtterm");
if (check == NULL)
check = g_find_program_in_path ("xterm");
if (check == NULL)
if (found_terminal == NULL)
{
g_debug ("Couldnt find a known terminal");
g_free (term_argv);
return FALSE;
}
term_argv[0] = check;
term_argv[1] = g_strdup ("-e");
}
}
/* check if the terminal require an option */
term_argc = term_arg ? 2 : 1;
real_argc = term_argc + *argc;
real_argv = g_new (char *, real_argc + 1);
for (i = 0; i < term_argc; i++)
real_argv[i] = term_argv[i];
i = 0;
real_argv[i++] = found_terminal;
for (j = 0; j < *argc; j++, i++)
real_argv[i] = (char *)the_argv[j];
if (term_arg)
real_argv[i++] = g_strdup (term_arg);
g_assert (i == term_argc);
for (int j = 0; j < *argc; j++)
real_argv[i++] = the_argv[j];
real_argv[i] = NULL;
@ -2716,9 +2704,6 @@ prepend_terminal_to_vector (int *argc,
*argv = real_argv;
*argc = real_argc;
/* we use g_free here as we sucked all the inner strings
* out from it into real_argv */
g_free (term_argv);
return TRUE;
#else
return FALSE;

View File

@ -1355,6 +1355,8 @@ test_id (void)
static const char *
get_terminal_divider (const char *terminal_name)
{
if (g_str_equal (terminal_name, "xdg-terminal-exec"))
return NULL;
if (g_str_equal (terminal_name, "gnome-terminal"))
return "--";
if (g_str_equal (terminal_name, "tilix"))
@ -1385,6 +1387,7 @@ test_launch_uris_with_terminal (gconstpointer data)
int fd;
int ret;
int flags;
int terminal_divider_arg_length;
const char *terminal_exec = data;
char *old_path;
char *command_line;
@ -1480,12 +1483,21 @@ test_launch_uris_with_terminal (gconstpointer data)
output_args = g_strsplit (output_contents, " ", -1);
g_clear_pointer (&output_contents, g_free);
g_assert_cmpuint (g_strv_length (output_args), ==, 4);
terminal_divider_arg_length = (get_terminal_divider (terminal_exec) != NULL) ? 1 : 0;
g_assert_cmpuint (g_strv_length (output_args), ==, 3 + terminal_divider_arg_length);
if (terminal_divider_arg_length == 1)
{
g_assert_cmpstr (output_args[0], ==, get_terminal_divider (terminal_exec));
g_assert_cmpstr (output_args[1], ==, "true");
g_assert_cmpstr (output_args[2], ==, command_line + 5);
}
else
{
g_assert_cmpstr (output_args[0], ==, "true");
g_assert_cmpstr (output_args[1], ==, command_line + 5);
}
paths = g_list_delete_link (paths,
g_list_find_custom (paths, output_args[3], g_str_equal));
g_list_find_custom (paths, output_args[2 + terminal_divider_arg_length], g_str_equal));
g_assert_cmpint (g_list_length (paths), ==, 1);
g_clear_pointer (&output_args, g_strfreev);
@ -1505,12 +1517,20 @@ test_launch_uris_with_terminal (gconstpointer data)
output_args = g_strsplit (output_contents, " ", -1);
g_clear_pointer (&output_contents, g_free);
g_assert_cmpuint (g_strv_length (output_args), ==, 4);
g_assert_cmpuint (g_strv_length (output_args), ==, 3 + terminal_divider_arg_length);
if (terminal_divider_arg_length > 0)
{
g_assert_cmpstr (output_args[0], ==, get_terminal_divider (terminal_exec));
g_assert_cmpstr (output_args[1], ==, "true");
g_assert_cmpstr (output_args[2], ==, command_line + 5);
}
else
{
g_assert_cmpstr (output_args[0], ==, "true");
g_assert_cmpstr (output_args[1], ==, command_line + 5);
}
paths = g_list_delete_link (paths,
g_list_find_custom (paths, output_args[3], g_str_equal));
g_list_find_custom (paths, output_args[2 + terminal_divider_arg_length], g_str_equal));
g_assert_cmpint (g_list_length (paths), ==, 0);
g_clear_pointer (&output_args, g_strfreev);
@ -1576,6 +1596,7 @@ main (int argc,
{
guint i;
const gchar *supported_terminals[] = {
"xdg-terminal-exec",
"gnome-terminal",
"mate-terminal",
"xfce4-terminal",