Remove Input-meter special-case
[ardour.git] / libs / ardour / vst_info_file.cc
index 61158d705afbfbca2c154f23be94058981a31c2d..b076c422270556c957924b60ec78bfda458daac0 100644 (file)
  *  e.g. its name, creator etc.
  */
 
-#include <iostream>
-#include <fstream>
 #include <cassert>
 
 #include <sys/types.h>
-#include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <errno.h>
 #include <string.h>
 
 #include <glib.h>
-#include <glib/gstdio.h>
+#include "pbd/gstdio_compat.h"
 #include <glibmm.h>
 
 #include "pbd/error.h"
+#include "pbd/compose.h"
 
 #ifndef VST_SCANNER_APP
 #include "ardour/plugin_manager.h" // scanner_bin_path
 
 #include "ardour/filesystem_paths.h"
 #include "ardour/linux_vst_support.h"
+#include "ardour/mac_vst_support.h"
 #include "ardour/plugin_types.h"
 #include "ardour/vst_info_file.h"
 
+#include "pbd/i18n.h"
 #include "sha1.c"
 
 #define MAX_STRING_LEN 256
@@ -75,6 +75,10 @@ vstfx_instantiate_and_get_info_fst (const char* dllpath, vector<VSTInfo*> *infos
 static bool vstfx_instantiate_and_get_info_lx (const char* dllpath, vector<VSTInfo*> *infos, int uniqueID);
 #endif
 
+#ifdef MACVST_SUPPORT
+static bool vstfx_instantiate_and_get_info_mac (const char* dllpath, vector<VSTInfo*> *infos, int uniqueID);
+#endif
+
 /* ID for shell plugins */
 static int vstfx_current_loading_id = 0;
 
@@ -106,13 +110,43 @@ vstfx_infofile_path (const char* dllpath)
 
 /* *** VST Blacklist *** */
 
+static void vstfx_read_blacklist (std::string &bl) {
+       FILE * blacklist_fd = NULL;
+       bl = "";
+
+       string fn = Glib::build_filename (ARDOUR::user_cache_directory (), VST_BLACKLIST);
+
+       if (!Glib::file_test (fn, Glib::FILE_TEST_EXISTS)) {
+               return;
+       }
+
+       if (! (blacklist_fd = g_fopen (fn.c_str (), "rb"))) {
+               return;
+       }
+
+       while (!feof (blacklist_fd)) {
+               char buf[1024];
+               size_t s = fread (buf, sizeof(char), 1024, blacklist_fd);
+               if (ferror (blacklist_fd)) {
+                       PBD::error << string_compose (_("error reading VST Blacklist file %1 (%2)"), fn, strerror (errno)) << endmsg;
+                       bl = "";
+                       break;
+               }
+               if (s == 0) {
+                       break;
+               }
+               bl.append (buf, s);
+       }
+       ::fclose (blacklist_fd);
+}
+
 /** mark plugin as blacklisted */
 static void vstfx_blacklist (const char *id)
 {
        string fn = Glib::build_filename (ARDOUR::user_cache_directory (), VST_BLACKLIST);
        FILE * blacklist_fd = NULL;
        if (! (blacklist_fd = g_fopen (fn.c_str (), "a"))) {
-               PBD::error << "Cannot append to VST blacklist for '"<< id <<"'\n";
+               PBD::error << string_compose (_("Cannot append to VST blacklist for '%1'"), id) << endmsg;
                return;
        }
        assert (NULL == strchr (id, '\n'));
@@ -126,15 +160,16 @@ static void vstfx_un_blacklist (const char *idcs)
        string id (idcs);
        string fn = Glib::build_filename (ARDOUR::user_cache_directory (), VST_BLACKLIST);
        if (!Glib::file_test (fn, Glib::FILE_TEST_EXISTS)) {
-               PBD::warning << "Expected VST Blacklist file does not exist.\n";
+               PBD::warning << _("Expected VST Blacklist file does not exist.") << endmsg;
                return;
        }
 
        std::string bl;
-       std::ifstream ifs (fn.c_str ());
-       bl.assign ((std::istreambuf_iterator<char> (ifs)), (std::istreambuf_iterator<char> ()));
+       vstfx_read_blacklist (bl);
+
        ::g_unlink (fn.c_str ());
 
+       assert (!Glib::file_test (fn, Glib::FILE_TEST_EXISTS));
        assert (id.find ("\n") == string::npos);
 
        id += "\n"; // add separator
@@ -148,7 +183,7 @@ static void vstfx_un_blacklist (const char *idcs)
 
        FILE * blacklist_fd = NULL;
        if (! (blacklist_fd = g_fopen (fn.c_str (), "w"))) {
-               PBD::error << "Cannot open VST blacklist.\n";
+               PBD::error << _("Cannot open VST blacklist.") << endmsg;;
                return;
        }
        fprintf (blacklist_fd, "%s", bl.c_str ());
@@ -164,9 +199,9 @@ static bool vst_is_blacklisted (const char *idcs)
        if (!Glib::file_test (fn, Glib::FILE_TEST_EXISTS)) {
                return false;
        }
+
        std::string bl;
-       std::ifstream ifs (fn.c_str ());
-       bl.assign ((std::istreambuf_iterator<char> (ifs)), (std::istreambuf_iterator<char> ()));
+       vstfx_read_blacklist (bl);
 
        assert (id.find ("\n") == string::npos);
 
@@ -270,6 +305,12 @@ vstfx_load_info_block (FILE* fp, VSTInfo *info)
                info->wantMidi = 1;
        }
 
+       // TODO read isInstrument -- effFlagsIsSynth
+       info->isInstrument = info->numInputs == 0 && info->numOutputs > 0 && 1 == (info->wantMidi & 1);
+       if (!strcmp (info->Category, "Synth")) {
+               info->isInstrument = true;
+       }
+
        if ((info->numParams) == 0) {
                info->ParamNames = NULL;
                info->ParamLabels = NULL;
@@ -351,6 +392,7 @@ vstfx_write_info_block (FILE* fp, VSTInfo *info)
        fprintf (fp, "%d\n", info->wantMidi);
        fprintf (fp, "%d\n", info->hasEditor);
        fprintf (fp, "%d\n", info->canProcessReplacing);
+       // TODO write isInstrument in a backwards compat way
 
        for (int i = 0; i < info->numParams; i++) {
                fprintf (fp, "%s\n", info->ParamNames[i]);
@@ -382,7 +424,7 @@ vstfx_write_info_file (FILE* fp, vector<VSTInfo *> *infos)
        } 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.
+               PBD::warning << _("VST object file contains no plugins.") << endmsg;
        }
 }
 
