/[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 1653 - (show annotations) (download)
Wed Jan 30 01:51:46 2008 UTC (9 years, 9 months ago) by schoenebeck
File size: 10694 byte(s)
* added support for notifying instrument editors on note-on / note-off
  events (e.g. to highlight the pressed keys on the virtual keyboard
  of gigedit)
* fixed return value of recently added Thread::TestCancel() method
* be verbose on DLL load errors (on Linux)

1 /***************************************************************************
2 * *
3 * Copyright (C) 2007, 2008 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
25 #if defined(WIN32)
26 #include <windows.h>
27 #else
28 #include <dlfcn.h>
29 #include <errno.h>
30 #include <dirent.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34 #endif
35 #include <string.h>
36
37 #ifndef CONFIG_PLUGIN_DIR
38 # error "Configuration macro CONFIG_PLUGIN_DIR not defined!"
39 #endif // CONFIG_PLUGIN_DIR
40
41 #if defined(WIN32)
42 typedef void* (*InnerFactoryRegisterFunction)(void);
43 #endif
44
45 namespace LinuxSampler {
46
47 std::map<String, InstrumentEditorFactory::InnerFactory*> InstrumentEditorFactory::InnerFactories;
48
49 bool InstrumentEditorFactory::bPluginsLoaded = false;
50
51 std::list<void*> InstrumentEditorFactory::LoadedDLLs;
52
53 std::vector<String> InstrumentEditorFactory::AvailableEditors() {
54 // make sure plugins were loaded already
55 LoadPlugins();
56 // render result
57 std::vector<String> result;
58 std::map<String, InnerFactory*>::iterator iter = InnerFactories.begin();
59 for (; iter != InnerFactories.end(); iter++)
60 result.push_back(iter->first);
61 return result;
62 }
63
64 std::vector<String> InstrumentEditorFactory::MatchingEditors(String sTypeName, String sTypeVersion) {
65 // make sure plugins were loaded already
66 LoadPlugins();
67 // render result
68 std::vector<String> result;
69 std::map<String, InnerFactory*>::iterator iter = InnerFactories.begin();
70 for (; iter != InnerFactories.end(); iter++) {
71 InstrumentEditor* pEditor = iter->second->Create();
72 if (pEditor->IsTypeSupported(sTypeName, sTypeVersion))
73 result.push_back(iter->first);
74 iter->second->Destroy(pEditor);
75 }
76 return result;
77 }
78
79 String InstrumentEditorFactory::AvailableEditorsAsString() {
80 std::vector<String> drivers = AvailableEditors();
81 String result;
82 std::vector<String>::iterator iter = drivers.begin();
83 for (; iter != drivers.end(); iter++) {
84 if (result != "") result += ",";
85 result += "'" + *iter + "'";
86 }
87 return result;
88 }
89
90 InstrumentEditor* InstrumentEditorFactory::Create(String InstrumentEditorName) throw (Exception) {
91 if (InnerFactories.count(InstrumentEditorName)) {
92 InnerFactory* pInnerFactory = InnerFactories[InstrumentEditorName];
93 return pInnerFactory->Create();
94 } else throw Exception("unknown instrument editor");
95 }
96
97 void InstrumentEditorFactory::Destroy(InstrumentEditor* pInstrumentEditor) throw (Exception) {
98 if (InnerFactories.count(pInstrumentEditor->Name())) {
99 InnerFactory* pInnerFactory = InnerFactories[pInstrumentEditor->Name()];
100 return pInnerFactory->Destroy(pInstrumentEditor);
101 } else throw Exception("unknown instrument editor");
102 }
103
104 void InstrumentEditorFactory::LoadPlugins() {
105 if (!bPluginsLoaded) {
106 dmsg(1,("Loading instrument editor plugins..."));
107 #if defined(WIN32)
108 WIN32_FIND_DATA win32FindData;
109 const String plugindir = (String)CONFIG_PLUGIN_DIR + (String)("\\*.DLL");
110 HANDLE hDir = FindFirstFile(plugindir.c_str(), &win32FindData);
111 if (hDir == INVALID_HANDLE_VALUE) {
112 if (GetLastError() != ERROR_FILE_NOT_FOUND) {
113 std::cerr << "Could not open instrument editor plugins "
114 << "directory (" << CONFIG_PLUGIN_DIR << "), "
115 << "Error: " << GetLastError() << std::endl;
116 } else {
117 dmsg(1,("None"));
118 }
119 // either dir doesn't exist or is empty
120 return;
121 }
122
123 do {
124 // skip directory entries
125 if (win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
126 continue;
127 // dir entry name as full qualified path
128 const String sPath = (String)CONFIG_PLUGIN_DIR + ((String)"\\" + (String)win32FindData.cFileName);
129 // load the DLL
130 HINSTANCE hinstLib;
131 void* pDLL = hinstLib = LoadLibrary(sPath.c_str());
132 if (!pDLL) {
133 std::cerr << "Failed to load instrument editor plugin: "
134 << sPath << std::endl;
135 continue;
136 }
137
138 //(InnerFactory*) (*fn)(void);
139 InnerFactoryRegisterFunction fn =
140 (InnerFactoryRegisterFunction)
141 GetProcAddress(
142 hinstLib,
143 "createInstrumentEditorInnerFactory"
144 );
145 if (fn == NULL) {
146 std::cerr << "ERROR: unable to find "
147 "createInstrumentEditorInnerFactory() "
148 "in DLL\n" << std::flush;
149 FreeLibrary(hinstLib);
150 continue;
151 }
152
153 // get the plugin instance and register it to the factory
154
155 InnerFactory* pInnerFactory = (InnerFactory*)fn();
156 if (!pInnerFactory) {
157 std::cerr << "ERROR: !pInnerFactory\n" << std::flush;
158 FreeLibrary(hinstLib);
159 continue;
160 }
161 InstrumentEditor* pEditor = pInnerFactory->Create();
162 if (InnerFactories.count(pEditor->Name())) {
163 std::cerr << "ERROR: a plugin with name '"
164 << pEditor->Name()
165 << "' already loaded (skipping)\n"
166 << std::flush;
167 pInnerFactory->Destroy(pEditor);
168 FreeLibrary(hinstLib);
169 continue;
170 }
171 InnerFactories[pEditor->Name()] = pInnerFactory;
172 pInnerFactory->Destroy(pEditor);
173
174 LoadedDLLs.push_back(pDLL);
175 } while (FindNextFile(hDir, &win32FindData));
176 if (hDir != INVALID_HANDLE_VALUE) FindClose(hDir);
177 #else // POSIX
178 #if defined(__APPLE__) /* 20071224 Toshi Nagata */
179 String Config_plugin_dir = (String)CONFIG_PLUGIN_DIR;
180 if (Config_plugin_dir.find("~") == 0)
181 Config_plugin_dir.replace(0, 1, getenv("HOME"));
182 DIR* hDir = opendir(Config_plugin_dir.c_str());
183 #else
184 DIR* hDir = opendir(CONFIG_PLUGIN_DIR);
185 #endif
186 if (!hDir) {
187 std::cerr << "Could not open instrument editor plugins directory "
188 << "(" << CONFIG_PLUGIN_DIR << "): "
189 << strerror(errno) << std::endl;
190 return;
191 }
192 for (dirent* pEntry = readdir(hDir); pEntry; pEntry = readdir(hDir)) {
193 // dir entry name as full qualified path
194 #if defined(__APPLE__) /* 20080110 Toshi Nagata */
195 const String sPath = Config_plugin_dir + ("/" + String(pEntry->d_name));
196 #else
197 const String sPath = CONFIG_PLUGIN_DIR + ("/" + String(pEntry->d_name));
198 #endif
199 // skip entries that are not regular files
200 struct stat entry_stat;
201 if (lstat(sPath.c_str(), &entry_stat) != 0 ||
202 (entry_stat.st_mode & S_IFMT) != S_IFREG)
203 continue;
204 // skip files that are not .so files
205 if (sPath.length() < 3 ||
206 sPath.substr(sPath.length() - 3) != ".so" &&
207 sPath.find(".so.") == String::npos
208 ) continue;
209 // load the DLL (the plugins should register themselfes automatically)
210 void* pDLL = dlopen(sPath.c_str(), RTLD_NOW);
211 if (pDLL) LoadedDLLs.push_back(pDLL);
212 else {
213 std::cerr << "Failed to load instrument editor plugin: '"
214 << sPath << "', cause: " << dlerror() << std::endl;
215 }
216 }
217 closedir(hDir);
218 #endif
219 bPluginsLoaded = true;
220 dmsg(1,("OK\n"));
221 }
222 }
223
224 void InstrumentEditorFactory::ClosePlugins() {
225 if (LoadedDLLs.size()) {
226 dmsg(1,("Unloading instrument editor plugins..."));
227 // free all inner factories
228 {
229 std::map<String, InnerFactory*>::iterator iter = InnerFactories.begin();
230 for (; iter != InnerFactories.end(); iter++) delete iter->second;
231 InnerFactories.clear();
232 }
233 // free the DLLs
234 {
235 std::list<void*>::iterator iter = LoadedDLLs.begin();
236 for (; iter != LoadedDLLs.end(); iter++) {
237 #if defined(WIN32)
238 FreeLibrary((HINSTANCE)*iter);
239 #else
240 dlclose(*iter);
241 #endif
242 }
243 LoadedDLLs.clear();
244 dmsg(1,("OK\n"));
245 }
246 }
247 bPluginsLoaded = false;
248 }
249
250 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC