1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2017 LoRd_MuldeR <MuldeR2@GMX.de>
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
23 #include <MUtils/Startup.h>
24 #include <MUtils/OSSupport.h>
25 #include <MUtils/Terminal.h>
26 #include <MUtils/ErrorHandler.h>
27 #include <MUtils/Registry.h>
28 #include <MUtils/Exception.h>
31 #include <QApplication>
33 #include <QStringList>
34 #include <QLibraryInfo>
36 #include <QImageReader>
38 #include <QMessageBox>
40 #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
41 #include <QAbstractNativeEventFilter>
43 #define QAbstractNativeEventFilter QObject
44 #define Q_DECL_OVERRIDE
52 #define FORCE_INLINE __forceinline
54 #define FORCE_INLINE inline
57 ///////////////////////////////////////////////////////////////////////////////
58 // Qt Static Initialization
59 ///////////////////////////////////////////////////////////////////////////////
63 #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
67 Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin
)
68 Q_IMPORT_PLUGIN(QICOPlugin
)
71 static void doInitializeResources(void)
73 Q_INIT_RESOURCE(MUtilsData
);
76 static void doCleanupResources(void)
78 Q_CLEANUP_RESOURCE(MUtilsData
);
87 class ResourceInitializer
90 ResourceInitializer(void)
92 doInitializeResources();
95 ~ResourceInitializer(void)
101 static ResourceInitializer resourceInitializer
;
108 ///////////////////////////////////////////////////////////////////////////////
110 ///////////////////////////////////////////////////////////////////////////////
112 #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
113 static void qt_message_handler(QtMsgType type
, const char *const msg
)
117 MUtils::Terminal::write(type
, msg
);
118 if ((type
== QtCriticalMsg
) || (type
== QtFatalMsg
))
120 MUtils::OS::fatal_exit(MUTILS_WCHR(QString::fromUtf8(msg
)));
125 #define qInstallMsgHandler(X) qInstallMessageHandler((X))
126 static void qt_message_handler(QtMsgType type
, const QMessageLogContext
&, const QString
&msg
)
130 MUtils::Terminal::write(type
, msg
.toUtf8().constData());
131 if ((type
== QtCriticalMsg
) || (type
== QtFatalMsg
))
133 MUtils::OS::fatal_exit(MUTILS_WCHR(msg
));
139 ///////////////////////////////////////////////////////////////////////////////
141 ///////////////////////////////////////////////////////////////////////////////
149 class NativeEventFilter
: public QAbstractNativeEventFilter
152 bool nativeEventFilter(const QByteArray
&, void *message
, long *result
) Q_DECL_OVERRIDE
154 return filterEvent(message
, result
);
157 static FORCE_INLINE
bool filterEvent(void *message
, long *result
)
159 return MUtils::OS::handle_os_message(message
, result
);
162 static NativeEventFilter
*instance(void)
164 while (m_instance
.isNull())
166 m_instance
.reset(new NativeEventFilter());
168 return m_instance
.data();
172 NativeEventFilter(void) {}
173 static QScopedPointer
<MUtils::Startup::Internal::NativeEventFilter
> m_instance
;
179 ///////////////////////////////////////////////////////////////////////////////
181 ///////////////////////////////////////////////////////////////////////////////
183 static FORCE_INLINE
int startup_main(int &argc
, char **argv
, MUtils::Startup::main_function_t
*const entry_point
, const char* const appName
, const bool &debugConsole
)
185 qInstallMsgHandler(qt_message_handler
);
186 MUtils::Terminal::setup(argc
, argv
, appName
, MUTILS_DEBUG
|| debugConsole
);
187 return entry_point(argc
, argv
);
190 static FORCE_INLINE
int startup_helper(int &argc
, char **argv
, MUtils::Startup::main_function_t
*const entry_point
, const char* const appName
, const bool &debugConsole
)
195 iResult
= startup_main(argc
, argv
, entry_point
, appName
, debugConsole
);
197 catch(const std::exception
&error
)
199 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error:\n%s\n", error
.what());
200 MUtils::OS::fatal_exit(L
"Unhandeled C++ exception error, application will exit!");
204 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnknown exception error!\n");
205 MUtils::OS::fatal_exit(L
"Unhandeled C++ exception error, application will exit!");
210 int MUtils::Startup::startup(int &argc
, char **argv
, main_function_t
*const entry_point
, const char* const appName
, const bool &debugConsole
)
215 _CrtSetDbgFlag(_CRTDBG_CHECK_ALWAYS_DF
|| _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG
));
217 iResult
= startup_main(argc
, argv
, entry_point
, appName
, debugConsole
);
222 MUtils::ErrorHandler::initialize();
223 MUtils::OS::check_debugger();
224 iResult
= startup_helper(argc
, argv
, entry_point
, appName
, debugConsole
);
228 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnhandeled structured exception error!\n");
229 MUtils::OS::fatal_exit(L
"Unhandeled structured exception error, application will exit!");
232 MUtils::ErrorHandler::initialize();
233 MUtils::OS::check_debugger();
234 iResult
= startup_helper(argc
, argv
, entry_point
, appName
, debugConsole
);
236 #endif //MUTILS_DEBUG
240 ///////////////////////////////////////////////////////////////////////////////
242 ///////////////////////////////////////////////////////////////////////////////
244 static QMutex g_init_lock
;
245 static const char *const g_imageformats
[] = {"bmp", "png", "jpg", "gif", "ico", "xpm", "svg", NULL
};
247 static FORCE_INLINE QString
getExecutableName(int &argc
, char **argv
)
251 const char *argv0
= argv
[0];
252 for (int i
= 0; i
< 2; i
++)
254 static const char SEP
[2] = { '/', '\\' };
255 if (const char *const ptr
= strrchr(argv0
, SEP
[i
]))
260 if(strlen(argv0
) > 1)
262 return QString::fromLatin1(argv0
);
265 return QLatin1String("Program.exe");
268 static FORCE_INLINE
void qt_registry_cleanup(void)
270 static const wchar_t *const QT_JUNK_KEY
= L
"Software\\Trolltech\\OrganizationDefaults";
271 MUtils::Registry::reg_key_delete(MUtils::Registry::root_user
, MUTILS_QSTR(QT_JUNK_KEY
), true, true);
274 QApplication
*MUtils::Startup::create_qt(int &argc
, char **argv
, const QString
&appName
, const QString
&appAuthor
, const QString
&appDomain
)
276 QMutexLocker
lock(&g_init_lock
);
277 const OS::ArgumentMap
&arguments
= MUtils::OS::arguments();
279 //Don't initialized again, if done already
280 QScopedPointer
<QApplication
> application(dynamic_cast<QApplication
*>(QApplication::instance()));
281 if(!application
.isNull())
283 qWarning("Qt is already initialized!");
284 return application
.take();
287 //Extract executable name from argv[] array
288 const QString executableName
= getExecutableName(argc
, argv
);
292 qDebug("Using Qt v%s [%s], %s, %s", qVersion(), QLibraryInfo::buildDate().toString(Qt::ISODate
).toLatin1().constData(), (qSharedBuild() ? "DLL" : "Static"), QLibraryInfo::buildKey().toLatin1().constData());
293 qDebug("Compiled with Qt v%s, %s\n", QT_VERSION_STR
, QT_BUILD_KEY
);
294 if(_stricmp(qVersion(), QT_VERSION_STR
))
296 qFatal("%s", QApplication::tr("Executable '%1' requires Qt v%2, but found Qt v%3.").arg(executableName
, QString::fromLatin1(QT_VERSION_STR
), QString::fromLatin1(qVersion())).toLatin1().constData());
299 if(QLibraryInfo::buildKey().compare(QString::fromLatin1(QT_BUILD_KEY
), Qt::CaseInsensitive
))
301 qFatal("%s", QApplication::tr("Executable '%1' was built for Qt '%2', but found Qt '%3'.").arg(executableName
, QString::fromLatin1(QT_BUILD_KEY
), QLibraryInfo::buildKey()).toLatin1().constData());
305 qDebug("Using Qt v%s [%s], %s", qVersion(), QLibraryInfo::buildDate().toString(Qt::ISODate
).toLatin1().constData(), (qSharedBuild() ? "DLL" : "Static"));
306 qDebug("Compiled with Qt v%s\n", QT_VERSION_STR
);
309 //Check the Windows version
310 const MUtils::OS::Version::os_version_t
&osVersion
= MUtils::OS::os_version();
311 if((osVersion
.type
!= MUtils::OS::Version::OS_WINDOWS
) || (osVersion
< MUtils::OS::Version::WINDOWS_WINXP
))
313 qFatal("%s", QApplication::tr("Executable '%1' requires Windows XP or later.").arg(executableName
).toLatin1().constData());
316 //Check whether we are running on a supported Windows version
317 if(const char *const friendlyName
= MUtils::OS::os_friendly_name(osVersion
))
319 qDebug("Running on %s (NT v%u.%u.%u).\n", friendlyName
, osVersion
.versionMajor
, osVersion
.versionMinor
, osVersion
.versionBuild
);
323 const QString message
= QString().sprintf("Running on an unknown WindowsNT-based system (v%u.%u.%u).", osVersion
.versionMajor
, osVersion
.versionMinor
, osVersion
.versionBuild
);
324 qWarning("%s\n", MUTILS_UTF8(message
));
325 MUtils::OS::system_message_wrn(MUTILS_WCHR(executableName
), MUTILS_WCHR(message
));
328 //Check for compat mode
329 if(osVersion
.overrideFlag
&& (osVersion
<= MUtils::OS::Version::WINDOWS_WN100
))
331 qWarning("Windows compatibility mode detected!");
332 if(!arguments
.contains("ignore-compat-mode"))
334 qFatal("%s", QApplication::tr("Executable '%1' doesn't support Windows compatibility mode.").arg(executableName
).toLatin1().constData());
340 if(MUtils::OS::running_on_wine())
342 qWarning("It appears we are running under Wine, unexpected things might happen!\n");
345 //Set text Codec for locale
346 QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
348 //Create Qt application instance
349 application
.reset(new QApplication(argc
, argv
));
351 //Register the Qt clean-up function
352 atexit(qt_registry_cleanup
);
354 //Load plugins from application directory
355 QCoreApplication::setLibraryPaths(QStringList() << QApplication::applicationDirPath());
356 qDebug("Library Path:\n%s\n", MUTILS_UTF8(QApplication::libraryPaths().first()));
358 //Set application properties
359 application
->setApplicationName(appName
);
360 application
->setOrganizationDomain(appDomain
);
361 application
->setOrganizationName(appAuthor
);
362 #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
363 application
->setEventFilter(&Internal::NativeEventFilter::filterEvent
);
365 application
->installNativeEventFilter(Internal::NativeEventFilter::instance());
368 //Check for supported image formats
369 QList
<QByteArray
> supportedFormats
= QImageReader::supportedImageFormats();
370 for(int i
= 0; g_imageformats
[i
]; i
++)
372 if(!supportedFormats
.contains(g_imageformats
[i
]))
374 qFatal("Qt initialization error: QImageIOHandler for '%s' missing!", g_imageformats
[i
]);
380 MUtils::Terminal::set_icon(QIcon(":/mutils/icons/bug.png"));
382 //Enable larger/smaller font size
383 double fontScaleFactor
= 1.0;
384 if(arguments
.contains("huge-font" )) fontScaleFactor
= 1.500;
385 if(arguments
.contains("big-font" )) fontScaleFactor
= 1.250;
386 if(arguments
.contains("small-font")) fontScaleFactor
= 0.875;
387 if(arguments
.contains("tiny-font" )) fontScaleFactor
= 0.750;
388 if(!qFuzzyCompare(fontScaleFactor
, 1.0))
390 qWarning("Application font scale factor set to: %.3f\n", fontScaleFactor
);
391 QFont appFont
= application
->font();
392 appFont
.setPointSizeF(appFont
.pointSizeF() * fontScaleFactor
);
393 application
->setFont(appFont
);
396 //Check for process elevation
397 if(MUtils::OS::is_elevated() && (!MUtils::OS::running_on_wine()))
399 QMessageBox
messageBox(QMessageBox::Warning
, executableName
, "<nobr>This program was started with 'elevated' rights, altough it does not need these rights.<br>Running an applications with unnecessary rights is a potential security risk!</nobr>", QMessageBox::NoButton
, NULL
, Qt::Dialog
| Qt::MSWindowsFixedSizeDialogHint
| Qt::WindowStaysOnTopHint
);
400 messageBox
.addButton("Quit Program (Recommended)", QMessageBox::NoRole
);
401 messageBox
.addButton("Ignore", QMessageBox::NoRole
);
402 if(messageBox
.exec() == 0)
408 //QApplication created successfully
409 return application
.take();
412 ///////////////////////////////////////////////////////////////////////////////