Modes no longer have to pass a name that matches the name they were loaded with, this was just absurdly bad design and I don't know how I came up with it in the first place

This commit is contained in:
Scoopta 2020-01-20 20:09:40 -08:00
parent c57070122e
commit 05490241a9
5 changed files with 48 additions and 40 deletions

View File

@ -30,15 +30,17 @@ struct cache_line {
struct wl_list link; struct wl_list link;
}; };
struct mode;
char* wofi_parse_image_escapes(const char* text); 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); bool wofi_allow_images(void);

View File

@ -17,11 +17,10 @@
#include <wofi.h> #include <wofi.h>
#define MODE "dmenu"
static const char* arg_names[] = {"parse_action"}; static const char* arg_names[] = {"parse_action"};
static bool parse_action; static bool parse_action;
static struct mode* mode;
struct node { struct node {
struct widget* widget; struct widget* widget;
@ -30,13 +29,14 @@ struct node {
static struct wl_list widgets; 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; parse_action = strcmp(config_get(config, "parse_action", "false"), "true") == 0;
wl_list_init(&widgets); wl_list_init(&widgets);
struct map* cached = map_init(); 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; struct wl_list entries;
wl_list_init(&entries); wl_list_init(&entries);
@ -62,10 +62,10 @@ void wofi_dmenu_init(struct map* config) {
if(map_contains(entry_map, node->line)) { if(map_contains(entry_map, node->line)) {
map_put(cached, node->line, "true"); map_put(cached, node->line, "true");
struct node* widget = malloc(sizeof(struct node)); 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); wl_list_insert(&widgets, &widget->link);
} else { } else {
wofi_remove_cache(MODE, node->line); wofi_remove_cache(mode, node->line);
} }
free(node->line); free(node->line);
wl_list_remove(&node->link); 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) { wl_list_for_each_reverse_safe(node, tmp, &entries, link) {
if(!map_contains(cached, node->line)) { if(!map_contains(cached, node->line)) {
struct node* widget = malloc(sizeof(struct node)); 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); wl_list_insert(&widgets, &widget->link);
} }
free(node->line); free(node->line);
@ -113,7 +113,7 @@ void wofi_dmenu_exec(const gchar* cmd) {
action = out; action = out;
} }
} }
wofi_write_cache(MODE, cmd); wofi_write_cache(mode, cmd);
printf("%s\n", action); printf("%s\n", action);
free(action); free(action);
exit(0); exit(0);

View File

