main.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 "qhexedit.h"
  9. #include "main.h"
  10. #include "common/common.h"
  11. #include "common/logging/text_formatter.h"
  12. #include "common/logging/log.h"
  13. #include "common/logging/backend.h"
  14. #include "common/logging/filter.h"
  15. #include "common/make_unique.h"
  16. #include "common/platform.h"
  17. #include "common/scope_exit.h"
  18. #if EMU_PLATFORM == PLATFORM_LINUX
  19. #include <unistd.h>
  20. #endif
  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_vertex_shader.h"
  33. #include "debugger/profiler.h"
  34. #include "core/settings.h"
  35. #include "core/system.h"
  36. #include "core/core.h"
  37. #include "core/loader/loader.h"
  38. #include "core/arm/disassembler/load_symbol_map.h"
  39. #include "citra_qt/config.h"
  40. #include "version.h"
  41. GMainWindow::GMainWindow() : emu_thread(nullptr)
  42. {
  43. Pica::g_debug_context = Pica::DebugContext::Construct();
  44. Config config;
  45. ui.setupUi(this);
  46. statusBar()->hide();
  47. render_window = new GRenderWindow(this, emu_thread.get());
  48. render_window->hide();
  49. profilerWidget = new ProfilerWidget(this);
  50. addDockWidget(Qt::BottomDockWidgetArea, profilerWidget);
  51. profilerWidget->hide();
  52. disasmWidget = new DisassemblerWidget(this, emu_thread.get());
  53. addDockWidget(Qt::BottomDockWidgetArea, disasmWidget);
  54. disasmWidget->hide();
  55. registersWidget = new RegistersWidget(this);
  56. addDockWidget(Qt::RightDockWidgetArea, registersWidget);
  57. registersWidget->hide();
  58. callstackWidget = new CallstackWidget(this);
  59. addDockWidget(Qt::RightDockWidgetArea, callstackWidget);
  60. callstackWidget->hide();
  61. graphicsWidget = new GPUCommandStreamWidget(this);
  62. addDockWidget(Qt::RightDockWidgetArea, graphicsWidget);
  63. graphicsWidget ->hide();
  64. graphicsCommandsWidget = new GPUCommandListWidget(this);
  65. addDockWidget(Qt::RightDockWidgetArea, graphicsCommandsWidget);
  66. graphicsCommandsWidget->hide();
  67. auto graphicsBreakpointsWidget = new GraphicsBreakPointsWidget(Pica::g_debug_context, this);
  68. addDockWidget(Qt::RightDockWidgetArea, graphicsBreakpointsWidget);
  69. graphicsBreakpointsWidget->hide();
  70. auto graphicsFramebufferWidget = new GraphicsFramebufferWidget(Pica::g_debug_context, this);
  71. addDockWidget(Qt::RightDockWidgetArea, graphicsFramebufferWidget);
  72. graphicsFramebufferWidget->hide();
  73. auto graphicsVertexShaderWidget = new GraphicsVertexShaderWidget(Pica::g_debug_context, this);
  74. addDockWidget(Qt::RightDockWidgetArea, graphicsVertexShaderWidget);
  75. graphicsVertexShaderWidget->hide();
  76. QMenu* debug_menu = ui.menu_View->addMenu(tr("Debugging"));
  77. debug_menu->addAction(profilerWidget->toggleViewAction());
  78. debug_menu->addAction(disasmWidget->toggleViewAction());
  79. debug_menu->addAction(registersWidget->toggleViewAction());
  80. debug_menu->addAction(callstackWidget->toggleViewAction());
  81. debug_menu->addAction(graphicsWidget->toggleViewAction());
  82. debug_menu->addAction(graphicsCommandsWidget->toggleViewAction());
  83. debug_menu->addAction(graphicsBreakpointsWidget->toggleViewAction());
  84. debug_menu->addAction(graphicsFramebufferWidget->toggleViewAction());
  85. debug_menu->addAction(graphicsVertexShaderWidget->toggleViewAction());
  86. // Set default UI state
  87. // geometry: 55% of the window contents are in the upper screen half, 45% in the lower half
  88. QDesktopWidget* desktop = ((QApplication*)QApplication::instance())->desktop();
  89. QRect screenRect = desktop->screenGeometry(this);
  90. int x, y, w, h;
  91. w = screenRect.width() * 2 / 3;
  92. h = screenRect.height() / 2;
  93. x = (screenRect.x() + screenRect.width()) / 2 - w / 2;
  94. y = (screenRect.y() + screenRect.height()) / 2 - h * 55 / 100;
  95. setGeometry(x, y, w, h);
  96. // Restore UI state
  97. QSettings settings(QSettings::IniFormat, QSettings::UserScope, "Citra team", "Citra");
  98. restoreGeometry(settings.value("geometry").toByteArray());
  99. restoreState(settings.value("state").toByteArray());
  100. render_window->restoreGeometry(settings.value("geometryRenderWindow").toByteArray());
  101. ui.action_Single_Window_Mode->setChecked(settings.value("singleWindowMode", true).toBool());
  102. ToggleWindowMode();
  103. ui.actionDisplay_widget_title_bars->setChecked(settings.value("displayTitleBars", true).toBool());
  104. OnDisplayTitleBars(ui.actionDisplay_widget_title_bars->isChecked());
  105. // Setup connections
  106. connect(ui.action_Load_File, SIGNAL(triggered()), this, SLOT(OnMenuLoadFile()));
  107. connect(ui.action_Load_Symbol_Map, SIGNAL(triggered()), this, SLOT(OnMenuLoadSymbolMap()));
  108. connect(ui.action_Start, SIGNAL(triggered()), this, SLOT(OnStartGame()));
  109. connect(ui.action_Pause, SIGNAL(triggered()), this, SLOT(OnPauseGame()));
  110. connect(ui.action_Stop, SIGNAL(triggered()), this, SLOT(OnStopGame()));
  111. connect(ui.action_Single_Window_Mode, SIGNAL(triggered(bool)), this, SLOT(ToggleWindowMode()));
  112. connect(ui.action_Hotkeys, SIGNAL(triggered()), this, SLOT(OnOpenHotkeysDialog()));
  113. connect(this, SIGNAL(EmulationStarting(EmuThread*)), disasmWidget, SLOT(OnEmulationStarting(EmuThread*)));
  114. connect(this, SIGNAL(EmulationStopping()), disasmWidget, SLOT(OnEmulationStopping()));
  115. connect(this, SIGNAL(EmulationStarting(EmuThread*)), registersWidget, SLOT(OnEmulationStarting(EmuThread*)));
  116. connect(this, SIGNAL(EmulationStopping()), registersWidget, SLOT(OnEmulationStopping()));
  117. connect(this, SIGNAL(EmulationStarting(EmuThread*)), render_window, SLOT(OnEmulationStarting(EmuThread*)));
  118. connect(this, SIGNAL(EmulationStopping()), render_window, SLOT(OnEmulationStopping()));
  119. // Setup hotkeys
  120. RegisterHotkey("Main Window", "Load File", QKeySequence::Open);
  121. RegisterHotkey("Main Window", "Start Emulation");
  122. LoadHotkeys(settings);
  123. connect(GetHotkey("Main Window", "Load File", this), SIGNAL(activated()), this, SLOT(OnMenuLoadFile()));
  124. connect(GetHotkey("Main Window", "Start Emulation", this), SIGNAL(activated()), this, SLOT(OnStartGame()));
  125. std::string window_title = Common::StringFromFormat("Citra | %s-%s", Common::g_scm_branch, Common::g_scm_desc);
  126. setWindowTitle(window_title.c_str());
  127. show();
  128. QStringList args = QApplication::arguments();
  129. if (args.length() >= 2) {
  130. BootGame(args[1].toStdString());
  131. }
  132. }
  133. GMainWindow::~GMainWindow()
  134. {
  135. // will get automatically deleted otherwise
  136. if (render_window->parent() == nullptr)
  137. delete render_window;
  138. Pica::g_debug_context.reset();
  139. }
  140. void GMainWindow::OnDisplayTitleBars(bool show)
  141. {
  142. QList<QDockWidget*> widgets = findChildren<QDockWidget*>();
  143. if (show) {
  144. for (QDockWidget* widget: widgets) {
  145. QWidget* old = widget->titleBarWidget();
  146. widget->setTitleBarWidget(nullptr);
  147. if (old != nullptr)
  148. delete old;
  149. }
  150. } else {
  151. for (QDockWidget* widget: widgets) {
  152. QWidget* old = widget->titleBarWidget();
  153. widget->setTitleBarWidget(new QWidget());
  154. if (old != nullptr)
  155. delete old;
  156. }
  157. }
  158. }
  159. void GMainWindow::BootGame(std::string filename) {
  160. LOG_INFO(Frontend, "Citra starting...\n");
  161. // Initialize the core emulation
  162. System::Init(render_window);
  163. // Load the game
  164. if (Loader::ResultStatus::Success != Loader::LoadFile(filename)) {
  165. LOG_CRITICAL(Frontend, "Failed to load ROM!");
  166. System::Shutdown();
  167. return;
  168. }
  169. // Create and start the emulation thread
  170. emu_thread = Common::make_unique<EmuThread>(render_window);
  171. emit EmulationStarting(emu_thread.get());
  172. emu_thread->start();
  173. // BlockingQueuedConnection is important here, it makes sure we've finished refreshing our views before the CPU continues
  174. connect(emu_thread.get(), SIGNAL(DebugModeEntered()), disasmWidget, SLOT(OnDebugModeEntered()), Qt::BlockingQueuedConnection);
  175. connect(emu_thread.get(), SIGNAL(DebugModeEntered()), registersWidget, SLOT(OnDebugModeEntered()), Qt::BlockingQueuedConnection);
  176. connect(emu_thread.get(), SIGNAL(DebugModeEntered()), callstackWidget, SLOT(OnDebugModeEntered()), Qt::BlockingQueuedConnection);
  177. connect(emu_thread.get(), SIGNAL(DebugModeLeft()), disasmWidget, SLOT(OnDebugModeLeft()), Qt::BlockingQueuedConnection);
  178. connect(emu_thread.get(), SIGNAL(DebugModeLeft()), registersWidget, SLOT(OnDebugModeLeft()), Qt::BlockingQueuedConnection);
  179. connect(emu_thread.get(), SIGNAL(DebugModeLeft()), callstackWidget, SLOT(OnDebugModeLeft()), Qt::BlockingQueuedConnection);
  180. // Update the GUI
  181. registersWidget->OnDebugModeEntered();
  182. callstackWidget->OnDebugModeEntered();
  183. render_window->show();
  184. OnStartGame();
  185. }
  186. void GMainWindow::ShutdownGame() {
  187. emu_thread->RequestStop();
  188. // Release emu threads from any breakpoints
  189. // This belongs after RequestStop() and before wait() because if emulation stops on a GPU
  190. // breakpoint after (or before) RequestStop() is called, the emulation would never be able
  191. // to continue out to the main loop and terminate. Thus wait() would hang forever.
  192. // TODO(bunnei): This function is not thread safe, but it's being used as if it were
  193. Pica::g_debug_context->ClearBreakpoints();
  194. emit EmulationStopping();
  195. // Wait for emulation thread to complete and delete it
  196. emu_thread->wait();
  197. emu_thread = nullptr;
  198. // Shutdown the core emulation
  199. System::Shutdown();
  200. // Update the GUI
  201. ui.action_Start->setEnabled(false);
  202. ui.action_Pause->setEnabled(false);
  203. ui.action_Stop->setEnabled(false);
  204. render_window->hide();
  205. }
  206. void GMainWindow::OnMenuLoadFile()
  207. {
  208. QString filename = QFileDialog::getOpenFileName(this, tr("Load File"), QString(), tr("3DS executable (*.3ds *.3dsx *.elf *.axf *.bin *.cci *.cxi)"));
  209. if (filename.size()) {
  210. // Shutdown previous session if the emu thread is still active...
  211. if (emu_thread != nullptr)
  212. ShutdownGame();
  213. BootGame(filename.toLatin1().data());
  214. }
  215. }
  216. void GMainWindow::OnMenuLoadSymbolMap() {
  217. QString filename = QFileDialog::getOpenFileName(this, tr("Load Symbol Map"), QString(), tr("Symbol map (*)"));
  218. if (filename.size())
  219. LoadSymbolMap(filename.toLatin1().data());
  220. }
  221. void GMainWindow::OnStartGame()
  222. {
  223. emu_thread->SetRunning(true);
  224. ui.action_Start->setEnabled(false);
  225. ui.action_Pause->setEnabled(true);
  226. ui.action_Stop->setEnabled(true);
  227. }
  228. void GMainWindow::OnPauseGame()
  229. {
  230. emu_thread->SetRunning(false);
  231. ui.action_Start->setEnabled(true);
  232. ui.action_Pause->setEnabled(false);
  233. ui.action_Stop->setEnabled(true);
  234. }
  235. void GMainWindow::OnStopGame() {
  236. ShutdownGame();
  237. }
  238. void GMainWindow::OnOpenHotkeysDialog()
  239. {
  240. GHotkeysDialog dialog(this);
  241. dialog.exec();
  242. }
  243. void GMainWindow::ToggleWindowMode() {
  244. if (ui.action_Single_Window_Mode->isChecked()) {
  245. // Render in the main window...
  246. render_window->BackupGeometry();
  247. ui.horizontalLayout->addWidget(render_window);
  248. render_window->setVisible(true);
  249. render_window->setFocusPolicy(Qt::ClickFocus);
  250. render_window->setFocus();
  251. } else {
  252. // Render in a separate window...
  253. ui.horizontalLayout->removeWidget(render_window);
  254. render_window->setParent(nullptr);
  255. render_window->setVisible(true);
  256. render_window->RestoreGeometry();
  257. render_window->setFocusPolicy(Qt::NoFocus);
  258. }
  259. }
  260. void GMainWindow::OnConfigure()
  261. {
  262. //GControllerConfigDialog* dialog = new GControllerConfigDialog(controller_ports, this);
  263. }
  264. void GMainWindow::closeEvent(QCloseEvent* event)
  265. {
  266. // Save window layout
  267. QSettings settings(QSettings::IniFormat, QSettings::UserScope, "Citra team", "Citra");
  268. settings.setValue("geometry", saveGeometry());
  269. settings.setValue("state", saveState());
  270. settings.setValue("geometryRenderWindow", render_window->saveGeometry());
  271. settings.setValue("singleWindowMode", ui.action_Single_Window_Mode->isChecked());
  272. settings.setValue("displayTitleBars", ui.actionDisplay_widget_title_bars->isChecked());
  273. settings.setValue("firstStart", false);
  274. SaveHotkeys(settings);
  275. render_window->close();
  276. QWidget::closeEvent(event);
  277. }
  278. #ifdef main
  279. #undef main
  280. #endif
  281. int __cdecl main(int argc, char* argv[])
  282. {
  283. std::shared_ptr<Log::Logger> logger = Log::InitGlobalLogger();
  284. Log::Filter log_filter(Log::Level::Info);
  285. Log::SetFilter(&log_filter);
  286. std::thread logging_thread(Log::TextLoggingLoop, logger);
  287. SCOPE_EXIT({
  288. logger->Close();
  289. logging_thread.join();
  290. });
  291. QApplication::setAttribute(Qt::AA_X11InitThreads);
  292. QApplication app(argc, argv);
  293. GMainWindow main_window;
  294. // After settings have been loaded by GMainWindow, apply the filter
  295. log_filter.ParseFilterString(Settings::values.log_filter);
  296. main_window.show();
  297. return app.exec();
  298. }