@@ -405,6 +447,8 @@ vstfx_infofile_for_read (const char* dllpath)
        if (
                        (slen <= 3 || g_ascii_strcasecmp (&dllpath[slen-3], ".so"))
                        &&
+                       (slen <= 4 || g_ascii_strcasecmp (&dllpath[slen-4], ".vst"))
+                       &&
                        (slen <= 4 || g_ascii_strcasecmp (&dllpath[slen-4], ".dll"))
           ) {
                return 0;
@@ -413,19 +457,19 @@ vstfx_infofile_for_read (const char* dllpath)
        string const path = vstfx_infofile_path (dllpath);
 
        if (Glib::file_test (path, Glib::FileTest (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_REGULAR))) {
-               struct stat dllstat;
-               struct stat fsistat;
+               GStatBuf dllstat;
+               GStatBuf fsistat;
 
-               if (stat (dllpath, &dllstat) == 0) {
-                       if (stat (path.c_str (), &fsistat) == 0) {
+               if (g_stat (dllpath, &dllstat) == 0) {
+                       if (g_stat (path.c_str (), &fsistat) == 0) {
                                if (dllstat.st_mtime <= fsistat.st_mtime) {
                                        /* plugin is older than info file */
                                        return g_fopen (path.c_str (), "rb");
                                }
                        }
                }
-               PBD::warning << "Ignored VST plugin which is newer than cache: " << dllpath << " (cache: " << path << " )" << endmsg;
-               PBD::into << "Re-Scan Plugins (Preferences > Plugis) to update the cache, also make sure your system-time is set correctly." << endmsg;
+               PBD::warning << string_compose (_("Ignored VST plugin which is newer than cache: '%1' (cache: '%2')"), dllpath, path) << endmsg;
+               PBD::info << _("Re-Scan Plugins (Preferences > Plugins) to update the cache, also make sure your system-time is set correctly.") << endmsg;
        }
        return NULL;
 }
@@ -440,15 +484,14 @@ vstfx_infofile_for_write (const char* dllpath)
        if (
                        (slen <= 3 || g_ascii_strcasecmp (&dllpath[slen-3], ".so"))
                        &&
+                       (slen <= 4 || g_ascii_strcasecmp (&dllpath[slen-4], ".vst"))
+                       &&
                        (slen <= 4 || g_ascii_strcasecmp (&dllpath[slen-4], ".dll"))
           ) {
                return NULL;
        }
 
        string const path = vstfx_infofile_path (dllpath);
-#ifndef NDEBUG
-       PBD::info << "Creating VST cache file " << path << endmsg;
-#endif
        return g_fopen (path.c_str (), "wb");
 }
 
@@ -465,7 +508,7 @@ vstfx_get_info_from_file (const char* dllpath, vector<VSTInfo*> *infos)
                rv = vstfx_load_info_file (infofile, infos);
                fclose (infofile);
                if (!rv) {
-                       PBD::warning << "Cannot get VST information form " << dllpath << ": info file load failed." << endmsg;
+                       PBD::warning << string_compose (_("Cannot get VST information for '%1': failed to load cache file."), dllpath) << endmsg;
                }
        }
        return rv;
