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