Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 : #include "baseapplication.h"
23 :
24 : #include "global/types/version.h"
25 : #include "global/globalmodule.h"
26 : #include "global/async/processevents.h"
27 :
28 : #include "muse_framework_config.h"
29 :
30 : #ifndef NO_QT_SUPPORT
31 : #include <QApplication>
32 : #endif
33 :
34 : #ifdef QT_QPROCESS_SUPPORTED
35 : #include <QProcess>
36 : #endif
37 :
38 : #ifdef QT_CONCURRENT_SUPPORTED
39 : #include <QThreadPool>
40 : #endif
41 :
42 : #include "log.h"
43 :
44 : using namespace muse;
45 :
46 96 : String BaseApplication::appName()
47 : {
48 : #ifdef MUSE_APP_NAME_MACHINE_READABLE
49 96 : return String::fromAscii(MUSE_APP_NAME_MACHINE_READABLE);
50 : #else
51 : return String();
52 : #endif
53 : }
54 :
55 0 : String BaseApplication::appTitle()
56 : {
57 : #ifdef MUSE_APP_NAME_HUMAN_READABLE
58 : #ifdef MUSE_APP_UNSTABLE
59 0 : return String::fromAscii(MUSE_APP_NAME_HUMAN_READABLE) + u" Development";
60 : #else
61 : return String::fromAscii(MUSE_APP_NAME_HUMAN_READABLE);
62 : #endif
63 : #else
64 : return String();
65 : #endif
66 : }
67 :
68 0 : bool BaseApplication::appUnstable()
69 : {
70 : #ifdef MUSE_APP_UNSTABLE
71 0 : return true;
72 : #else
73 : return false;
74 : #endif
75 : }
76 :
77 0 : Version BaseApplication::appVersion()
78 : {
79 : #ifdef MUSE_APP_VERSION
80 0 : static Version v(MUSE_APP_VERSION);
81 : #else
82 : static Version v("0.0.0");
83 : #endif
84 0 : return v;
85 : }
86 :
87 0 : Version BaseApplication::appFullVersion()
88 : {
89 0 : static Version fv(appVersion());
90 :
91 : #ifdef MUSE_APP_VERSION_LABEL
92 : static bool once = false;
93 : if (!once) {
94 : String versionLabel = String::fromAscii(MUSE_APP_VERSION_LABEL);
95 : if (!versionLabel.isEmpty()) {
96 : fv.setSuffix(versionLabel);
97 : }
98 : once = true;
99 : }
100 : #endif
101 :
102 0 : return fv;
103 : }
104 :
105 0 : String BaseApplication::appBuild()
106 : {
107 : #ifdef MUSE_APP_BUILD_NUMBER
108 0 : return String::fromAscii(MUSE_APP_BUILD_NUMBER);
109 : #else
110 : return String();
111 : #endif
112 : }
113 :
114 0 : String BaseApplication::appRevision()
115 : {
116 : #ifdef MUSE_APP_REVISION
117 0 : return String::fromAscii(MUSE_APP_REVISION);
118 : #else
119 : return String();
120 : #endif
121 : }
122 :
123 0 : BaseApplication::BaseApplication(const std::shared_ptr<CmdOptions>& appOptions)
124 0 : : m_appOptions(appOptions)
125 : {
126 0 : }
127 :
128 0 : void BaseApplication::setRunMode(const RunMode& mode)
129 : {
130 0 : m_runMode = mode;
131 0 : }
132 :
133 0 : IApplication::RunMode BaseApplication::runMode() const
134 : {
135 0 : return m_runMode;
136 : }
137 :
138 0 : bool BaseApplication::noGui() const
139 : {
140 0 : switch (m_runMode) {
141 0 : case RunMode::GuiApp: return false;
142 0 : case RunMode::ConsoleApp: return true;
143 0 : case RunMode::AudioPluginRegistration: return true;
144 : }
145 0 : return false;
146 : }
147 :
148 0 : void BaseApplication::addModule(muse::modularity::IModuleSetup* module)
149 : {
150 0 : m_modules.push_back(module);
151 0 : }
152 :
153 0 : void BaseApplication::setup()
154 : {
155 0 : doSetup(m_appOptions);
156 0 : }
157 :
158 0 : void BaseApplication::doSetup(const std::shared_ptr<CmdOptions>& options)
159 : {
160 0 : IF_ASSERT_FAILED(options) {
161 0 : return;
162 : }
163 :
164 0 : const IApplication::RunMode runMode = options->runMode;
165 :
166 0 : setRunMode(runMode);
167 :
168 : // ====================================================
169 : // Setup modules: Resources, Exports, Imports, UiTypes
170 : // ====================================================
171 :
172 0 : modularity::globalIoc()->registerExport<IApplication>("global", shared_from_this());
173 :
174 0 : m_globalModule = new GlobalModule();
175 0 : m_globalModule->registerResources();
176 0 : m_globalModule->registerExports();
177 0 : m_globalModule->registerUiTypes();
178 :
179 0 : for (modularity::IModuleSetup* m : m_modules) {
180 0 : m->registerResources();
181 0 : m->registerExports();
182 : }
183 :
184 0 : m_globalModule->resolveImports();
185 0 : m_globalModule->registerApi();
186 0 : for (modularity::IModuleSetup* m : m_modules) {
187 0 : m->registerUiTypes();
188 0 : m->resolveImports();
189 : }
190 :
191 : // ====================================================
192 : // Setup modules: apply the command line options
193 : // ====================================================
194 0 : applyCommandLineOptions(options);
195 :
196 : // ====================================================
197 : // Setup modules: onPreInit
198 : // ====================================================
199 0 : m_globalModule->onPreInit(runMode);
200 0 : for (modularity::IModuleSetup* m : m_modules) {
201 0 : m->onPreInit(runMode);
202 : }
203 :
204 : // ====================================================
205 : // Setup modules: onInit
206 : // ====================================================
207 0 : m_globalModule->onInit(runMode);
208 0 : for (modularity::IModuleSetup* m : m_modules) {
209 0 : m->onInit(runMode);
210 0 : m->registerApi();
211 : }
212 :
213 : // ====================================================
214 : // Setup modules: onAllInited
215 : // ====================================================
216 0 : m_globalModule->onAllInited(runMode);
217 0 : for (modularity::IModuleSetup* m : m_modules) {
218 0 : m->onAllInited(runMode);
219 : }
220 :
221 : // ====================================================
222 : // Setup modules: onStartApp (on next event loop)
223 : // ====================================================
224 0 : QMetaObject::invokeMethod(qApp, [this]() {
225 0 : m_globalModule->onStartApp();
226 0 : for (modularity::IModuleSetup* m : m_modules) {
227 0 : m->onStartApp();
228 : }
229 0 : }, Qt::QueuedConnection);
230 : }
231 :
232 0 : void BaseApplication::applyCommandLineOptions(const std::shared_ptr<CmdOptions>& options)
233 : {
234 0 : if (options->global.loggerLevel) {
235 0 : m_globalModule->setLoggerLevel(options->global.loggerLevel.value());
236 : }
237 :
238 0 : if (options->global.userDataDir) {
239 0 : m_globalModule->setUserDataDir(options->global.userDataDir.value());
240 : }
241 0 : }
242 :
243 0 : BaseApplication::ContextData& BaseApplication::contextData(const muse::modularity::ContextPtr& ctx)
244 : {
245 0 : for (ContextData& c : m_contexts) {
246 0 : if (c.ctxId->id == ctx->id) {
247 0 : return c;
248 : }
249 : }
250 :
251 0 : m_contexts.emplace_back();
252 :
253 0 : ContextData& ref = m_contexts.back();
254 0 : ref.ctxId = ctx;
255 :
256 0 : modularity::IContextSetup* global = m_globalModule->newContext(ctx);
257 0 : if (global) {
258 0 : ref.setups.push_back(global);
259 : }
260 :
261 0 : for (modularity::IModuleSetup* m : m_modules) {
262 0 : modularity::IContextSetup* s = m->newContext(ctx);
263 0 : if (s) {
264 0 : ref.setups.push_back(s);
265 : }
266 : }
267 :
268 0 : return ref;
269 : }
270 :
271 0 : std::vector<muse::modularity::ContextPtr> BaseApplication::contexts() const
272 : {
273 0 : std::vector<muse::modularity::ContextPtr> ctxs;
274 0 : ctxs.reserve(m_contexts.size());
275 0 : for (const ContextData& c : m_contexts) {
276 0 : ctxs.push_back(c.ctxId);
277 : }
278 0 : return ctxs;
279 0 : }
280 :
281 0 : size_t BaseApplication::contextCount() const
282 : {
283 0 : return m_contexts.size();
284 : }
285 :
286 0 : std::shared_ptr<CmdOptions> BaseApplication::makeContextOptions(const muse::StringList&) const
287 : {
288 0 : return m_appOptions;
289 : }
290 :
291 0 : void BaseApplication::showContextSplash(const muse::modularity::ContextPtr&)
292 : {
293 0 : }
294 :
295 0 : muse::modularity::ContextPtr BaseApplication::setupNewContext(const StringList& args)
296 : {
297 : #ifndef MUSE_MODULE_MULTIWINDOWS_SINGLEPROC_MODE
298 : static bool once = false;
299 : IF_ASSERT_FAILED(!once) {
300 : return nullptr;
301 : }
302 : once = true;
303 : #endif
304 :
305 : static int lastId = 0;
306 :
307 0 : modularity::ContextPtr ctxId = std::make_shared<modularity::Context>();
308 0 : ++lastId;
309 0 : ctxId->id = lastId;
310 :
311 0 : ContextData& ctx = contextData(ctxId);
312 0 : ctx.initializing = true;
313 0 : ctx.options = makeContextOptions(args);
314 :
315 0 : LOGI() << "Creating new context with id: " << ctxId->id;
316 :
317 0 : QMetaObject::invokeMethod(qApp, [this, ctxId]() {
318 0 : showContextSplash(ctxId);
319 0 : QMetaObject::invokeMethod(qApp, [this, ctxId]() {
320 0 : setupContext(ctxId);
321 0 : QMetaObject::invokeMethod(qApp, [this, ctxId]() {
322 0 : ContextData& ctx = contextData(ctxId);
323 0 : ctx.initializing = false;
324 0 : startupScenario(ctxId);
325 0 : }, Qt::QueuedConnection);
326 0 : }, Qt::QueuedConnection);
327 0 : }, Qt::QueuedConnection);
328 :
329 0 : return ctxId;
330 0 : }
331 :
332 0 : void BaseApplication::setupContext(const muse::modularity::ContextPtr& ctxId)
333 : {
334 0 : const IApplication::RunMode runMode = m_appOptions->runMode;
335 : // Setup
336 0 : std::vector<muse::modularity::IContextSetup*>& csetups = contextData(ctxId).setups;
337 :
338 0 : for (modularity::IContextSetup* s : csetups) {
339 0 : s->registerExports();
340 : }
341 :
342 0 : for (modularity::IContextSetup* s : csetups) {
343 0 : s->resolveImports();
344 : }
345 :
346 0 : for (modularity::IContextSetup* s : csetups) {
347 0 : s->onPreInit(runMode);
348 : }
349 :
350 0 : for (modularity::IContextSetup* s : csetups) {
351 0 : s->onInit(runMode);
352 : }
353 :
354 0 : for (modularity::IContextSetup* s : csetups) {
355 0 : s->onAllInited(runMode);
356 : }
357 0 : }
358 :
359 0 : void BaseApplication::destroyContext(const modularity::ContextPtr& ctx)
360 : {
361 0 : TRACEFUNC;
362 :
363 0 : IF_ASSERT_FAILED(ctx) {
364 0 : return;
365 : }
366 :
367 0 : LOGI() << "Destroying context with id: " << ctx->id;
368 :
369 0 : auto it = std::find_if(m_contexts.begin(), m_contexts.end(),
370 0 : [&ctx](const ContextData& c) { return c.ctxId->id == ctx->id; });
371 0 : if (it == m_contexts.end()) {
372 0 : LOGW() << "Context not found: " << ctx->id;
373 0 : return;
374 : }
375 :
376 : //! The context is in the process of initializing.
377 : //! It is not possible to delete the context until initialization is complete.
378 0 : IF_ASSERT_FAILED(!it->initializing) {
379 0 : return;
380 : }
381 :
382 0 : doDestroyContext(*it);
383 :
384 0 : m_contexts.erase(it);
385 0 : }
386 :
387 0 : void BaseApplication::doDestroyContext(const ContextData& data)
388 : {
389 0 : for (modularity::IContextSetup* s : data.setups) {
390 0 : s->onDeinit();
391 : }
392 :
393 0 : for (modularity::IContextSetup* s : data.setups) {
394 0 : s->onDestroy();
395 : }
396 :
397 0 : qDeleteAll(data.setups);
398 :
399 0 : modularity::removeIoC(data.ctxId);
400 0 : }
401 :
402 : #ifndef NO_QT_SUPPORT
403 0 : QWindow* BaseApplication::focusWindow() const
404 : {
405 0 : return qApp->focusWindow();
406 : }
407 :
408 0 : bool BaseApplication::notify(QObject* object, QEvent* event)
409 : {
410 0 : return qApp->notify(object, event);
411 : }
412 :
413 0 : Qt::KeyboardModifiers BaseApplication::keyboardModifiers() const
414 : {
415 0 : return QApplication::keyboardModifiers();
416 : }
417 :
418 : #endif
419 :
420 0 : void BaseApplication::restart()
421 : {
422 0 : m_finishMode = FinishMode::Restart;
423 0 : QCoreApplication::exit();
424 0 : }
425 :
426 0 : void BaseApplication::finish()
427 : {
428 0 : TRACEFUNC;
429 :
430 0 : doFinish();
431 :
432 0 : PROFILER_PRINT;
433 0 : }
434 :
435 0 : void BaseApplication::doFinish()
436 : {
437 : // Wait Thread Poll
438 : #ifdef QT_CONCURRENT_SUPPORTED
439 0 : QThreadPool* globalThreadPool = QThreadPool::globalInstance();
440 0 : if (globalThreadPool) {
441 0 : LOGI() << "activeThreadCount: " << globalThreadPool->activeThreadCount();
442 0 : globalThreadPool->waitForDone();
443 : }
444 : #endif
445 :
446 : // Deinit
447 0 : async::processMessages();
448 :
449 : // Deinit and delete contexts
450 0 : std::vector<muse::modularity::ContextPtr> ctxs = contexts();
451 0 : for (auto& c : ctxs) {
452 0 : destroyContext(c);
453 : }
454 :
455 0 : for (modularity::IModuleSetup* m : m_modules) {
456 0 : m->onDeinit();
457 : }
458 :
459 0 : m_globalModule->onDeinit();
460 :
461 0 : for (modularity::IModuleSetup* m : m_modules) {
462 0 : m->onDestroy();
463 : }
464 :
465 0 : m_globalModule->onDestroy();
466 :
467 : // Delete modules
468 0 : qDeleteAll(m_modules);
469 0 : m_modules.clear();
470 :
471 0 : delete m_globalModule;
472 0 : m_globalModule = nullptr;
473 :
474 0 : muse::modularity::resetAll();
475 :
476 0 : if (m_finishMode == IApplication::FinishMode::Restart) {
477 : #ifdef QT_QPROCESS_SUPPORTED
478 0 : QString program = qApp->arguments()[0];
479 :
480 : // NOTE: remove the first argument - the program name
481 0 : QStringList arguments = qApp->arguments().mid(1);
482 :
483 0 : QProcess::startDetached(program, arguments);
484 : #else
485 : NOT_SUPPORTED;
486 : #endif
487 0 : }
488 0 : }
489 :
490 0 : void BaseApplication::processEvents()
491 : {
492 0 : qApp->processEvents();
493 0 : tickerProvider()->forceSchedule();
494 0 : }
|