26 JUCEApplicationBase::CreateInstanceFunction JUCEApplicationBase::createInstance =
nullptr;
27 JUCEApplicationBase* JUCEApplicationBase::appInstance =
nullptr;
30 void* JUCEApplicationBase::iOSCustomDelegate =
nullptr;
33 JUCEApplicationBase::JUCEApplicationBase()
41 jassert (appInstance ==
this);
42 appInstance =
nullptr;
47 appReturnValue = newReturnValue;
51 void JUCEApplicationBase::appWillTerminateByForce()
56 const std::unique_ptr<JUCEApplicationBase> app (appInstance);
72 void JUCEApplicationBase::sendUnhandledException (
const std::exception*
const e,
73 const char*
const sourceFile,
84 app->unhandledException (e, sourceFile, lineNumber);
89 #if ! (JUCE_IOS || JUCE_ANDROID)
90 #define JUCE_HANDLE_MULTIPLE_INSTANCES 1
93 #if JUCE_HANDLE_MULTIPLE_INSTANCES
94 struct JUCEApplicationBase::MultipleInstanceHandler :
public ActionListener
96 MultipleInstanceHandler (
const String& appName)
97 : appLock (
"juceAppLock_" + appName)
101 bool sendCommandLineToPreexistingInstance()
103 if (appLock.enter (0))
116 void actionListenerCallback (
const String& message)
override
120 auto appName = app->getApplicationName();
122 if (message.startsWith (appName +
"/"))
123 app->anotherInstanceStarted (message.substring (appName.length() + 1));
128 InterProcessLock appLock;
130 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultipleInstanceHandler)
133 bool JUCEApplicationBase::sendCommandLineToPreexistingInstance()
135 jassert (multipleInstanceHandler ==
nullptr);
138 return multipleInstanceHandler->sendCommandLineToPreexistingInstance();
153 #if JUCE_WINDOWS && ! defined (_CONSOLE)
167 if (
auto argv = CommandLineToArgvW (GetCommandLineW(), &argc))
169 s = StringArray (argv + 1, argc - 1);
179 extern int juce_iOSMain (
int argc,
const char* argv[],
void* classPtr);
183 extern void initialiseNSApplication();
186 #if JUCE_LINUX && JUCE_MODULE_AVAILABLE_juce_gui_extra && (! defined(JUCE_WEB_BROWSER) || JUCE_WEB_BROWSER)
187 extern int juce_gtkWebkitMain (
int argc,
const char* argv[]);
191 const char*
const* juce_argv =
nullptr;
194 extern const char*
const* juce_argv;
195 extern int juce_argc;
202 for (
int i = 1; i < juce_argc; ++i)
204 String arg (juce_argv[i]);
206 if (arg.containsChar (
' ') && ! arg.isQuotedString())
207 arg = arg.quoted (
'"');
209 argString << arg <<
' ';
212 return argString.trim();
217 return StringArray (juce_argv + 1, juce_argc - 1);
220 int JUCEApplicationBase::main (
int argc,
const char* argv[])
228 initialiseNSApplication();
231 #if JUCE_LINUX && JUCE_MODULE_AVAILABLE_juce_gui_extra && (! defined(JUCE_WEB_BROWSER) || JUCE_WEB_BROWSER)
232 if (argc >= 2 && String (argv[1]) ==
"--juce-gtkwebkitfork-child")
233 return juce_gtkWebkitMain (argc, argv);
237 return juce_iOSMain (argc, argv, iOSCustomDelegate);
240 return JUCEApplicationBase::main();
248 int JUCEApplicationBase::main()
250 ScopedJuceInitialiser_GUI libraryInitialiser;
251 jassert (createInstance !=
nullptr);
253 const std::unique_ptr<JUCEApplicationBase> app (createInstance());
254 jassert (app !=
nullptr);
256 if (! app->initialiseApp())
257 return app->shutdownApp();
266 return app->shutdownApp();
272 bool JUCEApplicationBase::initialiseApp()
274 #if JUCE_HANDLE_MULTIPLE_INSTANCES
277 DBG (
"Another instance is running - quitting...");
282 #if JUCE_WINDOWS && JUCE_STANDALONE_APPLICATION && (! defined (_CONSOLE)) && (! JUCE_MINGW)
283 if (AttachConsole (ATTACH_PARENT_PROCESS) != 0)
290 if (_fileno(stdout) < 0) freopen_s (&ignore,
"CONOUT$",
"w", stdout);
291 if (_fileno(stderr) < 0) freopen_s (&ignore,
"CONOUT$",
"w", stderr);
292 if (_fileno(stdin) < 0) freopen_s (&ignore,
"CONIN$",
"r", stdin);
299 stillInitialising =
false;
304 #if JUCE_HANDLE_MULTIPLE_INSTANCES
305 if (
auto* mih = multipleInstanceHandler.get())
312 int JUCEApplicationBase::shutdownApp()
316 #if JUCE_HANDLE_MULTIPLE_INSTANCES
317 if (
auto* mih = multipleInstanceHandler.get())
328 multipleInstanceHandler.reset();
Wraps a pointer to a null-terminated UTF-16 character string, and provides various methods to operate...
static Type findEndOfToken(Type text, BreakType breakCharacters, Type quoteCharacters)
Returns a pointer to the first character in the string which is found in the breakCharacters string.
static void deleteAll()
Deletes all extant objects.
static bool isAbsolutePath(StringRef path)
Returns true if the string seems to be a fully-specified absolute path.
virtual ~JUCEApplicationBase()
Destructor.
virtual void initialise(const String &commandLineParameters)=0
Called when the application starts.
static JUCEApplicationBase * getInstance() noexcept
Returns the global instance of the application object that's running.
virtual const String getApplicationName()=0
Returns the application's name.
static String JUCE_CALLTYPE getCommandLineParameters()
Returns the application's command line parameters as a single string.
static void quit()
Signals that the main message loop should stop and the application should terminate.
static StringArray JUCE_CALLTYPE getCommandLineParameterArray()
Returns the application's command line parameters as a set of strings.
void setApplicationReturnValue(int newReturnValue) noexcept
Sets the value that should be returned as the application's exit code when the app quits.
static bool isStandaloneApp() noexcept
Returns true if this executable is running as an app (as opposed to being a plugin or other kind of s...
virtual bool moreThanOneInstanceAllowed()=0
Checks whether multiple instances of the app are allowed.
int getApplicationReturnValue() const noexcept
Returns the value that has been set as the application's exit code.
static void broadcastMessage(const String &messageText)
Sends a message to all other JUCE applications that are running.
void runDispatchLoop()
Runs the event dispatch loop until a stop message is posted.
void stopDispatchLoop()
Sends a signal that the dispatch loop should terminate.
void deregisterBroadcastListener(ActionListener *listener)
Deregisters a broadcast listener.
void registerBroadcastListener(ActionListener *listener)
Registers a listener to get told about broadcast messages.
static void deleteInstance()
Deletes the global MessageManager instance.
static MessageManager * getInstance()
Returns the global instance of the MessageManager.
A special array for holding a list of strings.