Changed the widget insertion system, this should hopefully increase performance and be an all around better system

This commit is contained in:
Scoopta 2020-01-16 16:35:51 -08:00
parent 27227fa3bb
commit e5300b2995
5 changed files with 103 additions and 18 deletions

View File

@ -52,7 +52,7 @@ void wofi_remove_cache(const gchar* mode, const gchar* cmd);
struct wl_list* wofi_read_cache(char* mode); 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); struct widget* wofi_create_widget(char* mode, char** text, char* search_text, char** actions, size_t action_count);
bool wofi_allow_images(void); bool wofi_allow_images(void);

View File

@ -23,9 +23,18 @@ static const char* arg_names[] = {"parse_action"};
static bool parse_action; static bool parse_action;
struct node {
struct widget* widget;
struct wl_list link;
};
static struct wl_list widgets;
void wofi_dmenu_init(struct map* config) { void wofi_dmenu_init(struct map* config) {
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);
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);
@ -52,7 +61,9 @@ void wofi_dmenu_init(struct map* config) {
wl_list_for_each_safe(node, tmp, cache, link) { wl_list_for_each_safe(node, tmp, cache, link) {
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");
wofi_insert_widget(MODE, &node->line, node->line, &node->line, 1); struct node* widget = malloc(sizeof(struct node));
widget->widget = wofi_create_widget(MODE, &node->line, node->line, &node->line, 1);
wl_list_insert(&widgets, &widget->link);
} else { } else {
wofi_remove_cache(MODE, node->line); wofi_remove_cache(MODE, node->line);
} }
@ -66,7 +77,9 @@ 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)) {
wofi_insert_widget(MODE, &node->line, node->line, &node->line, 1); struct node* widget = malloc(sizeof(struct node));
widget->widget = wofi_create_widget(MODE, &node->line, node->line, &node->line, 1);
wl_list_insert(&widgets, &widget->link);
} }
free(node->line); free(node->line);
wl_list_remove(&node->link); wl_list_remove(&node->link);
@ -75,6 +88,17 @@ void wofi_dmenu_init(struct map* config) {
map_free(cached); map_free(cached);
} }
struct widget* wofi_dmenu_get_widget() {
struct node* node, *tmp;
wl_list_for_each_reverse_safe(node, tmp, &widgets, link) {
struct widget* widget = node->widget;
wl_list_remove(&node->link);
free(node);
return widget;
}
return NULL;
}
void wofi_dmenu_exec(const gchar* cmd) { void wofi_dmenu_exec(const gchar* cmd) {
char* action = strdup(cmd); char* action = strdup(cmd);
if(parse_action) { if(parse_action) {

View File

@ -19,6 +19,13 @@
#define MODE "drun" #define MODE "drun"
struct node {
struct widget* widget;
struct wl_list link;
};
static struct wl_list widgets;
static char* get_text(char* file, char* action) { static char* get_text(char* file, char* action) {
GDesktopAppInfo* info = g_desktop_app_info_new_from_filename(file); 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)) { if(info == NULL || g_desktop_app_info_get_is_hidden(info) || g_desktop_app_info_get_nodisplay(info)) {
@ -197,7 +204,10 @@ static void insert_dir(char* app_dir, struct map* cached, struct map* entries) {
char** actions = get_action_actions(full_path, &action_count); char** actions = get_action_actions(full_path, &action_count);
char* search_text = get_search_text(full_path); char* search_text = get_search_text(full_path);
wofi_insert_widget(MODE, text, search_text, actions, action_count);
struct node* node = malloc(sizeof(struct node));
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) { for(size_t count = 0; count < action_count; ++count) {
free(actions[count]); free(actions[count]);
@ -218,6 +228,8 @@ void wofi_drun_init(void) {
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);
struct cache_line* node, *tmp; struct cache_line* node, *tmp;
wl_list_for_each_safe(node, tmp, cache, link) { wl_list_for_each_safe(node, tmp, cache, link) {
size_t action_count; size_t action_count;
@ -230,7 +242,9 @@ void wofi_drun_init(void) {
char** actions = get_action_actions(node->line, &action_count); char** actions = get_action_actions(node->line, &action_count);
char* search_text = get_search_text(node->line); char* search_text = get_search_text(node->line);
wofi_insert_widget(MODE, text, search_text, actions, action_count); struct node* widget = malloc(sizeof(struct node));
widget->widget = wofi_create_widget(MODE, text, search_text, actions, action_count);
wl_list_insert(&widgets, &widget->link);
char* id = get_id(node->line); char* id = get_id(node->line);
@ -280,6 +294,17 @@ void wofi_drun_init(void) {
map_free(entries); map_free(entries);
} }
struct widget* wofi_drun_get_widget() {
struct node* node, *tmp;
wl_list_for_each_reverse_safe(node, tmp, &widgets, link) {
struct widget* widget = node->widget;
wl_list_remove(&node->link);
free(node);
return widget;
}
return NULL;
}
static void launch_done(GObject* obj, GAsyncResult* result, gpointer data) { static void launch_done(GObject* obj, GAsyncResult* result, gpointer data) {
if(g_app_info_launch_uris_finish(G_APP_INFO(obj), result, NULL)) { if(g_app_info_launch_uris_finish(G_APP_INFO(obj), result, NULL)) {
exit(0); exit(0);

View File

@ -24,10 +24,19 @@ 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;
struct node {
struct widget* widget;
struct wl_list link;
};
static struct wl_list widgets;
void wofi_run_init(struct map* config) { void wofi_run_init(struct map* config) {
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);
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);
@ -45,7 +54,9 @@ void wofi_run_init(struct map* config) {
struct stat info; struct stat info;
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)) {
wofi_insert_widget(MODE, &text, text, &node->line, 1); struct node* widget = malloc(sizeof(struct node));
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(cached, node->line, "true");
map_put(entries, text, "true"); map_put(entries, text, "true");
} else { } else {
@ -78,7 +89,9 @@ void wofi_run_init(struct map* config) {
if(access(full_path, X_OK) == 0 && S_ISREG(info.st_mode) && !map_contains(cached, full_path) && (show_all || !map_contains(entries, entry->d_name))) { if(access(full_path, X_OK) == 0 && S_ISREG(info.st_mode) && !map_contains(cached, full_path) && (show_all || !map_contains(entries, entry->d_name))) {
char* text = strdup(entry->d_name); char* text = strdup(entry->d_name);
map_put(entries, text, "true"); map_put(entries, text, "true");
wofi_insert_widget(MODE, &text, text, &full_path, 1); struct node* widget = malloc(sizeof(struct node));
widget->widget = wofi_create_widget(MODE, &text, text, &full_path, 1);
wl_list_insert(&widgets, &widget->link);
free(text); free(text);
} }
free(full_path); free(full_path);
@ -90,6 +103,17 @@ void wofi_run_init(struct map* config) {
map_free(entries); map_free(entries);
} }
struct widget* wofi_run_get_widget() {
struct node* node, *tmp;
wl_list_for_each_reverse_safe(node, tmp, &widgets, link) {
struct widget* widget = node->widget;
wl_list_remove(&node->link);
free(node);
return widget;
}
return NULL;
}
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);

View File

@ -62,13 +62,14 @@ static struct map* config;
static enum locations location; static enum locations location;
static bool no_actions; static bool no_actions;
struct node {
size_t action_count;
char* mode, **text, *search_text, **actions;
};
struct mode { struct mode {
void (*mode_exec)(const gchar* cmd); void (*mode_exec)(const gchar* cmd);
struct widget* (*mode_get_widget)();
};
struct widget {
size_t action_count;
char* mode, **text, *search_text, **actions;
}; };
static void nop() {} static void nop() {}
@ -355,7 +356,16 @@ static void expand(GtkExpander* expander, gpointer data) {
} }
static gboolean _insert_widget(gpointer data) { static gboolean _insert_widget(gpointer data) {
struct node* node = data; struct mode* mode = data;
struct widget* node;
if(mode->mode_get_widget == NULL) {
return FALSE;
} else {
node = mode->mode_get_widget();
}
if(node == NULL) {
return FALSE;
}
GtkWidget* parent; GtkWidget* parent;
if(node->action_count > 1 && !no_actions) { if(node->action_count > 1 && !no_actions) {
parent = gtk_expander_new(""); parent = gtk_expander_new("");
@ -393,7 +403,7 @@ static gboolean _insert_widget(gpointer data) {
} }
free(node->actions); free(node->actions);
free(node); free(node);
return FALSE; return TRUE;
} }
void wofi_write_cache(const gchar* mode, const gchar* cmd) { void wofi_write_cache(const gchar* mode, const gchar* cmd) {
@ -539,8 +549,8 @@ struct wl_list* wofi_read_cache(char* mode) {
return cache; return cache;
} }
void wofi_insert_widget(char* mode, char** text, char* search_text, char** actions, size_t action_count) { struct widget* wofi_create_widget(char* mode, char** text, char* search_text, char** actions, size_t action_count) {
struct node* widget = malloc(sizeof(struct node)); struct widget* widget = malloc(sizeof(struct widget));
widget->mode = strdup(mode); widget->mode = strdup(mode);
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) {
@ -552,8 +562,7 @@ void wofi_insert_widget(char* mode, char** text, char* search_text, char** actio
for(size_t count = 0; count < action_count; ++count) { for(size_t count = 0; count < action_count; ++count) {
widget->actions[count] = strdup(actions[count]); widget->actions[count] = strdup(actions[count]);
} }
g_idle_add(_insert_widget, widget); return widget;
utils_sleep_millis(1);
} }
bool wofi_allow_images(void) { bool wofi_allow_images(void) {
@ -757,6 +766,7 @@ static void add_mode(char* mode) {
get_arg_names = get_plugin_proc(mode, "_get_arg_names"); get_arg_names = get_plugin_proc(mode, "_get_arg_names");
get_arg_count = get_plugin_proc(mode, "_get_arg_count"); get_arg_count = get_plugin_proc(mode, "_get_arg_count");
mode_ptr->mode_exec = get_plugin_proc(mode, "_exec"); mode_ptr->mode_exec = get_plugin_proc(mode, "_exec");
mode_ptr->mode_get_widget = get_plugin_proc(mode, "_get_widget");
} else { } else {
char* plugins_dir = utils_concat(2, config_dir, "/plugins/"); char* plugins_dir = utils_concat(2, config_dir, "/plugins/");
char* full_name = utils_concat(2, plugins_dir, mode); char* full_name = utils_concat(2, plugins_dir, mode);
@ -767,6 +777,7 @@ static void add_mode(char* mode) {
get_arg_names = dlsym(plugin, "get_arg_names"); get_arg_names = dlsym(plugin, "get_arg_names");
get_arg_count = dlsym(plugin, "get_arg_count"); get_arg_count = dlsym(plugin, "get_arg_count");
mode_ptr->mode_exec = dlsym(plugin, "exec"); mode_ptr->mode_exec = dlsym(plugin, "exec");
mode_ptr->mode_get_widget = dlsym(plugin, "get_widget");
} }
const char** arg_names = NULL; const char** arg_names = NULL;
@ -786,6 +797,7 @@ static void add_mode(char* mode) {
if(init != NULL) { if(init != NULL) {
init(props); init(props);
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);
exit(1); exit(1);