@ -17,7 +17,7 @@
#include <wofi.h> #include <wofi.h>
#define MODE "drun" static struct mode* mode;
struct node { struct node {
struct widget* widget; 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); char* search_text = get_search_text(full_path);
struct node* node = malloc(sizeof(struct node)); 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); wl_list_insert(&widgets, &node->link);
for(size_t count = 0; count < action_count; ++count) { 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); 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* cached = map_init();
struct map* entries = 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); wl_list_init(&widgets);
@ -235,7 +238,7 @@ void wofi_drun_init(void) {
size_t action_count; size_t action_count;
char** text = get_action_text(node->line, &action_count); char** text = get_action_text(node->line, &action_count);
if(text == NULL) { if(text == NULL) {
wofi_remove_cache(MODE, node->line); wofi_remove_cache(mode, node->line);
goto cache_cont; goto cache_cont;
} }
@ -243,7 +246,7 @@ void wofi_drun_init(void) {
char* search_text = get_search_text(node->line); char* search_text = get_search_text(node->line);
struct node* widget = malloc(sizeof(struct node)); 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); wl_list_insert(&widgets, &widget->link);
char* id = get_id(node->line); 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) { void wofi_drun_exec(const gchar* cmd) {
GDesktopAppInfo* info = g_desktop_app_info_new_from_filename(cmd); GDesktopAppInfo* info = g_desktop_app_info_new_from_filename(cmd);
if(G_IS_DESKTOP_APP_INFO(info)) { 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); g_app_info_launch_uris_async(G_APP_INFO(info), NULL, NULL, NULL, launch_done, (gchar*) cmd);
} else if(strrchr(cmd, ' ') != NULL) { } else if(strrchr(cmd, ' ') != NULL) {
char* space = strrchr(cmd, ' '); char* space = strrchr(cmd, ' ');
*space = 0; *space = 0;
wofi_write_cache(MODE, cmd); wofi_write_cache(mode, cmd);
info = g_desktop_app_info_new_from_filename(cmd); info = g_desktop_app_info_new_from_filename(cmd);
char* action = space + 1; char* action = space + 1;
g_desktop_app_info_launch_action(info, action, NULL); g_desktop_app_info_launch_action(info, action, NULL);

View File

@ -17,12 +17,11 @@
#include <wofi.h> #include <wofi.h>
#define MODE "run"
static const char* arg_names[] = {"always_parse_args", "show_all"}; static const char* arg_names[] = {"always_parse_args", "show_all"};
static bool always_parse_args; static bool always_parse_args;
static bool show_all; static bool show_all;
static struct mode* mode;
struct node { struct node {
struct widget* widget; struct widget* widget;
@ -31,14 +30,15 @@ struct node {
static struct wl_list widgets; 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; 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; show_all = strcmp(config_get(config, arg_names[1], "true"), "true") == 0;
wl_list_init(&widgets); wl_list_init(&widgets);
struct map* cached = map_init(); 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(); struct map* entries = map_init();
@ -55,12 +55,12 @@ void wofi_run_init(struct map* config) {
stat(node->line, &info); stat(node->line, &info);
if(access(node->line, X_OK) == 0 && S_ISREG(info.st_mode)) { if(access(node->line, X_OK) == 0 && S_ISREG(info.st_mode)) {
struct node* widget = malloc(sizeof(struct node)); 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); wl_list_insert(&widgets, &widget->link);
map_put(cached, node->line, "true"); map_put(cached, node->line, "true");
map_put(entries, text, "true"); map_put(entries, text, "true");
} else { } else {
wofi_remove_cache(MODE, node->line); wofi_remove_cache(mode, node->line);
} }
free(node->line); free(node->line);
wl_list_remove(&node->link); wl_list_remove(&node->link);
@ -90,7 +90,7 @@ void wofi_run_init(struct map* config) {
char* text = strdup(entry->d_name); char* text = strdup(entry->d_name);
map_put(entries, text, "true"); map_put(entries, text, "true");
struct node* widget = malloc(sizeof(struct node)); 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); wl_list_insert(&widgets, &widget->link);
free(text); free(text);
} }
@ -116,7 +116,7 @@ struct widget* wofi_run_get_widget() {
void wofi_run_exec(const gchar* cmd) { void wofi_run_exec(const gchar* cmd) {
if(wofi_mod_shift()) { if(wofi_mod_shift()) {
wofi_write_cache(MODE, cmd); wofi_write_cache(mode, cmd);
wofi_term_run(cmd); wofi_term_run(cmd);
} }
if(wofi_mod_control() || always_parse_args) { if(wofi_mod_control() || always_parse_args) {
@ -135,10 +135,10 @@ void wofi_run_exec(const gchar* cmd) {
args[count++] = str; args[count++] = str;
} while((str = strtok_r(NULL, "\n", &save_ptr)) != NULL); } while((str = strtok_r(NULL, "\n", &save_ptr)) != NULL);
args[space_count - 1] = NULL; args[space_count - 1] = NULL;
wofi_write_cache(MODE, tmp); wofi_write_cache(mode, tmp);
execvp(tmp, args); execvp(tmp, args);
} else { } else {
wofi_write_cache(MODE, cmd); wofi_write_cache(mode, cmd);
execl(cmd, cmd, NULL); execl(cmd, cmd, NULL);
} }
fprintf(stderr, "%s cannot be executed\n", cmd); fprintf(stderr, "%s cannot be executed\n", cmd);

View File

@ -65,6 +65,7 @@ static bool no_actions;
struct mode { struct mode {
void (*mode_exec)(const gchar* cmd); void (*mode_exec)(const gchar* cmd);
struct widget* (*mode_get_widget)(); struct widget* (*mode_get_widget)();
char* name;
}; };
struct widget { struct widget {
@ -415,8 +416,8 @@ static gboolean _insert_widget(gpointer data) {
return TRUE; return TRUE;
} }
void wofi_write_cache(const char* mode, const char* cmd) { void wofi_write_cache(struct mode* mode, const char* cmd) {
char* cache_path = get_cache_path(mode); char* cache_path = get_cache_path(mode->name);
struct wl_list lines; struct wl_list lines;
wl_list_init(&lines); wl_list_init(&lines);
bool inc_count = false; bool inc_count = false;
@ -483,8 +484,8 @@ void wofi_write_cache(const char* mode, const char* cmd) {
free(tmp_path); free(tmp_path);
} }
void wofi_remove_cache(const char* mode, const char* cmd) { void wofi_remove_cache(struct mode* mode, const char* cmd) {
char* cache_path = get_cache_path(mode); char* cache_path = get_cache_path(mode->name);
if(access(cache_path, R_OK | W_OK) == 0) { if(access(cache_path, R_OK | W_OK) == 0) {
struct wl_list lines; struct wl_list lines;
wl_list_init(&lines); wl_list_init(&lines);
@ -520,8 +521,8 @@ void wofi_remove_cache(const char* mode, const char* cmd) {
free(cache_path); free(cache_path);
} }
struct wl_list* wofi_read_cache(char* mode) { struct wl_list* wofi_read_cache(struct mode* mode) {
char* cache_path = get_cache_path(mode); char* cache_path = get_cache_path(mode->name);
struct wl_list* cache = malloc(sizeof(struct wl_list)); struct wl_list* cache = malloc(sizeof(struct wl_list));
wl_list_init(cache); wl_list_init(cache);
struct wl_list lines; struct wl_list lines;
@ -562,9 +563,9 @@ struct wl_list* wofi_read_cache(char* mode) {
return cache; 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)); struct widget* widget = malloc(sizeof(struct widget));
widget->mode = strdup(mode); widget->mode = strdup(mode->name);
widget->text = malloc(action_count * sizeof(char*)); widget->text = malloc(action_count * sizeof(char*));
for(size_t count = 0; count < action_count; ++count) { for(size_t count = 0; count < action_count; ++count) {
widget->text[count] = strdup(text[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)); struct mode* mode_ptr = calloc(1, sizeof(struct mode));
map_put_void(modes, mode, mode_ptr); 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)(); const char** (*get_arg_names)();
size_t (*get_arg_count)(); size_t (*get_arg_count)();
if(dso == NULL) { if(dso == NULL) {
@ -809,7 +812,7 @@ static void add_mode(char* mode) {
} }
if(init != NULL) { if(init != NULL) {
init(props); init(mode_ptr, props);
gdk_threads_add_idle(_insert_widget, mode_ptr); gdk_threads_add_idle(_insert_widget, mode_ptr);
} else { } else {
fprintf(stderr, "I would love to show %s but Idk what it is\n", mode); fprintf(stderr, "I would love to show %s but Idk what it is\n", mode);