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