From fdcda651241ace04d895b9efa77d261029e0da68 Mon Sep 17 00:00:00 2001 From: Phil Jones Date: Sat, 22 Oct 2022 21:55:58 +0100 Subject: Add --terminal option. --- src/config.c | 2 ++ src/drun.c | 26 ++++++++++++++++++++++---- src/drun.h | 2 +- src/main.c | 12 +++++++++++- src/tofi.h | 2 ++ 5 files changed, 38 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/config.c b/src/config.c index 915e548..4305c3e 100644 --- a/src/config.c +++ b/src/config.c @@ -431,6 +431,8 @@ bool parse_option(struct tofi *tofi, const char *filename, size_t lineno, const } else if (strcasecmp(option, "drun-print-exec") == 0) { log_warning("drun-print-exec is deprecated, as it is now always true.\n" " This option may be removed in a future version of tofi.\n"); + } else if (strcasecmp(option, "terminal") == 0) { + snprintf(tofi->default_terminal, N_ELEM(tofi->default_terminal), "%s", value); } else if (strcasecmp(option, "hint-font") == 0) { tofi->window.entry.harfbuzz.disable_hinting = !parse_bool(filename, lineno, value, &err); } else if (strcasecmp(option, "late-keyboard-init") == 0) { diff --git a/src/drun.c b/src/drun.c index 3ab42d0..9c6ead8 100644 --- a/src/drun.c +++ b/src/drun.c @@ -278,7 +278,7 @@ struct desktop_vec drun_generate_cached() return apps; } -void drun_print(const char *filename) +void drun_print(const char *filename, const char *terminal_command) { GKeyFile *file = g_key_file_new(); if (!g_key_file_load_from_file(file, filename, G_KEY_FILE_NONE, NULL)) { @@ -326,8 +326,18 @@ void drun_print(const char *filename) } string_vec_add(&pieces, last); - /* Build the command line from our vector. */ - for (size_t i = 0; i < pieces.count; i++) { + /* + * If this is a terminal application, the command line needs to be + * preceded by the terminal command. + */ + bool terminal = g_key_file_get_boolean(file, group, "Terminal", NULL); + if (terminal) { + fputs(terminal_command, stdout); + fputc(' ', stdout); + } + + /* Build the command line from our vector. */ + for (size_t i = 0; i < pieces.count; i++) { fputs(pieces.buf[i].string, stdout); } fputc('\n', stdout); @@ -341,11 +351,19 @@ void drun_launch(const char *filename) { GDesktopAppInfo *info = g_desktop_app_info_new_from_filename(filename); GAppLaunchContext *context = g_app_launch_context_new(); + GError *err = NULL; - if (!g_app_info_launch((GAppInfo *)info, NULL, context, NULL)) { + if (!g_app_info_launch((GAppInfo *)info, NULL, context, &err)) { log_error("Failed to launch %s.\n", filename); + log_error("%s.\n", err->message); + log_error( + "If this is a terminal issue, you can use `--drun-launch=false`,\n" + " and pass your preferred terminal command to `--terminal`.\n" + " For more information, see https://gitlab.gnome.org/GNOME/glib/-/issues/338\n" + " and https://github.com/philj56/tofi/issues/46.\n"); } + g_clear_error(&err); g_object_unref(context); g_object_unref(info); } diff --git a/src/drun.h b/src/drun.h index 207d384..8e65d26 100644 --- a/src/drun.h +++ b/src/drun.h @@ -8,7 +8,7 @@ struct desktop_vec drun_generate(void); struct desktop_vec drun_generate_cached(void); void drun_history_sort(struct desktop_vec *apps, struct history *history); -void drun_print(const char *filename); +void drun_print(const char *filename, const char *terminal_command); void drun_launch(const char *filename); #endif /* DRUN_H */ diff --git a/src/main.c b/src/main.c index 3e84f49..900066b 100644 --- a/src/main.c +++ b/src/main.c @@ -678,6 +678,8 @@ static void usage() " --drun-print-exec Print a command line in drun mode.\n" " This is now always the case,\n" " and this option is deprecated.\n" +" --terminal Terminal to use for command line\n" +" programs in drun mode.\n" " --hint-font Perform font hinting.\n" " --late-keyboard-init (EXPERIMENTAL) Delay keyboard\n" " initialisation until after the first\n" @@ -729,6 +731,7 @@ const struct option long_options[] = { {"hidden-character", required_argument, NULL, 0}, {"drun-launch", required_argument, NULL, 0}, {"drun-print-exec", required_argument, NULL, 0}, + {"terminal", required_argument, NULL, 0}, {"hint-font", required_argument, NULL, 0}, {"output", required_argument, NULL, 0}, {"scale", required_argument, NULL, 0}, @@ -835,7 +838,7 @@ static bool do_submit(struct tofi *tofi) if (tofi->drun_launch) { drun_launch(res); } else { - drun_print(res); + drun_print(res, tofi->default_terminal); } } else { printf("%s\n", res); @@ -894,6 +897,13 @@ int main(int argc, char *argv[]) .use_scale = true, }; wl_list_init(&tofi.output_list); + if (getenv("TERMINAL") != NULL) { + snprintf( + tofi.default_terminal, + N_ELEM(tofi.default_terminal), + "%s", + getenv("TERMINAL")); + } parse_args(&tofi, argc, argv); diff --git a/src/tofi.h b/src/tofi.h index be3e821..994ee0f 100644 --- a/src/tofi.h +++ b/src/tofi.h @@ -11,6 +11,7 @@ #include "wlr-layer-shell-unstable-v1.h" #define MAX_OUTPUT_NAME_LEN 256 +#define MAX_TERMINAL_NAME_LEN 256 struct output_list_element { struct wl_list link; @@ -88,6 +89,7 @@ struct tofi { bool fuzzy_match; bool require_match; char target_output_name[MAX_OUTPUT_NAME_LEN]; + char default_terminal[MAX_TERMINAL_NAME_LEN]; }; #endif /* TOFI_H */ -- cgit v1.2.3