@@ -485,7 +528,10 @@ bool vstfx_midi_input (VSTState* vstfx)
        if (vst_version >= 2) {
                /* should we send it VST events (i.e. MIDI) */
 
-               if ((plugin->flags & effFlagsIsSynth) || (plugin->dispatcher (plugin, effCanDo, 0, 0, const_cast<char*> ("receiveVstEvents"), 0.0f) > 0)) {
+               if ((plugin->flags & effFlagsIsSynth)
+                               || (plugin->dispatcher (plugin, effCanDo, 0, 0, const_cast<char*> ("receiveVstEvents"), 0.0f) > 0)
+                               || (plugin->dispatcher (plugin, effCanDo, 0, 0, const_cast<char*> ("receiveVstMidiEvents"), 0.0f) > 0)
+                               ) {
                        return true;
                }
        }
@@ -603,10 +649,10 @@ vstfx_parse_vst_state (VSTState* vstfx)
        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 kPlugCategSynth:          info->Category = strdup ("Instrument"); break;
+               case kPlugCategAnalysis:       info->Category = strdup ("Analyser"); break;
                case kPlugCategMastering:      info->Category = strdup ("Mastering"); break;
-               case kPlugCategSpacializer:    info->Category = strdup ("Spacializer"); break;
+               case kPlugCategSpacializer:    info->Category = strdup ("Spatial"); break;
                case kPlugCategRoomFx:         info->Category = strdup ("RoomFx"); break;
                case kPlugSurroundFx:          info->Category = strdup ("SurroundFx"); break;
                case kPlugCategRestoration:    info->Category = strdup ("Restoration"); break;
@@ -623,13 +669,21 @@ vstfx_parse_vst_state (VSTState* vstfx)
        info->numParams = plugin->numParams;
        info->wantMidi = (vstfx_midi_input (vstfx) ? 1 : 0) | (vstfx_midi_output (vstfx) ? 2 : 0);
        info->hasEditor = plugin->flags & effFlagsHasEditor ? true : false;
+       info->isInstrument = (plugin->flags & effFlagsIsSynth) ? 1 : 0;
        info->canProcessReplacing = plugin->flags & effFlagsCanReplacing ? true : false;
        info->ParamNames = (char **) malloc (sizeof (char*)*info->numParams);
        info->ParamLabels = (char **) malloc (sizeof (char*)*info->numParams);
 
+#ifdef __APPLE__
+       if (info->hasEditor) {
+               /* we only support Cocoa UIs (just like Reaper) */
+               info->hasEditor = (plugin->dispatcher (plugin, effCanDo, 0, 0, const_cast<char*> ("hasCockosViewAsConfig"), 0.0f) & 0xffff0000) == 0xbeef0000;
+       }
+#endif
+
        for (int i = 0; i < info->numParams; ++i) {
-               char name[64];
-               char label[64];
+               char name[VestigeMaxLabelLen];
+               char label[VestigeMaxLabelLen];
 
                /* Not all plugins give parameters labels as well as names */
 
@@ -686,6 +740,11 @@ vstfx_info_from_plugin (const char *dllpath, VSTState* vstfx, vector<VSTInfo *>
                        case ARDOUR::LXVST:
                                vstfx_close (vstfx);
                                break;
+#endif
+#ifdef MACVST_SUPPORT
+                       case ARDOUR::MacVST:
+                               mac_vst_close (vstfx);
+                               break;
 #endif
                        default:
                                assert (0);
@@ -708,6 +767,11 @@ vstfx_info_from_plugin (const char *dllpath, VSTState* vstfx, vector<VSTInfo *>
                                case ARDOUR::LXVST:
                                        ok = vstfx_instantiate_and_get_info_lx (dllpath, infos, id);
                                        break;
+#endif
+#ifdef MACVST_SUPPORT
+                               case ARDOUR::MacVST:
+                                       ok = vstfx_instantiate_and_get_info_mac (dllpath, infos, id);
+                                       break;
 #endif
                                default:
                                        ok = false;
@@ -739,6 +803,11 @@ vstfx_info_from_plugin (const char *dllpath, VSTState* vstfx, vector<VSTInfo *>
                        case ARDOUR::LXVST:
                                vstfx_close (vstfx);
                                break;
+#endif
+#ifdef MACVST_SUPPORT
+                       case ARDOUR::MacVST:
+                               mac_vst_close (vstfx);
+                               break;
 #endif
                        default:
                                assert (0);
@@ -760,7 +829,7 @@ vstfx_instantiate_and_get_info_lx (
        VSTHandle* h;
        VSTState* vstfx;
        if (!(h = vstfx_load (dllpath))) {
-               PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": load failed." << endmsg;
+               PBD::warning << string_compose (_("Cannot get LinuxVST information from '%1': load failed."), dllpath) << endmsg;
                return false;
        }
 
@@ -768,7 +837,7 @@ vstfx_instantiate_and_get_info_lx (
 
        if (!(vstfx = vstfx_instantiate (h, simple_master_callback, 0))) {
                vstfx_unload (h);
-               PBD::warning << "Cannot get LinuxVST information from " << dllpath << ": instantiation failed." << endmsg;
+               PBD::warning << string_compose (_("Cannot get LinuxVST information from '%1': instantiation failed."), dllpath) << endmsg;
                return false;
        }
 
@@ -789,7 +858,7 @@ vstfx_instantiate_and_get_info_fst (
        VSTHandle* h;
        VSTState* vstfx;
        if (!(h = fst_load (dllpath))) {
-               PBD::warning << "Cannot get Windows VST information from " << dllpath << ": load failed." << endmsg;
+               PBD::warning << string_compose (_("Cannot get Windows VST information from '%1': load failed."), dllpath) << endmsg;
                return false;
        }
 
@@ -798,7 +867,7 @@ vstfx_instantiate_and_get_info_fst (
        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;
+               PBD::warning << string_compose (_("Cannot get Windows VST information from '%1': instantiation failed."), dllpath) << endmsg;
                return false;
        }
        vstfx_current_loading_id = 0;
@@ -809,7 +878,35 @@ vstfx_instantiate_and_get_info_fst (
 }
 #endif
 
+#ifdef MACVST_SUPPORT
+static bool
+vstfx_instantiate_and_get_info_mac (
+               const char* dllpath, vector<VSTInfo*> *infos, int uniqueID)
+{
+       printf("vstfx_instantiate_and_get_info_mac %s\n", dllpath);
+       VSTHandle* h;
+       VSTState* vstfx;
+       if (!(h = mac_vst_load (dllpath))) {
+               PBD::warning << string_compose (_("Cannot get MacVST information from '%1': load failed."), dllpath) << endmsg;
+               return false;
+       }
 
+       vstfx_current_loading_id = uniqueID;
+
+       if (!(vstfx = mac_vst_instantiate (h, simple_master_callback, 0))) {
+               mac_vst_unload (h);
+               PBD::warning << string_compose (_("Cannot get MacVST information from '%1': instantiation failed."), dllpath) << endmsg;
+               return false;
+       }
+
+       vstfx_current_loading_id = 0;
+
+       vstfx_info_from_plugin (dllpath, vstfx, infos, ARDOUR::MacVST);
+
+       mac_vst_unload (h);
+       return true;
+}
+#endif
 
 /* *** ERROR LOGGING *** */
 #ifndef VST_SCANNER_APP
@@ -899,7 +996,7 @@ vstfx_get_info (const char* dllpath, enum ARDOUR::PluginType type, enum VSTScanM
                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;
+                       PBD::error << string_compose (_("Cannot launch VST scanner app '%1': %2"), scanner_bin_path, strerror (errno)) << endmsg;
                        close_error_log ();
                        return infos;
                } else {
@@ -955,6 +1052,11 @@ vstfx_get_info (const char* dllpath, enum ARDOUR::PluginType type, enum VSTScanM
                case ARDOUR::LXVST:
                        ok = vstfx_instantiate_and_get_info_lx (dllpath, infos, 0);
                        break;
+#endif
+#ifdef MACVST_SUPPORT
+               case ARDOUR::MacVST:
+                       ok = vstfx_instantiate_and_get_info_mac (dllpath, infos, 0);
+                       break;
 #endif
                default:
                        ok = false;
@@ -971,7 +1073,7 @@ vstfx_get_info (const char* dllpath, enum ARDOUR::PluginType type, enum VSTScanM
        /* crate cache/whitelist */
        infofile = vstfx_infofile_for_write (dllpath);
        if (!infofile) {
-               PBD::warning << "Cannot cache VST information for " << dllpath << ": cannot create new FST info file." << endmsg;
+               PBD::warning << string_compose (_("Cannot cache VST information for '%1': cannot create cache file."), dllpath) << endmsg;
                return infos;
        } else {
                vstfx_write_info_file (infofile, infos);
@@ -1000,6 +1102,14 @@ vstfx_get_info_lx (char* dllpath, enum VSTScanMode mode)
 }
 #endif
 
+#ifdef MACVST_SUPPORT
+vector<VSTInfo *> *
+vstfx_get_info_mac (char* dllpath, enum VSTScanMode mode)
+{
+       return vstfx_get_info (dllpath, ARDOUR::MacVST, mode);
+}
+#endif
+
 #ifdef WINDOWS_VST_SUPPORT
 vector<VSTInfo *> *
 vstfx_get_info_fst (char* dllpath, enum VSTScanMode mode)