partial patch/partial by-hand merge of 2.X commits 3169&3170 to 3.X codebase
[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 #include <cstdlib>
28 #include <fstream>
29
30 #ifdef VST_SUPPORT
31 #include <fst.h>
32 #include <pbd/basename.h>
33 #include <cstring>
34 #endif // VST_SUPPORT
35
36 #include <glibmm/miscutils.h>
37
38 #include <pbd/pathscanner.h>
39
40 #include <ardour/ladspa.h>
41 #include <ardour/session.h>
42 #include <ardour/plugin_manager.h>
43 #include <ardour/plugin.h>
44 #include <ardour/ladspa_plugin.h>
45 #include <ardour/filesystem_paths.h>
46
47 #ifdef HAVE_SLV2
48 #include <slv2/slv2.h>
49 #include <ardour/lv2_plugin.h>
50 #endif
51
52 #ifdef VST_SUPPORT
53 #include <ardour/vst_plugin.h>
54 #endif
55
56 #ifdef HAVE_AUDIOUNITS
57 #include <ardour/audio_unit.h>
58 #include <Carbon/Carbon.h>
59 #endif
60
61 #include <pbd/error.h>
62 #include <pbd/stl_delete.h>
63
64 #include "i18n.h"
65
66 using namespace ARDOUR;
67 using namespace PBD;
68 using namespace std;
69
70 PluginManager* PluginManager::_manager = 0;
71
72 PluginManager::PluginManager ()
73 {
74         char* s;
75         string lrdf_path;
76
77         load_favorites ();
78
79 #ifdef GTKOSX
80         ProcessSerialNumber psn = { 0, kCurrentProcess }; 
81         OSStatus returnCode = TransformProcessType(& psn, kProcessTransformToForegroundApplication);
82         if( returnCode != 0) {
83                 error << _("Cannot become GUI app") << endmsg;
84         }
85 #endif
86
87         if ((s = getenv ("LADSPA_RDF_PATH"))){
88                 lrdf_path = s;
89         }
90
91         if (lrdf_path.length() == 0) {
92                 lrdf_path = "/usr/local/share/ladspa/rdf:/usr/share/ladspa/rdf";
93         }
94
95         add_lrdf_data(lrdf_path);
96         add_ladspa_presets();
97 #ifdef VST_SUPPORT
98         if (Config->get_use_vst()) {
99                 add_vst_presets();
100         }
101 #endif /* VST_SUPPORT */
102
103         if ((s = getenv ("LADSPA_PATH"))) {
104                 ladspa_path = s;
105         }
106
107         if ((s = getenv ("VST_PATH"))) {
108                 vst_path = s;
109         } else if ((s = getenv ("VST_PLUGINS"))) {
110                 vst_path = s;
111         }
112
113         if (_manager == 0) {
114                 _manager = this;
115         }
116
117         /* the plugin manager is constructed too early to use Profile */
118
119         if (getenv ("ARDOUR_SAE")) {
120                 ladspa_plugin_whitelist.push_back (1203); // single band parametric
121                 ladspa_plugin_whitelist.push_back (1772); // caps compressor
122                 ladspa_plugin_whitelist.push_back (1913); // fast lookahead limiter
123                 ladspa_plugin_whitelist.push_back (1075); // simple RMS expander
124                 ladspa_plugin_whitelist.push_back (1061); // feedback delay line (max 5s)
125                 ladspa_plugin_whitelist.push_back (1216); // gverb
126                 ladspa_plugin_whitelist.push_back (2150); // tap pitch shifter
127         } 
128
129 #ifdef HAVE_SLV2
130         _lv2_world = new LV2World();
131 #endif
132
133         refresh ();
134 }
135
136 void
137 PluginManager::refresh ()
138 {
139         ladspa_refresh ();
140 #ifdef HAVE_SLV2
141         lv2_refresh ();
142 #endif
143 #ifdef VST_SUPPORT
144         if (Config->get_use_vst()) {
145                 vst_refresh ();
146         }
147 #endif // VST_SUPPORT
148 #ifdef HAVE_AUDIOUNITS
149         au_refresh ();
150 #endif
151 }
152
153 void
154 PluginManager::ladspa_refresh ()
155 {
156         _ladspa_plugin_info.clear ();
157         
158         static const char *standard_paths[] = {
159                 "/usr/local/lib64/ladspa",
160                 "/usr/local/lib/ladspa",
161                 "/usr/lib64/ladspa",
162                 "/usr/lib/ladspa",
163                 "/Library/Audio/Plug-Ins/LADSPA",
164                 ""
165         };
166         
167         /* allow LADSPA_PATH to augment, not override standard locations */
168  
169         /* Only add standard locations to ladspa_path if it doesn't
170          * already contain them. Check for trailing '/'s too.
171          */
172          
173         int i;
174         for (i = 0; standard_paths[i][0]; i++) {
175                 size_t found = ladspa_path.find(standard_paths[i]);
176                 if (found != ladspa_path.npos) {
177                         switch (ladspa_path[found + strlen(standard_paths[i])]) {
178                                 case ':' :
179                                 case '\0':
180                                         continue;
181                                 case '/' :
182                                         if (ladspa_path[found + strlen(standard_paths[i]) + 1] == ':' ||
183                                             ladspa_path[found + strlen(standard_paths[i]) + 1] == '\0') {
184                                                 continue;
185                                         }
186                         }
187                 }
188                 if (!ladspa_path.empty())
189                         ladspa_path += ":";
190  
191                 ladspa_path += standard_paths[i]; 
192                 
193         }
194   
195         ladspa_discover_from_path (ladspa_path);
196 }
197
198
199 int
200 PluginManager::add_ladspa_directory (string path)
201 {
202         if (ladspa_discover_from_path (path) == 0) {
203                 ladspa_path += ':';
204                 ladspa_path += path;
205                 return 0;
206         } 
207         return -1;
208 }
209
210 static bool ladspa_filter (const string& str, void *arg)
211 {
212         /* Not a dotfile, has a prefix before a period, suffix is "so" */
213         
214         return str[0] != '.' && (str.length() > 3 && str.find (".so") == (str.length() - 3));
215 }
216
217 int
218 PluginManager::ladspa_discover_from_path (string path)
219 {
220         PathScanner scanner;
221         vector<string *> *plugin_objects;
222         vector<string *>::iterator x;
223         int ret = 0;
224
225         plugin_objects = scanner (ladspa_path, ladspa_filter, 0, true, true);
226
227         if (plugin_objects) {
228                 for (x = plugin_objects->begin(); x != plugin_objects->end (); ++x) {
229                         ladspa_discover (**x);
230                 }
231         }
232
233         vector_delete (plugin_objects);
234         return ret;
235 }
236
237 static bool rdf_filter (const string &str, void *arg)
238 {
239         return str[0] != '.' && 
240                    ((str.find(".rdf")  == (str.length() - 4)) ||
241             (str.find(".rdfs") == (str.length() - 5)) ||
242                     (str.find(".n3")   == (str.length() - 3)));
243 }
244
245 void
246 PluginManager::add_ladspa_presets()
247 {
248         add_presets ("ladspa");
249 }
250
251 void
252 PluginManager::add_vst_presets()
253 {
254         add_presets ("vst");
255 }
256 void
257 PluginManager::add_presets(string domain)
258 {
259
260         PathScanner scanner;
261         vector<string *> *presets;
262         vector<string *>::iterator x;
263
264         char* envvar;
265         if ((envvar = getenv ("HOME")) == 0) {
266                 return;
267         }
268
269         string path = string_compose("%1/.%2/rdf", envvar, domain);
270         presets = scanner (path, rdf_filter, 0, true, true);
271
272         if (presets) {
273                 for (x = presets->begin(); x != presets->end (); ++x) {
274                         string file = "file:" + **x;
275                         if (lrdf_read_file(file.c_str())) {
276                                 warning << string_compose(_("Could not parse rdf file: %1"), *x) << endmsg;
277                         }
278                 }
279         }
280
281         vector_delete (presets);
282 }
283
284 void
285 PluginManager::add_lrdf_data (const string &path)
286 {
287         PathScanner scanner;
288         vector<string *>* rdf_files;
289         vector<string *>::iterator x;
290         string uri;
291
292         rdf_files = scanner (path, rdf_filter, 0, true, true);
293
294         if (rdf_files) {
295                 for (x = rdf_files->begin(); x != rdf_files->end (); ++x) {
296                         uri = "file://" + **x;
297
298                         if (lrdf_read_file(uri.c_str())) {
299                                 warning << "Could not parse rdf file: " << uri << endmsg;
300                         }
301                 }
302         }
303
304         vector_delete (rdf_files);
305 }
306
307 int 
308 PluginManager::ladspa_discover (string path)
309 {
310         void *module;
311         const LADSPA_Descriptor *descriptor;
312         LADSPA_Descriptor_Function dfunc;
313         const char *errstr;
314
315         if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
316                 error << string_compose(_("LADSPA: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
317                 return -1;
318         }
319
320         dfunc = (LADSPA_Descriptor_Function) dlsym (module, "ladspa_descriptor");
321
322         if ((errstr = dlerror()) != 0) {
323                 error << string_compose(_("LADSPA: module \"%1\" has no descriptor function."), path) << endmsg;
324                 error << errstr << endmsg;
325                 dlclose (module);
326                 return -1;
327         }
328
329         for (uint32_t i = 0; ; ++i) {
330                 if ((descriptor = dfunc (i)) == 0) {
331                         break;
332                 }
333
334                 if (!ladspa_plugin_whitelist.empty()) {
335                         if (find (ladspa_plugin_whitelist.begin(), ladspa_plugin_whitelist.end(), descriptor->UniqueID) == ladspa_plugin_whitelist.end()) {
336                                 continue;
337                         }
338                 } 
339
340                 PluginInfoPtr info(new LadspaPluginInfo);
341                 info->name = descriptor->Name;
342                 info->category = get_ladspa_category(descriptor->UniqueID);
343                 info->creator = descriptor->Maker;
344                 info->path = path;
345                 info->index = i;
346                 info->n_inputs = ChanCount();
347                 info->n_outputs = ChanCount();
348                 info->type = ARDOUR::LADSPA;
349                 
350                 char buf[32];
351                 snprintf (buf, sizeof (buf), "%lu", descriptor->UniqueID);
352                 info->unique_id = buf;
353                 
354                 for (uint32_t n=0; n < descriptor->PortCount; ++n) {
355                         if ( LADSPA_IS_PORT_AUDIO (descriptor->PortDescriptors[n]) ) {
356                                 if ( LADSPA_IS_PORT_INPUT (descriptor->PortDescriptors[n]) ) {
357                                         info->n_inputs.set_audio(info->n_inputs.n_audio() + 1);
358                                 }
359                                 else if ( LADSPA_IS_PORT_OUTPUT (descriptor->PortDescriptors[n]) ) {
360                                         info->n_outputs.set_audio(info->n_outputs.n_audio() + 1);
361                                 }
362                         }
363                 }
364
365                 _ladspa_plugin_info.push_back (info);
366         }
367
368 // GDB WILL NOT LIKE YOU IF YOU DO THIS
369 //      dlclose (module);
370
371         return 0;
372 }
373
374 string
375 PluginManager::get_ladspa_category (uint32_t plugin_id)
376 {
377         char buf[256];
378         lrdf_statement pattern;
379
380         snprintf(buf, sizeof(buf), "%s%" PRIu32, LADSPA_BASE, plugin_id);
381         pattern.subject = buf;
382         pattern.predicate = (char*)RDF_TYPE;
383         pattern.object = 0;
384         pattern.object_type = lrdf_uri;
385
386         lrdf_statement* matches1 = lrdf_matches (&pattern);
387
388         if (!matches1) {
389                 return "";
390         }
391
392         pattern.subject = matches1->object;
393         pattern.predicate = (char*)(LADSPA_BASE "hasLabel");
394         pattern.object = 0;
395         pattern.object_type = lrdf_literal;
396
397         lrdf_statement* matches2 = lrdf_matches (&pattern);
398         lrdf_free_statements(matches1);
399
400         if (!matches2) {
401                 return ("");
402         }
403
404         string label = matches2->object;
405         lrdf_free_statements(matches2);
406
407         return label;
408 }
409
410 #ifdef HAVE_SLV2
411 void
412 PluginManager::lv2_refresh ()
413 {
414         lv2_discover();
415 }
416
417 int
418 PluginManager::lv2_discover ()
419 {
420         _lv2_plugin_info = LV2PluginInfo::discover(_lv2_world);
421         return 0;
422 }
423 #endif
424
425 #ifdef HAVE_AUDIOUNITS
426 void
427 PluginManager::au_refresh ()
428 {
429         au_discover();
430 }
431
432 int
433 PluginManager::au_discover ()
434 {
435         _au_plugin_info = AUPluginInfo::discover();
436         return 0;
437 }
438
439 #endif
440
441 #ifdef VST_SUPPORT
442
443 void
444 PluginManager::vst_refresh ()
445 {
446         _vst_plugin_info.clear ();
447
448         if (vst_path.length() == 0) {
449                 vst_path = "/usr/local/lib/vst:/usr/lib/vst";
450         }
451
452         vst_discover_from_path (vst_path);
453 }
454
455 int
456 PluginManager::add_vst_directory (string path)
457 {
458         if (vst_discover_from_path (path) == 0) {
459                 vst_path += ':';
460                 vst_path += path;
461                 return 0;
462         } 
463         return -1;
464 }
465
466 static bool vst_filter (const string& str, void *arg)
467 {
468         /* Not a dotfile, has a prefix before a period, suffix is "dll" */
469
470         return str[0] != '.' && (str.length() > 4 && str.find (".dll") == (str.length() - 4));
471 }
472
473 int
474 PluginManager::vst_discover_from_path (string path)
475 {
476         PathScanner scanner;
477         vector<string *> *plugin_objects;
478         vector<string *>::iterator x;
479         int ret = 0;
480
481         info << "detecting VST plugins along " << path << endmsg;
482
483         plugin_objects = scanner (vst_path, vst_filter, 0, true, true);
484
485         if (plugin_objects) {
486                 for (x = plugin_objects->begin(); x != plugin_objects->end (); ++x) {
487                         vst_discover (**x);
488                 }
489         }
490
491         vector_delete (plugin_objects);
492         return ret;
493 }
494
495 int
496 PluginManager::vst_discover (string path)
497 {
498         FSTInfo* finfo;
499         char buf[32];
500
501         if ((finfo = fst_get_info (const_cast<char *> (path.c_str()))) == 0) {
502                 warning << "Cannot get VST information from " << path << endmsg;
503                 return -1;
504         }
505
506         if (!finfo->canProcessReplacing) {
507                 warning << string_compose (_("VST plugin %1 does not support processReplacing, and so cannot be used in ardour at this time"),
508                                     finfo->name)
509                         << endl;
510         }
511         
512         PluginInfoPtr info(new VSTPluginInfo);
513
514         /* what a joke freeware VST is */
515
516         if (!strcasecmp ("The Unnamed plugin", finfo->name)) {
517                 info->name = PBD::basename_nosuffix (path);
518         } else {
519                 info->name = finfo->name;
520         }
521
522         
523         snprintf (buf, sizeof (buf), "%d", finfo->UniqueID);
524         info->unique_id = buf;
525         info->category = "VST";
526         info->path = path;
527         // need to set info->creator but FST doesn't provide it
528         info->index = 0;
529         info->n_inputs = finfo->numInputs;
530         info->n_outputs = finfo->numOutputs;
531         info->type = ARDOUR::VST;
532         
533         _vst_plugin_info.push_back (info);
534         fst_free_info (finfo);
535
536         return 0;
537 }
538
539 #endif // VST_SUPPORT
540
541 bool
542 PluginManager::is_a_favorite_plugin (const PluginInfoPtr& pi)
543 {
544         FavoritePlugin fp (pi->type, pi->unique_id);
545         return find (favorites.begin(), favorites.end(), fp) !=  favorites.end();
546 }
547
548 void
549 PluginManager::save_favorites ()
550 {
551         ofstream ofs;
552         sys::path path = user_config_directory();
553         path /= "favorite_plugins";
554
555         ofs.open (path.to_string().c_str(), ios_base::openmode (ios::out|ios::trunc));
556
557         if (!ofs) {
558                 return;
559         }
560
561         for (FavoritePluginList::iterator i = favorites.begin(); i != favorites.end(); ++i) {
562                 switch ((*i).type) {
563                 case LADSPA:
564                         ofs << "LADSPA";
565                         break;
566                 case AudioUnit:
567                         ofs << "AudioUnit";
568                         break;
569                 case LV2:
570                         ofs << "LV2";
571                         break;
572                 case VST:
573                         ofs << "VST";
574                         break;
575                 }
576                 
577                 ofs << ' ' << (*i).unique_id << endl;
578         }
579
580         ofs.close ();
581 }
582
583 void
584 PluginManager::load_favorites ()
585 {
586         sys::path path = user_config_directory();
587         path /= "favorite_plugins";
588         ifstream ifs (path.to_string().c_str());
589
590         if (!ifs) {
591                 return;
592         }
593         
594         std::string stype;
595         std::string id;
596         PluginType type;
597
598         while (ifs) {
599
600                 ifs >> stype;
601                 if (!ifs) {
602                         break;
603
604                 }
605                 ifs >> id;
606                 if (!ifs) {
607                         break;
608                 }
609
610                 if (stype == "LADSPA") {
611                         type = LADSPA;
612                 } else if (stype == "AudioUnit") {
613                         type = AudioUnit;
614                 } else if (stype == "LV2") {
615                         type = LV2;
616                 } else if (stype == "VST") {
617                         type = VST;
618                 } else {
619                         error << string_compose (_("unknown favorite plugin type \"%1\" - ignored"), stype)
620                               << endmsg;
621                         continue;
622                 }
623                 
624                 add_favorite (type, id);
625         }
626         
627         ifs.close ();
628 }
629
630 void
631 PluginManager::add_favorite (PluginType t, string id)
632 {
633         FavoritePlugin fp (t, id);
634         pair<FavoritePluginList::iterator,bool> res = favorites.insert (fp);
635         cerr << "Added " << t << " " << id << " success ? " << res.second << endl;
636 }
637
638 void
639 PluginManager::remove_favorite (PluginType t, string id)
640 {
641         FavoritePlugin fp (t, id);
642         favorites.erase (fp);
643 }