Audio plugin host https://kx.studio/carla
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.

317 lines
9.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. JUCEApplicationBase::CreateInstanceFunction JUCEApplicationBase::createInstance = 0;
  24. JUCEApplicationBase* JUCEApplicationBase::appInstance = nullptr;
  25. JUCEApplicationBase::JUCEApplicationBase()
  26. : appReturnValue (0),
  27. stillInitialising (true)
  28. {
  29. jassert (isStandaloneApp() && appInstance == nullptr);
  30. appInstance = this;
  31. }
  32. JUCEApplicationBase::~JUCEApplicationBase()
  33. {
  34. jassert (appInstance == this);
  35. appInstance = nullptr;
  36. }
  37. void JUCEApplicationBase::setApplicationReturnValue (const int newReturnValue) noexcept
  38. {
  39. appReturnValue = newReturnValue;
  40. }
  41. // This is called on the Mac and iOS where the OS doesn't allow the stack to unwind on shutdown..
  42. void JUCEApplicationBase::appWillTerminateByForce()
  43. {
  44. JUCE_AUTORELEASEPOOL
  45. {
  46. {
  47. const ScopedPointer<JUCEApplicationBase> app (appInstance);
  48. if (app != nullptr)
  49. app->shutdownApp();
  50. }
  51. DeletedAtShutdown::deleteAll();
  52. MessageManager::deleteInstance();
  53. }
  54. }
  55. void JUCEApplicationBase::quit()
  56. {
  57. MessageManager::getInstance()->stopDispatchLoop();
  58. }
  59. void JUCEApplicationBase::sendUnhandledException (const std::exception* const e,
  60. const char* const sourceFile,
  61. const int lineNumber)
  62. {
  63. if (JUCEApplicationBase* const app = JUCEApplicationBase::getInstance())
  64. app->unhandledException (e, sourceFile, lineNumber);
  65. }
  66. //==============================================================================
  67. #if ! (JUCE_IOS || JUCE_ANDROID)
  68. #define JUCE_HANDLE_MULTIPLE_INSTANCES 1
  69. #endif
  70. #if JUCE_HANDLE_MULTIPLE_INSTANCES
  71. struct JUCEApplicationBase::MultipleInstanceHandler : public ActionListener
  72. {
  73. public:
  74. MultipleInstanceHandler (const String& appName)
  75. : appLock ("juceAppLock_" + appName)
  76. {
  77. }
  78. bool sendCommandLineToPreexistingInstance()
  79. {
  80. if (appLock.enter (0))
  81. return false;
  82. JUCEApplicationBase* const app = JUCEApplicationBase::getInstance();
  83. jassert (app != nullptr);
  84. MessageManager::broadcastMessage (app->getApplicationName()
  85. + "/" + app->getCommandLineParameters());
  86. return true;
  87. }
  88. void actionListenerCallback (const String& message) override
  89. {
  90. if (JUCEApplicationBase* const app = JUCEApplicationBase::getInstance())
  91. {
  92. const String appName (app->getApplicationName());
  93. if (message.startsWith (appName + "/"))
  94. app->anotherInstanceStarted (message.substring (appName.length() + 1));
  95. }
  96. }
  97. private:
  98. InterProcessLock appLock;
  99. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultipleInstanceHandler)
  100. };
  101. bool JUCEApplicationBase::sendCommandLineToPreexistingInstance()
  102. {
  103. jassert (multipleInstanceHandler == nullptr); // this must only be called once!
  104. multipleInstanceHandler = new MultipleInstanceHandler (getApplicationName());
  105. return multipleInstanceHandler->sendCommandLineToPreexistingInstance();
  106. }
  107. #else
  108. struct JUCEApplicationBase::MultipleInstanceHandler {};
  109. #endif
  110. //==============================================================================
  111. #if JUCE_ANDROID
  112. StringArray JUCEApplicationBase::getCommandLineParameterArray() { return StringArray(); }
  113. String JUCEApplicationBase::getCommandLineParameters() { return String(); }
  114. #else
  115. #if JUCE_WINDOWS && ! defined (_CONSOLE)
  116. String JUCE_CALLTYPE JUCEApplicationBase::getCommandLineParameters()
  117. {
  118. return CharacterFunctions::findEndOfToken (CharPointer_UTF16 (GetCommandLineW()),
  119. CharPointer_UTF16 (L" "),
  120. CharPointer_UTF16 (L"\"")).findEndOfWhitespace();
  121. }
  122. StringArray JUCE_CALLTYPE JUCEApplicationBase::getCommandLineParameterArray()
  123. {
  124. StringArray s;
  125. int argc = 0;
  126. if (LPWSTR* const argv = CommandLineToArgvW (GetCommandLineW(), &argc))
  127. {
  128. s = StringArray (argv + 1, argc - 1);
  129. LocalFree (argv);
  130. }
  131. return s;
  132. }
  133. #else
  134. #if JUCE_IOS
  135. extern int juce_iOSMain (int argc, const char* argv[], void* classPtr);
  136. #endif
  137. #if JUCE_MAC
  138. extern void initialiseNSApplication();
  139. #endif
  140. #if JUCE_WINDOWS
  141. const char* const* juce_argv = nullptr;
  142. int juce_argc = 0;
  143. #else
  144. extern const char* const* juce_argv; // declared in juce_core
  145. extern int juce_argc;
  146. #endif
  147. String JUCEApplicationBase::getCommandLineParameters()
  148. {
  149. String argString;
  150. for (int i = 1; i < juce_argc; ++i)
  151. {
  152. String arg (juce_argv[i]);
  153. if (arg.containsChar (' ') && ! arg.isQuotedString())
  154. arg = arg.quoted ('"');
  155. argString << arg << ' ';
  156. }
  157. return argString.trim();
  158. }
  159. StringArray JUCEApplicationBase::getCommandLineParameterArray()
  160. {
  161. return StringArray (juce_argv + 1, juce_argc - 1);
  162. }
  163. int JUCEApplicationBase::main (int argc, const char* argv[], void* customDelegate)
  164. {
  165. JUCE_AUTORELEASEPOOL
  166. {
  167. juce_argc = argc;
  168. juce_argv = argv;
  169. #if JUCE_MAC
  170. initialiseNSApplication();
  171. #endif
  172. #if JUCE_IOS
  173. return juce_iOSMain (argc, argv, customDelegate);
  174. #else
  175. ignoreUnused (customDelegate);
  176. return JUCEApplicationBase::main();
  177. #endif
  178. }
  179. }
  180. #endif
  181. //==============================================================================
  182. int JUCEApplicationBase::main()
  183. {
  184. ScopedJuceInitialiser_GUI libraryInitialiser;
  185. jassert (createInstance != nullptr);
  186. const ScopedPointer<JUCEApplicationBase> app (createInstance());
  187. jassert (app != nullptr);
  188. if (! app->initialiseApp())
  189. return app->shutdownApp();
  190. JUCE_TRY
  191. {
  192. // loop until a quit message is received..
  193. MessageManager::getInstance()->runDispatchLoop();
  194. }
  195. JUCE_CATCH_EXCEPTION
  196. return app->shutdownApp();
  197. }
  198. #endif
  199. //==============================================================================
  200. bool JUCEApplicationBase::initialiseApp()
  201. {
  202. #if JUCE_HANDLE_MULTIPLE_INSTANCES
  203. if ((! moreThanOneInstanceAllowed()) && sendCommandLineToPreexistingInstance())
  204. {
  205. DBG ("Another instance is running - quitting...");
  206. return false;
  207. }
  208. #endif
  209. #if JUCE_WINDOWS && JUCE_STANDALONE_APPLICATION && (! defined (_CONSOLE)) && (! JUCE_MINGW)
  210. if (AttachConsole (ATTACH_PARENT_PROCESS) != 0)
  211. {
  212. // if we've launched a GUI app from cmd.exe or PowerShell, we need this to enable printf etc.
  213. // However, only reassign stdout, stderr, stdin if they have not been already opened by
  214. // a redirect or similar.
  215. FILE* ignore;
  216. if (_fileno(stdout) < 0) freopen_s (&ignore, "CONOUT$", "w", stdout);
  217. if (_fileno(stderr) < 0) freopen_s (&ignore, "CONOUT$", "w", stderr);
  218. if (_fileno(stdin) < 0) freopen_s (&ignore, "CONIN$", "r", stdin);
  219. }
  220. #endif
  221. // let the app do its setting-up..
  222. initialise (getCommandLineParameters());
  223. stillInitialising = false;
  224. if (MessageManager::getInstance()->hasStopMessageBeenSent())
  225. return false;
  226. #if JUCE_HANDLE_MULTIPLE_INSTANCES
  227. if (multipleInstanceHandler != nullptr)
  228. MessageManager::getInstance()->registerBroadcastListener (multipleInstanceHandler);
  229. #endif
  230. return true;
  231. }
  232. int JUCEApplicationBase::shutdownApp()
  233. {
  234. jassert (JUCEApplicationBase::getInstance() == this);
  235. #if JUCE_HANDLE_MULTIPLE_INSTANCES
  236. if (multipleInstanceHandler != nullptr)
  237. MessageManager::getInstance()->deregisterBroadcastListener (multipleInstanceHandler);
  238. #endif
  239. JUCE_TRY
  240. {
  241. // give the app a chance to clean up..
  242. shutdown();
  243. }
  244. JUCE_CATCH_EXCEPTION
  245. multipleInstanceHandler = nullptr;
  246. return getApplicationReturnValue();
  247. }