diff options
author | James Booth <boothj5@gmail.com> | 2016-07-04 21:48:04 +0100 |
---|---|---|
committer | James Booth <boothj5@gmail.com> | 2016-07-04 21:48:04 +0100 |
commit | a779ad0764b830fe9e72bc188789ca22ecd4e394 (patch) | |
tree | 59fc6cfab46532586bd83b6fa9be0f03e36d84d3 /src | |
parent | 3fe1d76a059f476372b242dee5f9fb93d434e66a (diff) | |
download | profani-tty-a779ad0764b830fe9e72bc188789ca22ecd4e394.tar.gz |
Use hash table for plugin timed functions
Diffstat (limited to 'src')
-rw-r--r-- | src/plugins/api.c | 2 | ||||
-rw-r--r-- | src/plugins/callbacks.c | 70 | ||||
-rw-r--r-- | src/plugins/callbacks.h | 4 |
3 files changed, 52 insertions, 24 deletions
diff --git a/src/plugins/api.c b/src/plugins/api.c index 2a577296..955a7bde 100644 --- a/src/plugins/api.c +++ b/src/plugins/api.c @@ -157,7 +157,7 @@ api_register_timed(const char *const plugin_name, void *callback, int interval_s timed_function->interval_seconds = interval_seconds; timed_function->timer = g_timer_new(); - callbacks_add_timed(timed_function); + callbacks_add_timed(plugin_name, timed_function); } void diff --git a/src/plugins/callbacks.c b/src/plugins/callbacks.c index 2569962c..41ba30db 100644 --- a/src/plugins/callbacks.c +++ b/src/plugins/callbacks.c @@ -45,7 +45,7 @@ #include "ui/ui.h" static GHashTable *p_commands = NULL; -static GSList *p_timed_functions = NULL; +static GHashTable *p_timed_functions = NULL; static GHashTable *p_window_callbacks = NULL; static void @@ -57,14 +57,6 @@ _free_window_callback(PluginWindowCallback *window_callback) free(window_callback); } -//typedef struct cmd_help_t { -// const gchar *tags[20]; -// const gchar *synopsis[50]; -// const gchar *desc; -// const gchar *args[128][2]; -// const gchar *examples[20]; -//} CommandHelp; - static void _free_command_help(CommandHelp *help) { @@ -104,6 +96,7 @@ _free_command(PluginCommand *command) free(command->command_name); _free_command_help(command->help); + free(command); } @@ -113,10 +106,29 @@ _free_command_hash(GHashTable *command_hash) g_hash_table_destroy(command_hash); } +static void +_free_timed_function(PluginTimedFunction *timed_function) +{ + if (timed_function->callback_destroy) { + timed_function->callback_destroy(timed_function->callback); + } + + g_timer_destroy(timed_function->timer); + + free(timed_function); +} + +static void +_free_timed_function_list(GList *timed_functions) +{ + g_list_free_full(timed_functions, (GDestroyNotify)_free_timed_function); +} + void callbacks_init(void) { p_commands = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)_free_command_hash); + p_timed_functions = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)_free_timed_function_list); p_window_callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)_free_window_callback); } @@ -125,6 +137,7 @@ void callbacks_close(void) { g_hash_table_destroy(p_commands); + g_hash_table_destroy(p_timed_functions); g_hash_table_destroy(p_window_callbacks); } @@ -144,9 +157,16 @@ callbacks_add_command(const char *const plugin_name, PluginCommand *command) } void -callbacks_add_timed(PluginTimedFunction *timed_function) +callbacks_add_timed(const char *const plugin_name, PluginTimedFunction *timed_function) { - p_timed_functions = g_slist_append(p_timed_functions, timed_function); + GList *timed_function_list = g_hash_table_lookup(p_timed_functions, plugin_name); + if (timed_function_list) { + timed_function_list = g_list_append(timed_function_list, timed_function); + g_hash_table_replace(p_timed_functions, strdup(plugin_name), timed_function_list); + } else { + timed_function_list = g_list_append(timed_function_list, timed_function); + g_hash_table_insert(p_timed_functions, strdup(plugin_name), timed_function_list); + } } void @@ -225,20 +245,28 @@ plugins_get_help(const char *const cmd) void plugins_run_timed(void) { - GSList *p_timed_function = p_timed_functions; + GList *timed_functions_lists = g_hash_table_get_values(p_timed_functions); - while (p_timed_function) { - PluginTimedFunction *timed_function = p_timed_function->data; - gdouble elapsed = g_timer_elapsed(timed_function->timer, NULL); + GList *curr_list = timed_functions_lists; + while (curr_list) { + GList *timed_function_list = curr_list->data; + GList *curr = timed_function_list; + while (curr) { + PluginTimedFunction *timed_function = curr->data; - if (timed_function->interval_seconds > 0 && elapsed >= timed_function->interval_seconds) { - timed_function->callback_exec(timed_function); - g_timer_start(timed_function->timer); - } + gdouble elapsed = g_timer_elapsed(timed_function->timer, NULL); - p_timed_function = g_slist_next(p_timed_function); + if (timed_function->interval_seconds > 0 && elapsed >= timed_function->interval_seconds) { + timed_function->callback_exec(timed_function); + g_timer_start(timed_function->timer); + } + + curr = g_list_next(curr); + } + curr_list = g_list_next(curr_list); } - return; + + g_list_free(timed_functions_lists); } GList* diff --git a/src/plugins/callbacks.h b/src/plugins/callbacks.h index fa78d2c6..7284ba28 100644 --- a/src/plugins/callbacks.h +++ b/src/plugins/callbacks.h @@ -66,8 +66,8 @@ typedef struct p_window_input_callback { void callbacks_init(void); void callbacks_close(void); -void callbacks_add_command(const char *const plugin, PluginCommand *command); -void callbacks_add_timed(PluginTimedFunction *timed_function); +void callbacks_add_command(const char *const plugin_name, PluginCommand *command); +void callbacks_add_timed(const char *const plugin_name, PluginTimedFunction *timed_function); void callbacks_add_window_handler(const char *tag, PluginWindowCallback *window_callback); void * callbacks_get_window_handler(const char *tag); |