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