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