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.

360 lines
12KB

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