VST? yes, we can do.
[ardour.git] / libs / ardour / vst_info_file.cc
index 7d5aaf3795fd7752a7643d4ff4ffa317bbffd1ed..07c1f565439c18cb69b29851492517a7c62b2a68 100644 (file)
 
 #include "pbd/error.h"
 
+#ifndef VST_SCANNER_APP
+#include "pbd/system_exec.h"
+#include "ardour/plugin_manager.h" // scanner_bin_path
+#endif
+
+#include "ardour/filesystem_paths.h"
 #include "ardour/linux_vst_support.h"
+#include "ardour/plugin_types.h"
 #include "ardour/vst_info_file.h"
 
 #define MAX_STRING_LEN 256
+#define PLUGIN_SCAN_TIMEOUT (600) // in deciseconds
+
+
+/* CACHE FILE PATHS */
+#define EXT_BLACKLIST ".fsb"
+#define EXT_ERRORFILE ".err"
+#define EXT_INFOFILE  ".fsi"
+
+#ifdef PLATFORM_WINDOWS
+#define PFX_DOTFILE   ""
+#else
+#define PFX_DOTFILE   "."
+#endif
+
 
 using namespace std;
+#ifndef VST_SCANNER_APP
+namespace ARDOUR {
+#endif
+
+/* prototypes */
+#ifdef WINDOWS_VST_SUPPORT
+#include <fst.h>
+static bool
+vstfx_instantiate_and_get_info_fst (const char* dllpath, vector<VSTInfo*> *infos, int uniqueID);
+#endif
+
+#ifdef LXVST_SUPPORT
+static bool vstfx_instantiate_and_get_info_lx (const char* dllpath, vector<VSTInfo*> *infos, int uniqueID);
+#endif
+
+/* ID for shell plugins */
+static int vstfx_current_loading_id = 0;
+
+
+
+/* *** CACHE FILE PATHS *** */
+
+static string
+vstfx_cache_file (const char* dllpath, int personal, const char *ext)
+{
+       string dir;
+       if (personal) {
+               dir = get_personal_vst_blacklist_dir();
+       } else {
+               dir = Glib::path_get_dirname (std::string(dllpath));
+       }
+
+       stringstream s;
+       s << PFX_DOTFILE << Glib::path_get_basename (dllpath) << ext;
+       return Glib::build_filename (dir, s.str ());
+}
+
+static string
+vstfx_blacklist_path (const char* dllpath, int personal)
+{
+       return vstfx_cache_file(dllpath, personal, EXT_BLACKLIST);
+}
+
+static string
+vstfx_infofile_path (const char* dllpath, int personal)
+{
+       return vstfx_cache_file(dllpath, personal, EXT_INFOFILE);
+}
+
+static string
+vstfx_errorfile_path (const char* dllpath, int personal)
+{
+       return vstfx_cache_file(dllpath, personal, EXT_ERRORFILE);
+}
 
+
+
+/* *** MEMORY MANAGEMENT *** */
+
+/** cleanup single allocated VSTInfo */
+static void
+vstfx_free_info (VSTInfo *info)
+{
+       for (int i = 0; i < info->numParams; i++) {
+               free (info->ParamNames[i]);
+               free (info->ParamLabels[i]);
+       }
+
+       free (info->name);
+       free (info->creator);
+       free (info->Category);
+       free (info->ParamNames);
+       free (info->ParamLabels);
+       free (info);
+}
+
+/** reset vector */
+static void
+vstfx_clear_info_list (vector<VSTInfo *> *infos)
+{
+       for (vector<VSTInfo *>::iterator i = infos->begin(); i != infos->end(); ++i) {
+               vstfx_free_info(*i);
+       }
+       infos->clear();
+}
+
+
+
+/* *** CACHE FILE I/O *** */
+
+/** Helper function to read a line from the cache file
+ * @return newly allocated string of NULL
+ */
 static char *
 read_string (FILE *fp)
 {
@@ -84,51 +197,87 @@ read_int (FILE* fp, int* n)
        return (sscanf (p, "%d", n) != 1);
 }
 
-static VSTInfo *
-load_vstfx_info_file (FILE* fp)
+/** parse a plugin-block from the cache info file */
+static bool
+vstfx_load_info_block(FILE* fp, VSTInfo *info)
 {
-       VSTInfo *info;
-
-       if ((info = (VSTInfo*) malloc (sizeof (VSTInfo))) == 0) {
-               return 0;
+       if ((info->name = read_string(fp)) == 0) return false;
+       if ((info->creator = read_string(fp)) == 0) return false;
+       if (read_int (fp, &info->UniqueID)) return false;
+       if ((info->Category = read_string(fp)) == 0) return false;
+       if (read_int (fp, &info->numInputs)) return false;
+       if (read_int (fp, &info->numOutputs)) return false;
+       if (read_int (fp, &info->numParams)) return false;
+       if (read_int (fp, &info->wantMidi)) return false;
+       if (read_int (fp, &info->hasEditor)) return false;
+       if (read_int (fp, &info->canProcessReplacing)) return false;
+
+       /* backwards compatibility with old .fsi files */
+       if (info->wantMidi == -1) {
+               info->wantMidi = 1;
        }
 
-       if ((info->name = read_string(fp)) == 0) goto error;
-       if ((info->creator = read_string(fp)) == 0) goto error;
-       if (read_int (fp, &info->UniqueID)) goto error;
-       if ((info->Category = read_string(fp)) == 0) goto error;
-       if (read_int (fp, &info->numInputs)) goto error;
-       if (read_int (fp, &info->numOutputs)) goto error;
-       if (read_int (fp, &info->numParams)) goto error;
-       if (read_int (fp, &info->wantMidi)) goto error;
-       if (read_int (fp, &info->hasEditor)) goto error;
-       if (read_int (fp, &info->canProcessReplacing)) goto error;
-
        if ((info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
-               goto error;
+               return false;
        }
 
        for (int i = 0; i < info->numParams; ++i) {
-               if ((info->ParamNames[i] = read_string(fp)) == 0) goto error;
+               if ((info->ParamNames[i] = read_string(fp)) == 0) return false;
        }
 
        if ((info->ParamLabels = (char **) malloc(sizeof(char*)*info->numParams)) == 0) {
-               goto error;
+               return false;
        }
 
        for (int i = 0; i < info->numParams; ++i) {
-               if ((info->ParamLabels[i] = read_string(fp)) == 0) goto error;
+               if ((info->ParamLabels[i] = read_string(fp)) == 0) {
+                       return false;
+               }
        }
+       return true;
+}
 
-       return info;
-
-error:
-       free (info);
-       return 0;
+/** parse all blocks in a cache info file */
+static bool
+vstfx_load_info_file (FILE* fp, vector<VSTInfo*> *infos)
+{
+       VSTInfo *info;
+       if ((info = (VSTInfo*) calloc (1, sizeof (VSTInfo))) == 0) {
+               return false;
+       }
+       if (vstfx_load_info_block(fp, info)) {
+               if (strncmp (info->Category, "Shell", 5)) {
+                       infos->push_back(info);
+               } else {
+                       int plugin_cnt = 0;
+                       vstfx_free_info(info);
+                       if (read_int (fp, &plugin_cnt)) {
+                               for (int i = 0; i < plugin_cnt; i++) {
+                                       if ((info = (VSTInfo*) calloc (1, sizeof (VSTInfo))) == 0) {
+                                               vstfx_clear_info_list(infos);
+                                               return false;
+                                       }
+                                       if (vstfx_load_info_block(fp, info)) {
+                                               infos->push_back(info);
+                                       } else {
+                                               vstfx_free_info(info);
+                                               vstfx_clear_info_list(infos);
+                                               return false;
+                                       }
+                               }
+                       } else {
+                               return false; /* Bad file */
+                       }
+               }
+               return true;
+       }
+       vstfx_free_info(info);
+       vstfx_clear_info_list(infos);
+       return false;
 }
 
-static int
-save_vstfx_info_file (VSTInfo *info, FILE* fp)
+static void
+vstfx_write_info_block (FILE* fp, VSTInfo *info)
 {
        assert (info);
        assert (fp);
@@ -151,35 +300,117 @@ save_vstfx_info_file (VSTInfo *info, FILE* fp)
        for (int i = 0; i < info->numParams; i++) {
                fprintf (fp, "%s\n", info->ParamLabels[i]);
        }
+}
 
-       return 0;
+static void
+vstfx_write_info_file (FILE* fp, vector<VSTInfo *> *infos)
+{
+       assert(infos);
+       assert(fp);
+
+       if (infos->size() > 1) {
+               vector<VSTInfo *>::iterator x = infos->begin();
+               /* write out the shell info first along with count of the number of
+                * plugins contained in this shell
+                */
+               vstfx_write_info_block(fp, *x);
+               fprintf( fp, "%d\n", (int)infos->size() - 1 );
+               ++x;
+               /* Now write out the info for each plugin */
+               for (; x != infos->end(); ++x) {
+                       vstfx_write_info_block(fp, *x);
+               }
+       } else if (infos->size() == 1) {
+               vstfx_write_info_block(fp, infos->front());
+       } else {
+               PBD::error << "Zero plugins in VST." << endmsg; // XXX here? rather make this impossible before if it ain't already.
+       }
 }
 
-static string
-vstfx_infofile_path (char* dllpath, int personal)
+
+/* *** CACHE AND BLACKLIST MANAGEMENT *** */
+
+/* return true if plugin is blacklisted or has an invalid file extension */
+static bool
+vstfx_blacklist_stat (const char *dllpath, int personal)
 {
-       string dir;
-       if (personal) {
-               dir = Glib::build_filename (Glib::get_home_dir (), ".fst");
+       if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
+               return true;
+       }
+       string const path = vstfx_blacklist_path (dllpath, personal);
+
+       if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
+               struct stat dllstat;
+               struct stat fsbstat;
 
-               /* If the directory doesn't exist, try to create it */
-               if (!Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
-                       if (g_mkdir (dir.c_str (), 0700)) {
-                               return 0;
+               if (stat (dllpath, &dllstat) == 0 && stat (path.c_str(), &fsbstat) == 0) {
+                       if (dllstat.st_mtime > fsbstat.st_mtime) {
+                               /* plugin is newer than blacklist file */
+                               return true;
                        }
                }
+               /* stat failed or plugin is older than blacklist file */
+               return true;
+       }
+       /* blacklist file does not exist */
+       return false;
+}
 
-       } else {
-               dir = Glib::path_get_dirname (std::string(dllpath));
+/* return true if plugin is blacklisted, checks both personal
+ * and global folder */
+static bool
+vstfx_check_blacklist (const char *dllpath)
+{
+       if (vstfx_blacklist_stat(dllpath, 0)) return true;
+       if (vstfx_blacklist_stat(dllpath, 1)) return true;
+       return false;
+}
+
+/* create blacklist file, preferably in same folder as the
+ * plugin, fall back to personal folder in $HOME
+ */
+static FILE *
+vstfx_blacklist_file (const char *dllpath)
+{
+       FILE *f;
+       if ((f = fopen (vstfx_blacklist_path (dllpath, 0).c_str(), "w"))) {
+               return f;
        }
+       return fopen (vstfx_blacklist_path (dllpath, 1).c_str(), "w");
+}
 
-       stringstream s;
-       s << "." << Glib::path_get_basename (dllpath) << ".fsi";
-       return Glib::build_filename (dir, s.str ());
+/** mark plugin as blacklisted */
+static bool
+vstfx_blacklist (const char *dllpath)
+{
+       FILE *f = vstfx_blacklist_file(dllpath);
+       if (f) {
+               fclose(f);
+               return true;
+       }
+       return false;
 }
 
+/** mark plugin as not blacklisted */
+static void
+vstfx_un_blacklist (const char *dllpath)
+{
+       ::g_unlink(vstfx_blacklist_path (dllpath, 0).c_str());
+       ::g_unlink(vstfx_blacklist_path (dllpath, 1).c_str());
+}
+
+/** remove info file from cache */
+static void
+vstfx_remove_infofile (const char *dllpath)
+{
+       ::g_unlink(vstfx_infofile_path (dllpath, 0).c_str());
+       ::g_unlink(vstfx_infofile_path (dllpath, 1).c_str());
+}
+
+/** helper function, check if cache is newer than plugin
+ * @return path to cache file */
 static char *
-vstfx_infofile_stat (char *dllpath, struct stat* statbuf, int personal)
+vstfx_infofile_stat (const char *dllpath, struct stat* statbuf, int personal)
 {
        if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
                return 0;
@@ -189,11 +420,6 @@ vstfx_infofile_stat (char *dllpath, struct stat* statbuf, int personal)
 
        if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
 
-               /* info file exists in same location as the shared object, so
-                  check if its current and up to date
-               */
-
-
                struct stat dllstat;
 
                if (stat (dllpath, &dllstat) == 0) {
@@ -209,9 +435,10 @@ vstfx_infofile_stat (char *dllpath, struct stat* statbuf, int personal)
        return 0;
 }
 
-
+/** cache file for given plugin
+ * @return FILE of the .fsi cache if found and up-to-date*/
 static FILE *
-vstfx_infofile_for_read (char* dllpath)
+vstfx_infofile_for_read (const char* dllpath)
 {
        struct stat own_statbuf;
        struct stat sys_statbuf;
@@ -238,8 +465,11 @@ vstfx_infofile_for_read (char* dllpath)
        return rv;
 }
 
+/** helper function for \ref vstfx_infofile_for_write
+ * abstract global and personal cache folders
+ */
 static FILE *
-vstfx_infofile_create (char* dllpath, int personal)
+vstfx_infofile_create (const char* dllpath, int personal)
 {
        if (strstr (dllpath, ".so" ) == 0 && strstr(dllpath, ".dll") == 0) {
                return 0;
@@ -249,8 +479,11 @@ vstfx_infofile_create (char* dllpath, int personal)
        return fopen (path.c_str(), "w");
 }
 
+/** newly created cache file for given plugin
+ * @return FILE for the .fsi cache, NULL if neither personal,
+ * nor global cache folder is writable */
 static FILE *
-vstfx_infofile_for_write (char* dllpath)
+vstfx_infofile_for_write (const char* dllpath)
 {
        FILE* f;
 
@@ -261,8 +494,31 @@ vstfx_infofile_for_write (char* dllpath)
        return f;
 }
 
+/** check if cache-file exists, is up-to-date and parse cache file
+ * @param infos [return] loaded plugin info
+ * @return true if .fsi cache was read successfully, false otherwise
+ */
+static bool
+vstfx_get_info_from_file(const char* dllpath, vector<VSTInfo*> *infos)
+{
+       FILE* infofile;
+       bool rv = false;
+       if ((infofile = vstfx_infofile_for_read (dllpath)) != 0) {
+               rv = vstfx_load_info_file(infofile, infos);
+               fclose (infofile);
+               if (!rv) {
+                       PBD::warning << "Cannot get VST information form " << dllpath << ": info file load failed." << endmsg;
+               }
+       }
+       return rv;
+}
+
+
+
+/* *** VST system-under-test methods *** */
+
 static
-int vstfx_can_midi (VSTState* vstfx)
+bool vstfx_midi_input (VSTState* vstfx)
 {
        AEffect* plugin = vstfx->plugin;
 
@@ -272,15 +528,75 @@ int vstfx_can_midi (VSTState* vstfx)
                /* should we send it VST events (i.e. MIDI) */
 
                if ((plugin->flags & effFlagsIsSynth) || (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "receiveVstEvents", 0.0f) > 0)) {
-                       return -1;
+                       return true;
+               }
+       }
+
+       return false;
+}
+
+static
+bool vstfx_midi_output (VSTState* vstfx)
+{
+       AEffect* plugin = vstfx->plugin;
+
+       int const vst_version = plugin->dispatcher (plugin, effGetVstVersion, 0, 0, 0, 0.0f);
+
+       if (vst_version >= 2) {
+               /* should we send it VST events (i.e. MIDI) */
+
+               if (   (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "sendVstEvents", 0.0f) > 0)
+                               || (plugin->dispatcher (plugin, effCanDo, 0, 0,(void*) "sendVstMidiEvent", 0.0f) > 0)
+                        ) {
+                       return true;
                }
        }
 
        return false;
 }
 
-static VSTInfo *
-vstfx_info_from_plugin (VSTState* vstfx)
+/** simple 'dummy' audiomaster callback to instantiate the plugin
+ * and query information
+ */
+static intptr_t
+simple_master_callback (AEffect *, int32_t opcode, int32_t, intptr_t, void *ptr, float)
+{
+       const char* vstfx_can_do_strings[] = {
+               "supplyIdle",
+               "sendVstTimeInfo",
+               "sendVstEvents",
+               "sendVstMidiEvent",
+               "receiveVstEvents",
+               "receiveVstMidiEvent",
+               "supportShell",
+               "shellCategory",
+               "shellCategorycurID"
+       };
+       const int vstfx_can_do_string_count = 9;
+
+       if (opcode == audioMasterVersion) {
+               return 2400;
+       }
+       else if (opcode == audioMasterCanDo) {
+               for (int i = 0; i < vstfx_can_do_string_count; i++) {
+                       if (! strcmp(vstfx_can_do_strings[i], (const char*)ptr)) {
+                               return 1;
+                       }
+               }
+               return 0;
+       }
+       else if (opcode == audioMasterCurrentId) {
+               return vstfx_current_loading_id;
+       }
+       else {
+               return 0;
+       }
+}
+
+
+/** main plugin query and test function */
+static VSTInfo*
+vstfx_parse_vst_state (VSTState* vstfx)
 {
        assert (vstfx);
 
@@ -313,13 +629,29 @@ vstfx_info_from_plugin (VSTState* vstfx)
                info->creator = strdup (creator);
        }
 
+
+       switch (plugin->dispatcher (plugin, effGetPlugCategory, 0, 0, 0, 0))
+       {
+               case kPlugCategEffect:         info->Category = strdup ("Effect"); break;
+               case kPlugCategSynth:          info->Category = strdup ("Synth"); break;
+               case kPlugCategAnalysis:       info->Category = strdup ("Anaylsis"); break;
+               case kPlugCategMastering:      info->Category = strdup ("Mastering"); break;
+               case kPlugCategSpacializer:    info->Category = strdup ("Spacializer"); break;
+               case kPlugCategRoomFx:         info->Category = strdup ("RoomFx"); break;
+               case kPlugSurroundFx:          info->Category = strdup ("SurroundFx"); break;
+               case kPlugCategRestoration:    info->Category = strdup ("Restoration"); break;
+               case kPlugCategOfflineProcess: info->Category = strdup ("Offline"); break;
+               case kPlugCategShell:          info->Category = strdup ("Shell"); break;
+               case kPlugCategGenerator:      info->Category = strdup ("Generator"); break;
+               default:                       info->Category = strdup ("Unknown"); break;
+       }
+
        info->UniqueID = plugin->uniqueID;
 
-       info->Category = strdup("None"); /* XXX */
        info->numInputs = plugin->numInputs;
        info->numOutputs = plugin->numOutputs;
        info->numParams = plugin->numParams;
-       info->wantMidi = vstfx_can_midi(vstfx);
+       info->wantMidi = (vstfx_midi_input(vstfx) ? 1 : 0) | (vstfx_midi_output(vstfx) ? 2 : 0);
        info->hasEditor = plugin->flags & effFlagsHasEditor ? true : false;
        info->canProcessReplacing = plugin->flags & effFlagsCanReplacing ? true : false;
        info->ParamNames = (char **) malloc(sizeof(char*)*info->numParams);
@@ -344,149 +676,362 @@ vstfx_info_from_plugin (VSTState* vstfx)
        return info;
 }
 
-/* A simple 'dummy' audiomaster callback which should be ok,
-   we will only be instantiating the plugin in order to get its info
-*/
-
-static intptr_t
-simple_master_callback (AEffect *, int32_t opcode, int32_t, intptr_t, void *, float)
+/** wrapper around \ref vstfx_parse_vst_state,
+ * iterate over plugins in shell, translate VST-info into ardour VSTState
+ */
+static void
+vstfx_info_from_plugin (const char *dllpath, VSTState* vstfx, vector<VSTInfo *> *infos, enum ARDOUR::PluginType type)
 {
-       if (opcode == audioMasterVersion) {
-               return 2;
-       } else {
-               return 0;
+       assert(vstfx);
+       VSTInfo *info;
+
+       if (!(info = vstfx_parse_vst_state(vstfx))) {
+               return;
        }
-}
 
-static VSTInfo *
-vstfx_get_info_from_file(char* dllpath, bool &found)
-{
-       FILE* infofile;
-       VSTInfo *info = 0;
-       found = false;
-       if ((infofile = vstfx_infofile_for_read (dllpath)) != 0) {
-               found = true;
-               info = load_vstfx_info_file (infofile);
-               fclose (infofile);
-               if (info == 0) {
-                       PBD::warning << "Cannot get LinuxVST information form " << dllpath << ": info file load failed." << endmsg;
+       infos->push_back(info);
+#if 1 // shell-plugin support
+       /* If this plugin is a Shell and we are not already inside a shell plugin
+        * read the info for all of the plugins contained in this shell.
+        */
+       if (!strncmp (info->Category, "Shell", 5)
+                       && vstfx->handle->plugincnt == 1) {
+               int id;
+               vector< pair<int, string> > ids;
+               AEffect *plugin = vstfx->plugin;
+               string path = vstfx->handle->path;
+
+               do {
+                       char name[65] = "Unknown\0";
+                       id = plugin->dispatcher (plugin, effShellGetNextPlugin, 0, 0, name, 0);
+                       ids.push_back(std::make_pair(id, name));
+               } while ( id != 0 );
+
+               switch(type) {
+#ifdef WINDOWS_VST_SUPPORT
+                       case ARDOUR::Windows_VST: fst_close(vstfx); break;
+#endif
+#ifdef LXVST_SUPPORT
+                       case ARDOUR::LXVST: vstfx_close (vstfx); break;
+#endif
+                       default: assert(0); break;
+               }
+
+               for (vector< pair<int, string> >::iterator x = ids.begin(); x != ids.end(); ++x) {
+                       id = (*x).first;
+                       if (id == 0) continue;
+                       /* recurse vstfx_get_info() */
+
+                       bool ok;
+                       switch (type) {
+#ifdef WINDOWS_VST_SUPPORT
+                               case ARDOUR::Windows_VST:  ok = vstfx_instantiate_and_get_info_fst(dllpath, infos, id); break;
+#endif
+#ifdef LXVST_SUPPORT
+                               case ARDOUR::LXVST:  ok = vstfx_instantiate_and_get_info_lx(dllpath, infos, id); break;
+#endif
+                               default: ok = false;
+                       }
+                       if (ok) {
+                               // One shell (some?, all?) does not report the actual plugin name
+                               // even after the shelled plugin has been instantiated.
+                               // Replace the name of the shell with the real name.
+                               info = infos->back();
+                               free (info->name);
+
+                               if ((*x).second.length() == 0) {
+                                       info->name = strdup("Unknown");
+                               }
+                               else {
+                                       info->name = strdup ((*x).second.c_str());
+                               }
+                       }
+               }
+       } else {
+               switch(type) {
+#ifdef WINDOWS_VST_SUPPORT
+                       case ARDOUR::Windows_VST: fst_close(vstfx); break;
+#endif
+#ifdef LXVST_SUPPORT
+                       case ARDOUR::LXVST: vstfx_close (vstfx); break;
+#endif
+                       default: assert(0); break;
                }
        }
-       return info;
+#endif
 }
 
-void
-vstfx_free_info (VSTInfo *info)
-{
-       for (int i = 0; i < info->numParams; i++) {
-               free (info->ParamNames[i]);
-               free (info->ParamLabels[i]);
-       }
 
-       free (info->name);
-       free (info->creator);
-       free (info->Category);
-       free (info->ParamNames);
-       free (info->ParamLabels);
-       free (info);
-}
 
-#ifdef LXVST_SUPPORT
-/** Try to get plugin info - first by looking for a .fsi cache of the
-    data, and if that doesn't exist, load the plugin, get its data and
-    then cache it for future ref
-*/
+/* *** TOP-LEVEL PLUGIN INSTANTIATION FUNCTIONS *** */
 
-VSTInfo *
-vstfx_get_info_lx (char* dllpath)
+#ifdef LXVST_SUPPORT
+static bool
+vstfx_instantiate_and_get_info_lx (
+               const char* dllpath, vector<VSTInfo*> *infos, int uniqueID)
 {
-       FILE* infofile;
        VSTHandle* h;
        VSTState* vstfx;
-
-       bool used_cache;
-       VSTInfo* info = vstfx_get_info_from_file(dllpath, used_cache);
-       if (used_cache) {
-               PBD::info << "using cache for LinuxVST plugin '" << dllpath << "'" << endmsg;
-               return info;
-       }
-
        if (!(h = vstfx_load(dllpath))) {
                PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": load failed." << endmsg;
-               return 0;
+               return false;
        }
 
+       vstfx_current_loading_id = uniqueID;
+
        if (!(vstfx = vstfx_instantiate(h, simple_master_callback, 0))) {
                vstfx_unload(h);
                PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": instantiation failed." << endmsg;
-               return 0;
+               return false;
        }
 
-       infofile = vstfx_infofile_for_write (dllpath);
+       vstfx_current_loading_id = 0;
 
-       if (!infofile) {
-               vstfx_close(vstfx);
-               vstfx_unload(h);
-               PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": cannot create new FST info file." << endmsg;
-               return 0;
+       vstfx_info_from_plugin(dllpath, vstfx, infos, ARDOUR::LXVST);
+
+       vstfx_unload (h);
+       return true;
+}
+#endif
+
+#ifdef WINDOWS_VST_SUPPORT
+static bool
+vstfx_instantiate_and_get_info_fst (
+               const char* dllpath, vector<VSTInfo*> *infos, int uniqueID)
+{
+       VSTHandle* h;
+       VSTState* vstfx;
+       if(!(h = fst_load(dllpath))) {
+               PBD::warning << "Cannot get Windows VST information from " << dllpath << ": load failed." << endmsg;
+               return false;
        }
 
-       info = vstfx_info_from_plugin (vstfx);
+       vstfx_current_loading_id = uniqueID;
 
-       save_vstfx_info_file (info, infofile);
-       fclose (infofile);
+       if(!(vstfx = fst_instantiate(h, simple_master_callback, 0))) {
+               fst_unload(&h);
+               vstfx_current_loading_id = 0;
+               PBD::warning << "Cannot get Windows VST information from " << dllpath << ": instantiation failed." << endmsg;
+               return false;
+       }
+       vstfx_current_loading_id = 0;
 
-       vstfx_close (vstfx);
-       vstfx_unload (h);
+       vstfx_info_from_plugin(dllpath, vstfx, infos, ARDOUR::Windows_VST);
 
-       return info;
+       return true;
 }
 #endif
 
-#ifdef WINDOWS_VST_SUPPORT
-#include <fst.h>
 
-VSTInfo *
-vstfx_get_info_fst (char* dllpath)
+
+/* *** ERROR LOGGING *** */
+#ifndef VST_SCANNER_APP
+
+static FILE * _errorlog_fd = 0;
+static char * _errorlog_dll = 0;
+
+static void parse_scanner_output (std::string msg, size_t /*len*/)
+{
+       if (!_errorlog_fd && !_errorlog_dll) {
+               PBD::error << "VST scanner: " << msg;
+               return;
+       }
+
+       if (!_errorlog_fd) {
+               if (!(_errorlog_fd = fopen(vstfx_errorfile_path(_errorlog_dll, 0).c_str(), "w"))) {
+                       if (!(_errorlog_fd = fopen(vstfx_errorfile_path(_errorlog_dll, 1).c_str(), "w"))) {
+                               PBD::error << "Cannot create plugin error-log for plugin " << _errorlog_dll;
+                               free(_errorlog_dll);
+                               _errorlog_dll = NULL;
+                       }
+               }
+       }
+
+       if (_errorlog_fd) {
+               fprintf (_errorlog_fd, "%s\n", msg.c_str());
+       } else {
+               PBD::error << "VST scanner: " << msg;
+       }
+}
+
+static void
+set_error_log (const char* dllpath) {
+       assert(!_errorlog_fd);
+       assert(!_errorlog_dll);
+       _errorlog_dll = strdup(dllpath);
+}
+
+static void
+close_error_log () {
+       if (_errorlog_fd) {
+               fclose(_errorlog_fd);
+               _errorlog_fd = 0;
+       }
+       free(_errorlog_dll);
+       _errorlog_dll = 0;
+}
+
+#endif
+
+
+/* *** THE MAIN FUNCTION THAT USES ALL OF THE ABOVE :) *** */
+
+static vector<VSTInfo *> *
+vstfx_get_info (const char* dllpath, enum ARDOUR::PluginType type, enum VSTScanMode mode)
 {
        FILE* infofile;
-       VSTHandle* h;
-       VSTState* vstfx;
+       vector<VSTInfo*> *infos = new vector<VSTInfo*>;
 
-       bool used_cache;
-       VSTInfo* info = vstfx_get_info_from_file(dllpath, used_cache);
-       if (used_cache) {
-               PBD::info << "using cache for VST plugin '" << dllpath << "'" << endmsg;
-               return info;
+       if (vstfx_check_blacklist(dllpath)) {
+               return infos;
        }
 
-       if(!(h = fst_load(dllpath))) {
-               PBD::warning << "Cannot get VST information from " << dllpath << ": load failed." << endmsg;
-               return 0;
+       if (vstfx_get_info_from_file(dllpath, infos)) {
+               return infos;
        }
 
-       if(!(vstfx = fst_instantiate(h, simple_master_callback, 0))) {
-               fst_unload(&h);
-               PBD::warning << "Cannot get VST information from " << dllpath << ": instantiation failed." << endmsg;
-               return 0;
+#ifndef VST_SCANNER_APP
+       std::string scanner_bin_path = ARDOUR::PluginManager::scanner_bin_path;
+
+       if (mode == VST_SCAN_CACHE_ONLY) {
+               /* never scan explicitly, use cache only */
+               return infos;
        }
+       else if (mode == VST_SCAN_USE_APP && scanner_bin_path != "") {
+               /* use external scanner app */
+
+               char **argp= (char**) calloc(3,sizeof(char*));
+               argp[0] = strdup(scanner_bin_path.c_str());
+               argp[1] = strdup(dllpath);
+               argp[2] = 0;
+
+               set_error_log(dllpath);
+               PBD::SystemExec scanner (scanner_bin_path, argp);
+               PBD::ScopedConnectionList cons;
+               scanner.ReadStdout.connect_same_thread (cons, boost::bind (&parse_scanner_output, _1 ,_2));
+               if (scanner.start (2 /* send stderr&stdout via signal */)) {
+                       PBD::error << "Cannot launch VST scanner app '" << scanner_bin_path << "': "<< strerror(errno) << endmsg;
+                       close_error_log();
+                       return infos;
+               } else {
+                       int timeout = PLUGIN_SCAN_TIMEOUT;
+                       while (scanner.is_running() && --timeout) {
+                               ARDOUR::GUIIdle();
+                               Glib::usleep (100000);
+
+                               if (ARDOUR::PluginManager::instance().cancelled()) {
+                                       // remove info file (might be incomplete)
+                                       vstfx_remove_infofile(dllpath);
+                                       // remove temporary blacklist file (scan incomplete)
+                                       vstfx_un_blacklist(dllpath);
+                                       scanner.terminate();
+                                       close_error_log();
+                                       return infos;
+                               }
+                       }
+                       scanner.terminate();
+               }
+               close_error_log();
+               /* re-read index (generated by external scanner) */
+               vstfx_clear_info_list(infos);
+               if (!vstfx_check_blacklist(dllpath)) {
+                       vstfx_get_info_from_file(dllpath, infos);
+               }
+               return infos;
+       }
+       /* else .. instantiate and check in in ardour process itself */
+#else
+       (void) mode; // unused parameter
+#endif
 
-       infofile = vstfx_infofile_for_write (dllpath);
+       bool ok;
+       /* blacklist in case instantiation fails */
+       vstfx_blacklist(dllpath);
+
+       switch (type) {
+#ifdef WINDOWS_VST_SUPPORT
+               case ARDOUR::Windows_VST:  ok = vstfx_instantiate_and_get_info_fst(dllpath, infos, 0); break;
+#endif
+#ifdef LXVST_SUPPORT
+               case ARDOUR::LXVST:  ok = vstfx_instantiate_and_get_info_lx(dllpath, infos, 0); break;
+#endif
+               default: ok = false;
+       }
 
+       if (!ok) {
+               return infos;
+       }
+
+       /* remove from blacklist */
+       vstfx_un_blacklist(dllpath);
+
+       /* crate cache/whitelist */
+       infofile = vstfx_infofile_for_write (dllpath);
        if (!infofile) {
-               fst_close(vstfx);
-               //fst_unload(&h); // XXX -> fst_close()
-               PBD::warning << "Cannot get VST information from " << dllpath << ": cannot create new FST info file." << endmsg;
-               return 0;
+               PBD::warning << "Cannot cache VST information for " << dllpath << ": cannot create new FST info file." << endmsg;
+               return infos;
+       } else {
+               vstfx_write_info_file (infofile, infos);
+               fclose (infofile);
        }
+       return infos;
+}
 
-       info = vstfx_info_from_plugin(vstfx);
-       save_vstfx_info_file (info, infofile);
-       fclose (infofile);
 
-       fst_close(vstfx);
-       //fst_unload(&h); // XXX -> fst_close()
 
-       return info;
+/* *** public API *** */
+
+void
+vstfx_free_info_list (vector<VSTInfo *> *infos)
+{
+       for (vector<VSTInfo *>::iterator i = infos->begin(); i != infos->end(); ++i) {
+               vstfx_free_info(*i);
+       }
+       delete infos;
+}
+
+string
+get_personal_vst_blacklist_dir() {
+       string dir = Glib::build_filename (ARDOUR::user_cache_directory(), "fst_blacklist");
+       /* if the directory doesn't exist, try to create it */
+       if (!Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
+               if (g_mkdir (dir.c_str (), 0700)) {
+                       PBD::error << "Cannot create VST blacklist folder '" << dir << "'" << endmsg;
+                       //exit(1);
+               }
+       }
+       return dir;
 }
+
+string
+get_personal_vst_info_cache_dir() {
+       string dir = Glib::build_filename (ARDOUR::user_cache_directory(), "fst_info");
+       /* if the directory doesn't exist, try to create it */
+       if (!Glib::file_test (dir, Glib::FILE_TEST_IS_DIR)) {
+               if (g_mkdir (dir.c_str (), 0700)) {
+                       PBD::error << "Cannot create VST info folder '" << dir << "'" << endmsg;
+                       //exit(1);
+               }
+       }
+       return dir;
+}
+
+#ifdef LXVST_SUPPORT
+vector<VSTInfo *> *
+vstfx_get_info_lx (char* dllpath, enum VSTScanMode mode)
+{
+       return vstfx_get_info(dllpath, ARDOUR::LXVST, mode);
+}
+#endif
+
+#ifdef WINDOWS_VST_SUPPORT
+vector<VSTInfo *> *
+vstfx_get_info_fst (char* dllpath, enum VSTScanMode mode)
+{
+       return vstfx_get_info(dllpath, ARDOUR::Windows_VST, mode);
+}
+#endif
+
+#ifndef VST_SCANNER_APP
+} // namespace
 #endif