diff --git a/inc/wofi_api.h b/inc/wofi_api.h index 8f77507..bc2f30c 100644 --- a/inc/wofi_api.h +++ b/inc/wofi_api.h @@ -30,15 +30,17 @@ struct cache_line { struct wl_list link; }; +struct mode; + char* wofi_parse_image_escapes(const char* text); -void wofi_write_cache(const char* mode, const char* cmd); +void wofi_write_cache(struct mode* mode, const char* cmd); -void wofi_remove_cache(const char* mode, const char* cmd); +void wofi_remove_cache(struct mode* mode, const char* cmd); -struct wl_list* wofi_read_cache(char* mode); +struct wl_list* wofi_read_cache(struct mode* mode); -struct widget* wofi_create_widget(char* mode, char** text, char* search_text, char** actions, size_t action_count); +struct widget* wofi_create_widget(struct mode* mode, char** text, char* search_text, char** actions, size_t action_count); bool wofi_allow_images(void); diff --git a/modes/dmenu.c b/modes/dmenu.c index 3d97e26..a042b1c 100644 --- a/modes/dmenu.c +++ b/modes/dmenu.c @@ -17,11 +17,10 @@ #include -#define MODE "dmenu" - static const char* arg_names[] = {"parse_action"}; static bool parse_action; +static struct mode* mode; struct node { struct widget* widget; @@ -30,13 +29,14 @@ struct node { static struct wl_list widgets; -void wofi_dmenu_init(struct map* config) { +void wofi_dmenu_init(struct mode* this, struct map* config) { + mode = this; parse_action = strcmp(config_get(config, "parse_action", "false"), "true") == 0; wl_list_init(&widgets); struct map* cached = map_init(); - struct wl_list* cache = wofi_read_cache(MODE); + struct wl_list* cache = wofi_read_cache(mode); struct wl_list entries; wl_list_init(&entries); @@ -62,10 +62,10 @@ void wofi_dmenu_init(struct map* config) { if(map_contains(entry_map, node->line)) { map_put(cached, node->line, "true"); struct node* widget = malloc(sizeof(struct node)); - widget->widget = wofi_create_widget(MODE, &node->line, node->line, &node->line, 1); + widget->widget = wofi_create_widget(mode, &node->line, node->line, &node->line, 1); wl_list_insert(&widgets, &widget->link); } else { - wofi_remove_cache(MODE, node->line); + wofi_remove_cache(mode, node->line); } free(node->line); wl_list_remove(&node->link); @@ -78,7 +78,7 @@ void wofi_dmenu_init(struct map* config) { wl_list_for_each_reverse_safe(node, tmp, &entries, link) { if(!map_contains(cached, node->line)) { struct node* widget = malloc(sizeof(struct node)); - widget->widget = wofi_create_widget(MODE, &node->line, node->line, &node->line, 1); + widget->widget = wofi_create_widget(mode, &node->line, node->line, &node->line, 1); wl_list_insert(&widgets, &widget->link); } free(node->line); @@ -113,7 +113,7 @@ void wofi_dmenu_exec(const gchar* cmd) { action = out; } } - wofi_write_cache(MODE, cmd); + wofi_write_cache(mode, cmd); printf("%s\n", action); free(action); exit(0); diff --git a/modes/drun.c b/modes/drun.c index e334ad5..277e62a 100644 --- a/modes/drun.c +++ b/modes/drun.c @@ -17,7 +17,7 @@ #include -#define MODE "drun" +static struct mode* mode; struct node { struct widget* widget; @@ -206,7 +206,7 @@ static void insert_dir(char* app_dir, struct map* cached, struct map* entries) { char* search_text = get_search_text(full_path); struct node* node = malloc(sizeof(struct node)); - node->widget = wofi_create_widget(MODE, text, search_text, actions, action_count); + node->widget = wofi_create_widget(mode, text, search_text, actions, action_count); wl_list_insert(&widgets, &node->link); for(size_t count = 0; count < action_count; ++count) { @@ -223,10 +223,13 @@ static void insert_dir(char* app_dir, struct map* cached, struct map* entries) { closedir(dir); } -void wofi_drun_init(void) { +void wofi_drun_init(struct mode* this, struct map* config) { + (void) config; + mode = this; + struct map* cached = map_init(); struct map* entries = map_init(); - struct wl_list* cache = wofi_read_cache(MODE); + struct wl_list* cache = wofi_read_cache(mode); wl_list_init(&widgets); @@ -235,7 +238,7 @@ void wofi_drun_init(void) { size_t action_count; char** text = get_action_text(node->line, &action_count); if(text == NULL) { - wofi_remove_cache(MODE, node->line); + wofi_remove_cache(mode, node->line); goto cache_cont; } @@ -243,7 +246,7 @@ void wofi_drun_init(void) { char* search_text = get_search_text(node->line); struct node* widget = malloc(sizeof(struct node)); - widget->widget = wofi_create_widget(MODE, text, search_text, actions, action_count); + widget->widget = wofi_create_widget(mode, text, search_text, actions, action_count); wl_list_insert(&widgets, &widget->link); char* id = get_id(node->line); @@ -323,12 +326,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); + 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); + 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 29795fa..6179a5e 100644 --- a/modes/run.c +++ b/modes/run.c @@ -17,12 +17,11 @@ #include -#define MODE "run" - static const char* arg_names[] = {"always_parse_args", "show_all"}; static bool always_parse_args; static bool show_all; +static struct mode* mode; struct node { struct widget* widget; @@ -31,14 +30,15 @@ struct node { static struct wl_list widgets; -void wofi_run_init(struct map* config) { +void wofi_run_init(struct mode* this, struct map* config) { + mode = this; always_parse_args = strcmp(config_get(config, arg_names[0], "false"), "true") == 0; show_all = strcmp(config_get(config, arg_names[1], "true"), "true") == 0; wl_list_init(&widgets); struct map* cached = map_init(); - struct wl_list* cache = wofi_read_cache(MODE); + struct wl_list* cache = wofi_read_cache(mode); struct map* entries = map_init(); @@ -55,12 +55,12 @@ void wofi_run_init(struct map* config) { stat(node->line, &info); if(access(node->line, X_OK) == 0 && S_ISREG(info.st_mode)) { struct node* widget = malloc(sizeof(struct node)); - widget->widget = wofi_create_widget(MODE, &text, text, &node->line, 1); + widget->widget = wofi_create_widget(mode, &text, text, &node->line, 1); wl_list_insert(&widgets, &widget->link); map_put(cached, node->line, "true"); map_put(entries, text, "true"); } else { - wofi_remove_cache(MODE, node->line); + wofi_remove_cache(mode, node->line); } free(node->line); wl_list_remove(&node->link); @@ -90,7 +90,7 @@ void wofi_run_init(struct map* config) { char* text = strdup(entry->d_name); map_put(entries, text, "true"); struct node* widget = malloc(sizeof(struct node)); - widget->widget = wofi_create_widget(MODE, &text, text, &full_path, 1); + widget->widget = wofi_create_widget(mode, &text, text, &full_path, 1); wl_list_insert(&widgets, &widget->link); free(text); } @@ -116,7 +116,7 @@ struct widget* wofi_run_get_widget() { void wofi_run_exec(const gchar* cmd) { if(wofi_mod_shift()) { - wofi_write_cache(MODE, cmd); + wofi_write_cache(mode, cmd); wofi_term_run(cmd); } if(wofi_mod_control() || always_parse_args) { @@ -135,10 +135,10 @@ void wofi_run_exec(const gchar* cmd) { args[count++] = str; } while((str = strtok_r(NULL, "\n", &save_ptr)) != NULL); args[space_count - 1] = NULL; - wofi_write_cache(MODE, tmp); + wofi_write_cache(mode, tmp); execvp(tmp, args); } else { - wofi_write_cache(MODE, cmd); + wofi_write_cache(mode, cmd); execl(cmd, cmd, NULL); } fprintf(stderr, "%s cannot be executed\n", cmd); diff --git a/src/wofi.c b/src/wofi.c index e7c54b6..9a98b01 100644 --- a/src/wofi.c +++ b/src/wofi.c @@ -65,6 +65,7 @@ static bool no_actions; struct mode { void (*mode_exec)(const gchar* cmd); struct widget* (*mode_get_widget)(); + char* name; }; struct widget { @@ -415,8 +416,8 @@ static gboolean _insert_widget(gpointer data) { return TRUE; } -void wofi_write_cache(const char* mode, const char* cmd) { - char* cache_path = get_cache_path(mode); +void wofi_write_cache(struct mode* mode, const char* cmd) { + char* cache_path = get_cache_path(mode->name); struct wl_list lines; wl_list_init(&lines); bool inc_count = false; @@ -483,8 +484,8 @@ void wofi_write_cache(const char* mode, const char* cmd) { free(tmp_path); } -void wofi_remove_cache(const char* mode, const char* cmd) { - char* cache_path = get_cache_path(mode); +void wofi_remove_cache(struct mode* mode, const char* cmd) { + char* cache_path = get_cache_path(mode->name); if(access(cache_path, R_OK | W_OK) == 0) { struct wl_list lines; wl_list_init(&lines); @@ -520,8 +521,8 @@ void wofi_remove_cache(const char* mode, const char* cmd) { free(cache_path); } -struct wl_list* wofi_read_cache(char* mode) { - char* cache_path = get_cache_path(mode); +struct wl_list* wofi_read_cache(struct mode* mode) { + char* cache_path = get_cache_path(mode->name); struct wl_list* cache = malloc(sizeof(struct wl_list)); wl_list_init(cache); struct wl_list lines; @@ -562,9 +563,9 @@ struct wl_list* wofi_read_cache(char* mode) { return cache; } -struct widget* wofi_create_widget(char* mode, char** text, char* search_text, char** actions, size_t action_count) { +struct widget* wofi_create_widget(struct mode* mode, char** text, char* search_text, char** actions, size_t action_count) { struct widget* widget = malloc(sizeof(struct widget)); - widget->mode = strdup(mode); + widget->mode = strdup(mode->name); widget->text = malloc(action_count * sizeof(char*)); for(size_t count = 0; count < action_count; ++count) { widget->text[count] = strdup(text[count]); @@ -771,7 +772,9 @@ static void add_mode(char* mode) { struct mode* mode_ptr = calloc(1, sizeof(struct mode)); map_put_void(modes, mode, mode_ptr); - void (*init)(struct map* props); + mode_ptr->name = strdup(mode); + + void (*init)(struct mode* mode, struct map* props); const char** (*get_arg_names)(); size_t (*get_arg_count)(); if(dso == NULL) { @@ -809,7 +812,7 @@ static void add_mode(char* mode) { } if(init != NULL) { - init(props); + init(mode_ptr, props); gdk_threads_add_idle(_insert_widget, mode_ptr); } else { fprintf(stderr, "I would love to show %s but Idk what it is\n", mode);