main.cpp 24 KB

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