Merged with trunk R776
[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
87         if (_manager == 0) {
88                 _manager = this;
89         }
90 }
91
92 void
93 PluginManager::refresh ()
94 {
95         ladspa_refresh ();
96 #ifdef VST_SUPPORT
97         if (Config->get_use_vst()) {
98                 vst_refresh ();
99         }
100 #endif // VST_SUPPORT
101 }
102
103 void
104 PluginManager::ladspa_refresh ()
105 {
106         _ladspa_plugin_info.clear ();
107
108         if (ladspa_path.length() == 0) {
109                 ladspa_path = "/usr/local/lib/ladspa:/usr/lib/ladspa";
110         }
111
112         ladspa_discover_from_path (ladspa_path);
113 }
114
115 int
116 PluginManager::add_ladspa_directory (string path)
117 {
118         if (ladspa_discover_from_path (path) == 0) {
119                 ladspa_path += ':';
120                 ladspa_path += path;
121                 return 0;
122         } 
123         return -1;
124 }
125
126 static bool ladspa_filter (const string& str, void *arg)
127 {
128         /* Not a dotfile, has a prefix before a period, suffix is "so" */
129         
130         return str[0] != '.' && (str.length() > 3 && str.find (".so") == (str.length() - 3));
131 }
132
133 int
134 PluginManager::ladspa_discover_from_path (string path)
135 {
136         PathScanner scanner;
137         vector<string *> *plugin_objects;
138         vector<string *>::iterator x;
139         int ret = 0;
140
141         plugin_objects = scanner (ladspa_path, ladspa_filter, 0, true, true);
142
143         if (plugin_objects) {
144                 for (x = plugin_objects->begin(); x != plugin_objects->end (); ++x) {
145                         ladspa_discover (**x);
146                 }
147         }
148
149         vector_delete (plugin_objects);
150         return ret;
151 }
152
153 static bool rdf_filter (const string &str, void *arg)
154 {
155         return str[0] != '.' && 
156                    ((str.find(".rdf")  == (str.length() - 4)) ||
157             (str.find(".rdfs") == (str.length() - 5)) ||
158                     (str.find(".n3")   == (str.length() - 3)));
159 }
160
161 void
162 PluginManager::add_ladspa_presets()
163 {
164         add_presets ("ladspa");
165 }
166
167 void
168 PluginManager::add_vst_presets()
169 {
170         add_presets ("vst");
171 }
172 void
173 PluginManager::add_presets(string domain)
174 {
175
176         PathScanner scanner;
177         vector<string *> *presets;
178         vector<string *>::iterator x;
179
180         char* envvar;
181         if ((envvar = getenv ("HOME")) == 0) {
182                 return;
183         }
184
185         string path = string_compose("%1/.%2/rdf", envvar, domain);
186         presets = scanner (path, rdf_filter, 0, true, true);
187
188         if (presets) {
189                 for (x = presets->begin(); x != presets->end (); ++x) {
190                         string file = "file:" + **x;
191                         if (lrdf_read_file(file.c_str())) {
192                                 warning << string_compose(_("Could not parse rdf file: %1"), *x) << endmsg;
193                         }
194                 }
195         }
196
197         vector_delete (presets);
198 }
199
200 void
201 PluginManager::add_lrdf_data (const string &path)
202 {
203         PathScanner scanner;
204         vector<string *>* rdf_files;
205         vector<string *>::iterator x;
206         string uri;
207
208         rdf_files = scanner (path, rdf_filter, 0, true, true);
209
210         if (rdf_files) {
211                 for (x = rdf_files->begin(); x != rdf_files->end (); ++x) {
212                         uri = "file://" + **x;
213
214                         if (lrdf_read_file(uri.c_str())) {
215                                 warning << "Could not parse rdf file: " << uri << endmsg;
216                         }
217                 }
218         }
219
220         vector_delete (rdf_files);
221 }
222
223 int 
224 PluginManager::ladspa_discover (string path)
225 {
226         void *module;
227         const LADSPA_Descriptor *descriptor;
228         LADSPA_Descriptor_Function dfunc;
229         const char *errstr;
230
231         if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
232                 error << string_compose(_("LADSPA: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
233                 return -1;
234         }
235
236         dfunc = (LADSPA_Descriptor_Function) dlsym (module, "ladspa_descriptor");
237
238         if ((errstr = dlerror()) != 0) {
239                 error << string_compose(_("LADSPA: module \"%1\" has no descriptor function."), path) << endmsg;
240                 error << errstr << endmsg;
241                 dlclose (module);
242                 return -1;
243         }
244
245         for (uint32_t i = 0; ; ++i) {
246                 if ((descriptor = dfunc (i)) == 0) {
247                         break;
248                 }
249
250                 PluginInfoPtr info(new LadspaPluginInfo);
251                 info->name = descriptor->Name;
252                 info->category = get_ladspa_category(descriptor->UniqueID);
253                 info->path = path;
254                 info->index = i;
255                 info->n_inputs = 0;
256                 info->n_outputs = 0;
257                 info->type = PluginInfo::LADSPA;
258                 info->unique_id = descriptor->UniqueID;
259                 
260                 for (uint32_t n=0; n < descriptor->PortCount; ++n) {
261                         if ( LADSPA_IS_PORT_AUDIO (descriptor->PortDescriptors[n]) ) {
262                                 if ( LADSPA_IS_PORT_INPUT (descriptor->PortDescriptors[n]) ) {
263                                         info->n_inputs++;
264                                 }
265                                 else if ( LADSPA_IS_PORT_OUTPUT (descriptor->PortDescriptors[n]) ) {
266                                         info->n_outputs++;
267                                 }
268                         }
269                 }
270
271                 _ladspa_plugin_info.push_back (info);
272         }
273
274 // GDB WILL NOT LIKE YOU IF YOU DO THIS
275 //      dlclose (module);
276
277         return 0;
278 }
279
280 string
281 PluginManager::get_ladspa_category (uint32_t plugin_id)
282 {
283         char buf[256];
284         lrdf_statement pattern;
285
286         snprintf(buf, sizeof(buf), "%s%" PRIu32, LADSPA_BASE, plugin_id);
287         pattern.subject = buf;
288         pattern.predicate = RDF_TYPE;
289         pattern.object = 0;
290         pattern.object_type = lrdf_uri;
291
292         lrdf_statement* matches1 = lrdf_matches (&pattern);
293
294         if (!matches1) {
295                 return _("Unknown");
296         }
297
298         pattern.subject = matches1->object;
299         pattern.predicate = LADSPA_BASE "hasLabel";
300         pattern.object = 0;
301         pattern.object_type = lrdf_literal;
302
303         lrdf_statement* matches2 = lrdf_matches (&pattern);
304         lrdf_free_statements(matches1);
305
306         if (!matches2) {
307                 return _("Unknown");
308         }
309
310         string label = matches2->object;
311         lrdf_free_statements(matches2);
312
313         return label;
314 }
315
316 #ifdef VST_SUPPORT
317
318 void
319 PluginManager::vst_refresh ()
320 {
321         _vst_plugin_info.clear ();
322
323         if (vst_path.length() == 0) {
324                 vst_path = "/usr/local/lib/vst:/usr/lib/vst";
325         }
326
327         vst_discover_from_path (vst_path);
328 }
329
330 int
331 PluginManager::add_vst_directory (string path)
332 {
333         if (vst_discover_from_path (path) == 0) {
334                 vst_path += ':';
335                 vst_path += path;
336                 return 0;
337         } 
338         return -1;
339 }
340
341 static bool vst_filter (const string& str, void *arg)
342 {
343         /* Not a dotfile, has a prefix before a period, suffix is "dll" */
344         
345         return str[0] != '.' && (str.length() > 4 && str.find (".dll") == (str.length() - 4));
346 }
347
348 int
349 PluginManager::vst_discover_from_path (string path)
350 {
351         PathScanner scanner;
352         vector<string *> *plugin_objects;
353         vector<string *>::iterator x;
354         int ret = 0;
355
356         info << "detecting VST plugins along " << path << endmsg;
357
358         plugin_objects = scanner (vst_path, vst_filter, 0, true, true);
359
360         if (plugin_objects) {
361                 for (x = plugin_objects->begin(); x != plugin_objects->end (); ++x) {
362                         vst_discover (**x);
363                 }
364         }
365
366         vector_delete (plugin_objects);
367         return ret;
368 }
369
370 int
371 PluginManager::vst_discover (string path)
372 {
373         FSTInfo* finfo;
374
375         if ((finfo = fst_get_info (const_cast<char *> (path.c_str()))) == 0) {
376                 return -1;
377         }
378
379         if (!finfo->canProcessReplacing) {
380                 warning << string_compose (_("VST plugin %1 does not support processReplacing, and so cannot be used in ardour at this time"),
381                                     finfo->name)
382                         << endl;
383         }
384         
385         PluginInfoPtr info(new VSTPluginInfo);
386
387         /* what a goddam joke freeware VST is */
388
389         if (!strcasecmp ("The Unnamed plugin", finfo->name)) {
390                 info->name = PBD::basename_nosuffix (path);
391         } else {
392                 info->name = finfo->name;
393         }
394
395         info->category = "VST";
396         info->path = path;
397         info->index = 0;
398         info->n_inputs = finfo->numInputs;
399         info->n_outputs = finfo->numOutputs;
400         info->type = PluginInfo::VST;
401         
402         _vst_plugin_info.push_back (info);
403         fst_free_info (finfo);
404
405         return 0;
406 }
407
408 #endif // VST_SUPPORT