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 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. JUCEApplicationBase::CreateInstanceFunction JUCEApplicationBase::createInstance = nullptr;
  20. JUCEApplicationBase* JUCEApplicationBase::appInstance = nullptr;
  21. #if JUCE_IOS
  22. void* JUCEApplicationBase::iOSCustomDelegate = nullptr;
  23. #endif
  24. JUCEApplicationBase::JUCEApplicationBase()
  25. {
  26. jassert (isStandaloneApp() && appInstance == nullptr);
  27. appInstance = this;
  28. }
  29. JUCEApplicationBase::~JUCEApplicationBase()
  30. {
  31. jassert (appInstance == this);
  32. appInstance = nullptr;
  33. }
  34. void JUCEApplicationBase::setApplicationReturnValue (const int newReturnValue) noexcept
  35. {
  36. appReturnValue = newReturnValue;
  37. }
  38. // This is called on the Mac and iOS where the OS doesn't allow the stack to unwind on shutdown..
  39. void JUCEApplicationBase::appWillTerminateByForce()
  40. {
  41. JUCE_AUTORELEASEPOOL
  42. {
  43. {
  44. const std::unique_ptr<JUCEApplicationBase> app (appInstance);
  45. if (app != nullptr)
  46. app->shutdownApp();
  47. }
  48. DeletedAtShutdown::deleteAll();
  49. MessageManager::deleteInstance();
  50. }
  51. }
  52. void JUCEApplicationBase::quit()
  53. {
  54. MessageManager::getInstance()->stopDispatchLoop();
  55. }
  56. void JUCEApplicationBase::sendUnhandledException (const std::exception* const e,
  57. const char* const sourceFile,
  58. const int lineNumber)
  59. {
  60. if (auto* app = JUCEApplicationBase::getInstance())
  61. {
  62. // If you hit this assertion then the __FILE__ macro is providing a
  63. // relative path instead of an absolute path. On Windows this will be
  64. // a path relative to the build directory rather than the currently
  65. // running application. To fix this you must compile with the /FC flag.
  66. jassert (File::isAbsolutePath (sourceFile));
  67. app->unhandledException (e, sourceFile, lineNumber);
  68. }
  69. }
  70. //==============================================================================
  71. #if ! (JUCE_IOS || JUCE_ANDROID)
  72. #define JUCE_HANDLE_MULTIPLE_INSTANCES 1
  73. #endif
  74. #if JUCE_HANDLE_MULTIPLE_INSTANCES
  75. struct JUCEApplicationBase::MultipleInstanceHandler : public ActionListener
  76. {
  77. MultipleInstanceHandler (const String& appName)
  78. : appLock ("juceAppLock_" + appName)
  79. {
  80. }
  81. bool sendCommandLineToPreexistingInstance()
  82. {
  83. if (appLock.enter (0))
  84. return false;
  85. if (auto* app = JUCEApplicationBase::getInstance())
  86. {
  87. MessageManager::broadcastMessage (app->getApplicationName() + "/" + app->getCommandLineParameters());
  88. return true;
  89. }
  90. jassertfalse;
  91. return false;
  92. }
  93. void actionListenerCallback (const String& message) override
  94. {
  95. if (auto* app = JUCEApplicationBase::getInstance())
  96. {
  97. auto appName = app->getApplicationName();
  98. if (message.startsWith (appName + "/"))
  99. app->anotherInstanceStarted (message.substring (appName.length() + 1));
  100. }
  101. }
  102. private:
  103. InterProcessLock appLock;
  104. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultipleInstanceHandler)
  105. };
  106. bool JUCEApplicationBase::sendCommandLineToPreexistingInstance()
  107. {
  108. jassert (multipleInstanceHandler == nullptr); // this must only be called once!
  109. multipleInstanceHandler.reset (new MultipleInstanceHandler (getApplicationName()));
  110. return multipleInstanceHandler->sendCommandLineToPreexistingInstance();
  111. }
  112. #else
  113. struct JUCEApplicationBase::MultipleInstanceHandler {};
  114. #endif
  115. //==============================================================================
  116. #if JUCE_ANDROID
  117. StringArray JUCEApplicationBase::getCommandLineParameterArray() { return {}; }
  118. String JUCEApplicationBase::getCommandLineParameters() { return {}; }
  119. #else
  120. #if JUCE_WINDOWS && ! defined (_CONSOLE)
  121. String JUCE_CALLTYPE JUCEApplicationBase::getCommandLineParameters()
  122. {
  123. return CharacterFunctions::findEndOfToken (CharPointer_UTF16 (GetCommandLineW()),
  124. CharPointer_UTF16 (L" "),
  125. CharPointer_UTF16 (L"\"")).findEndOfWhitespace();
  126. }
  127. StringArray JUCE_CALLTYPE JUCEApplicationBase::getCommandLineParameterArray()
  128. {
  129. StringArray s;
  130. int argc = 0;
  131. if (auto argv = CommandLineToArgvW (GetCommandLineW(), &argc))
  132. {
  133. s = StringArray (argv + 1, argc - 1);
  134. LocalFree (argv);
  135. }
  136. return s;
  137. }
  138. #else
  139. #if JUCE_IOS
  140. extern int juce_iOSMain (int argc, const char* argv[], void* classPtr);
  141. #endif
  142. #if JUCE_MAC
  143. extern void initialiseNSApplication();
  144. #endif
  145. #if JUCE_LINUX && JUCE_MODULE_AVAILABLE_juce_gui_extra && (! defined(JUCE_WEB_BROWSER) || JUCE_WEB_BROWSER)
  146. extern int juce_gtkWebkitMain (int argc, const char* argv[]);
  147. #endif
  148. #if JUCE_WINDOWS
  149. const char* const* juce_argv = nullptr;
  150. int juce_argc = 0;
  151. #else
  152. extern const char* const* juce_argv; // declared in juce_core
  153. extern int juce_argc;
  154. #endif
  155. String JUCEApplicationBase::getCommandLineParameters()
  156. {
  157. String argString;
  158. for (int i = 1; i < juce_argc; ++i)
  159. {
  160. String arg (juce_argv[i]);
  161. if (arg.containsChar (' ') && ! arg.isQuotedString())
  162. arg = arg.quoted ('"');
  163. argString << arg << ' ';
  164. }
  165. return argString.trim();
  166. }
  167. StringArray JUCEApplicationBase::getCommandLineParameterArray()
  168. {
  169. return StringArray (juce_argv + 1, juce_argc - 1);
  170. }
  171. int JUCEApplicationBase::main (int argc, const char* argv[])
  172. {
  173. JUCE_AUTORELEASEPOOL
  174. {
  175. juce_argc = argc;
  176. juce_argv = argv;
  177. #if JUCE_MAC
  178. initialiseNSApplication();
  179. #endif
  180. #if JUCE_LINUX && JUCE_MODULE_AVAILABLE_juce_gui_extra && (! defined(JUCE_WEB_BROWSER) || JUCE_WEB_BROWSER)
  181. if (argc >= 2 && String (argv[1]) == "--juce-gtkwebkitfork-child")
  182. return juce_gtkWebkitMain (argc, argv);
  183. #endif
  184. #if JUCE_IOS
  185. return juce_iOSMain (argc, argv, iOSCustomDelegate);
  186. #else
  187. return JUCEApplicationBase::main();
  188. #endif
  189. }
  190. }
  191. #endif
  192. //==============================================================================
  193. int JUCEApplicationBase::main()
  194. {
  195. ScopedJuceInitialiser_GUI libraryInitialiser;
  196. jassert (createInstance != nullptr);
  197. const std::unique_ptr<JUCEApplicationBase> app (createInstance());
  198. jassert (app != nullptr);
  199. if (! app->initialiseApp())
  200. return app->shutdownApp();
  201. JUCE_TRY
  202. {
  203. // loop until a quit message is received..
  204. MessageManager::getInstance()->runDispatchLoop();
  205. }
  206. JUCE_CATCH_EXCEPTION
  207. return app->shutdownApp();
  208. }
  209. #endif
  210. //==============================================================================
  211. bool JUCEApplicationBase::initialiseApp()
  212. {
  213. #if JUCE_HANDLE_MULTIPLE_INSTANCES
  214. if ((! moreThanOneInstanceAllowed()) && sendCommandLineToPreexistingInstance())
  215. {
  216. DBG ("Another instance is running - quitting...");
  217. return false;
  218. }
  219. #endif
  220. #if JUCE_WINDOWS && JUCE_STANDALONE_APPLICATION && (! defined (_CONSOLE)) && (! JUCE_MINGW)
  221. if (AttachConsole (ATTACH_PARENT_PROCESS) != 0)
  222. {
  223. // if we've launched a GUI app from cmd.exe or PowerShell, we need this to enable printf etc.
  224. // However, only reassign stdout, stderr, stdin if they have not been already opened by
  225. // a redirect or similar.
  226. FILE* ignore;
  227. if (_fileno(stdout) < 0) freopen_s (&ignore, "CONOUT$", "w", stdout);
  228. if (_fileno(stderr) < 0) freopen_s (&ignore, "CONOUT$", "w", stderr);
  229. if (_fileno(stdin) < 0) freopen_s (&ignore, "CONIN$", "r", stdin);
  230. }
  231. #endif
  232. // let the app do its setting-up..
  233. initialise (getCommandLineParameters());
  234. stillInitialising = false;
  235. if (MessageManager::getInstance()->hasStopMessageBeenSent())
  236. return false;
  237. #if JUCE_HANDLE_MULTIPLE_INSTANCES
  238. if (auto* mih = multipleInstanceHandler.get())
  239. MessageManager::getInstance()->registerBroadcastListener (mih);
  240. #endif
  241. return true;
  242. }
  243. int JUCEApplicationBase::shutdownApp()
  244. {
  245. jassert (JUCEApplicationBase::getInstance() == this);
  246. #if JUCE_HANDLE_MULTIPLE_INSTANCES
  247. if (auto* mih = multipleInstanceHandler.get())
  248. MessageManager::getInstance()->deregisterBroadcastListener (mih);
  249. #endif
  250. JUCE_TRY
  251. {
  252. // give the app a chance to clean up..
  253. shutdown();
  254. }
  255. JUCE_CATCH_EXCEPTION
  256. multipleInstanceHandler.reset();
  257. return getApplicationReturnValue();
  258. }
  259. } // namespace juce