1 |
/*************************************************************************** |
2 |
* * |
3 |
* Copyright (C) 2007 - 2016 Christian Schoenebeck * |
4 |
* * |
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 |
#include "../common/global_private.h" |
24 |
#include "../Sampler.h" |
25 |
|
26 |
#if defined(WIN32) |
27 |
#include <windows.h> |
28 |
#else |
29 |
#include <dlfcn.h> |
30 |
#include <errno.h> |
31 |
#include <dirent.h> |
32 |
#include <sys/types.h> |
33 |
#include <sys/stat.h> |
34 |
#include <unistd.h> |
35 |
#endif |
36 |
#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 |
void InstrumentEditorFactory::LoadPlugins() { |
102 |
if (!bPluginsLoaded) { |
103 |
dmsg(1,("Loading instrument editor plugins...")); |
104 |
// getenv() is available on Posix and Windows |
105 |
char* pcPluginDir = getenv("LINUXSAMPLER_PLUGIN_DIR"); |
106 |
#if defined(WIN32) |
107 |
String installDir = Sampler::GetInstallDir(); |
108 |
String pluginDir; |
109 |
if (pcPluginDir) |
110 |
pluginDir = pcPluginDir; |
111 |
if (pluginDir.empty()) |
112 |
pluginDir = installDir + "\\plugins"; |
113 |
|
114 |
// Put the LS installation directory first in DLL search |
115 |
// path, so the plugin finds for example the bundled GTK |
116 |
// libraries before any other installed versions |
117 |
if (!installDir.empty()) { |
118 |
// The SetDllDirectory function is only available on |
119 |
// XP and higher, so we call it dynamically |
120 |
HMODULE k32 = GetModuleHandleA("kernel32.dll"); |
121 |
if (k32) { |
122 |
BOOL WINAPI (*setDllDirectory)(LPCSTR) = |
123 |
(BOOL WINAPI (*)(LPCSTR))GetProcAddress(k32, "SetDllDirectoryA"); |
124 |
if (setDllDirectory) { |
125 |
setDllDirectory(installDir.c_str()); |
126 |
} |
127 |
} |
128 |
} |
129 |
|
130 |
if (pluginDir.empty() || !LoadPlugins(pluginDir)) { |
131 |
if (!LoadPlugins(CONFIG_PLUGIN_DIR)) { |
132 |
std::cerr << "Could not open instrument editor plugins " |
133 |
<< "directory ('" << pluginDir << "' or '" |
134 |
<< CONFIG_PLUGIN_DIR << "'), Error: " |
135 |
<< GetLastError() << std::endl; |
136 |
return; |
137 |
} |
138 |
} |
139 |
#else |
140 |
String dir; |
141 |
if (pcPluginDir) |
142 |
dir = pcPluginDir; |
143 |
if (dir.empty()) |
144 |
dir = CONFIG_PLUGIN_DIR; |
145 |
if (!LoadPlugins(dir)) { |
146 |
std::cerr << "Could not open instrument editor plugins " |
147 |
<< "directory ('" << dir << "'): " |
148 |
<< strerror(errno) << std::endl; |
149 |
return; |
150 |
} |
151 |
#endif |
152 |
bPluginsLoaded = true; |
153 |
dmsg(1,("OK\n")); |
154 |
} |
155 |
} |
156 |
|
157 |
bool InstrumentEditorFactory::LoadPlugins(String plugindir) { |
158 |
#if defined(WIN32) |
159 |
WIN32_FIND_DATA win32FindData; |
160 |
const String pluginpattern = plugindir + "\\*.dll"; |
161 |
HANDLE hDir = FindFirstFile(pluginpattern.c_str(), &win32FindData); |
162 |
if (hDir == INVALID_HANDLE_VALUE) { |
163 |
if (GetLastError() != ERROR_FILE_NOT_FOUND) { |
164 |
return false; |
165 |
} else { |
166 |
dmsg(1,("None")); |
167 |
return true; |
168 |
} |
169 |
} |
170 |
|
171 |
do { |
172 |
// skip directory entries |
173 |
if (win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
174 |
continue; |
175 |
// dir entry name as full qualified path |
176 |
const String sPath = plugindir + "\\" + win32FindData.cFileName; |
177 |
// load the DLL |
178 |
HINSTANCE hinstLib; |
179 |
void* pDLL = hinstLib = LoadLibrary(sPath.c_str()); |
180 |
if (!pDLL) { |
181 |
std::cerr << "Failed to load instrument editor plugin: " |
182 |
<< sPath << std::endl; |
183 |
continue; |
184 |
} |
185 |
|
186 |
InnerFactory* (*fn)() = (InnerFactory* (*)()) |
187 |
GetProcAddress(hinstLib, "createInstrumentEditorInnerFactory"); |
188 |
if (fn == NULL) { |
189 |
std::cerr << "ERROR: unable to find " |
190 |
"createInstrumentEditorInnerFactory() " |
191 |
"in DLL\n" << std::flush; |
192 |
FreeLibrary(hinstLib); |
193 |
continue; |
194 |
} |
195 |
|
196 |
// get the plugin instance and register it to the factory |
197 |
|
198 |
InnerFactory* pInnerFactory = fn(); |
199 |
if (!pInnerFactory) { |
200 |
std::cerr << "ERROR: !pInnerFactory\n" << std::flush; |
201 |
FreeLibrary(hinstLib); |
202 |
continue; |
203 |
} |
204 |
InstrumentEditor* pEditor = pInnerFactory->Create(); |
205 |
if (InnerFactories.count(pEditor->Name())) { |
206 |
std::cerr << "ERROR: a plugin with name '" |
207 |
<< pEditor->Name() |
208 |
<< "' already loaded (skipping)\n" |
209 |
<< std::flush; |
210 |
pInnerFactory->Destroy(pEditor); |
211 |
FreeLibrary(hinstLib); |
212 |
continue; |
213 |
} |
214 |
InnerFactories[pEditor->Name()] = pInnerFactory; |
215 |
pInnerFactory->Destroy(pEditor); |
216 |
|
217 |
LoadedDLLs.push_back(pDLL); |
218 |
} while (FindNextFile(hDir, &win32FindData)); |
219 |
|
220 |
if (hDir != INVALID_HANDLE_VALUE) FindClose(hDir); |
221 |
|
222 |
#else // POSIX |
223 |
|
224 |
#if defined(__APPLE__) /* 20071224 Toshi Nagata */ |
225 |
if (plugindir.find("~") == 0) |
226 |
plugindir.replace(0, 1, getenv("HOME")); |
227 |
#endif |
228 |
DIR* hDir = opendir(plugindir.c_str()); |
229 |
if (!hDir) { |
230 |
return false; |
231 |
} |
232 |
for (dirent* pEntry = readdir(hDir); pEntry; pEntry = readdir(hDir)) { |
233 |
// dir entry name as full qualified path |
234 |
const String sPath = plugindir + "/" + pEntry->d_name; |
235 |
// skip entries that are not regular files |
236 |
struct stat entry_stat; |
237 |
if (lstat(sPath.c_str(), &entry_stat) != 0 || |
238 |
(entry_stat.st_mode & S_IFMT) != S_IFREG) |
239 |
continue; |
240 |
// skip files that are not .so files |
241 |
if (sPath.length() < 3 || |
242 |
(sPath.substr(sPath.length() - 3) != ".so" && |
243 |
sPath.find(".so.") == String::npos) ) |
244 |
continue; |
245 |
// load the DLL (the plugins should register themselfes automatically) |
246 |
void* pDLL = dlopen(sPath.c_str(), RTLD_NOW); |
247 |
if (pDLL) LoadedDLLs.push_back(pDLL); |
248 |
else { |
249 |
std::cerr << "Failed to load instrument editor plugin: '" |
250 |
<< sPath << "', cause: " << dlerror() << std::endl; |
251 |
} |
252 |
} |
253 |
closedir(hDir); |
254 |
#endif |
255 |
return true; |
256 |
} |
257 |
|
258 |
void InstrumentEditorFactory::ClosePlugins() { |
259 |
if (LoadedDLLs.size()) { |
260 |
dmsg(1,("Unloading instrument editor plugins...")); |
261 |
// free all inner factories |
262 |
{ |
263 |
std::map<String, InnerFactory*>::iterator iter = InnerFactories.begin(); |
264 |
for (; iter != InnerFactories.end(); iter++) delete iter->second; |
265 |
InnerFactories.clear(); |
266 |
} |
267 |
// free the DLLs |
268 |
{ |
269 |
std::list<void*>::iterator iter = LoadedDLLs.begin(); |
270 |
for (; iter != LoadedDLLs.end(); iter++) { |
271 |
#if defined(WIN32) |
272 |
FreeLibrary((HINSTANCE)*iter); |
273 |
#else |
274 |
dlclose(*iter); |
275 |
#endif |
276 |
} |
277 |
LoadedDLLs.clear(); |
278 |
dmsg(1,("OK\n")); |
279 |
} |
280 |
} |
281 |
bPluginsLoaded = false; |
282 |
} |
283 |
|
284 |
} // namespace LinuxSampler |