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.

juce_ApplicationBase.cpp 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. JUCEApplicationBase::CreateInstanceFunction JUCEApplicationBase::createInstance = 0;
  18. JUCEApplicationBase* JUCEApplicationBase::appInstance = nullptr;
  19. JUCEApplicationBase::JUCEApplicationBase()
  20. : appReturnValue (0),
  21. stillInitialising (true)
  22. {
  23. jassert (isStandaloneApp() && appInstance == nullptr);
  24. appInstance = this;
  25. }
  26. JUCEApplicationBase::~JUCEApplicationBase()
  27. {
  28. jassert (appInstance == this);
  29. appInstance = nullptr;
  30. }
  31. void JUCEApplicationBase::setApplicationReturnValue (const int newReturnValue) noexcept
  32. {
  33. appReturnValue = newReturnValue;
  34. }
  35. // This is called on the Mac and iOS where the OS doesn't allow the stack to unwind on shutdown..
  36. void JUCEApplicationBase::appWillTerminateByForce()
  37. {
  38. JUCE_AUTORELEASEPOOL
  39. {
  40. {
  41. const ScopedPointer<JUCEApplicationBase> app (appInstance);
  42. if (app != nullptr)
  43. app->shutdownApp();
  44. }
  45. DeletedAtShutdown::deleteAll();
  46. MessageManager::deleteInstance();
  47. }
  48. }
  49. void JUCEApplicationBase::quit()
  50. {
  51. MessageManager::getInstance()->stopDispatchLoop();
  52. }
  53. void JUCEApplicationBase::sendUnhandledException (const std::exception* const e,
  54. const char* const sourceFile,
  55. const int lineNumber)
  56. {
  57. if (JUCEApplicationBase* const app = JUCEApplicationBase::getInstance())
  58. app->unhandledException (e, sourceFile, lineNumber);
  59. }
  60. //==============================================================================
  61. #if ! (JUCE_IOS || JUCE_ANDROID)
  62. #define JUCE_HANDLE_MULTIPLE_INSTANCES 1
  63. #endif
  64. #if JUCE_HANDLE_MULTIPLE_INSTANCES
  65. struct JUCEApplicationBase::MultipleInstanceHandler : public ActionListener
  66. {
  67. public:
  68. MultipleInstanceHandler (const String& appName)
  69. : appLock ("juceAppLock_" + appName)
  70. {
  71. }
  72. bool sendCommandLineToPreexistingInstance()
  73. {
  74. if (appLock.enter (0))
  75. return false;
  76. JUCEApplicationBase* const app = JUCEApplicationBase::getInstance();
  77. jassert (app != nullptr);
  78. MessageManager::broadcastMessage (app->getApplicationName()
  79. + "/" + app->getCommandLineParameters());
  80. return true;
  81. }
  82. void actionListenerCallback (const String& message) override
  83. {
  84. if (JUCEApplicationBase* const app = JUCEApplicationBase::getInstance())
  85. {
  86. const String appName (app->getApplicationName());
  87. if (message.startsWith (appName + "/"))
  88. app->anotherInstanceStarted (message.substring (appName.length() + 1));
  89. }
  90. }
  91. private:
  92. InterProcessLock appLock;
  93. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultipleInstanceHandler)
  94. };
  95. bool JUCEApplicationBase::sendCommandLineToPreexistingInstance()
  96. {
  97. jassert (multipleInstanceHandler == nullptr); // this must only be called once!
  98. multipleInstanceHandler = new MultipleInstanceHandler (getApplicationName());
  99. return multipleInstanceHandler->sendCommandLineToPreexistingInstance();
  100. }
  101. #else
  102. struct JUCEApplicationBase::MultipleInstanceHandler {};
  103. #endif
  104. //==============================================================================
  105. #if JUCE_ANDROID
  106. StringArray JUCEApplicationBase::getCommandLineParameterArray() { return StringArray(); }
  107. String JUCEApplicationBase::getCommandLineParameters() { return String(); }
  108. #else
  109. #if JUCE_WINDOWS
  110. String JUCE_CALLTYPE JUCEApplicationBase::getCommandLineParameters()
  111. {
  112. return CharacterFunctions::findEndOfToken (CharPointer_UTF16 (GetCommandLineW()),
  113. CharPointer_UTF16 (L" "),
  114. CharPointer_UTF16 (L"\"")).findEndOfWhitespace();
  115. }
  116. StringArray JUCE_CALLTYPE JUCEApplicationBase::getCommandLineParameterArray()
  117. {
  118. StringArray s;
  119. int argc = 0;
  120. if (LPWSTR* const argv = CommandLineToArgvW (GetCommandLineW(), &argc))
  121. {
  122. s = StringArray (argv + 1, argc - 1);
  123. LocalFree (argv);
  124. }
  125. return s;
  126. }
  127. #else
  128. #if JUCE_IOS
  129. extern int juce_iOSMain (int argc, const char* argv[]);
  130. #endif
  131. #if JUCE_MAC
  132. extern void initialiseNSApplication();
  133. #endif
  134. extern const char* const* juce_argv; // declared in juce_core
  135. extern int juce_argc;
  136. String JUCEApplicationBase::getCommandLineParameters()
  137. {
  138. String argString;
  139. for (int i = 1; i < juce_argc; ++i)
  140. {
  141. String arg (juce_argv[i]);
  142. if (arg.containsChar (' ') && ! arg.isQuotedString())
  143. arg = arg.quoted ('"');
  144. argString << arg << ' ';
  145. }
  146. return argString.trim();
  147. }
  148. StringArray JUCEApplicationBase::getCommandLineParameterArray()
  149. {
  150. return StringArray (juce_argv + 1, juce_argc - 1);
  151. }
  152. int JUCEApplicationBase::main (int argc, const char* argv[])
  153. {
  154. JUCE_AUTORELEASEPOOL
  155. {
  156. juce_argc = argc;
  157. juce_argv = argv;
  158. #if JUCE_MAC
  159. initialiseNSApplication();
  160. #endif
  161. #if JUCE_IOS
  162. return juce_iOSMain (argc, argv);
  163. #else
  164. return JUCEApplicationBase::main();
  165. #endif
  166. }
  167. }
  168. #endif
  169. //==============================================================================
  170. int JUCEApplicationBase::main()
  171. {
  172. ScopedJuceInitialiser_GUI libraryInitialiser;
  173. jassert (createInstance != nullptr);
  174. const ScopedPointer<JUCEApplicationBase> app (createInstance());
  175. jassert (app != nullptr);
  176. if (! app->initialiseApp())
  177. return 0;
  178. JUCE_TRY
  179. {
  180. // loop until a quit message is received..
  181. MessageManager::getInstance()->runDispatchLoop();
  182. }
  183. JUCE_CATCH_EXCEPTION
  184. return app->shutdownApp();
  185. }
  186. #endif
  187. //==============================================================================
  188. bool JUCEApplicationBase::initialiseApp()
  189. {
  190. #if JUCE_HANDLE_MULTIPLE_INSTANCES
  191. if ((! moreThanOneInstanceAllowed()) && sendCommandLineToPreexistingInstance())
  192. {
  193. DBG ("Another instance is running - quitting...");
  194. return false;
  195. }
  196. #endif
  197. // let the app do its setting-up..
  198. initialise (getCommandLineParameters());
  199. stillInitialising = false;
  200. if (MessageManager::getInstance()->hasStopMessageBeenSent())
  201. return false;
  202. #if JUCE_HANDLE_MULTIPLE_INSTANCES
  203. if (multipleInstanceHandler != nullptr)
  204. MessageManager::getInstance()->registerBroadcastListener (multipleInstanceHandler);
  205. #endif
  206. return true;
  207. }
  208. int JUCEApplicationBase::shutdownApp()
  209. {
  210. jassert (JUCEApplicationBase::getInstance() == this);
  211. #if JUCE_HANDLE_MULTIPLE_INSTANCES
  212. if (multipleInstanceHandler != nullptr)
  213. MessageManager::getInstance()->deregisterBroadcastListener (multipleInstanceHandler);
  214. #endif
  215. JUCE_TRY
  216. {
  217. // give the app a chance to clean up..
  218. shutdown();
  219. }
  220. JUCE_CATCH_EXCEPTION
  221. multipleInstanceHandler = nullptr;
  222. return getApplicationReturnValue();
  223. }