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.

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