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.

283 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_APPLICATIONBASE_H_INCLUDED
  18. #define JUCE_APPLICATIONBASE_H_INCLUDED
  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 = 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. ScopedPointer<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. */
  64. class JUCE_API JUCEApplicationBase
  65. {
  66. protected:
  67. //==============================================================================
  68. JUCEApplicationBase();
  69. public:
  70. /** Destructor. */
  71. virtual ~JUCEApplicationBase();
  72. //==============================================================================
  73. /** Returns the global instance of the application object that's running. */
  74. static JUCEApplicationBase* getInstance() noexcept { return appInstance; }
  75. //==============================================================================
  76. /** Returns the application's name. */
  77. virtual const String getApplicationName() = 0;
  78. /** Returns the application's version number. */
  79. virtual const String getApplicationVersion() = 0;
  80. /** Checks whether multiple instances of the app are allowed.
  81. If you application class returns true for this, more than one instance is
  82. permitted to run (except on the Mac where this isn't possible).
  83. If it's false, the second instance won't start, but it you will still get a
  84. callback to anotherInstanceStarted() to tell you about this - which
  85. gives you a chance to react to what the user was trying to do.
  86. */
  87. virtual bool moreThanOneInstanceAllowed() = 0;
  88. /** Called when the application starts.
  89. This will be called once to let the application do whatever initialisation
  90. it needs, create its windows, etc.
  91. After the method returns, the normal event-dispatch loop will be run,
  92. until the quit() method is called, at which point the shutdown()
  93. method will be called to let the application clear up anything it needs
  94. to delete.
  95. If during the initialise() method, the application decides not to start-up
  96. after all, it can just call the quit() method and the event loop won't be run.
  97. @param commandLineParameters the line passed in does not include the name of
  98. the executable, just the parameter list. To get the
  99. parameters as an array, you can call
  100. JUCEApplication::getCommandLineParameters()
  101. @see shutdown, quit
  102. */
  103. virtual void initialise (const String& commandLineParameters) = 0;
  104. /* Called to allow the application to clear up before exiting.
  105. After JUCEApplication::quit() has been called, the event-dispatch loop will
  106. terminate, and this method will get called to allow the app to sort itself
  107. out.
  108. Be careful that nothing happens in this method that might rely on messages
  109. being sent, or any kind of window activity, because the message loop is no
  110. longer running at this point.
  111. @see DeletedAtShutdown
  112. */
  113. virtual void shutdown() = 0;
  114. /** Indicates that the user has tried to start up another instance of the app.
  115. This will get called even if moreThanOneInstanceAllowed() is false.
  116. */
  117. virtual void anotherInstanceStarted (const String& commandLine) = 0;
  118. /** Called when the operating system is trying to close the application.
  119. The default implementation of this method is to call quit(), but it may
  120. be overloaded to ignore the request or do some other special behaviour
  121. instead. For example, you might want to offer the user the chance to save
  122. their changes before quitting, and give them the chance to cancel.
  123. If you want to send a quit signal to your app, this is the correct method
  124. to call, because it means that requests that come from the system get handled
  125. in the same way as those from your own application code. So e.g. you'd
  126. call this method from a "quit" item on a menu bar.
  127. */
  128. virtual void systemRequestedQuit() = 0;
  129. /** This method is called when the application is being put into background mode
  130. by the operating system.
  131. */
  132. virtual void suspended() = 0;
  133. /** This method is called when the application is being woken from background mode
  134. by the operating system.
  135. */
  136. virtual void resumed() = 0;
  137. /** If any unhandled exceptions make it through to the message dispatch loop, this
  138. callback will be triggered, in case you want to log them or do some other
  139. type of error-handling.
  140. If the type of exception is derived from the std::exception class, the pointer
  141. passed-in will be valid. If the exception is of unknown type, this pointer
  142. will be null.
  143. */
  144. virtual void unhandledException (const std::exception*,
  145. const String& sourceFilename,
  146. int lineNumber) = 0;
  147. //==============================================================================
  148. /** Signals that the main message loop should stop and the application should terminate.
  149. This isn't synchronous, it just posts a quit message to the main queue, and
  150. when this message arrives, the message loop will stop, the shutdown() method
  151. will be called, and the app will exit.
  152. Note that this will cause an unconditional quit to happen, so if you need an
  153. extra level before this, e.g. to give the user the chance to save their work
  154. and maybe cancel the quit, you'll need to handle this in the systemRequestedQuit()
  155. method - see that method's help for more info.
  156. @see MessageManager
  157. */
  158. static void quit();
  159. //==============================================================================
  160. /** Returns the application's command line parameters as a set of strings.
  161. @see getCommandLineParameters
  162. */
  163. static StringArray JUCE_CALLTYPE getCommandLineParameterArray();
  164. /** Returns the application's command line parameters as a single string.
  165. @see getCommandLineParameterArray
  166. */
  167. static String JUCE_CALLTYPE getCommandLineParameters();
  168. //==============================================================================
  169. /** Sets the value that should be returned as the application's exit code when the
  170. app quits.
  171. This is the value that's returned by the main() function. Normally you'd leave this
  172. as 0 unless you want to indicate an error code.
  173. @see getApplicationReturnValue
  174. */
  175. void setApplicationReturnValue (int newReturnValue) noexcept;
  176. /** Returns the value that has been set as the application's exit code.
  177. @see setApplicationReturnValue
  178. */
  179. int getApplicationReturnValue() const noexcept { return appReturnValue; }
  180. //==============================================================================
  181. /** Returns true if this executable is running as an app (as opposed to being a plugin
  182. or other kind of shared library. */
  183. static bool isStandaloneApp() noexcept { return createInstance != nullptr; }
  184. /** Returns true if the application hasn't yet completed its initialise() method
  185. and entered the main event loop.
  186. This is handy for things like splash screens to know when the app's up-and-running
  187. properly.
  188. */
  189. bool isInitialising() const noexcept { return stillInitialising; }
  190. //==============================================================================
  191. #ifndef DOXYGEN
  192. // The following methods are for internal use only...
  193. static int main();
  194. static int main (int argc, const char* argv[]);
  195. static void appWillTerminateByForce();
  196. typedef JUCEApplicationBase* (*CreateInstanceFunction)();
  197. static CreateInstanceFunction createInstance;
  198. virtual bool initialiseApp();
  199. int shutdownApp();
  200. static void JUCE_CALLTYPE sendUnhandledException (const std::exception*, const char* sourceFile, int lineNumber);
  201. bool sendCommandLineToPreexistingInstance();
  202. #endif
  203. private:
  204. //==============================================================================
  205. static JUCEApplicationBase* appInstance;
  206. int appReturnValue;
  207. bool stillInitialising;
  208. struct MultipleInstanceHandler;
  209. friend struct MultipleInstanceHandler;
  210. friend struct ContainerDeletePolicy<MultipleInstanceHandler>;
  211. ScopedPointer<MultipleInstanceHandler> multipleInstanceHandler;
  212. JUCE_DECLARE_NON_COPYABLE (JUCEApplicationBase)
  213. };
  214. #endif // JUCE_APPLICATIONBASE_H_INCLUDED