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