diff options
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/c_plugins.c | 5 | ||||
-rw-r--r-- | src/plugins/plugins.c | 99 | ||||
-rw-r--r-- | src/plugins/plugins.h | 4 | ||||
-rw-r--r-- | src/plugins/python_plugins.c | 2 |
4 files changed, 88 insertions, 22 deletions
diff --git a/src/plugins/c_plugins.c b/src/plugins/c_plugins.c index 07a618b6..ff05791d 100644 --- a/src/plugins/c_plugins.c +++ b/src/plugins/c_plugins.c @@ -74,10 +74,8 @@ c_plugin_create(const char *const filename) return NULL; } - gchar *module_name = g_strndup(filename, strlen(filename) - 3); - plugin = malloc(sizeof(ProfPlugin)); - plugin->name = strdup(module_name); + plugin->name = strdup(filename); plugin->lang = LANG_C; plugin->module = handle; plugin->init_func = c_init_hook; @@ -110,7 +108,6 @@ c_plugin_create(const char *const filename) plugin->on_room_win_focus = c_on_room_win_focus_hook; g_string_free(path, TRUE); - g_free(module_name); return plugin; } diff --git a/src/plugins/plugins.c b/src/plugins/plugins.c index 73f3f873..e2d0bdf3 100644 --- a/src/plugins/plugins.c +++ b/src/plugins/plugins.c @@ -78,13 +78,13 @@ plugins_init(void) plugin_settings_init(); // load plugins - gchar **plugins_load = prefs_get_plugins(); - if (plugins_load) { + gchar **plugins_pref = prefs_get_plugins(); + if (plugins_pref) { int i; - for (i = 0; i < g_strv_length(plugins_load); i++) + for (i = 0; i < g_strv_length(plugins_pref); i++) { gboolean loaded = FALSE; - gchar *filename = plugins_load[i]; + gchar *filename = plugins_pref[i]; #ifdef HAVE_PYTHON if (g_str_has_suffix(filename, ".py")) { ProfPlugin *plugin = python_plugin_create(filename); @@ -103,8 +103,10 @@ plugins_init(void) } } #endif - if (loaded == TRUE) { + if (loaded) { log_info("Loaded plugin: %s", filename); + } else { + log_info("Failed to load plugin: %s", filename); } } @@ -117,29 +119,94 @@ plugins_init(void) } } - prefs_free_plugins(plugins_load); + prefs_free_plugins(plugins_pref); return; } +gboolean +_find_by_name(gconstpointer pluginp, gconstpointer namep) +{ + char *name = (char*)namep; + ProfPlugin *plugin = (ProfPlugin*)pluginp; + + return g_strcmp0(name, plugin->name); +} + +gboolean +plugins_load(const char *const name) +{ + GSList *found = g_slist_find_custom(plugins, name, (GCompareFunc)_find_by_name); + if (found) { + log_info("Failed to load plugin: %s, plugin already loaded", name); + return FALSE; + } + + ProfPlugin *plugin = NULL; +#ifdef HAVE_PYTHON + if (g_str_has_suffix(name, ".py")) { + plugin = python_plugin_create(name); + } +#endif +#ifdef HAVE_C + if (g_str_has_suffix(name, ".so")) { + plugin = c_plugin_create(name); + } +#endif + if (plugin) { + plugins = g_slist_append(plugins, plugin); + plugin->init_func(plugin, PACKAGE_VERSION, PACKAGE_STATUS); + log_info("Loaded plugin: %s", name); + return TRUE; + } else { + log_info("Failed to load plugin: %s", name); + return FALSE; + } +} + GSList * plugins_get_list(void) { return plugins; } -char * -plugins_get_lang_string(ProfPlugin *plugin) +static gchar* +_get_plugins_dir(void) +{ + gchar *xdg_data = xdg_get_data_home(); + GString *plugins_dir = g_string_new(xdg_data); + g_free(xdg_data); + g_string_append(plugins_dir, "/profanity/plugins"); + return g_string_free(plugins_dir, FALSE); +} + +void +_plugins_list_dir(const gchar *const dir, GSList **result) { - switch (plugin->lang) - { - case LANG_PYTHON: - return "Python"; - case LANG_C: - return "C"; - default: - return "Unknown"; + GDir *plugins = g_dir_open(dir, 0, NULL); + if (plugins == NULL) { + return; } + + const gchar *plugin = g_dir_read_name(plugins); + while (plugin) { + if (g_str_has_suffix(plugin, ".so") || g_str_has_suffix(plugin, ".py")) { + *result = g_slist_append(*result, strdup(plugin)); + } + plugin = g_dir_read_name(plugins); + } + g_dir_close(plugins); +} + +GSList* +plugins_file_list(void) +{ + GSList *result = NULL; + char *plugins_dir = _get_plugins_dir(); + _plugins_list_dir(plugins_dir, &result); + free(plugins_dir); + + return result; } char * diff --git a/src/plugins/plugins.h b/src/plugins/plugins.h index 335bf8f5..85792674 100644 --- a/src/plugins/plugins.h +++ b/src/plugins/plugins.h @@ -99,11 +99,13 @@ typedef struct prof_plugin_t { void plugins_init(void); GSList* plugins_get_list(void); -char* plugins_get_lang_string(ProfPlugin *plugin); +GSList *plugins_file_list(void); char* plugins_autocomplete(const char *const input); void plugins_reset_autocomplete(void); void plugins_shutdown(void); +gboolean plugins_load(const char *const name); + void plugins_on_start(void); void plugins_on_shutdown(void); diff --git a/src/plugins/python_plugins.c b/src/plugins/python_plugins.c index 90ff36ed..a73cc15b 100644 --- a/src/plugins/python_plugins.c +++ b/src/plugins/python_plugins.c @@ -95,7 +95,7 @@ python_plugin_create(const char *const filename) python_check_error(); if (p_module) { ProfPlugin *plugin = malloc(sizeof(ProfPlugin)); - plugin->name = strdup(module_name); + plugin->name = strdup(filename); plugin->lang = LANG_PYTHON; plugin->module = p_module; plugin->init_func = python_init_hook; |