main.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. restoreGeometry(settings.value("geometry").toByteArray());
  108. restoreState(settings.value("state").toByteArray());
  109. render_window->restoreGeometry(settings.value("geometryRenderWindow").toByteArray());
  110. microProfileDialog->restoreGeometry(settings.value("microProfileDialogGeometry").toByteArray());
  111. microProfileDialog->setVisible(settings.value("microProfileDialogVisible").toBool());
  112. ui.action_Use_Hardware_Renderer->setChecked(Settings::values.use_hw_renderer);
  113. SetHardwareRendererEnabled(ui.action_Use_Hardware_Renderer->isChecked());
  114. ui.action_Use_Shader_JIT->setChecked(Settings::values.use_shader_jit);
  115. SetShaderJITEnabled(ui.action_Use_Shader_JIT->isChecked());
  116. ui.action_Single_Window_Mode->setChecked(settings.value("singleWindowMode", true).toBool());
  117. ToggleWindowMode();
  118. ui.actionDisplay_widget_title_bars->setChecked(settings.value("displayTitleBars", true).toBool());
  119. OnDisplayTitleBars(ui.actionDisplay_widget_title_bars->isChecked());
  120. // Prepare actions for recent files
  121. for (int i = 0; i < max_recent_files_item; ++i) {
  122. actions_recent_files[i] = new QAction(this);
  123. actions_recent_files[i]->setVisible(false);
  124. connect(actions_recent_files[i], SIGNAL(triggered()), this, SLOT(OnMenuRecentFile()));
  125. ui.menu_recent_files->addAction(actions_recent_files[i]);
  126. }
  127. UpdateRecentFiles();
  128. // Setup connections
  129. connect(ui.action_Load_File, SIGNAL(triggered()), this, SLOT(OnMenuLoadFile()));
  130. connect(ui.action_Load_Symbol_Map, SIGNAL(triggered()), this, SLOT(OnMenuLoadSymbolMap()));
  131. connect(ui.action_Start, SIGNAL(triggered()), this, SLOT(OnStartGame()));
  132. connect(ui.action_Pause, SIGNAL(triggered()), this, SLOT(OnPauseGame()));
  133. connect(ui.action_Stop, SIGNAL(triggered()), this, SLOT(OnStopGame()));
  134. connect(ui.action_Use_Hardware_Renderer, SIGNAL(triggered(bool)), this, SLOT(SetHardwareRendererEnabled(bool)));
  135. connect(ui.action_Use_Shader_JIT, SIGNAL(triggered(bool)), this, SLOT(SetShaderJITEnabled(bool)));
  136. connect(ui.action_Single_Window_Mode, SIGNAL(triggered(bool)), this, SLOT(ToggleWindowMode()));
  137. connect(ui.action_Hotkeys, SIGNAL(triggered()), this, SLOT(OnOpenHotkeysDialog()));
  138. connect(this, SIGNAL(EmulationStarting(EmuThread*)), disasmWidget, SLOT(OnEmulationStarting(EmuThread*)));
  139. connect(this, SIGNAL(EmulationStopping()), disasmWidget, SLOT(OnEmulationStopping()));
  140. connect(this, SIGNAL(EmulationStarting(EmuThread*)), registersWidget, SLOT(OnEmulationStarting(EmuThread*)));
  141. connect(this, SIGNAL(EmulationStopping()), registersWidget, SLOT(OnEmulationStopping()));
  142. connect(this, SIGNAL(EmulationStarting(EmuThread*)), render_window, SLOT(OnEmulationStarting(EmuThread*)));
  143. connect(this, SIGNAL(EmulationStopping()), render_window, SLOT(OnEmulationStopping()));
  144. connect(this, SIGNAL(EmulationStarting(EmuThread*)), graphicsTracingWidget, SLOT(OnEmulationStarting(EmuThread*)));
  145. connect(this, SIGNAL(EmulationStopping()), graphicsTracingWidget, SLOT(OnEmulationStopping()));
  146. // Setup hotkeys
  147. RegisterHotkey("Main Window", "Load File", QKeySequence::Open);
  148. RegisterHotkey("Main Window", "Start Emulation");
  149. LoadHotkeys(settings);
  150. connect(GetHotkey("Main Window", "Load File", this), SIGNAL(activated()), this, SLOT(OnMenuLoadFile()));
  151. connect(GetHotkey("Main Window", "Start Emulation", this), SIGNAL(activated()), this, SLOT(OnStartGame()));
  152. std::string window_title = Common::StringFromFormat("Citra | %s-%s", Common::g_scm_branch, Common::g_scm_desc);
  153. setWindowTitle(window_title.c_str());
  154. show();
  155. QStringList args = QApplication::arguments();
  156. if (args.length() >= 2) {
  157. BootGame(args[1].toStdString());
  158. }
  159. }
  160. GMainWindow::~GMainWindow()
  161. {
  162. // will get automatically deleted otherwise
  163. if (render_window->parent() == nullptr)
  164. delete render_window;
  165. Pica::g_debug_context.reset();
  166. }
  167. void GMainWindow::OnDisplayTitleBars(bool show)
  168. {
  169. QList<QDockWidget*> widgets = findChildren<QDockWidget*>();
  170. if (show) {
  171. for (QDockWidget* widget: widgets) {
  172. QWidget* old = widget->titleBarWidget();
  173. widget->setTitleBarWidget(nullptr);
  174. if (old != nullptr)
  175. delete old;
  176. }
  177. } else {
  178. for (QDockWidget* widget: widgets) {
  179. QWidget* old = widget->titleBarWidget();
  180. widget->setTitleBarWidget(new QWidget());
  181. if (old != nullptr)
  182. delete old;
  183. }
  184. }
  185. }
  186. void GMainWindow::BootGame(const std::string& filename) {
  187. LOG_INFO(Frontend, "Citra starting...\n");
  188. // Shutdown previous session if the emu thread is still active...
  189. if (emu_thread != nullptr)
  190. ShutdownGame();
  191. // Initialize the core emulation
  192. System::Init(render_window);
  193. // Load the game
  194. if (Loader::ResultStatus::Success != Loader::LoadFile(filename)) {
  195. LOG_CRITICAL(Frontend, "Failed to load ROM!");
  196. System::Shutdown();
  197. return;
  198. }
  199. // Create and start the emulation thread
  200. emu_thread = Common::make_unique<EmuThread>(render_window);
  201. emit EmulationStarting(emu_thread.get());
  202. render_window->moveContext();
  203. emu_thread->start();
  204. // BlockingQueuedConnection is important here, it makes sure we've finished refreshing our views before the CPU continues
  205. connect(emu_thread.get(), SIGNAL(DebugModeEntered()), disasmWidget, SLOT(OnDebugModeEntered()), Qt::BlockingQueuedConnection);
  206. connect(emu_thread.get(), SIGNAL(DebugModeEntered()), registersWidget, SLOT(OnDebugModeEntered()), Qt::BlockingQueuedConnection);
  207. connect(emu_thread.get(), SIGNAL(DebugModeEntered()), callstackWidget, SLOT(OnDebugModeEntered()), Qt::BlockingQueuedConnection);
  208. connect(emu_thread.get(), SIGNAL(DebugModeLeft()), disasmWidget, SLOT(OnDebugModeLeft()), Qt::BlockingQueuedConnection);
  209. connect(emu_thread.get(), SIGNAL(DebugModeLeft()), registersWidget, SLOT(OnDebugModeLeft()), Qt::BlockingQueuedConnection);
  210. connect(emu_thread.get(), SIGNAL(DebugModeLeft()), callstackWidget, SLOT(OnDebugModeLeft()), Qt::BlockingQueuedConnection);
  211. // Update the GUI
  212. registersWidget->OnDebugModeEntered();
  213. callstackWidget->OnDebugModeEntered();
  214. render_window->show();
  215. OnStartGame();
  216. }
  217. void GMainWindow::ShutdownGame() {
  218. emu_thread->RequestStop();
  219. // Release emu threads from any breakpoints
  220. // This belongs after RequestStop() and before wait() because if emulation stops on a GPU
  221. // breakpoint after (or before) RequestStop() is called, the emulation would never be able
  222. // to continue out to the main loop and terminate. Thus wait() would hang forever.
  223. // TODO(bunnei): This function is not thread safe, but it's being used as if it were
  224. Pica::g_debug_context->ClearBreakpoints();
  225. emit EmulationStopping();
  226. // Wait for emulation thread to complete and delete it
  227. emu_thread->wait();
  228. emu_thread = nullptr;
  229. // Update the GUI
  230. ui.action_Start->setEnabled(false);
  231. ui.action_Start->setText(tr("Start"));
  232. ui.action_Pause->setEnabled(false);
  233. ui.action_Stop->setEnabled(false);
  234. render_window->hide();
  235. }
  236. void GMainWindow::StoreRecentFile(const QString& filename)
  237. {
  238. QSettings settings;
  239. QStringList recent_files = settings.value("recentFiles").toStringList();
  240. recent_files.prepend(filename);
  241. recent_files.removeDuplicates();
  242. while (recent_files.size() > max_recent_files_item) {
  243. recent_files.removeLast();
  244. }
  245. settings.setValue("recentFiles", recent_files);
  246. UpdateRecentFiles();
  247. }
  248. void GMainWindow::UpdateRecentFiles() {
  249. QSettings settings;
  250. QStringList recent_files = settings.value("recentFiles").toStringList();
  251. unsigned int num_recent_files = std::min(recent_files.size(), static_cast<int>(max_recent_files_item));
  252. for (unsigned int i = 0; i < num_recent_files; i++) {
  253. QString text = QString("&%1. %2").arg(i + 1).arg(QFileInfo(recent_files[i]).fileName());
  254. actions_recent_files[i]->setText(text);
  255. actions_recent_files[i]->setData(recent_files[i]);
  256. actions_recent_files[i]->setToolTip(recent_files[i]);
  257. actions_recent_files[i]->setVisible(true);
  258. }
  259. for (int j = num_recent_files; j < max_recent_files_item; ++j) {
  260. actions_recent_files[j]->setVisible(false);
  261. }
  262. // Grey out the recent files menu if the list is empty
  263. if (num_recent_files == 0) {
  264. ui.menu_recent_files->setEnabled(false);
  265. } else {
  266. ui.menu_recent_files->setEnabled(true);
  267. }
  268. }
  269. void GMainWindow::OnMenuLoadFile() {
  270. QSettings settings;
  271. QString rom_path = settings.value("romsPath", QString()).toString();
  272. QString filename = QFileDialog::getOpenFileName(this, tr("Load File"), rom_path, tr("3DS executable (*.3ds *.3dsx *.elf *.axf *.cci *.cxi)"));
  273. if (filename.size()) {
  274. settings.setValue("romsPath", QFileInfo(filename).path());
  275. StoreRecentFile(filename);
  276. BootGame(filename.toLatin1().data());
  277. }
  278. }
  279. void GMainWindow::OnMenuLoadSymbolMap() {
  280. QSettings settings;
  281. QString symbol_path = settings.value("symbolsPath", QString()).toString();
  282. QString filename = QFileDialog::getOpenFileName(this, tr("Load Symbol Map"), symbol_path, tr("Symbol map (*)"));
  283. if (filename.size()) {
  284. settings.setValue("symbolsPath", QFileInfo(filename).path());
  285. LoadSymbolMap(filename.toLatin1().data());
  286. }
  287. }
  288. void GMainWindow::OnMenuRecentFile() {
  289. QAction* action = qobject_cast<QAction*>(sender());
  290. assert(action);
  291. QString filename = action->data().toString();
  292. QFileInfo file_info(filename);
  293. if (file_info.exists()) {
  294. BootGame(filename.toLatin1().data());
  295. StoreRecentFile(filename); // Put the filename on top of the list
  296. } else {
  297. // Display an error message and remove the file from the list.
  298. QMessageBox::information(this, tr("File not found"), tr("File \"%1\" not found").arg(filename));
  299. QSettings settings;
  300. QStringList recent_files = settings.value("recentFiles").toStringList();
  301. recent_files.removeOne(filename);
  302. settings.setValue("recentFiles", recent_files);
  303. UpdateRecentFiles();
  304. }
  305. }
  306. void GMainWindow::OnStartGame() {
  307. emu_thread->SetRunning(true);
  308. ui.action_Start->setEnabled(false);
  309. ui.action_Start->setText(tr("Continue"));
  310. ui.action_Pause->setEnabled(true);
  311. ui.action_Stop->setEnabled(true);
  312. }
  313. void GMainWindow::OnPauseGame() {
  314. emu_thread->SetRunning(false);
  315. ui.action_Start->setEnabled(true);
  316. ui.action_Pause->setEnabled(false);
  317. ui.action_Stop->setEnabled(true);
  318. }
  319. void GMainWindow::OnStopGame() {
  320. ShutdownGame();
  321. }
  322. void GMainWindow::OnOpenHotkeysDialog() {
  323. GHotkeysDialog dialog(this);
  324. dialog.exec();
  325. }
  326. void GMainWindow::SetHardwareRendererEnabled(bool enabled) {
  327. VideoCore::g_hw_renderer_enabled = enabled;
  328. }
  329. void GMainWindow::SetShaderJITEnabled(bool enabled) {
  330. VideoCore::g_shader_jit_enabled = enabled;
  331. }
  332. void GMainWindow::ToggleWindowMode() {
  333. if (ui.action_Single_Window_Mode->isChecked()) {
  334. // Render in the main window...
  335. render_window->BackupGeometry();
  336. ui.horizontalLayout->addWidget(render_window);
  337. render_window->setVisible(true);
  338. render_window->setFocusPolicy(Qt::ClickFocus);
  339. render_window->setFocus();
  340. } else {
  341. // Render in a separate window...
  342. ui.horizontalLayout->removeWidget(render_window);
  343. render_window->setParent(nullptr);
  344. render_window->setVisible(true);
  345. render_window->RestoreGeometry();
  346. render_window->setFocusPolicy(Qt::NoFocus);
  347. }
  348. }
  349. void GMainWindow::OnConfigure() {
  350. //GControllerConfigDialog* dialog = new GControllerConfigDialog(controller_ports, this);
  351. }
  352. void GMainWindow::closeEvent(QCloseEvent* event) {
  353. // Save window layout
  354. QSettings settings(QSettings::IniFormat, QSettings::UserScope, "Citra team", "Citra");
  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.setValue("singleWindowMode", ui.action_Single_Window_Mode->isChecked());
  361. settings.setValue("displayTitleBars", ui.actionDisplay_widget_title_bars->isChecked());
  362. settings.setValue("firstStart", false);
  363. SaveHotkeys(settings);
  364. // Shutdown session if the emu thread is active...
  365. if (emu_thread != nullptr)
  366. ShutdownGame();
  367. render_window->close();
  368. QWidget::closeEvent(event);
  369. }
  370. #ifdef main
  371. #undef main
  372. #endif
  373. int main(int argc, char* argv[]) {
  374. Log::Filter log_filter(Log::Level::Info);
  375. Log::SetFilter(&log_filter);
  376. MicroProfileOnThreadCreate("Frontend");
  377. SCOPE_EXIT({
  378. MicroProfileShutdown();
  379. });
  380. // Init settings params
  381. QSettings::setDefaultFormat(QSettings::IniFormat);
  382. QCoreApplication::setOrganizationName("Citra team");
  383. QCoreApplication::setApplicationName("Citra");
  384. QApplication::setAttribute(Qt::AA_X11InitThreads);
  385. QApplication app(argc, argv);
  386. GMainWindow main_window;
  387. // After settings have been loaded by GMainWindow, apply the filter
  388. log_filter.ParseFilterString(Settings::values.log_filter);
  389. main_window.show();
  390. return app.exec();
  391. }