Added support for running software in a terminal from run mode

This commit is contained in:
Scoopta 2019-09-26 23:24:48 -07:00
parent 734dd6d6b6
commit dcdca16a38
4 changed files with 48 additions and 5 deletions

View File

@ -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

View File

@ -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);

View File

@ -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);

View File

@ -17,6 +17,8 @@
#include <wofi.h>
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);