1 |
schoenebeck |
1374 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
schoenebeck |
4010 |
* Copyright (C) 2007 - 2021 Christian Schoenebeck * |
4 |
schoenebeck |
1374 |
* * |
5 |
|
|
* This program is free software; you can redistribute it and/or modify * |
6 |
|
|
* it under the terms of the GNU General Public License as published by * |
7 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
8 |
|
|
* (at your option) any later version. * |
9 |
|
|
* * |
10 |
|
|
* This program is distributed in the hope that it will be useful, * |
11 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 |
|
|
* GNU General Public License for more details. * |
14 |
|
|
* * |
15 |
|
|
* You should have received a copy of the GNU General Public License * |
16 |
|
|
* along with this program; if not, write to the Free Software * |
17 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
18 |
|
|
* MA 02111-1307 USA * |
19 |
|
|
***************************************************************************/ |
20 |
|
|
|
21 |
|
|
#include "InstrumentEditorFactory.h" |
22 |
|
|
|
23 |
schoenebeck |
1425 |
#include "../common/global_private.h" |
24 |
persson |
1897 |
#include "../Sampler.h" |
25 |
schoenebeck |
1425 |
|
26 |
senoner |
1478 |
#if defined(WIN32) |
27 |
|
|
#include <windows.h> |
28 |
|
|
#else |
29 |
schoenebeck |
1374 |
#include <dlfcn.h> |
30 |
|
|
#include <errno.h> |
31 |
senoner |
1478 |
#include <dirent.h> |
32 |
schoenebeck |
1601 |
#include <sys/types.h> |
33 |
|
|
#include <sys/stat.h> |
34 |
|
|
#include <unistd.h> |
35 |
senoner |
1478 |
#endif |
36 |
schoenebeck |
1374 |
#include <string.h> |
37 |
|
|
|
38 |
|
|
#ifndef CONFIG_PLUGIN_DIR |
39 |
|
|
# error "Configuration macro CONFIG_PLUGIN_DIR not defined!" |
40 |
|
|
#endif // CONFIG_PLUGIN_DIR |
41 |
|
|
|
42 |
|
|
namespace LinuxSampler { |
43 |
|
|
|
44 |
|
|
std::map<String, InstrumentEditorFactory::InnerFactory*> InstrumentEditorFactory::InnerFactories; |
45 |
|
|
|
46 |
|
|
bool InstrumentEditorFactory::bPluginsLoaded = false; |
47 |
|
|
|
48 |
|
|
std::list<void*> InstrumentEditorFactory::LoadedDLLs; |
49 |
|
|
|
50 |
|
|
std::vector<String> InstrumentEditorFactory::AvailableEditors() { |
51 |
|
|
// make sure plugins were loaded already |
52 |
|
|
LoadPlugins(); |
53 |
|
|
// render result |
54 |
|
|
std::vector<String> result; |
55 |
|
|
std::map<String, InnerFactory*>::iterator iter = InnerFactories.begin(); |
56 |
|
|
for (; iter != InnerFactories.end(); iter++) |
57 |
|
|
result.push_back(iter->first); |
58 |
|
|
return result; |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
std::vector<String> InstrumentEditorFactory::MatchingEditors(String sTypeName, String sTypeVersion) { |
62 |
|
|
// make sure plugins were loaded already |
63 |
|
|
LoadPlugins(); |
64 |
|
|
// render result |
65 |
|
|
std::vector<String> result; |
66 |
|
|
std::map<String, InnerFactory*>::iterator iter = InnerFactories.begin(); |
67 |
|
|
for (; iter != InnerFactories.end(); iter++) { |
68 |
|
|
InstrumentEditor* pEditor = iter->second->Create(); |
69 |
|
|
if (pEditor->IsTypeSupported(sTypeName, sTypeVersion)) |
70 |
|
|
result.push_back(iter->first); |
71 |
|
|
iter->second->Destroy(pEditor); |
72 |
|
|
} |
73 |
|
|
return result; |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
String InstrumentEditorFactory::AvailableEditorsAsString() { |
77 |
|
|
std::vector<String> drivers = AvailableEditors(); |
78 |
|
|
String result; |
79 |
|
|
std::vector<String>::iterator iter = drivers.begin(); |
80 |
|
|
for (; iter != drivers.end(); iter++) { |
81 |
|
|
if (result != "") result += ","; |
82 |
|
|
result += "'" + *iter + "'"; |
83 |
|
|
} |
84 |
|
|
return result; |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
InstrumentEditor* InstrumentEditorFactory::Create(String InstrumentEditorName) throw (Exception) { |
88 |
|
|
if (InnerFactories.count(InstrumentEditorName)) { |
89 |
|
|
InnerFactory* pInnerFactory = InnerFactories[InstrumentEditorName]; |
90 |
|
|
return pInnerFactory->Create(); |
91 |
|
|
} else throw Exception("unknown instrument editor"); |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
void InstrumentEditorFactory::Destroy(InstrumentEditor* pInstrumentEditor) throw (Exception) { |
95 |
|
|
if (InnerFactories.count(pInstrumentEditor->Name())) { |
96 |
|
|
InnerFactory* pInnerFactory = InnerFactories[pInstrumentEditor->Name()]; |
97 |
|
|
return pInnerFactory->Destroy(pInstrumentEditor); |
98 |
|
|
} else throw Exception("unknown instrument editor"); |
99 |
|
|
} |
100 |
|
|
|
101 |
schoenebeck |
4010 |
std::vector<String> InstrumentEditorFactory::PluginDirs() { |
102 |
|
|
std::vector<String> dirs; |
103 |
|
|
|
104 |
|
|
// getenv() is available on POSIX and Windows |
105 |
|
|
char* pcEnvPluginDir = getenv("LINUXSAMPLER_PLUGIN_DIR"); |
106 |
|
|
if (pcEnvPluginDir) { |
107 |
|
|
String sEnvPluginDir = pcEnvPluginDir; |
108 |
|
|
if (!sEnvPluginDir.empty()) |
109 |
|
|
dirs.push_back(sEnvPluginDir); |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
#if defined(WIN32) |
113 |
|
|
if (dirs.empty()) { |
114 |
|
|
String installDir = Sampler::GetInstallDir(); |
115 |
|
|
if (!installDir.empty()) |
116 |
|
|
dirs.push_back(installDir + "\\plugins"); |
117 |
|
|
} |
118 |
|
|
#endif |
119 |
|
|
|
120 |
|
|
if (dirs.empty()) |
121 |
|
|
dirs.push_back(CONFIG_PLUGIN_DIR); |
122 |
|
|
|
123 |
|
|
return dirs; |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
String InstrumentEditorFactory::PluginDirsAsString() { |
127 |
|
|
std::vector<String> dirs = PluginDirs(); |
128 |
|
|
String result; |
129 |
|
|
for (const String& dir : dirs) { |
130 |
|
|
if (!result.empty()) result += ", "; |
131 |
|
|
result += "'" + dir + "'"; |
132 |
|
|
} |
133 |
|
|
return result; |
134 |
|
|
} |
135 |
|
|
|
136 |
schoenebeck |
1374 |
void InstrumentEditorFactory::LoadPlugins() { |
137 |
|
|
if (!bPluginsLoaded) { |
138 |
|
|
dmsg(1,("Loading instrument editor plugins...")); |
139 |
schoenebeck |
2053 |
// getenv() is available on Posix and Windows |
140 |
|
|
char* pcPluginDir = getenv("LINUXSAMPLER_PLUGIN_DIR"); |
141 |
senoner |
1478 |
#if defined(WIN32) |
142 |
schoenebeck |
2053 |
String installDir = Sampler::GetInstallDir(); |
143 |
|
|
String pluginDir; |
144 |
|
|
if (pcPluginDir) |
145 |
|
|
pluginDir = pcPluginDir; |
146 |
|
|
if (pluginDir.empty()) |
147 |
|
|
pluginDir = installDir + "\\plugins"; |
148 |
persson |
1951 |
|
149 |
|
|
// Put the LS installation directory first in DLL search |
150 |
|
|
// path, so the plugin finds for example the bundled GTK |
151 |
|
|
// libraries before any other installed versions |
152 |
schoenebeck |
2053 |
if (!installDir.empty()) { |
153 |
persson |
1951 |
// The SetDllDirectory function is only available on |
154 |
|
|
// XP and higher, so we call it dynamically |
155 |
|
|
HMODULE k32 = GetModuleHandleA("kernel32.dll"); |
156 |
|
|
if (k32) { |
157 |
|
|
BOOL WINAPI (*setDllDirectory)(LPCSTR) = |
158 |
|
|
(BOOL WINAPI (*)(LPCSTR))GetProcAddress(k32, "SetDllDirectoryA"); |
159 |
|
|
if (setDllDirectory) { |
160 |
schoenebeck |
2053 |
setDllDirectory(installDir.c_str()); |
161 |
persson |
1951 |
} |
162 |
|
|
} |
163 |
|
|
} |
164 |
|
|
|
165 |
schoenebeck |
2053 |
if (pluginDir.empty() || !LoadPlugins(pluginDir)) { |
166 |
persson |
1897 |
if (!LoadPlugins(CONFIG_PLUGIN_DIR)) { |
167 |
schoenebeck |
1602 |
std::cerr << "Could not open instrument editor plugins " |
168 |
schoenebeck |
2053 |
<< "directory ('" << pluginDir << "' or '" |
169 |
|
|
<< CONFIG_PLUGIN_DIR << "'), Error: " |
170 |
persson |
1897 |
<< GetLastError() << std::endl; |
171 |
|
|
return; |
172 |
senoner |
1478 |
} |
173 |
persson |
1897 |
} |
174 |
|
|
#else |
175 |
schoenebeck |
2053 |
String dir; |
176 |
|
|
if (pcPluginDir) |
177 |
|
|
dir = pcPluginDir; |
178 |
|
|
if (dir.empty()) |
179 |
|
|
dir = CONFIG_PLUGIN_DIR; |
180 |
|
|
if (!LoadPlugins(dir)) { |
181 |
persson |
1897 |
std::cerr << "Could not open instrument editor plugins " |
182 |
schoenebeck |
2053 |
<< "directory ('" << dir << "'): " |
183 |
persson |
1897 |
<< strerror(errno) << std::endl; |
184 |
schoenebeck |
1602 |
return; |
185 |
senoner |
1478 |
} |
186 |
persson |
1897 |
#endif |
187 |
|
|
bPluginsLoaded = true; |
188 |
|
|
dmsg(1,("OK\n")); |
189 |
|
|
} |
190 |
|
|
} |
191 |
senoner |
1478 |
|
192 |
persson |
1897 |
bool InstrumentEditorFactory::LoadPlugins(String plugindir) { |
193 |
|
|
#if defined(WIN32) |
194 |
|
|
WIN32_FIND_DATA win32FindData; |
195 |
|
|
const String pluginpattern = plugindir + "\\*.dll"; |
196 |
|
|
HANDLE hDir = FindFirstFile(pluginpattern.c_str(), &win32FindData); |
197 |
|
|
if (hDir == INVALID_HANDLE_VALUE) { |
198 |
|
|
if (GetLastError() != ERROR_FILE_NOT_FOUND) { |
199 |
|
|
return false; |
200 |
|
|
} else { |
201 |
|
|
dmsg(1,("None")); |
202 |
|
|
return true; |
203 |
|
|
} |
204 |
|
|
} |
205 |
schoenebeck |
1602 |
|
206 |
persson |
1897 |
do { |
207 |
|
|
// skip directory entries |
208 |
|
|
if (win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
209 |
|
|
continue; |
210 |
|
|
// dir entry name as full qualified path |
211 |
|
|
const String sPath = plugindir + "\\" + win32FindData.cFileName; |
212 |
|
|
// load the DLL |
213 |
|
|
HINSTANCE hinstLib; |
214 |
|
|
void* pDLL = hinstLib = LoadLibrary(sPath.c_str()); |
215 |
|
|
if (!pDLL) { |
216 |
|
|
std::cerr << "Failed to load instrument editor plugin: " |
217 |
|
|
<< sPath << std::endl; |
218 |
|
|
continue; |
219 |
|
|
} |
220 |
schoenebeck |
1602 |
|
221 |
persson |
1897 |
InnerFactory* (*fn)() = (InnerFactory* (*)()) |
222 |
|
|
GetProcAddress(hinstLib, "createInstrumentEditorInnerFactory"); |
223 |
|
|
if (fn == NULL) { |
224 |
|
|
std::cerr << "ERROR: unable to find " |
225 |
|
|
"createInstrumentEditorInnerFactory() " |
226 |
|
|
"in DLL\n" << std::flush; |
227 |
|
|
FreeLibrary(hinstLib); |
228 |
|
|
continue; |
229 |
|
|
} |
230 |
schoenebeck |
1602 |
|
231 |
persson |
1897 |
// get the plugin instance and register it to the factory |
232 |
schoenebeck |
1602 |
|
233 |
persson |
1897 |
InnerFactory* pInnerFactory = fn(); |
234 |
|
|
if (!pInnerFactory) { |
235 |
|
|
std::cerr << "ERROR: !pInnerFactory\n" << std::flush; |
236 |
|
|
FreeLibrary(hinstLib); |
237 |
|
|
continue; |
238 |
schoenebeck |
1374 |
} |
239 |
persson |
1897 |
InstrumentEditor* pEditor = pInnerFactory->Create(); |
240 |
|
|
if (InnerFactories.count(pEditor->Name())) { |
241 |
|
|
std::cerr << "ERROR: a plugin with name '" |
242 |
|
|
<< pEditor->Name() |
243 |
|
|
<< "' already loaded (skipping)\n" |
244 |
|
|
<< std::flush; |
245 |
|
|
pInnerFactory->Destroy(pEditor); |
246 |
|
|
FreeLibrary(hinstLib); |
247 |
|
|
continue; |
248 |
schoenebeck |
1374 |
} |
249 |
persson |
1897 |
InnerFactories[pEditor->Name()] = pInnerFactory; |
250 |
|
|
pInnerFactory->Destroy(pEditor); |
251 |
|
|
|
252 |
|
|
LoadedDLLs.push_back(pDLL); |
253 |
|
|
} while (FindNextFile(hDir, &win32FindData)); |
254 |
|
|
|
255 |
|
|
if (hDir != INVALID_HANDLE_VALUE) FindClose(hDir); |
256 |
|
|
|
257 |
|
|
#else // POSIX |
258 |
|
|
|
259 |
|
|
#if defined(__APPLE__) /* 20071224 Toshi Nagata */ |
260 |
|
|
if (plugindir.find("~") == 0) |
261 |
|
|
plugindir.replace(0, 1, getenv("HOME")); |
262 |
|
|
#endif |
263 |
|
|
DIR* hDir = opendir(plugindir.c_str()); |
264 |
|
|
if (!hDir) { |
265 |
|
|
return false; |
266 |
schoenebeck |
1374 |
} |
267 |
persson |
1897 |
for (dirent* pEntry = readdir(hDir); pEntry; pEntry = readdir(hDir)) { |
268 |
|
|
// dir entry name as full qualified path |
269 |
|
|
const String sPath = plugindir + "/" + pEntry->d_name; |
270 |
|
|
// skip entries that are not regular files |
271 |
|
|
struct stat entry_stat; |
272 |
|
|
if (lstat(sPath.c_str(), &entry_stat) != 0 || |
273 |
|
|
(entry_stat.st_mode & S_IFMT) != S_IFREG) |
274 |
|
|
continue; |
275 |
|
|
// skip files that are not .so files |
276 |
|
|
if (sPath.length() < 3 || |
277 |
schoenebeck |
3034 |
(sPath.substr(sPath.length() - 3) != ".so" && |
278 |
|
|
sPath.find(".so.") == String::npos) ) |
279 |
persson |
1897 |
continue; |
280 |
|
|
// load the DLL (the plugins should register themselfes automatically) |
281 |
|
|
void* pDLL = dlopen(sPath.c_str(), RTLD_NOW); |
282 |
|
|
if (pDLL) LoadedDLLs.push_back(pDLL); |
283 |
|
|
else { |
284 |
|
|
std::cerr << "Failed to load instrument editor plugin: '" |
285 |
|
|
<< sPath << "', cause: " << dlerror() << std::endl; |
286 |
|
|
} |
287 |
|
|
} |
288 |
|
|
closedir(hDir); |
289 |
|
|
#endif |
290 |
|
|
return true; |
291 |
schoenebeck |
1374 |
} |
292 |
|
|
|
293 |
schoenebeck |
4021 |
//NOTE: method currently not used at all, see FIXME comment below why |
294 |
schoenebeck |
1374 |
void InstrumentEditorFactory::ClosePlugins() { |
295 |
|
|
if (LoadedDLLs.size()) { |
296 |
|
|
dmsg(1,("Unloading instrument editor plugins...")); |
297 |
|
|
// free all inner factories |
298 |
|
|
{ |
299 |
schoenebeck |
4021 |
//FIXME: potential race with 'InnerFactoryRegistrator' |
300 |
|
|
// destructor, the latter is executed deferred when the DLL is |
301 |
|
|
// eventually unloaded by the OS, and that in turn seems to have |
302 |
|
|
// created a race when this method was called in the past on |
303 |
|
|
// LSCP "RESET" command, leading to the situation that plugins |
304 |
|
|
// were not available as bPluginsLoaded ended up true due to |
305 |
|
|
// LoadPlugins() call while the subsequent deferred destructor |
306 |
|
|
// removed the plugin instances without resetting bPluginsLoaded |
307 |
|
|
// https://sourceforge.net/p/linuxsampler/mailman/message/37411956/ |
308 |
schoenebeck |
1374 |
std::map<String, InnerFactory*>::iterator iter = InnerFactories.begin(); |
309 |
|
|
for (; iter != InnerFactories.end(); iter++) delete iter->second; |
310 |
|
|
InnerFactories.clear(); |
311 |
|
|
} |
312 |
|
|
// free the DLLs |
313 |
|
|
{ |
314 |
|
|
std::list<void*>::iterator iter = LoadedDLLs.begin(); |
315 |
senoner |
1478 |
for (; iter != LoadedDLLs.end(); iter++) { |
316 |
|
|
#if defined(WIN32) |
317 |
|
|
FreeLibrary((HINSTANCE)*iter); |
318 |
|
|
#else |
319 |
|
|
dlclose(*iter); |
320 |
|
|
#endif |
321 |
|
|
} |
322 |
schoenebeck |
1374 |
LoadedDLLs.clear(); |
323 |
|
|
dmsg(1,("OK\n")); |
324 |
|
|
} |
325 |
|
|
} |
326 |
|
|
bPluginsLoaded = false; |
327 |
|
|
} |
328 |
|
|
|
329 |
|
|
} // namespace LinuxSampler |