annotate plugin/PluginPathSetter.cpp @ 1473:f52bf66b9096 plugin-path-config

Provide means of getting the initial environment values
author Chris Cannam
date Mon, 04 Jun 2018 15:43:27 +0100
parents 5d7057af0c68
children 5ac102155409
rev   line source
Chris@1472 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1472 2
Chris@1472 3 /*
Chris@1472 4 Sonic Visualiser
Chris@1472 5 An audio file viewer and annotation editor.
Chris@1472 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1472 7
Chris@1472 8 This program is free software; you can redistribute it and/or
Chris@1472 9 modify it under the terms of the GNU General Public License as
Chris@1472 10 published by the Free Software Foundation; either version 2 of the
Chris@1472 11 License, or (at your option) any later version. See the file
Chris@1472 12 COPYING included with this distribution for more information.
Chris@1472 13 */
Chris@1472 14
Chris@1472 15 #include "PluginPathSetter.h"
Chris@1472 16
Chris@1472 17 #include <vamp-hostsdk/PluginHostAdapter.h>
Chris@1472 18
Chris@1472 19 #include "RealTimePluginFactory.h"
Chris@1472 20 #include "LADSPAPluginFactory.h"
Chris@1472 21 #include "DSSIPluginFactory.h"
Chris@1472 22
Chris@1472 23 #include <QSettings>
Chris@1472 24 #include <QMutexLocker>
Chris@1472 25
Chris@1472 26 QMutex
Chris@1472 27 PluginPathSetter::m_mutex;
Chris@1472 28
Chris@1472 29 PluginPathSetter::Paths
Chris@1472 30 PluginPathSetter::m_defaultPaths;
Chris@1472 31
Chris@1472 32 PluginPathSetter::Paths
Chris@1473 33 PluginPathSetter::m_environmentPaths;
Chris@1473 34
Chris@1473 35 std::map<QString, QString>
Chris@1473 36 PluginPathSetter::m_originalEnvValues;
Chris@1473 37
Chris@1473 38 PluginPathSetter::Paths
Chris@1473 39 PluginPathSetter::getEnvironmentPathsUncached()
Chris@1472 40 {
Chris@1472 41 Paths paths;
Chris@1472 42
Chris@1472 43 auto vampPath = Vamp::PluginHostAdapter::getPluginPath();
Chris@1472 44
Chris@1472 45 QStringList qVampPath;
Chris@1472 46 for (auto s: vampPath) {
Chris@1472 47 qVampPath.push_back(QString::fromStdString(s));
Chris@1472 48 }
Chris@1472 49 paths["Vamp"] = { qVampPath, "VAMP_PATH", true };
Chris@1472 50
Chris@1472 51 auto dssiPath = DSSIPluginFactory::getPluginPath();
Chris@1472 52
Chris@1472 53 QStringList qDssiPath;
Chris@1472 54 for (auto s: dssiPath) {
Chris@1472 55 qDssiPath.push_back(s);
Chris@1472 56 }
Chris@1472 57 paths["DSSI"] = { qDssiPath, "DSSI_PATH", true };
Chris@1472 58
Chris@1472 59 auto ladspaPath = LADSPAPluginFactory::getPluginPath();
Chris@1472 60
Chris@1472 61 QStringList qLadspaPath;
Chris@1472 62 for (auto s: ladspaPath) {
Chris@1472 63 qLadspaPath.push_back(s);
Chris@1472 64 }
Chris@1472 65 paths["LADSPA"] = { qLadspaPath, "LADSPA_PATH", true };
Chris@1472 66
Chris@1473 67 return paths;
Chris@1473 68 }
Chris@1473 69
Chris@1473 70 PluginPathSetter::Paths
Chris@1473 71 PluginPathSetter::getDefaultPaths()
Chris@1473 72 {
Chris@1473 73 QMutexLocker locker(&m_mutex);
Chris@1473 74
Chris@1473 75 if (!m_defaultPaths.empty()) {
Chris@1473 76 return m_defaultPaths;
Chris@1473 77 }
Chris@1473 78
Chris@1473 79 QString savedPathVamp = qEnvironmentVariable("VAMP_PATH");
Chris@1473 80 QString savedPathDssi = qEnvironmentVariable("DSSI_PATH");
Chris@1473 81 QString savedPathLadspa = qEnvironmentVariable("LADSPA_PATH");
Chris@1473 82
Chris@1473 83 qunsetenv("VAMP_PATH");
Chris@1473 84 qunsetenv("DSSI_PATH");
Chris@1473 85 qunsetenv("LADSPA_PATH");
Chris@1473 86
Chris@1473 87 Paths paths = getEnvironmentPathsUncached();
Chris@1473 88
Chris@1473 89 qputenv("VAMP_PATH", savedPathVamp.toUtf8());
Chris@1473 90 qputenv("DSSI_PATH", savedPathDssi.toUtf8());
Chris@1473 91 qputenv("LADSPA_PATH", savedPathLadspa.toUtf8());
Chris@1473 92
Chris@1472 93 m_defaultPaths = paths;
Chris@1472 94 return m_defaultPaths;
Chris@1472 95 }
Chris@1472 96
Chris@1472 97 PluginPathSetter::Paths
Chris@1473 98 PluginPathSetter::getEnvironmentPaths()
Chris@1473 99 {
Chris@1473 100 QMutexLocker locker(&m_mutex);
Chris@1473 101
Chris@1473 102 if (!m_environmentPaths.empty()) {
Chris@1473 103 return m_environmentPaths;
Chris@1473 104 }
Chris@1473 105
Chris@1473 106 m_environmentPaths = getEnvironmentPathsUncached();
Chris@1473 107 return m_environmentPaths;
Chris@1473 108 }
Chris@1473 109
Chris@1473 110 PluginPathSetter::Paths
Chris@1472 111 PluginPathSetter::getPaths()
Chris@1472 112 {
Chris@1473 113 Paths paths = getEnvironmentPaths();
Chris@1472 114
Chris@1472 115 QSettings settings;
Chris@1472 116 settings.beginGroup("Plugins");
Chris@1472 117
Chris@1472 118 for (auto p: paths) {
Chris@1472 119
Chris@1472 120 QString tag = p.first;
Chris@1472 121
Chris@1472 122 QStringList directories =
Chris@1472 123 settings.value(QString("directories-%1").arg(tag),
Chris@1472 124 p.second.directories)
Chris@1472 125 .toStringList();
Chris@1472 126 QString envVariable =
Chris@1472 127 settings.value(QString("env-variable-%1").arg(tag),
Chris@1472 128 p.second.envVariable)
Chris@1472 129 .toString();
Chris@1472 130 bool useEnvVariable =
Chris@1472 131 settings.value(QString("use-env-variable-%1").arg(tag),
Chris@1472 132 p.second.useEnvVariable)
Chris@1472 133 .toBool();
Chris@1472 134 std::string envVarStr = envVariable.toStdString();
Chris@1472 135
Chris@1472 136 QString currentValue = qEnvironmentVariable(envVarStr.c_str());
Chris@1472 137 if (currentValue != QString() && useEnvVariable) {
Chris@1472 138 directories = currentValue.split(
Chris@1472 139 #ifdef Q_OS_WIN
Chris@1472 140 ";"
Chris@1472 141 #else
Chris@1472 142 ":"
Chris@1472 143 #endif
Chris@1472 144 );
Chris@1472 145 }
Chris@1472 146
Chris@1472 147 paths[tag] = { directories, envVariable, useEnvVariable };
Chris@1472 148 }
Chris@1472 149
Chris@1472 150 settings.endGroup();
Chris@1472 151
Chris@1472 152 return paths;
Chris@1472 153 }
Chris@1472 154
Chris@1472 155 void
Chris@1472 156 PluginPathSetter::savePathSettings(Paths paths)
Chris@1472 157 {
Chris@1472 158 QSettings settings;
Chris@1472 159 settings.beginGroup("Plugins");
Chris@1472 160
Chris@1472 161 for (auto p: paths) {
Chris@1472 162 QString tag = p.first;
Chris@1472 163 settings.setValue(QString("directories-%1").arg(tag),
Chris@1472 164 p.second.directories);
Chris@1472 165 settings.setValue(QString("env-variable-%1").arg(tag),
Chris@1472 166 p.second.envVariable);
Chris@1472 167 settings.setValue(QString("use-env-variable-%1").arg(tag),
Chris@1472 168 p.second.useEnvVariable);
Chris@1472 169 }
Chris@1472 170
Chris@1472 171 settings.endGroup();
Chris@1472 172 }
Chris@1472 173
Chris@1473 174 QString
Chris@1473 175 PluginPathSetter::getOriginalEnvironmentValue(QString envVariable)
Chris@1473 176 {
Chris@1473 177 if (m_originalEnvValues.find(envVariable) != m_originalEnvValues.end()) {
Chris@1473 178 return m_originalEnvValues.at(envVariable);
Chris@1473 179 } else {
Chris@1473 180 return QString();
Chris@1473 181 }
Chris@1473 182 }
Chris@1473 183
Chris@1472 184 void
Chris@1473 185 PluginPathSetter::initialiseEnvironmentVariables()
Chris@1472 186 {
Chris@1472 187 // Set the relevant environment variables from user configuration,
Chris@1472 188 // so that later lookups through the standard APIs will follow the
Chris@1472 189 // same paths as we have in the user config
Chris@1472 190
Chris@1472 191 // First ensure the default paths have been recorded for later, so
Chris@1472 192 // we don't erroneously re-read them from the environment
Chris@1472 193 // variables we've just set
Chris@1472 194 (void)getDefaultPaths();
Chris@1473 195 (void)getEnvironmentPaths();
Chris@1472 196
Chris@1472 197 Paths paths = getPaths();
Chris@1472 198
Chris@1472 199 for (auto p: paths) {
Chris@1472 200 QString envVariable = p.second.envVariable;
Chris@1472 201 std::string envVarStr = envVariable.toStdString();
Chris@1472 202 QString currentValue = qEnvironmentVariable(envVarStr.c_str());
Chris@1473 203 m_originalEnvValues[envVariable] = currentValue;
Chris@1472 204 if (currentValue != QString() && p.second.useEnvVariable) {
Chris@1472 205 // don't override
Chris@1472 206 continue;
Chris@1472 207 }
Chris@1472 208 QString separator =
Chris@1472 209 #ifdef Q_OS_WIN
Chris@1472 210 ";"
Chris@1472 211 #else
Chris@1472 212 ":"
Chris@1472 213 #endif
Chris@1472 214 ;
Chris@1472 215 QString proposedValue = p.second.directories.join(separator);
Chris@1472 216 qputenv(envVarStr.c_str(), proposedValue.toUtf8());
Chris@1472 217 }
Chris@1472 218 }
Chris@1472 219