From 5cabcf9b79880d550782da2967b9d67a531a8f7d Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 7 Mar 2016 20:36:25 +0000 Subject: [PATCH] Plugins: Added callbacks init and close functions Included destroy function in PluginWindowCallback --- src/plugins/api.c | 6 +++++- src/plugins/api.h | 5 ++++- src/plugins/c_api.c | 2 +- src/plugins/callbacks.c | 25 +++++++++++++++++++++---- src/plugins/callbacks.h | 4 ++++ src/plugins/plugins.c | 2 ++ 6 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/plugins/api.c b/src/plugins/api.c index a3740f2f..e81467a9 100644 --- a/src/plugins/api.c +++ b/src/plugins/api.c @@ -239,12 +239,16 @@ api_win_exists(const char *tag) } void -api_win_create(const char *tag, void *callback, +api_win_create( + const char *tag, + void *callback, + void(*destroy)(void *callback), void(*callback_func)(PluginWindowCallback *window_callback, const char *tag, const char * const line)) { PluginWindowCallback *window = malloc(sizeof(PluginWindowCallback)); window->callback = callback; window->callback_func = callback_func; + window->destroy = destroy; callbacks_add_window_handler(tag, window); wins_new_plugin(tag); diff --git a/src/plugins/api.h b/src/plugins/api.h index b56be781..6494097e 100644 --- a/src/plugins/api.h +++ b/src/plugins/api.h @@ -61,7 +61,10 @@ void api_log_warning(const char *message); void api_log_error(const char *message); int api_win_exists(const char *tag); -void api_win_create(const char *tag, void *callback, +void api_win_create( + const char *tag, + void *callback, + void(*destroy)(void *callback), void(*callback_func)(PluginWindowCallback *window_callback, char *tag, char *line)); int api_win_focus(const char *tag); int api_win_show(const char *tag, const char *line); diff --git a/src/plugins/c_api.c b/src/plugins/c_api.c index d8619c49..f2cce6a6 100644 --- a/src/plugins/c_api.c +++ b/src/plugins/c_api.c @@ -167,7 +167,7 @@ c_api_win_create(char *tag, void(*callback)(char *tag, char *line)) { WindowWrapper *wrapper = malloc(sizeof(WindowWrapper)); wrapper->func = callback; - api_win_create(tag, wrapper, c_window_callback); + api_win_create(tag, wrapper, free, c_window_callback); } static int diff --git a/src/plugins/callbacks.c b/src/plugins/callbacks.c index a3cbfc1d..6f796259 100644 --- a/src/plugins/callbacks.c +++ b/src/plugins/callbacks.c @@ -46,6 +46,27 @@ static GSList *p_commands = NULL; static GSList *p_timed_functions = NULL; static GHashTable *p_window_callbacks = NULL; +static void +_free_window_callback(PluginWindowCallback *window_callback) +{ + if (window_callback->destroy) { + window_callback->destroy(window_callback->callback); + } + free(window_callback); +} + +void +callbacks_init(void) +{ + p_window_callbacks = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)_free_window_callback); +} + +void +callbacks_close(void) +{ + g_hash_table_destroy(p_window_callbacks); +} + void callbacks_add_command(PluginCommand *command) { @@ -64,10 +85,6 @@ callbacks_add_timed(PluginTimedFunction *timed_function) void callbacks_add_window_handler(const char *tag, PluginWindowCallback *window_callback) { - if (p_window_callbacks == NULL) { - p_window_callbacks = g_hash_table_new(g_str_hash, g_str_equal); - } - g_hash_table_insert(p_window_callbacks, strdup(tag), window_callback); } diff --git a/src/plugins/callbacks.h b/src/plugins/callbacks.h index 09dc0375..dc6b78bb 100644 --- a/src/plugins/callbacks.h +++ b/src/plugins/callbacks.h @@ -57,9 +57,13 @@ typedef struct p_timed_function { typedef struct p_window_input_callback { void *callback; + void (*destroy)(void *callback); void (*callback_func)(struct p_window_input_callback *window_callback, const char *tag, const char * const line); } PluginWindowCallback; +void callbacks_init(void); +void callbacks_close(void); + void callbacks_add_command(PluginCommand *command); void callbacks_add_timed(PluginTimedFunction *timed_function); void callbacks_add_window_handler(const char *tag, PluginWindowCallback *window_callback); diff --git a/src/plugins/plugins.c b/src/plugins/plugins.c index 20293c88..c1c7df3a 100644 --- a/src/plugins/plugins.c +++ b/src/plugins/plugins.c @@ -57,6 +57,7 @@ void plugins_init(void) { plugins = NULL; + callbacks_init(); autocompleters_init(); plugin_themes_init(); @@ -404,6 +405,7 @@ plugins_shutdown(void) autocompleters_destroy(); plugin_themes_close(); + callbacks_close(); } gchar *