main.cpp 30 KB

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