main.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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. setAcceptDrops(true);
  55. ui.setupUi(this);
  56. statusBar()->hide();
  57. InitializeWidgets();
  58. InitializeDebugWidgets();
  59. InitializeRecentFileMenuActions();
  60. InitializeHotkeys();
  61. SetDefaultUIGeometry();
  62. RestoreUIState();
  63. ConnectMenuEvents();
  64. ConnectWidgetEvents();
  65. setWindowTitle(QString("Citra | %1-%2").arg(Common::g_scm_branch, Common::g_scm_desc));
  66. show();
  67. game_list->PopulateAsync(UISettings::values.gamedir, UISettings::values.gamedir_deepscan);
  68. QStringList args = QApplication::arguments();
  69. if (args.length() >= 2) {
  70. BootGame(args[1]);
  71. }
  72. }
  73. GMainWindow::~GMainWindow() {
  74. // will get automatically deleted otherwise
  75. if (render_window->parent() == nullptr)
  76. delete render_window;
  77. Pica::g_debug_context.reset();
  78. }
  79. void GMainWindow::InitializeWidgets() {
  80. render_window = new GRenderWindow(this, emu_thread.get());
  81. render_window->hide();
  82. game_list = new GameList();
  83. ui.horizontalLayout->addWidget(game_list);
  84. // Create status bar
  85. emu_speed_label = new QLabel();
  86. game_fps_label = new QLabel();
  87. emu_frametime_label = new QLabel();
  88. for (auto& label : {emu_speed_label, game_fps_label, emu_frametime_label}) {
  89. label->setVisible(false);
  90. statusBar()->addPermanentWidget(label);
  91. }
  92. statusBar()->setVisible(true);
  93. }
  94. void GMainWindow::InitializeDebugWidgets() {
  95. connect(ui.action_Create_Pica_Surface_Viewer, &QAction::triggered, this,
  96. &GMainWindow::OnCreateGraphicsSurfaceViewer);
  97. QMenu* debug_menu = ui.menu_View_Debugging;
  98. profilerWidget = new ProfilerWidget(this);
  99. addDockWidget(Qt::BottomDockWidgetArea, profilerWidget);
  100. profilerWidget->hide();
  101. debug_menu->addAction(profilerWidget->toggleViewAction());
  102. #if MICROPROFILE_ENABLED
  103. microProfileDialog = new MicroProfileDialog(this);
  104. microProfileDialog->hide();
  105. debug_menu->addAction(microProfileDialog->toggleViewAction());
  106. #endif
  107. disasmWidget = new DisassemblerWidget(this, emu_thread.get());
  108. addDockWidget(Qt::BottomDockWidgetArea, disasmWidget);
  109. disasmWidget->hide();
  110. debug_menu->addAction(disasmWidget->toggleViewAction());
  111. connect(this, &GMainWindow::EmulationStarting, disasmWidget,
  112. &DisassemblerWidget::OnEmulationStarting);
  113. connect(this, &GMainWindow::EmulationStopping, disasmWidget,
  114. &DisassemblerWidget::OnEmulationStopping);
  115. registersWidget = new RegistersWidget(this);
  116. addDockWidget(Qt::RightDockWidgetArea, registersWidget);
  117. registersWidget->hide();
  118. debug_menu->addAction(registersWidget->toggleViewAction());
  119. connect(this, &GMainWindow::EmulationStarting, registersWidget,
  120. &RegistersWidget::OnEmulationStarting);
  121. connect(this, &GMainWindow::EmulationStopping, registersWidget,
  122. &RegistersWidget::OnEmulationStopping);
  123. callstackWidget = new CallstackWidget(this);
  124. addDockWidget(Qt::RightDockWidgetArea, callstackWidget);
  125. callstackWidget->hide();
  126. debug_menu->addAction(callstackWidget->toggleViewAction());
  127. graphicsWidget = new GPUCommandStreamWidget(this);
  128. addDockWidget(Qt::RightDockWidgetArea, graphicsWidget);
  129. graphicsWidget->hide();
  130. debug_menu->addAction(graphicsWidget->toggleViewAction());
  131. graphicsCommandsWidget = new GPUCommandListWidget(this);
  132. addDockWidget(Qt::RightDockWidgetArea, graphicsCommandsWidget);
  133. graphicsCommandsWidget->hide();
  134. debug_menu->addAction(graphicsCommandsWidget->toggleViewAction());
  135. graphicsBreakpointsWidget = new GraphicsBreakPointsWidget(Pica::g_debug_context, this);
  136. addDockWidget(Qt::RightDockWidgetArea, graphicsBreakpointsWidget);
  137. graphicsBreakpointsWidget->hide();
  138. debug_menu->addAction(graphicsBreakpointsWidget->toggleViewAction());
  139. graphicsVertexShaderWidget = new GraphicsVertexShaderWidget(Pica::g_debug_context, this);
  140. addDockWidget(Qt::RightDockWidgetArea, graphicsVertexShaderWidget);
  141. graphicsVertexShaderWidget->hide();
  142. debug_menu->addAction(graphicsVertexShaderWidget->toggleViewAction());
  143. graphicsTracingWidget = new GraphicsTracingWidget(Pica::g_debug_context, this);
  144. addDockWidget(Qt::RightDockWidgetArea, graphicsTracingWidget);
  145. graphicsTracingWidget->hide();
  146. debug_menu->addAction(graphicsTracingWidget->toggleViewAction());
  147. connect(this, &GMainWindow::EmulationStarting, graphicsTracingWidget,
  148. &GraphicsTracingWidget::OnEmulationStarting);
  149. connect(this, &GMainWindow::EmulationStopping, graphicsTracingWidget,
  150. &GraphicsTracingWidget::OnEmulationStopping);
  151. waitTreeWidget = new WaitTreeWidget(this);
  152. addDockWidget(Qt::LeftDockWidgetArea, waitTreeWidget);
  153. waitTreeWidget->hide();
  154. debug_menu->addAction(waitTreeWidget->toggleViewAction());
  155. connect(this, &GMainWindow::EmulationStarting, waitTreeWidget,
  156. &WaitTreeWidget::OnEmulationStarting);
  157. connect(this, &GMainWindow::EmulationStopping, waitTreeWidget,
  158. &WaitTreeWidget::OnEmulationStopping);
  159. }
  160. void GMainWindow::InitializeRecentFileMenuActions() {
  161. for (int i = 0; i < max_recent_files_item; ++i) {
  162. actions_recent_files[i] = new QAction(this);
  163. actions_recent_files[i]->setVisible(false);
  164. connect(actions_recent_files[i], SIGNAL(triggered()), this, SLOT(OnMenuRecentFile()));
  165. ui.menu_recent_files->addAction(actions_recent_files[i]);
  166. }
  167. UpdateRecentFiles();
  168. }
  169. void GMainWindow::InitializeHotkeys() {
  170. RegisterHotkey("Main Window", "Load File", QKeySequence::Open);
  171. RegisterHotkey("Main Window", "Swap Screens", QKeySequence::NextChild);
  172. RegisterHotkey("Main Window", "Start Emulation");
  173. LoadHotkeys();
  174. connect(GetHotkey("Main Window", "Load File", this), SIGNAL(activated()), this,
  175. SLOT(OnMenuLoadFile()));
  176. connect(GetHotkey("Main Window", "Start Emulation", this), SIGNAL(activated()), this,
  177. SLOT(OnStartGame()));
  178. connect(GetHotkey("Main Window", "Swap Screens", render_window), SIGNAL(activated()), this,
  179. SLOT(OnSwapScreens()));
  180. }
  181. void GMainWindow::SetDefaultUIGeometry() {
  182. // geometry: 55% of the window contents are in the upper screen half, 45% in the lower half
  183. const QRect screenRect = QApplication::desktop()->screenGeometry(this);
  184. const int w = screenRect.width() * 2 / 3;
  185. const int h = screenRect.height() / 2;
  186. const int x = (screenRect.x() + screenRect.width()) / 2 - w / 2;
  187. const int y = (screenRect.y() + screenRect.height()) / 2 - h * 55 / 100;
  188. setGeometry(x, y, w, h);
  189. }
  190. void GMainWindow::RestoreUIState() {
  191. restoreGeometry(UISettings::values.geometry);
  192. restoreState(UISettings::values.state);
  193. render_window->restoreGeometry(UISettings::values.renderwindow_geometry);
  194. #if MICROPROFILE_ENABLED
  195. microProfileDialog->restoreGeometry(UISettings::values.microprofile_geometry);
  196. microProfileDialog->setVisible(UISettings::values.microprofile_visible);
  197. #endif
  198. game_list->LoadInterfaceLayout();
  199. ui.action_Single_Window_Mode->setChecked(UISettings::values.single_window_mode);
  200. ToggleWindowMode();
  201. ui.action_Display_Dock_Widget_Headers->setChecked(UISettings::values.display_titlebar);
  202. OnDisplayTitleBars(ui.action_Display_Dock_Widget_Headers->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_Load_Symbol_Map, &QAction::triggered, this,
  219. &GMainWindow::OnMenuLoadSymbolMap);
  220. connect(ui.action_Select_Game_List_Root, &QAction::triggered, this,
  221. &GMainWindow::OnMenuSelectGameListRoot);
  222. connect(ui.action_Exit, &QAction::triggered, this, &QMainWindow::close);
  223. // Emulation
  224. connect(ui.action_Start, &QAction::triggered, this, &GMainWindow::OnStartGame);
  225. connect(ui.action_Pause, &QAction::triggered, this, &GMainWindow::OnPauseGame);
  226. connect(ui.action_Stop, &QAction::triggered, this, &GMainWindow::OnStopGame);
  227. connect(ui.action_Configure, &QAction::triggered, this, &GMainWindow::OnConfigure);
  228. // View
  229. connect(ui.action_Single_Window_Mode, &QAction::triggered, this,
  230. &GMainWindow::ToggleWindowMode);
  231. connect(ui.action_Display_Dock_Widget_Headers, &QAction::triggered, this,
  232. &GMainWindow::OnDisplayTitleBars);
  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 starting Citra!"),
  261. tr("Failed to initialize the video core!\n\n"
  262. "Please ensure that your GPU supports OpenGL 3.3 and that you "
  263. "have the latest graphics driver."));
  264. return false;
  265. }
  266. Core::System& system{Core::System::GetInstance()};
  267. const Core::System::ResultStatus result{system.Load(render_window, filename.toStdString())};
  268. if (result != Core::System::ResultStatus::Success) {
  269. switch (result) {
  270. case Core::System::ResultStatus::ErrorGetLoader:
  271. LOG_CRITICAL(Frontend, "Failed to obtain loader for %s!",
  272. filename.toStdString().c_str());
  273. QMessageBox::critical(this, tr("Error while loading ROM!"),
  274. tr("The ROM format is not supported."));
  275. break;
  276. case Core::System::ResultStatus::ErrorSystemMode:
  277. LOG_CRITICAL(Frontend, "Failed to load ROM!");
  278. QMessageBox::critical(this, tr("Error while loading ROM!"),
  279. tr("Could not determine the system mode."));
  280. break;
  281. case Core::System::ResultStatus::ErrorLoader_ErrorEncrypted: {
  282. // Build the MessageBox ourselves to have clickable link
  283. QMessageBox popup_error;
  284. popup_error.setTextFormat(Qt::RichText);
  285. popup_error.setWindowTitle(tr("Error while loading ROM!"));
  286. popup_error.setText(
  287. tr("The game that you are trying to load must be decrypted before being used with "
  288. "Citra.<br/><br/>"
  289. "For more information on dumping and decrypting games, please see: <a "
  290. "href='https://citra-emu.org/wiki/Dumping-Game-Cartridges'>https://"
  291. "citra-emu.org/wiki/Dumping-Game-Cartridges</a>"));
  292. popup_error.setIcon(QMessageBox::Critical);
  293. popup_error.exec();
  294. break;
  295. }
  296. case Core::System::ResultStatus::ErrorLoader_ErrorInvalidFormat:
  297. QMessageBox::critical(this, tr("Error while loading ROM!"),
  298. tr("The ROM format is not supported."));
  299. break;
  300. default:
  301. QMessageBox::critical(this, tr("Error while loading ROM!"), tr("Unknown error!"));
  302. break;
  303. }
  304. return false;
  305. }
  306. return true;
  307. }
  308. void GMainWindow::BootGame(const QString& filename) {
  309. LOG_INFO(Frontend, "Citra starting...");
  310. StoreRecentFile(filename); // Put the filename on top of the list
  311. if (!LoadROM(filename))
  312. return;
  313. // Create and start the emulation thread
  314. emu_thread = std::make_unique<EmuThread>(render_window);
  315. emit EmulationStarting(emu_thread.get());
  316. render_window->moveContext();
  317. emu_thread->start();
  318. connect(render_window, SIGNAL(Closed()), this, SLOT(OnStopGame()));
  319. // BlockingQueuedConnection is important here, it makes sure we've finished refreshing our views
  320. // before the CPU continues
  321. connect(emu_thread.get(), SIGNAL(DebugModeEntered()), disasmWidget, SLOT(OnDebugModeEntered()),
  322. Qt::BlockingQueuedConnection);
  323. connect(emu_thread.get(), SIGNAL(DebugModeEntered()), registersWidget,
  324. SLOT(OnDebugModeEntered()), Qt::BlockingQueuedConnection);
  325. connect(emu_thread.get(), SIGNAL(DebugModeEntered()), callstackWidget,
  326. SLOT(OnDebugModeEntered()), Qt::BlockingQueuedConnection);
  327. connect(emu_thread.get(), SIGNAL(DebugModeEntered()), waitTreeWidget,
  328. SLOT(OnDebugModeEntered()), Qt::BlockingQueuedConnection);
  329. connect(emu_thread.get(), SIGNAL(DebugModeLeft()), disasmWidget, SLOT(OnDebugModeLeft()),
  330. Qt::BlockingQueuedConnection);
  331. connect(emu_thread.get(), SIGNAL(DebugModeLeft()), registersWidget, SLOT(OnDebugModeLeft()),
  332. Qt::BlockingQueuedConnection);
  333. connect(emu_thread.get(), SIGNAL(DebugModeLeft()), callstackWidget, SLOT(OnDebugModeLeft()),
  334. Qt::BlockingQueuedConnection);
  335. connect(emu_thread.get(), SIGNAL(DebugModeLeft()), waitTreeWidget, SLOT(OnDebugModeLeft()),
  336. Qt::BlockingQueuedConnection);
  337. // Update the GUI
  338. registersWidget->OnDebugModeEntered();
  339. callstackWidget->OnDebugModeEntered();
  340. if (ui.action_Single_Window_Mode->isChecked()) {
  341. game_list->hide();
  342. }
  343. status_bar_update_timer.start(1000);
  344. render_window->show();
  345. render_window->setFocus();
  346. emulation_running = true;
  347. OnStartGame();
  348. }
  349. void GMainWindow::ShutdownGame() {
  350. emu_thread->RequestStop();
  351. // Release emu threads from any breakpoints
  352. // This belongs after RequestStop() and before wait() because if emulation stops on a GPU
  353. // breakpoint after (or before) RequestStop() is called, the emulation would never be able
  354. // to continue out to the main loop and terminate. Thus wait() would hang forever.
  355. // TODO(bunnei): This function is not thread safe, but it's being used as if it were
  356. Pica::g_debug_context->ClearBreakpoints();
  357. emit EmulationStopping();
  358. // Wait for emulation thread to complete and delete it
  359. emu_thread->wait();
  360. emu_thread = nullptr;
  361. // The emulation is stopped, so closing the window or not does not matter anymore
  362. disconnect(render_window, SIGNAL(Closed()), this, SLOT(OnStopGame()));
  363. // Update the GUI
  364. ui.action_Start->setEnabled(false);
  365. ui.action_Start->setText(tr("Start"));
  366. ui.action_Pause->setEnabled(false);
  367. ui.action_Stop->setEnabled(false);
  368. render_window->hide();
  369. game_list->show();
  370. // Disable status bar updates
  371. status_bar_update_timer.stop();
  372. emu_speed_label->setVisible(false);
  373. game_fps_label->setVisible(false);
  374. emu_frametime_label->setVisible(false);
  375. emulation_running = false;
  376. }
  377. void GMainWindow::StoreRecentFile(const QString& filename) {
  378. UISettings::values.recent_files.prepend(filename);
  379. UISettings::values.recent_files.removeDuplicates();
  380. while (UISettings::values.recent_files.size() > max_recent_files_item) {
  381. UISettings::values.recent_files.removeLast();
  382. }
  383. UpdateRecentFiles();
  384. }
  385. void GMainWindow::UpdateRecentFiles() {
  386. unsigned int num_recent_files =
  387. std::min(UISettings::values.recent_files.size(), static_cast<int>(max_recent_files_item));
  388. for (unsigned int i = 0; i < num_recent_files; i++) {
  389. QString text = QString("&%1. %2").arg(i + 1).arg(
  390. QFileInfo(UISettings::values.recent_files[i]).fileName());
  391. actions_recent_files[i]->setText(text);
  392. actions_recent_files[i]->setData(UISettings::values.recent_files[i]);
  393. actions_recent_files[i]->setToolTip(UISettings::values.recent_files[i]);
  394. actions_recent_files[i]->setVisible(true);
  395. }
  396. for (int j = num_recent_files; j < max_recent_files_item; ++j) {
  397. actions_recent_files[j]->setVisible(false);
  398. }
  399. // Grey out the recent files menu if the list is empty
  400. if (num_recent_files == 0) {
  401. ui.menu_recent_files->setEnabled(false);
  402. } else {
  403. ui.menu_recent_files->setEnabled(true);
  404. }
  405. }
  406. void GMainWindow::OnGameListLoadFile(QString game_path) {
  407. BootGame(game_path);
  408. }
  409. void GMainWindow::OnGameListOpenSaveFolder(u64 program_id) {
  410. std::string sdmc_dir = FileUtil::GetUserPath(D_SDMC_IDX);
  411. std::string path = FileSys::ArchiveSource_SDSaveData::GetSaveDataPathFor(sdmc_dir, program_id);
  412. QString qpath = QString::fromStdString(path);
  413. QDir dir(qpath);
  414. if (!dir.exists()) {
  415. QMessageBox::critical(this, tr("Error Opening Save Folder"), tr("Folder does not exist!"));
  416. return;
  417. }
  418. LOG_INFO(Frontend, "Opening save data path for program_id=%" PRIu64, program_id);
  419. QDesktopServices::openUrl(QUrl::fromLocalFile(qpath));
  420. }
  421. void GMainWindow::OnMenuLoadFile() {
  422. QString extensions;
  423. for (const auto& piece : game_list->supported_file_extensions)
  424. extensions += "*." + piece + " ";
  425. QString file_filter = tr("3DS Executable") + " (" + extensions + ")";
  426. file_filter += ";;" + tr("All Files (*.*)");
  427. QString filename = QFileDialog::getOpenFileName(this, tr("Load File"),
  428. UISettings::values.roms_path, file_filter);
  429. if (!filename.isEmpty()) {
  430. UISettings::values.roms_path = QFileInfo(filename).path();
  431. BootGame(filename);
  432. }
  433. }
  434. void GMainWindow::OnMenuLoadSymbolMap() {
  435. QString filename = QFileDialog::getOpenFileName(
  436. this, tr("Load Symbol Map"), UISettings::values.symbols_path, tr("Symbol Map (*.*)"));
  437. if (!filename.isEmpty()) {
  438. UISettings::values.symbols_path = QFileInfo(filename).path();
  439. LoadSymbolMap(filename.toStdString());
  440. }
  441. }
  442. void GMainWindow::OnMenuSelectGameListRoot() {
  443. QString dir_path = QFileDialog::getExistingDirectory(this, tr("Select Directory"));
  444. if (!dir_path.isEmpty()) {
  445. UISettings::values.gamedir = dir_path;
  446. game_list->PopulateAsync(dir_path, UISettings::values.gamedir_deepscan);
  447. }
  448. }
  449. void GMainWindow::OnMenuRecentFile() {
  450. QAction* action = qobject_cast<QAction*>(sender());
  451. assert(action);
  452. QString filename = action->data().toString();
  453. QFileInfo file_info(filename);
  454. if (file_info.exists()) {
  455. BootGame(filename);
  456. } else {
  457. // Display an error message and remove the file from the list.
  458. QMessageBox::information(this, tr("File not found"),
  459. tr("File \"%1\" not found").arg(filename));
  460. UISettings::values.recent_files.removeOne(filename);
  461. UpdateRecentFiles();
  462. }
  463. }
  464. void GMainWindow::OnStartGame() {
  465. emu_thread->SetRunning(true);
  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. render_window->ReloadSetKeymaps();
  509. config->Save();
  510. }
  511. }
  512. void GMainWindow::OnSwapScreens() {
  513. Settings::values.swap_screen = !Settings::values.swap_screen;
  514. Settings::Apply();
  515. }
  516. void GMainWindow::OnCreateGraphicsSurfaceViewer() {
  517. auto graphicsSurfaceViewerWidget = new GraphicsSurfaceWidget(Pica::g_debug_context, this);
  518. addDockWidget(Qt::RightDockWidgetArea, graphicsSurfaceViewerWidget);
  519. // TODO: Maybe graphicsSurfaceViewerWidget->setFloating(true);
  520. graphicsSurfaceViewerWidget->show();
  521. }
  522. void GMainWindow::UpdateStatusBar() {
  523. if (emu_thread == nullptr) {
  524. status_bar_update_timer.stop();
  525. return;
  526. }
  527. auto results = Core::System::GetInstance().GetAndResetPerfStats();
  528. emu_speed_label->setText(tr("Speed: %1%").arg(results.emulation_speed * 100.0, 0, 'f', 2));
  529. game_fps_label->setText(tr("Game: %1 FPS").arg(results.game_fps, 0, 'f', 1));
  530. emu_frametime_label->setText(tr("Frame: %1 ms").arg(results.frametime * 1000.0, 0, 'f', 2));
  531. emu_speed_label->setVisible(true);
  532. game_fps_label->setVisible(true);
  533. emu_frametime_label->setVisible(true);
  534. }
  535. bool GMainWindow::ConfirmClose() {
  536. if (emu_thread == nullptr || !UISettings::values.confirm_before_closing)
  537. return true;
  538. auto answer =
  539. QMessageBox::question(this, tr("Citra"), tr("Are you sure you want to close Citra?"),
  540. QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
  541. return answer != QMessageBox::No;
  542. }
  543. void GMainWindow::closeEvent(QCloseEvent* event) {
  544. if (!ConfirmClose()) {
  545. event->ignore();
  546. return;
  547. }
  548. UISettings::values.geometry = saveGeometry();
  549. UISettings::values.state = saveState();
  550. UISettings::values.renderwindow_geometry = render_window->saveGeometry();
  551. #if MICROPROFILE_ENABLED
  552. UISettings::values.microprofile_geometry = microProfileDialog->saveGeometry();
  553. UISettings::values.microprofile_visible = microProfileDialog->isVisible();
  554. #endif
  555. UISettings::values.single_window_mode = ui.action_Single_Window_Mode->isChecked();
  556. UISettings::values.display_titlebar = ui.action_Display_Dock_Widget_Headers->isChecked();
  557. UISettings::values.show_status_bar = ui.action_Show_Status_Bar->isChecked();
  558. UISettings::values.first_start = false;
  559. game_list->SaveInterfaceLayout();
  560. SaveHotkeys();
  561. // Shutdown session if the emu thread is active...
  562. if (emu_thread != nullptr)
  563. ShutdownGame();
  564. render_window->close();
  565. QWidget::closeEvent(event);
  566. }
  567. static bool IsSingleFileDropEvent(QDropEvent* event) {
  568. const QMimeData* mimeData = event->mimeData();
  569. return mimeData->hasUrls() && mimeData->urls().length() == 1;
  570. }
  571. void GMainWindow::dropEvent(QDropEvent* event) {
  572. if (IsSingleFileDropEvent(event) && ConfirmChangeGame()) {
  573. const QMimeData* mimeData = event->mimeData();
  574. QString filename = mimeData->urls().at(0).toLocalFile();
  575. BootGame(filename);
  576. }
  577. }
  578. void GMainWindow::dragEnterEvent(QDragEnterEvent* event) {
  579. if (IsSingleFileDropEvent(event)) {
  580. event->acceptProposedAction();
  581. }
  582. }
  583. void GMainWindow::dragMoveEvent(QDragMoveEvent* event) {
  584. event->acceptProposedAction();
  585. }
  586. bool GMainWindow::ConfirmChangeGame() {
  587. if (emu_thread == nullptr)
  588. return true;
  589. auto answer = QMessageBox::question(
  590. this, tr("Citra"),
  591. tr("Are you sure you want to stop the emulation? Any unsaved progress will be lost."),
  592. QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
  593. return answer != QMessageBox::No;
  594. }
  595. #ifdef main
  596. #undef main
  597. #endif
  598. int main(int argc, char* argv[]) {
  599. Log::Filter log_filter(Log::Level::Info);
  600. Log::SetFilter(&log_filter);
  601. MicroProfileOnThreadCreate("Frontend");
  602. SCOPE_EXIT({ MicroProfileShutdown(); });
  603. // Init settings params
  604. QCoreApplication::setOrganizationName("Citra team");
  605. QCoreApplication::setApplicationName("Citra");
  606. QApplication::setAttribute(Qt::AA_X11InitThreads);
  607. QApplication app(argc, argv);
  608. // Qt changes the locale and causes issues in float conversion using std::to_string() when
  609. // generating shaders
  610. setlocale(LC_ALL, "C");
  611. GMainWindow main_window;
  612. // After settings have been loaded by GMainWindow, apply the filter
  613. log_filter.ParseFilterString(Settings::values.log_filter);
  614. main_window.show();
  615. return app.exec();
  616. }