1 |
/*************************************************************************** |
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 |
#include <dlfcn.h> |
24 |
#include <errno.h> |
25 |
#include <string.h> |
26 |
#include <dirent.h> |
27 |
|
28 |
#ifndef CONFIG_PLUGIN_DIR |
29 |
# error "Configuration macro CONFIG_PLUGIN_DIR not defined!" |
30 |
#endif // CONFIG_PLUGIN_DIR |
31 |
|
32 |
namespace LinuxSampler { |
33 |
|
34 |
std::map<String, InstrumentEditorFactory::InnerFactory*> InstrumentEditorFactory::InnerFactories; |
35 |
|
36 |
bool InstrumentEditorFactory::bPluginsLoaded = false; |
37 |
|
38 |
std::list<void*> InstrumentEditorFactory::LoadedDLLs; |
39 |
|
40 |
std::vector<String> InstrumentEditorFactory::AvailableEditors() { |
41 |
// make sure plugins were loaded already |
42 |
LoadPlugins(); |
43 |
// render result |
44 |
std::vector<String> result; |
45 |
std::map<String, InnerFactory*>::iterator iter = InnerFactories.begin(); |
46 |
for (; iter != InnerFactories.end(); iter++) |
47 |
result.push_back(iter->first); |
48 |
return result; |
49 |
} |
50 |
|
51 |
std::vector<String> InstrumentEditorFactory::MatchingEditors(String sTypeName, String sTypeVersion) { |
52 |
// make sure plugins were loaded already |
53 |
LoadPlugins(); |
54 |
// render result |
55 |
std::vector<String> result; |
56 |
std::map<String, InnerFactory*>::iterator iter = InnerFactories.begin(); |
57 |
for (; iter != InnerFactories.end(); iter++) { |
58 |
InstrumentEditor* pEditor = iter->second->Create(); |
59 |
if (pEditor->IsTypeSupported(sTypeName, sTypeVersion)) |
60 |
result.push_back(iter->first); |
61 |
iter->second->Destroy(pEditor); |
62 |
} |
63 |
return result; |
64 |
} |
65 |
|
66 |
String InstrumentEditorFactory::AvailableEditorsAsString() { |
67 |
std::vector<String> drivers = AvailableEditors(); |
68 |
String result; |
69 |
std::vector<String>::iterator iter = drivers.begin(); |
70 |
for (; iter != drivers.end(); iter++) { |
71 |
if (result != "") result += ","; |
72 |
result += "'" + *iter + "'"; |
73 |
} |
74 |
return result; |
75 |
} |
76 |
|
77 |
InstrumentEditor* InstrumentEditorFactory::Create(String InstrumentEditorName) throw (Exception) { |
78 |
if (InnerFactories.count(InstrumentEditorName)) { |
79 |
InnerFactory* pInnerFactory = InnerFactories[InstrumentEditorName]; |
80 |
return pInnerFactory->Create(); |
81 |
} else throw Exception("unknown instrument editor"); |
82 |
} |
83 |
|
84 |
void InstrumentEditorFactory::Destroy(InstrumentEditor* pInstrumentEditor) throw (Exception) { |
85 |
if (InnerFactories.count(pInstrumentEditor->Name())) { |
86 |
InnerFactory* pInnerFactory = InnerFactories[pInstrumentEditor->Name()]; |
87 |
return pInnerFactory->Destroy(pInstrumentEditor); |
88 |
} else throw Exception("unknown instrument editor"); |
89 |
} |
90 |
|
91 |
void InstrumentEditorFactory::LoadPlugins() { |
92 |
if (!bPluginsLoaded) { |
93 |
dmsg(1,("Loading instrument editor plugins...")); |
94 |
DIR* hDir = opendir(CONFIG_PLUGIN_DIR); |
95 |
if (!hDir) { |
96 |
std::cerr << "Could not open instrument editor plugins directory " |
97 |
<< "(" << CONFIG_PLUGIN_DIR << "): " |
98 |
<< strerror(errno) << std::endl; |
99 |
return; |
100 |
} |
101 |
for (dirent* pEntry = readdir(hDir); pEntry; pEntry = readdir(hDir)) { |
102 |
// skip entries that are not regular files |
103 |
if (pEntry->d_type != DT_REG) continue; |
104 |
String sPath = pEntry->d_name; |
105 |
// skip files that are not .so files |
106 |
if ( |
107 |
sPath.substr(sPath.length() - 3) != ".so" && |
108 |
sPath.find(".so.") == String::npos |
109 |
) continue; |
110 |
// make it a full qualified path |
111 |
sPath = CONFIG_PLUGIN_DIR + ("/" + sPath); |
112 |
// load the DLL (the plugins should register themselfes automatically) |
113 |
void* pDLL = dlopen(sPath.c_str(), RTLD_NOW); |
114 |
if (pDLL) LoadedDLLs.push_back(pDLL); |
115 |
else { |
116 |
std::cerr << "Failed to load instrument editor plugin: " |
117 |
<< sPath << std::endl; |
118 |
} |
119 |
} |
120 |
closedir(hDir); |
121 |
bPluginsLoaded = true; |
122 |
dmsg(1,("OK\n")); |
123 |
} |
124 |
} |
125 |
|
126 |
void InstrumentEditorFactory::ClosePlugins() { |
127 |
if (LoadedDLLs.size()) { |
128 |
dmsg(1,("Unloading instrument editor plugins...")); |
129 |
// free all inner factories |
130 |
{ |
131 |
std::map<String, InnerFactory*>::iterator iter = InnerFactories.begin(); |
132 |
for (; iter != InnerFactories.end(); iter++) delete iter->second; |
133 |
InnerFactories.clear(); |
134 |
} |
135 |
// free the DLLs |
136 |
{ |
137 |
std::list<void*>::iterator iter = LoadedDLLs.begin(); |
138 |
for (; iter != LoadedDLLs.end(); iter++) dlclose(*iter); |
139 |
LoadedDLLs.clear(); |
140 |
dmsg(1,("OK\n")); |
141 |
} |
142 |
} |
143 |
bPluginsLoaded = false; |
144 |
} |
145 |
|
146 |
} // namespace LinuxSampler |