1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
/**
bespoke synth, a software modular synthesizer
Copyright (C) 2021 Ryan Challinor (contact: awwbees@gmail.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
//
// VSTScanner.cpp
// Bespoke
//
// Created by Ryan Challinor on 10/16/21.
//
//
#include "VSTScanner.h"
#include "VSTPlugin.h"
#include "ModularSynth.h"
#include "juce_gui_basics/juce_gui_basics.h"
extern juce::ApplicationProperties& getAppProperties();
CustomPluginScanner::CustomPluginScanner()
{
if (auto* file = getAppProperties().getUserSettings())
file->addChangeListener(this);
changeListenerCallback(nullptr);
}
CustomPluginScanner::~CustomPluginScanner()
{
if (auto* file = getAppProperties().getUserSettings())
file->removeChangeListener(this);
}
bool CustomPluginScanner::findPluginTypesFor(juce::AudioPluginFormat& format,
juce::OwnedArray<juce::PluginDescription>& result,
const juce::String& fileOrIdentifier)
{
if (!scanWithExternalProcess)
{
superprocess = nullptr;
format.findAllTypesForFile(result, fileOrIdentifier);
return true;
}
if (superprocess == nullptr)
{
superprocess = std::make_unique<Superprocess>(*this);
std::unique_lock<std::mutex> lock(mutex);
connectionLost = false;
}
juce::MemoryBlock block;
juce::MemoryOutputStream stream{ block, true };
stream.writeString(format.getName());
stream.writeString(fileOrIdentifier);
if (superprocess->sendMessageToWorker(block))
{
std::unique_lock<std::mutex> lock(mutex);
gotResponse = false;
pluginDescription = nullptr;
for (;;)
{
if (condvar.wait_for(lock,
std::chrono::milliseconds(50),
[this]
{
return gotResponse || shouldExit();
}))
{
break;
}
}
if (shouldExit())
{
superprocess = nullptr;
return true;
}
if (connectionLost)
{
superprocess = nullptr;
return false;
}
if (pluginDescription != nullptr)
{
for (const auto* item : pluginDescription->getChildIterator())
{
auto desc = std::make_unique<juce::PluginDescription>();
if (desc->loadFromXml(*item))
result.add(std::move(desc));
}
}
return true;
}
superprocess = nullptr;
return false;
}
void CustomPluginScanner::changeListenerCallback(juce::ChangeBroadcaster*)
{
if (auto* file = getAppProperties().getUserSettings())
scanWithExternalProcess = (file->getIntValue(kScanModeKey) == 0);
}
CustomPluginScanner::Superprocess::Superprocess(CustomPluginScanner& o)
: owner(o)
{
launchWorkerProcess(juce::File::getSpecialLocation(juce::File::currentExecutableFile), kScanProcessUID, 0, 0);
}
void CustomPluginScanner::Superprocess::handleMessageFromWorker(const juce::MemoryBlock& mb)
{
auto xml = parseXML(mb.toString());
const std::lock_guard<std::mutex> lock(owner.mutex);
owner.pluginDescription = std::move(xml);
owner.gotResponse = true;
owner.condvar.notify_one();
}
void CustomPluginScanner::Superprocess::handleConnectionLost()
{
const std::lock_guard<std::mutex> lock(owner.mutex);
owner.pluginDescription = nullptr;
owner.gotResponse = true;
owner.connectionLost = true;
owner.condvar.notify_one();
}
/////////////////////////////////////////////////////////////////////////////////////////////////
CustomPluginListComponent::CustomPluginListComponent(juce::AudioPluginFormatManager& manager,
juce::KnownPluginList& listToRepresent,
const juce::File& pedal,
juce::PropertiesFile* props,
bool async)
: juce::PluginListComponent(manager, listToRepresent, pedal, props, async)
{
addAndMakeVisible(validationModeLabel);
addAndMakeVisible(validationModeBox);
validationModeLabel.attachToComponent(&validationModeBox, true);
validationModeLabel.setJustificationType(juce::Justification::right);
validationModeLabel.setSize(100, 30);
auto unusedId = 1;
for (const auto mode : { "Avoid crashes", "Within process" })
validationModeBox.addItem(mode, unusedId++);
validationModeBox.setSelectedItemIndex(getAppProperties().getUserSettings()->getIntValue(kScanModeKey));
validationModeBox.onChange = [this]
{
getAppProperties().getUserSettings()->setValue(kScanModeKey, validationModeBox.getSelectedItemIndex());
};
OnResize();
}
void CustomPluginListComponent::OnResize()
{
const auto& buttonBounds = getOptionsButton().getBounds();
validationModeBox.setBounds(buttonBounds.withWidth(130).withRightX(getWidth() - buttonBounds.getX()));
//validationModeBox.setBounds(juce::Rectangle(5,5,130,40));
}
/////////////////////////////////////////////////////////////////////////////////////////////////
PluginListWindow::PluginListWindow(juce::AudioPluginFormatManager& pluginFormatManager, WindowCloseListener* listener)
: juce::DocumentWindow("Plugin Manager",
juce::LookAndFeel::getDefaultLookAndFeel().findColour(juce::ResizableWindow::backgroundColourId),
juce::DocumentWindow::closeButton)
, mListener(listener)
{
auto deadMansPedalFile(juce::File(ofToDataPath("vst/deadmanspedal.txt")));
TheSynth->GetKnownPluginList().setCustomScanner(std::make_unique<CustomPluginScanner>());
mComponent = new CustomPluginListComponent(pluginFormatManager,
TheSynth->GetKnownPluginList(),
deadMansPedalFile,
getAppProperties().getUserSettings(), true);
setContentOwned(mComponent, true);
setResizable(true, false);
setResizeLimits(300, 400, 1500, 1500);
setSize(600, 600);
//setAlwaysOnTop(true);
if (const auto* dpy = juce::Desktop::getInstance().getDisplays().getDisplayForRect(TheSynth->GetMainComponent()->getScreenBounds()))
{
const auto& mainMon = dpy->userArea;
setTopLeftPosition(mainMon.getX() + mainMon.getWidth() / 4,
mainMon.getY() + mainMon.getHeight() / 4);
}
restoreWindowStateFromString(getAppProperties().getUserSettings()->getValue("listWindowPos"));
setVisible(true);
}
PluginListWindow::~PluginListWindow()
{
getAppProperties().getUserSettings()->setValue("listWindowPos", getWindowStateAsString());
clearContentComponent();
}
void PluginListWindow::resized()
{
juce::DocumentWindow::resized();
mComponent->OnResize();
}
void PluginListWindow::closeButtonPressed()
{
mListener->OnWindowClosed();
}
/////////////////////////////////////////////////////////////////////////////////////////////////
PluginScannerSubprocess::PluginScannerSubprocess()
{
addDefaultFormatsToManager(formatManager);
}
void PluginScannerSubprocess::handleMessageFromCoordinator(const juce::MemoryBlock& mb)
{
{
const std::lock_guard<std::mutex> lock(mutex);
pendingBlocks.emplace(mb);
}
triggerAsyncUpdate();
}
void PluginScannerSubprocess::handleConnectionLost()
{
juce::JUCEApplicationBase::quit();
}
// It's important to run the plugin scan on the main thread!
void PluginScannerSubprocess::handleAsyncUpdate()
{
for (;;)
{
const auto block = [&]() -> juce::MemoryBlock
{
const std::lock_guard<std::mutex> lock(mutex);
if (pendingBlocks.empty())
return {};
auto out = std::move(pendingBlocks.front());
pendingBlocks.pop();
return out;
}();
if (block.isEmpty())
return;
juce::MemoryInputStream stream{ block, false };
const auto formatName = stream.readString();
const auto identifier = stream.readString();
juce::OwnedArray<juce::PluginDescription> results;
for (auto* format : formatManager.getFormats())
if (format->getName() == formatName)
format->findAllTypesForFile(results, identifier);
juce::XmlElement xml("LIST");
for (const auto& desc : results)
xml.addChildElement(desc->createXml().release());
const auto str = xml.toString();
sendMessageToCoordinator({ str.toRawUTF8(), str.getNumBytesAsUTF8() });
}
}
|