From dcdca16a3840ba3c379b4b92da0b4f539ce7b341 Mon Sep 17 00:00:00 2001 From: Scoopta Date: Thu, 26 Sep 2019 23:24:48 -0700 Subject: [PATCH] Added support for running software in a terminal from run mode --- inc/wofi.h | 4 ++++ modes/run.c | 3 +++ src/main.c | 16 +++++++++++++++- src/wofi.c | 30 ++++++++++++++++++++++++++---- 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/inc/wofi.h b/inc/wofi.h index 980dcb8..204fc17 100644 --- a/inc/wofi.h +++ b/inc/wofi.h @@ -50,4 +50,8 @@ void wofi_insert_widget(char* text, char* action); bool wofi_allow_images(); uint64_t wofi_get_image_size(); + +bool wofi_run_in_term(); + +void wofi_term_run(const char* cmd); #endif diff --git a/modes/run.c b/modes/run.c index 228badd..b65192e 100644 --- a/modes/run.c +++ b/modes/run.c @@ -63,6 +63,9 @@ void run_init() { } void run_exec(const gchar* cmd) { + if(wofi_run_in_term()) { + wofi_term_run(cmd); + } execl(cmd, cmd, NULL); fprintf(stderr, "%s cannot be executed\n", cmd); exit(errno); diff --git a/src/main.c b/src/main.c index a63b7c0..955f705 100644 --- a/src/main.c +++ b/src/main.c @@ -64,6 +64,7 @@ static void print_usage(char** argv) { printf("--allow-images\t-i\tAllows images to be rendered\n"); printf("--allow-markup\t-m\tAllows pango markup\n"); printf("--cache-file\t-k\tSets the cache file to use\n"); + printf("--term\t\t-t\tSpecifies the terminal to use when running in a term\n"); exit(0); } @@ -263,6 +264,12 @@ int main(int argc, char** argv) { .flag = NULL, .val = 'k' }, + { + .name = "term", + .has_arg = required_argument, + .flag = NULL, + .val = 't' + }, { .name = NULL, .has_arg = 0, @@ -284,8 +291,9 @@ int main(int argc, char** argv) { char* allow_images = NULL; char* allow_markup = NULL; char* cache_file = NULL; + char* terminal = NULL; char opt; - while((opt = getopt_long(argc, argv, "hfc:s:C:dS:W:H:p:x:y:nimk:", opts, NULL)) != -1) { + while((opt = getopt_long(argc, argv, "hfc:s:C:dS:W:H:p:x:y:nimk:t:", opts, NULL)) != -1) { switch(opt) { case 'h': print_usage(argv); @@ -340,6 +348,9 @@ int main(int argc, char** argv) { case 'k': cache_file = optarg; break; + case 't': + terminal = optarg; + break; } } @@ -445,6 +456,9 @@ int main(int argc, char** argv) { if(cache_file != NULL) { map_put(config, "cache_file", cache_file); } + if(terminal != NULL) { + map_put(config, "term", terminal); + } gtk_init(&argc, &argv); diff --git a/src/wofi.c b/src/wofi.c index b9b3d4e..385c97b 100644 --- a/src/wofi.c +++ b/src/wofi.c @@ -17,6 +17,8 @@ #include +static const char* terminals[] = {"kitty", "termite", "gnome-terminal", "weston-terminal"}; + static uint64_t width, height; static int64_t x, y; static struct zwlr_layer_shell_v1* shell; @@ -29,6 +31,8 @@ static bool allow_images, allow_markup; static uint64_t image_size; static char* cache_file = NULL; static char* config_dir; +static bool run_in_term; +static char* terminal; static void (*mode_exec)(const gchar* cmd); static bool (*exec_search)(); @@ -224,6 +228,22 @@ uint64_t wofi_get_image_size() { return image_size; } +bool wofi_run_in_term() { + return run_in_term; +} + +void wofi_term_run(const char* cmd) { + if(terminal != NULL) { + execlp(terminal, terminal, "--", cmd, NULL); + } + size_t term_count = sizeof(terminals) / sizeof(char*); + for(size_t count = 0; count < term_count; ++count) { + execlp(terminals[count], terminals[count], "--", cmd, NULL); + } + fprintf(stderr, "No terminal emulator found please set term in config or use --term\n"); + exit(1); +} + static void execute_action(char* mode, const gchar* cmd) { char* cache_path = get_cache_path(mode); struct wl_list lines; @@ -318,11 +338,8 @@ static gboolean do_filter(GtkFlowBoxChild* row, gpointer data) { static gboolean key_press(GtkWidget* widget, GdkEvent* event, gpointer data) { (void) widget; - (void) event; (void) data; - guint code; - gdk_event_get_keyval(event, &code); - switch(code) { + switch(event->key.keyval) { case GDK_KEY_Escape: exit(0); break; @@ -330,6 +347,10 @@ static gboolean key_press(GtkWidget* widget, GdkEvent* event, gpointer data) { case GDK_KEY_Left: case GDK_KEY_Right: case GDK_KEY_Return: + run_in_term = (event->key.state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK; + if(run_in_term) { + event->key.state &= ~GDK_SHIFT_MASK; + } if(gtk_widget_has_focus(scroll)) { gtk_entry_grab_focus_without_selecting(GTK_ENTRY(entry)); } @@ -406,6 +427,7 @@ void wofi_init(struct map* config) { image_size = strtol(config_get(config, "image_size", "32"), NULL, 10); cache_file = map_get(config, "cache_file"); config_dir = map_get(config, "config_dir"); + terminal = map_get(config, "term"); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_widget_realize(window);