/[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 1897 - (show annotations) (download)
Sun May 10 09:31:51 2009 UTC (14 years, 11 months ago) by persson
File size: 10297 byte(s)
* Windows: look for editor plugins and Fantasia using base directory
  of liblinuxsampler dll
* lscp bugfix: SET CHANNEL MIDI_INPUT_TYPE didn't work with the MME
  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 if (dir.empty() || !LoadPlugins(dir + "\\plugins")) {
107 if (!LoadPlugins(CONFIG_PLUGIN_DIR)) {
108 std::cerr << "Could not open instrument editor plugins "
109 << "directory (" << dir << "\\plugins or "
110 << CONFIG_PLUGIN_DIR << "), Error: "
111 << GetLastError() << std::endl;
112 return;
113 }
114 }
115 #else
116 if (!LoadPlugins(CONFIG_PLUGIN_DIR)) {
117 std::cerr << "Could not open instrument editor plugins "
118 << "directory (" << CONFIG_PLUGIN_DIR << "): "
119 << strerror(errno) << std::endl;
120 return;
121 }
122 #endif
123 bPluginsLoaded = true;
124 dmsg(1,("OK\n"));
125 }
126 }
127
128 bool InstrumentEditorFactory::LoadPlugins(String plugindir) {
129 #if defined(WIN32)
130 WIN32_FIND_DATA win32FindData;
131 const String pluginpattern = plugindir + "\\*.dll";
132 HANDLE hDir = FindFirstFile(pluginpattern.c_str(), &win32FindData);
133 if (hDir == INVALID_HANDLE_VALUE) {
134 if (GetLastError() != ERROR_FILE_NOT_FOUND) {
135 return false;
136 } else {
137 dmsg(1,("None"));
138 return true;
139 }
140 }
141
142 do {
143 // skip directory entries
144 if (win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
145 continue;
146 // dir entry name as full qualified path
147 const String sPath = plugindir + "\\" + win32FindData.cFileName;
148 // load the DLL
149 HINSTANCE hinstLib;
150 void* pDLL = hinstLib = LoadLibrary(sPath.c_str());
151 if (!pDLL) {
152 std::cerr << "Failed to load instrument editor plugin: "
153 << sPath << std::endl;
154 continue;
155 }
156
157 InnerFactory* (*fn)() = (InnerFactory* (*)())
158 GetProcAddress(hinstLib, "createInstrumentEditorInnerFactory");
159 if (fn == NULL) {
160 std::cerr << "ERROR: unable to find "
161 "createInstrumentEditorInnerFactory() "
162 "in DLL\n" << std::flush;
163 FreeLibrary(hinstLib);
164 continue;
165 }
166
167 // get the plugin instance and register it to the factory
168
169 InnerFactory* pInnerFactory = fn();
170 if (!pInnerFactory) {
171 std::cerr << "ERROR: !pInnerFactory\n" << std::flush;
172 FreeLibrary(hinstLib);
173 continue;
174 }
175 InstrumentEditor* pEditor = pInnerFactory->Create();
176 if (InnerFactories.count(pEditor->Name())) {
177 std::cerr << "ERROR: a plugin with name '"
178 << pEditor->Name()
179 << "' already loaded (skipping)\n"
180 << std::flush;
181 pInnerFactory->Destroy(pEditor);
182 FreeLibrary(hinstLib);
183 continue;
184 }
185 InnerFactories[pEditor->Name()] = pInnerFactory;
186 pInnerFactory->Destroy(pEditor);
187
188 LoadedDLLs.push_back(pDLL);
189 } while (FindNextFile(hDir, &win32FindData));
190
191 if (hDir != INVALID_HANDLE_VALUE) FindClose(hDir);
192
193 #else // POSIX
194
195 #if defined(__APPLE__) /* 20071224 Toshi Nagata */
196 if (plugindir.find("~") == 0)
197 plugindir.replace(0, 1, getenv("HOME"));
198 #endif
199 DIR* hDir = opendir(plugindir.c_str());
200 if (!hDir) {
201 return false;
202 }
203 for (dirent* pEntry = readdir(hDir); pEntry; pEntry = readdir(hDir)) {
204 // dir entry name as full qualified path
205 const String sPath = plugindir + "/" + pEntry->d_name;
206 // skip entries that are not regular files
207 struct stat entry_stat;
208 if (lstat(sPath.c_str(), &entry_stat) != 0 ||
209 (entry_stat.st_mode & S_IFMT) != S_IFREG)
210 continue;
211 // skip files that are not .so files
212 if (sPath.length() < 3 ||
213 sPath.substr(sPath.length() - 3) != ".so" &&
214 sPath.find(".so.") == String::npos)
215 continue;
216 // load the DLL (the plugins should register themselfes automatically)
217 void* pDLL = dlopen(sPath.c_str(), RTLD_NOW);
218 if (pDLL) LoadedDLLs.push_back(pDLL);
219 else {
220 std::cerr << "Failed to load instrument editor plugin: '"
221 << sPath << "', cause: " << dlerror() << std::endl;
222 }
223 }
224 closedir(hDir);
225 #endif
226 return true;
227 }
228
229 void InstrumentEditorFactory::ClosePlugins() {
230 if (LoadedDLLs.size()) {
231 dmsg(1,("Unloading instrument editor plugins..."));
232 // free all inner factories
233 {
234 std::map<String, InnerFactory*>::iterator iter = InnerFactories.begin();
235 for (; iter != InnerFactories.end(); iter++) delete iter->second;
236 InnerFactories.clear();
237 }
238 // free the DLLs
239 {
240 std::list<void*>::iterator iter = LoadedDLLs.begin();
241 for (; iter != LoadedDLLs.end(); iter++) {
242 #if defined(WIN32)
243 FreeLibrary((HINSTANCE)*iter);
244 #else
245 dlclose(*iter);
246 #endif
247 }
248 LoadedDLLs.clear();
249 dmsg(1,("OK\n"));
250 }
251 }
252 bPluginsLoaded = false;
253 }
254
255 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC