20 |
|
|
21 |
#include "InstrumentEditorFactory.h" |
#include "InstrumentEditorFactory.h" |
22 |
|
|
23 |
|
#include "../common/global_private.h" |
24 |
|
|
25 |
|
#if defined(WIN32) |
26 |
|
#include <windows.h> |
27 |
|
#else |
28 |
#include <dlfcn.h> |
#include <dlfcn.h> |
29 |
#include <errno.h> |
#include <errno.h> |
|
#include <string.h> |
|
30 |
#include <dirent.h> |
#include <dirent.h> |
31 |
|
#endif |
32 |
|
#include <string.h> |
33 |
|
|
34 |
#ifndef CONFIG_PLUGIN_DIR |
#ifndef CONFIG_PLUGIN_DIR |
35 |
# error "Configuration macro CONFIG_PLUGIN_DIR not defined!" |
# error "Configuration macro CONFIG_PLUGIN_DIR not defined!" |
36 |
#endif // CONFIG_PLUGIN_DIR |
#endif // CONFIG_PLUGIN_DIR |
37 |
|
|
38 |
|
#if defined(WIN32) |
39 |
|
typedef void* (*InnerFactoryRegisterFunction)(void); |
40 |
|
#endif |
41 |
|
|
42 |
namespace LinuxSampler { |
namespace LinuxSampler { |
43 |
|
|
44 |
std::map<String, InstrumentEditorFactory::InnerFactory*> InstrumentEditorFactory::InnerFactories; |
std::map<String, InstrumentEditorFactory::InnerFactory*> InstrumentEditorFactory::InnerFactories; |
101 |
void InstrumentEditorFactory::LoadPlugins() { |
void InstrumentEditorFactory::LoadPlugins() { |
102 |
if (!bPluginsLoaded) { |
if (!bPluginsLoaded) { |
103 |
dmsg(1,("Loading instrument editor plugins...")); |
dmsg(1,("Loading instrument editor plugins...")); |
104 |
|
#if defined(WIN32) |
105 |
|
bool firstFileFound = true; |
106 |
|
WIN32_FIND_DATA win32FindData; |
107 |
|
String plugindir = (String)CONFIG_PLUGIN_DIR + (String)("\\*.DLL"); |
108 |
|
HANDLE hDir = FindFirstFile(plugindir.c_str(), &win32FindData); |
109 |
|
if (hDir == INVALID_HANDLE_VALUE) { |
110 |
|
if(GetLastError() != ERROR_FILE_NOT_FOUND) { |
111 |
|
std::cerr << "Could not open instrument editor plugins directory " |
112 |
|
<< "(" << CONFIG_PLUGIN_DIR << "): Error " |
113 |
|
<< GetLastError() << std::endl; |
114 |
|
return; |
115 |
|
} |
116 |
|
else { |
117 |
|
firstFileFound = false; |
118 |
|
} |
119 |
|
} |
120 |
|
|
121 |
|
while(GetLastError() != ERROR_NO_MORE_FILES && firstFileFound) { |
122 |
|
if(!(win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { |
123 |
|
String sPath = (String)CONFIG_PLUGIN_DIR + ((String)"\\" + (String)win32FindData.cFileName); |
124 |
|
// load the DLL (the plugins should register themselfes automatically) |
125 |
|
HINSTANCE hinstLib; |
126 |
|
void* pDLL = hinstLib = LoadLibrary( sPath.c_str() ); |
127 |
|
if (!pDLL) { |
128 |
|
std::cerr << "Failed to load instrument editor plugin: " |
129 |
|
<< sPath << std::endl; |
130 |
|
continue; |
131 |
|
} |
132 |
|
|
133 |
|
//(InnerFactory*) (*fn)(void); |
134 |
|
InnerFactoryRegisterFunction fn; |
135 |
|
fn = (InnerFactoryRegisterFunction) |
136 |
|
GetProcAddress( |
137 |
|
hinstLib, |
138 |
|
"createInstrumentEditorInnerFactory" |
139 |
|
); |
140 |
|
if (fn == NULL) { |
141 |
|
std::cerr << "ERROR: unable to find " |
142 |
|
"createInstrumentEditorInnerFactory() " |
143 |
|
"in DLL\n" << std::flush; |
144 |
|
FreeLibrary(hinstLib); |
145 |
|
continue; |
146 |
|
} |
147 |
|
|
148 |
|
// get the plugin instance and register it to the factory |
149 |
|
|
150 |
|
InnerFactory* pInnerFactory = (InnerFactory*)fn(); |
151 |
|
if (!pInnerFactory) { |
152 |
|
std::cerr << "ERROR: !pInnerFactory\n" << std::flush; |
153 |
|
FreeLibrary(hinstLib); |
154 |
|
continue; |
155 |
|
} |
156 |
|
InstrumentEditor* pEditor = pInnerFactory->Create(); |
157 |
|
if (InnerFactories.count(pEditor->Name())) { |
158 |
|
std::cerr << "ERROR: a plugin with name '" |
159 |
|
<< pEditor->Name() |
160 |
|
<< "' already loaded (skipping)\n" |
161 |
|
<< std::flush; |
162 |
|
pInnerFactory->Destroy(pEditor); |
163 |
|
FreeLibrary(hinstLib); |
164 |
|
continue; |
165 |
|
} |
166 |
|
InnerFactories[pEditor->Name()] = pInnerFactory; |
167 |
|
pInnerFactory->Destroy(pEditor); |
168 |
|
|
169 |
|
LoadedDLLs.push_back(pDLL); |
170 |
|
} |
171 |
|
int res = FindNextFile(hDir, &win32FindData); |
172 |
|
if(res == 0 && GetLastError() != ERROR_NO_MORE_FILES) { |
173 |
|
std::cerr << "Error while reading plugins directory FindNextFile Error " |
174 |
|
<< GetLastError() << std::endl; |
175 |
|
return; |
176 |
|
} |
177 |
|
} |
178 |
|
FindClose(hDir); |
179 |
|
#else // POSIX |
180 |
DIR* hDir = opendir(CONFIG_PLUGIN_DIR); |
DIR* hDir = opendir(CONFIG_PLUGIN_DIR); |
181 |
if (!hDir) { |
if (!hDir) { |
182 |
std::cerr << "Could not open instrument editor plugins directory " |
std::cerr << "Could not open instrument editor plugins directory " |
204 |
} |
} |
205 |
} |
} |
206 |
closedir(hDir); |
closedir(hDir); |
207 |
|
#endif |
208 |
bPluginsLoaded = true; |
bPluginsLoaded = true; |
209 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
210 |
} |
} |
222 |
// free the DLLs |
// free the DLLs |
223 |
{ |
{ |
224 |
std::list<void*>::iterator iter = LoadedDLLs.begin(); |
std::list<void*>::iterator iter = LoadedDLLs.begin(); |
225 |
for (; iter != LoadedDLLs.end(); iter++) dlclose(*iter); |
for (; iter != LoadedDLLs.end(); iter++) { |
226 |
|
#if defined(WIN32) |
227 |
|
FreeLibrary((HINSTANCE)*iter); |
228 |
|
#else |
229 |
|
dlclose(*iter); |
230 |
|
#endif |
231 |
|
} |
232 |
LoadedDLLs.clear(); |
LoadedDLLs.clear(); |
233 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
234 |
} |
} |