main.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <thread>
  5. #include <QtGui>
  6. #include <QDesktopWidget>
  7. #include <QFileDialog>
  8. #include <QMessageBox>
  9. #include "qhexedit.h"
  10. #include "main.h"
  11. #include "common/string_util.h"
  12. #include "common/logging/text_formatter.h"
  13. #include "common/logging/log.h"
  14. #include "common/logging/backend.h"
  15. #include "common/logging/filter.h"
  16. #include "common/make_unique.h"
  17. #include "common/microprofile.h"
  18. #include "common/platform.h"
  19. #include "common/scm_rev.h"
  20. #include "common/scope_exit.h"
  21. #include "bootmanager.h"
  22. #include "hotkeys.h"
  23. //debugger
  24. #include "debugger/disassembler.h"
  25. #include "debugger/registers.h"
  26. #include "debugger/callstack.h"
  27. #include "debugger/ramview.h"
  28. #include "debugger/graphics.h"
  29. #include "debugger/graphics_breakpoints.h"
  30. #include "debugger/graphics_cmdlists.h"
  31. #include "debugger/graphics_framebuffer.h"
  32. #include "debugger/graphics_tracing.h"
  33. #include "debugger/graphics_vertex_shader.h"
  34. #include "debugger/profiler.h"
  35. #include "core/settings.h"
  36. #include "core/system.h"
  37. #include "core/core.h"
  38. #include "core/loader/loader.h"
  39. #include "core/arm/disassembler/load_symbol_map.h"
  40. #include "citra_qt/config.h"
  41. #include "video_core/video_core.h"
  42. #include "version.h"
  43. GMainWindow::GMainWindow() : emu_thread(nullptr)
  44. {
  45. Pica::g_debug_context = Pica::DebugContext::Construct();
  46. Config config;
  47. ui.setupUi(this);
  48. statusBar()->hide();
  49. render_window = new GRenderWindow(this, emu_thread.get());
  50. render_window->hide();
  51. profilerWidget = new ProfilerWidget(this);
  52. addDockWidget(Qt::BottomDockWidgetArea, profilerWidget);
  53. profilerWidget->hide();
  54. microProfileDialog = new MicroProfileDialog(this);
  55. microProfileDialog->hide();
  56. disasmWidget = new DisassemblerWidget(this, emu_thread.get());
  57. addDockWidget(Qt::BottomDockWidgetArea, disasmWidget);
  58. disasmWidget->hide();
  59. registersWidget = new RegistersWidget(this);
  60. addDockWidget(Qt::RightDockWidgetArea, registersWidget);
  61. registersWidget->hide();
  62. callstackWidget = new CallstackWidget(this);
  63. addDockWidget(Qt::RightDockWidgetArea, callstackWidget);
  64. callstackWidget->hide();
  65. graphicsWidget = new GPUCommandStreamWidget(this);
  66. addDockWidget(Qt::RightDockWidgetArea, graphicsWidget);
  67. graphicsWidget ->hide();
  68. graphicsCommandsWidget = new GPUCommandListWidget(this);
  69. addDockWidget(Qt::RightDockWidgetArea, graphicsCommandsWidget);
  70. graphicsCommandsWidget->hide();
  71. auto graphicsBreakpointsWidget = new GraphicsBreakPointsWidget(Pica::g_debug_context, this);
  72. addDockWidget(Qt::RightDockWidgetArea, graphicsBreakpointsWidget);
  73. graphicsBreakpointsWidget->hide();
  74. auto graphicsFramebufferWidget = new GraphicsFramebufferWidget(Pica::g_debug_context, this);
  75. addDockWidget(Qt::RightDockWidgetArea, graphicsFramebufferWidget);
  76. graphicsFramebufferWidget->hide();
  77. auto graphicsVertexShaderWidget = new GraphicsVertexShaderWidget(Pica::g_debug_context, this);
  78. addDockWidget(Qt::RightDockWidgetArea, graphicsVertexShaderWidget);
  79. graphicsVertexShaderWidget->hide();
  80. auto graphicsTracingWidget = new GraphicsTracingWidget(Pica::g_debug_context, this);
  81. addDockWidget(Qt::RightDockWidgetArea, graphicsTracingWidget);
  82. graphicsTracingWidget->hide();
  83. QMenu* debug_menu = ui.menu_View->addMenu(tr("Debugging"));
  84. debug_menu->addAction(profilerWidget->toggleViewAction());
  85. debug_menu->addAction(microProfileDialog->toggleViewAction());
  86. debug_menu->addAction(disasmWidget->toggleViewAction());
  87. debug_menu->addAction(registersWidget->toggleViewAction());
  88. debug_menu->addAction(callstackWidget->toggleViewAction());
  89. debug_menu->addAction(graphicsWidget->toggleViewAction());
  90. debug_menu->addAction(graphicsCommandsWidget->toggleViewAction());
  91. debug_menu->addAction(graphicsBreakpointsWidget->toggleViewAction());
  92. debug_menu->addAction(graphicsFramebufferWidget->toggleViewAction());
  93. debug_menu->addAction(graphicsVertexShaderWidget->toggleViewAction());
  94. debug_menu->addAction(graphicsTracingWidget->toggleViewAction());
  95. // Set default UI state
  96. // geometry: 55% of the window contents are in the upper screen half, 45% in the lower half
  97. QDesktopWidget* desktop = ((QApplication*)QApplication::instance())->desktop();
  98. QRect screenRect = desktop->screenGeometry(this);
  99. int x, y, w, h;
  100. w = screenRect.width() * 2 / 3;
  101. h = screenRect.height() / 2;
  102. x = (screenRect.x() + screenRect.width()) / 2 - w / 2;
  103. y = (screenRect.y() + screenRect.height()) / 2 - h * 55 / 100;
  104. setGeometry(x, y, w, h);
  105. // Restore UI state
  106. QSettings settings;
  107. settings.beginGroup("UILayout");
  108. restoreGeometry(settings.value("geometry").toByteArray());
  109. restoreState(settings.value("state").toByteArray());
  110. render_window->restoreGeometry(settings.value("geometryRenderWindow").toByteArray());
  111. microProfileDialog->restoreGeometry(settings.value("microProfileDialogGeometry").toByteArray());
  112. microProfileDialog->setVisible(settings.value("microProfileDialogVisible").toBool());
  113. settings.endGroup();
  114. ui.action_Use_Hardware_Renderer->setChecked(Settings::values.use_hw_renderer);
  115. SetHardwareRendererEnabled(ui.action_Use_Hardware_Renderer->isChecked());
  116. ui.action_Use_Shader_JIT->setChecked(Settings::values.use_shader_jit);
  117. SetShaderJITEnabled(ui.action_Use_Shader_JIT->isChecked());
  118. ui.action_Single_Window_Mode->setChecked(settings.value("singleWindowMode", true).toBool());
  119. ToggleWindowMode();
  120. ui.actionDisplay_widget_title_bars->setChecked(settings.value("displayTitleBars", true).toBool());
  121. OnDisplayTitleBars(ui.actionDisplay_widget_title_bars->isChecked());
  122. // Prepare actions for recent files
  123. for (int i = 0; i < max_recent_files_item; ++i) {
  124. actions_recent_files[i] = new QAction(this);
  125. actions_recent_files[i]->setVisible(false);
  126. connect(actions_recent_files[i], SIGNAL(triggered()), this, SLOT(OnMenuRecentFile()));
  127. ui.menu_recent_files->addAction(actions_recent_files[i]);
  128. }
  129. UpdateRecentFiles();
  130. // Setup connections
  131. connect(ui.action_Load_File, SIGNAL(triggered()), this, SLOT(OnMenuLoadFile()));
  132. connect(ui.action_Load_Symbol_Map, SIGNAL(triggered()), this, SLOT(OnMenuLoadSymbolMap()));
  133. connect(ui.action_Start, SIGNAL(triggered()), this, SLOT(OnStartGame()));
  134. connect(ui.action_Pause, SIGNAL(triggered()), this, SLOT(OnPauseGame()));
  135. connect(ui.action_Stop, SIGNAL(triggered()), this, SLOT(OnStopGame()));
  136. connect(ui.action_Use_Hardware_Renderer, SIGNAL(triggered(bool)), this, SLOT(SetHardwareRendererEnabled(bool)));
  137. connect(ui.action_Use_Shader_JIT, SIGNAL(triggered(bool)), this, SLOT(SetShaderJITEnabled(bool)));
  138. connect(ui.action_Single_Window_Mode, SIGNAL(triggered(bool)), this, SLOT(ToggleWindowMode()));
  139. connect(ui.action_Hotkeys, SIGNAL(triggered()), this, SLOT(OnOpenHotkeysDialog()));
  140. connect(this, SIGNAL(EmulationStarting(EmuThread*)), disasmWidget, SLOT(OnEmulationStarting(EmuThread*)));
  141. connect(this, SIGNAL(EmulationStopping()), disasmWidget, SLOT(OnEmulationStopping()));
  142. connect(this, SIGNAL(EmulationStarting(EmuThread*)), registersWidget, SLOT(OnEmulationStarting(EmuThread*)));
  143. connect(this, SIGNAL(EmulationStopping()), registersWidget, SLOT(OnEmulationStopping()));
  144. connect(this, SIGNAL(EmulationStarting(EmuThread*)), render_window, SLOT(OnEmulationStarting(EmuThread*)));
  145. connect(this, SIGNAL(EmulationStopping()), render_window, SLOT(OnEmulationStopping()));
  146. connect(this, SIGNAL(EmulationStarting(EmuThread*)), graphicsTracingWidget, SLOT(OnEmulationStarting(EmuThread*)));
  147. connect(this, SIGNAL(EmulationStopping()), graphicsTracingWidget, SLOT(OnEmulationStopping()));
  148. // Setup hotkeys
  149. RegisterHotkey("Main Window", "Load File", QKeySequence::Open);
  150. RegisterHotkey("Main Window", "Start Emulation");
  151. LoadHotkeys(settings);
  152. connect(GetHotkey("Main Window", "Load File", this), SIGNAL(activated()), this, SLOT(OnMenuLoadFile()));
  153. connect(GetHotkey("Main Window", "Start Emulation", this), SIGNAL(activated()), this, SLOT(OnStartGame()));
  154. std::string window_title = Common::StringFromFormat("Citra | %s-%s", Common::g_scm_branch, Common::g_scm_desc);
  155. setWindowTitle(window_title.c_str());
  156. show();
  157. QStringList args = QApplication::arguments();
  158. if (args.length() >= 2) {
  159. BootGame(args[1].toStdString());
  160. }
  161. }
  162. GMainWindow::~GMainWindow()
  163. {
  164. // will get automatically deleted otherwise
  165. if (render_window->parent() == nullptr)
  166. delete render_window;
  167. Pica::g_debug_context.reset();
  168. }
  169. void GMainWindow::OnDisplayTitleBars(bool show)
  170. {
  171. QList<QDockWidget*> widgets = findChildren<QDockWidget*>();
  172. if (show) {
  173. for (QDockWidget* widget: widgets) {
  174. QWidget* old = widget->titleBarWidget();
  175. widget->setTitleBarWidget(nullptr);
  176. if (old != nullptr)
  177. delete old;
  178. }
  179. } else {
  180. for (QDockWidget* widget: widgets) {
  181. QWidget* old = widget->titleBarWidget();
  182. widget->setTitleBarWidget(new QWidget());
  183. if (old != nullptr)
  184. delete old;
  185. }
  186. }
  187. }
  188. void GMainWindow::BootGame(const std::string& filename) {
  189. LOG_INFO(Frontend, "Citra starting...\n");
  190. // Shutdown previous session if the emu thread is still active...
  191. if (emu_thread != nullptr)
  192. ShutdownGame();
  193. // Initialize the core emulation
  194. System::Init(render_window);
  195. // Load the game
  196. if (Loader::ResultStatus::Success != Loader::LoadFile(filename)) {
  197. LOG_CRITICAL(Frontend, "Failed to load ROM!");
  198. System::Shutdown();
  199. return;
  200. }
  201. // Create and start the emulation thread
  202. emu_thread = Common::make_unique<EmuThread>(render_window);
  203. emit EmulationStarting(emu_thread.get());
  204. render_window->moveContext();
  205. emu_thread->start();
  206. // BlockingQueuedConnection is important here, it makes sure we've finished refreshing our views before the CPU continues
  207. connect(emu_thread.get(), SIGNAL(DebugModeEntered()), disasmWidget, SLOT(OnDebugModeEntered()), Qt::BlockingQueuedConnection);
  208. connect(emu_thread.get(), SIGNAL(DebugModeEntered()), registersWidget, SLOT(OnDebugModeEntered()), Qt::BlockingQueuedConnection);
  209. connect(emu_thread.get(), SIGNAL(DebugModeEntered()), callstackWidget, SLOT(OnDebugModeEntered()), Qt::BlockingQueuedConnection);
  210. connect(emu_thread.get(), SIGNAL(DebugModeLeft()), disasmWidget, SLOT(OnDebugModeLeft()), Qt::BlockingQueuedConnection);
  211. connect(emu_thread.get(), SIGNAL(DebugModeLeft()), registersWidget, SLOT(OnDebugModeLeft()), Qt::BlockingQueuedConnection);
  212. connect(emu_thread.get(), SIGNAL(DebugModeLeft()), callstackWidget, SLOT(OnDebugModeLeft()), Qt::BlockingQueuedConnection);
  213. // Update the GUI
  214. registersWidget->OnDebugModeEntered();
  215. callstackWidget->OnDebugModeEntered();
  216. render_window->show();
  217. OnStartGame();
  218. }
  219. void GMainWindow::ShutdownGame() {
  220. emu_thread->RequestStop();
  221. // Release emu threads from any breakpoints
  222. // This belongs after RequestStop() and before wait() because if emulation stops on a GPU
  223. // breakpoint after (or before) RequestStop() is called, the emulation would never be able
  224. // to continue out to the main loop and terminate. Thus wait() would hang forever.
  225. // TODO(bunnei): This function is not thread safe, but it's being used as if it were
  226. Pica::g_debug_context->ClearBreakpoints();
  227. emit EmulationStopping();
  228. // Wait for emulation thread to complete and delete it
  229. emu_thread->wait();
  230. emu_thread = nullptr;
  231. // Update the GUI
  232. ui.action_Start->setEnabled(false);
  233. ui.action_Start->setText(tr("Start"));
  234. ui.action_Pause->setEnabled(false);
  235. ui.action_Stop->setEnabled(false);
  236. render_window->hide();
  237. }
  238. void GMainWindow::StoreRecentFile(const QString& filename)
  239. {
  240. QSettings settings;
  241. QStringList recent_files = settings.value("recentFiles").toStringList();
  242. recent_files.prepend(filename);
  243. recent_files.removeDuplicates();
  244. settings.setValue("recentFiles", recent_files);
  245. UpdateRecentFiles();
  246. }
  247. void GMainWindow::UpdateRecentFiles() {
  248. QSettings settings;
  249. QStringList recent_files = settings.value("recentFiles").toStringList();
  250. unsigned int num_recent_files = std::min(recent_files.size(), static_cast<int>(max_recent_files_item));
  251. for (unsigned int i = 0; i < num_recent_files; i++) {
  252. QString text = QString("&%1. %2").arg(i + 1).arg(QFileInfo(recent_files[i]).fileName());
  253. actions_recent_files[i]->setText(text);
  254. actions_recent_files[i]->setData(recent_files[i]);
  255. actions_recent_files[i]->setToolTip(recent_files[i]);
  256. actions_recent_files[i]->setVisible(true);
  257. }
  258. for (int j = num_recent_files; j < max_recent_files_item; ++j) {
  259. actions_recent_files[j]->setVisible(false);
  260. }
  261. // Grey out the recent files menu if the list is empty
  262. if (num_recent_files == 0) {
  263. ui.menu_recent_files->setEnabled(false);
  264. } else {
  265. ui.menu_recent_files->setEnabled(true);
  266. }
  267. }
  268. void GMainWindow::OnMenuLoadFile() {
  269. QSettings settings;
  270. QString rom_path = settings.value("romsPath", QString()).toString();
  271. QString filename = QFileDialog::getOpenFileName(this, tr("Load File"), rom_path, tr("3DS executable (*.3ds *.3dsx *.elf *.axf *.cci *.cxi)"));
  272. if (filename.size()) {
  273. settings.setValue("romsPath", QFileInfo(filename).path());
  274. StoreRecentFile(filename);
  275. BootGame(filename.toLatin1().data());
  276. }
  277. }
  278. void GMainWindow::OnMenuLoadSymbolMap() {
  279. QSettings settings;
  280. QString symbol_path = settings.value("symbolsPath", QString()).toString();
  281. QString filename = QFileDialog::getOpenFileName(this, tr("Load Symbol Map"), symbol_path, tr("Symbol map (*)"));
  282. if (filename.size()) {
  283. settings.setValue("symbolsPath", QFileInfo(filename).path());
  284. LoadSymbolMap(filename.toLatin1().data());
  285. }
  286. }
  287. void GMainWindow::OnMenuRecentFile() {
  288. QAction* action = qobject_cast<QAction*>(sender());
  289. assert(action);
  290. QString filename = action->data().toString();
  291. QFileInfo file_info(filename);
  292. if (file_info.exists()) {
  293. BootGame(filename.toLatin1().data());
  294. StoreRecentFile(filename); // Put the filename on top of the list
  295. } else {
  296. // Display an error message and remove the file from the list.
  297. QMessageBox::information(this, tr("File not found"), tr("File \"%1\" not found").arg(filename));
  298. QSettings settings;
  299. QStringList recent_files = settings.value("recentFiles").toStringList();
  300. recent_files.removeOne(filename);
  301. settings.setValue("recentFiles", recent_files);
  302. UpdateRecentFiles();
  303. }
  304. }
  305. void GMainWindow::OnStartGame() {
  306. emu_thread->SetRunning(true);
  307. ui.action_Start->setEnabled(false);
  308. ui.action_Start->setText(tr("Continue"));
  309. ui.action_Pause->setEnabled(true);
  310. ui.action_Stop->setEnabled(true);
  311. }
  312. void GMainWindow::OnPauseGame() {
  313. emu_thread->SetRunning(false);
  314. ui.action_Start->setEnabled(true);
  315. ui.action_Pause->setEnabled(false);
  316. ui.action_Stop->setEnabled(true);
  317. }
  318. void GMainWindow::OnStopGame() {
  319. ShutdownGame();
  320. }
  321. void GMainWindow::OnOpenHotkeysDialog() {
  322. GHotkeysDialog dialog(this);
  323. dialog.exec();
  324. }
  325. void GMainWindow::SetHardwareRendererEnabled(bool enabled) {
  326. VideoCore::g_hw_renderer_enabled = enabled;
  327. }
  328. void GMainWindow::SetShaderJITEnabled(bool enabled) {
  329. VideoCore::g_shader_jit_enabled = enabled;
  330. }
  331. void GMainWindow::ToggleWindowMode() {
  332. if (ui.action_Single_Window_Mode->isChecked()) {
  333. // Render in the main window...
  334. render_window->BackupGeometry();
  335. ui.horizontalLayout->addWidget(render_window);
  336. render_window->setVisible(true);
  337. render_window->setFocusPolicy(Qt::ClickFocus);
  338. render_window->setFocus();
  339. } else {
  340. // Render in a separate window...
  341. ui.horizontalLayout->removeWidget(render_window);
  342. render_window->setParent(nullptr);
  343. render_window->setVisible(true);
  344. render_window->RestoreGeometry();
  345. render_window->setFocusPolicy(Qt::NoFocus);
  346. }
  347. }
  348. void GMainWindow::OnConfigure() {
  349. //GControllerConfigDialog* dialog = new GControllerConfigDialog(controller_ports, this);
  350. }
  351. void GMainWindow::closeEvent(QCloseEvent* event) {
  352. // Save window layout
  353. QSettings settings(QSettings::IniFormat, QSettings::UserScope, "Citra team", "Citra");
  354. settings.beginGroup("UILayout");
  355. settings.setValue("geometry", saveGeometry());
  356. settings.setValue("state", saveState());
  357. settings.setValue("geometryRenderWindow", render_window->saveGeometry());
  358. settings.setValue("microProfileDialogGeometry", microProfileDialog->saveGeometry());
  359. settings.setValue("microProfileDialogVisible", microProfileDialog->isVisible());
  360. settings.endGroup();
  361. settings.setValue("singleWindowMode", ui.action_Single_Window_Mode->isChecked());
  362. settings.setValue("displayTitleBars", ui.actionDisplay_widget_title_bars->isChecked());
  363. settings.setValue("firstStart", false);
  364. SaveHotkeys(settings);
  365. // Shutdown session if the emu thread is active...
  366. if (emu_thread != nullptr)
  367. ShutdownGame();
  368. render_window->close();
  369. QWidget::closeEvent(event);
  370. }
  371. #ifdef main
  372. #undef main
  373. #endif
  374. int main(int argc, char* argv[]) {
  375. Log::Filter log_filter(Log::Level::Info);
  376. Log::SetFilter(&log_filter);
  377. MicroProfileOnThreadCreate("Frontend");
  378. SCOPE_EXIT({
  379. MicroProfileShutdown();
  380. });
  381. // Init settings params
  382. QSettings::setDefaultFormat(QSettings::IniFormat);
  383. QCoreApplication::setOrganizationName("Citra team");
  384. QCoreApplication::setApplicationName("Citra");
  385. QApplication::setAttribute(Qt::AA_X11InitThreads);
  386. QApplication app(argc, argv);
  387. GMainWindow main_window;
  388. // After settings have been loaded by GMainWindow, apply the filter
  389. log_filter.ParseFilterString(Settings::values.log_filter);
  390. main_window.show();
  391. return app.exec();
  392. }