LMMS
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
38
44
45void JUCEApplicationBase::setApplicationReturnValue (const int newReturnValue) noexcept
46{
47 appReturnValue = newReturnValue;
48}
49
50// This is called on the Mac and iOS where the OS doesn't allow the stack to unwind on shutdown..
52{
54 {
55 {
56 const std::unique_ptr<JUCEApplicationBase> app (appInstance);
57
58 if (app != nullptr)
59 app->shutdownApp();
60 }
61
64 }
65}
66
68{
69 MessageManager::getInstance()->stopDispatchLoop();
70}
71
72void JUCEApplicationBase::sendUnhandledException (const std::exception* const e,
73 const char* const sourceFile,
74 const int lineNumber)
75{
76 if (auto* app = JUCEApplicationBase::getInstance())
77 {
78 // If you hit this assertion then the __FILE__ macro is providing a
79 // relative path instead of an absolute path. On Windows this will be
80 // a path relative to the build directory rather than the currently
81 // running application. To fix this you must compile with the /FC flag.
82 jassert (File::isAbsolutePath (sourceFile));
83
84 app->unhandledException (e, sourceFile, lineNumber);
85 }
86}
87
88//==============================================================================
89#if ! (JUCE_IOS || JUCE_ANDROID)
90 #define JUCE_HANDLE_MULTIPLE_INSTANCES 1
91#endif
92
93#if JUCE_HANDLE_MULTIPLE_INSTANCES
95{
97 : appLock ("juceAppLock_" + appName)
98 {
99 }
100
102 {
103 if (appLock.enter (0))
104 return false;
105
106 if (auto* app = JUCEApplicationBase::getInstance())
107 {
108 MessageManager::broadcastMessage (app->getApplicationName() + "/" + app->getCommandLineParameters());
109 return true;
110 }
111
113 return false;
114 }
115
116 void actionListenerCallback (const String& message) override
117 {
118 if (auto* app = JUCEApplicationBase::getInstance())
119 {
120 auto appName = app->getApplicationName();
121
122 if (message.startsWith (appName + "/"))
123 app->anotherInstanceStarted (message.substring (appName.length() + 1));
124 }
125 }
126
127private:
129
131};
132
134{
135 jassert (multipleInstanceHandler == nullptr); // this must only be called once!
136
138 return multipleInstanceHandler->sendCommandLineToPreexistingInstance();
139}
140
141#else
143#endif
144
145//==============================================================================
146#if JUCE_ANDROID
147
148StringArray JUCEApplicationBase::getCommandLineParameterArray() { return {}; }
149String JUCEApplicationBase::getCommandLineParameters() { return {}; }
150
151#else
152
153#if JUCE_WINDOWS && ! defined (_CONSOLE)
154
155String JUCE_CALLTYPE JUCEApplicationBase::getCommandLineParameters()
156{
157 return CharacterFunctions::findEndOfToken (CharPointer_UTF16 (GetCommandLineW()),
158 CharPointer_UTF16 (L" "),
159 CharPointer_UTF16 (L"\"")).findEndOfWhitespace();
160}
161
162StringArray JUCE_CALLTYPE JUCEApplicationBase::getCommandLineParameterArray()
163{
164 StringArray s;
165 int argc = 0;
166
167 if (auto argv = CommandLineToArgvW (GetCommandLineW(), &argc))
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 int juce_gtkWebkitMain (int argc, const char* 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{
200 String argString;
201
202 for (int i = 1; i < juce_argc; ++i)
203 {
205
206 if (arg.containsChar (' ') && ! arg.isQuotedString())
207 arg = arg.quoted ('"');
208
209 argString << arg << ' ';
210 }
211
212 return argString.trim();
213}
214
219
220int JUCEApplicationBase::main (int argc, const char* argv[])
221{
223 {
224 juce_argc = argc;
225 juce_argv = argv;
226
227 #if JUCE_MAC
228 initialiseNSApplication();
229 #endif
230
231 #if (JUCE_LINUX || JUCE_BSD) && 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);
234 #endif
235
236 #if JUCE_IOS && JUCE_MODULE_AVAILABLE_juce_gui_basics
237 return juce_iOSMain (argc, argv, iOSCustomDelegate);
238 #else
239
241 #endif
242 }
243}
244
245#endif
246
247//==============================================================================
249{
250 ScopedJuceInitialiser_GUI libraryInitialiser;
251 jassert (createInstance != nullptr);
252
253 const std::unique_ptr<JUCEApplicationBase> app (createInstance());
254 jassert (app != nullptr);
255
256 if (! app->initialiseApp())
257 return app->shutdownApp();
258
260 {
261 // loop until a quit message is received..
262 MessageManager::getInstance()->runDispatchLoop();
263 }
265
266 return app->shutdownApp();
267}
268
269#endif
270
271//==============================================================================
273{
274 #if JUCE_HANDLE_MULTIPLE_INSTANCES
276 {
277 DBG ("Another instance is running - quitting...");
278 return false;
279 }
280 #endif
281
282 #if JUCE_WINDOWS && JUCE_STANDALONE_APPLICATION && (! defined (_CONSOLE)) && (! JUCE_MINGW)
283 if (AttachConsole (ATTACH_PARENT_PROCESS) != 0)
284 {
285 // if we've launched a GUI app from cmd.exe or PowerShell, we need this to enable printf etc.
286 // However, only reassign stdout, stderr, stdin if they have not been already opened by
287 // a redirect or similar.
288 FILE* ignore;
289
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);
293 }
294 #endif
295
296 // let the app do its setting-up..
298
299 stillInitialising = false;
300
301 if (MessageManager::getInstance()->hasStopMessageBeenSent())
302 return false;
303
304 #if JUCE_HANDLE_MULTIPLE_INSTANCES
305 if (auto* mih = multipleInstanceHandler.get())
306 MessageManager::getInstance()->registerBroadcastListener (mih);
307 #endif
308
309 return true;
310}
311
313{
315
316 #if JUCE_HANDLE_MULTIPLE_INSTANCES
317 if (auto* mih = multipleInstanceHandler.get())
318 MessageManager::getInstance()->deregisterBroadcastListener (mih);
319 #endif
320
322 {
323 // give the app a chance to clean up..
324 shutdown();
325 }
327
330}
331
332} // namespace juce
static void shutdown(void)
Definition adplugdb.cpp:297
static void message(int level, const char *fmt,...)
Definition adplugdb.cpp:120
Definition StringArray.h:41
Definition juce_ActionListener.h:35
Definition juce_CharPointer_UTF8.h:35
static void deleteAll()
Definition juce_DeletedAtShutdown.cpp:50
static bool isAbsolutePath(StringRef path)
Definition juce_File.cpp:400
Definition juce_InterProcessLock.h:35
static JUCEApplicationBase * appInstance
Definition juce_ApplicationBase.h:300
virtual ~JUCEApplicationBase()
Definition juce_ApplicationBase.cpp:39
virtual void initialise(const String &commandLineParameters)=0
virtual const String getApplicationName()=0
static CreateInstanceFunction createInstance
Definition juce_ApplicationBase.h:286
static void appWillTerminateByForce()
Definition juce_ApplicationBase.cpp:51
virtual bool initialiseApp()
Definition juce_ApplicationBase.cpp:272
static String JUCE_CALLTYPE getCommandLineParameters()
Definition juce_ApplicationBase.cpp:198
int shutdownApp()
Definition juce_ApplicationBase.cpp:312
static int main()
Definition juce_ApplicationBase.cpp:248
std::unique_ptr< MultipleInstanceHandler > multipleInstanceHandler
Definition juce_ApplicationBase.h:305
bool stillInitialising
Definition juce_ApplicationBase.h:302
static void quit()
Definition juce_ApplicationBase.cpp:67
JUCEApplicationBase()
Definition juce_ApplicationBase.cpp:33
static StringArray JUCE_CALLTYPE getCommandLineParameterArray()
Definition juce_ApplicationBase.cpp:215
void setApplicationReturnValue(int newReturnValue) noexcept
Definition juce_ApplicationBase.cpp:45
bool sendCommandLineToPreexistingInstance()
Definition juce_ApplicationBase.cpp:133
static void JUCE_CALLTYPE sendUnhandledException(const std::exception *, const char *sourceFile, int lineNumber)
Definition juce_ApplicationBase.cpp:72
static JUCEApplicationBase * getInstance() noexcept
Definition juce_ApplicationBase.h:96
static bool isStandaloneApp() noexcept
Definition juce_ApplicationBase.h:267
virtual bool moreThanOneInstanceAllowed()=0
int appReturnValue
Definition juce_ApplicationBase.h:301
int getApplicationReturnValue() const noexcept
Definition juce_ApplicationBase.h:262
static void broadcastMessage(const String &messageText)
Definition juce_linux_Messaging.cpp:327
static void deleteInstance()
Definition juce_MessageManager.cpp:63
static MessageManager * getInstance()
Definition juce_MessageManager.cpp:47
Definition juce_Initialisation.h:70
Definition juce_StringArray.h:35
Definition juce_String.h:53
String trim() const
Definition juce_String.cpp:1656
* e
Definition inflate.c:1404
register unsigned i
Definition inflate.c:1575
unsigned s
Definition inflate.c:1555
char * argv[]
Definition unzip.c:738
static bool ignore
Definition pugl.h:1696
#define JUCE_TRY
Definition juce_ApplicationBase.h:329
#define JUCE_CATCH_EXCEPTION
Definition juce_ApplicationBase.h:330
#define JUCE_AUTORELEASEPOOL
Definition juce_Memory.h:158
#define jassert(expression)
#define DBG(textToWrite)
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
#define jassertfalse
#define JUCE_CALLTYPE
Definition carla_juce.cpp:31
jack_client_t client jack_client_t client jack_client_t client jack_client_t JackInfoShutdownCallback void * arg
Definition juce_linux_JackAudio.cpp:63
int juce_argc
Definition juce_linux_Files.cpp:105
int juce_gtkWebkitMain(int argc, const char *argv[])
Definition juce_linux_X11_WebBrowserComponent.cpp:1013
const char *const * juce_argv
Definition juce_linux_Files.cpp:104
Definition juce_ApplicationBase.cpp:95
void actionListenerCallback(const String &message) override
Definition juce_ApplicationBase.cpp:116
MultipleInstanceHandler(const String &appName)
Definition juce_ApplicationBase.cpp:96
InterProcessLock appLock
Definition juce_ApplicationBase.cpp:128
bool sendCommandLineToPreexistingInstance()
Definition juce_ApplicationBase.cpp:101
int argc
Definition zipinfo.c:455