The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

383 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #include <JuceHeader.h>
  19. #include "UI/MainHostWindow.h"
  20. #include "Plugins/InternalPlugins.h"
  21. #if ! (JUCE_PLUGINHOST_VST || JUCE_PLUGINHOST_VST3 || JUCE_PLUGINHOST_AU)
  22. #error "If you're building the audio plugin host, you probably want to enable VST and/or AU support"
  23. #endif
  24. class PluginScannerSubprocess final : private ChildProcessWorker,
  25. private AsyncUpdater
  26. {
  27. public:
  28. PluginScannerSubprocess()
  29. {
  30. formatManager.addDefaultFormats();
  31. }
  32. using ChildProcessWorker::initialiseFromCommandLine;
  33. private:
  34. void handleMessageFromCoordinator (const MemoryBlock& mb) override
  35. {
  36. if (mb.isEmpty())
  37. return;
  38. const std::lock_guard<std::mutex> lock (mutex);
  39. if (const auto results = doScan (mb); ! results.isEmpty())
  40. {
  41. sendResults (results);
  42. }
  43. else
  44. {
  45. pendingBlocks.emplace (mb);
  46. triggerAsyncUpdate();
  47. }
  48. }
  49. void handleConnectionLost() override
  50. {
  51. JUCEApplicationBase::quit();
  52. }
  53. void handleAsyncUpdate() override
  54. {
  55. for (;;)
  56. {
  57. const std::lock_guard<std::mutex> lock (mutex);
  58. if (pendingBlocks.empty())
  59. return;
  60. sendResults (doScan (pendingBlocks.front()));
  61. pendingBlocks.pop();
  62. }
  63. }
  64. OwnedArray<PluginDescription> doScan (const MemoryBlock& block)
  65. {
  66. MemoryInputStream stream { block, false };
  67. const auto formatName = stream.readString();
  68. const auto identifier = stream.readString();
  69. PluginDescription pd;
  70. pd.fileOrIdentifier = identifier;
  71. pd.uniqueId = pd.deprecatedUid = 0;
  72. const auto matchingFormat = [&]() -> AudioPluginFormat*
  73. {
  74. for (auto* format : formatManager.getFormats())
  75. if (format->getName() == formatName)
  76. return format;
  77. return nullptr;
  78. }();
  79. OwnedArray<PluginDescription> results;
  80. if (matchingFormat != nullptr
  81. && (MessageManager::getInstance()->isThisTheMessageThread()
  82. || matchingFormat->requiresUnblockedMessageThreadDuringCreation (pd)))
  83. {
  84. matchingFormat->findAllTypesForFile (results, identifier);
  85. }
  86. return results;
  87. }
  88. void sendResults (const OwnedArray<PluginDescription>& results)
  89. {
  90. XmlElement xml ("LIST");
  91. for (const auto& desc : results)
  92. xml.addChildElement (desc->createXml().release());
  93. const auto str = xml.toString();
  94. sendMessageToCoordinator ({ str.toRawUTF8(), str.getNumBytesAsUTF8() });
  95. }
  96. std::mutex mutex;
  97. std::queue<MemoryBlock> pendingBlocks;
  98. AudioPluginFormatManager formatManager;
  99. };
  100. //==============================================================================
  101. class PluginHostApp final : public JUCEApplication,
  102. private AsyncUpdater
  103. {
  104. public:
  105. PluginHostApp() = default;
  106. void initialise (const String& commandLine) override
  107. {
  108. auto scannerSubprocess = std::make_unique<PluginScannerSubprocess>();
  109. if (scannerSubprocess->initialiseFromCommandLine (commandLine, processUID))
  110. {
  111. storedScannerSubprocess = std::move (scannerSubprocess);
  112. return;
  113. }
  114. // initialise our settings file..
  115. PropertiesFile::Options options;
  116. options.applicationName = "Juce Audio Plugin Host";
  117. options.filenameSuffix = "settings";
  118. options.osxLibrarySubFolder = "Preferences";
  119. appProperties.reset (new ApplicationProperties());
  120. appProperties->setStorageParameters (options);
  121. mainWindow.reset (new MainHostWindow());
  122. commandManager.registerAllCommandsForTarget (this);
  123. commandManager.registerAllCommandsForTarget (mainWindow.get());
  124. mainWindow->menuItemsChanged();
  125. // Important note! We're going to use an async update here so that if we need
  126. // to re-open a file and instantiate some plugins, it will happen AFTER this
  127. // initialisation method has returned.
  128. // On Windows this probably won't make a difference, but on OSX there's a subtle event loop
  129. // issue that can happen if a plugin runs one of those irritating modal dialogs while it's
  130. // being loaded. If that happens inside this method, the OSX event loop seems to be in some
  131. // kind of special "initialisation" mode and things get confused. But if we load the plugin
  132. // later when the normal event loop is running, everything's fine.
  133. triggerAsyncUpdate();
  134. }
  135. void handleAsyncUpdate() override
  136. {
  137. File fileToOpen;
  138. #if JUCE_ANDROID || JUCE_IOS
  139. fileToOpen = PluginGraph::getDefaultGraphDocumentOnMobile();
  140. #else
  141. for (int i = 0; i < getCommandLineParameterArray().size(); ++i)
  142. {
  143. fileToOpen = File::getCurrentWorkingDirectory().getChildFile (getCommandLineParameterArray()[i]);
  144. if (fileToOpen.existsAsFile())
  145. break;
  146. }
  147. #endif
  148. if (! fileToOpen.existsAsFile())
  149. {
  150. RecentlyOpenedFilesList recentFiles;
  151. recentFiles.restoreFromString (getAppProperties().getUserSettings()->getValue ("recentFilterGraphFiles"));
  152. if (recentFiles.getNumFiles() > 0)
  153. fileToOpen = recentFiles.getFile (0);
  154. }
  155. if (fileToOpen.existsAsFile())
  156. if (auto* graph = mainWindow->graphHolder.get())
  157. if (auto* ioGraph = graph->graph.get())
  158. ioGraph->loadFrom (fileToOpen, true);
  159. }
  160. void shutdown() override
  161. {
  162. mainWindow = nullptr;
  163. appProperties = nullptr;
  164. LookAndFeel::setDefaultLookAndFeel (nullptr);
  165. }
  166. void suspended() override
  167. {
  168. #if JUCE_ANDROID || JUCE_IOS
  169. if (auto graph = mainWindow->graphHolder.get())
  170. if (auto ioGraph = graph->graph.get())
  171. ioGraph->saveDocument (PluginGraph::getDefaultGraphDocumentOnMobile());
  172. #endif
  173. }
  174. void systemRequestedQuit() override
  175. {
  176. if (mainWindow != nullptr)
  177. mainWindow->tryToQuitApplication();
  178. else
  179. JUCEApplicationBase::quit();
  180. }
  181. bool backButtonPressed() override
  182. {
  183. if (mainWindow->graphHolder != nullptr)
  184. mainWindow->graphHolder->hideLastSidePanel();
  185. return true;
  186. }
  187. const String getApplicationName() override { return "Juce Plug-In Host"; }
  188. const String getApplicationVersion() override { return ProjectInfo::versionString; }
  189. bool moreThanOneInstanceAllowed() override { return true; }
  190. ApplicationCommandManager commandManager;
  191. std::unique_ptr<ApplicationProperties> appProperties;
  192. private:
  193. std::unique_ptr<MainHostWindow> mainWindow;
  194. std::unique_ptr<PluginScannerSubprocess> storedScannerSubprocess;
  195. };
  196. static PluginHostApp& getApp() { return *dynamic_cast<PluginHostApp*> (JUCEApplication::getInstance()); }
  197. ApplicationProperties& getAppProperties() { return *getApp().appProperties; }
  198. ApplicationCommandManager& getCommandManager() { return getApp().commandManager; }
  199. bool isOnTouchDevice()
  200. {
  201. static bool isTouch = Desktop::getInstance().getMainMouseSource().isTouch();
  202. return isTouch;
  203. }
  204. //==============================================================================
  205. static AutoScale autoScaleFromString (StringRef str)
  206. {
  207. if (str.isEmpty()) return AutoScale::useDefault;
  208. if (str == CharPointer_ASCII { "0" }) return AutoScale::scaled;
  209. if (str == CharPointer_ASCII { "1" }) return AutoScale::unscaled;
  210. jassertfalse;
  211. return AutoScale::useDefault;
  212. }
  213. static const char* autoScaleToString (AutoScale autoScale)
  214. {
  215. if (autoScale == AutoScale::scaled) return "0";
  216. if (autoScale == AutoScale::unscaled) return "1";
  217. return {};
  218. }
  219. AutoScale getAutoScaleValueForPlugin (const String& identifier)
  220. {
  221. if (identifier.isNotEmpty())
  222. {
  223. auto plugins = StringArray::fromLines (getAppProperties().getUserSettings()->getValue ("autoScalePlugins"));
  224. plugins.removeEmptyStrings();
  225. for (auto& plugin : plugins)
  226. {
  227. auto fromIdentifier = plugin.fromFirstOccurrenceOf (identifier, false, false);
  228. if (fromIdentifier.isNotEmpty())
  229. return autoScaleFromString (fromIdentifier.fromFirstOccurrenceOf (":", false, false));
  230. }
  231. }
  232. return AutoScale::useDefault;
  233. }
  234. void setAutoScaleValueForPlugin (const String& identifier, AutoScale s)
  235. {
  236. auto plugins = StringArray::fromLines (getAppProperties().getUserSettings()->getValue ("autoScalePlugins"));
  237. plugins.removeEmptyStrings();
  238. auto index = [identifier, plugins]
  239. {
  240. auto it = std::find_if (plugins.begin(), plugins.end(),
  241. [&] (const String& str) { return str.startsWith (identifier); });
  242. return (int) std::distance (plugins.begin(), it);
  243. }();
  244. if (s == AutoScale::useDefault && index != plugins.size())
  245. {
  246. plugins.remove (index);
  247. }
  248. else
  249. {
  250. auto str = identifier + ":" + autoScaleToString (s);
  251. if (index != plugins.size())
  252. plugins.getReference (index) = str;
  253. else
  254. plugins.add (str);
  255. }
  256. getAppProperties().getUserSettings()->setValue ("autoScalePlugins", plugins.joinIntoString ("\n"));
  257. }
  258. static bool isAutoScaleAvailableForPlugin (const PluginDescription& description)
  259. {
  260. return autoScaleOptionAvailable
  261. && (description.pluginFormatName.containsIgnoreCase ("VST")
  262. || description.pluginFormatName.containsIgnoreCase ("LV2"));
  263. }
  264. bool shouldAutoScalePlugin (const PluginDescription& description)
  265. {
  266. if (! isAutoScaleAvailableForPlugin (description))
  267. return false;
  268. const auto scaleValue = getAutoScaleValueForPlugin (description.fileOrIdentifier);
  269. return (scaleValue == AutoScale::scaled
  270. || (scaleValue == AutoScale::useDefault
  271. && getAppProperties().getUserSettings()->getBoolValue ("autoScalePluginWindows")));
  272. }
  273. void addPluginAutoScaleOptionsSubMenu (AudioPluginInstance* pluginInstance,
  274. PopupMenu& menu)
  275. {
  276. if (pluginInstance == nullptr)
  277. return;
  278. auto description = pluginInstance->getPluginDescription();
  279. if (! isAutoScaleAvailableForPlugin (description))
  280. return;
  281. auto identifier = description.fileOrIdentifier;
  282. PopupMenu autoScaleMenu;
  283. autoScaleMenu.addItem ("Default",
  284. true,
  285. getAutoScaleValueForPlugin (identifier) == AutoScale::useDefault,
  286. [identifier] { setAutoScaleValueForPlugin (identifier, AutoScale::useDefault); });
  287. autoScaleMenu.addItem ("Enabled",
  288. true,
  289. getAutoScaleValueForPlugin (identifier) == AutoScale::scaled,
  290. [identifier] { setAutoScaleValueForPlugin (identifier, AutoScale::scaled); });
  291. autoScaleMenu.addItem ("Disabled",
  292. true,
  293. getAutoScaleValueForPlugin (identifier) == AutoScale::unscaled,
  294. [identifier] { setAutoScaleValueForPlugin (identifier, AutoScale::unscaled); });
  295. menu.addSubMenu ("Auto-scale window", autoScaleMenu);
  296. }
  297. // This kicks the whole thing off..
  298. START_JUCE_APPLICATION (PluginHostApp)