Chris@1472: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@1472: Chris@1472: /* Chris@1472: Sonic Visualiser Chris@1472: An audio file viewer and annotation editor. Chris@1472: Centre for Digital Music, Queen Mary, University of London. Chris@1472: Chris@1472: This program is free software; you can redistribute it and/or Chris@1472: modify it under the terms of the GNU General Public License as Chris@1472: published by the Free Software Foundation; either version 2 of the Chris@1472: License, or (at your option) any later version. See the file Chris@1472: COPYING included with this distribution for more information. Chris@1472: */ Chris@1472: Chris@1472: #include "PluginPathSetter.h" Chris@1472: Chris@1472: #include Chris@1472: Chris@1472: #include "RealTimePluginFactory.h" Chris@1472: #include "LADSPAPluginFactory.h" Chris@1472: #include "DSSIPluginFactory.h" Chris@1472: Chris@1472: #include Chris@1472: #include Chris@1472: Chris@1480: #include "system/System.h" Chris@1481: #include "base/Preferences.h" Chris@1481: #include "base/HelperExecPath.h" Chris@1480: Chris@1472: QMutex Chris@1472: PluginPathSetter::m_mutex; Chris@1472: Chris@1472: PluginPathSetter::Paths Chris@1472: PluginPathSetter::m_defaultPaths; Chris@1472: Chris@1472: PluginPathSetter::Paths Chris@1473: PluginPathSetter::m_environmentPaths; Chris@1473: Chris@1473: std::map Chris@1473: PluginPathSetter::m_originalEnvValues; Chris@1473: Chris@1481: PluginPathSetter::TypeKeys Chris@1481: PluginPathSetter::m_supportedKeys; Chris@1480: Chris@1481: using namespace std; Chris@1481: Chris@1481: PluginPathSetter::TypeKeys Chris@1481: PluginPathSetter::getSupportedKeys() Chris@1481: { Chris@1481: QMutexLocker locker(&m_mutex); Chris@1481: Chris@1481: if (!m_supportedKeys.empty()) { Chris@1481: return m_supportedKeys; Chris@1481: } Chris@1481: Chris@1481: TypeKeys keys; Chris@1481: keys.push_back({ KnownPlugins::VampPlugin, KnownPlugins::FormatNative }); Chris@1481: Chris@1481: bool inProcess = Preferences::getInstance()->getRunPluginsInProcess(); Chris@1481: HelperExecPath hep(inProcess ? Chris@1481: HelperExecPath::NativeArchitectureOnly : Chris@1481: HelperExecPath::AllInstalled); Chris@1481: auto execs = hep.getHelperExecutables("vamp-plugin-load-checker"); Chris@1481: if (execs.size() > 1) { Chris@1481: keys.push_back({ Chris@1481: KnownPlugins::VampPlugin, KnownPlugins::FormatNonNative32Bit }); Chris@1481: } Chris@1481: Chris@1481: keys.push_back({ KnownPlugins::LADSPAPlugin, KnownPlugins::FormatNative }); Chris@1481: keys.push_back({ KnownPlugins::DSSIPlugin, KnownPlugins::FormatNative }); Chris@1481: Chris@1481: m_supportedKeys = keys; Chris@1481: return keys; Chris@1481: } Chris@1481: Chris@1481: // call with mutex held please Chris@1473: PluginPathSetter::Paths Chris@1481: PluginPathSetter::getEnvironmentPathsUncached(const TypeKeys &keys) Chris@1472: { Chris@1472: Paths paths; Chris@1472: Chris@1481: for (auto k: keys) { Chris@1472: Chris@1481: KnownPlugins kp(k.second); Chris@1481: Chris@1481: auto path = kp.getPathFor(k.first); Chris@1481: QStringList qPath; Chris@1481: for (auto s: path) { Chris@1481: qPath.push_back(QString::fromStdString(s)); Chris@1481: } Chris@1481: Chris@1481: auto var = kp.getPathEnvironmentVariableFor(k.first); Chris@1481: QString qVar = QString::fromStdString(var); Chris@1481: Chris@1481: paths[k] = { qPath, qVar, true }; Chris@1472: } Chris@1472: Chris@1473: return paths; Chris@1473: } Chris@1473: Chris@1473: PluginPathSetter::Paths Chris@1473: PluginPathSetter::getDefaultPaths() Chris@1473: { Chris@1481: TypeKeys keys = getSupportedKeys(); Chris@1481: Chris@1473: QMutexLocker locker(&m_mutex); Chris@1473: Chris@1481: Paths paths; Chris@1481: Chris@1481: for (auto k: keys) { Chris@1481: Chris@1481: KnownPlugins kp(k.second); Chris@1481: Chris@1481: auto path = kp.getDefaultPathFor(k.first); Chris@1481: QStringList qPath; Chris@1481: for (auto s: path) { Chris@1481: qPath.push_back(QString::fromStdString(s)); Chris@1481: } Chris@1481: Chris@1481: auto var = kp.getPathEnvironmentVariableFor(k.first); Chris@1481: QString qVar = QString::fromStdString(var); Chris@1481: Chris@1481: paths[k] = { qPath, qVar, true }; Chris@1473: } Chris@1473: Chris@1481: return paths; Chris@1472: } Chris@1472: Chris@1472: PluginPathSetter::Paths Chris@1473: PluginPathSetter::getEnvironmentPaths() Chris@1473: { Chris@1481: TypeKeys keys = getSupportedKeys(); Chris@1481: Chris@1473: QMutexLocker locker(&m_mutex); Chris@1473: Chris@1473: if (!m_environmentPaths.empty()) { Chris@1473: return m_environmentPaths; Chris@1473: } Chris@1473: Chris@1481: m_environmentPaths = getEnvironmentPathsUncached(keys); Chris@1473: return m_environmentPaths; Chris@1473: } Chris@1473: Chris@1481: QString Chris@1481: PluginPathSetter::getSettingTagFor(TypeKey tk) Chris@1481: { Chris@1481: string tag = KnownPlugins(tk.second).getTagFor(tk.first); Chris@1481: if (tk.second == KnownPlugins::FormatNonNative32Bit) { Chris@1481: tag += "-32"; Chris@1481: } Chris@1481: return QString::fromStdString(tag); Chris@1481: } Chris@1481: Chris@1473: PluginPathSetter::Paths Chris@1472: PluginPathSetter::getPaths() Chris@1472: { Chris@1473: Paths paths = getEnvironmentPaths(); Chris@1472: Chris@1472: QSettings settings; Chris@1472: settings.beginGroup("Plugins"); Chris@1472: Chris@1472: for (auto p: paths) { Chris@1472: Chris@1481: TypeKey tk = p.first; Chris@1481: Chris@1481: QString settingTag = getSettingTagFor(tk); Chris@1472: Chris@1472: QStringList directories = Chris@1481: settings.value(QString("directories-%1").arg(settingTag), Chris@1472: p.second.directories) Chris@1472: .toStringList(); Chris@1472: QString envVariable = Chris@1481: settings.value(QString("env-variable-%1").arg(settingTag), Chris@1472: p.second.envVariable) Chris@1472: .toString(); Chris@1472: bool useEnvVariable = Chris@1481: settings.value(QString("use-env-variable-%1").arg(settingTag), Chris@1472: p.second.useEnvVariable) Chris@1472: .toBool(); Chris@1480: Chris@1480: string envVarStr = envVariable.toStdString(); Chris@1480: string currentValue; Chris@1480: (void)getEnvUtf8(envVarStr, currentValue); Chris@1480: Chris@1480: if (currentValue != "" && useEnvVariable) { Chris@1480: directories = QString::fromStdString(currentValue).split( Chris@1472: #ifdef Q_OS_WIN Chris@1472: ";" Chris@1472: #else Chris@1472: ":" Chris@1472: #endif Chris@1472: ); Chris@1472: } Chris@1472: Chris@1481: paths[tk] = { directories, envVariable, useEnvVariable }; Chris@1472: } Chris@1472: Chris@1472: settings.endGroup(); Chris@1472: Chris@1472: return paths; Chris@1472: } Chris@1472: Chris@1472: void Chris@1472: PluginPathSetter::savePathSettings(Paths paths) Chris@1472: { Chris@1472: QSettings settings; Chris@1472: settings.beginGroup("Plugins"); Chris@1472: Chris@1472: for (auto p: paths) { Chris@1481: QString settingTag = getSettingTagFor(p.first); Chris@1481: settings.setValue(QString("directories-%1").arg(settingTag), Chris@1472: p.second.directories); Chris@1481: settings.setValue(QString("env-variable-%1").arg(settingTag), Chris@1472: p.second.envVariable); Chris@1481: settings.setValue(QString("use-env-variable-%1").arg(settingTag), Chris@1472: p.second.useEnvVariable); Chris@1472: } Chris@1472: Chris@1472: settings.endGroup(); Chris@1472: } Chris@1472: Chris@1473: QString Chris@1473: PluginPathSetter::getOriginalEnvironmentValue(QString envVariable) Chris@1473: { Chris@1473: if (m_originalEnvValues.find(envVariable) != m_originalEnvValues.end()) { Chris@1473: return m_originalEnvValues.at(envVariable); Chris@1473: } else { Chris@1473: return QString(); Chris@1473: } Chris@1473: } Chris@1473: Chris@1472: void Chris@1473: PluginPathSetter::initialiseEnvironmentVariables() Chris@1472: { Chris@1472: // Set the relevant environment variables from user configuration, Chris@1472: // so that later lookups through the standard APIs will follow the Chris@1472: // same paths as we have in the user config Chris@1472: Chris@1472: // First ensure the default paths have been recorded for later, so Chris@1472: // we don't erroneously re-read them from the environment Chris@1472: // variables we've just set Chris@1472: (void)getDefaultPaths(); Chris@1473: (void)getEnvironmentPaths(); Chris@1472: Chris@1472: Paths paths = getPaths(); Chris@1472: Chris@1472: for (auto p: paths) { Chris@1472: QString envVariable = p.second.envVariable; Chris@1480: string envVarStr = envVariable.toStdString(); Chris@1481: string currentValue; Chris@1481: getEnvUtf8(envVarStr, currentValue); Chris@1481: m_originalEnvValues[envVariable] = QString::fromStdString(currentValue); Chris@1481: if (currentValue != "" && p.second.useEnvVariable) { Chris@1472: // don't override Chris@1482: SVDEBUG << "PluginPathSetter: for environment variable " Chris@1482: << envVariable << ", useEnvVariable setting is false; " Chris@1482: << "leaving current value alone: it is \"" Chris@1482: << currentValue << "\"" << endl; Chris@1472: continue; Chris@1472: } Chris@1472: QString separator = Chris@1472: #ifdef Q_OS_WIN Chris@1472: ";" Chris@1472: #else Chris@1472: ":" Chris@1472: #endif Chris@1472: ; Chris@1472: QString proposedValue = p.second.directories.join(separator); Chris@1482: SVDEBUG << "PluginPathSetter: for environment variable " Chris@1482: << envVariable << ", useEnvVariable setting is true or " Chris@1482: << "variable is currently unset; " Chris@1482: << "changing value from \"" << currentValue Chris@1482: << "\" to setting preference of \"" << proposedValue Chris@1482: << "\"" << endl; Chris@1480: putEnvUtf8(envVarStr, proposedValue.toStdString()); Chris@1472: } Chris@1472: } Chris@1472: