/[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 1425 - (show annotations) (download)
Sun Oct 14 22:01:28 2007 UTC (13 years, 6 months ago) by schoenebeck
File size: 6453 byte(s)
- postponed commit for the recent commit batch

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

  ViewVC Help
Powered by ViewVC