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.

334 lines
13KB

  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. //==============================================================================
  20. /**
  21. Abstract base class for application classes.
  22. Note that in the juce_gui_basics module, there's a utility class JUCEApplication
  23. which derives from JUCEApplicationBase, and takes care of a few chores. Most
  24. of the time you'll want to derive your class from JUCEApplication rather than
  25. using JUCEApplicationBase directly, but if you're not using the juce_gui_basics
  26. module then you might need to go straight to this base class.
  27. Any application that wants to run an event loop must declare a subclass of
  28. JUCEApplicationBase, and implement its various pure virtual methods.
  29. It then needs to use the START_JUCE_APPLICATION macro somewhere in a CPP file
  30. to declare an instance of this class and generate suitable platform-specific
  31. boilerplate code to launch the app.
  32. e.g. @code
  33. class MyJUCEApp : public JUCEApplication
  34. {
  35. public:
  36. MyJUCEApp() {}
  37. ~MyJUCEApp() {}
  38. void initialise (const String& commandLine) override
  39. {
  40. myMainWindow.reset (new MyApplicationWindow());
  41. myMainWindow->setBounds (100, 100, 400, 500);
  42. myMainWindow->setVisible (true);
  43. }
  44. void shutdown() override
  45. {
  46. myMainWindow = nullptr;
  47. }
  48. const String getApplicationName() override
  49. {
  50. return "Super JUCE-o-matic";
  51. }
  52. const String getApplicationVersion() override
  53. {
  54. return "1.0";
  55. }
  56. private:
  57. std::unique_ptr<MyApplicationWindow> myMainWindow;
  58. };
  59. // this generates boilerplate code to launch our app class:
  60. START_JUCE_APPLICATION (MyJUCEApp)
  61. @endcode
  62. @see JUCEApplication, START_JUCE_APPLICATION
  63. @tags{Events}
  64. */
  65. class JUCE_API JUCEApplicationBase
  66. {
  67. protected:
  68. //==============================================================================
  69. JUCEApplicationBase();
  70. public:
  71. /** Destructor. */
  72. virtual ~JUCEApplicationBase();
  73. //==============================================================================
  74. /** Returns the global instance of the application object that's running. */
  75. static JUCEApplicationBase* getInstance() noexcept { return appInstance; }
  76. //==============================================================================
  77. /** Returns the application's name. */
  78. virtual const String getApplicationName() = 0;
  79. /** Returns the application's version number. */
  80. virtual const String getApplicationVersion() = 0;
  81. /** Checks whether multiple instances of the app are allowed.
  82. If your application class returns true for this, more than one instance is
  83. permitted to run (except on the Mac where this isn't possible).
  84. If it's false, the second instance won't start, but you will still get a
  85. callback to anotherInstanceStarted() to tell you about this - which
  86. gives you a chance to react to what the user was trying to do.
  87. @see anotherInstanceStarted
  88. */
  89. virtual bool moreThanOneInstanceAllowed() = 0;
  90. /** Called when the application starts.
  91. This will be called once to let the application do whatever initialisation
  92. it needs, create its windows, etc.
  93. After the method returns, the normal event-dispatch loop will be run,
  94. until the quit() method is called, at which point the shutdown()
  95. method will be called to let the application clear up anything it needs
  96. to delete.
  97. If during the initialise() method, the application decides not to start-up
  98. after all, it can just call the quit() method and the event loop won't be run.
  99. @param commandLineParameters the line passed in does not include the name of
  100. the executable, just the parameter list. To get the
  101. parameters as an array, you can call
  102. JUCEApplication::getCommandLineParameters()
  103. @see shutdown, quit
  104. */
  105. virtual void initialise (const String& commandLineParameters) = 0;
  106. /* Called to allow the application to clear up before exiting.
  107. After JUCEApplication::quit() has been called, the event-dispatch loop will
  108. terminate, and this method will get called to allow the app to sort itself
  109. out.
  110. Be careful that nothing happens in this method that might rely on messages
  111. being sent, or any kind of window activity, because the message loop is no
  112. longer running at this point.
  113. @see DeletedAtShutdown
  114. */
  115. virtual void shutdown() = 0;
  116. /** Indicates that the user has tried to start up another instance of the app.
  117. This will get called even if moreThanOneInstanceAllowed() is false.
  118. It is currently only implemented on Windows and Mac.
  119. @see moreThanOneInstanceAllowed
  120. */
  121. virtual void anotherInstanceStarted (const String& commandLine) = 0;
  122. /** Called when the operating system is trying to close the application.
  123. The default implementation of this method is to call quit(), but it may
  124. be overloaded to ignore the request or do some other special behaviour
  125. instead. For example, you might want to offer the user the chance to save
  126. their changes before quitting, and give them the chance to cancel.
  127. If you want to send a quit signal to your app, this is the correct method
  128. to call, because it means that requests that come from the system get handled
  129. in the same way as those from your own application code. So e.g. you'd
  130. call this method from a "quit" item on a menu bar.
  131. */
  132. virtual void systemRequestedQuit() = 0;
  133. /** This method is called when the application is being put into background mode
  134. by the operating system.
  135. */
  136. virtual void suspended() = 0;
  137. /** This method is called when the application is being woken from background mode
  138. by the operating system.
  139. */
  140. virtual void resumed() = 0;
  141. /** If any unhandled exceptions make it through to the message dispatch loop, this
  142. callback will be triggered, in case you want to log them or do some other
  143. type of error-handling.
  144. If the type of exception is derived from the std::exception class, the pointer
  145. passed-in will be valid. If the exception is of unknown type, this pointer
  146. will be null.
  147. */
  148. virtual void unhandledException (const std::exception*,
  149. const String& sourceFilename,
  150. int lineNumber) = 0;
  151. /** Called by the operating system to indicate that you should reduce your memory
  152. footprint.
  153. You should override this method to free up some memory gracefully, if possible,
  154. otherwise the host may forcibly kill your app.
  155. At the moment this method is only called on iOS.
  156. */
  157. virtual void memoryWarningReceived() { jassertfalse; }
  158. //==============================================================================
  159. /** This will be called when the back button on a device is pressed. The return value
  160. should be used to indicate whether the back button event has been handled by
  161. the application, for example if you want to implement custom navigation instead
  162. of the standard behaviour on Android.
  163. This is currently only implemented on Android devices.
  164. @returns true if the event has been handled, or false if the default OS
  165. behaviour should happen
  166. */
  167. virtual bool backButtonPressed() { return false; }
  168. //==============================================================================
  169. /** Signals that the main message loop should stop and the application should terminate.
  170. This isn't synchronous, it just posts a quit message to the main queue, and
  171. when this message arrives, the message loop will stop, the shutdown() method
  172. will be called, and the app will exit.
  173. Note that this will cause an unconditional quit to happen, so if you need an
  174. extra level before this, e.g. to give the user the chance to save their work
  175. and maybe cancel the quit, you'll need to handle this in the systemRequestedQuit()
  176. method - see that method's help for more info.
  177. @see MessageManager
  178. */
  179. static void quit();
  180. //==============================================================================
  181. /** Returns the application's command line parameters as a set of strings.
  182. @see getCommandLineParameters
  183. */
  184. static StringArray JUCE_CALLTYPE getCommandLineParameterArray();
  185. /** Returns the application's command line parameters as a single string.
  186. @see getCommandLineParameterArray
  187. */
  188. static String JUCE_CALLTYPE getCommandLineParameters();
  189. //==============================================================================
  190. /** Sets the value that should be returned as the application's exit code when the
  191. app quits.
  192. This is the value that's returned by the main() function. Normally you'd leave this
  193. as 0 unless you want to indicate an error code.
  194. @see getApplicationReturnValue
  195. */
  196. void setApplicationReturnValue (int newReturnValue) noexcept;
  197. /** Returns the value that has been set as the application's exit code.
  198. @see setApplicationReturnValue
  199. */
  200. int getApplicationReturnValue() const noexcept { return appReturnValue; }
  201. //==============================================================================
  202. /** Returns true if this executable is running as an app (as opposed to being a plugin
  203. or other kind of shared library. */
  204. static bool isStandaloneApp() noexcept { return createInstance != nullptr; }
  205. /** Returns true if the application hasn't yet completed its initialise() method
  206. and entered the main event loop.
  207. This is handy for things like splash screens to know when the app's up-and-running
  208. properly.
  209. */
  210. bool isInitialising() const noexcept { return stillInitialising; }
  211. //==============================================================================
  212. #ifndef DOXYGEN
  213. // The following methods are for internal use only...
  214. static int main();
  215. static int main (int argc, const char* argv[]);
  216. static void appWillTerminateByForce();
  217. using CreateInstanceFunction = JUCEApplicationBase* (*)();
  218. static CreateInstanceFunction createInstance;
  219. #if JUCE_IOS
  220. static void* iOSCustomDelegate;
  221. #endif
  222. virtual bool initialiseApp();
  223. int shutdownApp();
  224. static void JUCE_CALLTYPE sendUnhandledException (const std::exception*, const char* sourceFile, int lineNumber);
  225. bool sendCommandLineToPreexistingInstance();
  226. #endif
  227. private:
  228. //==============================================================================
  229. static JUCEApplicationBase* appInstance;
  230. int appReturnValue = 0;
  231. bool stillInitialising = true;
  232. struct MultipleInstanceHandler;
  233. std::unique_ptr<MultipleInstanceHandler> multipleInstanceHandler;
  234. JUCE_DECLARE_NON_COPYABLE (JUCEApplicationBase)
  235. };
  236. //==============================================================================
  237. #if JUCE_CATCH_UNHANDLED_EXCEPTIONS || defined (DOXYGEN)
  238. /** The JUCE_TRY/JUCE_CATCH_EXCEPTION wrappers can be used to pass any uncaught exceptions to
  239. the JUCEApplicationBase::sendUnhandledException() method.
  240. This functionality can be enabled with the JUCE_CATCH_UNHANDLED_EXCEPTIONS macro.
  241. */
  242. #define JUCE_TRY try
  243. /** The JUCE_TRY/JUCE_CATCH_EXCEPTION wrappers can be used to pass any uncaught exceptions to
  244. the JUCEApplicationBase::sendUnhandledException() method.
  245. This functionality can be enabled with the JUCE_CATCH_UNHANDLED_EXCEPTIONS macro.
  246. */
  247. #define JUCE_CATCH_EXCEPTION \
  248. catch (const std::exception& e) { juce::JUCEApplicationBase::sendUnhandledException (&e, __FILE__, __LINE__); } \
  249. catch (...) { juce::JUCEApplicationBase::sendUnhandledException (nullptr, __FILE__, __LINE__); }
  250. #else
  251. #define JUCE_TRY
  252. #define JUCE_CATCH_EXCEPTION
  253. #endif
  254. } // namespace juce