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.

279 lines
9.6KB

  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. //==============================================================================
  25. class PluginHostApp : public JUCEApplication,
  26. private AsyncUpdater
  27. {
  28. public:
  29. PluginHostApp() {}
  30. void initialise (const String&) override
  31. {
  32. // initialise our settings file..
  33. PropertiesFile::Options options;
  34. options.applicationName = "Juce Audio Plugin Host";
  35. options.filenameSuffix = "settings";
  36. options.osxLibrarySubFolder = "Preferences";
  37. appProperties.reset (new ApplicationProperties());
  38. appProperties->setStorageParameters (options);
  39. mainWindow.reset (new MainHostWindow());
  40. mainWindow->setUsingNativeTitleBar (true);
  41. commandManager.registerAllCommandsForTarget (this);
  42. commandManager.registerAllCommandsForTarget (mainWindow.get());
  43. mainWindow->menuItemsChanged();
  44. // Important note! We're going to use an async update here so that if we need
  45. // to re-open a file and instantiate some plugins, it will happen AFTER this
  46. // initialisation method has returned.
  47. // On Windows this probably won't make a difference, but on OSX there's a subtle event loop
  48. // issue that can happen if a plugin runs one of those irritating modal dialogs while it's
  49. // being loaded. If that happens inside this method, the OSX event loop seems to be in some
  50. // kind of special "initialisation" mode and things get confused. But if we load the plugin
  51. // later when the normal event loop is running, everything's fine.
  52. triggerAsyncUpdate();
  53. }
  54. void handleAsyncUpdate() override
  55. {
  56. File fileToOpen;
  57. #if JUCE_ANDROID || JUCE_IOS
  58. fileToOpen = PluginGraph::getDefaultGraphDocumentOnMobile();
  59. #else
  60. for (int i = 0; i < getCommandLineParameterArray().size(); ++i)
  61. {
  62. fileToOpen = File::getCurrentWorkingDirectory().getChildFile (getCommandLineParameterArray()[i]);
  63. if (fileToOpen.existsAsFile())
  64. break;
  65. }
  66. #endif
  67. if (! fileToOpen.existsAsFile())
  68. {
  69. RecentlyOpenedFilesList recentFiles;
  70. recentFiles.restoreFromString (getAppProperties().getUserSettings()->getValue ("recentFilterGraphFiles"));
  71. if (recentFiles.getNumFiles() > 0)
  72. fileToOpen = recentFiles.getFile (0);
  73. }
  74. if (fileToOpen.existsAsFile())
  75. if (auto* graph = mainWindow->graphHolder.get())
  76. if (auto* ioGraph = graph->graph.get())
  77. ioGraph->loadFrom (fileToOpen, true);
  78. }
  79. void shutdown() override
  80. {
  81. mainWindow = nullptr;
  82. appProperties = nullptr;
  83. LookAndFeel::setDefaultLookAndFeel (nullptr);
  84. }
  85. void suspended() override
  86. {
  87. #if JUCE_ANDROID || JUCE_IOS
  88. if (auto graph = mainWindow->graphHolder.get())
  89. if (auto ioGraph = graph->graph.get())
  90. ioGraph->saveDocument (PluginGraph::getDefaultGraphDocumentOnMobile());
  91. #endif
  92. }
  93. void systemRequestedQuit() override
  94. {
  95. if (mainWindow != nullptr)
  96. mainWindow->tryToQuitApplication();
  97. else
  98. JUCEApplicationBase::quit();
  99. }
  100. bool backButtonPressed() override
  101. {
  102. if (mainWindow->graphHolder != nullptr)
  103. mainWindow->graphHolder->hideLastSidePanel();
  104. return true;
  105. }
  106. const String getApplicationName() override { return "Juce Plug-In Host"; }
  107. const String getApplicationVersion() override { return ProjectInfo::versionString; }
  108. bool moreThanOneInstanceAllowed() override { return true; }
  109. ApplicationCommandManager commandManager;
  110. std::unique_ptr<ApplicationProperties> appProperties;
  111. private:
  112. std::unique_ptr<MainHostWindow> mainWindow;
  113. };
  114. static PluginHostApp& getApp() { return *dynamic_cast<PluginHostApp*>(JUCEApplication::getInstance()); }
  115. ApplicationProperties& getAppProperties() { return *getApp().appProperties; }
  116. ApplicationCommandManager& getCommandManager() { return getApp().commandManager; }
  117. bool isOnTouchDevice()
  118. {
  119. static bool isTouch = Desktop::getInstance().getMainMouseSource().isTouch();
  120. return isTouch;
  121. }
  122. //==============================================================================
  123. static AutoScale autoScaleFromString (StringRef str)
  124. {
  125. if (str.isEmpty()) return AutoScale::useDefault;
  126. if (str == CharPointer_ASCII { "0" }) return AutoScale::scaled;
  127. if (str == CharPointer_ASCII { "1" }) return AutoScale::unscaled;
  128. jassertfalse;
  129. return AutoScale::useDefault;
  130. }
  131. static const char* autoScaleToString (AutoScale autoScale)
  132. {
  133. if (autoScale == AutoScale::scaled) return "0";
  134. if (autoScale == AutoScale::unscaled) return "1";
  135. return {};
  136. }
  137. AutoScale getAutoScaleValueForPlugin (const String& identifier)
  138. {
  139. if (identifier.isNotEmpty())
  140. {
  141. auto plugins = StringArray::fromLines (getAppProperties().getUserSettings()->getValue ("autoScalePlugins"));
  142. plugins.removeEmptyStrings();
  143. for (auto& plugin : plugins)
  144. {
  145. auto fromIdentifier = plugin.fromFirstOccurrenceOf (identifier, false, false);
  146. if (fromIdentifier.isNotEmpty())
  147. return autoScaleFromString (fromIdentifier.fromFirstOccurrenceOf (":", false, false));
  148. }
  149. }
  150. return AutoScale::useDefault;
  151. }
  152. void setAutoScaleValueForPlugin (const String& identifier, AutoScale s)
  153. {
  154. auto plugins = StringArray::fromLines (getAppProperties().getUserSettings()->getValue ("autoScalePlugins"));
  155. plugins.removeEmptyStrings();
  156. auto index = [identifier, plugins]
  157. {
  158. auto it = std::find_if (plugins.begin(), plugins.end(),
  159. [&] (const String& str) { return str.startsWith (identifier); });
  160. return (int) std::distance (plugins.begin(), it);
  161. }();
  162. if (s == AutoScale::useDefault && index != plugins.size())
  163. {
  164. plugins.remove (index);
  165. }
  166. else
  167. {
  168. auto str = identifier + ":" + autoScaleToString (s);
  169. if (index != plugins.size())
  170. plugins.getReference (index) = str;
  171. else
  172. plugins.add (str);
  173. }
  174. getAppProperties().getUserSettings()->setValue ("autoScalePlugins", plugins.joinIntoString ("\n"));
  175. }
  176. static bool isAutoScaleAvailableForPlugin (const PluginDescription& description)
  177. {
  178. return autoScaleOptionAvailable
  179. && description.pluginFormatName.containsIgnoreCase ("VST");
  180. }
  181. bool shouldAutoScalePlugin (const PluginDescription& description)
  182. {
  183. if (! isAutoScaleAvailableForPlugin (description))
  184. return false;
  185. const auto scaleValue = getAutoScaleValueForPlugin (description.fileOrIdentifier);
  186. return (scaleValue == AutoScale::scaled
  187. || (scaleValue == AutoScale::useDefault
  188. && getAppProperties().getUserSettings()->getBoolValue ("autoScalePluginWindows")));
  189. }
  190. void addPluginAutoScaleOptionsSubMenu (AudioPluginInstance* pluginInstance,
  191. PopupMenu& menu)
  192. {
  193. if (pluginInstance == nullptr)
  194. return;
  195. auto description = pluginInstance->getPluginDescription();
  196. if (! isAutoScaleAvailableForPlugin (description))
  197. return;
  198. auto identifier = description.fileOrIdentifier;
  199. PopupMenu autoScaleMenu;
  200. autoScaleMenu.addItem ("Default",
  201. true,
  202. getAutoScaleValueForPlugin (identifier) == AutoScale::useDefault,
  203. [identifier] { setAutoScaleValueForPlugin (identifier, AutoScale::useDefault); });
  204. autoScaleMenu.addItem ("Enabled",
  205. true,
  206. getAutoScaleValueForPlugin (identifier) == AutoScale::scaled,
  207. [identifier] { setAutoScaleValueForPlugin (identifier, AutoScale::scaled); });
  208. autoScaleMenu.addItem ("Disabled",
  209. true,
  210. getAutoScaleValueForPlugin (identifier) == AutoScale::unscaled,
  211. [identifier] { setAutoScaleValueForPlugin (identifier, AutoScale::unscaled); });
  212. menu.addSubMenu ("Auto-scale window", autoScaleMenu);
  213. }
  214. // This kicks the whole thing off..
  215. START_JUCE_APPLICATION (PluginHostApp)