2 #include "lv_plugin_registry.h"
9 #include "lv_libvisual.h"
10 #include "lv_module.hpp"
13 #include <unordered_map>
19 typedef std::unordered_map<PluginType, PluginList, std::hash<int>> PluginListMap;
21 typedef const VisPluginInfo *(*PluginGetInfoFunc)();
24 class PluginRegistry::Impl
28 std::vector<std::string> plugin_paths;
30 PluginListMap plugin_list_map;
32 PluginList get_plugins_from_dir (std::string
const& dir)
const;
35 PluginRef* load_plugin_ref (std::string
const& plugin_path)
39 auto module = Module::load (plugin_path);
45 auto plugin_version =
static_cast<int*
> (module->get_symbol (VISUAL_PLUGIN_VERSION_TAG));
49 plugin_path.c_str (), visual_get_version ());
53 auto get_plugin_info =
54 reinterpret_cast<PluginGetInfoFunc
> (module->get_symbol (
"get_plugin_info"));
56 if (!get_plugin_info) {
61 auto plugin_info = get_plugin_info ();
68 auto ref =
new PluginRef;
69 ref->info = plugin_info;
70 ref->file = plugin_path;
77 LV_API PluginRegistry* Singleton<PluginRegistry>::m_instance =
nullptr;
79 void PluginRegistry::init ()
82 m_instance =
new PluginRegistry;
85 PluginRegistry::PluginRegistry ()
91 add_path (VISUAL_PLUGIN_PATH
"/actor");
92 add_path (VISUAL_PLUGIN_PATH
"/input");
93 add_path (VISUAL_PLUGIN_PATH
"/morph");
94 add_path (VISUAL_PLUGIN_PATH
"/transform");
96 #if defined(VISUAL_OS_POSIX)
98 auto const home_env = std::getenv (
"HOME");
101 std::string home_dir {home_env};
103 add_path (home_dir +
"/.libvisual/actor");
104 add_path (home_dir +
"/.libvisual/input");
105 add_path (home_dir +
"/.libvisual/morph");
106 add_path (home_dir +
"/.libvisual/transform");
120 m_impl->plugin_paths.push_back (path);
122 auto plugins = m_impl->get_plugins_from_dir (path);
124 for (
auto& plugin : plugins)
126 auto& list = m_impl->plugin_list_map[plugin.info->type];
127 list.push_back (plugin);
131 PluginRef const* PluginRegistry::find_plugin (
PluginType type, std::string
const& name)
const
134 if (name == plugin.info->plugname) {
144 return find_plugin (type, name) !=
nullptr;
149 static PluginList empty;
151 auto match = m_impl->plugin_list_map.find (type);
152 if (match == m_impl->plugin_list_map.end ())
155 return match->second;
160 auto ref = find_plugin (type, name);
162 return ref ? ref->info :
nullptr;
165 PluginList PluginRegistry::Impl::get_plugins_from_dir (std::string
const& dir)
const
170 for_each_file_in_dir (dir,
171 [&] (std::string
const& path) ->
bool {
174 [&] (std::string
const& path) ->
bool {
175 auto ref = load_plugin_ref (path);
179 list.push_back (*ref);