main.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #include <QtGui>
  2. #include <QDesktopWidget>
  3. #include <QFileDialog>
  4. #include "qhexedit.h"
  5. #include "main.hxx"
  6. #include "common/common.h"
  7. #include "common/platform.h"
  8. #include "common/log_manager.h"
  9. #if EMU_PLATFORM == PLATFORM_LINUX
  10. #include <unistd.h>
  11. #endif
  12. #include "bootmanager.hxx"
  13. #include "hotkeys.hxx"
  14. //debugger
  15. #include "debugger/disassembler.hxx"
  16. #include "debugger/registers.hxx"
  17. #include "debugger/callstack.hxx"
  18. #include "debugger/ramview.hxx"
  19. #include "debugger/graphics.hxx"
  20. #include "debugger/graphics_breakpoints.hxx"
  21. #include "debugger/graphics_cmdlists.hxx"
  22. #include "debugger/graphics_framebuffer.hxx"
  23. #include "core/settings.h"
  24. #include "core/system.h"
  25. #include "core/core.h"
  26. #include "core/loader/loader.h"
  27. #include "core/arm/disassembler/load_symbol_map.h"
  28. #include "citra_qt/config.h"
  29. #include "version.h"
  30. GMainWindow::GMainWindow()
  31. {
  32. LogManager::Init();
  33. Pica::g_debug_context = Pica::DebugContext::Construct();
  34. Config config;
  35. if (!Settings::values.enable_log)
  36. LogManager::Shutdown();
  37. ui.setupUi(this);
  38. statusBar()->hide();
  39. render_window = new GRenderWindow;
  40. render_window->hide();
  41. disasmWidget = new DisassemblerWidget(this, render_window->GetEmuThread());
  42. addDockWidget(Qt::BottomDockWidgetArea, disasmWidget);
  43. disasmWidget->hide();
  44. registersWidget = new RegistersWidget(this);
  45. addDockWidget(Qt::RightDockWidgetArea, registersWidget);
  46. registersWidget->hide();
  47. callstackWidget = new CallstackWidget(this);
  48. addDockWidget(Qt::RightDockWidgetArea, callstackWidget);
  49. callstackWidget->hide();
  50. graphicsWidget = new GPUCommandStreamWidget(this);
  51. addDockWidget(Qt::RightDockWidgetArea, graphicsWidget);
  52. graphicsWidget ->hide();
  53. graphicsCommandsWidget = new GPUCommandListWidget(this);
  54. addDockWidget(Qt::RightDockWidgetArea, graphicsCommandsWidget);
  55. graphicsCommandsWidget->hide();
  56. auto graphicsBreakpointsWidget = new GraphicsBreakPointsWidget(Pica::g_debug_context, this);
  57. addDockWidget(Qt::RightDockWidgetArea, graphicsBreakpointsWidget);
  58. graphicsBreakpointsWidget->hide();
  59. auto graphicsFramebufferWidget = new GraphicsFramebufferWidget(Pica::g_debug_context, this);
  60. addDockWidget(Qt::RightDockWidgetArea, graphicsFramebufferWidget);
  61. graphicsFramebufferWidget->hide();
  62. QMenu* debug_menu = ui.menu_View->addMenu(tr("Debugging"));
  63. debug_menu->addAction(disasmWidget->toggleViewAction());
  64. debug_menu->addAction(registersWidget->toggleViewAction());
  65. debug_menu->addAction(callstackWidget->toggleViewAction());
  66. debug_menu->addAction(graphicsWidget->toggleViewAction());
  67. debug_menu->addAction(graphicsCommandsWidget->toggleViewAction());
  68. debug_menu->addAction(graphicsBreakpointsWidget->toggleViewAction());
  69. debug_menu->addAction(graphicsFramebufferWidget->toggleViewAction());
  70. // Set default UI state
  71. // geometry: 55% of the window contents are in the upper screen half, 45% in the lower half
  72. QDesktopWidget* desktop = ((QApplication*)QApplication::instance())->desktop();
  73. QRect screenRect = desktop->screenGeometry(this);
  74. int x, y, w, h;
  75. w = screenRect.width() * 2 / 3;
  76. h = screenRect.height() / 2;
  77. x = (screenRect.x() + screenRect.width()) / 2 - w / 2;
  78. y = (screenRect.y() + screenRect.height()) / 2 - h * 55 / 100;
  79. setGeometry(x, y, w, h);
  80. // Restore UI state
  81. QSettings settings(QSettings::IniFormat, QSettings::UserScope, "Citra team", "Citra");
  82. restoreGeometry(settings.value("geometry").toByteArray());
  83. restoreState(settings.value("state").toByteArray());
  84. render_window->restoreGeometry(settings.value("geometryRenderWindow").toByteArray());
  85. ui.action_Popout_Window_Mode->setChecked(settings.value("popoutWindowMode", true).toBool());
  86. ToggleWindowMode();
  87. // Setup connections
  88. connect(ui.action_Load_File, SIGNAL(triggered()), this, SLOT(OnMenuLoadFile()));
  89. connect(ui.action_Load_Symbol_Map, SIGNAL(triggered()), this, SLOT(OnMenuLoadSymbolMap()));
  90. connect(ui.action_Start, SIGNAL(triggered()), this, SLOT(OnStartGame()));
  91. connect(ui.action_Pause, SIGNAL(triggered()), this, SLOT(OnPauseGame()));
  92. connect(ui.action_Stop, SIGNAL(triggered()), this, SLOT(OnStopGame()));
  93. connect(ui.action_Popout_Window_Mode, SIGNAL(triggered(bool)), this, SLOT(ToggleWindowMode()));
  94. connect(ui.action_Hotkeys, SIGNAL(triggered()), this, SLOT(OnOpenHotkeysDialog()));
  95. // BlockingQueuedConnection is important here, it makes sure we've finished refreshing our views before the CPU continues
  96. connect(&render_window->GetEmuThread(), SIGNAL(CPUStepped()), disasmWidget, SLOT(OnCPUStepped()), Qt::BlockingQueuedConnection);
  97. connect(&render_window->GetEmuThread(), SIGNAL(CPUStepped()), registersWidget, SLOT(OnCPUStepped()), Qt::BlockingQueuedConnection);
  98. connect(&render_window->GetEmuThread(), SIGNAL(CPUStepped()), callstackWidget, SLOT(OnCPUStepped()), Qt::BlockingQueuedConnection);
  99. // Setup hotkeys
  100. RegisterHotkey("Main Window", "Load File", QKeySequence::Open);
  101. RegisterHotkey("Main Window", "Start Emulation");
  102. LoadHotkeys(settings);
  103. connect(GetHotkey("Main Window", "Load File", this), SIGNAL(activated()), this, SLOT(OnMenuLoadFile()));
  104. connect(GetHotkey("Main Window", "Start Emulation", this), SIGNAL(activated()), this, SLOT(OnStartGame()));
  105. std::string window_title = Common::StringFromFormat("Citra | %s-%s", Common::g_scm_branch, Common::g_scm_desc);
  106. setWindowTitle(window_title.c_str());
  107. show();
  108. QStringList args = QApplication::arguments();
  109. if (args.length() >= 2) {
  110. BootGame(args[1].toStdString());
  111. }
  112. }
  113. GMainWindow::~GMainWindow()
  114. {
  115. // will get automatically deleted otherwise
  116. if (render_window->parent() == nullptr)
  117. delete render_window;
  118. Pica::g_debug_context.reset();
  119. }
  120. void GMainWindow::BootGame(std::string filename)
  121. {
  122. NOTICE_LOG(MASTER_LOG, "Citra starting...\n");
  123. System::Init(render_window);
  124. if (Core::Init()) {
  125. ERROR_LOG(MASTER_LOG, "Core initialization failed, exiting...");
  126. Core::Stop();
  127. exit(1);
  128. }
  129. // Load a game or die...
  130. if (Loader::ResultStatus::Success != Loader::LoadFile(filename)) {
  131. ERROR_LOG(BOOT, "Failed to load ROM!");
  132. }
  133. disasmWidget->Init();
  134. registersWidget->OnCPUStepped();
  135. callstackWidget->OnCPUStepped();
  136. render_window->GetEmuThread().SetFilename(filename);
  137. render_window->GetEmuThread().start();
  138. render_window->show();
  139. OnStartGame();
  140. }
  141. void GMainWindow::OnMenuLoadFile()
  142. {
  143. QString filename = QFileDialog::getOpenFileName(this, tr("Load file"), QString(), tr("3DS executable (*.3dsx *.elf *.axf *.bin *.cci *.cxi)"));
  144. if (filename.size())
  145. BootGame(filename.toLatin1().data());
  146. }
  147. void GMainWindow::OnMenuLoadSymbolMap() {
  148. QString filename = QFileDialog::getOpenFileName(this, tr("Load symbol map"), QString(), tr("Symbol map (*)"));
  149. if (filename.size())
  150. LoadSymbolMap(filename.toLatin1().data());
  151. }
  152. void GMainWindow::OnStartGame()
  153. {
  154. render_window->GetEmuThread().SetCpuRunning(true);
  155. ui.action_Start->setEnabled(false);
  156. ui.action_Pause->setEnabled(true);
  157. ui.action_Stop->setEnabled(true);
  158. }
  159. void GMainWindow::OnPauseGame()
  160. {
  161. render_window->GetEmuThread().SetCpuRunning(false);
  162. ui.action_Start->setEnabled(true);
  163. ui.action_Pause->setEnabled(false);
  164. ui.action_Stop->setEnabled(true);
  165. }
  166. void GMainWindow::OnStopGame()
  167. {
  168. render_window->GetEmuThread().SetCpuRunning(false);
  169. // TODO: Shutdown core
  170. ui.action_Start->setEnabled(true);
  171. ui.action_Pause->setEnabled(false);
  172. ui.action_Stop->setEnabled(false);
  173. }
  174. void GMainWindow::OnOpenHotkeysDialog()
  175. {
  176. GHotkeysDialog dialog(this);
  177. dialog.exec();
  178. }
  179. void GMainWindow::ToggleWindowMode()
  180. {
  181. bool enable = ui.action_Popout_Window_Mode->isChecked();
  182. if (enable && render_window->parent() != nullptr)
  183. {
  184. ui.horizontalLayout->removeWidget(render_window);
  185. render_window->setParent(nullptr);
  186. render_window->setVisible(true);
  187. render_window->RestoreGeometry();
  188. }
  189. else if (!enable && render_window->parent() == nullptr)
  190. {
  191. render_window->BackupGeometry();
  192. ui.horizontalLayout->addWidget(render_window);
  193. render_window->setVisible(true);
  194. }
  195. }
  196. void GMainWindow::OnConfigure()
  197. {
  198. //GControllerConfigDialog* dialog = new GControllerConfigDialog(controller_ports, this);
  199. }
  200. void GMainWindow::closeEvent(QCloseEvent* event)
  201. {
  202. // Save window layout
  203. QSettings settings(QSettings::IniFormat, QSettings::UserScope, "Citra team", "Citra");
  204. settings.setValue("geometry", saveGeometry());
  205. settings.setValue("state", saveState());
  206. settings.setValue("geometryRenderWindow", render_window->saveGeometry());
  207. settings.setValue("popoutWindowMode", ui.action_Popout_Window_Mode->isChecked());
  208. settings.setValue("firstStart", false);
  209. SaveHotkeys(settings);
  210. render_window->close();
  211. QWidget::closeEvent(event);
  212. }
  213. #ifdef main
  214. #undef main
  215. #endif
  216. int __cdecl main(int argc, char* argv[])
  217. {
  218. QApplication::setAttribute(Qt::AA_X11InitThreads);
  219. QApplication app(argc, argv);
  220. GMainWindow main_window;
  221. main_window.show();
  222. return app.exec();
  223. }