main.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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 "citra_qt/bootmanager.h"
  15. #include "citra_qt/config.h"
  16. #include "citra_qt/configure_dialog.h"
  17. #include "citra_qt/debugger/callstack.h"
  18. #include "citra_qt/debugger/disassembler.h"
  19. #include "citra_qt/debugger/graphics/graphics.h"
  20. #include "citra_qt/debugger/graphics/graphics_breakpoints.h"
  21. #include "citra_qt/debugger/graphics/graphics_cmdlists.h"
  22. #include "citra_qt/debugger/graphics/graphics_surface.h"
  23. #include "citra_qt/debugger/graphics/graphics_tracing.h"
  24. #include "citra_qt/debugger/graphics/graphics_vertex_shader.h"
  25. #include "citra_qt/debugger/profiler.h"
  26. #include "citra_qt/debugger/ramview.h"
  27. #include "citra_qt/debugger/registers.h"
  28. #include "citra_qt/debugger/wait_tree.h"
  29. #include "citra_qt/game_list.h"
  30. #include "citra_qt/hotkeys.h"
  31. #include "citra_qt/main.h"
  32. #include "citra_qt/ui_settings.h"
  33. #include "common/logging/backend.h"
  34. #include "common/logging/filter.h"
  35. #include "common/logging/log.h"
  36. #include "common/logging/text_formatter.h"
  37. #include "common/microprofile.h"
  38. #include "common/platform.h"
  39. #include "common/scm_rev.h"
  40. #include "common/scope_exit.h"
  41. #include "common/string_util.h"
  42. #include "core/arm/disassembler/load_symbol_map.h"
  43. #include "core/core.h"
  44. #include "core/file_sys/archive_source_sd_savedata.h"
  45. #include "core/gdbstub/gdbstub.h"
  46. #include "core/loader/loader.h"
  47. #include "core/settings.h"
  48. #include "qhexedit.h"
  49. #include "video_core/video_core.h"
  50. #ifdef QT_STATICPLUGIN
  51. Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin);
  52. #endif
  53. GMainWindow::GMainWindow() : config(new Config()), emu_thread(nullptr) {
  54. Pica::g_debug_context = Pica::DebugContext::Construct();
  55. ui.setupUi(this);
  56. statusBar()->hide();
  57. InitializeWidgets();
  58. InitializeDebugMenuActions();
  59. InitializeRecentFileMenuActions();
  60. InitializeHotkeys();
  61. SetDefaultUIGeometry();
  62. RestoreUIState();
  63. ConnectWidgetEvents();
  64. setWindowTitle(QString("Citra | %1-%2").arg(Common::g_scm_branch, Common::g_scm_desc));
  65. show();
  66. game_list->PopulateAsync(UISettings::values.gamedir, UISettings::values.gamedir_deepscan);
  67. QStringList args = QApplication::arguments();
  68. if (args.length() >= 2) {
  69. BootGame(args[1].toStdString());
  70. }
  71. }
  72. GMainWindow::~GMainWindow() {
  73. // will get automatically deleted otherwise
  74. if (render_window->parent() == nullptr)
  75. delete render_window;
  76. Pica::g_debug_context.reset();
  77. }
  78. void GMainWindow::InitializeWidgets() {
  79. render_window = new GRenderWindow(this, emu_thread.get());
  80. render_window->hide();
  81. game_list = new GameList();
  82. ui.horizontalLayout->addWidget(game_list);
  83. profilerWidget = new ProfilerWidget(this);
  84. addDockWidget(Qt::BottomDockWidgetArea, profilerWidget);
  85. profilerWidget->hide();
  86. #if MICROPROFILE_ENABLED
  87. microProfileDialog = new MicroProfileDialog(this);
  88. microProfileDialog->hide();
  89. #endif
  90. disasmWidget = new DisassemblerWidget(this, emu_thread.get());
  91. addDockWidget(Qt::BottomDockWidgetArea, disasmWidget);
  92. disasmWidget->hide();
  93. registersWidget = new RegistersWidget(this);
  94. addDockWidget(Qt::RightDockWidgetArea, registersWidget);
  95. registersWidget->hide();
  96. callstackWidget = new CallstackWidget(this);
  97. addDockWidget(Qt::RightDockWidgetArea, callstackWidget);
  98. callstackWidget->hide();
  99. graphicsWidget = new GPUCommandStreamWidget(this);
  100. addDockWidget(Qt::RightDockWidgetArea, graphicsWidget);
  101. graphicsWidget->hide();
  102. graphicsCommandsWidget = new GPUCommandListWidget(this);
  103. addDockWidget(Qt::RightDockWidgetArea, graphicsCommandsWidget);
  104. graphicsCommandsWidget->hide();
  105. graphicsBreakpointsWidget = new GraphicsBreakPointsWidget(Pica::g_debug_context, this);
  106. addDockWidget(Qt::RightDockWidgetArea, graphicsBreakpointsWidget);
  107. graphicsBreakpointsWidget->hide();
  108. graphicsVertexShaderWidget = new GraphicsVertexShaderWidget(Pica::g_debug_context, this);
  109. addDockWidget(Qt::RightDockWidgetArea, graphicsVertexShaderWidget);
  110. graphicsVertexShaderWidget->hide();
  111. graphicsTracingWidget = new GraphicsTracingWidget(Pica::g_debug_context, this);
  112. addDockWidget(Qt::RightDockWidgetArea, graphicsTracingWidget);
  113. graphicsTracingWidget->hide();
  114. waitTreeWidget = new WaitTreeWidget(this);
  115. addDockWidget(Qt::LeftDockWidgetArea, waitTreeWidget);
  116. waitTreeWidget->hide();
  117. }
  118. void GMainWindow::InitializeDebugMenuActions() {
  119. auto graphicsSurfaceViewerAction = new QAction(tr("Create Pica Surface Viewer"), this);
  120. connect(graphicsSurfaceViewerAction, SIGNAL(triggered()), this,
  121. SLOT(OnCreateGraphicsSurfaceViewer()));
  122. QMenu* debug_menu = ui.menu_View->addMenu(tr("Debugging"));
  123. debug_menu->addAction(graphicsSurfaceViewerAction);
  124. debug_menu->addSeparator();
  125. debug_menu->addAction(profilerWidget->toggleViewAction());
  126. #if MICROPROFILE_ENABLED
  127. debug_menu->addAction(microProfileDialog->toggleViewAction());
  128. #endif
  129. debug_menu->addAction(disasmWidget->toggleViewAction());
  130. debug_menu->addAction(registersWidget->toggleViewAction());
  131. debug_menu->addAction(callstackWidget->toggleViewAction());
  132. debug_menu->addAction(graphicsWidget->toggleViewAction());
  133. debug_menu->addAction(graphicsCommandsWidget->toggleViewAction());
  134. debug_menu->addAction(graphicsBreakpointsWidget->toggleViewAction());
  135. debug_menu->addAction(graphicsVertexShaderWidget->toggleViewAction());
  136. debug_menu->addAction(graphicsTracingWidget->toggleViewAction());
  137. debug_menu->addAction(waitTreeWidget->toggleViewAction());
  138. }
  139. void GMainWindow::InitializeRecentFileMenuActions() {
  140. for (int i = 0; i < max_recent_files_item; ++i) {
  141. actions_recent_files[i] = new QAction(this);
  142. actions_recent_files[i]->setVisible(false);
  143. connect(actions_recent_files[i], SIGNAL(triggered()), this, SLOT(OnMenuRecentFile()));
  144. ui.menu_recent_files->addAction(actions_recent_files[i]);
  145. }
  146. UpdateRecentFiles();
  147. }
  148. void GMainWindow::InitializeHotkeys() {
  149. RegisterHotkey("Main Window", "Load File", QKeySequence::Open);
  150. RegisterHotkey("Main Window", "Swap Screens", QKeySequence::NextChild);
  151. RegisterHotkey("Main Window", "Start Emulation");
  152. LoadHotkeys();
  153. connect(GetHotkey("Main Window", "Load File", this), SIGNAL(activated()), this,
  154. SLOT(OnMenuLoadFile()));
  155. connect(GetHotkey("Main Window", "Start Emulation", this), SIGNAL(activated()), this,
  156. SLOT(OnStartGame()));
  157. connect(GetHotkey("Main Window", "Swap Screens", render_window), SIGNAL(activated()), this,
  158. SLOT(OnSwapScreens()));
  159. }
  160. void GMainWindow::SetDefaultUIGeometry() {
  161. // geometry: 55% of the window contents are in the upper screen half, 45% in the lower half
  162. const QRect screenRect = QApplication::desktop()->screenGeometry(this);
  163. const int w = screenRect.width() * 2 / 3;
  164. const int h = screenRect.height() / 2;
  165. const int x = (screenRect.x() + screenRect.width()) / 2 - w / 2;
  166. const int y = (screenRect.y() + screenRect.height()) / 2 - h * 55 / 100;
  167. setGeometry(x, y, w, h);
  168. }
  169. void GMainWindow::RestoreUIState() {
  170. restoreGeometry(UISettings::values.geometry);
  171. restoreState(UISettings::values.state);
  172. render_window->restoreGeometry(UISettings::values.renderwindow_geometry);
  173. #if MICROPROFILE_ENABLED
  174. microProfileDialog->restoreGeometry(UISettings::values.microprofile_geometry);
  175. microProfileDialog->setVisible(UISettings::values.microprofile_visible);
  176. #endif
  177. game_list->LoadInterfaceLayout();
  178. ui.action_Single_Window_Mode->setChecked(UISettings::values.single_window_mode);
  179. ToggleWindowMode();
  180. ui.actionDisplay_widget_title_bars->setChecked(UISettings::values.display_titlebar);
  181. OnDisplayTitleBars(ui.actionDisplay_widget_title_bars->isChecked());
  182. }
  183. void GMainWindow::ConnectWidgetEvents() {
  184. connect(game_list, SIGNAL(GameChosen(QString)), this, SLOT(OnGameListLoadFile(QString)),
  185. Qt::DirectConnection);
  186. connect(game_list, SIGNAL(OpenSaveFolderRequested(u64)), this,
  187. SLOT(OnGameListOpenSaveFolder(u64)), Qt::DirectConnection);
  188. connect(ui.action_Configure, SIGNAL(triggered()), this, SLOT(OnConfigure()));
  189. connect(ui.action_Load_File, SIGNAL(triggered()), this, SLOT(OnMenuLoadFile()),
  190. Qt::DirectConnection);
  191. connect(ui.action_Load_Symbol_Map, SIGNAL(triggered()), this, SLOT(OnMenuLoadSymbolMap()));
  192. connect(ui.action_Select_Game_List_Root, SIGNAL(triggered()), this,
  193. SLOT(OnMenuSelectGameListRoot()));
  194. connect(ui.action_Start, SIGNAL(triggered()), this, SLOT(OnStartGame()));
  195. connect(ui.action_Pause, SIGNAL(triggered()), this, SLOT(OnPauseGame()));
  196. connect(ui.action_Stop, SIGNAL(triggered()), this, SLOT(OnStopGame()));
  197. connect(ui.action_Single_Window_Mode, SIGNAL(triggered(bool)), this, SLOT(ToggleWindowMode()));
  198. connect(this, SIGNAL(EmulationStarting(EmuThread*)), disasmWidget,
  199. SLOT(OnEmulationStarting(EmuThread*)));
  200. connect(this, SIGNAL(EmulationStopping()), disasmWidget, SLOT(OnEmulationStopping()));
  201. connect(this, SIGNAL(EmulationStarting(EmuThread*)), registersWidget,
  202. SLOT(OnEmulationStarting(EmuThread*)));
  203. connect(this, SIGNAL(EmulationStopping()), registersWidget, SLOT(OnEmulationStopping()));
  204. connect(this, SIGNAL(EmulationStarting(EmuThread*)), render_window,
  205. SLOT(OnEmulationStarting(EmuThread*)));
  206. connect(this, SIGNAL(EmulationStopping()), render_window, SLOT(OnEmulationStopping()));
  207. connect(this, SIGNAL(EmulationStarting(EmuThread*)), graphicsTracingWidget,
  208. SLOT(OnEmulationStarting(EmuThread*)));
  209. connect(this, SIGNAL(EmulationStopping()), graphicsTracingWidget, SLOT(OnEmulationStopping()));
  210. connect(this, SIGNAL(EmulationStarting(EmuThread*)), waitTreeWidget,
  211. SLOT(OnEmulationStarting(EmuThread*)));
  212. connect(this, SIGNAL(EmulationStopping()), waitTreeWidget, SLOT(OnEmulationStopping()));
  213. }
  214. void GMainWindow::OnDisplayTitleBars(bool show) {
  215. QList<QDockWidget*> widgets = findChildren<QDockWidget*>();
  216. if (show) {
  217. for (QDockWidget* widget : widgets) {
  218. QWidget* old = widget->titleBarWidget();
  219. widget->setTitleBarWidget(nullptr);
  220. if (old != nullptr)
  221. delete old;
  222. }
  223. } else {
  224. for (QDockWidget* widget : widgets) {
  225. QWidget* old = widget->titleBarWidget();
  226. widget->setTitleBarWidget(new QWidget());
  227. if (old != nullptr)
  228. delete old;
  229. }
  230. }
  231. }
  232. bool GMainWindow::LoadROM(const std::string& filename) {
  233. // Shutdown previous session if the emu thread is still active...
  234. if (emu_thread != nullptr)
  235. ShutdownGame();
  236. render_window->InitRenderTarget();
  237. render_window->MakeCurrent();
  238. if (!gladLoadGL()) {
  239. QMessageBox::critical(this, tr("Error while starting Citra!"),
  240. tr("Failed to initialize the video core!\n\n"
  241. "Please ensure that your GPU supports OpenGL 3.3 and that you "
  242. "have the latest graphics driver."));
  243. return false;
  244. }
  245. Core::System& system{Core::System::GetInstance()};
  246. const Core::System::ResultStatus result{system.Load(render_window, filename)};
  247. if (result != Core::System::ResultStatus::Success) {
  248. switch (result) {
  249. case Core::System::ResultStatus::ErrorGetLoader:
  250. LOG_CRITICAL(Frontend, "Failed to obtain loader for %s!", filename.c_str());
  251. QMessageBox::critical(this, tr("Error while loading ROM!"),
  252. tr("The ROM format is not supported."));
  253. break;
  254. case Core::System::ResultStatus::ErrorSystemMode:
  255. LOG_CRITICAL(Frontend, "Failed to load ROM!");
  256. QMessageBox::critical(this, tr("Error while loading ROM!"),
  257. tr("Could not determine the system mode."));
  258. break;
  259. case Core::System::ResultStatus::ErrorLoader_ErrorEncrypted: {
  260. // Build the MessageBox ourselves to have clickable link
  261. QMessageBox popup_error;
  262. popup_error.setTextFormat(Qt::RichText);
  263. popup_error.setWindowTitle(tr("Error while loading ROM!"));
  264. popup_error.setText(
  265. tr("The game that you are trying to load must be decrypted before being used with "
  266. "Citra.<br/><br/>"
  267. "For more information on dumping and decrypting games, please see: <a "
  268. "href='https://citra-emu.org/wiki/Dumping-Game-Cartridges'>https://"
  269. "citra-emu.org/wiki/Dumping-Game-Cartridges</a>"));
  270. popup_error.setIcon(QMessageBox::Critical);
  271. popup_error.exec();
  272. break;
  273. }
  274. case Core::System::ResultStatus::ErrorLoader_ErrorInvalidFormat:
  275. QMessageBox::critical(this, tr("Error while loading ROM!"),
  276. tr("The ROM format is not supported."));
  277. break;
  278. default:
  279. QMessageBox::critical(this, tr("Error while loading ROM!"), tr("Unknown error!"));
  280. break;
  281. }
  282. return false;
  283. }
  284. return true;
  285. }
  286. void GMainWindow::BootGame(const std::string& filename) {
  287. LOG_INFO(Frontend, "Citra starting...");
  288. StoreRecentFile(filename); // Put the filename on top of the list
  289. if (!LoadROM(filename))
  290. return;
  291. // Create and start the emulation thread
  292. emu_thread = std::make_unique<EmuThread>(render_window);
  293. emit EmulationStarting(emu_thread.get());
  294. render_window->moveContext();
  295. emu_thread->start();
  296. connect(render_window, SIGNAL(Closed()), this, SLOT(OnStopGame()));
  297. // BlockingQueuedConnection is important here, it makes sure we've finished refreshing our views
  298. // before the CPU continues
  299. connect(emu_thread.get(), SIGNAL(DebugModeEntered()), disasmWidget, SLOT(OnDebugModeEntered()),
  300. Qt::BlockingQueuedConnection);
  301. connect(emu_thread.get(), SIGNAL(DebugModeEntered()), registersWidget,
  302. SLOT(OnDebugModeEntered()), Qt::BlockingQueuedConnection);
  303. connect(emu_thread.get(), SIGNAL(DebugModeEntered()), callstackWidget,
  304. SLOT(OnDebugModeEntered()), Qt::BlockingQueuedConnection);
  305. connect(emu_thread.get(), SIGNAL(DebugModeEntered()), waitTreeWidget,
  306. SLOT(OnDebugModeEntered()), Qt::BlockingQueuedConnection);
  307. connect(emu_thread.get(), SIGNAL(DebugModeLeft()), disasmWidget, SLOT(OnDebugModeLeft()),
  308. Qt::BlockingQueuedConnection);
  309. connect(emu_thread.get(), SIGNAL(DebugModeLeft()), registersWidget, SLOT(OnDebugModeLeft()),
  310. Qt::BlockingQueuedConnection);
  311. connect(emu_thread.get(), SIGNAL(DebugModeLeft()), callstackWidget, SLOT(OnDebugModeLeft()),
  312. Qt::BlockingQueuedConnection);
  313. connect(emu_thread.get(), SIGNAL(DebugModeLeft()), waitTreeWidget, SLOT(OnDebugModeLeft()),
  314. Qt::BlockingQueuedConnection);
  315. // Update the GUI
  316. registersWidget->OnDebugModeEntered();
  317. callstackWidget->OnDebugModeEntered();
  318. if (ui.action_Single_Window_Mode->isChecked()) {
  319. game_list->hide();
  320. }
  321. render_window->show();
  322. render_window->setFocus();
  323. emulation_running = true;
  324. OnStartGame();
  325. }
  326. void GMainWindow::ShutdownGame() {
  327. emu_thread->RequestStop();
  328. // Release emu threads from any breakpoints
  329. // This belongs after RequestStop() and before wait() because if emulation stops on a GPU
  330. // breakpoint after (or before) RequestStop() is called, the emulation would never be able
  331. // to continue out to the main loop and terminate. Thus wait() would hang forever.
  332. // TODO(bunnei): This function is not thread safe, but it's being used as if it were
  333. Pica::g_debug_context->ClearBreakpoints();
  334. emit EmulationStopping();
  335. // Wait for emulation thread to complete and delete it
  336. emu_thread->wait();
  337. emu_thread = nullptr;
  338. // The emulation is stopped, so closing the window or not does not matter anymore
  339. disconnect(render_window, SIGNAL(Closed()), this, SLOT(OnStopGame()));
  340. // Update the GUI
  341. ui.action_Start->setEnabled(false);
  342. ui.action_Start->setText(tr("Start"));
  343. ui.action_Pause->setEnabled(false);
  344. ui.action_Stop->setEnabled(false);
  345. render_window->hide();
  346. game_list->show();
  347. emulation_running = false;
  348. }
  349. void GMainWindow::StoreRecentFile(const std::string& filename) {
  350. UISettings::values.recent_files.prepend(QString::fromStdString(filename));
  351. UISettings::values.recent_files.removeDuplicates();
  352. while (UISettings::values.recent_files.size() > max_recent_files_item) {
  353. UISettings::values.recent_files.removeLast();
  354. }
  355. UpdateRecentFiles();
  356. }
  357. void GMainWindow::UpdateRecentFiles() {
  358. unsigned int num_recent_files =
  359. std::min(UISettings::values.recent_files.size(), static_cast<int>(max_recent_files_item));
  360. for (unsigned int i = 0; i < num_recent_files; i++) {
  361. QString text = QString("&%1. %2").arg(i + 1).arg(
  362. QFileInfo(UISettings::values.recent_files[i]).fileName());
  363. actions_recent_files[i]->setText(text);
  364. actions_recent_files[i]->setData(UISettings::values.recent_files[i]);
  365. actions_recent_files[i]->setToolTip(UISettings::values.recent_files[i]);
  366. actions_recent_files[i]->setVisible(true);
  367. }
  368. for (int j = num_recent_files; j < max_recent_files_item; ++j) {
  369. actions_recent_files[j]->setVisible(false);
  370. }
  371. // Grey out the recent files menu if the list is empty
  372. if (num_recent_files == 0) {
  373. ui.menu_recent_files->setEnabled(false);
  374. } else {
  375. ui.menu_recent_files->setEnabled(true);
  376. }
  377. }
  378. void GMainWindow::OnGameListLoadFile(QString game_path) {
  379. BootGame(game_path.toStdString());
  380. }
  381. void GMainWindow::OnGameListOpenSaveFolder(u64 program_id) {
  382. std::string sdmc_dir = FileUtil::GetUserPath(D_SDMC_IDX);
  383. std::string path = FileSys::ArchiveSource_SDSaveData::GetSaveDataPathFor(sdmc_dir, program_id);
  384. QString qpath = QString::fromStdString(path);
  385. QDir dir(qpath);
  386. if (!dir.exists()) {
  387. QMessageBox::critical(this, tr("Error Opening Save Folder"), tr("Folder does not exist!"));
  388. return;
  389. }
  390. LOG_INFO(Frontend, "Opening save data path for program_id=%" PRIu64, program_id);
  391. QDesktopServices::openUrl(QUrl::fromLocalFile(qpath));
  392. }
  393. void GMainWindow::OnMenuLoadFile() {
  394. QString filename =
  395. QFileDialog::getOpenFileName(this, tr("Load File"), UISettings::values.roms_path,
  396. tr("3DS executable (*.3ds *.3dsx *.elf *.axf *.cci *.cxi)"));
  397. if (!filename.isEmpty()) {
  398. UISettings::values.roms_path = QFileInfo(filename).path();
  399. BootGame(filename.toStdString());
  400. }
  401. }
  402. void GMainWindow::OnMenuLoadSymbolMap() {
  403. QString filename = QFileDialog::getOpenFileName(
  404. this, tr("Load Symbol Map"), UISettings::values.symbols_path, tr("Symbol map (*)"));
  405. if (!filename.isEmpty()) {
  406. UISettings::values.symbols_path = QFileInfo(filename).path();
  407. LoadSymbolMap(filename.toStdString());
  408. }
  409. }
  410. void GMainWindow::OnMenuSelectGameListRoot() {
  411. QString dir_path = QFileDialog::getExistingDirectory(this, tr("Select Directory"));
  412. if (!dir_path.isEmpty()) {
  413. UISettings::values.gamedir = dir_path;
  414. game_list->PopulateAsync(dir_path, UISettings::values.gamedir_deepscan);
  415. }
  416. }
  417. void GMainWindow::OnMenuRecentFile() {
  418. QAction* action = qobject_cast<QAction*>(sender());
  419. assert(action);
  420. QString filename = action->data().toString();
  421. QFileInfo file_info(filename);
  422. if (file_info.exists()) {
  423. BootGame(filename.toStdString());
  424. } else {
  425. // Display an error message and remove the file from the list.
  426. QMessageBox::information(this, tr("File not found"),
  427. tr("File \"%1\" not found").arg(filename));
  428. UISettings::values.recent_files.removeOne(filename);
  429. UpdateRecentFiles();
  430. }
  431. }
  432. void GMainWindow::OnStartGame() {
  433. emu_thread->SetRunning(true);
  434. ui.action_Start->setEnabled(false);
  435. ui.action_Start->setText(tr("Continue"));
  436. ui.action_Pause->setEnabled(true);
  437. ui.action_Stop->setEnabled(true);
  438. }
  439. void GMainWindow::OnPauseGame() {
  440. emu_thread->SetRunning(false);
  441. ui.action_Start->setEnabled(true);
  442. ui.action_Pause->setEnabled(false);
  443. ui.action_Stop->setEnabled(true);
  444. }
  445. void GMainWindow::OnStopGame() {
  446. ShutdownGame();
  447. }
  448. void GMainWindow::ToggleWindowMode() {
  449. if (ui.action_Single_Window_Mode->isChecked()) {
  450. // Render in the main window...
  451. render_window->BackupGeometry();
  452. ui.horizontalLayout->addWidget(render_window);
  453. render_window->setFocusPolicy(Qt::ClickFocus);
  454. if (emulation_running) {
  455. render_window->setVisible(true);
  456. render_window->setFocus();
  457. game_list->hide();
  458. }
  459. } else {
  460. // Render in a separate window...
  461. ui.horizontalLayout->removeWidget(render_window);
  462. render_window->setParent(nullptr);
  463. render_window->setFocusPolicy(Qt::NoFocus);
  464. if (emulation_running) {
  465. render_window->setVisible(true);
  466. render_window->RestoreGeometry();
  467. game_list->show();
  468. }
  469. }
  470. }
  471. void GMainWindow::OnConfigure() {
  472. ConfigureDialog configureDialog(this);
  473. auto result = configureDialog.exec();
  474. if (result == QDialog::Accepted) {
  475. configureDialog.applyConfiguration();
  476. render_window->ReloadSetKeymaps();
  477. config->Save();
  478. }
  479. }
  480. void GMainWindow::OnSwapScreens() {
  481. Settings::values.swap_screen = !Settings::values.swap_screen;
  482. Settings::Apply();
  483. }
  484. void GMainWindow::OnCreateGraphicsSurfaceViewer() {
  485. auto graphicsSurfaceViewerWidget = new GraphicsSurfaceWidget(Pica::g_debug_context, this);
  486. addDockWidget(Qt::RightDockWidgetArea, graphicsSurfaceViewerWidget);
  487. // TODO: Maybe graphicsSurfaceViewerWidget->setFloating(true);
  488. graphicsSurfaceViewerWidget->show();
  489. }
  490. bool GMainWindow::ConfirmClose() {
  491. if (emu_thread == nullptr || !UISettings::values.confirm_before_closing)
  492. return true;
  493. auto answer =
  494. QMessageBox::question(this, tr("Citra"), tr("Are you sure you want to close Citra?"),
  495. QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
  496. return answer != QMessageBox::No;
  497. }
  498. void GMainWindow::closeEvent(QCloseEvent* event) {
  499. if (!ConfirmClose()) {
  500. event->ignore();
  501. return;
  502. }
  503. UISettings::values.geometry = saveGeometry();
  504. UISettings::values.state = saveState();
  505. UISettings::values.renderwindow_geometry = render_window->saveGeometry();
  506. #if MICROPROFILE_ENABLED
  507. UISettings::values.microprofile_geometry = microProfileDialog->saveGeometry();
  508. UISettings::values.microprofile_visible = microProfileDialog->isVisible();
  509. #endif
  510. UISettings::values.single_window_mode = ui.action_Single_Window_Mode->isChecked();
  511. UISettings::values.display_titlebar = ui.actionDisplay_widget_title_bars->isChecked();
  512. UISettings::values.first_start = false;
  513. game_list->SaveInterfaceLayout();
  514. SaveHotkeys();
  515. // Shutdown session if the emu thread is active...
  516. if (emu_thread != nullptr)
  517. ShutdownGame();
  518. render_window->close();
  519. QWidget::closeEvent(event);
  520. }
  521. #ifdef main
  522. #undef main
  523. #endif
  524. int main(int argc, char* argv[]) {
  525. Log::Filter log_filter(Log::Level::Info);
  526. Log::SetFilter(&log_filter);
  527. MicroProfileOnThreadCreate("Frontend");
  528. SCOPE_EXIT({ MicroProfileShutdown(); });
  529. // Init settings params
  530. QCoreApplication::setOrganizationName("Citra team");
  531. QCoreApplication::setApplicationName("Citra");
  532. QApplication::setAttribute(Qt::AA_X11InitThreads);
  533. QApplication app(argc, argv);
  534. // Qt changes the locale and causes issues in float conversion using std::to_string() when
  535. // generating shaders
  536. setlocale(LC_ALL, "C");
  537. GMainWindow main_window;
  538. // After settings have been loaded by GMainWindow, apply the filter
  539. log_filter.ParseFilterString(Settings::values.log_filter);
  540. main_window.show();
  541. return app.exec();
  542. }