add missing semicolon
[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 #include <stdint.h>
25
26 #include <sys/types.h>
27 #include <cstdio>
28 #include <cstdlib>
29
30 #include <glib.h>
31 #include "pbd/gstdio_compat.h"
32
33 #ifdef HAVE_LRDF
34 #include <lrdf.h>
35 #endif
36
37 #ifdef WINDOWS_VST_SUPPORT
38 #include "ardour/vst_info_file.h"
39 #include "fst.h"
40 #include "pbd/basename.h"
41 #include <cstring>
42
43 // dll-info
44 #include <sys/stat.h>
45 #include <fcntl.h>
46 #include <unistd.h>
47 #include <stdint.h>
48
49 #endif // WINDOWS_VST_SUPPORT
50
51 #ifdef LXVST_SUPPORT
52 #include "ardour/vst_info_file.h"
53 #include "ardour/linux_vst_support.h"
54 #include "pbd/basename.h"
55 #include <cstring>
56 #endif //LXVST_SUPPORT
57
58 #include <glibmm/miscutils.h>
59 #include <glibmm/pattern.h>
60 #include <glibmm/fileutils.h>
61 #include <glibmm/miscutils.h>
62
63 #include "pbd/whitespace.h"
64 #include "pbd/file_utils.h"
65
66 #include "ardour/debug.h"
67 #include "ardour/filesystem_paths.h"
68 #include "ardour/ladspa.h"
69 #include "ardour/ladspa_plugin.h"
70 #include "ardour/plugin.h"
71 #include "ardour/plugin_manager.h"
72 #include "ardour/rc_configuration.h"
73
74 #include "ardour/search_paths.h"
75
76 #ifdef LV2_SUPPORT
77 #include "ardour/lv2_plugin.h"
78 #endif
79
80 #ifdef WINDOWS_VST_SUPPORT
81 #include "ardour/windows_vst_plugin.h"
82 #endif
83
84 #ifdef LXVST_SUPPORT
85 #include "ardour/lxvst_plugin.h"
86 #endif
87
88 #ifdef AUDIOUNIT_SUPPORT
89 #include "ardour/audio_unit.h"
90 #include <Carbon/Carbon.h>
91 #endif
92
93 #include "pbd/error.h"
94 #include "pbd/stl_delete.h"
95
96 #include "i18n.h"
97
98 #include "ardour/debug.h"
99
100 using namespace ARDOUR;
101 using namespace PBD;
102 using namespace std;
103
104 PluginManager* PluginManager::_instance = 0;
105 std::string PluginManager::scanner_bin_path = "";
106
107 PluginManager&
108 PluginManager::instance()
109 {
110         if (!_instance) {
111                 _instance = new PluginManager;
112         }
113         return *_instance;
114 }
115
116 PluginManager::PluginManager ()
117         : _windows_vst_plugin_info(0)
118         , _lxvst_plugin_info(0)
119         , _ladspa_plugin_info(0)
120         , _lv2_plugin_info(0)
121         , _au_plugin_info(0)
122         , _cancel_scan(false)
123         , _cancel_timeout(false)
124 {
125         char* s;
126         string lrdf_path;
127
128 #if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT
129         // source-tree (ardev, etc)
130         PBD::Searchpath vstsp(Glib::build_filename(ARDOUR::ardour_dll_directory(), "fst"));
131
132 #ifdef PLATFORM_WINDOWS
133         // on windows the .exe needs to be in the same folder with libardour.dll
134         vstsp += Glib::build_filename(windows_package_directory_path(), "bin");
135 #else
136         // on Unices additional internal-use binaries are deployed to $libdir
137         vstsp += ARDOUR::ardour_dll_directory();
138 #endif
139
140         if (!PBD::find_file (vstsp,
141 #ifdef PLATFORM_WINDOWS
142     #ifdef DEBUGGABLE_SCANNER_APP
143         #if defined(DEBUG) || defined(_DEBUG)
144                                 "ardour-vst-scannerD.exe"
145         #else
146                                 "ardour-vst-scannerRDC.exe"
147         #endif
148     #else
149                                 "ardour-vst-scanner.exe"
150     #endif
151 #else
152                                 "ardour-vst-scanner"
153 #endif
154                                 , scanner_bin_path)) {
155                 PBD::warning << "VST scanner app (ardour-vst-scanner) not found in path " << vstsp.to_string() <<  endmsg;
156         }
157 #endif
158
159         load_statuses ();
160
161         if ((s = getenv ("LADSPA_RDF_PATH"))){
162                 lrdf_path = s;
163         }
164
165         if (lrdf_path.length() == 0) {
166                 lrdf_path = "/usr/local/share/ladspa/rdf:/usr/share/ladspa/rdf";
167         }
168
169         add_lrdf_data(lrdf_path);
170         add_ladspa_presets();
171 #ifdef WINDOWS_VST_SUPPORT
172         if (Config->get_use_windows_vst ()) {
173                 add_windows_vst_presets ();
174         }
175 #endif /* WINDOWS_VST_SUPPORT */
176
177 #ifdef LXVST_SUPPORT
178         if (Config->get_use_lxvst()) {
179                 add_lxvst_presets();
180         }
181 #endif /* Native LinuxVST support*/
182
183         if ((s = getenv ("VST_PATH"))) {
184                 windows_vst_path = s;
185         } else if ((s = getenv ("VST_PLUGINS"))) {
186                 windows_vst_path = s;
187         }
188
189         if (windows_vst_path.length() == 0) {
190                 windows_vst_path = vst_search_path ();
191         }
192
193         if ((s = getenv ("LXVST_PATH"))) {
194                 lxvst_path = s;
195         } else if ((s = getenv ("LXVST_PLUGINS"))) {
196                 lxvst_path = s;
197         }
198
199         if (lxvst_path.length() == 0) {
200                 lxvst_path = "/usr/local/lib64/lxvst:/usr/local/lib/lxvst:/usr/lib64/lxvst:/usr/lib/lxvst:"
201                         "/usr/local/lib64/linux_vst:/usr/local/lib/linux_vst:/usr/lib64/linux_vst:/usr/lib/linux_vst:"
202                         "/usr/lib/vst:/usr/local/lib/vst";
203         }
204
205         /* first time setup, use 'default' path */
206         if (Config->get_plugin_path_lxvst() == X_("@default@")) {
207                 Config->set_plugin_path_lxvst(get_default_lxvst_path());
208         }
209         if (Config->get_plugin_path_vst() == X_("@default@")) {
210                 Config->set_plugin_path_vst(get_default_windows_vst_path());
211         }
212
213         if (_instance == 0) {
214                 _instance = this;
215         }
216
217         BootMessage (_("Discovering Plugins"));
218 }
219
220
221 PluginManager::~PluginManager()
222 {
223         if (getenv ("ARDOUR_RUNNING_UNDER_VALGRIND")) {
224                 // don't bother, just exit quickly.
225                 delete _windows_vst_plugin_info;
226                 delete _lxvst_plugin_info;
227                 delete _ladspa_plugin_info;
228                 delete _lv2_plugin_info;
229                 delete _au_plugin_info;
230         }
231 }
232
233 void
234 PluginManager::refresh (bool cache_only)
235 {
236         Glib::Threads::Mutex::Lock lm (_lock, Glib::Threads::TRY_LOCK);
237
238         if (!lm.locked()) {
239                 return;
240         }
241
242         DEBUG_TRACE (DEBUG::PluginManager, "PluginManager::refresh\n");
243         _cancel_scan = false;
244
245         BootMessage (_("Scanning LADSPA Plugins"));
246         ladspa_refresh ();
247 #ifdef LV2_SUPPORT
248         BootMessage (_("Scanning LV2 Plugins"));
249         lv2_refresh ();
250 #endif
251 #ifdef WINDOWS_VST_SUPPORT
252         if (Config->get_use_windows_vst()) {
253                 if (cache_only) {
254                         BootMessage (_("Scanning Windows VST Plugins"));
255                 } else {
256                         BootMessage (_("Discovering Windows VST Plugins"));
257                 }
258                 windows_vst_refresh (cache_only);
259         }
260 #endif // WINDOWS_VST_SUPPORT
261
262 #ifdef LXVST_SUPPORT
263         if(Config->get_use_lxvst()) {
264                 if (cache_only) {
265                         BootMessage (_("Scanning Linux VST Plugins"));
266                 } else {
267                         BootMessage (_("Discovering Linux VST Plugins"));
268                 }
269                 lxvst_refresh(cache_only);
270         }
271 #endif //Native linuxVST SUPPORT
272
273 #if (defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT)
274                 if (!cache_only) {
275                         string fn = Glib::build_filename (ARDOUR::user_cache_directory(), VST_BLACKLIST);
276                         if (Glib::file_test (fn, Glib::FILE_TEST_EXISTS)) {
277                                 gchar *bl = NULL;
278                                 if (g_file_get_contents(fn.c_str (), &bl, NULL, NULL)) {
279                                         PBD::info << _("VST Blacklist:") << "\n" << bl << "-----" << endmsg;
280                                         g_free (bl);
281                                 }
282                         }
283                 }
284 #endif
285
286 #ifdef AUDIOUNIT_SUPPORT
287         if (cache_only) {
288                 BootMessage (_("Scanning AU Plugins"));
289         } else {
290                 BootMessage (_("Discovering AU Plugins"));
291         }
292         au_refresh (cache_only);
293 #endif
294
295         BootMessage (_("Plugin Scan Complete..."));
296         PluginListChanged (); /* EMIT SIGNAL */
297         PluginScanMessage(X_("closeme"), "", false);
298         _cancel_scan = false;
299 }
300
301 void
302 PluginManager::cancel_plugin_scan ()
303 {
304         _cancel_scan = true;
305 }
306
307 void
308 PluginManager::cancel_plugin_timeout ()
309 {
310         _cancel_timeout = true;
311 }
312
313 void
314 PluginManager::clear_vst_cache ()
315 {
316 #if 1 // clean old cache and error files. (remove this code after 4.3 or 5.0)
317 #ifdef WINDOWS_VST_SUPPORT
318         {
319                 vector<string> fsi_files;
320                 find_files_matching_regex (fsi_files, Config->get_plugin_path_vst(), "\\" VST_EXT_INFOFILE "$", true);
321                 for (vector<string>::iterator i = fsi_files.begin(); i != fsi_files.end (); ++i) {
322                         ::g_unlink(i->c_str());
323                 }
324         }
325         {
326                 vector<string> fsi_files;
327                 find_files_matching_regex (fsi_files, Config->get_plugin_path_vst(), "\\.fsi$", true);
328                 for (vector<string>::iterator i = fsi_files.begin(); i != fsi_files.end (); ++i) {
329                         ::g_unlink(i->c_str());
330                 }
331         }
332         {
333                 vector<string> fsi_files;
334                 find_files_matching_regex (fsi_files, Config->get_plugin_path_vst(), "\\.err$", true);
335                 for (vector<string>::iterator i = fsi_files.begin(); i != fsi_files.end (); ++i) {
336                         ::g_unlink(i->c_str());
337                 }
338         }
339 #endif
340
341 #ifdef LXVST_SUPPORT
342         {
343                 vector<string> fsi_files;
344                 find_files_matching_regex (fsi_files, Config->get_plugin_path_lxvst(), "\\" VST_EXT_INFOFILE "$", true);
345                 for (vector<string>::iterator i = fsi_files.begin(); i != fsi_files.end (); ++i) {
346                         ::g_unlink(i->c_str());
347                 }
348         }
349         {
350                 vector<string> fsi_files;
351                 find_files_matching_regex (fsi_files, Config->get_plugin_path_lxvst(), "\\.fsi$", true);
352                 for (vector<string>::iterator i = fsi_files.begin(); i != fsi_files.end (); ++i) {
353                         ::g_unlink(i->c_str());
354                 }
355         }
356         {
357                 vector<string> fsi_files;
358                 find_files_matching_regex (fsi_files, Config->get_plugin_path_lxvst(), "\\.err$", true);
359                 for (vector<string>::iterator i = fsi_files.begin(); i != fsi_files.end (); ++i) {
360                         ::g_unlink(i->c_str());
361                 }
362         }
363 #endif
364 #if (defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT)
365         {
366                 string dir = Glib::build_filename (ARDOUR::user_cache_directory(), "fst_info");
367                 if (Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
368                         PBD::remove_directory (dir);
369                 }
370         }
371 #endif
372 #endif // old cache cleanup
373
374 #if (defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT)
375         {
376                 string dn = Glib::build_filename (ARDOUR::user_cache_directory(), "vst");
377                 vector<string> fsi_files;
378                 find_files_matching_regex (fsi_files, dn, "\\" VST_EXT_INFOFILE "$", /* user cache is flat, no recursion */ false);
379                 for (vector<string>::iterator i = fsi_files.begin(); i != fsi_files.end (); ++i) {
380                         ::g_unlink(i->c_str());
381                 }
382         }
383 #endif
384 }
385
386 void
387 PluginManager::clear_vst_blacklist ()
388 {
389 #if 1 // remove old blacklist files. (remove this code after 4.3 or 5.0)
390
391 #ifdef WINDOWS_VST_SUPPORT
392         {
393                 vector<string> fsi_files;
394                 find_files_matching_regex (fsi_files, Config->get_plugin_path_vst(), "\\" VST_EXT_BLACKLIST "$", true);
395                 for (vector<string>::iterator i = fsi_files.begin(); i != fsi_files.end (); ++i) {
396                         ::g_unlink(i->c_str());
397                 }
398         }
399 #endif
400
401 #ifdef LXVST_SUPPORT
402         {
403                 vector<string> fsi_files;
404                 find_files_matching_regex (fsi_files, Config->get_plugin_path_lxvst(), "\\" VST_EXT_BLACKLIST "$", true);
405                 for (vector<string>::iterator i = fsi_files.begin(); i != fsi_files.end (); ++i) {
406                         ::g_unlink(i->c_str());
407                 }
408         }
409 #endif
410 #if (defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT)
411         {
412                 string dir = Glib::build_filename (ARDOUR::user_cache_directory(), "fst_blacklist");
413                 if (Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
414                         PBD::remove_directory (dir);
415                 }
416         }
417 #endif
418
419 #endif // old blacklist cleanup
420
421 #if (defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT)
422         {
423                 string fn = Glib::build_filename (ARDOUR::user_cache_directory(), VST_BLACKLIST);
424                 if (Glib::file_test (fn, Glib::FILE_TEST_EXISTS)) {
425                         ::g_unlink (fn.c_str());
426                 }
427         }
428 #endif
429
430 }
431
432 void
433 PluginManager::clear_au_cache ()
434 {
435 #ifdef AUDIOUNIT_SUPPORT
436         // AUPluginInfo::au_cache_path ()
437         string fn = Glib::build_filename (ARDOUR::user_config_directory(), "au_cache");
438         if (Glib::file_test (fn, Glib::FILE_TEST_EXISTS)) {
439                 ::g_unlink(fn.c_str());
440         }
441 #endif
442 }
443
444 void
445 PluginManager::clear_au_blacklist ()
446 {
447 #ifdef AUDIOUNIT_SUPPORT
448         string fn = Glib::build_filename (ARDOUR::user_cache_directory(), "au_blacklist.txt");
449         if (Glib::file_test (fn, Glib::FILE_TEST_EXISTS)) {
450                 ::g_unlink(fn.c_str());
451         }
452 #endif
453 }
454
455 void
456 PluginManager::ladspa_refresh ()
457 {
458         if (_ladspa_plugin_info) {
459                 _ladspa_plugin_info->clear ();
460         } else {
461                 _ladspa_plugin_info = new ARDOUR::PluginInfoList ();
462         }
463
464         /* allow LADSPA_PATH to augment, not override standard locations */
465
466         /* Only add standard locations to ladspa_path if it doesn't
467          * already contain them. Check for trailing G_DIR_SEPARATOR too.
468          */
469
470         vector<string> ladspa_modules;
471
472         DEBUG_TRACE (DEBUG::PluginManager, string_compose ("LADSPA: search along: [%1]\n", ladspa_search_path().to_string()));
473
474         find_files_matching_pattern (ladspa_modules, ladspa_search_path (), "*.so");
475         find_files_matching_pattern (ladspa_modules, ladspa_search_path (), "*.dylib");
476         find_files_matching_pattern (ladspa_modules, ladspa_search_path (), "*.dll");
477
478         for (vector<std::string>::iterator i = ladspa_modules.begin(); i != ladspa_modules.end(); ++i) {
479                 ARDOUR::PluginScanMessage(_("LADSPA"), *i, false);
480                 ladspa_discover (*i);
481         }
482 }
483
484 #ifdef HAVE_LRDF
485 static bool rdf_filter (const string &str, void* /*arg*/)
486 {
487         return str[0] != '.' &&
488                    ((str.find(".rdf")  == (str.length() - 4)) ||
489             (str.find(".rdfs") == (str.length() - 5)) ||
490                     (str.find(".n3")   == (str.length() - 3)) ||
491                     (str.find(".ttl")  == (str.length() - 4)));
492 }
493 #endif
494
495 void
496 PluginManager::add_ladspa_presets()
497 {
498         add_presets ("ladspa");
499 }
500
501 void
502 PluginManager::add_windows_vst_presets()
503 {
504         add_presets ("windows-vst");
505 }
506
507 void
508 PluginManager::add_lxvst_presets()
509 {
510         add_presets ("lxvst");
511 }
512
513 void
514 PluginManager::add_presets(string domain)
515 {
516 #ifdef HAVE_LRDF
517         vector<string> presets;
518         vector<string>::iterator x;
519
520         char* envvar;
521         if ((envvar = getenv ("HOME")) == 0) {
522                 return;
523         }
524
525         string path = string_compose("%1/.%2/rdf", envvar, domain);
526         find_files_matching_filter (presets, path, rdf_filter, 0, false, true);
527
528         for (x = presets.begin(); x != presets.end (); ++x) {
529                 string file = "file:" + *x;
530                 if (lrdf_read_file(file.c_str())) {
531                         warning << string_compose(_("Could not parse rdf file: %1"), *x) << endmsg;
532                 }
533         }
534
535 #endif
536 }
537
538 void
539 PluginManager::add_lrdf_data (const string &path)
540 {
541 #ifdef HAVE_LRDF
542         vector<string> rdf_files;
543         vector<string>::iterator x;
544
545         find_files_matching_filter (rdf_files, path, rdf_filter, 0, false, true);
546
547         for (x = rdf_files.begin(); x != rdf_files.end (); ++x) {
548                 const string uri(string("file://") + *x);
549
550                 if (lrdf_read_file(uri.c_str())) {
551                         warning << "Could not parse rdf file: " << uri << endmsg;
552                 }
553         }
554 #endif
555 }
556
557 int
558 PluginManager::ladspa_discover (string path)
559 {
560         DEBUG_TRACE (DEBUG::PluginManager, string_compose ("Checking for LADSPA plugin at %1\n", path));
561
562         Glib::Module module(path);
563         const LADSPA_Descriptor *descriptor;
564         LADSPA_Descriptor_Function dfunc;
565         void* func = 0;
566
567         if (!module) {
568                 error << string_compose(_("LADSPA: cannot load module \"%1\" (%2)"),
569                         path, Glib::Module::get_last_error()) << endmsg;
570                 return -1;
571         }
572
573
574         if (!module.get_symbol("ladspa_descriptor", func)) {
575                 error << string_compose(_("LADSPA: module \"%1\" has no descriptor function."), path) << endmsg;
576                 error << Glib::Module::get_last_error() << endmsg;
577                 return -1;
578         }
579
580         dfunc = (LADSPA_Descriptor_Function)func;
581
582         DEBUG_TRACE (DEBUG::PluginManager, string_compose ("LADSPA plugin found at %1\n", path));
583
584         for (uint32_t i = 0; ; ++i) {
585                 if ((descriptor = dfunc (i)) == 0) {
586                         break;
587                 }
588
589                 if (!ladspa_plugin_whitelist.empty()) {
590                         if (find (ladspa_plugin_whitelist.begin(), ladspa_plugin_whitelist.end(), descriptor->UniqueID) == ladspa_plugin_whitelist.end()) {
591                                 continue;
592                         }
593                 }
594
595                 PluginInfoPtr info(new LadspaPluginInfo);
596                 info->name = descriptor->Name;
597                 info->category = get_ladspa_category(descriptor->UniqueID);
598                 info->creator = descriptor->Maker;
599                 info->path = path;
600                 info->index = i;
601                 info->n_inputs = ChanCount();
602                 info->n_outputs = ChanCount();
603                 info->type = ARDOUR::LADSPA;
604
605                 char buf[32];
606                 snprintf (buf, sizeof (buf), "%lu", descriptor->UniqueID);
607                 info->unique_id = buf;
608
609                 for (uint32_t n=0; n < descriptor->PortCount; ++n) {
610                         if ( LADSPA_IS_PORT_AUDIO (descriptor->PortDescriptors[n]) ) {
611                                 if ( LADSPA_IS_PORT_INPUT (descriptor->PortDescriptors[n]) ) {
612                                         info->n_inputs.set_audio(info->n_inputs.n_audio() + 1);
613                                 }
614                                 else if ( LADSPA_IS_PORT_OUTPUT (descriptor->PortDescriptors[n]) ) {
615                                         info->n_outputs.set_audio(info->n_outputs.n_audio() + 1);
616                                 }
617                         }
618                 }
619
620                 if(_ladspa_plugin_info->empty()){
621                         _ladspa_plugin_info->push_back (info);
622                 }
623
624                 //Ensure that the plugin is not already in the plugin list.
625
626                 bool found = false;
627
628                 for (PluginInfoList::const_iterator i = _ladspa_plugin_info->begin(); i != _ladspa_plugin_info->end(); ++i) {
629                         if(0 == info->unique_id.compare((*i)->unique_id)){
630                               found = true;
631                         }
632                 }
633
634                 if(!found){
635                     _ladspa_plugin_info->push_back (info);
636                 }
637
638                 DEBUG_TRACE (DEBUG::PluginManager, string_compose ("Found LADSPA plugin, name: %1, Inputs: %2, Outputs: %3\n", info->name, info->n_inputs, info->n_outputs));
639         }
640
641 // GDB WILL NOT LIKE YOU IF YOU DO THIS
642 //      dlclose (module);
643
644         return 0;
645 }
646
647 string
648 PluginManager::get_ladspa_category (uint32_t plugin_id)
649 {
650 #ifdef HAVE_LRDF
651         char buf[256];
652         lrdf_statement pattern;
653
654         snprintf(buf, sizeof(buf), "%s%" PRIu32, LADSPA_BASE, plugin_id);
655         pattern.subject = buf;
656         pattern.predicate = const_cast<char*>(RDF_TYPE);
657         pattern.object = 0;
658         pattern.object_type = lrdf_uri;
659
660         lrdf_statement* matches1 = lrdf_matches (&pattern);
661
662         if (!matches1) {
663                 return "Unknown";
664         }
665
666         pattern.subject = matches1->object;
667         pattern.predicate = const_cast<char*>(LADSPA_BASE "hasLabel");
668         pattern.object = 0;
669         pattern.object_type = lrdf_literal;
670
671         lrdf_statement* matches2 = lrdf_matches (&pattern);
672         lrdf_free_statements(matches1);
673
674         if (!matches2) {
675                 return ("Unknown");
676         }
677
678         string label = matches2->object;
679         lrdf_free_statements(matches2);
680
681         /* Kludge LADSPA class names to be singular and match LV2 class names.
682            This avoids duplicate plugin menus for every class, which is necessary
683            to make the plugin category menu at all usable, but is obviously a
684            filthy kludge.
685
686            In the short term, lrdf could be updated so the labels match and a new
687            release made. To support both specs, we should probably be mapping the
688            URIs to the same category in code and perhaps tweaking that hierarchy
689            dynamically to suit the user. Personally, I (drobilla) think that time
690            is better spent replacing the little-used LRDF.
691
692            In the longer term, we will abandon LRDF entirely in favour of LV2 and
693            use that class hierarchy. Aside from fixing this problem properly, that
694            will also allow for translated labels. SWH plugins have been LV2 for
695            ages; TAP needs porting. I don't know of anything else with LRDF data.
696         */
697         if (label == "Utilities") {
698                 return "Utility";
699         } else if (label == "Pitch shifters") {
700                 return "Pitch Shifter";
701         } else if (label != "Dynamics" && label != "Chorus"
702                    &&label[label.length() - 1] == 's'
703                    && label[label.length() - 2] != 's') {
704                 return label.substr(0, label.length() - 1);
705         } else {
706                 return label;
707         }
708 #else
709                 return ("Unknown");
710 #endif
711 }
712
713 #ifdef LV2_SUPPORT
714 void
715 PluginManager::lv2_refresh ()
716 {
717         DEBUG_TRACE (DEBUG::PluginManager, "LV2: refresh\n");
718         delete _lv2_plugin_info;
719         _lv2_plugin_info = LV2PluginInfo::discover();
720 }
721 #endif
722
723 #ifdef AUDIOUNIT_SUPPORT
724 void
725 PluginManager::au_refresh (bool cache_only)
726 {
727         DEBUG_TRACE (DEBUG::PluginManager, "AU: refresh\n");
728
729         // disable automatic discovery in case we crash
730         bool discover_at_start = Config->get_discover_audio_units ();
731         Config->set_discover_audio_units (false);
732         Config->save_state();
733
734         delete _au_plugin_info;
735         _au_plugin_info = AUPluginInfo::discover(cache_only && !discover_at_start);
736
737         // successful scan re-enabled automatic discovery if it was set
738         Config->set_discover_audio_units (discover_at_start);
739         Config->save_state();
740 }
741
742 #endif
743
744 #ifdef WINDOWS_VST_SUPPORT
745
746 void
747 PluginManager::windows_vst_refresh (bool cache_only)
748 {
749         if (_windows_vst_plugin_info) {
750                 _windows_vst_plugin_info->clear ();
751         } else {
752                 _windows_vst_plugin_info = new ARDOUR::PluginInfoList();
753         }
754
755         windows_vst_discover_from_path (Config->get_plugin_path_vst(), cache_only);
756 }
757
758 static bool windows_vst_filter (const string& str, void * /*arg*/)
759 {
760         /* Not a dotfile, has a prefix before a period, suffix is "dll" */
761         return str[0] != '.' && str.length() > 4 && strings_equal_ignore_case (".dll", str.substr(str.length() - 4));
762 }
763
764 int
765 PluginManager::windows_vst_discover_from_path (string path, bool cache_only)
766 {
767         vector<string> plugin_objects;
768         vector<string>::iterator x;
769         int ret = 0;
770
771         DEBUG_TRACE (DEBUG::PluginManager, string_compose ("Discovering Windows VST plugins along %1\n", path));
772
773         if (Config->get_verbose_plugin_scan()) {
774                 info << string_compose (_("--- Windows VST plugins Scan: %1"), path) << endmsg;
775         }
776
777         find_files_matching_filter (plugin_objects, Config->get_plugin_path_vst(), windows_vst_filter, 0, false, true, true);
778
779         for (x = plugin_objects.begin(); x != plugin_objects.end (); ++x) {
780                 ARDOUR::PluginScanMessage(_("VST"), *x, !cache_only && !cancelled());
781                 windows_vst_discover (*x, cache_only || cancelled());
782         }
783
784         if (Config->get_verbose_plugin_scan()) {
785                 info << _("--- Windows VST plugins Scan Done") << endmsg;
786         }
787
788         return ret;
789 }
790
791 static std::string dll_info (std::string path) {
792         std::string rv;
793         uint8_t buf[68];
794         uint16_t type = 0;
795         off_t pe_hdr_off = 0;
796
797         int fd = open(path.c_str(), O_RDONLY, 0444);
798
799         if (fd < 0) {
800                 return _("- cannot open dll"); // TODO strerror()
801         }
802
803         if (68 != read (fd, buf, 68)) {
804                 rv = _("- invalid dll, file too small");
805                 goto errorout;
806         }
807         if (buf[0] != 'M' && buf[1] != 'Z') {
808                 rv = _("- not a dll");
809                 goto errorout;
810         }
811
812         pe_hdr_off = *((int32_t*) &buf[60]);
813         if (pe_hdr_off !=lseek (fd, pe_hdr_off, SEEK_SET)) {
814                 rv = _("- cannot determine dll type");
815                 goto errorout;
816         }
817         if (6 != read (fd, buf, 6)) {
818                 rv = _("- cannot read dll PE header");
819                 goto errorout;
820         }
821
822         if (buf[0] != 'P' && buf[1] != 'E') {
823                 rv = _("- invalid dll PE header");
824                 goto errorout;
825         }
826
827         type = *((uint16_t*) &buf[4]);
828         switch (type) {
829                 case 0x014c:
830                         rv = _("- i386 (32bit)");
831                         break;
832                 case  0x0200:
833                         rv = _("- Itanium");
834                         break;
835                 case 0x8664:
836                         rv = _("- x64 (64bit)");
837                         break;
838                 case 0:
839                         rv = _("- Native Architecture");
840                         break;
841                 default:
842                         rv = _("- Unknown Architecture");
843                         break;
844         }
845 errorout:
846         close (fd);
847         return rv;
848 }
849
850 int
851 PluginManager::windows_vst_discover (string path, bool cache_only)
852 {
853         DEBUG_TRACE (DEBUG::PluginManager, string_compose ("windows_vst_discover '%1'\n", path));
854
855         if (Config->get_verbose_plugin_scan()) {
856                 info << string_compose (_(" *  %1 %2 %3"), path, (cache_only ? _(" (cache only)") : "", dll_info (path))) << endmsg;
857         }
858
859         _cancel_timeout = false;
860         vector<VSTInfo*> * finfos = vstfx_get_info_fst (const_cast<char *> (path.c_str()),
861                         cache_only ? VST_SCAN_CACHE_ONLY : VST_SCAN_USE_APP);
862
863         // TODO  get extended error messae from vstfx_get_info_fst() e.g  blacklisted, 32/64bit compat,
864         // .err file scanner output etc.
865
866         if (finfos->empty()) {
867                 DEBUG_TRACE (DEBUG::PluginManager, string_compose ("Cannot get Windows VST information from '%1'\n", path));
868                 if (Config->get_verbose_plugin_scan()) {
869                         info << _(" -> Cannot get Windows VST information, plugin ignored.") << endmsg;
870                 }
871                 return -1;
872         }
873
874         uint32_t discovered = 0;
875         for (vector<VSTInfo *>::iterator x = finfos->begin(); x != finfos->end(); ++x) {
876                 VSTInfo* finfo = *x;
877                 char buf[32];
878
879                 if (!finfo->canProcessReplacing) {
880                         warning << string_compose (_("VST plugin %1 does not support processReplacing, and cannot be used in %2 at this time"),
881                                                          finfo->name, PROGRAM_NAME)
882                                 << endl;
883                         continue;
884                 }
885
886                 PluginInfoPtr info (new WindowsVSTPluginInfo);
887
888                 /* what a joke freeware VST is */
889
890                 if (!strcasecmp ("The Unnamed plugin", finfo->name)) {
891                         info->name = PBD::basename_nosuffix (path);
892                 } else {
893                         info->name = finfo->name;
894                 }
895
896
897                 snprintf (buf, sizeof (buf), "%d", finfo->UniqueID);
898                 info->unique_id = buf;
899                 info->category = "VST";
900                 info->path = path;
901                 info->creator = finfo->creator;
902                 info->index = 0;
903                 info->n_inputs.set_audio (finfo->numInputs);
904                 info->n_outputs.set_audio (finfo->numOutputs);
905                 info->n_inputs.set_midi ((finfo->wantMidi&1) ? 1 : 0);
906                 info->n_outputs.set_midi ((finfo->wantMidi&2) ? 1 : 0);
907                 info->type = ARDOUR::Windows_VST;
908
909                 // TODO: check dup-IDs (lxvst AND windows vst)
910                 bool duplicate = false;
911
912                 if (!_windows_vst_plugin_info->empty()) {
913                         for (PluginInfoList::iterator i =_windows_vst_plugin_info->begin(); i != _windows_vst_plugin_info->end(); ++i) {
914                                 if ((info->type == (*i)->type) && (info->unique_id == (*i)->unique_id)) {
915                                         warning << string_compose (_("Ignoring duplicate Windows VST plugin \"%1\""), info->name) << endmsg;
916                                         duplicate = true;
917                                         break;
918                                 }
919                         }
920                 }
921
922                 if (!duplicate) {
923                         DEBUG_TRACE (DEBUG::PluginManager, string_compose ("Windows VST plugin ID '%1'\n", info->unique_id));
924                         _windows_vst_plugin_info->push_back (info);
925                         discovered++;
926                         if (Config->get_verbose_plugin_scan()) {
927                                 PBD::info << string_compose (_(" -> OK (VST Plugin \"%1\" was added)."), info->name) << endmsg;
928                         }
929                 }
930         }
931
932         vstfx_free_info_list (finfos);
933         return discovered > 0 ? 0 : -1;
934 }
935
936 #endif // WINDOWS_VST_SUPPORT
937
938 #ifdef LXVST_SUPPORT
939
940 void
941 PluginManager::lxvst_refresh (bool cache_only)
942 {
943         if (_lxvst_plugin_info) {
944                 _lxvst_plugin_info->clear ();
945         } else {
946                 _lxvst_plugin_info = new ARDOUR::PluginInfoList();
947         }
948
949         lxvst_discover_from_path (Config->get_plugin_path_lxvst(), cache_only);
950 }
951
952 static bool lxvst_filter (const string& str, void *)
953 {
954         /* Not a dotfile, has a prefix before a period, suffix is "so" */
955
956         return str[0] != '.' && (str.length() > 3 && str.find (".so") == (str.length() - 3));
957 }
958
959 int
960 PluginManager::lxvst_discover_from_path (string path, bool cache_only)
961 {
962         vector<string> plugin_objects;
963         vector<string>::iterator x;
964         int ret = 0;
965
966 #ifndef NDEBUG
967         (void) path;
968 #endif
969
970         DEBUG_TRACE (DEBUG::PluginManager, string_compose ("Discovering linuxVST plugins along %1\n", path));
971
972         find_files_matching_filter (plugin_objects, Config->get_plugin_path_lxvst(), lxvst_filter, 0, false, true, true);
973
974         for (x = plugin_objects.begin(); x != plugin_objects.end (); ++x) {
975                 ARDOUR::PluginScanMessage(_("LXVST"), *x, !cache_only && !cancelled());
976                 lxvst_discover (*x, cache_only || cancelled());
977         }
978
979         return ret;
980 }
981
982 int
983 PluginManager::lxvst_discover (string path, bool cache_only)
984 {
985         DEBUG_TRACE (DEBUG::PluginManager, string_compose ("checking apparent LXVST plugin at %1\n", path));
986
987         _cancel_timeout = false;
988         vector<VSTInfo*> * finfos = vstfx_get_info_lx (const_cast<char *> (path.c_str()),
989                         cache_only ? VST_SCAN_CACHE_ONLY : VST_SCAN_USE_APP);
990
991         if (finfos->empty()) {
992                 DEBUG_TRACE (DEBUG::PluginManager, string_compose ("Cannot get Linux VST information from '%1'\n", path));
993                 return -1;
994         }
995
996         uint32_t discovered = 0;
997         for (vector<VSTInfo *>::iterator x = finfos->begin(); x != finfos->end(); ++x) {
998                 VSTInfo* finfo = *x;
999                 char buf[32];
1000
1001                 if (!finfo->canProcessReplacing) {
1002                         warning << string_compose (_("linuxVST plugin %1 does not support processReplacing, and so cannot be used in %2 at this time"),
1003                                                          finfo->name, PROGRAM_NAME)
1004                                 << endl;
1005                         continue;
1006                 }
1007
1008                 PluginInfoPtr info(new LXVSTPluginInfo);
1009
1010                 if (!strcasecmp ("The Unnamed plugin", finfo->name)) {
1011                         info->name = PBD::basename_nosuffix (path);
1012                 } else {
1013                         info->name = finfo->name;
1014                 }
1015
1016
1017                 snprintf (buf, sizeof (buf), "%d", finfo->UniqueID);
1018                 info->unique_id = buf;
1019                 info->category = "linuxVSTs";
1020                 info->path = path;
1021                 info->creator = finfo->creator;
1022                 info->index = 0;
1023                 info->n_inputs.set_audio (finfo->numInputs);
1024                 info->n_outputs.set_audio (finfo->numOutputs);
1025                 info->n_inputs.set_midi ((finfo->wantMidi&1) ? 1 : 0);
1026                 info->n_outputs.set_midi ((finfo->wantMidi&2) ? 1 : 0);
1027                 info->type = ARDOUR::LXVST;
1028
1029                                         /* Make sure we don't find the same plugin in more than one place along
1030                          the LXVST_PATH We can't use a simple 'find' because the path is included
1031                          in the PluginInfo, and that is the one thing we can be sure MUST be
1032                          different if a duplicate instance is found.  So we just compare the type
1033                          and unique ID (which for some VSTs isn't actually unique...)
1034                 */
1035
1036                 // TODO: check dup-IDs with windowsVST, too
1037                 bool duplicate = false;
1038                 if (!_lxvst_plugin_info->empty()) {
1039                         for (PluginInfoList::iterator i =_lxvst_plugin_info->begin(); i != _lxvst_plugin_info->end(); ++i) {
1040                                 if ((info->type == (*i)->type)&&(info->unique_id == (*i)->unique_id)) {
1041                                         warning << "Ignoring duplicate Linux VST plugin " << info->name << "\n";
1042                                         duplicate = true;
1043                                         break;
1044                                 }
1045                         }
1046                 }
1047
1048                 if (!duplicate) {
1049                         _lxvst_plugin_info->push_back (info);
1050                         discovered++;
1051                 }
1052         }
1053
1054         vstfx_free_info_list (finfos);
1055         return discovered > 0 ? 0 : -1;
1056 }
1057
1058 #endif // LXVST_SUPPORT
1059
1060
1061 PluginManager::PluginStatusType
1062 PluginManager::get_status (const PluginInfoPtr& pi)
1063 {
1064         PluginStatus ps (pi->type, pi->unique_id);
1065         PluginStatusList::const_iterator i =  find (statuses.begin(), statuses.end(), ps);
1066         if (i ==  statuses.end() ) {
1067                 return Normal;
1068         } else {
1069                 return i->status;
1070         }
1071 }
1072
1073 void
1074 PluginManager::save_statuses ()
1075 {
1076         std::string path = Glib::build_filename (user_config_directory(), "plugin_statuses");
1077         stringstream ofs;
1078
1079         for (PluginStatusList::iterator i = statuses.begin(); i != statuses.end(); ++i) {
1080                 switch ((*i).type) {
1081                 case LADSPA:
1082                         ofs << "LADSPA";
1083                         break;
1084                 case AudioUnit:
1085                         ofs << "AudioUnit";
1086                         break;
1087                 case LV2:
1088                         ofs << "LV2";
1089                         break;
1090                 case Windows_VST:
1091                         ofs << "Windows-VST";
1092                         break;
1093                 case LXVST:
1094                         ofs << "LXVST";
1095                         break;
1096                 }
1097
1098                 ofs << ' ';
1099
1100                 switch ((*i).status) {
1101                 case Normal:
1102                         ofs << "Normal";
1103                         break;
1104                 case Favorite:
1105                         ofs << "Favorite";
1106                         break;
1107                 case Hidden:
1108                         ofs << "Hidden";
1109                         break;
1110                 }
1111
1112                 ofs << ' ';
1113                 ofs << (*i).unique_id;;
1114                 ofs << endl;
1115         }
1116         g_file_set_contents (path.c_str(), ofs.str().c_str(), -1, NULL);
1117 }
1118
1119 void
1120 PluginManager::load_statuses ()
1121 {
1122         std::string path = Glib::build_filename (user_config_directory(), "plugin_statuses");
1123         gchar *fbuf = NULL;
1124         if (!g_file_get_contents (path.c_str(), &fbuf, NULL, NULL))  {
1125                 return;
1126         }
1127         stringstream ifs (fbuf);
1128         g_free (fbuf);
1129
1130         std::string stype;
1131         std::string sstatus;
1132         std::string id;
1133         PluginType type;
1134         PluginStatusType status;
1135         char buf[1024];
1136
1137         while (ifs) {
1138
1139                 ifs >> stype;
1140                 if (!ifs) {
1141                         break;
1142
1143                 }
1144
1145                 ifs >> sstatus;
1146                 if (!ifs) {
1147                         break;
1148
1149                 }
1150
1151                 /* rest of the line is the plugin ID */
1152
1153                 ifs.getline (buf, sizeof (buf), '\n');
1154                 if (!ifs) {
1155                         break;
1156                 }
1157
1158                 if (sstatus == "Normal") {
1159                         status = Normal;
1160                 } else if (sstatus == "Favorite") {
1161                         status = Favorite;
1162                 } else if (sstatus == "Hidden") {
1163                         status = Hidden;
1164                 } else {
1165                         error << string_compose (_("unknown plugin status type \"%1\" - all entries ignored"), sstatus)
1166                                   << endmsg;
1167                         statuses.clear ();
1168                         break;
1169                 }
1170
1171                 if (stype == "LADSPA") {
1172                         type = LADSPA;
1173                 } else if (stype == "AudioUnit") {
1174                         type = AudioUnit;
1175                 } else if (stype == "LV2") {
1176                         type = LV2;
1177                 } else if (stype == "Windows-VST") {
1178                         type = Windows_VST;
1179                 } else if (stype == "LXVST") {
1180                         type = LXVST;
1181                 } else {
1182                         error << string_compose (_("unknown plugin type \"%1\" - ignored"), stype)
1183                               << endmsg;
1184                         continue;
1185                 }
1186
1187                 id = buf;
1188                 strip_whitespace_edges (id);
1189                 set_status (type, id, status);
1190         }
1191 }
1192
1193 void
1194 PluginManager::set_status (PluginType t, string id, PluginStatusType status)
1195 {
1196         PluginStatus ps (t, id, status);
1197         statuses.erase (ps);
1198
1199         if (status == Normal) {
1200                 return;
1201         }
1202
1203         statuses.insert (ps);
1204 }
1205
1206 ARDOUR::PluginInfoList&
1207 PluginManager::windows_vst_plugin_info ()
1208 {
1209 #ifdef WINDOWS_VST_SUPPORT
1210         if (!_windows_vst_plugin_info) {
1211                 windows_vst_refresh ();
1212         }
1213         return *_windows_vst_plugin_info;
1214 #else
1215         return _empty_plugin_info;
1216 #endif
1217 }
1218
1219 ARDOUR::PluginInfoList&
1220 PluginManager::lxvst_plugin_info ()
1221 {
1222 #ifdef LXVST_SUPPORT
1223         assert(_lxvst_plugin_info);
1224         return *_lxvst_plugin_info;
1225 #else
1226         return _empty_plugin_info;
1227 #endif
1228 }
1229
1230 ARDOUR::PluginInfoList&
1231 PluginManager::ladspa_plugin_info ()
1232 {
1233         assert(_ladspa_plugin_info);
1234         return *_ladspa_plugin_info;
1235 }
1236
1237 ARDOUR::PluginInfoList&
1238 PluginManager::lv2_plugin_info ()
1239 {
1240 #ifdef LV2_SUPPORT
1241         assert(_lv2_plugin_info);
1242         return *_lv2_plugin_info;
1243 #else
1244         return _empty_plugin_info;
1245 #endif
1246 }
1247
1248 ARDOUR::PluginInfoList&
1249 PluginManager::au_plugin_info ()
1250 {
1251 #ifdef AUDIOUNIT_SUPPORT
1252         if (_au_plugin_info) {
1253                 return *_au_plugin_info;
1254         }
1255 #endif
1256         return _empty_plugin_info;
1257 }