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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1601 - (hide annotations) (download)
Sat Dec 29 20:27:53 2007 UTC (16 years, 5 months ago) by schoenebeck
File size: 10705 byte(s)
* bugfix: on some POSIX systems instrument editor plugins refused to
  load as we used a non-portable Linux specific struct field
  (fixes bug #70, patch by Ronald Baljeu)

1 schoenebeck 1374 /***************************************************************************
2     * *
3     * Copyright (C) 2007 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 schoenebeck 1425 #include "../common/global_private.h"
24    
25 senoner 1478 #if defined(WIN32)
26     #include <windows.h>
27     #else
28 schoenebeck 1374 #include <dlfcn.h>
29     #include <errno.h>
30 senoner 1478 #include <dirent.h>
31 schoenebeck 1601 #include <sys/types.h>
32     #include <sys/stat.h>
33     #include <unistd.h>
34 senoner 1478 #endif
35 schoenebeck 1374 #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 senoner 1481 #if defined(WIN32)
42     typedef void* (*InnerFactoryRegisterFunction)(void);
43     #endif
44    
45 schoenebeck 1374 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 senoner 1478 #if defined(WIN32)
108     bool firstFileFound = true;
109     WIN32_FIND_DATA win32FindData;
110     String plugindir = (String)CONFIG_PLUGIN_DIR + (String)("\\*.DLL");
111     HANDLE hDir = FindFirstFile(plugindir.c_str(), &win32FindData);
112     if (hDir == INVALID_HANDLE_VALUE) {
113     if(GetLastError() != ERROR_FILE_NOT_FOUND) {
114     std::cerr << "Could not open instrument editor plugins directory "
115     << "(" << CONFIG_PLUGIN_DIR << "): Error "
116     << GetLastError() << std::endl;
117     return;
118     }
119     else {
120     firstFileFound = false;
121     }
122     }
123    
124     while(GetLastError() != ERROR_NO_MORE_FILES && firstFileFound) {
125     if(!(win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
126     String sPath = (String)CONFIG_PLUGIN_DIR + ((String)"\\" + (String)win32FindData.cFileName);
127     // load the DLL (the plugins should register themselfes automatically)
128 senoner 1481 HINSTANCE hinstLib;
129     void* pDLL = hinstLib = LoadLibrary( sPath.c_str() );
130     if (!pDLL) {
131 senoner 1478 std::cerr << "Failed to load instrument editor plugin: "
132     << sPath << std::endl;
133 senoner 1481 continue;
134 senoner 1478 }
135 senoner 1481
136     //(InnerFactory*) (*fn)(void);
137     InnerFactoryRegisterFunction fn;
138     fn = (InnerFactoryRegisterFunction)
139     GetProcAddress(
140     hinstLib,
141     "createInstrumentEditorInnerFactory"
142     );
143     if (fn == NULL) {
144     std::cerr << "ERROR: unable to find "
145     "createInstrumentEditorInnerFactory() "
146     "in DLL\n" << std::flush;
147     FreeLibrary(hinstLib);
148     continue;
149     }
150    
151     // get the plugin instance and register it to the factory
152    
153     InnerFactory* pInnerFactory = (InnerFactory*)fn();
154     if (!pInnerFactory) {
155     std::cerr << "ERROR: !pInnerFactory\n" << std::flush;
156     FreeLibrary(hinstLib);
157     continue;
158     }
159     InstrumentEditor* pEditor = pInnerFactory->Create();
160     if (InnerFactories.count(pEditor->Name())) {
161     std::cerr << "ERROR: a plugin with name '"
162     << pEditor->Name()
163     << "' already loaded (skipping)\n"
164     << std::flush;
165     pInnerFactory->Destroy(pEditor);
166     FreeLibrary(hinstLib);
167     continue;
168     }
169     InnerFactories[pEditor->Name()] = pInnerFactory;
170     pInnerFactory->Destroy(pEditor);
171    
172     LoadedDLLs.push_back(pDLL);
173 senoner 1478 }
174     int res = FindNextFile(hDir, &win32FindData);
175     if(res == 0 && GetLastError() != ERROR_NO_MORE_FILES) {
176     std::cerr << "Error while reading plugins directory FindNextFile Error "
177     << GetLastError() << std::endl;
178     return;
179     }
180     }
181     FindClose(hDir);
182     #else // POSIX
183 schoenebeck 1374 DIR* hDir = opendir(CONFIG_PLUGIN_DIR);
184     if (!hDir) {
185     std::cerr << "Could not open instrument editor plugins directory "
186     << "(" << CONFIG_PLUGIN_DIR << "): "
187     << strerror(errno) << std::endl;
188     return;
189     }
190     for (dirent* pEntry = readdir(hDir); pEntry; pEntry = readdir(hDir)) {
191 schoenebeck 1601 // dir entry name as full qualified path
192     const String sPath = CONFIG_PLUGIN_DIR + ("/" + String(pEntry->d_name));
193 schoenebeck 1374 // skip entries that are not regular files
194 schoenebeck 1601 struct stat entry_stat;
195     if (lstat(sPath.c_str(), &entry_stat) != 0 ||
196     (entry_stat.st_mode & S_IFMT) != S_IFREG)
197     continue;
198 schoenebeck 1374 // skip files that are not .so files
199 schoenebeck 1601 if (sPath.length() < 3 ||
200 schoenebeck 1374 sPath.substr(sPath.length() - 3) != ".so" &&
201     sPath.find(".so.") == String::npos
202     ) continue;
203     // load the DLL (the plugins should register themselfes automatically)
204     void* pDLL = dlopen(sPath.c_str(), RTLD_NOW);
205     if (pDLL) LoadedDLLs.push_back(pDLL);
206     else {
207     std::cerr << "Failed to load instrument editor plugin: "
208     << sPath << std::endl;
209     }
210     }
211     closedir(hDir);
212 senoner 1478 #endif
213 schoenebeck 1374 bPluginsLoaded = true;
214     dmsg(1,("OK\n"));
215     }
216     }
217    
218     void InstrumentEditorFactory::ClosePlugins() {
219     if (LoadedDLLs.size()) {
220     dmsg(1,("Unloading instrument editor plugins..."));
221     // free all inner factories
222     {
223     std::map<String, InnerFactory*>::iterator iter = InnerFactories.begin();
224     for (; iter != InnerFactories.end(); iter++) delete iter->second;
225     InnerFactories.clear();
226     }
227     // free the DLLs
228     {
229     std::list<void*>::iterator iter = LoadedDLLs.begin();
230 senoner 1478 for (; iter != LoadedDLLs.end(); iter++) {
231     #if defined(WIN32)
232     FreeLibrary((HINSTANCE)*iter);
233     #else
234     dlclose(*iter);
235     #endif
236     }
237 schoenebeck 1374 LoadedDLLs.clear();
238     dmsg(1,("OK\n"));
239     }
240     }
241     bPluginsLoaded = false;
242     }
243    
244     } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC