ps4delta/code/delta_qt/main.cpp
2020-04-02 01:43:04 +02:00

145 lines
No EOL
3.9 KiB
C++

/*
* PS4Delta : PS4 emulation and research project
*
* Copyright 2019-2020 Force67.
* For information regarding licensing see LICENSE
* in the root of the source tree.
*/
#include <base.h>
#include <logger/logger.h>
#include <QDir>
#include <QTimer>
#include <QApplication>
#include <QCommandLineParser>
#include <QFileInfo>
#include <utl/path.h>
#include "ui/mainwindow.h"
#include <config.h>
#include <main.h>
#include <core.h>
inline std::string sstr(const QString& _in) {
return _in.toStdString();
}
class deltaApp final : public QApplication {
public:
deltaApp(int& argc, char** argv) : QApplication(argc, argv) {}
bool init() {
LOG_INFO("Initializing delta qt app " rsc_copyright " at " GIT_BRANCH
"-" GIT_COMMIT);
auto& sys = core::System::get();
if (!sys.init())
return false;
if (!headless)
window = std::make_unique<mainWindow>(sys);
else
LOG_INFO("Starting in headless mode");
if (window)
window->init();
return true;
}
private:
std::unique_ptr<mainWindow> window;
bool headless = false;
};
#ifdef _WIN32
#include <Windows.h>
#include <memory.h>
#include <utl/string_util.h>
static void applyQtFixes() {
using NtQueryTimerResolution_t = LONG(WINAPI*)(PULONG, PULONG, PULONG);
using NtSetTimerResolution_t = LONG(WINAPI*)(ULONG, BOOLEAN, PULONG);
auto hNtLib = GetModuleHandleW(L"ntdll.dll");
auto NtQueryTimerResolution_f = reinterpret_cast<NtQueryTimerResolution_t>(
GetProcAddress(hNtLib, "NtQueryTimerResolution"));
auto NtSetTimerResolution_f =
reinterpret_cast<NtSetTimerResolution_t>(GetProcAddress(hNtLib, "NtSetTimerResolution"));
if (NtQueryTimerResolution_f && NtSetTimerResolution_f) {
// qt resets our timer, so wet set it back manually
ULONG min_res, max_res, orig_res, new_res;
if (NtQueryTimerResolution_f(&min_res, &max_res, &orig_res) == 0)
NtSetTimerResolution_f(max_res, TRUE, &new_res);
}
// don't even get me started on how stupid
// locales are
setlocale(LC_ALL, "C");
}
#endif
inline int dmain(int argc, char** argv) {
// create log sinks
utl::createLogger(true);
// create opts
config::load();
// search plug ins in /qt/ dir
{
static auto x = QString::fromUtf8(utl::make_abs_path("qt").c_str());
QCoreApplication::addLibraryPath(x);
}
QCoreApplication::setOrganizationName(rsc_company);
QCoreApplication::setApplicationName(rsc_productname);
QCoreApplication::setApplicationVersion(rsc_productversion);
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QScopedPointer<deltaApp> app(new deltaApp(argc, argv));
#ifdef _WIN32
applyQtFixes();
#endif
QCommandLineParser parser;
parser.setApplicationDescription("Welcome to the " rsc_productname " command line.");
parser.addPositionalArgument("File", "Path for directly loading a file");
parser.addPositionalArgument("[Args...]", "Optional args for the executable");
parser.addOption(QCommandLineOption("headless", "Run " rsc_productname " in headless mode."));
parser.process(*app);
if (app->init()) {
// propagate command line arguments
auto args = parser.positionalArguments();
if (args.length() > 0) {
std::vector<std::string> xargv;
if (args.length() > 1) {
xargv.emplace_back();
for (int i = 1; i < args.length(); i++)
xargv.emplace_back(args[i].toStdString());
}
// Ugly workaround
QTimer::singleShot(2, [path = sstr(QFileInfo(args.at(0)).canonicalFilePath()),
xargv = std::move(xargv)]() mutable {
core::System::get().argv = std::move(xargv);
core::System::get().load(path);
});
}
return app->exec();
}
return 0;
}
DELTA_MAIN(dmain);