diff src/knownplugins.cpp @ 37:3ccc384c0161

Merge from branch plugin-path-config
author Chris Cannam
date Mon, 11 Jun 2018 17:46:50 +0100
parents ae74d39e9e4e
children
line wrap: on
line diff
--- a/src/knownplugins.cpp	Sun Mar 05 17:15:22 2017 +0000
+++ b/src/knownplugins.cpp	Mon Jun 11 17:46:50 2018 +0100
@@ -1,6 +1,6 @@
 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
 /*
-  Copyright (c) 2016 Queen Mary, University of London
+  Copyright (c) 2016-2018 Queen Mary, University of London
 
   Permission is hereby granted, free of charge, to any person
   obtaining a copy of this software and associated documentation
@@ -29,61 +29,128 @@
 
 #include "knownplugins.h"
 
-#include <sstream>
+#include <iostream>
 
 using namespace std;
 
 #if defined(_WIN32)
+#include <windows.h>
 #define PATH_SEPARATOR ';'
 #else
 #define PATH_SEPARATOR ':'
 #endif
 
-KnownPlugins::KnownPlugins(string helperExecutableName,
-                           PluginCandidates::LogCallback *cb) :
-    m_candidates(helperExecutableName),
-    m_helperExecutableName(helperExecutableName)
+static bool
+getEnvUtf8(std::string variable, std::string &value)
 {
-    m_candidates.setLogCallback(cb);
+    value = "";
     
-    m_known = {
-        {
-            VampPlugin,
-            {
-                "vamp",
-                expandConventionalPath(VampPlugin, "VAMP_PATH"),
-                "vampGetPluginDescriptor"
-            },
-        }, {
-            LADSPAPlugin,
-            {
-                "ladspa",
-                expandConventionalPath(LADSPAPlugin, "LADSPA_PATH"),
-                "ladspa_descriptor"
-            },
-        }, {
-            DSSIPlugin,
-            {
-                "dssi",
-                expandConventionalPath(DSSIPlugin, "DSSI_PATH"),
-                "dssi_descriptor"
-            }
-        }
+#ifdef _WIN32
+    int wvarlen = MultiByteToWideChar(CP_UTF8, 0,
+                                      variable.c_str(), int(variable.length()),
+                                      0, 0);
+    if (wvarlen < 0) {
+        cerr << "WARNING: Unable to convert environment variable name "
+             << variable << " to wide characters" << endl;
+        return false;
+    }
+    
+    wchar_t *wvarbuf = new wchar_t[wvarlen + 1];
+    (void)MultiByteToWideChar(CP_UTF8, 0,
+                              variable.c_str(), int(variable.length()),
+                              wvarbuf, wvarlen);
+    wvarbuf[wvarlen] = L'\0';
+    
+    wchar_t *wvalue = _wgetenv(wvarbuf);
+
+    delete[] wvarbuf;
+
+    if (!wvalue) {
+        return false;
+    }
+
+    int wvallen = int(wcslen(wvalue));
+    int vallen = WideCharToMultiByte(CP_UTF8, 0,
+                                     wvalue, wvallen,
+                                     0, 0, 0, 0);
+    if (vallen < 0) {
+        cerr << "WARNING: Unable to convert environment value to UTF-8" << endl;
+        return false;
+    }
+
+    char *val = new char[vallen + 1];
+    (void)WideCharToMultiByte(CP_UTF8, 0,
+                              wvalue, wvallen,
+                              val, vallen, 0, 0);
+    val[vallen] = '\0';
+
+    value = val;
+
+    delete[] val;
+    return true;
+
+#else
+
+    char *val = getenv(variable.c_str());
+    if (!val) {
+        return false;
+    }
+
+    value = val;
+    return true;
+    
+#endif
+}
+
+KnownPlugins::KnownPlugins(BinaryFormat format) :
+    m_format(format)
+{
+    string variableSuffix = "";
+    if (m_format == FormatNonNative32Bit) {
+        variableSuffix = "_32";
+    }
+    
+    m_known[VampPlugin] = {
+        "vamp",
+        "VAMP_PATH" + variableSuffix,
+        {}, {},
+        "vampGetPluginDescriptor"
+    };
+    
+    m_known[LADSPAPlugin] = {
+        "ladspa",
+        "LADSPA_PATH" + variableSuffix,
+        {}, {},
+        "ladspa_descriptor"
     };
 
-    for (const auto &k: m_known) {
-        m_candidates.scan(k.second.tag, k.second.path, k.second.descriptor);
+    m_known[DSSIPlugin] = {
+        "dssi",
+        "DSSI_PATH" + variableSuffix,
+        {}, {},
+        "dssi_descriptor"
+    };
+
+    for (auto &k: m_known) {
+        k.second.defaultPath = expandPathString(getDefaultPathString(k.first));
+        k.second.path = expandConventionalPath(k.first, k.second.variable);
     }
 }
 
-bool
-KnownPlugins::is32bit() const
+vector<KnownPlugins::PluginType>
+KnownPlugins::getKnownPluginTypes() const
 {
-    return m_helperExecutableName.find("-32") != std::string::npos;
+    vector<PluginType> kt;
+
+    for (const auto &k: m_known) {
+        kt.push_back(k.first);
+    }
+
+    return kt;
 }
 
 string
-KnownPlugins::getDefaultPath(PluginType type)
+KnownPlugins::getUnexpandedDefaultPathString(PluginType type)
 {
     switch (type) {
 
@@ -119,57 +186,51 @@
     throw logic_error("unknown or unhandled plugin type");
 }
 
+string
+KnownPlugins::getDefaultPathString(PluginType type)
+{
+    string path = getUnexpandedDefaultPathString(type);
+
+    if (path == "") {
+        return path;
+    }
+
+    string home;
+    if (getEnvUtf8("HOME", home)) {
+        string::size_type f;
+        while ((f = path.find("$HOME")) != string::npos &&
+               f < path.length()) {
+            path.replace(f, 5, home);
+        }
+    }
+
+#ifdef _WIN32
+    string pfiles, pfiles32;
+    if (!getEnvUtf8("ProgramFiles", pfiles)) {
+        pfiles = "C:\\Program Files";
+    }
+    if (!getEnvUtf8("ProgramFiles(x86)", pfiles32)) {
+        pfiles32 = "C:\\Program Files (x86)";
+    }
+    
+    string::size_type f;
+    while ((f = path.find("%ProgramFiles%")) != string::npos &&
+           f < path.length()) {
+        if (m_format == FormatNonNative32Bit) {
+            path.replace(f, 14, pfiles32);
+        } else {
+            path.replace(f, 14, pfiles);
+        }
+    }
+#endif
+
+    return path;
+}
+
 vector<string>
-KnownPlugins::expandConventionalPath(PluginType type, string var)
+KnownPlugins::expandPathString(string path)
 {
     vector<string> pathList;
-    string path;
-
-    char *cpath = getenv(var.c_str());
-    if (cpath) path = cpath;
-
-#ifdef _WIN32
-    bool is32 = is32bit();
-#endif
-
-    if (path == "") {
-
-        path = getDefaultPath(type);
-
-        if (path != "") {
-
-            char *home = getenv("HOME");
-            if (home) {
-                string::size_type f;
-                while ((f = path.find("$HOME")) != string::npos &&
-                       f < path.length()) {
-                    path.replace(f, 5, home);
-                }
-            }
-
-#ifdef _WIN32
-            const char *pfiles = 0;
-            if (is32) {
-                pfiles = getenv("ProgramFiles(x86)");
-            }
-            if (!pfiles) {
-                pfiles = getenv("ProgramFiles");
-            }
-            if (!pfiles) {
-                if (is32) {
-                    pfiles = "C:\\Program Files (x86)";
-                } else {
-                    pfiles = "C:\\Program Files";
-                }
-            }
-            string::size_type f;
-            while ((f = path.find("%ProgramFiles%")) != string::npos &&
-                   f < path.length()) {
-                path.replace(f, 14, pfiles);
-            }
-#endif
-        }
-    }
 
     string::size_type index = 0, newindex = 0;
 
@@ -183,41 +244,14 @@
     return pathList;
 }
 
-string
-KnownPlugins::getFailureReport() const
+vector<string>
+KnownPlugins::expandConventionalPath(PluginType type, string var)
 {
-    vector<PluginCandidates::FailureRec> failures;
+    string path;
 
-    for (auto t: getKnownPluginTypes()) {
-        auto ff = m_candidates.getFailedLibrariesFor(getTagFor(t));
-        failures.insert(failures.end(), ff.begin(), ff.end());
+    if (!getEnvUtf8(var, path)) {
+        path = getDefaultPathString(type);
     }
 
-    if (failures.empty()) return "";
-
-    int n = int(failures.size());
-    int i = 0;
-
-    ostringstream os;
-    
-    os << "<ul>";
-    for (auto f: failures) {
-        os << "<li>" + f.library;
-        if (f.message != "") {
-            os << "<br><i>" + f.message + "</i>";
-        } else {
-            os << "<br><i>unknown error</i>";
-        }
-        os << "</li>";
-
-        if (n > 10) {
-            if (++i == 5) {
-                os << "<li>(... and " << (n - i) << " further failures)</li>";
-                break;
-            }
-        }
-    }
-    os << "</ul>";
-
-    return os.str();
+    return expandPathString(path);
 }