Replaced stuff that wasnt supposed to be commited
[ardour.git] / libs / ardour / plugin_manager.cc
1 /*
2     Copyright (C) 2000-2006 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id$
19 */
20
21 #include <sys/types.h>
22 #include <cstdio>
23 #include <lrdf.h>
24 #include <dlfcn.h>
25
26 #ifdef VST_SUPPORT
27 #include <fst.h>
28 #include <pbd/basename.h>
29 #include <string.h>
30 #endif // VST_SUPPORT
31
32 #include <pbd/pathscanner.h>
33
34 #include <ardour/ladspa.h>
35 #include <ardour/session.h>
36 #include <ardour/plugin_manager.h>
37 #include <ardour/plugin.h>
38 #include <ardour/ladspa_plugin.h>
39
40 #ifdef VST_SUPPORT
41 #include <ardour/vst_plugin.h>
42 #endif
43
44 #include <pbd/error.h>
45 #include <pbd/stl_delete.h>
46
47 #include "i18n.h"
48
49 using namespace ARDOUR;
50 using namespace PBD;
51
52 PluginManager* PluginManager::_manager = 0;
53
54 PluginManager::PluginManager ()
55 {
56         char* s;
57         string lrdf_path;
58
59         if ((s = getenv ("LADSPA_RDF_PATH"))){
60                 lrdf_path = s;
61         }
62
63         if (lrdf_path.length() == 0) {
64                 lrdf_path = "/usr/local/share/ladspa/rdf:/usr/share/ladspa/rdf";
65         }
66
67         add_lrdf_data(lrdf_path);
68         add_ladspa_presets();
69 #ifdef VST_SUPPORT
70         if (Config->get_use_vst()) {
71                 add_vst_presets();
72         }
73 #endif /* VST_SUPPORT */
74
75         if ((s = getenv ("LADSPA_PATH"))) {
76                 ladspa_path = s;
77         }
78
79         if ((s = getenv ("VST_PATH"))) {
80                 vst_path = s;
81         } else if ((s = getenv ("VST_PLUGINS"))) {
82                 vst_path = s;
83         }
84
85         refresh ();
86         if (_manager == 0) {
87                 _manager = this;
88         }
89 }
90
91 void
92 PluginManager::refresh ()
93 {
94         ladspa_refresh ();
95 #ifdef VST_SUPPORT
96         if (Config->get_use_vst()) {
97                 vst_refresh ();
98         }
99 #endif // VST_SUPPORT
100 }
101
102 void
103 PluginManager::ladspa_refresh ()
104 {
105         _ladspa_plugin_info.clear ();
106
107         if (ladspa_path.length() == 0) {
108                 ladspa_path = "/usr/local/lib64/ladspa:/usr/local/lib/ladspa:/usr/lib64/ladspa:/usr/lib/ladspa:/Library/Audio/Plug-Ins/LADSPA";
109         }
110
111         ladspa_discover_from_path (ladspa_path);
112 }
113
114 int
115 PluginManager::add_ladspa_directory (string path)
116 {
117         if (ladspa_discover_from_path (path) == 0) {
118                 ladspa_path += ':';
119                 ladspa_path += path;
120                 return 0;
121         } 
122         return -1;
123 }
124
125 static bool ladspa_filter (const string& str, void *arg)
126 {
127         /* Not a dotfile, has a prefix before a period, suffix is "so" */
128         
129         return str[0] != '.' && (str.length() > 3 && str.find (".so") == (str.length() - 3));
130 }
131
132 int
133 PluginManager::ladspa_discover_from_path (string path)
134 {
135         PathScanner scanner;
136         vector<string *> *plugin_objects;
137         vector<string *>::iterator x;
138         int ret = 0;
139
140         plugin_objects = scanner (ladspa_path, ladspa_filter, 0, true, true);
141
142         if (plugin_objects) {
143                 for (x = plugin_objects->begin(); x != plugin_objects->end (); ++x) {
144                         ladspa_discover (**x);
145                 }
146         }
147
148         vector_delete (plugin_objects);
149         return ret;
150 }
151
152 static bool rdf_filter (const string &str, void *arg)
153 {
154         return str[0] != '.' && 
155                    ((str.find(".rdf")  == (str.length() - 4)) ||
156             (str.find(".rdfs") == (str.length() - 5)) ||
157                     (str.find(".n3")   == (str.length() - 3)));
158 }
159
160 void
161 PluginManager::add_ladspa_presets()
162 {
163         add_presets ("ladspa");
164 }
165
166 void
167 PluginManager::add_vst_presets()
168 {
169         add_presets ("vst");
170 }
171 void
172 PluginManager::add_presets(string domain)
173 {
174
175         PathScanner scanner;
176         vector<string *> *presets;
177         vector<string *>::iterator x;
178
179         char* envvar;
180         if ((envvar = getenv ("HOME")) == 0) {
181                 return;
182         }
183
184         string path = string_compose("%1/.%2/rdf", envvar, domain);
185         presets = scanner (path, rdf_filter, 0, true, true);
186
187         if (presets) {
188                 for (x = presets->begin(); x != presets->end (); ++x) {
189                         string file = "file:" + **x;
190                         if (lrdf_read_file(file.c_str())) {
191                                 warning << string_compose(_("Could not parse rdf file: %1"), *x) << endmsg;
192                         }
193                 }
194         }
195
196         vector_delete (presets);
197 }
198
199 void
200 PluginManager::add_lrdf_data (const string &path)
201 {
202         PathScanner scanner;
203         vector<string *>* rdf_files;
204         vector<string *>::iterator x;
205         string uri;
206
207         rdf_files = scanner (path, rdf_filter, 0, true, true);
208
209         if (rdf_files) {
210                 for (x = rdf_files->begin(); x != rdf_files->end (); ++x) {
211                         uri = "file://" + **x;
212
213                         if (lrdf_read_file(uri.c_str())) {
214                                 warning << "Could not parse rdf file: " << uri << endmsg;
215                         }
216                 }
217         }
218
219         vector_delete (rdf_files);
220 }
221
222 int 
223 PluginManager::ladspa_discover (string path)
224 {
225         void *module;
226         const LADSPA_Descriptor *descriptor;
227         LADSPA_Descriptor_Function dfunc;
228         const char *errstr;
229
230         if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
231                 error << string_compose(_("LADSPA: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
232                 return -1;
233         }
234
235         dfunc = (LADSPA_Descriptor_Function) dlsym (module, "ladspa_descriptor");
236
237         if ((errstr = dlerror()) != 0) {
238                 error << string_compose(_("LADSPA: module \"%1\" has no descriptor function."), path) << endmsg;
239                 error << errstr << endmsg;
240                 dlclose (module);
241                 return -1;
242         }
243
244         for (uint32_t i = 0; ; ++i) {
245                 if ((descriptor = dfunc (i)) == 0) {
246                         break;
247                 }
248
249                 PluginInfoPtr info(new LadspaPluginInfo);
250                 info->name = descriptor->Name;
251                 info->category = get_ladspa_category(descriptor->UniqueID);
252                 info->path = path;
253                 info->index = i;
254                 info->n_inputs = 0;
255                 info->n_outputs = 0;
256                 info->type = ARDOUR::LADSPA;
257                 info->unique_id = descriptor->UniqueID;
258                 
259                 for (uint32_t n=0; n < descriptor->PortCount; ++n) {
260                         if ( LADSPA_IS_PORT_AUDIO (descriptor->PortDescriptors[n]) ) {
261                                 if ( LADSPA_IS_PORT_INPUT (descriptor->PortDescriptors[n]) ) {
262                                         info->n_inputs++;
263                                 }
264                                 else if ( LADSPA_IS_PORT_OUTPUT (descriptor->PortDescriptors[n]) ) {
265                                         info->n_outputs++;
266                                 }
267                         }
268                 }
269
270                 _ladspa_plugin_info.push_back (info);
271         }
272
273 // GDB WILL NOT LIKE YOU IF YOU DO THIS
274 //      dlclose (module);
275
276         return 0;
277 }
278
279 string
280 PluginManager::get_ladspa_category (uint32_t plugin_id)
281 {
282         char buf[256];
283         lrdf_statement pattern;
284
285         snprintf(buf, sizeof(buf), "%s%" PRIu32, LADSPA_BASE, plugin_id);
286         pattern.subject = buf;
287         pattern.predicate = RDF_TYPE;
288         pattern.object = 0;
289         pattern.object_type = lrdf_uri;
290
291         lrdf_statement* matches1 = lrdf_matches (&pattern);
292
293         if (!matches1) {
294                 return _("Unknown");
295         }
296
297         pattern.subject = matches1->object;
298         pattern.predicate = LADSPA_BASE "hasLabel";
299         pattern.object = 0;
300         pattern.object_type = lrdf_literal;
301
302         lrdf_statement* matches2 = lrdf_matches (&pattern);
303         lrdf_free_statements(matches1);
304
305         if (!matches2) {
306                 return _("Unknown");
307         }
308
309         string label = matches2->object;
310         lrdf_free_statements(matches2);
311
312         return label;
313 }
314
315 #ifdef VST_SUPPORT
316
317 void
318 PluginManager::vst_refresh ()
319 {
320         _vst_plugin_info.clear ();
321
322         if (vst_path.length() == 0) {
323                 vst_path = "/usr/local/lib/vst:/usr/lib/vst";
324         }
325
326         vst_discover_from_path (vst_path);
327 }
328
329 int
330 PluginManager::add_vst_directory (string path)
331 {
332         if (vst_discover_from_path (path) == 0) {
333                 vst_path += ':';
334                 vst_path += path;
335                 return 0;
336         } 
337         return -1;
338 }
339
340 static bool vst_filter (const string& str, void *arg)
341 {
342         /* Not a dotfile, has a prefix before a period, suffix is "dll" */
343         
344         return str[0] != '.' && (str.length() > 4 && str.find (".dll") == (str.length() - 4));
345 }
346
347 int
348 PluginManager::vst_discover_from_path (string path)
349 {
350         PathScanner scanner;
351         vector<string *> *plugin_objects;
352         vector<string *>::iterator x;
353         int ret = 0;
354
355         info << "detecting VST plugins along " << path << endmsg;
356
357         plugin_objects = scanner (vst_path, vst_filter, 0, true, true);
358
359         if (plugin_objects) {
360                 for (x = plugin_objects->begin(); x != plugin_objects->end (); ++x) {
361                         vst_discover (**x);
362                 }
363         }
364
365         vector_delete (plugin_objects);
366         return ret;
367 }
368
369 int
370 PluginManager::vst_discover (string path)
371 {
372         FSTInfo* finfo;
373
374         if ((finfo = fst_get_info (const_cast<char *> (path.c_str()))) == 0) {
375                 return -1;
376         }
377
378         if (!finfo->canProcessReplacing) {
379                 warning << string_compose (_("VST plugin %1 does not support processReplacing, and so cannot be used in ardour at this time"),
380                                     finfo->name)
381                         << endl;
382         }
383         
384         PluginInfoPtr info(new VSTPluginInfo);
385
386         /* what a goddam joke freeware VST is */
387
388         if (!strcasecmp ("The Unnamed plugin", finfo->name)) {
389                 info->name = PBD::basename_nosuffix (path);
390         } else {
391                 info->name = finfo->name;
392         }
393
394         info->category = "VST";
395         info->path = path;
396         info->index = 0;
397         info->n_inputs = finfo->numInputs;
398         info->n_outputs = finfo->numOutputs;
399         info->type = ARDOUR::VST;
400         
401         _vst_plugin_info.push_back (info);
402         fst_free_info (finfo);
403
404         return 0;
405 }
406
407 #endif // VST_SUPPORT