Loading...
Searching...
No Matches
juce_ApplicationBase.cpp
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2022 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
To use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
JUCEApplicationBase::CreateInstanceFunction JUCEApplicationBase::createInstance = nullptr;
JUCEApplicationBase* JUCEApplicationBase::appInstance = nullptr;
#if JUCE_IOS
void* JUCEApplicationBase::iOSCustomDelegate = nullptr;
#endif
JUCEApplicationBase::JUCEApplicationBase()
{
jassert (isStandaloneApp() && appInstance == nullptr);
appInstance = this;
}
JUCEApplicationBase::~JUCEApplicationBase()
{
jassert (appInstance == this);
appInstance = nullptr;
}
void JUCEApplicationBase::setApplicationReturnValue (const int newReturnValue) noexcept
{
appReturnValue = newReturnValue;
}
// This is called on the Mac and iOS where the OS doesn't allow the stack to unwind on shutdown..
void JUCEApplicationBase::appWillTerminateByForce()
{
JUCE_AUTORELEASEPOOL
{
{
const std::unique_ptr<JUCEApplicationBase> app (appInstance);
if (app != nullptr)
app->shutdownApp();
}
DeletedAtShutdown::deleteAll();
MessageManager::deleteInstance();
}
}
void JUCEApplicationBase::quit()
{
MessageManager::getInstance()->stopDispatchLoop();
}
void JUCEApplicationBase::sendUnhandledException (const std::exception* const e,
const char* const sourceFile,
const int lineNumber)
{
if (auto* app = JUCEApplicationBase::getInstance())
{
// If you hit this assertion then the __FILE__ macro is providing a
// relative path instead of an absolute path. On Windows this will be
// a path relative to the build directory rather than the currently
// running application. To fix this you must compile with the /FC flag.
jassert (File::isAbsolutePath (sourceFile));
app->unhandledException (e, sourceFile, lineNumber);
}
}
//==============================================================================
#if ! (JUCE_IOS || JUCE_ANDROID)
#define JUCE_HANDLE_MULTIPLE_INSTANCES 1
#endif
#if JUCE_HANDLE_MULTIPLE_INSTANCES
struct JUCEApplicationBase::MultipleInstanceHandler final : public ActionListener
{
MultipleInstanceHandler (const String& appName)
: appLock ("juceAppLock_" + appName)
{
}
bool sendCommandLineToPreexistingInstance()
{
if (appLock.enter (0))
return false;
if (auto* app = JUCEApplicationBase::getInstance())
{
MessageManager::broadcastMessage (app->getApplicationName() + "/" + app->getCommandLineParameters());
return true;
}
jassertfalse;
return false;
}
void actionListenerCallback (const String& message) override
{
if (auto* app = JUCEApplicationBase::getInstance())
{
auto appName = app->getApplicationName();
if (message.startsWith (appName + "/"))
app->anotherInstanceStarted (message.substring (appName.length() + 1));
}
}
private:
InterProcessLock appLock;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultipleInstanceHandler)
};
bool JUCEApplicationBase::sendCommandLineToPreexistingInstance()
{
jassert (multipleInstanceHandler == nullptr); // this must only be called once!
multipleInstanceHandler.reset (new MultipleInstanceHandler (getApplicationName()));
return multipleInstanceHandler->sendCommandLineToPreexistingInstance();
}
#else
struct JUCEApplicationBase::MultipleInstanceHandler {};
#endif
//==============================================================================
#if JUCE_ANDROID
StringArray JUCEApplicationBase::getCommandLineParameterArray() { return {}; }
String JUCEApplicationBase::getCommandLineParameters() { return {}; }
#else
#if JUCE_WINDOWS && ! defined (_CONSOLE)
String JUCE_CALLTYPE JUCEApplicationBase::getCommandLineParameters()
{
return CharacterFunctions::findEndOfToken (CharPointer_UTF16 (GetCommandLineW()),
CharPointer_UTF16 (L" "),
CharPointer_UTF16 (L"\"")).findEndOfWhitespace();
}
StringArray JUCE_CALLTYPE JUCEApplicationBase::getCommandLineParameterArray()
{
StringArray s;
int argc = 0;
if (auto argv = CommandLineToArgvW (GetCommandLineW(), &argc))
{
s = StringArray (argv + 1, argc - 1);
LocalFree (argv);
}
return s;
}
#else
#if JUCE_IOS && JUCE_MODULE_AVAILABLE_juce_gui_basics
extern int juce_iOSMain (int argc, const char* argv[], void* classPtr);
#endif
#if JUCE_MAC
extern void initialiseNSApplication();
#endif
#if (JUCE_LINUX || JUCE_BSD) && JUCE_MODULE_AVAILABLE_juce_gui_extra && (! defined (JUCE_WEB_BROWSER) || JUCE_WEB_BROWSER)
extern "C" int juce_gtkWebkitMain (int argc, const char* const* argv);
#endif
#if JUCE_WINDOWS
const char* const* juce_argv = nullptr;
int juce_argc = 0;
#else
extern const char* const* juce_argv; // declared in juce_core
extern int juce_argc;
#endif
String JUCEApplicationBase::getCommandLineParameters()
{
String argString;
for (const auto& arg : getCommandLineParameterArray())
{
const auto withQuotes = arg.containsChar (' ') && ! arg.isQuotedString()
? arg.quoted ('"')
: arg;
argString << withQuotes << ' ';
}
return argString.trim();
}
StringArray JUCEApplicationBase::getCommandLineParameterArray()
{
StringArray result;
for (int i = 1; i < juce_argc; ++i)
result.add (CharPointer_UTF8 (juce_argv[i]));
return result;
}
int JUCEApplicationBase::main (int argc, const char* argv[])
{
JUCE_AUTORELEASEPOOL
{
juce_argc = argc;
juce_argv = argv;
#if JUCE_MAC
initialiseNSApplication();
#endif
#if (JUCE_LINUX || JUCE_BSD) && JUCE_MODULE_AVAILABLE_juce_gui_extra && (! defined (JUCE_WEB_BROWSER) || JUCE_WEB_BROWSER)
if (argc >= 2 && String (argv[1]) == "--juce-gtkwebkitfork-child")
return juce_gtkWebkitMain (argc, argv);
#endif
#if JUCE_IOS && JUCE_MODULE_AVAILABLE_juce_gui_basics
return juce_iOSMain (argc, argv, iOSCustomDelegate);
#else
return JUCEApplicationBase::main();
#endif
}
}
#endif
//==============================================================================
int JUCEApplicationBase::main()
{
ScopedJuceInitialiser_GUI libraryInitialiser;
jassert (createInstance != nullptr);
const std::unique_ptr<JUCEApplicationBase> app (createInstance());
jassert (app != nullptr);
if (! app->initialiseApp())
return app->shutdownApp();
JUCE_TRY
{
// loop until a quit message is received..
MessageManager::getInstance()->runDispatchLoop();
}
JUCE_CATCH_EXCEPTION
return app->shutdownApp();
}
#endif
//==============================================================================
bool JUCEApplicationBase::initialiseApp()
{
#if JUCE_HANDLE_MULTIPLE_INSTANCES
if ((! moreThanOneInstanceAllowed()) && sendCommandLineToPreexistingInstance())
{
DBG ("Another instance is running - quitting...");
return false;
}
#endif
#if JUCE_WINDOWS && (! defined (_CONSOLE)) && (! JUCE_MINGW)
if (isStandaloneApp() && AttachConsole (ATTACH_PARENT_PROCESS) != 0)
{
// if we've launched a GUI app from cmd.exe or PowerShell, we need this to enable printf etc.
// However, only reassign stdout, stderr, stdin if they have not been already opened by
// a redirect or similar.
FILE* ignore;
if (_fileno (stdout) < 0) freopen_s (&ignore, "CONOUT$", "w", stdout);
if (_fileno (stderr) < 0) freopen_s (&ignore, "CONOUT$", "w", stderr);
if (_fileno (stdin) < 0) freopen_s (&ignore, "CONIN$", "r", stdin);
}
#endif
// let the app do its setting-up..
initialise (getCommandLineParameters());
stillInitialising = false;
if (MessageManager::getInstance()->hasStopMessageBeenSent())
return false;
#if JUCE_HANDLE_MULTIPLE_INSTANCES
if (auto* mih = multipleInstanceHandler.get())
MessageManager::getInstance()->registerBroadcastListener (mih);
#endif
return true;
}
int JUCEApplicationBase::shutdownApp()
{
jassert (JUCEApplicationBase::getInstance() == this);
#if JUCE_HANDLE_MULTIPLE_INSTANCES
if (auto* mih = multipleInstanceHandler.get())
MessageManager::getInstance()->deregisterBroadcastListener (mih);
#endif
JUCE_TRY
{
// give the app a chance to clean up..
shutdown();
}
JUCE_CATCH_EXCEPTION
multipleInstanceHandler.reset();
return getApplicationReturnValue();
}
} // namespace juce
juce::ActionListener
Definition juce_ActionListener.h:35
juce::CharPointer_UTF8
Definition juce_CharPointer_UTF8.h:35
juce::CharacterFunctions::findEndOfToken
static Type findEndOfToken(Type text, BreakType breakCharacters, Type quoteCharacters)
Definition juce_CharacterFunctions.h:828
juce::DeletedAtShutdown::deleteAll
static void deleteAll()
Definition juce_DeletedAtShutdown.cpp:50
juce::File::isAbsolutePath
static bool isAbsolutePath(StringRef path)
Definition juce_File.cpp:408
juce::JUCEApplicationBase
Definition juce_ApplicationBase.h:85
juce::JUCEApplicationBase::~JUCEApplicationBase
virtual ~JUCEApplicationBase()
Definition juce_ApplicationBase.cpp:39
juce::JUCEApplicationBase::initialise
virtual void initialise(const String &commandLineParameters)=0
juce::JUCEApplicationBase::getApplicationName
virtual const String getApplicationName()=0
juce::JUCEApplicationBase::getCommandLineParameters
static String JUCE_CALLTYPE getCommandLineParameters()
Definition juce_ApplicationBase.cpp:198
juce::JUCEApplicationBase::quit
static void quit()
Definition juce_ApplicationBase.cpp:67
juce::JUCEApplicationBase::getCommandLineParameterArray
static StringArray JUCE_CALLTYPE getCommandLineParameterArray()
Definition juce_ApplicationBase.cpp:213
juce::JUCEApplicationBase::setApplicationReturnValue
void setApplicationReturnValue(int newReturnValue) noexcept
Definition juce_ApplicationBase.cpp:45
juce::JUCEApplicationBase::getInstance
static JUCEApplicationBase * getInstance() noexcept
Definition juce_ApplicationBase.h:96
juce::JUCEApplicationBase::isStandaloneApp
static bool isStandaloneApp() noexcept
Definition juce_ApplicationBase.h:267
juce::JUCEApplicationBase::moreThanOneInstanceAllowed
virtual bool moreThanOneInstanceAllowed()=0
juce::JUCEApplicationBase::getApplicationReturnValue
int getApplicationReturnValue() const noexcept
Definition juce_ApplicationBase.h:262
juce::MessageManager::broadcastMessage
static void broadcastMessage(const String &messageText)
juce::MessageManager::runDispatchLoop
void runDispatchLoop()
Definition juce_MessageManager.cpp:104
juce::MessageManager::stopDispatchLoop
void stopDispatchLoop()
Definition juce_MessageManager.cpp:119
juce::MessageManager::deregisterBroadcastListener
void deregisterBroadcastListener(ActionListener *listener)
Definition juce_MessageManager.cpp:222
juce::MessageManager::registerBroadcastListener
void registerBroadcastListener(ActionListener *listener)
Definition juce_MessageManager.cpp:214
juce::MessageManager::deleteInstance
static void deleteInstance()
Definition juce_MessageManager.cpp:63
juce::MessageManager::getInstance
static MessageManager * getInstance()
Definition juce_MessageManager.cpp:47
juce::StringArray
Definition juce_StringArray.h:35
juce::StringArray::add
void add(String stringToAdd)
Definition juce_StringArray.cpp:136
juce::String
Definition juce_String.h:53
juce::String::trim
String trim() const
Definition juce_String.cpp:1656