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