dolphin/Source/Core/DolphinQt/Main.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

311 lines
10 KiB
C++
Raw Normal View History

2015-11-27 01:33:07 -07:00
// Copyright 2015 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2015-11-27 01:33:07 -07:00
2018-04-29 14:20:03 -06:00
#ifdef _WIN32
#include <string>
#include <vector>
2018-04-29 14:20:03 -06:00
#include <Windows.h>
#include <cstdio>
#endif
#include <OptionParser.h>
#include <QAbstractEventDispatcher>
2015-11-27 01:33:07 -07:00
#include <QApplication>
2017-05-20 15:41:02 -06:00
#include <QObject>
#include <QPushButton>
#include <QWidget>
2015-11-27 01:33:07 -07:00
#include "Common/Config/Config.h"
#include "Common/MsgHandler.h"
#include "Common/ScopeGuard.h"
2019-03-04 12:49:00 -07:00
#include "Core/Boot/Boot.h"
#include "Core/Config/MainSettings.h"
2015-11-27 01:33:07 -07:00
#include "Core/Core.h"
#include "Core/DolphinAnalytics.h"
2019-03-04 12:49:00 -07:00
2018-07-06 16:40:15 -06:00
#include "DolphinQt/Host.h"
#include "DolphinQt/MainWindow.h"
2019-03-04 12:49:00 -07:00
#include "DolphinQt/QtUtils/ModalMessageBox.h"
2018-07-06 16:40:15 -06:00
#include "DolphinQt/QtUtils/RunOnObject.h"
#include "DolphinQt/Resources.h"
#include "DolphinQt/Settings.h"
#include "DolphinQt/Translation.h"
#include "DolphinQt/Updater.h"
2019-03-04 12:49:00 -07:00
#include "UICommon/CommandLineParse.h"
2015-11-27 01:33:07 -07:00
#include "UICommon/UICommon.h"
static bool QtMsgAlertHandler(const char* caption, const char* text, bool yes_no,
Common::MsgType style)
{
const bool called_from_cpu_thread = Core::IsCPUThread();
const bool called_from_gpu_thread = Core::IsGPUThread();
std::optional<bool> r = RunOnObject(QApplication::instance(), [&] {
// If we were called from the CPU/GPU thread, set us as the CPU/GPU thread.
// This information is used in order to avoid deadlocks when calling e.g.
// Host::SetRenderFocus or Core::RunAsCPUThread. (Host::SetRenderFocus
// can get called automatically when a dialog steals the focus.)
Common::ScopeGuard cpu_scope_guard(&Core::UndeclareAsCPUThread);
Common::ScopeGuard gpu_scope_guard(&Core::UndeclareAsGPUThread);
if (!called_from_cpu_thread)
cpu_scope_guard.Dismiss();
if (!called_from_gpu_thread)
gpu_scope_guard.Dismiss();
if (called_from_cpu_thread)
Core::DeclareAsCPUThread();
if (called_from_gpu_thread)
Core::DeclareAsGPUThread();
ModalMessageBox message_box(QApplication::activeWindow(), Qt::ApplicationModal);
message_box.setWindowTitle(QString::fromUtf8(caption));
message_box.setText(QString::fromUtf8(text));
message_box.setStandardButtons(yes_no ? QMessageBox::Yes | QMessageBox::No : QMessageBox::Ok);
if (style == Common::MsgType::Warning)
message_box.addButton(QMessageBox::Ignore)->setText(QObject::tr("Ignore for this session"));
message_box.setIcon([&] {
switch (style)
{
case Common::MsgType::Information:
return QMessageBox::Information;
case Common::MsgType::Question:
return QMessageBox::Question;
case Common::MsgType::Warning:
return QMessageBox::Warning;
case Common::MsgType::Critical:
return QMessageBox::Critical;
}
// appease MSVC
return QMessageBox::NoIcon;
}());
const int button = message_box.exec();
if (button == QMessageBox::Yes)
return true;
if (button == QMessageBox::Ignore)
{
Common::SetEnableAlert(false);
return true;
}
return false;
});
if (r.has_value())
return *r;
return false;
}
#ifdef _WIN32
#define main app_main
#endif
2015-11-27 01:33:07 -07:00
int main(int argc, char* argv[])
{
#ifdef _WIN32
2018-04-29 14:20:03 -06:00
const bool console_attached = AttachConsole(ATTACH_PARENT_PROCESS) != FALSE;
HANDLE stdout_handle = ::GetStdHandle(STD_OUTPUT_HANDLE);
if (console_attached && stdout_handle)
{
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
}
#endif
Host::GetInstance()->DeclareAsHostThread();
#ifdef __APPLE__
// On macOS, a command line option matching the format "-psn_X_XXXXXX" is passed when
// the application is launched for the first time. This is to set the "ProcessSerialNumber",
// something used by the legacy Process Manager from Carbon. optparse will fail if it finds
// this as it isn't a valid Dolphin command line option, so pretend like it doesn't exist
// if found.
if (strncmp(argv[argc - 1], "-psn", 4) == 0)
{
argc--;
}
#endif
2020-03-21 22:49:32 -06:00
auto parser = CommandLineParse::CreateParser(CommandLineParse::ParserOptions::IncludeGUIOptions);
const optparse::Values& options = CommandLineParse::ParseArguments(parser.get(), argc, argv);
2020-03-21 22:49:32 -06:00
const std::vector<std::string> args = parser->args();
2021-04-24 18:18:28 -06:00
// setHighDpiScaleFactorRoundingPolicy was added in 5.14, but default behavior changed in 6.0
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
// Set to the previous default behavior
QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Round);
#else
2017-05-30 14:42:21 -06:00
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
2021-04-24 18:18:28 -06:00
#endif
QCoreApplication::setOrganizationName(QStringLiteral("Dolphin Emulator"));
QCoreApplication::setOrganizationDomain(QStringLiteral("dolphin-emu.org"));
2018-01-01 05:31:53 -07:00
QCoreApplication::setApplicationName(QStringLiteral("dolphin-emu"));
2017-05-30 14:42:21 -06:00
2020-09-21 09:25:47 -06:00
#ifdef _WIN32
QApplication app(__argc, __argv);
#else
2015-11-27 01:33:07 -07:00
QApplication app(argc, argv);
2020-09-21 09:25:47 -06:00
#endif
2015-11-27 01:33:07 -07:00
2018-04-29 14:20:03 -06:00
#ifdef _WIN32
FreeConsole();
#endif
UICommon::SetUserDirectory(static_cast<const char*>(options.get("user")));
2015-11-27 01:33:07 -07:00
UICommon::CreateDirectories();
UICommon::Init();
Resources::Init();
Settings::Instance().SetBatchModeEnabled(options.is_set("batch"));
2015-11-27 01:33:07 -07:00
// Hook up alerts from core
Common::RegisterMsgAlertHandler(QtMsgAlertHandler);
2017-07-17 15:50:40 -06:00
// Hook up translations
Translation::Initialize();
// Whenever the event loop is about to go to sleep, dispatch the jobs
// queued in the Core first.
QObject::connect(QAbstractEventDispatcher::instance(), &QAbstractEventDispatcher::aboutToBlock,
&app, &Core::HostDispatchJobs);
std::optional<std::string> save_state_path;
if (options.is_set("save_state"))
{
save_state_path = static_cast<const char*>(options.get("save_state"));
}
std::unique_ptr<BootParameters> boot;
bool game_specified = false;
2017-11-26 10:32:28 -07:00
if (options.is_set("exec"))
{
2018-11-05 11:20:45 -07:00
const std::list<std::string> paths_list = options.all("exec");
const std::vector<std::string> paths{std::make_move_iterator(std::begin(paths_list)),
std::make_move_iterator(std::end(paths_list))};
boot = BootParameters::GenerateFromFile(
paths, BootSessionData(save_state_path, DeleteSavestateAfterBoot::No));
game_specified = true;
2017-11-26 10:32:28 -07:00
}
else if (options.is_set("nand_title"))
{
const std::string hex_string = static_cast<const char*>(options.get("nand_title"));
if (hex_string.length() == 16)
{
const u64 title_id = std::stoull(hex_string, nullptr, 16);
boot = std::make_unique<BootParameters>(BootParameters::NANDTitle{title_id});
}
else
{
2019-03-04 12:49:00 -07:00
ModalMessageBox::critical(nullptr, QObject::tr("Error"), QObject::tr("Invalid title ID."));
}
game_specified = true;
}
else if (!args.empty())
{
boot = BootParameters::GenerateFromFile(
args.front(), BootSessionData(save_state_path, DeleteSavestateAfterBoot::No));
game_specified = true;
}
2018-03-20 00:41:47 -06:00
int retval;
if (save_state_path && !game_specified)
{
ModalMessageBox::critical(
nullptr, QObject::tr("Error"),
QObject::tr("A save state cannot be loaded without specifying a game to launch."));
retval = 1;
}
else if (Settings::Instance().IsBatchModeEnabled() && !game_specified)
{
ModalMessageBox::critical(
nullptr, QObject::tr("Error"),
QObject::tr("Batch mode cannot be used without specifying a game to launch."));
retval = 1;
}
else if (!boot && (Settings::Instance().IsBatchModeEnabled() || save_state_path))
{
// A game to launch was specified, but it was invalid.
// An error has already been shown by code above, so exit without showing another error.
retval = 1;
}
else
{
DolphinAnalytics::Instance().ReportDolphinStart("qt");
2017-05-20 15:41:02 -06:00
MainWindow win{std::move(boot), static_cast<const char*>(options.get("movie"))};
Settings::Instance().SetCurrentUserStyle(Settings::Instance().GetCurrentUserStyle());
if (options.is_set("debugger"))
Settings::Instance().SetDebugModeEnabled(true);
win.Show();
2017-05-20 15:41:02 -06:00
#if defined(USE_ANALYTICS) && USE_ANALYTICS
if (!Config::Get(Config::MAIN_ANALYTICS_PERMISSION_ASKED))
2017-05-20 15:41:02 -06:00
{
2019-03-04 12:49:00 -07:00
ModalMessageBox analytics_prompt(&win);
2017-05-20 15:41:02 -06:00
analytics_prompt.setIcon(QMessageBox::Question);
analytics_prompt.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
analytics_prompt.setWindowTitle(QObject::tr("Allow Usage Statistics Reporting"));
analytics_prompt.setText(
QObject::tr("Do you authorize Dolphin to report information to Dolphin's developers?"));
2017-05-20 15:41:02 -06:00
analytics_prompt.setInformativeText(
QObject::tr("If authorized, Dolphin can collect data on its performance, "
"feature usage, and configuration, as well as data on your system's "
"hardware and operating system.\n\n"
"No private data is ever collected. This data helps us understand "
"how people and emulated games use Dolphin and prioritize our "
"efforts. It also helps us identify rare configurations that are "
"causing bugs, performance and stability issues.\n"
"This authorization can be revoked at any time through Dolphin's "
"settings."));
2017-05-20 15:41:02 -06:00
const int answer = analytics_prompt.exec();
Config::SetBase(Config::MAIN_ANALYTICS_PERMISSION_ASKED, true);
Settings::Instance().SetAnalyticsEnabled(answer == QMessageBox::Yes);
2017-05-20 15:41:02 -06:00
DolphinAnalytics::Instance().ReloadConfig();
2017-05-20 15:41:02 -06:00
}
#endif
if (!Settings::Instance().IsBatchModeEnabled())
{
auto* updater = new Updater(&win, Config::Get(Config::MAIN_AUTOUPDATE_UPDATE_TRACK),
Config::Get(Config::MAIN_AUTOUPDATE_HASH_OVERRIDE));
updater->start();
}
retval = app.exec();
}
2015-11-27 01:33:07 -07:00
Core::Shutdown();
UICommon::Shutdown();
Host::GetInstance()->deleteLater();
return retval;
}
#ifdef _WIN32
int WINAPI wWinMain(_In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ LPWSTR, _In_ int)
{
std::vector<std::string> args = CommandLineToUtf8Argv(GetCommandLineW());
const int argc = static_cast<int>(args.size());
std::vector<char*> argv(args.size());
for (size_t i = 0; i < args.size(); ++i)
argv[i] = args[i].data();
return main(argc, argv.data());
}
#undef main
#endif