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

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