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 ec72a576..976a6698 100644 --- a/src/plugins/plugins.c +++ b/src/plugins/plugins.c @@ -63,6 +63,7 @@ void plugins_init(void) { plugins = NULL; + callbacks_init(); autocompleters_init(); #ifdef PROF_HAVE_PYTHON @@ -434,6 +435,7 @@ plugins_shutdown(void) autocompleters_destroy(); plugin_themes_close(); + callbacks_close(); } gchar * diff --git a/src/plugins/python_api.c b/src/plugins/python_api.c index 22b99486..a3964889 100644 --- a/src/plugins/python_api.c +++ b/src/plugins/python_api.c @@ -325,7 +325,7 @@ python_api_win_create(PyObject *self, PyObject *args) } if (p_callback && PyCallable_Check(p_callback)) { - api_win_create(tag, p_callback, python_window_callback); + api_win_create(tag, p_callback, NULL, python_window_callback); } return Py_BuildValue("");