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