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