bootmanager.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include <QHBoxLayout>
  2. #include <QKeyEvent>
  3. #include <QApplication>
  4. #include "common/common.h"
  5. #include "bootmanager.hxx"
  6. #include "core/core.h"
  7. #include "core/loader/loader.h"
  8. #include "core/hw/hw.h"
  9. #include "video_core/video_core.h"
  10. #include "version.h"
  11. #define APP_NAME "citra"
  12. #define APP_VERSION "0.1-" VERSION
  13. #define APP_TITLE APP_NAME " " APP_VERSION
  14. #define COPYRIGHT "Copyright (C) 2013-2014 Citra Team"
  15. EmuThread::EmuThread(GRenderWindow* render_window) :
  16. filename(""), exec_cpu_step(false), cpu_running(false),
  17. stop_run(false), render_window(render_window)
  18. {
  19. }
  20. void EmuThread::SetFilename(std::string filename)
  21. {
  22. this->filename = filename;
  23. }
  24. void EmuThread::run()
  25. {
  26. stop_run = false;
  27. while (!stop_run)
  28. {
  29. for (int tight_loop = 0; tight_loop < 10000; ++tight_loop)
  30. {
  31. if (cpu_running || exec_cpu_step)
  32. {
  33. if (exec_cpu_step)
  34. exec_cpu_step = false;
  35. Core::SingleStep();
  36. if (!cpu_running) {
  37. emit CPUStepped();
  38. yieldCurrentThread();
  39. }
  40. }
  41. }
  42. }
  43. render_window->moveContext();
  44. Core::Stop();
  45. }
  46. void EmuThread::Stop()
  47. {
  48. if (!isRunning())
  49. {
  50. INFO_LOG(MASTER_LOG, "EmuThread::Stop called while emu thread wasn't running, returning...");
  51. return;
  52. }
  53. stop_run = true;
  54. //core::g_state = core::SYS_DIE;
  55. wait(500);
  56. if (isRunning())
  57. {
  58. WARN_LOG(MASTER_LOG, "EmuThread still running, terminating...");
  59. quit();
  60. wait(1000);
  61. if (isRunning())
  62. {
  63. WARN_LOG(MASTER_LOG, "EmuThread STILL running, something is wrong here...");
  64. terminate();
  65. }
  66. }
  67. INFO_LOG(MASTER_LOG, "EmuThread stopped");
  68. }
  69. // This class overrides paintEvent and resizeEvent to prevent the GUI thread from stealing GL context.
  70. // The corresponding functionality is handled in EmuThread instead
  71. class GGLWidgetInternal : public QGLWidget
  72. {
  73. public:
  74. GGLWidgetInternal(QGLFormat fmt, GRenderWindow* parent) : QGLWidget(fmt, parent)
  75. {
  76. parent_ = parent;
  77. }
  78. void paintEvent(QPaintEvent* ev)
  79. {
  80. }
  81. void resizeEvent(QResizeEvent* ev) {
  82. parent_->SetClientAreaWidth(size().width());
  83. parent_->SetClientAreaHeight(size().height());
  84. }
  85. private:
  86. GRenderWindow* parent_;
  87. };
  88. EmuThread& GRenderWindow::GetEmuThread()
  89. {
  90. return emu_thread;
  91. }
  92. static const std::pair<int, HID_User::PadState> default_key_map[] = {
  93. { Qt::Key_A, HID_User::PAD_A },
  94. { Qt::Key_B, HID_User::PAD_B },
  95. { Qt::Key_Backslash, HID_User::PAD_SELECT },
  96. { Qt::Key_Enter, HID_User::PAD_START },
  97. { Qt::Key_Right, HID_User::PAD_RIGHT },
  98. { Qt::Key_Left, HID_User::PAD_LEFT },
  99. { Qt::Key_Up, HID_User::PAD_UP },
  100. { Qt::Key_Down, HID_User::PAD_DOWN },
  101. { Qt::Key_R, HID_User::PAD_R },
  102. { Qt::Key_L, HID_User::PAD_L },
  103. { Qt::Key_X, HID_User::PAD_X },
  104. { Qt::Key_Y, HID_User::PAD_Y },
  105. { Qt::Key_H, HID_User::PAD_CIRCLE_RIGHT },
  106. { Qt::Key_F, HID_User::PAD_CIRCLE_LEFT },
  107. { Qt::Key_T, HID_User::PAD_CIRCLE_UP },
  108. { Qt::Key_G, HID_User::PAD_CIRCLE_DOWN },
  109. };
  110. GRenderWindow::GRenderWindow(QWidget* parent) : QWidget(parent), emu_thread(this)
  111. {
  112. // Register a new ID for the default keyboard
  113. keyboard_id = KeyMap::NewDeviceId();
  114. // Set default key mappings for keyboard
  115. for (auto mapping : default_key_map) {
  116. KeyMap::SetKeyMapping({mapping.first, keyboard_id}, mapping.second);
  117. }
  118. // TODO: One of these flags might be interesting: WA_OpaquePaintEvent, WA_NoBackground, WA_DontShowOnScreen, WA_DeleteOnClose
  119. QGLFormat fmt;
  120. fmt.setProfile(QGLFormat::CoreProfile);
  121. fmt.setVersion(3,2);
  122. fmt.setSampleBuffers(true);
  123. fmt.setSamples(4);
  124. child = new GGLWidgetInternal(fmt, this);
  125. QBoxLayout* layout = new QHBoxLayout(this);
  126. resize(VideoCore::kScreenTopWidth, VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight);
  127. layout->addWidget(child);
  128. layout->setMargin(0);
  129. setLayout(layout);
  130. QObject::connect(&emu_thread, SIGNAL(started()), this, SLOT(moveContext()));
  131. BackupGeometry();
  132. }
  133. void GRenderWindow::moveContext()
  134. {
  135. DoneCurrent();
  136. // We need to move GL context to the swapping thread in Qt5
  137. #if QT_VERSION > QT_VERSION_CHECK(5, 0, 0)
  138. // If the thread started running, move the GL Context to the new thread. Otherwise, move it back.
  139. child->context()->moveToThread((QThread::currentThread() == qApp->thread()) ? &emu_thread : qApp->thread());
  140. #endif
  141. }
  142. GRenderWindow::~GRenderWindow()
  143. {
  144. if (emu_thread.isRunning())
  145. emu_thread.Stop();
  146. }
  147. void GRenderWindow::SwapBuffers()
  148. {
  149. // MakeCurrent is already called in renderer_opengl
  150. child->swapBuffers();
  151. }
  152. void GRenderWindow::closeEvent(QCloseEvent* event)
  153. {
  154. if (emu_thread.isRunning())
  155. emu_thread.Stop();
  156. QWidget::closeEvent(event);
  157. }
  158. void GRenderWindow::MakeCurrent()
  159. {
  160. child->makeCurrent();
  161. }
  162. void GRenderWindow::DoneCurrent()
  163. {
  164. child->doneCurrent();
  165. }
  166. void GRenderWindow::PollEvents() {
  167. // TODO(ShizZy): Does this belong here? This is a reasonable place to update the window title
  168. // from the main thread, but this should probably be in an event handler...
  169. /*
  170. static char title[128];
  171. sprintf(title, "%s (FPS: %02.02f)", window_title_.c_str(),
  172. video_core::g_renderer->current_fps());
  173. setWindowTitle(title);
  174. */
  175. }
  176. void GRenderWindow::BackupGeometry()
  177. {
  178. geometry = ((QGLWidget*)this)->saveGeometry();
  179. }
  180. void GRenderWindow::RestoreGeometry()
  181. {
  182. // We don't want to back up the geometry here (obviously)
  183. QWidget::restoreGeometry(geometry);
  184. }
  185. void GRenderWindow::restoreGeometry(const QByteArray& geometry)
  186. {
  187. // Make sure users of this class don't need to deal with backing up the geometry themselves
  188. QWidget::restoreGeometry(geometry);
  189. BackupGeometry();
  190. }
  191. QByteArray GRenderWindow::saveGeometry()
  192. {
  193. // If we are a top-level widget, store the current geometry
  194. // otherwise, store the last backup
  195. if (parent() == NULL)
  196. return ((QGLWidget*)this)->saveGeometry();
  197. else
  198. return geometry;
  199. }
  200. void GRenderWindow::keyPressEvent(QKeyEvent* event)
  201. {
  202. EmuWindow::KeyPressed({event->key(), keyboard_id});
  203. HID_User::PadUpdateComplete();
  204. }
  205. void GRenderWindow::keyReleaseEvent(QKeyEvent* event)
  206. {
  207. EmuWindow::KeyReleased({event->key(), keyboard_id});
  208. HID_User::PadUpdateComplete();
  209. }