16#include <private/qproperty_p.h>
17#include <qdatastream.h>
22#include <private/qfilesystementry_p.h>
24#include <private/qloggingregistry_p.h>
25#include <qscopeguard.h>
26#include <qstandardpaths.h>
29#include <qthreadstorage.h>
31#include <QtCore/qpromise.h>
33#include <private/qthread_p.h>
35#include <qthreadpool.h>
36#include <private/qthreadpool_p.h>
39#include <qlibraryinfo.h>
41#include <qvarlengtharray.h>
42#include <private/qfactoryloader_p.h>
43#include <private/qfunctions_p.h>
44#include <private/qlocale_p.h>
45#include <private/qlocking_p.h>
46#include <private/qhooks_p.h>
47#include <private/qnativeinterface_p.h>
49#if QT_CONFIG(permissions)
50#include <private/qpermissions_p.h>
55# if defined(Q_OS_DARWIN)
56# include "qeventdispatcher_cf_p.h"
58# if !defined(QT_NO_GLIB)
59# include "qeventdispatcher_glib_p.h"
62# if !defined(Q_OS_WASM)
63# include "qeventdispatcher_unix_p.h"
67#include "qeventdispatcher_win_p.h"
71#if defined(Q_OS_ANDROID)
72#include <QtCore/qjniobject.h>
76# include "qcore_mac_p.h"
83# ifndef Q_OS_INTEGRITY
87# include <sys/types.h>
89# include "qcore_unix_p.h"
94# if defined(Q_OS_LINUX) && !defined(AT_EXECFN
)
104#include <emscripten/val.h>
107#ifdef QT_BOOTSTRAPPED
108#include <private/qtrace_p.h>
110#include <qtcore_tracepoints_p.h>
118# include <qt_windows.h>
123using namespace Qt::StringLiterals;
126 "#include <qcoreevent.h>"
138#if (defined(Q_OS_WIN) || defined(Q_OS_DARWIN)) && !defined(QT_BOOTSTRAPPED)
139extern QString qAppFileName();
142Q_CONSTINIT
bool QCoreApplicationPrivate::setuidAllowed =
false;
144#if QT_VERSION >= QT_VERSION_CHECK(7
, 0
, 0
)
145# warning "Audit remaining direct usages of this variable for memory ordering semantics"
146Q_CONSTINIT QBasicAtomicPointer<QCoreApplication> QCoreApplication::self =
nullptr;
149Q_CONSTINIT
static QBasicAtomicPointer<QCoreApplication> g_self =
nullptr;
151# define qApp g_self.loadRelaxed()
154
155
156
157
158
159
160
161
162
163bool QCoreApplication::instanceExists()
noexcept
165 return qApp !=
nullptr;
169#if !defined(Q_OS_WIN) || defined(QT_BOOTSTRAPPED)
171QString QCoreApplicationPrivate::infoDictionaryStringProperty(
const QString &propertyName)
174 QCFString cfPropertyName = propertyName.toCFString();
175 CFTypeRef string = CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(),
178 bundleName = QString::fromCFString(
static_cast<CFStringRef>(string));
182QString QCoreApplicationPrivate::appName()
const
184 QString applicationName;
186 applicationName = infoDictionaryStringProperty(QStringLiteral(
"CFBundleName"));
188 if (applicationName.isEmpty() && argv[0]) {
189 char *p = strrchr(argv[0],
'/');
190 applicationName = QString::fromLocal8Bit(p ? p + 1 : argv[0]);
193 return applicationName;
195QString QCoreApplicationPrivate::appVersion()
const
197 QString applicationVersion;
198#if defined(Q_OS_DARWIN)
199 applicationVersion = infoDictionaryStringProperty(QStringLiteral(
"CFBundleVersion"));
200#elif defined(Q_OS_ANDROID) && !defined(QT_BOOTSTRAPPED)
201 QJniObject context(QNativeInterface::QAndroidApplication::context());
202 if (context.isValid()) {
203 QJniObject pm = context.callObjectMethod(
204 "getPackageManager",
"()Landroid/content/pm/PackageManager;");
205 QJniObject pn = context.callObjectMethod<jstring>(
"getPackageName");
206 if (pm.isValid() && pn.isValid()) {
207 QJniObject packageInfo = pm.callObjectMethod(
208 "getPackageInfo",
"(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;",
210 if (packageInfo.isValid()) {
211 QJniObject versionName = packageInfo.getObjectField(
212 "versionName",
"Ljava/lang/String;");
213 if (versionName.isValid())
214 return versionName.toString();
219 return applicationVersion;
223bool QCoreApplicationPrivate::checkInstance(
const char *function)
225 bool b = (
qApp !=
nullptr);
227 qWarning(
"QApplication::%s: Please instantiate the QApplication object first", function);
231#if QT_CONFIG(commandlineparser)
232void QCoreApplicationPrivate::addQtOptions(QList<QCommandLineOption> *options)
234 options->append(QCommandLineOption(QStringLiteral(
"qmljsdebugger"),
235 QStringLiteral(
"Activates the QML/JS debugger with a specified port. The value must be of format port:1234[,block]. \"block\" makes the application wait for a connection."),
236 QStringLiteral(
"value")));
240void QCoreApplicationPrivate::processCommandLineArguments()
242 int j = argc ? 1 : 0;
243 for (
int i = 1; i < argc; ++i) {
246 if (*argv[i] !=
'-') {
250 const char *arg = argv[i];
253 if (strncmp(arg,
"-qmljsdebugger=", 15) == 0) {
254 qmljs_debug_arguments = QString::fromLocal8Bit(arg + 15);
255 }
else if (strcmp(arg,
"-qmljsdebugger") == 0 && i < argc - 1) {
257 qmljs_debug_arguments = QString::fromLocal8Bit(argv[i]);
282Q_GLOBAL_STATIC(QVFuncList, postRList)
283Q_CONSTINIT
static QBasicMutex globalRoutinesMutex;
287
288
289
290
291
305 const auto locker = qt_scoped_lock(globalRoutinesMutex);
314 const auto locker = qt_scoped_lock(globalRoutinesMutex);
323 const auto locker = qt_scoped_lock(globalRoutinesMutex);
332 if (!preRList.exists())
336 const auto locker = qt_scoped_lock(globalRoutinesMutex);
343 for (QtStartUpFunction f : list)
347void Q_CORE_EXPORT qt_call_post_routines()
349 if (!postRList.exists())
356 const auto locker = qt_scoped_lock(globalRoutinesMutex);
357 qSwap(*postRList, list);
362 for (QtCleanUpFunction f : std::as_const(list))
371Q_CONSTINIT
bool QCoreApplicationPrivate::is_app_running =
false;
373Q_CONSTINIT
bool QCoreApplicationPrivate::is_app_closing =
false;
377 const QPostEventList &l = QThreadData::current()->postEventList;
378 return l.size() - l.startOffset;
385Q_CONSTINIT uint QCoreApplicationPrivate::attribs =
386 (1 << Qt::AA_SynthesizeMouseForUnhandledTouchEvents) |
387 (1 << Qt::AA_SynthesizeMouseForUnhandledTabletEvents);
402#if QT_CONFIG(library)
415Q_GLOBAL_STATIC(QCoreApplicationData, coreappdata)
418Q_CONSTINIT
static bool quitLockEnabled =
true;
427static inline bool isArgvModified(
int argc,
char **argv)
429 if (__argc != argc || !__argv )
433 for (
int a = 0; a < argc; ++a) {
434 if (argv[a] != __argv[a] && strcmp(argv[a], __argv[a]))
440static inline bool contains(
int argc,
char **argv,
const char *needle)
442 for (
int a = 0; a < argc; ++a) {
443 if (!strcmp(argv[a], needle))
450QCoreApplicationPrivate::QCoreApplicationPrivate(
int &aargc,
char **aargv)
451 : argc(aargc), argv(aargv)
453 static const char *
const empty =
"";
454 if (argc == 0 || argv ==
nullptr) {
456 argv =
const_cast<
char **>(&empty);
459 if (!isArgvModified(argc, argv)) {
461 origArgv = q20::make_unique_for_overwrite<
char *[]>(argc);
462 std::copy(argv, argv + argc, origArgv.get());
467 QCoreApplicationPrivate::is_app_closing =
false;
469# if defined(Q_OS_UNIX)
470 if (Q_UNLIKELY(!setuidAllowed && (geteuid() != getuid())))
471 qFatal(
"FATAL: The application binary appears to be running setuid, this is a security hole.");
474 QThread *cur = QThread::currentThread();
475 if (cur != theMainThread.loadAcquire())
476 qWarning(
"WARNING: QApplication was not created in the main() thread.");
480QCoreApplicationPrivate::~QCoreApplicationPrivate()
485#if defined(Q_OS_WIN) && !defined(QT_BOOTSTRAPPED)
486 cleanupDebuggingConsole();
492void QCoreApplicationPrivate::cleanupThreadData()
494 auto thisThreadData = threadData.loadRelaxed();
496 if (thisThreadData && !threadData_clean) {
498 QThreadStoragePrivate::finish(&thisThreadData->tls);
502 const auto locker = qt_scoped_lock(thisThreadData->postEventList.mutex);
503 for (
const QPostEvent &pe : std::as_const(thisThreadData->postEventList)) {
505 pe.receiver->d_func()->postedEvents.fetchAndSubAcquire(1);
506 pe.event->m_posted =
false;
510 thisThreadData->postEventList.clear();
511 thisThreadData->postEventList.recursion = 0;
512 thisThreadData->quitNow =
false;
513 threadData_clean =
true;
517void QCoreApplicationPrivate::createEventDispatcher()
519 Q_Q(QCoreApplication);
520 QThreadData *data = QThreadData::current();
521 Q_ASSERT(!data->hasEventDispatcher());
522 eventDispatcher = data->createEventDispatcher();
523 eventDispatcher->setParent(q);
526void QCoreApplicationPrivate::eventDispatcherReady()
530Q_CONSTINIT QBasicAtomicPointer<QThread> QCoreApplicationPrivate::theMainThread = Q_BASIC_ATOMIC_INITIALIZER(
nullptr);
531Q_CONSTINIT QBasicAtomicPointer<
void> QCoreApplicationPrivate::theMainThreadId = Q_BASIC_ATOMIC_INITIALIZER(
nullptr);
532QThread *QCoreApplicationPrivate::mainThread()
534 Q_ASSERT(theMainThread.loadRelaxed() !=
nullptr);
535 return theMainThread.loadRelaxed();
538void QCoreApplicationPrivate::checkReceiverThread(QObject *receiver)
540 QThread *currentThread = QThread::currentThread();
541 QThread *thr = receiver->thread();
542 Q_ASSERT_X(currentThread == thr || !thr,
543 "QCoreApplication::sendEvent",
544 qPrintable(QString::fromLatin1(
"Cannot send events to objects owned by a different thread. "
545 "Current thread %1. Receiver '%2' was created in thread %3").arg(
546 QDebug::toString(currentThread), QDebug::toString(receiver), QDebug::toString(thr))));
547 Q_UNUSED(currentThread);
555 if (!QCoreApplicationPrivate::checkInstance(
"qAppName"))
557 return QCoreApplication::instance()->d_func()->appName();
560void QCoreApplicationPrivate::initLocale()
562#if defined(QT_BOOTSTRAPPED)
564#elif defined(Q_OS_UNIX)
565 Q_CONSTINIT
static bool qt_locale_initialized =
false;
566 if (qt_locale_initialized)
568 qt_locale_initialized =
true;
573 setlocale(LC_ALL,
"");
577# if defined(Q_OS_INTEGRITY)
578 setlocale(LC_CTYPE,
"UTF-8");
579# elif defined(Q_OS_QNX)
582# elif defined(Q_OS_ANDROID) && __ANDROID_API__ < __ANDROID_API_O__
585# elif defined(Q_OS_VXWORKS)
589 const std::string oldEncoding = nl_langinfo(CODESET);
590 if (!Q_LIKELY(qstricmp(oldEncoding.data(),
"UTF-8") == 0
591 || qstricmp(oldEncoding.data(),
"utf8") == 0)) {
592 const QByteArray oldLocale = setlocale(LC_ALL,
nullptr);
593 QByteArray newLocale;
594 bool warnOnOverride =
true;
595# if defined(Q_OS_DARWIN)
599 warnOnOverride = qstrcmp(setlocale(LC_CTYPE,
nullptr),
"C") != 0
600 || getenv(
"LC_ALL") || getenv(
"LC_CTYPE") || getenv(
"LANG");
604 newLocale = setlocale(LC_CTYPE,
"UTF-8");
606 newLocale = setlocale(LC_CTYPE,
nullptr);
607 if (qsizetype dot = newLocale.indexOf(
'.'); dot != -1)
608 newLocale.truncate(dot);
609 if (qsizetype at = newLocale.indexOf(
'@'); at != -1)
610 newLocale.truncate(at);
611 newLocale +=
".UTF-8";
612 newLocale = setlocale(LC_CTYPE, newLocale);
615 if (newLocale.isEmpty())
616 newLocale = setlocale(LC_CTYPE,
"C.UTF-8");
617 if (newLocale.isEmpty())
618 newLocale = setlocale(LC_CTYPE,
"C.utf8");
621 if (newLocale.isEmpty()) {
623 qWarning(
"Detected locale \"%s\" with character encoding \"%s\", which is not UTF-8.\n"
624 "Qt depends on a UTF-8 locale, but has failed to switch to one.\n"
625 "If this causes problems, reconfigure your locale. See the locale(1) manual\n"
626 "for more information.", oldLocale.constData(), oldEncoding.data());
627 }
else if (warnOnOverride) {
629 qWarning(
"Detected locale \"%s\" with character encoding \"%s\", which is not UTF-8.\n"
630 "Qt depends on a UTF-8 locale, and has switched to \"%s\" instead.\n"
631 "If this causes problems, reconfigure your locale. See the locale(1) manual\n"
632 "for more information.",
633 oldLocale.constData(), oldEncoding.data(), newLocale.constData());
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
718
719
720
721
722
723
724
727
728
729QCoreApplication::QCoreApplication(QCoreApplicationPrivate &p)
733 : QObject(p,
nullptr)
736 d_func()->q_ptr =
this;
742
743
744
745
746
747
748
749
750
751
752
753
754
755QCoreApplication::QCoreApplication(
int &argc,
char **argv
761 : d_ptr(
new QCoreApplicationPrivate(argc, argv))
763 : QObject(*
new QCoreApplicationPrivate(argc, argv))
766 d_func()->q_ptr =
this;
769 QCoreApplicationPrivate::eventDispatcher->startingUp();
774
775
776
777
778
780void Q_TRACE_INSTRUMENT(qtcore) QCoreApplicationPrivate::init()
782 Q_TRACE_SCOPE(QCoreApplicationPrivate_init);
784#if defined(Q_OS_MACOS)
785 QMacAutoReleasePool pool;
788 Q_Q(QCoreApplication);
790#if defined(Q_OS_WIN) && !defined(QT_BOOTSTRAPPED)
791 initDebuggingConsole();
796 Q_ASSERT_X(!QCoreApplication::self,
"QCoreApplication",
"there should be only one application object");
797#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
798 QCoreApplication::self = q;
799 g_self.storeRelaxed(q);
801 QCoreApplication::self.storeRelaxed(q);
806 emscripten::val hardwareConcurrency = emscripten::val::global(
"navigator")[
"hardwareConcurrency"];
807 if (hardwareConcurrency.isUndefined())
808 QThreadPrivate::idealThreadCount = 2;
810 QThreadPrivate::idealThreadCount = hardwareConcurrency.as<
int>();
815 if (!coreappdata()->applicationNameSet)
816 coreappdata()->application = appName();
818 if (!coreappdata()->applicationVersionSet)
819 coreappdata()->applicationVersion = appVersion();
821#if defined(Q_OS_ANDROID) && !defined(QT_BOOTSTRAPPED)
826 QLoggingRegistry::instance()->initializeRules();
829#if QT_CONFIG(library)
833 if (coreappdata->libPathsInitialized()) {
834 const QStringList appPaths = std::move(coreappdata->app_libpaths);
835 Q_ASSERT(!coreappdata->libPathsInitialized());
837 if (coreappdata->libPathsManuallySet()) {
838 const QStringList manualPaths = std::move(coreappdata->manual_libpaths);
839 Q_ASSERT(!coreappdata->libPathsManuallySet());
845 QStringList newPaths(q->libraryPaths());
846 for (qsizetype i = manualPaths.size(), j = appPaths.size(); i > 0 || j > 0; qt_noop()) {
848 newPaths.prepend(manualPaths[--i]);
849 }
else if (--i < 0) {
850 newPaths.removeAll(appPaths[j]);
851 }
else if (manualPaths[i] != appPaths[j]) {
852 newPaths.removeAll(appPaths[j]);
856 coreappdata->manual_libpaths.swap(newPaths);
863 Q_ASSERT(!eventDispatcher);
864 auto thisThreadData = threadData.loadRelaxed();
865 eventDispatcher = thisThreadData->eventDispatcher.loadRelaxed();
868 if (!eventDispatcher)
869 createEventDispatcher();
870 Q_ASSERT(eventDispatcher);
872 if (!eventDispatcher->parent()) {
873 eventDispatcher->moveToThread(thisThreadData->thread.loadAcquire());
874 eventDispatcher->setParent(q);
877 thisThreadData->eventDispatcher = eventDispatcher;
878 eventDispatcherReady();
881 processCommandLineArguments();
883 qt_call_pre_routines();
884 QT_MANGLE_NAMESPACE(qt_startup_hook)();
885#ifndef QT_BOOTSTRAPPED
886 QtPrivate::initBindingStatusThreadId();
887 if (Q_UNLIKELY(qtHookData[QHooks::Startup]))
888 reinterpret_cast<QHooks::StartupCallback>(qtHookData[QHooks::Startup])();
892 is_app_running =
true;
897
898
899QCoreApplication::~QCoreApplication()
901 preRoutinesCalled =
false;
903 qt_call_post_routines();
905#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
907 g_self.storeRelaxed(
nullptr);
909 self.storeRelaxed(
nullptr);
913 QCoreApplicationPrivate::is_app_closing =
true;
914 QCoreApplicationPrivate::is_app_running =
false;
919 QThreadPool *globalThreadPool =
nullptr;
921 globalThreadPool = QThreadPool::globalInstance();
925 if (globalThreadPool) {
926 globalThreadPool->waitForDone();
927 delete globalThreadPool;
932 d_func()->threadData.loadRelaxed()->eventDispatcher =
nullptr;
933 if (QCoreApplicationPrivate::eventDispatcher)
934 QCoreApplicationPrivate::eventDispatcher->closingDown();
935 QCoreApplicationPrivate::eventDispatcher =
nullptr;
938#if QT_CONFIG(library)
939 if (coreappdata.exists()) {
941 coreappdata->app_libpaths = QStringList();
942 coreappdata->manual_libpaths = QStringList();
943 Q_ASSERT(!coreappdata->libPathsManuallySet());
944 Q_ASSERT(!coreappdata->libPathsInitialized());
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972void QCoreApplication::setSetuidAllowed(
bool allow)
974 QCoreApplicationPrivate::setuidAllowed = allow;
978
979
980
981
982
983
984
985bool QCoreApplication::isSetuidAllowed()
987 return QCoreApplicationPrivate::setuidAllowed;
991
992
993
994
995
996
997
998
999
1000void QCoreApplication::setAttribute(Qt::ApplicationAttribute attribute,
bool on)
1004 static_assert(Qt::AA_AttributeCount <=
sizeof(QCoreApplicationPrivate::attribs) * CHAR_BIT);
1007 QCoreApplicationPrivate::attribs |= 1 << attribute;
1009 QCoreApplicationPrivate::attribs &= ~(1 << attribute);
1010#if defined(QT_NO_QOBJECT)
1011 if (Q_UNLIKELY(qApp)) {
1013 if (Q_UNLIKELY(QCoreApplicationPrivate::is_app_running)) {
1015 switch (attribute) {
1016 case Qt::AA_PluginApplication:
1017 case Qt::AA_UseDesktopOpenGL:
1018 case Qt::AA_UseOpenGLES:
1019 case Qt::AA_UseSoftwareOpenGL:
1020#ifdef QT_BOOTSTRAPPED
1021 qWarning(
"Attribute %d must be set before QCoreApplication is created.",
1024 qWarning(
"Attribute Qt::%s must be set before QCoreApplication is created.",
1025 QMetaEnum::fromType<Qt::ApplicationAttribute>().valueToKey(attribute));
1035
1036
1037
1038
1039
1040bool QCoreApplication::testAttribute(Qt::ApplicationAttribute attribute)
1042 return QCoreApplicationPrivate::testAttribute(attribute);
1045#ifndef QT_NO_QOBJECT
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1066bool QCoreApplication::isQuitLockEnabled()
1068 return quitLockEnabled;
1071static bool doNotify(QObject *, QEvent *);
1073void QCoreApplication::setQuitLockEnabled(
bool enabled)
1075 quitLockEnabled = enabled;
1079
1080
1081
1082
1083
1084
1085bool QCoreApplication::notifyInternal2(QObject *receiver, QEvent *event)
1091 QObjectPrivate *d = receiver->d_func();
1092 QThreadData *threadData = d->threadData.loadAcquire();
1093 bool selfRequired = threadData->requiresCoreApplication;
1094 if (selfRequired && !
qApp)
1099 bool result =
false;
1100 void *cbdata[] = { receiver, event, &result };
1101 if (QInternal::activateCallbacks(QInternal::EventNotifyCallback, cbdata)) {
1105 QScopedScopeLevelCounter scopeLevelCounter(threadData);
1107 return doNotify(receiver, event);
1109#if QT_VERSION >= QT_VERSION_CHECK(7
, 0
, 0
)
1110 if (!QThread::isMainThread())
1113 return qApp->notify(receiver, event);
1117
1118
1119
1120
1121
1122
1123bool QCoreApplication::forwardEvent(QObject *receiver, QEvent *event, QEvent *originatingEvent)
1125 if (event && originatingEvent)
1126 event->m_spont = originatingEvent->m_spont;
1128 return notifyInternal2(receiver, event);
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1184bool QCoreApplication::notify(QObject *receiver, QEvent *event)
1189#if QT_VERSION >= QT_VERSION_CHECK(7
, 0
, 0
)
1190 Q_ASSERT(receiver->d_func()->threadData.loadAcquire()->thread.loadRelaxed()
1191 == QCoreApplicationPrivate::mainThread());
1195 if (QCoreApplicationPrivate::is_app_closing)
1197 return doNotify(receiver, event);
1200static bool doNotify(QObject *receiver, QEvent *event)
1205 if (receiver ==
nullptr) {
1206 qWarning(
"QCoreApplication::notify: Unexpected null receiver");
1211 QCoreApplicationPrivate::checkReceiverThread(receiver);
1214 return receiver->isWidgetType() ?
false : QCoreApplicationPrivate::notify_helper(receiver, event);
1217bool QCoreApplicationPrivate::sendThroughApplicationEventFilters(QObject *receiver, QEvent *event)
1220 Q_ASSERT(QThread::isMainThread());
1224 for (qsizetype i = 0; i < extraData->eventFilters.size(); ++i) {
1225 QObject *obj = extraData->eventFilters.at(i);
1228 if (obj->d_func()->threadData.loadRelaxed() != threadData.loadRelaxed()) {
1229 qWarning(
"QCoreApplication: Application event filter cannot be in a different thread.");
1232 if (obj->eventFilter(receiver, event))
1239bool QCoreApplicationPrivate::sendThroughObjectEventFilters(QObject *receiver, QEvent *event)
1241 if (receiver !=
qApp && receiver->d_func()->extraData) {
1242 for (qsizetype i = 0; i < receiver->d_func()->extraData->eventFilters.size(); ++i) {
1243 QObject *obj = receiver->d_func()->extraData->eventFilters.at(i);
1246 if (obj->d_func()->threadData.loadRelaxed() != receiver->d_func()->threadData.loadRelaxed()) {
1247 qWarning(
"QCoreApplication: Object event filter cannot be in a different thread.");
1250 if (obj->eventFilter(receiver, event))
1258
1259
1260
1261
1262bool QCoreApplicationPrivate::notify_helper(QObject *receiver, QEvent * event)
1266 Q_TRACE(QCoreApplication_notify_entry, receiver, event, event->type());
1267 bool consumed =
false;
1268 bool filtered =
false;
1269 Q_TRACE_EXIT(QCoreApplication_notify_exit, consumed, filtered);
1272 if (QThread::isMainThread()
1273 && QCoreApplication::self
1274 && QCoreApplication::self->d_func()->sendThroughApplicationEventFilters(receiver, event)) {
1279 if (sendThroughObjectEventFilters(receiver, event)) {
1285 consumed = receiver->event(event);
1290
1291
1292
1293
1294
1296bool QCoreApplication::startingUp()
1298 return !QCoreApplicationPrivate::is_app_running;
1302
1303
1304
1305
1306
1308bool QCoreApplication::closingDown()
1310 return QCoreApplicationPrivate::is_app_closing;
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags)
1345 QThreadData *data = QThreadData::current();
1346 if (!data->hasEventDispatcher())
1348 data->eventDispatcher.loadRelaxed()->processEvents(flags);
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags,
int ms)
1365 QCoreApplication::processEvents(flags, QDeadlineTimer(ms));
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392void QCoreApplication::processEvents(QEventLoop::ProcessEventsFlags flags, QDeadlineTimer deadline)
1397 QThreadData *data = QThreadData::current();
1398 if (!data->hasEventDispatcher())
1401 while (data->eventDispatcher.loadRelaxed()->processEvents(flags & ~QEventLoop::WaitForMoreEvents)) {
1402 if (deadline.hasExpired())
1408
1409
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437int QCoreApplication::exec()
1439 if (!QCoreApplicationPrivate::checkInstance(
"exec"))
1442 QThreadData *threadData = self->d_func()->threadData.loadAcquire();
1443 if (threadData != QThreadData::current()) {
1444 qWarning(
"%s::exec: Must be called from the main thread", self->metaObject()->className());
1447 if (!threadData->eventLoops.isEmpty()) {
1448 qWarning(
"QCoreApplication::exec: The event loop is already running");
1452 threadData->quitNow =
false;
1453 QEventLoop eventLoop;
1454 self->d_func()->in_exec =
true;
1455 self->d_func()->aboutToQuitEmitted =
false;
1456 int returnCode = eventLoop.exec(QEventLoop::ApplicationExec);
1457 threadData->quitNow =
false;
1460 self->d_func()->execCleanup();
1471void QCoreApplicationPrivate::execCleanup()
1473 threadData.loadRelaxed()->quitNow =
false;
1475 QCoreApplication::sendPostedEvents(
nullptr, QEvent::DeferredDelete);
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509void QCoreApplication::exit(
int returnCode)
1513 QCoreApplicationPrivate *d = self->d_func();
1514 if (!d->aboutToQuitEmitted) {
1515 emit self->aboutToQuit(QCoreApplication::QPrivateSignal());
1516 d->aboutToQuitEmitted =
true;
1518 QThreadData *data = d->threadData.loadRelaxed();
1519 data->quitNow =
true;
1520 for (qsizetype i = 0; i < data->eventLoops.size(); ++i) {
1521 QEventLoop *eventLoop = data->eventLoops.at(i);
1522 eventLoop->exit(returnCode);
1527
1528
1530#ifndef QT_NO_QOBJECT
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545bool QCoreApplication::sendEvent(QObject *receiver, QEvent *event)
1547 Q_ASSERT_X(receiver,
"QCoreApplication::sendEvent",
"Unexpected null receiver");
1548 Q_ASSERT_X(event,
"QCoreApplication::sendEvent",
"Unexpected null event");
1550 Q_TRACE(QCoreApplication_sendEvent, receiver, event, event->type());
1552 event->m_spont =
false;
1553 return notifyInternal2(receiver, event);
1557
1558
1559bool QCoreApplication::sendSpontaneousEvent(QObject *receiver, QEvent *event)
1561 Q_ASSERT_X(receiver,
"QCoreApplication::sendSpontaneousEvent",
"Unexpected null receiver");
1562 Q_ASSERT_X(event,
"QCoreApplication::sendSpontaneousEvent",
"Unexpected null event");
1564 Q_TRACE(QCoreApplication_sendSpontaneousEvent, receiver, event, event->type());
1566 event->m_spont =
true;
1567 return notifyInternal2(receiver, event);
1572QCoreApplicationPrivate::QPostEventListLocker QCoreApplicationPrivate::lockThreadPostEventList(QObject *object)
1574 QPostEventListLocker locker;
1577 locker.threadData = QThreadData::current();
1578 locker.locker = qt_unique_lock(locker.threadData->postEventList.mutex);
1582 auto &threadData = QObjectPrivate::get(object)->threadData;
1587 locker.threadData = threadData.loadAcquire();
1588 if (!locker.threadData) {
1593 auto temporaryLocker = qt_unique_lock(locker.threadData->postEventList.mutex);
1594 if (locker.threadData == threadData.loadAcquire()) {
1595 locker.locker = std::move(temporaryLocker);
1600 Q_ASSERT(locker.threadData);
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640void QCoreApplication::postEvent(QObject *receiver, QEvent *event,
int priority)
1642 Q_ASSERT_X(event,
"QCoreApplication::postEvent",
"Unexpected null event");
1644 Q_TRACE_SCOPE(QCoreApplication_postEvent, receiver, event, event->type());
1647 if (receiver ==
nullptr) {
1648 qWarning(
"QCoreApplication::postEvent: Unexpected null receiver");
1653 auto locker = QCoreApplicationPrivate::lockThreadPostEventList(receiver);
1654 if (!locker.threadData) {
1660 QThreadData *data = locker.threadData;
1663 QT_WARNING_DISABLE_DEPRECATED
1665 if (receiver->d_func()->postedEvents.loadAcquire()
1666 && self && self->compressEvent(event, receiver, &data->postEventList)) {
1667 Q_TRACE(QCoreApplication_postEvent_event_compressed, receiver, event);
1674 std::unique_ptr<QEvent> eventDeleter(event);
1675 Q_TRACE(QCoreApplication_postEvent_event_posted, receiver, event, event->type());
1676 data->postEventList.addEvent(QPostEvent(receiver, event, priority));
1677 Q_UNUSED(eventDeleter.release());
1678 event->m_posted =
true;
1679 receiver->d_func()->postedEvents.fetchAndAddRelease(1);
1680 data->canWait =
false;
1683 QAbstractEventDispatcher* dispatcher = data->eventDispatcher.loadAcquire();
1685 dispatcher->wakeUp();
1689
1690
1691
1692#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
1693bool QCoreApplication::compressEvent(QEvent *event, QObject *receiver, QPostEventList *postedEvents)
1695 return d_func()->compressEvent(event, receiver, postedEvents);
1699bool QCoreApplicationPrivate::compressEvent(QEvent *event, QObject *receiver, QPostEventList *postedEvents)
1703 Q_ASSERT(postedEvents);
1706 if (event->type() == QEvent::Timer) {
1707 const int timerId =
static_cast<QTimerEvent *>(event)->timerId();
1708 auto it = postedEvents->cbegin();
1709 const auto end = postedEvents->cend();
1711 if (it->event && it->event->type() == QEvent::Timer && it->receiver == receiver) {
1712 if (
static_cast<QTimerEvent *>(it->event)->timerId() == timerId) {
1722 if (event->type() == QEvent::Quit) {
1723 for (
const QPostEvent &cur : std::as_const(*postedEvents)) {
1724 if (cur.receiver != receiver
1725 || cur.event ==
nullptr
1726 || cur.event->type() != event->type())
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754void QCoreApplication::sendPostedEvents(QObject *receiver,
int event_type)
1759 QThreadData *data = QThreadData::current();
1761 QCoreApplicationPrivate::sendPostedEvents(receiver, event_type, data);
1764void QCoreApplicationPrivate::sendPostedEvents(QObject *receiver,
int event_type,
1767 if (event_type == -1) {
1772 if (receiver && receiver->d_func()->threadData.loadRelaxed() != data) {
1773 qWarning(
"QCoreApplication::sendPostedEvents: Cannot send "
1774 "posted events for objects in another thread");
1778 ++data->postEventList.recursion;
1780 auto locker = qt_unique_lock(data->postEventList.mutex);
1785 data->canWait = (data->postEventList.size() == 0);
1787 if (data->postEventList.size() == 0
1788 || (receiver && !receiver->d_func()->postedEvents.loadAcquire())) {
1789 --data->postEventList.recursion;
1793 data->canWait =
true;
1797 qsizetype startOffset = data->postEventList.startOffset;
1798 qsizetype &i = (!event_type && !receiver) ? data->postEventList.startOffset : startOffset;
1799 data->postEventList.insertionOffset = data->postEventList.size();
1803 Q_DISABLE_COPY_MOVE(CleanUp)
1808 bool exceptionCaught;
1810 inline CleanUp(QObject *receiver,
int event_type, QThreadData *data) :
1811 receiver(receiver), event_type(event_type), data(data), exceptionCaught(
true)
1815 if (exceptionCaught) {
1817 data->canWait =
false;
1820 --data->postEventList.recursion;
1821 if (!data->postEventList.recursion && !data->canWait && data->hasEventDispatcher())
1822 data->eventDispatcher.loadRelaxed()->wakeUp();
1826 if (!event_type && !receiver && data->postEventList.startOffset >= 0) {
1827 const QPostEventList::iterator it = data->postEventList.begin();
1828 data->postEventList.erase(it, it + data->postEventList.startOffset);
1829 data->postEventList.insertionOffset -= data->postEventList.startOffset;
1830 Q_ASSERT(data->postEventList.insertionOffset >= 0);
1831 data->postEventList.startOffset = 0;
1835 CleanUp cleanup(receiver, event_type, data);
1837 while (i < data->postEventList.size()) {
1839 if (i >= data->postEventList.insertionOffset)
1842 const QPostEvent &pe = data->postEventList.at(i);
1847 if ((receiver && receiver != pe.receiver) || (event_type && event_type != pe.event->type())) {
1848 data->canWait =
false;
1852 if (pe.event->type() == QEvent::DeferredDelete) {
1859 const int eventLoopLevel =
static_cast<QDeferredDeleteEvent *>(pe.event)->loopLevel();
1860 const int eventScopeLevel =
static_cast<QDeferredDeleteEvent *>(pe.event)->scopeLevel();
1862 const bool postedBeforeOutermostLoop = eventLoopLevel == 0;
1863 const bool allowDeferredDelete =
1864 (eventLoopLevel + eventScopeLevel > data->loopLevel + data->scopeLevel
1865 || (postedBeforeOutermostLoop && data->loopLevel > 0)
1866 || (event_type == QEvent::DeferredDelete
1867 && eventLoopLevel + eventScopeLevel == data->loopLevel + data->scopeLevel));
1868 if (!allowDeferredDelete) {
1870 if (!event_type && !receiver) {
1875 QPostEvent pe_copy = pe;
1879 const_cast<QPostEvent &>(pe).event =
nullptr;
1882 data->postEventList.addEvent(pe_copy);
1890 pe.event->m_posted =
false;
1891 QEvent *e = pe.event;
1892 QObject * r = pe.receiver;
1895 const auto previous = r->d_func()->postedEvents.fetchAndSubAcquire(1);
1896 Q_ASSERT(previous > 0);
1900 const_cast<QPostEvent &>(pe).event =
nullptr;
1903 const auto relocker = qScopeGuard([&locker] { locker.lock(); });
1905 const std::unique_ptr<QEvent> event_deleter(e);
1908 QCoreApplication::sendEvent(r, e);
1915 cleanup.exceptionCaught =
false;
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1937void QCoreApplication::removePostedEvents(QObject *receiver,
int eventType)
1939 auto locker = QCoreApplicationPrivate::lockThreadPostEventList(receiver);
1940 QThreadData *data = locker.threadData;
1946 if (receiver && !receiver->d_func()->postedEvents.loadAcquire())
1951 QVarLengthArray<QEvent*> events;
1952 qsizetype n = data->postEventList.size();
1955 for (qsizetype i = 0; i < n; ++i) {
1956 const QPostEvent &pe = data->postEventList.at(i);
1958 if ((!receiver || pe.receiver == receiver)
1959 && (pe.event && (eventType == 0 || pe.event->type() == eventType))) {
1960 pe.receiver->d_func()->postedEvents.fetchAndSubAcquire(1);
1961 pe.event->m_posted =
false;
1962 events.append(pe.event);
1963 const_cast<QPostEvent &>(pe).event =
nullptr;
1964 }
else if (!data->postEventList.recursion) {
1966 qSwap(data->postEventList[i], data->postEventList[j]);
1972 if (receiver && eventType == 0) {
1973 Q_ASSERT(!receiver->d_func()->postedEvents.loadRelaxed());
1977 if (!data->postEventList.recursion) {
1979 data->postEventList.erase(data->postEventList.begin() + j, data->postEventList.end());
1987
1988
1989
1990
1991
1992
1993
1994
1995
1997void QCoreApplicationPrivate::removePostedEvent(QEvent * event)
1999 if (!event || !event->m_posted)
2002 QThreadData *data = QThreadData::current();
2004 const auto locker = qt_scoped_lock(data->postEventList.mutex);
2006 if (data->postEventList.size() == 0) {
2007#if defined(QT_DEBUG)
2008 qDebug(
"QCoreApplication::removePostedEvent: Internal error: %p %d is posted",
2009 (
void*)event, event->type());
2014 for (
const QPostEvent &pe : std::as_const(data->postEventList)) {
2015 if (pe.event == event) {
2017 qWarning(
"QCoreApplication::removePostedEvent: Event of type %d deleted while posted to %s %s",
2019 pe.receiver->metaObject()->className(),
2020 pe.receiver->objectName().toLocal8Bit().data());
2022 pe.receiver->d_func()->postedEvents.fetchAndSubAcquire(1);
2023 pe.event->m_posted =
false;
2025 const_cast<QPostEvent &>(pe).event =
nullptr;
2032
2033
2034bool QCoreApplication::event(QEvent *e)
2036 if (e->type() == QEvent::Quit) {
2040 return QObject::event(e);
2043void QCoreApplicationPrivate::ref()
2048void QCoreApplicationPrivate::deref()
2050 quitLockRef.deref();
2052 if (quitLockEnabled && canQuitAutomatically())
2053 quitAutomatically();
2056bool QCoreApplicationPrivate::canQuitAutomatically()
2067 if (quitLockRef.loadRelaxed())
2073void QCoreApplicationPrivate::quitAutomatically()
2075 Q_Q(QCoreApplication);
2083 QCoreApplication::postEvent(q,
new QEvent(QEvent::Quit));
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120void QCoreApplication::quit()
2125 if (!self->d_func()->in_exec)
2128 self->d_func()->quit();
2131void QCoreApplicationPrivate::quit()
2133 Q_Q(QCoreApplication);
2135 if (QThread::isMainThread()) {
2136 QEvent quitEvent(QEvent::Quit);
2137 QCoreApplication::sendEvent(q, &quitEvent);
2139 QCoreApplication::postEvent(q,
new QEvent(QEvent::Quit));
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2166#ifndef QT_NO_TRANSLATION
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2198bool QCoreApplication::installTranslator(QTranslator *translationFile)
2200 if (!translationFile)
2203 if (!QCoreApplicationPrivate::checkInstance(
"installTranslator"))
2206 QCoreApplicationPrivate *d = self->d_func();
2208 QWriteLocker locker(&d->translateMutex);
2209 d->translators.prepend(translationFile);
2212 if (translationFile->isEmpty())
2215#ifndef QT_NO_QOBJECT
2216 QEvent ev(QEvent::LanguageChange);
2217 QCoreApplication::sendEvent(self, &ev);
2224
2225
2226
2227
2228
2229
2230
2231
2233bool QCoreApplication::removeTranslator(QTranslator *translationFile)
2235 if (!translationFile)
2237 if (!QCoreApplicationPrivate::checkInstance(
"removeTranslator"))
2239 QCoreApplicationPrivate *d = self->d_func();
2240 QWriteLocker locker(&d->translateMutex);
2241 if (d->translators.removeAll(translationFile)) {
2242#ifndef QT_NO_QOBJECT
2244 if (!self->closingDown()) {
2245 QEvent ev(QEvent::LanguageChange);
2246 QCoreApplication::sendEvent(self, &ev);
2257 qsizetype percentPos = 0;
2259 while ((percentPos = result->indexOf(u'%', percentPos + len)) != -1) {
2261 if (percentPos + len == result->size())
2264 if (result->at(percentPos + len) == u'L') {
2266 if (percentPos + len == result->size())
2272 if (result->at(percentPos + len) == u'n') {
2275 result->replace(percentPos, len, fmt);
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315QString QCoreApplication::translate(
const char *context,
const char *sourceText,
2316 const char *disambiguation,
int n)
2324 QCoreApplicationPrivate *d = self->d_func();
2325 QReadLocker locker(&d->translateMutex);
2326 if (!d->translators.isEmpty()) {
2327 QList<QTranslator*>::ConstIterator it;
2328 QTranslator *translationFile;
2329 for (it = d->translators.constBegin(); it != d->translators.constEnd(); ++it) {
2330 translationFile = *it;
2331 result = translationFile->translate(context, sourceText, disambiguation, n);
2332 if (!result.isNull())
2338 if (result.isNull())
2339 result = QString::fromUtf8(sourceText);
2341 replacePercentN(&result, n);
2348 return QCoreApplication::translate(
nullptr, id,
nullptr, n);
2351bool QCoreApplicationPrivate::isTranslatorInstalled(QTranslator *translator)
2353 if (!QCoreApplication::self)
2355 QCoreApplicationPrivate *d = QCoreApplication::self->d_func();
2356 QReadLocker locker(&d->translateMutex);
2357 return d->translators.contains(translator);
2362QString QCoreApplication::translate(
const char *context,
const char *sourceText,
2363 const char *disambiguation,
int n)
2366 Q_UNUSED(disambiguation);
2367 QString ret = QString::fromUtf8(sourceText);
2369 ret.replace(
"%n"_L1, QString::number(n));
2375#ifndef QT_BOOTSTRAPPED
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399QString QCoreApplication::applicationDirPath()
2402 qWarning(
"QCoreApplication::applicationDirPath: Please instantiate the QApplication object first");
2406 QFileSystemEntry appFilePath(applicationFilePath(), QFileSystemEntry::FromInternalPath{});
2407 return appFilePath.isEmpty() ? QString() : appFilePath.path();
2410#if !defined(Q_OS_WIN) && !defined(Q_OS_DARWIN)
2414# if defined(Q_OS_ANDROID)
2417# elif defined(Q_OS_LINUX)
2420# if QT_CONFIG(getauxval)
2422 if (
auto ptr =
reinterpret_cast<
const char *>(getauxval(AT_EXECFN)); ptr && *ptr ==
'/')
2423 return QFile::decodeName(ptr);
2427 return QFile::decodeName(qt_readlink(
"/proc/self/exe"));
2428# elif defined(AT_EXECPATH)
2430 char execfn[PATH_MAX];
2431 if (elf_aux_info(AT_EXECPATH, execfn,
sizeof(execfn)) != 0)
2434 qsizetype len = qstrlen(execfn);
2435 return QFile::decodeName(QByteArray::fromRawData(execfn, len));
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458QString QCoreApplication::applicationFilePath()
2461 qWarning(
"QCoreApplication::applicationFilePath: Please instantiate the QApplication object first");
2465 QCoreApplicationPrivate *d = self->d_func();
2468 static QByteArray procName = QByteArray(d->argv[0]);
2469 if (procName != QByteArrayView(d->argv[0])) {
2471 d->cachedApplicationFilePath = QString();
2472 procName.assign(d->argv[0]);
2476 if (!d->cachedApplicationFilePath.isNull())
2477 return d->cachedApplicationFilePath;
2479 QString absPath = qAppFileName();
2480 if (Q_LIKELY(!absPath.isEmpty())) {
2482 return d->cachedApplicationFilePath = std::move(absPath);
2485 if (
const QStringList args = arguments(); !args.isEmpty()) {
2486 const QString &argv0 = args[0];
2488 if (!argv0.isEmpty() && argv0.at(0) == u'/') {
2490
2491
2492
2494 }
else if (argv0.contains(u'/')) {
2496
2497
2498
2499 absPath = QDir::current().absoluteFilePath(argv0);
2502
2503
2504
2505 absPath = QStandardPaths::findExecutable(argv0);
2509 absPath = QFileInfo(absPath).canonicalFilePath();
2510 if (!absPath.isEmpty()) {
2511 return d->cachedApplicationFilePath = std::move(absPath);
2518
2519
2520
2521
2522qint64 QCoreApplication::applicationPid()
noexcept
2524#if defined(Q_OS_WIN)
2525 return GetCurrentProcessId();
2526#elif defined(Q_OS_VXWORKS)
2527 return (pid_t) taskIdCurrent;
2534static QStringList winCmdArgs()
2543 if (
wchar_t **argv = CommandLineToArgvW(GetCommandLine(), &size)) {
2544 result.reserve(size);
2545 wchar_t **argvEnd = argv + size;
2546 for (
wchar_t **a = argv; a < argvEnd; ++a)
2547 result.append(QString::fromWCharArray(*a));
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2586QStringList QCoreApplication::arguments()
2591 qWarning(
"QCoreApplication::arguments: Please instantiate the QApplication object first");
2595 const QCoreApplicationPrivate *d = self->d_func();
2597 const int argc = d->argc;
2598 char **
const argv = d->argv;
2601#if defined(Q_OS_WIN)
2602 const bool argsModifiedByUser = d->origArgv ==
nullptr;
2603 if (!argsModifiedByUser) {
2604 QStringList commandLineArguments = winCmdArgs();
2609 if (argc != d->origArgc) {
2616 for (
int i = 0; i < d->origArgc; ++i) {
2617 if (!contains(argc, argv, d->origArgv[i]))
2618 commandLineArguments.removeAll(QString::fromLocal8Bit(d->origArgv[i]));
2622 return commandLineArguments;
2626 for (
int a = 0; a < argc; ++a)
2627 list << QString::fromLocal8Bit(argv[a]);
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2649
2650
2651
2652
2653
2654
2655
2656void QCoreApplication::setOrganizationName(
const QString &orgName)
2658 if (coreappdata()->orgName == orgName)
2660 coreappdata()->orgName = orgName;
2661#ifndef QT_NO_QOBJECT
2662 if (QCoreApplication::self)
2663 emit QCoreApplication::self->organizationNameChanged();
2667QString QCoreApplication::organizationName()
2669 return coreappdata()->orgName;
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2688
2689
2690
2691
2692
2693void QCoreApplication::setOrganizationDomain(
const QString &orgDomain)
2695 if (coreappdata()->orgDomain == orgDomain)
2697 coreappdata()->orgDomain = orgDomain;
2698#ifndef QT_NO_QOBJECT
2699 if (QCoreApplication::self)
2700 emit QCoreApplication::self->organizationDomainChanged();
2704QString QCoreApplication::organizationDomain()
2706 return coreappdata()->orgDomain;
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2726
2727
2728
2729
2730
2731void QCoreApplication::setApplicationName(
const QString &application)
2733 coreappdata()->applicationNameSet = !application.isEmpty();
2734 QString newAppName = application;
2735 if (newAppName.isEmpty() && QCoreApplication::self)
2736 newAppName = QCoreApplication::self->d_func()->appName();
2737 if (coreappdata()->application == newAppName)
2739 coreappdata()->application = newAppName;
2740#ifndef QT_NO_QOBJECT
2741 if (QCoreApplication::self)
2742 emit QCoreApplication::self->applicationNameChanged();
2746QString QCoreApplication::applicationName()
2748 return coreappdata() ? coreappdata()->application : QString();
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2779
2780
2781
2782
2783
2784void QCoreApplication::setApplicationVersion(
const QString &version)
2786 coreappdata()->applicationVersionSet = !version.isEmpty();
2787 QString newVersion = version;
2788 if (newVersion.isEmpty() && QCoreApplication::self)
2789 newVersion = QCoreApplication::self->d_func()->appVersion();
2790 if (coreappdata()->applicationVersion == newVersion)
2792 coreappdata()->applicationVersion = newVersion;
2793#ifndef QT_NO_QOBJECT
2794 if (QCoreApplication::self)
2795 emit QCoreApplication::self->applicationVersionChanged();
2799QString QCoreApplication::applicationVersion()
2801 return coreappdata() ? coreappdata()->applicationVersion : QString();
2804#if QT_CONFIG(permissions) || defined(Q_QDOC)
2807
2808
2809
2810
2811
2812
2813
2814
2815Qt::PermissionStatus QCoreApplication::checkPermission(
const QPermission &permission)
2817 return QPermissions::Private::checkPermission(permission);
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2887
2888
2889
2890
2891
2892
2893
2894void QCoreApplication::requestPermissionImpl(
const QPermission &requestedPermission,
2895 QtPrivate::QSlotObjectBase *slotObjRaw,
const QObject *context)
2897 QtPrivate::SlotObjUniquePtr slotObj{slotObjRaw};
2900 if (!QThread::isMainThread()) {
2901 qCWarning(lcPermissions,
"Permissions can only be requested from the GUI (main) thread");
2905 class PermissionReceiver :
public QObject
2908 explicit PermissionReceiver(QtPrivate::SlotObjUniquePtr &&slotObject,
const QObject *context)
2909 : slotObject(std::move(slotObject)), context(context ? context :
this)
2911 Q_ASSERT(
this->context);
2912 moveToThread(
this->context->thread());
2915 void finalizePermissionRequest(
const QPermission &permission)
2917 Q_ASSERT(slotObject);
2920 void *args[] = {
nullptr,
const_cast<QPermission *>(&permission) };
2921 slotObject->call(
const_cast<QObject *>(context.data()), args);
2926 QtPrivate::SlotObjUniquePtr slotObject;
2927 QPointer<
const QObject> context;
2931 auto receiver = std::make_shared<PermissionReceiver>(std::move(slotObj), context);
2933 QPermissions::Private::requestPermission(requestedPermission,
2934 [=, receiver = std::move(receiver)](Qt::PermissionStatus status)
mutable {
2935 if (status == Qt::PermissionStatus::Undetermined) {
2936 Q_ASSERT_X(
false,
"QPermission",
2937 "Internal error: requestPermission() should never return Undetermined");
2938 status = Qt::PermissionStatus::Denied;
2941 if (QCoreApplication::self) {
2942 QPermission permission = requestedPermission;
2943 permission.m_status = status;
2944 auto receiverObject = receiver.get();
2945 QMetaObject::invokeMethod(receiverObject,
2946 [receiver = std::move(receiver), permission] {
2947 receiver->finalizePermissionRequest(permission);
2948 }, Qt::QueuedConnection);
2955#if QT_CONFIG(library)
2956static QStringList libraryPathsLocked();
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987QStringList QCoreApplication::libraryPaths()
2989 QMutexLocker locker(&coreappdata->libraryPathMutex);
2990 return libraryPathsLocked();
2994
2995
2996static QStringList libraryPathsLocked()
2998 QCoreApplicationData *d = coreappdata;
2999 if (d->libPathsManuallySet())
3000 return d->manual_libpaths;
3002 QStringList *app_libpaths = &d->app_libpaths;
3003 if (!d->libPathsInitialized()) {
3005 auto setPathsFromEnv = [&](QString libPathEnv) {
3006 if (!libPathEnv.isEmpty()) {
3007 QStringList paths = libPathEnv.split(QDir::listSeparator(), Qt::SkipEmptyParts);
3008 for (QStringList::const_iterator it = paths.constBegin(); it != paths.constEnd(); ++it) {
3009 QString canonicalPath = QDir(*it).canonicalPath();
3010 if (!canonicalPath.isEmpty()
3011 && !app_libpaths->contains(canonicalPath)) {
3012 app_libpaths->append(canonicalPath);
3017 setPathsFromEnv(qEnvironmentVariable(
"QT_PLUGIN_PATH"));
3023 if (CFBundleRef bundleRef = CFBundleGetMainBundle()) {
3024 if (QCFType<CFURLRef> urlRef = CFBundleCopyBuiltInPlugInsURL(bundleRef)) {
3025 if (QCFType<CFURLRef> absoluteUrlRef = CFURLCopyAbsoluteURL(urlRef)) {
3026 if (QCFString path = CFURLCopyFileSystemPath(absoluteUrlRef, kCFURLPOSIXPathStyle)) {
3027 if (QFile::exists(path)) {
3028 path = QDir(path).canonicalPath();
3029 if (!app_libpaths->contains(path))
3030 app_libpaths->append(path);
3038 QString installPathPlugins = QLibraryInfo::path(QLibraryInfo::PluginsPath);
3039 if (QFile::exists(installPathPlugins)) {
3041 installPathPlugins = QDir(installPathPlugins).canonicalPath();
3042 if (!app_libpaths->contains(installPathPlugins))
3043 app_libpaths->append(installPathPlugins);
3049 QString app_location = QCoreApplication::applicationFilePath();
3050 app_location.truncate(app_location.lastIndexOf(u'/'));
3051 app_location = QDir(app_location).canonicalPath();
3052 if (QFile::exists(app_location) && !app_libpaths->contains(app_location))
3053 app_libpaths->append(app_location);
3055 if (app_libpaths->isEmpty())
3056 app_libpaths->reserve(1);
3057 Q_ASSERT(d->libPathsInitialized());
3059 return *app_libpaths;
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076void QCoreApplication::setLibraryPaths(
const QStringList &paths)
3078 QCoreApplicationData *d = coreappdata;
3079 QMutexLocker locker(&d->libraryPathMutex);
3084 if (!d->libPathsInitialized())
3085 libraryPathsLocked();
3087 d->manual_libpaths = paths;
3088 if (d->manual_libpaths.isEmpty())
3089 d->manual_libpaths.reserve(1);
3090 Q_ASSERT(d->libPathsManuallySet());
3093 QFactoryLoader::refreshAll();
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112void QCoreApplication::addLibraryPath(
const QString &path)
3117 QString canonicalPath = QDir(path).canonicalPath();
3118 if (canonicalPath.isEmpty())
3121 QCoreApplicationData *d = coreappdata;
3122 QMutexLocker locker(&d->libraryPathMutex);
3124 QStringList *libpaths = &d->manual_libpaths;
3125 if (d->libPathsManuallySet()) {
3126 if (d->manual_libpaths.contains(canonicalPath))
3130 libraryPathsLocked();
3131 QStringList *app_libpaths = &d->app_libpaths;
3132 if (app_libpaths->contains(canonicalPath))
3135 *libpaths = *app_libpaths;
3138 libpaths->prepend(canonicalPath);
3139 Q_ASSERT(d->libPathsManuallySet());
3141 QFactoryLoader::refreshAll();
3145
3146
3147
3148
3149
3150
3151
3152
3153void QCoreApplication::removeLibraryPath(
const QString &path)
3158 QString canonicalPath = QDir(path).canonicalPath();
3159 if (canonicalPath.isEmpty())
3162 QCoreApplicationData *d = coreappdata;
3163 QMutexLocker locker(&d->libraryPathMutex);
3165 QStringList *libpaths = &d->manual_libpaths;
3166 if (d->libPathsManuallySet()) {
3167 if (libpaths->removeAll(canonicalPath) == 0)
3171 libraryPathsLocked();
3172 QStringList *app_libpaths = &d->app_libpaths;
3173 if (!app_libpaths->contains(canonicalPath))
3176 *libpaths = *app_libpaths;
3177 libpaths->removeAll(canonicalPath);
3178 Q_ASSERT(d->libPathsManuallySet());
3182 QFactoryLoader::refreshAll();
3187#ifndef QT_NO_QOBJECT
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218void QCoreApplication::installNativeEventFilter(QAbstractNativeEventFilter *filterObj)
3220 if (QCoreApplication::testAttribute(Qt::AA_PluginApplication)) {
3221 qWarning(
"Native event filters are not applied when the Qt::AA_PluginApplication attribute is set");
3225 QAbstractEventDispatcher *eventDispatcher = QAbstractEventDispatcher::instance(QCoreApplicationPrivate::theMainThread.loadAcquire());
3226 if (!filterObj || !eventDispatcher)
3228 eventDispatcher->installNativeEventFilter(filterObj);
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244void QCoreApplication::removeNativeEventFilter(QAbstractNativeEventFilter *filterObject)
3246 QAbstractEventDispatcher *eventDispatcher = QAbstractEventDispatcher::instance(QCoreApplicationPrivate::theMainThread.loadAcquire());
3247 if (!filterObject || !eventDispatcher)
3249 eventDispatcher->removeNativeEventFilter(filterObject);
3253
3254
3255
3256QAbstractEventDispatcher *QCoreApplication::eventDispatcher()
3258 if (QCoreApplicationPrivate::theMainThread.loadAcquire())
3259 return QCoreApplicationPrivate::theMainThread.loadRelaxed()->eventDispatcher();
3264
3265
3266
3267
3268
3269void QCoreApplication::setEventDispatcher(QAbstractEventDispatcher *eventDispatcher)
3271 QThread *mainThread = QCoreApplicationPrivate::theMainThread.loadAcquire();
3273 mainThread = QThread::currentThread();
3274 mainThread->setEventDispatcher(eventDispatcher);
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3388void *QCoreApplication::resolveInterface(
const char *name,
int revision)
const
3390#if defined(Q_OS_ANDROID)
3395 using namespace QNativeInterface;
3396 struct AndroidApplication :
public QAndroidApplication {};
3397 static AndroidApplication androidApplication;
3398 QT_NATIVE_INTERFACE_RETURN_IF(QAndroidApplication, &androidApplication);
3400 Q_UNUSED(name); Q_UNUSED(revision);
3406#ifndef QT_NO_QOBJECT
3407#include "moc_qcoreapplication.cpp"
Q_TRACE_POINT(qtcore, QCoreApplication_notify_exit, bool consumed, bool filtered)
void QT_MANGLE_NAMESPACE qt_startup_hook()
static QString qAppFileName()
QList< QtStartUpFunction > QStartUpFuncList
Q_TRACE_METADATA(qtcore, "ENUM { AUTO, RANGE User ... MaxUser } QEvent::Type;")
QString qtTrId(const char *id, int n)
Q_TRACE_POINT(qtcore, QCoreApplication_postEvent_exit)
static void qt_call_pre_routines()
qsizetype qGlobalPostedEventsCount()
static Q_CONSTINIT bool preRoutinesCalled
QList< QtCleanUpFunction > QVFuncList
static void replacePercentN(QString *result, int n)
Q_TRACE_POINT(qtcore, QCoreApplication_postEvent_event_compressed, QObject *receiver, QEvent *event)
Q_TRACE_POINT(qtcore, QCoreApplication_postEvent_entry, QObject *receiver, QEvent *event, QEvent::Type type)
Q_TRACE_PREFIX(qtcore, "#include <qcoreevent.h>")
Q_CORE_EXPORT void qAddPostRoutine(QtCleanUpFunction)
void(* QtCleanUpFunction)()
void(* QtStartUpFunction)()
Q_CORE_EXPORT void qAddPreRoutine(QtStartUpFunction)
Q_CORE_EXPORT void qRemovePostRoutine(QtCleanUpFunction)
Q_GLOBAL_STATIC(QReadWriteLock, g_updateMutex)
QString applicationVersion
QCoreApplicationData() noexcept
bool applicationVersionSet