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