main.cpp 32 KB

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