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