diff --git a/inc/wofi.h b/inc/wofi.h index ce6bbe6..39789cf 100644 --- a/inc/wofi.h +++ b/inc/wofi.h @@ -43,6 +43,8 @@ struct cache_line { void wofi_init(struct map* config); +void wofi_write_cache(const gchar* mode, const gchar* cmd); + struct wl_list* wofi_read_cache(char* mode); void wofi_insert_widget(char* mode, char** text, char* search_text, char** actions, size_t action_count); diff --git a/modes/dmenu.c b/modes/dmenu.c index c4cf03a..6ddfaf5 100644 --- a/modes/dmenu.c +++ b/modes/dmenu.c @@ -17,13 +17,15 @@ #include +#define MODE "dmenu" + void wofi_dmenu_init() { struct map* cached = map_init(); - struct wl_list* cache = wofi_read_cache("dmenu"); + struct wl_list* cache = wofi_read_cache(MODE); struct cache_line* node, *tmp; wl_list_for_each_safe(node, tmp, cache, link) { - wofi_insert_widget("dmenu", &node->line, node->line, &node->line, 1); + wofi_insert_widget(MODE, &node->line, node->line, &node->line, 1); map_put(cached, node->line, "true"); free(node->line); wl_list_remove(&node->link); @@ -42,13 +44,14 @@ void wofi_dmenu_init() { if(map_contains(cached, line)) { continue; } - wofi_insert_widget("dmenu", &line, line, &line, 1); + wofi_insert_widget(MODE, &line, line, &line, 1); } free(line); map_free(cached); } void wofi_dmenu_exec(const gchar* cmd) { + wofi_write_cache(MODE, cmd); printf("%s\n", cmd); exit(0); } diff --git a/modes/drun.c b/modes/drun.c index a83e27b..49d70a7 100644 --- a/modes/drun.c +++ b/modes/drun.c @@ -17,6 +17,8 @@ #include +#define MODE "drun" + static char* get_text(char* file, char* action) { GDesktopAppInfo* info = g_desktop_app_info_new_from_filename(file); if(info == NULL || g_desktop_app_info_get_is_hidden(info) || g_desktop_app_info_get_nodisplay(info)) { @@ -158,7 +160,7 @@ static void insert_dir(char* app_dir, struct map* cached, struct map* entries) { char** actions = get_action_actions(full_path, &action_count); char* search_text = get_search_text(full_path); - wofi_insert_widget("drun", text, search_text, actions, action_count); + wofi_insert_widget(MODE, text, search_text, actions, action_count); for(size_t count = 0; count < action_count; ++count) { free(actions[count]); @@ -176,7 +178,7 @@ static void insert_dir(char* app_dir, struct map* cached, struct map* entries) { void wofi_drun_init() { struct map* cached = map_init(); struct map* entries = map_init(); - struct wl_list* cache = wofi_read_cache("drun"); + struct wl_list* cache = wofi_read_cache(MODE); struct cache_line* node, *tmp; wl_list_for_each_safe(node, tmp, cache, link) { @@ -189,7 +191,7 @@ void wofi_drun_init() { char** actions = get_action_actions(node->line, &action_count); char* search_text = get_search_text(node->line); - wofi_insert_widget("drun", text, search_text, actions, action_count); + wofi_insert_widget(MODE, text, search_text, actions, action_count); map_put(cached, node->line, "true"); free(search_text); @@ -247,10 +249,12 @@ static void launch_done(GObject* obj, GAsyncResult* result, gpointer data) { void wofi_drun_exec(const gchar* cmd) { GDesktopAppInfo* info = g_desktop_app_info_new_from_filename(cmd); if(G_IS_DESKTOP_APP_INFO(info)) { + wofi_write_cache(MODE, cmd); g_app_info_launch_uris_async(G_APP_INFO(info), NULL, NULL, NULL, launch_done, (gchar*) cmd); } else if(strrchr(cmd, ' ') != NULL) { char* space = strrchr(cmd, ' '); *space = 0; + wofi_write_cache(MODE, cmd); info = g_desktop_app_info_new_from_filename(cmd); char* action = space + 1; g_desktop_app_info_launch_action(info, action, NULL); diff --git a/modes/run.c b/modes/run.c index f4b3916..3d8f280 100644 --- a/modes/run.c +++ b/modes/run.c @@ -17,9 +17,11 @@ #include +#define MODE "run" + void wofi_run_init() { struct map* cached = map_init(); - struct wl_list* cache = wofi_read_cache("run"); + struct wl_list* cache = wofi_read_cache(MODE); struct cache_line* node, *tmp; wl_list_for_each_safe(node, tmp, cache, link) { @@ -34,7 +36,7 @@ void wofi_run_init() { search_prefix = text; } char* search_text = utils_concat(2, search_prefix, node->line); - wofi_insert_widget("run", &text, search_text, &node->line, 1); + wofi_insert_widget(MODE, &text, search_text, &node->line, 1); map_put(cached, node->line, "true"); free(search_text); free(node->line); @@ -64,7 +66,7 @@ void wofi_run_init() { if(access(full_path, X_OK) == 0 && S_ISREG(info.st_mode) && !map_contains(cached, full_path)) { char* search_text = utils_concat(2, entry->d_name, full_path); char* text = strdup(entry->d_name); - wofi_insert_widget("run", &text, search_text, &full_path, 1); + wofi_insert_widget(MODE, &text, search_text, &full_path, 1); free(search_text); free(text); } @@ -77,6 +79,7 @@ void wofi_run_init() { } void wofi_run_exec(const gchar* cmd) { + wofi_write_cache(MODE, cmd); if(wofi_run_in_term()) { wofi_term_run(cmd); } diff --git a/src/wofi.c b/src/wofi.c index 944a9a1..113a628 100644 --- a/src/wofi.c +++ b/src/wofi.c @@ -156,52 +156,8 @@ static char* get_cache_path(const gchar* mode) { return cache_path; } -static void execute_action(const gchar* mode, const gchar* cmd, bool primary_action) { +static void execute_action(const gchar* mode, const gchar* cmd) { struct mode* mode_ptr = map_get(modes, mode); - if(primary_action) { - char* cache_path = get_cache_path(mode); - struct wl_list lines; - wl_list_init(&lines); - bool inc_count = false; - if(access(cache_path, R_OK) == 0) { - FILE* file = fopen(cache_path, "r"); - char* line = NULL; - size_t size = 0; - while(getline(&line, &size, file) != -1) { - struct cache_line* node = malloc(sizeof(struct cache_line)); - if(strstr(line, cmd) != NULL) { - uint64_t count = strtol(line, NULL, 10) + 1; - char num[6]; - snprintf(num, 5, "%" PRIu64, count); - node->line = utils_concat(4, num, " ", cmd, "\n"); - inc_count = true; - } else { - node->line = strdup(line); - } - wl_list_insert(&lines, &node->link); - } - free(line); - fclose(file); - } - if(!inc_count) { - struct cache_line* node = malloc(sizeof(struct cache_line)); - node->line = utils_concat(3, "1 ", cmd, "\n"); - wl_list_insert(&lines, &node->link); - } - - FILE* file = fopen(cache_path, "w"); - struct cache_line* node, *tmp; - wl_list_for_each_safe(node, tmp, &lines, link) { - fwrite(node->line, 1, strlen(node->line), file); - free(node->line); - wl_list_remove(&node->link); - free(node); - } - - fclose(file); - - free(cache_path); - } mode_ptr->mode_exec(cmd); } @@ -213,7 +169,7 @@ static void activate_item(GtkFlowBox* flow_box, GtkFlowBoxChild* row, gpointer d if(primary_action) { box = gtk_expander_get_label_widget(GTK_EXPANDER(box)); } - execute_action(wofi_property_box_get_property(WOFI_PROPERTY_BOX(box), "mode"), wofi_property_box_get_property(WOFI_PROPERTY_BOX(box), "action"), primary_action); + execute_action(wofi_property_box_get_property(WOFI_PROPERTY_BOX(box), "mode"), wofi_property_box_get_property(WOFI_PROPERTY_BOX(box), "action")); } static gboolean _insert_widget(gpointer data) { @@ -251,6 +207,51 @@ static gboolean _insert_widget(gpointer data) { return FALSE; } +void wofi_write_cache(const gchar* mode, const gchar* cmd) { + char* cache_path = get_cache_path(mode); + struct wl_list lines; + wl_list_init(&lines); + bool inc_count = false; + if(access(cache_path, R_OK) == 0) { + FILE* file = fopen(cache_path, "r"); + char* line = NULL; + size_t size = 0; + while(getline(&line, &size, file) != -1) { + struct cache_line* node = malloc(sizeof(struct cache_line)); + if(strstr(line, cmd) != NULL) { + uint64_t count = strtol(line, NULL, 10) + 1; + char num[6]; + snprintf(num, 5, "%" PRIu64, count); + node->line = utils_concat(4, num, " ", cmd, "\n"); + inc_count = true; + } else { + node->line = strdup(line); + } + wl_list_insert(&lines, &node->link); + } + free(line); + fclose(file); + } + if(!inc_count) { + struct cache_line* node = malloc(sizeof(struct cache_line)); + node->line = utils_concat(3, "1 ", cmd, "\n"); + wl_list_insert(&lines, &node->link); + } + + FILE* file = fopen(cache_path, "w"); + struct cache_line* node, *tmp; + wl_list_for_each_safe(node, tmp, &lines, link) { + fwrite(node->line, 1, strlen(node->line), file); + free(node->line); + wl_list_remove(&node->link); + free(node); + } + + fclose(file); + + free(cache_path); +} + struct wl_list* wofi_read_cache(char* mode) { char* cache_path = get_cache_path(mode); struct wl_list* cache = malloc(sizeof(struct wl_list)); @@ -370,14 +371,14 @@ static void activate_search(GtkEntry* entry, gpointer data) { (void) data; GtkWidget* child = get_first_child(GTK_CONTAINER(inner_box)); if(exec_search || child == NULL) { - execute_action(mode, gtk_entry_get_text(entry), true); + execute_action(mode, gtk_entry_get_text(entry)); } else { GtkWidget* box = gtk_bin_get_child(GTK_BIN(child)); bool primary_action = GTK_IS_EXPANDER(box); if(primary_action) { box = gtk_expander_get_label_widget(GTK_EXPANDER(box)); } - execute_action(wofi_property_box_get_property(WOFI_PROPERTY_BOX(box), "mode"), wofi_property_box_get_property(WOFI_PROPERTY_BOX(box), "action"), primary_action); + execute_action(wofi_property_box_get_property(WOFI_PROPERTY_BOX(box), "mode"), wofi_property_box_get_property(WOFI_PROPERTY_BOX(box), "action")); } }