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.

326 lines
9.6KB

  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 {}; }
  113. String JUCEApplicationBase::getCommandLineParameters() { return {}; }
  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_LINUX && JUCE_MODULE_AVAILABLE_juce_gui_extra
  141. extern int juce_gtkWebkitMain (int argc, const char* argv[]);
  142. #endif
  143. #if JUCE_WINDOWS
  144. const char* const* juce_argv = nullptr;
  145. int juce_argc = 0;
  146. #else
  147. extern const char* const* juce_argv; // declared in juce_core
  148. extern int juce_argc;
  149. #endif
  150. String JUCEApplicationBase::getCommandLineParameters()
  151. {
  152. String argString;
  153. for (int i = 1; i < juce_argc; ++i)
  154. {
  155. String arg (juce_argv[i]);
  156. if (arg.containsChar (' ') && ! arg.isQuotedString())
  157. arg = arg.quoted ('"');
  158. argString << arg << ' ';
  159. }
  160. return argString.trim();
  161. }
  162. StringArray JUCEApplicationBase::getCommandLineParameterArray()
  163. {
  164. return StringArray (juce_argv + 1, juce_argc - 1);
  165. }
  166. int JUCEApplicationBase::main (int argc, const char* argv[], void* customDelegate)
  167. {
  168. JUCE_AUTORELEASEPOOL
  169. {
  170. juce_argc = argc;
  171. juce_argv = argv;
  172. #if JUCE_MAC
  173. initialiseNSApplication();
  174. #endif
  175. #if JUCE_LINUX && JUCE_MODULE_AVAILABLE_juce_gui_extra
  176. if (argc >= 2 && String (argv[1]) == "--juce-gtkwebkitfork-child")
  177. return juce_gtkWebkitMain (argc, argv);
  178. #endif
  179. #if JUCE_IOS
  180. return juce_iOSMain (argc, argv, customDelegate);
  181. #else
  182. ignoreUnused (customDelegate);
  183. return JUCEApplicationBase::main();
  184. #endif
  185. }
  186. }
  187. #endif
  188. //==============================================================================
  189. int JUCEApplicationBase::main()
  190. {
  191. ScopedJuceInitialiser_GUI libraryInitialiser;
  192. jassert (createInstance != nullptr);
  193. const ScopedPointer<JUCEApplicationBase> app (createInstance());
  194. jassert (app != nullptr);
  195. if (! app->initialiseApp())
  196. return app->shutdownApp();
  197. JUCE_TRY
  198. {
  199. // loop until a quit message is received..
  200. MessageManager::getInstance()->runDispatchLoop();
  201. }
  202. JUCE_CATCH_EXCEPTION
  203. return app->shutdownApp();
  204. }
  205. #endif
  206. //==============================================================================
  207. bool JUCEApplicationBase::initialiseApp()
  208. {
  209. #if JUCE_HANDLE_MULTIPLE_INSTANCES
  210. if ((! moreThanOneInstanceAllowed()) && sendCommandLineToPreexistingInstance())
  211. {
  212. DBG ("Another instance is running - quitting...");
  213. return false;
  214. }
  215. #endif
  216. #if JUCE_WINDOWS && JUCE_STANDALONE_APPLICATION && (! defined (_CONSOLE)) && (! JUCE_MINGW)
  217. if (AttachConsole (ATTACH_PARENT_PROCESS) != 0)
  218. {
  219. // if we've launched a GUI app from cmd.exe or PowerShell, we need this to enable printf etc.
  220. // However, only reassign stdout, stderr, stdin if they have not been already opened by
  221. // a redirect or similar.
  222. FILE* ignore;
  223. if (_fileno(stdout) < 0) freopen_s (&ignore, "CONOUT$", "w", stdout);
  224. if (_fileno(stderr) < 0) freopen_s (&ignore, "CONOUT$", "w", stderr);
  225. if (_fileno(stdin) < 0) freopen_s (&ignore, "CONIN$", "r", stdin);
  226. }
  227. #endif
  228. // let the app do its setting-up..
  229. initialise (getCommandLineParameters());
  230. stillInitialising = false;
  231. if (MessageManager::getInstance()->hasStopMessageBeenSent())
  232. return false;
  233. #if JUCE_HANDLE_MULTIPLE_INSTANCES
  234. if (multipleInstanceHandler != nullptr)
  235. MessageManager::getInstance()->registerBroadcastListener (multipleInstanceHandler);
  236. #endif
  237. return true;
  238. }
  239. int JUCEApplicationBase::shutdownApp()
  240. {
  241. jassert (JUCEApplicationBase::getInstance() == this);
  242. #if JUCE_HANDLE_MULTIPLE_INSTANCES
  243. if (multipleInstanceHandler != nullptr)
  244. MessageManager::getInstance()->deregisterBroadcastListener (multipleInstanceHandler);
  245. #endif
  246. JUCE_TRY
  247. {
  248. // give the app a chance to clean up..
  249. shutdown();
  250. }
  251. JUCE_CATCH_EXCEPTION
  252. multipleInstanceHandler = nullptr;
  253. return getApplicationReturnValue();
  254. }