/[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 1481 - (hide annotations) (download)
Wed Nov 14 23:42:15 2007 UTC (16 years, 5 months ago) by senoner
File size: 10492 byte(s)
* win32 port work in progress:
* - implemented win32 support in the following classes:
* Thread, Condition, Mutex, Path, LscpServer
* - lscp.y use DONTCARE instead of VOID
*  (a win32 symbol defined)
* - completed win32 editor plugin loader

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

  ViewVC Help
Powered by ViewVC