bootmanager.cpp 6.4 KB

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