/[svn]/linuxsampler/trunk/src/plugins/InstrumentEditorFactory.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/plugins/InstrumentEditorFactory.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1951 - (show annotations) (download)
Wed Jul 29 15:31:09 2009 UTC (10 years ago) by persson
File size: 11052 byte(s)
* ASIO fixes: avoid initializing the device twice, avoid throwing
  exception when getting parameters from a disconnected device
* Windows: add the installation directory to the DLL search path when
  loading an editor plugin (solves problems with VST and gigedit on
  systems with other GTK versions installed)
* fixed minor memory leak in ALSA MIDI driver

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

  ViewVC Help
Powered by ViewVC