main.cpp 28 KB

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