main.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cinttypes>
  5. #include <clocale>
  6. #include <memory>
  7. #include <thread>
  8. #include <glad/glad.h>
  9. #define QT_NO_OPENGL
  10. #include <QDesktopWidget>
  11. #include <QFileDialog>
  12. #include <QMessageBox>
  13. #include <QtGui>
  14. #include <QtWidgets>
  15. #include "common/logging/backend.h"
  16. #include "common/logging/filter.h"
  17. #include "common/logging/log.h"
  18. #include "common/logging/text_formatter.h"
  19. #include "common/microprofile.h"
  20. #include "common/platform.h"
  21. #include "common/scm_rev.h"
  22. #include "common/scope_exit.h"
  23. #include "common/string_util.h"
  24. #include "core/core.h"
  25. #include "core/gdbstub/gdbstub.h"
  26. #include "core/loader/loader.h"
  27. #include "core/settings.h"
  28. #include "yuzu/about_dialog.h"
  29. #include "yuzu/bootmanager.h"
  30. #include "yuzu/configuration/config.h"
  31. #include "yuzu/configuration/configure_dialog.h"
  32. #include "yuzu/debugger/profiler.h"
  33. #include "yuzu/debugger/registers.h"
  34. #include "yuzu/debugger/wait_tree.h"
  35. #include "yuzu/game_list.h"
  36. #include "yuzu/hotkeys.h"
  37. #include "yuzu/main.h"
  38. #include "yuzu/ui_settings.h"
  39. #ifdef QT_STATICPLUGIN
  40. Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin);
  41. #endif
  42. /**
  43. * "Callouts" are one-time instructional messages shown to the user. In the config settings, there
  44. * is a bitfield "callout_flags" options, used to track if a message has already been shown to the
  45. * user. This is 32-bits - if we have more than 32 callouts, we should retire and recyle old ones.
  46. */
  47. enum class CalloutFlag : uint32_t {
  48. Telemetry = 0x1,
  49. };
  50. static void ShowCalloutMessage(const QString& message, CalloutFlag flag) {
  51. if (UISettings::values.callout_flags & static_cast<uint32_t>(flag)) {
  52. return;
  53. }
  54. UISettings::values.callout_flags |= static_cast<uint32_t>(flag);
  55. QMessageBox msg;
  56. msg.setText(message);
  57. msg.setStandardButtons(QMessageBox::Ok);
  58. msg.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  59. msg.setStyleSheet("QLabel{min-width: 900px;}");
  60. msg.exec();
  61. }
  62. void GMainWindow::ShowCallouts() {}
  63. GMainWindow::GMainWindow() : config(new Config()), emu_thread(nullptr) {
  64. setAcceptDrops(true);
  65. ui.setupUi(this);
  66. statusBar()->hide();
  67. InitializeWidgets();
  68. InitializeDebugWidgets();
  69. InitializeRecentFileMenuActions();
  70. InitializeHotkeys();
  71. SetDefaultUIGeometry();
  72. RestoreUIState();
  73. ConnectMenuEvents();
  74. ConnectWidgetEvents();
  75. setWindowTitle(QString("yuzu %1| %2-%3")
  76. .arg(Common::g_build_name, Common::g_scm_branch, Common::g_scm_desc));
  77. show();
  78. game_list->PopulateAsync(UISettings::values.gamedir, UISettings::values.gamedir_deepscan);
  79. // Show one-time "callout" messages to the user
  80. ShowCallouts();
  81. QStringList args = QApplication::arguments();
  82. if (args.length() >= 2) {
  83. BootGame(args[1]);
  84. }
  85. }
  86. GMainWindow::~GMainWindow() {
  87. // will get automatically deleted otherwise
  88. if (render_window->parent() == nullptr)
  89. delete render_window;
  90. }
  91. void GMainWindow::InitializeWidgets() {
  92. render_window = new GRenderWindow(this, emu_thread.get());
  93. render_window->hide();
  94. game_list = new GameList(this);
  95. ui.horizontalLayout->addWidget(game_list);
  96. // Create status bar
  97. message_label = new QLabel();
  98. // Configured separately for left alignment
  99. message_label->setVisible(false);
  100. message_label->setFrameStyle(QFrame::NoFrame);
  101. message_label->setContentsMargins(4, 0, 4, 0);
  102. message_label->setAlignment(Qt::AlignLeft);
  103. statusBar()->addPermanentWidget(message_label, 1);
  104. emu_speed_label = new QLabel();
  105. emu_speed_label->setToolTip(
  106. tr("Current emulation speed. Values higher or lower than 100% "
  107. "indicate emulation is running faster or slower than a Switch."));
  108. game_fps_label = new QLabel();
  109. game_fps_label->setToolTip(tr("How many frames per second the game is currently displaying. "
  110. "This will vary from game to game and scene to scene."));
  111. emu_frametime_label = new QLabel();
  112. emu_frametime_label->setToolTip(
  113. tr("Time taken to emulate a Switch frame, not counting framelimiting or v-sync. For "
  114. "full-speed emulation this should be at most 16.67 ms."));
  115. for (auto& label : {emu_speed_label, game_fps_label, emu_frametime_label}) {
  116. label->setVisible(false);
  117. label->setFrameStyle(QFrame::NoFrame);
  118. label->setContentsMargins(4, 0, 4, 0);
  119. statusBar()->addPermanentWidget(label, 0);
  120. }
  121. statusBar()->setVisible(true);
  122. setStyleSheet("QStatusBar::item{border: none;}");
  123. }
  124. void GMainWindow::InitializeDebugWidgets() {
  125. QMenu* debug_menu = ui.menu_View_Debugging;
  126. #if MICROPROFILE_ENABLED
  127. microProfileDialog = new MicroProfileDialog(this);
  128. microProfileDialog->hide();
  129. debug_menu->addAction(microProfileDialog->toggleViewAction());
  130. #endif
  131. registersWidget = new RegistersWidget(this);
  132. addDockWidget(Qt::RightDockWidgetArea, registersWidget);
  133. registersWidget->hide();
  134. debug_menu->addAction(registersWidget->toggleViewAction());
  135. connect(this, &GMainWindow::EmulationStarting, registersWidget,
  136. &RegistersWidget::OnEmulationStarting);
  137. connect(this, &GMainWindow::EmulationStopping, registersWidget,
  138. &RegistersWidget::OnEmulationStopping);
  139. waitTreeWidget = new WaitTreeWidget(this);
  140. addDockWidget(Qt::LeftDockWidgetArea, waitTreeWidget);
  141. waitTreeWidget->hide();
  142. debug_menu->addAction(waitTreeWidget->toggleViewAction());
  143. connect(this, &GMainWindow::EmulationStarting, waitTreeWidget,
  144. &WaitTreeWidget::OnEmulationStarting);
  145. connect(this, &GMainWindow::EmulationStopping, waitTreeWidget,
  146. &WaitTreeWidget::OnEmulationStopping);
  147. }
  148. void GMainWindow::InitializeRecentFileMenuActions() {
  149. for (int i = 0; i < max_recent_files_item; ++i) {
  150. actions_recent_files[i] = new QAction(this);
  151. actions_recent_files[i]->setVisible(false);
  152. connect(actions_recent_files[i], &QAction::triggered, this, &GMainWindow::OnMenuRecentFile);
  153. ui.menu_recent_files->addAction(actions_recent_files[i]);
  154. }
  155. UpdateRecentFiles();
  156. }
  157. void GMainWindow::InitializeHotkeys() {
  158. RegisterHotkey("Main Window", "Load File", QKeySequence::Open);
  159. RegisterHotkey("Main Window", "Start Emulation");
  160. RegisterHotkey("Main Window", "Fullscreen", QKeySequence::FullScreen);
  161. RegisterHotkey("Main Window", "Exit Fullscreen", QKeySequence::Cancel, Qt::ApplicationShortcut);
  162. LoadHotkeys();
  163. connect(GetHotkey("Main Window", "Load File", this), &QShortcut::activated, this,
  164. &GMainWindow::OnMenuLoadFile);
  165. connect(GetHotkey("Main Window", "Start Emulation", this), &QShortcut::activated, this,
  166. &GMainWindow::OnStartGame);
  167. connect(GetHotkey("Main Window", "Fullscreen", render_window), &QShortcut::activated,
  168. ui.action_Fullscreen, &QAction::trigger);
  169. connect(GetHotkey("Main Window", "Fullscreen", render_window), &QShortcut::activatedAmbiguously,
  170. ui.action_Fullscreen, &QAction::trigger);
  171. connect(GetHotkey("Main Window", "Exit Fullscreen", this), &QShortcut::activated, this, [&] {
  172. if (emulation_running) {
  173. ui.action_Fullscreen->setChecked(false);
  174. ToggleFullscreen();
  175. }
  176. });
  177. }
  178. void GMainWindow::SetDefaultUIGeometry() {
  179. // geometry: 55% of the window contents are in the upper screen half, 45% in the lower half
  180. const QRect screenRect = QApplication::desktop()->screenGeometry(this);
  181. const int w = screenRect.width() * 2 / 3;
  182. const int h = screenRect.height() / 2;
  183. const int x = (screenRect.x() + screenRect.width()) / 2 - w / 2;
  184. const int y = (screenRect.y() + screenRect.height()) / 2 - h * 55 / 100;
  185. setGeometry(x, y, w, h);
  186. }
  187. void GMainWindow::RestoreUIState() {
  188. restoreGeometry(UISettings::values.geometry);
  189. restoreState(UISettings::values.state);
  190. render_window->restoreGeometry(UISettings::values.renderwindow_geometry);
  191. #if MICROPROFILE_ENABLED
  192. microProfileDialog->restoreGeometry(UISettings::values.microprofile_geometry);
  193. microProfileDialog->setVisible(UISettings::values.microprofile_visible);
  194. #endif
  195. game_list->LoadInterfaceLayout();
  196. ui.action_Single_Window_Mode->setChecked(UISettings::values.single_window_mode);
  197. ToggleWindowMode();
  198. ui.action_Fullscreen->setChecked(UISettings::values.fullscreen);
  199. ui.action_Display_Dock_Widget_Headers->setChecked(UISettings::values.display_titlebar);
  200. OnDisplayTitleBars(ui.action_Display_Dock_Widget_Headers->isChecked());
  201. ui.action_Show_Filter_Bar->setChecked(UISettings::values.show_filter_bar);
  202. game_list->setFilterVisible(ui.action_Show_Filter_Bar->isChecked());
  203. ui.action_Show_Status_Bar->setChecked(UISettings::values.show_status_bar);
  204. statusBar()->setVisible(ui.action_Show_Status_Bar->isChecked());
  205. }
  206. void GMainWindow::ConnectWidgetEvents() {
  207. connect(game_list, &GameList::GameChosen, this, &GMainWindow::OnGameListLoadFile);
  208. connect(game_list, &GameList::OpenSaveFolderRequested, this,
  209. &GMainWindow::OnGameListOpenSaveFolder);
  210. connect(this, &GMainWindow::EmulationStarting, render_window,
  211. &GRenderWindow::OnEmulationStarting);
  212. connect(this, &GMainWindow::EmulationStopping, render_window,
  213. &GRenderWindow::OnEmulationStopping);
  214. connect(&status_bar_update_timer, &QTimer::timeout, this, &GMainWindow::UpdateStatusBar);
  215. }
  216. void GMainWindow::ConnectMenuEvents() {
  217. // File
  218. connect(ui.action_Load_File, &QAction::triggered, this, &GMainWindow::OnMenuLoadFile);
  219. connect(ui.action_Select_Game_List_Root, &QAction::triggered, this,
  220. &GMainWindow::OnMenuSelectGameListRoot);
  221. connect(ui.action_Exit, &QAction::triggered, this, &QMainWindow::close);
  222. // Emulation
  223. connect(ui.action_Start, &QAction::triggered, this, &GMainWindow::OnStartGame);
  224. connect(ui.action_Pause, &QAction::triggered, this, &GMainWindow::OnPauseGame);
  225. connect(ui.action_Stop, &QAction::triggered, this, &GMainWindow::OnStopGame);
  226. connect(ui.action_Configure, &QAction::triggered, this, &GMainWindow::OnConfigure);
  227. // View
  228. connect(ui.action_Single_Window_Mode, &QAction::triggered, this,
  229. &GMainWindow::ToggleWindowMode);
  230. connect(ui.action_Display_Dock_Widget_Headers, &QAction::triggered, this,
  231. &GMainWindow::OnDisplayTitleBars);
  232. ui.action_Show_Filter_Bar->setShortcut(tr("CTRL+F"));
  233. connect(ui.action_Show_Filter_Bar, &QAction::triggered, this, &GMainWindow::OnToggleFilterBar);
  234. connect(ui.action_Show_Status_Bar, &QAction::triggered, statusBar(), &QStatusBar::setVisible);
  235. // Fullscreen
  236. ui.action_Fullscreen->setShortcut(GetHotkey("Main Window", "Fullscreen", this)->key());
  237. connect(ui.action_Fullscreen, &QAction::triggered, this, &GMainWindow::ToggleFullscreen);
  238. // Help
  239. connect(ui.action_About, &QAction::triggered, this, &GMainWindow::OnAbout);
  240. }
  241. void GMainWindow::OnDisplayTitleBars(bool show) {
  242. QList<QDockWidget*> widgets = findChildren<QDockWidget*>();
  243. if (show) {
  244. for (QDockWidget* widget : widgets) {
  245. QWidget* old = widget->titleBarWidget();
  246. widget->setTitleBarWidget(nullptr);
  247. if (old != nullptr)
  248. delete old;
  249. }
  250. } else {
  251. for (QDockWidget* widget : widgets) {
  252. QWidget* old = widget->titleBarWidget();
  253. widget->setTitleBarWidget(new QWidget());
  254. if (old != nullptr)
  255. delete old;
  256. }
  257. }
  258. }
  259. bool GMainWindow::LoadROM(const QString& filename) {
  260. // Shutdown previous session if the emu thread is still active...
  261. if (emu_thread != nullptr)
  262. ShutdownGame();
  263. render_window->InitRenderTarget();
  264. render_window->MakeCurrent();
  265. if (!gladLoadGL()) {
  266. QMessageBox::critical(this, tr("Error while initializing OpenGL 3.3 Core!"),
  267. tr("Your GPU may not support OpenGL 3.3, or you do not "
  268. "have the latest graphics driver."));
  269. return false;
  270. }
  271. Core::System& system{Core::System::GetInstance()};
  272. const Core::System::ResultStatus result{system.Load(render_window, filename.toStdString())};
  273. Core::Telemetry().AddField(Telemetry::FieldType::App, "Frontend", "Qt");
  274. if (result != Core::System::ResultStatus::Success) {
  275. switch (result) {
  276. case Core::System::ResultStatus::ErrorGetLoader:
  277. LOG_CRITICAL(Frontend, "Failed to obtain loader for %s!",
  278. filename.toStdString().c_str());
  279. QMessageBox::critical(this, tr("Error while loading ROM!"),
  280. tr("The ROM format is not supported."));
  281. break;
  282. case Core::System::ResultStatus::ErrorSystemMode:
  283. LOG_CRITICAL(Frontend, "Failed to load ROM!");
  284. QMessageBox::critical(this, tr("Error while loading ROM!"),
  285. tr("Could not determine the system mode."));
  286. break;
  287. case Core::System::ResultStatus::ErrorLoader_ErrorEncrypted: {
  288. QMessageBox::critical(
  289. this, tr("Error while loading ROM!"),
  290. tr("The game that you are trying to load must be decrypted before being used with "
  291. "yuzu. A real Switch is required.<br/><br/>"
  292. "For more information on dumping and decrypting games, please see the following "
  293. "wiki pages: <ul>"
  294. "<li><a href='https://citra-emu.org/wiki/dumping-game-cartridges/'>Dumping Game "
  295. "Cartridges</a></li>"
  296. "<li><a href='https://citra-emu.org/wiki/dumping-installed-titles/'>Dumping "
  297. "Installed Titles</a></li>"
  298. "</ul>"));
  299. break;
  300. }
  301. case Core::System::ResultStatus::ErrorLoader_ErrorInvalidFormat:
  302. QMessageBox::critical(this, tr("Error while loading ROM!"),
  303. tr("The ROM format is not supported."));
  304. break;
  305. case Core::System::ResultStatus::ErrorVideoCore:
  306. QMessageBox::critical(
  307. this, tr("An error occured in the video core."),
  308. tr("yuzu has encountered an error while running the video core, please see the "
  309. "log for more details."
  310. "For more information on accessing the log, please see the following page: "
  311. "<a href='https://community.citra-emu.org/t/how-to-upload-the-log-file/296'>How "
  312. "to "
  313. "Upload the Log File</a>."
  314. "Ensure that you have the latest graphics drivers for your GPU."));
  315. break;
  316. default:
  317. QMessageBox::critical(
  318. this, tr("Error while loading ROM!"),
  319. tr("An unknown error occured. Please see the log for more details."));
  320. break;
  321. }
  322. return false;
  323. }
  324. return true;
  325. }
  326. void GMainWindow::BootGame(const QString& filename) {
  327. LOG_INFO(Frontend, "yuzu starting...");
  328. StoreRecentFile(filename); // Put the filename on top of the list
  329. if (!LoadROM(filename))
  330. return;
  331. // Create and start the emulation thread
  332. emu_thread = std::make_unique<EmuThread>(render_window);
  333. emit EmulationStarting(emu_thread.get());
  334. render_window->moveContext();
  335. emu_thread->start();
  336. connect(render_window, &GRenderWindow::Closed, this, &GMainWindow::OnStopGame);
  337. // BlockingQueuedConnection is important here, it makes sure we've finished refreshing our views
  338. // before the CPU continues
  339. connect(emu_thread.get(), &EmuThread::DebugModeEntered, registersWidget,
  340. &RegistersWidget::OnDebugModeEntered, Qt::BlockingQueuedConnection);
  341. connect(emu_thread.get(), &EmuThread::DebugModeEntered, waitTreeWidget,
  342. &WaitTreeWidget::OnDebugModeEntered, Qt::BlockingQueuedConnection);
  343. connect(emu_thread.get(), &EmuThread::DebugModeLeft, registersWidget,
  344. &RegistersWidget::OnDebugModeLeft, Qt::BlockingQueuedConnection);
  345. connect(emu_thread.get(), &EmuThread::DebugModeLeft, waitTreeWidget,
  346. &WaitTreeWidget::OnDebugModeLeft, Qt::BlockingQueuedConnection);
  347. // Update the GUI
  348. registersWidget->OnDebugModeEntered();
  349. if (ui.action_Single_Window_Mode->isChecked()) {
  350. game_list->hide();
  351. }
  352. status_bar_update_timer.start(2000);
  353. render_window->show();
  354. render_window->setFocus();
  355. emulation_running = true;
  356. if (ui.action_Fullscreen->isChecked()) {
  357. ShowFullscreen();
  358. }
  359. OnStartGame();
  360. }
  361. void GMainWindow::ShutdownGame() {
  362. emu_thread->RequestStop();
  363. emit EmulationStopping();
  364. // Wait for emulation thread to complete and delete it
  365. emu_thread->wait();
  366. emu_thread = nullptr;
  367. // The emulation is stopped, so closing the window or not does not matter anymore
  368. disconnect(render_window, &GRenderWindow::Closed, this, &GMainWindow::OnStopGame);
  369. // Update the GUI
  370. ui.action_Start->setEnabled(false);
  371. ui.action_Start->setText(tr("Start"));
  372. ui.action_Pause->setEnabled(false);
  373. ui.action_Stop->setEnabled(false);
  374. render_window->hide();
  375. game_list->show();
  376. game_list->setFilterFocus();
  377. // Disable status bar updates
  378. status_bar_update_timer.stop();
  379. message_label->setVisible(false);
  380. emu_speed_label->setVisible(false);
  381. game_fps_label->setVisible(false);
  382. emu_frametime_label->setVisible(false);
  383. emulation_running = false;
  384. }
  385. void GMainWindow::StoreRecentFile(const QString& filename) {
  386. UISettings::values.recent_files.prepend(filename);
  387. UISettings::values.recent_files.removeDuplicates();
  388. while (UISettings::values.recent_files.size() > max_recent_files_item) {
  389. UISettings::values.recent_files.removeLast();
  390. }
  391. UpdateRecentFiles();
  392. }
  393. void GMainWindow::UpdateRecentFiles() {
  394. unsigned int num_recent_files =
  395. std::min(UISettings::values.recent_files.size(), static_cast<int>(max_recent_files_item));
  396. for (unsigned int i = 0; i < num_recent_files; i++) {
  397. QString text = QString("&%1. %2").arg(i + 1).arg(
  398. QFileInfo(UISettings::values.recent_files[i]).fileName());
  399. actions_recent_files[i]->setText(text);
  400. actions_recent_files[i]->setData(UISettings::values.recent_files[i]);
  401. actions_recent_files[i]->setToolTip(UISettings::values.recent_files[i]);
  402. actions_recent_files[i]->setVisible(true);
  403. }
  404. for (int j = num_recent_files; j < max_recent_files_item; ++j) {
  405. actions_recent_files[j]->setVisible(false);
  406. }
  407. // Grey out the recent files menu if the list is empty
  408. if (num_recent_files == 0) {
  409. ui.menu_recent_files->setEnabled(false);
  410. } else {
  411. ui.menu_recent_files->setEnabled(true);
  412. }
  413. }
  414. void GMainWindow::OnGameListLoadFile(QString game_path) {
  415. BootGame(game_path);
  416. }
  417. void GMainWindow::OnGameListOpenSaveFolder(u64 program_id) {
  418. UNIMPLEMENTED();
  419. }
  420. void GMainWindow::OnMenuLoadFile() {
  421. QString extensions;
  422. for (const auto& piece : game_list->supported_file_extensions)
  423. extensions += "*." + piece + " ";
  424. QString file_filter = tr("Switch Executable") + " (" + extensions + ")";
  425. file_filter += ";;" + tr("All Files (*.*)");
  426. QString filename = QFileDialog::getOpenFileName(this, tr("Load File"),
  427. UISettings::values.roms_path, file_filter);
  428. if (!filename.isEmpty()) {
  429. UISettings::values.roms_path = QFileInfo(filename).path();
  430. BootGame(filename);
  431. }
  432. }
  433. void GMainWindow::OnMenuSelectGameListRoot() {
  434. QString dir_path = QFileDialog::getExistingDirectory(this, tr("Select Directory"));
  435. if (!dir_path.isEmpty()) {
  436. UISettings::values.gamedir = dir_path;
  437. game_list->PopulateAsync(dir_path, UISettings::values.gamedir_deepscan);
  438. }
  439. }
  440. void GMainWindow::OnMenuRecentFile() {
  441. QAction* action = qobject_cast<QAction*>(sender());
  442. assert(action);
  443. QString filename = action->data().toString();
  444. QFileInfo file_info(filename);
  445. if (file_info.exists()) {
  446. BootGame(filename);
  447. } else {
  448. // Display an error message and remove the file from the list.
  449. QMessageBox::information(this, tr("File not found"),
  450. tr("File \"%1\" not found").arg(filename));
  451. UISettings::values.recent_files.removeOne(filename);
  452. UpdateRecentFiles();
  453. }
  454. }
  455. void GMainWindow::OnStartGame() {
  456. emu_thread->SetRunning(true);
  457. qRegisterMetaType<Core::System::ResultStatus>("Core::System::ResultStatus");
  458. qRegisterMetaType<std::string>("std::string");
  459. connect(emu_thread.get(), &EmuThread::ErrorThrown, this, &GMainWindow::OnCoreError);
  460. ui.action_Start->setEnabled(false);
  461. ui.action_Start->setText(tr("Continue"));
  462. ui.action_Pause->setEnabled(true);
  463. ui.action_Stop->setEnabled(true);
  464. }
  465. void GMainWindow::OnPauseGame() {
  466. emu_thread->SetRunning(false);
  467. ui.action_Start->setEnabled(true);
  468. ui.action_Pause->setEnabled(false);
  469. ui.action_Stop->setEnabled(true);
  470. }
  471. void GMainWindow::OnStopGame() {
  472. ShutdownGame();
  473. }
  474. void GMainWindow::ToggleFullscreen() {
  475. if (!emulation_running) {
  476. return;
  477. }
  478. if (ui.action_Fullscreen->isChecked()) {
  479. ShowFullscreen();
  480. } else {
  481. HideFullscreen();
  482. }
  483. }
  484. void GMainWindow::ShowFullscreen() {
  485. if (ui.action_Single_Window_Mode->isChecked()) {
  486. UISettings::values.geometry = saveGeometry();
  487. ui.menubar->hide();
  488. statusBar()->hide();
  489. showFullScreen();
  490. } else {
  491. UISettings::values.renderwindow_geometry = render_window->saveGeometry();
  492. render_window->showFullScreen();
  493. }
  494. }
  495. void GMainWindow::HideFullscreen() {
  496. if (ui.action_Single_Window_Mode->isChecked()) {
  497. statusBar()->setVisible(ui.action_Show_Status_Bar->isChecked());
  498. ui.menubar->show();
  499. showNormal();
  500. restoreGeometry(UISettings::values.geometry);
  501. } else {
  502. render_window->showNormal();
  503. render_window->restoreGeometry(UISettings::values.renderwindow_geometry);
  504. }
  505. }
  506. void GMainWindow::ToggleWindowMode() {
  507. if (ui.action_Single_Window_Mode->isChecked()) {
  508. // Render in the main window...
  509. render_window->BackupGeometry();
  510. ui.horizontalLayout->addWidget(render_window);
  511. render_window->setFocusPolicy(Qt::ClickFocus);
  512. if (emulation_running) {
  513. render_window->setVisible(true);
  514. render_window->setFocus();
  515. game_list->hide();
  516. }
  517. } else {
  518. // Render in a separate window...
  519. ui.horizontalLayout->removeWidget(render_window);
  520. render_window->setParent(nullptr);
  521. render_window->setFocusPolicy(Qt::NoFocus);
  522. if (emulation_running) {
  523. render_window->setVisible(true);
  524. render_window->RestoreGeometry();
  525. game_list->show();
  526. }
  527. }
  528. }
  529. void GMainWindow::OnConfigure() {
  530. ConfigureDialog configureDialog(this);
  531. auto result = configureDialog.exec();
  532. if (result == QDialog::Accepted) {
  533. configureDialog.applyConfiguration();
  534. config->Save();
  535. }
  536. }
  537. void GMainWindow::OnAbout() {
  538. AboutDialog aboutDialog(this);
  539. aboutDialog.exec();
  540. }
  541. void GMainWindow::OnToggleFilterBar() {
  542. game_list->setFilterVisible(ui.action_Show_Filter_Bar->isChecked());
  543. if (ui.action_Show_Filter_Bar->isChecked()) {
  544. game_list->setFilterFocus();
  545. } else {
  546. game_list->clearFilter();
  547. }
  548. }
  549. void GMainWindow::UpdateStatusBar() {
  550. if (emu_thread == nullptr) {
  551. status_bar_update_timer.stop();
  552. return;
  553. }
  554. auto results = Core::System::GetInstance().GetAndResetPerfStats();
  555. emu_speed_label->setText(tr("Speed: %1%").arg(results.emulation_speed * 100.0, 0, 'f', 0));
  556. game_fps_label->setText(tr("Game: %1 FPS").arg(results.game_fps, 0, 'f', 0));
  557. emu_frametime_label->setText(tr("Frame: %1 ms").arg(results.frametime * 1000.0, 0, 'f', 2));
  558. emu_speed_label->setVisible(true);
  559. game_fps_label->setVisible(true);
  560. emu_frametime_label->setVisible(true);
  561. }
  562. void GMainWindow::OnCoreError(Core::System::ResultStatus result, std::string details) {
  563. QMessageBox::StandardButton answer;
  564. QString status_message;
  565. const QString common_message =
  566. tr("The game you are trying to load requires additional files from your 3DS to be dumped "
  567. "before playing.<br/><br/>For more information on dumping these files, please see the "
  568. "following wiki page: <a "
  569. "href='https://citra-emu.org/wiki/"
  570. "dumping-system-archives-and-the-shared-fonts-from-a-3ds-console/'>Dumping System "
  571. "Archives and the Shared Fonts from a 3DS Console</a>.<br/><br/>Would you like to quit "
  572. "back to the game list? Continuing emulation may result in crashes, corrupted save "
  573. "data, or other bugs.");
  574. switch (result) {
  575. case Core::System::ResultStatus::ErrorSystemFiles: {
  576. QString message = "Citra was unable to locate a 3DS system archive";
  577. if (!details.empty()) {
  578. message.append(tr(": %1. ").arg(details.c_str()));
  579. } else {
  580. message.append(". ");
  581. }
  582. message.append(common_message);
  583. answer = QMessageBox::question(this, tr("System Archive Not Found"), message,
  584. QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
  585. status_message = "System Archive Missing";
  586. break;
  587. }
  588. case Core::System::ResultStatus::ErrorSharedFont: {
  589. QString message = tr("Citra was unable to locate the 3DS shared fonts. ");
  590. message.append(common_message);
  591. answer = QMessageBox::question(this, tr("Shared Fonts Not Found"), message,
  592. QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
  593. status_message = "Shared Font Missing";
  594. break;
  595. }
  596. default:
  597. answer = QMessageBox::question(
  598. this, tr("Fatal Error"),
  599. tr("yuzu has encountered a fatal error, please see the log for more details. "
  600. "For more information on accessing the log, please see the following page: "
  601. "<a href='https://community.citra-emu.org/t/how-to-upload-the-log-file/296'>How to "
  602. "Upload the Log File</a>.<br/><br/>Would you like to quit back to the game list? "
  603. "Continuing emulation may result in crashes, corrupted save data, or other bugs."),
  604. QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
  605. status_message = "Fatal Error encountered";
  606. break;
  607. }
  608. if (answer == QMessageBox::Yes) {
  609. if (emu_thread) {
  610. ShutdownGame();
  611. }
  612. } else {
  613. // Only show the message if the game is still running.
  614. if (emu_thread) {
  615. emu_thread->SetRunning(true);
  616. message_label->setText(status_message);
  617. message_label->setVisible(true);
  618. }
  619. }
  620. }
  621. bool GMainWindow::ConfirmClose() {
  622. if (emu_thread == nullptr || !UISettings::values.confirm_before_closing)
  623. return true;
  624. QMessageBox::StandardButton answer =
  625. QMessageBox::question(this, tr("yuzu"), tr("Are you sure you want to close yuzu?"),
  626. QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
  627. return answer != QMessageBox::No;
  628. }
  629. void GMainWindow::closeEvent(QCloseEvent* event) {
  630. if (!ConfirmClose()) {
  631. event->ignore();
  632. return;
  633. }
  634. UISettings::values.geometry = saveGeometry();
  635. UISettings::values.state = saveState();
  636. UISettings::values.renderwindow_geometry = render_window->saveGeometry();
  637. #if MICROPROFILE_ENABLED
  638. UISettings::values.microprofile_geometry = microProfileDialog->saveGeometry();
  639. UISettings::values.microprofile_visible = microProfileDialog->isVisible();
  640. #endif
  641. UISettings::values.single_window_mode = ui.action_Single_Window_Mode->isChecked();
  642. UISettings::values.fullscreen = ui.action_Fullscreen->isChecked();
  643. UISettings::values.display_titlebar = ui.action_Display_Dock_Widget_Headers->isChecked();
  644. UISettings::values.show_filter_bar = ui.action_Show_Filter_Bar->isChecked();
  645. UISettings::values.show_status_bar = ui.action_Show_Status_Bar->isChecked();
  646. UISettings::values.first_start = false;
  647. game_list->SaveInterfaceLayout();
  648. SaveHotkeys();
  649. // Shutdown session if the emu thread is active...
  650. if (emu_thread != nullptr)
  651. ShutdownGame();
  652. render_window->close();
  653. QWidget::closeEvent(event);
  654. }
  655. static bool IsSingleFileDropEvent(QDropEvent* event) {
  656. const QMimeData* mimeData = event->mimeData();
  657. return mimeData->hasUrls() && mimeData->urls().length() == 1;
  658. }
  659. void GMainWindow::dropEvent(QDropEvent* event) {
  660. if (IsSingleFileDropEvent(event) && ConfirmChangeGame()) {
  661. const QMimeData* mimeData = event->mimeData();
  662. QString filename = mimeData->urls().at(0).toLocalFile();
  663. BootGame(filename);
  664. }
  665. }
  666. void GMainWindow::dragEnterEvent(QDragEnterEvent* event) {
  667. if (IsSingleFileDropEvent(event)) {
  668. event->acceptProposedAction();
  669. }
  670. }
  671. void GMainWindow::dragMoveEvent(QDragMoveEvent* event) {
  672. event->acceptProposedAction();
  673. }
  674. bool GMainWindow::ConfirmChangeGame() {
  675. if (emu_thread == nullptr)
  676. return true;
  677. auto answer = QMessageBox::question(
  678. this, tr("yuzu"),
  679. tr("Are you sure you want to stop the emulation? Any unsaved progress will be lost."),
  680. QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
  681. return answer != QMessageBox::No;
  682. }
  683. void GMainWindow::filterBarSetChecked(bool state) {
  684. ui.action_Show_Filter_Bar->setChecked(state);
  685. emit(OnToggleFilterBar());
  686. }
  687. #ifdef main
  688. #undef main
  689. #endif
  690. int main(int argc, char* argv[]) {
  691. Log::Filter log_filter(Log::Level::Info);
  692. Log::SetFilter(&log_filter);
  693. MicroProfileOnThreadCreate("Frontend");
  694. SCOPE_EXIT({ MicroProfileShutdown(); });
  695. // Init settings params
  696. QCoreApplication::setOrganizationName("yuzu team");
  697. QCoreApplication::setApplicationName("yuzu");
  698. QApplication::setAttribute(Qt::AA_X11InitThreads);
  699. QApplication app(argc, argv);
  700. // Qt changes the locale and causes issues in float conversion using std::to_string() when
  701. // generating shaders
  702. setlocale(LC_ALL, "C");
  703. GMainWindow main_window;
  704. // After settings have been loaded by GMainWindow, apply the filter
  705. log_filter.ParseFilterString(Settings::values.log_filter);
  706. main_window.show();
  707. return app.exec();
  708. }