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