| 1 | // Copyright 2015 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #ifdef _WIN32 |
| 5 | #include <cstdio> |
| 6 | #include <string> |
| 7 | #include <vector> |
| 8 | |
| 9 | #include <Windows.h> |
| 10 | #endif |
| 11 | |
| 12 | #ifdef __linux__ |
| 13 | #include <cstdlib> |
| 14 | #endif |
| 15 | |
| 16 | #include <OptionParser.h> |
| 17 | #include <QAbstractEventDispatcher> |
| 18 | #include <QApplication> |
| 19 | #include <QObject> |
| 20 | #include <QPushButton> |
| 21 | #include <QWidget> |
| 22 | |
| 23 | #include "Common/Config/Config.h" |
| 24 | #include "Common/MsgHandler.h" |
| 25 | #include "Common/ScopeGuard.h" |
| 26 | #include "Common/StringUtil.h" |
| 27 | |
| 28 | #include "Core/Boot/Boot.h" |
| 29 | #include "Core/Config/MainSettings.h" |
| 30 | #include "Core/Core.h" |
| 31 | #include "Core/DolphinAnalytics.h" |
| 32 | #include "Core/System.h" |
| 33 | |
| 34 | #include "DolphinQt/Host.h" |
| 35 | #include "DolphinQt/MainWindow.h" |
| 36 | #include "DolphinQt/QtUtils/AnalyticsPrompt.h" |
| 37 | #include "DolphinQt/QtUtils/ModalMessageBox.h" |
| 38 | #include "DolphinQt/QtUtils/RunOnObject.h" |
| 39 | #ifdef _WIN32 |
| 40 | #include "DolphinQt/QtUtils/SetWindowDecorations.h" |
| 41 | #endif |
| 42 | #include "DolphinQt/Resources.h" |
| 43 | #include "DolphinQt/Settings.h" |
| 44 | #include "DolphinQt/Translation.h" |
| 45 | #include "DolphinQt/Updater.h" |
| 46 | |
| 47 | #include "UICommon/CommandLineParse.h" |
| 48 | #include "UICommon/UICommon.h" |
| 49 | |
| 50 | static bool QtMsgAlertHandler(const char* caption, const char* text, bool yes_no, |
| 51 | Common::MsgType style) |
| 52 | { |
| 53 | const bool called_from_cpu_thread = Core::IsCPUThread(); |
| 54 | const bool called_from_gpu_thread = Core::IsGPUThread(); |
| 55 | |
| 56 | std::optional<bool> r = RunOnObject(QApplication::instance(), [&] { |
| 57 | // If we were called from the CPU/GPU thread, set us as the CPU/GPU thread. |
| 58 | // This information is used in order to avoid deadlocks when calling e.g. |
| 59 | // Host::SetRenderFocus or Core::CPUThreadGuard. (Host::SetRenderFocus |
| 60 | // can get called automatically when a dialog steals the focus.) |
| 61 | |
| 62 | Common::ScopeGuard cpu_scope_guard(&Core::UndeclareAsCPUThread); |
| 63 | Common::ScopeGuard gpu_scope_guard(&Core::UndeclareAsGPUThread); |
| 64 | |
| 65 | if (!called_from_cpu_thread) |
| 66 | cpu_scope_guard.Dismiss(); |
| 67 | if (!called_from_gpu_thread) |
| 68 | gpu_scope_guard.Dismiss(); |
| 69 | |
| 70 | if (called_from_cpu_thread) |
| 71 | Core::DeclareAsCPUThread(); |
| 72 | if (called_from_gpu_thread) |
| 73 | Core::DeclareAsGPUThread(); |
| 74 | |
| 75 | ModalMessageBox message_box(QApplication::activeWindow(), Qt::ApplicationModal); |
| 76 | message_box.setWindowTitle(QString::fromUtf8(caption)); |
| 77 | message_box.setText(QString::fromUtf8(text)); |
| 78 | |
| 79 | message_box.setStandardButtons(yes_no ? QMessageBox::Yes | QMessageBox::No : QMessageBox::Ok); |
| 80 | if (style == Common::MsgType::Warning) |
| 81 | message_box.addButton(QMessageBox::Ignore)->setText(QObject::tr("Ignore for this session")); |
| 82 | |
| 83 | message_box.setIcon([&] { |
| 84 | switch (style) |
| 85 | { |
| 86 | case Common::MsgType::Information: |
| 87 | return QMessageBox::Information; |
| 88 | case Common::MsgType::Question: |
| 89 | return QMessageBox::Question; |
| 90 | case Common::MsgType::Warning: |
| 91 | return QMessageBox::Warning; |
| 92 | case Common::MsgType::Critical: |
| 93 | return QMessageBox::Critical; |
| 94 | } |
| 95 | // appease MSVC |
| 96 | return QMessageBox::NoIcon; |
| 97 | }()); |
| 98 | |
| 99 | const int button = message_box.exec(); |
| 100 | if (button == QMessageBox::Yes) |
| 101 | return true; |
| 102 | |
| 103 | if (button == QMessageBox::Ignore) |
| 104 | { |
| 105 | Config::SetCurrent(Config::MAIN_USE_PANIC_HANDLERS, false); |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | return false; |
| 110 | }); |
| 111 | if (r.has_value()) |
| 112 | return *r; |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | #ifdef _WIN32 |
| 117 | #define main app_main |
| 118 | #endif |
| 119 | |
| 120 | int main(int argc, char* argv[]) |
| 121 | { |
| 122 | #ifdef _WIN32 |
| 123 | const bool console_attached = AttachConsole(ATTACH_PARENT_PROCESS) != FALSE; |
| 124 | HANDLE stdout_handle = ::GetStdHandle(STD_OUTPUT_HANDLE); |
| 125 | if (console_attached && stdout_handle) |
| 126 | { |
| 127 | freopen("CONOUT$", "w", stdout); |
| 128 | freopen("CONOUT$", "w", stderr); |
| 129 | } |
| 130 | #endif |
| 131 | |
| 132 | #ifdef __APPLE__ |
| 133 | // On macOS, a command line option matching the format "-psn_X_XXXXXX" is passed when |
| 134 | // the application is launched for the first time. This is to set the "ProcessSerialNumber", |
| 135 | // something used by the legacy Process Manager from Carbon. optparse will fail if it finds |
| 136 | // this as it isn't a valid Dolphin command line option, so pretend like it doesn't exist |
| 137 | // if found. |
| 138 | if (strncmp(argv[argc - 1], "-psn", 4) == 0) |
| 139 | { |
| 140 | argc--; |
| 141 | } |
| 142 | #endif |
| 143 | |
| 144 | #ifdef __linux__ |
| 145 | // Qt 6.3+ has a bug which causes mouse inputs to not be registered in our XInput2 code. |
| 146 | // If we define QT_XCB_NO_XI2, Qt's xcb platform plugin no longer initializes its XInput |
| 147 | // code, which makes mouse inputs work again. |
| 148 | // For more information: https://bugs.dolphin-emu.org/issues/12913 |
| 149 | #if (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0)) |
| 150 | setenv("QT_XCB_NO_XI2", "1", true); |
| 151 | #endif |
| 152 | // Dolphin currently doesn't work on Wayland (Only the UI does, games do not launch.) This makes |
| 153 | // XCB the default and forces it on if the platform is specified to be wayland, to prevent this |
| 154 | // from happening. |
| 155 | // For more information: https://bugs.dolphin-emu.org/issues/11807 |
| 156 | const char* current_qt_platform = getenv("QT_QPA_PLATFORM"); |
| 157 | const bool replace_qt_platform = current_qt_platform != nullptr && |
| 158 | Common::CaseInsensitiveContains(current_qt_platform, "wayland"); |
| 159 | setenv("QT_QPA_PLATFORM", "xcb", replace_qt_platform); |
| 160 | #endif |
| 161 | |
| 162 | QCoreApplication::setOrganizationName(QStringLiteral("Dolphin Emulator")); |
| 163 | QCoreApplication::setOrganizationDomain(QStringLiteral("dolphin-emu.org")); |
| 164 | QCoreApplication::setApplicationName(QStringLiteral("dolphin-emu")); |
| 165 | |
| 166 | // QApplication will parse arguments and remove any it recognizes as targeting Qt |
| 167 | QApplication app(argc, argv); |
| 168 | |
| 169 | auto parser = CommandLineParse::CreateParser(CommandLineParse::ParserOptions::IncludeGUIOptions); |
| 170 | const optparse::Values& options = CommandLineParse::ParseArguments(parser.get(), argc, argv); |
| 171 | const std::vector<std::string> args = parser->args(); |
| 172 | |
| 173 | #ifdef _WIN32 |
| 174 | QtUtils::InstallWindowDecorationFilter(&app); |
| 175 | |
| 176 | FreeConsole(); |
| 177 | #endif |
| 178 | |
| 179 | UICommon::SetUserDirectory(static_cast<const char*>(options.get("user"))); |
| 180 | UICommon::CreateDirectories(); |
| 181 | UICommon::Init(); |
| 182 | Resources::Init(); |
| 183 | Settings::Instance().SetBatchModeEnabled(options.is_set("batch")); |
| 184 | |
| 185 | // Hook up alerts from core |
| 186 | Common::RegisterMsgAlertHandler(QtMsgAlertHandler); |
| 187 | |
| 188 | // Hook up translations |
| 189 | Translation::Initialize(); |
| 190 | |
| 191 | // Whenever the event loop is about to go to sleep, dispatch the jobs |
| 192 | // queued in the Core first. |
| 193 | QObject::connect(QAbstractEventDispatcher::instance(), &QAbstractEventDispatcher::aboutToBlock, |
| 194 | &app, [] { Core::HostDispatchJobs(Core::System::GetInstance()); }); |
| 195 | |
| 196 | std::optional<std::string> save_state_path; |
| 197 | if (options.is_set("save_state")) |
| 198 | { |
| 199 | save_state_path = static_cast<const char*>(options.get("save_state")); |
| 200 | } |
| 201 | |
| 202 | std::unique_ptr<BootParameters> boot; |
| 203 | bool game_specified = false; |
| 204 | if (options.is_set("exec")) |
| 205 | { |
| 206 | const std::list<std::string> paths_list = options.all("exec"); |
| 207 | const std::vector<std::string> paths{std::make_move_iterator(std::begin(paths_list)), |
| 208 | std::make_move_iterator(std::end(paths_list))}; |
| 209 | boot = BootParameters::GenerateFromFile( |
| 210 | paths, BootSessionData(save_state_path, DeleteSavestateAfterBoot::No)); |
| 211 | game_specified = true; |
| 212 | } |
| 213 | else if (options.is_set("nand_title")) |
| 214 | { |
| 215 | const std::string hex_string = static_cast<const char*>(options.get("nand_title")); |
| 216 | if (hex_string.length() == 16) |
| 217 | { |
| 218 | const u64 title_id = std::stoull(hex_string, nullptr, 16); |
| 219 | boot = std::make_unique<BootParameters>(BootParameters::NANDTitle{title_id}); |
| 220 | } |
| 221 | else |
| 222 | { |
| 223 | ModalMessageBox::critical(nullptr, QObject::tr("Error"), QObject::tr("Invalid title ID.")); |
| 224 | } |
| 225 | game_specified = true; |
| 226 | } |
| 227 | else if (!args.empty()) |
| 228 | { |
| 229 | boot = BootParameters::GenerateFromFile( |
| 230 | args.front(), BootSessionData(save_state_path, DeleteSavestateAfterBoot::No)); |
| 231 | game_specified = true; |
| 232 | } |
| 233 | |
| 234 | int retval; |
| 235 | |
| 236 | if (save_state_path && !game_specified) |
| 237 | { |
| 238 | ModalMessageBox::critical( |
| 239 | nullptr, QObject::tr("Error"), |
| 240 | QObject::tr("A save state cannot be loaded without specifying a game to launch.")); |
| 241 | retval = 1; |
| 242 | } |
| 243 | else if (Settings::Instance().IsBatchModeEnabled() && !game_specified) |
| 244 | { |
| 245 | ModalMessageBox::critical( |
| 246 | nullptr, QObject::tr("Error"), |
| 247 | QObject::tr("Batch mode cannot be used without specifying a game to launch.")); |
| 248 | retval = 1; |
| 249 | } |
| 250 | else if (!boot && (Settings::Instance().IsBatchModeEnabled() || save_state_path)) |
| 251 | { |
| 252 | // A game to launch was specified, but it was invalid. |
| 253 | // An error has already been shown by code above, so exit without showing another error. |
| 254 | retval = 1; |
| 255 | } |
| 256 | else |
| 257 | { |
| 258 | DolphinAnalytics::Instance().ReportDolphinStart("qt"); |
| 259 | |
| 260 | Settings::Instance().InitDefaultPalette(); |
| 261 | Settings::Instance().ApplyStyle(); |
| 262 | |
| 263 | MainWindow win{Core::System::GetInstance(), std::move(boot), |
| 264 | static_cast<const char*>(options.get("movie"))}; |
| 265 | |
| 266 | #if defined(USE_ANALYTICS) && USE_ANALYTICS |
| 267 | if (!Config::Get(Config::MAIN_ANALYTICS_PERMISSION_ASKED)) |
| 268 | { |
| 269 | // To ensure that the analytics prompt appears aligned with the center of the main window, |
| 270 | // the dialog is only shown after the application is ready, as only then it is guaranteed that |
| 271 | // the main window has been placed in its final position. |
| 272 | auto* const connection_context = new QObject(&win); |
| 273 | QObject::connect(qApp, &QGuiApplication::applicationStateChanged, connection_context, |
| 274 | [connection_context, &win](const Qt::ApplicationState state) { |
| 275 | if (state != Qt::ApplicationState::ApplicationActive) |
| 276 | return; |
| 277 | |
| 278 | // Severe the connection after the first run. |
| 279 | delete connection_context; |
| 280 | |
| 281 | ShowAnalyticsPrompt(&win); |
| 282 | }); |
| 283 | } |
| 284 | #endif |
| 285 | |
| 286 | if (!Settings::Instance().IsBatchModeEnabled()) |
| 287 | { |
| 288 | auto* updater = new Updater(&win, Config::Get(Config::MAIN_AUTOUPDATE_UPDATE_TRACK), |
| 289 | Config::Get(Config::MAIN_AUTOUPDATE_HASH_OVERRIDE)); |
| 290 | updater->start(); |
| 291 | } |
| 292 | |
| 293 | retval = app.exec(); |
| 294 | } |
| 295 | |
| 296 | Core::Shutdown(Core::System::GetInstance()); |
| 297 | UICommon::Shutdown(); |
| 298 | Host::GetInstance()->deleteLater(); |
| 299 | |
| 300 | return retval; |
| 301 | } |
| 302 | |
| 303 | #ifdef _WIN32 |
| 304 | int WINAPI wWinMain(_In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ LPWSTR, _In_ int) |
| 305 | { |
| 306 | std::vector<std::string> args = Common::CommandLineToUtf8Argv(GetCommandLineW()); |
| 307 | const int argc = static_cast<int>(args.size()); |
| 308 | std::vector<char*> argv(args.size()); |
| 309 | for (size_t i = 0; i < args.size(); ++i) |
| 310 | argv[i] = args[i].data(); |
| 311 | |
| 312 | return main(argc, argv.data()); |
| 313 | } |
| 314 | |
| 315 | #undef main |
| 316 | #endif |