JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_ApplicationBase.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26JUCEApplicationBase::CreateInstanceFunction JUCEApplicationBase::createInstance = nullptr;
27JUCEApplicationBase* JUCEApplicationBase::appInstance = nullptr;
28
29#if JUCE_IOS
30void* JUCEApplicationBase::iOSCustomDelegate = nullptr;
31#endif
32
34{
35 appReturnValue = newReturnValue;
36}
37
38// This is called on the Mac and iOS where the OS doesn't allow the stack to unwind on shutdown..
39void JUCEApplicationBase::appWillTerminateByForce()
40{
42 {
43 {
44 const std::unique_ptr<JUCEApplicationBase> app (appInstance);
45
46 if (app != nullptr)
47 app->shutdownApp();
48 }
49
52 }
53}
54
59
60void JUCEApplicationBase::sendUnhandledException (const std::exception* const e,
61 const char* const sourceFile,
62 const int lineNumber)
63{
65 {
66 // If you hit this assertion then the __FILE__ macro is providing a
67 // relative path instead of an absolute path. On Windows this will be
68 // a path relative to the build directory rather than the currently
69 // running application. To fix this you must compile with the /FC flag.
71
72 app->unhandledException (e, sourceFile, lineNumber);
73 }
74}
75
76//==============================================================================
77#if ! (JUCE_IOS || JUCE_ANDROID)
78// #define JUCE_HANDLE_MULTIPLE_INSTANCES 1
79#endif
80
81#if JUCE_HANDLE_MULTIPLE_INSTANCES
82struct JUCEApplicationBase::MultipleInstanceHandler final : public ActionListener
83{
84 MultipleInstanceHandler (const String& appName)
85 : appLock ("juceAppLock_" + appName)
86 {
87 }
88
90 {
91 if (appLock.enter (0))
92 return false;
93
95 {
96 MessageManager::broadcastMessage (app->getApplicationName() + "/" + app->getCommandLineParameters());
97 return true;
98 }
99
101 return false;
102 }
103
104 void actionListenerCallback (const String& message) override
105 {
107 {
108 auto appName = app->getApplicationName();
109
110 if (message.startsWith (appName + "/"))
111 app->anotherInstanceStarted (message.substring (appName.length() + 1));
112 }
113 }
114
115private:
116 InterProcessLock appLock;
117
118 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultipleInstanceHandler)
119};
120
121bool JUCEApplicationBase::sendCommandLineToPreexistingInstance()
122{
123 jassert (multipleInstanceHandler == nullptr); // this must only be called once!
124
125 multipleInstanceHandler.reset (new MultipleInstanceHandler (getApplicationName()));
126 return multipleInstanceHandler->sendCommandLineToPreexistingInstance();
127}
128
129#else
131#endif
132
133JUCEApplicationBase::JUCEApplicationBase()
134{
135 jassert (isStandaloneApp() && appInstance == nullptr);
136 appInstance = this;
137}
138
140{
141 jassert (appInstance == this);
142 appInstance = nullptr;
143}
144
145//==============================================================================
146#if JUCE_ANDROID
147
150
151#else
152
153#if JUCE_WINDOWS && ! defined (_CONSOLE)
154
156{
157 return CharacterFunctions::findEndOfToken (CharPointer_UTF16 (GetCommandLineW()),
158 CharPointer_UTF16 (L" "),
159 CharPointer_UTF16 (L"\"")).findEndOfWhitespace();
160}
161
163{
164 StringArray s;
165 int argc = 0;
166
168 {
169 s = StringArray (argv + 1, argc - 1);
170 LocalFree (argv);
171 }
172
173 return s;
174}
175
176#else
177
178#if JUCE_IOS && JUCE_MODULE_AVAILABLE_juce_gui_basics
179 extern int juce_iOSMain (int argc, const char* argv[], void* classPtr);
180#endif
181
182#if JUCE_MAC
183 extern void initialiseNSApplication();
184#endif
185
186#if (JUCE_LINUX || JUCE_BSD) && JUCE_MODULE_AVAILABLE_juce_gui_extra && (! defined (JUCE_WEB_BROWSER) || JUCE_WEB_BROWSER)
187 extern "C" int juce_gtkWebkitMain (int argc, const char* const* argv);
188#endif
189
190#if JUCE_WINDOWS
191 const char* const* juce_argv = nullptr;
192 int juce_argc = 0;
193#else
194 extern const char* const* juce_argv; // declared in juce_core
195 extern int juce_argc;
196#endif
197
199{
201
202 for (const auto& arg : getCommandLineParameterArray())
203 {
204 const auto withQuotes = arg.containsChar (' ') && ! arg.isQuotedString()
205 ? arg.quoted ('"')
206 : arg;
207 argString << withQuotes << ' ';
208 }
209
210 return argString.trim();
211}
212
214{
215 StringArray result;
216
217 for (int i = 1; i < juce_argc; ++i)
218 result.add (CharPointer_UTF8 (juce_argv[i]));
219
220 return result;
221}
222
223int JUCEApplicationBase::main (int argc, const char* argv[])
224{
226 {
227 juce_argc = argc;
228 juce_argv = argv;
229
230 #if JUCE_MAC
232 #endif
233
234 #if (JUCE_LINUX || JUCE_BSD) && JUCE_MODULE_AVAILABLE_juce_gui_extra && (! defined (JUCE_WEB_BROWSER) || JUCE_WEB_BROWSER)
235 if (argc >= 2 && String (argv[1]) == "--juce-gtkwebkitfork-child")
236 return juce_gtkWebkitMain (argc, argv);
237 #endif
238
239 #if JUCE_IOS && JUCE_MODULE_AVAILABLE_juce_gui_basics
241 #else
242
243 return JUCEApplicationBase::main();
244 #endif
245 }
246}
247
248#endif
249
250//==============================================================================
251int JUCEApplicationBase::main()
252{
253 ScopedJuceInitialiser_GUI libraryInitialiser;
254 jassert (createInstance != nullptr);
255
256 const std::unique_ptr<JUCEApplicationBase> app (createInstance());
257 jassert (app != nullptr);
258
259 if (! app->initialiseApp())
260 return app->shutdownApp();
261
263 {
264 // loop until a quit message is received..
266 }
268
269 return app->shutdownApp();
270}
271
272#endif
273
274//==============================================================================
275bool JUCEApplicationBase::initialiseApp()
276{
277 #if JUCE_HANDLE_MULTIPLE_INSTANCES
279 {
280 DBG ("Another instance is running - quitting...");
281 return false;
282 }
283 #endif
284
285 #if JUCE_WINDOWS && (! defined (_CONSOLE)) && (! JUCE_MINGW)
287 {
288 // if we've launched a GUI app from cmd.exe or PowerShell, we need this to enable printf etc.
289 // However, only reassign stdout, stderr, stdin if they have not been already opened by
290 // a redirect or similar.
291 FILE* ignore;
292
293 if (_fileno (stdout) < 0) freopen_s (&ignore, "CONOUT$", "w", stdout);
294 if (_fileno (stderr) < 0) freopen_s (&ignore, "CONOUT$", "w", stderr);
295 if (_fileno (stdin) < 0) freopen_s (&ignore, "CONIN$", "r", stdin);
296 }
297 #endif
298
299 // let the app do its setting-up..
301
302 stillInitialising = false;
303
304 if (MessageManager::getInstance()->hasStopMessageBeenSent())
305 return false;
306
307 #if JUCE_HANDLE_MULTIPLE_INSTANCES
308 if (auto* mih = multipleInstanceHandler.get())
310 #endif
311
312 return true;
313}
314
315int JUCEApplicationBase::shutdownApp()
316{
318
319 #if JUCE_HANDLE_MULTIPLE_INSTANCES
320 if (auto* mih = multipleInstanceHandler.get())
322 #endif
323
325 {
326 // give the app a chance to clean up..
327 shutdown();
328 }
330
331 multipleInstanceHandler.reset();
333}
334
335} // namespace juce
Wraps a pointer to a null-terminated UTF-8 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.
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 JUCEApplicationBase * getInstance() noexcept
Returns the global instance of the application object that's running.
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.
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.
static void broadcastMessage(const String &messageText)
Sends a message to all other JUCE applications that are running.
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.
void add(String stringToAdd)
Appends a string at the end of the array.
The JUCE String class!
Definition juce_String.h:53
bool containsChar(juce_wchar character) const noexcept
Tests whether the string contains a particular character.
#define JUCE_TRY
The JUCE_TRY/JUCE_CATCH_EXCEPTION wrappers can be used to pass any uncaught exceptions to the JUCEApp...
#define JUCE_CATCH_EXCEPTION
The JUCE_TRY/JUCE_CATCH_EXCEPTION wrappers can be used to pass any uncaught exceptions to the JUCEApp...
#define JUCE_AUTORELEASEPOOL
A macro that can be used to easily declare a local ScopedAutoReleasePool object for RAII-based obj-C ...
#define jassert(expression)
Platform-independent assertion macro.
#define DBG(textToWrite)
Writes a string to the standard error stream.
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
This is a shorthand way of writing both a JUCE_DECLARE_NON_COPYABLE and JUCE_LEAK_DETECTOR macro for ...
#define jassertfalse
This will always cause an assertion failure.
#define JUCE_CALLTYPE
This macro defines the C calling convention used as the standard for JUCE calls.
JUCE Namespace.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88